xref: /freebsd/sys/fs/fuse/fuse_vfsops.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following disclaimer
15  *   in the documentation and/or other materials provided with the
16  *   distribution.
17  * * Neither the name of Google Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Copyright (c) 2019 The FreeBSD Foundation
37  *
38  * Portions of this software were developed by BFF Storage Systems, LLC under
39  * sponsorship from the FreeBSD Foundation.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 #include <sys/param.h>
65 #include <sys/buf.h>
66 #include <sys/module.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/capsicum.h>
71 #include <sys/conf.h>
72 #include <sys/filedesc.h>
73 #include <sys/uio.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
76 #include <sys/lock.h>
77 #include <sys/sx.h>
78 #include <sys/mutex.h>
79 #include <sys/proc.h>
80 #include <sys/vnode.h>
81 #include <sys/namei.h>
82 #include <sys/mount.h>
83 #include <sys/sysctl.h>
84 #include <sys/fcntl.h>
85 
86 #include "fuse.h"
87 #include "fuse_node.h"
88 #include "fuse_ipc.h"
89 #include "fuse_internal.h"
90 
91 #include <sys/priv.h>
92 #include <security/mac/mac_framework.h>
93 
94 SDT_PROVIDER_DECLARE(fusefs);
95 /*
96  * Fuse trace probe:
97  * arg0: verbosity.  Higher numbers give more verbose messages
98  * arg1: Textual message
99  */
100 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
101 
102 /* This will do for privilege types for now */
103 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
104 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
105 #endif
106 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
107 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
108 #endif
109 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
110 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
111 #endif
112 
113 static vfs_fhtovp_t fuse_vfsop_fhtovp;
114 static vfs_mount_t fuse_vfsop_mount;
115 static vfs_unmount_t fuse_vfsop_unmount;
116 static vfs_root_t fuse_vfsop_root;
117 static vfs_statfs_t fuse_vfsop_statfs;
118 static vfs_vget_t fuse_vfsop_vget;
119 
120 struct vfsops fuse_vfsops = {
121 	.vfs_fhtovp = fuse_vfsop_fhtovp,
122 	.vfs_mount = fuse_vfsop_mount,
123 	.vfs_unmount = fuse_vfsop_unmount,
124 	.vfs_root = fuse_vfsop_root,
125 	.vfs_statfs = fuse_vfsop_statfs,
126 	.vfs_vget = fuse_vfsop_vget,
127 };
128 
129 static int fuse_enforce_dev_perms = 0;
130 
131 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
132     &fuse_enforce_dev_perms, 0,
133     "enforce fuse device permissions for secondary mounts");
134 
135 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
136 
137 static int
138 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
139 {
140 	struct nameidata nd, *ndp = &nd;
141 	struct vnode *devvp;
142 	struct cdev *fdev;
143 	int err;
144 
145 	/*
146 	 * Not an update, or updating the name: look up the name
147 	 * and verify that it refers to a sensible disk device.
148 	 */
149 
150 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec);
151 	if ((err = namei(ndp)) != 0)
152 		return err;
153 	NDFREE_PNBUF(ndp);
154 	devvp = ndp->ni_vp;
155 
156 	if (devvp->v_type != VCHR) {
157 		vrele(devvp);
158 		return ENXIO;
159 	}
160 	fdev = devvp->v_rdev;
161 	dev_ref(fdev);
162 
163 	if (fuse_enforce_dev_perms) {
164 		/*
165 	         * Check if mounter can open the fuse device.
166 	         *
167 	         * This has significance only if we are doing a secondary mount
168 	         * which doesn't involve actually opening fuse devices, but we
169 	         * still want to enforce the permissions of the device (in
170 	         * order to keep control over the circle of fuse users).
171 	         *
172 	         * (In case of primary mounts, we are either the superuser so
173 	         * we can do anything anyway, or we can mount only if the
174 	         * device is already opened by us, ie. we are permitted to open
175 	         * the device.)
176 	         */
177 #if 0
178 #ifdef MAC
179 		err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
180 		if (!err)
181 #endif
182 #endif /* 0 */
183 			err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
184 		if (err) {
185 			vrele(devvp);
186 			dev_rel(fdev);
187 			return err;
188 		}
189 	}
190 	/*
191 	 * according to coda code, no extra lock is needed --
192 	 * although in sys/vnode.h this field is marked "v"
193 	 */
194 	vrele(devvp);
195 
196 	if (!fdev->si_devsw ||
197 	    strcmp("fuse", fdev->si_devsw->d_name)) {
198 		dev_rel(fdev);
199 		return ENXIO;
200 	}
201 	*fdevp = fdev;
202 
203 	return 0;
204 }
205 
206 #define FUSE_FLAGOPT(fnam, fval) do {				\
207 	vfs_flagopt(opts, #fnam, &mntopts, fval);		\
208 	vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);	\
209 } while (0)
210 
211 SDT_PROBE_DEFINE1(fusefs, , vfsops, mntopts, "uint64_t");
212 SDT_PROBE_DEFINE4(fusefs, , vfsops, mount_err, "char*", "struct fuse_data*",
213 	"struct mount*", "int");
214 
215 static int
216 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
217 	uint32_t max_read, int daemon_timeout)
218 {
219 	int err = 0;
220 	struct fuse_data *data = fuse_get_mpdata(mp);
221 	/* Don't allow these options to be changed */
222 	const static unsigned long long cant_update_opts =
223 		MNT_USER;	/* Mount owner must be the user running the daemon */
224 
225 	FUSE_LOCK();
226 
227 	if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
228 		err = EOPNOTSUPP;
229 		SDT_PROBE4(fusefs, , vfsops, mount_err,
230 			"Can't change these mount options during remount",
231 			data, mp, err);
232 		goto out;
233 	}
234 	if (((data->dataflags ^ mntopts) & FSESS_MNTOPTS_MASK) ||
235 	     (data->max_read != max_read) ||
236 	     (data->daemon_timeout != daemon_timeout)) {
237 		// TODO: allow changing options where it makes sense
238 		err = EOPNOTSUPP;
239 		SDT_PROBE4(fusefs, , vfsops, mount_err,
240 			"Can't change fuse mount options during remount",
241 			data, mp, err);
242 		goto out;
243 	}
244 
245 	if (fdata_get_dead(data)) {
246 		err = ENOTCONN;
247 		SDT_PROBE4(fusefs, , vfsops, mount_err,
248 			"device is dead during mount", data, mp, err);
249 		goto out;
250 	}
251 
252 	/* Sanity + permission checks */
253 	if (!data->daemoncred)
254 		panic("fuse daemon found, but identity unknown");
255 	if (mntopts & FSESS_DAEMON_CAN_SPY)
256 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
257 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
258 		/* are we allowed to do the first mount? */
259 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
260 
261 out:
262 	FUSE_UNLOCK();
263 	return err;
264 }
265 
266 static int
267 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
268 	struct vnode **vpp)
269 {
270 	struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
271 	struct fuse_vnode_data *fvdat;
272 	struct vnode *nvp;
273 	int error;
274 
275 	if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
276 		return EOPNOTSUPP;
277 
278 	error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
279 	if (error) {
280 		*vpp = NULLVP;
281 		return (error);
282 	}
283 	fvdat = VTOFUD(nvp);
284 	if (fvdat->generation != ffhp->gen ) {
285 		vput(nvp);
286 		*vpp = NULLVP;
287 		return (ESTALE);
288 	}
289 	*vpp = nvp;
290 	vnode_create_vobject(*vpp, 0, curthread);
291 	return (0);
292 }
293 
294 static int
295 fuse_vfsop_mount(struct mount *mp)
296 {
297 	int err;
298 
299 	uint64_t mntopts, __mntopts;
300 	uint32_t max_read;
301 	int linux_errnos;
302 	int daemon_timeout;
303 	int fd;
304 
305 	struct cdev *fdev;
306 	struct fuse_data *data = NULL;
307 	struct thread *td;
308 	struct file *fp, *fptmp;
309 	char *fspec, *subtype, *fsname = NULL;
310 	int fsnamelen;
311 	struct vfsoptlist *opts;
312 
313 	subtype = NULL;
314 	max_read = ~0;
315 	linux_errnos = 0;
316 	err = 0;
317 	mntopts = 0;
318 	__mntopts = 0;
319 	td = curthread;
320 
321 	/* Get the new options passed to mount */
322 	opts = mp->mnt_optnew;
323 
324 	if (!opts)
325 		return EINVAL;
326 
327 	/* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
328 	if (!vfs_getopts(opts, "fspath", &err))
329 		return err;
330 
331 	/*
332 	 * With the help of underscored options the mount program
333 	 * can inform us from the flags it sets by default
334 	 */
335 	FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
336 	FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
337 	FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
338 	FUSE_FLAGOPT(intr, FSESS_INTR);
339 
340 	(void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
341 	(void)vfs_scanopt(opts, "linux_errnos", "%d", &linux_errnos);
342 	if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
343 		if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
344 			daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
345 		else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
346 			daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
347 	} else {
348 		daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
349 	}
350 	subtype = vfs_getopts(opts, "subtype=", &err);
351 
352 	SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
353 
354 	if (mp->mnt_flag & MNT_UPDATE) {
355 		return fuse_vfs_remount(mp, td, mntopts, max_read,
356 			daemon_timeout);
357 	}
358 
359 	/* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
360 	fspec = vfs_getopts(opts, "from", &err);
361 	if (!fspec)
362 		return err;
363 
364 	/* `fd' contains the filedescriptor for this session; REQUIRED */
365 	if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
366 		return EINVAL;
367 
368 	err = fuse_getdevice(fspec, td, &fdev);
369 	if (err != 0)
370 		return err;
371 
372 	err = fget(td, fd, &cap_read_rights, &fp);
373 	if (err != 0) {
374 		SDT_PROBE2(fusefs, , vfsops, trace, 1,
375 			"invalid or not opened device");
376 		goto out;
377 	}
378 	fptmp = td->td_fpop;
379 	td->td_fpop = fp;
380 	err = devfs_get_cdevpriv((void **)&data);
381 	td->td_fpop = fptmp;
382 	fdrop(fp, td);
383 	FUSE_LOCK();
384 
385 	if (err != 0 || data == NULL) {
386 		err = ENXIO;
387 		SDT_PROBE4(fusefs, , vfsops, mount_err,
388 			"invalid or not opened device", data, mp, err);
389 		FUSE_UNLOCK();
390 		goto out;
391 	}
392 	if (fdata_get_dead(data)) {
393 		err = ENOTCONN;
394 		SDT_PROBE4(fusefs, , vfsops, mount_err,
395 			"device is dead during mount", data, mp, err);
396 		FUSE_UNLOCK();
397 		goto out;
398 	}
399 	/* Sanity + permission checks */
400 	if (!data->daemoncred)
401 		panic("fuse daemon found, but identity unknown");
402 	if (mntopts & FSESS_DAEMON_CAN_SPY)
403 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
404 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
405 		/* are we allowed to do the first mount? */
406 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
407 	if (err) {
408 		FUSE_UNLOCK();
409 		goto out;
410 	}
411 	data->ref++;
412 	data->mp = mp;
413 	data->dataflags |= mntopts;
414 	data->max_read = max_read;
415 	data->daemon_timeout = daemon_timeout;
416 	data->linux_errnos = linux_errnos;
417 	data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
418 	FUSE_UNLOCK();
419 
420 	vfs_getnewfsid(mp);
421 	MNT_ILOCK(mp);
422 	mp->mnt_data = data;
423 	/*
424 	 * FUSE file systems can be either local or remote, but the kernel
425 	 * can't tell the difference.
426 	 */
427 	mp->mnt_flag &= ~MNT_LOCAL;
428 	mp->mnt_kern_flag |= MNTK_USES_BCACHE;
429 	/*
430 	 * Disable nullfs cacheing because it can consume too many resources in
431 	 * the FUSE server.
432 	 */
433 	mp->mnt_kern_flag |= MNTK_NULL_NOCACHE;
434 	MNT_IUNLOCK(mp);
435 	/* We need this here as this slot is used by getnewvnode() */
436 	mp->mnt_stat.f_iosize = maxbcachebuf;
437 	if (subtype) {
438 		strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
439 		strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
440 	}
441 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
442 	vfs_getopt(opts, "fsname=", (void**)&fsname, &fsnamelen);
443 	strlcpy(mp->mnt_stat.f_mntfromname,
444 		fsname == NULL ? fspec : fsname, MNAMELEN);
445 	mp->mnt_iosize_max = maxphys;
446 
447 	/* Now handshaking with daemon */
448 	fuse_internal_send_init(data, td);
449 
450 out:
451 	if (err) {
452 		FUSE_LOCK();
453 		if (data != NULL && data->mp == mp) {
454 			/*
455 			 * Destroy device only if we acquired reference to
456 			 * it
457 			 */
458 			SDT_PROBE4(fusefs, , vfsops, mount_err,
459 				"mount failed, destroy device", data, mp, err);
460 			data->mp = NULL;
461 			mp->mnt_data = NULL;
462 			fdata_trydestroy(data);
463 		}
464 		FUSE_UNLOCK();
465 		dev_rel(fdev);
466 	}
467 	return err;
468 }
469 
470 static int
471 fuse_vfsop_unmount(struct mount *mp, int mntflags)
472 {
473 	int err = 0;
474 	int flags = 0;
475 
476 	struct cdev *fdev;
477 	struct fuse_data *data;
478 	struct fuse_dispatcher fdi;
479 	struct thread *td = curthread;
480 
481 	if (mntflags & MNT_FORCE) {
482 		flags |= FORCECLOSE;
483 	}
484 	data = fuse_get_mpdata(mp);
485 	if (!data) {
486 		panic("no private data for mount point?");
487 	}
488 	/* There is 1 extra root vnode reference (mp->mnt_data). */
489 	FUSE_LOCK();
490 	if (data->vroot != NULL) {
491 		struct vnode *vroot = data->vroot;
492 
493 		data->vroot = NULL;
494 		FUSE_UNLOCK();
495 		vrele(vroot);
496 	} else
497 		FUSE_UNLOCK();
498 	err = vflush(mp, 0, flags, td);
499 	if (err) {
500 		return err;
501 	}
502 	if (fdata_get_dead(data)) {
503 		goto alreadydead;
504 	}
505 	if (fsess_maybe_impl(mp, FUSE_DESTROY)) {
506 		fdisp_init(&fdi, 0);
507 		fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
508 
509 		(void)fdisp_wait_answ(&fdi);
510 		fdisp_destroy(&fdi);
511 	}
512 
513 	fdata_set_dead(data);
514 
515 alreadydead:
516 	FUSE_LOCK();
517 	data->mp = NULL;
518 	fdev = data->fdev;
519 	fdata_trydestroy(data);
520 	FUSE_UNLOCK();
521 
522 	MNT_ILOCK(mp);
523 	mp->mnt_data = NULL;
524 	MNT_IUNLOCK(mp);
525 
526 	dev_rel(fdev);
527 
528 	return 0;
529 }
530 
531 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
532 	"struct mount*");
533 static int
534 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
535 {
536 	struct fuse_data *data = fuse_get_mpdata(mp);
537 	uint64_t nodeid = ino;
538 	struct thread *td = curthread;
539 	struct fuse_dispatcher fdi;
540 	struct fuse_entry_out *feo;
541 	struct fuse_vnode_data *fvdat;
542 	struct timespec now;
543 	const char dot[] = ".";
544 	__enum_uint8(vtype) vtyp;
545 	int error;
546 
547 	if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
548 		/*
549 		 * Unreachable unless you do something stupid, like export a
550 		 * nullfs mount of a fusefs file system.
551 		 */
552 		SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
553 		return (EOPNOTSUPP);
554 	}
555 
556 	error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
557 	if (error || *vpp != NULL)
558 		return error;
559 
560 	getnanouptime(&now);
561 
562 	/* Do a LOOKUP, using nodeid as the parent and "." as filename */
563 	fdisp_init(&fdi, sizeof(dot));
564 	fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
565 	memcpy(fdi.indata, dot, sizeof(dot));
566 	error = fdisp_wait_answ(&fdi);
567 
568 	if (error)
569 		return error;
570 
571 	feo = (struct fuse_entry_out *)fdi.answ;
572 	if (feo->nodeid == 0) {
573 		/* zero nodeid means ENOENT and cache it */
574 		error = ENOENT;
575 		goto out;
576 	}
577 
578 	vtyp = IFTOVT(feo->attr.mode);
579 	error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
580 	if (error)
581 		goto out;
582 	fvdat = VTOFUD(*vpp);
583 
584 	if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
585 		/*
586 		 * Attributes from the server are definitely newer than the
587 		 * last attributes we sent to the server, so cache them.
588 		 */
589 		fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
590 			feo->attr_valid_nsec, NULL, true);
591 	}
592 	fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
593 		&fvdat->entry_cache_timeout);
594 out:
595 	fdisp_destroy(&fdi);
596 	return error;
597 }
598 
599 static int
600 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
601 {
602 	struct fuse_data *data = fuse_get_mpdata(mp);
603 	int err = 0;
604 
605 	if (data->vroot != NULL) {
606 		err = vget(data->vroot, lkflags);
607 		if (err == 0)
608 			*vpp = data->vroot;
609 	} else {
610 		err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
611 		    VDIR);
612 		if (err == 0) {
613 			FUSE_LOCK();
614 			MPASS(data->vroot == NULL || data->vroot == *vpp);
615 			if (data->vroot == NULL) {
616 				SDT_PROBE2(fusefs, , vfsops, trace, 1,
617 					"new root vnode");
618 				data->vroot = *vpp;
619 				FUSE_UNLOCK();
620 				vref(*vpp);
621 			} else if (data->vroot != *vpp) {
622 				SDT_PROBE2(fusefs, , vfsops, trace, 1,
623 					"root vnode race");
624 				FUSE_UNLOCK();
625 				vput(*vpp);
626 				vrecycle(*vpp);
627 				*vpp = data->vroot;
628 			} else
629 				FUSE_UNLOCK();
630 		}
631 	}
632 	return err;
633 }
634 
635 static int
636 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
637 {
638 	struct fuse_dispatcher fdi;
639 	int err = 0;
640 
641 	struct fuse_statfs_out *fsfo;
642 	struct fuse_data *data;
643 
644 	data = fuse_get_mpdata(mp);
645 
646 	if (!(data->dataflags & FSESS_INITED))
647 		goto fake;
648 
649 	fdisp_init(&fdi, 0);
650 	fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
651 	err = fdisp_wait_answ(&fdi);
652 	if (err) {
653 		fdisp_destroy(&fdi);
654 		if (err == ENOTCONN) {
655 			/*
656 	                 * We want to seem a legitimate fs even if the daemon
657 	                 * is stiff dead... (so that, eg., we can still do path
658 	                 * based unmounting after the daemon dies).
659 	                 */
660 			goto fake;
661 		}
662 		return err;
663 	}
664 	fsfo = fdi.answ;
665 
666 	sbp->f_blocks = fsfo->st.blocks;
667 	sbp->f_bfree = fsfo->st.bfree;
668 	sbp->f_bavail = fsfo->st.bavail;
669 	sbp->f_files = fsfo->st.files;
670 	sbp->f_ffree = fsfo->st.ffree;	/* cast from uint64_t to int64_t */
671 	sbp->f_namemax = fsfo->st.namelen;
672 	sbp->f_bsize = fsfo->st.frsize;	/* cast from uint32_t to uint64_t */
673 
674 	fdisp_destroy(&fdi);
675 	return 0;
676 
677 fake:
678 	sbp->f_blocks = 0;
679 	sbp->f_bfree = 0;
680 	sbp->f_bavail = 0;
681 	sbp->f_files = 0;
682 	sbp->f_ffree = 0;
683 	sbp->f_namemax = 0;
684 	sbp->f_bsize = S_BLKSIZE;
685 
686 	return 0;
687 }
688