]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_vfsops.c
fusefs: WIP fixing writeback cacheing
[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         /* Get the new options passed to mount */
318         opts = mp->mnt_optnew;
319
320         if (!opts)
321                 return EINVAL;
322
323         /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
324         if (!vfs_getopts(opts, "fspath", &err))
325                 return err;
326
327         /*
328          * With the help of underscored options the mount program
329          * can inform us from the flags it sets by default
330          */
331         FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
332         FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
333         FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
334         FUSE_FLAGOPT(no_attrcache, FSESS_NO_ATTRCACHE);
335         FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD);
336         FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE);
337         FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE);
338         FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP);
339
340         (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
341         if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
342                 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
343                         daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
344                 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
345                         daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
346         } else {
347                 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
348         }
349         subtype = vfs_getopts(opts, "subtype=", &err);
350
351         SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
352
353         if (mp->mnt_flag & MNT_UPDATE) {
354                 return fuse_vfs_remount(mp, td, mntopts, max_read,
355                         daemon_timeout);
356         }
357
358         /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
359         fspec = vfs_getopts(opts, "from", &err);
360         if (!fspec)
361                 return err;
362
363         /* `fd' contains the filedescriptor for this session; REQUIRED */
364         if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
365                 return EINVAL;
366
367         err = fuse_getdevice(fspec, td, &fdev);
368         if (err != 0)
369                 return err;
370
371         err = fget(td, fd, &cap_read_rights, &fp);
372         if (err != 0) {
373                 SDT_PROBE2(fusefs, , vfsops, trace, 1,
374                         "invalid or not opened device");
375                 goto out;
376         }
377         fptmp = td->td_fpop;
378         td->td_fpop = fp;
379         err = devfs_get_cdevpriv((void **)&data);
380         td->td_fpop = fptmp;
381         fdrop(fp, td);
382         FUSE_LOCK();
383
384         if (err != 0 || data == NULL) {
385                 err = ENXIO;
386                 SDT_PROBE4(fusefs, , vfsops, mount_err,
387                         "invalid or not opened device", data, mp, err);
388                 FUSE_UNLOCK();
389                 goto out;
390         }
391         if (fdata_get_dead(data)) {
392                 err = ENOTCONN;
393                 SDT_PROBE4(fusefs, , vfsops, mount_err,
394                         "device is dead during mount", data, mp, err);
395                 FUSE_UNLOCK();
396                 goto out;
397         }
398         /* Sanity + permission checks */
399         if (!data->daemoncred)
400                 panic("fuse daemon found, but identity unknown");
401         if (mntopts & FSESS_DAEMON_CAN_SPY)
402                 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
403         if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
404                 /* are we allowed to do the first mount? */
405                 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
406         if (err) {
407                 FUSE_UNLOCK();
408                 goto out;
409         }
410         data->ref++;
411         data->mp = mp;
412         data->dataflags |= mntopts;
413         data->max_read = max_read;
414         data->daemon_timeout = daemon_timeout;
415         data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
416         FUSE_UNLOCK();
417
418         vfs_getnewfsid(mp);
419         MNT_ILOCK(mp);
420         mp->mnt_data = data;
421         /* 
422          * FUSE file systems can be either local or remote, but the kernel
423          * can't tell the difference.
424          */
425         mp->mnt_flag &= ~MNT_LOCAL;
426         mp->mnt_kern_flag |= MNTK_USES_BCACHE;
427         MNT_IUNLOCK(mp);
428         /* We need this here as this slot is used by getnewvnode() */
429         mp->mnt_stat.f_iosize = maxbcachebuf;
430         if (subtype) {
431                 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
432                 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
433         }
434         copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
435         bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
436
437         /* Now handshaking with daemon */
438         fuse_internal_send_init(data, td);
439
440 out:
441         if (err) {
442                 FUSE_LOCK();
443                 if (data != NULL && data->mp == mp) {
444                         /*
445                          * Destroy device only if we acquired reference to
446                          * it
447                          */
448                         SDT_PROBE4(fusefs, , vfsops, mount_err,
449                                 "mount failed, destroy device", data, mp, err);
450                         data->mp = NULL;
451                         mp->mnt_data = NULL;
452                         fdata_trydestroy(data);
453                 }
454                 FUSE_UNLOCK();
455                 dev_rel(fdev);
456         }
457         return err;
458 }
459
460 static int
461 fuse_vfsop_unmount(struct mount *mp, int mntflags)
462 {
463         int err = 0;
464         int flags = 0;
465
466         struct cdev *fdev;
467         struct fuse_data *data;
468         struct fuse_dispatcher fdi;
469         struct thread *td = curthread;
470
471         if (mntflags & MNT_FORCE) {
472                 flags |= FORCECLOSE;
473         }
474         data = fuse_get_mpdata(mp);
475         if (!data) {
476                 panic("no private data for mount point?");
477         }
478         /* There is 1 extra root vnode reference (mp->mnt_data). */
479         FUSE_LOCK();
480         if (data->vroot != NULL) {
481                 struct vnode *vroot = data->vroot;
482
483                 data->vroot = NULL;
484                 FUSE_UNLOCK();
485                 vrele(vroot);
486         } else
487                 FUSE_UNLOCK();
488         err = vflush(mp, 0, flags, td);
489         if (err) {
490                 return err;
491         }
492         if (fdata_get_dead(data)) {
493                 goto alreadydead;
494         }
495         fdisp_init(&fdi, 0);
496         fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
497
498         err = fdisp_wait_answ(&fdi);
499         fdisp_destroy(&fdi);
500
501         fdata_set_dead(data);
502
503 alreadydead:
504         FUSE_LOCK();
505         data->mp = NULL;
506         fdev = data->fdev;
507         fdata_trydestroy(data);
508         FUSE_UNLOCK();
509
510         MNT_ILOCK(mp);
511         mp->mnt_data = NULL;
512         MNT_IUNLOCK(mp);
513
514         dev_rel(fdev);
515
516         return 0;
517 }
518
519 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
520         "struct mount*");
521 static int
522 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
523 {
524         struct fuse_data *data = fuse_get_mpdata(mp);
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         if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
536                 /*
537                  * Unreachable unless you do something stupid, like export a
538                  * nullfs mount of a fusefs file system.
539                  */
540                 SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
541                 return (EOPNOTSUPP);
542         }
543
544         error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
545         if (error || *vpp != NULL)
546                 return error;
547
548         /* Do a LOOKUP, using nodeid as the parent and "." as filename */
549         fdisp_init(&fdi, sizeof(dot));
550         fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
551         memcpy(fdi.indata, dot, sizeof(dot));
552         error = fdisp_wait_answ(&fdi);
553
554         if (error)
555                 return error;
556
557         feo = (struct fuse_entry_out *)fdi.answ;
558         if (feo->nodeid == 0) {
559                 /* zero nodeid means ENOENT and cache it */
560                 error = ENOENT;
561                 goto out;
562         }
563
564         vtyp = IFTOVT(feo->attr.mode);
565         error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
566         if (error)
567                 goto out;
568         filesize = feo->attr.size;
569
570         /*
571          * In the case where we are looking up a FUSE node represented by an
572          * existing cached vnode, and the true size reported by FUSE_LOOKUP
573          * doesn't match the vnode's cached size, then any cached writes beyond
574          * the file's current size are lost.
575          *
576          * We can get here:
577          * * following attribute cache expiration, or
578          * * due a bug in the daemon, or
579          */
580         fvdat = VTOFUD(*vpp);
581         if (vnode_isreg(*vpp) &&
582             filesize != fvdat->cached_attrs.va_size &&
583             fvdat->flag & FN_SIZECHANGE) {
584                 printf("%s: WB cache incoherent on %s!\n", __func__,
585                     vnode_mount(*vpp)->mnt_stat.f_mntonname);
586
587                 fvdat->flag &= ~FN_SIZECHANGE;
588         }
589
590         fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
591                 feo->attr_valid_nsec, NULL);
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, curthread);
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                                 VOP_UNLOCK(*vpp, 0);
626                                 vrele(*vpp);
627                                 vrecycle(*vpp);
628                                 *vpp = data->vroot;
629                         } else
630                                 FUSE_UNLOCK();
631                 }
632         }
633         return err;
634 }
635
636 static int
637 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
638 {
639         struct fuse_dispatcher fdi;
640         int err = 0;
641
642         struct fuse_statfs_out *fsfo;
643         struct fuse_data *data;
644
645         data = fuse_get_mpdata(mp);
646
647         if (!(data->dataflags & FSESS_INITED))
648                 goto fake;
649
650         fdisp_init(&fdi, 0);
651         fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
652         err = fdisp_wait_answ(&fdi);
653         if (err) {
654                 fdisp_destroy(&fdi);
655                 if (err == ENOTCONN) {
656                         /*
657                          * We want to seem a legitimate fs even if the daemon
658                          * is stiff dead... (so that, eg., we can still do path
659                          * based unmounting after the daemon dies).
660                          */
661                         goto fake;
662                 }
663                 return err;
664         }
665         fsfo = fdi.answ;
666
667         sbp->f_blocks = fsfo->st.blocks;
668         sbp->f_bfree = fsfo->st.bfree;
669         sbp->f_bavail = fsfo->st.bavail;
670         sbp->f_files = fsfo->st.files;
671         sbp->f_ffree = fsfo->st.ffree;  /* cast from uint64_t to int64_t */
672         sbp->f_namemax = fsfo->st.namelen;
673         sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
674
675         fdisp_destroy(&fdi);
676         return 0;
677
678 fake:
679         sbp->f_blocks = 0;
680         sbp->f_bfree = 0;
681         sbp->f_bavail = 0;
682         sbp->f_files = 0;
683         sbp->f_ffree = 0;
684         sbp->f_namemax = 0;
685         sbp->f_bsize = S_BLKSIZE;
686
687         return 0;
688 }