xref: /freebsd/sys/fs/fuse/fuse_main.c (revision 5fe580195f54892e4d3d0e878b011d5c71460a66)
1*5fe58019SAttilio Rao /*
2*5fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc.
3*5fe58019SAttilio Rao  * All rights reserved.
4*5fe58019SAttilio Rao  *
5*5fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
6*5fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
7*5fe58019SAttilio Rao  * met:
8*5fe58019SAttilio Rao  *
9*5fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
10*5fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
11*5fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
12*5fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
13*5fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
14*5fe58019SAttilio Rao  *   distribution.
15*5fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
16*5fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
17*5fe58019SAttilio Rao  *   this software without specific prior written permission.
18*5fe58019SAttilio Rao  *
19*5fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*5fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*5fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*5fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*5fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*5fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*5fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*5fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*5fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*5fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*5fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*5fe58019SAttilio Rao  *
31*5fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
32*5fe58019SAttilio Rao  * All rights reserved.
33*5fe58019SAttilio Rao  *
34*5fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
35*5fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
36*5fe58019SAttilio Rao  * are met:
37*5fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
38*5fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
39*5fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
40*5fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
41*5fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
42*5fe58019SAttilio Rao  *
43*5fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44*5fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45*5fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46*5fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47*5fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48*5fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49*5fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50*5fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51*5fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52*5fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53*5fe58019SAttilio Rao  * SUCH DAMAGE.
54*5fe58019SAttilio Rao  */
55*5fe58019SAttilio Rao 
56*5fe58019SAttilio Rao #include <sys/cdefs.h>
57*5fe58019SAttilio Rao __FBSDID("$FreeBSD$");
58*5fe58019SAttilio Rao 
59*5fe58019SAttilio Rao #include <sys/types.h>
60*5fe58019SAttilio Rao #include <sys/module.h>
61*5fe58019SAttilio Rao #include <sys/systm.h>
62*5fe58019SAttilio Rao #include <sys/errno.h>
63*5fe58019SAttilio Rao #include <sys/param.h>
64*5fe58019SAttilio Rao #include <sys/kernel.h>
65*5fe58019SAttilio Rao #include <sys/conf.h>
66*5fe58019SAttilio Rao #include <sys/mutex.h>
67*5fe58019SAttilio Rao #include <sys/proc.h>
68*5fe58019SAttilio Rao #include <sys/mount.h>
69*5fe58019SAttilio Rao #include <sys/vnode.h>
70*5fe58019SAttilio Rao #include <sys/stat.h>
71*5fe58019SAttilio Rao #include <sys/file.h>
72*5fe58019SAttilio Rao #include <sys/buf.h>
73*5fe58019SAttilio Rao #include <sys/sysctl.h>
74*5fe58019SAttilio Rao 
75*5fe58019SAttilio Rao #include "fuse.h"
76*5fe58019SAttilio Rao 
77*5fe58019SAttilio Rao static void fuse_bringdown(eventhandler_tag eh_tag);
78*5fe58019SAttilio Rao static int fuse_loader(struct module *m, int what, void *arg);
79*5fe58019SAttilio Rao 
80*5fe58019SAttilio Rao struct mtx fuse_mtx;
81*5fe58019SAttilio Rao 
82*5fe58019SAttilio Rao extern struct vfsops fuse_vfsops;
83*5fe58019SAttilio Rao extern struct cdevsw fuse_cdevsw;
84*5fe58019SAttilio Rao extern struct vop_vector fuse_vnops;
85*5fe58019SAttilio Rao extern int fuse_pbuf_freecnt;
86*5fe58019SAttilio Rao 
87*5fe58019SAttilio Rao static struct vfsconf fuse_vfsconf = {
88*5fe58019SAttilio Rao 	.vfc_version = VFS_VERSION,
89*5fe58019SAttilio Rao 	.vfc_name = "fusefs",
90*5fe58019SAttilio Rao 	.vfc_vfsops = &fuse_vfsops,
91*5fe58019SAttilio Rao 	.vfc_typenum = -1,
92*5fe58019SAttilio Rao 	.vfc_flags = VFCF_SYNTHETIC
93*5fe58019SAttilio Rao };
94*5fe58019SAttilio Rao 
95*5fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, kernelabi_major, CTLFLAG_RD,
96*5fe58019SAttilio Rao     0, FUSE_KERNEL_VERSION, "FUSE kernel abi major version");
97*5fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, kernelabi_minor, CTLFLAG_RD,
98*5fe58019SAttilio Rao     0, FUSE_KERNEL_MINOR_VERSION, "FUSE kernel abi minor version");
99*5fe58019SAttilio Rao 
100*5fe58019SAttilio Rao /******************************
101*5fe58019SAttilio Rao  *
102*5fe58019SAttilio Rao  * >>> Module management stuff
103*5fe58019SAttilio Rao  *
104*5fe58019SAttilio Rao  ******************************/
105*5fe58019SAttilio Rao 
106*5fe58019SAttilio Rao static void
107*5fe58019SAttilio Rao fuse_bringdown(eventhandler_tag eh_tag)
108*5fe58019SAttilio Rao {
109*5fe58019SAttilio Rao 
110*5fe58019SAttilio Rao 	fuse_ipc_destroy();
111*5fe58019SAttilio Rao 	fuse_device_destroy();
112*5fe58019SAttilio Rao 	mtx_destroy(&fuse_mtx);
113*5fe58019SAttilio Rao }
114*5fe58019SAttilio Rao 
115*5fe58019SAttilio Rao static int
116*5fe58019SAttilio Rao fuse_loader(struct module *m, int what, void *arg)
117*5fe58019SAttilio Rao {
118*5fe58019SAttilio Rao 	static eventhandler_tag eh_tag = NULL;
119*5fe58019SAttilio Rao 	int err = 0;
120*5fe58019SAttilio Rao 
121*5fe58019SAttilio Rao 	switch (what) {
122*5fe58019SAttilio Rao 	case MOD_LOAD:			/* kldload */
123*5fe58019SAttilio Rao 		fuse_pbuf_freecnt = nswbuf / 2 + 1;
124*5fe58019SAttilio Rao 		mtx_init(&fuse_mtx, "fuse_mtx", NULL, MTX_DEF);
125*5fe58019SAttilio Rao 		err = fuse_device_init();
126*5fe58019SAttilio Rao 		if (err) {
127*5fe58019SAttilio Rao 			mtx_destroy(&fuse_mtx);
128*5fe58019SAttilio Rao 			return (err);
129*5fe58019SAttilio Rao 		}
130*5fe58019SAttilio Rao 		fuse_ipc_init();
131*5fe58019SAttilio Rao 
132*5fe58019SAttilio Rao 		/* vfs_modevent ignores its first arg */
133*5fe58019SAttilio Rao 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
134*5fe58019SAttilio Rao 			fuse_bringdown(eh_tag);
135*5fe58019SAttilio Rao 		else
136*5fe58019SAttilio Rao 			printf("fuse-freebsd: version %s, FUSE ABI %d.%d\n",
137*5fe58019SAttilio Rao 			    FUSE_FREEBSD_VERSION,
138*5fe58019SAttilio Rao 			    FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
139*5fe58019SAttilio Rao 
140*5fe58019SAttilio Rao 		break;
141*5fe58019SAttilio Rao 	case MOD_UNLOAD:
142*5fe58019SAttilio Rao 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
143*5fe58019SAttilio Rao 			return (err);
144*5fe58019SAttilio Rao 		fuse_bringdown(eh_tag);
145*5fe58019SAttilio Rao 		break;
146*5fe58019SAttilio Rao 	default:
147*5fe58019SAttilio Rao 		return (EINVAL);
148*5fe58019SAttilio Rao 	}
149*5fe58019SAttilio Rao 
150*5fe58019SAttilio Rao 	return (err);
151*5fe58019SAttilio Rao }
152*5fe58019SAttilio Rao 
153*5fe58019SAttilio Rao /* Registering the module */
154*5fe58019SAttilio Rao 
155*5fe58019SAttilio Rao static moduledata_t fuse_moddata = {
156*5fe58019SAttilio Rao 	"fuse",
157*5fe58019SAttilio Rao 	fuse_loader,
158*5fe58019SAttilio Rao 	&fuse_vfsconf
159*5fe58019SAttilio Rao };
160*5fe58019SAttilio Rao 
161*5fe58019SAttilio Rao DECLARE_MODULE(fuse, fuse_moddata, SI_SUB_VFS, SI_ORDER_MIDDLE);
162*5fe58019SAttilio Rao MODULE_VERSION(fuse, 1);
163