geom_kern.c revision 93250
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 93250 2002-03-26 22:07:38Z phk $
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/bio.h>
43#include <sys/sysctl.h>
44#include <sys/proc.h>
45#include <sys/kthread.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/sx.h>
49#include <sys/sbuf.h>
50#include <sys/errno.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;
61
62static void
63g_up_procbody(void)
64{
65	struct proc *p = g_up_proc;
66	struct thread *tp = &p->p_xxthread;
67
68	curthread->td_base_pri = PRIBIO;
69	for(;;) {
70		g_io_schedule_up(tp);
71		mtx_lock(&Giant);
72		tsleep(&g_wait_up, PRIBIO, "g_up", hz/10);
73		mtx_unlock(&Giant);
74	}
75}
76
77struct kproc_desc g_up_kp = {
78	"g_up",
79	g_up_procbody,
80	&g_up_proc,
81};
82
83static struct proc *g_down_proc;
84
85static void
86g_down_procbody(void)
87{
88	struct proc *p = g_down_proc;
89	struct thread *tp = &p->p_xxthread;
90
91	curthread->td_base_pri = PRIBIO;
92	for(;;) {
93		g_io_schedule_down(tp);
94		mtx_lock(&Giant);
95		tsleep(&g_wait_down, PRIBIO, "g_down", hz/10);
96		mtx_unlock(&Giant);
97	}
98}
99
100struct kproc_desc g_down_kp = {
101	"g_down",
102	g_down_procbody,
103	&g_down_proc,
104};
105
106static struct proc *g_event_proc;
107
108static void
109g_event_procbody(void)
110{
111
112	curthread->td_base_pri = PRIBIO;
113	for(;;) {
114		g_run_events();
115		mtx_lock(&Giant);
116		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
117		mtx_unlock(&Giant);
118	}
119}
120
121struct kproc_desc g_event_kp = {
122	"g_event",
123	g_event_procbody,
124	&g_event_proc,
125};
126
127void
128g_init(void)
129{
130	printf("Initializing GEOMetry subsystem\n");
131	if (bootverbose)
132		g_debugflags |= G_T_TOPOLOGY;
133	sx_init(&topology_lock, "GEOM topology");
134	g_io_init();
135	g_event_init();
136	mtx_lock(&Giant);
137	kproc_start(&g_event_kp);
138	kproc_start(&g_up_kp);
139	kproc_start(&g_down_kp);
140	mtx_unlock(&Giant);
141}
142
143static int
144sysctl_debug_geomdot(SYSCTL_HANDLER_ARGS)
145{
146	int i, error;
147	struct sbuf *sb;
148
149	i = 0;
150	sb = g_confdot();
151	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
152	sbuf_delete(sb);
153	return (error);
154}
155
156SYSCTL_PROC(_debug, OID_AUTO, geomdot, CTLTYPE_STRING|CTLFLAG_RD,
157	0, 0, sysctl_debug_geomdot, "A",
158	"Dump the GEOM config");
159
160static int
161sysctl_debug_geomconf(SYSCTL_HANDLER_ARGS)
162{
163	int i, error;
164	struct sbuf *sb;
165
166	i = 0;
167	sb = g_conf();
168	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
169	sbuf_delete(sb);
170	return (error);
171}
172
173SYSCTL_PROC(_debug, OID_AUTO, geomconf, CTLTYPE_STRING|CTLFLAG_RD,
174	0, 0, sysctl_debug_geomconf, "A",
175	"Dump the GEOM config");
176
177SYSCTL_INT(_debug, OID_AUTO, geomdebugflags, CTLTYPE_INT|CTLFLAG_RW,
178	&g_debugflags, 0, "");
179
180SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLTYPE_INT|CTLFLAG_RD,
181	0, sizeof(struct g_class), "");
182SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLTYPE_INT|CTLFLAG_RD,
183	0, sizeof(struct g_geom), "");
184SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLTYPE_INT|CTLFLAG_RD,
185	0, sizeof(struct g_provider), "");
186SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLTYPE_INT|CTLFLAG_RD,
187	0, sizeof(struct g_consumer), "");
188SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLTYPE_INT|CTLFLAG_RD,
189	0, sizeof(struct g_bioq), "");
190SYSCTL_INT(_debug_sizeof, OID_AUTO, g_event, CTLTYPE_INT|CTLFLAG_RD,
191	0, sizeof(struct g_event), "");
192