]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_vfsops.c
MFHead@r345275
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_vfsops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
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
16  *   distribution.
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.
20  *
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.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
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.
44  *
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
55  * SUCH DAMAGE.
56  */
57
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
61 #include <sys/param.h>
62 #include <sys/buf.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>
68 #include <sys/conf.h>
69 #include <sys/filedesc.h>
70 #include <sys/uio.h>
71 #include <sys/malloc.h>
72 #include <sys/queue.h>
73 #include <sys/lock.h>
74 #include <sys/sx.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.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>
82
83 #include "fuse.h"
84 #include "fuse_param.h"
85 #include "fuse_node.h"
86 #include "fuse_ipc.h"
87 #include "fuse_internal.h"
88
89 #include <sys/priv.h>
90 #include <security/mac/mac_framework.h>
91
92 SDT_PROVIDER_DECLARE(fuse);
93 /* 
94  * Fuse trace probe:
95  * arg0: verbosity.  Higher numbers give more verbose messages
96  * arg1: Textual message
97  */
98 SDT_PROBE_DEFINE2(fuse, , vfsops, trace, "int", "char*");
99
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
103 #endif
104 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
105 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
106 #endif
107 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
108 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
109 #endif
110
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
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,
121 };
122
123 SYSCTL_INT(_vfs_fuse, OID_AUTO, init_backgrounded, CTLFLAG_RD,
124     SYSCTL_NULL_INT_PTR, 1, "indicate async handshake");
125 static int fuse_enforce_dev_perms = 0;
126
127 SYSCTL_INT(_vfs_fuse, 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;
131
132 SYSCTL_UINT(_vfs_fuse, OID_AUTO, sync_unmount, CTLFLAG_RW,
133     &sync_unmount, 0, "specify when to use synchronous unmount");
134
135 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
136
137 static int
138 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
139 {
140         struct nameidata nd, *ndp = &nd;
141         struct vnode *devvp;
142         struct cdev *fdev;
143         int err;
144
145         /*
146          * Not an update, or updating the name: look up the name
147          * and verify that it refers to a sensible disk device.
148          */
149
150         NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
151         if ((err = namei(ndp)) != 0)
152                 return err;
153         NDFREE(ndp, NDF_ONLY_PNBUF);
154         devvp = ndp->ni_vp;
155
156         if (devvp->v_type != VCHR) {
157                 vrele(devvp);
158                 return ENXIO;
159         }
160         fdev = devvp->v_rdev;
161         dev_ref(fdev);
162
163         if (fuse_enforce_dev_perms) {
164                 /*
165                  * Check if mounter can open the fuse device.
166                  *
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).
171                  *
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
175                  * the device.)
176                  */
177 #if 0
178 #ifdef MAC
179                 err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
180                 if (!err)
181 #endif
182 #endif /* 0 */
183                         err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
184                 if (err) {
185                         vrele(devvp);
186                         dev_rel(fdev);
187                         return err;
188                 }
189         }
190         /*
191          * according to coda code, no extra lock is needed --
192          * although in sys/vnode.h this field is marked "v"
193          */
194         vrele(devvp);
195
196         if (!fdev->si_devsw ||
197             strcmp("fuse", fdev->si_devsw->d_name)) {
198                 dev_rel(fdev);
199                 return ENXIO;
200         }
201         *fdevp = fdev;
202
203         return 0;
204 }
205
206 #define FUSE_FLAGOPT(fnam, fval) do {                           \
207         vfs_flagopt(opts, #fnam, &mntopts, fval);               \
208         vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);        \
209 } while (0)
210
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");
214
215 static int
216 fuse_vfsop_mount(struct mount *mp)
217 {
218         int err;
219
220         uint64_t mntopts, __mntopts;
221         uint32_t max_read;
222         int daemon_timeout;
223         int fd;
224
225         size_t len;
226
227         struct cdev *fdev;
228         struct fuse_data *data;
229         struct thread *td;
230         struct file *fp, *fptmp;
231         char *fspec, *subtype;
232         struct vfsoptlist *opts;
233
234         subtype = NULL;
235         max_read = ~0;
236         err = 0;
237         mntopts = 0;
238         __mntopts = 0;
239         td = curthread;
240
241         if (mp->mnt_flag & MNT_UPDATE)
242                 return EOPNOTSUPP;
243
244         MNT_ILOCK(mp);
245         mp->mnt_flag |= MNT_SYNCHRONOUS;
246         mp->mnt_data = NULL;
247         MNT_IUNLOCK(mp);
248         /* Get the new options passed to mount */
249         opts = mp->mnt_optnew;
250
251         if (!opts)
252                 return EINVAL;
253
254         /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
255         if (!vfs_getopts(opts, "fspath", &err))
256                 return err;
257
258         /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
259         fspec = vfs_getopts(opts, "from", &err);
260         if (!fspec)
261                 return err;
262
263         /* `fd' contains the filedescriptor for this session; REQUIRED */
264         if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
265                 return EINVAL;
266
267         err = fuse_getdevice(fspec, td, &fdev);
268         if (err != 0)
269                 return err;
270
271         /*
272          * With the help of underscored options the mount program
273          * can inform us from the flags it sets by default
274          */
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);
284
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;
291         } else {
292                 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
293         }
294         subtype = vfs_getopts(opts, "subtype=", &err);
295
296         SDT_PROBE1(fuse, , vfsops, mntopts, mntopts);
297
298         err = fget(td, fd, &cap_read_rights, &fp);
299         if (err != 0) {
300                 SDT_PROBE2(fuse, , vfsops, trace, 1,
301                         "invalid or not opened device");
302                 goto out;
303         }
304         fptmp = td->td_fpop;
305         td->td_fpop = fp;
306         err = devfs_get_cdevpriv((void **)&data);
307         td->td_fpop = fptmp;
308         fdrop(fp, td);
309         FUSE_LOCK();
310         if (err != 0 || data == NULL || data->mp != NULL) {
311                 err = ENXIO;
312                 SDT_PROBE4(fuse, , vfsops, mount_err,
313                         "invalid or not opened device", data, mp, err);
314                 FUSE_UNLOCK();
315                 goto out;
316         }
317         if (fdata_get_dead(data)) {
318                 err = ENOTCONN;
319                 SDT_PROBE4(fuse, , vfsops, mount_err,
320                         "device is dead during mount", data, mp, err);
321                 FUSE_UNLOCK();
322                 goto out;
323         }
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);
332         if (err) {
333                 FUSE_UNLOCK();
334                 goto out;
335         }
336         data->ref++;
337         data->mp = mp;
338         data->dataflags |= mntopts;
339         data->max_read = max_read;
340         data->daemon_timeout = daemon_timeout;
341         FUSE_UNLOCK();
342
343         vfs_getnewfsid(mp);
344         MNT_ILOCK(mp);
345         mp->mnt_data = data;
346         mp->mnt_flag |= MNT_LOCAL;
347         mp->mnt_kern_flag |= MNTK_USES_BCACHE;
348         MNT_IUNLOCK(mp);
349         /* We need this here as this slot is used by getnewvnode() */
350         mp->mnt_stat.f_iosize = maxbcachebuf;
351         if (subtype) {
352                 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
353                 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
354         }
355         copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
356         bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
357
358         /* Now handshaking with daemon */
359         fuse_internal_send_init(data, td);
360
361 out:
362         if (err) {
363                 FUSE_LOCK();
364                 if (data->mp == mp) {
365                         /*
366                          * Destroy device only if we acquired reference to
367                          * it
368                          */
369                         SDT_PROBE4(fuse, , vfsops, mount_err,
370                                 "mount failed, destroy device", data, mp, err);
371                         data->mp = NULL;
372                         fdata_trydestroy(data);
373                 }
374                 FUSE_UNLOCK();
375                 dev_rel(fdev);
376         }
377         return err;
378 }
379
380 static int
381 fuse_vfsop_unmount(struct mount *mp, int mntflags)
382 {
383         int err = 0;
384         int flags = 0;
385
386         struct cdev *fdev;
387         struct fuse_data *data;
388         struct fuse_dispatcher fdi;
389         struct thread *td = curthread;
390
391         if (mntflags & MNT_FORCE) {
392                 flags |= FORCECLOSE;
393         }
394         data = fuse_get_mpdata(mp);
395         if (!data) {
396                 panic("no private data for mount point?");
397         }
398         /* There is 1 extra root vnode reference (mp->mnt_data). */
399         FUSE_LOCK();
400         if (data->vroot != NULL) {
401                 struct vnode *vroot = data->vroot;
402
403                 data->vroot = NULL;
404                 FUSE_UNLOCK();
405                 vrele(vroot);
406         } else
407                 FUSE_UNLOCK();
408         err = vflush(mp, 0, flags, td);
409         if (err) {
410                 return err;
411         }
412         if (fdata_get_dead(data)) {
413                 goto alreadydead;
414         }
415         fdisp_init(&fdi, 0);
416         fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
417
418         err = fdisp_wait_answ(&fdi);
419         fdisp_destroy(&fdi);
420
421         fdata_set_dead(data);
422
423 alreadydead:
424         FUSE_LOCK();
425         data->mp = NULL;
426         fdev = data->fdev;
427         fdata_trydestroy(data);
428         FUSE_UNLOCK();
429
430         MNT_ILOCK(mp);
431         mp->mnt_data = NULL;
432         mp->mnt_flag &= ~MNT_LOCAL;
433         MNT_IUNLOCK(mp);
434
435         dev_rel(fdev);
436
437         return 0;
438 }
439
440 static int
441 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
442 {
443         struct fuse_data *data = fuse_get_mpdata(mp);
444         int err = 0;
445
446         if (data->vroot != NULL) {
447                 err = vget(data->vroot, lkflags, curthread);
448                 if (err == 0)
449                         *vpp = data->vroot;
450         } else {
451                 err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
452                     VDIR);
453                 if (err == 0) {
454                         FUSE_LOCK();
455                         MPASS(data->vroot == NULL || data->vroot == *vpp);
456                         if (data->vroot == NULL) {
457                                 SDT_PROBE2(fuse, , vfsops, trace, 1,
458                                         "new root vnode");
459                                 data->vroot = *vpp;
460                                 FUSE_UNLOCK();
461                                 vref(*vpp);
462                         } else if (data->vroot != *vpp) {
463                                 SDT_PROBE2(fuse, , vfsops, trace, 1,
464                                         "root vnode race");
465                                 FUSE_UNLOCK();
466                                 VOP_UNLOCK(*vpp, 0);
467                                 vrele(*vpp);
468                                 vrecycle(*vpp);
469                                 *vpp = data->vroot;
470                         } else
471                                 FUSE_UNLOCK();
472                 }
473         }
474         return err;
475 }
476
477 static int
478 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
479 {
480         struct fuse_dispatcher fdi;
481         int err = 0;
482
483         struct fuse_statfs_out *fsfo;
484         struct fuse_data *data;
485
486         data = fuse_get_mpdata(mp);
487
488         if (!(data->dataflags & FSESS_INITED))
489                 goto fake;
490
491         fdisp_init(&fdi, 0);
492         fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
493         err = fdisp_wait_answ(&fdi);
494         if (err) {
495                 fdisp_destroy(&fdi);
496                 if (err == ENOTCONN) {
497                         /*
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).
501                          */
502                         goto fake;
503                 }
504                 return err;
505         }
506         fsfo = fdi.answ;
507
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 */
515
516         fdisp_destroy(&fdi);
517         return 0;
518
519 fake:
520         sbp->f_blocks = 0;
521         sbp->f_bfree = 0;
522         sbp->f_bavail = 0;
523         sbp->f_files = 0;
524         sbp->f_ffree = 0;
525         sbp->f_namemax = 0;
526         sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE;
527
528         return 0;
529 }