xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision 474ba6fa3b81b13b183a47f8591e83fa8ba499f4)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
365fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
375fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
385fe58019SAttilio Rao  * are met:
395fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
405fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
415fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
425fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
435fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
445fe58019SAttilio Rao  *
455fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
465fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
475fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
485fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
495fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
525fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
535fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
545fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
555fe58019SAttilio Rao  * SUCH DAMAGE.
565fe58019SAttilio Rao  */
575fe58019SAttilio Rao 
585fe58019SAttilio Rao #include <sys/cdefs.h>
595fe58019SAttilio Rao __FBSDID("$FreeBSD$");
605fe58019SAttilio Rao 
61cf169498SAlan Somers #include <sys/param.h>
625fe58019SAttilio Rao #include <sys/module.h>
635fe58019SAttilio Rao #include <sys/systm.h>
645fe58019SAttilio Rao #include <sys/errno.h>
655fe58019SAttilio Rao #include <sys/kernel.h>
665fe58019SAttilio Rao #include <sys/conf.h>
675fe58019SAttilio Rao #include <sys/uio.h>
685fe58019SAttilio Rao #include <sys/malloc.h>
695fe58019SAttilio Rao #include <sys/queue.h>
705fe58019SAttilio Rao #include <sys/lock.h>
7189f6b863SAttilio Rao #include <sys/rwlock.h>
725fe58019SAttilio Rao #include <sys/sx.h>
735fe58019SAttilio Rao #include <sys/proc.h>
745fe58019SAttilio Rao #include <sys/mount.h>
755fe58019SAttilio Rao #include <sys/vnode.h>
765fe58019SAttilio Rao #include <sys/namei.h>
7704660064SFedor Uporov #include <sys/extattr.h>
785fe58019SAttilio Rao #include <sys/stat.h>
795fe58019SAttilio Rao #include <sys/unistd.h>
805fe58019SAttilio Rao #include <sys/filedesc.h>
815fe58019SAttilio Rao #include <sys/file.h>
825fe58019SAttilio Rao #include <sys/fcntl.h>
835fe58019SAttilio Rao #include <sys/dirent.h>
845fe58019SAttilio Rao #include <sys/bio.h>
855fe58019SAttilio Rao #include <sys/buf.h>
865fe58019SAttilio Rao #include <sys/sysctl.h>
87ca148cdaSGleb Smirnoff #include <sys/vmmeter.h>
885fe58019SAttilio Rao 
895fe58019SAttilio Rao #include <vm/vm.h>
905fe58019SAttilio Rao #include <vm/vm_extern.h>
915fe58019SAttilio Rao #include <vm/pmap.h>
925fe58019SAttilio Rao #include <vm/vm_map.h>
935fe58019SAttilio Rao #include <vm/vm_page.h>
945fe58019SAttilio Rao #include <vm/vm_param.h>
955fe58019SAttilio Rao #include <vm/vm_object.h>
965fe58019SAttilio Rao #include <vm/vm_pager.h>
975fe58019SAttilio Rao #include <vm/vnode_pager.h>
985fe58019SAttilio Rao #include <vm/vm_object.h>
995fe58019SAttilio Rao 
1005fe58019SAttilio Rao #include "fuse.h"
1015fe58019SAttilio Rao #include "fuse_file.h"
1025fe58019SAttilio Rao #include "fuse_internal.h"
1035fe58019SAttilio Rao #include "fuse_ipc.h"
1045fe58019SAttilio Rao #include "fuse_node.h"
1055fe58019SAttilio Rao #include "fuse_io.h"
1065fe58019SAttilio Rao 
1075fe58019SAttilio Rao #include <sys/priv.h>
1085fe58019SAttilio Rao 
109fd2749f2SAlan Somers /* Maximum number of hardlinks to a single FUSE file */
110fd2749f2SAlan Somers #define FUSE_LINK_MAX                      UINT32_MAX
111fd2749f2SAlan Somers 
112419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs);
113cf169498SAlan Somers /*
114cf169498SAlan Somers  * Fuse trace probe:
115cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
116cf169498SAlan Somers  * arg1: Textual message
117cf169498SAlan Somers  */
118419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
1195fe58019SAttilio Rao 
1205fe58019SAttilio Rao /* vnode ops */
1215fe58019SAttilio Rao static vop_access_t fuse_vnop_access;
122f067b609SAlan Somers static vop_advlock_t fuse_vnop_advlock;
123f9b0e30bSAlan Somers static vop_close_t fuse_fifo_close;
1245fe58019SAttilio Rao static vop_close_t fuse_vnop_close;
1255fe58019SAttilio Rao static vop_create_t fuse_vnop_create;
12604660064SFedor Uporov static vop_deleteextattr_t fuse_vnop_deleteextattr;
127915012e0SAlan Somers static vop_fdatasync_t fuse_vnop_fdatasync;
1285fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync;
1295fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr;
13004660064SFedor Uporov static vop_getextattr_t fuse_vnop_getextattr;
1315fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive;
1325fe58019SAttilio Rao static vop_link_t fuse_vnop_link;
13304660064SFedor Uporov static vop_listextattr_t fuse_vnop_listextattr;
1345fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup;
1355fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir;
1365fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod;
1375fe58019SAttilio Rao static vop_open_t fuse_vnop_open;
138746c92e0SJohn Baldwin static vop_pathconf_t fuse_vnop_pathconf;
1395fe58019SAttilio Rao static vop_read_t fuse_vnop_read;
1405fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir;
1415fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink;
1425fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim;
1435fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove;
1445fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename;
1455fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir;
1465fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr;
14704660064SFedor Uporov static vop_setextattr_t fuse_vnop_setextattr;
1485fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy;
1495fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink;
1505fe58019SAttilio Rao static vop_write_t fuse_vnop_write;
1515fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages;
1525fe58019SAttilio Rao static vop_putpages_t fuse_vnop_putpages;
1535fe58019SAttilio Rao static vop_print_t fuse_vnop_print;
1545fe58019SAttilio Rao 
155f9b0e30bSAlan Somers struct vop_vector fuse_fifoops = {
156f9b0e30bSAlan Somers 	.vop_default =		&fifo_specops,
157f9b0e30bSAlan Somers 	.vop_access =		fuse_vnop_access,
158f9b0e30bSAlan Somers 	.vop_close =		fuse_fifo_close,
159f9b0e30bSAlan Somers 	.vop_fsync =		fuse_vnop_fsync,
160f9b0e30bSAlan Somers 	.vop_getattr =		fuse_vnop_getattr,
161f9b0e30bSAlan Somers 	.vop_inactive =		fuse_vnop_inactive,
162f9b0e30bSAlan Somers 	.vop_pathconf =		fuse_vnop_pathconf,
163f9b0e30bSAlan Somers 	.vop_print =		fuse_vnop_print,
164f9b0e30bSAlan Somers 	.vop_read =		VOP_PANIC,
165f9b0e30bSAlan Somers 	.vop_reclaim =		fuse_vnop_reclaim,
166f9b0e30bSAlan Somers 	.vop_setattr =		fuse_vnop_setattr,
167f9b0e30bSAlan Somers 	.vop_write =		VOP_PANIC,
168f9b0e30bSAlan Somers };
169f9b0e30bSAlan Somers 
1705fe58019SAttilio Rao struct vop_vector fuse_vnops = {
1715fe58019SAttilio Rao 	.vop_default = &default_vnodeops,
1725fe58019SAttilio Rao 	.vop_access = fuse_vnop_access,
173f067b609SAlan Somers 	.vop_advlock = fuse_vnop_advlock,
1745fe58019SAttilio Rao 	.vop_close = fuse_vnop_close,
1755fe58019SAttilio Rao 	.vop_create = fuse_vnop_create,
17604660064SFedor Uporov 	.vop_deleteextattr = fuse_vnop_deleteextattr,
1775fe58019SAttilio Rao 	.vop_fsync = fuse_vnop_fsync,
178915012e0SAlan Somers 	.vop_fdatasync = fuse_vnop_fdatasync,
1795fe58019SAttilio Rao 	.vop_getattr = fuse_vnop_getattr,
18004660064SFedor Uporov 	.vop_getextattr = fuse_vnop_getextattr,
1815fe58019SAttilio Rao 	.vop_inactive = fuse_vnop_inactive,
1825fe58019SAttilio Rao 	.vop_link = fuse_vnop_link,
18304660064SFedor Uporov 	.vop_listextattr = fuse_vnop_listextattr,
1845fe58019SAttilio Rao 	.vop_lookup = fuse_vnop_lookup,
1855fe58019SAttilio Rao 	.vop_mkdir = fuse_vnop_mkdir,
1865fe58019SAttilio Rao 	.vop_mknod = fuse_vnop_mknod,
1875fe58019SAttilio Rao 	.vop_open = fuse_vnop_open,
188746c92e0SJohn Baldwin 	.vop_pathconf = fuse_vnop_pathconf,
1895fe58019SAttilio Rao 	.vop_read = fuse_vnop_read,
1905fe58019SAttilio Rao 	.vop_readdir = fuse_vnop_readdir,
1915fe58019SAttilio Rao 	.vop_readlink = fuse_vnop_readlink,
1925fe58019SAttilio Rao 	.vop_reclaim = fuse_vnop_reclaim,
1935fe58019SAttilio Rao 	.vop_remove = fuse_vnop_remove,
1945fe58019SAttilio Rao 	.vop_rename = fuse_vnop_rename,
1955fe58019SAttilio Rao 	.vop_rmdir = fuse_vnop_rmdir,
1965fe58019SAttilio Rao 	.vop_setattr = fuse_vnop_setattr,
19704660064SFedor Uporov 	.vop_setextattr = fuse_vnop_setextattr,
1985fe58019SAttilio Rao 	.vop_strategy = fuse_vnop_strategy,
1995fe58019SAttilio Rao 	.vop_symlink = fuse_vnop_symlink,
2005fe58019SAttilio Rao 	.vop_write = fuse_vnop_write,
2015fe58019SAttilio Rao 	.vop_getpages = fuse_vnop_getpages,
2025fe58019SAttilio Rao 	.vop_putpages = fuse_vnop_putpages,
2035fe58019SAttilio Rao 	.vop_print = fuse_vnop_print,
2045fe58019SAttilio Rao };
2055fe58019SAttilio Rao 
2065fe58019SAttilio Rao static u_long fuse_lookup_cache_hits = 0;
2075fe58019SAttilio Rao 
208123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
209bd4cb2a4SConrad Meyer     &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
2105fe58019SAttilio Rao 
2115fe58019SAttilio Rao static u_long fuse_lookup_cache_misses = 0;
2125fe58019SAttilio Rao 
213123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
214bd4cb2a4SConrad Meyer     &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
2155fe58019SAttilio Rao 
2165fe58019SAttilio Rao /*
2175fe58019SAttilio Rao  * XXX: This feature is highly experimental and can bring to instabilities,
2185fe58019SAttilio Rao  * needs revisiting before to be enabled by default.
2195fe58019SAttilio Rao  */
2205fe58019SAttilio Rao static int fuse_reclaim_revoked = 0;
2215fe58019SAttilio Rao 
222123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
2235fe58019SAttilio Rao     &fuse_reclaim_revoked, 0, "");
2245fe58019SAttilio Rao 
225756a5412SGleb Smirnoff uma_zone_t fuse_pbuf_zone;
2265fe58019SAttilio Rao 
2275fe58019SAttilio Rao #define fuse_vm_page_lock(m)		vm_page_lock((m));
2285fe58019SAttilio Rao #define fuse_vm_page_unlock(m)		vm_page_unlock((m));
2295fe58019SAttilio Rao #define fuse_vm_page_lock_queues()	((void)0)
2305fe58019SAttilio Rao #define fuse_vm_page_unlock_queues()	((void)0)
2315fe58019SAttilio Rao 
232666f8543SAlan Somers /* Check permission for extattr operations, much like extattr_check_cred */
233666f8543SAlan Somers static int
234666f8543SAlan Somers fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
235666f8543SAlan Somers 	struct thread *td, accmode_t accmode)
236666f8543SAlan Somers {
237666f8543SAlan Somers 	struct mount *mp = vnode_mount(vp);
238666f8543SAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
239666f8543SAlan Somers 
240666f8543SAlan Somers 	/*
241666f8543SAlan Somers 	 * Kernel-invoked always succeeds.
242666f8543SAlan Somers 	 */
243666f8543SAlan Somers 	if (cred == NOCRED)
244666f8543SAlan Somers 		return (0);
245666f8543SAlan Somers 
246666f8543SAlan Somers 	/*
247666f8543SAlan Somers 	 * Do not allow privileged processes in jail to directly manipulate
248666f8543SAlan Somers 	 * system attributes.
249666f8543SAlan Somers 	 */
250666f8543SAlan Somers 	switch (ns) {
251666f8543SAlan Somers 	case EXTATTR_NAMESPACE_SYSTEM:
252666f8543SAlan Somers 		if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
253666f8543SAlan Somers 			return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
254666f8543SAlan Somers 		}
255666f8543SAlan Somers 		/* FALLTHROUGH */
256666f8543SAlan Somers 	case EXTATTR_NAMESPACE_USER:
257666f8543SAlan Somers 		return (fuse_internal_access(vp, accmode, td, cred));
258666f8543SAlan Somers 	default:
259666f8543SAlan Somers 		return (EPERM);
260666f8543SAlan Somers 	}
261666f8543SAlan Somers }
262666f8543SAlan Somers 
263f8d4af10SAlan Somers /* Get a filehandle for a directory */
264f8d4af10SAlan Somers static int
265f8d4af10SAlan Somers fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
266f8d4af10SAlan Somers 	struct ucred *cred, pid_t pid)
267f8d4af10SAlan Somers {
2689f10f423SAlan Somers 	if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
269f8d4af10SAlan Somers 		return 0;
2709f10f423SAlan Somers 	return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
2719f10f423SAlan Somers }
2729f10f423SAlan Somers 
2739f10f423SAlan Somers /* Send FUSE_FLUSH for this vnode */
2749f10f423SAlan Somers static int
2759f10f423SAlan Somers fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
2769f10f423SAlan Somers {
2779f10f423SAlan Somers 	struct fuse_flush_in *ffi;
2789f10f423SAlan Somers 	struct fuse_filehandle *fufh;
2799f10f423SAlan Somers 	struct fuse_dispatcher fdi;
2809f10f423SAlan Somers 	struct thread *td = curthread;
2819f10f423SAlan Somers 	struct mount *mp = vnode_mount(vp);
2829f10f423SAlan Somers 	int err;
2839f10f423SAlan Somers 
2849f10f423SAlan Somers 	if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
2859f10f423SAlan Somers 		return 0;
2869f10f423SAlan Somers 
287f067b609SAlan Somers 	err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
2889f10f423SAlan Somers 	if (err)
2899f10f423SAlan Somers 		return err;
2909f10f423SAlan Somers 
2919f10f423SAlan Somers 	fdisp_init(&fdi, sizeof(*ffi));
2929f10f423SAlan Somers 	fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
2939f10f423SAlan Somers 	ffi = fdi.indata;
2949f10f423SAlan Somers 	ffi->fh = fufh->fh_id;
295f067b609SAlan Somers 	/*
296f067b609SAlan Somers 	 * If the file has a POSIX lock then we're supposed to set lock_owner.
297f067b609SAlan Somers 	 * If not, then lock_owner is undefined.  So we may as well always set
298f067b609SAlan Somers 	 * it.
299f067b609SAlan Somers 	 */
300f067b609SAlan Somers 	ffi->lock_owner = td->td_proc->p_pid;
3019f10f423SAlan Somers 
3029f10f423SAlan Somers 	err = fdisp_wait_answ(&fdi);
3039f10f423SAlan Somers 	if (err == ENOSYS) {
3049f10f423SAlan Somers 		fsess_set_notimpl(mp, FUSE_FLUSH);
3059f10f423SAlan Somers 		err = 0;
3069f10f423SAlan Somers 	}
3079f10f423SAlan Somers 	fdisp_destroy(&fdi);
3089f10f423SAlan Somers 	return err;
309f8d4af10SAlan Somers }
310f8d4af10SAlan Somers 
311f9b0e30bSAlan Somers /* Close wrapper for fifos.  */
312f9b0e30bSAlan Somers static int
313f9b0e30bSAlan Somers fuse_fifo_close(struct vop_close_args *ap)
314f9b0e30bSAlan Somers {
315f9b0e30bSAlan Somers 	return (fifo_specops.vop_close(ap));
316f9b0e30bSAlan Somers }
317f9b0e30bSAlan Somers 
3185fe58019SAttilio Rao /*
3195fe58019SAttilio Rao     struct vnop_access_args {
3205fe58019SAttilio Rao 	struct vnode *a_vp;
3215fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T
3225fe58019SAttilio Rao 	accmode_t a_accmode;
3235fe58019SAttilio Rao #else
3245fe58019SAttilio Rao 	int a_mode;
3255fe58019SAttilio Rao #endif
3265fe58019SAttilio Rao 	struct ucred *a_cred;
3275fe58019SAttilio Rao 	struct thread *a_td;
3285fe58019SAttilio Rao     };
3295fe58019SAttilio Rao */
3305fe58019SAttilio Rao static int
3315fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap)
3325fe58019SAttilio Rao {
3335fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
3345fe58019SAttilio Rao 	int accmode = ap->a_accmode;
3355fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
3365fe58019SAttilio Rao 
3375fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
3385fe58019SAttilio Rao 
3395fe58019SAttilio Rao 	int err;
3405fe58019SAttilio Rao 
3415fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
3425fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
3435fe58019SAttilio Rao 			return 0;
3445fe58019SAttilio Rao 		}
3455fe58019SAttilio Rao 		return ENXIO;
3465fe58019SAttilio Rao 	}
3475fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
3485fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
349cc426dd3SMateusz Guzik 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
3505fe58019SAttilio Rao 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
3515fe58019SAttilio Rao 				return 0;
3525fe58019SAttilio Rao 			}
3535fe58019SAttilio Rao 		}
3545fe58019SAttilio Rao 		return EBADF;
3555fe58019SAttilio Rao 	}
3565fe58019SAttilio Rao 	if (vnode_islnk(vp)) {
3575fe58019SAttilio Rao 		return 0;
3585fe58019SAttilio Rao 	}
3595fe58019SAttilio Rao 
360666f8543SAlan Somers 	err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
3615fe58019SAttilio Rao 	return err;
3625fe58019SAttilio Rao }
3635fe58019SAttilio Rao 
3645fe58019SAttilio Rao /*
365f067b609SAlan Somers  * struct vop_advlock_args {
366f067b609SAlan Somers  *	struct vop_generic_args a_gen;
367f067b609SAlan Somers  *	struct vnode *a_vp;
368f067b609SAlan Somers  *	void *a_id;
369f067b609SAlan Somers  *	int a_op;
370f067b609SAlan Somers  *	struct flock *a_fl;
371f067b609SAlan Somers  *	int a_flags;
372f067b609SAlan Somers  * }
373f067b609SAlan Somers  */
374f067b609SAlan Somers static int
375f067b609SAlan Somers fuse_vnop_advlock(struct vop_advlock_args *ap)
376f067b609SAlan Somers {
377f067b609SAlan Somers 	struct vnode *vp = ap->a_vp;
378f067b609SAlan Somers 	struct flock *fl = ap->a_fl;
379f067b609SAlan Somers 	struct thread *td = curthread;
380f067b609SAlan Somers 	struct ucred *cred = td->td_ucred;
381f067b609SAlan Somers 	pid_t pid = td->td_proc->p_pid;
382f067b609SAlan Somers 	struct fuse_filehandle *fufh;
383f067b609SAlan Somers 	struct fuse_dispatcher fdi;
384f067b609SAlan Somers 	struct fuse_lk_in *fli;
385f067b609SAlan Somers 	struct fuse_lk_out *flo;
386f067b609SAlan Somers 	enum fuse_opcode op;
387f067b609SAlan Somers 	int dataflags, err;
388f067b609SAlan Somers 
389f067b609SAlan Somers 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
390f067b609SAlan Somers 
391f067b609SAlan Somers 	if (fuse_isdeadfs(vp)) {
392f067b609SAlan Somers 		return ENXIO;
393f067b609SAlan Somers 	}
394f067b609SAlan Somers 
395f067b609SAlan Somers 	if (!(dataflags & FSESS_POSIX_LOCKS))
396f067b609SAlan Somers 		return vop_stdadvlock(ap);
397f067b609SAlan Somers 
398f067b609SAlan Somers 	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
399f067b609SAlan Somers 	if (err)
400f067b609SAlan Somers 		return err;
401f067b609SAlan Somers 
402f067b609SAlan Somers 	fdisp_init(&fdi, sizeof(*fli));
403f067b609SAlan Somers 
404f067b609SAlan Somers 	switch(ap->a_op) {
405f067b609SAlan Somers 	case F_GETLK:
406f067b609SAlan Somers 		op = FUSE_GETLK;
407f067b609SAlan Somers 		break;
408f067b609SAlan Somers 	case F_SETLK:
409f067b609SAlan Somers 		op = FUSE_SETLK;
410f067b609SAlan Somers 		break;
411f067b609SAlan Somers 	case F_SETLKW:
412f067b609SAlan Somers 		op = FUSE_SETLKW;
413f067b609SAlan Somers 		break;
414f067b609SAlan Somers 	default:
415f067b609SAlan Somers 		return EINVAL;
416f067b609SAlan Somers 	}
417f067b609SAlan Somers 
418f067b609SAlan Somers 	fdisp_make_vp(&fdi, op, vp, td, cred);
419f067b609SAlan Somers 	fli = fdi.indata;
420f067b609SAlan Somers 	fli->fh = fufh->fh_id;
421f067b609SAlan Somers 	fli->owner = fl->l_pid;
422f067b609SAlan Somers 	fli->lk.start = fl->l_start;
423f067b609SAlan Somers 	if (fl->l_len != 0)
424f067b609SAlan Somers 		fli->lk.end = fl->l_start + fl->l_len - 1;
425f067b609SAlan Somers 	else
426f067b609SAlan Somers 		fli->lk.end = INT64_MAX;
427f067b609SAlan Somers 	fli->lk.type = fl->l_type;
428f067b609SAlan Somers 	fli->lk.pid = fl->l_pid;
429f067b609SAlan Somers 
430f067b609SAlan Somers 	err = fdisp_wait_answ(&fdi);
431f067b609SAlan Somers 	fdisp_destroy(&fdi);
432f067b609SAlan Somers 
433f067b609SAlan Somers 	if (err == 0 && op == FUSE_GETLK) {
434f067b609SAlan Somers 		flo = fdi.answ;
435f067b609SAlan Somers 		fl->l_type = flo->lk.type;
436f067b609SAlan Somers 		fl->l_pid = flo->lk.pid;
437f067b609SAlan Somers 		if (flo->lk.type != F_UNLCK) {
438f067b609SAlan Somers 			fl->l_start = flo->lk.start;
439f067b609SAlan Somers 			if (flo->lk.end == INT64_MAX)
440f067b609SAlan Somers 				fl->l_len = 0;
441f067b609SAlan Somers 			else
442f067b609SAlan Somers 				fl->l_len = flo->lk.end - flo->lk.start + 1;
443f067b609SAlan Somers 			fl->l_start = flo->lk.start;
444f067b609SAlan Somers 		}
445f067b609SAlan Somers 	}
446f067b609SAlan Somers 
447f067b609SAlan Somers 	return err;
448f067b609SAlan Somers }
449f067b609SAlan Somers 
450f067b609SAlan Somers /*
4519f10f423SAlan Somers     struct vop_close_args {
4525fe58019SAttilio Rao 	struct vnode *a_vp;
4535fe58019SAttilio Rao 	int  a_fflag;
4545fe58019SAttilio Rao 	struct ucred *a_cred;
4555fe58019SAttilio Rao 	struct thread *a_td;
4565fe58019SAttilio Rao     };
4575fe58019SAttilio Rao */
4585fe58019SAttilio Rao static int
4595fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap)
4605fe58019SAttilio Rao {
4615fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4625fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
4635fe58019SAttilio Rao 	int fflag = ap->a_fflag;
464f8d4af10SAlan Somers 	struct thread *td = ap->a_td;
465f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
4669f10f423SAlan Somers 	int err = 0;
4675fe58019SAttilio Rao 
46835cf0e7eSAlan Somers 	if (fuse_isdeadfs(vp))
4695fe58019SAttilio Rao 		return 0;
47035cf0e7eSAlan Somers 	if (vnode_isdir(vp))
47135cf0e7eSAlan Somers 		return 0;
47235cf0e7eSAlan Somers 	if (fflag & IO_NDELAY)
47335cf0e7eSAlan Somers 		return 0;
4745ec10aa5SAlan Somers 
4759f10f423SAlan Somers 	err = fuse_flush(vp, cred, pid, fflag);
4765ec10aa5SAlan Somers 	/* TODO: close the file handle, if we're sure it's no longer used */
4775fe58019SAttilio Rao 	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
478f8d4af10SAlan Somers 		fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
4795fe58019SAttilio Rao 	}
4809f10f423SAlan Somers 	return err;
4815fe58019SAttilio Rao }
4825fe58019SAttilio Rao 
48319ef317dSAlan Somers static void
48419ef317dSAlan Somers fdisp_make_mknod_for_fallback(
48519ef317dSAlan Somers 	struct fuse_dispatcher *fdip,
48619ef317dSAlan Somers 	struct componentname *cnp,
48719ef317dSAlan Somers 	struct vnode *dvp,
48819ef317dSAlan Somers 	uint64_t parentnid,
48919ef317dSAlan Somers 	struct thread *td,
49019ef317dSAlan Somers 	struct ucred *cred,
49119ef317dSAlan Somers 	mode_t mode,
49219ef317dSAlan Somers 	enum fuse_opcode *op)
49319ef317dSAlan Somers {
49419ef317dSAlan Somers 	struct fuse_mknod_in *fmni;
49519ef317dSAlan Somers 
49619ef317dSAlan Somers 	fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
49719ef317dSAlan Somers 	*op = FUSE_MKNOD;
49819ef317dSAlan Somers 	fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
49919ef317dSAlan Somers 	fmni = fdip->indata;
50019ef317dSAlan Somers 	fmni->mode = mode;
50119ef317dSAlan Somers 	fmni->rdev = 0;
50219ef317dSAlan Somers 	memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
50319ef317dSAlan Somers 	    cnp->cn_namelen);
50419ef317dSAlan Somers 	((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
50519ef317dSAlan Somers }
5065fe58019SAttilio Rao /*
5075fe58019SAttilio Rao     struct vnop_create_args {
5085fe58019SAttilio Rao 	struct vnode *a_dvp;
5095fe58019SAttilio Rao 	struct vnode **a_vpp;
5105fe58019SAttilio Rao 	struct componentname *a_cnp;
5115fe58019SAttilio Rao 	struct vattr *a_vap;
5125fe58019SAttilio Rao     };
5135fe58019SAttilio Rao */
5145fe58019SAttilio Rao static int
5155fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap)
5165fe58019SAttilio Rao {
5175fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
5185fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
5195fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
5205fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
5215fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
5225fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
5235fe58019SAttilio Rao 
5245fe58019SAttilio Rao 	struct fuse_open_in *foi;
5255fe58019SAttilio Rao 	struct fuse_entry_out *feo;
52619ef317dSAlan Somers 	struct fuse_open_out *foo;
52719ef317dSAlan Somers 	struct fuse_dispatcher fdi, fdi2;
5285fe58019SAttilio Rao 	struct fuse_dispatcher *fdip = &fdi;
52919ef317dSAlan Somers 	struct fuse_dispatcher *fdip2 = NULL;
5305fe58019SAttilio Rao 
5315fe58019SAttilio Rao 	int err;
5325fe58019SAttilio Rao 
5335fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
5345fe58019SAttilio Rao 	uint64_t parentnid = VTOFUD(dvp)->nid;
5355fe58019SAttilio Rao 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
53619ef317dSAlan Somers 	enum fuse_opcode op;
5379e444871SAlan Somers 	int flags;
5389e444871SAlan Somers 
5399e444871SAlan Somers 	/*
5409e444871SAlan Somers 	 * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
5419e444871SAlan Somers 	 * writable mode makes sense, and we might as well include readability
5429e444871SAlan Somers 	 * too.
5439e444871SAlan Somers 	 */
5449e444871SAlan Somers 	flags = O_RDWR;
5455fe58019SAttilio Rao 
5465fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
5475fe58019SAttilio Rao 		return ENXIO;
5485fe58019SAttilio Rao 	}
5495fe58019SAttilio Rao 	bzero(&fdi, sizeof(fdi));
5505fe58019SAttilio Rao 
551ede571e4SAlan Somers 	if ((vap->va_type != VREG && vap->va_type != VSOCK))
552372b97d0SRick Macklem 		return (EINVAL);
5535fe58019SAttilio Rao 
5545fe58019SAttilio Rao 	if (!fsess_isimpl(mp, FUSE_CREATE)) {
55519ef317dSAlan Somers 		/* Fallback to FUSE_MKNOD/FUSE_OPEN */
55619ef317dSAlan Somers 		fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
55719ef317dSAlan Somers 			cred, mode, &op);
55819ef317dSAlan Somers 	} else {
55919ef317dSAlan Somers 		/* Use FUSE_CREATE */
56019ef317dSAlan Somers 		op = FUSE_CREATE;
56119ef317dSAlan Somers 		fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
56219ef317dSAlan Somers 		fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
5635fe58019SAttilio Rao 		foi = fdip->indata;
5645fe58019SAttilio Rao 		foi->mode = mode;
5659e444871SAlan Somers 		foi->flags = O_CREAT | flags;
5665fe58019SAttilio Rao 		memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
5675fe58019SAttilio Rao 		    cnp->cn_namelen);
5685fe58019SAttilio Rao 		((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
56919ef317dSAlan Somers 	}
5705fe58019SAttilio Rao 
5715fe58019SAttilio Rao 	err = fdisp_wait_answ(fdip);
5725fe58019SAttilio Rao 
5736de3b00dSAttilio Rao 	if (err) {
57419ef317dSAlan Somers 		if (err == ENOSYS && op == FUSE_CREATE) {
5755fe58019SAttilio Rao 			fsess_set_notimpl(mp, FUSE_CREATE);
57619ef317dSAlan Somers 			fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
57719ef317dSAlan Somers 				parentnid, td, cred, mode, &op);
57819ef317dSAlan Somers 			err = fdisp_wait_answ(fdip);
57919ef317dSAlan Somers 		}
58019ef317dSAlan Somers 		if (err)
5815fe58019SAttilio Rao 			goto out;
5825fe58019SAttilio Rao 	}
583372b97d0SRick Macklem 
5845fe58019SAttilio Rao 	feo = fdip->answ;
5855fe58019SAttilio Rao 
586ede571e4SAlan Somers 	if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
5875fe58019SAttilio Rao 		goto out;
5885fe58019SAttilio Rao 	}
58919ef317dSAlan Somers 
59019ef317dSAlan Somers 	if (op == FUSE_CREATE) {
59119ef317dSAlan Somers 		foo = (struct fuse_open_out*)(feo + 1);
59219ef317dSAlan Somers 	} else {
59319ef317dSAlan Somers 		/* Issue a separate FUSE_OPEN */
59419ef317dSAlan Somers 		fdip2 = &fdi2;
59519ef317dSAlan Somers 		fdisp_init(fdip2, sizeof(*foi));
59619ef317dSAlan Somers 		fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
59719ef317dSAlan Somers 			cred);
59819ef317dSAlan Somers 		foi = fdip2->indata;
59919ef317dSAlan Somers 		foi->mode = mode;
6009e444871SAlan Somers 		foi->flags = flags;
60119ef317dSAlan Somers 		err = fdisp_wait_answ(fdip2);
60219ef317dSAlan Somers 		if (err)
60319ef317dSAlan Somers 			goto out;
60419ef317dSAlan Somers 		foo = fdip2->answ;
60519ef317dSAlan Somers 	}
606ede571e4SAlan Somers 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
6075fe58019SAttilio Rao 	if (err) {
6085fe58019SAttilio Rao 		struct fuse_release_in *fri;
6095fe58019SAttilio Rao 		uint64_t nodeid = feo->nodeid;
61019ef317dSAlan Somers 		uint64_t fh_id = foo->fh;
6115fe58019SAttilio Rao 
6125fe58019SAttilio Rao 		fdisp_init(fdip, sizeof(*fri));
6135fe58019SAttilio Rao 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
6145fe58019SAttilio Rao 		fri = fdip->indata;
6155fe58019SAttilio Rao 		fri->fh = fh_id;
6169e444871SAlan Somers 		fri->flags = flags;
6175fe58019SAttilio Rao 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
618268c28edSAlan Somers 		fuse_insert_message(fdip->tick, false);
61919ef317dSAlan Somers 		goto out;
6205fe58019SAttilio Rao 	}
6215fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
622cad67791SAlan Somers 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
623cad67791SAlan Somers 		feo->attr_valid_nsec, NULL);
6245fe58019SAttilio Rao 
625a7e81cb3SAlan Somers 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
62619ef317dSAlan Somers 	fuse_vnode_open(*vpp, foo->open_flags, td);
6275fe58019SAttilio Rao 	cache_purge_negative(dvp);
6285fe58019SAttilio Rao 
6295fe58019SAttilio Rao out:
63019ef317dSAlan Somers 	if (fdip2)
63119ef317dSAlan Somers 		fdisp_destroy(fdip2);
6325fe58019SAttilio Rao 	fdisp_destroy(fdip);
6335fe58019SAttilio Rao 	return err;
6345fe58019SAttilio Rao }
6355fe58019SAttilio Rao 
6365fe58019SAttilio Rao /*
637915012e0SAlan Somers     struct vnop_fdatasync_args {
638915012e0SAlan Somers 	struct vop_generic_args a_gen;
639915012e0SAlan Somers 	struct vnode * a_vp;
640915012e0SAlan Somers 	struct thread * a_td;
641915012e0SAlan Somers     };
6425fe58019SAttilio Rao */
643915012e0SAlan Somers static int
644915012e0SAlan Somers fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
645915012e0SAlan Somers {
646915012e0SAlan Somers 	struct vnode *vp = ap->a_vp;
647915012e0SAlan Somers 	struct thread *td = ap->a_td;
648915012e0SAlan Somers 	int waitfor = MNT_WAIT;
649915012e0SAlan Somers 
650915012e0SAlan Somers 	int err = 0;
651915012e0SAlan Somers 
652915012e0SAlan Somers 	if (fuse_isdeadfs(vp)) {
653915012e0SAlan Somers 		return 0;
654915012e0SAlan Somers 	}
655915012e0SAlan Somers 	if ((err = vop_stdfdatasync_buf(ap)))
656915012e0SAlan Somers 		return err;
657915012e0SAlan Somers 
658915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, true);
659915012e0SAlan Somers }
6605fe58019SAttilio Rao 
6615fe58019SAttilio Rao /*
6625fe58019SAttilio Rao     struct vnop_fsync_args {
663915012e0SAlan Somers 	struct vop_generic_args a_gen;
6645fe58019SAttilio Rao 	struct vnode * a_vp;
6655fe58019SAttilio Rao 	int  a_waitfor;
6665fe58019SAttilio Rao 	struct thread * a_td;
6675fe58019SAttilio Rao     };
6685fe58019SAttilio Rao */
6695fe58019SAttilio Rao static int
6705fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap)
6715fe58019SAttilio Rao {
6725fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
6735fe58019SAttilio Rao 	struct thread *td = ap->a_td;
67490612f3cSAlan Somers 	int waitfor = ap->a_waitfor;
675915012e0SAlan Somers 	int err = 0;
6765fe58019SAttilio Rao 
6775fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
6785fe58019SAttilio Rao 		return 0;
6795fe58019SAttilio Rao 	}
6805fe58019SAttilio Rao 	if ((err = vop_stdfsync(ap)))
6815fe58019SAttilio Rao 		return err;
6825fe58019SAttilio Rao 
683915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, false);
6845fe58019SAttilio Rao }
6855fe58019SAttilio Rao 
6865fe58019SAttilio Rao /*
6875fe58019SAttilio Rao     struct vnop_getattr_args {
6885fe58019SAttilio Rao 	struct vnode *a_vp;
6895fe58019SAttilio Rao 	struct vattr *a_vap;
6905fe58019SAttilio Rao 	struct ucred *a_cred;
6915fe58019SAttilio Rao 	struct thread *a_td;
6925fe58019SAttilio Rao     };
6935fe58019SAttilio Rao */
6945fe58019SAttilio Rao static int
6955fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap)
6965fe58019SAttilio Rao {
6975fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
6985fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
6995fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
7005fe58019SAttilio Rao 	struct thread *td = curthread;
7015fe58019SAttilio Rao 
7025fe58019SAttilio Rao 	int err = 0;
7035fe58019SAttilio Rao 	int dataflags;
7045fe58019SAttilio Rao 
7055fe58019SAttilio Rao 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
7065fe58019SAttilio Rao 
7075fe58019SAttilio Rao 	/* Note that we are not bailing out on a dead file system just yet. */
7085fe58019SAttilio Rao 
7095fe58019SAttilio Rao 	if (!(dataflags & FSESS_INITED)) {
7105fe58019SAttilio Rao 		if (!vnode_isvroot(vp)) {
7115fe58019SAttilio Rao 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
7125fe58019SAttilio Rao 			err = ENOTCONN;
7135fe58019SAttilio Rao 			return err;
7145fe58019SAttilio Rao 		} else {
7155fe58019SAttilio Rao 			goto fake;
7165fe58019SAttilio Rao 		}
7175fe58019SAttilio Rao 	}
718cad67791SAlan Somers 	err = fuse_internal_getattr(vp, vap, cred, td);
719cad67791SAlan Somers 	if (err == ENOTCONN && vnode_isvroot(vp)) {
720cf169498SAlan Somers 		/* see comment in fuse_vfsop_statfs() */
7215fe58019SAttilio Rao 		goto fake;
722cad67791SAlan Somers 	} else {
7235fe58019SAttilio Rao 		return err;
724cad67791SAlan Somers 	}
7255fe58019SAttilio Rao 
7265fe58019SAttilio Rao fake:
7275fe58019SAttilio Rao 	bzero(vap, sizeof(*vap));
7285fe58019SAttilio Rao 	vap->va_type = vnode_vtype(vp);
7295fe58019SAttilio Rao 
7305fe58019SAttilio Rao 	return 0;
7315fe58019SAttilio Rao }
7325fe58019SAttilio Rao 
7335fe58019SAttilio Rao /*
7345fe58019SAttilio Rao     struct vnop_inactive_args {
7355fe58019SAttilio Rao 	struct vnode *a_vp;
7365fe58019SAttilio Rao 	struct thread *a_td;
7375fe58019SAttilio Rao     };
7385fe58019SAttilio Rao */
7395fe58019SAttilio Rao static int
7405fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap)
7415fe58019SAttilio Rao {
7425fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
7435fe58019SAttilio Rao 	struct thread *td = ap->a_td;
7445fe58019SAttilio Rao 
7455fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
7465ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
7475fe58019SAttilio Rao 
7485ec10aa5SAlan Somers 	int need_flush = 1;
7495fe58019SAttilio Rao 
7505ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
7515fe58019SAttilio Rao 		if (need_flush && vp->v_type == VREG) {
7525fe58019SAttilio Rao 			if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
753f8d4af10SAlan Somers 				fuse_vnode_savesize(vp, NULL, 0);
7545fe58019SAttilio Rao 			}
7555fe58019SAttilio Rao 			if (fuse_data_cache_invalidate ||
7565fe58019SAttilio Rao 			    (fvdat->flag & FN_REVOKED) != 0)
7575fe58019SAttilio Rao 				fuse_io_invalbuf(vp, td);
7585fe58019SAttilio Rao 			else
7595fe58019SAttilio Rao 				fuse_io_flushbuf(vp, MNT_WAIT, td);
7605fe58019SAttilio Rao 			need_flush = 0;
7615fe58019SAttilio Rao 		}
7625ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
7635fe58019SAttilio Rao 	}
7645fe58019SAttilio Rao 
7655fe58019SAttilio Rao 	if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
7665fe58019SAttilio Rao 		vrecycle(vp);
7675fe58019SAttilio Rao 	}
7685fe58019SAttilio Rao 	return 0;
7695fe58019SAttilio Rao }
7705fe58019SAttilio Rao 
7715fe58019SAttilio Rao /*
7725fe58019SAttilio Rao     struct vnop_link_args {
7735fe58019SAttilio Rao 	struct vnode *a_tdvp;
7745fe58019SAttilio Rao 	struct vnode *a_vp;
7755fe58019SAttilio Rao 	struct componentname *a_cnp;
7765fe58019SAttilio Rao     };
7775fe58019SAttilio Rao */
7785fe58019SAttilio Rao static int
7795fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap)
7805fe58019SAttilio Rao {
7815fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
7825fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
7835fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
7845fe58019SAttilio Rao 
7855fe58019SAttilio Rao 	struct vattr *vap = VTOVA(vp);
7865fe58019SAttilio Rao 
7875fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
7885fe58019SAttilio Rao 	struct fuse_entry_out *feo;
7895fe58019SAttilio Rao 	struct fuse_link_in fli;
7905fe58019SAttilio Rao 
7915fe58019SAttilio Rao 	int err;
7925fe58019SAttilio Rao 
7935fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
7945fe58019SAttilio Rao 		return ENXIO;
7955fe58019SAttilio Rao 	}
7965fe58019SAttilio Rao 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
7975fe58019SAttilio Rao 		return EXDEV;
7985fe58019SAttilio Rao 	}
79978a7722fSConrad Meyer 
80078a7722fSConrad Meyer 	/*
80178a7722fSConrad Meyer 	 * This is a seatbelt check to protect naive userspace filesystems from
80278a7722fSConrad Meyer 	 * themselves and the limitations of the FUSE IPC protocol.  If a
80378a7722fSConrad Meyer 	 * filesystem does not allow attribute caching, assume it is capable of
80478a7722fSConrad Meyer 	 * validating that nlink does not overflow.
80578a7722fSConrad Meyer 	 */
80678a7722fSConrad Meyer 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
8075fe58019SAttilio Rao 		return EMLINK;
8085fe58019SAttilio Rao 	fli.oldnodeid = VTOI(vp);
8095fe58019SAttilio Rao 
8105fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
8115fe58019SAttilio Rao 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
8125fe58019SAttilio Rao 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
8135fe58019SAttilio Rao 	if ((err = fdisp_wait_answ(&fdi))) {
8145fe58019SAttilio Rao 		goto out;
8155fe58019SAttilio Rao 	}
8165fe58019SAttilio Rao 	feo = fdi.answ;
8175fe58019SAttilio Rao 
8185fe58019SAttilio Rao 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
8195fe58019SAttilio Rao out:
8205fe58019SAttilio Rao 	fdisp_destroy(&fdi);
8215fe58019SAttilio Rao 	return err;
8225fe58019SAttilio Rao }
8235fe58019SAttilio Rao 
824dc14d593SAlan Somers struct fuse_lookup_alloc_arg {
825dc14d593SAlan Somers 	struct fuse_entry_out *feo;
826dc14d593SAlan Somers 	struct componentname *cnp;
827dc14d593SAlan Somers 	uint64_t nid;
828dc14d593SAlan Somers 	enum vtype vtyp;
829dc14d593SAlan Somers };
830dc14d593SAlan Somers 
831dc14d593SAlan Somers /* Callback for vn_get_ino */
832dc14d593SAlan Somers static int
833dc14d593SAlan Somers fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
834dc14d593SAlan Somers {
835dc14d593SAlan Somers 	struct fuse_lookup_alloc_arg *flaa = arg;
836dc14d593SAlan Somers 
837dc14d593SAlan Somers 	return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
838dc14d593SAlan Somers 		flaa->vtyp);
839dc14d593SAlan Somers }
840dc14d593SAlan Somers 
841419e7ff6SAlan Somers SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
842ccb75e49SAlan Somers 	"int", "struct timespec*", "struct timespec*");
8435fe58019SAttilio Rao /*
8445fe58019SAttilio Rao     struct vnop_lookup_args {
8455fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
8465fe58019SAttilio Rao 	struct vnode *a_dvp;
8475fe58019SAttilio Rao 	struct vnode **a_vpp;
8485fe58019SAttilio Rao 	struct componentname *a_cnp;
8495fe58019SAttilio Rao     };
8505fe58019SAttilio Rao */
8515fe58019SAttilio Rao int
8525fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap)
8535fe58019SAttilio Rao {
8545fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
8555fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
8565fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
8575fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
8585fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
8595fe58019SAttilio Rao 
8605fe58019SAttilio Rao 	int nameiop = cnp->cn_nameiop;
8615fe58019SAttilio Rao 	int flags = cnp->cn_flags;
8625fe58019SAttilio Rao 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
8635fe58019SAttilio Rao 	int islastcn = flags & ISLASTCN;
8645fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
8655fe58019SAttilio Rao 
8665fe58019SAttilio Rao 	int err = 0;
8675fe58019SAttilio Rao 	int lookup_err = 0;
8685fe58019SAttilio Rao 	struct vnode *vp = NULL;
8695fe58019SAttilio Rao 
8705fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
871438b8a6fSAlan Somers 	bool did_lookup = false;
87244f10c6eSAlan Somers 	struct fuse_entry_out *feo = NULL;
873438b8a6fSAlan Somers 	enum vtype vtyp;	/* vnode type of target */
874438b8a6fSAlan Somers 	off_t filesize;		/* filesize of target */
8755fe58019SAttilio Rao 
8765fe58019SAttilio Rao 	uint64_t nid;
8775fe58019SAttilio Rao 
8785fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
8795fe58019SAttilio Rao 		*vpp = NULL;
8805fe58019SAttilio Rao 		return ENXIO;
8815fe58019SAttilio Rao 	}
88273825da3SAlan Somers 	if (!vnode_isdir(dvp))
8835fe58019SAttilio Rao 		return ENOTDIR;
8845fe58019SAttilio Rao 
88573825da3SAlan Somers 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
88673825da3SAlan Somers 		return EROFS;
88773825da3SAlan Somers 
88873825da3SAlan Somers 	if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
8895fe58019SAttilio Rao 		return err;
89073825da3SAlan Somers 
8915fe58019SAttilio Rao 	if (flags & ISDOTDOT) {
8925fe58019SAttilio Rao 		nid = VTOFUD(dvp)->parent_nid;
893438b8a6fSAlan Somers 		if (nid == 0)
8945fe58019SAttilio Rao 			return ENOENT;
895438b8a6fSAlan Somers 		/* .. is obviously a directory */
896438b8a6fSAlan Somers 		vtyp = VDIR;
897438b8a6fSAlan Somers 		filesize = 0;
8985fe58019SAttilio Rao 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
8995fe58019SAttilio Rao 		nid = VTOI(dvp);
900438b8a6fSAlan Somers 		/* . is obviously a directory */
901438b8a6fSAlan Somers 		vtyp = VDIR;
902438b8a6fSAlan Somers 		filesize = 0;
903ccb75e49SAlan Somers 	} else {
9043f2c630cSAlan Somers 		struct timespec now, timeout;
9053f2c630cSAlan Somers 
9063f2c630cSAlan Somers 		err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
9073f2c630cSAlan Somers 		getnanouptime(&now);
908419e7ff6SAlan Somers 		SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
909ccb75e49SAlan Somers 		switch (err) {
910ccb75e49SAlan Somers 		case -1:		/* positive match */
911ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, >)) {
9123f2c630cSAlan Somers 				atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
9133f2c630cSAlan Somers 			} else {
9143f2c630cSAlan Somers 				/* Cache timeout */
9153f2c630cSAlan Somers 				atomic_add_acq_long(&fuse_lookup_cache_misses,
9163f2c630cSAlan Somers 					1);
9174683b905SAlan Somers 				cache_purge(*vpp);
918ccb75e49SAlan Somers 				if (dvp != *vpp)
919ccb75e49SAlan Somers 					vput(*vpp);
920ccb75e49SAlan Somers 				else
921ccb75e49SAlan Somers 					vrele(*vpp);
922ccb75e49SAlan Somers 				*vpp = NULL;
9233f2c630cSAlan Somers 				break;
9243f2c630cSAlan Somers 			}
9253f2c630cSAlan Somers 			return 0;
9263f2c630cSAlan Somers 
9273f2c630cSAlan Somers 		case 0:		/* no match in cache */
9283f2c630cSAlan Somers 			atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
9293f2c630cSAlan Somers 			break;
9303f2c630cSAlan Somers 
9313f2c630cSAlan Somers 		case ENOENT:		/* negative match */
9323f2c630cSAlan Somers 			getnanouptime(&now);
933ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, <=)) {
9343f2c630cSAlan Somers 				/* Cache timeout */
935ccb75e49SAlan Somers 				cache_purge_negative(dvp);
9363f2c630cSAlan Somers 				break;
9373f2c630cSAlan Somers 			}
9383f2c630cSAlan Somers 			/* fall through */
9393f2c630cSAlan Somers 		default:
9403f2c630cSAlan Somers 			return err;
9413f2c630cSAlan Somers 		}
942438b8a6fSAlan Somers 
9435fe58019SAttilio Rao 		nid = VTOI(dvp);
9445fe58019SAttilio Rao 		fdisp_init(&fdi, cnp->cn_namelen + 1);
945438b8a6fSAlan Somers 		fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
9465fe58019SAttilio Rao 
9475fe58019SAttilio Rao 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
9485fe58019SAttilio Rao 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
9495fe58019SAttilio Rao 		lookup_err = fdisp_wait_answ(&fdi);
950438b8a6fSAlan Somers 		did_lookup = true;
9515fe58019SAttilio Rao 
952438b8a6fSAlan Somers 		if (!lookup_err) {
953ccb75e49SAlan Somers 			/* lookup call succeeded */
9545fe58019SAttilio Rao 			nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
955438b8a6fSAlan Somers 			feo = (struct fuse_entry_out *)fdi.answ;
95644f10c6eSAlan Somers 			if (nid == 0) {
95744f10c6eSAlan Somers 				/* zero nodeid means ENOENT and cache it */
95844f10c6eSAlan Somers 				struct timespec timeout;
95944f10c6eSAlan Somers 
9606248288eSAlan Somers 				fdi.answ_stat = ENOENT;
9615fe58019SAttilio Rao 				lookup_err = ENOENT;
96244f10c6eSAlan Somers 				if (cnp->cn_flags & MAKEENTRY) {
96344f10c6eSAlan Somers 					fuse_validity_2_timespec(feo, &timeout);
964438b8a6fSAlan Somers 					cache_enter_time(dvp, *vpp, cnp,
965438b8a6fSAlan Somers 						&timeout, NULL);
96644f10c6eSAlan Somers 				}
9675fe58019SAttilio Rao 			} else if (nid == FUSE_ROOT_ID) {
9685fe58019SAttilio Rao 				lookup_err = EINVAL;
9695fe58019SAttilio Rao 			}
970438b8a6fSAlan Somers 			vtyp = IFTOVT(feo->attr.mode);
971438b8a6fSAlan Somers 			filesize = feo->attr.size;
9725fe58019SAttilio Rao 		}
973438b8a6fSAlan Somers 		if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
9745fe58019SAttilio Rao 			fdisp_destroy(&fdi);
9755fe58019SAttilio Rao 			return lookup_err;
9765fe58019SAttilio Rao 		}
977438b8a6fSAlan Somers 	}
9785fe58019SAttilio Rao 	/* lookup_err, if non-zero, must be ENOENT at this point */
9795fe58019SAttilio Rao 
9805fe58019SAttilio Rao 	if (lookup_err) {
981ff4fbdf5SAlan Somers 		/* Entry not found */
982ff4fbdf5SAlan Somers 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
983666f8543SAlan Somers 			err = fuse_internal_access(dvp, VWRITE, td, cred);
9846124fd71SAlan Somers 			if (!err) {
9855fe58019SAttilio Rao 				/*
986dc14d593SAlan Somers 				 * Set the SAVENAME flag to hold onto the
9876124fd71SAlan Somers 				 * pathname for use later in VOP_CREATE or
9886124fd71SAlan Somers 				 * VOP_RENAME.
9895fe58019SAttilio Rao 				 */
9905fe58019SAttilio Rao 				cnp->cn_flags |= SAVENAME;
9915fe58019SAttilio Rao 
9925fe58019SAttilio Rao 				err = EJUSTRETURN;
9935fe58019SAttilio Rao 			}
9946124fd71SAlan Somers 		} else {
9955fe58019SAttilio Rao 			err = ENOENT;
9966124fd71SAlan Somers 		}
9975fe58019SAttilio Rao 	} else {
998ff4fbdf5SAlan Somers 		/* Entry was found */
9995fe58019SAttilio Rao 		if (flags & ISDOTDOT) {
1000dc14d593SAlan Somers 			struct fuse_lookup_alloc_arg flaa;
10015fe58019SAttilio Rao 
1002dc14d593SAlan Somers 			flaa.nid = nid;
1003dc14d593SAlan Somers 			flaa.feo = feo;
1004dc14d593SAlan Somers 			flaa.cnp = cnp;
1005dc14d593SAlan Somers 			flaa.vtyp = vtyp;
1006dc14d593SAlan Somers 			err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1007dc14d593SAlan Somers 				&vp);
10085fe58019SAttilio Rao 			*vpp = vp;
10095fe58019SAttilio Rao 		} else if (nid == VTOI(dvp)) {
10105fe58019SAttilio Rao 			vref(dvp);
10115fe58019SAttilio Rao 			*vpp = dvp;
10125fe58019SAttilio Rao 		} else {
10133c324b94SConrad Meyer 			struct fuse_vnode_data *fvdat;
10143c324b94SConrad Meyer 
101509176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1016438b8a6fSAlan Somers 			    &vp, cnp, vtyp);
10176124fd71SAlan Somers 			if (err)
10185fe58019SAttilio Rao 				goto out;
10196124fd71SAlan Somers 			*vpp = vp;
10206124fd71SAlan Somers 
10215fe58019SAttilio Rao 			fuse_vnode_setparent(vp, dvp);
10223c324b94SConrad Meyer 
10233c324b94SConrad Meyer 			/*
10243c324b94SConrad Meyer 			 * In the case where we are looking up a FUSE node
10253c324b94SConrad Meyer 			 * represented by an existing cached vnode, and the
10263c324b94SConrad Meyer 			 * true size reported by FUSE_LOOKUP doesn't match
10273c324b94SConrad Meyer 			 * the vnode's cached size, fix the vnode cache to
10283c324b94SConrad Meyer 			 * match the real object size.
10293c324b94SConrad Meyer 			 *
10306124fd71SAlan Somers 			 * We can get here:
10316124fd71SAlan Somers 			 * * following attribute cache expiration, or
10326124fd71SAlan Somers 			 * * due a bug in the daemon, or
10336124fd71SAlan Somers 			 * * the first time that we looked up the file.
10343c324b94SConrad Meyer 			 */
10353c324b94SConrad Meyer 			fvdat = VTOFUD(vp);
10363c324b94SConrad Meyer 			if (vnode_isreg(vp) &&
1037438b8a6fSAlan Somers 			    filesize != fvdat->filesize) {
10383c324b94SConrad Meyer 				/*
10393c324b94SConrad Meyer 				 * The FN_SIZECHANGE flag reflects a dirty
10403c324b94SConrad Meyer 				 * append.  If userspace lets us know our cache
10413c324b94SConrad Meyer 				 * is invalid, that write was lost.  (Dirty
10423c324b94SConrad Meyer 				 * writes that do not cause append are also
10433c324b94SConrad Meyer 				 * lost, but we don't detect them here.)
10443c324b94SConrad Meyer 				 *
10453c324b94SConrad Meyer 				 * XXX: Maybe disable WB caching on this mount.
10463c324b94SConrad Meyer 				 */
10473c324b94SConrad Meyer 				if (fvdat->flag & FN_SIZECHANGE)
10483c324b94SConrad Meyer 					printf("%s: WB cache incoherent on "
10493c324b94SConrad Meyer 					    "%s!\n", __func__,
10503c324b94SConrad Meyer 					    vnode_mount(vp)->mnt_stat.f_mntonname);
10513c324b94SConrad Meyer 
1052438b8a6fSAlan Somers 				(void)fuse_vnode_setsize(vp, cred, filesize);
10533c324b94SConrad Meyer 				fvdat->flag &= ~FN_SIZECHANGE;
10543c324b94SConrad Meyer 			}
10555fe58019SAttilio Rao 
10566124fd71SAlan Somers 			MPASS(feo != NULL);
1057438b8a6fSAlan Somers 			fuse_internal_cache_attrs(*vpp, &feo->attr,
1058438b8a6fSAlan Somers 				feo->attr_valid, feo->attr_valid_nsec, NULL);
10596124fd71SAlan Somers 
10606124fd71SAlan Somers 			if ((nameiop == DELETE || nameiop == RENAME) &&
10616124fd71SAlan Somers 				islastcn)
10626124fd71SAlan Somers 			{
10636124fd71SAlan Somers 				struct vattr dvattr;
10646124fd71SAlan Somers 
10656124fd71SAlan Somers 				err = fuse_internal_access(dvp, VWRITE, td,
10666124fd71SAlan Somers 					cred);
10676124fd71SAlan Somers 				if (err != 0)
10686124fd71SAlan Somers 					goto out;
10696124fd71SAlan Somers 				/*
10706124fd71SAlan Somers 				 * if the parent's sticky bit is set, check
10716124fd71SAlan Somers 				 * whether we're allowed to remove the file.
10726124fd71SAlan Somers 				 * Need to figure out the vnode locking to make
10736124fd71SAlan Somers 				 * this work.
10746124fd71SAlan Somers 				 */
10756124fd71SAlan Somers 				fuse_internal_getattr(dvp, &dvattr, cred, td);
10766124fd71SAlan Somers 				if ((dvattr.va_mode & S_ISTXT) &&
10776124fd71SAlan Somers 					fuse_internal_access(dvp, VADMIN, td,
10786124fd71SAlan Somers 						cred) &&
10796124fd71SAlan Somers 					fuse_internal_access(*vpp, VADMIN, td,
10806124fd71SAlan Somers 						cred)) {
10816124fd71SAlan Somers 					err = EPERM;
10826124fd71SAlan Somers 					goto out;
10836124fd71SAlan Somers 				}
10846124fd71SAlan Somers 			}
10856124fd71SAlan Somers 
10866124fd71SAlan Somers 			if (islastcn && (
10876124fd71SAlan Somers 				(nameiop == DELETE) ||
10886124fd71SAlan Somers 				(nameiop == RENAME && wantparent))) {
10896124fd71SAlan Somers 				cnp->cn_flags |= SAVENAME;
10906124fd71SAlan Somers 			}
10916124fd71SAlan Somers 
10925fe58019SAttilio Rao 		}
10935fe58019SAttilio Rao 	}
10945fe58019SAttilio Rao out:
10956124fd71SAlan Somers 	if (err) {
10966124fd71SAlan Somers 		if (vp != NULL && dvp != vp)
10976124fd71SAlan Somers 			vput(vp);
10986124fd71SAlan Somers 		else if (vp != NULL)
10996124fd71SAlan Somers 			vrele(vp);
11006124fd71SAlan Somers 		*vpp = NULL;
11015fe58019SAttilio Rao 	}
1102438b8a6fSAlan Somers 	if (did_lookup)
11035fe58019SAttilio Rao 		fdisp_destroy(&fdi);
11045fe58019SAttilio Rao 
11055fe58019SAttilio Rao 	return err;
11065fe58019SAttilio Rao }
11075fe58019SAttilio Rao 
11085fe58019SAttilio Rao /*
11095fe58019SAttilio Rao     struct vnop_mkdir_args {
11105fe58019SAttilio Rao 	struct vnode *a_dvp;
11115fe58019SAttilio Rao 	struct vnode **a_vpp;
11125fe58019SAttilio Rao 	struct componentname *a_cnp;
11135fe58019SAttilio Rao 	struct vattr *a_vap;
11145fe58019SAttilio Rao     };
11155fe58019SAttilio Rao */
11165fe58019SAttilio Rao static int
11175fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap)
11185fe58019SAttilio Rao {
11195fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
11205fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
11215fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
11225fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
11235fe58019SAttilio Rao 
11245fe58019SAttilio Rao 	struct fuse_mkdir_in fmdi;
11255fe58019SAttilio Rao 
11265fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
11275fe58019SAttilio Rao 		return ENXIO;
11285fe58019SAttilio Rao 	}
11295fe58019SAttilio Rao 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
11305fe58019SAttilio Rao 
1131d9454fabSAttilio Rao 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1132d9454fabSAttilio Rao 	    sizeof(fmdi), VDIR));
11335fe58019SAttilio Rao }
11345fe58019SAttilio Rao 
11355fe58019SAttilio Rao /*
11365fe58019SAttilio Rao     struct vnop_mknod_args {
11375fe58019SAttilio Rao 	struct vnode *a_dvp;
11385fe58019SAttilio Rao 	struct vnode **a_vpp;
11395fe58019SAttilio Rao 	struct componentname *a_cnp;
11405fe58019SAttilio Rao 	struct vattr *a_vap;
11415fe58019SAttilio Rao     };
11425fe58019SAttilio Rao */
11435fe58019SAttilio Rao static int
11445fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap)
11455fe58019SAttilio Rao {
11465fe58019SAttilio Rao 
1147bf4d7084SAlan Somers 	struct vnode *dvp = ap->a_dvp;
1148bf4d7084SAlan Somers 	struct vnode **vpp = ap->a_vpp;
1149bf4d7084SAlan Somers 	struct componentname *cnp = ap->a_cnp;
1150bf4d7084SAlan Somers 	struct vattr *vap = ap->a_vap;
1151bf4d7084SAlan Somers 	struct fuse_mknod_in fmni;
11525fe58019SAttilio Rao 
1153bf4d7084SAlan Somers 	if (fuse_isdeadfs(dvp))
1154bf4d7084SAlan Somers 		return ENXIO;
1155bf4d7084SAlan Somers 
1156bf4d7084SAlan Somers 	fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1157bf4d7084SAlan Somers 	fmni.rdev = vap->va_rdev;
1158bf4d7084SAlan Somers 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1159bf4d7084SAlan Somers 	    sizeof(fmni), vap->va_type));
1160bf4d7084SAlan Somers }
11615fe58019SAttilio Rao 
11625fe58019SAttilio Rao /*
11635fe58019SAttilio Rao     struct vnop_open_args {
11645fe58019SAttilio Rao 	struct vnode *a_vp;
11655fe58019SAttilio Rao 	int  a_mode;
11665fe58019SAttilio Rao 	struct ucred *a_cred;
11675fe58019SAttilio Rao 	struct thread *a_td;
11685fe58019SAttilio Rao 	int a_fdidx; / struct file *a_fp;
11695fe58019SAttilio Rao     };
11705fe58019SAttilio Rao */
11715fe58019SAttilio Rao static int
11725fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap)
11735fe58019SAttilio Rao {
11745fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11759e444871SAlan Somers 	int a_mode = ap->a_mode;
11765fe58019SAttilio Rao 	struct thread *td = ap->a_td;
11775fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1178f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
11795fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat;
11805fe58019SAttilio Rao 
1181bf4d7084SAlan Somers 	if (fuse_isdeadfs(vp))
11825fe58019SAttilio Rao 		return ENXIO;
1183bf4d7084SAlan Somers 	if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1184bf4d7084SAlan Somers 		return (EOPNOTSUPP);
11859e444871SAlan Somers 	if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1186e7df9886SConrad Meyer 		return EINVAL;
1187e7df9886SConrad Meyer 
11885fe58019SAttilio Rao 	fvdat = VTOFUD(vp);
11895fe58019SAttilio Rao 
11909e444871SAlan Somers 	if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
11915fccbf31SAlan Somers 		fuse_vnode_open(vp, 0, td);
11925fe58019SAttilio Rao 		return 0;
11935fe58019SAttilio Rao 	}
11945fe58019SAttilio Rao 
11959e444871SAlan Somers 	return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
11965fe58019SAttilio Rao }
11975fe58019SAttilio Rao 
1198746c92e0SJohn Baldwin static int
1199746c92e0SJohn Baldwin fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1200746c92e0SJohn Baldwin {
1201746c92e0SJohn Baldwin 
1202746c92e0SJohn Baldwin 	switch (ap->a_name) {
1203746c92e0SJohn Baldwin 	case _PC_FILESIZEBITS:
1204746c92e0SJohn Baldwin 		*ap->a_retval = 64;
1205746c92e0SJohn Baldwin 		return (0);
1206599afe53SJohn Baldwin 	case _PC_NAME_MAX:
1207599afe53SJohn Baldwin 		*ap->a_retval = NAME_MAX;
1208599afe53SJohn Baldwin 		return (0);
1209599afe53SJohn Baldwin 	case _PC_LINK_MAX:
1210f83f3d79SJohn Baldwin 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1211599afe53SJohn Baldwin 		return (0);
1212746c92e0SJohn Baldwin 	case _PC_SYMLINK_MAX:
1213746c92e0SJohn Baldwin 		*ap->a_retval = MAXPATHLEN;
1214746c92e0SJohn Baldwin 		return (0);
1215746c92e0SJohn Baldwin 	case _PC_NO_TRUNC:
1216746c92e0SJohn Baldwin 		*ap->a_retval = 1;
1217746c92e0SJohn Baldwin 		return (0);
1218746c92e0SJohn Baldwin 	default:
1219746c92e0SJohn Baldwin 		return (vop_stdpathconf(ap));
1220746c92e0SJohn Baldwin 	}
1221746c92e0SJohn Baldwin }
1222746c92e0SJohn Baldwin 
12235fe58019SAttilio Rao /*
12245fe58019SAttilio Rao     struct vnop_read_args {
12255fe58019SAttilio Rao 	struct vnode *a_vp;
12265fe58019SAttilio Rao 	struct uio *a_uio;
12275fe58019SAttilio Rao 	int  a_ioflag;
12285fe58019SAttilio Rao 	struct ucred *a_cred;
12295fe58019SAttilio Rao     };
12305fe58019SAttilio Rao */
12315fe58019SAttilio Rao static int
12325fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap)
12335fe58019SAttilio Rao {
12345fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12355fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12365fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
12375fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1238f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
12395fe58019SAttilio Rao 
12405fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12415fe58019SAttilio Rao 		return ENXIO;
12425fe58019SAttilio Rao 	}
1243ead063e0SEdward Tomasz Napierala 
1244ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1245ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1246ead063e0SEdward Tomasz Napierala 	}
1247ead063e0SEdward Tomasz Napierala 
12489c7ec331SAlan Somers 	return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
12495fe58019SAttilio Rao }
12505fe58019SAttilio Rao 
12515fe58019SAttilio Rao /*
12525fe58019SAttilio Rao     struct vnop_readdir_args {
12535fe58019SAttilio Rao 	struct vnode *a_vp;
12545fe58019SAttilio Rao 	struct uio *a_uio;
12555fe58019SAttilio Rao 	struct ucred *a_cred;
12565fe58019SAttilio Rao 	int *a_eofflag;
12575fe58019SAttilio Rao 	int *ncookies;
12585fe58019SAttilio Rao 	u_long **a_cookies;
12595fe58019SAttilio Rao     };
12605fe58019SAttilio Rao */
12615fe58019SAttilio Rao static int
12625fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap)
12635fe58019SAttilio Rao {
12645fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12655fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12665fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
12675fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
12685fe58019SAttilio Rao 	struct fuse_iov cookediov;
12695fe58019SAttilio Rao 	int err = 0;
1270f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
12715fe58019SAttilio Rao 
12725fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12735fe58019SAttilio Rao 		return ENXIO;
12745fe58019SAttilio Rao 	}
12755fe58019SAttilio Rao 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
12765fe58019SAttilio Rao 	    (uio_resid(uio) < sizeof(struct dirent))) {
12775fe58019SAttilio Rao 		return EINVAL;
12785fe58019SAttilio Rao 	}
12795fe58019SAttilio Rao 
128035cf0e7eSAlan Somers 	err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
128135cf0e7eSAlan Somers 	if (err)
12825fe58019SAttilio Rao 		return (err);
12835fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
12845fe58019SAttilio Rao 	fiov_init(&cookediov, DIRCOOKEDSIZE);
12855fe58019SAttilio Rao 
12865fe58019SAttilio Rao 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
12875fe58019SAttilio Rao 
12885fe58019SAttilio Rao 	fiov_teardown(&cookediov);
12895ec10aa5SAlan Somers 
12905fe58019SAttilio Rao 	return err;
12915fe58019SAttilio Rao }
12925fe58019SAttilio Rao 
12935fe58019SAttilio Rao /*
12945fe58019SAttilio Rao     struct vnop_readlink_args {
12955fe58019SAttilio Rao 	struct vnode *a_vp;
12965fe58019SAttilio Rao 	struct uio *a_uio;
12975fe58019SAttilio Rao 	struct ucred *a_cred;
12985fe58019SAttilio Rao     };
12995fe58019SAttilio Rao */
13005fe58019SAttilio Rao static int
13015fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap)
13025fe58019SAttilio Rao {
13035fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13045fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
13055fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
13065fe58019SAttilio Rao 
13075fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
13085fe58019SAttilio Rao 	int err;
13095fe58019SAttilio Rao 
13105fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13115fe58019SAttilio Rao 		return ENXIO;
13125fe58019SAttilio Rao 	}
13135fe58019SAttilio Rao 	if (!vnode_islnk(vp)) {
13145fe58019SAttilio Rao 		return EINVAL;
13155fe58019SAttilio Rao 	}
13165fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
13175fe58019SAttilio Rao 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
13185fe58019SAttilio Rao 	if (err) {
13195fe58019SAttilio Rao 		goto out;
13205fe58019SAttilio Rao 	}
13215fe58019SAttilio Rao 	if (((char *)fdi.answ)[0] == '/' &&
13225fe58019SAttilio Rao 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
13235fe58019SAttilio Rao 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
13245fe58019SAttilio Rao 
13255fe58019SAttilio Rao 		err = uiomove(mpth, strlen(mpth), uio);
13265fe58019SAttilio Rao 	}
13275fe58019SAttilio Rao 	if (!err) {
13285fe58019SAttilio Rao 		err = uiomove(fdi.answ, fdi.iosize, uio);
13295fe58019SAttilio Rao 	}
13305fe58019SAttilio Rao out:
13315fe58019SAttilio Rao 	fdisp_destroy(&fdi);
13325fe58019SAttilio Rao 	return err;
13335fe58019SAttilio Rao }
13345fe58019SAttilio Rao 
13355fe58019SAttilio Rao /*
13365fe58019SAttilio Rao     struct vnop_reclaim_args {
13375fe58019SAttilio Rao 	struct vnode *a_vp;
13385fe58019SAttilio Rao 	struct thread *a_td;
13395fe58019SAttilio Rao     };
13405fe58019SAttilio Rao */
13415fe58019SAttilio Rao static int
13425fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap)
13435fe58019SAttilio Rao {
13445fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13455fe58019SAttilio Rao 	struct thread *td = ap->a_td;
13465fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
13475ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
13485fe58019SAttilio Rao 
13495fe58019SAttilio Rao 	if (!fvdat) {
13505fe58019SAttilio Rao 		panic("FUSE: no vnode data during recycling");
13515fe58019SAttilio Rao 	}
13525ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
13535ec10aa5SAlan Somers 		printf("FUSE: vnode being reclaimed with open fufh "
13549e444871SAlan Somers 			"(type=%#x)", fufh->fufh_type);
13555ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
13565fe58019SAttilio Rao 	}
13575fe58019SAttilio Rao 
13585fe58019SAttilio Rao 	if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
13595fe58019SAttilio Rao 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
13605fe58019SAttilio Rao 		    fvdat->nlookup);
13615fe58019SAttilio Rao 	}
13625fe58019SAttilio Rao 	fuse_vnode_setparent(vp, NULL);
13635fe58019SAttilio Rao 	cache_purge(vp);
13645fe58019SAttilio Rao 	vfs_hash_remove(vp);
13655fe58019SAttilio Rao 	vnode_destroy_vobject(vp);
13665fe58019SAttilio Rao 	fuse_vnode_destroy(vp);
13675fe58019SAttilio Rao 
13685fe58019SAttilio Rao 	return 0;
13695fe58019SAttilio Rao }
13705fe58019SAttilio Rao 
13715fe58019SAttilio Rao /*
13725fe58019SAttilio Rao     struct vnop_remove_args {
13735fe58019SAttilio Rao 	struct vnode *a_dvp;
13745fe58019SAttilio Rao 	struct vnode *a_vp;
13755fe58019SAttilio Rao 	struct componentname *a_cnp;
13765fe58019SAttilio Rao     };
13775fe58019SAttilio Rao */
13785fe58019SAttilio Rao static int
13795fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap)
13805fe58019SAttilio Rao {
13815fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
13825fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13835fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
13845fe58019SAttilio Rao 
13855fe58019SAttilio Rao 	int err;
13865fe58019SAttilio Rao 
13875fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13885fe58019SAttilio Rao 		return ENXIO;
13895fe58019SAttilio Rao 	}
13905fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
13915fe58019SAttilio Rao 		return EPERM;
13925fe58019SAttilio Rao 	}
13935fe58019SAttilio Rao 	cache_purge(vp);
13945fe58019SAttilio Rao 
13955fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
13965fe58019SAttilio Rao 
1397d9454fabSAttilio Rao 	if (err == 0)
13985fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
13995fe58019SAttilio Rao 	return err;
14005fe58019SAttilio Rao }
14015fe58019SAttilio Rao 
14025fe58019SAttilio Rao /*
14035fe58019SAttilio Rao     struct vnop_rename_args {
14045fe58019SAttilio Rao 	struct vnode *a_fdvp;
14055fe58019SAttilio Rao 	struct vnode *a_fvp;
14065fe58019SAttilio Rao 	struct componentname *a_fcnp;
14075fe58019SAttilio Rao 	struct vnode *a_tdvp;
14085fe58019SAttilio Rao 	struct vnode *a_tvp;
14095fe58019SAttilio Rao 	struct componentname *a_tcnp;
14105fe58019SAttilio Rao     };
14115fe58019SAttilio Rao */
14125fe58019SAttilio Rao static int
14135fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap)
14145fe58019SAttilio Rao {
14155fe58019SAttilio Rao 	struct vnode *fdvp = ap->a_fdvp;
14165fe58019SAttilio Rao 	struct vnode *fvp = ap->a_fvp;
14175fe58019SAttilio Rao 	struct componentname *fcnp = ap->a_fcnp;
14185fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
14195fe58019SAttilio Rao 	struct vnode *tvp = ap->a_tvp;
14205fe58019SAttilio Rao 	struct componentname *tcnp = ap->a_tcnp;
14215fe58019SAttilio Rao 	struct fuse_data *data;
14225fe58019SAttilio Rao 
14235fe58019SAttilio Rao 	int err = 0;
14245fe58019SAttilio Rao 
14255fe58019SAttilio Rao 	if (fuse_isdeadfs(fdvp)) {
14265fe58019SAttilio Rao 		return ENXIO;
14275fe58019SAttilio Rao 	}
14285fe58019SAttilio Rao 	if (fvp->v_mount != tdvp->v_mount ||
14295fe58019SAttilio Rao 	    (tvp && fvp->v_mount != tvp->v_mount)) {
1430419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
14315fe58019SAttilio Rao 		err = EXDEV;
14325fe58019SAttilio Rao 		goto out;
14335fe58019SAttilio Rao 	}
14345fe58019SAttilio Rao 	cache_purge(fvp);
14355fe58019SAttilio Rao 
14365fe58019SAttilio Rao 	/*
14375fe58019SAttilio Rao 	 * FUSE library is expected to check if target directory is not
14385fe58019SAttilio Rao 	 * under the source directory in the file system tree.
14395fe58019SAttilio Rao 	 * Linux performs this check at VFS level.
14405fe58019SAttilio Rao 	 */
14415fe58019SAttilio Rao 	data = fuse_get_mpdata(vnode_mount(tdvp));
14425fe58019SAttilio Rao 	sx_xlock(&data->rename_lock);
14435fe58019SAttilio Rao 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
14445fe58019SAttilio Rao 	if (err == 0) {
1445d9454fabSAttilio Rao 		if (tdvp != fdvp)
14465fe58019SAttilio Rao 			fuse_vnode_setparent(fvp, tdvp);
14475fe58019SAttilio Rao 		if (tvp != NULL)
14485fe58019SAttilio Rao 			fuse_vnode_setparent(tvp, NULL);
14495fe58019SAttilio Rao 	}
14505fe58019SAttilio Rao 	sx_unlock(&data->rename_lock);
14515fe58019SAttilio Rao 
14525fe58019SAttilio Rao 	if (tvp != NULL && tvp != fvp) {
14535fe58019SAttilio Rao 		cache_purge(tvp);
14545fe58019SAttilio Rao 	}
14555fe58019SAttilio Rao 	if (vnode_isdir(fvp)) {
14565fe58019SAttilio Rao 		if ((tvp != NULL) && vnode_isdir(tvp)) {
14575fe58019SAttilio Rao 			cache_purge(tdvp);
14585fe58019SAttilio Rao 		}
14595fe58019SAttilio Rao 		cache_purge(fdvp);
14605fe58019SAttilio Rao 	}
14615fe58019SAttilio Rao out:
14625fe58019SAttilio Rao 	if (tdvp == tvp) {
14635fe58019SAttilio Rao 		vrele(tdvp);
14645fe58019SAttilio Rao 	} else {
14655fe58019SAttilio Rao 		vput(tdvp);
14665fe58019SAttilio Rao 	}
14675fe58019SAttilio Rao 	if (tvp != NULL) {
14685fe58019SAttilio Rao 		vput(tvp);
14695fe58019SAttilio Rao 	}
14705fe58019SAttilio Rao 	vrele(fdvp);
14715fe58019SAttilio Rao 	vrele(fvp);
14725fe58019SAttilio Rao 
14735fe58019SAttilio Rao 	return err;
14745fe58019SAttilio Rao }
14755fe58019SAttilio Rao 
14765fe58019SAttilio Rao /*
14775fe58019SAttilio Rao     struct vnop_rmdir_args {
14785fe58019SAttilio Rao 	    struct vnode *a_dvp;
14795fe58019SAttilio Rao 	    struct vnode *a_vp;
14805fe58019SAttilio Rao 	    struct componentname *a_cnp;
14815fe58019SAttilio Rao     } *ap;
14825fe58019SAttilio Rao */
14835fe58019SAttilio Rao static int
14845fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap)
14855fe58019SAttilio Rao {
14865fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
14875fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
14885fe58019SAttilio Rao 
14895fe58019SAttilio Rao 	int err;
14905fe58019SAttilio Rao 
14915fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
14925fe58019SAttilio Rao 		return ENXIO;
14935fe58019SAttilio Rao 	}
14945fe58019SAttilio Rao 	if (VTOFUD(vp) == VTOFUD(dvp)) {
14955fe58019SAttilio Rao 		return EINVAL;
14965fe58019SAttilio Rao 	}
14975fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
14985fe58019SAttilio Rao 
1499d9454fabSAttilio Rao 	if (err == 0)
15005fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
15015fe58019SAttilio Rao 	return err;
15025fe58019SAttilio Rao }
15035fe58019SAttilio Rao 
15045fe58019SAttilio Rao /*
15055fe58019SAttilio Rao     struct vnop_setattr_args {
15065fe58019SAttilio Rao 	struct vnode *a_vp;
15075fe58019SAttilio Rao 	struct vattr *a_vap;
15085fe58019SAttilio Rao 	struct ucred *a_cred;
15095fe58019SAttilio Rao 	struct thread *a_td;
15105fe58019SAttilio Rao     };
15115fe58019SAttilio Rao */
15125fe58019SAttilio Rao static int
15135fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap)
15145fe58019SAttilio Rao {
15155fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
15165fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
15175fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
15185fe58019SAttilio Rao 	struct thread *td = curthread;
15195fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
15205fe58019SAttilio Rao 	struct fuse_setattr_in *fsai;
1521*474ba6faSAlan Somers 	struct mount *mp;
1522f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
1523*474ba6faSAlan Somers 	struct fuse_data *data;
1524*474ba6faSAlan Somers 	int dataflags;
15255fe58019SAttilio Rao 	int err = 0;
15265fe58019SAttilio Rao 	enum vtype vtyp;
15275fe58019SAttilio Rao 	int sizechanged = 0;
15285fe58019SAttilio Rao 	uint64_t newsize = 0;
1529ff4fbdf5SAlan Somers 	accmode_t accmode = 0;
15305fe58019SAttilio Rao 
1531*474ba6faSAlan Somers 	mp = vnode_mount(vp);
1532*474ba6faSAlan Somers 	data = fuse_get_mpdata(mp);
1533*474ba6faSAlan Somers 	dataflags = data->dataflags;
1534*474ba6faSAlan Somers 
15355fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
15365fe58019SAttilio Rao 		return ENXIO;
15375fe58019SAttilio Rao 	}
15385fe58019SAttilio Rao 	fdisp_init(&fdi, sizeof(*fsai));
15395fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
15405fe58019SAttilio Rao 	fsai = fdi.indata;
15415fe58019SAttilio Rao 	fsai->valid = 0;
15425fe58019SAttilio Rao 
15435fe58019SAttilio Rao 	if (vap->va_uid != (uid_t)VNOVAL) {
1544*474ba6faSAlan Somers 		if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
1545*474ba6faSAlan Somers 			/* Only root may change a file's owner */
1546*474ba6faSAlan Somers 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1547*474ba6faSAlan Somers 			if (err)
1548*474ba6faSAlan Somers 				return err;
1549*474ba6faSAlan Somers 		}
15505fe58019SAttilio Rao 		fsai->uid = vap->va_uid;
15515fe58019SAttilio Rao 		fsai->valid |= FATTR_UID;
1552ff4fbdf5SAlan Somers 		accmode |= VADMIN;
15535fe58019SAttilio Rao 	}
15545fe58019SAttilio Rao 	if (vap->va_gid != (gid_t)VNOVAL) {
1555*474ba6faSAlan Somers 		if (dataflags & FSESS_DEFAULT_PERMISSIONS &&
1556*474ba6faSAlan Somers 		    !groupmember(vap->va_gid, cred))
1557*474ba6faSAlan Somers 		{
1558*474ba6faSAlan Somers 			/*
1559*474ba6faSAlan Somers 			 * Non-root users may only chgrp to one of their own
1560*474ba6faSAlan Somers 			 * groups
1561*474ba6faSAlan Somers 			 */
1562*474ba6faSAlan Somers 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1563*474ba6faSAlan Somers 			if (err)
1564*474ba6faSAlan Somers 				return err;
1565*474ba6faSAlan Somers 		}
15665fe58019SAttilio Rao 		fsai->gid = vap->va_gid;
15675fe58019SAttilio Rao 		fsai->valid |= FATTR_GID;
1568ff4fbdf5SAlan Somers 		accmode |= VADMIN;
15695fe58019SAttilio Rao 	}
15705fe58019SAttilio Rao 	if (vap->va_size != VNOVAL) {
15715fe58019SAttilio Rao 
15725fe58019SAttilio Rao 		struct fuse_filehandle *fufh = NULL;
15735fe58019SAttilio Rao 
15745fe58019SAttilio Rao 		/*Truncate to a new value. */
15755fe58019SAttilio Rao 		fsai->size = vap->va_size;
15765fe58019SAttilio Rao 		sizechanged = 1;
15775fe58019SAttilio Rao 		newsize = vap->va_size;
15785fe58019SAttilio Rao 		fsai->valid |= FATTR_SIZE;
1579ff4fbdf5SAlan Somers 		accmode |= VWRITE;
15805fe58019SAttilio Rao 
15819f10f423SAlan Somers 		fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
15825fe58019SAttilio Rao 		if (fufh) {
15835fe58019SAttilio Rao 			fsai->fh = fufh->fh_id;
15845fe58019SAttilio Rao 			fsai->valid |= FATTR_FH;
15855fe58019SAttilio Rao 		}
15865fe58019SAttilio Rao 	}
15875fe58019SAttilio Rao 	if (vap->va_atime.tv_sec != VNOVAL) {
15885fe58019SAttilio Rao 		fsai->atime = vap->va_atime.tv_sec;
15895fe58019SAttilio Rao 		fsai->atimensec = vap->va_atime.tv_nsec;
15905fe58019SAttilio Rao 		fsai->valid |= FATTR_ATIME;
1591ff4fbdf5SAlan Somers 		accmode |= VADMIN;
1592ff4fbdf5SAlan Somers 		/*
1593ff4fbdf5SAlan Somers 		 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1594ff4fbdf5SAlan Somers 		 */
15955fe58019SAttilio Rao 	}
15965fe58019SAttilio Rao 	if (vap->va_mtime.tv_sec != VNOVAL) {
15975fe58019SAttilio Rao 		fsai->mtime = vap->va_mtime.tv_sec;
15985fe58019SAttilio Rao 		fsai->mtimensec = vap->va_mtime.tv_nsec;
15995fe58019SAttilio Rao 		fsai->valid |= FATTR_MTIME;
1600ff4fbdf5SAlan Somers 		accmode |= VADMIN;
1601ff4fbdf5SAlan Somers 		/*
1602ff4fbdf5SAlan Somers 		 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1603ff4fbdf5SAlan Somers 		 */
16045fe58019SAttilio Rao 	}
16055fe58019SAttilio Rao 	if (vap->va_mode != (mode_t)VNOVAL) {
16065fe58019SAttilio Rao 		fsai->mode = vap->va_mode & ALLPERMS;
16075fe58019SAttilio Rao 		fsai->valid |= FATTR_MODE;
1608ff4fbdf5SAlan Somers 		accmode |= VADMIN;
16095fe58019SAttilio Rao 	}
16105fe58019SAttilio Rao 	if (!fsai->valid) {
16115fe58019SAttilio Rao 		goto out;
16125fe58019SAttilio Rao 	}
16135fe58019SAttilio Rao 	vtyp = vnode_vtype(vp);
16145fe58019SAttilio Rao 
16155fe58019SAttilio Rao 	if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
16165fe58019SAttilio Rao 		err = EISDIR;
16175fe58019SAttilio Rao 		goto out;
16185fe58019SAttilio Rao 	}
16195fe58019SAttilio Rao 	if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
16205fe58019SAttilio Rao 		err = EROFS;
16215fe58019SAttilio Rao 		goto out;
16225fe58019SAttilio Rao 	}
1623666f8543SAlan Somers 	err = fuse_internal_access(vp, accmode, td, cred);
1624ff4fbdf5SAlan Somers 	if (err)
1625ff4fbdf5SAlan Somers 		goto out;
16265fe58019SAttilio Rao 
1627d9454fabSAttilio Rao 	if ((err = fdisp_wait_answ(&fdi)))
16285fe58019SAttilio Rao 		goto out;
16295fe58019SAttilio Rao 	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
16305fe58019SAttilio Rao 
16315fe58019SAttilio Rao 	if (vnode_vtype(vp) != vtyp) {
16325fe58019SAttilio Rao 		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1633419e7ff6SAlan Somers 			SDT_PROBE2(fusefs, , vnops, trace, 1, "FUSE: Dang! "
1634cf169498SAlan Somers 				"vnode_vtype is VNON and vtype isn't.");
16355fe58019SAttilio Rao 		} else {
16365fe58019SAttilio Rao 			/*
16375fe58019SAttilio Rao 	                 * STALE vnode, ditch
16385fe58019SAttilio Rao 	                 *
1639cf169498SAlan Somers 			 * The vnode has changed its type "behind our back".
1640cf169498SAlan Somers 			 * There's nothing really we can do, so let us just
1641cf169498SAlan Somers 			 * force an internal revocation and tell the caller to
1642cf169498SAlan Somers 			 * try again, if interested.
16435fe58019SAttilio Rao 	                 */
16445fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
16455fe58019SAttilio Rao 			err = EAGAIN;
16465fe58019SAttilio Rao 		}
16475fe58019SAttilio Rao 	}
16487e4844f7SAlan Somers 	if (err == 0) {
16497e4844f7SAlan Somers 		struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
16507e4844f7SAlan Somers 		fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
16517e4844f7SAlan Somers 			fao->attr_valid_nsec, NULL);
16527e4844f7SAlan Somers 	}
1653194e691aSConrad Meyer 
16545fe58019SAttilio Rao out:
16555fe58019SAttilio Rao 	fdisp_destroy(&fdi);
16565fe58019SAttilio Rao 	if (!err && sizechanged) {
16575fe58019SAttilio Rao 		fuse_vnode_setsize(vp, cred, newsize);
16585fe58019SAttilio Rao 		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
16595fe58019SAttilio Rao 	}
16605fe58019SAttilio Rao 	return err;
16615fe58019SAttilio Rao }
16625fe58019SAttilio Rao 
16635fe58019SAttilio Rao /*
16645fe58019SAttilio Rao     struct vnop_strategy_args {
16655fe58019SAttilio Rao 	struct vnode *a_vp;
16665fe58019SAttilio Rao 	struct buf *a_bp;
16675fe58019SAttilio Rao     };
16685fe58019SAttilio Rao */
16695fe58019SAttilio Rao static int
16705fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap)
16715fe58019SAttilio Rao {
16725fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
16735fe58019SAttilio Rao 	struct buf *bp = ap->a_bp;
16745fe58019SAttilio Rao 
16755fe58019SAttilio Rao 	if (!vp || fuse_isdeadfs(vp)) {
16765fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
16775fe58019SAttilio Rao 		bp->b_error = ENXIO;
16785fe58019SAttilio Rao 		bufdone(bp);
167998852a32SAlan Somers 		return 0;
16805fe58019SAttilio Rao 	}
1681f203d173SAlan Somers 	if (bp->b_iocmd == BIO_WRITE) {
1682f203d173SAlan Somers 		int err;
16835fe58019SAttilio Rao 
1684f203d173SAlan Somers 		err = fuse_vnode_refreshsize(vp, NOCRED);
1685f203d173SAlan Somers 		if (err) {
1686f203d173SAlan Somers 			bp->b_ioflags |= BIO_ERROR;
1687f203d173SAlan Somers 			bp->b_error = err;
168898852a32SAlan Somers 			bufdone(bp);
1689f203d173SAlan Somers 			return 0;
1690f203d173SAlan Somers 		}
1691f203d173SAlan Somers 	}
16925fe58019SAttilio Rao 
16935fe58019SAttilio Rao 	/*
1694f203d173SAlan Somers 	 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1695f203d173SAlan Somers 	 * fuse_io_strategy sets bp's error fields
16965fe58019SAttilio Rao 	 */
1697f203d173SAlan Somers 	(void)fuse_io_strategy(vp, bp);
1698f203d173SAlan Somers 
16995fe58019SAttilio Rao 	return 0;
17005fe58019SAttilio Rao }
17015fe58019SAttilio Rao 
17025fe58019SAttilio Rao 
17035fe58019SAttilio Rao /*
17045fe58019SAttilio Rao     struct vnop_symlink_args {
17055fe58019SAttilio Rao 	struct vnode *a_dvp;
17065fe58019SAttilio Rao 	struct vnode **a_vpp;
17075fe58019SAttilio Rao 	struct componentname *a_cnp;
17085fe58019SAttilio Rao 	struct vattr *a_vap;
17095fe58019SAttilio Rao 	char *a_target;
17105fe58019SAttilio Rao     };
17115fe58019SAttilio Rao */
17125fe58019SAttilio Rao static int
17135fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap)
17145fe58019SAttilio Rao {
17155fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
17165fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
17175fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
17181493c2eeSBrooks Davis 	const char *target = ap->a_target;
17195fe58019SAttilio Rao 
17205fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
17215fe58019SAttilio Rao 
17225fe58019SAttilio Rao 	int err;
17235fe58019SAttilio Rao 	size_t len;
17245fe58019SAttilio Rao 
17255fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
17265fe58019SAttilio Rao 		return ENXIO;
17275fe58019SAttilio Rao 	}
17285fe58019SAttilio Rao 	/*
17295fe58019SAttilio Rao 	 * Unlike the other creator type calls, here we have to create a message
17305fe58019SAttilio Rao 	 * where the name of the new entry comes first, and the data describing
17315fe58019SAttilio Rao 	 * the entry comes second.
17325fe58019SAttilio Rao 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
17335fe58019SAttilio Rao 	 * but put together the message manually and just call the core part.
17345fe58019SAttilio Rao 	 */
17355fe58019SAttilio Rao 
17365fe58019SAttilio Rao 	len = strlen(target) + 1;
17375fe58019SAttilio Rao 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
17385fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
17395fe58019SAttilio Rao 
17405fe58019SAttilio Rao 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
17415fe58019SAttilio Rao 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
17425fe58019SAttilio Rao 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
17435fe58019SAttilio Rao 
17445fe58019SAttilio Rao 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
17455fe58019SAttilio Rao 	fdisp_destroy(&fdi);
17465fe58019SAttilio Rao 	return err;
17475fe58019SAttilio Rao }
17485fe58019SAttilio Rao 
17495fe58019SAttilio Rao /*
17505fe58019SAttilio Rao     struct vnop_write_args {
17515fe58019SAttilio Rao 	struct vnode *a_vp;
17525fe58019SAttilio Rao 	struct uio *a_uio;
17535fe58019SAttilio Rao 	int  a_ioflag;
17545fe58019SAttilio Rao 	struct ucred *a_cred;
17555fe58019SAttilio Rao     };
17565fe58019SAttilio Rao */
17575fe58019SAttilio Rao static int
17585fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap)
17595fe58019SAttilio Rao {
17605fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
17615fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
17625fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
17635fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1764f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
1765f203d173SAlan Somers 	int err;
17665fe58019SAttilio Rao 
17675fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
17685fe58019SAttilio Rao 		return ENXIO;
17695fe58019SAttilio Rao 	}
1770f203d173SAlan Somers 	err = fuse_vnode_refreshsize(vp, cred);
1771f203d173SAlan Somers 	if (err)
1772f203d173SAlan Somers 		return err;
17735fe58019SAttilio Rao 
1774ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1775ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1776ead063e0SEdward Tomasz Napierala 	}
1777ead063e0SEdward Tomasz Napierala 
17789c7ec331SAlan Somers 	return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
17795fe58019SAttilio Rao }
17805fe58019SAttilio Rao 
1781419e7ff6SAlan Somers SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
17825fe58019SAttilio Rao /*
17835fe58019SAttilio Rao     struct vnop_getpages_args {
17845fe58019SAttilio Rao 	struct vnode *a_vp;
17855fe58019SAttilio Rao 	vm_page_t *a_m;
17865fe58019SAttilio Rao 	int a_count;
17875fe58019SAttilio Rao 	int a_reqpage;
17885fe58019SAttilio Rao     };
17895fe58019SAttilio Rao */
17905fe58019SAttilio Rao static int
17915fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap)
17925fe58019SAttilio Rao {
17935fe58019SAttilio Rao 	int i, error, nextoff, size, toff, count, npages;
17945fe58019SAttilio Rao 	struct uio uio;
17955fe58019SAttilio Rao 	struct iovec iov;
17965fe58019SAttilio Rao 	vm_offset_t kva;
17975fe58019SAttilio Rao 	struct buf *bp;
17985fe58019SAttilio Rao 	struct vnode *vp;
17995fe58019SAttilio Rao 	struct thread *td;
18005fe58019SAttilio Rao 	struct ucred *cred;
18015fe58019SAttilio Rao 	vm_page_t *pages;
1802f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
18035fe58019SAttilio Rao 
18045fe58019SAttilio Rao 	vp = ap->a_vp;
18055fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to getpages"));
18065fe58019SAttilio Rao 	td = curthread;			/* XXX */
18075fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
18085fe58019SAttilio Rao 	pages = ap->a_m;
1809f17f88d3SGleb Smirnoff 	npages = ap->a_count;
18105fe58019SAttilio Rao 
18115fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
1812419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
1813cf169498SAlan Somers 			"called on non-cacheable vnode??\n");
18145fe58019SAttilio Rao 		return (VM_PAGER_ERROR);
18155fe58019SAttilio Rao 	}
18165fe58019SAttilio Rao 
18175fe58019SAttilio Rao 	/*
1818b0cd2017SGleb Smirnoff 	 * If the last page is partially valid, just return it and allow
1819b0cd2017SGleb Smirnoff 	 * the pager to zero-out the blanks.  Partially valid pages can
1820b0cd2017SGleb Smirnoff 	 * only occur at the file EOF.
1821b0cd2017SGleb Smirnoff 	 *
1822b0cd2017SGleb Smirnoff 	 * XXXGL: is that true for FUSE, which is a local filesystem,
1823b0cd2017SGleb Smirnoff 	 * but still somewhat disconnected from the kernel?
18245fe58019SAttilio Rao 	 */
182589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
1826f17f88d3SGleb Smirnoff 	if (pages[npages - 1]->valid != 0 && --npages == 0)
1827f17f88d3SGleb Smirnoff 		goto out;
182889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
18295fe58019SAttilio Rao 
18305fe58019SAttilio Rao 	/*
18315fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1832b3a15dddSPedro F. Giffuni 	 * convenient and fast.
18335fe58019SAttilio Rao 	 */
1834756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
18355fe58019SAttilio Rao 
18365fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
18375fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
183883c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
183983c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, npages);
18405fe58019SAttilio Rao 
1841f17f88d3SGleb Smirnoff 	count = npages << PAGE_SHIFT;
18425fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
18435fe58019SAttilio Rao 	iov.iov_len = count;
18445fe58019SAttilio Rao 	uio.uio_iov = &iov;
18455fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
18465fe58019SAttilio Rao 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
18475fe58019SAttilio Rao 	uio.uio_resid = count;
18485fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
18495fe58019SAttilio Rao 	uio.uio_rw = UIO_READ;
18505fe58019SAttilio Rao 	uio.uio_td = td;
18515fe58019SAttilio Rao 
18529c7ec331SAlan Somers 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
18535fe58019SAttilio Rao 	pmap_qremove(kva, npages);
18545fe58019SAttilio Rao 
1855756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
18565fe58019SAttilio Rao 
18575fe58019SAttilio Rao 	if (error && (uio.uio_resid == count)) {
1858419e7ff6SAlan Somers 		SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
18595fe58019SAttilio Rao 		return VM_PAGER_ERROR;
18605fe58019SAttilio Rao 	}
18615fe58019SAttilio Rao 	/*
18625fe58019SAttilio Rao 	 * Calculate the number of bytes read and validate only that number
18635fe58019SAttilio Rao 	 * of bytes.  Note that due to pending writes, size may be 0.  This
18645fe58019SAttilio Rao 	 * does not mean that the remaining data is invalid!
18655fe58019SAttilio Rao 	 */
18665fe58019SAttilio Rao 
18675fe58019SAttilio Rao 	size = count - uio.uio_resid;
186889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
18695fe58019SAttilio Rao 	fuse_vm_page_lock_queues();
18705fe58019SAttilio Rao 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
18715fe58019SAttilio Rao 		vm_page_t m;
18725fe58019SAttilio Rao 
18735fe58019SAttilio Rao 		nextoff = toff + PAGE_SIZE;
18745fe58019SAttilio Rao 		m = pages[i];
18755fe58019SAttilio Rao 
18765fe58019SAttilio Rao 		if (nextoff <= size) {
18775fe58019SAttilio Rao 			/*
18785fe58019SAttilio Rao 			 * Read operation filled an entire page
18795fe58019SAttilio Rao 			 */
18805fe58019SAttilio Rao 			m->valid = VM_PAGE_BITS_ALL;
18815fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18825fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18835fe58019SAttilio Rao 		} else if (size > toff) {
18845fe58019SAttilio Rao 			/*
18855fe58019SAttilio Rao 			 * Read operation filled a partial page.
18865fe58019SAttilio Rao 			 */
18875fe58019SAttilio Rao 			m->valid = 0;
18885fe58019SAttilio Rao 			vm_page_set_valid_range(m, 0, size - toff);
18895fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18905fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18915fe58019SAttilio Rao 		} else {
18925fe58019SAttilio Rao 			/*
1893b3a15dddSPedro F. Giffuni 			 * Read operation was short.  If no error occurred
18945fe58019SAttilio Rao 			 * we may have hit a zero-fill section.   We simply
18955fe58019SAttilio Rao 			 * leave valid set to 0.
18965fe58019SAttilio Rao 			 */
18975fe58019SAttilio Rao 			;
18985fe58019SAttilio Rao 		}
18995fe58019SAttilio Rao 	}
19005fe58019SAttilio Rao 	fuse_vm_page_unlock_queues();
1901f17f88d3SGleb Smirnoff out:
190289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
1903f17f88d3SGleb Smirnoff 	if (ap->a_rbehind)
1904f17f88d3SGleb Smirnoff 		*ap->a_rbehind = 0;
1905f17f88d3SGleb Smirnoff 	if (ap->a_rahead)
1906f17f88d3SGleb Smirnoff 		*ap->a_rahead = 0;
1907f17f88d3SGleb Smirnoff 	return (VM_PAGER_OK);
19085fe58019SAttilio Rao }
19095fe58019SAttilio Rao 
19105fe58019SAttilio Rao /*
19115fe58019SAttilio Rao     struct vnop_putpages_args {
19125fe58019SAttilio Rao 	struct vnode *a_vp;
19135fe58019SAttilio Rao 	vm_page_t *a_m;
19145fe58019SAttilio Rao 	int a_count;
19155fe58019SAttilio Rao 	int a_sync;
19165fe58019SAttilio Rao 	int *a_rtvals;
19175fe58019SAttilio Rao 	vm_ooffset_t a_offset;
19185fe58019SAttilio Rao     };
19195fe58019SAttilio Rao */
19205fe58019SAttilio Rao static int
19215fe58019SAttilio Rao fuse_vnop_putpages(struct vop_putpages_args *ap)
19225fe58019SAttilio Rao {
19235fe58019SAttilio Rao 	struct uio uio;
19245fe58019SAttilio Rao 	struct iovec iov;
19255fe58019SAttilio Rao 	vm_offset_t kva;
19265fe58019SAttilio Rao 	struct buf *bp;
19275fe58019SAttilio Rao 	int i, error, npages, count;
19285fe58019SAttilio Rao 	off_t offset;
19295fe58019SAttilio Rao 	int *rtvals;
19305fe58019SAttilio Rao 	struct vnode *vp;
19315fe58019SAttilio Rao 	struct thread *td;
19325fe58019SAttilio Rao 	struct ucred *cred;
19335fe58019SAttilio Rao 	vm_page_t *pages;
19345fe58019SAttilio Rao 	vm_ooffset_t fsize;
1935f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
19365fe58019SAttilio Rao 
19375fe58019SAttilio Rao 	vp = ap->a_vp;
19385fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to putpages"));
19395fe58019SAttilio Rao 	fsize = vp->v_object->un_pager.vnp.vnp_size;
19405fe58019SAttilio Rao 	td = curthread;			/* XXX */
19415fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
19425fe58019SAttilio Rao 	pages = ap->a_m;
19435fe58019SAttilio Rao 	count = ap->a_count;
19445fe58019SAttilio Rao 	rtvals = ap->a_rtvals;
19455fe58019SAttilio Rao 	npages = btoc(count);
19465fe58019SAttilio Rao 	offset = IDX_TO_OFF(pages[0]->pindex);
19475fe58019SAttilio Rao 
19485fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
1949419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
1950cf169498SAlan Somers 			"called on non-cacheable vnode??\n");
19515fe58019SAttilio Rao 	}
19525fe58019SAttilio Rao 	for (i = 0; i < npages; i++)
19535fe58019SAttilio Rao 		rtvals[i] = VM_PAGER_AGAIN;
19545fe58019SAttilio Rao 
19555fe58019SAttilio Rao 	/*
19565fe58019SAttilio Rao 	 * When putting pages, do not extend file past EOF.
19575fe58019SAttilio Rao 	 */
19585fe58019SAttilio Rao 
19595fe58019SAttilio Rao 	if (offset + count > fsize) {
19605fe58019SAttilio Rao 		count = fsize - offset;
19615fe58019SAttilio Rao 		if (count < 0)
19625fe58019SAttilio Rao 			count = 0;
19635fe58019SAttilio Rao 	}
19645fe58019SAttilio Rao 	/*
19655fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1966b3a15dddSPedro F. Giffuni 	 * convenient and fast.
19675fe58019SAttilio Rao 	 */
1968756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
19695fe58019SAttilio Rao 
19705fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
19715fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
197283c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodeout);
197383c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsout, count);
19745fe58019SAttilio Rao 
19755fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
19765fe58019SAttilio Rao 	iov.iov_len = count;
19775fe58019SAttilio Rao 	uio.uio_iov = &iov;
19785fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
19795fe58019SAttilio Rao 	uio.uio_offset = offset;
19805fe58019SAttilio Rao 	uio.uio_resid = count;
19815fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
19825fe58019SAttilio Rao 	uio.uio_rw = UIO_WRITE;
19835fe58019SAttilio Rao 	uio.uio_td = td;
19845fe58019SAttilio Rao 
19859c7ec331SAlan Somers 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
19865fe58019SAttilio Rao 
19875fe58019SAttilio Rao 	pmap_qremove(kva, npages);
1988756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
19895fe58019SAttilio Rao 
19905fe58019SAttilio Rao 	if (!error) {
19915fe58019SAttilio Rao 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
19925fe58019SAttilio Rao 
19935fe58019SAttilio Rao 		for (i = 0; i < nwritten; i++) {
19945fe58019SAttilio Rao 			rtvals[i] = VM_PAGER_OK;
199589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(pages[i]->object);
19965fe58019SAttilio Rao 			vm_page_undirty(pages[i]);
199789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(pages[i]->object);
19985fe58019SAttilio Rao 		}
19995fe58019SAttilio Rao 	}
20005fe58019SAttilio Rao 	return rtvals[0];
20015fe58019SAttilio Rao }
20025fe58019SAttilio Rao 
200304660064SFedor Uporov static const char extattr_namespace_separator = '.';
200404660064SFedor Uporov 
200504660064SFedor Uporov /*
200604660064SFedor Uporov     struct vop_getextattr_args {
200704660064SFedor Uporov 	struct vop_generic_args a_gen;
200804660064SFedor Uporov 	struct vnode *a_vp;
200904660064SFedor Uporov 	int a_attrnamespace;
201004660064SFedor Uporov 	const char *a_name;
201104660064SFedor Uporov 	struct uio *a_uio;
201204660064SFedor Uporov 	size_t *a_size;
201304660064SFedor Uporov 	struct ucred *a_cred;
201404660064SFedor Uporov 	struct thread *a_td;
201504660064SFedor Uporov     };
201604660064SFedor Uporov */
201704660064SFedor Uporov static int
201804660064SFedor Uporov fuse_vnop_getextattr(struct vop_getextattr_args *ap)
201904660064SFedor Uporov {
202004660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
202104660064SFedor Uporov 	struct uio *uio = ap->a_uio;
202228f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
202304660064SFedor Uporov 	struct fuse_getxattr_in *get_xattr_in;
202404660064SFedor Uporov 	struct fuse_getxattr_out *get_xattr_out;
202504660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
202604660064SFedor Uporov 	struct thread *td = ap->a_td;
202704660064SFedor Uporov 	struct ucred *cred = ap->a_cred;
202828f4f623SFedor Uporov 	char *prefix;
202928f4f623SFedor Uporov 	char *attr_str;
203028f4f623SFedor Uporov 	size_t len;
203128f4f623SFedor Uporov 	int err;
203204660064SFedor Uporov 
203304660064SFedor Uporov 	if (fuse_isdeadfs(vp))
203428f4f623SFedor Uporov 		return (ENXIO);
203504660064SFedor Uporov 
20361f4a83f9SAlan Somers 	if (!fsess_isimpl(mp, FUSE_GETXATTR))
20371f4a83f9SAlan Somers 		return EOPNOTSUPP;
20381f4a83f9SAlan Somers 
2039666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2040ff4fbdf5SAlan Somers 	if (err)
2041ff4fbdf5SAlan Somers 		return err;
2042ff4fbdf5SAlan Somers 
204304660064SFedor Uporov 	/* Default to looking for user attributes. */
204404660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
204504660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
204604660064SFedor Uporov 	else
204704660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
204804660064SFedor Uporov 
204904660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
205004660064SFedor Uporov 	    strlen(ap->a_name) + 1;
205104660064SFedor Uporov 
205204660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
205304660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
205404660064SFedor Uporov 
205504660064SFedor Uporov 	get_xattr_in = fdi.indata;
205604660064SFedor Uporov 	/*
205704660064SFedor Uporov 	 * Check to see whether we're querying the available size or
205804660064SFedor Uporov 	 * issuing the actual request.  If we pass in 0, we get back struct
205904660064SFedor Uporov 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
206004660064SFedor Uporov 	 * that much data, without the struct fuse_getxattr_out header.
206104660064SFedor Uporov 	 */
2062493b4a8cSFedor Uporov 	if (uio == NULL)
206304660064SFedor Uporov 		get_xattr_in->size = 0;
206404660064SFedor Uporov 	else
206504660064SFedor Uporov 		get_xattr_in->size = uio->uio_resid;
206604660064SFedor Uporov 
206704660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
206804660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
206904660064SFedor Uporov 	    ap->a_name);
207004660064SFedor Uporov 
207104660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
207204660064SFedor Uporov 	if (err != 0) {
20731f4a83f9SAlan Somers 		if (err == ENOSYS) {
207404660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_GETXATTR);
20751f4a83f9SAlan Somers 			err = EOPNOTSUPP;
20761f4a83f9SAlan Somers 		}
207704660064SFedor Uporov 		goto out;
207804660064SFedor Uporov 	}
207904660064SFedor Uporov 
208004660064SFedor Uporov 	get_xattr_out = fdi.answ;
208104660064SFedor Uporov 
2082493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
208304660064SFedor Uporov 		*ap->a_size = get_xattr_out->size;
2084493b4a8cSFedor Uporov 
2085493b4a8cSFedor Uporov 	if (uio != NULL)
208604660064SFedor Uporov 		err = uiomove(fdi.answ, fdi.iosize, uio);
208704660064SFedor Uporov 
208804660064SFedor Uporov out:
208904660064SFedor Uporov 	fdisp_destroy(&fdi);
209004660064SFedor Uporov 	return (err);
209104660064SFedor Uporov }
209204660064SFedor Uporov 
209304660064SFedor Uporov /*
209404660064SFedor Uporov     struct vop_setextattr_args {
209504660064SFedor Uporov 	struct vop_generic_args a_gen;
209604660064SFedor Uporov 	struct vnode *a_vp;
209704660064SFedor Uporov 	int a_attrnamespace;
209804660064SFedor Uporov 	const char *a_name;
209904660064SFedor Uporov 	struct uio *a_uio;
210004660064SFedor Uporov 	struct ucred *a_cred;
210104660064SFedor Uporov 	struct thread *a_td;
210204660064SFedor Uporov     };
210304660064SFedor Uporov */
210404660064SFedor Uporov static int
210504660064SFedor Uporov fuse_vnop_setextattr(struct vop_setextattr_args *ap)
210604660064SFedor Uporov {
210704660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
210804660064SFedor Uporov 	struct uio *uio = ap->a_uio;
210928f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
211004660064SFedor Uporov 	struct fuse_setxattr_in *set_xattr_in;
211104660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
211228f4f623SFedor Uporov 	struct thread *td = ap->a_td;
211328f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
211404660064SFedor Uporov 	char *prefix;
211504660064SFedor Uporov 	size_t len;
211604660064SFedor Uporov 	char *attr_str;
211728f4f623SFedor Uporov 	int err;
211804660064SFedor Uporov 
211904660064SFedor Uporov 	if (fuse_isdeadfs(vp))
212028f4f623SFedor Uporov 		return (ENXIO);
212104660064SFedor Uporov 
21221f4a83f9SAlan Somers 	if (!fsess_isimpl(mp, FUSE_SETXATTR))
21231f4a83f9SAlan Somers 		return EOPNOTSUPP;
21241f4a83f9SAlan Somers 
2125666f8543SAlan Somers 	if (vfs_isrdonly(mp))
2126666f8543SAlan Somers 		return EROFS;
2127666f8543SAlan Somers 
2128ff4fbdf5SAlan Somers 	/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
21291f4a83f9SAlan Somers 	if (ap->a_uio == NULL) {
21301f4a83f9SAlan Somers 		/*
21311f4a83f9SAlan Somers 		 * If we got here as fallback from VOP_DELETEEXTATTR, then
21321f4a83f9SAlan Somers 		 * return EOPNOTSUPP.
21331f4a83f9SAlan Somers 		 */
21341f4a83f9SAlan Somers 		if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
21351f4a83f9SAlan Somers 			return (EOPNOTSUPP);
21361f4a83f9SAlan Somers 		else
2137ff4fbdf5SAlan Somers 			return (EINVAL);
21381f4a83f9SAlan Somers 	}
2139ff4fbdf5SAlan Somers 
2140666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2141666f8543SAlan Somers 		VWRITE);
2142ff4fbdf5SAlan Somers 	if (err)
2143ff4fbdf5SAlan Somers 		return err;
2144ff4fbdf5SAlan Somers 
214504660064SFedor Uporov 	/* Default to looking for user attributes. */
214604660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
214704660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
214804660064SFedor Uporov 	else
214904660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
215004660064SFedor Uporov 
215104660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
215204660064SFedor Uporov 	    strlen(ap->a_name) + 1;
215304660064SFedor Uporov 
215404660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
215504660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
215604660064SFedor Uporov 
215704660064SFedor Uporov 	set_xattr_in = fdi.indata;
215804660064SFedor Uporov 	set_xattr_in->size = uio->uio_resid;
215904660064SFedor Uporov 
216004660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
216104660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
216204660064SFedor Uporov 	    ap->a_name);
216304660064SFedor Uporov 
216404660064SFedor Uporov 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
216504660064SFedor Uporov 	    uio->uio_resid, uio);
216604660064SFedor Uporov 	if (err != 0) {
216704660064SFedor Uporov 		goto out;
216804660064SFedor Uporov 	}
216904660064SFedor Uporov 
217004660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
217104660064SFedor Uporov 
21721f4a83f9SAlan Somers 	if (err == ENOSYS) {
217304660064SFedor Uporov 		fsess_set_notimpl(mp, FUSE_SETXATTR);
21741f4a83f9SAlan Somers 		err = EOPNOTSUPP;
217504660064SFedor Uporov 	}
2176f0f7fc1bSAlan Somers 	if (err == ERESTART) {
2177f0f7fc1bSAlan Somers 		/* Can't restart after calling uiomove */
2178f0f7fc1bSAlan Somers 		err = EINTR;
2179f0f7fc1bSAlan Somers 	}
218004660064SFedor Uporov 
218104660064SFedor Uporov out:
218204660064SFedor Uporov 	fdisp_destroy(&fdi);
218304660064SFedor Uporov 	return (err);
218404660064SFedor Uporov }
218504660064SFedor Uporov 
218604660064SFedor Uporov /*
218704660064SFedor Uporov  * The Linux / FUSE extended attribute list is simply a collection of
218804660064SFedor Uporov  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
218904660064SFedor Uporov  * byte length followed by a non-NUL terminated string.  So, this allows
219004660064SFedor Uporov  * conversion of the Linux / FUSE format to the FreeBSD format in place.
219104660064SFedor Uporov  * Linux attribute names are reported with the namespace as a prefix (e.g.
219204660064SFedor Uporov  * "user.attribute_name"), but in FreeBSD they are reported without the
219304660064SFedor Uporov  * namespace prefix (e.g. "attribute_name").  So, we're going from:
219404660064SFedor Uporov  *
219504660064SFedor Uporov  * user.attr_name1\0user.attr_name2\0
219604660064SFedor Uporov  *
219704660064SFedor Uporov  * to:
219804660064SFedor Uporov  *
219904660064SFedor Uporov  * <num>attr_name1<num>attr_name2
220004660064SFedor Uporov  *
220104660064SFedor Uporov  * Where "<num>" is a single byte number of characters in the attribute name.
220204660064SFedor Uporov  *
220304660064SFedor Uporov  * Args:
220404660064SFedor Uporov  * prefix - exattr namespace prefix string
220504660064SFedor Uporov  * list, list_len - input list with namespace prefixes
220604660064SFedor Uporov  * bsd_list, bsd_list_len - output list compatible with bsd vfs
220704660064SFedor Uporov  */
220804660064SFedor Uporov static int
220904660064SFedor Uporov fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
221004660064SFedor Uporov     char *bsd_list, int *bsd_list_len)
221104660064SFedor Uporov {
221204660064SFedor Uporov 	int len, pos, dist_to_next, prefix_len;
221304660064SFedor Uporov 
221404660064SFedor Uporov 	pos = 0;
221504660064SFedor Uporov 	*bsd_list_len = 0;
221604660064SFedor Uporov 	prefix_len = strlen(prefix);
221704660064SFedor Uporov 
221804660064SFedor Uporov 	while (pos < list_len && list[pos] != '\0') {
221904660064SFedor Uporov 		dist_to_next = strlen(&list[pos]) + 1;
222004660064SFedor Uporov 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
222104660064SFedor Uporov 		    list[pos + prefix_len] == extattr_namespace_separator) {
222204660064SFedor Uporov 			len = dist_to_next -
222304660064SFedor Uporov 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
222404660064SFedor Uporov 			if (len >= EXTATTR_MAXNAMELEN)
222504660064SFedor Uporov 				return (ENAMETOOLONG);
222604660064SFedor Uporov 
222704660064SFedor Uporov 			bsd_list[*bsd_list_len] = len;
222804660064SFedor Uporov 			memcpy(&bsd_list[*bsd_list_len + 1],
222904660064SFedor Uporov 			    &list[pos + prefix_len +
223004660064SFedor Uporov 			    sizeof(extattr_namespace_separator)], len);
223104660064SFedor Uporov 
223204660064SFedor Uporov 			*bsd_list_len += len + 1;
223304660064SFedor Uporov 		}
223404660064SFedor Uporov 
223504660064SFedor Uporov 		pos += dist_to_next;
223604660064SFedor Uporov 	}
223704660064SFedor Uporov 
223804660064SFedor Uporov 	return (0);
223904660064SFedor Uporov }
224004660064SFedor Uporov 
224104660064SFedor Uporov /*
224204660064SFedor Uporov     struct vop_listextattr_args {
224304660064SFedor Uporov 	struct vop_generic_args a_gen;
224404660064SFedor Uporov 	struct vnode *a_vp;
224504660064SFedor Uporov 	int a_attrnamespace;
224604660064SFedor Uporov 	struct uio *a_uio;
224704660064SFedor Uporov 	size_t *a_size;
224804660064SFedor Uporov 	struct ucred *a_cred;
224904660064SFedor Uporov 	struct thread *a_td;
225004660064SFedor Uporov     };
225104660064SFedor Uporov */
225204660064SFedor Uporov static int
225304660064SFedor Uporov fuse_vnop_listextattr(struct vop_listextattr_args *ap)
225404660064SFedor Uporov {
225504660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
225604660064SFedor Uporov 	struct uio *uio = ap->a_uio;
225728f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
2258493b4a8cSFedor Uporov 	struct fuse_listxattr_in *list_xattr_in;
2259493b4a8cSFedor Uporov 	struct fuse_listxattr_out *list_xattr_out;
226004660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
226128f4f623SFedor Uporov 	struct thread *td = ap->a_td;
226228f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
226304660064SFedor Uporov 	size_t len;
226404660064SFedor Uporov 	char *prefix;
226504660064SFedor Uporov 	char *attr_str;
226604660064SFedor Uporov 	char *bsd_list = NULL;
2267493b4a8cSFedor Uporov 	char *linux_list;
226804660064SFedor Uporov 	int bsd_list_len;
2269493b4a8cSFedor Uporov 	int linux_list_len;
227028f4f623SFedor Uporov 	int err;
227104660064SFedor Uporov 
227204660064SFedor Uporov 	if (fuse_isdeadfs(vp))
227328f4f623SFedor Uporov 		return (ENXIO);
227404660064SFedor Uporov 
22751f4a83f9SAlan Somers 	if (!fsess_isimpl(mp, FUSE_LISTXATTR))
22761f4a83f9SAlan Somers 		return EOPNOTSUPP;
22771f4a83f9SAlan Somers 
2278666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2279ff4fbdf5SAlan Somers 	if (err)
2280ff4fbdf5SAlan Somers 		return err;
2281ff4fbdf5SAlan Somers 
228204660064SFedor Uporov 	/*
228304660064SFedor Uporov 	 * Add space for a NUL and the period separator if enabled.
228404660064SFedor Uporov 	 * Default to looking for user attributes.
228504660064SFedor Uporov 	 */
228604660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
228704660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
228804660064SFedor Uporov 	else
228904660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
229004660064SFedor Uporov 
229104660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
229204660064SFedor Uporov 
2293493b4a8cSFedor Uporov 	fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
229404660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
229504660064SFedor Uporov 
2296493b4a8cSFedor Uporov 	/*
2297493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list size.
2298493b4a8cSFedor Uporov 	 */
2299493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2300493b4a8cSFedor Uporov 	list_xattr_in->size = 0;
2301493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
230204660064SFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
230304660064SFedor Uporov 
230404660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
230504660064SFedor Uporov 	if (err != 0) {
23061f4a83f9SAlan Somers 		if (err == ENOSYS) {
230704660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
23081f4a83f9SAlan Somers 			err = EOPNOTSUPP;
23091f4a83f9SAlan Somers 		}
231004660064SFedor Uporov 		goto out;
231104660064SFedor Uporov 	}
231204660064SFedor Uporov 
2313493b4a8cSFedor Uporov 	list_xattr_out = fdi.answ;
2314493b4a8cSFedor Uporov 	linux_list_len = list_xattr_out->size;
2315493b4a8cSFedor Uporov 	if (linux_list_len == 0) {
2316493b4a8cSFedor Uporov 		if (ap->a_size != NULL)
2317493b4a8cSFedor Uporov 			*ap->a_size = linux_list_len;
231804660064SFedor Uporov 		goto out;
231904660064SFedor Uporov 	}
232004660064SFedor Uporov 
232104660064SFedor Uporov 	/*
2322493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list values.
2323493b4a8cSFedor Uporov 	 */
232412292a99SAlan Somers 	fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2325493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2326493b4a8cSFedor Uporov 	list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
232712292a99SAlan Somers 	list_xattr_in->flags = 0;
2328493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2329493b4a8cSFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2330493b4a8cSFedor Uporov 
2331493b4a8cSFedor Uporov 	err = fdisp_wait_answ(&fdi);
2332493b4a8cSFedor Uporov 	if (err != 0)
2333493b4a8cSFedor Uporov 		goto out;
2334493b4a8cSFedor Uporov 
2335493b4a8cSFedor Uporov 	linux_list = fdi.answ;
2336493b4a8cSFedor Uporov 	linux_list_len = fdi.iosize;
2337493b4a8cSFedor Uporov 
2338493b4a8cSFedor Uporov 	/*
2339493b4a8cSFedor Uporov 	 * Retrieve the BSD compatible list values.
234004660064SFedor Uporov 	 * The Linux / FUSE attribute list format isn't the same
234104660064SFedor Uporov 	 * as FreeBSD's format. So we need to transform it into
234204660064SFedor Uporov 	 * FreeBSD's format before giving it to the user.
234304660064SFedor Uporov 	 */
2344493b4a8cSFedor Uporov 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2345493b4a8cSFedor Uporov 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
234604660064SFedor Uporov 	    bsd_list, &bsd_list_len);
234704660064SFedor Uporov 	if (err != 0)
234804660064SFedor Uporov 		goto out;
234904660064SFedor Uporov 
2350493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
2351493b4a8cSFedor Uporov 		*ap->a_size = bsd_list_len;
2352493b4a8cSFedor Uporov 
2353493b4a8cSFedor Uporov 	if (uio != NULL)
235404660064SFedor Uporov 		err = uiomove(bsd_list, bsd_list_len, uio);
235504660064SFedor Uporov 
235604660064SFedor Uporov out:
235704660064SFedor Uporov 	free(bsd_list, M_TEMP);
235804660064SFedor Uporov 	fdisp_destroy(&fdi);
235904660064SFedor Uporov 	return (err);
236004660064SFedor Uporov }
236104660064SFedor Uporov 
236204660064SFedor Uporov /*
236304660064SFedor Uporov     struct vop_deleteextattr_args {
236404660064SFedor Uporov 	struct vop_generic_args a_gen;
236504660064SFedor Uporov 	struct vnode *a_vp;
236604660064SFedor Uporov 	int a_attrnamespace;
236704660064SFedor Uporov 	const char *a_name;
236804660064SFedor Uporov 	struct ucred *a_cred;
236904660064SFedor Uporov 	struct thread *a_td;
237004660064SFedor Uporov     };
237104660064SFedor Uporov */
237204660064SFedor Uporov static int
237304660064SFedor Uporov fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
237404660064SFedor Uporov {
237504660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
237628f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
237704660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
237828f4f623SFedor Uporov 	struct thread *td = ap->a_td;
237928f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
238004660064SFedor Uporov 	char *prefix;
238104660064SFedor Uporov 	size_t len;
238204660064SFedor Uporov 	char *attr_str;
238304660064SFedor Uporov 	int err;
238404660064SFedor Uporov 
238504660064SFedor Uporov 	if (fuse_isdeadfs(vp))
238628f4f623SFedor Uporov 		return (ENXIO);
238704660064SFedor Uporov 
23881f4a83f9SAlan Somers 	if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
23891f4a83f9SAlan Somers 		return EOPNOTSUPP;
23901f4a83f9SAlan Somers 
2391666f8543SAlan Somers 	if (vfs_isrdonly(mp))
2392666f8543SAlan Somers 		return EROFS;
2393666f8543SAlan Somers 
2394666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2395666f8543SAlan Somers 		VWRITE);
2396ff4fbdf5SAlan Somers 	if (err)
2397ff4fbdf5SAlan Somers 		return err;
2398ff4fbdf5SAlan Somers 
239904660064SFedor Uporov 	/* Default to looking for user attributes. */
240004660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
240104660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
240204660064SFedor Uporov 	else
240304660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
240404660064SFedor Uporov 
240504660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
240604660064SFedor Uporov 	    strlen(ap->a_name) + 1;
240704660064SFedor Uporov 
240804660064SFedor Uporov 	fdisp_init(&fdi, len);
240904660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
241004660064SFedor Uporov 
241104660064SFedor Uporov 	attr_str = fdi.indata;
241204660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
241304660064SFedor Uporov 	    ap->a_name);
241404660064SFedor Uporov 
241504660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
24161f4a83f9SAlan Somers 	if (err == ENOSYS) {
241704660064SFedor Uporov 		fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
24181f4a83f9SAlan Somers 		err = EOPNOTSUPP;
241904660064SFedor Uporov 	}
242004660064SFedor Uporov 
242104660064SFedor Uporov 	fdisp_destroy(&fdi);
242204660064SFedor Uporov 	return (err);
242304660064SFedor Uporov }
242404660064SFedor Uporov 
24255fe58019SAttilio Rao /*
24265fe58019SAttilio Rao     struct vnop_print_args {
24275fe58019SAttilio Rao 	struct vnode *a_vp;
24285fe58019SAttilio Rao     };
24295fe58019SAttilio Rao */
24305fe58019SAttilio Rao static int
24315fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap)
24325fe58019SAttilio Rao {
24335fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
24345fe58019SAttilio Rao 
24355fe58019SAttilio Rao 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
24365fe58019SAttilio Rao 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
24375fe58019SAttilio Rao 	    (uintmax_t)fvdat->nlookup,
24385fe58019SAttilio Rao 	    fvdat->flag);
24395fe58019SAttilio Rao 
24405fe58019SAttilio Rao 	return 0;
24415fe58019SAttilio Rao }
2442