xref: /freebsd/sys/fs/fuse/fuse_vfsops.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1 /*
2  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  *   copyright notice, this list of conditions and the following disclaimer
13  *   in the documentation and/or other materials provided with the
14  *   distribution.
15  * * Neither the name of Google Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from
17  *   this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Copyright (C) 2005 Csaba Henk.
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58 
59 #include <sys/types.h>
60 #include <sys/module.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/param.h>
64 #include <sys/kernel.h>
65 #include <sys/capability.h>
66 #include <sys/conf.h>
67 #include <sys/filedesc.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/lock.h>
72 #include <sys/sx.h>
73 #include <sys/mutex.h>
74 #include <sys/proc.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/mount.h>
78 #include <sys/sysctl.h>
79 #include <sys/fcntl.h>
80 
81 #include "fuse.h"
82 #include "fuse_param.h"
83 #include "fuse_node.h"
84 #include "fuse_ipc.h"
85 #include "fuse_internal.h"
86 
87 #include <sys/priv.h>
88 #include <security/mac/mac_framework.h>
89 
90 #define FUSE_DEBUG_MODULE VFSOPS
91 #include "fuse_debug.h"
92 
93 /* This will do for privilege types for now */
94 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
95 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
96 #endif
97 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
98 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
99 #endif
100 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
101 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
102 #endif
103 
104 static vfs_mount_t fuse_vfsop_mount;
105 static vfs_unmount_t fuse_vfsop_unmount;
106 static vfs_root_t fuse_vfsop_root;
107 static vfs_statfs_t fuse_vfsop_statfs;
108 
109 struct vfsops fuse_vfsops = {
110 	.vfs_mount = fuse_vfsop_mount,
111 	.vfs_unmount = fuse_vfsop_unmount,
112 	.vfs_root = fuse_vfsop_root,
113 	.vfs_statfs = fuse_vfsop_statfs,
114 };
115 
116 SYSCTL_INT(_vfs_fuse, OID_AUTO, init_backgrounded, CTLFLAG_RD,
117     0, 1, "indicate async handshake");
118 static int fuse_enforce_dev_perms = 0;
119 
120 SYSCTL_LONG(_vfs_fuse, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
121     &fuse_enforce_dev_perms, 0,
122     "enforce fuse device permissions for secondary mounts");
123 static unsigned sync_unmount = 1;
124 
125 SYSCTL_UINT(_vfs_fuse, OID_AUTO, sync_unmount, CTLFLAG_RW,
126     &sync_unmount, 0, "specify when to use synchronous unmount");
127 
128 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
129 
130 static int
131 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
132 {
133 	struct nameidata nd, *ndp = &nd;
134 	struct vnode *devvp;
135 	struct cdev *fdev;
136 	int err;
137 
138 	/*
139          * Not an update, or updating the name: look up the name
140          * and verify that it refers to a sensible disk device.
141          */
142 
143 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
144 	if ((err = namei(ndp)) != 0)
145 		return err;
146 	NDFREE(ndp, NDF_ONLY_PNBUF);
147 	devvp = ndp->ni_vp;
148 
149 	if (devvp->v_type != VCHR) {
150 		vrele(devvp);
151 		return ENXIO;
152 	}
153 	fdev = devvp->v_rdev;
154 	dev_ref(fdev);
155 
156 	if (fuse_enforce_dev_perms) {
157 		/*
158 	         * Check if mounter can open the fuse device.
159 	         *
160 	         * This has significance only if we are doing a secondary mount
161 	         * which doesn't involve actually opening fuse devices, but we
162 	         * still want to enforce the permissions of the device (in
163 	         * order to keep control over the circle of fuse users).
164 	         *
165 	         * (In case of primary mounts, we are either the superuser so
166 	         * we can do anything anyway, or we can mount only if the
167 	         * device is already opened by us, ie. we are permitted to open
168 	         * the device.)
169 	         */
170 #if 0
171 #ifdef MAC
172 		err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
173 		if (!err)
174 #endif
175 #endif /* 0 */
176 			err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
177 		if (err) {
178 			vrele(devvp);
179 			dev_rel(fdev);
180 			return err;
181 		}
182 	}
183 	/*
184          * according to coda code, no extra lock is needed --
185          * although in sys/vnode.h this field is marked "v"
186          */
187 	vrele(devvp);
188 
189 	if (!fdev->si_devsw ||
190 	    strcmp("fuse", fdev->si_devsw->d_name)) {
191 		dev_rel(fdev);
192 		return ENXIO;
193 	}
194 	*fdevp = fdev;
195 
196 	return 0;
197 }
198 
199 #define FUSE_FLAGOPT(fnam, fval) do {				\
200     vfs_flagopt(opts, #fnam, &mntopts, fval);		\
201     vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);	\
202 } while (0)
203 
204 static int
205 fuse_vfsop_mount(struct mount *mp)
206 {
207 	int err;
208 
209 	uint64_t mntopts, __mntopts;
210 	int max_read_set;
211 	uint32_t max_read;
212 	int daemon_timeout;
213 	int fd;
214 
215 	size_t len;
216 
217 	struct cdev *fdev;
218 	struct fuse_data *data;
219 	struct thread *td;
220 	struct file *fp, *fptmp;
221 	char *fspec, *subtype;
222 	struct vfsoptlist *opts;
223 
224 	subtype = NULL;
225 	max_read_set = 0;
226 	max_read = ~0;
227 	err = 0;
228 	mntopts = 0;
229 	__mntopts = 0;
230 	td = curthread;
231 
232 	fuse_trace_printf_vfsop();
233 
234 	if (mp->mnt_flag & MNT_UPDATE)
235 		return EOPNOTSUPP;
236 
237 	MNT_ILOCK(mp);
238 	mp->mnt_flag |= MNT_SYNCHRONOUS;
239 	mp->mnt_data = NULL;
240 	MNT_IUNLOCK(mp);
241 	/* Get the new options passed to mount */
242 	opts = mp->mnt_optnew;
243 
244 	if (!opts)
245 		return EINVAL;
246 
247 	/* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
248 	if (!vfs_getopts(opts, "fspath", &err))
249 		return err;
250 
251 	/* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
252 	fspec = vfs_getopts(opts, "from", &err);
253 	if (!fspec)
254 		return err;
255 
256 	/* `fd' contains the filedescriptor for this session; REQUIRED */
257 	if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
258 		return EINVAL;
259 
260 	err = fuse_getdevice(fspec, td, &fdev);
261 	if (err != 0)
262 		return err;
263 
264 	/*
265          * With the help of underscored options the mount program
266          * can inform us from the flags it sets by default
267          */
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);
277 
278 	if (vfs_scanopt(opts, "max_read=", "%u", &max_read) == 1)
279 		max_read_set = 1;
280 	if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
281 		if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
282 			daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
283 		else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
284 			daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
285 	} else {
286 		daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
287 	}
288 	subtype = vfs_getopts(opts, "subtype=", &err);
289 
290 	FS_DEBUG2G("mntopts 0x%jx\n", (uintmax_t)mntopts);
291 
292 	err = fget(td, fd, CAP_READ, &fp);
293 	if (err != 0) {
294 		FS_DEBUG("invalid or not opened device: data=%p\n", data);
295 		goto out;
296 	}
297 	fptmp = td->td_fpop;
298 	td->td_fpop = fp;
299         err = devfs_get_cdevpriv((void **)&data);
300 	td->td_fpop = fptmp;
301 	fdrop(fp, td);
302 	FUSE_LOCK();
303 	if (err != 0 || data == NULL || data->mp != NULL) {
304 		FS_DEBUG("invalid or not opened device: data=%p data.mp=%p\n",
305 		    data, data != NULL ? data->mp : NULL);
306 		err = ENXIO;
307 		FUSE_UNLOCK();
308 		goto out;
309 	}
310 	if (fdata_get_dead(data)) {
311 		FS_DEBUG("device is dead during mount: data=%p\n", data);
312 		err = ENOTCONN;
313 		FUSE_UNLOCK();
314 		goto out;
315 	}
316 	/* Sanity + permission checks */
317 	if (!data->daemoncred)
318 		panic("fuse daemon found, but identity unknown");
319 	if (mntopts & FSESS_DAEMON_CAN_SPY)
320 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
321 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
322 		/* are we allowed to do the first mount? */
323 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
324 	if (err) {
325 		FUSE_UNLOCK();
326 		goto out;
327 	}
328 	data->ref++;
329 	data->mp = mp;
330 	data->dataflags |= mntopts;
331 	data->max_read = max_read;
332 	data->daemon_timeout = daemon_timeout;
333 	FUSE_UNLOCK();
334 
335 	vfs_getnewfsid(mp);
336 	MNT_ILOCK(mp);
337 	mp->mnt_data = data;
338 	mp->mnt_flag |= MNT_LOCAL;
339 	MNT_IUNLOCK(mp);
340 	/* We need this here as this slot is used by getnewvnode() */
341 	mp->mnt_stat.f_iosize = PAGE_SIZE;
342 	if (subtype) {
343 		strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
344 		strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
345 	}
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);
349 
350 	/* Now handshaking with daemon */
351 	fuse_internal_send_init(data, td);
352 
353 out:
354 	if (err) {
355 		FUSE_LOCK();
356 		if (data->mp == mp) {
357 			/*
358 			 * Destroy device only if we acquired reference to
359 			 * it
360 			 */
361 			FS_DEBUG("mount failed, destroy device: data=%p mp=%p"
362 			      " err=%d\n",
363 			    data, mp, err);
364 			data->mp = NULL;
365 			fdata_trydestroy(data);
366 		}
367 		FUSE_UNLOCK();
368 		dev_rel(fdev);
369 	}
370 	return err;
371 }
372 
373 static int
374 fuse_vfsop_unmount(struct mount *mp, int mntflags)
375 {
376 	int err = 0;
377 	int flags = 0;
378 
379 	struct cdev *fdev;
380 	struct fuse_data *data;
381 	struct fuse_dispatcher fdi;
382 	struct thread *td = curthread;
383 
384 	fuse_trace_printf_vfsop();
385 
386 	if (mntflags & MNT_FORCE) {
387 		flags |= FORCECLOSE;
388 	}
389 	data = fuse_get_mpdata(mp);
390 	if (!data) {
391 		panic("no private data for mount point?");
392 	}
393 	/* There is 1 extra root vnode reference (mp->mnt_data). */
394 	FUSE_LOCK();
395 	if (data->vroot != NULL) {
396 		struct vnode *vroot = data->vroot;
397 
398 		data->vroot = NULL;
399 		FUSE_UNLOCK();
400 		vrele(vroot);
401 	} else
402 		FUSE_UNLOCK();
403 	err = vflush(mp, 0, flags, td);
404 	if (err) {
405 		debug_printf("vflush failed");
406 		return err;
407 	}
408 	if (fdata_get_dead(data)) {
409 		goto alreadydead;
410 	}
411 	fdisp_init(&fdi, 0);
412 	fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
413 
414 	err = fdisp_wait_answ(&fdi);
415 	fdisp_destroy(&fdi);
416 
417 	fdata_set_dead(data);
418 
419 alreadydead:
420 	FUSE_LOCK();
421 	data->mp = NULL;
422 	fdev = data->fdev;
423 	fdata_trydestroy(data);
424 	FUSE_UNLOCK();
425 
426 	MNT_ILOCK(mp);
427 	mp->mnt_data = NULL;
428 	mp->mnt_flag &= ~MNT_LOCAL;
429 	MNT_IUNLOCK(mp);
430 
431 	dev_rel(fdev);
432 
433 	return 0;
434 }
435 
436 static int
437 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
438 {
439 	struct fuse_data *data = fuse_get_mpdata(mp);
440 	int err = 0;
441 
442 	if (data->vroot != NULL) {
443 		err = vget(data->vroot, lkflags, curthread);
444 		if (err == 0)
445 			*vpp = data->vroot;
446 	} else {
447 		err = fuse_vnode_get(mp, FUSE_ROOT_ID, NULL, vpp, NULL, VDIR);
448 		if (err == 0) {
449 			FUSE_LOCK();
450 			MPASS(data->vroot == NULL || data->vroot == *vpp);
451 			if (data->vroot == NULL) {
452 				FS_DEBUG("new root vnode\n");
453 				data->vroot = *vpp;
454 				FUSE_UNLOCK();
455 				vref(*vpp);
456 			} else if (data->vroot != *vpp) {
457 				FS_DEBUG("root vnode race\n");
458 				FUSE_UNLOCK();
459 				VOP_UNLOCK(*vpp, 0);
460 				vrele(*vpp);
461 				vrecycle(*vpp);
462 				*vpp = data->vroot;
463 			} else
464 				FUSE_UNLOCK();
465 		}
466 	}
467 	return err;
468 }
469 
470 static int
471 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
472 {
473 	struct fuse_dispatcher fdi;
474 	int err = 0;
475 
476 	struct fuse_statfs_out *fsfo;
477 	struct fuse_data *data;
478 
479 	FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname);
480 	data = fuse_get_mpdata(mp);
481 
482 	if (!(data->dataflags & FSESS_INITED))
483 		goto fake;
484 
485 	fdisp_init(&fdi, 0);
486 	fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
487 	err = fdisp_wait_answ(&fdi);
488 	if (err) {
489 		fdisp_destroy(&fdi);
490 		if (err == ENOTCONN) {
491 			/*
492 	                 * We want to seem a legitimate fs even if the daemon
493 	                 * is stiff dead... (so that, eg., we can still do path
494 	                 * based unmounting after the daemon dies).
495 	                 */
496 			goto fake;
497 		}
498 		return err;
499 	}
500 	fsfo = fdi.answ;
501 
502 	sbp->f_blocks = fsfo->st.blocks;
503 	sbp->f_bfree = fsfo->st.bfree;
504 	sbp->f_bavail = fsfo->st.bavail;
505 	sbp->f_files = fsfo->st.files;
506 	sbp->f_ffree = fsfo->st.ffree;	/* cast from uint64_t to int64_t */
507 	sbp->f_namemax = fsfo->st.namelen;
508 	sbp->f_bsize = fsfo->st.frsize;	/* cast from uint32_t to uint64_t */
509 
510 	FS_DEBUG("fuse_statfs_out -- blocks: %llu, bfree: %llu, bavail: %llu, "
511 	      "fil	es: %llu, ffree: %llu, bsize: %i, namelen: %i\n",
512 	      (unsigned long long)fsfo->st.blocks,
513 	      (unsigned long long)fsfo->st.bfree,
514 	      (unsigned long long)fsfo->st.bavail,
515 	      (unsigned long long)fsfo->st.files,
516 	      (unsigned long long)fsfo->st.ffree, fsfo->st.bsize,
517 	      fsfo->st.namelen);
518 
519 	fdisp_destroy(&fdi);
520 	return 0;
521 
522 fake:
523 	sbp->f_blocks = 0;
524 	sbp->f_bfree = 0;
525 	sbp->f_bavail = 0;
526 	sbp->f_files = 0;
527 	sbp->f_ffree = 0;
528 	sbp->f_namemax = 0;
529 	sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE;
530 
531 	return 0;
532 }
533