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 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.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>
69 #include <sys/filedesc.h>
71 #include <sys/malloc.h>
72 #include <sys/queue.h>
75 #include <sys/mutex.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>
84 #include "fuse_param.h"
85 #include "fuse_node.h"
87 #include "fuse_internal.h"
90 #include <security/mac/mac_framework.h>
92 SDT_PROVIDER_DECLARE(fuse);
95 * arg0: verbosity. Higher numbers give more verbose messages
96 * arg1: Textual message
98 SDT_PROBE_DEFINE2(fuse, , vfsops, trace, "int", "char*");
100 /* This will do for privilege types for now */
101 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
102 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
104 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
105 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
107 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
108 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
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;
116 struct vfsops fuse_vfsops = {
117 .vfs_mount = fuse_vfsop_mount,
118 .vfs_unmount = fuse_vfsop_unmount,
119 .vfs_root = fuse_vfsop_root,
120 .vfs_statfs = fuse_vfsop_statfs,
123 SYSCTL_INT(_vfs_fusefs, OID_AUTO, init_backgrounded, CTLFLAG_RD,
124 SYSCTL_NULL_INT_PTR, 1, "indicate async handshake");
125 static int fuse_enforce_dev_perms = 0;
127 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
128 &fuse_enforce_dev_perms, 0,
129 "enforce fuse device permissions for secondary mounts");
130 static unsigned sync_unmount = 1;
132 SYSCTL_UINT(_vfs_fusefs, OID_AUTO, sync_unmount, CTLFLAG_RW,
133 &sync_unmount, 0, "specify when to use synchronous unmount");
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, td);
151 if ((err = namei(ndp)) != 0)
153 NDFREE(ndp, NDF_ONLY_PNBUF);
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(fuse, , vfsops, mntopts, "uint64_t");
212 SDT_PROBE_DEFINE4(fuse, , vfsops, mount_err, "char*", "struct fuse_data*",
213 "struct mount*", "int");
216 fuse_vfsop_mount(struct mount *mp)
220 uint64_t mntopts, __mntopts;
228 struct fuse_data *data;
230 struct file *fp, *fptmp;
231 char *fspec, *subtype;
232 struct vfsoptlist *opts;
241 if (mp->mnt_flag & MNT_UPDATE)
245 mp->mnt_flag |= MNT_SYNCHRONOUS;
248 /* Get the new options passed to mount */
249 opts = mp->mnt_optnew;
254 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
255 if (!vfs_getopts(opts, "fspath", &err))
258 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
259 fspec = vfs_getopts(opts, "from", &err);
263 /* `fd' contains the filedescriptor for this session; REQUIRED */
264 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
267 err = fuse_getdevice(fspec, td, &fdev);
272 * With the help of underscored options the mount program
273 * can inform us from the flags it sets by default
275 FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
276 FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
277 FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
278 FUSE_FLAGOPT(no_attrcache, FSESS_NO_ATTRCACHE);
279 FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD);
280 FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE);
281 FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE);
282 FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP);
283 FUSE_FLAGOPT(brokenio, FSESS_BROKENIO);
285 (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
286 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
287 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
288 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
289 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
290 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
292 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
294 subtype = vfs_getopts(opts, "subtype=", &err);
296 SDT_PROBE1(fuse, , vfsops, mntopts, mntopts);
298 err = fget(td, fd, &cap_read_rights, &fp);
300 SDT_PROBE2(fuse, , vfsops, trace, 1,
301 "invalid or not opened device");
306 err = devfs_get_cdevpriv((void **)&data);
310 if (err != 0 || data == NULL || data->mp != NULL) {
312 SDT_PROBE4(fuse, , vfsops, mount_err,
313 "invalid or not opened device", data, mp, err);
317 if (fdata_get_dead(data)) {
319 SDT_PROBE4(fuse, , vfsops, mount_err,
320 "device is dead during mount", data, mp, err);
324 /* Sanity + permission checks */
325 if (!data->daemoncred)
326 panic("fuse daemon found, but identity unknown");
327 if (mntopts & FSESS_DAEMON_CAN_SPY)
328 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
329 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
330 /* are we allowed to do the first mount? */
331 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
338 data->dataflags |= mntopts;
339 data->max_read = max_read;
340 data->daemon_timeout = daemon_timeout;
346 mp->mnt_flag |= MNT_LOCAL;
347 mp->mnt_kern_flag |= MNTK_USES_BCACHE;
349 /* We need this here as this slot is used by getnewvnode() */
350 mp->mnt_stat.f_iosize = maxbcachebuf;
352 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
353 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
355 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
356 bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
358 /* Now handshaking with daemon */
359 fuse_internal_send_init(data, td);
364 if (data->mp == mp) {
366 * Destroy device only if we acquired reference to
369 SDT_PROBE4(fuse, , vfsops, mount_err,
370 "mount failed, destroy device", data, mp, err);
372 fdata_trydestroy(data);
381 fuse_vfsop_unmount(struct mount *mp, int mntflags)
387 struct fuse_data *data;
388 struct fuse_dispatcher fdi;
389 struct thread *td = curthread;
391 if (mntflags & MNT_FORCE) {
394 data = fuse_get_mpdata(mp);
396 panic("no private data for mount point?");
398 /* There is 1 extra root vnode reference (mp->mnt_data). */
400 if (data->vroot != NULL) {
401 struct vnode *vroot = data->vroot;
408 err = vflush(mp, 0, flags, td);
412 if (fdata_get_dead(data)) {
416 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
418 err = fdisp_wait_answ(&fdi);
421 fdata_set_dead(data);
427 fdata_trydestroy(data);
432 mp->mnt_flag &= ~MNT_LOCAL;
441 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
443 struct fuse_data *data = fuse_get_mpdata(mp);
446 if (data->vroot != NULL) {
447 err = vget(data->vroot, lkflags, curthread);
451 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
455 MPASS(data->vroot == NULL || data->vroot == *vpp);
456 if (data->vroot == NULL) {
457 SDT_PROBE2(fuse, , vfsops, trace, 1,
462 } else if (data->vroot != *vpp) {
463 SDT_PROBE2(fuse, , vfsops, trace, 1,
478 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
480 struct fuse_dispatcher fdi;
483 struct fuse_statfs_out *fsfo;
484 struct fuse_data *data;
486 data = fuse_get_mpdata(mp);
488 if (!(data->dataflags & FSESS_INITED))
492 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
493 err = fdisp_wait_answ(&fdi);
496 if (err == ENOTCONN) {
498 * We want to seem a legitimate fs even if the daemon
499 * is stiff dead... (so that, eg., we can still do path
500 * based unmounting after the daemon dies).
508 sbp->f_blocks = fsfo->st.blocks;
509 sbp->f_bfree = fsfo->st.bfree;
510 sbp->f_bavail = fsfo->st.bavail;
511 sbp->f_files = fsfo->st.files;
512 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */
513 sbp->f_namemax = fsfo->st.namelen;
514 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
526 sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE;