2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.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>
74 #include <sys/filedesc.h>
76 #include <sys/malloc.h>
77 #include <sys/queue.h>
80 #include <sys/mutex.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>
89 #include "fuse_node.h"
91 #include "fuse_internal.h"
94 #include <security/mac/mac_framework.h>
96 SDT_PROVIDER_DECLARE(fusefs);
99 * arg0: verbosity. Higher numbers give more verbose messages
100 * arg1: Textual message
102 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
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
108 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
109 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
111 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
112 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
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;
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,
131 static int fuse_enforce_dev_perms = 0;
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");
137 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
140 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
142 struct nameidata nd, *ndp = &nd;
148 * Not an update, or updating the name: look up the name
149 * and verify that it refers to a sensible disk device.
152 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
153 if ((err = namei(ndp)) != 0)
155 NDFREE(ndp, NDF_ONLY_PNBUF);
158 if (devvp->v_type != VCHR) {
162 fdev = devvp->v_rdev;
165 if (fuse_enforce_dev_perms) {
167 * Check if mounter can open the fuse device.
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).
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
181 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
185 err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
193 * according to coda code, no extra lock is needed --
194 * although in sys/vnode.h this field is marked "v"
198 if (!fdev->si_devsw ||
199 strcmp("fuse", fdev->si_devsw->d_name)) {
208 #define FUSE_FLAGOPT(fnam, fval) do { \
209 vfs_flagopt(opts, #fnam, &mntopts, fval); \
210 vfs_flagopt(opts, "__" #fnam, &__mntopts, fval); \
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");
218 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
219 uint32_t max_read, int daemon_timeout)
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 */
229 if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
231 SDT_PROBE4(fusefs, , vfsops, mount_err,
232 "Can't change these mount options during remount",
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
241 SDT_PROBE4(fusefs, , vfsops, mount_err,
242 "Can't change fuse mount options during remount",
247 if (fdata_get_dead(data)) {
249 SDT_PROBE4(fusefs, , vfsops, mount_err,
250 "device is dead during mount", data, mp, err);
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);
269 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
272 struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
273 struct fuse_vnode_data *fvdat;
277 if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
280 error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
286 if (fvdat->generation != ffhp->gen ) {
292 vnode_create_vobject(*vpp, 0, curthread);
297 fuse_vfsop_mount(struct mount *mp)
301 uint64_t mntopts, __mntopts;
308 struct fuse_data *data = NULL;
310 struct file *fp, *fptmp;
311 char *fspec, *subtype;
312 struct vfsoptlist *opts;
322 /* Get the new options passed to mount */
323 opts = mp->mnt_optnew;
328 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
329 if (!vfs_getopts(opts, "fspath", &err))
333 * With the help of underscored options the mount program
334 * can inform us from the flags it sets by default
336 FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
337 FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
338 FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
339 FUSE_FLAGOPT(intr, FSESS_INTR);
341 (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
342 (void)vfs_scanopt(opts, "linux_errnos", "%d", &linux_errnos);
343 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
344 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
345 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
346 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
347 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
349 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
351 subtype = vfs_getopts(opts, "subtype=", &err);
353 SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
355 if (mp->mnt_flag & MNT_UPDATE) {
356 return fuse_vfs_remount(mp, td, mntopts, max_read,
360 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
361 fspec = vfs_getopts(opts, "from", &err);
365 /* `fd' contains the filedescriptor for this session; REQUIRED */
366 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
369 err = fuse_getdevice(fspec, td, &fdev);
373 err = fget(td, fd, &cap_read_rights, &fp);
375 SDT_PROBE2(fusefs, , vfsops, trace, 1,
376 "invalid or not opened device");
381 err = devfs_get_cdevpriv((void **)&data);
386 if (err != 0 || data == NULL) {
388 SDT_PROBE4(fusefs, , vfsops, mount_err,
389 "invalid or not opened device", data, mp, err);
393 if (fdata_get_dead(data)) {
395 SDT_PROBE4(fusefs, , vfsops, mount_err,
396 "device is dead during mount", data, mp, err);
400 /* Sanity + permission checks */
401 if (!data->daemoncred)
402 panic("fuse daemon found, but identity unknown");
403 if (mntopts & FSESS_DAEMON_CAN_SPY)
404 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
405 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
406 /* are we allowed to do the first mount? */
407 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
414 data->dataflags |= mntopts;
415 data->max_read = max_read;
416 data->daemon_timeout = daemon_timeout;
417 data->linux_errnos = linux_errnos;
418 data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
425 * FUSE file systems can be either local or remote, but the kernel
426 * can't tell the difference.
428 mp->mnt_flag &= ~MNT_LOCAL;
429 mp->mnt_kern_flag |= MNTK_USES_BCACHE;
431 * Disable nullfs cacheing because it can consume too many resources in
434 mp->mnt_kern_flag |= MNTK_NULL_NOCACHE;
436 /* We need this here as this slot is used by getnewvnode() */
437 mp->mnt_stat.f_iosize = maxbcachebuf;
439 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
440 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
442 memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
443 strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
444 mp->mnt_iosize_max = maxphys;
446 /* Now handshaking with daemon */
447 fuse_internal_send_init(data, td);
452 if (data != NULL && data->mp == mp) {
454 * Destroy device only if we acquired reference to
457 SDT_PROBE4(fusefs, , vfsops, mount_err,
458 "mount failed, destroy device", data, mp, err);
461 fdata_trydestroy(data);
470 fuse_vfsop_unmount(struct mount *mp, int mntflags)
476 struct fuse_data *data;
477 struct fuse_dispatcher fdi;
478 struct thread *td = curthread;
480 if (mntflags & MNT_FORCE) {
483 data = fuse_get_mpdata(mp);
485 panic("no private data for mount point?");
487 /* There is 1 extra root vnode reference (mp->mnt_data). */
489 if (data->vroot != NULL) {
490 struct vnode *vroot = data->vroot;
497 err = vflush(mp, 0, flags, td);
501 if (fdata_get_dead(data)) {
504 if (fsess_isimpl(mp, FUSE_DESTROY)) {
506 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
508 (void)fdisp_wait_answ(&fdi);
512 fdata_set_dead(data);
518 fdata_trydestroy(data);
530 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
533 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
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 const char dot[] = ".";
546 if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
548 * Unreachable unless you do something stupid, like export a
549 * nullfs mount of a fusefs file system.
551 SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
555 error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
556 if (error || *vpp != NULL)
559 /* Do a LOOKUP, using nodeid as the parent and "." as filename */
560 fdisp_init(&fdi, sizeof(dot));
561 fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
562 memcpy(fdi.indata, dot, sizeof(dot));
563 error = fdisp_wait_answ(&fdi);
568 feo = (struct fuse_entry_out *)fdi.answ;
569 if (feo->nodeid == 0) {
570 /* zero nodeid means ENOENT and cache it */
575 vtyp = IFTOVT(feo->attr.mode);
576 error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
579 filesize = feo->attr.size;
582 * In the case where we are looking up a FUSE node represented by an
583 * existing cached vnode, and the true size reported by FUSE_LOOKUP
584 * doesn't match the vnode's cached size, then any cached writes beyond
585 * the file's current size are lost.
588 * * following attribute cache expiration, or
589 * * due a bug in the daemon, or
591 fvdat = VTOFUD(*vpp);
592 if (vnode_isreg(*vpp) &&
593 filesize != fvdat->cached_attrs.va_size &&
594 fvdat->flag & FN_SIZECHANGE) {
595 printf("%s: WB cache incoherent on %s!\n", __func__,
596 vnode_mount(*vpp)->mnt_stat.f_mntonname);
598 fvdat->flag &= ~FN_SIZECHANGE;
601 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
602 feo->attr_valid_nsec, NULL);
603 fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
604 &fvdat->entry_cache_timeout);
611 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
613 struct fuse_data *data = fuse_get_mpdata(mp);
616 if (data->vroot != NULL) {
617 err = vget(data->vroot, lkflags);
621 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
625 MPASS(data->vroot == NULL || data->vroot == *vpp);
626 if (data->vroot == NULL) {
627 SDT_PROBE2(fusefs, , vfsops, trace, 1,
632 } else if (data->vroot != *vpp) {
633 SDT_PROBE2(fusefs, , vfsops, trace, 1,
648 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
650 struct fuse_dispatcher fdi;
653 struct fuse_statfs_out *fsfo;
654 struct fuse_data *data;
656 data = fuse_get_mpdata(mp);
658 if (!(data->dataflags & FSESS_INITED))
662 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
663 err = fdisp_wait_answ(&fdi);
666 if (err == ENOTCONN) {
668 * We want to seem a legitimate fs even if the daemon
669 * is stiff dead... (so that, eg., we can still do path
670 * based unmounting after the daemon dies).
678 sbp->f_blocks = fsfo->st.blocks;
679 sbp->f_bfree = fsfo->st.bfree;
680 sbp->f_bavail = fsfo->st.bavail;
681 sbp->f_files = fsfo->st.files;
682 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */
683 sbp->f_namemax = fsfo->st.namelen;
684 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
696 sbp->f_bsize = S_BLKSIZE;