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