xref: /freebsd/sys/geom/geom_kern.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/bio.h>
47 #include <sys/sysctl.h>
48 #include <sys/proc.h>
49 #include <sys/unistd.h>
50 #include <sys/kthread.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/sbuf.h>
54 #include <sys/sched.h>
55 #include <sys/sx.h>
56 #include <geom/geom.h>
57 #include <geom/geom_int.h>
58 
59 MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
60 
61 struct sx topology_lock;
62 
63 static struct proc *g_proc;
64 static struct thread *g_up_td;
65 static struct thread *g_down_td;
66 static struct thread *g_event_td;
67 
68 int g_debugflags;
69 int g_collectstats = 1;
70 int g_shutdown;
71 int g_notaste;
72 
73 /*
74  * G_UP and G_DOWN are the two threads which push I/O through the
75  * stack.
76  *
77  * Things are procesed in a FIFO order, but these threads could be
78  * part of I/O prioritization by deciding which bios/bioqs to service
79  * in what order.
80  *
81  * We have only one thread in each direction, it is believed that until
82  * a very non-trivial workload in the UP/DOWN path this will be enough,
83  * but more than one can actually be run without problems.
84  *
85  * Holding the "mymutex" is a debugging feature:  It prevents people
86  * from sleeping in the UP/DOWN I/O path by mistake or design (doing
87  * so almost invariably result in deadlocks since it stalls all I/O
88  * processing in the given direction.
89  */
90 
91 static void
92 g_up_procbody(void *arg)
93 {
94 
95 	thread_lock(g_up_td);
96 	sched_prio(g_up_td, PRIBIO);
97 	thread_unlock(g_up_td);
98 	for(;;) {
99 		g_io_schedule_up(g_up_td);
100 	}
101 }
102 
103 static void
104 g_down_procbody(void *arg)
105 {
106 
107 	thread_lock(g_down_td);
108 	sched_prio(g_down_td, PRIBIO);
109 	thread_unlock(g_down_td);
110 	for(;;) {
111 		g_io_schedule_down(g_down_td);
112 	}
113 }
114 
115 static void
116 g_event_procbody(void *arg)
117 {
118 
119 	thread_lock(g_event_td);
120 	sched_prio(g_event_td, PRIBIO);
121 	thread_unlock(g_event_td);
122 	g_run_events();
123 	/* NOTREACHED */
124 }
125 
126 int
127 g_is_geom_thread(struct thread *td)
128 {
129 
130 	return (td == g_up_td || td == g_down_td || td == g_event_td);
131 }
132 
133 static void
134 geom_shutdown(void *foo __unused)
135 {
136 
137 	g_shutdown = 1;
138 }
139 
140 void
141 g_init(void)
142 {
143 
144 	g_trace(G_T_TOPOLOGY, "g_ignition");
145 	sx_init(&topology_lock, "GEOM topology");
146 	g_io_init();
147 	g_event_init();
148 	g_ctl_init();
149 	kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
150 	    RFHIGHPID, 0, "geom", "g_event");
151 	kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
152 	    RFHIGHPID, 0, "geom", "g_up");
153 	kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
154 	    RFHIGHPID, 0, "geom", "g_down");
155 	EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
156 		SHUTDOWN_PRI_FIRST);
157 }
158 
159 static int
160 sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
161 {
162 	int error;
163 	struct sbuf *sb;
164 
165 	sb = sbuf_new_auto();
166 	g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL);
167 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
168 	sbuf_delete(sb);
169 	return error;
170 }
171 
172 static int
173 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
174 {
175 	int error;
176 	struct sbuf *sb;
177 
178 	sb = sbuf_new_auto();
179 	g_waitfor_event(g_confdot, sb, M_WAITOK, NULL);
180 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
181 	sbuf_delete(sb);
182 	return error;
183 }
184 
185 static int
186 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
187 {
188 	int error;
189 	struct sbuf *sb;
190 
191 	sb = sbuf_new_auto();
192 	g_waitfor_event(g_confxml, sb, M_WAITOK, NULL);
193 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
194 	sbuf_delete(sb);
195 	return error;
196 }
197 
198 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");
199 
200 SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD,
201 	0, 0, sysctl_kern_geom_confxml, "",
202 	"Dump the GEOM config in XML");
203 
204 SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD,
205 	0, 0, sysctl_kern_geom_confdot, "",
206 	"Dump the GEOM config in dot");
207 
208 SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD,
209 	0, 0, sysctl_kern_geom_conftxt, "",
210 	"Dump the GEOM config in txt");
211 
212 SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
213 	&g_debugflags, 0, "Set various trace levels for GEOM debugging");
214 
215 SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
216 	&g_notaste, 0, "Prevent GEOM tasting");
217 
218 SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
219 	&g_collectstats, 0,
220 	"Control statistics collection on GEOM providers and consumers");
221 
222 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
223 	SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
224 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
225 	SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
226 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
227 	SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
228 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
229 	SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
230 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
231 	SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");
232