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