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