geom_kern.c revision 104359
1169092Sdeischen/*-
2169092Sdeischen * Copyright (c) 2002 Poul-Henning Kamp
3169092Sdeischen * Copyright (c) 2002 Networks Associates Technology, Inc.
4156608Sdeischen * All rights reserved.
5156608Sdeischen *
6169092Sdeischen * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7156608Sdeischen * and NAI Labs, the Security Research Division of Network Associates, Inc.
8156608Sdeischen * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9156608Sdeischen * DARPA CHATS research program.
10156608Sdeischen *
11156608Sdeischen * Redistribution and use in source and binary forms, with or without
12169092Sdeischen * modification, are permitted provided that the following conditions
13156608Sdeischen * are met:
14156608Sdeischen * 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 104359 2002-10-02 07:51:02Z 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 <geom/geom.h>
51#include <geom/geom_int.h>
52
53MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
54
55struct sx topology_lock;
56
57static struct proc *g_up_proc;
58
59int g_debugflags;
60
61/*
62 * G_UP and G_DOWN are the two threads which push I/O through the
63 * stack.
64 *
65 * Things are procesed in a FIFO order, but these threads could be
66 * part of I/O prioritization by deciding which bios/bioqs to service
67 * in what order.
68 *
69 * We have only one thread in each direction, it is belived that until
70 * a very non-trivial workload in the UP/DOWN path this will be enough,
71 * but more than one can actually be run without problems.
72 *
73 * Holding the "mymutex" is a debugging feature:  It prevents people
74 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
75 * so almost invariably result in deadlocks since it stalls all I/O
76 * processing in the given direction.
77 */
78
79static void
80g_up_procbody(void)
81{
82	struct proc *p = g_up_proc;
83	struct thread *tp = FIRST_THREAD_IN_PROC(p);
84	struct mtx mymutex;
85
86	bzero(&mymutex, sizeof mymutex);
87	mtx_init(&mymutex, "g_up", MTX_DEF, 0);
88	mtx_lock(&mymutex);
89	tp->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	bzero(&mymutex, sizeof mymutex);
112	mtx_init(&mymutex, "g_down", MTX_DEF, 0);
113	mtx_lock(&mymutex);
114	tp->td_base_pri = PRIBIO;
115	for(;;) {
116		g_io_schedule_down(tp);
117		msleep(&g_wait_down, &mymutex, PRIBIO, "g_down", hz/10);
118	}
119}
120
121struct kproc_desc g_down_kp = {
122	"g_down",
123	g_down_procbody,
124	&g_down_proc,
125};
126
127static struct proc *g_event_proc;
128
129static void
130g_event_procbody(void)
131{
132	struct proc *p = g_down_proc;
133	struct thread *tp = FIRST_THREAD_IN_PROC(p);
134
135	tp->td_base_pri = PRIBIO;
136	for(;;) {
137		g_run_events();
138		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
139	}
140}
141
142struct kproc_desc g_event_kp = {
143	"g_event",
144	g_event_procbody,
145	&g_event_proc,
146};
147
148void
149g_init(void)
150{
151	printf("Initializing GEOMetry subsystem\n");
152	if (bootverbose)
153		g_debugflags |= G_T_TOPOLOGY;
154	sx_init(&topology_lock, "GEOM topology");
155	g_io_init();
156	g_event_init();
157	mtx_lock(&Giant);
158	kproc_start(&g_event_kp);
159	kproc_start(&g_up_kp);
160	kproc_start(&g_down_kp);
161	mtx_unlock(&Giant);
162}
163
164static int
165sysctl_kern_geom_dotconf(SYSCTL_HANDLER_ARGS)
166{
167	int i, error;
168	struct sbuf *sb;
169
170	i = 0;
171	sb = g_confdot();
172	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
173	sbuf_delete(sb);
174	return (error);
175}
176
177static int
178sysctl_kern_geom_xmlconf(SYSCTL_HANDLER_ARGS)
179{
180	int i, error;
181	struct sbuf *sb;
182
183	i = 0;
184	sb = g_conf();
185	error = sysctl_handle_opaque(oidp, sbuf_data(sb), sbuf_len(sb), req);
186	sbuf_delete(sb);
187	return (error);
188}
189
190SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");
191
192SYSCTL_PROC(_kern_geom, OID_AUTO, xmlconf, CTLTYPE_STRING|CTLFLAG_RD,
193	0, 0, sysctl_kern_geom_xmlconf, "A",
194	"Dump the GEOM config");
195
196SYSCTL_PROC(_kern_geom, OID_AUTO, dotconf, CTLTYPE_STRING|CTLFLAG_RD,
197	0, 0, sysctl_kern_geom_dotconf, "A",
198	"Dump the GEOM config");
199
200SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLTYPE_INT|CTLFLAG_RW,
201	&g_debugflags, 0, "");
202
203SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLTYPE_INT|CTLFLAG_RD,
204	0, sizeof(struct g_class), "");
205SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLTYPE_INT|CTLFLAG_RD,
206	0, sizeof(struct g_geom), "");
207SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLTYPE_INT|CTLFLAG_RD,
208	0, sizeof(struct g_provider), "");
209SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLTYPE_INT|CTLFLAG_RD,
210	0, sizeof(struct g_consumer), "");
211SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLTYPE_INT|CTLFLAG_RD,
212	0, sizeof(struct g_bioq), "");
213SYSCTL_INT(_debug_sizeof, OID_AUTO, g_event, CTLTYPE_INT|CTLFLAG_RD,
214	0, sizeof(struct g_event), "");
215