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