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_node.h"
86 #include "fuse_internal.h"
89 #include <security/mac/mac_framework.h>
91 SDT_PROVIDER_DECLARE(fusefs);
94 * arg0: verbosity. Higher numbers give more verbose messages
95 * arg1: Textual message
97 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
99 /* This will do for privilege types for now */
100 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
101 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
103 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
104 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
106 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
107 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
110 static vfs_fhtovp_t fuse_vfsop_fhtovp;
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;
115 static vfs_vget_t fuse_vfsop_vget;
117 struct vfsops fuse_vfsops = {
118 .vfs_fhtovp = fuse_vfsop_fhtovp,
119 .vfs_mount = fuse_vfsop_mount,
120 .vfs_unmount = fuse_vfsop_unmount,
121 .vfs_root = fuse_vfsop_root,
122 .vfs_statfs = fuse_vfsop_statfs,
123 .vfs_vget = fuse_vfsop_vget,
126 static int fuse_enforce_dev_perms = 0;
128 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
129 &fuse_enforce_dev_perms, 0,
130 "enforce fuse device permissions for secondary mounts");
132 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
135 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
137 struct nameidata nd, *ndp = &nd;
143 * Not an update, or updating the name: look up the name
144 * and verify that it refers to a sensible disk device.
147 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
148 if ((err = namei(ndp)) != 0)
150 NDFREE(ndp, NDF_ONLY_PNBUF);
153 if (devvp->v_type != VCHR) {
157 fdev = devvp->v_rdev;
160 if (fuse_enforce_dev_perms) {
162 * Check if mounter can open the fuse device.
164 * This has significance only if we are doing a secondary mount
165 * which doesn't involve actually opening fuse devices, but we
166 * still want to enforce the permissions of the device (in
167 * order to keep control over the circle of fuse users).
169 * (In case of primary mounts, we are either the superuser so
170 * we can do anything anyway, or we can mount only if the
171 * device is already opened by us, ie. we are permitted to open
176 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
180 err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
188 * according to coda code, no extra lock is needed --
189 * although in sys/vnode.h this field is marked "v"
193 if (!fdev->si_devsw ||
194 strcmp("fuse", fdev->si_devsw->d_name)) {
203 #define FUSE_FLAGOPT(fnam, fval) do { \
204 vfs_flagopt(opts, #fnam, &mntopts, fval); \
205 vfs_flagopt(opts, "__" #fnam, &__mntopts, fval); \
208 SDT_PROBE_DEFINE1(fusefs, , vfsops, mntopts, "uint64_t");
209 SDT_PROBE_DEFINE4(fusefs, , vfsops, mount_err, "char*", "struct fuse_data*",
210 "struct mount*", "int");
213 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
214 uint32_t max_read, int daemon_timeout)
217 struct fuse_data *data = fuse_get_mpdata(mp);
218 /* Don't allow these options to be changed */
219 const static unsigned long long cant_update_opts =
220 MNT_USER; /* Mount owner must be the user running the daemon */
224 if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
226 SDT_PROBE4(fusefs, , vfsops, mount_err,
227 "Can't change these mount options during remount",
231 if (((data->dataflags ^ mntopts) & FSESS_MNTOPTS_MASK) ||
232 (data->max_read != max_read) ||
233 (data->daemon_timeout != daemon_timeout)) {
234 // TODO: allow changing options where it makes sense
236 SDT_PROBE4(fusefs, , vfsops, mount_err,
237 "Can't change fuse mount options during remount",
242 if (fdata_get_dead(data)) {
244 SDT_PROBE4(fusefs, , vfsops, mount_err,
245 "device is dead during mount", data, mp, err);
249 /* Sanity + permission checks */
250 if (!data->daemoncred)
251 panic("fuse daemon found, but identity unknown");
252 if (mntopts & FSESS_DAEMON_CAN_SPY)
253 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
254 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
255 /* are we allowed to do the first mount? */
256 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
264 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
267 struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
268 struct fuse_vnode_data *fvdat;
272 if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
275 error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
281 if (fvdat->generation != ffhp->gen ) {
287 vnode_create_vobject(*vpp, 0, curthread);
292 fuse_vfsop_mount(struct mount *mp)
296 uint64_t mntopts, __mntopts;
304 struct fuse_data *data = NULL;
306 struct file *fp, *fptmp;
307 char *fspec, *subtype;
308 struct vfsoptlist *opts;
318 mp->mnt_flag |= MNT_SYNCHRONOUS;
320 /* Get the new options passed to mount */
321 opts = mp->mnt_optnew;
326 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
327 if (!vfs_getopts(opts, "fspath", &err))
331 * With the help of underscored options the mount program
332 * can inform us from the flags it sets by default
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(no_attrcache, FSESS_NO_ATTRCACHE);
338 FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD);
339 FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE);
340 FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE);
341 FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP);
343 (void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
344 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
345 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
346 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
347 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
348 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
350 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
352 subtype = vfs_getopts(opts, "subtype=", &err);
354 SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
356 if (mp->mnt_flag & MNT_UPDATE) {
357 return fuse_vfs_remount(mp, td, mntopts, max_read,
361 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
362 fspec = vfs_getopts(opts, "from", &err);
366 /* `fd' contains the filedescriptor for this session; REQUIRED */
367 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
370 err = fuse_getdevice(fspec, td, &fdev);
374 err = fget(td, fd, &cap_read_rights, &fp);
376 SDT_PROBE2(fusefs, , vfsops, trace, 1,
377 "invalid or not opened device");
382 err = devfs_get_cdevpriv((void **)&data);
387 if (err != 0 || data == NULL) {
389 SDT_PROBE4(fusefs, , vfsops, mount_err,
390 "invalid or not opened device", data, mp, err);
394 if (fdata_get_dead(data)) {
396 SDT_PROBE4(fusefs, , vfsops, mount_err,
397 "device is dead during mount", data, mp, err);
401 /* Sanity + permission checks */
402 if (!data->daemoncred)
403 panic("fuse daemon found, but identity unknown");
404 if (mntopts & FSESS_DAEMON_CAN_SPY)
405 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
406 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
407 /* are we allowed to do the first mount? */
408 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
415 data->dataflags |= mntopts;
416 data->max_read = max_read;
417 data->daemon_timeout = daemon_timeout;
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 /* We need this here as this slot is used by getnewvnode() */
432 mp->mnt_stat.f_iosize = maxbcachebuf;
434 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
435 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
437 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
438 bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
440 /* Now handshaking with daemon */
441 fuse_internal_send_init(data, td);
446 if (data != NULL && data->mp == mp) {
448 * Destroy device only if we acquired reference to
451 SDT_PROBE4(fusefs, , vfsops, mount_err,
452 "mount failed, destroy device", data, mp, err);
455 fdata_trydestroy(data);
464 fuse_vfsop_unmount(struct mount *mp, int mntflags)
470 struct fuse_data *data;
471 struct fuse_dispatcher fdi;
472 struct thread *td = curthread;
474 if (mntflags & MNT_FORCE) {
477 data = fuse_get_mpdata(mp);
479 panic("no private data for mount point?");
481 /* There is 1 extra root vnode reference (mp->mnt_data). */
483 if (data->vroot != NULL) {
484 struct vnode *vroot = data->vroot;
491 err = vflush(mp, 0, flags, td);
495 if (fdata_get_dead(data)) {
499 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
501 err = fdisp_wait_answ(&fdi);
504 fdata_set_dead(data);
510 fdata_trydestroy(data);
523 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
525 uint64_t nodeid = ino;
526 struct thread *td = curthread;
527 struct fuse_dispatcher fdi;
528 struct fuse_entry_out *feo;
529 struct fuse_vnode_data *fvdat;
530 const char dot[] = ".";
535 error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
536 if (error || *vpp != NULL)
539 /* Do a LOOKUP, using nodeid as the parent and "." as filename */
540 fdisp_init(&fdi, sizeof(dot));
541 fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
542 memcpy(fdi.indata, dot, sizeof(dot));
543 error = fdisp_wait_answ(&fdi);
548 feo = (struct fuse_entry_out *)fdi.answ;
549 if (feo->nodeid == 0) {
550 /* zero nodeid means ENOENT and cache it */
555 vtyp = IFTOVT(feo->attr.mode);
556 error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
559 filesize = feo->attr.size;
562 * In the case where we are looking up a FUSE node represented by an
563 * existing cached vnode, and the true size reported by FUSE_LOOKUP
564 * doesn't match the vnode's cached size, then any cached writes beyond
565 * the file's current size are lost.
568 * * following attribute cache expiration, or
569 * * due a bug in the daemon, or
571 fvdat = VTOFUD(*vpp);
572 if (vnode_isreg(*vpp) &&
573 filesize != fvdat->cached_attrs.va_size &&
574 fvdat->flag & FN_SIZECHANGE) {
575 printf("%s: WB cache incoherent on %s!\n", __func__,
576 vnode_mount(*vpp)->mnt_stat.f_mntonname);
578 fvdat->flag &= ~FN_SIZECHANGE;
581 fuse_internal_cache_attrs(*vpp, td->td_ucred, &feo->attr,
582 feo->attr_valid, feo->attr_valid_nsec, NULL);
583 fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
584 &fvdat->entry_cache_timeout);
591 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
593 struct fuse_data *data = fuse_get_mpdata(mp);
596 if (data->vroot != NULL) {
597 err = vget(data->vroot, lkflags, curthread);
601 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
605 MPASS(data->vroot == NULL || data->vroot == *vpp);
606 if (data->vroot == NULL) {
607 SDT_PROBE2(fusefs, , vfsops, trace, 1,
612 } else if (data->vroot != *vpp) {
613 SDT_PROBE2(fusefs, , vfsops, trace, 1,
628 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
630 struct fuse_dispatcher fdi;
633 struct fuse_statfs_out *fsfo;
634 struct fuse_data *data;
636 data = fuse_get_mpdata(mp);
638 if (!(data->dataflags & FSESS_INITED))
642 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
643 err = fdisp_wait_answ(&fdi);
646 if (err == ENOTCONN) {
648 * We want to seem a legitimate fs even if the daemon
649 * is stiff dead... (so that, eg., we can still do path
650 * based unmounting after the daemon dies).
658 sbp->f_blocks = fsfo->st.blocks;
659 sbp->f_bfree = fsfo->st.bfree;
660 sbp->f_bavail = fsfo->st.bavail;
661 sbp->f_files = fsfo->st.files;
662 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */
663 sbp->f_namemax = fsfo->st.namelen;
664 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */
676 sbp->f_bsize = S_BLKSIZE;