xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following disclaimer
15  *   in the documentation and/or other materials provided with the
16  *   distribution.
17  * * Neither the name of Google Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Copyright (c) 2019 The FreeBSD Foundation
37  *
38  * Portions of this software were developed by BFF Storage Systems, LLC under
39  * sponsorship from the FreeBSD Foundation.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/param.h>
64 #include <sys/module.h>
65 #include <sys/systm.h>
66 #include <sys/errno.h>
67 #include <sys/kernel.h>
68 #include <sys/conf.h>
69 #include <sys/filio.h>
70 #include <sys/uio.h>
71 #include <sys/malloc.h>
72 #include <sys/queue.h>
73 #include <sys/limits.h>
74 #include <sys/lock.h>
75 #include <sys/rwlock.h>
76 #include <sys/sx.h>
77 #include <sys/proc.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
80 #include <sys/namei.h>
81 #include <sys/extattr.h>
82 #include <sys/stat.h>
83 #include <sys/unistd.h>
84 #include <sys/filedesc.h>
85 #include <sys/file.h>
86 #include <sys/fcntl.h>
87 #include <sys/dirent.h>
88 #include <sys/bio.h>
89 #include <sys/buf.h>
90 #include <sys/sysctl.h>
91 #include <sys/vmmeter.h>
92 
93 #include <vm/vm.h>
94 #include <vm/vm_extern.h>
95 #include <vm/pmap.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_param.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vnode_pager.h>
102 #include <vm/vm_object.h>
103 
104 #include "fuse.h"
105 #include "fuse_file.h"
106 #include "fuse_internal.h"
107 #include "fuse_ipc.h"
108 #include "fuse_node.h"
109 #include "fuse_io.h"
110 
111 #include <sys/priv.h>
112 
113 /* Maximum number of hardlinks to a single FUSE file */
114 #define FUSE_LINK_MAX                      UINT32_MAX
115 
116 SDT_PROVIDER_DECLARE(fusefs);
117 /*
118  * Fuse trace probe:
119  * arg0: verbosity.  Higher numbers give more verbose messages
120  * arg1: Textual message
121  */
122 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
123 
124 /* vnode ops */
125 static vop_access_t fuse_vnop_access;
126 static vop_advlock_t fuse_vnop_advlock;
127 static vop_allocate_t fuse_vnop_allocate;
128 static vop_bmap_t fuse_vnop_bmap;
129 static vop_close_t fuse_fifo_close;
130 static vop_close_t fuse_vnop_close;
131 static vop_copy_file_range_t fuse_vnop_copy_file_range;
132 static vop_create_t fuse_vnop_create;
133 static vop_deallocate_t fuse_vnop_deallocate;
134 static vop_deleteextattr_t fuse_vnop_deleteextattr;
135 static vop_fdatasync_t fuse_vnop_fdatasync;
136 static vop_fsync_t fuse_vnop_fsync;
137 static vop_getattr_t fuse_vnop_getattr;
138 static vop_getextattr_t fuse_vnop_getextattr;
139 static vop_inactive_t fuse_vnop_inactive;
140 static vop_ioctl_t fuse_vnop_ioctl;
141 static vop_link_t fuse_vnop_link;
142 static vop_listextattr_t fuse_vnop_listextattr;
143 static vop_lookup_t fuse_vnop_lookup;
144 static vop_mkdir_t fuse_vnop_mkdir;
145 static vop_mknod_t fuse_vnop_mknod;
146 static vop_open_t fuse_vnop_open;
147 static vop_pathconf_t fuse_vnop_pathconf;
148 static vop_read_t fuse_vnop_read;
149 static vop_readdir_t fuse_vnop_readdir;
150 static vop_readlink_t fuse_vnop_readlink;
151 static vop_reclaim_t fuse_vnop_reclaim;
152 static vop_remove_t fuse_vnop_remove;
153 static vop_rename_t fuse_vnop_rename;
154 static vop_rmdir_t fuse_vnop_rmdir;
155 static vop_setattr_t fuse_vnop_setattr;
156 static vop_setextattr_t fuse_vnop_setextattr;
157 static vop_strategy_t fuse_vnop_strategy;
158 static vop_symlink_t fuse_vnop_symlink;
159 static vop_write_t fuse_vnop_write;
160 static vop_getpages_t fuse_vnop_getpages;
161 static vop_print_t fuse_vnop_print;
162 static vop_vptofh_t fuse_vnop_vptofh;
163 
164 struct vop_vector fuse_fifoops = {
165 	.vop_default =		&fifo_specops,
166 	.vop_access =		fuse_vnop_access,
167 	.vop_close =		fuse_fifo_close,
168 	.vop_fsync =		fuse_vnop_fsync,
169 	.vop_getattr =		fuse_vnop_getattr,
170 	.vop_inactive =		fuse_vnop_inactive,
171 	.vop_pathconf =		fuse_vnop_pathconf,
172 	.vop_print =		fuse_vnop_print,
173 	.vop_read =		VOP_PANIC,
174 	.vop_reclaim =		fuse_vnop_reclaim,
175 	.vop_setattr =		fuse_vnop_setattr,
176 	.vop_write =		VOP_PANIC,
177 	.vop_vptofh =		fuse_vnop_vptofh,
178 };
179 VFS_VOP_VECTOR_REGISTER(fuse_fifoops);
180 
181 struct vop_vector fuse_vnops = {
182 	.vop_allocate =	fuse_vnop_allocate,
183 	.vop_default = &default_vnodeops,
184 	.vop_access = fuse_vnop_access,
185 	.vop_advlock = fuse_vnop_advlock,
186 	.vop_bmap = fuse_vnop_bmap,
187 	.vop_close = fuse_vnop_close,
188 	.vop_copy_file_range = fuse_vnop_copy_file_range,
189 	.vop_create = fuse_vnop_create,
190 	.vop_deallocate = fuse_vnop_deallocate,
191 	.vop_deleteextattr = fuse_vnop_deleteextattr,
192 	.vop_fsync = fuse_vnop_fsync,
193 	.vop_fdatasync = fuse_vnop_fdatasync,
194 	.vop_getattr = fuse_vnop_getattr,
195 	.vop_getextattr = fuse_vnop_getextattr,
196 	.vop_inactive = fuse_vnop_inactive,
197 	.vop_ioctl = fuse_vnop_ioctl,
198 	.vop_link = fuse_vnop_link,
199 	.vop_listextattr = fuse_vnop_listextattr,
200 	.vop_lookup = fuse_vnop_lookup,
201 	.vop_mkdir = fuse_vnop_mkdir,
202 	.vop_mknod = fuse_vnop_mknod,
203 	.vop_open = fuse_vnop_open,
204 	.vop_pathconf = fuse_vnop_pathconf,
205 	/*
206 	 * TODO: implement vop_poll after upgrading to protocol 7.21.
207 	 * FUSE_POLL was added in protocol 7.11, but it's kind of broken until
208 	 * 7.21, which adds the ability for the client to choose which poll
209 	 * events it wants, and for a client to deregister a file handle
210 	 */
211 	.vop_read = fuse_vnop_read,
212 	.vop_readdir = fuse_vnop_readdir,
213 	.vop_readlink = fuse_vnop_readlink,
214 	.vop_reclaim = fuse_vnop_reclaim,
215 	.vop_remove = fuse_vnop_remove,
216 	.vop_rename = fuse_vnop_rename,
217 	.vop_rmdir = fuse_vnop_rmdir,
218 	.vop_setattr = fuse_vnop_setattr,
219 	.vop_setextattr = fuse_vnop_setextattr,
220 	.vop_strategy = fuse_vnop_strategy,
221 	.vop_symlink = fuse_vnop_symlink,
222 	.vop_write = fuse_vnop_write,
223 	.vop_getpages = fuse_vnop_getpages,
224 	.vop_print = fuse_vnop_print,
225 	.vop_vptofh = fuse_vnop_vptofh,
226 };
227 VFS_VOP_VECTOR_REGISTER(fuse_vnops);
228 
229 /* Check permission for extattr operations, much like extattr_check_cred */
230 static int
231 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
232 	struct thread *td, accmode_t accmode)
233 {
234 	struct mount *mp = vnode_mount(vp);
235 	struct fuse_data *data = fuse_get_mpdata(mp);
236 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
237 
238 	/*
239 	 * Kernel-invoked always succeeds.
240 	 */
241 	if (cred == NOCRED)
242 		return (0);
243 
244 	/*
245 	 * Do not allow privileged processes in jail to directly manipulate
246 	 * system attributes.
247 	 */
248 	switch (ns) {
249 	case EXTATTR_NAMESPACE_SYSTEM:
250 		if (default_permissions) {
251 			return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
252 		}
253 		return (0);
254 	case EXTATTR_NAMESPACE_USER:
255 		if (default_permissions) {
256 			return (fuse_internal_access(vp, accmode, td, cred));
257 		}
258 		return (0);
259 	default:
260 		return (EPERM);
261 	}
262 }
263 
264 /* Get a filehandle for a directory */
265 static int
266 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
267 	struct ucred *cred, pid_t pid)
268 {
269 	if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
270 		return 0;
271 	return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
272 }
273 
274 /* Send FUSE_FLUSH for this vnode */
275 static int
276 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
277 {
278 	struct fuse_flush_in *ffi;
279 	struct fuse_filehandle *fufh;
280 	struct fuse_dispatcher fdi;
281 	struct thread *td = curthread;
282 	struct mount *mp = vnode_mount(vp);
283 	int err;
284 
285 	if (fsess_not_impl(vnode_mount(vp), FUSE_FLUSH))
286 		return 0;
287 
288 	err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
289 	if (err)
290 		return err;
291 
292 	fdisp_init(&fdi, sizeof(*ffi));
293 	fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
294 	ffi = fdi.indata;
295 	ffi->fh = fufh->fh_id;
296 	/*
297 	 * If the file has a POSIX lock then we're supposed to set lock_owner.
298 	 * If not, then lock_owner is undefined.  So we may as well always set
299 	 * it.
300 	 */
301 	ffi->lock_owner = td->td_proc->p_pid;
302 
303 	err = fdisp_wait_answ(&fdi);
304 	if (err == ENOSYS) {
305 		fsess_set_notimpl(mp, FUSE_FLUSH);
306 		err = 0;
307 	}
308 	fdisp_destroy(&fdi);
309 	return err;
310 }
311 
312 /* Close wrapper for fifos.  */
313 static int
314 fuse_fifo_close(struct vop_close_args *ap)
315 {
316 	return (fifo_specops.vop_close(ap));
317 }
318 
319 /* Invalidate a range of cached data, whether dirty of not */
320 static int
321 fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end)
322 {
323 	struct buf *bp;
324 	daddr_t left_lbn, end_lbn, right_lbn;
325 	off_t new_filesize;
326 	int iosize, left_on, right_on, right_blksize;
327 
328 	iosize = fuse_iosize(vp);
329 	left_lbn = start / iosize;
330 	end_lbn = howmany(end, iosize);
331 	left_on = start & (iosize - 1);
332 	if (left_on != 0) {
333 		bp = getblk(vp, left_lbn, iosize, PCATCH, 0, 0);
334 		if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyend >= left_on) {
335 			/*
336 			 * Flush the dirty buffer, because we don't have a
337 			 * byte-granular way to record which parts of the
338 			 * buffer are valid.
339 			 */
340 			bwrite(bp);
341 			if (bp->b_error)
342 				return (bp->b_error);
343 		} else {
344 			brelse(bp);
345 		}
346 	}
347 	right_on = end & (iosize - 1);
348 	if (right_on != 0) {
349 		right_lbn = end / iosize;
350 		new_filesize = MAX(filesize, end);
351 		right_blksize = MIN(iosize, new_filesize - iosize * right_lbn);
352 		bp = getblk(vp, right_lbn, right_blksize, PCATCH, 0, 0);
353 		if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyoff < right_on) {
354 			/*
355 			 * Flush the dirty buffer, because we don't have a
356 			 * byte-granular way to record which parts of the
357 			 * buffer are valid.
358 			 */
359 			bwrite(bp);
360 			if (bp->b_error)
361 				return (bp->b_error);
362 		} else {
363 			brelse(bp);
364 		}
365 	}
366 
367 	v_inval_buf_range(vp, left_lbn, end_lbn, iosize);
368 	return (0);
369 }
370 
371 
372 /* Send FUSE_LSEEK for this node */
373 static int
374 fuse_vnop_do_lseek(struct vnode *vp, struct thread *td, struct ucred *cred,
375 	pid_t pid, off_t *offp, int whence)
376 {
377 	struct fuse_dispatcher fdi;
378 	struct fuse_filehandle *fufh;
379 	struct fuse_lseek_in *flsi;
380 	struct fuse_lseek_out *flso;
381 	struct mount *mp = vnode_mount(vp);
382 	int err;
383 
384 	ASSERT_VOP_LOCKED(vp, __func__);
385 
386 	err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
387 	if (err)
388 		return (err);
389 	fdisp_init(&fdi, sizeof(*flsi));
390 	fdisp_make_vp(&fdi, FUSE_LSEEK, vp, td, cred);
391 	flsi = fdi.indata;
392 	flsi->fh = fufh->fh_id;
393 	flsi->offset = *offp;
394 	flsi->whence = whence;
395 	err = fdisp_wait_answ(&fdi);
396 	if (err == ENOSYS) {
397 		fsess_set_notimpl(mp, FUSE_LSEEK);
398 	} else if (err == ENXIO) {
399 		/* Note: ENXIO means "no more hole/data regions until EOF" */
400 		fsess_set_impl(mp, FUSE_LSEEK);
401 	} else if (err == 0) {
402 		fsess_set_impl(mp, FUSE_LSEEK);
403 		flso = fdi.answ;
404 		*offp = flso->offset;
405 	}
406 	fdisp_destroy(&fdi);
407 
408 	return (err);
409 }
410 
411 /*
412     struct vnop_access_args {
413 	struct vnode *a_vp;
414 #if VOP_ACCESS_TAKES_ACCMODE_T
415 	accmode_t a_accmode;
416 #else
417 	int a_mode;
418 #endif
419 	struct ucred *a_cred;
420 	struct thread *a_td;
421     };
422 */
423 static int
424 fuse_vnop_access(struct vop_access_args *ap)
425 {
426 	struct vnode *vp = ap->a_vp;
427 	int accmode = ap->a_accmode;
428 	struct ucred *cred = ap->a_cred;
429 
430 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
431 
432 	int err;
433 
434 	if (fuse_isdeadfs(vp)) {
435 		if (vnode_isvroot(vp)) {
436 			return 0;
437 		}
438 		return ENXIO;
439 	}
440 	if (!(data->dataflags & FSESS_INITED)) {
441 		if (vnode_isvroot(vp)) {
442 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
443 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
444 				return 0;
445 			}
446 		}
447 		return EBADF;
448 	}
449 	if (vnode_islnk(vp)) {
450 		return 0;
451 	}
452 
453 	err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
454 	return err;
455 }
456 
457 /*
458  * struct vop_advlock_args {
459  *	struct vop_generic_args a_gen;
460  *	struct vnode *a_vp;
461  *	void *a_id;
462  *	int a_op;
463  *	struct flock *a_fl;
464  *	int a_flags;
465  * }
466  */
467 static int
468 fuse_vnop_advlock(struct vop_advlock_args *ap)
469 {
470 	struct vnode *vp = ap->a_vp;
471 	struct flock *fl = ap->a_fl;
472 	struct thread *td = curthread;
473 	struct ucred *cred = td->td_ucred;
474 	pid_t pid = td->td_proc->p_pid;
475 	struct fuse_filehandle *fufh;
476 	struct fuse_dispatcher fdi;
477 	struct fuse_lk_in *fli;
478 	struct fuse_lk_out *flo;
479 	struct vattr vattr;
480 	enum fuse_opcode op;
481 	off_t size, start;
482 	int dataflags, err;
483 	int flags = ap->a_flags;
484 
485 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
486 
487 	if (fuse_isdeadfs(vp)) {
488 		return ENXIO;
489 	}
490 
491 	switch(ap->a_op) {
492 	case F_GETLK:
493 		op = FUSE_GETLK;
494 		break;
495 	case F_SETLK:
496 		if (flags & F_WAIT)
497 			op = FUSE_SETLKW;
498 		else
499 			op = FUSE_SETLK;
500 		break;
501 	case F_UNLCK:
502 		op = FUSE_SETLK;
503 		break;
504 	default:
505 		return EINVAL;
506 	}
507 
508 	if (!(dataflags & FSESS_POSIX_LOCKS))
509 		return vop_stdadvlock(ap);
510 	/* FUSE doesn't properly support flock until protocol 7.17 */
511 	if (flags & F_FLOCK)
512 		return vop_stdadvlock(ap);
513 
514 	vn_lock(vp, LK_SHARED | LK_RETRY);
515 
516 	switch (fl->l_whence) {
517 	case SEEK_SET:
518 	case SEEK_CUR:
519 		/*
520 		 * Caller is responsible for adding any necessary offset
521 		 * when SEEK_CUR is used.
522 		 */
523 		start = fl->l_start;
524 		break;
525 
526 	case SEEK_END:
527 		err = fuse_internal_getattr(vp, &vattr, cred, td);
528 		if (err)
529 			goto out;
530 		size = vattr.va_size;
531 		if (size > OFF_MAX ||
532 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start)) {
533 			err = EOVERFLOW;
534 			goto out;
535 		}
536 		start = size + fl->l_start;
537 		break;
538 
539 	default:
540 		return (EINVAL);
541 	}
542 
543 	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
544 	if (err)
545 		goto out;
546 
547 	fdisp_init(&fdi, sizeof(*fli));
548 
549 	fdisp_make_vp(&fdi, op, vp, td, cred);
550 	fli = fdi.indata;
551 	fli->fh = fufh->fh_id;
552 	fli->owner = td->td_proc->p_pid;
553 	fli->lk.start = start;
554 	if (fl->l_len != 0)
555 		fli->lk.end = start + fl->l_len - 1;
556 	else
557 		fli->lk.end = INT64_MAX;
558 	fli->lk.type = fl->l_type;
559 	fli->lk.pid = td->td_proc->p_pid;
560 
561 	err = fdisp_wait_answ(&fdi);
562 	fdisp_destroy(&fdi);
563 
564 	if (err == 0 && op == FUSE_GETLK) {
565 		flo = fdi.answ;
566 		fl->l_type = flo->lk.type;
567 		fl->l_whence = SEEK_SET;
568 		if (flo->lk.type != F_UNLCK) {
569 			fl->l_pid = flo->lk.pid;
570 			fl->l_start = flo->lk.start;
571 			if (flo->lk.end == INT64_MAX)
572 				fl->l_len = 0;
573 			else
574 				fl->l_len = flo->lk.end - flo->lk.start + 1;
575 			fl->l_start = flo->lk.start;
576 		}
577 	}
578 
579 out:
580 	VOP_UNLOCK(vp);
581 	return err;
582 }
583 
584 static int
585 fuse_vnop_allocate(struct vop_allocate_args *ap)
586 {
587 	struct vnode *vp = ap->a_vp;
588 	off_t *len = ap->a_len;
589 	off_t *offset = ap->a_offset;
590 	struct ucred *cred = ap->a_cred;
591 	struct fuse_filehandle *fufh;
592 	struct mount *mp = vnode_mount(vp);
593 	struct fuse_dispatcher fdi;
594 	struct fuse_fallocate_in *ffi;
595 	struct uio io;
596 	pid_t pid = curthread->td_proc->p_pid;
597 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
598 	off_t filesize;
599 	int err;
600 
601 	if (fuse_isdeadfs(vp))
602 		return (ENXIO);
603 
604 	switch (vp->v_type) {
605 	case VFIFO:
606 		return (ESPIPE);
607 	case VLNK:
608 	case VREG:
609 		if (vfs_isrdonly(mp))
610 			return (EROFS);
611 		break;
612 	default:
613 		return (ENODEV);
614 	}
615 
616 	if (vfs_isrdonly(mp))
617 		return (EROFS);
618 
619 	if (fsess_not_impl(mp, FUSE_FALLOCATE))
620 		return (EINVAL);
621 
622 	io.uio_offset = *offset;
623 	io.uio_resid = *len;
624 	err = vn_rlimit_fsize(vp, &io, curthread);
625 	if (err)
626 		return (err);
627 
628 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
629 	if (err)
630 		return (err);
631 
632 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
633 
634 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
635 	if (err)
636 		return (err);
637 	fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
638 
639 	fdisp_init(&fdi, sizeof(*ffi));
640 	fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
641 	ffi = fdi.indata;
642 	ffi->fh = fufh->fh_id;
643 	ffi->offset = *offset;
644 	ffi->length = *len;
645 	ffi->mode = 0;
646 	err = fdisp_wait_answ(&fdi);
647 
648 	if (err == ENOSYS) {
649 		fsess_set_notimpl(mp, FUSE_FALLOCATE);
650 		err = EINVAL;
651 	} else if (err == EOPNOTSUPP) {
652 		/*
653 		 * The file system server does not support FUSE_FALLOCATE with
654 		 * the supplied mode for this particular file.
655 		 */
656 		err = EINVAL;
657 	} else if (!err) {
658 		*offset += *len;
659 		*len = 0;
660 		fuse_vnode_undirty_cached_timestamps(vp, false);
661 		fuse_internal_clear_suid_on_write(vp, cred, curthread);
662 		if (*offset > fvdat->cached_attrs.va_size) {
663 			fuse_vnode_setsize(vp, *offset, false);
664 			getnanouptime(&fvdat->last_local_modify);
665 		}
666 	}
667 
668 	fdisp_destroy(&fdi);
669 	return (err);
670 }
671 
672 /* {
673 	struct vnode *a_vp;
674 	daddr_t a_bn;
675 	struct bufobj **a_bop;
676 	daddr_t *a_bnp;
677 	int *a_runp;
678 	int *a_runb;
679 } */
680 static int
681 fuse_vnop_bmap(struct vop_bmap_args *ap)
682 {
683 	struct vnode *vp = ap->a_vp;
684 	struct bufobj **bo = ap->a_bop;
685 	struct thread *td = curthread;
686 	struct mount *mp;
687 	struct fuse_dispatcher fdi;
688 	struct fuse_bmap_in *fbi;
689 	struct fuse_bmap_out *fbo;
690 	struct fuse_data *data;
691 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
692 	uint64_t biosize;
693 	off_t fsize;
694 	daddr_t lbn = ap->a_bn;
695 	daddr_t *pbn = ap->a_bnp;
696 	int *runp = ap->a_runp;
697 	int *runb = ap->a_runb;
698 	int error = 0;
699 	int maxrun;
700 
701 	if (fuse_isdeadfs(vp)) {
702 		return ENXIO;
703 	}
704 
705 	mp = vnode_mount(vp);
706 	data = fuse_get_mpdata(mp);
707 	biosize = fuse_iosize(vp);
708 	maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
709 		data->max_readahead_blocks);
710 
711 	if (bo != NULL)
712 		*bo = &vp->v_bufobj;
713 
714 	/*
715 	 * The FUSE_BMAP operation does not include the runp and runb
716 	 * variables, so we must guess.  Report nonzero contiguous runs so
717 	 * cluster_read will combine adjacent reads.  It's worthwhile to reduce
718 	 * upcalls even if we don't know the true physical layout of the file.
719 	 *
720 	 * FUSE file systems may opt out of read clustering in two ways:
721 	 * * mounting with -onoclusterr
722 	 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
723 	 */
724 	if (runb != NULL)
725 		*runb = MIN(lbn, maxrun);
726 	if (runp != NULL && maxrun == 0)
727 		*runp = 0;
728 	else if (runp != NULL) {
729 		/*
730 		 * If the file's size is cached, use that value to calculate
731 		 * runp, even if the cache is expired.  runp is only advisory,
732 		 * and the risk of getting it wrong is not worth the cost of
733 		 * another upcall.
734 		 */
735 		if (fvdat->cached_attrs.va_size != VNOVAL)
736 			fsize = fvdat->cached_attrs.va_size;
737 		else
738 			error = fuse_vnode_size(vp, &fsize, td->td_ucred, td);
739 		if (error == 0)
740 			*runp = MIN(MAX(0, fsize / (off_t)biosize - lbn - 1),
741 				    maxrun);
742 		else
743 			*runp = 0;
744 	}
745 
746 	if (fsess_maybe_impl(mp, FUSE_BMAP)) {
747 		fdisp_init(&fdi, sizeof(*fbi));
748 		fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
749 		fbi = fdi.indata;
750 		fbi->block = lbn;
751 		fbi->blocksize = biosize;
752 		error = fdisp_wait_answ(&fdi);
753 		if (error == ENOSYS) {
754 			fdisp_destroy(&fdi);
755 			fsess_set_notimpl(mp, FUSE_BMAP);
756 			error = 0;
757 		} else {
758 			fbo = fdi.answ;
759 			if (error == 0 && pbn != NULL)
760 				*pbn = fbo->block;
761 			fdisp_destroy(&fdi);
762 			return error;
763 		}
764 	}
765 
766 	/* If the daemon doesn't support BMAP, make up a sensible default */
767 	if (pbn != NULL)
768 		*pbn = lbn * btodb(biosize);
769 	return (error);
770 }
771 
772 /*
773     struct vop_close_args {
774 	struct vnode *a_vp;
775 	int  a_fflag;
776 	struct ucred *a_cred;
777 	struct thread *a_td;
778     };
779 */
780 static int
781 fuse_vnop_close(struct vop_close_args *ap)
782 {
783 	struct vnode *vp = ap->a_vp;
784 	struct mount *mp = vnode_mount(vp);
785 	struct ucred *cred = ap->a_cred;
786 	int fflag = ap->a_fflag;
787 	struct thread *td = ap->a_td;
788 	pid_t pid = td->td_proc->p_pid;
789 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
790 	int err = 0;
791 
792 	if (fuse_isdeadfs(vp))
793 		return 0;
794 	if (vnode_isdir(vp))
795 		return 0;
796 	if (fflag & IO_NDELAY)
797 		return 0;
798 
799 	err = fuse_flush(vp, cred, pid, fflag);
800 	if (err == 0 && (fvdat->flag & FN_ATIMECHANGE) && !vfs_isrdonly(mp)) {
801 		struct vattr vap;
802 		struct fuse_data *data;
803 		int dataflags;
804 		int access_e = 0;
805 
806 		data = fuse_get_mpdata(mp);
807 		dataflags = data->dataflags;
808 		if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
809 			struct vattr va;
810 
811 			fuse_internal_getattr(vp, &va, cred, td);
812 			access_e = vaccess(vp->v_type, va.va_mode, va.va_uid,
813 			    va.va_gid, VWRITE, cred);
814 		}
815 		if (access_e == 0) {
816 			VATTR_NULL(&vap);
817 			vap.va_atime = fvdat->cached_attrs.va_atime;
818 			/*
819 			 * Ignore errors setting when setting atime.  That
820 			 * should not cause close(2) to fail.
821 			 */
822 			fuse_internal_setattr(vp, &vap, td, NULL);
823 		}
824 	}
825 	/* TODO: close the file handle, if we're sure it's no longer used */
826 	if ((fvdat->flag & FN_SIZECHANGE) != 0) {
827 		fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
828 	}
829 	return err;
830 }
831 
832 /*
833    struct vop_copy_file_range_args {
834 	struct vop_generic_args a_gen;
835 	struct vnode *a_invp;
836 	off_t *a_inoffp;
837 	struct vnode *a_outvp;
838 	off_t *a_outoffp;
839 	size_t *a_lenp;
840 	unsigned int a_flags;
841 	struct ucred *a_incred;
842 	struct ucred *a_outcred;
843 	struct thread *a_fsizetd;
844 }
845  */
846 static int
847 fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
848 {
849 	struct vnode *invp = ap->a_invp;
850 	struct vnode *outvp = ap->a_outvp;
851 	struct mount *mp = vnode_mount(invp);
852 	struct fuse_vnode_data *outfvdat = VTOFUD(outvp);
853 	struct fuse_dispatcher fdi;
854 	struct fuse_filehandle *infufh, *outfufh;
855 	struct fuse_copy_file_range_in *fcfri;
856 	struct ucred *incred = ap->a_incred;
857 	struct ucred *outcred = ap->a_outcred;
858 	struct fuse_write_out *fwo;
859 	struct thread *td;
860 	struct uio io;
861 	off_t outfilesize;
862 	ssize_t r = 0;
863 	pid_t pid;
864 	int err;
865 
866 	err = ENOSYS;
867 	if (mp == NULL || mp != vnode_mount(outvp))
868 		goto fallback;
869 
870 	if (incred->cr_uid != outcred->cr_uid)
871 		goto fallback;
872 
873 	if (incred->cr_groups[0] != outcred->cr_groups[0])
874 		goto fallback;
875 
876 	/* Caller busied mp, mnt_data can be safely accessed. */
877 	if (fsess_not_impl(mp, FUSE_COPY_FILE_RANGE))
878 		goto fallback;
879 
880 	if (ap->a_fsizetd == NULL)
881 		td = curthread;
882 	else
883 		td = ap->a_fsizetd;
884 	pid = td->td_proc->p_pid;
885 
886 	vn_lock_pair(invp, false, LK_SHARED, outvp, false, LK_EXCLUSIVE);
887 	if (invp->v_data == NULL || outvp->v_data == NULL) {
888 		err = EBADF;
889 		goto unlock;
890 	}
891 
892 	err = fuse_filehandle_getrw(invp, FREAD, &infufh, incred, pid);
893 	if (err)
894 		goto unlock;
895 
896 	err = fuse_filehandle_getrw(outvp, FWRITE, &outfufh, outcred, pid);
897 	if (err)
898 		goto unlock;
899 
900 	io.uio_resid = *ap->a_lenp;
901 	if (ap->a_fsizetd) {
902 		io.uio_offset = *ap->a_outoffp;
903 		err = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
904 		if (err != 0)
905 			goto unlock;
906 	}
907 
908 	err = fuse_vnode_size(outvp, &outfilesize, outcred, curthread);
909 	if (err)
910 		goto unlock;
911 
912 	vnode_pager_clean_sync(invp);
913 	err = fuse_inval_buf_range(outvp, outfilesize, *ap->a_outoffp,
914 		*ap->a_outoffp + io.uio_resid);
915 	if (err)
916 		goto unlock;
917 
918 	fdisp_init(&fdi, sizeof(*fcfri));
919 	fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred);
920 	fcfri = fdi.indata;
921 	fcfri->fh_in = infufh->fh_id;
922 	fcfri->off_in = *ap->a_inoffp;
923 	fcfri->nodeid_out = VTOI(outvp);
924 	fcfri->fh_out = outfufh->fh_id;
925 	fcfri->off_out = *ap->a_outoffp;
926 	fcfri->len = io.uio_resid;
927 	fcfri->flags = 0;
928 
929 	err = fdisp_wait_answ(&fdi);
930 	if (err == 0) {
931 		fwo = fdi.answ;
932 		*ap->a_lenp = fwo->size;
933 		*ap->a_inoffp += fwo->size;
934 		*ap->a_outoffp += fwo->size;
935 		fuse_internal_clear_suid_on_write(outvp, outcred, td);
936 		if (*ap->a_outoffp > outfvdat->cached_attrs.va_size) {
937                         fuse_vnode_setsize(outvp, *ap->a_outoffp, false);
938 			getnanouptime(&outfvdat->last_local_modify);
939 		}
940 		fuse_vnode_update(invp, FN_ATIMECHANGE);
941 		fuse_vnode_update(outvp, FN_MTIMECHANGE | FN_CTIMECHANGE);
942 	}
943 	fdisp_destroy(&fdi);
944 
945 unlock:
946 	if (invp != outvp)
947 		VOP_UNLOCK(invp);
948 	VOP_UNLOCK(outvp);
949 
950 	if (err == ENOSYS)
951 		fsess_set_notimpl(mp, FUSE_COPY_FILE_RANGE);
952 fallback:
953 
954 	/*
955 	 * No need to call vn_rlimit_fsizex_res before return, since the uio is
956 	 * local.
957 	 */
958 	return (err);
959 }
960 
961 static void
962 fdisp_make_mknod_for_fallback(
963 	struct fuse_dispatcher *fdip,
964 	struct componentname *cnp,
965 	struct vnode *dvp,
966 	uint64_t parentnid,
967 	struct thread *td,
968 	struct ucred *cred,
969 	mode_t mode,
970 	enum fuse_opcode *op)
971 {
972 	struct fuse_mknod_in *fmni;
973 
974 	fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
975 	*op = FUSE_MKNOD;
976 	fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
977 	fmni = fdip->indata;
978 	fmni->mode = mode;
979 	fmni->rdev = 0;
980 	memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
981 	    cnp->cn_namelen);
982 	((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
983 }
984 /*
985     struct vnop_create_args {
986 	struct vnode *a_dvp;
987 	struct vnode **a_vpp;
988 	struct componentname *a_cnp;
989 	struct vattr *a_vap;
990     };
991 */
992 static int
993 fuse_vnop_create(struct vop_create_args *ap)
994 {
995 	struct vnode *dvp = ap->a_dvp;
996 	struct vnode **vpp = ap->a_vpp;
997 	struct componentname *cnp = ap->a_cnp;
998 	struct vattr *vap = ap->a_vap;
999 	struct thread *td = curthread;
1000 	struct ucred *cred = cnp->cn_cred;
1001 
1002 	struct fuse_data *data;
1003 	struct fuse_create_in *fci;
1004 	struct fuse_entry_out *feo;
1005 	struct fuse_open_out *foo;
1006 	struct fuse_dispatcher fdi, fdi2;
1007 	struct fuse_dispatcher *fdip = &fdi;
1008 	struct fuse_dispatcher *fdip2 = NULL;
1009 
1010 	int err;
1011 
1012 	struct mount *mp = vnode_mount(dvp);
1013 	data = fuse_get_mpdata(mp);
1014 	uint64_t parentnid = VTOFUD(dvp)->nid;
1015 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
1016 	enum fuse_opcode op;
1017 	int flags;
1018 
1019 	if (fuse_isdeadfs(dvp))
1020 		return ENXIO;
1021 
1022 	/* FUSE expects sockets to be created with FUSE_MKNOD */
1023 	if (vap->va_type == VSOCK)
1024 		return fuse_internal_mknod(dvp, vpp, cnp, vap);
1025 
1026 	/*
1027 	 * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
1028 	 * writable mode makes sense, and we might as well include readability
1029 	 * too.
1030 	 */
1031 	flags = O_RDWR;
1032 
1033 	bzero(&fdi, sizeof(fdi));
1034 
1035 	if (vap->va_type != VREG)
1036 		return (EINVAL);
1037 
1038 	if (fsess_not_impl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
1039 		/* Fallback to FUSE_MKNOD/FUSE_OPEN */
1040 		fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
1041 			cred, mode, &op);
1042 	} else {
1043 		/* Use FUSE_CREATE */
1044 		size_t insize;
1045 
1046 		op = FUSE_CREATE;
1047 		fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
1048 		fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
1049 		fci = fdip->indata;
1050 		fci->mode = mode;
1051 		fci->flags = O_CREAT | flags;
1052 		if (fuse_libabi_geq(data, 7, 12)) {
1053 			insize = sizeof(*fci);
1054 			fci->umask = td->td_proc->p_pd->pd_cmask;
1055 		} else {
1056 			insize = sizeof(struct fuse_open_in);
1057 		}
1058 
1059 		memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
1060 		    cnp->cn_namelen);
1061 		((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
1062 	}
1063 
1064 	err = fdisp_wait_answ(fdip);
1065 
1066 	if (err) {
1067 		if (err == ENOSYS && op == FUSE_CREATE) {
1068 			fsess_set_notimpl(mp, FUSE_CREATE);
1069 			fdisp_destroy(fdip);
1070 			fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
1071 				parentnid, td, cred, mode, &op);
1072 			err = fdisp_wait_answ(fdip);
1073 		}
1074 		if (err)
1075 			goto out;
1076 	}
1077 
1078 	feo = fdip->answ;
1079 
1080 	if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
1081 		goto out;
1082 	}
1083 
1084 	if (op == FUSE_CREATE) {
1085 		if (fuse_libabi_geq(data, 7, 9))
1086 			foo = (struct fuse_open_out*)(feo + 1);
1087 		else
1088 			foo = (struct fuse_open_out*)((char*)feo +
1089 				FUSE_COMPAT_ENTRY_OUT_SIZE);
1090 	} else {
1091 		/* Issue a separate FUSE_OPEN */
1092 		struct fuse_open_in *foi;
1093 
1094 		fdip2 = &fdi2;
1095 		fdisp_init(fdip2, sizeof(*foi));
1096 		fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
1097 			cred);
1098 		foi = fdip2->indata;
1099 		foi->flags = flags;
1100 		err = fdisp_wait_answ(fdip2);
1101 		if (err)
1102 			goto out;
1103 		foo = fdip2->answ;
1104 	}
1105 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
1106 	if (err) {
1107 		struct fuse_release_in *fri;
1108 		uint64_t nodeid = feo->nodeid;
1109 		uint64_t fh_id = foo->fh;
1110 
1111 		fdisp_destroy(fdip);
1112 		fdisp_init(fdip, sizeof(*fri));
1113 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
1114 		fri = fdip->indata;
1115 		fri->fh = fh_id;
1116 		fri->flags = flags;
1117 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
1118 		fuse_insert_message(fdip->tick, false);
1119 		goto out;
1120 	}
1121 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
1122 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
1123 		feo->attr_valid_nsec, NULL, true);
1124 
1125 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
1126 	fuse_vnode_open(*vpp, foo->open_flags, td);
1127 	/*
1128 	 * Purge the parent's attribute cache because the daemon should've
1129 	 * updated its mtime and ctime
1130 	 */
1131 	fuse_vnode_clear_attr_cache(dvp);
1132 	cache_purge_negative(dvp);
1133 
1134 out:
1135 	if (fdip2)
1136 		fdisp_destroy(fdip2);
1137 	fdisp_destroy(fdip);
1138 	return err;
1139 }
1140 
1141 /*
1142     struct vnop_fdatasync_args {
1143 	struct vop_generic_args a_gen;
1144 	struct vnode * a_vp;
1145 	struct thread * a_td;
1146     };
1147 */
1148 static int
1149 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
1150 {
1151 	struct vnode *vp = ap->a_vp;
1152 	struct thread *td = ap->a_td;
1153 	int waitfor = MNT_WAIT;
1154 
1155 	int err = 0;
1156 
1157 	if (fuse_isdeadfs(vp)) {
1158 		return 0;
1159 	}
1160 	if ((err = vop_stdfdatasync_buf(ap)))
1161 		return err;
1162 
1163 	return fuse_internal_fsync(vp, td, waitfor, true);
1164 }
1165 
1166 /*
1167     struct vnop_fsync_args {
1168 	struct vop_generic_args a_gen;
1169 	struct vnode * a_vp;
1170 	int  a_waitfor;
1171 	struct thread * a_td;
1172     };
1173 */
1174 static int
1175 fuse_vnop_fsync(struct vop_fsync_args *ap)
1176 {
1177 	struct vnode *vp = ap->a_vp;
1178 	struct thread *td = ap->a_td;
1179 	int waitfor = ap->a_waitfor;
1180 	int err = 0;
1181 
1182 	if (fuse_isdeadfs(vp)) {
1183 		return 0;
1184 	}
1185 	if ((err = vop_stdfsync(ap)))
1186 		return err;
1187 
1188 	return fuse_internal_fsync(vp, td, waitfor, false);
1189 }
1190 
1191 /*
1192     struct vnop_getattr_args {
1193 	struct vnode *a_vp;
1194 	struct vattr *a_vap;
1195 	struct ucred *a_cred;
1196 	struct thread *a_td;
1197     };
1198 */
1199 static int
1200 fuse_vnop_getattr(struct vop_getattr_args *ap)
1201 {
1202 	struct vnode *vp = ap->a_vp;
1203 	struct vattr *vap = ap->a_vap;
1204 	struct ucred *cred = ap->a_cred;
1205 	struct thread *td = curthread;
1206 
1207 	int err = 0;
1208 	int dataflags;
1209 
1210 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
1211 
1212 	/* Note that we are not bailing out on a dead file system just yet. */
1213 
1214 	if (!(dataflags & FSESS_INITED)) {
1215 		if (!vnode_isvroot(vp)) {
1216 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
1217 			err = ENOTCONN;
1218 			return err;
1219 		} else {
1220 			goto fake;
1221 		}
1222 	}
1223 	err = fuse_internal_getattr(vp, vap, cred, td);
1224 	if (err == ENOTCONN && vnode_isvroot(vp)) {
1225 		/* see comment in fuse_vfsop_statfs() */
1226 		goto fake;
1227 	} else {
1228 		return err;
1229 	}
1230 
1231 fake:
1232 	bzero(vap, sizeof(*vap));
1233 	vap->va_type = vnode_vtype(vp);
1234 
1235 	return 0;
1236 }
1237 
1238 /*
1239     struct vnop_inactive_args {
1240 	struct vnode *a_vp;
1241     };
1242 */
1243 static int
1244 fuse_vnop_inactive(struct vop_inactive_args *ap)
1245 {
1246 	struct vnode *vp = ap->a_vp;
1247 	struct thread *td = curthread;
1248 
1249 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
1250 	struct fuse_filehandle *fufh, *fufh_tmp;
1251 
1252 	int need_flush = 1;
1253 
1254 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1255 		if (need_flush && vp->v_type == VREG) {
1256 			if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1257 				fuse_vnode_savesize(vp, NULL, 0);
1258 			}
1259 			if ((fvdat->flag & FN_REVOKED) != 0)
1260 				fuse_io_invalbuf(vp, td);
1261 			else
1262 				fuse_io_flushbuf(vp, MNT_WAIT, td);
1263 			need_flush = 0;
1264 		}
1265 		fuse_filehandle_close(vp, fufh, td, NULL);
1266 	}
1267 
1268 	if ((fvdat->flag & FN_REVOKED) != 0)
1269 		vrecycle(vp);
1270 
1271 	return 0;
1272 }
1273 
1274 /*
1275     struct vnop_ioctl_args {
1276 	struct vnode *a_vp;
1277 	u_long a_command;
1278 	caddr_t a_data;
1279 	int a_fflag;
1280 	struct ucred *a_cred;
1281 	struct thread *a_td;
1282     };
1283 */
1284 static int
1285 fuse_vnop_ioctl(struct vop_ioctl_args *ap)
1286 {
1287 	struct vnode *vp = ap->a_vp;
1288 	struct mount *mp = vnode_mount(vp);
1289 	struct ucred *cred = ap->a_cred;
1290 	off_t *offp;
1291 	pid_t pid = ap->a_td->td_proc->p_pid;
1292 	int err;
1293 
1294 	switch (ap->a_command) {
1295 	case FIOSEEKDATA:
1296 	case FIOSEEKHOLE:
1297 		/* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
1298 		if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
1299 			int whence;
1300 
1301 			offp = ap->a_data;
1302 			if (ap->a_command == FIOSEEKDATA)
1303 				whence = SEEK_DATA;
1304 			else
1305 				whence = SEEK_HOLE;
1306 
1307 			vn_lock(vp, LK_SHARED | LK_RETRY);
1308 			err = fuse_vnop_do_lseek(vp, ap->a_td, cred, pid, offp,
1309 			    whence);
1310 			VOP_UNLOCK(vp);
1311 		}
1312 		if (fsess_not_impl(mp, FUSE_LSEEK))
1313 			err = vop_stdioctl(ap);
1314 		break;
1315 	default:
1316 		/* TODO: implement FUSE_IOCTL */
1317 		err = ENOTTY;
1318 		break;
1319 	}
1320 	return (err);
1321 }
1322 
1323 
1324 /*
1325     struct vnop_link_args {
1326 	struct vnode *a_tdvp;
1327 	struct vnode *a_vp;
1328 	struct componentname *a_cnp;
1329     };
1330 */
1331 static int
1332 fuse_vnop_link(struct vop_link_args *ap)
1333 {
1334 	struct vnode *vp = ap->a_vp;
1335 	struct vnode *tdvp = ap->a_tdvp;
1336 	struct componentname *cnp = ap->a_cnp;
1337 
1338 	struct vattr *vap = VTOVA(vp);
1339 
1340 	struct fuse_dispatcher fdi;
1341 	struct fuse_entry_out *feo;
1342 	struct fuse_link_in fli;
1343 
1344 	int err;
1345 
1346 	if (fuse_isdeadfs(vp)) {
1347 		return ENXIO;
1348 	}
1349 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
1350 		return EXDEV;
1351 	}
1352 
1353 	/*
1354 	 * This is a seatbelt check to protect naive userspace filesystems from
1355 	 * themselves and the limitations of the FUSE IPC protocol.  If a
1356 	 * filesystem does not allow attribute caching, assume it is capable of
1357 	 * validating that nlink does not overflow.
1358 	 */
1359 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
1360 		return EMLINK;
1361 	fli.oldnodeid = VTOI(vp);
1362 
1363 	fdisp_init(&fdi, 0);
1364 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
1365 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
1366 	if ((err = fdisp_wait_answ(&fdi))) {
1367 		goto out;
1368 	}
1369 	feo = fdi.answ;
1370 
1371 	if (fli.oldnodeid != feo->nodeid) {
1372 		struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
1373 		fuse_warn(data, FSESS_WARN_ILLEGAL_INODE,
1374 			"Assigned wrong inode for a hard link.");
1375 		fuse_vnode_clear_attr_cache(vp);
1376 		fuse_vnode_clear_attr_cache(tdvp);
1377 		err = EIO;
1378 		goto out;
1379 	}
1380 
1381 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1382 	if (!err) {
1383 		/*
1384 		 * Purge the parent's attribute cache because the daemon
1385 		 * should've updated its mtime and ctime
1386 		 */
1387 		fuse_vnode_clear_attr_cache(tdvp);
1388 		fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
1389 			feo->attr_valid_nsec, NULL, true);
1390 	}
1391 out:
1392 	fdisp_destroy(&fdi);
1393 	return err;
1394 }
1395 
1396 struct fuse_lookup_alloc_arg {
1397 	struct fuse_entry_out *feo;
1398 	struct componentname *cnp;
1399 	uint64_t nid;
1400 	__enum_uint8(vtype) vtyp;
1401 };
1402 
1403 /* Callback for vn_get_ino */
1404 static int
1405 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1406 {
1407 	struct fuse_lookup_alloc_arg *flaa = arg;
1408 
1409 	return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1410 		flaa->vtyp);
1411 }
1412 
1413 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1414 	"int", "struct timespec*", "struct timespec*");
1415 /*
1416     struct vnop_lookup_args {
1417 	struct vnodeop_desc *a_desc;
1418 	struct vnode *a_dvp;
1419 	struct vnode **a_vpp;
1420 	struct componentname *a_cnp;
1421     };
1422 */
1423 int
1424 fuse_vnop_lookup(struct vop_lookup_args *ap)
1425 {
1426 	struct vnode *dvp = ap->a_dvp;
1427 	struct vnode **vpp = ap->a_vpp;
1428 	struct componentname *cnp = ap->a_cnp;
1429 	struct thread *td = curthread;
1430 	struct ucred *cred = cnp->cn_cred;
1431 	struct timespec now;
1432 
1433 	int nameiop = cnp->cn_nameiop;
1434 	int flags = cnp->cn_flags;
1435 	int islastcn = flags & ISLASTCN;
1436 	struct mount *mp = vnode_mount(dvp);
1437 	struct fuse_data *data = fuse_get_mpdata(mp);
1438 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
1439 	bool is_dot;
1440 
1441 	int err = 0;
1442 	int lookup_err = 0;
1443 	struct vnode *vp = NULL;
1444 
1445 	struct fuse_dispatcher fdi;
1446 	bool did_lookup = false;
1447 	struct fuse_entry_out *feo = NULL;
1448 	__enum_uint8(vtype) vtyp;	/* vnode type of target */
1449 
1450 	uint64_t nid;
1451 
1452 	if (fuse_isdeadfs(dvp)) {
1453 		*vpp = NULL;
1454 		return ENXIO;
1455 	}
1456 	if (!vnode_isdir(dvp))
1457 		return ENOTDIR;
1458 
1459 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1460 		return EROFS;
1461 
1462 	if ((cnp->cn_flags & NOEXECCHECK) != 0)
1463 		cnp->cn_flags &= ~NOEXECCHECK;
1464 	else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1465 		return err;
1466 
1467 	is_dot = cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.';
1468 	if ((flags & ISDOTDOT) && !(data->dataflags & FSESS_EXPORT_SUPPORT))
1469 	{
1470 		if (!(VTOFUD(dvp)->flag & FN_PARENT_NID)) {
1471 			/*
1472 			 * Since the file system doesn't support ".." lookups,
1473 			 * we have no way to find this entry.
1474 			 */
1475 			return ESTALE;
1476 		}
1477 		nid = VTOFUD(dvp)->parent_nid;
1478 		if (nid == 0)
1479 			return ENOENT;
1480 		/* .. is obviously a directory */
1481 		vtyp = VDIR;
1482 	} else if (is_dot) {
1483 		nid = VTOI(dvp);
1484 		/* . is obviously a directory */
1485 		vtyp = VDIR;
1486 	} else {
1487 		struct timespec timeout;
1488 		int ncpticks; /* here to accommodate for API contract */
1489 
1490 		err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
1491 		getnanouptime(&now);
1492 		SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1493 		switch (err) {
1494 		case -1:		/* positive match */
1495 			if (timespeccmp(&timeout, &now, >)) {
1496 				counter_u64_add(fuse_lookup_cache_hits, 1);
1497 			} else {
1498 				/* Cache timeout */
1499 				counter_u64_add(fuse_lookup_cache_misses, 1);
1500 				bintime_clear(
1501 					&VTOFUD(*vpp)->entry_cache_timeout);
1502 				cache_purge(*vpp);
1503 				if (dvp != *vpp)
1504 					vput(*vpp);
1505 				else
1506 					vrele(*vpp);
1507 				*vpp = NULL;
1508 				break;
1509 			}
1510 			return 0;
1511 
1512 		case 0:		/* no match in cache */
1513 			counter_u64_add(fuse_lookup_cache_misses, 1);
1514 			break;
1515 
1516 		case ENOENT:		/* negative match */
1517 			if (timespeccmp(&timeout, &now, <=)) {
1518 				/* Cache timeout */
1519 				cache_purge_negative(dvp);
1520 				break;
1521 			}
1522 			/* fall through */
1523 		default:
1524 			return err;
1525 		}
1526 
1527 		fdisp_init(&fdi, cnp->cn_namelen + 1);
1528 		fdisp_make(&fdi, FUSE_LOOKUP, mp, VTOI(dvp), td, cred);
1529 
1530 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1531 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1532 		lookup_err = fdisp_wait_answ(&fdi);
1533 		did_lookup = true;
1534 
1535 		if (!lookup_err) {
1536 			/* lookup call succeeded */
1537 			feo = (struct fuse_entry_out *)fdi.answ;
1538 			nid = feo->nodeid;
1539 			if (nid == 0) {
1540 				/* zero nodeid means ENOENT and cache it */
1541 				struct timespec timeout;
1542 
1543 				fdi.answ_stat = ENOENT;
1544 				lookup_err = ENOENT;
1545 				if (cnp->cn_flags & MAKEENTRY) {
1546 					fuse_validity_2_timespec(feo, &timeout);
1547 					/* Use the same entry_time for .. as for
1548 					 * the file itself.  That doesn't honor
1549 					 * exactly what the fuse server tells
1550 					 * us, but to do otherwise would require
1551 					 * another cache lookup at this point.
1552 					 */
1553 					struct timespec *dtsp = NULL;
1554 					cache_enter_time(dvp, *vpp, cnp,
1555 						&timeout, dtsp);
1556 				}
1557 			}
1558 			vtyp = IFTOVT(feo->attr.mode);
1559 		}
1560 		if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1561 			fdisp_destroy(&fdi);
1562 			return lookup_err;
1563 		}
1564 	}
1565 	/* lookup_err, if non-zero, must be ENOENT at this point */
1566 
1567 	if (lookup_err) {
1568 		/* Entry not found */
1569 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1570 			if (default_permissions)
1571 				err = fuse_internal_access(dvp, VWRITE, td,
1572 				    cred);
1573 			else
1574 				err = 0;
1575 			if (!err) {
1576 				err = EJUSTRETURN;
1577 			}
1578 		} else {
1579 			err = ENOENT;
1580 		}
1581 	} else {
1582 		/* Entry was found */
1583 		if (flags & ISDOTDOT) {
1584 			struct fuse_lookup_alloc_arg flaa;
1585 
1586 			flaa.nid = nid;
1587 			flaa.feo = feo;
1588 			flaa.cnp = cnp;
1589 			flaa.vtyp = vtyp;
1590 			err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1591 				&vp);
1592 			*vpp = vp;
1593 		} else if (nid == VTOI(dvp)) {
1594 			if (is_dot) {
1595 				vref(dvp);
1596 				*vpp = dvp;
1597 			} else {
1598 				fuse_warn(fuse_get_mpdata(mp),
1599 				    FSESS_WARN_ILLEGAL_INODE,
1600 				    "Assigned same inode to both parent and "
1601 				    "child.");
1602 				err = EIO;
1603 			}
1604 
1605 		} else {
1606 			struct fuse_vnode_data *fvdat;
1607 
1608 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1609 			    &vp, cnp, vtyp);
1610 			if (err)
1611 				goto out;
1612 			*vpp = vp;
1613 			fvdat = VTOFUD(vp);
1614 
1615 			MPASS(feo != NULL);
1616 			if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
1617 				/*
1618 				 * Attributes from the server are definitely
1619 				 * newer than the last attributes we sent to
1620 				 * the server, so cache them.
1621 				 */
1622 				fuse_internal_cache_attrs(*vpp, &feo->attr,
1623 					feo->attr_valid, feo->attr_valid_nsec,
1624 					NULL, true);
1625 			}
1626 			fuse_validity_2_bintime(feo->entry_valid,
1627 				feo->entry_valid_nsec,
1628 				&fvdat->entry_cache_timeout);
1629 
1630 			if ((nameiop == DELETE || nameiop == RENAME) &&
1631 				islastcn && default_permissions)
1632 			{
1633 				struct vattr dvattr;
1634 
1635 				err = fuse_internal_access(dvp, VWRITE, td,
1636 					cred);
1637 				if (err != 0)
1638 					goto out;
1639 				/*
1640 				 * if the parent's sticky bit is set, check
1641 				 * whether we're allowed to remove the file.
1642 				 * Need to figure out the vnode locking to make
1643 				 * this work.
1644 				 */
1645 				fuse_internal_getattr(dvp, &dvattr, cred, td);
1646 				if ((dvattr.va_mode & S_ISTXT) &&
1647 					fuse_internal_access(dvp, VADMIN, td,
1648 						cred) &&
1649 					fuse_internal_access(*vpp, VADMIN, td,
1650 						cred)) {
1651 					err = EPERM;
1652 					goto out;
1653 				}
1654 			}
1655 		}
1656 	}
1657 out:
1658 	if (err) {
1659 		if (vp != NULL && dvp != vp)
1660 			vput(vp);
1661 		else if (vp != NULL)
1662 			vrele(vp);
1663 		*vpp = NULL;
1664 	}
1665 	if (did_lookup)
1666 		fdisp_destroy(&fdi);
1667 
1668 	return err;
1669 }
1670 
1671 /*
1672     struct vnop_mkdir_args {
1673 	struct vnode *a_dvp;
1674 	struct vnode **a_vpp;
1675 	struct componentname *a_cnp;
1676 	struct vattr *a_vap;
1677     };
1678 */
1679 static int
1680 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1681 {
1682 	struct vnode *dvp = ap->a_dvp;
1683 	struct vnode **vpp = ap->a_vpp;
1684 	struct componentname *cnp = ap->a_cnp;
1685 	struct vattr *vap = ap->a_vap;
1686 
1687 	struct fuse_mkdir_in fmdi;
1688 
1689 	if (fuse_isdeadfs(dvp)) {
1690 		return ENXIO;
1691 	}
1692 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1693 	fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
1694 
1695 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1696 	    sizeof(fmdi), VDIR));
1697 }
1698 
1699 /*
1700     struct vnop_mknod_args {
1701 	struct vnode *a_dvp;
1702 	struct vnode **a_vpp;
1703 	struct componentname *a_cnp;
1704 	struct vattr *a_vap;
1705     };
1706 */
1707 static int
1708 fuse_vnop_mknod(struct vop_mknod_args *ap)
1709 {
1710 
1711 	struct vnode *dvp = ap->a_dvp;
1712 	struct vnode **vpp = ap->a_vpp;
1713 	struct componentname *cnp = ap->a_cnp;
1714 	struct vattr *vap = ap->a_vap;
1715 
1716 	if (fuse_isdeadfs(dvp))
1717 		return ENXIO;
1718 
1719 	return fuse_internal_mknod(dvp, vpp, cnp, vap);
1720 }
1721 
1722 /*
1723     struct vop_open_args {
1724 	struct vnode *a_vp;
1725 	int  a_mode;
1726 	struct ucred *a_cred;
1727 	struct thread *a_td;
1728 	int a_fdidx; / struct file *a_fp;
1729     };
1730 */
1731 static int
1732 fuse_vnop_open(struct vop_open_args *ap)
1733 {
1734 	struct vnode *vp = ap->a_vp;
1735 	int a_mode = ap->a_mode;
1736 	struct thread *td = ap->a_td;
1737 	struct ucred *cred = ap->a_cred;
1738 	pid_t pid = td->td_proc->p_pid;
1739 
1740 	if (fuse_isdeadfs(vp))
1741 		return ENXIO;
1742 	if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1743 		return (EOPNOTSUPP);
1744 	if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1745 		return EINVAL;
1746 
1747 	if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1748 		fuse_vnode_open(vp, 0, td);
1749 		return 0;
1750 	}
1751 
1752 	return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1753 }
1754 
1755 static int
1756 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1757 {
1758 	struct vnode *vp = ap->a_vp;
1759 	struct mount *mp;
1760 	struct fuse_filehandle *fufh;
1761 	int err;
1762 	bool closefufh = false;
1763 
1764 	switch (ap->a_name) {
1765 	case _PC_FILESIZEBITS:
1766 		*ap->a_retval = 64;
1767 		return (0);
1768 	case _PC_NAME_MAX:
1769 		*ap->a_retval = NAME_MAX;
1770 		return (0);
1771 	case _PC_LINK_MAX:
1772 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1773 		return (0);
1774 	case _PC_SYMLINK_MAX:
1775 		*ap->a_retval = MAXPATHLEN;
1776 		return (0);
1777 	case _PC_NO_TRUNC:
1778 		*ap->a_retval = 1;
1779 		return (0);
1780 	case _PC_MIN_HOLE_SIZE:
1781 		/*
1782 		 * The FUSE protocol provides no mechanism for a server to
1783 		 * report _PC_MIN_HOLE_SIZE.  It's a protocol bug.  Instead,
1784 		 * return EINVAL if the server does not support FUSE_LSEEK, or
1785 		 * 1 if it does.
1786 		 */
1787 		mp = vnode_mount(vp);
1788 		if (!fsess_is_impl(mp, FUSE_LSEEK) &&
1789 		    !fsess_not_impl(mp, FUSE_LSEEK)) {
1790 			off_t offset = 0;
1791 
1792 			/*
1793 			 * Issue a FUSE_LSEEK to find out if it's supported.
1794 			 * Use SEEK_DATA instead of SEEK_HOLE, because the
1795 			 * latter generally requires sequential scans of file
1796 			 * metadata, which can be slow.
1797 			 */
1798 			err = fuse_vnop_do_lseek(vp, curthread,
1799 			    curthread->td_ucred, curthread->td_proc->p_pid,
1800 			    &offset, SEEK_DATA);
1801 			if (err == EBADF) {
1802 				/*
1803 				 * pathconf() doesn't necessarily open the
1804 				 * file.  So we may need to do it here.
1805 				 */
1806 				err = fuse_filehandle_open(vp, FREAD, &fufh,
1807 				    curthread, curthread->td_ucred);
1808 				if (err == 0) {
1809 					closefufh = true;
1810 					err = fuse_vnop_do_lseek(vp, curthread,
1811 					    curthread->td_ucred,
1812 					    curthread->td_proc->p_pid, &offset,
1813 					    SEEK_DATA);
1814 				}
1815 				if (closefufh)
1816 					fuse_filehandle_close(vp, fufh,
1817 					    curthread, curthread->td_ucred);
1818 			}
1819 
1820 		}
1821 
1822 		if (fsess_is_impl(mp, FUSE_LSEEK)) {
1823 			*ap->a_retval = 1;
1824 			return (0);
1825 		} else if (fsess_not_impl(mp, FUSE_LSEEK)) {
1826 			/* FUSE_LSEEK is not implemented */
1827 			return (EINVAL);
1828 		} else {
1829 			return (err);
1830 		}
1831 	default:
1832 		return (vop_stdpathconf(ap));
1833 	}
1834 }
1835 
1836 SDT_PROBE_DEFINE3(fusefs, , vnops, filehandles_closed, "struct vnode*",
1837     "struct uio*", "struct ucred*");
1838 /*
1839     struct vnop_read_args {
1840 	struct vnode *a_vp;
1841 	struct uio *a_uio;
1842 	int  a_ioflag;
1843 	struct ucred *a_cred;
1844     };
1845 */
1846 static int
1847 fuse_vnop_read(struct vop_read_args *ap)
1848 {
1849 	struct vnode *vp = ap->a_vp;
1850 	struct uio *uio = ap->a_uio;
1851 	int ioflag = ap->a_ioflag;
1852 	struct ucred *cred = ap->a_cred;
1853 	pid_t pid = curthread->td_proc->p_pid;
1854 	struct fuse_filehandle *fufh;
1855 	int err;
1856 	bool closefufh = false, directio;
1857 
1858 	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
1859 
1860 	if (fuse_isdeadfs(vp)) {
1861 		return ENXIO;
1862 	}
1863 
1864 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1865 		ioflag |= IO_DIRECT;
1866 	}
1867 
1868 	err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
1869 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1870 		/*
1871 		 * nfsd will do I/O without first doing VOP_OPEN.  We
1872 		 * must implicitly open the file here
1873 		 */
1874 		err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1875 		closefufh = true;
1876 	}
1877 	if (err) {
1878 		SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
1879 		return err;
1880 	}
1881 
1882 	/*
1883          * Ideally, when the daemon asks for direct io at open time, the
1884          * standard file flag should be set according to this, so that would
1885          * just change the default mode, which later on could be changed via
1886          * fcntl(2).
1887          * But this doesn't work, the O_DIRECT flag gets cleared at some point
1888          * (don't know where). So to make any use of the Fuse direct_io option,
1889          * we hardwire it into the file's private data (similarly to Linux,
1890          * btw.).
1891          */
1892 	directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
1893 
1894 	fuse_vnode_update(vp, FN_ATIMECHANGE);
1895 	if (directio) {
1896 		SDT_PROBE2(fusefs, , vnops, trace, 1, "direct read of vnode");
1897 		err = fuse_read_directbackend(vp, uio, cred, fufh);
1898 	} else {
1899 		SDT_PROBE2(fusefs, , vnops, trace, 1, "buffered read of vnode");
1900 		err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh, pid);
1901 	}
1902 
1903 	if (closefufh)
1904 		fuse_filehandle_close(vp, fufh, curthread, cred);
1905 
1906 	return (err);
1907 }
1908 
1909 /*
1910     struct vnop_readdir_args {
1911 	struct vnode *a_vp;
1912 	struct uio *a_uio;
1913 	struct ucred *a_cred;
1914 	int *a_eofflag;
1915 	int *a_ncookies;
1916 	uint64_t **a_cookies;
1917     };
1918 */
1919 static int
1920 fuse_vnop_readdir(struct vop_readdir_args *ap)
1921 {
1922 	struct vnode *vp = ap->a_vp;
1923 	struct uio *uio = ap->a_uio;
1924 	struct ucred *cred = ap->a_cred;
1925 	struct fuse_filehandle *fufh = NULL;
1926 	struct mount *mp = vnode_mount(vp);
1927 	struct fuse_iov cookediov;
1928 	int err = 0;
1929 	uint64_t *cookies;
1930 	ssize_t tresid;
1931 	int ncookies;
1932 	bool closefufh = false;
1933 	pid_t pid = curthread->td_proc->p_pid;
1934 
1935 	if (ap->a_eofflag)
1936 		*ap->a_eofflag = 0;
1937 	if (fuse_isdeadfs(vp)) {
1938 		return ENXIO;
1939 	}
1940 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
1941 	    (uio_resid(uio) < sizeof(struct dirent))) {
1942 		return EINVAL;
1943 	}
1944 
1945 	tresid = uio->uio_resid;
1946 	err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1947 	if (err == EBADF && mp->mnt_flag & MNT_EXPORTED) {
1948 		KASSERT(fuse_get_mpdata(mp)->dataflags
1949 				& FSESS_NO_OPENDIR_SUPPORT,
1950 			("FUSE file systems that don't set "
1951 			 "FUSE_NO_OPENDIR_SUPPORT should not be exported"));
1952 		/*
1953 		 * nfsd will do VOP_READDIR without first doing VOP_OPEN.  We
1954 		 * must implicitly open the directory here.
1955 		 */
1956 		err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1957 		closefufh = true;
1958 	}
1959 	if (err)
1960 		return (err);
1961 	if (ap->a_ncookies != NULL) {
1962 		ncookies = uio->uio_resid /
1963 			(offsetof(struct dirent, d_name) + 4) + 1;
1964 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1965 		*ap->a_ncookies = ncookies;
1966 		*ap->a_cookies = cookies;
1967 	} else {
1968 		ncookies = 0;
1969 		cookies = NULL;
1970 	}
1971 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1972 	fiov_init(&cookediov, DIRCOOKEDSIZE);
1973 
1974 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov,
1975 		&ncookies, cookies);
1976 
1977 	fiov_teardown(&cookediov);
1978 	if (closefufh)
1979 		fuse_filehandle_close(vp, fufh, curthread, cred);
1980 
1981 	if (ap->a_ncookies != NULL) {
1982 		if (err == 0) {
1983 			*ap->a_ncookies -= ncookies;
1984 		} else {
1985 			free(*ap->a_cookies, M_TEMP);
1986 			*ap->a_ncookies = 0;
1987 			*ap->a_cookies = NULL;
1988 		}
1989 	}
1990 	if (err == 0 && tresid == uio->uio_resid)
1991 		*ap->a_eofflag = 1;
1992 
1993 	return err;
1994 }
1995 
1996 /*
1997     struct vnop_readlink_args {
1998 	struct vnode *a_vp;
1999 	struct uio *a_uio;
2000 	struct ucred *a_cred;
2001     };
2002 */
2003 static int
2004 fuse_vnop_readlink(struct vop_readlink_args *ap)
2005 {
2006 	struct vnode *vp = ap->a_vp;
2007 	struct uio *uio = ap->a_uio;
2008 	struct ucred *cred = ap->a_cred;
2009 
2010 	struct fuse_dispatcher fdi;
2011 	int err;
2012 
2013 	if (fuse_isdeadfs(vp)) {
2014 		return ENXIO;
2015 	}
2016 	if (!vnode_islnk(vp)) {
2017 		return EINVAL;
2018 	}
2019 	fdisp_init(&fdi, 0);
2020 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
2021 	if (err) {
2022 		goto out;
2023 	}
2024 	if (strnlen(fdi.answ, fdi.iosize) + 1 < fdi.iosize) {
2025 		struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2026 		fuse_warn(data, FSESS_WARN_READLINK_EMBEDDED_NUL,
2027 				"Returned an embedded NUL from FUSE_READLINK.");
2028 		err = EIO;
2029 		goto out;
2030 	}
2031 	if (((char *)fdi.answ)[0] == '/' &&
2032 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
2033 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
2034 
2035 		err = uiomove(mpth, strlen(mpth), uio);
2036 	}
2037 	if (!err) {
2038 		err = uiomove(fdi.answ, fdi.iosize, uio);
2039 	}
2040 out:
2041 	fdisp_destroy(&fdi);
2042 	return err;
2043 }
2044 
2045 /*
2046     struct vnop_reclaim_args {
2047 	struct vnode *a_vp;
2048     };
2049 */
2050 static int
2051 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
2052 {
2053 	struct vnode *vp = ap->a_vp;
2054 	struct thread *td = curthread;
2055 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
2056 	struct fuse_filehandle *fufh, *fufh_tmp;
2057 
2058 	if (!fvdat) {
2059 		panic("FUSE: no vnode data during recycling");
2060 	}
2061 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
2062 		printf("FUSE: vnode being reclaimed with open fufh "
2063 			"(type=%#x)", fufh->fufh_type);
2064 		fuse_filehandle_close(vp, fufh, td, NULL);
2065 	}
2066 
2067 	if (VTOI(vp) == 1) {
2068 		/*
2069 		 * Don't send FUSE_FORGET for the root inode, because
2070 		 * we never send FUSE_LOOKUP for it (see
2071 		 * fuse_vfsop_root) and we don't want the server to see
2072 		 * mismatched lookup counts.
2073 		 */
2074 		struct fuse_data *data;
2075 		struct vnode *vroot;
2076 
2077 		data = fuse_get_mpdata(vnode_mount(vp));
2078 		FUSE_LOCK();
2079 		vroot = data->vroot;
2080 		data->vroot = NULL;
2081 		FUSE_UNLOCK();
2082 		if (vroot)
2083 			vrele(vroot);
2084 	} else if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
2085 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
2086 		    fvdat->nlookup);
2087 	}
2088 	cache_purge(vp);
2089 	vfs_hash_remove(vp);
2090 	fuse_vnode_destroy(vp);
2091 
2092 	return 0;
2093 }
2094 
2095 /*
2096     struct vnop_remove_args {
2097 	struct vnode *a_dvp;
2098 	struct vnode *a_vp;
2099 	struct componentname *a_cnp;
2100     };
2101 */
2102 static int
2103 fuse_vnop_remove(struct vop_remove_args *ap)
2104 {
2105 	struct vnode *dvp = ap->a_dvp;
2106 	struct vnode *vp = ap->a_vp;
2107 	struct componentname *cnp = ap->a_cnp;
2108 
2109 	int err;
2110 
2111 	if (fuse_isdeadfs(vp)) {
2112 		return ENXIO;
2113 	}
2114 	if (vnode_isdir(vp)) {
2115 		return EPERM;
2116 	}
2117 
2118 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
2119 
2120 	return err;
2121 }
2122 
2123 /*
2124     struct vnop_rename_args {
2125 	struct vnode *a_fdvp;
2126 	struct vnode *a_fvp;
2127 	struct componentname *a_fcnp;
2128 	struct vnode *a_tdvp;
2129 	struct vnode *a_tvp;
2130 	struct componentname *a_tcnp;
2131     };
2132 */
2133 static int
2134 fuse_vnop_rename(struct vop_rename_args *ap)
2135 {
2136 	struct vnode *fdvp = ap->a_fdvp;
2137 	struct vnode *fvp = ap->a_fvp;
2138 	struct componentname *fcnp = ap->a_fcnp;
2139 	struct vnode *tdvp = ap->a_tdvp;
2140 	struct vnode *tvp = ap->a_tvp;
2141 	struct componentname *tcnp = ap->a_tcnp;
2142 	struct fuse_data *data;
2143 	bool newparent = fdvp != tdvp;
2144 	bool isdir = fvp->v_type == VDIR;
2145 	int err = 0;
2146 
2147 	if (fuse_isdeadfs(fdvp)) {
2148 		return ENXIO;
2149 	}
2150 	if (fvp->v_mount != tdvp->v_mount ||
2151 	    (tvp && fvp->v_mount != tvp->v_mount)) {
2152 		SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
2153 		err = EXDEV;
2154 		goto out;
2155 	}
2156 	cache_purge(fvp);
2157 
2158 	/*
2159 	 * FUSE library is expected to check if target directory is not
2160 	 * under the source directory in the file system tree.
2161 	 * Linux performs this check at VFS level.
2162 	 */
2163 	/*
2164 	 * If source is a directory, and it will get a new parent, user must
2165 	 * have write permission to it, so ".." can be modified.
2166 	 */
2167 	data = fuse_get_mpdata(vnode_mount(tdvp));
2168 	if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
2169 		err = fuse_internal_access(fvp, VWRITE,
2170 			curthread, tcnp->cn_cred);
2171 		if (err)
2172 			goto out;
2173 	}
2174 	sx_xlock(&data->rename_lock);
2175 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
2176 	if (err == 0) {
2177 		if (tdvp != fdvp)
2178 			fuse_vnode_setparent(fvp, tdvp);
2179 		if (tvp != NULL)
2180 			fuse_vnode_setparent(tvp, NULL);
2181 	}
2182 	sx_unlock(&data->rename_lock);
2183 
2184 	if (tvp != NULL && tvp != fvp) {
2185 		cache_purge(tvp);
2186 	}
2187 	if (vnode_isdir(fvp)) {
2188 		if (((tvp != NULL) && vnode_isdir(tvp)) || vnode_isdir(fvp)) {
2189 			cache_purge(tdvp);
2190 		}
2191 		cache_purge(fdvp);
2192 	}
2193 out:
2194 	if (tdvp == tvp) {
2195 		vrele(tdvp);
2196 	} else {
2197 		vput(tdvp);
2198 	}
2199 	if (tvp != NULL) {
2200 		vput(tvp);
2201 	}
2202 	vrele(fdvp);
2203 	vrele(fvp);
2204 
2205 	return err;
2206 }
2207 
2208 /*
2209     struct vnop_rmdir_args {
2210 	    struct vnode *a_dvp;
2211 	    struct vnode *a_vp;
2212 	    struct componentname *a_cnp;
2213     } *ap;
2214 */
2215 static int
2216 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
2217 {
2218 	struct vnode *dvp = ap->a_dvp;
2219 	struct vnode *vp = ap->a_vp;
2220 
2221 	int err;
2222 
2223 	if (fuse_isdeadfs(vp)) {
2224 		return ENXIO;
2225 	}
2226 	if (VTOFUD(vp) == VTOFUD(dvp)) {
2227 		return EINVAL;
2228 	}
2229 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
2230 
2231 	return err;
2232 }
2233 
2234 /*
2235     struct vnop_setattr_args {
2236 	struct vnode *a_vp;
2237 	struct vattr *a_vap;
2238 	struct ucred *a_cred;
2239 	struct thread *a_td;
2240     };
2241 */
2242 static int
2243 fuse_vnop_setattr(struct vop_setattr_args *ap)
2244 {
2245 	struct vnode *vp = ap->a_vp;
2246 	struct vattr *vap = ap->a_vap;
2247 	struct ucred *cred = ap->a_cred;
2248 	struct thread *td = curthread;
2249 	struct mount *mp;
2250 	struct fuse_data *data;
2251 	struct vattr old_va;
2252 	int dataflags;
2253 	int err = 0, err2;
2254 	accmode_t accmode = 0;
2255 	bool checkperm;
2256 	bool drop_suid = false;
2257 
2258 	mp = vnode_mount(vp);
2259 	data = fuse_get_mpdata(mp);
2260 	dataflags = data->dataflags;
2261 	checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
2262 
2263 	if (fuse_isdeadfs(vp)) {
2264 		return ENXIO;
2265 	}
2266 
2267 	if (vap->va_uid != (uid_t)VNOVAL) {
2268 		if (checkperm) {
2269 			/* Only root may change a file's owner */
2270 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2271 			if (err) {
2272 				/* As a special case, allow the null chown */
2273 				err2 = fuse_internal_getattr(vp, &old_va, cred,
2274 					td);
2275 				if (err2)
2276 					return (err2);
2277 				if (vap->va_uid != old_va.va_uid)
2278 					return err;
2279 				drop_suid = true;
2280 			}
2281 		}
2282 		accmode |= VADMIN;
2283 	}
2284 	if (vap->va_gid != (gid_t)VNOVAL) {
2285 		if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
2286 			drop_suid = true;
2287 		if (checkperm && !groupmember(vap->va_gid, cred)) {
2288 			/*
2289 			 * Non-root users may only chgrp to one of their own
2290 			 * groups
2291 			 */
2292 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2293 			if (err) {
2294 				/* As a special case, allow the null chgrp */
2295 				err2 = fuse_internal_getattr(vp, &old_va, cred,
2296 					td);
2297 				if (err2)
2298 					return (err2);
2299 				if (vap->va_gid != old_va.va_gid)
2300 					return err;
2301 			}
2302 		}
2303 		accmode |= VADMIN;
2304 	}
2305 	if (vap->va_size != VNOVAL) {
2306 		switch (vp->v_type) {
2307 		case VDIR:
2308 			return (EISDIR);
2309 		case VLNK:
2310 		case VREG:
2311 			if (vfs_isrdonly(mp))
2312 				return (EROFS);
2313 			err = vn_rlimit_trunc(vap->va_size, td);
2314 			if (err)
2315 				return (err);
2316 			break;
2317 		default:
2318 			/*
2319 			 * According to POSIX, the result is unspecified
2320 			 * for file types other than regular files,
2321 			 * directories and shared memory objects.  We
2322 			 * don't support shared memory objects in the file
2323 			 * system, and have dubious support for truncating
2324 			 * symlinks.  Just ignore the request in other cases.
2325 			 */
2326 			return (0);
2327 		}
2328 		/* Don't set accmode.  Permission to trunc is checked upstack */
2329 	}
2330 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2331 		if (vap->va_vaflags & VA_UTIMES_NULL)
2332 			accmode |= VWRITE;
2333 		else
2334 			accmode |= VADMIN;
2335 	}
2336 	if (drop_suid) {
2337 		if (vap->va_mode != (mode_t)VNOVAL)
2338 			vap->va_mode &= ~(S_ISUID | S_ISGID);
2339 		else {
2340 			err = fuse_internal_getattr(vp, &old_va, cred, td);
2341 			if (err)
2342 				return (err);
2343 			vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2344 		}
2345 	}
2346 	if (vap->va_mode != (mode_t)VNOVAL) {
2347 		/* Only root may set the sticky bit on non-directories */
2348 		if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2349 		    && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2350 			return EFTYPE;
2351 		if (checkperm && (vap->va_mode & S_ISGID)) {
2352 			err = fuse_internal_getattr(vp, &old_va, cred, td);
2353 			if (err)
2354 				return (err);
2355 			if (!groupmember(old_va.va_gid, cred)) {
2356 				err = priv_check_cred(cred, PRIV_VFS_SETGID);
2357 				if (err)
2358 					return (err);
2359 			}
2360 		}
2361 		accmode |= VADMIN;
2362 	}
2363 
2364 	if (vfs_isrdonly(mp))
2365 		return EROFS;
2366 
2367 	if (checkperm) {
2368 		err = fuse_internal_access(vp, accmode, td, cred);
2369 	} else {
2370 		err = 0;
2371 	}
2372 	if (err)
2373 		return err;
2374 	else
2375 		return fuse_internal_setattr(vp, vap, td, cred);
2376 }
2377 
2378 /*
2379     struct vnop_strategy_args {
2380 	struct vnode *a_vp;
2381 	struct buf *a_bp;
2382     };
2383 */
2384 static int
2385 fuse_vnop_strategy(struct vop_strategy_args *ap)
2386 {
2387 	struct vnode *vp = ap->a_vp;
2388 	struct buf *bp = ap->a_bp;
2389 
2390 	if (!vp || fuse_isdeadfs(vp)) {
2391 		bp->b_ioflags |= BIO_ERROR;
2392 		bp->b_error = ENXIO;
2393 		bufdone(bp);
2394 		return 0;
2395 	}
2396 
2397 	/*
2398 	 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2399 	 * fuse_io_strategy sets bp's error fields
2400 	 */
2401 	(void)fuse_io_strategy(vp, bp);
2402 
2403 	return 0;
2404 }
2405 
2406 /*
2407     struct vnop_symlink_args {
2408 	struct vnode *a_dvp;
2409 	struct vnode **a_vpp;
2410 	struct componentname *a_cnp;
2411 	struct vattr *a_vap;
2412 	char *a_target;
2413     };
2414 */
2415 static int
2416 fuse_vnop_symlink(struct vop_symlink_args *ap)
2417 {
2418 	struct vnode *dvp = ap->a_dvp;
2419 	struct vnode **vpp = ap->a_vpp;
2420 	struct componentname *cnp = ap->a_cnp;
2421 	const char *target = ap->a_target;
2422 
2423 	struct fuse_dispatcher fdi;
2424 
2425 	int err;
2426 	size_t len;
2427 
2428 	if (fuse_isdeadfs(dvp)) {
2429 		return ENXIO;
2430 	}
2431 	/*
2432 	 * Unlike the other creator type calls, here we have to create a message
2433 	 * where the name of the new entry comes first, and the data describing
2434 	 * the entry comes second.
2435 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
2436 	 * but put together the message manually and just call the core part.
2437 	 */
2438 
2439 	len = strlen(target) + 1;
2440 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
2441 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
2442 
2443 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
2444 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
2445 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
2446 
2447 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
2448 	fdisp_destroy(&fdi);
2449 	return err;
2450 }
2451 
2452 /*
2453     struct vnop_write_args {
2454 	struct vnode *a_vp;
2455 	struct uio *a_uio;
2456 	int  a_ioflag;
2457 	struct ucred *a_cred;
2458     };
2459 */
2460 static int
2461 fuse_vnop_write(struct vop_write_args *ap)
2462 {
2463 	struct vnode *vp = ap->a_vp;
2464 	struct uio *uio = ap->a_uio;
2465 	int ioflag = ap->a_ioflag;
2466 	struct ucred *cred = ap->a_cred;
2467 	pid_t pid = curthread->td_proc->p_pid;
2468 	struct fuse_filehandle *fufh;
2469 	int err;
2470 	bool closefufh = false, directio;
2471 
2472 	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
2473 
2474 	if (fuse_isdeadfs(vp)) {
2475 		return ENXIO;
2476 	}
2477 
2478 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2479 		ioflag |= IO_DIRECT;
2480 	}
2481 
2482 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2483 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2484 		/*
2485 		 * nfsd will do I/O without first doing VOP_OPEN.  We
2486 		 * must implicitly open the file here
2487 		 */
2488 		err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2489 		closefufh = true;
2490 	}
2491 	if (err) {
2492 		SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
2493 		return err;
2494 	}
2495 
2496 	/*
2497          * Ideally, when the daemon asks for direct io at open time, the
2498          * standard file flag should be set according to this, so that would
2499          * just change the default mode, which later on could be changed via
2500          * fcntl(2).
2501          * But this doesn't work, the O_DIRECT flag gets cleared at some point
2502          * (don't know where). So to make any use of the Fuse direct_io option,
2503          * we hardwire it into the file's private data (similarly to Linux,
2504          * btw.).
2505          */
2506 	directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
2507 
2508 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2509 	if (directio) {
2510 		off_t start, end, filesize;
2511 		bool pages = (ioflag & IO_VMIO) != 0;
2512 
2513 		SDT_PROBE2(fusefs, , vnops, trace, 1, "direct write of vnode");
2514 
2515 		err = fuse_vnode_size(vp, &filesize, cred, curthread);
2516 		if (err)
2517 			goto out;
2518 
2519 		start = uio->uio_offset;
2520 		end = start + uio->uio_resid;
2521 		if (!pages) {
2522 			err = fuse_inval_buf_range(vp, filesize, start,
2523 			    end);
2524 			if (err)
2525 				goto out;
2526 		}
2527 		err = fuse_write_directbackend(vp, uio, cred, fufh,
2528 			filesize, ioflag, pages);
2529 	} else {
2530 		SDT_PROBE2(fusefs, , vnops, trace, 1,
2531 			"buffered write of vnode");
2532 		if (!fsess_opt_writeback(vnode_mount(vp)))
2533 			ioflag |= IO_SYNC;
2534 		err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag, pid);
2535 	}
2536 	fuse_internal_clear_suid_on_write(vp, cred, uio->uio_td);
2537 
2538 out:
2539 	if (closefufh)
2540 		fuse_filehandle_close(vp, fufh, curthread, cred);
2541 
2542 	return (err);
2543 }
2544 
2545 static daddr_t
2546 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2547 {
2548 	const int biosize = fuse_iosize(vp);
2549 
2550 	return (off / biosize);
2551 }
2552 
2553 static int
2554 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2555 {
2556 	off_t filesize;
2557 	int err;
2558 	const int biosize = fuse_iosize(vp);
2559 
2560 	err = fuse_vnode_size(vp, &filesize, NULL, NULL);
2561 	if (err) {
2562 		/* This will turn into a SIGBUS */
2563 		return (EIO);
2564 	} else if ((off_t)lbn * biosize >= filesize) {
2565 		*blksz = 0;
2566 	} else if ((off_t)(lbn + 1) * biosize > filesize) {
2567 		*blksz = filesize - (off_t)lbn *biosize;
2568 	} else {
2569 		*blksz = biosize;
2570 	}
2571 	return (0);
2572 }
2573 
2574 /*
2575     struct vnop_getpages_args {
2576 	struct vnode *a_vp;
2577 	vm_page_t *a_m;
2578 	int a_count;
2579 	int a_reqpage;
2580     };
2581 */
2582 static int
2583 fuse_vnop_getpages(struct vop_getpages_args *ap)
2584 {
2585 	struct vnode *vp = ap->a_vp;
2586 
2587 	if (!fsess_opt_mmap(vnode_mount(vp))) {
2588 		SDT_PROBE2(fusefs, , vnops, trace, 1,
2589 			"called on non-cacheable vnode??\n");
2590 		return (VM_PAGER_ERROR);
2591 	}
2592 
2593 	return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2594 	    ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2595 }
2596 
2597 static const char extattr_namespace_separator = '.';
2598 
2599 /*
2600     struct vop_getextattr_args {
2601 	struct vop_generic_args a_gen;
2602 	struct vnode *a_vp;
2603 	int a_attrnamespace;
2604 	const char *a_name;
2605 	struct uio *a_uio;
2606 	size_t *a_size;
2607 	struct ucred *a_cred;
2608 	struct thread *a_td;
2609     };
2610 */
2611 static int
2612 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2613 {
2614 	struct vnode *vp = ap->a_vp;
2615 	struct uio *uio = ap->a_uio;
2616 	struct fuse_dispatcher fdi;
2617 	struct fuse_getxattr_in *get_xattr_in;
2618 	struct fuse_getxattr_out *get_xattr_out;
2619 	struct mount *mp = vnode_mount(vp);
2620 	struct thread *td = ap->a_td;
2621 	struct ucred *cred = ap->a_cred;
2622 	char *prefix;
2623 	char *attr_str;
2624 	size_t len;
2625 	int err;
2626 
2627 	if (fuse_isdeadfs(vp))
2628 		return (ENXIO);
2629 
2630 	if (fsess_not_impl(mp, FUSE_GETXATTR))
2631 		return EOPNOTSUPP;
2632 
2633 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2634 	if (err)
2635 		return err;
2636 
2637 	/* Default to looking for user attributes. */
2638 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2639 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2640 	else
2641 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2642 
2643 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2644 	    strlen(ap->a_name) + 1;
2645 
2646 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2647 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2648 
2649 	get_xattr_in = fdi.indata;
2650 	/*
2651 	 * Check to see whether we're querying the available size or
2652 	 * issuing the actual request.  If we pass in 0, we get back struct
2653 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
2654 	 * that much data, without the struct fuse_getxattr_out header.
2655 	 */
2656 	if (uio == NULL)
2657 		get_xattr_in->size = 0;
2658 	else
2659 		get_xattr_in->size = uio->uio_resid;
2660 
2661 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2662 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2663 	    ap->a_name);
2664 
2665 	err = fdisp_wait_answ(&fdi);
2666 	if (err != 0) {
2667 		if (err == ENOSYS) {
2668 			fsess_set_notimpl(mp, FUSE_GETXATTR);
2669 			err = EOPNOTSUPP;
2670 		}
2671 		goto out;
2672 	}
2673 
2674 	get_xattr_out = fdi.answ;
2675 
2676 	if (ap->a_size != NULL)
2677 		*ap->a_size = get_xattr_out->size;
2678 
2679 	if (uio != NULL)
2680 		err = uiomove(fdi.answ, fdi.iosize, uio);
2681 
2682 out:
2683 	fdisp_destroy(&fdi);
2684 	return (err);
2685 }
2686 
2687 /*
2688     struct vop_setextattr_args {
2689 	struct vop_generic_args a_gen;
2690 	struct vnode *a_vp;
2691 	int a_attrnamespace;
2692 	const char *a_name;
2693 	struct uio *a_uio;
2694 	struct ucred *a_cred;
2695 	struct thread *a_td;
2696     };
2697 */
2698 static int
2699 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2700 {
2701 	struct vnode *vp = ap->a_vp;
2702 	struct uio *uio = ap->a_uio;
2703 	struct fuse_dispatcher fdi;
2704 	struct fuse_setxattr_in *set_xattr_in;
2705 	struct mount *mp = vnode_mount(vp);
2706 	struct thread *td = ap->a_td;
2707 	struct ucred *cred = ap->a_cred;
2708 	char *prefix;
2709 	size_t len;
2710 	char *attr_str;
2711 	int err;
2712 
2713 	if (fuse_isdeadfs(vp))
2714 		return (ENXIO);
2715 
2716 	if (fsess_not_impl(mp, FUSE_SETXATTR))
2717 		return EOPNOTSUPP;
2718 
2719 	if (vfs_isrdonly(mp))
2720 		return EROFS;
2721 
2722 	/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2723 	if (ap->a_uio == NULL) {
2724 		/*
2725 		 * If we got here as fallback from VOP_DELETEEXTATTR, then
2726 		 * return EOPNOTSUPP.
2727 		 */
2728 		if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2729 			return (EOPNOTSUPP);
2730 		else
2731 			return (EINVAL);
2732 	}
2733 
2734 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2735 		VWRITE);
2736 	if (err)
2737 		return err;
2738 
2739 	/* Default to looking for user attributes. */
2740 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2741 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2742 	else
2743 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2744 
2745 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2746 	    strlen(ap->a_name) + 1;
2747 
2748 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2749 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2750 
2751 	set_xattr_in = fdi.indata;
2752 	set_xattr_in->size = uio->uio_resid;
2753 
2754 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2755 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2756 	    ap->a_name);
2757 
2758 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2759 	    uio->uio_resid, uio);
2760 	if (err != 0) {
2761 		goto out;
2762 	}
2763 
2764 	err = fdisp_wait_answ(&fdi);
2765 
2766 	if (err == ENOSYS) {
2767 		fsess_set_notimpl(mp, FUSE_SETXATTR);
2768 		err = EOPNOTSUPP;
2769 	}
2770 	if (err == ERESTART) {
2771 		/* Can't restart after calling uiomove */
2772 		err = EINTR;
2773 	}
2774 
2775 out:
2776 	fdisp_destroy(&fdi);
2777 	return (err);
2778 }
2779 
2780 /*
2781  * The Linux / FUSE extended attribute list is simply a collection of
2782  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
2783  * byte length followed by a non-NUL terminated string.  So, this allows
2784  * conversion of the Linux / FUSE format to the FreeBSD format in place.
2785  * Linux attribute names are reported with the namespace as a prefix (e.g.
2786  * "user.attribute_name"), but in FreeBSD they are reported without the
2787  * namespace prefix (e.g. "attribute_name").  So, we're going from:
2788  *
2789  * user.attr_name1\0user.attr_name2\0
2790  *
2791  * to:
2792  *
2793  * <num>attr_name1<num>attr_name2
2794  *
2795  * Where "<num>" is a single byte number of characters in the attribute name.
2796  *
2797  * Args:
2798  * prefix - exattr namespace prefix string
2799  * list, list_len - input list with namespace prefixes
2800  * bsd_list, bsd_list_len - output list compatible with bsd vfs
2801  */
2802 static int
2803 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2804     char *bsd_list, int *bsd_list_len)
2805 {
2806 	int len, pos, dist_to_next, prefix_len;
2807 
2808 	pos = 0;
2809 	*bsd_list_len = 0;
2810 	prefix_len = strlen(prefix);
2811 
2812 	while (pos < list_len && list[pos] != '\0') {
2813 		dist_to_next = strlen(&list[pos]) + 1;
2814 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2815 		    list[pos + prefix_len] == extattr_namespace_separator) {
2816 			len = dist_to_next -
2817 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2818 			if (len >= EXTATTR_MAXNAMELEN)
2819 				return (ENAMETOOLONG);
2820 
2821 			bsd_list[*bsd_list_len] = len;
2822 			memcpy(&bsd_list[*bsd_list_len + 1],
2823 			    &list[pos + prefix_len +
2824 			    sizeof(extattr_namespace_separator)], len);
2825 
2826 			*bsd_list_len += len + 1;
2827 		}
2828 
2829 		pos += dist_to_next;
2830 	}
2831 
2832 	return (0);
2833 }
2834 
2835 /*
2836  * List extended attributes
2837  *
2838  * The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
2839  * has a number of differences compared to its FreeBSD equivalent,
2840  * extattr_list_file:
2841  *
2842  * - FUSE_LISTXATTR returns all extended attributes across all namespaces,
2843  *   whereas listxattr(2) only returns attributes for a single namespace
2844  * - FUSE_LISTXATTR prepends each attribute name with "namespace."
2845  * - If the provided buffer is not large enough to hold the result,
2846  *   FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
2847  *   return as many results as will fit.
2848  */
2849 /*
2850     struct vop_listextattr_args {
2851 	struct vop_generic_args a_gen;
2852 	struct vnode *a_vp;
2853 	int a_attrnamespace;
2854 	struct uio *a_uio;
2855 	size_t *a_size;
2856 	struct ucred *a_cred;
2857 	struct thread *a_td;
2858     };
2859 */
2860 static int
2861 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2862 {
2863 	struct vnode *vp = ap->a_vp;
2864 	struct uio *uio = ap->a_uio;
2865 	struct fuse_dispatcher fdi;
2866 	struct fuse_listxattr_in *list_xattr_in;
2867 	struct fuse_listxattr_out *list_xattr_out;
2868 	struct mount *mp = vnode_mount(vp);
2869 	struct thread *td = ap->a_td;
2870 	struct ucred *cred = ap->a_cred;
2871 	char *prefix;
2872 	char *bsd_list = NULL;
2873 	char *linux_list;
2874 	int bsd_list_len;
2875 	int linux_list_len;
2876 	int err;
2877 
2878 	if (fuse_isdeadfs(vp))
2879 		return (ENXIO);
2880 
2881 	if (fsess_not_impl(mp, FUSE_LISTXATTR))
2882 		return EOPNOTSUPP;
2883 
2884 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2885 	if (err)
2886 		return err;
2887 
2888 	/*
2889 	 * Add space for a NUL and the period separator if enabled.
2890 	 * Default to looking for user attributes.
2891 	 */
2892 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2893 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2894 	else
2895 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2896 
2897 	fdisp_init(&fdi, sizeof(*list_xattr_in));
2898 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2899 
2900 	/*
2901 	 * Retrieve Linux / FUSE compatible list size.
2902 	 */
2903 	list_xattr_in = fdi.indata;
2904 	list_xattr_in->size = 0;
2905 
2906 	err = fdisp_wait_answ(&fdi);
2907 	if (err != 0) {
2908 		if (err == ENOSYS) {
2909 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
2910 			err = EOPNOTSUPP;
2911 		}
2912 		goto out;
2913 	}
2914 
2915 	list_xattr_out = fdi.answ;
2916 	linux_list_len = list_xattr_out->size;
2917 	if (linux_list_len == 0) {
2918 		if (ap->a_size != NULL)
2919 			*ap->a_size = linux_list_len;
2920 		goto out;
2921 	}
2922 
2923 	/*
2924 	 * Retrieve Linux / FUSE compatible list values.
2925 	 */
2926 	fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2927 	list_xattr_in = fdi.indata;
2928 	list_xattr_in->size = linux_list_len;
2929 
2930 	err = fdisp_wait_answ(&fdi);
2931 	if (err == ERANGE) {
2932 		/*
2933 		 * Race detected.  The attribute list must've grown since the
2934 		 * first FUSE_LISTXATTR call.  Start over.  Go all the way back
2935 		 * to userland so we can process signals, if necessary, before
2936 		 * restarting.
2937 		 */
2938 		err = ERESTART;
2939 		goto out;
2940 	} else if (err != 0)
2941 		goto out;
2942 
2943 	linux_list = fdi.answ;
2944 	/* FUSE doesn't allow the server to return more data than requested */
2945 	if (fdi.iosize > linux_list_len) {
2946 		struct fuse_data *data = fuse_get_mpdata(mp);
2947 
2948 		fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
2949 			"server returned "
2950 			"more extended attribute data than requested; "
2951 			"should've returned ERANGE instead.");
2952 	} else {
2953 		/* But returning less data is fine */
2954 		linux_list_len = fdi.iosize;
2955 	}
2956 
2957 	/*
2958 	 * Retrieve the BSD compatible list values.
2959 	 * The Linux / FUSE attribute list format isn't the same
2960 	 * as FreeBSD's format. So we need to transform it into
2961 	 * FreeBSD's format before giving it to the user.
2962 	 */
2963 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2964 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2965 	    bsd_list, &bsd_list_len);
2966 	if (err != 0)
2967 		goto out;
2968 
2969 	if (ap->a_size != NULL)
2970 		*ap->a_size = bsd_list_len;
2971 
2972 	if (uio != NULL)
2973 		err = uiomove(bsd_list, bsd_list_len, uio);
2974 
2975 out:
2976 	free(bsd_list, M_TEMP);
2977 	fdisp_destroy(&fdi);
2978 	return (err);
2979 }
2980 
2981 /*
2982     struct vop_deallocate_args {
2983 	struct vop_generic_args a_gen;
2984 	struct vnode *a_vp;
2985 	off_t *a_offset;
2986 	off_t *a_len;
2987 	int a_flags;
2988 	int a_ioflag;
2989         struct ucred *a_cred;
2990     };
2991 */
2992 static int
2993 fuse_vnop_deallocate(struct vop_deallocate_args *ap)
2994 {
2995 	struct vnode *vp = ap->a_vp;
2996 	struct mount *mp = vnode_mount(vp);
2997 	struct fuse_filehandle *fufh;
2998 	struct fuse_dispatcher fdi;
2999 	struct fuse_fallocate_in *ffi;
3000 	struct ucred *cred = ap->a_cred;
3001 	pid_t pid = curthread->td_proc->p_pid;
3002 	off_t *len = ap->a_len;
3003 	off_t *offset = ap->a_offset;
3004 	int ioflag = ap->a_ioflag;
3005 	off_t filesize;
3006 	int err;
3007 	bool closefufh = false;
3008 
3009 	if (fuse_isdeadfs(vp))
3010 		return (ENXIO);
3011 
3012 	if (vfs_isrdonly(mp))
3013 		return (EROFS);
3014 
3015 	if (fsess_not_impl(mp, FUSE_FALLOCATE))
3016 		goto fallback;
3017 
3018 	err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
3019 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
3020 		/*
3021 		 * nfsd will do I/O without first doing VOP_OPEN.  We
3022 		 * must implicitly open the file here
3023 		 */
3024 		err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
3025 		closefufh = true;
3026 	}
3027 	if (err)
3028 		return (err);
3029 
3030 	fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
3031 
3032 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
3033 	if (err)
3034 		goto out;
3035 	fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
3036 
3037 	fdisp_init(&fdi, sizeof(*ffi));
3038 	fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
3039 	ffi = fdi.indata;
3040 	ffi->fh = fufh->fh_id;
3041 	ffi->offset = *offset;
3042 	ffi->length = *len;
3043 	/*
3044 	 * FreeBSD's fspacectl is equivalent to Linux's fallocate with
3045 	 * mode == FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE
3046 	 */
3047 	ffi->mode = FUSE_FALLOC_FL_PUNCH_HOLE | FUSE_FALLOC_FL_KEEP_SIZE;
3048 	err = fdisp_wait_answ(&fdi);
3049 
3050 	if (err == ENOSYS) {
3051 		fdisp_destroy(&fdi);
3052 		fsess_set_notimpl(mp, FUSE_FALLOCATE);
3053 		goto fallback;
3054 	} else if (err == EOPNOTSUPP) {
3055 		/*
3056 		 * The file system server does not support FUSE_FALLOCATE with
3057 		 * the supplied mode for this particular file.
3058 		 */
3059 		fdisp_destroy(&fdi);
3060 		goto fallback;
3061 	} else if (!err) {
3062 		/*
3063 		 * Clip the returned offset to EoF.  Do it here rather than
3064 		 * before FUSE_FALLOCATE just in case the kernel's cached file
3065 		 * size is out of date.  Unfortunately, FUSE does not return
3066 		 * any information about filesize from that operation.
3067 		 */
3068 		*offset = MIN(*offset + *len, filesize);
3069 		*len = 0;
3070 		fuse_vnode_undirty_cached_timestamps(vp, false);
3071 		fuse_internal_clear_suid_on_write(vp, cred, curthread);
3072 
3073 		if (ioflag & IO_SYNC)
3074 			err = fuse_internal_fsync(vp, curthread, MNT_WAIT,
3075 			    false);
3076 	}
3077 
3078 	fdisp_destroy(&fdi);
3079 out:
3080 	if (closefufh)
3081 		fuse_filehandle_close(vp, fufh, curthread, cred);
3082 
3083 	return (err);
3084 
3085 fallback:
3086 	if (closefufh)
3087 		fuse_filehandle_close(vp, fufh, curthread, cred);
3088 
3089 	return (vop_stddeallocate(ap));
3090 }
3091 
3092 /*
3093     struct vop_deleteextattr_args {
3094 	struct vop_generic_args a_gen;
3095 	struct vnode *a_vp;
3096 	int a_attrnamespace;
3097 	const char *a_name;
3098 	struct ucred *a_cred;
3099 	struct thread *a_td;
3100     };
3101 */
3102 static int
3103 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
3104 {
3105 	struct vnode *vp = ap->a_vp;
3106 	struct fuse_dispatcher fdi;
3107 	struct mount *mp = vnode_mount(vp);
3108 	struct thread *td = ap->a_td;
3109 	struct ucred *cred = ap->a_cred;
3110 	char *prefix;
3111 	size_t len;
3112 	char *attr_str;
3113 	int err;
3114 
3115 	if (fuse_isdeadfs(vp))
3116 		return (ENXIO);
3117 
3118 	if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
3119 		return EOPNOTSUPP;
3120 
3121 	if (vfs_isrdonly(mp))
3122 		return EROFS;
3123 
3124 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
3125 		VWRITE);
3126 	if (err)
3127 		return err;
3128 
3129 	/* Default to looking for user attributes. */
3130 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
3131 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
3132 	else
3133 		prefix = EXTATTR_NAMESPACE_USER_STRING;
3134 
3135 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
3136 	    strlen(ap->a_name) + 1;
3137 
3138 	fdisp_init(&fdi, len);
3139 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
3140 
3141 	attr_str = fdi.indata;
3142 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
3143 	    ap->a_name);
3144 
3145 	err = fdisp_wait_answ(&fdi);
3146 	if (err == ENOSYS) {
3147 		fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
3148 		err = EOPNOTSUPP;
3149 	}
3150 
3151 	fdisp_destroy(&fdi);
3152 	return (err);
3153 }
3154 
3155 /*
3156     struct vnop_print_args {
3157 	struct vnode *a_vp;
3158     };
3159 */
3160 static int
3161 fuse_vnop_print(struct vop_print_args *ap)
3162 {
3163 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
3164 
3165 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
3166 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
3167 	    (uintmax_t)fvdat->nlookup,
3168 	    fvdat->flag);
3169 
3170 	return 0;
3171 }
3172 
3173 /*
3174  * Get an NFS filehandle for a FUSE file.
3175  *
3176  * This will only work for FUSE file systems that guarantee the uniqueness of
3177  * nodeid:generation, which most don't.
3178  */
3179 /*
3180 vop_vptofh {
3181 	IN struct vnode *a_vp;
3182 	IN struct fid *a_fhp;
3183 };
3184 */
3185 static int
3186 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
3187 {
3188 	struct vnode *vp = ap->a_vp;
3189 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
3190 	struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
3191 	_Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
3192 		"FUSE fid type is too big");
3193 	struct mount *mp = vnode_mount(vp);
3194 	struct fuse_data *data = fuse_get_mpdata(mp);
3195 	struct vattr va;
3196 	int err;
3197 
3198 	if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
3199 		/* NFS requires lookups for "." and ".." */
3200 		SDT_PROBE2(fusefs, , vnops, trace, 1,
3201 			"VOP_VPTOFH without FUSE_EXPORT_SUPPORT");
3202 		return EOPNOTSUPP;
3203 	}
3204 	if ((mp->mnt_flag & MNT_EXPORTED) &&
3205 		!(data->dataflags & FSESS_NO_OPENDIR_SUPPORT))
3206 	{
3207 		/*
3208 		 * NFS is stateless, so nfsd must reopen a directory on every
3209 		 * call to VOP_READDIR, passing in the d_off field from the
3210 		 * final dirent of the previous invocation.  But without
3211 		 * FUSE_NO_OPENDIR_SUPPORT, the FUSE protocol does not
3212 		 * guarantee that d_off will be valid after a directory is
3213 		 * closed and reopened.  So prohibit exporting FUSE file
3214 		 * systems that don't set that flag.
3215 		 *
3216 		 * But userspace NFS servers don't have this problem.
3217                  */
3218 		SDT_PROBE2(fusefs, , vnops, trace, 1,
3219 			"VOP_VPTOFH without FUSE_NO_OPENDIR_SUPPORT");
3220 		return EOPNOTSUPP;
3221 	}
3222 
3223 	err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
3224 	if (err)
3225 		return err;
3226 
3227 	/*ip = VTOI(ap->a_vp);*/
3228 	/*ufhp = (struct ufid *)ap->a_fhp;*/
3229 	fhp->len = sizeof(struct fuse_fid);
3230 	fhp->nid = fvdat->nid;
3231 	if (fvdat->generation <= UINT32_MAX)
3232 		fhp->gen = fvdat->generation;
3233 	else
3234 		return EOVERFLOW;
3235 	return (0);
3236 }
3237