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