geom_kern.c revision 104063
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 104063 2002-09-27 21:24:40Z 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
62/*
63 * G_UP and G_DOWN are the two threads which push I/O through the
64 * stack.
65 *
66 * Things are procesed in a FIFO order, but these threads could be
67 * part of I/O prioritization by deciding which bios/bioqs to service
68 * in what order.
69 *
70 * We have only one thread in each direction, it is belived that until
71 * a very non-trivial workload in the UP/DOWN path this will be enough,
72 * but more than one can actually be run without problems.
73 *
74 * Holding the "mymutex" is a debugging feature:  It prevents people
75 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
76 * so almost invariably result in deadlocks since it stalls all I/O
77 * processing in the given direction.
78 */
79
80static void
81g_up_procbody(void)
82{
83	struct proc *p = g_up_proc;
84	struct thread *tp = FIRST_THREAD_IN_PROC(p);
85	struct mtx mymutex;
86
87	mtx_init(&mymutex, "g_up", MTX_DEF, 0);
88	mtx_lock(&mymutex);
89	curthread->td_base_pri = PRIBIO;
90	for(;;) {
91		g_io_schedule_up(tp);
92		msleep(&g_wait_up, &mymutex, PRIBIO, "g_up", hz/10);
93	}
94}
95
96struct kproc_desc g_up_kp = {
97	"g_up",
98	g_up_procbody,
99	&g_up_proc,
100};
101
102static struct proc *g_down_proc;
103
104static void
105g_down_procbody(void)
106{
107	struct proc *p = g_down_proc;
108	struct thread *tp = FIRST_THREAD_IN_PROC(p);
109	struct mtx mymutex;
110
111	mtx_init(&mymutex, "g_down", MTX_DEF, 0);
112	mtx_lock(&mymutex);
113	curthread->td_base_pri = PRIBIO;
114	for(;;) {
115		g_io_schedule_down(tp);
116		msleep(&g_wait_down, &mymutex, PRIBIO, "g_down", hz/10);
117	}
118}
119
120struct kproc_desc g_down_kp = {
121	"g_down",
122	g_down_procbody,
123	&g_down_proc,
124};
125
126static struct proc *g_event_proc;
127
128static void
129g_event_procbody(void)
130{
131
132	curthread->td_base_pri = PRIBIO;
133	for(;;) {
134		g_run_events();
135		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
136	}
137}
138
139struct kproc_desc g_event_kp = {
140	"g_event",
141	g_event_procbody,
142	&g_event_proc,
143};
144
145void
146g_init(void)
147{
148	printf("Initializing GEOMetry subsystem\n");
149	if (bootverbose)
150		g_debugflags |= G_T_TOPOLOGY;
151	sx_init(&topology_lock, "GEOM topology");
152	g_io_init();
153	g_event_init();
154	mtx_lock(&Giant);
155	kproc_start(&g_event_kp);
156	kproc_start(&g_up_kp);
157	kproc_start(&g_down_kp);
158	mtx_unlock(&Giant);
159}
160
161static int
162sysctl_debug_geomdot(SYSCTL_HANDLER_ARGS)
163{
164	int i, error;
165	struct sbuf *sb;
166
167	i = 0;
168	sb = g_confdot();
169	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
170	sbuf_delete(sb);
171	return (error);
172}
173
174SYSCTL_PROC(_debug, OID_AUTO, geomdot, CTLTYPE_STRING|CTLFLAG_RD,
175	0, 0, sysctl_debug_geomdot, "A",
176	"Dump the GEOM config");
177
178static int
179sysctl_debug_geomconf(SYSCTL_HANDLER_ARGS)
180{
181	int i, error;
182	struct sbuf *sb;
183
184	i = 0;
185	sb = g_conf();
186	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
187	sbuf_delete(sb);
188	return (error);
189}
190
191SYSCTL_PROC(_debug, OID_AUTO, geomconf, CTLTYPE_STRING|CTLFLAG_RD,
192	0, 0, sysctl_debug_geomconf, "A",
193	"Dump the GEOM config");
194
195SYSCTL_INT(_debug, OID_AUTO, geomdebugflags, CTLTYPE_INT|CTLFLAG_RW,
196	&g_debugflags, 0, "");
197
198SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLTYPE_INT|CTLFLAG_RD,
199	0, sizeof(struct g_class), "");
200SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLTYPE_INT|CTLFLAG_RD,
201	0, sizeof(struct g_geom), "");
202SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLTYPE_INT|CTLFLAG_RD,
203	0, sizeof(struct g_provider), "");
204SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLTYPE_INT|CTLFLAG_RD,
205	0, sizeof(struct g_consumer), "");
206SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLTYPE_INT|CTLFLAG_RD,
207	0, sizeof(struct g_bioq), "");
208SYSCTL_INT(_debug_sizeof, OID_AUTO, g_event, CTLTYPE_INT|CTLFLAG_RD,
209	0, sizeof(struct g_event), "");
210