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