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