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