geom_kern.c revision 110592
1266988Smarkj/*-
2266988Smarkj * Copyright (c) 2002 Poul-Henning Kamp
3266988Smarkj * Copyright (c) 2002 Networks Associates Technology, Inc.
4266988Smarkj * All rights reserved.
5266988Smarkj *
6266988Smarkj * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7266988Smarkj * and NAI Labs, the Security Research Division of Network Associates, Inc.
8266988Smarkj * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9266988Smarkj * DARPA CHATS research program.
10266988Smarkj *
11266988Smarkj * Redistribution and use in source and binary forms, with or without
12266988Smarkj * modification, are permitted provided that the following conditions
13266988Smarkj * are met:
14266988Smarkj * 1. Redistributions of source code must retain the above copyright
15266988Smarkj *    notice, this list of conditions and the following disclaimer.
16266988Smarkj * 2. Redistributions in binary form must reproduce the above copyright
17266988Smarkj *    notice, this list of conditions and the following disclaimer in the
18266988Smarkj *    documentation and/or other materials provided with the distribution.
19266988Smarkj * 3. The names of the authors may not be used to endorse or promote
20266988Smarkj *    products derived from this software without specific prior written
21266988Smarkj *    permission.
22266988Smarkj *
23266988Smarkj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24266988Smarkj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25266988Smarkj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26266988Smarkj * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27266988Smarkj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28266988Smarkj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29266988Smarkj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30266988Smarkj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31266988Smarkj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32266988Smarkj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33266988Smarkj * SUCH DAMAGE.
34266988Smarkj *
35266988Smarkj * $FreeBSD: head/sys/geom/geom_kern.c 110592 2003-02-09 17:04:57Z phk $
36266988Smarkj */
37266988Smarkj
38266988Smarkj#include <sys/param.h>
39266988Smarkj#include <sys/systm.h>
40266988Smarkj#include <sys/kernel.h>
41266988Smarkj#include <sys/malloc.h>
42266988Smarkj#include <sys/bio.h>
43266988Smarkj#include <sys/sysctl.h>
44266988Smarkj#include <sys/proc.h>
45266988Smarkj#include <sys/kthread.h>
46266988Smarkj#include <sys/lock.h>
47266988Smarkj#include <sys/mutex.h>
48266988Smarkj#include <sys/sx.h>
49266988Smarkj#include <sys/sbuf.h>
50266988Smarkj#include <geom/geom.h>
51266988Smarkj#include <geom/geom_int.h>
52266988Smarkj#include <geom/geom_stats.h>
53266988Smarkj
54266988SmarkjMALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
55266988Smarkj
56266988Smarkjstruct sx topology_lock;
57266988Smarkj
58266988Smarkjstatic struct proc *g_up_proc;
59266988Smarkj
60int g_debugflags;
61int g_collectstats = 1;
62
63/*
64 * G_UP and G_DOWN are the two threads which push I/O through the
65 * stack.
66 *
67 * Things are procesed in a FIFO order, but these threads could be
68 * part of I/O prioritization by deciding which bios/bioqs to service
69 * in what order.
70 *
71 * We have only one thread in each direction, it is belived that until
72 * a very non-trivial workload in the UP/DOWN path this will be enough,
73 * but more than one can actually be run without problems.
74 *
75 * Holding the "mymutex" is a debugging feature:  It prevents people
76 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
77 * so almost invariably result in deadlocks since it stalls all I/O
78 * processing in the given direction.
79 */
80
81static void
82g_up_procbody(void)
83{
84	struct proc *p = g_up_proc;
85	struct thread *tp = FIRST_THREAD_IN_PROC(p);
86	struct mtx mymutex;
87
88	mtx_assert(&Giant, MA_NOTOWNED);
89	bzero(&mymutex, sizeof mymutex);
90	mtx_init(&mymutex, "g_up", MTX_DEF, 0);
91	mtx_lock(&mymutex);
92	tp->td_base_pri = PRIBIO;
93	for(;;) {
94		g_io_schedule_up(tp);
95		msleep(&g_wait_up, &mymutex, PRIBIO, "g_up", hz/10);
96	}
97}
98
99struct kproc_desc g_up_kp = {
100	"g_up",
101	g_up_procbody,
102	&g_up_proc,
103};
104
105static struct proc *g_down_proc;
106
107static void
108g_down_procbody(void)
109{
110	struct proc *p = g_down_proc;
111	struct thread *tp = FIRST_THREAD_IN_PROC(p);
112	struct mtx mymutex;
113
114	mtx_assert(&Giant, MA_NOTOWNED);
115	bzero(&mymutex, sizeof mymutex);
116	mtx_init(&mymutex, "g_down", MTX_DEF, 0);
117	mtx_lock(&mymutex);
118	tp->td_base_pri = PRIBIO;
119	for(;;) {
120		g_io_schedule_down(tp);
121		msleep(&g_wait_down, &mymutex, PRIBIO, "g_down", hz/10);
122	}
123}
124
125struct kproc_desc g_down_kp = {
126	"g_down",
127	g_down_procbody,
128	&g_down_proc,
129};
130
131static struct proc *g_event_proc;
132
133static void
134g_event_procbody(void)
135{
136	struct proc *p = g_event_proc;
137	struct thread *tp = FIRST_THREAD_IN_PROC(p);
138
139	mtx_assert(&Giant, MA_NOTOWNED);
140	tp->td_base_pri = PRIBIO;
141	for(;;) {
142		g_run_events();
143		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
144	}
145}
146
147struct kproc_desc g_event_kp = {
148	"g_event",
149	g_event_procbody,
150	&g_event_proc,
151};
152
153void
154g_init(void)
155{
156	printf("Initializing GEOMetry subsystem\n");
157	sx_init(&topology_lock, "GEOM topology");
158	g_stat_init();
159	g_io_init();
160	g_event_init();
161	mtx_lock(&Giant);
162	kproc_start(&g_event_kp);
163	kproc_start(&g_up_kp);
164	kproc_start(&g_down_kp);
165	mtx_unlock(&Giant);
166}
167
168static int
169sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
170{
171	int error;
172	struct sbuf *sb;
173
174	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
175	sbuf_clear(sb);
176	g_call_me(g_conftxt, sb);
177	do {
178		tsleep(sb, PZERO, "g_conftxt", hz);
179	} while(!sbuf_done(sb));
180	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
181	sbuf_delete(sb);
182	return error;
183}
184
185static int
186sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
187{
188	int error;
189	struct sbuf *sb;
190
191	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
192	sbuf_clear(sb);
193	g_call_me(g_confdot, sb);
194	do {
195		tsleep(sb, PZERO, "g_confdot", hz);
196	} while(!sbuf_done(sb));
197	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
198	sbuf_delete(sb);
199	return error;
200}
201
202static int
203sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
204{
205	int error;
206	struct sbuf *sb;
207
208	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
209	sbuf_clear(sb);
210	g_call_me(g_confxml, sb);
211	do {
212		tsleep(sb, PZERO, "g_confxml", hz);
213	} while(!sbuf_done(sb));
214	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
215	sbuf_delete(sb);
216	return error;
217}
218
219SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");
220
221SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD,
222	0, 0, sysctl_kern_geom_confxml, "",
223	"Dump the GEOM config in XML");
224
225SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD,
226	0, 0, sysctl_kern_geom_confdot, "",
227	"Dump the GEOM config in dot");
228
229SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD,
230	0, 0, sysctl_kern_geom_conftxt, "",
231	"Dump the GEOM config in txt");
232
233SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW,
234	&g_debugflags, 0, "");
235
236SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
237	&g_collectstats, 0, "");
238
239SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
240	0, sizeof(struct g_class), "");
241SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
242	0, sizeof(struct g_geom), "");
243SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
244	0, sizeof(struct g_provider), "");
245SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
246	0, sizeof(struct g_consumer), "");
247SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
248	0, sizeof(struct g_bioq), "");
249SYSCTL_INT(_debug_sizeof, OID_AUTO, g_event, CTLFLAG_RD,
250	0, sizeof(struct g_event), "");
251SYSCTL_INT(_debug_sizeof, OID_AUTO, g_stat, CTLFLAG_RD,
252	0, sizeof(struct g_stat), "");
253