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