xref: /freebsd/sys/fs/fuse/fuse_main.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * Copyright (c) 2007-2009 Google Inc.
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/conf.h>
66 #include <sys/mutex.h>
67 #include <sys/proc.h>
68 #include <sys/mount.h>
69 #include <sys/vnode.h>
70 #include <sys/stat.h>
71 #include <sys/file.h>
72 #include <sys/buf.h>
73 #include <sys/sysctl.h>
74 
75 #include "fuse.h"
76 
77 static void fuse_bringdown(eventhandler_tag eh_tag);
78 static int fuse_loader(struct module *m, int what, void *arg);
79 
80 struct mtx fuse_mtx;
81 
82 extern struct vfsops fuse_vfsops;
83 extern struct cdevsw fuse_cdevsw;
84 extern struct vop_vector fuse_vnops;
85 extern int fuse_pbuf_freecnt;
86 
87 static struct vfsconf fuse_vfsconf = {
88 	.vfc_version = VFS_VERSION,
89 	.vfc_name = "fusefs",
90 	.vfc_vfsops = &fuse_vfsops,
91 	.vfc_typenum = -1,
92 	.vfc_flags = VFCF_SYNTHETIC
93 };
94 
95 SYSCTL_INT(_vfs_fuse, OID_AUTO, kernelabi_major, CTLFLAG_RD,
96     0, FUSE_KERNEL_VERSION, "FUSE kernel abi major version");
97 SYSCTL_INT(_vfs_fuse, OID_AUTO, kernelabi_minor, CTLFLAG_RD,
98     0, FUSE_KERNEL_MINOR_VERSION, "FUSE kernel abi minor version");
99 
100 /******************************
101  *
102  * >>> Module management stuff
103  *
104  ******************************/
105 
106 static void
107 fuse_bringdown(eventhandler_tag eh_tag)
108 {
109 
110 	fuse_ipc_destroy();
111 	fuse_device_destroy();
112 	mtx_destroy(&fuse_mtx);
113 }
114 
115 static int
116 fuse_loader(struct module *m, int what, void *arg)
117 {
118 	static eventhandler_tag eh_tag = NULL;
119 	int err = 0;
120 
121 	switch (what) {
122 	case MOD_LOAD:			/* kldload */
123 		fuse_pbuf_freecnt = nswbuf / 2 + 1;
124 		mtx_init(&fuse_mtx, "fuse_mtx", NULL, MTX_DEF);
125 		err = fuse_device_init();
126 		if (err) {
127 			mtx_destroy(&fuse_mtx);
128 			return (err);
129 		}
130 		fuse_ipc_init();
131 
132 		/* vfs_modevent ignores its first arg */
133 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
134 			fuse_bringdown(eh_tag);
135 		else
136 			printf("fuse-freebsd: version %s, FUSE ABI %d.%d\n",
137 			    FUSE_FREEBSD_VERSION,
138 			    FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
139 
140 		break;
141 	case MOD_UNLOAD:
142 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
143 			return (err);
144 		fuse_bringdown(eh_tag);
145 		break;
146 	default:
147 		return (EINVAL);
148 	}
149 
150 	return (err);
151 }
152 
153 /* Registering the module */
154 
155 static moduledata_t fuse_moddata = {
156 	"fuse",
157 	fuse_loader,
158 	&fuse_vfsconf
159 };
160 
161 DECLARE_MODULE(fuse, fuse_moddata, SI_SUB_VFS, SI_ORDER_MIDDLE);
162 MODULE_VERSION(fuse, 1);
163