xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include <sys/param.h>
67 #include <sys/module.h>
68 #include <sys/systm.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
71 #include <sys/conf.h>
72 #include <sys/filio.h>
73 #include <sys/uio.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
76 #include <sys/limits.h>
77 #include <sys/lock.h>
78 #include <sys/rwlock.h>
79 #include <sys/sx.h>
80 #include <sys/proc.h>
81 #include <sys/mount.h>
82 #include <sys/vnode.h>
83 #include <sys/namei.h>
84 #include <sys/extattr.h>
85 #include <sys/stat.h>
86 #include <sys/unistd.h>
87 #include <sys/filedesc.h>
88 #include <sys/file.h>
89 #include <sys/fcntl.h>
90 #include <sys/dirent.h>
91 #include <sys/bio.h>
92 #include <sys/buf.h>
93 #include <sys/sysctl.h>
94 #include <sys/vmmeter.h>
95 
96 #include <vm/vm.h>
97 #include <vm/vm_extern.h>
98 #include <vm/pmap.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_param.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_pager.h>
104 #include <vm/vnode_pager.h>
105 #include <vm/vm_object.h>
106 
107 #include "fuse.h"
108 #include "fuse_file.h"
109 #include "fuse_internal.h"
110 #include "fuse_ipc.h"
111 #include "fuse_node.h"
112 #include "fuse_io.h"
113 
114 #include <sys/priv.h>
115 
116 /* Maximum number of hardlinks to a single FUSE file */
117 #define FUSE_LINK_MAX                      UINT32_MAX
118 
119 SDT_PROVIDER_DECLARE(fusefs);
120 /*
121  * Fuse trace probe:
122  * arg0: verbosity.  Higher numbers give more verbose messages
123  * arg1: Textual message
124  */
125 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
126 
127 /* vnode ops */
128 static vop_access_t fuse_vnop_access;
129 static vop_advlock_t fuse_vnop_advlock;
130 static vop_bmap_t fuse_vnop_bmap;
131 static vop_close_t fuse_fifo_close;
132 static vop_close_t fuse_vnop_close;
133 static vop_copy_file_range_t fuse_vnop_copy_file_range;
134 static vop_create_t fuse_vnop_create;
135 static vop_deleteextattr_t fuse_vnop_deleteextattr;
136 static vop_fdatasync_t fuse_vnop_fdatasync;
137 static vop_fsync_t fuse_vnop_fsync;
138 static vop_getattr_t fuse_vnop_getattr;
139 static vop_getextattr_t fuse_vnop_getextattr;
140 static vop_inactive_t fuse_vnop_inactive;
141 static vop_ioctl_t fuse_vnop_ioctl;
142 static vop_link_t fuse_vnop_link;
143 static vop_listextattr_t fuse_vnop_listextattr;
144 static vop_lookup_t fuse_vnop_lookup;
145 static vop_mkdir_t fuse_vnop_mkdir;
146 static vop_mknod_t fuse_vnop_mknod;
147 static vop_open_t fuse_vnop_open;
148 static vop_pathconf_t fuse_vnop_pathconf;
149 static vop_read_t fuse_vnop_read;
150 static vop_readdir_t fuse_vnop_readdir;
151 static vop_readlink_t fuse_vnop_readlink;
152 static vop_reclaim_t fuse_vnop_reclaim;
153 static vop_remove_t fuse_vnop_remove;
154 static vop_rename_t fuse_vnop_rename;
155 static vop_rmdir_t fuse_vnop_rmdir;
156 static vop_setattr_t fuse_vnop_setattr;
157 static vop_setextattr_t fuse_vnop_setextattr;
158 static vop_strategy_t fuse_vnop_strategy;
159 static vop_symlink_t fuse_vnop_symlink;
160 static vop_write_t fuse_vnop_write;
161 static vop_getpages_t fuse_vnop_getpages;
162 static vop_print_t fuse_vnop_print;
163 static vop_vptofh_t fuse_vnop_vptofh;
164 
165 struct vop_vector fuse_fifoops = {
166 	.vop_default =		&fifo_specops,
167 	.vop_access =		fuse_vnop_access,
168 	.vop_close =		fuse_fifo_close,
169 	.vop_fsync =		fuse_vnop_fsync,
170 	.vop_getattr =		fuse_vnop_getattr,
171 	.vop_inactive =		fuse_vnop_inactive,
172 	.vop_pathconf =		fuse_vnop_pathconf,
173 	.vop_print =		fuse_vnop_print,
174 	.vop_read =		VOP_PANIC,
175 	.vop_reclaim =		fuse_vnop_reclaim,
176 	.vop_setattr =		fuse_vnop_setattr,
177 	.vop_write =		VOP_PANIC,
178 	.vop_vptofh =		fuse_vnop_vptofh,
179 };
180 VFS_VOP_VECTOR_REGISTER(fuse_fifoops);
181 
182 struct vop_vector fuse_vnops = {
183 	.vop_allocate =	VOP_EINVAL,
184 	.vop_default = &default_vnodeops,
185 	.vop_access = fuse_vnop_access,
186 	.vop_advlock = fuse_vnop_advlock,
187 	.vop_bmap = fuse_vnop_bmap,
188 	.vop_close = fuse_vnop_close,
189 	.vop_copy_file_range = fuse_vnop_copy_file_range,
190 	.vop_create = fuse_vnop_create,
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 uma_zone_t fuse_pbuf_zone;
230 
231 /* Check permission for extattr operations, much like extattr_check_cred */
232 static int
233 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
234 	struct thread *td, accmode_t accmode)
235 {
236 	struct mount *mp = vnode_mount(vp);
237 	struct fuse_data *data = fuse_get_mpdata(mp);
238 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
239 
240 	/*
241 	 * Kernel-invoked always succeeds.
242 	 */
243 	if (cred == NOCRED)
244 		return (0);
245 
246 	/*
247 	 * Do not allow privileged processes in jail to directly manipulate
248 	 * system attributes.
249 	 */
250 	switch (ns) {
251 	case EXTATTR_NAMESPACE_SYSTEM:
252 		if (default_permissions) {
253 			return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
254 		}
255 		return (0);
256 	case EXTATTR_NAMESPACE_USER:
257 		if (default_permissions) {
258 			return (fuse_internal_access(vp, accmode, td, cred));
259 		}
260 		return (0);
261 	default:
262 		return (EPERM);
263 	}
264 }
265 
266 /* Get a filehandle for a directory */
267 static int
268 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
269 	struct ucred *cred, pid_t pid)
270 {
271 	if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
272 		return 0;
273 	return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
274 }
275 
276 /* Send FUSE_FLUSH for this vnode */
277 static int
278 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
279 {
280 	struct fuse_flush_in *ffi;
281 	struct fuse_filehandle *fufh;
282 	struct fuse_dispatcher fdi;
283 	struct thread *td = curthread;
284 	struct mount *mp = vnode_mount(vp);
285 	int err;
286 
287 	if (fsess_not_impl(vnode_mount(vp), FUSE_FLUSH))
288 		return 0;
289 
290 	err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
291 	if (err)
292 		return err;
293 
294 	fdisp_init(&fdi, sizeof(*ffi));
295 	fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
296 	ffi = fdi.indata;
297 	ffi->fh = fufh->fh_id;
298 	/*
299 	 * If the file has a POSIX lock then we're supposed to set lock_owner.
300 	 * If not, then lock_owner is undefined.  So we may as well always set
301 	 * it.
302 	 */
303 	ffi->lock_owner = td->td_proc->p_pid;
304 
305 	err = fdisp_wait_answ(&fdi);
306 	if (err == ENOSYS) {
307 		fsess_set_notimpl(mp, FUSE_FLUSH);
308 		err = 0;
309 	}
310 	fdisp_destroy(&fdi);
311 	return err;
312 }
313 
314 /* Close wrapper for fifos.  */
315 static int
316 fuse_fifo_close(struct vop_close_args *ap)
317 {
318 	return (fifo_specops.vop_close(ap));
319 }
320 
321 /* Send FUSE_LSEEK for this node */
322 static int
323 fuse_vnop_do_lseek(struct vnode *vp, struct thread *td, struct ucred *cred,
324 	pid_t pid, off_t *offp, int whence)
325 {
326 	struct fuse_dispatcher fdi;
327 	struct fuse_filehandle *fufh;
328 	struct fuse_lseek_in *flsi;
329 	struct fuse_lseek_out *flso;
330 	struct mount *mp = vnode_mount(vp);
331 	int err;
332 
333 	ASSERT_VOP_LOCKED(vp, __func__);
334 
335 	err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
336 	if (err)
337 		return (err);
338 	fdisp_init(&fdi, sizeof(*flsi));
339 	fdisp_make_vp(&fdi, FUSE_LSEEK, vp, td, cred);
340 	flsi = fdi.indata;
341 	flsi->fh = fufh->fh_id;
342 	flsi->offset = *offp;
343 	flsi->whence = whence;
344 	err = fdisp_wait_answ(&fdi);
345 	if (err == ENOSYS) {
346 		fsess_set_notimpl(mp, FUSE_LSEEK);
347 	} else if (err == 0) {
348 		fsess_set_impl(mp, FUSE_LSEEK);
349 		flso = fdi.answ;
350 		*offp = flso->offset;
351 	}
352 	fdisp_destroy(&fdi);
353 
354 	return (err);
355 }
356 
357 /*
358     struct vnop_access_args {
359 	struct vnode *a_vp;
360 #if VOP_ACCESS_TAKES_ACCMODE_T
361 	accmode_t a_accmode;
362 #else
363 	int a_mode;
364 #endif
365 	struct ucred *a_cred;
366 	struct thread *a_td;
367     };
368 */
369 static int
370 fuse_vnop_access(struct vop_access_args *ap)
371 {
372 	struct vnode *vp = ap->a_vp;
373 	int accmode = ap->a_accmode;
374 	struct ucred *cred = ap->a_cred;
375 
376 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
377 
378 	int err;
379 
380 	if (fuse_isdeadfs(vp)) {
381 		if (vnode_isvroot(vp)) {
382 			return 0;
383 		}
384 		return ENXIO;
385 	}
386 	if (!(data->dataflags & FSESS_INITED)) {
387 		if (vnode_isvroot(vp)) {
388 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
389 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
390 				return 0;
391 			}
392 		}
393 		return EBADF;
394 	}
395 	if (vnode_islnk(vp)) {
396 		return 0;
397 	}
398 
399 	err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
400 	return err;
401 }
402 
403 /*
404  * struct vop_advlock_args {
405  *	struct vop_generic_args a_gen;
406  *	struct vnode *a_vp;
407  *	void *a_id;
408  *	int a_op;
409  *	struct flock *a_fl;
410  *	int a_flags;
411  * }
412  */
413 static int
414 fuse_vnop_advlock(struct vop_advlock_args *ap)
415 {
416 	struct vnode *vp = ap->a_vp;
417 	struct flock *fl = ap->a_fl;
418 	struct thread *td = curthread;
419 	struct ucred *cred = td->td_ucred;
420 	pid_t pid = td->td_proc->p_pid;
421 	struct fuse_filehandle *fufh;
422 	struct fuse_dispatcher fdi;
423 	struct fuse_lk_in *fli;
424 	struct fuse_lk_out *flo;
425 	enum fuse_opcode op;
426 	int dataflags, err;
427 	int flags = ap->a_flags;
428 
429 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
430 
431 	if (fuse_isdeadfs(vp)) {
432 		return ENXIO;
433 	}
434 
435 	switch(ap->a_op) {
436 	case F_GETLK:
437 		op = FUSE_GETLK;
438 		break;
439 	case F_SETLK:
440 		if (flags & F_WAIT)
441 			op = FUSE_SETLKW;
442 		else
443 			op = FUSE_SETLK;
444 		break;
445 	case F_UNLCK:
446 		op = FUSE_SETLK;
447 		break;
448 	default:
449 		return EINVAL;
450 	}
451 
452 	if (!(dataflags & FSESS_POSIX_LOCKS))
453 		return vop_stdadvlock(ap);
454 	/* FUSE doesn't properly support flock until protocol 7.17 */
455 	if (flags & F_FLOCK)
456 		return vop_stdadvlock(ap);
457 
458 	vn_lock(vp, LK_SHARED | LK_RETRY);
459 
460 	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
461 	if (err)
462 		goto out;
463 
464 	fdisp_init(&fdi, sizeof(*fli));
465 
466 	fdisp_make_vp(&fdi, op, vp, td, cred);
467 	fli = fdi.indata;
468 	fli->fh = fufh->fh_id;
469 	fli->owner = td->td_proc->p_pid;
470 	fli->lk.start = fl->l_start;
471 	if (fl->l_len != 0)
472 		fli->lk.end = fl->l_start + fl->l_len - 1;
473 	else
474 		fli->lk.end = INT64_MAX;
475 	fli->lk.type = fl->l_type;
476 	fli->lk.pid = td->td_proc->p_pid;
477 
478 	err = fdisp_wait_answ(&fdi);
479 	fdisp_destroy(&fdi);
480 
481 	if (err == 0 && op == FUSE_GETLK) {
482 		flo = fdi.answ;
483 		fl->l_type = flo->lk.type;
484 		fl->l_pid = flo->lk.pid;
485 		if (flo->lk.type != F_UNLCK) {
486 			fl->l_start = flo->lk.start;
487 			if (flo->lk.end == INT64_MAX)
488 				fl->l_len = 0;
489 			else
490 				fl->l_len = flo->lk.end - flo->lk.start + 1;
491 			fl->l_start = flo->lk.start;
492 		}
493 	}
494 
495 out:
496 	VOP_UNLOCK(vp);
497 	return err;
498 }
499 
500 /* {
501 	struct vnode *a_vp;
502 	daddr_t a_bn;
503 	struct bufobj **a_bop;
504 	daddr_t *a_bnp;
505 	int *a_runp;
506 	int *a_runb;
507 } */
508 static int
509 fuse_vnop_bmap(struct vop_bmap_args *ap)
510 {
511 	struct vnode *vp = ap->a_vp;
512 	struct bufobj **bo = ap->a_bop;
513 	struct thread *td = curthread;
514 	struct mount *mp;
515 	struct fuse_dispatcher fdi;
516 	struct fuse_bmap_in *fbi;
517 	struct fuse_bmap_out *fbo;
518 	struct fuse_data *data;
519 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
520 	uint64_t biosize;
521 	off_t fsize;
522 	daddr_t lbn = ap->a_bn;
523 	daddr_t *pbn = ap->a_bnp;
524 	int *runp = ap->a_runp;
525 	int *runb = ap->a_runb;
526 	int error = 0;
527 	int maxrun;
528 
529 	if (fuse_isdeadfs(vp)) {
530 		return ENXIO;
531 	}
532 
533 	mp = vnode_mount(vp);
534 	data = fuse_get_mpdata(mp);
535 	biosize = fuse_iosize(vp);
536 	maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
537 		data->max_readahead_blocks);
538 
539 	if (bo != NULL)
540 		*bo = &vp->v_bufobj;
541 
542 	/*
543 	 * The FUSE_BMAP operation does not include the runp and runb
544 	 * variables, so we must guess.  Report nonzero contiguous runs so
545 	 * cluster_read will combine adjacent reads.  It's worthwhile to reduce
546 	 * upcalls even if we don't know the true physical layout of the file.
547 	 *
548 	 * FUSE file systems may opt out of read clustering in two ways:
549 	 * * mounting with -onoclusterr
550 	 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
551 	 */
552 	if (runb != NULL)
553 		*runb = MIN(lbn, maxrun);
554 	if (runp != NULL && maxrun == 0)
555 		*runp = 0;
556 	else if (runp != NULL) {
557 		/*
558 		 * If the file's size is cached, use that value to calculate
559 		 * runp, even if the cache is expired.  runp is only advisory,
560 		 * and the risk of getting it wrong is not worth the cost of
561 		 * another upcall.
562 		 */
563 		if (fvdat->cached_attrs.va_size != VNOVAL)
564 			fsize = fvdat->cached_attrs.va_size;
565 		else
566 			error = fuse_vnode_size(vp, &fsize, td->td_ucred, td);
567 		if (error == 0)
568 			*runp = MIN(MAX(0, fsize / (off_t)biosize - lbn - 1),
569 				    maxrun);
570 		else
571 			*runp = 0;
572 	}
573 
574 	if (fsess_maybe_impl(mp, FUSE_BMAP)) {
575 		fdisp_init(&fdi, sizeof(*fbi));
576 		fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
577 		fbi = fdi.indata;
578 		fbi->block = lbn;
579 		fbi->blocksize = biosize;
580 		error = fdisp_wait_answ(&fdi);
581 		if (error == ENOSYS) {
582 			fdisp_destroy(&fdi);
583 			fsess_set_notimpl(mp, FUSE_BMAP);
584 			error = 0;
585 		} else {
586 			fbo = fdi.answ;
587 			if (error == 0 && pbn != NULL)
588 				*pbn = fbo->block;
589 			fdisp_destroy(&fdi);
590 			return error;
591 		}
592 	}
593 
594 	/* If the daemon doesn't support BMAP, make up a sensible default */
595 	if (pbn != NULL)
596 		*pbn = lbn * btodb(biosize);
597 	return (error);
598 }
599 
600 /*
601     struct vop_close_args {
602 	struct vnode *a_vp;
603 	int  a_fflag;
604 	struct ucred *a_cred;
605 	struct thread *a_td;
606     };
607 */
608 static int
609 fuse_vnop_close(struct vop_close_args *ap)
610 {
611 	struct vnode *vp = ap->a_vp;
612 	struct ucred *cred = ap->a_cred;
613 	int fflag = ap->a_fflag;
614 	struct thread *td = ap->a_td;
615 	pid_t pid = td->td_proc->p_pid;
616 	int err = 0;
617 
618 	if (fuse_isdeadfs(vp))
619 		return 0;
620 	if (vnode_isdir(vp))
621 		return 0;
622 	if (fflag & IO_NDELAY)
623 		return 0;
624 
625 	err = fuse_flush(vp, cred, pid, fflag);
626 	/* TODO: close the file handle, if we're sure it's no longer used */
627 	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
628 		fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
629 	}
630 	return err;
631 }
632 
633 /*
634    struct vop_copy_file_range_args {
635 	struct vop_generic_args a_gen;
636 	struct vnode *a_invp;
637 	off_t *a_inoffp;
638 	struct vnode *a_outvp;
639 	off_t *a_outoffp;
640 	size_t *a_lenp;
641 	unsigned int a_flags;
642 	struct ucred *a_incred;
643 	struct ucred *a_outcred;
644 	struct thread *a_fsizetd;
645 }
646  */
647 static int
648 fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
649 {
650 	struct vnode *invp = ap->a_invp;
651 	struct vnode *outvp = ap->a_outvp;
652 	struct mount *mp = vnode_mount(invp);
653 	struct fuse_dispatcher fdi;
654 	struct fuse_filehandle *infufh, *outfufh;
655 	struct fuse_copy_file_range_in *fcfri;
656 	struct ucred *incred = ap->a_incred;
657 	struct ucred *outcred = ap->a_outcred;
658 	struct fuse_write_out *fwo;
659 	struct thread *td;
660 	struct uio io;
661 	pid_t pid;
662 	int err;
663 
664 	if (mp != vnode_mount(outvp))
665 		goto fallback;
666 
667 	if (incred->cr_uid != outcred->cr_uid)
668 		goto fallback;
669 
670 	if (incred->cr_groups[0] != outcred->cr_groups[0])
671 		goto fallback;
672 
673 	if (fsess_not_impl(mp, FUSE_COPY_FILE_RANGE))
674 		goto fallback;
675 
676 	if (ap->a_fsizetd == NULL)
677 		td = curthread;
678 	else
679 		td = ap->a_fsizetd;
680 	pid = td->td_proc->p_pid;
681 
682 	/* Lock both vnodes, avoiding risk of deadlock. */
683 	do {
684 		err = vn_lock(outvp, LK_EXCLUSIVE);
685 		if (invp == outvp)
686 			break;
687 		if (err == 0) {
688 			err = vn_lock(invp, LK_SHARED | LK_NOWAIT);
689 			if (err == 0)
690 				break;
691 			VOP_UNLOCK(outvp);
692 			err = vn_lock(invp, LK_SHARED);
693 			if (err == 0)
694 				VOP_UNLOCK(invp);
695 		}
696 	} while (err == 0);
697 	if (err != 0)
698 		return (err);
699 
700 	err = fuse_filehandle_getrw(invp, FREAD, &infufh, incred, pid);
701 	if (err)
702 		goto unlock;
703 
704 	err = fuse_filehandle_getrw(outvp, FWRITE, &outfufh, outcred, pid);
705 	if (err)
706 		goto unlock;
707 
708 	if (ap->a_fsizetd) {
709 		io.uio_offset = *ap->a_outoffp;
710 		io.uio_resid = *ap->a_lenp;
711 		err = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
712 		if (err)
713 			goto unlock;
714 	}
715 
716 	fdisp_init(&fdi, sizeof(*fcfri));
717 	fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred);
718 	fcfri = fdi.indata;
719 	fcfri->fh_in = infufh->fh_id;
720 	fcfri->off_in = *ap->a_inoffp;
721 	fcfri->nodeid_out = VTOI(outvp);
722 	fcfri->fh_out = outfufh->fh_id;
723 	fcfri->off_out = *ap->a_outoffp;
724 	fcfri->len = *ap->a_lenp;
725 	fcfri->flags = 0;
726 
727 	err = fdisp_wait_answ(&fdi);
728 	if (err == 0) {
729 		fwo = fdi.answ;
730 		*ap->a_lenp = fwo->size;
731 		*ap->a_inoffp += fwo->size;
732 		*ap->a_outoffp += fwo->size;
733 		fuse_internal_clear_suid_on_write(outvp, outcred, td);
734 	}
735 	fdisp_destroy(&fdi);
736 
737 unlock:
738 	if (invp != outvp)
739 		VOP_UNLOCK(invp);
740 	VOP_UNLOCK(outvp);
741 
742 	if (err == ENOSYS) {
743 		fsess_set_notimpl(mp, FUSE_COPY_FILE_RANGE);
744 fallback:
745 		err = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
746 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
747 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
748 	}
749 
750 	return (err);
751 }
752 
753 static void
754 fdisp_make_mknod_for_fallback(
755 	struct fuse_dispatcher *fdip,
756 	struct componentname *cnp,
757 	struct vnode *dvp,
758 	uint64_t parentnid,
759 	struct thread *td,
760 	struct ucred *cred,
761 	mode_t mode,
762 	enum fuse_opcode *op)
763 {
764 	struct fuse_mknod_in *fmni;
765 
766 	fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
767 	*op = FUSE_MKNOD;
768 	fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
769 	fmni = fdip->indata;
770 	fmni->mode = mode;
771 	fmni->rdev = 0;
772 	memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
773 	    cnp->cn_namelen);
774 	((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
775 }
776 /*
777     struct vnop_create_args {
778 	struct vnode *a_dvp;
779 	struct vnode **a_vpp;
780 	struct componentname *a_cnp;
781 	struct vattr *a_vap;
782     };
783 */
784 static int
785 fuse_vnop_create(struct vop_create_args *ap)
786 {
787 	struct vnode *dvp = ap->a_dvp;
788 	struct vnode **vpp = ap->a_vpp;
789 	struct componentname *cnp = ap->a_cnp;
790 	struct vattr *vap = ap->a_vap;
791 	struct thread *td = curthread;
792 	struct ucred *cred = cnp->cn_cred;
793 
794 	struct fuse_data *data;
795 	struct fuse_create_in *fci;
796 	struct fuse_entry_out *feo;
797 	struct fuse_open_out *foo;
798 	struct fuse_dispatcher fdi, fdi2;
799 	struct fuse_dispatcher *fdip = &fdi;
800 	struct fuse_dispatcher *fdip2 = NULL;
801 
802 	int err;
803 
804 	struct mount *mp = vnode_mount(dvp);
805 	data = fuse_get_mpdata(mp);
806 	uint64_t parentnid = VTOFUD(dvp)->nid;
807 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
808 	enum fuse_opcode op;
809 	int flags;
810 
811 	if (fuse_isdeadfs(dvp))
812 		return ENXIO;
813 
814 	/* FUSE expects sockets to be created with FUSE_MKNOD */
815 	if (vap->va_type == VSOCK)
816 		return fuse_internal_mknod(dvp, vpp, cnp, vap);
817 
818 	/*
819 	 * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
820 	 * writable mode makes sense, and we might as well include readability
821 	 * too.
822 	 */
823 	flags = O_RDWR;
824 
825 	bzero(&fdi, sizeof(fdi));
826 
827 	if (vap->va_type != VREG)
828 		return (EINVAL);
829 
830 	if (fsess_not_impl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
831 		/* Fallback to FUSE_MKNOD/FUSE_OPEN */
832 		fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
833 			cred, mode, &op);
834 	} else {
835 		/* Use FUSE_CREATE */
836 		size_t insize;
837 
838 		op = FUSE_CREATE;
839 		fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
840 		fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
841 		fci = fdip->indata;
842 		fci->mode = mode;
843 		fci->flags = O_CREAT | flags;
844 		if (fuse_libabi_geq(data, 7, 12)) {
845 			insize = sizeof(*fci);
846 			fci->umask = td->td_proc->p_pd->pd_cmask;
847 		} else {
848 			insize = sizeof(struct fuse_open_in);
849 		}
850 
851 		memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
852 		    cnp->cn_namelen);
853 		((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
854 	}
855 
856 	err = fdisp_wait_answ(fdip);
857 
858 	if (err) {
859 		if (err == ENOSYS && op == FUSE_CREATE) {
860 			fsess_set_notimpl(mp, FUSE_CREATE);
861 			fdisp_destroy(fdip);
862 			fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
863 				parentnid, td, cred, mode, &op);
864 			err = fdisp_wait_answ(fdip);
865 		}
866 		if (err)
867 			goto out;
868 	}
869 
870 	feo = fdip->answ;
871 
872 	if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
873 		goto out;
874 	}
875 
876 	if (op == FUSE_CREATE) {
877 		foo = (struct fuse_open_out*)(feo + 1);
878 	} else {
879 		/* Issue a separate FUSE_OPEN */
880 		struct fuse_open_in *foi;
881 
882 		fdip2 = &fdi2;
883 		fdisp_init(fdip2, sizeof(*foi));
884 		fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
885 			cred);
886 		foi = fdip2->indata;
887 		foi->flags = flags;
888 		err = fdisp_wait_answ(fdip2);
889 		if (err)
890 			goto out;
891 		foo = fdip2->answ;
892 	}
893 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
894 	if (err) {
895 		struct fuse_release_in *fri;
896 		uint64_t nodeid = feo->nodeid;
897 		uint64_t fh_id = foo->fh;
898 
899 		fdisp_init(fdip, sizeof(*fri));
900 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
901 		fri = fdip->indata;
902 		fri->fh = fh_id;
903 		fri->flags = flags;
904 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
905 		fuse_insert_message(fdip->tick, false);
906 		goto out;
907 	}
908 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
909 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
910 		feo->attr_valid_nsec, NULL, true);
911 
912 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
913 	fuse_vnode_open(*vpp, foo->open_flags, td);
914 	/*
915 	 * Purge the parent's attribute cache because the daemon should've
916 	 * updated its mtime and ctime
917 	 */
918 	fuse_vnode_clear_attr_cache(dvp);
919 	cache_purge_negative(dvp);
920 
921 out:
922 	if (fdip2)
923 		fdisp_destroy(fdip2);
924 	fdisp_destroy(fdip);
925 	return err;
926 }
927 
928 /*
929     struct vnop_fdatasync_args {
930 	struct vop_generic_args a_gen;
931 	struct vnode * a_vp;
932 	struct thread * a_td;
933     };
934 */
935 static int
936 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
937 {
938 	struct vnode *vp = ap->a_vp;
939 	struct thread *td = ap->a_td;
940 	int waitfor = MNT_WAIT;
941 
942 	int err = 0;
943 
944 	if (fuse_isdeadfs(vp)) {
945 		return 0;
946 	}
947 	if ((err = vop_stdfdatasync_buf(ap)))
948 		return err;
949 
950 	return fuse_internal_fsync(vp, td, waitfor, true);
951 }
952 
953 /*
954     struct vnop_fsync_args {
955 	struct vop_generic_args a_gen;
956 	struct vnode * a_vp;
957 	int  a_waitfor;
958 	struct thread * a_td;
959     };
960 */
961 static int
962 fuse_vnop_fsync(struct vop_fsync_args *ap)
963 {
964 	struct vnode *vp = ap->a_vp;
965 	struct thread *td = ap->a_td;
966 	int waitfor = ap->a_waitfor;
967 	int err = 0;
968 
969 	if (fuse_isdeadfs(vp)) {
970 		return 0;
971 	}
972 	if ((err = vop_stdfsync(ap)))
973 		return err;
974 
975 	return fuse_internal_fsync(vp, td, waitfor, false);
976 }
977 
978 /*
979     struct vnop_getattr_args {
980 	struct vnode *a_vp;
981 	struct vattr *a_vap;
982 	struct ucred *a_cred;
983 	struct thread *a_td;
984     };
985 */
986 static int
987 fuse_vnop_getattr(struct vop_getattr_args *ap)
988 {
989 	struct vnode *vp = ap->a_vp;
990 	struct vattr *vap = ap->a_vap;
991 	struct ucred *cred = ap->a_cred;
992 	struct thread *td = curthread;
993 
994 	int err = 0;
995 	int dataflags;
996 
997 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
998 
999 	/* Note that we are not bailing out on a dead file system just yet. */
1000 
1001 	if (!(dataflags & FSESS_INITED)) {
1002 		if (!vnode_isvroot(vp)) {
1003 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
1004 			err = ENOTCONN;
1005 			return err;
1006 		} else {
1007 			goto fake;
1008 		}
1009 	}
1010 	err = fuse_internal_getattr(vp, vap, cred, td);
1011 	if (err == ENOTCONN && vnode_isvroot(vp)) {
1012 		/* see comment in fuse_vfsop_statfs() */
1013 		goto fake;
1014 	} else {
1015 		return err;
1016 	}
1017 
1018 fake:
1019 	bzero(vap, sizeof(*vap));
1020 	vap->va_type = vnode_vtype(vp);
1021 
1022 	return 0;
1023 }
1024 
1025 /*
1026     struct vnop_inactive_args {
1027 	struct vnode *a_vp;
1028     };
1029 */
1030 static int
1031 fuse_vnop_inactive(struct vop_inactive_args *ap)
1032 {
1033 	struct vnode *vp = ap->a_vp;
1034 	struct thread *td = curthread;
1035 
1036 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
1037 	struct fuse_filehandle *fufh, *fufh_tmp;
1038 
1039 	int need_flush = 1;
1040 
1041 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1042 		if (need_flush && vp->v_type == VREG) {
1043 			if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1044 				fuse_vnode_savesize(vp, NULL, 0);
1045 			}
1046 			if ((fvdat->flag & FN_REVOKED) != 0)
1047 				fuse_io_invalbuf(vp, td);
1048 			else
1049 				fuse_io_flushbuf(vp, MNT_WAIT, td);
1050 			need_flush = 0;
1051 		}
1052 		fuse_filehandle_close(vp, fufh, td, NULL);
1053 	}
1054 
1055 	if ((fvdat->flag & FN_REVOKED) != 0)
1056 		vrecycle(vp);
1057 
1058 	return 0;
1059 }
1060 
1061 /*
1062     struct vnop_ioctl_args {
1063 	struct vnode *a_vp;
1064 	u_long a_command;
1065 	caddr_t a_data;
1066 	int a_fflag;
1067 	struct ucred *a_cred;
1068 	struct thread *a_td;
1069     };
1070 */
1071 static int
1072 fuse_vnop_ioctl(struct vop_ioctl_args *ap)
1073 {
1074 	struct vnode *vp = ap->a_vp;
1075 	struct mount *mp = vnode_mount(vp);
1076 	struct ucred *cred = ap->a_cred;
1077 	off_t *offp;
1078 	pid_t pid = ap->a_td->td_proc->p_pid;
1079 	int err;
1080 
1081 	switch (ap->a_command) {
1082 	case FIOSEEKDATA:
1083 	case FIOSEEKHOLE:
1084 		/* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
1085 		if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
1086 			int whence;
1087 
1088 			offp = ap->a_data;
1089 			if (ap->a_command == FIOSEEKDATA)
1090 				whence = SEEK_DATA;
1091 			else
1092 				whence = SEEK_HOLE;
1093 
1094 			vn_lock(vp, LK_SHARED | LK_RETRY);
1095 			err = fuse_vnop_do_lseek(vp, ap->a_td, cred, pid, offp,
1096 			    whence);
1097 			VOP_UNLOCK(vp);
1098 		}
1099 		if (fsess_not_impl(mp, FUSE_LSEEK))
1100 			err = vop_stdioctl(ap);
1101 		break;
1102 	default:
1103 		/* TODO: implement FUSE_IOCTL */
1104 		err = ENOTTY;
1105 		break;
1106 	}
1107 	return (err);
1108 }
1109 
1110 
1111 /*
1112     struct vnop_link_args {
1113 	struct vnode *a_tdvp;
1114 	struct vnode *a_vp;
1115 	struct componentname *a_cnp;
1116     };
1117 */
1118 static int
1119 fuse_vnop_link(struct vop_link_args *ap)
1120 {
1121 	struct vnode *vp = ap->a_vp;
1122 	struct vnode *tdvp = ap->a_tdvp;
1123 	struct componentname *cnp = ap->a_cnp;
1124 
1125 	struct vattr *vap = VTOVA(vp);
1126 
1127 	struct fuse_dispatcher fdi;
1128 	struct fuse_entry_out *feo;
1129 	struct fuse_link_in fli;
1130 
1131 	int err;
1132 
1133 	if (fuse_isdeadfs(vp)) {
1134 		return ENXIO;
1135 	}
1136 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
1137 		return EXDEV;
1138 	}
1139 
1140 	/*
1141 	 * This is a seatbelt check to protect naive userspace filesystems from
1142 	 * themselves and the limitations of the FUSE IPC protocol.  If a
1143 	 * filesystem does not allow attribute caching, assume it is capable of
1144 	 * validating that nlink does not overflow.
1145 	 */
1146 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
1147 		return EMLINK;
1148 	fli.oldnodeid = VTOI(vp);
1149 
1150 	fdisp_init(&fdi, 0);
1151 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
1152 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
1153 	if ((err = fdisp_wait_answ(&fdi))) {
1154 		goto out;
1155 	}
1156 	feo = fdi.answ;
1157 
1158 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1159 	if (!err) {
1160 		/*
1161 		 * Purge the parent's attribute cache because the daemon
1162 		 * should've updated its mtime and ctime
1163 		 */
1164 		fuse_vnode_clear_attr_cache(tdvp);
1165 		fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
1166 			feo->attr_valid_nsec, NULL, true);
1167 	}
1168 out:
1169 	fdisp_destroy(&fdi);
1170 	return err;
1171 }
1172 
1173 struct fuse_lookup_alloc_arg {
1174 	struct fuse_entry_out *feo;
1175 	struct componentname *cnp;
1176 	uint64_t nid;
1177 	enum vtype vtyp;
1178 };
1179 
1180 /* Callback for vn_get_ino */
1181 static int
1182 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1183 {
1184 	struct fuse_lookup_alloc_arg *flaa = arg;
1185 
1186 	return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1187 		flaa->vtyp);
1188 }
1189 
1190 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1191 	"int", "struct timespec*", "struct timespec*");
1192 /*
1193     struct vnop_lookup_args {
1194 	struct vnodeop_desc *a_desc;
1195 	struct vnode *a_dvp;
1196 	struct vnode **a_vpp;
1197 	struct componentname *a_cnp;
1198     };
1199 */
1200 int
1201 fuse_vnop_lookup(struct vop_lookup_args *ap)
1202 {
1203 	struct vnode *dvp = ap->a_dvp;
1204 	struct vnode **vpp = ap->a_vpp;
1205 	struct componentname *cnp = ap->a_cnp;
1206 	struct thread *td = curthread;
1207 	struct ucred *cred = cnp->cn_cred;
1208 
1209 	int nameiop = cnp->cn_nameiop;
1210 	int flags = cnp->cn_flags;
1211 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
1212 	int islastcn = flags & ISLASTCN;
1213 	struct mount *mp = vnode_mount(dvp);
1214 	struct fuse_data *data = fuse_get_mpdata(mp);
1215 	int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
1216 
1217 	int err = 0;
1218 	int lookup_err = 0;
1219 	struct vnode *vp = NULL;
1220 
1221 	struct fuse_dispatcher fdi;
1222 	bool did_lookup = false;
1223 	struct fuse_entry_out *feo = NULL;
1224 	enum vtype vtyp;	/* vnode type of target */
1225 	off_t filesize;		/* filesize of target */
1226 
1227 	uint64_t nid;
1228 
1229 	if (fuse_isdeadfs(dvp)) {
1230 		*vpp = NULL;
1231 		return ENXIO;
1232 	}
1233 	if (!vnode_isdir(dvp))
1234 		return ENOTDIR;
1235 
1236 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1237 		return EROFS;
1238 
1239 	if ((cnp->cn_flags & NOEXECCHECK) != 0)
1240 		cnp->cn_flags &= ~NOEXECCHECK;
1241 	else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1242 		return err;
1243 
1244 	if (flags & ISDOTDOT) {
1245 		KASSERT(VTOFUD(dvp)->flag & FN_PARENT_NID,
1246 			("Looking up .. is TODO"));
1247 		nid = VTOFUD(dvp)->parent_nid;
1248 		if (nid == 0)
1249 			return ENOENT;
1250 		/* .. is obviously a directory */
1251 		vtyp = VDIR;
1252 		filesize = 0;
1253 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
1254 		nid = VTOI(dvp);
1255 		/* . is obviously a directory */
1256 		vtyp = VDIR;
1257 		filesize = 0;
1258 	} else {
1259 		struct timespec now, timeout;
1260 		int ncpticks; /* here to accomodate for API contract */
1261 
1262 		err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
1263 		getnanouptime(&now);
1264 		SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1265 		switch (err) {
1266 		case -1:		/* positive match */
1267 			if (timespeccmp(&timeout, &now, >)) {
1268 				counter_u64_add(fuse_lookup_cache_hits, 1);
1269 			} else {
1270 				/* Cache timeout */
1271 				counter_u64_add(fuse_lookup_cache_misses, 1);
1272 				bintime_clear(
1273 					&VTOFUD(*vpp)->entry_cache_timeout);
1274 				cache_purge(*vpp);
1275 				if (dvp != *vpp)
1276 					vput(*vpp);
1277 				else
1278 					vrele(*vpp);
1279 				*vpp = NULL;
1280 				break;
1281 			}
1282 			return 0;
1283 
1284 		case 0:		/* no match in cache */
1285 			counter_u64_add(fuse_lookup_cache_misses, 1);
1286 			break;
1287 
1288 		case ENOENT:		/* negative match */
1289 			getnanouptime(&now);
1290 			if (timespeccmp(&timeout, &now, <=)) {
1291 				/* Cache timeout */
1292 				cache_purge_negative(dvp);
1293 				break;
1294 			}
1295 			/* fall through */
1296 		default:
1297 			return err;
1298 		}
1299 
1300 		nid = VTOI(dvp);
1301 		fdisp_init(&fdi, cnp->cn_namelen + 1);
1302 		fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
1303 
1304 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1305 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1306 		lookup_err = fdisp_wait_answ(&fdi);
1307 		did_lookup = true;
1308 
1309 		if (!lookup_err) {
1310 			/* lookup call succeeded */
1311 			feo = (struct fuse_entry_out *)fdi.answ;
1312 			nid = feo->nodeid;
1313 			if (nid == 0) {
1314 				/* zero nodeid means ENOENT and cache it */
1315 				struct timespec timeout;
1316 
1317 				fdi.answ_stat = ENOENT;
1318 				lookup_err = ENOENT;
1319 				if (cnp->cn_flags & MAKEENTRY) {
1320 					fuse_validity_2_timespec(feo, &timeout);
1321 					cache_enter_time(dvp, *vpp, cnp,
1322 						&timeout, NULL);
1323 				}
1324 			} else if (nid == FUSE_ROOT_ID) {
1325 				lookup_err = EINVAL;
1326 			}
1327 			vtyp = IFTOVT(feo->attr.mode);
1328 			filesize = feo->attr.size;
1329 		}
1330 		if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1331 			fdisp_destroy(&fdi);
1332 			return lookup_err;
1333 		}
1334 	}
1335 	/* lookup_err, if non-zero, must be ENOENT at this point */
1336 
1337 	if (lookup_err) {
1338 		/* Entry not found */
1339 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1340 			if (default_permissions)
1341 				err = fuse_internal_access(dvp, VWRITE, td,
1342 				    cred);
1343 			else
1344 				err = 0;
1345 			if (!err) {
1346 				/*
1347 				 * Set the SAVENAME flag to hold onto the
1348 				 * pathname for use later in VOP_CREATE or
1349 				 * VOP_RENAME.
1350 				 */
1351 				cnp->cn_flags |= SAVENAME;
1352 
1353 				err = EJUSTRETURN;
1354 			}
1355 		} else {
1356 			err = ENOENT;
1357 		}
1358 	} else {
1359 		/* Entry was found */
1360 		if (flags & ISDOTDOT) {
1361 			struct fuse_lookup_alloc_arg flaa;
1362 
1363 			flaa.nid = nid;
1364 			flaa.feo = feo;
1365 			flaa.cnp = cnp;
1366 			flaa.vtyp = vtyp;
1367 			err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1368 				&vp);
1369 			*vpp = vp;
1370 		} else if (nid == VTOI(dvp)) {
1371 			vref(dvp);
1372 			*vpp = dvp;
1373 		} else {
1374 			struct fuse_vnode_data *fvdat;
1375 
1376 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1377 			    &vp, cnp, vtyp);
1378 			if (err)
1379 				goto out;
1380 			*vpp = vp;
1381 			fvdat = VTOFUD(vp);
1382 
1383 			MPASS(feo != NULL);
1384 			fuse_internal_cache_attrs(*vpp, &feo->attr,
1385 				feo->attr_valid, feo->attr_valid_nsec, NULL, true);
1386 			fuse_validity_2_bintime(feo->entry_valid,
1387 				feo->entry_valid_nsec,
1388 				&fvdat->entry_cache_timeout);
1389 
1390 			if ((nameiop == DELETE || nameiop == RENAME) &&
1391 				islastcn && default_permissions)
1392 			{
1393 				struct vattr dvattr;
1394 
1395 				err = fuse_internal_access(dvp, VWRITE, td,
1396 					cred);
1397 				if (err != 0)
1398 					goto out;
1399 				/*
1400 				 * if the parent's sticky bit is set, check
1401 				 * whether we're allowed to remove the file.
1402 				 * Need to figure out the vnode locking to make
1403 				 * this work.
1404 				 */
1405 				fuse_internal_getattr(dvp, &dvattr, cred, td);
1406 				if ((dvattr.va_mode & S_ISTXT) &&
1407 					fuse_internal_access(dvp, VADMIN, td,
1408 						cred) &&
1409 					fuse_internal_access(*vpp, VADMIN, td,
1410 						cred)) {
1411 					err = EPERM;
1412 					goto out;
1413 				}
1414 			}
1415 
1416 			if (islastcn && (
1417 				(nameiop == DELETE) ||
1418 				(nameiop == RENAME && wantparent))) {
1419 				cnp->cn_flags |= SAVENAME;
1420 			}
1421 		}
1422 	}
1423 out:
1424 	if (err) {
1425 		if (vp != NULL && dvp != vp)
1426 			vput(vp);
1427 		else if (vp != NULL)
1428 			vrele(vp);
1429 		*vpp = NULL;
1430 	}
1431 	if (did_lookup)
1432 		fdisp_destroy(&fdi);
1433 
1434 	return err;
1435 }
1436 
1437 /*
1438     struct vnop_mkdir_args {
1439 	struct vnode *a_dvp;
1440 	struct vnode **a_vpp;
1441 	struct componentname *a_cnp;
1442 	struct vattr *a_vap;
1443     };
1444 */
1445 static int
1446 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1447 {
1448 	struct vnode *dvp = ap->a_dvp;
1449 	struct vnode **vpp = ap->a_vpp;
1450 	struct componentname *cnp = ap->a_cnp;
1451 	struct vattr *vap = ap->a_vap;
1452 
1453 	struct fuse_mkdir_in fmdi;
1454 
1455 	if (fuse_isdeadfs(dvp)) {
1456 		return ENXIO;
1457 	}
1458 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1459 	fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
1460 
1461 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1462 	    sizeof(fmdi), VDIR));
1463 }
1464 
1465 /*
1466     struct vnop_mknod_args {
1467 	struct vnode *a_dvp;
1468 	struct vnode **a_vpp;
1469 	struct componentname *a_cnp;
1470 	struct vattr *a_vap;
1471     };
1472 */
1473 static int
1474 fuse_vnop_mknod(struct vop_mknod_args *ap)
1475 {
1476 
1477 	struct vnode *dvp = ap->a_dvp;
1478 	struct vnode **vpp = ap->a_vpp;
1479 	struct componentname *cnp = ap->a_cnp;
1480 	struct vattr *vap = ap->a_vap;
1481 
1482 	if (fuse_isdeadfs(dvp))
1483 		return ENXIO;
1484 
1485 	return fuse_internal_mknod(dvp, vpp, cnp, vap);
1486 }
1487 
1488 /*
1489     struct vop_open_args {
1490 	struct vnode *a_vp;
1491 	int  a_mode;
1492 	struct ucred *a_cred;
1493 	struct thread *a_td;
1494 	int a_fdidx; / struct file *a_fp;
1495     };
1496 */
1497 static int
1498 fuse_vnop_open(struct vop_open_args *ap)
1499 {
1500 	struct vnode *vp = ap->a_vp;
1501 	int a_mode = ap->a_mode;
1502 	struct thread *td = ap->a_td;
1503 	struct ucred *cred = ap->a_cred;
1504 	pid_t pid = td->td_proc->p_pid;
1505 	struct fuse_vnode_data *fvdat;
1506 
1507 	if (fuse_isdeadfs(vp))
1508 		return ENXIO;
1509 	if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1510 		return (EOPNOTSUPP);
1511 	if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1512 		return EINVAL;
1513 
1514 	fvdat = VTOFUD(vp);
1515 
1516 	if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1517 		fuse_vnode_open(vp, 0, td);
1518 		return 0;
1519 	}
1520 
1521 	return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1522 }
1523 
1524 static int
1525 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1526 {
1527 	struct vnode *vp = ap->a_vp;
1528 	struct mount *mp;
1529 
1530 	switch (ap->a_name) {
1531 	case _PC_FILESIZEBITS:
1532 		*ap->a_retval = 64;
1533 		return (0);
1534 	case _PC_NAME_MAX:
1535 		*ap->a_retval = NAME_MAX;
1536 		return (0);
1537 	case _PC_LINK_MAX:
1538 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1539 		return (0);
1540 	case _PC_SYMLINK_MAX:
1541 		*ap->a_retval = MAXPATHLEN;
1542 		return (0);
1543 	case _PC_NO_TRUNC:
1544 		*ap->a_retval = 1;
1545 		return (0);
1546 	case _PC_MIN_HOLE_SIZE:
1547 		/*
1548 		 * The FUSE protocol provides no mechanism for a server to
1549 		 * report _PC_MIN_HOLE_SIZE.  It's a protocol bug.  Instead,
1550 		 * return EINVAL if the server does not support FUSE_LSEEK, or
1551 		 * 1 if it does.
1552 		 */
1553 		mp = vnode_mount(vp);
1554 		if (!fsess_is_impl(mp, FUSE_LSEEK) &&
1555 		    !fsess_not_impl(mp, FUSE_LSEEK)) {
1556 			off_t offset = 0;
1557 
1558 			/* Issue a FUSE_LSEEK to find out if it's implemented */
1559 			fuse_vnop_do_lseek(vp, curthread, curthread->td_ucred,
1560 			    curthread->td_proc->p_pid, &offset, SEEK_DATA);
1561 		}
1562 
1563 		if (fsess_is_impl(mp, FUSE_LSEEK)) {
1564 			*ap->a_retval = 1;
1565 			return (0);
1566 		} else {
1567 			/*
1568 			 * Probably FUSE_LSEEK is not implemented.  It might
1569 			 * be, if the FUSE_LSEEK above returned an error like
1570 			 * EACCES, but in that case we can't tell, so it's
1571 			 * safest to report EINVAL anyway.
1572 			 */
1573 			return (EINVAL);
1574 		}
1575 	default:
1576 		return (vop_stdpathconf(ap));
1577 	}
1578 }
1579 
1580 /*
1581     struct vnop_read_args {
1582 	struct vnode *a_vp;
1583 	struct uio *a_uio;
1584 	int  a_ioflag;
1585 	struct ucred *a_cred;
1586     };
1587 */
1588 static int
1589 fuse_vnop_read(struct vop_read_args *ap)
1590 {
1591 	struct vnode *vp = ap->a_vp;
1592 	struct uio *uio = ap->a_uio;
1593 	int ioflag = ap->a_ioflag;
1594 	struct ucred *cred = ap->a_cred;
1595 	pid_t pid = curthread->td_proc->p_pid;
1596 
1597 	if (fuse_isdeadfs(vp)) {
1598 		return ENXIO;
1599 	}
1600 
1601 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1602 		ioflag |= IO_DIRECT;
1603 	}
1604 
1605 	return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1606 }
1607 
1608 /*
1609     struct vnop_readdir_args {
1610 	struct vnode *a_vp;
1611 	struct uio *a_uio;
1612 	struct ucred *a_cred;
1613 	int *a_eofflag;
1614 	int *a_ncookies;
1615 	u_long **a_cookies;
1616     };
1617 */
1618 static int
1619 fuse_vnop_readdir(struct vop_readdir_args *ap)
1620 {
1621 	struct vnode *vp = ap->a_vp;
1622 	struct uio *uio = ap->a_uio;
1623 	struct ucred *cred = ap->a_cred;
1624 	struct fuse_filehandle *fufh = NULL;
1625 	struct fuse_iov cookediov;
1626 	int err = 0;
1627 	u_long *cookies;
1628 	off_t startoff;
1629 	ssize_t tresid;
1630 	int ncookies;
1631 	bool closefufh = false;
1632 	pid_t pid = curthread->td_proc->p_pid;
1633 
1634 	if (ap->a_eofflag)
1635 		*ap->a_eofflag = 0;
1636 	if (fuse_isdeadfs(vp)) {
1637 		return ENXIO;
1638 	}
1639 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
1640 	    (uio_resid(uio) < sizeof(struct dirent))) {
1641 		return EINVAL;
1642 	}
1643 
1644 	tresid = uio->uio_resid;
1645 	startoff = uio->uio_offset;
1646 	err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1647 	if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1648 		/*
1649 		 * nfsd will do VOP_READDIR without first doing VOP_OPEN.  We
1650 		 * must implicitly open the directory here
1651 		 */
1652 		err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1653 		if (err == 0) {
1654 			/*
1655 			 * When a directory is opened, it must be read from
1656 			 * the beginning.  Hopefully, the "startoff" still
1657 			 * exists as an offset cookie for the directory.
1658 			 * If not, it will read the entire directory without
1659 			 * returning any entries and just return eof.
1660 			 */
1661 			uio->uio_offset = 0;
1662 		}
1663 		closefufh = true;
1664 	}
1665 	if (err)
1666 		return (err);
1667 	if (ap->a_ncookies != NULL) {
1668 		ncookies = uio->uio_resid /
1669 			(offsetof(struct dirent, d_name) + 4) + 1;
1670 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1671 		*ap->a_ncookies = ncookies;
1672 		*ap->a_cookies = cookies;
1673 	} else {
1674 		ncookies = 0;
1675 		cookies = NULL;
1676 	}
1677 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1678 	fiov_init(&cookediov, DIRCOOKEDSIZE);
1679 
1680 	err = fuse_internal_readdir(vp, uio, startoff, fufh, &cookediov,
1681 		&ncookies, cookies);
1682 
1683 	fiov_teardown(&cookediov);
1684 	if (closefufh)
1685 		fuse_filehandle_close(vp, fufh, curthread, cred);
1686 
1687 	if (ap->a_ncookies != NULL) {
1688 		if (err == 0) {
1689 			*ap->a_ncookies -= ncookies;
1690 		} else {
1691 			free(*ap->a_cookies, M_TEMP);
1692 			*ap->a_ncookies = 0;
1693 			*ap->a_cookies = NULL;
1694 		}
1695 	}
1696 	if (err == 0 && tresid == uio->uio_resid)
1697 		*ap->a_eofflag = 1;
1698 
1699 	return err;
1700 }
1701 
1702 /*
1703     struct vnop_readlink_args {
1704 	struct vnode *a_vp;
1705 	struct uio *a_uio;
1706 	struct ucred *a_cred;
1707     };
1708 */
1709 static int
1710 fuse_vnop_readlink(struct vop_readlink_args *ap)
1711 {
1712 	struct vnode *vp = ap->a_vp;
1713 	struct uio *uio = ap->a_uio;
1714 	struct ucred *cred = ap->a_cred;
1715 
1716 	struct fuse_dispatcher fdi;
1717 	int err;
1718 
1719 	if (fuse_isdeadfs(vp)) {
1720 		return ENXIO;
1721 	}
1722 	if (!vnode_islnk(vp)) {
1723 		return EINVAL;
1724 	}
1725 	fdisp_init(&fdi, 0);
1726 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1727 	if (err) {
1728 		goto out;
1729 	}
1730 	if (((char *)fdi.answ)[0] == '/' &&
1731 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1732 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1733 
1734 		err = uiomove(mpth, strlen(mpth), uio);
1735 	}
1736 	if (!err) {
1737 		err = uiomove(fdi.answ, fdi.iosize, uio);
1738 	}
1739 out:
1740 	fdisp_destroy(&fdi);
1741 	return err;
1742 }
1743 
1744 /*
1745     struct vnop_reclaim_args {
1746 	struct vnode *a_vp;
1747     };
1748 */
1749 static int
1750 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1751 {
1752 	struct vnode *vp = ap->a_vp;
1753 	struct thread *td = curthread;
1754 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
1755 	struct fuse_filehandle *fufh, *fufh_tmp;
1756 
1757 	if (!fvdat) {
1758 		panic("FUSE: no vnode data during recycling");
1759 	}
1760 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1761 		printf("FUSE: vnode being reclaimed with open fufh "
1762 			"(type=%#x)", fufh->fufh_type);
1763 		fuse_filehandle_close(vp, fufh, td, NULL);
1764 	}
1765 
1766 	if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
1767 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1768 		    fvdat->nlookup);
1769 	}
1770 	cache_purge(vp);
1771 	vfs_hash_remove(vp);
1772 	fuse_vnode_destroy(vp);
1773 
1774 	return 0;
1775 }
1776 
1777 /*
1778     struct vnop_remove_args {
1779 	struct vnode *a_dvp;
1780 	struct vnode *a_vp;
1781 	struct componentname *a_cnp;
1782     };
1783 */
1784 static int
1785 fuse_vnop_remove(struct vop_remove_args *ap)
1786 {
1787 	struct vnode *dvp = ap->a_dvp;
1788 	struct vnode *vp = ap->a_vp;
1789 	struct componentname *cnp = ap->a_cnp;
1790 
1791 	int err;
1792 
1793 	if (fuse_isdeadfs(vp)) {
1794 		return ENXIO;
1795 	}
1796 	if (vnode_isdir(vp)) {
1797 		return EPERM;
1798 	}
1799 
1800 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1801 
1802 	return err;
1803 }
1804 
1805 /*
1806     struct vnop_rename_args {
1807 	struct vnode *a_fdvp;
1808 	struct vnode *a_fvp;
1809 	struct componentname *a_fcnp;
1810 	struct vnode *a_tdvp;
1811 	struct vnode *a_tvp;
1812 	struct componentname *a_tcnp;
1813     };
1814 */
1815 static int
1816 fuse_vnop_rename(struct vop_rename_args *ap)
1817 {
1818 	struct vnode *fdvp = ap->a_fdvp;
1819 	struct vnode *fvp = ap->a_fvp;
1820 	struct componentname *fcnp = ap->a_fcnp;
1821 	struct vnode *tdvp = ap->a_tdvp;
1822 	struct vnode *tvp = ap->a_tvp;
1823 	struct componentname *tcnp = ap->a_tcnp;
1824 	struct fuse_data *data;
1825 	bool newparent = fdvp != tdvp;
1826 	bool isdir = fvp->v_type == VDIR;
1827 	int err = 0;
1828 
1829 	if (fuse_isdeadfs(fdvp)) {
1830 		return ENXIO;
1831 	}
1832 	if (fvp->v_mount != tdvp->v_mount ||
1833 	    (tvp && fvp->v_mount != tvp->v_mount)) {
1834 		SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1835 		err = EXDEV;
1836 		goto out;
1837 	}
1838 	cache_purge(fvp);
1839 
1840 	/*
1841 	 * FUSE library is expected to check if target directory is not
1842 	 * under the source directory in the file system tree.
1843 	 * Linux performs this check at VFS level.
1844 	 */
1845 	/*
1846 	 * If source is a directory, and it will get a new parent, user must
1847 	 * have write permission to it, so ".." can be modified.
1848 	 */
1849 	data = fuse_get_mpdata(vnode_mount(tdvp));
1850 	if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1851 		err = fuse_internal_access(fvp, VWRITE,
1852 			curthread, tcnp->cn_cred);
1853 		if (err)
1854 			goto out;
1855 	}
1856 	sx_xlock(&data->rename_lock);
1857 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1858 	if (err == 0) {
1859 		if (tdvp != fdvp)
1860 			fuse_vnode_setparent(fvp, tdvp);
1861 		if (tvp != NULL)
1862 			fuse_vnode_setparent(tvp, NULL);
1863 	}
1864 	sx_unlock(&data->rename_lock);
1865 
1866 	if (tvp != NULL && tvp != fvp) {
1867 		cache_purge(tvp);
1868 	}
1869 	if (vnode_isdir(fvp)) {
1870 		if ((tvp != NULL) && vnode_isdir(tvp)) {
1871 			cache_purge(tdvp);
1872 		}
1873 		cache_purge(fdvp);
1874 	}
1875 out:
1876 	if (tdvp == tvp) {
1877 		vrele(tdvp);
1878 	} else {
1879 		vput(tdvp);
1880 	}
1881 	if (tvp != NULL) {
1882 		vput(tvp);
1883 	}
1884 	vrele(fdvp);
1885 	vrele(fvp);
1886 
1887 	return err;
1888 }
1889 
1890 /*
1891     struct vnop_rmdir_args {
1892 	    struct vnode *a_dvp;
1893 	    struct vnode *a_vp;
1894 	    struct componentname *a_cnp;
1895     } *ap;
1896 */
1897 static int
1898 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1899 {
1900 	struct vnode *dvp = ap->a_dvp;
1901 	struct vnode *vp = ap->a_vp;
1902 
1903 	int err;
1904 
1905 	if (fuse_isdeadfs(vp)) {
1906 		return ENXIO;
1907 	}
1908 	if (VTOFUD(vp) == VTOFUD(dvp)) {
1909 		return EINVAL;
1910 	}
1911 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1912 
1913 	return err;
1914 }
1915 
1916 /*
1917     struct vnop_setattr_args {
1918 	struct vnode *a_vp;
1919 	struct vattr *a_vap;
1920 	struct ucred *a_cred;
1921 	struct thread *a_td;
1922     };
1923 */
1924 static int
1925 fuse_vnop_setattr(struct vop_setattr_args *ap)
1926 {
1927 	struct vnode *vp = ap->a_vp;
1928 	struct vattr *vap = ap->a_vap;
1929 	struct ucred *cred = ap->a_cred;
1930 	struct thread *td = curthread;
1931 	struct mount *mp;
1932 	struct fuse_data *data;
1933 	struct vattr old_va;
1934 	int dataflags;
1935 	int err = 0, err2;
1936 	accmode_t accmode = 0;
1937 	bool checkperm;
1938 	bool drop_suid = false;
1939 	gid_t cr_gid;
1940 
1941 	mp = vnode_mount(vp);
1942 	data = fuse_get_mpdata(mp);
1943 	dataflags = data->dataflags;
1944 	checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1945 	if (cred->cr_ngroups > 0)
1946 		cr_gid = cred->cr_groups[0];
1947 	else
1948 		cr_gid = 0;
1949 
1950 	if (fuse_isdeadfs(vp)) {
1951 		return ENXIO;
1952 	}
1953 
1954 	if (vap->va_uid != (uid_t)VNOVAL) {
1955 		if (checkperm) {
1956 			/* Only root may change a file's owner */
1957 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1958 			if (err) {
1959 				/* As a special case, allow the null chown */
1960 				err2 = fuse_internal_getattr(vp, &old_va, cred,
1961 					td);
1962 				if (err2)
1963 					return (err2);
1964 				if (vap->va_uid != old_va.va_uid)
1965 					return err;
1966 				else
1967 					accmode |= VADMIN;
1968 				drop_suid = true;
1969 			} else
1970 				accmode |= VADMIN;
1971 		} else
1972 			accmode |= VADMIN;
1973 	}
1974 	if (vap->va_gid != (gid_t)VNOVAL) {
1975 		if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1976 			drop_suid = true;
1977 		if (checkperm && !groupmember(vap->va_gid, cred))
1978 		{
1979 			/*
1980 			 * Non-root users may only chgrp to one of their own
1981 			 * groups
1982 			 */
1983 			err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1984 			if (err) {
1985 				/* As a special case, allow the null chgrp */
1986 				err2 = fuse_internal_getattr(vp, &old_va, cred,
1987 					td);
1988 				if (err2)
1989 					return (err2);
1990 				if (vap->va_gid != old_va.va_gid)
1991 					return err;
1992 				accmode |= VADMIN;
1993 			} else
1994 				accmode |= VADMIN;
1995 		} else
1996 			accmode |= VADMIN;
1997 	}
1998 	if (vap->va_size != VNOVAL) {
1999 		switch (vp->v_type) {
2000 		case VDIR:
2001 			return (EISDIR);
2002 		case VLNK:
2003 		case VREG:
2004 			if (vfs_isrdonly(mp))
2005 				return (EROFS);
2006 			break;
2007 		default:
2008 			/*
2009 			 * According to POSIX, the result is unspecified
2010 			 * for file types other than regular files,
2011 			 * directories and shared memory objects.  We
2012 			 * don't support shared memory objects in the file
2013 			 * system, and have dubious support for truncating
2014 			 * symlinks.  Just ignore the request in other cases.
2015 			 */
2016 			return (0);
2017 		}
2018 		/* Don't set accmode.  Permission to trunc is checked upstack */
2019 	}
2020 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2021 		if (vap->va_vaflags & VA_UTIMES_NULL)
2022 			accmode |= VWRITE;
2023 		else
2024 			accmode |= VADMIN;
2025 	}
2026 	if (drop_suid) {
2027 		if (vap->va_mode != (mode_t)VNOVAL)
2028 			vap->va_mode &= ~(S_ISUID | S_ISGID);
2029 		else {
2030 			err = fuse_internal_getattr(vp, &old_va, cred, td);
2031 			if (err)
2032 				return (err);
2033 			vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2034 		}
2035 	}
2036 	if (vap->va_mode != (mode_t)VNOVAL) {
2037 		/* Only root may set the sticky bit on non-directories */
2038 		if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2039 		    && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2040 			return EFTYPE;
2041 		if (checkperm && (vap->va_mode & S_ISGID)) {
2042 			err = fuse_internal_getattr(vp, &old_va, cred, td);
2043 			if (err)
2044 				return (err);
2045 			if (!groupmember(old_va.va_gid, cred)) {
2046 				err = priv_check_cred(cred, PRIV_VFS_SETGID);
2047 				if (err)
2048 					return (err);
2049 			}
2050 		}
2051 		accmode |= VADMIN;
2052 	}
2053 
2054 	if (vfs_isrdonly(mp))
2055 		return EROFS;
2056 
2057 	if (checkperm) {
2058 		err = fuse_internal_access(vp, accmode, td, cred);
2059 	} else {
2060 		err = 0;
2061 	}
2062 	if (err)
2063 		return err;
2064 	else
2065 		return fuse_internal_setattr(vp, vap, td, cred);
2066 }
2067 
2068 /*
2069     struct vnop_strategy_args {
2070 	struct vnode *a_vp;
2071 	struct buf *a_bp;
2072     };
2073 */
2074 static int
2075 fuse_vnop_strategy(struct vop_strategy_args *ap)
2076 {
2077 	struct vnode *vp = ap->a_vp;
2078 	struct buf *bp = ap->a_bp;
2079 
2080 	if (!vp || fuse_isdeadfs(vp)) {
2081 		bp->b_ioflags |= BIO_ERROR;
2082 		bp->b_error = ENXIO;
2083 		bufdone(bp);
2084 		return 0;
2085 	}
2086 
2087 	/*
2088 	 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2089 	 * fuse_io_strategy sets bp's error fields
2090 	 */
2091 	(void)fuse_io_strategy(vp, bp);
2092 
2093 	return 0;
2094 }
2095 
2096 /*
2097     struct vnop_symlink_args {
2098 	struct vnode *a_dvp;
2099 	struct vnode **a_vpp;
2100 	struct componentname *a_cnp;
2101 	struct vattr *a_vap;
2102 	char *a_target;
2103     };
2104 */
2105 static int
2106 fuse_vnop_symlink(struct vop_symlink_args *ap)
2107 {
2108 	struct vnode *dvp = ap->a_dvp;
2109 	struct vnode **vpp = ap->a_vpp;
2110 	struct componentname *cnp = ap->a_cnp;
2111 	const char *target = ap->a_target;
2112 
2113 	struct fuse_dispatcher fdi;
2114 
2115 	int err;
2116 	size_t len;
2117 
2118 	if (fuse_isdeadfs(dvp)) {
2119 		return ENXIO;
2120 	}
2121 	/*
2122 	 * Unlike the other creator type calls, here we have to create a message
2123 	 * where the name of the new entry comes first, and the data describing
2124 	 * the entry comes second.
2125 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
2126 	 * but put together the message manually and just call the core part.
2127 	 */
2128 
2129 	len = strlen(target) + 1;
2130 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
2131 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
2132 
2133 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
2134 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
2135 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
2136 
2137 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
2138 	fdisp_destroy(&fdi);
2139 	return err;
2140 }
2141 
2142 /*
2143     struct vnop_write_args {
2144 	struct vnode *a_vp;
2145 	struct uio *a_uio;
2146 	int  a_ioflag;
2147 	struct ucred *a_cred;
2148     };
2149 */
2150 static int
2151 fuse_vnop_write(struct vop_write_args *ap)
2152 {
2153 	struct vnode *vp = ap->a_vp;
2154 	struct uio *uio = ap->a_uio;
2155 	int ioflag = ap->a_ioflag;
2156 	struct ucred *cred = ap->a_cred;
2157 	pid_t pid = curthread->td_proc->p_pid;
2158 
2159 	if (fuse_isdeadfs(vp)) {
2160 		return ENXIO;
2161 	}
2162 
2163 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2164 		ioflag |= IO_DIRECT;
2165 	}
2166 
2167 	return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
2168 }
2169 
2170 static daddr_t
2171 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2172 {
2173 	const int biosize = fuse_iosize(vp);
2174 
2175 	return (off / biosize);
2176 }
2177 
2178 static int
2179 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2180 {
2181 	off_t filesize;
2182 	int err;
2183 	const int biosize = fuse_iosize(vp);
2184 
2185 	err = fuse_vnode_size(vp, &filesize, NULL, NULL);
2186 	if (err) {
2187 		/* This will turn into a SIGBUS */
2188 		return (EIO);
2189 	} else if ((off_t)lbn * biosize >= filesize) {
2190 		*blksz = 0;
2191 	} else if ((off_t)(lbn + 1) * biosize > filesize) {
2192 		*blksz = filesize - (off_t)lbn *biosize;
2193 	} else {
2194 		*blksz = biosize;
2195 	}
2196 	return (0);
2197 }
2198 
2199 /*
2200     struct vnop_getpages_args {
2201 	struct vnode *a_vp;
2202 	vm_page_t *a_m;
2203 	int a_count;
2204 	int a_reqpage;
2205     };
2206 */
2207 static int
2208 fuse_vnop_getpages(struct vop_getpages_args *ap)
2209 {
2210 	struct vnode *vp = ap->a_vp;
2211 
2212 	if (!fsess_opt_mmap(vnode_mount(vp))) {
2213 		SDT_PROBE2(fusefs, , vnops, trace, 1,
2214 			"called on non-cacheable vnode??\n");
2215 		return (VM_PAGER_ERROR);
2216 	}
2217 
2218 	return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2219 	    ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2220 }
2221 
2222 static const char extattr_namespace_separator = '.';
2223 
2224 /*
2225     struct vop_getextattr_args {
2226 	struct vop_generic_args a_gen;
2227 	struct vnode *a_vp;
2228 	int a_attrnamespace;
2229 	const char *a_name;
2230 	struct uio *a_uio;
2231 	size_t *a_size;
2232 	struct ucred *a_cred;
2233 	struct thread *a_td;
2234     };
2235 */
2236 static int
2237 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2238 {
2239 	struct vnode *vp = ap->a_vp;
2240 	struct uio *uio = ap->a_uio;
2241 	struct fuse_dispatcher fdi;
2242 	struct fuse_getxattr_in *get_xattr_in;
2243 	struct fuse_getxattr_out *get_xattr_out;
2244 	struct mount *mp = vnode_mount(vp);
2245 	struct thread *td = ap->a_td;
2246 	struct ucred *cred = ap->a_cred;
2247 	char *prefix;
2248 	char *attr_str;
2249 	size_t len;
2250 	int err;
2251 
2252 	if (fuse_isdeadfs(vp))
2253 		return (ENXIO);
2254 
2255 	if (fsess_not_impl(mp, FUSE_GETXATTR))
2256 		return EOPNOTSUPP;
2257 
2258 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2259 	if (err)
2260 		return err;
2261 
2262 	/* Default to looking for user attributes. */
2263 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2264 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2265 	else
2266 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2267 
2268 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2269 	    strlen(ap->a_name) + 1;
2270 
2271 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2272 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2273 
2274 	get_xattr_in = fdi.indata;
2275 	/*
2276 	 * Check to see whether we're querying the available size or
2277 	 * issuing the actual request.  If we pass in 0, we get back struct
2278 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
2279 	 * that much data, without the struct fuse_getxattr_out header.
2280 	 */
2281 	if (uio == NULL)
2282 		get_xattr_in->size = 0;
2283 	else
2284 		get_xattr_in->size = uio->uio_resid;
2285 
2286 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2287 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2288 	    ap->a_name);
2289 
2290 	err = fdisp_wait_answ(&fdi);
2291 	if (err != 0) {
2292 		if (err == ENOSYS) {
2293 			fsess_set_notimpl(mp, FUSE_GETXATTR);
2294 			err = EOPNOTSUPP;
2295 		}
2296 		goto out;
2297 	}
2298 
2299 	get_xattr_out = fdi.answ;
2300 
2301 	if (ap->a_size != NULL)
2302 		*ap->a_size = get_xattr_out->size;
2303 
2304 	if (uio != NULL)
2305 		err = uiomove(fdi.answ, fdi.iosize, uio);
2306 
2307 out:
2308 	fdisp_destroy(&fdi);
2309 	return (err);
2310 }
2311 
2312 /*
2313     struct vop_setextattr_args {
2314 	struct vop_generic_args a_gen;
2315 	struct vnode *a_vp;
2316 	int a_attrnamespace;
2317 	const char *a_name;
2318 	struct uio *a_uio;
2319 	struct ucred *a_cred;
2320 	struct thread *a_td;
2321     };
2322 */
2323 static int
2324 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2325 {
2326 	struct vnode *vp = ap->a_vp;
2327 	struct uio *uio = ap->a_uio;
2328 	struct fuse_dispatcher fdi;
2329 	struct fuse_setxattr_in *set_xattr_in;
2330 	struct mount *mp = vnode_mount(vp);
2331 	struct thread *td = ap->a_td;
2332 	struct ucred *cred = ap->a_cred;
2333 	char *prefix;
2334 	size_t len;
2335 	char *attr_str;
2336 	int err;
2337 
2338 	if (fuse_isdeadfs(vp))
2339 		return (ENXIO);
2340 
2341 	if (fsess_not_impl(mp, FUSE_SETXATTR))
2342 		return EOPNOTSUPP;
2343 
2344 	if (vfs_isrdonly(mp))
2345 		return EROFS;
2346 
2347 	/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2348 	if (ap->a_uio == NULL) {
2349 		/*
2350 		 * If we got here as fallback from VOP_DELETEEXTATTR, then
2351 		 * return EOPNOTSUPP.
2352 		 */
2353 		if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2354 			return (EOPNOTSUPP);
2355 		else
2356 			return (EINVAL);
2357 	}
2358 
2359 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2360 		VWRITE);
2361 	if (err)
2362 		return err;
2363 
2364 	/* Default to looking for user attributes. */
2365 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2366 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2367 	else
2368 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2369 
2370 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2371 	    strlen(ap->a_name) + 1;
2372 
2373 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2374 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2375 
2376 	set_xattr_in = fdi.indata;
2377 	set_xattr_in->size = uio->uio_resid;
2378 
2379 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2380 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2381 	    ap->a_name);
2382 
2383 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2384 	    uio->uio_resid, uio);
2385 	if (err != 0) {
2386 		goto out;
2387 	}
2388 
2389 	err = fdisp_wait_answ(&fdi);
2390 
2391 	if (err == ENOSYS) {
2392 		fsess_set_notimpl(mp, FUSE_SETXATTR);
2393 		err = EOPNOTSUPP;
2394 	}
2395 	if (err == ERESTART) {
2396 		/* Can't restart after calling uiomove */
2397 		err = EINTR;
2398 	}
2399 
2400 out:
2401 	fdisp_destroy(&fdi);
2402 	return (err);
2403 }
2404 
2405 /*
2406  * The Linux / FUSE extended attribute list is simply a collection of
2407  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
2408  * byte length followed by a non-NUL terminated string.  So, this allows
2409  * conversion of the Linux / FUSE format to the FreeBSD format in place.
2410  * Linux attribute names are reported with the namespace as a prefix (e.g.
2411  * "user.attribute_name"), but in FreeBSD they are reported without the
2412  * namespace prefix (e.g. "attribute_name").  So, we're going from:
2413  *
2414  * user.attr_name1\0user.attr_name2\0
2415  *
2416  * to:
2417  *
2418  * <num>attr_name1<num>attr_name2
2419  *
2420  * Where "<num>" is a single byte number of characters in the attribute name.
2421  *
2422  * Args:
2423  * prefix - exattr namespace prefix string
2424  * list, list_len - input list with namespace prefixes
2425  * bsd_list, bsd_list_len - output list compatible with bsd vfs
2426  */
2427 static int
2428 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2429     char *bsd_list, int *bsd_list_len)
2430 {
2431 	int len, pos, dist_to_next, prefix_len;
2432 
2433 	pos = 0;
2434 	*bsd_list_len = 0;
2435 	prefix_len = strlen(prefix);
2436 
2437 	while (pos < list_len && list[pos] != '\0') {
2438 		dist_to_next = strlen(&list[pos]) + 1;
2439 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2440 		    list[pos + prefix_len] == extattr_namespace_separator) {
2441 			len = dist_to_next -
2442 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2443 			if (len >= EXTATTR_MAXNAMELEN)
2444 				return (ENAMETOOLONG);
2445 
2446 			bsd_list[*bsd_list_len] = len;
2447 			memcpy(&bsd_list[*bsd_list_len + 1],
2448 			    &list[pos + prefix_len +
2449 			    sizeof(extattr_namespace_separator)], len);
2450 
2451 			*bsd_list_len += len + 1;
2452 		}
2453 
2454 		pos += dist_to_next;
2455 	}
2456 
2457 	return (0);
2458 }
2459 
2460 /*
2461  * List extended attributes
2462  *
2463  * The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
2464  * has a number of differences compared to its FreeBSD equivalent,
2465  * extattr_list_file:
2466  *
2467  * - FUSE_LISTXATTR returns all extended attributes across all namespaces,
2468  *   whereas listxattr(2) only returns attributes for a single namespace
2469  * - FUSE_LISTXATTR prepends each attribute name with "namespace."
2470  * - If the provided buffer is not large enough to hold the result,
2471  *   FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
2472  *   return as many results as will fit.
2473  */
2474 /*
2475     struct vop_listextattr_args {
2476 	struct vop_generic_args a_gen;
2477 	struct vnode *a_vp;
2478 	int a_attrnamespace;
2479 	struct uio *a_uio;
2480 	size_t *a_size;
2481 	struct ucred *a_cred;
2482 	struct thread *a_td;
2483     };
2484 */
2485 static int
2486 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2487 {
2488 	struct vnode *vp = ap->a_vp;
2489 	struct uio *uio = ap->a_uio;
2490 	struct fuse_dispatcher fdi;
2491 	struct fuse_listxattr_in *list_xattr_in;
2492 	struct fuse_listxattr_out *list_xattr_out;
2493 	struct mount *mp = vnode_mount(vp);
2494 	struct thread *td = ap->a_td;
2495 	struct ucred *cred = ap->a_cred;
2496 	char *prefix;
2497 	char *bsd_list = NULL;
2498 	char *linux_list;
2499 	int bsd_list_len;
2500 	int linux_list_len;
2501 	int err;
2502 
2503 	if (fuse_isdeadfs(vp))
2504 		return (ENXIO);
2505 
2506 	if (fsess_not_impl(mp, FUSE_LISTXATTR))
2507 		return EOPNOTSUPP;
2508 
2509 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2510 	if (err)
2511 		return err;
2512 
2513 	/*
2514 	 * Add space for a NUL and the period separator if enabled.
2515 	 * Default to looking for user attributes.
2516 	 */
2517 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2518 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2519 	else
2520 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2521 
2522 	fdisp_init(&fdi, sizeof(*list_xattr_in));
2523 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2524 
2525 	/*
2526 	 * Retrieve Linux / FUSE compatible list size.
2527 	 */
2528 	list_xattr_in = fdi.indata;
2529 	list_xattr_in->size = 0;
2530 
2531 	err = fdisp_wait_answ(&fdi);
2532 	if (err != 0) {
2533 		if (err == ENOSYS) {
2534 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
2535 			err = EOPNOTSUPP;
2536 		}
2537 		goto out;
2538 	}
2539 
2540 	list_xattr_out = fdi.answ;
2541 	linux_list_len = list_xattr_out->size;
2542 	if (linux_list_len == 0) {
2543 		if (ap->a_size != NULL)
2544 			*ap->a_size = linux_list_len;
2545 		goto out;
2546 	}
2547 
2548 	/*
2549 	 * Retrieve Linux / FUSE compatible list values.
2550 	 */
2551 	fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2552 	list_xattr_in = fdi.indata;
2553 	list_xattr_in->size = linux_list_len;
2554 
2555 	err = fdisp_wait_answ(&fdi);
2556 	if (err == ERANGE) {
2557 		/*
2558 		 * Race detected.  The attribute list must've grown since the
2559 		 * first FUSE_LISTXATTR call.  Start over.  Go all the way back
2560 		 * to userland so we can process signals, if necessary, before
2561 		 * restarting.
2562 		 */
2563 		err = ERESTART;
2564 		goto out;
2565 	} else if (err != 0)
2566 		goto out;
2567 
2568 	linux_list = fdi.answ;
2569 	/* FUSE doesn't allow the server to return more data than requested */
2570 	if (fdi.iosize > linux_list_len) {
2571 		struct fuse_data *data = fuse_get_mpdata(mp);
2572 
2573 		fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
2574 			"server returned "
2575 			"more extended attribute data than requested; "
2576 			"should've returned ERANGE instead.");
2577 	} else {
2578 		/* But returning less data is fine */
2579 		linux_list_len = fdi.iosize;
2580 	}
2581 
2582 	/*
2583 	 * Retrieve the BSD compatible list values.
2584 	 * The Linux / FUSE attribute list format isn't the same
2585 	 * as FreeBSD's format. So we need to transform it into
2586 	 * FreeBSD's format before giving it to the user.
2587 	 */
2588 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2589 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2590 	    bsd_list, &bsd_list_len);
2591 	if (err != 0)
2592 		goto out;
2593 
2594 	if (ap->a_size != NULL)
2595 		*ap->a_size = bsd_list_len;
2596 
2597 	if (uio != NULL)
2598 		err = uiomove(bsd_list, bsd_list_len, uio);
2599 
2600 out:
2601 	free(bsd_list, M_TEMP);
2602 	fdisp_destroy(&fdi);
2603 	return (err);
2604 }
2605 
2606 /*
2607     struct vop_deleteextattr_args {
2608 	struct vop_generic_args a_gen;
2609 	struct vnode *a_vp;
2610 	int a_attrnamespace;
2611 	const char *a_name;
2612 	struct ucred *a_cred;
2613 	struct thread *a_td;
2614     };
2615 */
2616 static int
2617 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2618 {
2619 	struct vnode *vp = ap->a_vp;
2620 	struct fuse_dispatcher fdi;
2621 	struct mount *mp = vnode_mount(vp);
2622 	struct thread *td = ap->a_td;
2623 	struct ucred *cred = ap->a_cred;
2624 	char *prefix;
2625 	size_t len;
2626 	char *attr_str;
2627 	int err;
2628 
2629 	if (fuse_isdeadfs(vp))
2630 		return (ENXIO);
2631 
2632 	if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2633 		return EOPNOTSUPP;
2634 
2635 	if (vfs_isrdonly(mp))
2636 		return EROFS;
2637 
2638 	err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2639 		VWRITE);
2640 	if (err)
2641 		return err;
2642 
2643 	/* Default to looking for user attributes. */
2644 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2645 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2646 	else
2647 		prefix = EXTATTR_NAMESPACE_USER_STRING;
2648 
2649 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2650 	    strlen(ap->a_name) + 1;
2651 
2652 	fdisp_init(&fdi, len);
2653 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2654 
2655 	attr_str = fdi.indata;
2656 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2657 	    ap->a_name);
2658 
2659 	err = fdisp_wait_answ(&fdi);
2660 	if (err == ENOSYS) {
2661 		fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2662 		err = EOPNOTSUPP;
2663 	}
2664 
2665 	fdisp_destroy(&fdi);
2666 	return (err);
2667 }
2668 
2669 /*
2670     struct vnop_print_args {
2671 	struct vnode *a_vp;
2672     };
2673 */
2674 static int
2675 fuse_vnop_print(struct vop_print_args *ap)
2676 {
2677 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2678 
2679 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2680 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2681 	    (uintmax_t)fvdat->nlookup,
2682 	    fvdat->flag);
2683 
2684 	return 0;
2685 }
2686 
2687 /*
2688  * Get an NFS filehandle for a FUSE file.
2689  *
2690  * This will only work for FUSE file systems that guarantee the uniqueness of
2691  * nodeid:generation, which most don't.
2692  */
2693 /*
2694 vop_vptofh {
2695 	IN struct vnode *a_vp;
2696 	IN struct fid *a_fhp;
2697 };
2698 */
2699 static int
2700 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
2701 {
2702 	struct vnode *vp = ap->a_vp;
2703 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
2704 	struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
2705 	_Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
2706 		"FUSE fid type is too big");
2707 	struct mount *mp = vnode_mount(vp);
2708 	struct fuse_data *data = fuse_get_mpdata(mp);
2709 	struct vattr va;
2710 	int err;
2711 
2712 	if (!(data->dataflags & FSESS_EXPORT_SUPPORT))
2713 		return EOPNOTSUPP;
2714 
2715 	err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
2716 	if (err)
2717 		return err;
2718 
2719 	/*ip = VTOI(ap->a_vp);*/
2720 	/*ufhp = (struct ufid *)ap->a_fhp;*/
2721 	fhp->len = sizeof(struct fuse_fid);
2722 	fhp->nid = fvdat->nid;
2723 	if (fvdat->generation <= UINT32_MAX)
2724 		fhp->gen = fvdat->generation;
2725 	else
2726 		return EOVERFLOW;
2727 	return (0);
2728 }
2729