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 #include <sys/param.h>
66 #include <sys/module.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/capsicum.h>
72 #include <sys/filedesc.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
78 #include <sys/mutex.h>
80 #include <sys/vnode.h>
81 #include <sys/namei.h>
82 #include <sys/mount.h>
83 #include <sys/sysctl.h>
84 #include <sys/fcntl.h>
87 #include "fuse_node.h"
89 #include "fuse_internal.h"
92 #include <security/mac/mac_framework.h>
94 SDT_PROVIDER_DECLARE(fusefs);
97 * arg0: verbosity. Higher numbers give more verbose messages
98 * arg1: Textual message
100 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
102 /* This will do for privilege types for now */
103 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
104 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
106 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
107 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
109 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
110 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
113 static vfs_fhtovp_t fuse_vfsop_fhtovp;
114 static vfs_mount_t fuse_vfsop_mount;
115 static vfs_unmount_t fuse_vfsop_unmount;
116 static vfs_root_t fuse_vfsop_root;
117 static vfs_statfs_t fuse_vfsop_statfs;
118 static vfs_vget_t fuse_vfsop_vget;
120 struct vfsops fuse_vfsops = {
121 .vfs_fhtovp = fuse_vfsop_fhtovp,
122 .vfs_mount = fuse_vfsop_mount,
123 .vfs_unmount = fuse_vfsop_unmount,
124 .vfs_root = fuse_vfsop_root,
125 .vfs_statfs = fuse_vfsop_statfs,
126 .vfs_vget = fuse_vfsop_vget,
129 static int fuse_enforce_dev_perms = 0;
131 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
132 &fuse_enforce_dev_perms, 0,
133 "enforce fuse device permissions for secondary mounts");
135 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
138 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
140 struct nameidata nd, *ndp = &nd;
146 * Not an update, or updating the name: look up the name
147 * and verify that it refers to a sensible disk device.
150 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec);
151 if ((err = namei(ndp)) != 0)
156 if (devvp->v_type != VCHR) {
160 fdev = devvp->v_rdev;
163 if (fuse_enforce_dev_perms) {
165 * Check if mounter can open the fuse device.
167 * This has significance only if we are doing a secondary mount
168 * which doesn't involve actually opening fuse devices, but we
169 * still want to enforce the permissions of the device (in
170 * order to keep control over the circle of fuse users).
172 * (In case of primary mounts, we are either the superuser so
173 * we can do anything anyway, or we can mount only if the
174 * device is already opened by us, ie. we are permitted to open
179 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
183 err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
191 * according to coda code, no extra lock is needed --
192 * although in sys/vnode.h this field is marked "v"
196 if (!fdev->si_devsw ||
197 strcmp("fuse", fdev->si_devsw->d_name)) {
206 #define FUSE_FLAGOPT(fnam, fval) do { \
207 vfs_flagopt(opts, #fnam, &mntopts, fval); \
208 vfs_flagopt(opts, "__" #fnam, &__mntopts, fval); \
211 SDT_PROBE_DEFINE1(fusefs, , vfsops, mntopts, "uint64_t");
212 SDT_PROBE_DEFINE4(fusefs, , vfsops, mount_err, "char*", "struct fuse_data*",
213 "struct mount*", "int");
216 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
217 uint32_t max_read, int daemon_timeout)
220 struct fuse_data *data = fuse_get_mpdata(mp);
221 /* Don't allow these options to be changed */
222 const static unsigned long long cant_update_opts =
223 MNT_USER; /* Mount owner must be the user running the daemon */
227 if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
229 SDT_PROBE4(fusefs, , vfsops, mount_err,
230 "Can't change these mount options during remount",
234 if (((data->dataflags ^ mntopts) & FSESS_MNTOPTS_MASK) ||
235 (data->max_read != max_read) ||
236 (data->daemon_timeout != daemon_timeout)) {
237 // TODO: allow changing options where it makes sense
239 SDT_PROBE4(fusefs, , vfsops, mount_err,
240 "Can't change fuse mount options during remount",
245 if (fdata_get_dead(data)) {
247 SDT_PROBE4(fusefs, , vfsops, mount_err,
248 "device is dead during mount", data, mp, err);
252 /* Sanity + permission checks */
253 if (!data->daemoncred)
254 panic("fuse daemon found, but identity unknown");
255 if (mntopts & FSESS_DAEMON_CAN_SPY)
256 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
257 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
258 /* are we allowed to do the first mount? */
259 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
267 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
270 struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
271 struct fuse_vnode_data *fvdat;
275 if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
278 error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
284 if (fvdat->generation != ffhp->gen ) {
290 vnode_create_vobject(*vpp, 0, curthread);
295 fuse_vfsop_mount(struct mount *mp)
299 uint64_t mntopts, __mntopts;
306 struct fuse_data *data = NULL;
308 struct file *fp, *fptmp;
309 char *fspec, *subtype, *fsname = NULL;
311 struct vfsoptlist *opts;
321 /* Get the new options passed to mount */
322 opts = mp->mnt_optnew;
327 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
328 if (!vfs_getopts(opts, "fspath", &err))
332 * With the help of underscored options the mount program
333 * can inform us from the flags it sets by default
335 FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
336 FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
337 FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
338 FUSE_FLAGOPT(intr, FSESS_INTR);
340 (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
341 (void)vfs_scanopt(opts, "linux_errnos", "%d", &linux_errnos);
342 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
343 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
344 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
345 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
346 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
348 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
350 subtype = vfs_getopts(opts, "subtype=", &err);
352 SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
354 if (mp->mnt_flag & MNT_UPDATE) {
355 return fuse_vfs_remount(mp, td, mntopts, max_read,
359 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
360 fspec = vfs_getopts(opts, "from", &err);
364 /* `fd' contains the filedescriptor for this session; REQUIRED */
365 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
368 err = fuse_getdevice(fspec, td, &fdev);
372 err = fget(td, fd, &cap_read_rights, &fp);
374 SDT_PROBE2(fusefs, , vfsops, trace, 1,
375 "invalid or not opened device");
380 err = devfs_get_cdevpriv((void **)&data);
385 if (err != 0 || data == NULL) {
387 SDT_PROBE4(fusefs, , vfsops, mount_err,
388 "invalid or not opened device", data, mp, err);
392 if (fdata_get_dead(data)) {
394 SDT_PROBE4(fusefs, , vfsops, mount_err,
395 "device is dead during mount", data, mp, err);
399 /* Sanity + permission checks */
400 if (!data->daemoncred)
401 panic("fuse daemon found, but identity unknown");
402 if (mntopts & FSESS_DAEMON_CAN_SPY)
403 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
404 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
405 /* are we allowed to do the first mount? */
406 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
413 data->dataflags |= mntopts;
414 data->max_read = max_read;
415 data->daemon_timeout = daemon_timeout;
416 data->linux_errnos = linux_errnos;
417 data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
424 * FUSE file systems can be either local or remote, but the kernel
425 * can't tell the difference.
427 mp->mnt_flag &= ~MNT_LOCAL;
428 mp->mnt_kern_flag |= MNTK_USES_BCACHE;
430 * Disable nullfs cacheing because it can consume too many resources in
433 mp->mnt_kern_flag |= MNTK_NULL_NOCACHE;
435 /* We need this here as this slot is used by getnewvnode() */
436 mp->mnt_stat.f_iosize = maxbcachebuf;
438 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
439 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
441 memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
442 vfs_getopt(opts, "fsname=", (void**)&fsname, &fsnamelen);
443 strlcpy(mp->mnt_stat.f_mntfromname,
444 fsname == NULL ? fspec : fsname, MNAMELEN);
445 mp->mnt_iosize_max = maxphys;
447 /* Now handshaking with daemon */
448 fuse_internal_send_init(data, td);
453 if (data != NULL && data->mp == mp) {
455 * Destroy device only if we acquired reference to
458 SDT_PROBE4(fusefs, , vfsops, mount_err,
459 "mount failed, destroy device", data, mp, err);
462 fdata_trydestroy(data);
471 fuse_vfsop_unmount(struct mount *mp, int mntflags)
477 struct fuse_data *data;
478 struct fuse_dispatcher fdi;
479 struct thread *td = curthread;
481 if (mntflags & MNT_FORCE) {
484 data = fuse_get_mpdata(mp);
486 panic("no private data for mount point?");
488 /* There is 1 extra root vnode reference (mp->mnt_data). */
490 if (data->vroot != NULL) {
491 struct vnode *vroot = data->vroot;
498 err = vflush(mp, 0, flags, td);
502 if (fdata_get_dead(data)) {
505 if (fsess_maybe_impl(mp, FUSE_DESTROY)) {
507 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
509 (void)fdisp_wait_answ(&fdi);
513 fdata_set_dead(data);
519 fdata_trydestroy(data);
531 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
534 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
536 struct fuse_data *data = fuse_get_mpdata(mp);
537 uint64_t nodeid = ino;
538 struct thread *td = curthread;
539 struct fuse_dispatcher fdi;
540 struct fuse_entry_out *feo;
541 struct fuse_vnode_data *fvdat;
543 const char dot[] = ".";
544 __enum_uint8(vtype) vtyp;
547 if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
549 * Unreachable unless you do something stupid, like export a
550 * nullfs mount of a fusefs file system.
552 SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
556 error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
557 if (error || *vpp != NULL)
562 /* Do a LOOKUP, using nodeid as the parent and "." as filename */
563 fdisp_init(&fdi, sizeof(dot));
564 fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
565 memcpy(fdi.indata, dot, sizeof(dot));
566 error = fdisp_wait_answ(&fdi);
571 feo = (struct fuse_entry_out *)fdi.answ;
572 if (feo->nodeid == 0) {
573 /* zero nodeid means ENOENT and cache it */
578 vtyp = IFTOVT(feo->attr.mode);
579 error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
582 fvdat = VTOFUD(*vpp);
584 if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
586 * Attributes from the server are definitely newer than the
587 * last attributes we sent to the server, so cache them.
589 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
590 feo->attr_valid_nsec, NULL, true);
592 fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
593 &fvdat->entry_cache_timeout);
600 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
602 struct fuse_data *data = fuse_get_mpdata(mp);
605 if (data->vroot != NULL) {
606 err = vget(data->vroot, lkflags);
610 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
614 MPASS(data->vroot == NULL || data->vroot == *vpp);
615 if (data->vroot == NULL) {
616 SDT_PROBE2(fusefs, , vfsops, trace, 1,
621 } else if (data->vroot != *vpp) {
622 SDT_PROBE2(fusefs, , vfsops, trace, 1,
636 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
638 struct fuse_dispatcher fdi;
641 struct fuse_statfs_out *fsfo;
642 struct fuse_data *data;
644 data = fuse_get_mpdata(mp);
646 if (!(data->dataflags & FSESS_INITED))
650 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
651 err = fdisp_wait_answ(&fdi);
654 if (err == ENOTCONN) {
656 * We want to seem a legitimate fs even if the daemon
657 * is stiff dead... (so that, eg., we can still do path
658 * based unmounting after the daemon dies).
666 sbp->f_blocks = fsfo->st.blocks;
667 sbp->f_bfree = fsfo->st.bfree;
668 sbp->f_bavail = fsfo->st.bavail;
669 sbp->f_files = fsfo->st.files;
670 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */
671 sbp->f_namemax = fsfo->st.namelen;
672 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
684 sbp->f_bsize = S_BLKSIZE;