xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision ef1534cad8a085946105e135d2d75e275011ec4b)
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  *
368aafc8c3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
378aafc8c3SAlan Somers  *
388aafc8c3SAlan Somers  * Portions of this software were developed by BFF Storage Systems, LLC under
398aafc8c3SAlan Somers  * sponsorship from the FreeBSD Foundation.
408aafc8c3SAlan Somers  *
415fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
425fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
435fe58019SAttilio Rao  * are met:
445fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
455fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
465fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
475fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
485fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
495fe58019SAttilio Rao  *
505fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
515fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
545fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605fe58019SAttilio Rao  * SUCH DAMAGE.
615fe58019SAttilio Rao  */
625fe58019SAttilio Rao 
635fe58019SAttilio Rao #include <sys/cdefs.h>
645fe58019SAttilio Rao __FBSDID("$FreeBSD$");
655fe58019SAttilio Rao 
66cf169498SAlan Somers #include <sys/param.h>
675fe58019SAttilio Rao #include <sys/module.h>
685fe58019SAttilio Rao #include <sys/systm.h>
695fe58019SAttilio Rao #include <sys/errno.h>
705fe58019SAttilio Rao #include <sys/kernel.h>
715fe58019SAttilio Rao #include <sys/conf.h>
7237df9d3bSAlan Somers #include <sys/filio.h>
735fe58019SAttilio Rao #include <sys/uio.h>
745fe58019SAttilio Rao #include <sys/malloc.h>
755fe58019SAttilio Rao #include <sys/queue.h>
7691898857SMark Johnston #include <sys/limits.h>
775fe58019SAttilio Rao #include <sys/lock.h>
7889f6b863SAttilio Rao #include <sys/rwlock.h>
795fe58019SAttilio Rao #include <sys/sx.h>
805fe58019SAttilio Rao #include <sys/proc.h>
815fe58019SAttilio Rao #include <sys/mount.h>
825fe58019SAttilio Rao #include <sys/vnode.h>
835fe58019SAttilio Rao #include <sys/namei.h>
8404660064SFedor Uporov #include <sys/extattr.h>
855fe58019SAttilio Rao #include <sys/stat.h>
865fe58019SAttilio Rao #include <sys/unistd.h>
875fe58019SAttilio Rao #include <sys/filedesc.h>
885fe58019SAttilio Rao #include <sys/file.h>
895fe58019SAttilio Rao #include <sys/fcntl.h>
905fe58019SAttilio Rao #include <sys/dirent.h>
915fe58019SAttilio Rao #include <sys/bio.h>
925fe58019SAttilio Rao #include <sys/buf.h>
935fe58019SAttilio Rao #include <sys/sysctl.h>
94ca148cdaSGleb Smirnoff #include <sys/vmmeter.h>
955fe58019SAttilio Rao 
965fe58019SAttilio Rao #include <vm/vm.h>
975fe58019SAttilio Rao #include <vm/vm_extern.h>
985fe58019SAttilio Rao #include <vm/pmap.h>
995fe58019SAttilio Rao #include <vm/vm_map.h>
1005fe58019SAttilio Rao #include <vm/vm_page.h>
1015fe58019SAttilio Rao #include <vm/vm_param.h>
1025fe58019SAttilio Rao #include <vm/vm_object.h>
1035fe58019SAttilio Rao #include <vm/vm_pager.h>
1045fe58019SAttilio Rao #include <vm/vnode_pager.h>
1055fe58019SAttilio Rao #include <vm/vm_object.h>
1065fe58019SAttilio Rao 
1075fe58019SAttilio Rao #include "fuse.h"
1085fe58019SAttilio Rao #include "fuse_file.h"
1095fe58019SAttilio Rao #include "fuse_internal.h"
1105fe58019SAttilio Rao #include "fuse_ipc.h"
1115fe58019SAttilio Rao #include "fuse_node.h"
1125fe58019SAttilio Rao #include "fuse_io.h"
1135fe58019SAttilio Rao 
1145fe58019SAttilio Rao #include <sys/priv.h>
1155fe58019SAttilio Rao 
116fd2749f2SAlan Somers /* Maximum number of hardlinks to a single FUSE file */
117fd2749f2SAlan Somers #define FUSE_LINK_MAX                      UINT32_MAX
118fd2749f2SAlan Somers 
119419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs);
120cf169498SAlan Somers /*
121cf169498SAlan Somers  * Fuse trace probe:
122cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
123cf169498SAlan Somers  * arg1: Textual message
124cf169498SAlan Somers  */
125419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
1265fe58019SAttilio Rao 
1275fe58019SAttilio Rao /* vnode ops */
1285fe58019SAttilio Rao static vop_access_t fuse_vnop_access;
129f067b609SAlan Somers static vop_advlock_t fuse_vnop_advlock;
130398c88c7SAlan Somers static vop_allocate_t fuse_vnop_allocate;
131a1c9f4adSAlan Somers static vop_bmap_t fuse_vnop_bmap;
132f9b0e30bSAlan Somers static vop_close_t fuse_fifo_close;
1335fe58019SAttilio Rao static vop_close_t fuse_vnop_close;
13492bbfe1fSAlan Somers static vop_copy_file_range_t fuse_vnop_copy_file_range;
1355fe58019SAttilio Rao static vop_create_t fuse_vnop_create;
13689d57b94SAlan Somers static vop_deallocate_t fuse_vnop_deallocate;
13704660064SFedor Uporov static vop_deleteextattr_t fuse_vnop_deleteextattr;
138915012e0SAlan Somers static vop_fdatasync_t fuse_vnop_fdatasync;
1395fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync;
1405fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr;
14104660064SFedor Uporov static vop_getextattr_t fuse_vnop_getextattr;
1425fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive;
14337df9d3bSAlan Somers static vop_ioctl_t fuse_vnop_ioctl;
1445fe58019SAttilio Rao static vop_link_t fuse_vnop_link;
14504660064SFedor Uporov static vop_listextattr_t fuse_vnop_listextattr;
1465fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup;
1475fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir;
1485fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod;
1495fe58019SAttilio Rao static vop_open_t fuse_vnop_open;
150746c92e0SJohn Baldwin static vop_pathconf_t fuse_vnop_pathconf;
1515fe58019SAttilio Rao static vop_read_t fuse_vnop_read;
1525fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir;
1535fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink;
1545fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim;
1555fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove;
1565fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename;
1575fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir;
1585fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr;
15904660064SFedor Uporov static vop_setextattr_t fuse_vnop_setextattr;
1605fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy;
1615fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink;
1625fe58019SAttilio Rao static vop_write_t fuse_vnop_write;
1635fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages;
1645fe58019SAttilio Rao static vop_print_t fuse_vnop_print;
165e5b50fe7SAlan Somers static vop_vptofh_t fuse_vnop_vptofh;
1665fe58019SAttilio Rao 
167f9b0e30bSAlan Somers struct vop_vector fuse_fifoops = {
168f9b0e30bSAlan Somers 	.vop_default =		&fifo_specops,
169f9b0e30bSAlan Somers 	.vop_access =		fuse_vnop_access,
170f9b0e30bSAlan Somers 	.vop_close =		fuse_fifo_close,
171f9b0e30bSAlan Somers 	.vop_fsync =		fuse_vnop_fsync,
172f9b0e30bSAlan Somers 	.vop_getattr =		fuse_vnop_getattr,
173f9b0e30bSAlan Somers 	.vop_inactive =		fuse_vnop_inactive,
174f9b0e30bSAlan Somers 	.vop_pathconf =		fuse_vnop_pathconf,
175f9b0e30bSAlan Somers 	.vop_print =		fuse_vnop_print,
176f9b0e30bSAlan Somers 	.vop_read =		VOP_PANIC,
177f9b0e30bSAlan Somers 	.vop_reclaim =		fuse_vnop_reclaim,
178f9b0e30bSAlan Somers 	.vop_setattr =		fuse_vnop_setattr,
179f9b0e30bSAlan Somers 	.vop_write =		VOP_PANIC,
180e5b50fe7SAlan Somers 	.vop_vptofh =		fuse_vnop_vptofh,
181f9b0e30bSAlan Somers };
1826fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(fuse_fifoops);
183f9b0e30bSAlan Somers 
1845fe58019SAttilio Rao struct vop_vector fuse_vnops = {
185398c88c7SAlan Somers 	.vop_allocate =	fuse_vnop_allocate,
1865fe58019SAttilio Rao 	.vop_default = &default_vnodeops,
1875fe58019SAttilio Rao 	.vop_access = fuse_vnop_access,
188f067b609SAlan Somers 	.vop_advlock = fuse_vnop_advlock,
189a1c9f4adSAlan Somers 	.vop_bmap = fuse_vnop_bmap,
1905fe58019SAttilio Rao 	.vop_close = fuse_vnop_close,
19192bbfe1fSAlan Somers 	.vop_copy_file_range = fuse_vnop_copy_file_range,
1925fe58019SAttilio Rao 	.vop_create = fuse_vnop_create,
19389d57b94SAlan Somers 	.vop_deallocate = fuse_vnop_deallocate,
19404660064SFedor Uporov 	.vop_deleteextattr = fuse_vnop_deleteextattr,
1955fe58019SAttilio Rao 	.vop_fsync = fuse_vnop_fsync,
196915012e0SAlan Somers 	.vop_fdatasync = fuse_vnop_fdatasync,
1975fe58019SAttilio Rao 	.vop_getattr = fuse_vnop_getattr,
19804660064SFedor Uporov 	.vop_getextattr = fuse_vnop_getextattr,
1995fe58019SAttilio Rao 	.vop_inactive = fuse_vnop_inactive,
20037df9d3bSAlan Somers 	.vop_ioctl = fuse_vnop_ioctl,
2015fe58019SAttilio Rao 	.vop_link = fuse_vnop_link,
20204660064SFedor Uporov 	.vop_listextattr = fuse_vnop_listextattr,
2035fe58019SAttilio Rao 	.vop_lookup = fuse_vnop_lookup,
2045fe58019SAttilio Rao 	.vop_mkdir = fuse_vnop_mkdir,
2055fe58019SAttilio Rao 	.vop_mknod = fuse_vnop_mknod,
2065fe58019SAttilio Rao 	.vop_open = fuse_vnop_open,
207746c92e0SJohn Baldwin 	.vop_pathconf = fuse_vnop_pathconf,
208e039bafaSAlan Somers 	/*
209e039bafaSAlan Somers 	 * TODO: implement vop_poll after upgrading to protocol 7.21.
210e039bafaSAlan Somers 	 * FUSE_POLL was added in protocol 7.11, but it's kind of broken until
211e039bafaSAlan Somers 	 * 7.21, which adds the ability for the client to choose which poll
212e039bafaSAlan Somers 	 * events it wants, and for a client to deregister a file handle
213e039bafaSAlan Somers 	 */
2145fe58019SAttilio Rao 	.vop_read = fuse_vnop_read,
2155fe58019SAttilio Rao 	.vop_readdir = fuse_vnop_readdir,
2165fe58019SAttilio Rao 	.vop_readlink = fuse_vnop_readlink,
2175fe58019SAttilio Rao 	.vop_reclaim = fuse_vnop_reclaim,
2185fe58019SAttilio Rao 	.vop_remove = fuse_vnop_remove,
2195fe58019SAttilio Rao 	.vop_rename = fuse_vnop_rename,
2205fe58019SAttilio Rao 	.vop_rmdir = fuse_vnop_rmdir,
2215fe58019SAttilio Rao 	.vop_setattr = fuse_vnop_setattr,
22204660064SFedor Uporov 	.vop_setextattr = fuse_vnop_setextattr,
2235fe58019SAttilio Rao 	.vop_strategy = fuse_vnop_strategy,
2245fe58019SAttilio Rao 	.vop_symlink = fuse_vnop_symlink,
2255fe58019SAttilio Rao 	.vop_write = fuse_vnop_write,
2265fe58019SAttilio Rao 	.vop_getpages = fuse_vnop_getpages,
2275fe58019SAttilio Rao 	.vop_print = fuse_vnop_print,
228e5b50fe7SAlan Somers 	.vop_vptofh = fuse_vnop_vptofh,
2295fe58019SAttilio Rao };
2306fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(fuse_vnops);
2315fe58019SAttilio Rao 
232756a5412SGleb Smirnoff uma_zone_t fuse_pbuf_zone;
2335fe58019SAttilio Rao 
234666f8543SAlan Somers /* Check permission for extattr operations, much like extattr_check_cred */
235666f8543SAlan Somers static int
236666f8543SAlan Somers fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
237666f8543SAlan Somers 	struct thread *td, accmode_t accmode)
238666f8543SAlan Somers {
239666f8543SAlan Somers 	struct mount *mp = vnode_mount(vp);
240666f8543SAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
241bfcb817bSAlan Somers 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
242666f8543SAlan Somers 
243666f8543SAlan Somers 	/*
244666f8543SAlan Somers 	 * Kernel-invoked always succeeds.
245666f8543SAlan Somers 	 */
246666f8543SAlan Somers 	if (cred == NOCRED)
247666f8543SAlan Somers 		return (0);
248666f8543SAlan Somers 
249666f8543SAlan Somers 	/*
250666f8543SAlan Somers 	 * Do not allow privileged processes in jail to directly manipulate
251666f8543SAlan Somers 	 * system attributes.
252666f8543SAlan Somers 	 */
253666f8543SAlan Somers 	switch (ns) {
254666f8543SAlan Somers 	case EXTATTR_NAMESPACE_SYSTEM:
255bfcb817bSAlan Somers 		if (default_permissions) {
256666f8543SAlan Somers 			return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
257666f8543SAlan Somers 		}
258bfcb817bSAlan Somers 		return (0);
259666f8543SAlan Somers 	case EXTATTR_NAMESPACE_USER:
260bfcb817bSAlan Somers 		if (default_permissions) {
261666f8543SAlan Somers 			return (fuse_internal_access(vp, accmode, td, cred));
262bfcb817bSAlan Somers 		}
263bfcb817bSAlan Somers 		return (0);
264666f8543SAlan Somers 	default:
265666f8543SAlan Somers 		return (EPERM);
266666f8543SAlan Somers 	}
267666f8543SAlan Somers }
268666f8543SAlan Somers 
269f8d4af10SAlan Somers /* Get a filehandle for a directory */
270f8d4af10SAlan Somers static int
271f8d4af10SAlan Somers fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
272f8d4af10SAlan Somers 	struct ucred *cred, pid_t pid)
273f8d4af10SAlan Somers {
2749f10f423SAlan Somers 	if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
275f8d4af10SAlan Somers 		return 0;
2769f10f423SAlan Somers 	return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
2779f10f423SAlan Somers }
2789f10f423SAlan Somers 
2799f10f423SAlan Somers /* Send FUSE_FLUSH for this vnode */
2809f10f423SAlan Somers static int
2819f10f423SAlan Somers fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
2829f10f423SAlan Somers {
2839f10f423SAlan Somers 	struct fuse_flush_in *ffi;
2849f10f423SAlan Somers 	struct fuse_filehandle *fufh;
2859f10f423SAlan Somers 	struct fuse_dispatcher fdi;
2869f10f423SAlan Somers 	struct thread *td = curthread;
2879f10f423SAlan Somers 	struct mount *mp = vnode_mount(vp);
2889f10f423SAlan Somers 	int err;
2899f10f423SAlan Somers 
29037df9d3bSAlan Somers 	if (fsess_not_impl(vnode_mount(vp), FUSE_FLUSH))
2919f10f423SAlan Somers 		return 0;
2929f10f423SAlan Somers 
293f067b609SAlan Somers 	err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
2949f10f423SAlan Somers 	if (err)
2959f10f423SAlan Somers 		return err;
2969f10f423SAlan Somers 
2979f10f423SAlan Somers 	fdisp_init(&fdi, sizeof(*ffi));
2989f10f423SAlan Somers 	fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
2999f10f423SAlan Somers 	ffi = fdi.indata;
3009f10f423SAlan Somers 	ffi->fh = fufh->fh_id;
301f067b609SAlan Somers 	/*
302f067b609SAlan Somers 	 * If the file has a POSIX lock then we're supposed to set lock_owner.
303f067b609SAlan Somers 	 * If not, then lock_owner is undefined.  So we may as well always set
304f067b609SAlan Somers 	 * it.
305f067b609SAlan Somers 	 */
306f067b609SAlan Somers 	ffi->lock_owner = td->td_proc->p_pid;
3079f10f423SAlan Somers 
3089f10f423SAlan Somers 	err = fdisp_wait_answ(&fdi);
3099f10f423SAlan Somers 	if (err == ENOSYS) {
3109f10f423SAlan Somers 		fsess_set_notimpl(mp, FUSE_FLUSH);
3119f10f423SAlan Somers 		err = 0;
3129f10f423SAlan Somers 	}
3139f10f423SAlan Somers 	fdisp_destroy(&fdi);
3149f10f423SAlan Somers 	return err;
315f8d4af10SAlan Somers }
316f8d4af10SAlan Somers 
317f9b0e30bSAlan Somers /* Close wrapper for fifos.  */
318f9b0e30bSAlan Somers static int
319f9b0e30bSAlan Somers fuse_fifo_close(struct vop_close_args *ap)
320f9b0e30bSAlan Somers {
321f9b0e30bSAlan Somers 	return (fifo_specops.vop_close(ap));
322f9b0e30bSAlan Somers }
323f9b0e30bSAlan Somers 
324dc433e15SAlan Somers /* Invalidate a range of cached data, whether dirty of not */
325dc433e15SAlan Somers static int
326dc433e15SAlan Somers fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end)
327dc433e15SAlan Somers {
328dc433e15SAlan Somers 	struct buf *bp;
329dc433e15SAlan Somers 	daddr_t left_lbn, end_lbn, right_lbn;
330dc433e15SAlan Somers 	off_t new_filesize;
331dc433e15SAlan Somers 	int iosize, left_on, right_on, right_blksize;
332dc433e15SAlan Somers 
333dc433e15SAlan Somers 	iosize = fuse_iosize(vp);
334dc433e15SAlan Somers 	left_lbn = start / iosize;
335dc433e15SAlan Somers 	end_lbn = howmany(end, iosize);
336dc433e15SAlan Somers 	left_on = start & (iosize - 1);
337dc433e15SAlan Somers 	if (left_on != 0) {
338dc433e15SAlan Somers 		bp = getblk(vp, left_lbn, iosize, PCATCH, 0, 0);
339dc433e15SAlan Somers 		if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyend >= left_on) {
340dc433e15SAlan Somers 			/*
341dc433e15SAlan Somers 			 * Flush the dirty buffer, because we don't have a
342dc433e15SAlan Somers 			 * byte-granular way to record which parts of the
343dc433e15SAlan Somers 			 * buffer are valid.
344dc433e15SAlan Somers 			 */
345dc433e15SAlan Somers 			bwrite(bp);
346dc433e15SAlan Somers 			if (bp->b_error)
347dc433e15SAlan Somers 				return (bp->b_error);
348dc433e15SAlan Somers 		} else {
349dc433e15SAlan Somers 			brelse(bp);
350dc433e15SAlan Somers 		}
351dc433e15SAlan Somers 	}
352dc433e15SAlan Somers 	right_on = end & (iosize - 1);
353dc433e15SAlan Somers 	if (right_on != 0) {
354dc433e15SAlan Somers 		right_lbn = end / iosize;
355dc433e15SAlan Somers 		new_filesize = MAX(filesize, end);
356dc433e15SAlan Somers 		right_blksize = MIN(iosize, new_filesize - iosize * right_lbn);
357dc433e15SAlan Somers 		bp = getblk(vp, right_lbn, right_blksize, PCATCH, 0, 0);
358dc433e15SAlan Somers 		if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyoff < right_on) {
359dc433e15SAlan Somers 			/*
360dc433e15SAlan Somers 			 * Flush the dirty buffer, because we don't have a
361dc433e15SAlan Somers 			 * byte-granular way to record which parts of the
362dc433e15SAlan Somers 			 * buffer are valid.
363dc433e15SAlan Somers 			 */
364dc433e15SAlan Somers 			bwrite(bp);
365dc433e15SAlan Somers 			if (bp->b_error)
366dc433e15SAlan Somers 				return (bp->b_error);
367dc433e15SAlan Somers 		} else {
368dc433e15SAlan Somers 			brelse(bp);
369dc433e15SAlan Somers 		}
370dc433e15SAlan Somers 	}
371dc433e15SAlan Somers 
372dc433e15SAlan Somers 	v_inval_buf_range(vp, left_lbn, end_lbn, iosize);
373dc433e15SAlan Somers 	return (0);
374dc433e15SAlan Somers }
375dc433e15SAlan Somers 
376dc433e15SAlan Somers 
37737df9d3bSAlan Somers /* Send FUSE_LSEEK for this node */
37837df9d3bSAlan Somers static int
37937df9d3bSAlan Somers fuse_vnop_do_lseek(struct vnode *vp, struct thread *td, struct ucred *cred,
38037df9d3bSAlan Somers 	pid_t pid, off_t *offp, int whence)
38137df9d3bSAlan Somers {
38237df9d3bSAlan Somers 	struct fuse_dispatcher fdi;
38337df9d3bSAlan Somers 	struct fuse_filehandle *fufh;
38437df9d3bSAlan Somers 	struct fuse_lseek_in *flsi;
38537df9d3bSAlan Somers 	struct fuse_lseek_out *flso;
38637df9d3bSAlan Somers 	struct mount *mp = vnode_mount(vp);
38737df9d3bSAlan Somers 	int err;
38837df9d3bSAlan Somers 
38934477e25SAlan Somers 	ASSERT_VOP_LOCKED(vp, __func__);
39037df9d3bSAlan Somers 
39137df9d3bSAlan Somers 	err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
39237df9d3bSAlan Somers 	if (err)
39337df9d3bSAlan Somers 		return (err);
39437df9d3bSAlan Somers 	fdisp_init(&fdi, sizeof(*flsi));
39537df9d3bSAlan Somers 	fdisp_make_vp(&fdi, FUSE_LSEEK, vp, td, cred);
39637df9d3bSAlan Somers 	flsi = fdi.indata;
39737df9d3bSAlan Somers 	flsi->fh = fufh->fh_id;
39837df9d3bSAlan Somers 	flsi->offset = *offp;
39937df9d3bSAlan Somers 	flsi->whence = whence;
40037df9d3bSAlan Somers 	err = fdisp_wait_answ(&fdi);
40137df9d3bSAlan Somers 	if (err == ENOSYS) {
40237df9d3bSAlan Somers 		fsess_set_notimpl(mp, FUSE_LSEEK);
40337df9d3bSAlan Somers 	} else if (err == 0) {
40437df9d3bSAlan Somers 		fsess_set_impl(mp, FUSE_LSEEK);
40537df9d3bSAlan Somers 		flso = fdi.answ;
40637df9d3bSAlan Somers 		*offp = flso->offset;
40737df9d3bSAlan Somers 	}
40837df9d3bSAlan Somers 	fdisp_destroy(&fdi);
40937df9d3bSAlan Somers 
41037df9d3bSAlan Somers 	return (err);
41137df9d3bSAlan Somers }
41237df9d3bSAlan Somers 
4135fe58019SAttilio Rao /*
4145fe58019SAttilio Rao     struct vnop_access_args {
4155fe58019SAttilio Rao 	struct vnode *a_vp;
4165fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T
4175fe58019SAttilio Rao 	accmode_t a_accmode;
4185fe58019SAttilio Rao #else
4195fe58019SAttilio Rao 	int a_mode;
4205fe58019SAttilio Rao #endif
4215fe58019SAttilio Rao 	struct ucred *a_cred;
4225fe58019SAttilio Rao 	struct thread *a_td;
4235fe58019SAttilio Rao     };
4245fe58019SAttilio Rao */
4255fe58019SAttilio Rao static int
4265fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap)
4275fe58019SAttilio Rao {
4285fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4295fe58019SAttilio Rao 	int accmode = ap->a_accmode;
4305fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
4315fe58019SAttilio Rao 
4325fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
4335fe58019SAttilio Rao 
4345fe58019SAttilio Rao 	int err;
4355fe58019SAttilio Rao 
4365fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
4375fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
4385fe58019SAttilio Rao 			return 0;
4395fe58019SAttilio Rao 		}
4405fe58019SAttilio Rao 		return ENXIO;
4415fe58019SAttilio Rao 	}
4425fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
4435fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
444cc426dd3SMateusz Guzik 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
4455fe58019SAttilio Rao 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
4465fe58019SAttilio Rao 				return 0;
4475fe58019SAttilio Rao 			}
4485fe58019SAttilio Rao 		}
4495fe58019SAttilio Rao 		return EBADF;
4505fe58019SAttilio Rao 	}
4515fe58019SAttilio Rao 	if (vnode_islnk(vp)) {
4525fe58019SAttilio Rao 		return 0;
4535fe58019SAttilio Rao 	}
4545fe58019SAttilio Rao 
455666f8543SAlan Somers 	err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
4565fe58019SAttilio Rao 	return err;
4575fe58019SAttilio Rao }
4585fe58019SAttilio Rao 
4595fe58019SAttilio Rao /*
460f067b609SAlan Somers  * struct vop_advlock_args {
461f067b609SAlan Somers  *	struct vop_generic_args a_gen;
462f067b609SAlan Somers  *	struct vnode *a_vp;
463f067b609SAlan Somers  *	void *a_id;
464f067b609SAlan Somers  *	int a_op;
465f067b609SAlan Somers  *	struct flock *a_fl;
466f067b609SAlan Somers  *	int a_flags;
467f067b609SAlan Somers  * }
468f067b609SAlan Somers  */
469f067b609SAlan Somers static int
470f067b609SAlan Somers fuse_vnop_advlock(struct vop_advlock_args *ap)
471f067b609SAlan Somers {
472f067b609SAlan Somers 	struct vnode *vp = ap->a_vp;
473f067b609SAlan Somers 	struct flock *fl = ap->a_fl;
474f067b609SAlan Somers 	struct thread *td = curthread;
475f067b609SAlan Somers 	struct ucred *cred = td->td_ucred;
476f067b609SAlan Somers 	pid_t pid = td->td_proc->p_pid;
477f067b609SAlan Somers 	struct fuse_filehandle *fufh;
478f067b609SAlan Somers 	struct fuse_dispatcher fdi;
479f067b609SAlan Somers 	struct fuse_lk_in *fli;
480f067b609SAlan Somers 	struct fuse_lk_out *flo;
481f067b609SAlan Somers 	enum fuse_opcode op;
482f067b609SAlan Somers 	int dataflags, err;
4838aa24ed3SAlan Somers 	int flags = ap->a_flags;
484f067b609SAlan Somers 
485f067b609SAlan Somers 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
486f067b609SAlan Somers 
487f067b609SAlan Somers 	if (fuse_isdeadfs(vp)) {
488f067b609SAlan Somers 		return ENXIO;
489f067b609SAlan Somers 	}
490f067b609SAlan Somers 
491f067b609SAlan Somers 	switch(ap->a_op) {
492f067b609SAlan Somers 	case F_GETLK:
493f067b609SAlan Somers 		op = FUSE_GETLK;
494f067b609SAlan Somers 		break;
495f067b609SAlan Somers 	case F_SETLK:
496929acdb1SAlan Somers 		if (flags & F_WAIT)
497929acdb1SAlan Somers 			op = FUSE_SETLKW;
498929acdb1SAlan Somers 		else
499f067b609SAlan Somers 			op = FUSE_SETLK;
500f067b609SAlan Somers 		break;
501929acdb1SAlan Somers 	case F_UNLCK:
502929acdb1SAlan Somers 		op = FUSE_SETLK;
503f067b609SAlan Somers 		break;
504f067b609SAlan Somers 	default:
505f067b609SAlan Somers 		return EINVAL;
506f067b609SAlan Somers 	}
507f067b609SAlan Somers 
508542711e5SAlan Somers 	if (!(dataflags & FSESS_POSIX_LOCKS))
509542711e5SAlan Somers 		return vop_stdadvlock(ap);
510542711e5SAlan Somers 	/* FUSE doesn't properly support flock until protocol 7.17 */
511542711e5SAlan Somers 	if (flags & F_FLOCK)
512542711e5SAlan Somers 		return vop_stdadvlock(ap);
513542711e5SAlan Somers 
514542711e5SAlan Somers 	vn_lock(vp, LK_SHARED | LK_RETRY);
515542711e5SAlan Somers 
516542711e5SAlan Somers 	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
517542711e5SAlan Somers 	if (err)
518542711e5SAlan Somers 		goto out;
519542711e5SAlan Somers 
520542711e5SAlan Somers 	fdisp_init(&fdi, sizeof(*fli));
521542711e5SAlan Somers 
522f067b609SAlan Somers 	fdisp_make_vp(&fdi, op, vp, td, cred);
523f067b609SAlan Somers 	fli = fdi.indata;
524f067b609SAlan Somers 	fli->fh = fufh->fh_id;
52518b19f8cSAlan Somers 	fli->owner = td->td_proc->p_pid;
526f067b609SAlan Somers 	fli->lk.start = fl->l_start;
527f067b609SAlan Somers 	if (fl->l_len != 0)
528f067b609SAlan Somers 		fli->lk.end = fl->l_start + fl->l_len - 1;
529f067b609SAlan Somers 	else
530f067b609SAlan Somers 		fli->lk.end = INT64_MAX;
531f067b609SAlan Somers 	fli->lk.type = fl->l_type;
53218b19f8cSAlan Somers 	fli->lk.pid = td->td_proc->p_pid;
533f067b609SAlan Somers 
534f067b609SAlan Somers 	err = fdisp_wait_answ(&fdi);
535f067b609SAlan Somers 	fdisp_destroy(&fdi);
536f067b609SAlan Somers 
537f067b609SAlan Somers 	if (err == 0 && op == FUSE_GETLK) {
538f067b609SAlan Somers 		flo = fdi.answ;
539f067b609SAlan Somers 		fl->l_type = flo->lk.type;
540f067b609SAlan Somers 		fl->l_pid = flo->lk.pid;
541f067b609SAlan Somers 		if (flo->lk.type != F_UNLCK) {
542f067b609SAlan Somers 			fl->l_start = flo->lk.start;
543f067b609SAlan Somers 			if (flo->lk.end == INT64_MAX)
544f067b609SAlan Somers 				fl->l_len = 0;
545f067b609SAlan Somers 			else
546f067b609SAlan Somers 				fl->l_len = flo->lk.end - flo->lk.start + 1;
547f067b609SAlan Somers 			fl->l_start = flo->lk.start;
548f067b609SAlan Somers 		}
549f067b609SAlan Somers 	}
550f067b609SAlan Somers 
551542711e5SAlan Somers out:
552542711e5SAlan Somers 	VOP_UNLOCK(vp);
553f067b609SAlan Somers 	return err;
554f067b609SAlan Somers }
555f067b609SAlan Somers 
556398c88c7SAlan Somers static int
557398c88c7SAlan Somers fuse_vnop_allocate(struct vop_allocate_args *ap)
558398c88c7SAlan Somers {
559398c88c7SAlan Somers 	struct vnode *vp = ap->a_vp;
560398c88c7SAlan Somers 	off_t *len = ap->a_len;
561398c88c7SAlan Somers 	off_t *offset = ap->a_offset;
562398c88c7SAlan Somers 	struct ucred *cred = ap->a_cred;
563398c88c7SAlan Somers 	struct fuse_filehandle *fufh;
564398c88c7SAlan Somers 	struct mount *mp = vnode_mount(vp);
565398c88c7SAlan Somers 	struct fuse_dispatcher fdi;
566398c88c7SAlan Somers 	struct fuse_fallocate_in *ffi;
567398c88c7SAlan Somers 	struct uio io;
568398c88c7SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
569398c88c7SAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
570398c88c7SAlan Somers 	off_t filesize;
571398c88c7SAlan Somers 	int err;
572398c88c7SAlan Somers 
573398c88c7SAlan Somers 	if (fuse_isdeadfs(vp))
574398c88c7SAlan Somers 		return (ENXIO);
575398c88c7SAlan Somers 
576398c88c7SAlan Somers 	switch (vp->v_type) {
577398c88c7SAlan Somers 	case VFIFO:
578398c88c7SAlan Somers 		return (ESPIPE);
579398c88c7SAlan Somers 	case VLNK:
580398c88c7SAlan Somers 	case VREG:
581398c88c7SAlan Somers 		if (vfs_isrdonly(mp))
582398c88c7SAlan Somers 			return (EROFS);
583398c88c7SAlan Somers 		break;
584398c88c7SAlan Somers 	default:
585398c88c7SAlan Somers 		return (ENODEV);
586398c88c7SAlan Somers 	}
587398c88c7SAlan Somers 
588398c88c7SAlan Somers 	if (vfs_isrdonly(mp))
589398c88c7SAlan Somers 		return (EROFS);
590398c88c7SAlan Somers 
591398c88c7SAlan Somers 	if (fsess_not_impl(mp, FUSE_FALLOCATE))
592398c88c7SAlan Somers 		return (EINVAL);
593398c88c7SAlan Somers 
594398c88c7SAlan Somers 	io.uio_offset = *offset;
595398c88c7SAlan Somers 	io.uio_resid = *len;
596398c88c7SAlan Somers 	err = vn_rlimit_fsize(vp, &io, curthread);
597398c88c7SAlan Somers 	if (err)
598398c88c7SAlan Somers 		return (err);
599398c88c7SAlan Somers 
600398c88c7SAlan Somers 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
601398c88c7SAlan Somers 	if (err)
602398c88c7SAlan Somers 		return (err);
603398c88c7SAlan Somers 
604398c88c7SAlan Somers 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
605398c88c7SAlan Somers 
606398c88c7SAlan Somers 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
607398c88c7SAlan Somers 	if (err)
608398c88c7SAlan Somers 		return (err);
609398c88c7SAlan Somers 	fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
610398c88c7SAlan Somers 
611398c88c7SAlan Somers 	fdisp_init(&fdi, sizeof(*ffi));
612398c88c7SAlan Somers 	fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
613398c88c7SAlan Somers 	ffi = fdi.indata;
614398c88c7SAlan Somers 	ffi->fh = fufh->fh_id;
615398c88c7SAlan Somers 	ffi->offset = *offset;
616398c88c7SAlan Somers 	ffi->length = *len;
617398c88c7SAlan Somers 	ffi->mode = 0;
618398c88c7SAlan Somers 	err = fdisp_wait_answ(&fdi);
619398c88c7SAlan Somers 
620398c88c7SAlan Somers 	if (err == ENOSYS) {
621398c88c7SAlan Somers 		fsess_set_notimpl(mp, FUSE_FALLOCATE);
622398c88c7SAlan Somers 		err = EINVAL;
623398c88c7SAlan Somers 	} else if (err == EOPNOTSUPP) {
624398c88c7SAlan Somers 		/*
625398c88c7SAlan Somers 		 * The file system server does not support FUSE_FALLOCATE with
62689d57b94SAlan Somers 		 * the supplied mode for this particular file.
627398c88c7SAlan Somers 		 */
628398c88c7SAlan Somers 		err = EINVAL;
629398c88c7SAlan Somers 	} else if (!err) {
630398c88c7SAlan Somers 		*offset += *len;
631398c88c7SAlan Somers 		*len = 0;
632398c88c7SAlan Somers 		fuse_vnode_undirty_cached_timestamps(vp, false);
633398c88c7SAlan Somers 		fuse_internal_clear_suid_on_write(vp, cred, curthread);
634398c88c7SAlan Somers 		if (*offset > fvdat->cached_attrs.va_size) {
635398c88c7SAlan Somers 			fuse_vnode_setsize(vp, *offset, false);
636398c88c7SAlan Somers 			getnanouptime(&fvdat->last_local_modify);
637398c88c7SAlan Somers 		}
638398c88c7SAlan Somers 	}
639398c88c7SAlan Somers 
640398c88c7SAlan Somers 	return (err);
641398c88c7SAlan Somers }
642398c88c7SAlan Somers 
643a1c9f4adSAlan Somers /* {
644a1c9f4adSAlan Somers 	struct vnode *a_vp;
645a1c9f4adSAlan Somers 	daddr_t a_bn;
646a1c9f4adSAlan Somers 	struct bufobj **a_bop;
647a1c9f4adSAlan Somers 	daddr_t *a_bnp;
648a1c9f4adSAlan Somers 	int *a_runp;
649a1c9f4adSAlan Somers 	int *a_runb;
650a1c9f4adSAlan Somers } */
651a1c9f4adSAlan Somers static int
652a1c9f4adSAlan Somers fuse_vnop_bmap(struct vop_bmap_args *ap)
653a1c9f4adSAlan Somers {
654a1c9f4adSAlan Somers 	struct vnode *vp = ap->a_vp;
655a1c9f4adSAlan Somers 	struct bufobj **bo = ap->a_bop;
656a1c9f4adSAlan Somers 	struct thread *td = curthread;
657a1c9f4adSAlan Somers 	struct mount *mp;
658a1c9f4adSAlan Somers 	struct fuse_dispatcher fdi;
659a1c9f4adSAlan Somers 	struct fuse_bmap_in *fbi;
660a1c9f4adSAlan Somers 	struct fuse_bmap_out *fbo;
661a1c9f4adSAlan Somers 	struct fuse_data *data;
6627430017bSAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
663a1c9f4adSAlan Somers 	uint64_t biosize;
6647430017bSAlan Somers 	off_t fsize;
665a1c9f4adSAlan Somers 	daddr_t lbn = ap->a_bn;
666a1c9f4adSAlan Somers 	daddr_t *pbn = ap->a_bnp;
667a1c9f4adSAlan Somers 	int *runp = ap->a_runp;
668a1c9f4adSAlan Somers 	int *runb = ap->a_runb;
669a1c9f4adSAlan Somers 	int error = 0;
670a1c9f4adSAlan Somers 	int maxrun;
671a1c9f4adSAlan Somers 
672a1c9f4adSAlan Somers 	if (fuse_isdeadfs(vp)) {
673a1c9f4adSAlan Somers 		return ENXIO;
674a1c9f4adSAlan Somers 	}
675a1c9f4adSAlan Somers 
676a1c9f4adSAlan Somers 	mp = vnode_mount(vp);
677a1c9f4adSAlan Somers 	data = fuse_get_mpdata(mp);
678a1c9f4adSAlan Somers 	biosize = fuse_iosize(vp);
679a1c9f4adSAlan Somers 	maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
680a1c9f4adSAlan Somers 		data->max_readahead_blocks);
681a1c9f4adSAlan Somers 
682a1c9f4adSAlan Somers 	if (bo != NULL)
683a1c9f4adSAlan Somers 		*bo = &vp->v_bufobj;
684a1c9f4adSAlan Somers 
685a1c9f4adSAlan Somers 	/*
686a1c9f4adSAlan Somers 	 * The FUSE_BMAP operation does not include the runp and runb
687a1c9f4adSAlan Somers 	 * variables, so we must guess.  Report nonzero contiguous runs so
688a1c9f4adSAlan Somers 	 * cluster_read will combine adjacent reads.  It's worthwhile to reduce
689a1c9f4adSAlan Somers 	 * upcalls even if we don't know the true physical layout of the file.
690a1c9f4adSAlan Somers 	 *
691a1c9f4adSAlan Somers 	 * FUSE file systems may opt out of read clustering in two ways:
692a1c9f4adSAlan Somers 	 * * mounting with -onoclusterr
693a1c9f4adSAlan Somers 	 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
694a1c9f4adSAlan Somers 	 */
695a1c9f4adSAlan Somers 	if (runb != NULL)
696a1c9f4adSAlan Somers 		*runb = MIN(lbn, maxrun);
6977430017bSAlan Somers 	if (runp != NULL && maxrun == 0)
6987430017bSAlan Somers 		*runp = 0;
6997430017bSAlan Somers 	else if (runp != NULL) {
7007430017bSAlan Somers 		/*
7017430017bSAlan Somers 		 * If the file's size is cached, use that value to calculate
7027430017bSAlan Somers 		 * runp, even if the cache is expired.  runp is only advisory,
7037430017bSAlan Somers 		 * and the risk of getting it wrong is not worth the cost of
7047430017bSAlan Somers 		 * another upcall.
7057430017bSAlan Somers 		 */
7067430017bSAlan Somers 		if (fvdat->cached_attrs.va_size != VNOVAL)
7077430017bSAlan Somers 			fsize = fvdat->cached_attrs.va_size;
7087430017bSAlan Somers 		else
7097430017bSAlan Somers 			error = fuse_vnode_size(vp, &fsize, td->td_ucred, td);
710a1c9f4adSAlan Somers 		if (error == 0)
7117430017bSAlan Somers 			*runp = MIN(MAX(0, fsize / (off_t)biosize - lbn - 1),
712a1c9f4adSAlan Somers 				    maxrun);
713a1c9f4adSAlan Somers 		else
714a1c9f4adSAlan Somers 			*runp = 0;
715a1c9f4adSAlan Somers 	}
716a1c9f4adSAlan Somers 
71737df9d3bSAlan Somers 	if (fsess_maybe_impl(mp, FUSE_BMAP)) {
718a1c9f4adSAlan Somers 		fdisp_init(&fdi, sizeof(*fbi));
719a1c9f4adSAlan Somers 		fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
720a1c9f4adSAlan Somers 		fbi = fdi.indata;
721a1c9f4adSAlan Somers 		fbi->block = lbn;
722a1c9f4adSAlan Somers 		fbi->blocksize = biosize;
723a1c9f4adSAlan Somers 		error = fdisp_wait_answ(&fdi);
724a1c9f4adSAlan Somers 		if (error == ENOSYS) {
725caeea8b4SAlan Somers 			fdisp_destroy(&fdi);
726a1c9f4adSAlan Somers 			fsess_set_notimpl(mp, FUSE_BMAP);
727a1c9f4adSAlan Somers 			error = 0;
728a1c9f4adSAlan Somers 		} else {
729a1c9f4adSAlan Somers 			fbo = fdi.answ;
730a1c9f4adSAlan Somers 			if (error == 0 && pbn != NULL)
731a1c9f4adSAlan Somers 				*pbn = fbo->block;
732caeea8b4SAlan Somers 			fdisp_destroy(&fdi);
733a1c9f4adSAlan Somers 			return error;
734a1c9f4adSAlan Somers 		}
735a1c9f4adSAlan Somers 	}
736a1c9f4adSAlan Somers 
737a1c9f4adSAlan Somers 	/* If the daemon doesn't support BMAP, make up a sensible default */
738a1c9f4adSAlan Somers 	if (pbn != NULL)
739a1c9f4adSAlan Somers 		*pbn = lbn * btodb(biosize);
740a1c9f4adSAlan Somers 	return (error);
741a1c9f4adSAlan Somers }
742a1c9f4adSAlan Somers 
743f067b609SAlan Somers /*
7449f10f423SAlan Somers     struct vop_close_args {
7455fe58019SAttilio Rao 	struct vnode *a_vp;
7465fe58019SAttilio Rao 	int  a_fflag;
7475fe58019SAttilio Rao 	struct ucred *a_cred;
7485fe58019SAttilio Rao 	struct thread *a_td;
7495fe58019SAttilio Rao     };
7505fe58019SAttilio Rao */
7515fe58019SAttilio Rao static int
7525fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap)
7535fe58019SAttilio Rao {
7545fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
7555fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
7565fe58019SAttilio Rao 	int fflag = ap->a_fflag;
757f8d4af10SAlan Somers 	struct thread *td = ap->a_td;
758f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
75991972cfcSAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
7609f10f423SAlan Somers 	int err = 0;
7615fe58019SAttilio Rao 
76235cf0e7eSAlan Somers 	if (fuse_isdeadfs(vp))
7635fe58019SAttilio Rao 		return 0;
76435cf0e7eSAlan Somers 	if (vnode_isdir(vp))
76535cf0e7eSAlan Somers 		return 0;
76635cf0e7eSAlan Somers 	if (fflag & IO_NDELAY)
76735cf0e7eSAlan Somers 		return 0;
7685ec10aa5SAlan Somers 
7699f10f423SAlan Somers 	err = fuse_flush(vp, cred, pid, fflag);
77091972cfcSAlan Somers 	if (err == 0 && (fvdat->flag & FN_ATIMECHANGE)) {
77191972cfcSAlan Somers 		struct vattr vap;
77291972cfcSAlan Somers 
77391972cfcSAlan Somers 		VATTR_NULL(&vap);
77491972cfcSAlan Somers 		vap.va_atime = fvdat->cached_attrs.va_atime;
77591972cfcSAlan Somers 		err = fuse_internal_setattr(vp, &vap, td, NULL);
77691972cfcSAlan Somers 	}
7775ec10aa5SAlan Somers 	/* TODO: close the file handle, if we're sure it's no longer used */
77891972cfcSAlan Somers 	if ((fvdat->flag & FN_SIZECHANGE) != 0) {
779f8d4af10SAlan Somers 		fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
7805fe58019SAttilio Rao 	}
7819f10f423SAlan Somers 	return err;
7825fe58019SAttilio Rao }
7835fe58019SAttilio Rao 
78492bbfe1fSAlan Somers /*
78592bbfe1fSAlan Somers    struct vop_copy_file_range_args {
78692bbfe1fSAlan Somers 	struct vop_generic_args a_gen;
78792bbfe1fSAlan Somers 	struct vnode *a_invp;
78892bbfe1fSAlan Somers 	off_t *a_inoffp;
78992bbfe1fSAlan Somers 	struct vnode *a_outvp;
79092bbfe1fSAlan Somers 	off_t *a_outoffp;
79192bbfe1fSAlan Somers 	size_t *a_lenp;
79292bbfe1fSAlan Somers 	unsigned int a_flags;
79392bbfe1fSAlan Somers 	struct ucred *a_incred;
79492bbfe1fSAlan Somers 	struct ucred *a_outcred;
79592bbfe1fSAlan Somers 	struct thread *a_fsizetd;
79692bbfe1fSAlan Somers }
79792bbfe1fSAlan Somers  */
79892bbfe1fSAlan Somers static int
79992bbfe1fSAlan Somers fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
80092bbfe1fSAlan Somers {
80192bbfe1fSAlan Somers 	struct vnode *invp = ap->a_invp;
80292bbfe1fSAlan Somers 	struct vnode *outvp = ap->a_outvp;
80392bbfe1fSAlan Somers 	struct mount *mp = vnode_mount(invp);
80465d70b3bSAlan Somers 	struct fuse_vnode_data *outfvdat = VTOFUD(outvp);
80592bbfe1fSAlan Somers 	struct fuse_dispatcher fdi;
80692bbfe1fSAlan Somers 	struct fuse_filehandle *infufh, *outfufh;
80792bbfe1fSAlan Somers 	struct fuse_copy_file_range_in *fcfri;
80892bbfe1fSAlan Somers 	struct ucred *incred = ap->a_incred;
80992bbfe1fSAlan Somers 	struct ucred *outcred = ap->a_outcred;
81092bbfe1fSAlan Somers 	struct fuse_write_out *fwo;
81192bbfe1fSAlan Somers 	struct thread *td;
81292bbfe1fSAlan Somers 	struct uio io;
81341ae9f9eSAlan Somers 	off_t outfilesize;
81492bbfe1fSAlan Somers 	pid_t pid;
81592bbfe1fSAlan Somers 	int err;
81692bbfe1fSAlan Somers 
81792bbfe1fSAlan Somers 	if (mp != vnode_mount(outvp))
81892bbfe1fSAlan Somers 		goto fallback;
81992bbfe1fSAlan Somers 
82092bbfe1fSAlan Somers 	if (incred->cr_uid != outcred->cr_uid)
82192bbfe1fSAlan Somers 		goto fallback;
82292bbfe1fSAlan Somers 
82392bbfe1fSAlan Somers 	if (incred->cr_groups[0] != outcred->cr_groups[0])
82492bbfe1fSAlan Somers 		goto fallback;
82592bbfe1fSAlan Somers 
82692bbfe1fSAlan Somers 	if (fsess_not_impl(mp, FUSE_COPY_FILE_RANGE))
82792bbfe1fSAlan Somers 		goto fallback;
82892bbfe1fSAlan Somers 
82992bbfe1fSAlan Somers 	if (ap->a_fsizetd == NULL)
83092bbfe1fSAlan Somers 		td = curthread;
83192bbfe1fSAlan Somers 	else
83292bbfe1fSAlan Somers 		td = ap->a_fsizetd;
83392bbfe1fSAlan Somers 	pid = td->td_proc->p_pid;
83492bbfe1fSAlan Somers 
83592bbfe1fSAlan Somers 	/* Lock both vnodes, avoiding risk of deadlock. */
83692bbfe1fSAlan Somers 	do {
83792bbfe1fSAlan Somers 		err = vn_lock(outvp, LK_EXCLUSIVE);
83892bbfe1fSAlan Somers 		if (invp == outvp)
83992bbfe1fSAlan Somers 			break;
84092bbfe1fSAlan Somers 		if (err == 0) {
84192bbfe1fSAlan Somers 			err = vn_lock(invp, LK_SHARED | LK_NOWAIT);
84292bbfe1fSAlan Somers 			if (err == 0)
84392bbfe1fSAlan Somers 				break;
84492bbfe1fSAlan Somers 			VOP_UNLOCK(outvp);
84592bbfe1fSAlan Somers 			err = vn_lock(invp, LK_SHARED);
84692bbfe1fSAlan Somers 			if (err == 0)
84792bbfe1fSAlan Somers 				VOP_UNLOCK(invp);
84892bbfe1fSAlan Somers 		}
84992bbfe1fSAlan Somers 	} while (err == 0);
85092bbfe1fSAlan Somers 	if (err != 0)
85192bbfe1fSAlan Somers 		return (err);
85292bbfe1fSAlan Somers 
85317a82e6aSAlan Somers 	err = fuse_filehandle_getrw(invp, FREAD, &infufh, incred, pid);
85417a82e6aSAlan Somers 	if (err)
85517a82e6aSAlan Somers 		goto unlock;
85617a82e6aSAlan Somers 
85717a82e6aSAlan Somers 	err = fuse_filehandle_getrw(outvp, FWRITE, &outfufh, outcred, pid);
85817a82e6aSAlan Somers 	if (err)
85917a82e6aSAlan Somers 		goto unlock;
86017a82e6aSAlan Somers 
86192bbfe1fSAlan Somers 	if (ap->a_fsizetd) {
86292bbfe1fSAlan Somers 		io.uio_offset = *ap->a_outoffp;
86392bbfe1fSAlan Somers 		io.uio_resid = *ap->a_lenp;
86492bbfe1fSAlan Somers 		err = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
86592bbfe1fSAlan Somers 		if (err)
86692bbfe1fSAlan Somers 			goto unlock;
86792bbfe1fSAlan Somers 	}
86892bbfe1fSAlan Somers 
86941ae9f9eSAlan Somers 	err = fuse_vnode_size(outvp, &outfilesize, outcred, curthread);
87041ae9f9eSAlan Somers 	if (err)
87141ae9f9eSAlan Somers 		goto unlock;
87241ae9f9eSAlan Somers 
87341ae9f9eSAlan Somers 	err = fuse_inval_buf_range(outvp, outfilesize, *ap->a_outoffp,
87441ae9f9eSAlan Somers 		*ap->a_outoffp + *ap->a_lenp);
87541ae9f9eSAlan Somers 	if (err)
87641ae9f9eSAlan Somers 		goto unlock;
87741ae9f9eSAlan Somers 
87892bbfe1fSAlan Somers 	fdisp_init(&fdi, sizeof(*fcfri));
87992bbfe1fSAlan Somers 	fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred);
88092bbfe1fSAlan Somers 	fcfri = fdi.indata;
88192bbfe1fSAlan Somers 	fcfri->fh_in = infufh->fh_id;
88292bbfe1fSAlan Somers 	fcfri->off_in = *ap->a_inoffp;
88392bbfe1fSAlan Somers 	fcfri->nodeid_out = VTOI(outvp);
88492bbfe1fSAlan Somers 	fcfri->fh_out = outfufh->fh_id;
88592bbfe1fSAlan Somers 	fcfri->off_out = *ap->a_outoffp;
88692bbfe1fSAlan Somers 	fcfri->len = *ap->a_lenp;
88792bbfe1fSAlan Somers 	fcfri->flags = 0;
88892bbfe1fSAlan Somers 
88992bbfe1fSAlan Somers 	err = fdisp_wait_answ(&fdi);
89092bbfe1fSAlan Somers 	if (err == 0) {
89192bbfe1fSAlan Somers 		fwo = fdi.answ;
89292bbfe1fSAlan Somers 		*ap->a_lenp = fwo->size;
89392bbfe1fSAlan Somers 		*ap->a_inoffp += fwo->size;
89492bbfe1fSAlan Somers 		*ap->a_outoffp += fwo->size;
89592bbfe1fSAlan Somers 		fuse_internal_clear_suid_on_write(outvp, outcred, td);
89613d593a5SAlan Somers 		if (*ap->a_outoffp > outfvdat->cached_attrs.va_size) {
89765d70b3bSAlan Somers                         fuse_vnode_setsize(outvp, *ap->a_outoffp, false);
89813d593a5SAlan Somers 			getnanouptime(&outfvdat->last_local_modify);
89913d593a5SAlan Somers 		}
9005169832cSAlan Somers 		fuse_vnode_update(invp, FN_ATIMECHANGE);
9015169832cSAlan Somers 		fuse_vnode_update(outvp, FN_MTIMECHANGE | FN_CTIMECHANGE);
90292bbfe1fSAlan Somers 	}
90392bbfe1fSAlan Somers 	fdisp_destroy(&fdi);
90492bbfe1fSAlan Somers 
90592bbfe1fSAlan Somers unlock:
90692bbfe1fSAlan Somers 	if (invp != outvp)
90792bbfe1fSAlan Somers 		VOP_UNLOCK(invp);
90892bbfe1fSAlan Somers 	VOP_UNLOCK(outvp);
90992bbfe1fSAlan Somers 
91092bbfe1fSAlan Somers 	if (err == ENOSYS) {
91192bbfe1fSAlan Somers 		fsess_set_notimpl(mp, FUSE_COPY_FILE_RANGE);
91292bbfe1fSAlan Somers fallback:
91392bbfe1fSAlan Somers 		err = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
91492bbfe1fSAlan Somers 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
91592bbfe1fSAlan Somers 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
91692bbfe1fSAlan Somers 	}
91792bbfe1fSAlan Somers 
91892bbfe1fSAlan Somers 	return (err);
91992bbfe1fSAlan Somers }
92092bbfe1fSAlan Somers 
92119ef317dSAlan Somers static void
92219ef317dSAlan Somers fdisp_make_mknod_for_fallback(
92319ef317dSAlan Somers 	struct fuse_dispatcher *fdip,
92419ef317dSAlan Somers 	struct componentname *cnp,
92519ef317dSAlan Somers 	struct vnode *dvp,
92619ef317dSAlan Somers 	uint64_t parentnid,
92719ef317dSAlan Somers 	struct thread *td,
92819ef317dSAlan Somers 	struct ucred *cred,
92919ef317dSAlan Somers 	mode_t mode,
93019ef317dSAlan Somers 	enum fuse_opcode *op)
93119ef317dSAlan Somers {
93219ef317dSAlan Somers 	struct fuse_mknod_in *fmni;
93319ef317dSAlan Somers 
93419ef317dSAlan Somers 	fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
93519ef317dSAlan Somers 	*op = FUSE_MKNOD;
93619ef317dSAlan Somers 	fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
93719ef317dSAlan Somers 	fmni = fdip->indata;
93819ef317dSAlan Somers 	fmni->mode = mode;
93919ef317dSAlan Somers 	fmni->rdev = 0;
94019ef317dSAlan Somers 	memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
94119ef317dSAlan Somers 	    cnp->cn_namelen);
94219ef317dSAlan Somers 	((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
94319ef317dSAlan Somers }
9445fe58019SAttilio Rao /*
9455fe58019SAttilio Rao     struct vnop_create_args {
9465fe58019SAttilio Rao 	struct vnode *a_dvp;
9475fe58019SAttilio Rao 	struct vnode **a_vpp;
9485fe58019SAttilio Rao 	struct componentname *a_cnp;
9495fe58019SAttilio Rao 	struct vattr *a_vap;
9505fe58019SAttilio Rao     };
9515fe58019SAttilio Rao */
9525fe58019SAttilio Rao static int
9535fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap)
9545fe58019SAttilio Rao {
9555fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
9565fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
9575fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
9585fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
959b4a58fbfSMateusz Guzik 	struct thread *td = curthread;
9605fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
9615fe58019SAttilio Rao 
962a4856c96SAlan Somers 	struct fuse_data *data;
963a4856c96SAlan Somers 	struct fuse_create_in *fci;
9645fe58019SAttilio Rao 	struct fuse_entry_out *feo;
96519ef317dSAlan Somers 	struct fuse_open_out *foo;
96619ef317dSAlan Somers 	struct fuse_dispatcher fdi, fdi2;
9675fe58019SAttilio Rao 	struct fuse_dispatcher *fdip = &fdi;
96819ef317dSAlan Somers 	struct fuse_dispatcher *fdip2 = NULL;
9695fe58019SAttilio Rao 
9705fe58019SAttilio Rao 	int err;
9715fe58019SAttilio Rao 
9725fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
973a4856c96SAlan Somers 	data = fuse_get_mpdata(mp);
9745fe58019SAttilio Rao 	uint64_t parentnid = VTOFUD(dvp)->nid;
9755fe58019SAttilio Rao 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
97619ef317dSAlan Somers 	enum fuse_opcode op;
9779e444871SAlan Somers 	int flags;
9789e444871SAlan Somers 
979d5ff2688SAlan Somers 	if (fuse_isdeadfs(dvp))
980d5ff2688SAlan Somers 		return ENXIO;
981d5ff2688SAlan Somers 
982d5ff2688SAlan Somers 	/* FUSE expects sockets to be created with FUSE_MKNOD */
983d5ff2688SAlan Somers 	if (vap->va_type == VSOCK)
984d5ff2688SAlan Somers 		return fuse_internal_mknod(dvp, vpp, cnp, vap);
985d5ff2688SAlan Somers 
9869e444871SAlan Somers 	/*
9879e444871SAlan Somers 	 * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
9889e444871SAlan Somers 	 * writable mode makes sense, and we might as well include readability
9899e444871SAlan Somers 	 * too.
9909e444871SAlan Somers 	 */
9919e444871SAlan Somers 	flags = O_RDWR;
9925fe58019SAttilio Rao 
9935fe58019SAttilio Rao 	bzero(&fdi, sizeof(fdi));
9945fe58019SAttilio Rao 
995d5ff2688SAlan Somers 	if (vap->va_type != VREG)
996372b97d0SRick Macklem 		return (EINVAL);
9975fe58019SAttilio Rao 
99837df9d3bSAlan Somers 	if (fsess_not_impl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
99919ef317dSAlan Somers 		/* Fallback to FUSE_MKNOD/FUSE_OPEN */
100019ef317dSAlan Somers 		fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
100119ef317dSAlan Somers 			cred, mode, &op);
100219ef317dSAlan Somers 	} else {
100319ef317dSAlan Somers 		/* Use FUSE_CREATE */
1004a4856c96SAlan Somers 		size_t insize;
1005a4856c96SAlan Somers 
100619ef317dSAlan Somers 		op = FUSE_CREATE;
1007a4856c96SAlan Somers 		fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
100819ef317dSAlan Somers 		fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
1009a4856c96SAlan Somers 		fci = fdip->indata;
1010a4856c96SAlan Somers 		fci->mode = mode;
1011a4856c96SAlan Somers 		fci->flags = O_CREAT | flags;
1012a4856c96SAlan Somers 		if (fuse_libabi_geq(data, 7, 12)) {
1013a4856c96SAlan Somers 			insize = sizeof(*fci);
101485078b85SConrad Meyer 			fci->umask = td->td_proc->p_pd->pd_cmask;
1015a4856c96SAlan Somers 		} else {
1016a4856c96SAlan Somers 			insize = sizeof(struct fuse_open_in);
1017a4856c96SAlan Somers 		}
1018a4856c96SAlan Somers 
1019a4856c96SAlan Somers 		memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
10205fe58019SAttilio Rao 		    cnp->cn_namelen);
1021a4856c96SAlan Somers 		((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
102219ef317dSAlan Somers 	}
10235fe58019SAttilio Rao 
10245fe58019SAttilio Rao 	err = fdisp_wait_answ(fdip);
10255fe58019SAttilio Rao 
10266de3b00dSAttilio Rao 	if (err) {
102719ef317dSAlan Somers 		if (err == ENOSYS && op == FUSE_CREATE) {
10285fe58019SAttilio Rao 			fsess_set_notimpl(mp, FUSE_CREATE);
1029caeea8b4SAlan Somers 			fdisp_destroy(fdip);
103019ef317dSAlan Somers 			fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
103119ef317dSAlan Somers 				parentnid, td, cred, mode, &op);
103219ef317dSAlan Somers 			err = fdisp_wait_answ(fdip);
103319ef317dSAlan Somers 		}
103419ef317dSAlan Somers 		if (err)
10355fe58019SAttilio Rao 			goto out;
10365fe58019SAttilio Rao 	}
1037372b97d0SRick Macklem 
10385fe58019SAttilio Rao 	feo = fdip->answ;
10395fe58019SAttilio Rao 
1040ede571e4SAlan Somers 	if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
10415fe58019SAttilio Rao 		goto out;
10425fe58019SAttilio Rao 	}
104319ef317dSAlan Somers 
104419ef317dSAlan Somers 	if (op == FUSE_CREATE) {
104519ef317dSAlan Somers 		foo = (struct fuse_open_out*)(feo + 1);
104619ef317dSAlan Somers 	} else {
104719ef317dSAlan Somers 		/* Issue a separate FUSE_OPEN */
1048a4856c96SAlan Somers 		struct fuse_open_in *foi;
1049a4856c96SAlan Somers 
105019ef317dSAlan Somers 		fdip2 = &fdi2;
105119ef317dSAlan Somers 		fdisp_init(fdip2, sizeof(*foi));
105219ef317dSAlan Somers 		fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
105319ef317dSAlan Somers 			cred);
105419ef317dSAlan Somers 		foi = fdip2->indata;
10559e444871SAlan Somers 		foi->flags = flags;
105619ef317dSAlan Somers 		err = fdisp_wait_answ(fdip2);
105719ef317dSAlan Somers 		if (err)
105819ef317dSAlan Somers 			goto out;
105919ef317dSAlan Somers 		foo = fdip2->answ;
106019ef317dSAlan Somers 	}
1061ede571e4SAlan Somers 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
10625fe58019SAttilio Rao 	if (err) {
10635fe58019SAttilio Rao 		struct fuse_release_in *fri;
10645fe58019SAttilio Rao 		uint64_t nodeid = feo->nodeid;
106519ef317dSAlan Somers 		uint64_t fh_id = foo->fh;
10665fe58019SAttilio Rao 
10675fe58019SAttilio Rao 		fdisp_init(fdip, sizeof(*fri));
10685fe58019SAttilio Rao 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
10695fe58019SAttilio Rao 		fri = fdip->indata;
10705fe58019SAttilio Rao 		fri->fh = fh_id;
10719e444871SAlan Somers 		fri->flags = flags;
10725fe58019SAttilio Rao 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
1073268c28edSAlan Somers 		fuse_insert_message(fdip->tick, false);
107419ef317dSAlan Somers 		goto out;
10755fe58019SAttilio Rao 	}
10765fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
10770269ae4cSAlan Somers 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
10785d94aaacSAlan Somers 		feo->attr_valid_nsec, NULL, true);
10795fe58019SAttilio Rao 
1080a7e81cb3SAlan Somers 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
108119ef317dSAlan Somers 	fuse_vnode_open(*vpp, foo->open_flags, td);
1082002e54b0SAlan Somers 	/*
1083002e54b0SAlan Somers 	 * Purge the parent's attribute cache because the daemon should've
1084002e54b0SAlan Somers 	 * updated its mtime and ctime
1085002e54b0SAlan Somers 	 */
1086002e54b0SAlan Somers 	fuse_vnode_clear_attr_cache(dvp);
10875fe58019SAttilio Rao 	cache_purge_negative(dvp);
10885fe58019SAttilio Rao 
10895fe58019SAttilio Rao out:
109019ef317dSAlan Somers 	if (fdip2)
109119ef317dSAlan Somers 		fdisp_destroy(fdip2);
10925fe58019SAttilio Rao 	fdisp_destroy(fdip);
10935fe58019SAttilio Rao 	return err;
10945fe58019SAttilio Rao }
10955fe58019SAttilio Rao 
10965fe58019SAttilio Rao /*
1097915012e0SAlan Somers     struct vnop_fdatasync_args {
1098915012e0SAlan Somers 	struct vop_generic_args a_gen;
1099915012e0SAlan Somers 	struct vnode * a_vp;
1100915012e0SAlan Somers 	struct thread * a_td;
1101915012e0SAlan Somers     };
11025fe58019SAttilio Rao */
1103915012e0SAlan Somers static int
1104915012e0SAlan Somers fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
1105915012e0SAlan Somers {
1106915012e0SAlan Somers 	struct vnode *vp = ap->a_vp;
1107915012e0SAlan Somers 	struct thread *td = ap->a_td;
1108915012e0SAlan Somers 	int waitfor = MNT_WAIT;
1109915012e0SAlan Somers 
1110915012e0SAlan Somers 	int err = 0;
1111915012e0SAlan Somers 
1112915012e0SAlan Somers 	if (fuse_isdeadfs(vp)) {
1113915012e0SAlan Somers 		return 0;
1114915012e0SAlan Somers 	}
1115915012e0SAlan Somers 	if ((err = vop_stdfdatasync_buf(ap)))
1116915012e0SAlan Somers 		return err;
1117915012e0SAlan Somers 
1118915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, true);
1119915012e0SAlan Somers }
11205fe58019SAttilio Rao 
11215fe58019SAttilio Rao /*
11225fe58019SAttilio Rao     struct vnop_fsync_args {
1123915012e0SAlan Somers 	struct vop_generic_args a_gen;
11245fe58019SAttilio Rao 	struct vnode * a_vp;
11255fe58019SAttilio Rao 	int  a_waitfor;
11265fe58019SAttilio Rao 	struct thread * a_td;
11275fe58019SAttilio Rao     };
11285fe58019SAttilio Rao */
11295fe58019SAttilio Rao static int
11305fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap)
11315fe58019SAttilio Rao {
11325fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11335fe58019SAttilio Rao 	struct thread *td = ap->a_td;
113490612f3cSAlan Somers 	int waitfor = ap->a_waitfor;
1135915012e0SAlan Somers 	int err = 0;
11365fe58019SAttilio Rao 
11375fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
11385fe58019SAttilio Rao 		return 0;
11395fe58019SAttilio Rao 	}
11405fe58019SAttilio Rao 	if ((err = vop_stdfsync(ap)))
11415fe58019SAttilio Rao 		return err;
11425fe58019SAttilio Rao 
1143915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, false);
11445fe58019SAttilio Rao }
11455fe58019SAttilio Rao 
11465fe58019SAttilio Rao /*
11475fe58019SAttilio Rao     struct vnop_getattr_args {
11485fe58019SAttilio Rao 	struct vnode *a_vp;
11495fe58019SAttilio Rao 	struct vattr *a_vap;
11505fe58019SAttilio Rao 	struct ucred *a_cred;
11515fe58019SAttilio Rao 	struct thread *a_td;
11525fe58019SAttilio Rao     };
11535fe58019SAttilio Rao */
11545fe58019SAttilio Rao static int
11555fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap)
11565fe58019SAttilio Rao {
11575fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11585fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
11595fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
11605fe58019SAttilio Rao 	struct thread *td = curthread;
11615fe58019SAttilio Rao 
11625fe58019SAttilio Rao 	int err = 0;
11635fe58019SAttilio Rao 	int dataflags;
11645fe58019SAttilio Rao 
11655fe58019SAttilio Rao 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
11665fe58019SAttilio Rao 
11675fe58019SAttilio Rao 	/* Note that we are not bailing out on a dead file system just yet. */
11685fe58019SAttilio Rao 
11695fe58019SAttilio Rao 	if (!(dataflags & FSESS_INITED)) {
11705fe58019SAttilio Rao 		if (!vnode_isvroot(vp)) {
11715fe58019SAttilio Rao 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
11725fe58019SAttilio Rao 			err = ENOTCONN;
11735fe58019SAttilio Rao 			return err;
11745fe58019SAttilio Rao 		} else {
11755fe58019SAttilio Rao 			goto fake;
11765fe58019SAttilio Rao 		}
11775fe58019SAttilio Rao 	}
1178cad67791SAlan Somers 	err = fuse_internal_getattr(vp, vap, cred, td);
1179cad67791SAlan Somers 	if (err == ENOTCONN && vnode_isvroot(vp)) {
1180cf169498SAlan Somers 		/* see comment in fuse_vfsop_statfs() */
11815fe58019SAttilio Rao 		goto fake;
1182cad67791SAlan Somers 	} else {
11835fe58019SAttilio Rao 		return err;
1184cad67791SAlan Somers 	}
11855fe58019SAttilio Rao 
11865fe58019SAttilio Rao fake:
11875fe58019SAttilio Rao 	bzero(vap, sizeof(*vap));
11885fe58019SAttilio Rao 	vap->va_type = vnode_vtype(vp);
11895fe58019SAttilio Rao 
11905fe58019SAttilio Rao 	return 0;
11915fe58019SAttilio Rao }
11925fe58019SAttilio Rao 
11935fe58019SAttilio Rao /*
11945fe58019SAttilio Rao     struct vnop_inactive_args {
11955fe58019SAttilio Rao 	struct vnode *a_vp;
11965fe58019SAttilio Rao     };
11975fe58019SAttilio Rao */
11985fe58019SAttilio Rao static int
11995fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap)
12005fe58019SAttilio Rao {
12015fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
1202ab21ed17SMateusz Guzik 	struct thread *td = curthread;
12035fe58019SAttilio Rao 
12045fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
12055ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
12065fe58019SAttilio Rao 
12075ec10aa5SAlan Somers 	int need_flush = 1;
12085fe58019SAttilio Rao 
12095ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
12105fe58019SAttilio Rao 		if (need_flush && vp->v_type == VREG) {
12115fe58019SAttilio Rao 			if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1212f8d4af10SAlan Somers 				fuse_vnode_savesize(vp, NULL, 0);
12135fe58019SAttilio Rao 			}
12145940f822SAlan Somers 			if ((fvdat->flag & FN_REVOKED) != 0)
12155fe58019SAttilio Rao 				fuse_io_invalbuf(vp, td);
12165fe58019SAttilio Rao 			else
12175fe58019SAttilio Rao 				fuse_io_flushbuf(vp, MNT_WAIT, td);
12185fe58019SAttilio Rao 			need_flush = 0;
12195fe58019SAttilio Rao 		}
12205ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
12215fe58019SAttilio Rao 	}
12225fe58019SAttilio Rao 
1223435ecf40SAlan Somers 	if ((fvdat->flag & FN_REVOKED) != 0)
12245fe58019SAttilio Rao 		vrecycle(vp);
1225435ecf40SAlan Somers 
12265fe58019SAttilio Rao 	return 0;
12275fe58019SAttilio Rao }
12285fe58019SAttilio Rao 
12295fe58019SAttilio Rao /*
123037df9d3bSAlan Somers     struct vnop_ioctl_args {
123137df9d3bSAlan Somers 	struct vnode *a_vp;
123237df9d3bSAlan Somers 	u_long a_command;
123337df9d3bSAlan Somers 	caddr_t a_data;
123437df9d3bSAlan Somers 	int a_fflag;
123537df9d3bSAlan Somers 	struct ucred *a_cred;
123637df9d3bSAlan Somers 	struct thread *a_td;
123737df9d3bSAlan Somers     };
123837df9d3bSAlan Somers */
123937df9d3bSAlan Somers static int
124037df9d3bSAlan Somers fuse_vnop_ioctl(struct vop_ioctl_args *ap)
124137df9d3bSAlan Somers {
124237df9d3bSAlan Somers 	struct vnode *vp = ap->a_vp;
124337df9d3bSAlan Somers 	struct mount *mp = vnode_mount(vp);
124437df9d3bSAlan Somers 	struct ucred *cred = ap->a_cred;
124537df9d3bSAlan Somers 	off_t *offp;
124637df9d3bSAlan Somers 	pid_t pid = ap->a_td->td_proc->p_pid;
124737df9d3bSAlan Somers 	int err;
124837df9d3bSAlan Somers 
124937df9d3bSAlan Somers 	switch (ap->a_command) {
125037df9d3bSAlan Somers 	case FIOSEEKDATA:
125137df9d3bSAlan Somers 	case FIOSEEKHOLE:
125237df9d3bSAlan Somers 		/* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
125337df9d3bSAlan Somers 		if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
125437df9d3bSAlan Somers 			int whence;
125537df9d3bSAlan Somers 
125637df9d3bSAlan Somers 			offp = ap->a_data;
125737df9d3bSAlan Somers 			if (ap->a_command == FIOSEEKDATA)
125837df9d3bSAlan Somers 				whence = SEEK_DATA;
125937df9d3bSAlan Somers 			else
126037df9d3bSAlan Somers 				whence = SEEK_HOLE;
126137df9d3bSAlan Somers 
126237df9d3bSAlan Somers 			vn_lock(vp, LK_SHARED | LK_RETRY);
126337df9d3bSAlan Somers 			err = fuse_vnop_do_lseek(vp, ap->a_td, cred, pid, offp,
126437df9d3bSAlan Somers 			    whence);
126537df9d3bSAlan Somers 			VOP_UNLOCK(vp);
126637df9d3bSAlan Somers 		}
126737df9d3bSAlan Somers 		if (fsess_not_impl(mp, FUSE_LSEEK))
126837df9d3bSAlan Somers 			err = vop_stdioctl(ap);
126937df9d3bSAlan Somers 		break;
127037df9d3bSAlan Somers 	default:
127137df9d3bSAlan Somers 		/* TODO: implement FUSE_IOCTL */
127237df9d3bSAlan Somers 		err = ENOTTY;
127337df9d3bSAlan Somers 		break;
127437df9d3bSAlan Somers 	}
127537df9d3bSAlan Somers 	return (err);
127637df9d3bSAlan Somers }
127737df9d3bSAlan Somers 
127837df9d3bSAlan Somers 
127937df9d3bSAlan Somers /*
12805fe58019SAttilio Rao     struct vnop_link_args {
12815fe58019SAttilio Rao 	struct vnode *a_tdvp;
12825fe58019SAttilio Rao 	struct vnode *a_vp;
12835fe58019SAttilio Rao 	struct componentname *a_cnp;
12845fe58019SAttilio Rao     };
12855fe58019SAttilio Rao */
12865fe58019SAttilio Rao static int
12875fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap)
12885fe58019SAttilio Rao {
12895fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12905fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
12915fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
12925fe58019SAttilio Rao 
12935fe58019SAttilio Rao 	struct vattr *vap = VTOVA(vp);
12945fe58019SAttilio Rao 
12955fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
12965fe58019SAttilio Rao 	struct fuse_entry_out *feo;
12975fe58019SAttilio Rao 	struct fuse_link_in fli;
12985fe58019SAttilio Rao 
12995fe58019SAttilio Rao 	int err;
13005fe58019SAttilio Rao 
13015fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13025fe58019SAttilio Rao 		return ENXIO;
13035fe58019SAttilio Rao 	}
13045fe58019SAttilio Rao 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
13055fe58019SAttilio Rao 		return EXDEV;
13065fe58019SAttilio Rao 	}
130778a7722fSConrad Meyer 
130878a7722fSConrad Meyer 	/*
130978a7722fSConrad Meyer 	 * This is a seatbelt check to protect naive userspace filesystems from
131078a7722fSConrad Meyer 	 * themselves and the limitations of the FUSE IPC protocol.  If a
131178a7722fSConrad Meyer 	 * filesystem does not allow attribute caching, assume it is capable of
131278a7722fSConrad Meyer 	 * validating that nlink does not overflow.
131378a7722fSConrad Meyer 	 */
131478a7722fSConrad Meyer 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
13155fe58019SAttilio Rao 		return EMLINK;
13165fe58019SAttilio Rao 	fli.oldnodeid = VTOI(vp);
13175fe58019SAttilio Rao 
13185fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
13195fe58019SAttilio Rao 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
13205fe58019SAttilio Rao 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
13215fe58019SAttilio Rao 	if ((err = fdisp_wait_answ(&fdi))) {
13225fe58019SAttilio Rao 		goto out;
13235fe58019SAttilio Rao 	}
13245fe58019SAttilio Rao 	feo = fdi.answ;
13255fe58019SAttilio Rao 
13265fe58019SAttilio Rao 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1327002e54b0SAlan Somers 	if (!err) {
1328002e54b0SAlan Somers 		/*
1329002e54b0SAlan Somers 		 * Purge the parent's attribute cache because the daemon
1330002e54b0SAlan Somers 		 * should've updated its mtime and ctime
1331002e54b0SAlan Somers 		 */
1332002e54b0SAlan Somers 		fuse_vnode_clear_attr_cache(tdvp);
13330269ae4cSAlan Somers 		fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
13345d94aaacSAlan Somers 			feo->attr_valid_nsec, NULL, true);
1335002e54b0SAlan Somers 	}
13365fe58019SAttilio Rao out:
13375fe58019SAttilio Rao 	fdisp_destroy(&fdi);
13385fe58019SAttilio Rao 	return err;
13395fe58019SAttilio Rao }
13405fe58019SAttilio Rao 
1341dc14d593SAlan Somers struct fuse_lookup_alloc_arg {
1342dc14d593SAlan Somers 	struct fuse_entry_out *feo;
1343dc14d593SAlan Somers 	struct componentname *cnp;
1344dc14d593SAlan Somers 	uint64_t nid;
1345dc14d593SAlan Somers 	enum vtype vtyp;
1346dc14d593SAlan Somers };
1347dc14d593SAlan Somers 
1348dc14d593SAlan Somers /* Callback for vn_get_ino */
1349dc14d593SAlan Somers static int
1350dc14d593SAlan Somers fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1351dc14d593SAlan Somers {
1352dc14d593SAlan Somers 	struct fuse_lookup_alloc_arg *flaa = arg;
1353dc14d593SAlan Somers 
1354dc14d593SAlan Somers 	return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1355dc14d593SAlan Somers 		flaa->vtyp);
1356dc14d593SAlan Somers }
1357dc14d593SAlan Somers 
1358419e7ff6SAlan Somers SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1359ccb75e49SAlan Somers 	"int", "struct timespec*", "struct timespec*");
13605fe58019SAttilio Rao /*
13615fe58019SAttilio Rao     struct vnop_lookup_args {
13625fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
13635fe58019SAttilio Rao 	struct vnode *a_dvp;
13645fe58019SAttilio Rao 	struct vnode **a_vpp;
13655fe58019SAttilio Rao 	struct componentname *a_cnp;
13665fe58019SAttilio Rao     };
13675fe58019SAttilio Rao */
13685fe58019SAttilio Rao int
13695fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap)
13705fe58019SAttilio Rao {
13715fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
13725fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
13735fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
1374b4a58fbfSMateusz Guzik 	struct thread *td = curthread;
13755fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
137613d593a5SAlan Somers 	struct timespec now;
13775fe58019SAttilio Rao 
13785fe58019SAttilio Rao 	int nameiop = cnp->cn_nameiop;
13795fe58019SAttilio Rao 	int flags = cnp->cn_flags;
13805fe58019SAttilio Rao 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
13815fe58019SAttilio Rao 	int islastcn = flags & ISLASTCN;
13825fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
1383bfcb817bSAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
1384bfcb817bSAlan Somers 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
13855fe58019SAttilio Rao 
13865fe58019SAttilio Rao 	int err = 0;
13875fe58019SAttilio Rao 	int lookup_err = 0;
13885fe58019SAttilio Rao 	struct vnode *vp = NULL;
13895fe58019SAttilio Rao 
13905fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
1391438b8a6fSAlan Somers 	bool did_lookup = false;
139244f10c6eSAlan Somers 	struct fuse_entry_out *feo = NULL;
1393438b8a6fSAlan Somers 	enum vtype vtyp;	/* vnode type of target */
13945fe58019SAttilio Rao 
13955fe58019SAttilio Rao 	uint64_t nid;
13965fe58019SAttilio Rao 
13975fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
13985fe58019SAttilio Rao 		*vpp = NULL;
13995fe58019SAttilio Rao 		return ENXIO;
14005fe58019SAttilio Rao 	}
140173825da3SAlan Somers 	if (!vnode_isdir(dvp))
14025fe58019SAttilio Rao 		return ENOTDIR;
14035fe58019SAttilio Rao 
140473825da3SAlan Somers 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
140573825da3SAlan Somers 		return EROFS;
140673825da3SAlan Somers 
14076a5abb1eSKyle Evans 	if ((cnp->cn_flags & NOEXECCHECK) != 0)
14086a5abb1eSKyle Evans 		cnp->cn_flags &= ~NOEXECCHECK;
14096a5abb1eSKyle Evans 	else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
14105fe58019SAttilio Rao 		return err;
141173825da3SAlan Somers 
14121613087aSAlan Somers 	if ((flags & ISDOTDOT) && !(data->dataflags & FSESS_EXPORT_SUPPORT))
14131613087aSAlan Somers 	{
14141613087aSAlan Somers 		if (!(VTOFUD(dvp)->flag & FN_PARENT_NID)) {
14151613087aSAlan Somers 			/*
14161613087aSAlan Somers 			 * Since the file system doesn't support ".." lookups,
14171613087aSAlan Somers 			 * we have no way to find this entry.
14181613087aSAlan Somers 			 */
14191613087aSAlan Somers 			return ESTALE;
14201613087aSAlan Somers 		}
14215fe58019SAttilio Rao 		nid = VTOFUD(dvp)->parent_nid;
1422438b8a6fSAlan Somers 		if (nid == 0)
14235fe58019SAttilio Rao 			return ENOENT;
1424438b8a6fSAlan Somers 		/* .. is obviously a directory */
1425438b8a6fSAlan Somers 		vtyp = VDIR;
14265fe58019SAttilio Rao 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
14275fe58019SAttilio Rao 		nid = VTOI(dvp);
1428438b8a6fSAlan Somers 		/* . is obviously a directory */
1429438b8a6fSAlan Somers 		vtyp = VDIR;
1430ccb75e49SAlan Somers 	} else {
143113d593a5SAlan Somers 		struct timespec timeout;
1432*ef1534caSGordon Bergling 		int ncpticks; /* here to accommodate for API contract */
14333f2c630cSAlan Somers 
14344961e997SMateusz Guzik 		err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
14353f2c630cSAlan Somers 		getnanouptime(&now);
1436419e7ff6SAlan Somers 		SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1437ccb75e49SAlan Somers 		switch (err) {
1438ccb75e49SAlan Somers 		case -1:		/* positive match */
1439ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, >)) {
1440560a55d0SAlan Somers 				counter_u64_add(fuse_lookup_cache_hits, 1);
14413f2c630cSAlan Somers 			} else {
14423f2c630cSAlan Somers 				/* Cache timeout */
1443560a55d0SAlan Somers 				counter_u64_add(fuse_lookup_cache_misses, 1);
14440d2bf489SAlan Somers 				bintime_clear(
14450d2bf489SAlan Somers 					&VTOFUD(*vpp)->entry_cache_timeout);
14464683b905SAlan Somers 				cache_purge(*vpp);
1447ccb75e49SAlan Somers 				if (dvp != *vpp)
1448ccb75e49SAlan Somers 					vput(*vpp);
1449ccb75e49SAlan Somers 				else
1450ccb75e49SAlan Somers 					vrele(*vpp);
1451ccb75e49SAlan Somers 				*vpp = NULL;
14523f2c630cSAlan Somers 				break;
14533f2c630cSAlan Somers 			}
14543f2c630cSAlan Somers 			return 0;
14553f2c630cSAlan Somers 
14563f2c630cSAlan Somers 		case 0:		/* no match in cache */
1457560a55d0SAlan Somers 			counter_u64_add(fuse_lookup_cache_misses, 1);
14583f2c630cSAlan Somers 			break;
14593f2c630cSAlan Somers 
14603f2c630cSAlan Somers 		case ENOENT:		/* negative match */
1461ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, <=)) {
14623f2c630cSAlan Somers 				/* Cache timeout */
1463ccb75e49SAlan Somers 				cache_purge_negative(dvp);
14643f2c630cSAlan Somers 				break;
14653f2c630cSAlan Somers 			}
14663f2c630cSAlan Somers 			/* fall through */
14673f2c630cSAlan Somers 		default:
14683f2c630cSAlan Somers 			return err;
14693f2c630cSAlan Somers 		}
1470438b8a6fSAlan Somers 
14715fe58019SAttilio Rao 		fdisp_init(&fdi, cnp->cn_namelen + 1);
14721613087aSAlan Somers 		fdisp_make(&fdi, FUSE_LOOKUP, mp, VTOI(dvp), td, cred);
14735fe58019SAttilio Rao 
14745fe58019SAttilio Rao 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
14755fe58019SAttilio Rao 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
14765fe58019SAttilio Rao 		lookup_err = fdisp_wait_answ(&fdi);
1477438b8a6fSAlan Somers 		did_lookup = true;
14785fe58019SAttilio Rao 
1479438b8a6fSAlan Somers 		if (!lookup_err) {
1480ccb75e49SAlan Somers 			/* lookup call succeeded */
1481438b8a6fSAlan Somers 			feo = (struct fuse_entry_out *)fdi.answ;
1482e5b50fe7SAlan Somers 			nid = feo->nodeid;
148344f10c6eSAlan Somers 			if (nid == 0) {
148444f10c6eSAlan Somers 				/* zero nodeid means ENOENT and cache it */
148544f10c6eSAlan Somers 				struct timespec timeout;
148644f10c6eSAlan Somers 
14876248288eSAlan Somers 				fdi.answ_stat = ENOENT;
14885fe58019SAttilio Rao 				lookup_err = ENOENT;
148944f10c6eSAlan Somers 				if (cnp->cn_flags & MAKEENTRY) {
149044f10c6eSAlan Somers 					fuse_validity_2_timespec(feo, &timeout);
14911613087aSAlan Somers 					/* Use the same entry_time for .. as for
14921613087aSAlan Somers 					 * the file itself.  That doesn't honor
14931613087aSAlan Somers 					 * exactly what the fuse server tells
14941613087aSAlan Somers 					 * us, but to do otherwise would require
14951613087aSAlan Somers 					 * another cache lookup at this point.
14961613087aSAlan Somers 					 */
14971613087aSAlan Somers 					struct timespec *dtsp = NULL;
1498438b8a6fSAlan Somers 					cache_enter_time(dvp, *vpp, cnp,
14991613087aSAlan Somers 						&timeout, dtsp);
150044f10c6eSAlan Somers 				}
15015fe58019SAttilio Rao 			}
1502438b8a6fSAlan Somers 			vtyp = IFTOVT(feo->attr.mode);
15035fe58019SAttilio Rao 		}
1504438b8a6fSAlan Somers 		if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
15055fe58019SAttilio Rao 			fdisp_destroy(&fdi);
15065fe58019SAttilio Rao 			return lookup_err;
15075fe58019SAttilio Rao 		}
1508438b8a6fSAlan Somers 	}
15095fe58019SAttilio Rao 	/* lookup_err, if non-zero, must be ENOENT at this point */
15105fe58019SAttilio Rao 
15115fe58019SAttilio Rao 	if (lookup_err) {
1512ff4fbdf5SAlan Somers 		/* Entry not found */
1513ff4fbdf5SAlan Somers 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1514bfcb817bSAlan Somers 			if (default_permissions)
1515bfcb817bSAlan Somers 				err = fuse_internal_access(dvp, VWRITE, td,
1516bfcb817bSAlan Somers 				    cred);
1517bfcb817bSAlan Somers 			else
1518bfcb817bSAlan Somers 				err = 0;
15196124fd71SAlan Somers 			if (!err) {
15205fe58019SAttilio Rao 				/*
1521dc14d593SAlan Somers 				 * Set the SAVENAME flag to hold onto the
15226124fd71SAlan Somers 				 * pathname for use later in VOP_CREATE or
15236124fd71SAlan Somers 				 * VOP_RENAME.
15245fe58019SAttilio Rao 				 */
15255fe58019SAttilio Rao 				cnp->cn_flags |= SAVENAME;
15265fe58019SAttilio Rao 
15275fe58019SAttilio Rao 				err = EJUSTRETURN;
15285fe58019SAttilio Rao 			}
15296124fd71SAlan Somers 		} else {
15305fe58019SAttilio Rao 			err = ENOENT;
15316124fd71SAlan Somers 		}
15325fe58019SAttilio Rao 	} else {
1533ff4fbdf5SAlan Somers 		/* Entry was found */
15345fe58019SAttilio Rao 		if (flags & ISDOTDOT) {
1535dc14d593SAlan Somers 			struct fuse_lookup_alloc_arg flaa;
15365fe58019SAttilio Rao 
1537dc14d593SAlan Somers 			flaa.nid = nid;
1538dc14d593SAlan Somers 			flaa.feo = feo;
1539dc14d593SAlan Somers 			flaa.cnp = cnp;
1540dc14d593SAlan Somers 			flaa.vtyp = vtyp;
1541dc14d593SAlan Somers 			err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1542dc14d593SAlan Somers 				&vp);
15435fe58019SAttilio Rao 			*vpp = vp;
15445fe58019SAttilio Rao 		} else if (nid == VTOI(dvp)) {
15455fe58019SAttilio Rao 			vref(dvp);
15465fe58019SAttilio Rao 			*vpp = dvp;
15475fe58019SAttilio Rao 		} else {
15483c324b94SConrad Meyer 			struct fuse_vnode_data *fvdat;
15493c324b94SConrad Meyer 
155009176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1551438b8a6fSAlan Somers 			    &vp, cnp, vtyp);
15526124fd71SAlan Somers 			if (err)
15535fe58019SAttilio Rao 				goto out;
15546124fd71SAlan Somers 			*vpp = vp;
15553c324b94SConrad Meyer 			fvdat = VTOFUD(vp);
15565fe58019SAttilio Rao 
15576124fd71SAlan Somers 			MPASS(feo != NULL);
155813d593a5SAlan Somers 			if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
155913d593a5SAlan Somers 				/*
156013d593a5SAlan Somers 				 * Attributes from the server are definitely
156113d593a5SAlan Somers 				 * newer than the last attributes we sent to
156213d593a5SAlan Somers 				 * the server, so cache them.
156313d593a5SAlan Somers 				 */
15640269ae4cSAlan Somers 				fuse_internal_cache_attrs(*vpp, &feo->attr,
156513d593a5SAlan Somers 					feo->attr_valid, feo->attr_valid_nsec,
156613d593a5SAlan Somers 					NULL, true);
156713d593a5SAlan Somers 			}
15680d2bf489SAlan Somers 			fuse_validity_2_bintime(feo->entry_valid,
15690d2bf489SAlan Somers 				feo->entry_valid_nsec,
15700d2bf489SAlan Somers 				&fvdat->entry_cache_timeout);
15716124fd71SAlan Somers 
15726124fd71SAlan Somers 			if ((nameiop == DELETE || nameiop == RENAME) &&
1573bfcb817bSAlan Somers 				islastcn && default_permissions)
15746124fd71SAlan Somers 			{
15756124fd71SAlan Somers 				struct vattr dvattr;
15766124fd71SAlan Somers 
15776124fd71SAlan Somers 				err = fuse_internal_access(dvp, VWRITE, td,
15786124fd71SAlan Somers 					cred);
15796124fd71SAlan Somers 				if (err != 0)
15806124fd71SAlan Somers 					goto out;
15816124fd71SAlan Somers 				/*
15826124fd71SAlan Somers 				 * if the parent's sticky bit is set, check
15836124fd71SAlan Somers 				 * whether we're allowed to remove the file.
15846124fd71SAlan Somers 				 * Need to figure out the vnode locking to make
15856124fd71SAlan Somers 				 * this work.
15866124fd71SAlan Somers 				 */
15876124fd71SAlan Somers 				fuse_internal_getattr(dvp, &dvattr, cred, td);
15886124fd71SAlan Somers 				if ((dvattr.va_mode & S_ISTXT) &&
15896124fd71SAlan Somers 					fuse_internal_access(dvp, VADMIN, td,
15906124fd71SAlan Somers 						cred) &&
15916124fd71SAlan Somers 					fuse_internal_access(*vpp, VADMIN, td,
15926124fd71SAlan Somers 						cred)) {
15936124fd71SAlan Somers 					err = EPERM;
15946124fd71SAlan Somers 					goto out;
15956124fd71SAlan Somers 				}
15966124fd71SAlan Somers 			}
15976124fd71SAlan Somers 
15986124fd71SAlan Somers 			if (islastcn && (
15996124fd71SAlan Somers 				(nameiop == DELETE) ||
16006124fd71SAlan Somers 				(nameiop == RENAME && wantparent))) {
16016124fd71SAlan Somers 				cnp->cn_flags |= SAVENAME;
16026124fd71SAlan Somers 			}
16035fe58019SAttilio Rao 		}
16045fe58019SAttilio Rao 	}
16055fe58019SAttilio Rao out:
16066124fd71SAlan Somers 	if (err) {
16076124fd71SAlan Somers 		if (vp != NULL && dvp != vp)
16086124fd71SAlan Somers 			vput(vp);
16096124fd71SAlan Somers 		else if (vp != NULL)
16106124fd71SAlan Somers 			vrele(vp);
16116124fd71SAlan Somers 		*vpp = NULL;
16125fe58019SAttilio Rao 	}
1613438b8a6fSAlan Somers 	if (did_lookup)
16145fe58019SAttilio Rao 		fdisp_destroy(&fdi);
16155fe58019SAttilio Rao 
16165fe58019SAttilio Rao 	return err;
16175fe58019SAttilio Rao }
16185fe58019SAttilio Rao 
16195fe58019SAttilio Rao /*
16205fe58019SAttilio Rao     struct vnop_mkdir_args {
16215fe58019SAttilio Rao 	struct vnode *a_dvp;
16225fe58019SAttilio Rao 	struct vnode **a_vpp;
16235fe58019SAttilio Rao 	struct componentname *a_cnp;
16245fe58019SAttilio Rao 	struct vattr *a_vap;
16255fe58019SAttilio Rao     };
16265fe58019SAttilio Rao */
16275fe58019SAttilio Rao static int
16285fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap)
16295fe58019SAttilio Rao {
16305fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
16315fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
16325fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
16335fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
16345fe58019SAttilio Rao 
16355fe58019SAttilio Rao 	struct fuse_mkdir_in fmdi;
16365fe58019SAttilio Rao 
16375fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
16385fe58019SAttilio Rao 		return ENXIO;
16395fe58019SAttilio Rao 	}
16405fe58019SAttilio Rao 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
164185078b85SConrad Meyer 	fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
16425fe58019SAttilio Rao 
1643d9454fabSAttilio Rao 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1644d9454fabSAttilio Rao 	    sizeof(fmdi), VDIR));
16455fe58019SAttilio Rao }
16465fe58019SAttilio Rao 
16475fe58019SAttilio Rao /*
16485fe58019SAttilio Rao     struct vnop_mknod_args {
16495fe58019SAttilio Rao 	struct vnode *a_dvp;
16505fe58019SAttilio Rao 	struct vnode **a_vpp;
16515fe58019SAttilio Rao 	struct componentname *a_cnp;
16525fe58019SAttilio Rao 	struct vattr *a_vap;
16535fe58019SAttilio Rao     };
16545fe58019SAttilio Rao */
16555fe58019SAttilio Rao static int
16565fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap)
16575fe58019SAttilio Rao {
16585fe58019SAttilio Rao 
1659bf4d7084SAlan Somers 	struct vnode *dvp = ap->a_dvp;
1660bf4d7084SAlan Somers 	struct vnode **vpp = ap->a_vpp;
1661bf4d7084SAlan Somers 	struct componentname *cnp = ap->a_cnp;
1662bf4d7084SAlan Somers 	struct vattr *vap = ap->a_vap;
16635fe58019SAttilio Rao 
1664bf4d7084SAlan Somers 	if (fuse_isdeadfs(dvp))
1665bf4d7084SAlan Somers 		return ENXIO;
1666bf4d7084SAlan Somers 
1667d5ff2688SAlan Somers 	return fuse_internal_mknod(dvp, vpp, cnp, vap);
1668bf4d7084SAlan Somers }
16695fe58019SAttilio Rao 
16705fe58019SAttilio Rao /*
1671e76986fdSAlan Somers     struct vop_open_args {
16725fe58019SAttilio Rao 	struct vnode *a_vp;
16735fe58019SAttilio Rao 	int  a_mode;
16745fe58019SAttilio Rao 	struct ucred *a_cred;
16755fe58019SAttilio Rao 	struct thread *a_td;
16765fe58019SAttilio Rao 	int a_fdidx; / struct file *a_fp;
16775fe58019SAttilio Rao     };
16785fe58019SAttilio Rao */
16795fe58019SAttilio Rao static int
16805fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap)
16815fe58019SAttilio Rao {
16825fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
16839e444871SAlan Somers 	int a_mode = ap->a_mode;
16845fe58019SAttilio Rao 	struct thread *td = ap->a_td;
16855fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1686f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
16875fe58019SAttilio Rao 
1688bf4d7084SAlan Somers 	if (fuse_isdeadfs(vp))
16895fe58019SAttilio Rao 		return ENXIO;
1690bf4d7084SAlan Somers 	if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1691bf4d7084SAlan Somers 		return (EOPNOTSUPP);
16929e444871SAlan Somers 	if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1693e7df9886SConrad Meyer 		return EINVAL;
1694e7df9886SConrad Meyer 
16959e444871SAlan Somers 	if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
16965fccbf31SAlan Somers 		fuse_vnode_open(vp, 0, td);
16975fe58019SAttilio Rao 		return 0;
16985fe58019SAttilio Rao 	}
16995fe58019SAttilio Rao 
17009e444871SAlan Somers 	return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
17015fe58019SAttilio Rao }
17025fe58019SAttilio Rao 
1703746c92e0SJohn Baldwin static int
1704746c92e0SJohn Baldwin fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1705746c92e0SJohn Baldwin {
170637df9d3bSAlan Somers 	struct vnode *vp = ap->a_vp;
170737df9d3bSAlan Somers 	struct mount *mp;
1708746c92e0SJohn Baldwin 
1709746c92e0SJohn Baldwin 	switch (ap->a_name) {
1710746c92e0SJohn Baldwin 	case _PC_FILESIZEBITS:
1711746c92e0SJohn Baldwin 		*ap->a_retval = 64;
1712746c92e0SJohn Baldwin 		return (0);
1713599afe53SJohn Baldwin 	case _PC_NAME_MAX:
1714599afe53SJohn Baldwin 		*ap->a_retval = NAME_MAX;
1715599afe53SJohn Baldwin 		return (0);
1716599afe53SJohn Baldwin 	case _PC_LINK_MAX:
1717f83f3d79SJohn Baldwin 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1718599afe53SJohn Baldwin 		return (0);
1719746c92e0SJohn Baldwin 	case _PC_SYMLINK_MAX:
1720746c92e0SJohn Baldwin 		*ap->a_retval = MAXPATHLEN;
1721746c92e0SJohn Baldwin 		return (0);
1722746c92e0SJohn Baldwin 	case _PC_NO_TRUNC:
1723746c92e0SJohn Baldwin 		*ap->a_retval = 1;
1724746c92e0SJohn Baldwin 		return (0);
172537df9d3bSAlan Somers 	case _PC_MIN_HOLE_SIZE:
172637df9d3bSAlan Somers 		/*
172737df9d3bSAlan Somers 		 * The FUSE protocol provides no mechanism for a server to
172837df9d3bSAlan Somers 		 * report _PC_MIN_HOLE_SIZE.  It's a protocol bug.  Instead,
172937df9d3bSAlan Somers 		 * return EINVAL if the server does not support FUSE_LSEEK, or
173037df9d3bSAlan Somers 		 * 1 if it does.
173137df9d3bSAlan Somers 		 */
173237df9d3bSAlan Somers 		mp = vnode_mount(vp);
173337df9d3bSAlan Somers 		if (!fsess_is_impl(mp, FUSE_LSEEK) &&
173437df9d3bSAlan Somers 		    !fsess_not_impl(mp, FUSE_LSEEK)) {
173537df9d3bSAlan Somers 			off_t offset = 0;
173637df9d3bSAlan Somers 
173737df9d3bSAlan Somers 			/* Issue a FUSE_LSEEK to find out if it's implemented */
173837df9d3bSAlan Somers 			fuse_vnop_do_lseek(vp, curthread, curthread->td_ucred,
173937df9d3bSAlan Somers 			    curthread->td_proc->p_pid, &offset, SEEK_DATA);
174037df9d3bSAlan Somers 		}
174137df9d3bSAlan Somers 
174237df9d3bSAlan Somers 		if (fsess_is_impl(mp, FUSE_LSEEK)) {
174337df9d3bSAlan Somers 			*ap->a_retval = 1;
174437df9d3bSAlan Somers 			return (0);
174537df9d3bSAlan Somers 		} else {
174637df9d3bSAlan Somers 			/*
174737df9d3bSAlan Somers 			 * Probably FUSE_LSEEK is not implemented.  It might
174837df9d3bSAlan Somers 			 * be, if the FUSE_LSEEK above returned an error like
174937df9d3bSAlan Somers 			 * EACCES, but in that case we can't tell, so it's
175037df9d3bSAlan Somers 			 * safest to report EINVAL anyway.
175137df9d3bSAlan Somers 			 */
175237df9d3bSAlan Somers 			return (EINVAL);
175337df9d3bSAlan Somers 		}
1754746c92e0SJohn Baldwin 	default:
1755746c92e0SJohn Baldwin 		return (vop_stdpathconf(ap));
1756746c92e0SJohn Baldwin 	}
1757746c92e0SJohn Baldwin }
1758746c92e0SJohn Baldwin 
1759dc433e15SAlan Somers SDT_PROBE_DEFINE3(fusefs, , vnops, filehandles_closed, "struct vnode*",
1760dc433e15SAlan Somers     "struct uio*", "struct ucred*");
17615fe58019SAttilio Rao /*
17625fe58019SAttilio Rao     struct vnop_read_args {
17635fe58019SAttilio Rao 	struct vnode *a_vp;
17645fe58019SAttilio Rao 	struct uio *a_uio;
17655fe58019SAttilio Rao 	int  a_ioflag;
17665fe58019SAttilio Rao 	struct ucred *a_cred;
17675fe58019SAttilio Rao     };
17685fe58019SAttilio Rao */
17695fe58019SAttilio Rao static int
17705fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap)
17715fe58019SAttilio Rao {
17725fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
17735fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
17745fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
17755fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1776f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
1777dc433e15SAlan Somers 	struct fuse_filehandle *fufh;
1778dc433e15SAlan Somers 	int err;
1779dc433e15SAlan Somers 	bool closefufh = false, directio;
1780dc433e15SAlan Somers 
1781dc433e15SAlan Somers 	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
17825fe58019SAttilio Rao 
17835fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
17845fe58019SAttilio Rao 		return ENXIO;
17855fe58019SAttilio Rao 	}
1786ead063e0SEdward Tomasz Napierala 
1787ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1788ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1789ead063e0SEdward Tomasz Napierala 	}
1790ead063e0SEdward Tomasz Napierala 
1791dc433e15SAlan Somers 	err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
1792dc433e15SAlan Somers 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1793dc433e15SAlan Somers 		/*
1794dc433e15SAlan Somers 		 * nfsd will do I/O without first doing VOP_OPEN.  We
1795dc433e15SAlan Somers 		 * must implicitly open the file here
1796dc433e15SAlan Somers 		 */
1797dc433e15SAlan Somers 		err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1798dc433e15SAlan Somers 		closefufh = true;
1799dc433e15SAlan Somers 	}
1800dc433e15SAlan Somers 	if (err) {
1801dc433e15SAlan Somers 		SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
1802dc433e15SAlan Somers 		return err;
1803dc433e15SAlan Somers 	}
1804dc433e15SAlan Somers 
1805dc433e15SAlan Somers 	/*
1806dc433e15SAlan Somers          * Ideally, when the daemon asks for direct io at open time, the
1807dc433e15SAlan Somers          * standard file flag should be set according to this, so that would
1808dc433e15SAlan Somers          * just change the default mode, which later on could be changed via
1809dc433e15SAlan Somers          * fcntl(2).
1810dc433e15SAlan Somers          * But this doesn't work, the O_DIRECT flag gets cleared at some point
1811dc433e15SAlan Somers          * (don't know where). So to make any use of the Fuse direct_io option,
1812dc433e15SAlan Somers          * we hardwire it into the file's private data (similarly to Linux,
1813dc433e15SAlan Somers          * btw.).
1814dc433e15SAlan Somers          */
1815dc433e15SAlan Somers 	directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
1816dc433e15SAlan Somers 
1817dc433e15SAlan Somers 	fuse_vnode_update(vp, FN_ATIMECHANGE);
1818dc433e15SAlan Somers 	if (directio) {
1819dc433e15SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1, "direct read of vnode");
1820dc433e15SAlan Somers 		err = fuse_read_directbackend(vp, uio, cred, fufh);
1821dc433e15SAlan Somers 	} else {
1822dc433e15SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1, "buffered read of vnode");
1823dc433e15SAlan Somers 		err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh, pid);
1824dc433e15SAlan Somers 	}
1825dc433e15SAlan Somers 
1826dc433e15SAlan Somers 	if (closefufh)
1827dc433e15SAlan Somers 		fuse_filehandle_close(vp, fufh, curthread, cred);
1828dc433e15SAlan Somers 
1829dc433e15SAlan Somers 	return (err);
18305fe58019SAttilio Rao }
18315fe58019SAttilio Rao 
18325fe58019SAttilio Rao /*
18335fe58019SAttilio Rao     struct vnop_readdir_args {
18345fe58019SAttilio Rao 	struct vnode *a_vp;
18355fe58019SAttilio Rao 	struct uio *a_uio;
18365fe58019SAttilio Rao 	struct ucred *a_cred;
18375fe58019SAttilio Rao 	int *a_eofflag;
1838e76986fdSAlan Somers 	int *a_ncookies;
1839b214fcceSAlan Somers 	uint64_t **a_cookies;
18405fe58019SAttilio Rao     };
18415fe58019SAttilio Rao */
18425fe58019SAttilio Rao static int
18435fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap)
18445fe58019SAttilio Rao {
18455fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
18465fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
18475fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
18485fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
184900134a07SAlan Somers 	struct mount *mp = vnode_mount(vp);
18505fe58019SAttilio Rao 	struct fuse_iov cookediov;
18515fe58019SAttilio Rao 	int err = 0;
1852b214fcceSAlan Somers 	uint64_t *cookies;
1853e76986fdSAlan Somers 	ssize_t tresid;
1854e76986fdSAlan Somers 	int ncookies;
1855e76986fdSAlan Somers 	bool closefufh = false;
1856f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
18575fe58019SAttilio Rao 
1858e76986fdSAlan Somers 	if (ap->a_eofflag)
1859e76986fdSAlan Somers 		*ap->a_eofflag = 0;
18605fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
18615fe58019SAttilio Rao 		return ENXIO;
18625fe58019SAttilio Rao 	}
18635fe58019SAttilio Rao 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
18645fe58019SAttilio Rao 	    (uio_resid(uio) < sizeof(struct dirent))) {
18655fe58019SAttilio Rao 		return EINVAL;
18665fe58019SAttilio Rao 	}
18675fe58019SAttilio Rao 
1868e76986fdSAlan Somers 	tresid = uio->uio_resid;
186935cf0e7eSAlan Somers 	err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
187000134a07SAlan Somers 	if (err == EBADF && mp->mnt_flag & MNT_EXPORTED) {
187118ed2ce7SAlan Somers 		KASSERT(fuse_get_mpdata(mp)->dataflags
187218ed2ce7SAlan Somers 				& FSESS_NO_OPENDIR_SUPPORT,
187300134a07SAlan Somers 			("FUSE file systems that don't set "
187400134a07SAlan Somers 			 "FUSE_NO_OPENDIR_SUPPORT should not be exported"));
1875e76986fdSAlan Somers 		/*
1876e76986fdSAlan Somers 		 * nfsd will do VOP_READDIR without first doing VOP_OPEN.  We
187700134a07SAlan Somers 		 * must implicitly open the directory here.
1878e76986fdSAlan Somers 		 */
1879e76986fdSAlan Somers 		err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1880e76986fdSAlan Somers 		closefufh = true;
1881e76986fdSAlan Somers 	}
188235cf0e7eSAlan Somers 	if (err)
18835fe58019SAttilio Rao 		return (err);
1884e76986fdSAlan Somers 	if (ap->a_ncookies != NULL) {
1885e76986fdSAlan Somers 		ncookies = uio->uio_resid /
1886e76986fdSAlan Somers 			(offsetof(struct dirent, d_name) + 4) + 1;
1887e76986fdSAlan Somers 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1888e76986fdSAlan Somers 		*ap->a_ncookies = ncookies;
1889e76986fdSAlan Somers 		*ap->a_cookies = cookies;
1890e76986fdSAlan Somers 	} else {
1891e76986fdSAlan Somers 		ncookies = 0;
1892e76986fdSAlan Somers 		cookies = NULL;
1893e76986fdSAlan Somers 	}
18945fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
18955fe58019SAttilio Rao 	fiov_init(&cookediov, DIRCOOKEDSIZE);
18965fe58019SAttilio Rao 
189700134a07SAlan Somers 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov,
1898e76986fdSAlan Somers 		&ncookies, cookies);
18995fe58019SAttilio Rao 
19005fe58019SAttilio Rao 	fiov_teardown(&cookediov);
1901e76986fdSAlan Somers 	if (closefufh)
1902e76986fdSAlan Somers 		fuse_filehandle_close(vp, fufh, curthread, cred);
1903e76986fdSAlan Somers 
1904e76986fdSAlan Somers 	if (ap->a_ncookies != NULL) {
1905e76986fdSAlan Somers 		if (err == 0) {
1906e76986fdSAlan Somers 			*ap->a_ncookies -= ncookies;
1907e76986fdSAlan Somers 		} else {
1908e76986fdSAlan Somers 			free(*ap->a_cookies, M_TEMP);
1909e76986fdSAlan Somers 			*ap->a_ncookies = 0;
1910e76986fdSAlan Somers 			*ap->a_cookies = NULL;
1911e76986fdSAlan Somers 		}
1912e76986fdSAlan Somers 	}
1913e76986fdSAlan Somers 	if (err == 0 && tresid == uio->uio_resid)
1914e76986fdSAlan Somers 		*ap->a_eofflag = 1;
19155ec10aa5SAlan Somers 
19165fe58019SAttilio Rao 	return err;
19175fe58019SAttilio Rao }
19185fe58019SAttilio Rao 
19195fe58019SAttilio Rao /*
19205fe58019SAttilio Rao     struct vnop_readlink_args {
19215fe58019SAttilio Rao 	struct vnode *a_vp;
19225fe58019SAttilio Rao 	struct uio *a_uio;
19235fe58019SAttilio Rao 	struct ucred *a_cred;
19245fe58019SAttilio Rao     };
19255fe58019SAttilio Rao */
19265fe58019SAttilio Rao static int
19275fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap)
19285fe58019SAttilio Rao {
19295fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
19305fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
19315fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
19325fe58019SAttilio Rao 
19335fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
19345fe58019SAttilio Rao 	int err;
19355fe58019SAttilio Rao 
19365fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
19375fe58019SAttilio Rao 		return ENXIO;
19385fe58019SAttilio Rao 	}
19395fe58019SAttilio Rao 	if (!vnode_islnk(vp)) {
19405fe58019SAttilio Rao 		return EINVAL;
19415fe58019SAttilio Rao 	}
19425fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
19435fe58019SAttilio Rao 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
19445fe58019SAttilio Rao 	if (err) {
19455fe58019SAttilio Rao 		goto out;
19465fe58019SAttilio Rao 	}
19475fe58019SAttilio Rao 	if (((char *)fdi.answ)[0] == '/' &&
19485fe58019SAttilio Rao 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
19495fe58019SAttilio Rao 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
19505fe58019SAttilio Rao 
19515fe58019SAttilio Rao 		err = uiomove(mpth, strlen(mpth), uio);
19525fe58019SAttilio Rao 	}
19535fe58019SAttilio Rao 	if (!err) {
19545fe58019SAttilio Rao 		err = uiomove(fdi.answ, fdi.iosize, uio);
19555fe58019SAttilio Rao 	}
19565fe58019SAttilio Rao out:
19575fe58019SAttilio Rao 	fdisp_destroy(&fdi);
19585fe58019SAttilio Rao 	return err;
19595fe58019SAttilio Rao }
19605fe58019SAttilio Rao 
19615fe58019SAttilio Rao /*
19625fe58019SAttilio Rao     struct vnop_reclaim_args {
19635fe58019SAttilio Rao 	struct vnode *a_vp;
19645fe58019SAttilio Rao     };
19655fe58019SAttilio Rao */
19665fe58019SAttilio Rao static int
19675fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap)
19685fe58019SAttilio Rao {
19695fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
19708f226f4cSMateusz Guzik 	struct thread *td = curthread;
19715fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
19725ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
19735fe58019SAttilio Rao 
19745fe58019SAttilio Rao 	if (!fvdat) {
19755fe58019SAttilio Rao 		panic("FUSE: no vnode data during recycling");
19765fe58019SAttilio Rao 	}
19775ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
19785ec10aa5SAlan Somers 		printf("FUSE: vnode being reclaimed with open fufh "
19799e444871SAlan Somers 			"(type=%#x)", fufh->fufh_type);
19805ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
19815fe58019SAttilio Rao 	}
19825fe58019SAttilio Rao 
198342767f76SAlan Somers 	if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
19845fe58019SAttilio Rao 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
19855fe58019SAttilio Rao 		    fvdat->nlookup);
19865fe58019SAttilio Rao 	}
19875fe58019SAttilio Rao 	cache_purge(vp);
19885fe58019SAttilio Rao 	vfs_hash_remove(vp);
19895fe58019SAttilio Rao 	fuse_vnode_destroy(vp);
19905fe58019SAttilio Rao 
19915fe58019SAttilio Rao 	return 0;
19925fe58019SAttilio Rao }
19935fe58019SAttilio Rao 
19945fe58019SAttilio Rao /*
19955fe58019SAttilio Rao     struct vnop_remove_args {
19965fe58019SAttilio Rao 	struct vnode *a_dvp;
19975fe58019SAttilio Rao 	struct vnode *a_vp;
19985fe58019SAttilio Rao 	struct componentname *a_cnp;
19995fe58019SAttilio Rao     };
20005fe58019SAttilio Rao */
20015fe58019SAttilio Rao static int
20025fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap)
20035fe58019SAttilio Rao {
20045fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
20055fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
20065fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
20075fe58019SAttilio Rao 
20085fe58019SAttilio Rao 	int err;
20095fe58019SAttilio Rao 
20105fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
20115fe58019SAttilio Rao 		return ENXIO;
20125fe58019SAttilio Rao 	}
20135fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
20145fe58019SAttilio Rao 		return EPERM;
20155fe58019SAttilio Rao 	}
20165fe58019SAttilio Rao 
20175fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
20185fe58019SAttilio Rao 
20195fe58019SAttilio Rao 	return err;
20205fe58019SAttilio Rao }
20215fe58019SAttilio Rao 
20225fe58019SAttilio Rao /*
20235fe58019SAttilio Rao     struct vnop_rename_args {
20245fe58019SAttilio Rao 	struct vnode *a_fdvp;
20255fe58019SAttilio Rao 	struct vnode *a_fvp;
20265fe58019SAttilio Rao 	struct componentname *a_fcnp;
20275fe58019SAttilio Rao 	struct vnode *a_tdvp;
20285fe58019SAttilio Rao 	struct vnode *a_tvp;
20295fe58019SAttilio Rao 	struct componentname *a_tcnp;
20305fe58019SAttilio Rao     };
20315fe58019SAttilio Rao */
20325fe58019SAttilio Rao static int
20335fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap)
20345fe58019SAttilio Rao {
20355fe58019SAttilio Rao 	struct vnode *fdvp = ap->a_fdvp;
20365fe58019SAttilio Rao 	struct vnode *fvp = ap->a_fvp;
20375fe58019SAttilio Rao 	struct componentname *fcnp = ap->a_fcnp;
20385fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
20395fe58019SAttilio Rao 	struct vnode *tvp = ap->a_tvp;
20405fe58019SAttilio Rao 	struct componentname *tcnp = ap->a_tcnp;
20415fe58019SAttilio Rao 	struct fuse_data *data;
20428e45ec4eSAlan Somers 	bool newparent = fdvp != tdvp;
20438e45ec4eSAlan Somers 	bool isdir = fvp->v_type == VDIR;
20445fe58019SAttilio Rao 	int err = 0;
20455fe58019SAttilio Rao 
20465fe58019SAttilio Rao 	if (fuse_isdeadfs(fdvp)) {
20475fe58019SAttilio Rao 		return ENXIO;
20485fe58019SAttilio Rao 	}
20495fe58019SAttilio Rao 	if (fvp->v_mount != tdvp->v_mount ||
20505fe58019SAttilio Rao 	    (tvp && fvp->v_mount != tvp->v_mount)) {
2051419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
20525fe58019SAttilio Rao 		err = EXDEV;
20535fe58019SAttilio Rao 		goto out;
20545fe58019SAttilio Rao 	}
20555fe58019SAttilio Rao 	cache_purge(fvp);
20565fe58019SAttilio Rao 
20575fe58019SAttilio Rao 	/*
20585fe58019SAttilio Rao 	 * FUSE library is expected to check if target directory is not
20595fe58019SAttilio Rao 	 * under the source directory in the file system tree.
20605fe58019SAttilio Rao 	 * Linux performs this check at VFS level.
20615fe58019SAttilio Rao 	 */
20628e45ec4eSAlan Somers 	/*
20638e45ec4eSAlan Somers 	 * If source is a directory, and it will get a new parent, user must
20648e45ec4eSAlan Somers 	 * have write permission to it, so ".." can be modified.
20658e45ec4eSAlan Somers 	 */
20665fe58019SAttilio Rao 	data = fuse_get_mpdata(vnode_mount(tdvp));
20678e45ec4eSAlan Somers 	if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
20688e45ec4eSAlan Somers 		err = fuse_internal_access(fvp, VWRITE,
2069b4a58fbfSMateusz Guzik 			curthread, tcnp->cn_cred);
20708e45ec4eSAlan Somers 		if (err)
20718e45ec4eSAlan Somers 			goto out;
20728e45ec4eSAlan Somers 	}
20735fe58019SAttilio Rao 	sx_xlock(&data->rename_lock);
20745fe58019SAttilio Rao 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
20755fe58019SAttilio Rao 	if (err == 0) {
2076d9454fabSAttilio Rao 		if (tdvp != fdvp)
20775fe58019SAttilio Rao 			fuse_vnode_setparent(fvp, tdvp);
20785fe58019SAttilio Rao 		if (tvp != NULL)
20795fe58019SAttilio Rao 			fuse_vnode_setparent(tvp, NULL);
20805fe58019SAttilio Rao 	}
20815fe58019SAttilio Rao 	sx_unlock(&data->rename_lock);
20825fe58019SAttilio Rao 
20835fe58019SAttilio Rao 	if (tvp != NULL && tvp != fvp) {
20845fe58019SAttilio Rao 		cache_purge(tvp);
20855fe58019SAttilio Rao 	}
20865fe58019SAttilio Rao 	if (vnode_isdir(fvp)) {
2087e8553be9SAlan Somers 		if (((tvp != NULL) && vnode_isdir(tvp)) || vnode_isdir(fvp)) {
20885fe58019SAttilio Rao 			cache_purge(tdvp);
20895fe58019SAttilio Rao 		}
20905fe58019SAttilio Rao 		cache_purge(fdvp);
20915fe58019SAttilio Rao 	}
20925fe58019SAttilio Rao out:
20935fe58019SAttilio Rao 	if (tdvp == tvp) {
20945fe58019SAttilio Rao 		vrele(tdvp);
20955fe58019SAttilio Rao 	} else {
20965fe58019SAttilio Rao 		vput(tdvp);
20975fe58019SAttilio Rao 	}
20985fe58019SAttilio Rao 	if (tvp != NULL) {
20995fe58019SAttilio Rao 		vput(tvp);
21005fe58019SAttilio Rao 	}
21015fe58019SAttilio Rao 	vrele(fdvp);
21025fe58019SAttilio Rao 	vrele(fvp);
21035fe58019SAttilio Rao 
21045fe58019SAttilio Rao 	return err;
21055fe58019SAttilio Rao }
21065fe58019SAttilio Rao 
21075fe58019SAttilio Rao /*
21085fe58019SAttilio Rao     struct vnop_rmdir_args {
21095fe58019SAttilio Rao 	    struct vnode *a_dvp;
21105fe58019SAttilio Rao 	    struct vnode *a_vp;
21115fe58019SAttilio Rao 	    struct componentname *a_cnp;
21125fe58019SAttilio Rao     } *ap;
21135fe58019SAttilio Rao */
21145fe58019SAttilio Rao static int
21155fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap)
21165fe58019SAttilio Rao {
21175fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
21185fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
21195fe58019SAttilio Rao 
21205fe58019SAttilio Rao 	int err;
21215fe58019SAttilio Rao 
21225fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
21235fe58019SAttilio Rao 		return ENXIO;
21245fe58019SAttilio Rao 	}
21255fe58019SAttilio Rao 	if (VTOFUD(vp) == VTOFUD(dvp)) {
21265fe58019SAttilio Rao 		return EINVAL;
21275fe58019SAttilio Rao 	}
21285fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
21295fe58019SAttilio Rao 
21305fe58019SAttilio Rao 	return err;
21315fe58019SAttilio Rao }
21325fe58019SAttilio Rao 
21335fe58019SAttilio Rao /*
21345fe58019SAttilio Rao     struct vnop_setattr_args {
21355fe58019SAttilio Rao 	struct vnode *a_vp;
21365fe58019SAttilio Rao 	struct vattr *a_vap;
21375fe58019SAttilio Rao 	struct ucred *a_cred;
21385fe58019SAttilio Rao 	struct thread *a_td;
21395fe58019SAttilio Rao     };
21405fe58019SAttilio Rao */
21415fe58019SAttilio Rao static int
21425fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap)
21435fe58019SAttilio Rao {
21445fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
21455fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
21465fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
21475fe58019SAttilio Rao 	struct thread *td = curthread;
2148474ba6faSAlan Somers 	struct mount *mp;
2149474ba6faSAlan Somers 	struct fuse_data *data;
21504e83d655SAlan Somers 	struct vattr old_va;
2151474ba6faSAlan Somers 	int dataflags;
21524e83d655SAlan Somers 	int err = 0, err2;
2153ff4fbdf5SAlan Somers 	accmode_t accmode = 0;
2154a90e32deSAlan Somers 	bool checkperm;
2155a2bdd737SAlan Somers 	bool drop_suid = false;
21564e83d655SAlan Somers 	gid_t cr_gid;
21575fe58019SAttilio Rao 
2158474ba6faSAlan Somers 	mp = vnode_mount(vp);
2159474ba6faSAlan Somers 	data = fuse_get_mpdata(mp);
2160474ba6faSAlan Somers 	dataflags = data->dataflags;
2161a90e32deSAlan Somers 	checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
21624e83d655SAlan Somers 	if (cred->cr_ngroups > 0)
21634e83d655SAlan Somers 		cr_gid = cred->cr_groups[0];
21644e83d655SAlan Somers 	else
21654e83d655SAlan Somers 		cr_gid = 0;
2166474ba6faSAlan Somers 
21675fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
21685fe58019SAttilio Rao 		return ENXIO;
21695fe58019SAttilio Rao 	}
21705fe58019SAttilio Rao 
21715fe58019SAttilio Rao 	if (vap->va_uid != (uid_t)VNOVAL) {
2172a90e32deSAlan Somers 		if (checkperm) {
2173474ba6faSAlan Somers 			/* Only root may change a file's owner */
2174474ba6faSAlan Somers 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
21754e83d655SAlan Somers 			if (err) {
21764e83d655SAlan Somers 				/* As a special case, allow the null chown */
21774e83d655SAlan Somers 				err2 = fuse_internal_getattr(vp, &old_va, cred,
21784e83d655SAlan Somers 					td);
21794e83d655SAlan Somers 				if (err2)
21804e83d655SAlan Somers 					return (err2);
21814e83d655SAlan Somers 				if (vap->va_uid != old_va.va_uid)
21824e83d655SAlan Somers 					return err;
21834e83d655SAlan Somers 				else
21844e83d655SAlan Somers 					accmode |= VADMIN;
2185a2bdd737SAlan Somers 				drop_suid = true;
21864e83d655SAlan Somers 			} else
21874e83d655SAlan Somers 				accmode |= VADMIN;
21884e83d655SAlan Somers 		} else
2189ff4fbdf5SAlan Somers 			accmode |= VADMIN;
21905fe58019SAttilio Rao 	}
21915fe58019SAttilio Rao 	if (vap->va_gid != (gid_t)VNOVAL) {
2192a2bdd737SAlan Somers 		if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
2193a2bdd737SAlan Somers 			drop_suid = true;
2194a90e32deSAlan Somers 		if (checkperm && !groupmember(vap->va_gid, cred))
2195474ba6faSAlan Somers 		{
2196474ba6faSAlan Somers 			/*
2197474ba6faSAlan Somers 			 * Non-root users may only chgrp to one of their own
2198474ba6faSAlan Somers 			 * groups
2199474ba6faSAlan Somers 			 */
2200474ba6faSAlan Somers 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
22014e83d655SAlan Somers 			if (err) {
22024e83d655SAlan Somers 				/* As a special case, allow the null chgrp */
22034e83d655SAlan Somers 				err2 = fuse_internal_getattr(vp, &old_va, cred,
22044e83d655SAlan Somers 					td);
22054e83d655SAlan Somers 				if (err2)
22064e83d655SAlan Somers 					return (err2);
22074e83d655SAlan Somers 				if (vap->va_gid != old_va.va_gid)
2208a90e32deSAlan Somers 					return err;
22094e83d655SAlan Somers 				accmode |= VADMIN;
22104e83d655SAlan Somers 			} else
22114e83d655SAlan Somers 				accmode |= VADMIN;
22124e83d655SAlan Somers 		} else
2213ff4fbdf5SAlan Somers 			accmode |= VADMIN;
22145fe58019SAttilio Rao 	}
22155fe58019SAttilio Rao 	if (vap->va_size != VNOVAL) {
2216a90e32deSAlan Somers 		switch (vp->v_type) {
2217a90e32deSAlan Somers 		case VDIR:
2218a90e32deSAlan Somers 			return (EISDIR);
2219a90e32deSAlan Somers 		case VLNK:
2220a90e32deSAlan Somers 		case VREG:
2221a90e32deSAlan Somers 			if (vfs_isrdonly(mp))
2222a90e32deSAlan Somers 				return (EROFS);
2223a90e32deSAlan Somers 			break;
2224a90e32deSAlan Somers 		default:
2225a90e32deSAlan Somers 			/*
2226a90e32deSAlan Somers 			 * According to POSIX, the result is unspecified
2227a90e32deSAlan Somers 			 * for file types other than regular files,
2228a90e32deSAlan Somers 			 * directories and shared memory objects.  We
2229a90e32deSAlan Somers 			 * don't support shared memory objects in the file
2230a90e32deSAlan Somers 			 * system, and have dubious support for truncating
2231a90e32deSAlan Somers 			 * symlinks.  Just ignore the request in other cases.
2232a90e32deSAlan Somers 			 */
2233a90e32deSAlan Somers 			return (0);
2234a90e32deSAlan Somers 		}
22353fa12789SAlan Somers 		/* Don't set accmode.  Permission to trunc is checked upstack */
22365fe58019SAttilio Rao 	}
2237d943c93eSAlan Somers 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2238d943c93eSAlan Somers 		if (vap->va_vaflags & VA_UTIMES_NULL)
2239d943c93eSAlan Somers 			accmode |= VWRITE;
2240d943c93eSAlan Somers 		else
2241ff4fbdf5SAlan Somers 			accmode |= VADMIN;
2242d943c93eSAlan Somers 	}
2243a2bdd737SAlan Somers 	if (drop_suid) {
2244a2bdd737SAlan Somers 		if (vap->va_mode != (mode_t)VNOVAL)
2245a2bdd737SAlan Somers 			vap->va_mode &= ~(S_ISUID | S_ISGID);
2246a2bdd737SAlan Somers 		else {
2247a2bdd737SAlan Somers 			err = fuse_internal_getattr(vp, &old_va, cred, td);
2248a2bdd737SAlan Somers 			if (err)
2249a2bdd737SAlan Somers 				return (err);
2250a2bdd737SAlan Somers 			vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2251a2bdd737SAlan Somers 		}
2252a2bdd737SAlan Somers 	}
22535fe58019SAttilio Rao 	if (vap->va_mode != (mode_t)VNOVAL) {
2254e5ff3a7eSAlan Somers 		/* Only root may set the sticky bit on non-directories */
2255a90e32deSAlan Somers 		if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2256a90e32deSAlan Somers 		    && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2257a90e32deSAlan Somers 			return EFTYPE;
22588cfb4431SAlan Somers 		if (checkperm && (vap->va_mode & S_ISGID)) {
22598cfb4431SAlan Somers 			err = fuse_internal_getattr(vp, &old_va, cred, td);
22608cfb4431SAlan Somers 			if (err)
22618cfb4431SAlan Somers 				return (err);
22628cfb4431SAlan Somers 			if (!groupmember(old_va.va_gid, cred)) {
22638cfb4431SAlan Somers 				err = priv_check_cred(cred, PRIV_VFS_SETGID);
22648cfb4431SAlan Somers 				if (err)
22658cfb4431SAlan Somers 					return (err);
22668cfb4431SAlan Somers 			}
22678cfb4431SAlan Somers 		}
2268ff4fbdf5SAlan Somers 		accmode |= VADMIN;
22695fe58019SAttilio Rao 	}
22705fe58019SAttilio Rao 
2271a90e32deSAlan Somers 	if (vfs_isrdonly(mp))
2272a90e32deSAlan Somers 		return EROFS;
2273a90e32deSAlan Somers 
2274bfcb817bSAlan Somers 	if (checkperm) {
2275666f8543SAlan Somers 		err = fuse_internal_access(vp, accmode, td, cred);
2276bfcb817bSAlan Somers 	} else {
2277bfcb817bSAlan Somers 		err = 0;
2278bfcb817bSAlan Somers 	}
2279ff4fbdf5SAlan Somers 	if (err)
22805fe58019SAttilio Rao 		return err;
2281a90e32deSAlan Somers 	else
2282a90e32deSAlan Somers 		return fuse_internal_setattr(vp, vap, td, cred);
22835fe58019SAttilio Rao }
22845fe58019SAttilio Rao 
22855fe58019SAttilio Rao /*
22865fe58019SAttilio Rao     struct vnop_strategy_args {
22875fe58019SAttilio Rao 	struct vnode *a_vp;
22885fe58019SAttilio Rao 	struct buf *a_bp;
22895fe58019SAttilio Rao     };
22905fe58019SAttilio Rao */
22915fe58019SAttilio Rao static int
22925fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap)
22935fe58019SAttilio Rao {
22945fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
22955fe58019SAttilio Rao 	struct buf *bp = ap->a_bp;
22965fe58019SAttilio Rao 
22975fe58019SAttilio Rao 	if (!vp || fuse_isdeadfs(vp)) {
22985fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
22995fe58019SAttilio Rao 		bp->b_error = ENXIO;
23005fe58019SAttilio Rao 		bufdone(bp);
230198852a32SAlan Somers 		return 0;
23025fe58019SAttilio Rao 	}
23035fe58019SAttilio Rao 
23045fe58019SAttilio Rao 	/*
2305f203d173SAlan Somers 	 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2306f203d173SAlan Somers 	 * fuse_io_strategy sets bp's error fields
23075fe58019SAttilio Rao 	 */
2308f203d173SAlan Somers 	(void)fuse_io_strategy(vp, bp);
2309f203d173SAlan Somers 
23105fe58019SAttilio Rao 	return 0;
23115fe58019SAttilio Rao }
23125fe58019SAttilio Rao 
23135fe58019SAttilio Rao /*
23145fe58019SAttilio Rao     struct vnop_symlink_args {
23155fe58019SAttilio Rao 	struct vnode *a_dvp;
23165fe58019SAttilio Rao 	struct vnode **a_vpp;
23175fe58019SAttilio Rao 	struct componentname *a_cnp;
23185fe58019SAttilio Rao 	struct vattr *a_vap;
23195fe58019SAttilio Rao 	char *a_target;
23205fe58019SAttilio Rao     };
23215fe58019SAttilio Rao */
23225fe58019SAttilio Rao static int
23235fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap)
23245fe58019SAttilio Rao {
23255fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
23265fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
23275fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
23281493c2eeSBrooks Davis 	const char *target = ap->a_target;
23295fe58019SAttilio Rao 
23305fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
23315fe58019SAttilio Rao 
23325fe58019SAttilio Rao 	int err;
23335fe58019SAttilio Rao 	size_t len;
23345fe58019SAttilio Rao 
23355fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
23365fe58019SAttilio Rao 		return ENXIO;
23375fe58019SAttilio Rao 	}
23385fe58019SAttilio Rao 	/*
23395fe58019SAttilio Rao 	 * Unlike the other creator type calls, here we have to create a message
23405fe58019SAttilio Rao 	 * where the name of the new entry comes first, and the data describing
23415fe58019SAttilio Rao 	 * the entry comes second.
23425fe58019SAttilio Rao 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
23435fe58019SAttilio Rao 	 * but put together the message manually and just call the core part.
23445fe58019SAttilio Rao 	 */
23455fe58019SAttilio Rao 
23465fe58019SAttilio Rao 	len = strlen(target) + 1;
23475fe58019SAttilio Rao 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
23485fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
23495fe58019SAttilio Rao 
23505fe58019SAttilio Rao 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
23515fe58019SAttilio Rao 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
23525fe58019SAttilio Rao 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
23535fe58019SAttilio Rao 
23545fe58019SAttilio Rao 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
23555fe58019SAttilio Rao 	fdisp_destroy(&fdi);
23565fe58019SAttilio Rao 	return err;
23575fe58019SAttilio Rao }
23585fe58019SAttilio Rao 
23595fe58019SAttilio Rao /*
23605fe58019SAttilio Rao     struct vnop_write_args {
23615fe58019SAttilio Rao 	struct vnode *a_vp;
23625fe58019SAttilio Rao 	struct uio *a_uio;
23635fe58019SAttilio Rao 	int  a_ioflag;
23645fe58019SAttilio Rao 	struct ucred *a_cred;
23655fe58019SAttilio Rao     };
23665fe58019SAttilio Rao */
23675fe58019SAttilio Rao static int
23685fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap)
23695fe58019SAttilio Rao {
23705fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
23715fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
23725fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
23735fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2374f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
2375dc433e15SAlan Somers 	struct fuse_filehandle *fufh;
2376dc433e15SAlan Somers 	int err;
2377dc433e15SAlan Somers 	bool closefufh = false, directio;
2378dc433e15SAlan Somers 
2379dc433e15SAlan Somers 	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
23805fe58019SAttilio Rao 
23815fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
23825fe58019SAttilio Rao 		return ENXIO;
23835fe58019SAttilio Rao 	}
23845fe58019SAttilio Rao 
2385ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2386ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
2387ead063e0SEdward Tomasz Napierala 	}
2388ead063e0SEdward Tomasz Napierala 
2389dc433e15SAlan Somers 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2390dc433e15SAlan Somers 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2391dc433e15SAlan Somers 		/*
2392dc433e15SAlan Somers 		 * nfsd will do I/O without first doing VOP_OPEN.  We
2393dc433e15SAlan Somers 		 * must implicitly open the file here
2394dc433e15SAlan Somers 		 */
2395dc433e15SAlan Somers 		err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2396dc433e15SAlan Somers 		closefufh = true;
2397dc433e15SAlan Somers 	}
2398dc433e15SAlan Somers 	if (err) {
2399dc433e15SAlan Somers 		SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
2400dc433e15SAlan Somers 		return err;
2401dc433e15SAlan Somers 	}
2402dc433e15SAlan Somers 
2403dc433e15SAlan Somers 	/*
2404dc433e15SAlan Somers          * Ideally, when the daemon asks for direct io at open time, the
2405dc433e15SAlan Somers          * standard file flag should be set according to this, so that would
2406dc433e15SAlan Somers          * just change the default mode, which later on could be changed via
2407dc433e15SAlan Somers          * fcntl(2).
2408dc433e15SAlan Somers          * But this doesn't work, the O_DIRECT flag gets cleared at some point
2409dc433e15SAlan Somers          * (don't know where). So to make any use of the Fuse direct_io option,
2410dc433e15SAlan Somers          * we hardwire it into the file's private data (similarly to Linux,
2411dc433e15SAlan Somers          * btw.).
2412dc433e15SAlan Somers          */
2413dc433e15SAlan Somers 	directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
2414dc433e15SAlan Somers 
2415dc433e15SAlan Somers 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2416dc433e15SAlan Somers 	if (directio) {
2417dc433e15SAlan Somers 		off_t start, end, filesize;
2418dc433e15SAlan Somers 		bool pages = (ioflag & IO_VMIO) != 0;
2419dc433e15SAlan Somers 
2420dc433e15SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1, "direct write of vnode");
2421dc433e15SAlan Somers 
2422dc433e15SAlan Somers 		err = fuse_vnode_size(vp, &filesize, cred, curthread);
2423dc433e15SAlan Somers 		if (err)
2424dc433e15SAlan Somers 			goto out;
2425dc433e15SAlan Somers 
2426dc433e15SAlan Somers 		start = uio->uio_offset;
2427dc433e15SAlan Somers 		end = start + uio->uio_resid;
2428dc433e15SAlan Somers 		if (!pages) {
2429dc433e15SAlan Somers 			err = fuse_inval_buf_range(vp, filesize, start,
2430dc433e15SAlan Somers 			    end);
2431dc433e15SAlan Somers 			if (err)
2432dc433e15SAlan Somers 				goto out;
2433dc433e15SAlan Somers 		}
2434dc433e15SAlan Somers 		err = fuse_write_directbackend(vp, uio, cred, fufh,
2435dc433e15SAlan Somers 			filesize, ioflag, pages);
2436dc433e15SAlan Somers 	} else {
2437dc433e15SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
2438dc433e15SAlan Somers 			"buffered write of vnode");
2439dc433e15SAlan Somers 		if (!fsess_opt_writeback(vnode_mount(vp)))
2440dc433e15SAlan Somers 			ioflag |= IO_SYNC;
2441dc433e15SAlan Somers 		err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag, pid);
2442dc433e15SAlan Somers 	}
2443dc433e15SAlan Somers 	fuse_internal_clear_suid_on_write(vp, cred, uio->uio_td);
2444dc433e15SAlan Somers 
2445dc433e15SAlan Somers out:
2446dc433e15SAlan Somers 	if (closefufh)
2447dc433e15SAlan Somers 		fuse_filehandle_close(vp, fufh, curthread, cred);
2448dc433e15SAlan Somers 
2449dc433e15SAlan Somers 	return (err);
24505fe58019SAttilio Rao }
24515fe58019SAttilio Rao 
2452b9e20197SAlan Somers static daddr_t
2453b9e20197SAlan Somers fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2454b9e20197SAlan Somers {
2455b9e20197SAlan Somers 	const int biosize = fuse_iosize(vp);
2456b9e20197SAlan Somers 
2457b9e20197SAlan Somers 	return (off / biosize);
2458b9e20197SAlan Somers }
2459b9e20197SAlan Somers 
2460b9e20197SAlan Somers static int
24614f917847SAlan Somers fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2462b9e20197SAlan Somers {
2463b9e20197SAlan Somers 	off_t filesize;
24644f917847SAlan Somers 	int err;
2465b9e20197SAlan Somers 	const int biosize = fuse_iosize(vp);
2466b9e20197SAlan Somers 
2467b9e20197SAlan Somers 	err = fuse_vnode_size(vp, &filesize, NULL, NULL);
24684f917847SAlan Somers 	if (err) {
24694f917847SAlan Somers 		/* This will turn into a SIGBUS */
24704f917847SAlan Somers 		return (EIO);
24714f917847SAlan Somers 	} else if ((off_t)lbn * biosize >= filesize) {
24724f917847SAlan Somers 		*blksz = 0;
2473b9e20197SAlan Somers 	} else if ((off_t)(lbn + 1) * biosize > filesize) {
24744f917847SAlan Somers 		*blksz = filesize - (off_t)lbn *biosize;
2475b9e20197SAlan Somers 	} else {
24764f917847SAlan Somers 		*blksz = biosize;
2477b9e20197SAlan Somers 	}
2478197a4f29SKonstantin Belousov 	return (0);
2479b9e20197SAlan Somers }
2480b9e20197SAlan Somers 
24815fe58019SAttilio Rao /*
24825fe58019SAttilio Rao     struct vnop_getpages_args {
24835fe58019SAttilio Rao 	struct vnode *a_vp;
24845fe58019SAttilio Rao 	vm_page_t *a_m;
24855fe58019SAttilio Rao 	int a_count;
24865fe58019SAttilio Rao 	int a_reqpage;
24875fe58019SAttilio Rao     };
24885fe58019SAttilio Rao */
24895fe58019SAttilio Rao static int
24905fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap)
24915fe58019SAttilio Rao {
2492b9e20197SAlan Somers 	struct vnode *vp = ap->a_vp;
24935fe58019SAttilio Rao 
24945fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
2495419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
2496cf169498SAlan Somers 			"called on non-cacheable vnode??\n");
24975fe58019SAttilio Rao 		return (VM_PAGER_ERROR);
24985fe58019SAttilio Rao 	}
24995fe58019SAttilio Rao 
2500b9e20197SAlan Somers 	return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2501b9e20197SAlan Somers 	    ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
25025fe58019SAttilio Rao }
25035fe58019SAttilio Rao 
250404660064SFedor Uporov static const char extattr_namespace_separator = '.';
250504660064SFedor Uporov 
250604660064SFedor Uporov /*
250704660064SFedor Uporov     struct vop_getextattr_args {
250804660064SFedor Uporov 	struct vop_generic_args a_gen;
250904660064SFedor Uporov 	struct vnode *a_vp;
251004660064SFedor Uporov 	int a_attrnamespace;
251104660064SFedor Uporov 	const char *a_name;
251204660064SFedor Uporov 	struct uio *a_uio;
251304660064SFedor Uporov 	size_t *a_size;
251404660064SFedor Uporov 	struct ucred *a_cred;
251504660064SFedor Uporov 	struct thread *a_td;
251604660064SFedor Uporov     };
251704660064SFedor Uporov */
251804660064SFedor Uporov static int
251904660064SFedor Uporov fuse_vnop_getextattr(struct vop_getextattr_args *ap)
252004660064SFedor Uporov {
252104660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
252204660064SFedor Uporov 	struct uio *uio = ap->a_uio;
252328f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
252404660064SFedor Uporov 	struct fuse_getxattr_in *get_xattr_in;
252504660064SFedor Uporov 	struct fuse_getxattr_out *get_xattr_out;
252604660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
252704660064SFedor Uporov 	struct thread *td = ap->a_td;
252804660064SFedor Uporov 	struct ucred *cred = ap->a_cred;
252928f4f623SFedor Uporov 	char *prefix;
253028f4f623SFedor Uporov 	char *attr_str;
253128f4f623SFedor Uporov 	size_t len;
253228f4f623SFedor Uporov 	int err;
253304660064SFedor Uporov 
253404660064SFedor Uporov 	if (fuse_isdeadfs(vp))
253528f4f623SFedor Uporov 		return (ENXIO);
253604660064SFedor Uporov 
253737df9d3bSAlan Somers 	if (fsess_not_impl(mp, FUSE_GETXATTR))
25381f4a83f9SAlan Somers 		return EOPNOTSUPP;
25391f4a83f9SAlan Somers 
2540666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2541ff4fbdf5SAlan Somers 	if (err)
2542ff4fbdf5SAlan Somers 		return err;
2543ff4fbdf5SAlan Somers 
254404660064SFedor Uporov 	/* Default to looking for user attributes. */
254504660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
254604660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
254704660064SFedor Uporov 	else
254804660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
254904660064SFedor Uporov 
255004660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
255104660064SFedor Uporov 	    strlen(ap->a_name) + 1;
255204660064SFedor Uporov 
255304660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
255404660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
255504660064SFedor Uporov 
255604660064SFedor Uporov 	get_xattr_in = fdi.indata;
255704660064SFedor Uporov 	/*
255804660064SFedor Uporov 	 * Check to see whether we're querying the available size or
255904660064SFedor Uporov 	 * issuing the actual request.  If we pass in 0, we get back struct
256004660064SFedor Uporov 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
256104660064SFedor Uporov 	 * that much data, without the struct fuse_getxattr_out header.
256204660064SFedor Uporov 	 */
2563493b4a8cSFedor Uporov 	if (uio == NULL)
256404660064SFedor Uporov 		get_xattr_in->size = 0;
256504660064SFedor Uporov 	else
256604660064SFedor Uporov 		get_xattr_in->size = uio->uio_resid;
256704660064SFedor Uporov 
256804660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
256904660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
257004660064SFedor Uporov 	    ap->a_name);
257104660064SFedor Uporov 
257204660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
257304660064SFedor Uporov 	if (err != 0) {
25741f4a83f9SAlan Somers 		if (err == ENOSYS) {
257504660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_GETXATTR);
25761f4a83f9SAlan Somers 			err = EOPNOTSUPP;
25771f4a83f9SAlan Somers 		}
257804660064SFedor Uporov 		goto out;
257904660064SFedor Uporov 	}
258004660064SFedor Uporov 
258104660064SFedor Uporov 	get_xattr_out = fdi.answ;
258204660064SFedor Uporov 
2583493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
258404660064SFedor Uporov 		*ap->a_size = get_xattr_out->size;
2585493b4a8cSFedor Uporov 
2586493b4a8cSFedor Uporov 	if (uio != NULL)
258704660064SFedor Uporov 		err = uiomove(fdi.answ, fdi.iosize, uio);
258804660064SFedor Uporov 
258904660064SFedor Uporov out:
259004660064SFedor Uporov 	fdisp_destroy(&fdi);
259104660064SFedor Uporov 	return (err);
259204660064SFedor Uporov }
259304660064SFedor Uporov 
259404660064SFedor Uporov /*
259504660064SFedor Uporov     struct vop_setextattr_args {
259604660064SFedor Uporov 	struct vop_generic_args a_gen;
259704660064SFedor Uporov 	struct vnode *a_vp;
259804660064SFedor Uporov 	int a_attrnamespace;
259904660064SFedor Uporov 	const char *a_name;
260004660064SFedor Uporov 	struct uio *a_uio;
260104660064SFedor Uporov 	struct ucred *a_cred;
260204660064SFedor Uporov 	struct thread *a_td;
260304660064SFedor Uporov     };
260404660064SFedor Uporov */
260504660064SFedor Uporov static int
260604660064SFedor Uporov fuse_vnop_setextattr(struct vop_setextattr_args *ap)
260704660064SFedor Uporov {
260804660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
260904660064SFedor Uporov 	struct uio *uio = ap->a_uio;
261028f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
261104660064SFedor Uporov 	struct fuse_setxattr_in *set_xattr_in;
261204660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
261328f4f623SFedor Uporov 	struct thread *td = ap->a_td;
261428f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
261504660064SFedor Uporov 	char *prefix;
261604660064SFedor Uporov 	size_t len;
261704660064SFedor Uporov 	char *attr_str;
261828f4f623SFedor Uporov 	int err;
261904660064SFedor Uporov 
262004660064SFedor Uporov 	if (fuse_isdeadfs(vp))
262128f4f623SFedor Uporov 		return (ENXIO);
262204660064SFedor Uporov 
262337df9d3bSAlan Somers 	if (fsess_not_impl(mp, FUSE_SETXATTR))
26241f4a83f9SAlan Somers 		return EOPNOTSUPP;
26251f4a83f9SAlan Somers 
2626666f8543SAlan Somers 	if (vfs_isrdonly(mp))
2627666f8543SAlan Somers 		return EROFS;
2628666f8543SAlan Somers 
2629ff4fbdf5SAlan Somers 	/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
26301f4a83f9SAlan Somers 	if (ap->a_uio == NULL) {
26311f4a83f9SAlan Somers 		/*
26321f4a83f9SAlan Somers 		 * If we got here as fallback from VOP_DELETEEXTATTR, then
26331f4a83f9SAlan Somers 		 * return EOPNOTSUPP.
26341f4a83f9SAlan Somers 		 */
263537df9d3bSAlan Somers 		if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
26361f4a83f9SAlan Somers 			return (EOPNOTSUPP);
26371f4a83f9SAlan Somers 		else
2638ff4fbdf5SAlan Somers 			return (EINVAL);
26391f4a83f9SAlan Somers 	}
2640ff4fbdf5SAlan Somers 
2641666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2642666f8543SAlan Somers 		VWRITE);
2643ff4fbdf5SAlan Somers 	if (err)
2644ff4fbdf5SAlan Somers 		return err;
2645ff4fbdf5SAlan Somers 
264604660064SFedor Uporov 	/* Default to looking for user attributes. */
264704660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
264804660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
264904660064SFedor Uporov 	else
265004660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
265104660064SFedor Uporov 
265204660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
265304660064SFedor Uporov 	    strlen(ap->a_name) + 1;
265404660064SFedor Uporov 
265504660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
265604660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
265704660064SFedor Uporov 
265804660064SFedor Uporov 	set_xattr_in = fdi.indata;
265904660064SFedor Uporov 	set_xattr_in->size = uio->uio_resid;
266004660064SFedor Uporov 
266104660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
266204660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
266304660064SFedor Uporov 	    ap->a_name);
266404660064SFedor Uporov 
266504660064SFedor Uporov 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
266604660064SFedor Uporov 	    uio->uio_resid, uio);
266704660064SFedor Uporov 	if (err != 0) {
266804660064SFedor Uporov 		goto out;
266904660064SFedor Uporov 	}
267004660064SFedor Uporov 
267104660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
267204660064SFedor Uporov 
26731f4a83f9SAlan Somers 	if (err == ENOSYS) {
267404660064SFedor Uporov 		fsess_set_notimpl(mp, FUSE_SETXATTR);
26751f4a83f9SAlan Somers 		err = EOPNOTSUPP;
267604660064SFedor Uporov 	}
2677f0f7fc1bSAlan Somers 	if (err == ERESTART) {
2678f0f7fc1bSAlan Somers 		/* Can't restart after calling uiomove */
2679f0f7fc1bSAlan Somers 		err = EINTR;
2680f0f7fc1bSAlan Somers 	}
268104660064SFedor Uporov 
268204660064SFedor Uporov out:
268304660064SFedor Uporov 	fdisp_destroy(&fdi);
268404660064SFedor Uporov 	return (err);
268504660064SFedor Uporov }
268604660064SFedor Uporov 
268704660064SFedor Uporov /*
268804660064SFedor Uporov  * The Linux / FUSE extended attribute list is simply a collection of
268904660064SFedor Uporov  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
269004660064SFedor Uporov  * byte length followed by a non-NUL terminated string.  So, this allows
269104660064SFedor Uporov  * conversion of the Linux / FUSE format to the FreeBSD format in place.
269204660064SFedor Uporov  * Linux attribute names are reported with the namespace as a prefix (e.g.
269304660064SFedor Uporov  * "user.attribute_name"), but in FreeBSD they are reported without the
269404660064SFedor Uporov  * namespace prefix (e.g. "attribute_name").  So, we're going from:
269504660064SFedor Uporov  *
269604660064SFedor Uporov  * user.attr_name1\0user.attr_name2\0
269704660064SFedor Uporov  *
269804660064SFedor Uporov  * to:
269904660064SFedor Uporov  *
270004660064SFedor Uporov  * <num>attr_name1<num>attr_name2
270104660064SFedor Uporov  *
270204660064SFedor Uporov  * Where "<num>" is a single byte number of characters in the attribute name.
270304660064SFedor Uporov  *
270404660064SFedor Uporov  * Args:
270504660064SFedor Uporov  * prefix - exattr namespace prefix string
270604660064SFedor Uporov  * list, list_len - input list with namespace prefixes
270704660064SFedor Uporov  * bsd_list, bsd_list_len - output list compatible with bsd vfs
270804660064SFedor Uporov  */
270904660064SFedor Uporov static int
271004660064SFedor Uporov fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
271104660064SFedor Uporov     char *bsd_list, int *bsd_list_len)
271204660064SFedor Uporov {
271304660064SFedor Uporov 	int len, pos, dist_to_next, prefix_len;
271404660064SFedor Uporov 
271504660064SFedor Uporov 	pos = 0;
271604660064SFedor Uporov 	*bsd_list_len = 0;
271704660064SFedor Uporov 	prefix_len = strlen(prefix);
271804660064SFedor Uporov 
271904660064SFedor Uporov 	while (pos < list_len && list[pos] != '\0') {
272004660064SFedor Uporov 		dist_to_next = strlen(&list[pos]) + 1;
272104660064SFedor Uporov 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
272204660064SFedor Uporov 		    list[pos + prefix_len] == extattr_namespace_separator) {
272304660064SFedor Uporov 			len = dist_to_next -
272404660064SFedor Uporov 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
272504660064SFedor Uporov 			if (len >= EXTATTR_MAXNAMELEN)
272604660064SFedor Uporov 				return (ENAMETOOLONG);
272704660064SFedor Uporov 
272804660064SFedor Uporov 			bsd_list[*bsd_list_len] = len;
272904660064SFedor Uporov 			memcpy(&bsd_list[*bsd_list_len + 1],
273004660064SFedor Uporov 			    &list[pos + prefix_len +
273104660064SFedor Uporov 			    sizeof(extattr_namespace_separator)], len);
273204660064SFedor Uporov 
273304660064SFedor Uporov 			*bsd_list_len += len + 1;
273404660064SFedor Uporov 		}
273504660064SFedor Uporov 
273604660064SFedor Uporov 		pos += dist_to_next;
273704660064SFedor Uporov 	}
273804660064SFedor Uporov 
273904660064SFedor Uporov 	return (0);
274004660064SFedor Uporov }
274104660064SFedor Uporov 
274204660064SFedor Uporov /*
27435e633330SAlan Somers  * List extended attributes
27445e633330SAlan Somers  *
27455e633330SAlan Somers  * The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
27465e633330SAlan Somers  * has a number of differences compared to its FreeBSD equivalent,
27475e633330SAlan Somers  * extattr_list_file:
27485e633330SAlan Somers  *
27495e633330SAlan Somers  * - FUSE_LISTXATTR returns all extended attributes across all namespaces,
27505e633330SAlan Somers  *   whereas listxattr(2) only returns attributes for a single namespace
27515e633330SAlan Somers  * - FUSE_LISTXATTR prepends each attribute name with "namespace."
27525e633330SAlan Somers  * - If the provided buffer is not large enough to hold the result,
27535e633330SAlan Somers  *   FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
27545e633330SAlan Somers  *   return as many results as will fit.
27555e633330SAlan Somers  */
27565e633330SAlan Somers /*
275704660064SFedor Uporov     struct vop_listextattr_args {
275804660064SFedor Uporov 	struct vop_generic_args a_gen;
275904660064SFedor Uporov 	struct vnode *a_vp;
276004660064SFedor Uporov 	int a_attrnamespace;
276104660064SFedor Uporov 	struct uio *a_uio;
276204660064SFedor Uporov 	size_t *a_size;
276304660064SFedor Uporov 	struct ucred *a_cred;
276404660064SFedor Uporov 	struct thread *a_td;
276504660064SFedor Uporov     };
276604660064SFedor Uporov */
276704660064SFedor Uporov static int
276804660064SFedor Uporov fuse_vnop_listextattr(struct vop_listextattr_args *ap)
276904660064SFedor Uporov {
277004660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
277104660064SFedor Uporov 	struct uio *uio = ap->a_uio;
277228f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
2773493b4a8cSFedor Uporov 	struct fuse_listxattr_in *list_xattr_in;
2774493b4a8cSFedor Uporov 	struct fuse_listxattr_out *list_xattr_out;
277504660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
277628f4f623SFedor Uporov 	struct thread *td = ap->a_td;
277728f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
277804660064SFedor Uporov 	char *prefix;
277904660064SFedor Uporov 	char *bsd_list = NULL;
2780493b4a8cSFedor Uporov 	char *linux_list;
278104660064SFedor Uporov 	int bsd_list_len;
2782493b4a8cSFedor Uporov 	int linux_list_len;
278328f4f623SFedor Uporov 	int err;
278404660064SFedor Uporov 
278504660064SFedor Uporov 	if (fuse_isdeadfs(vp))
278628f4f623SFedor Uporov 		return (ENXIO);
278704660064SFedor Uporov 
278837df9d3bSAlan Somers 	if (fsess_not_impl(mp, FUSE_LISTXATTR))
27891f4a83f9SAlan Somers 		return EOPNOTSUPP;
27901f4a83f9SAlan Somers 
2791666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2792ff4fbdf5SAlan Somers 	if (err)
2793ff4fbdf5SAlan Somers 		return err;
2794ff4fbdf5SAlan Somers 
279504660064SFedor Uporov 	/*
279604660064SFedor Uporov 	 * Add space for a NUL and the period separator if enabled.
279704660064SFedor Uporov 	 * Default to looking for user attributes.
279804660064SFedor Uporov 	 */
279904660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
280004660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
280104660064SFedor Uporov 	else
280204660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
280304660064SFedor Uporov 
28043a79e8e7SAlan Somers 	fdisp_init(&fdi, sizeof(*list_xattr_in));
280504660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
280604660064SFedor Uporov 
2807493b4a8cSFedor Uporov 	/*
2808493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list size.
2809493b4a8cSFedor Uporov 	 */
2810493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2811493b4a8cSFedor Uporov 	list_xattr_in->size = 0;
281204660064SFedor Uporov 
281304660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
281404660064SFedor Uporov 	if (err != 0) {
28151f4a83f9SAlan Somers 		if (err == ENOSYS) {
281604660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
28171f4a83f9SAlan Somers 			err = EOPNOTSUPP;
28181f4a83f9SAlan Somers 		}
281904660064SFedor Uporov 		goto out;
282004660064SFedor Uporov 	}
282104660064SFedor Uporov 
2822493b4a8cSFedor Uporov 	list_xattr_out = fdi.answ;
2823493b4a8cSFedor Uporov 	linux_list_len = list_xattr_out->size;
2824493b4a8cSFedor Uporov 	if (linux_list_len == 0) {
2825493b4a8cSFedor Uporov 		if (ap->a_size != NULL)
2826493b4a8cSFedor Uporov 			*ap->a_size = linux_list_len;
282704660064SFedor Uporov 		goto out;
282804660064SFedor Uporov 	}
282904660064SFedor Uporov 
283004660064SFedor Uporov 	/*
2831493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list values.
2832493b4a8cSFedor Uporov 	 */
283312292a99SAlan Somers 	fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2834493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
28355e633330SAlan Somers 	list_xattr_in->size = linux_list_len;
2836493b4a8cSFedor Uporov 
2837493b4a8cSFedor Uporov 	err = fdisp_wait_answ(&fdi);
28385e633330SAlan Somers 	if (err == ERANGE) {
28395e633330SAlan Somers 		/*
28405e633330SAlan Somers 		 * Race detected.  The attribute list must've grown since the
28415e633330SAlan Somers 		 * first FUSE_LISTXATTR call.  Start over.  Go all the way back
28425e633330SAlan Somers 		 * to userland so we can process signals, if necessary, before
28435e633330SAlan Somers 		 * restarting.
28445e633330SAlan Somers 		 */
28455e633330SAlan Somers 		err = ERESTART;
28465e633330SAlan Somers 		goto out;
28475e633330SAlan Somers 	} else if (err != 0)
2848493b4a8cSFedor Uporov 		goto out;
2849493b4a8cSFedor Uporov 
2850493b4a8cSFedor Uporov 	linux_list = fdi.answ;
28515e633330SAlan Somers 	/* FUSE doesn't allow the server to return more data than requested */
28525e633330SAlan Somers 	if (fdi.iosize > linux_list_len) {
28530b9a5c6fSAlan Somers 		struct fuse_data *data = fuse_get_mpdata(mp);
28540b9a5c6fSAlan Somers 
28550b9a5c6fSAlan Somers 		fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
28560b9a5c6fSAlan Somers 			"server returned "
28575e633330SAlan Somers 			"more extended attribute data than requested; "
28580b9a5c6fSAlan Somers 			"should've returned ERANGE instead.");
28595e633330SAlan Somers 	} else {
28605e633330SAlan Somers 		/* But returning less data is fine */
2861493b4a8cSFedor Uporov 		linux_list_len = fdi.iosize;
28625e633330SAlan Somers 	}
2863493b4a8cSFedor Uporov 
2864493b4a8cSFedor Uporov 	/*
2865493b4a8cSFedor Uporov 	 * Retrieve the BSD compatible list values.
286604660064SFedor Uporov 	 * The Linux / FUSE attribute list format isn't the same
286704660064SFedor Uporov 	 * as FreeBSD's format. So we need to transform it into
286804660064SFedor Uporov 	 * FreeBSD's format before giving it to the user.
286904660064SFedor Uporov 	 */
2870493b4a8cSFedor Uporov 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2871493b4a8cSFedor Uporov 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
287204660064SFedor Uporov 	    bsd_list, &bsd_list_len);
287304660064SFedor Uporov 	if (err != 0)
287404660064SFedor Uporov 		goto out;
287504660064SFedor Uporov 
2876493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
2877493b4a8cSFedor Uporov 		*ap->a_size = bsd_list_len;
2878493b4a8cSFedor Uporov 
2879493b4a8cSFedor Uporov 	if (uio != NULL)
288004660064SFedor Uporov 		err = uiomove(bsd_list, bsd_list_len, uio);
288104660064SFedor Uporov 
288204660064SFedor Uporov out:
288304660064SFedor Uporov 	free(bsd_list, M_TEMP);
288404660064SFedor Uporov 	fdisp_destroy(&fdi);
288504660064SFedor Uporov 	return (err);
288604660064SFedor Uporov }
288704660064SFedor Uporov 
288804660064SFedor Uporov /*
288989d57b94SAlan Somers     struct vop_deallocate_args {
289089d57b94SAlan Somers 	struct vop_generic_args a_gen;
289189d57b94SAlan Somers 	struct vnode *a_vp;
289289d57b94SAlan Somers 	off_t *a_offset;
289389d57b94SAlan Somers 	off_t *a_len;
289489d57b94SAlan Somers 	int a_flags;
289589d57b94SAlan Somers 	int a_ioflag;
289689d57b94SAlan Somers         struct ucred *a_cred;
289789d57b94SAlan Somers     };
289889d57b94SAlan Somers */
289989d57b94SAlan Somers static int
290089d57b94SAlan Somers fuse_vnop_deallocate(struct vop_deallocate_args *ap)
290189d57b94SAlan Somers {
290289d57b94SAlan Somers 	struct vnode *vp = ap->a_vp;
290389d57b94SAlan Somers 	struct mount *mp = vnode_mount(vp);
290489d57b94SAlan Somers 	struct fuse_filehandle *fufh;
290589d57b94SAlan Somers 	struct fuse_dispatcher fdi;
290689d57b94SAlan Somers 	struct fuse_fallocate_in *ffi;
290789d57b94SAlan Somers 	struct ucred *cred = ap->a_cred;
290889d57b94SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
290989d57b94SAlan Somers 	off_t *len = ap->a_len;
291089d57b94SAlan Somers 	off_t *offset = ap->a_offset;
291189d57b94SAlan Somers 	int ioflag = ap->a_ioflag;
291289d57b94SAlan Somers 	off_t filesize;
291389d57b94SAlan Somers 	int err;
291489d57b94SAlan Somers 	bool closefufh = false;
291589d57b94SAlan Somers 
291689d57b94SAlan Somers 	if (fuse_isdeadfs(vp))
291789d57b94SAlan Somers 		return (ENXIO);
291889d57b94SAlan Somers 
291989d57b94SAlan Somers 	if (vfs_isrdonly(mp))
292089d57b94SAlan Somers 		return (EROFS);
292189d57b94SAlan Somers 
292289d57b94SAlan Somers 	if (fsess_not_impl(mp, FUSE_FALLOCATE))
292389d57b94SAlan Somers 		goto fallback;
292489d57b94SAlan Somers 
292589d57b94SAlan Somers 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
292689d57b94SAlan Somers 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
292789d57b94SAlan Somers 		/*
292889d57b94SAlan Somers 		 * nfsd will do I/O without first doing VOP_OPEN.  We
292989d57b94SAlan Somers 		 * must implicitly open the file here
293089d57b94SAlan Somers 		 */
293189d57b94SAlan Somers 		err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
293289d57b94SAlan Somers 		closefufh = true;
293389d57b94SAlan Somers 	}
293489d57b94SAlan Somers 	if (err)
293589d57b94SAlan Somers 		return (err);
293689d57b94SAlan Somers 
293789d57b94SAlan Somers 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
293889d57b94SAlan Somers 
293989d57b94SAlan Somers 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
294089d57b94SAlan Somers 	if (err)
294189d57b94SAlan Somers 		goto out;
294289d57b94SAlan Somers 	fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
294389d57b94SAlan Somers 
294489d57b94SAlan Somers 	fdisp_init(&fdi, sizeof(*ffi));
294589d57b94SAlan Somers 	fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
294689d57b94SAlan Somers 	ffi = fdi.indata;
294789d57b94SAlan Somers 	ffi->fh = fufh->fh_id;
294889d57b94SAlan Somers 	ffi->offset = *offset;
294989d57b94SAlan Somers 	ffi->length = *len;
295089d57b94SAlan Somers 	/*
295189d57b94SAlan Somers 	 * FreeBSD's fspacectl is equivalent to Linux's fallocate with
295289d57b94SAlan Somers 	 * mode == FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE
295389d57b94SAlan Somers 	 */
295489d57b94SAlan Somers 	ffi->mode = FUSE_FALLOC_FL_PUNCH_HOLE | FUSE_FALLOC_FL_KEEP_SIZE;
295589d57b94SAlan Somers 	err = fdisp_wait_answ(&fdi);
295689d57b94SAlan Somers 
295789d57b94SAlan Somers 	if (err == ENOSYS) {
295889d57b94SAlan Somers 		fsess_set_notimpl(mp, FUSE_FALLOCATE);
295989d57b94SAlan Somers 		goto fallback;
296089d57b94SAlan Somers 	} else if (err == EOPNOTSUPP) {
296189d57b94SAlan Somers 		/*
296289d57b94SAlan Somers 		 * The file system server does not support FUSE_FALLOCATE with
296389d57b94SAlan Somers 		 * the supplied mode for this particular file.
296489d57b94SAlan Somers 		 */
296589d57b94SAlan Somers 		goto fallback;
296689d57b94SAlan Somers 	} else if (!err) {
296789d57b94SAlan Somers 		/*
296889d57b94SAlan Somers 		 * Clip the returned offset to EoF.  Do it here rather than
296989d57b94SAlan Somers 		 * before FUSE_FALLOCATE just in case the kernel's cached file
297089d57b94SAlan Somers 		 * size is out of date.  Unfortunately, FUSE does not return
297189d57b94SAlan Somers 		 * any information about filesize from that operation.
297289d57b94SAlan Somers 		 */
297389d57b94SAlan Somers 		*offset = MIN(*offset + *len, filesize);
297489d57b94SAlan Somers 		*len = 0;
297589d57b94SAlan Somers 		fuse_vnode_undirty_cached_timestamps(vp, false);
297689d57b94SAlan Somers 		fuse_internal_clear_suid_on_write(vp, cred, curthread);
297789d57b94SAlan Somers 
297889d57b94SAlan Somers 		if (ioflag & IO_SYNC)
297989d57b94SAlan Somers 			err = fuse_internal_fsync(vp, curthread, MNT_WAIT,
298089d57b94SAlan Somers 			    false);
298189d57b94SAlan Somers 	}
298289d57b94SAlan Somers 
298389d57b94SAlan Somers out:
298489d57b94SAlan Somers 	if (closefufh)
298589d57b94SAlan Somers 		fuse_filehandle_close(vp, fufh, curthread, cred);
298689d57b94SAlan Somers 
298789d57b94SAlan Somers 	return (err);
298889d57b94SAlan Somers 
298989d57b94SAlan Somers fallback:
299089d57b94SAlan Somers 	if (closefufh)
299189d57b94SAlan Somers 		fuse_filehandle_close(vp, fufh, curthread, cred);
299289d57b94SAlan Somers 
299389d57b94SAlan Somers 	return (vop_stddeallocate(ap));
299489d57b94SAlan Somers }
299589d57b94SAlan Somers 
299689d57b94SAlan Somers /*
299704660064SFedor Uporov     struct vop_deleteextattr_args {
299804660064SFedor Uporov 	struct vop_generic_args a_gen;
299904660064SFedor Uporov 	struct vnode *a_vp;
300004660064SFedor Uporov 	int a_attrnamespace;
300104660064SFedor Uporov 	const char *a_name;
300204660064SFedor Uporov 	struct ucred *a_cred;
300304660064SFedor Uporov 	struct thread *a_td;
300404660064SFedor Uporov     };
300504660064SFedor Uporov */
300604660064SFedor Uporov static int
300704660064SFedor Uporov fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
300804660064SFedor Uporov {
300904660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
301028f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
301104660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
301228f4f623SFedor Uporov 	struct thread *td = ap->a_td;
301328f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
301404660064SFedor Uporov 	char *prefix;
301504660064SFedor Uporov 	size_t len;
301604660064SFedor Uporov 	char *attr_str;
301704660064SFedor Uporov 	int err;
301804660064SFedor Uporov 
301904660064SFedor Uporov 	if (fuse_isdeadfs(vp))
302028f4f623SFedor Uporov 		return (ENXIO);
302104660064SFedor Uporov 
302237df9d3bSAlan Somers 	if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
30231f4a83f9SAlan Somers 		return EOPNOTSUPP;
30241f4a83f9SAlan Somers 
3025666f8543SAlan Somers 	if (vfs_isrdonly(mp))
3026666f8543SAlan Somers 		return EROFS;
3027666f8543SAlan Somers 
3028666f8543SAlan Somers 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
3029666f8543SAlan Somers 		VWRITE);
3030ff4fbdf5SAlan Somers 	if (err)
3031ff4fbdf5SAlan Somers 		return err;
3032ff4fbdf5SAlan Somers 
303304660064SFedor Uporov 	/* Default to looking for user attributes. */
303404660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
303504660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
303604660064SFedor Uporov 	else
303704660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
303804660064SFedor Uporov 
303904660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
304004660064SFedor Uporov 	    strlen(ap->a_name) + 1;
304104660064SFedor Uporov 
304204660064SFedor Uporov 	fdisp_init(&fdi, len);
304304660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
304404660064SFedor Uporov 
304504660064SFedor Uporov 	attr_str = fdi.indata;
304604660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
304704660064SFedor Uporov 	    ap->a_name);
304804660064SFedor Uporov 
304904660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
30501f4a83f9SAlan Somers 	if (err == ENOSYS) {
305104660064SFedor Uporov 		fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
30521f4a83f9SAlan Somers 		err = EOPNOTSUPP;
305304660064SFedor Uporov 	}
305404660064SFedor Uporov 
305504660064SFedor Uporov 	fdisp_destroy(&fdi);
305604660064SFedor Uporov 	return (err);
305704660064SFedor Uporov }
305804660064SFedor Uporov 
30595fe58019SAttilio Rao /*
30605fe58019SAttilio Rao     struct vnop_print_args {
30615fe58019SAttilio Rao 	struct vnode *a_vp;
30625fe58019SAttilio Rao     };
30635fe58019SAttilio Rao */
30645fe58019SAttilio Rao static int
30655fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap)
30665fe58019SAttilio Rao {
30675fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
30685fe58019SAttilio Rao 
30695fe58019SAttilio Rao 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
30705fe58019SAttilio Rao 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
30715fe58019SAttilio Rao 	    (uintmax_t)fvdat->nlookup,
30725fe58019SAttilio Rao 	    fvdat->flag);
30735fe58019SAttilio Rao 
30745fe58019SAttilio Rao 	return 0;
30755fe58019SAttilio Rao }
3076e5b50fe7SAlan Somers 
3077e5b50fe7SAlan Somers /*
3078e5b50fe7SAlan Somers  * Get an NFS filehandle for a FUSE file.
3079e5b50fe7SAlan Somers  *
3080e5b50fe7SAlan Somers  * This will only work for FUSE file systems that guarantee the uniqueness of
30810d2bf489SAlan Somers  * nodeid:generation, which most don't.
3082e5b50fe7SAlan Somers  */
3083e5b50fe7SAlan Somers /*
3084e5b50fe7SAlan Somers vop_vptofh {
3085e5b50fe7SAlan Somers 	IN struct vnode *a_vp;
3086e5b50fe7SAlan Somers 	IN struct fid *a_fhp;
3087e5b50fe7SAlan Somers };
3088e5b50fe7SAlan Somers */
3089e5b50fe7SAlan Somers static int
3090e5b50fe7SAlan Somers fuse_vnop_vptofh(struct vop_vptofh_args *ap)
3091e5b50fe7SAlan Somers {
3092e5b50fe7SAlan Somers 	struct vnode *vp = ap->a_vp;
3093e5b50fe7SAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
3094e5b50fe7SAlan Somers 	struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
3095e5b50fe7SAlan Somers 	_Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
3096e5b50fe7SAlan Somers 		"FUSE fid type is too big");
3097e5b50fe7SAlan Somers 	struct mount *mp = vnode_mount(vp);
3098e5b50fe7SAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
3099e5b50fe7SAlan Somers 	struct vattr va;
3100e5b50fe7SAlan Somers 	int err;
3101e5b50fe7SAlan Somers 
310200134a07SAlan Somers 	if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
310300134a07SAlan Somers 		/* NFS requires lookups for "." and ".." */
310400134a07SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
310500134a07SAlan Somers 			"VOP_VPTOFH without FUSE_EXPORT_SUPPORT");
3106e5b50fe7SAlan Somers 		return EOPNOTSUPP;
310700134a07SAlan Somers 	}
310800134a07SAlan Somers 	if ((mp->mnt_flag & MNT_EXPORTED) &&
310900134a07SAlan Somers 		!(data->dataflags & FSESS_NO_OPENDIR_SUPPORT))
311000134a07SAlan Somers 	{
311100134a07SAlan Somers 		/*
311200134a07SAlan Somers 		 * NFS is stateless, so nfsd must reopen a directory on every
311300134a07SAlan Somers 		 * call to VOP_READDIR, passing in the d_off field from the
311400134a07SAlan Somers 		 * final dirent of the previous invocation.  But without
311500134a07SAlan Somers 		 * FUSE_NO_OPENDIR_SUPPORT, the FUSE protocol does not
311600134a07SAlan Somers 		 * guarantee that d_off will be valid after a directory is
311700134a07SAlan Somers 		 * closed and reopened.  So prohibit exporting FUSE file
311800134a07SAlan Somers 		 * systems that don't set that flag.
311900134a07SAlan Somers 		 *
312000134a07SAlan Somers 		 * But userspace NFS servers don't have this problem.
312100134a07SAlan Somers                  */
312200134a07SAlan Somers 		SDT_PROBE2(fusefs, , vnops, trace, 1,
312300134a07SAlan Somers 			"VOP_VPTOFH without FUSE_NO_OPENDIR_SUPPORT");
312400134a07SAlan Somers 		return EOPNOTSUPP;
312500134a07SAlan Somers 	}
3126e5b50fe7SAlan Somers 
3127e5b50fe7SAlan Somers 	err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
3128e5b50fe7SAlan Somers 	if (err)
3129e5b50fe7SAlan Somers 		return err;
3130e5b50fe7SAlan Somers 
3131e5b50fe7SAlan Somers 	/*ip = VTOI(ap->a_vp);*/
3132e5b50fe7SAlan Somers 	/*ufhp = (struct ufid *)ap->a_fhp;*/
3133e5b50fe7SAlan Somers 	fhp->len = sizeof(struct fuse_fid);
3134e5b50fe7SAlan Somers 	fhp->nid = fvdat->nid;
3135e5b50fe7SAlan Somers 	if (fvdat->generation <= UINT32_MAX)
3136e5b50fe7SAlan Somers 		fhp->gen = fvdat->generation;
3137e5b50fe7SAlan Somers 	else
3138e5b50fe7SAlan Somers 		return EOVERFLOW;
3139e5b50fe7SAlan Somers 	return (0);
3140e5b50fe7SAlan Somers }
3141