]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_vfsops.c
Initial import from vendor-sys branch of openzfs
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_vfsops.c
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/buf.h>
68 #include <sys/module.h>
69 #include <sys/systm.h>
70 #include <sys/errno.h>
71 #include <sys/kernel.h>
72 #include <sys/capsicum.h>
73 #include <sys/conf.h>
74 #include <sys/filedesc.h>
75 #include <sys/uio.h>
76 #include <sys/malloc.h>
77 #include <sys/queue.h>
78 #include <sys/lock.h>
79 #include <sys/sx.h>
80 #include <sys/mutex.h>
81 #include <sys/proc.h>
82 #include <sys/vnode.h>
83 #include <sys/namei.h>
84 #include <sys/mount.h>
85 #include <sys/sysctl.h>
86 #include <sys/fcntl.h>
87
88 #include "fuse.h"
89 #include "fuse_node.h"
90 #include "fuse_ipc.h"
91 #include "fuse_internal.h"
92
93 #include <sys/priv.h>
94 #include <security/mac/mac_framework.h>
95
96 SDT_PROVIDER_DECLARE(fusefs);
97 /* 
98  * Fuse trace probe:
99  * arg0: verbosity.  Higher numbers give more verbose messages
100  * arg1: Textual message
101  */
102 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
103
104 /* This will do for privilege types for now */
105 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
106 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
107 #endif
108 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
109 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
110 #endif
111 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
112 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
113 #endif
114
115 static vfs_fhtovp_t fuse_vfsop_fhtovp;
116 static vfs_mount_t fuse_vfsop_mount;
117 static vfs_unmount_t fuse_vfsop_unmount;
118 static vfs_root_t fuse_vfsop_root;
119 static vfs_statfs_t fuse_vfsop_statfs;
120 static vfs_vget_t fuse_vfsop_vget;
121
122 struct vfsops fuse_vfsops = {
123         .vfs_fhtovp = fuse_vfsop_fhtovp,
124         .vfs_mount = fuse_vfsop_mount,
125         .vfs_unmount = fuse_vfsop_unmount,
126         .vfs_root = fuse_vfsop_root,
127         .vfs_statfs = fuse_vfsop_statfs,
128         .vfs_vget = fuse_vfsop_vget,
129 };
130
131 static int fuse_enforce_dev_perms = 0;
132
133 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
134     &fuse_enforce_dev_perms, 0,
135     "enforce fuse device permissions for secondary mounts");
136
137 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
138
139 static int
140 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
141 {
142         struct nameidata nd, *ndp = &nd;
143         struct vnode *devvp;
144         struct cdev *fdev;
145         int err;
146
147         /*
148          * Not an update, or updating the name: look up the name
149          * and verify that it refers to a sensible disk device.
150          */
151
152         NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
153         if ((err = namei(ndp)) != 0)
154                 return err;
155         NDFREE(ndp, NDF_ONLY_PNBUF);
156         devvp = ndp->ni_vp;
157
158         if (devvp->v_type != VCHR) {
159                 vrele(devvp);
160                 return ENXIO;
161         }
162         fdev = devvp->v_rdev;
163         dev_ref(fdev);
164
165         if (fuse_enforce_dev_perms) {
166                 /*
167                  * Check if mounter can open the fuse device.
168                  *
169                  * This has significance only if we are doing a secondary mount
170                  * which doesn't involve actually opening fuse devices, but we
171                  * still want to enforce the permissions of the device (in
172                  * order to keep control over the circle of fuse users).
173                  *
174                  * (In case of primary mounts, we are either the superuser so
175                  * we can do anything anyway, or we can mount only if the
176                  * device is already opened by us, ie. we are permitted to open
177                  * the device.)
178                  */
179 #if 0
180 #ifdef MAC
181                 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
182                 if (!err)
183 #endif
184 #endif /* 0 */
185                         err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
186                 if (err) {
187                         vrele(devvp);
188                         dev_rel(fdev);
189                         return err;
190                 }
191         }
192         /*
193          * according to coda code, no extra lock is needed --
194          * although in sys/vnode.h this field is marked "v"
195          */
196         vrele(devvp);
197
198         if (!fdev->si_devsw ||
199             strcmp("fuse", fdev->si_devsw->d_name)) {
200                 dev_rel(fdev);
201                 return ENXIO;
202         }
203         *fdevp = fdev;
204
205         return 0;
206 }
207
208 #define FUSE_FLAGOPT(fnam, fval) do {                           \
209         vfs_flagopt(opts, #fnam, &mntopts, fval);               \
210         vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);        \
211 } while (0)
212
213 SDT_PROBE_DEFINE1(fusefs, , vfsops, mntopts, "uint64_t");
214 SDT_PROBE_DEFINE4(fusefs, , vfsops, mount_err, "char*", "struct fuse_data*",
215         "struct mount*", "int");
216
217 static int
218 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
219         uint32_t max_read, int daemon_timeout)
220 {
221         int err = 0;
222         struct fuse_data *data = fuse_get_mpdata(mp);
223         /* Don't allow these options to be changed */
224         const static unsigned long long cant_update_opts = 
225                 MNT_USER;       /* Mount owner must be the user running the daemon */
226
227         FUSE_LOCK();
228
229         if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
230                 err = EOPNOTSUPP;
231                 SDT_PROBE4(fusefs, , vfsops, mount_err,
232                         "Can't change these mount options during remount",
233                         data, mp, err);
234                 goto out;
235         }
236         if (((data->dataflags ^ mntopts) & FSESS_MNTOPTS_MASK) ||
237              (data->max_read != max_read) ||
238              (data->daemon_timeout != daemon_timeout)) {
239                 // TODO: allow changing options where it makes sense
240                 err = EOPNOTSUPP;
241                 SDT_PROBE4(fusefs, , vfsops, mount_err,
242                         "Can't change fuse mount options during remount",
243                         data, mp, err);
244                 goto out;
245         }
246
247         if (fdata_get_dead(data)) {
248                 err = ENOTCONN;
249                 SDT_PROBE4(fusefs, , vfsops, mount_err,
250                         "device is dead during mount", data, mp, err);
251                 goto out;
252         }
253
254         /* Sanity + permission checks */
255         if (!data->daemoncred)
256                 panic("fuse daemon found, but identity unknown");
257         if (mntopts & FSESS_DAEMON_CAN_SPY)
258                 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
259         if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
260                 /* are we allowed to do the first mount? */
261                 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
262
263 out:
264         FUSE_UNLOCK();
265         return err;
266 }
267
268 static int
269 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
270         struct vnode **vpp)
271 {
272         struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
273         struct fuse_vnode_data *fvdat;
274         struct vnode *nvp;
275         int error;
276
277         if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
278                 return EOPNOTSUPP;
279
280         error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
281         if (error) {
282                 *vpp = NULLVP;
283                 return (error);
284         }
285         fvdat = VTOFUD(nvp);
286         if (fvdat->generation != ffhp->gen ) {
287                 vput(nvp);
288                 *vpp = NULLVP;
289                 return (ESTALE);
290         }
291         *vpp = nvp;
292         vnode_create_vobject(*vpp, 0, curthread);
293         return (0);
294 }
295
296 static int
297 fuse_vfsop_mount(struct mount *mp)
298 {
299         int err;
300
301         uint64_t mntopts, __mntopts;
302         uint32_t max_read;
303         int daemon_timeout;
304         int fd;
305
306         struct cdev *fdev;
307         struct fuse_data *data = NULL;
308         struct thread *td;
309         struct file *fp, *fptmp;
310         char *fspec, *subtype;
311         struct vfsoptlist *opts;
312
313         subtype = NULL;
314         max_read = ~0;
315         err = 0;
316         mntopts = 0;
317         __mntopts = 0;
318         td = curthread;
319
320         /* Get the new options passed to mount */
321         opts = mp->mnt_optnew;
322
323         if (!opts)
324                 return EINVAL;
325
326         /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
327         if (!vfs_getopts(opts, "fspath", &err))
328                 return err;
329
330         /*
331          * With the help of underscored options the mount program
332          * can inform us from the flags it sets by default
333          */
334         FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
335         FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
336         FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
337         FUSE_FLAGOPT(intr, FSESS_INTR);
338
339         (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
340         if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
341                 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
342                         daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
343                 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
344                         daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
345         } else {
346                 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
347         }
348         subtype = vfs_getopts(opts, "subtype=", &err);
349
350         SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
351
352         if (mp->mnt_flag & MNT_UPDATE) {
353                 return fuse_vfs_remount(mp, td, mntopts, max_read,
354                         daemon_timeout);
355         }
356
357         /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
358         fspec = vfs_getopts(opts, "from", &err);
359         if (!fspec)
360                 return err;
361
362         /* `fd' contains the filedescriptor for this session; REQUIRED */
363         if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
364                 return EINVAL;
365
366         err = fuse_getdevice(fspec, td, &fdev);
367         if (err != 0)
368                 return err;
369
370         err = fget(td, fd, &cap_read_rights, &fp);
371         if (err != 0) {
372                 SDT_PROBE2(fusefs, , vfsops, trace, 1,
373                         "invalid or not opened device");
374                 goto out;
375         }
376         fptmp = td->td_fpop;
377         td->td_fpop = fp;
378         err = devfs_get_cdevpriv((void **)&data);
379         td->td_fpop = fptmp;
380         fdrop(fp, td);
381         FUSE_LOCK();
382
383         if (err != 0 || data == NULL) {
384                 err = ENXIO;
385                 SDT_PROBE4(fusefs, , vfsops, mount_err,
386                         "invalid or not opened device", data, mp, err);
387                 FUSE_UNLOCK();
388                 goto out;
389         }
390         if (fdata_get_dead(data)) {
391                 err = ENOTCONN;
392                 SDT_PROBE4(fusefs, , vfsops, mount_err,
393                         "device is dead during mount", data, mp, err);
394                 FUSE_UNLOCK();
395                 goto out;
396         }
397         /* Sanity + permission checks */
398         if (!data->daemoncred)
399                 panic("fuse daemon found, but identity unknown");
400         if (mntopts & FSESS_DAEMON_CAN_SPY)
401                 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
402         if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
403                 /* are we allowed to do the first mount? */
404                 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
405         if (err) {
406                 FUSE_UNLOCK();
407                 goto out;
408         }
409         data->ref++;
410         data->mp = mp;
411         data->dataflags |= mntopts;
412         data->max_read = max_read;
413         data->daemon_timeout = daemon_timeout;
414         data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
415         FUSE_UNLOCK();
416
417         vfs_getnewfsid(mp);
418         MNT_ILOCK(mp);
419         mp->mnt_data = data;
420         /* 
421          * FUSE file systems can be either local or remote, but the kernel
422          * can't tell the difference.
423          */
424         mp->mnt_flag &= ~MNT_LOCAL;
425         mp->mnt_kern_flag |= MNTK_USES_BCACHE;
426         /* 
427          * Disable nullfs cacheing because it can consume too many resources in
428          * the FUSE server.
429          */
430         mp->mnt_kern_flag |= MNTK_NULL_NOCACHE;
431         MNT_IUNLOCK(mp);
432         /* We need this here as this slot is used by getnewvnode() */
433         mp->mnt_stat.f_iosize = maxbcachebuf;
434         if (subtype) {
435                 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
436                 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
437         }
438         memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
439         strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
440         mp->mnt_iosize_max = MAXPHYS;
441
442         /* Now handshaking with daemon */
443         fuse_internal_send_init(data, td);
444
445 out:
446         if (err) {
447                 FUSE_LOCK();
448                 if (data != NULL && data->mp == mp) {
449                         /*
450                          * Destroy device only if we acquired reference to
451                          * it
452                          */
453                         SDT_PROBE4(fusefs, , vfsops, mount_err,
454                                 "mount failed, destroy device", data, mp, err);
455                         data->mp = NULL;
456                         mp->mnt_data = NULL;
457                         fdata_trydestroy(data);
458                 }
459                 FUSE_UNLOCK();
460                 dev_rel(fdev);
461         }
462         return err;
463 }
464
465 static int
466 fuse_vfsop_unmount(struct mount *mp, int mntflags)
467 {
468         int err = 0;
469         int flags = 0;
470
471         struct cdev *fdev;
472         struct fuse_data *data;
473         struct fuse_dispatcher fdi;
474         struct thread *td = curthread;
475
476         if (mntflags & MNT_FORCE) {
477                 flags |= FORCECLOSE;
478         }
479         data = fuse_get_mpdata(mp);
480         if (!data) {
481                 panic("no private data for mount point?");
482         }
483         /* There is 1 extra root vnode reference (mp->mnt_data). */
484         FUSE_LOCK();
485         if (data->vroot != NULL) {
486                 struct vnode *vroot = data->vroot;
487
488                 data->vroot = NULL;
489                 FUSE_UNLOCK();
490                 vrele(vroot);
491         } else
492                 FUSE_UNLOCK();
493         err = vflush(mp, 0, flags, td);
494         if (err) {
495                 return err;
496         }
497         if (fdata_get_dead(data)) {
498                 goto alreadydead;
499         }
500         if (fsess_isimpl(mp, FUSE_DESTROY)) {
501                 fdisp_init(&fdi, 0);
502                 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
503
504                 (void)fdisp_wait_answ(&fdi);
505                 fdisp_destroy(&fdi);
506         }
507
508         fdata_set_dead(data);
509
510 alreadydead:
511         FUSE_LOCK();
512         data->mp = NULL;
513         fdev = data->fdev;
514         fdata_trydestroy(data);
515         FUSE_UNLOCK();
516
517         MNT_ILOCK(mp);
518         mp->mnt_data = NULL;
519         MNT_IUNLOCK(mp);
520
521         dev_rel(fdev);
522
523         return 0;
524 }
525
526 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
527         "struct mount*");
528 static int
529 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
530 {
531         struct fuse_data *data = fuse_get_mpdata(mp);
532         uint64_t nodeid = ino;
533         struct thread *td = curthread;
534         struct fuse_dispatcher fdi;
535         struct fuse_entry_out *feo;
536         struct fuse_vnode_data *fvdat;
537         const char dot[] = ".";
538         off_t filesize;
539         enum vtype vtyp;
540         int error;
541
542         if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
543                 /*
544                  * Unreachable unless you do something stupid, like export a
545                  * nullfs mount of a fusefs file system.
546                  */
547                 SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
548                 return (EOPNOTSUPP);
549         }
550
551         error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
552         if (error || *vpp != NULL)
553                 return error;
554
555         /* Do a LOOKUP, using nodeid as the parent and "." as filename */
556         fdisp_init(&fdi, sizeof(dot));
557         fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
558         memcpy(fdi.indata, dot, sizeof(dot));
559         error = fdisp_wait_answ(&fdi);
560
561         if (error)
562                 return error;
563
564         feo = (struct fuse_entry_out *)fdi.answ;
565         if (feo->nodeid == 0) {
566                 /* zero nodeid means ENOENT and cache it */
567                 error = ENOENT;
568                 goto out;
569         }
570
571         vtyp = IFTOVT(feo->attr.mode);
572         error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
573         if (error)
574                 goto out;
575         filesize = feo->attr.size;
576
577         /*
578          * In the case where we are looking up a FUSE node represented by an
579          * existing cached vnode, and the true size reported by FUSE_LOOKUP
580          * doesn't match the vnode's cached size, then any cached writes beyond
581          * the file's current size are lost.
582          *
583          * We can get here:
584          * * following attribute cache expiration, or
585          * * due a bug in the daemon, or
586          */
587         fvdat = VTOFUD(*vpp);
588         if (vnode_isreg(*vpp) &&
589             filesize != fvdat->cached_attrs.va_size &&
590             fvdat->flag & FN_SIZECHANGE) {
591                 printf("%s: WB cache incoherent on %s!\n", __func__,
592                     vnode_mount(*vpp)->mnt_stat.f_mntonname);
593
594                 fvdat->flag &= ~FN_SIZECHANGE;
595         }
596
597         fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
598                 feo->attr_valid_nsec, NULL);
599         fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
600                 &fvdat->entry_cache_timeout);
601 out:
602         fdisp_destroy(&fdi);
603         return error;
604 }
605
606 static int
607 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
608 {
609         struct fuse_data *data = fuse_get_mpdata(mp);
610         int err = 0;
611
612         if (data->vroot != NULL) {
613                 err = vget(data->vroot, lkflags);
614                 if (err == 0)
615                         *vpp = data->vroot;
616         } else {
617                 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
618                     VDIR);
619                 if (err == 0) {
620                         FUSE_LOCK();
621                         MPASS(data->vroot == NULL || data->vroot == *vpp);
622                         if (data->vroot == NULL) {
623                                 SDT_PROBE2(fusefs, , vfsops, trace, 1,
624                                         "new root vnode");
625                                 data->vroot = *vpp;
626                                 FUSE_UNLOCK();
627                                 vref(*vpp);
628                         } else if (data->vroot != *vpp) {
629                                 SDT_PROBE2(fusefs, , vfsops, trace, 1,
630                                         "root vnode race");
631                                 FUSE_UNLOCK();
632                                 VOP_UNLOCK(*vpp);
633                                 vrele(*vpp);
634                                 vrecycle(*vpp);
635                                 *vpp = data->vroot;
636                         } else
637                                 FUSE_UNLOCK();
638                 }
639         }
640         return err;
641 }
642
643 static int
644 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
645 {
646         struct fuse_dispatcher fdi;
647         int err = 0;
648
649         struct fuse_statfs_out *fsfo;
650         struct fuse_data *data;
651
652         data = fuse_get_mpdata(mp);
653
654         if (!(data->dataflags & FSESS_INITED))
655                 goto fake;
656
657         fdisp_init(&fdi, 0);
658         fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
659         err = fdisp_wait_answ(&fdi);
660         if (err) {
661                 fdisp_destroy(&fdi);
662                 if (err == ENOTCONN) {
663                         /*
664                          * We want to seem a legitimate fs even if the daemon
665                          * is stiff dead... (so that, eg., we can still do path
666                          * based unmounting after the daemon dies).
667                          */
668                         goto fake;
669                 }
670                 return err;
671         }
672         fsfo = fdi.answ;
673
674         sbp->f_blocks = fsfo->st.blocks;
675         sbp->f_bfree = fsfo->st.bfree;
676         sbp->f_bavail = fsfo->st.bavail;
677         sbp->f_files = fsfo->st.files;
678         sbp->f_ffree = fsfo->st.ffree;  /* cast from uint64_t to int64_t */
679         sbp->f_namemax = fsfo->st.namelen;
680         sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
681
682         fdisp_destroy(&fdi);
683         return 0;
684
685 fake:
686         sbp->f_blocks = 0;
687         sbp->f_bfree = 0;
688         sbp->f_bavail = 0;
689         sbp->f_files = 0;
690         sbp->f_ffree = 0;
691         sbp->f_namemax = 0;
692         sbp->f_bsize = S_BLKSIZE;
693
694         return 0;
695 }