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