xref: /freebsd/sys/fs/fuse/fuse_internal.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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/systm.h>
68 #include <sys/counter.h>
69 #include <sys/module.h>
70 #include <sys/errno.h>
71 #include <sys/kernel.h>
72 #include <sys/conf.h>
73 #include <sys/uio.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
76 #include <sys/lock.h>
77 #include <sys/mutex.h>
78 #include <sys/sdt.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/stat.h>
85 #include <sys/unistd.h>
86 #include <sys/filedesc.h>
87 #include <sys/file.h>
88 #include <sys/fcntl.h>
89 #include <sys/dirent.h>
90 #include <sys/bio.h>
91 #include <sys/buf.h>
92 #include <sys/sysctl.h>
93 #include <sys/priv.h>
94 
95 #include "fuse.h"
96 #include "fuse_file.h"
97 #include "fuse_internal.h"
98 #include "fuse_io.h"
99 #include "fuse_ipc.h"
100 #include "fuse_node.h"
101 #include "fuse_file.h"
102 
103 SDT_PROVIDER_DECLARE(fusefs);
104 /*
105  * Fuse trace probe:
106  * arg0: verbosity.  Higher numbers give more verbose messages
107  * arg1: Textual message
108  */
109 SDT_PROBE_DEFINE2(fusefs, , internal, trace, "int", "char*");
110 
111 #ifdef ZERO_PAD_INCOMPLETE_BUFS
112 static int isbzero(void *buf, size_t len);
113 
114 #endif
115 
116 counter_u64_t fuse_lookup_cache_hits;
117 counter_u64_t fuse_lookup_cache_misses;
118 
119 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
120     &fuse_lookup_cache_hits, "number of positive cache hits in lookup");
121 
122 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
123     &fuse_lookup_cache_misses, "number of cache misses in lookup");
124 
125 int
126 fuse_internal_get_cached_vnode(struct mount* mp, ino_t ino, int flags,
127 	struct vnode **vpp)
128 {
129 	struct bintime now;
130 	struct thread *td = curthread;
131 	uint64_t nodeid = ino;
132 	int error;
133 
134 	*vpp = NULL;
135 
136 	error = vfs_hash_get(mp, fuse_vnode_hash(nodeid), flags, td, vpp,
137 	    fuse_vnode_cmp, &nodeid);
138 	if (error)
139 		return error;
140 	/*
141 	 * Check the entry cache timeout.  We have to do this within fusefs
142 	 * instead of by using cache_enter_time/cache_lookup because those
143 	 * routines are only intended to work with pathnames, not inodes
144 	 */
145 	if (*vpp != NULL) {
146 		getbinuptime(&now);
147 		if (bintime_cmp(&(VTOFUD(*vpp)->entry_cache_timeout), &now, >)){
148 			counter_u64_add(fuse_lookup_cache_hits, 1);
149 			return 0;
150 		} else {
151 			/* Entry cache timeout */
152 			counter_u64_add(fuse_lookup_cache_misses, 1);
153 			cache_purge(*vpp);
154 			vput(*vpp);
155 			*vpp = NULL;
156 		}
157 	}
158 	return 0;
159 }
160 
161 SDT_PROBE_DEFINE0(fusefs, , internal, access_vadmin);
162 /* Synchronously send a FUSE_ACCESS operation */
163 int
164 fuse_internal_access(struct vnode *vp,
165     accmode_t mode,
166     struct thread *td,
167     struct ucred *cred)
168 {
169 	int err = 0;
170 	uint32_t mask = F_OK;
171 	int dataflags;
172 	int vtype;
173 	struct mount *mp;
174 	struct fuse_dispatcher fdi;
175 	struct fuse_access_in *fai;
176 	struct fuse_data *data;
177 
178 	mp = vnode_mount(vp);
179 	vtype = vnode_vtype(vp);
180 
181 	data = fuse_get_mpdata(mp);
182 	dataflags = data->dataflags;
183 
184 	if (mode == 0)
185 		return 0;
186 
187 	if (mode & VMODIFY_PERMS && vfs_isrdonly(mp)) {
188 		switch (vp->v_type) {
189 		case VDIR:
190 			/* FALLTHROUGH */
191 		case VLNK:
192 			/* FALLTHROUGH */
193 		case VREG:
194 			return EROFS;
195 		default:
196 			break;
197 		}
198 	}
199 
200 	/* Unless explicitly permitted, deny everyone except the fs owner. */
201 	if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
202 		if (fuse_match_cred(data->daemoncred, cred))
203 			return EPERM;
204 	}
205 
206 	if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
207 		struct vattr va;
208 
209 		fuse_internal_getattr(vp, &va, cred, td);
210 		return vaccess(vp->v_type, va.va_mode, va.va_uid,
211 		    va.va_gid, mode, cred);
212 	}
213 
214 	if (mode & VADMIN) {
215 		/*
216 		 * The FUSE protocol doesn't have an equivalent of VADMIN, so
217 		 * it's a bug if we ever reach this point with that bit set.
218 		 */
219 		SDT_PROBE0(fusefs, , internal, access_vadmin);
220 	}
221 
222 	if (fsess_not_impl(mp, FUSE_ACCESS))
223 		return 0;
224 
225 	if ((mode & (VWRITE | VAPPEND)) != 0)
226 		mask |= W_OK;
227 	if ((mode & VREAD) != 0)
228 		mask |= R_OK;
229 	if ((mode & VEXEC) != 0)
230 		mask |= X_OK;
231 
232 	fdisp_init(&fdi, sizeof(*fai));
233 	fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
234 
235 	fai = fdi.indata;
236 	fai->mask = mask;
237 
238 	err = fdisp_wait_answ(&fdi);
239 	fdisp_destroy(&fdi);
240 
241 	if (err == ENOSYS) {
242 		fsess_set_notimpl(mp, FUSE_ACCESS);
243 		err = 0;
244 	}
245 	return err;
246 }
247 
248 /*
249  * Cache FUSE attributes from attr, in attribute cache associated with vnode
250  * 'vp'.  Optionally, if argument 'vap' is not NULL, store a copy of the
251  * converted attributes there as well.
252  *
253  * If the nominal attribute cache TTL is zero, do not cache on the 'vp' (but do
254  * return the result to the caller).
255  */
256 void
257 fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
258 	uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap)
259 {
260 	struct mount *mp;
261 	struct fuse_vnode_data *fvdat;
262 	struct fuse_data *data;
263 	struct vattr *vp_cache_at;
264 
265 	mp = vnode_mount(vp);
266 	fvdat = VTOFUD(vp);
267 	data = fuse_get_mpdata(mp);
268 
269 	ASSERT_VOP_ELOCKED(vp, "fuse_internal_cache_attrs");
270 
271 	fuse_validity_2_bintime(attr_valid, attr_valid_nsec,
272 		&fvdat->attr_cache_timeout);
273 
274 	/* Fix our buffers if the filesize changed without us knowing */
275 	if (vnode_isreg(vp) && attr->size != fvdat->cached_attrs.va_size) {
276 		(void)fuse_vnode_setsize(vp, attr->size);
277 		fvdat->cached_attrs.va_size = attr->size;
278 	}
279 
280 	if (attr_valid > 0 || attr_valid_nsec > 0)
281 		vp_cache_at = &(fvdat->cached_attrs);
282 	else if (vap != NULL)
283 		vp_cache_at = vap;
284 	else
285 		return;
286 
287 	vattr_null(vp_cache_at);
288 	vp_cache_at->va_fsid = mp->mnt_stat.f_fsid.val[0];
289 	vp_cache_at->va_fileid = attr->ino;
290 	vp_cache_at->va_mode = attr->mode & ~S_IFMT;
291 	vp_cache_at->va_nlink     = attr->nlink;
292 	vp_cache_at->va_uid       = attr->uid;
293 	vp_cache_at->va_gid       = attr->gid;
294 	vp_cache_at->va_rdev      = attr->rdev;
295 	vp_cache_at->va_size      = attr->size;
296 	/* XXX on i386, seconds are truncated to 32 bits */
297 	vp_cache_at->va_atime.tv_sec  = attr->atime;
298 	vp_cache_at->va_atime.tv_nsec = attr->atimensec;
299 	vp_cache_at->va_mtime.tv_sec  = attr->mtime;
300 	vp_cache_at->va_mtime.tv_nsec = attr->mtimensec;
301 	vp_cache_at->va_ctime.tv_sec  = attr->ctime;
302 	vp_cache_at->va_ctime.tv_nsec = attr->ctimensec;
303 	if (fuse_libabi_geq(data, 7, 9) && attr->blksize > 0)
304 		vp_cache_at->va_blocksize = attr->blksize;
305 	else
306 		vp_cache_at->va_blocksize = PAGE_SIZE;
307 	vp_cache_at->va_type = IFTOVT(attr->mode);
308 	vp_cache_at->va_bytes = attr->blocks * S_BLKSIZE;
309 	vp_cache_at->va_flags = 0;
310 
311 	if (vap != vp_cache_at && vap != NULL)
312 		memcpy(vap, vp_cache_at, sizeof(*vap));
313 }
314 
315 /* fsync */
316 
317 int
318 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
319 {
320 	if (tick->tk_aw_ohead.error == ENOSYS) {
321 		fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
322 	}
323 	return 0;
324 }
325 
326 int
327 fuse_internal_fsync(struct vnode *vp,
328     struct thread *td,
329     int waitfor,
330     bool datasync)
331 {
332 	struct fuse_fsync_in *ffsi = NULL;
333 	struct fuse_dispatcher fdi;
334 	struct fuse_filehandle *fufh;
335 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
336 	struct mount *mp = vnode_mount(vp);
337 	int op = FUSE_FSYNC;
338 	int err = 0;
339 
340 	if (fsess_not_impl(vnode_mount(vp),
341 	    (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
342 		return 0;
343 	}
344 	if (vnode_isdir(vp))
345 		op = FUSE_FSYNCDIR;
346 
347 	if (fsess_not_impl(mp, op))
348 		return 0;
349 
350 	fdisp_init(&fdi, sizeof(*ffsi));
351 	/*
352 	 * fsync every open file handle for this file, because we can't be sure
353 	 * which file handle the caller is really referring to.
354 	 */
355 	LIST_FOREACH(fufh, &fvdat->handles, next) {
356 		fdi.iosize = sizeof(*ffsi);
357 		if (ffsi == NULL)
358 			fdisp_make_vp(&fdi, op, vp, td, NULL);
359 		else
360 			fdisp_refresh_vp(&fdi, op, vp, td, NULL);
361 		ffsi = fdi.indata;
362 		ffsi->fh = fufh->fh_id;
363 		ffsi->fsync_flags = 0;
364 
365 		if (datasync)
366 			ffsi->fsync_flags = 1;
367 
368 		if (waitfor == MNT_WAIT) {
369 			err = fdisp_wait_answ(&fdi);
370 		} else {
371 			fuse_insert_callback(fdi.tick,
372 				fuse_internal_fsync_callback);
373 			fuse_insert_message(fdi.tick, false);
374 		}
375 		if (err == ENOSYS) {
376 			/* ENOSYS means "success, and don't call again" */
377 			fsess_set_notimpl(mp, op);
378 			err = 0;
379 			break;
380 		}
381 	}
382 	fdisp_destroy(&fdi);
383 
384 	return err;
385 }
386 
387 /* Asynchronous invalidation */
388 SDT_PROBE_DEFINE3(fusefs, , internal, invalidate_entry,
389 	"struct vnode*", "struct fuse_notify_inval_entry_out*", "char*");
390 int
391 fuse_internal_invalidate_entry(struct mount *mp, struct uio *uio)
392 {
393 	struct fuse_notify_inval_entry_out fnieo;
394 	struct componentname cn;
395 	struct vnode *dvp, *vp;
396 	char name[PATH_MAX];
397 	int err;
398 
399 	if ((err = uiomove(&fnieo, sizeof(fnieo), uio)) != 0)
400 		return (err);
401 
402 	if (fnieo.namelen >= sizeof(name))
403 		return (EINVAL);
404 
405 	if ((err = uiomove(name, fnieo.namelen, uio)) != 0)
406 		return (err);
407 	name[fnieo.namelen] = '\0';
408 	/* fusefs does not cache "." or ".." entries */
409 	if (strncmp(name, ".", sizeof(".")) == 0 ||
410 	    strncmp(name, "..", sizeof("..")) == 0)
411 		return (0);
412 
413 	if (fnieo.parent == FUSE_ROOT_ID)
414 		err = VFS_ROOT(mp, LK_SHARED, &dvp);
415 	else
416 		err = fuse_internal_get_cached_vnode( mp, fnieo.parent,
417 			LK_SHARED, &dvp);
418 	SDT_PROBE3(fusefs, , internal, invalidate_entry, dvp, &fnieo, name);
419 	/*
420 	 * If dvp is not in the cache, then it must've been reclaimed.  And
421 	 * since fuse_vnop_reclaim does a cache_purge, name's entry must've
422 	 * been invalidated already.  So we can safely return if dvp == NULL
423 	 */
424 	if (err != 0 || dvp == NULL)
425 		return (err);
426 	/*
427 	 * XXX we can't check dvp's generation because the FUSE invalidate
428 	 * entry message doesn't include it.  Worse case is that we invalidate
429 	 * an entry that didn't need to be invalidated.
430 	 */
431 
432 	cn.cn_nameiop = LOOKUP;
433 	cn.cn_flags = 0;	/* !MAKEENTRY means free cached entry */
434 	cn.cn_thread = curthread;
435 	cn.cn_cred = curthread->td_ucred;
436 	cn.cn_lkflags = LK_SHARED;
437 	cn.cn_pnbuf = NULL;
438 	cn.cn_nameptr = name;
439 	cn.cn_namelen = fnieo.namelen;
440 	err = cache_lookup(dvp, &vp, &cn, NULL, NULL);
441 	MPASS(err == 0);
442 	fuse_vnode_clear_attr_cache(dvp);
443 	vput(dvp);
444 	return (0);
445 }
446 
447 SDT_PROBE_DEFINE2(fusefs, , internal, invalidate_inode,
448 	"struct vnode*", "struct fuse_notify_inval_inode_out *");
449 int
450 fuse_internal_invalidate_inode(struct mount *mp, struct uio *uio)
451 {
452 	struct fuse_notify_inval_inode_out fniio;
453 	struct vnode *vp;
454 	int err;
455 
456 	if ((err = uiomove(&fniio, sizeof(fniio), uio)) != 0)
457 		return (err);
458 
459 	if (fniio.ino == FUSE_ROOT_ID)
460 		err = VFS_ROOT(mp, LK_EXCLUSIVE, &vp);
461 	else
462 		err = fuse_internal_get_cached_vnode(mp, fniio.ino, LK_SHARED,
463 			&vp);
464 	SDT_PROBE2(fusefs, , internal, invalidate_inode, vp, &fniio);
465 	if (err != 0 || vp == NULL)
466 		return (err);
467 	/*
468 	 * XXX we can't check vp's generation because the FUSE invalidate
469 	 * entry message doesn't include it.  Worse case is that we invalidate
470 	 * an inode that didn't need to be invalidated.
471 	 */
472 
473 	/*
474 	 * Flush and invalidate buffers if off >= 0.  Technically we only need
475 	 * to flush and invalidate the range of offsets [off, off + len), but
476 	 * for simplicity's sake we do everything.
477 	 */
478 	if (fniio.off >= 0)
479 		fuse_io_invalbuf(vp, curthread);
480 	fuse_vnode_clear_attr_cache(vp);
481 	vput(vp);
482 	return (0);
483 }
484 
485 /* mknod */
486 int
487 fuse_internal_mknod(struct vnode *dvp, struct vnode **vpp,
488 	struct componentname *cnp, struct vattr *vap)
489 {
490 	struct fuse_data *data;
491 	struct fuse_mknod_in fmni;
492 	size_t insize;
493 
494 	data = fuse_get_mpdata(dvp->v_mount);
495 
496 	fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
497 	fmni.rdev = vap->va_rdev;
498 	if (fuse_libabi_geq(data, 7, 12)) {
499 		insize = sizeof(fmni);
500 		fmni.umask = curthread->td_proc->p_pd->pd_cmask;
501 	} else {
502 		insize = FUSE_COMPAT_MKNOD_IN_SIZE;
503 	}
504 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
505 	    insize, vap->va_type));
506 }
507 
508 /* readdir */
509 
510 int
511 fuse_internal_readdir(struct vnode *vp,
512     struct uio *uio,
513     off_t startoff,
514     struct fuse_filehandle *fufh,
515     struct fuse_iov *cookediov,
516     int *ncookies,
517     u_long *cookies)
518 {
519 	int err = 0;
520 	struct fuse_dispatcher fdi;
521 	struct fuse_read_in *fri = NULL;
522 	int fnd_start;
523 
524 	if (uio_resid(uio) == 0)
525 		return 0;
526 	fdisp_init(&fdi, 0);
527 
528 	/*
529 	 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
530 	 * I/O).
531 	 */
532 
533 	/*
534 	 * fnd_start is set non-zero once the offset in the directory gets
535 	 * to the startoff.  This is done because directories must be read
536 	 * from the beginning (offset == 0) when fuse_vnop_readdir() needs
537 	 * to do an open of the directory.
538 	 * If it is not set non-zero here, it will be set non-zero in
539 	 * fuse_internal_readdir_processdata() when uio_offset == startoff.
540 	 */
541 	fnd_start = 0;
542 	if (uio->uio_offset == startoff)
543 		fnd_start = 1;
544 	while (uio_resid(uio) > 0) {
545 		fdi.iosize = sizeof(*fri);
546 		if (fri == NULL)
547 			fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
548 		else
549 			fdisp_refresh_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
550 
551 		fri = fdi.indata;
552 		fri->fh = fufh->fh_id;
553 		fri->offset = uio_offset(uio);
554 		fri->size = MIN(uio->uio_resid,
555 		    fuse_get_mpdata(vp->v_mount)->max_read);
556 
557 		if ((err = fdisp_wait_answ(&fdi)))
558 			break;
559 		if ((err = fuse_internal_readdir_processdata(uio, startoff,
560 		    &fnd_start, fri->size, fdi.answ, fdi.iosize, cookediov,
561 		    ncookies, &cookies)))
562 			break;
563 	}
564 
565 	fdisp_destroy(&fdi);
566 	return ((err == -1) ? 0 : err);
567 }
568 
569 /*
570  * Return -1 to indicate that this readdir is finished, 0 if it copied
571  * all the directory data read in and it may be possible to read more
572  * and greater than 0 for a failure.
573  */
574 int
575 fuse_internal_readdir_processdata(struct uio *uio,
576     off_t startoff,
577     int *fnd_start,
578     size_t reqsize,
579     void *buf,
580     size_t bufsize,
581     struct fuse_iov *cookediov,
582     int *ncookies,
583     u_long **cookiesp)
584 {
585 	int err = 0;
586 	int bytesavail;
587 	size_t freclen;
588 
589 	struct dirent *de;
590 	struct fuse_dirent *fudge;
591 	u_long *cookies;
592 
593 	cookies = *cookiesp;
594 	if (bufsize < FUSE_NAME_OFFSET)
595 		return -1;
596 	for (;;) {
597 		if (bufsize < FUSE_NAME_OFFSET) {
598 			err = -1;
599 			break;
600 		}
601 		fudge = (struct fuse_dirent *)buf;
602 		freclen = FUSE_DIRENT_SIZE(fudge);
603 
604 		if (bufsize < freclen) {
605 			/*
606 			 * This indicates a partial directory entry at the
607 			 * end of the directory data.
608 			 */
609 			err = -1;
610 			break;
611 		}
612 #ifdef ZERO_PAD_INCOMPLETE_BUFS
613 		if (isbzero(buf, FUSE_NAME_OFFSET)) {
614 			err = -1;
615 			break;
616 		}
617 #endif
618 
619 		if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
620 			err = EINVAL;
621 			break;
622 		}
623 		bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
624 					    &fudge->namelen);
625 
626 		if (bytesavail > uio_resid(uio)) {
627 			/* Out of space for the dir so we are done. */
628 			err = -1;
629 			break;
630 		}
631 		/*
632 		 * Don't start to copy the directory entries out until
633 		 * the requested offset in the directory is found.
634 		 */
635 		if (*fnd_start != 0) {
636 			fiov_adjust(cookediov, bytesavail);
637 			bzero(cookediov->base, bytesavail);
638 
639 			de = (struct dirent *)cookediov->base;
640 			de->d_fileno = fudge->ino;
641 			de->d_reclen = bytesavail;
642 			de->d_type = fudge->type;
643 			de->d_namlen = fudge->namelen;
644 			memcpy((char *)cookediov->base + sizeof(struct dirent) -
645 			       MAXNAMLEN - 1,
646 			       (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
647 			dirent_terminate(de);
648 
649 			err = uiomove(cookediov->base, cookediov->len, uio);
650 			if (err)
651 				break;
652 			if (cookies != NULL) {
653 				if (*ncookies == 0) {
654 					err = -1;
655 					break;
656 				}
657 				*cookies = fudge->off;
658 				cookies++;
659 				(*ncookies)--;
660 			}
661 		} else if (startoff == fudge->off)
662 			*fnd_start = 1;
663 		buf = (char *)buf + freclen;
664 		bufsize -= freclen;
665 		uio_setoffset(uio, fudge->off);
666 	}
667 	*cookiesp = cookies;
668 
669 	return err;
670 }
671 
672 /* remove */
673 
674 int
675 fuse_internal_remove(struct vnode *dvp,
676     struct vnode *vp,
677     struct componentname *cnp,
678     enum fuse_opcode op)
679 {
680 	struct fuse_dispatcher fdi;
681 	nlink_t nlink;
682 	int err = 0;
683 
684 	fdisp_init(&fdi, cnp->cn_namelen + 1);
685 	fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
686 
687 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
688 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
689 
690 	err = fdisp_wait_answ(&fdi);
691 	fdisp_destroy(&fdi);
692 
693 	if (err)
694 		return (err);
695 
696 	/*
697 	 * Access the cached nlink even if the attr cached has expired.  If
698 	 * it's inaccurate, the worst that will happen is:
699 	 * 1) We'll recycle the vnode even though the file has another link we
700 	 *    don't know about, costing a bit of cpu time, or
701 	 * 2) We won't recycle the vnode even though all of its links are gone.
702 	 *    It will linger around until vnlru reclaims it, costing a bit of
703 	 *    temporary memory.
704 	 */
705 	nlink = VTOFUD(vp)->cached_attrs.va_nlink--;
706 
707 	/*
708 	 * Purge the parent's attribute cache because the daemon
709 	 * should've updated its mtime and ctime.
710 	 */
711 	fuse_vnode_clear_attr_cache(dvp);
712 
713 	/* NB: nlink could be zero if it was never cached */
714 	if (nlink <= 1 || vnode_vtype(vp) == VDIR) {
715 		fuse_internal_vnode_disappear(vp);
716 	} else {
717 		cache_purge(vp);
718 		fuse_vnode_update(vp, FN_CTIMECHANGE);
719 	}
720 
721 	return err;
722 }
723 
724 /* rename */
725 
726 int
727 fuse_internal_rename(struct vnode *fdvp,
728     struct componentname *fcnp,
729     struct vnode *tdvp,
730     struct componentname *tcnp)
731 {
732 	struct fuse_dispatcher fdi;
733 	struct fuse_rename_in *fri;
734 	int err = 0;
735 
736 	fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
737 	fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
738 
739 	fri = fdi.indata;
740 	fri->newdir = VTOI(tdvp);
741 	memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
742 	    fcnp->cn_namelen);
743 	((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
744 	memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
745 	    tcnp->cn_nameptr, tcnp->cn_namelen);
746 	((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
747 	    tcnp->cn_namelen + 1] = '\0';
748 
749 	err = fdisp_wait_answ(&fdi);
750 	fdisp_destroy(&fdi);
751 	return err;
752 }
753 
754 /* strategy */
755 
756 /* entity creation */
757 
758 void
759 fuse_internal_newentry_makerequest(struct mount *mp,
760     uint64_t dnid,
761     struct componentname *cnp,
762     enum fuse_opcode op,
763     void *buf,
764     size_t bufsize,
765     struct fuse_dispatcher *fdip)
766 {
767 	fdip->iosize = bufsize + cnp->cn_namelen + 1;
768 
769 	fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
770 	memcpy(fdip->indata, buf, bufsize);
771 	memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
772 	((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
773 }
774 
775 int
776 fuse_internal_newentry_core(struct vnode *dvp,
777     struct vnode **vpp,
778     struct componentname *cnp,
779     enum vtype vtyp,
780     struct fuse_dispatcher *fdip)
781 {
782 	int err = 0;
783 	struct fuse_entry_out *feo;
784 	struct mount *mp = vnode_mount(dvp);
785 
786 	if ((err = fdisp_wait_answ(fdip))) {
787 		return err;
788 	}
789 	feo = fdip->answ;
790 
791 	if ((err = fuse_internal_checkentry(feo, vtyp))) {
792 		return err;
793 	}
794 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp);
795 	if (err) {
796 		fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
797 		    feo->nodeid, 1);
798 		return err;
799 	}
800 
801 	/*
802 	 * Purge the parent's attribute cache because the daemon should've
803 	 * updated its mtime and ctime
804 	 */
805 	fuse_vnode_clear_attr_cache(dvp);
806 
807 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
808 		feo->attr_valid_nsec, NULL);
809 
810 	return err;
811 }
812 
813 int
814 fuse_internal_newentry(struct vnode *dvp,
815     struct vnode **vpp,
816     struct componentname *cnp,
817     enum fuse_opcode op,
818     void *buf,
819     size_t bufsize,
820     enum vtype vtype)
821 {
822 	int err;
823 	struct fuse_dispatcher fdi;
824 	struct mount *mp = vnode_mount(dvp);
825 
826 	fdisp_init(&fdi, 0);
827 	fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
828 	    bufsize, &fdi);
829 	err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
830 	fdisp_destroy(&fdi);
831 
832 	return err;
833 }
834 
835 /* entity destruction */
836 
837 int
838 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
839 {
840 	fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
841 	    ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
842 
843 	return 0;
844 }
845 
846 void
847 fuse_internal_forget_send(struct mount *mp,
848     struct thread *td,
849     struct ucred *cred,
850     uint64_t nodeid,
851     uint64_t nlookup)
852 {
853 
854 	struct fuse_dispatcher fdi;
855 	struct fuse_forget_in *ffi;
856 
857 	/*
858          * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
859          *         (long long unsigned) nodeid));
860          */
861 
862 	fdisp_init(&fdi, sizeof(*ffi));
863 	fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
864 
865 	ffi = fdi.indata;
866 	ffi->nlookup = nlookup;
867 
868 	fuse_insert_message(fdi.tick, false);
869 	fdisp_destroy(&fdi);
870 }
871 
872 SDT_PROBE_DEFINE2(fusefs, , internal, getattr_cache_incoherent,
873 	"struct vnode*", "struct fuse_attr_out*");
874 
875 /* Fetch the vnode's attributes from the daemon*/
876 int
877 fuse_internal_do_getattr(struct vnode *vp, struct vattr *vap,
878 	struct ucred *cred, struct thread *td)
879 {
880 	struct fuse_dispatcher fdi;
881 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
882 	struct fuse_getattr_in *fgai;
883 	struct fuse_attr_out *fao;
884 	off_t old_filesize = fvdat->cached_attrs.va_size;
885 	struct timespec old_ctime = fvdat->cached_attrs.va_ctime;
886 	struct timespec old_mtime = fvdat->cached_attrs.va_mtime;
887 	enum vtype vtyp;
888 	int err;
889 
890 	fdisp_init(&fdi, sizeof(*fgai));
891 	fdisp_make_vp(&fdi, FUSE_GETATTR, vp, td, cred);
892 	fgai = fdi.indata;
893 	/*
894 	 * We could look up a file handle and set it in fgai->fh, but that
895 	 * involves extra runtime work and I'm unaware of any file systems that
896 	 * care.
897 	 */
898 	fgai->getattr_flags = 0;
899 	if ((err = fdisp_wait_answ(&fdi))) {
900 		if (err == ENOENT)
901 			fuse_internal_vnode_disappear(vp);
902 		goto out;
903 	}
904 
905 	fao = (struct fuse_attr_out *)fdi.answ;
906 	vtyp = IFTOVT(fao->attr.mode);
907 	if (fvdat->flag & FN_SIZECHANGE)
908 		fao->attr.size = old_filesize;
909 	if (fvdat->flag & FN_CTIMECHANGE) {
910 		fao->attr.ctime = old_ctime.tv_sec;
911 		fao->attr.ctimensec = old_ctime.tv_nsec;
912 	}
913 	if (fvdat->flag & FN_MTIMECHANGE) {
914 		fao->attr.mtime = old_mtime.tv_sec;
915 		fao->attr.mtimensec = old_mtime.tv_nsec;
916 	}
917 	if (vnode_isreg(vp) &&
918 	    fvdat->cached_attrs.va_size != VNOVAL &&
919 	    fao->attr.size != fvdat->cached_attrs.va_size) {
920 		/*
921 		 * The server changed the file's size even though we had it
922 		 * cached!  That's a server bug.
923 		 */
924 		SDT_PROBE2(fusefs, , internal, getattr_cache_incoherent, vp,
925 		    fao);
926 		printf("%s: cache incoherent on %s!  "
927 		    "Buggy FUSE server detected.  To prevent data corruption, "
928 		    "disable the data cache by mounting with -o direct_io, or "
929 		    "as directed otherwise by your FUSE server's "
930 		    "documentation\n", __func__,
931 		    vnode_mount(vp)->mnt_stat.f_mntonname);
932 		int iosize = fuse_iosize(vp);
933 		v_inval_buf_range(vp, 0, INT64_MAX, iosize);
934 	}
935 	fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
936 		fao->attr_valid_nsec, vap);
937 	if (vtyp != vnode_vtype(vp)) {
938 		fuse_internal_vnode_disappear(vp);
939 		err = ENOENT;
940 	}
941 
942 out:
943 	fdisp_destroy(&fdi);
944 	return err;
945 }
946 
947 /* Read a vnode's attributes from cache or fetch them from the fuse daemon */
948 int
949 fuse_internal_getattr(struct vnode *vp, struct vattr *vap, struct ucred *cred,
950 	struct thread *td)
951 {
952 	struct vattr *attrs;
953 
954 	if ((attrs = VTOVA(vp)) != NULL) {
955 		*vap = *attrs;	/* struct copy */
956 		return 0;
957 	}
958 
959 	return fuse_internal_do_getattr(vp, vap, cred, td);
960 }
961 
962 void
963 fuse_internal_vnode_disappear(struct vnode *vp)
964 {
965 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
966 
967 	ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
968 	fvdat->flag |= FN_REVOKED;
969 	cache_purge(vp);
970 }
971 
972 /* fuse start/stop */
973 
974 SDT_PROBE_DEFINE2(fusefs, , internal, init_done,
975 	"struct fuse_data*", "struct fuse_init_out*");
976 int
977 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
978 {
979 	int err = 0;
980 	struct fuse_data *data = tick->tk_data;
981 	struct fuse_init_out *fiio;
982 
983 	if ((err = tick->tk_aw_ohead.error)) {
984 		goto out;
985 	}
986 	if ((err = fticket_pull(tick, uio))) {
987 		goto out;
988 	}
989 	fiio = fticket_resp(tick)->base;
990 
991 	data->fuse_libabi_major = fiio->major;
992 	data->fuse_libabi_minor = fiio->minor;
993 	if (!fuse_libabi_geq(data, 7, 4)) {
994 		/*
995 		 * With a little work we could support servers as old as 7.1.
996 		 * But there would be little payoff.
997 		 */
998 		SDT_PROBE2(fusefs, , internal, trace, 1,
999 			"userpace version too low");
1000 		err = EPROTONOSUPPORT;
1001 		goto out;
1002 	}
1003 
1004 	if (fuse_libabi_geq(data, 7, 5)) {
1005 		if (fticket_resp(tick)->len == sizeof(struct fuse_init_out) ||
1006 		    fticket_resp(tick)->len == FUSE_COMPAT_22_INIT_OUT_SIZE) {
1007 			data->max_write = fiio->max_write;
1008 			if (fiio->flags & FUSE_ASYNC_READ)
1009 				data->dataflags |= FSESS_ASYNC_READ;
1010 			if (fiio->flags & FUSE_POSIX_LOCKS)
1011 				data->dataflags |= FSESS_POSIX_LOCKS;
1012 			if (fiio->flags & FUSE_EXPORT_SUPPORT)
1013 				data->dataflags |= FSESS_EXPORT_SUPPORT;
1014 			/*
1015 			 * Don't bother to check FUSE_BIG_WRITES, because it's
1016 			 * redundant with max_write
1017 			 */
1018 			/*
1019 			 * max_background and congestion_threshold are not
1020 			 * implemented
1021 			 */
1022 		} else {
1023 			err = EINVAL;
1024 		}
1025 	} else {
1026 		/* Old fixed values */
1027 		data->max_write = 4096;
1028 	}
1029 
1030 	if (fuse_libabi_geq(data, 7, 6))
1031 		data->max_readahead_blocks = fiio->max_readahead / maxbcachebuf;
1032 
1033 	if (!fuse_libabi_geq(data, 7, 7))
1034 		fsess_set_notimpl(data->mp, FUSE_INTERRUPT);
1035 
1036 	if (!fuse_libabi_geq(data, 7, 8)) {
1037 		fsess_set_notimpl(data->mp, FUSE_BMAP);
1038 		fsess_set_notimpl(data->mp, FUSE_DESTROY);
1039 	}
1040 
1041 	if (fuse_libabi_geq(data, 7, 23) && fiio->time_gran >= 1 &&
1042 	    fiio->time_gran <= 1000000000)
1043 		data->time_gran = fiio->time_gran;
1044 	else
1045 		data->time_gran = 1;
1046 
1047 	if (!fuse_libabi_geq(data, 7, 23))
1048 		data->cache_mode = fuse_data_cache_mode;
1049 	else if (fiio->flags & FUSE_WRITEBACK_CACHE)
1050 		data->cache_mode = FUSE_CACHE_WB;
1051 	else
1052 		data->cache_mode = FUSE_CACHE_WT;
1053 
1054 	if (!fuse_libabi_geq(data, 7, 24))
1055 		fsess_set_notimpl(data->mp, FUSE_LSEEK);
1056 
1057 	if (!fuse_libabi_geq(data, 7, 28))
1058 		fsess_set_notimpl(data->mp, FUSE_COPY_FILE_RANGE);
1059 
1060 out:
1061 	if (err) {
1062 		fdata_set_dead(data);
1063 	}
1064 	FUSE_LOCK();
1065 	data->dataflags |= FSESS_INITED;
1066 	SDT_PROBE2(fusefs, , internal, init_done, data, fiio);
1067 	wakeup(&data->ticketer);
1068 	FUSE_UNLOCK();
1069 
1070 	return 0;
1071 }
1072 
1073 void
1074 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
1075 {
1076 	struct fuse_init_in *fiii;
1077 	struct fuse_dispatcher fdi;
1078 
1079 	fdisp_init(&fdi, sizeof(*fiii));
1080 	fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
1081 	fiii = fdi.indata;
1082 	fiii->major = FUSE_KERNEL_VERSION;
1083 	fiii->minor = FUSE_KERNEL_MINOR_VERSION;
1084 	/*
1085 	 * fusefs currently reads ahead no more than one cache block at a time.
1086 	 * See fuse_read_biobackend
1087 	 */
1088 	fiii->max_readahead = maxbcachebuf;
1089 	/*
1090 	 * Unsupported features:
1091 	 * FUSE_FILE_OPS: No known FUSE server or client supports it
1092 	 * FUSE_ATOMIC_O_TRUNC: our VFS cannot support it
1093 	 * FUSE_DONT_MASK: unlike Linux, FreeBSD always applies the umask, even
1094 	 *	when default ACLs are in use.
1095 	 * FUSE_SPLICE_WRITE, FUSE_SPLICE_MOVE, FUSE_SPLICE_READ: FreeBSD
1096 	 *	doesn't have splice(2).
1097 	 * FUSE_FLOCK_LOCKS: not yet implemented
1098 	 * FUSE_HAS_IOCTL_DIR: not yet implemented
1099 	 * FUSE_AUTO_INVAL_DATA: not yet implemented
1100 	 * FUSE_DO_READDIRPLUS: not yet implemented
1101 	 * FUSE_READDIRPLUS_AUTO: not yet implemented
1102 	 * FUSE_ASYNC_DIO: not yet implemented
1103 	 * FUSE_NO_OPEN_SUPPORT: not yet implemented
1104 	 * FUSE_PARALLEL_DIROPS: not yet implemented
1105 	 * FUSE_HANDLE_KILLPRIV: not yet implemented
1106 	 * FUSE_POSIX_ACL: not yet implemented
1107 	 * FUSE_ABORT_ERROR: not yet implemented
1108 	 * FUSE_CACHE_SYMLINKS: not yet implemented
1109 	 * FUSE_MAX_PAGES: not yet implemented
1110 	 */
1111 	fiii->flags = FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_EXPORT_SUPPORT
1112 		| FUSE_BIG_WRITES | FUSE_WRITEBACK_CACHE;
1113 
1114 	fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
1115 	fuse_insert_message(fdi.tick, false);
1116 	fdisp_destroy(&fdi);
1117 }
1118 
1119 /*
1120  * Send a FUSE_SETATTR operation with no permissions checks.  If cred is NULL,
1121  * send the request with root credentials
1122  */
1123 int fuse_internal_setattr(struct vnode *vp, struct vattr *vap,
1124 	struct thread *td, struct ucred *cred)
1125 {
1126 	struct fuse_vnode_data *fvdat;
1127 	struct fuse_dispatcher fdi;
1128 	struct fuse_setattr_in *fsai;
1129 	struct mount *mp;
1130 	pid_t pid = td->td_proc->p_pid;
1131 	struct fuse_data *data;
1132 	int dataflags;
1133 	int err = 0;
1134 	enum vtype vtyp;
1135 	int sizechanged = -1;
1136 	uint64_t newsize = 0;
1137 
1138 	mp = vnode_mount(vp);
1139 	fvdat = VTOFUD(vp);
1140 	data = fuse_get_mpdata(mp);
1141 	dataflags = data->dataflags;
1142 
1143 	fdisp_init(&fdi, sizeof(*fsai));
1144 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1145 	if (!cred) {
1146 		fdi.finh->uid = 0;
1147 		fdi.finh->gid = 0;
1148 	}
1149 	fsai = fdi.indata;
1150 	fsai->valid = 0;
1151 
1152 	if (vap->va_uid != (uid_t)VNOVAL) {
1153 		fsai->uid = vap->va_uid;
1154 		fsai->valid |= FATTR_UID;
1155 	}
1156 	if (vap->va_gid != (gid_t)VNOVAL) {
1157 		fsai->gid = vap->va_gid;
1158 		fsai->valid |= FATTR_GID;
1159 	}
1160 	if (vap->va_size != VNOVAL) {
1161 		struct fuse_filehandle *fufh = NULL;
1162 
1163 		/*Truncate to a new value. */
1164 		fsai->size = vap->va_size;
1165 		sizechanged = 1;
1166 		newsize = vap->va_size;
1167 		fsai->valid |= FATTR_SIZE;
1168 
1169 		fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1170 		if (fufh) {
1171 			fsai->fh = fufh->fh_id;
1172 			fsai->valid |= FATTR_FH;
1173 		}
1174 		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1175 	}
1176 	if (vap->va_atime.tv_sec != VNOVAL) {
1177 		fsai->atime = vap->va_atime.tv_sec;
1178 		fsai->atimensec = vap->va_atime.tv_nsec;
1179 		fsai->valid |= FATTR_ATIME;
1180 		if (vap->va_vaflags & VA_UTIMES_NULL)
1181 			fsai->valid |= FATTR_ATIME_NOW;
1182 	}
1183 	if (vap->va_mtime.tv_sec != VNOVAL) {
1184 		fsai->mtime = vap->va_mtime.tv_sec;
1185 		fsai->mtimensec = vap->va_mtime.tv_nsec;
1186 		fsai->valid |= FATTR_MTIME;
1187 		if (vap->va_vaflags & VA_UTIMES_NULL)
1188 			fsai->valid |= FATTR_MTIME_NOW;
1189 	} else if (fvdat->flag & FN_MTIMECHANGE) {
1190 		fsai->mtime = fvdat->cached_attrs.va_mtime.tv_sec;
1191 		fsai->mtimensec = fvdat->cached_attrs.va_mtime.tv_nsec;
1192 		fsai->valid |= FATTR_MTIME;
1193 	}
1194 	if (fuse_libabi_geq(data, 7, 23) && fvdat->flag & FN_CTIMECHANGE) {
1195 		fsai->ctime = fvdat->cached_attrs.va_ctime.tv_sec;
1196 		fsai->ctimensec = fvdat->cached_attrs.va_ctime.tv_nsec;
1197 		fsai->valid |= FATTR_CTIME;
1198 	}
1199 	if (vap->va_mode != (mode_t)VNOVAL) {
1200 		fsai->mode = vap->va_mode & ALLPERMS;
1201 		fsai->valid |= FATTR_MODE;
1202 	}
1203 	if (!fsai->valid) {
1204 		goto out;
1205 	}
1206 
1207 	if ((err = fdisp_wait_answ(&fdi)))
1208 		goto out;
1209 	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1210 
1211 	if (vnode_vtype(vp) != vtyp) {
1212 		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1213 			SDT_PROBE2(fusefs, , internal, trace, 1, "FUSE: Dang! "
1214 				"vnode_vtype is VNON and vtype isn't.");
1215 		} else {
1216 			/*
1217 	                 * STALE vnode, ditch
1218 	                 *
1219 			 * The vnode has changed its type "behind our back".
1220 			 * There's nothing really we can do, so let us just
1221 			 * force an internal revocation and tell the caller to
1222 			 * try again, if interested.
1223 	                 */
1224 			fuse_internal_vnode_disappear(vp);
1225 			err = EAGAIN;
1226 		}
1227 	}
1228 	if (err == 0) {
1229 		struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1230 		fuse_vnode_undirty_cached_timestamps(vp);
1231 		fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1232 			fao->attr_valid_nsec, NULL);
1233 	}
1234 
1235 out:
1236 	fdisp_destroy(&fdi);
1237 	return err;
1238 }
1239 
1240 /*
1241  * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
1242  */
1243 void
1244 fuse_internal_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
1245 	struct thread *td)
1246 {
1247 	struct fuse_data *data;
1248 	struct mount *mp;
1249 	struct vattr va;
1250 	int dataflags;
1251 
1252 	mp = vnode_mount(vp);
1253 	data = fuse_get_mpdata(mp);
1254 	dataflags = data->dataflags;
1255 
1256 	ASSERT_VOP_LOCKED(vp, __func__);
1257 
1258 	if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
1259 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
1260 			fuse_internal_getattr(vp, &va, cred, td);
1261 			if (va.va_mode & (S_ISUID | S_ISGID)) {
1262 				mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
1263 				/* Clear all vattr fields except mode */
1264 				vattr_null(&va);
1265 				va.va_mode = mode;
1266 
1267 				/*
1268 				 * Ignore fuse_internal_setattr's return value,
1269 				 * because at this point the write operation has
1270 				 * already succeeded and we don't want to return
1271 				 * failing status for that.
1272 				 */
1273 				(void)fuse_internal_setattr(vp, &va, td, NULL);
1274 			}
1275 		}
1276 	}
1277 }
1278 
1279 #ifdef ZERO_PAD_INCOMPLETE_BUFS
1280 static int
1281 isbzero(void *buf, size_t len)
1282 {
1283 	int i;
1284 
1285 	for (i = 0; i < len; i++) {
1286 		if (((char *)buf)[i])
1287 			return (0);
1288 	}
1289 
1290 	return (1);
1291 }
1292 
1293 #endif
1294 
1295 void
1296 fuse_internal_init(void)
1297 {
1298 	fuse_lookup_cache_misses = counter_u64_alloc(M_WAITOK);
1299 	fuse_lookup_cache_hits = counter_u64_alloc(M_WAITOK);
1300 }
1301 
1302 void
1303 fuse_internal_destroy(void)
1304 {
1305 	counter_u64_free(fuse_lookup_cache_hits);
1306 	counter_u64_free(fuse_lookup_cache_misses);
1307 }
1308