xref: /freebsd/sys/fs/fuse/fuse_main.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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  * Copyright (c) 2019 The FreeBSD Foundation
37  *
38  * Portions of this software were developed by BFF Storage Systems, LLC under
39  * sponsorship from the FreeBSD Foundation.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include <sys/types.h>
67 #include <sys/param.h>
68 #include <sys/module.h>
69 #include <sys/systm.h>
70 #include <sys/errno.h>
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/conf.h>
74 #include <sys/mutex.h>
75 #include <sys/proc.h>
76 #include <sys/queue.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/stat.h>
80 #include <sys/file.h>
81 #include <sys/buf.h>
82 #include <sys/sdt.h>
83 #include <sys/sysctl.h>
84 
85 #include "fuse.h"
86 #include "fuse_file.h"
87 #include "fuse_ipc.h"
88 #include "fuse_internal.h"
89 #include "fuse_node.h"
90 
91 static void fuse_bringdown(eventhandler_tag eh_tag);
92 static int fuse_loader(struct module *m, int what, void *arg);
93 
94 struct mtx fuse_mtx;
95 
96 extern struct vfsops fuse_vfsops;
97 extern struct cdevsw fuse_cdevsw;
98 extern struct vop_vector fuse_fifonops;
99 extern uma_zone_t fuse_pbuf_zone;
100 
101 static struct vfsconf fuse_vfsconf = {
102 	.vfc_version = VFS_VERSION,
103 	.vfc_name = "fusefs",
104 	.vfc_vfsops = &fuse_vfsops,
105 	.vfc_typenum = -1,
106 	.vfc_flags = VFCF_JAIL | VFCF_SYNTHETIC
107 };
108 
109 SYSCTL_NODE(_vfs, OID_AUTO, fusefs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
110     "FUSE tunables");
111 SYSCTL_NODE(_vfs_fusefs, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
112     "FUSE statistics");
113 SYSCTL_INT(_vfs_fusefs, OID_AUTO, kernelabi_major, CTLFLAG_RD,
114     SYSCTL_NULL_INT_PTR, FUSE_KERNEL_VERSION, "FUSE kernel abi major version");
115 SYSCTL_INT(_vfs_fusefs, OID_AUTO, kernelabi_minor, CTLFLAG_RD,
116     SYSCTL_NULL_INT_PTR, FUSE_KERNEL_MINOR_VERSION, "FUSE kernel abi minor version");
117 SDT_PROVIDER_DEFINE(fusefs);
118 
119 /******************************
120  *
121  * >>> Module management stuff
122  *
123  ******************************/
124 
125 static void
126 fuse_bringdown(eventhandler_tag eh_tag)
127 {
128 	fuse_node_destroy();
129 	fuse_internal_destroy();
130 	fuse_file_destroy();
131 	fuse_ipc_destroy();
132 	fuse_device_destroy();
133 	mtx_destroy(&fuse_mtx);
134 }
135 
136 static int
137 fuse_loader(struct module *m, int what, void *arg)
138 {
139 	static eventhandler_tag eh_tag = NULL;
140 	int err = 0;
141 
142 	switch (what) {
143 	case MOD_LOAD:			/* kldload */
144 		mtx_init(&fuse_mtx, "fuse_mtx", NULL, MTX_DEF);
145 		err = fuse_device_init();
146 		if (err) {
147 			mtx_destroy(&fuse_mtx);
148 			return (err);
149 		}
150 		fuse_ipc_init();
151 		fuse_file_init();
152 		fuse_internal_init();
153 		fuse_node_init();
154 		fuse_pbuf_zone = pbuf_zsecond_create("fusepbuf", nswbuf / 2);
155 
156 		/* vfs_modevent ignores its first arg */
157 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
158 			fuse_bringdown(eh_tag);
159 		break;
160 	case MOD_UNLOAD:
161 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
162 			return (err);
163 		fuse_bringdown(eh_tag);
164 		uma_zdestroy(fuse_pbuf_zone);
165 		break;
166 	default:
167 		return (EINVAL);
168 	}
169 
170 	return (err);
171 }
172 
173 /* Registering the module */
174 
175 static moduledata_t fuse_moddata = {
176 	"fusefs",
177 	fuse_loader,
178 	&fuse_vfsconf
179 };
180 
181 DECLARE_MODULE(fusefs, fuse_moddata, SI_SUB_VFS, SI_ORDER_MIDDLE);
182 MODULE_VERSION(fusefs, 1);
183