geom_kern.c revision 113940
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 * $FreeBSD: head/sys/geom/geom_kern.c 113940 2003-04-23 21:28:27Z phk $
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/eventhandler.h>
42#include <sys/malloc.h>
43#include <sys/bio.h>
44#include <sys/sysctl.h>
45#include <sys/proc.h>
46#include <sys/kthread.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/sx.h>
50#include <sys/sbuf.h>
51#include <geom/geom.h>
52#include <geom/geom_int.h>
53
54MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
55
56struct sx topology_lock;
57
58static struct proc *g_up_proc;
59
60int g_debugflags;
61int g_collectstats = 1;
62int g_shutdown;
63
64/*
65 * G_UP and G_DOWN are the two threads which push I/O through the
66 * stack.
67 *
68 * Things are procesed in a FIFO order, but these threads could be
69 * part of I/O prioritization by deciding which bios/bioqs to service
70 * in what order.
71 *
72 * We have only one thread in each direction, it is belived that until
73 * a very non-trivial workload in the UP/DOWN path this will be enough,
74 * but more than one can actually be run without problems.
75 *
76 * Holding the "mymutex" is a debugging feature:  It prevents people
77 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
78 * so almost invariably result in deadlocks since it stalls all I/O
79 * processing in the given direction.
80 */
81
82static void
83g_up_procbody(void)
84{
85	struct proc *p = g_up_proc;
86	struct thread *tp = FIRST_THREAD_IN_PROC(p);
87
88	mtx_assert(&Giant, MA_NOTOWNED);
89	tp->td_base_pri = PRIBIO;
90	for(;;) {
91		g_io_schedule_up(tp);
92	}
93}
94
95struct kproc_desc g_up_kp = {
96	"g_up",
97	g_up_procbody,
98	&g_up_proc,
99};
100
101static struct proc *g_down_proc;
102
103static void
104g_down_procbody(void)
105{
106	struct proc *p = g_down_proc;
107	struct thread *tp = FIRST_THREAD_IN_PROC(p);
108
109	mtx_assert(&Giant, MA_NOTOWNED);
110	tp->td_base_pri = PRIBIO;
111	for(;;) {
112		g_io_schedule_down(tp);
113	}
114}
115
116struct kproc_desc g_down_kp = {
117	"g_down",
118	g_down_procbody,
119	&g_down_proc,
120};
121
122static struct proc *g_event_proc;
123
124static void
125g_event_procbody(void)
126{
127	struct proc *p = g_event_proc;
128	struct thread *tp = FIRST_THREAD_IN_PROC(p);
129
130	mtx_assert(&Giant, MA_NOTOWNED);
131	tp->td_base_pri = PRIBIO;
132	for(;;) {
133		g_run_events();
134		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
135	}
136}
137
138struct kproc_desc g_event_kp = {
139	"g_event",
140	g_event_procbody,
141	&g_event_proc,
142};
143
144static void
145geom_shutdown(void *foo __unused)
146{
147
148	g_shutdown = 1;
149}
150
151void
152g_init(void)
153{
154	sx_init(&topology_lock, "GEOM topology");
155	g_io_init();
156	g_event_init();
157	g_ctl_init();
158	mtx_lock(&Giant);
159	kproc_start(&g_event_kp);
160	kproc_start(&g_up_kp);
161	kproc_start(&g_down_kp);
162	mtx_unlock(&Giant);
163	EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
164		SHUTDOWN_PRI_FIRST);
165}
166
167static int
168sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
169{
170	int error;
171	struct sbuf *sb;
172
173	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
174	sbuf_clear(sb);
175	g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL);
176	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
177	sbuf_delete(sb);
178	return error;
179}
180
181static int
182sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
183{
184	int error;
185	struct sbuf *sb;
186
187	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
188	sbuf_clear(sb);
189	g_waitfor_event(g_confdot, sb, M_WAITOK, NULL);
190	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
191	sbuf_delete(sb);
192	return error;
193}
194
195static int
196sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
197{
198	int error;
199	struct sbuf *sb;
200
201	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
202	sbuf_clear(sb);
203	g_waitfor_event(g_confxml, sb, M_WAITOK, NULL);
204	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
205	sbuf_delete(sb);
206	return error;
207}
208
209SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");
210
211SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD,
212	0, 0, sysctl_kern_geom_confxml, "",
213	"Dump the GEOM config in XML");
214
215SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD,
216	0, 0, sysctl_kern_geom_confdot, "",
217	"Dump the GEOM config in dot");
218
219SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD,
220	0, 0, sysctl_kern_geom_conftxt, "",
221	"Dump the GEOM config in txt");
222
223SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW,
224	&g_debugflags, 0, "");
225
226SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
227	&g_collectstats, 0, "");
228
229SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
230	0, sizeof(struct g_class), "");
231SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
232	0, sizeof(struct g_geom), "");
233SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
234	0, sizeof(struct g_provider), "");
235SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
236	0, sizeof(struct g_consumer), "");
237SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
238	0, sizeof(struct g_bioq), "");
239