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