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/types.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/param.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 #define FUSE_DEBUG_MODULE VFSOPS
93 #include "fuse_debug.h"
95 /* This will do for privilege types for now */
96 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
97 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
99 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
100 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
102 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
103 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
106 static vfs_mount_t fuse_vfsop_mount;
107 static vfs_unmount_t fuse_vfsop_unmount;
108 static vfs_root_t fuse_vfsop_root;
109 static vfs_statfs_t fuse_vfsop_statfs;
111 struct vfsops fuse_vfsops = {
112 .vfs_mount = fuse_vfsop_mount,
113 .vfs_unmount = fuse_vfsop_unmount,
114 .vfs_root = fuse_vfsop_root,
115 .vfs_statfs = fuse_vfsop_statfs,
118 SYSCTL_INT(_vfs_fuse, OID_AUTO, init_backgrounded, CTLFLAG_RD,
119 SYSCTL_NULL_INT_PTR, 1, "indicate async handshake");
120 static int fuse_enforce_dev_perms = 0;
122 SYSCTL_INT(_vfs_fuse, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
123 &fuse_enforce_dev_perms, 0,
124 "enforce fuse device permissions for secondary mounts");
125 static unsigned sync_unmount = 1;
127 SYSCTL_UINT(_vfs_fuse, OID_AUTO, sync_unmount, CTLFLAG_RW,
128 &sync_unmount, 0, "specify when to use synchronous unmount");
130 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
133 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
135 struct nameidata nd, *ndp = &nd;
141 * Not an update, or updating the name: look up the name
142 * and verify that it refers to a sensible disk device.
145 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
146 if ((err = namei(ndp)) != 0)
148 NDFREE(ndp, NDF_ONLY_PNBUF);
151 if (devvp->v_type != VCHR) {
155 fdev = devvp->v_rdev;
158 if (fuse_enforce_dev_perms) {
160 * Check if mounter can open the fuse device.
162 * This has significance only if we are doing a secondary mount
163 * which doesn't involve actually opening fuse devices, but we
164 * still want to enforce the permissions of the device (in
165 * order to keep control over the circle of fuse users).
167 * (In case of primary mounts, we are either the superuser so
168 * we can do anything anyway, or we can mount only if the
169 * device is already opened by us, ie. we are permitted to open
174 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
178 err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
186 * according to coda code, no extra lock is needed --
187 * although in sys/vnode.h this field is marked "v"
191 if (!fdev->si_devsw ||
192 strcmp("fuse", fdev->si_devsw->d_name)) {
201 #define FUSE_FLAGOPT(fnam, fval) do { \
202 vfs_flagopt(opts, #fnam, &mntopts, fval); \
203 vfs_flagopt(opts, "__" #fnam, &__mntopts, fval); \
207 fuse_vfsop_mount(struct mount *mp)
211 uint64_t mntopts, __mntopts;
219 struct fuse_data *data;
221 struct file *fp, *fptmp;
222 char *fspec, *subtype;
223 struct vfsoptlist *opts;
232 fuse_trace_printf_vfsop();
234 if (mp->mnt_flag & MNT_UPDATE)
238 mp->mnt_flag |= MNT_SYNCHRONOUS;
241 /* Get the new options passed to mount */
242 opts = mp->mnt_optnew;
247 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
248 if (!vfs_getopts(opts, "fspath", &err))
251 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
252 fspec = vfs_getopts(opts, "from", &err);
256 /* `fd' contains the filedescriptor for this session; REQUIRED */
257 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
260 err = fuse_getdevice(fspec, td, &fdev);
265 * With the help of underscored options the mount program
266 * can inform us from the flags it sets by default
268 FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
269 FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
270 FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
271 FUSE_FLAGOPT(no_attrcache, FSESS_NO_ATTRCACHE);
272 FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD);
273 FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE);
274 FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE);
275 FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP);
276 FUSE_FLAGOPT(brokenio, FSESS_BROKENIO);
278 (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
279 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
280 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
281 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
282 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
283 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
285 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
287 subtype = vfs_getopts(opts, "subtype=", &err);
289 FS_DEBUG2G("mntopts 0x%jx\n", (uintmax_t)mntopts);
291 err = fget(td, fd, &cap_read_rights, &fp);
293 FS_DEBUG("invalid or not opened device: data=%p\n", data);
298 err = devfs_get_cdevpriv((void **)&data);
302 if (err != 0 || data == NULL || data->mp != NULL) {
303 FS_DEBUG("invalid or not opened device: data=%p data.mp=%p\n",
304 data, data != NULL ? data->mp : NULL);
309 if (fdata_get_dead(data)) {
310 FS_DEBUG("device is dead during mount: data=%p\n", data);
315 /* Sanity + permission checks */
316 if (!data->daemoncred)
317 panic("fuse daemon found, but identity unknown");
318 if (mntopts & FSESS_DAEMON_CAN_SPY)
319 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
320 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
321 /* are we allowed to do the first mount? */
322 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
329 data->dataflags |= mntopts;
330 data->max_read = max_read;
331 data->daemon_timeout = daemon_timeout;
337 mp->mnt_flag |= MNT_LOCAL;
338 mp->mnt_kern_flag |= MNTK_USES_BCACHE;
340 /* We need this here as this slot is used by getnewvnode() */
341 mp->mnt_stat.f_iosize = MIN(DFLTPHYS, MAXBSIZE);
343 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
344 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
346 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
347 bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
348 FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname);
350 /* Now handshaking with daemon */
351 fuse_internal_send_init(data, td);
356 if (data->mp == mp) {
358 * Destroy device only if we acquired reference to
361 FS_DEBUG("mount failed, destroy device: data=%p mp=%p"
365 fdata_trydestroy(data);
374 fuse_vfsop_unmount(struct mount *mp, int mntflags)
380 struct fuse_data *data;
381 struct fuse_dispatcher fdi;
382 struct thread *td = curthread;
384 fuse_trace_printf_vfsop();
386 if (mntflags & MNT_FORCE) {
389 data = fuse_get_mpdata(mp);
391 panic("no private data for mount point?");
393 /* There is 1 extra root vnode reference (mp->mnt_data). */
395 if (data->vroot != NULL) {
396 struct vnode *vroot = data->vroot;
403 err = vflush(mp, 0, flags, td);
405 debug_printf("vflush failed");
408 if (fdata_get_dead(data)) {
412 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
414 err = fdisp_wait_answ(&fdi);
417 fdata_set_dead(data);
423 fdata_trydestroy(data);
428 mp->mnt_flag &= ~MNT_LOCAL;
437 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
439 struct fuse_data *data = fuse_get_mpdata(mp);
442 if (data->vroot != NULL) {
443 err = vget(data->vroot, lkflags, curthread);
447 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
451 MPASS(data->vroot == NULL || data->vroot == *vpp);
452 if (data->vroot == NULL) {
453 FS_DEBUG("new root vnode\n");
457 } else if (data->vroot != *vpp) {
458 FS_DEBUG("root vnode race\n");
472 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
474 struct fuse_dispatcher fdi;
477 struct fuse_statfs_out *fsfo;
478 struct fuse_data *data;
480 FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname);
481 data = fuse_get_mpdata(mp);
483 if (!(data->dataflags & FSESS_INITED))
487 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
488 err = fdisp_wait_answ(&fdi);
491 if (err == ENOTCONN) {
493 * We want to seem a legitimate fs even if the daemon
494 * is stiff dead... (so that, eg., we can still do path
495 * based unmounting after the daemon dies).
503 sbp->f_blocks = fsfo->st.blocks;
504 sbp->f_bfree = fsfo->st.bfree;
505 sbp->f_bavail = fsfo->st.bavail;
506 sbp->f_files = fsfo->st.files;
507 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */
508 sbp->f_namemax = fsfo->st.namelen;
509 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
511 FS_DEBUG("fuse_statfs_out -- blocks: %llu, bfree: %llu, bavail: %llu, "
512 "fil es: %llu, ffree: %llu, bsize: %i, namelen: %i\n",
513 (unsigned long long)fsfo->st.blocks,
514 (unsigned long long)fsfo->st.bfree,
515 (unsigned long long)fsfo->st.bavail,
516 (unsigned long long)fsfo->st.files,
517 (unsigned long long)fsfo->st.ffree, fsfo->st.bsize,
530 sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE;