xref: /freebsd/sys/geom/geom_kern.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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$
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 #include <geom/geom_stats.h>
53 
54 MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
55 
56 struct sx topology_lock;
57 
58 static struct proc *g_up_proc;
59 
60 int g_debugflags;
61 int 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 
81 static void
82 g_up_procbody(void)
83 {
84 	struct proc *p = g_up_proc;
85 	struct thread *tp = FIRST_THREAD_IN_PROC(p);
86 
87 	mtx_assert(&Giant, MA_NOTOWNED);
88 	tp->td_base_pri = PRIBIO;
89 	for(;;) {
90 		g_io_schedule_up(tp);
91 	}
92 }
93 
94 struct kproc_desc g_up_kp = {
95 	"g_up",
96 	g_up_procbody,
97 	&g_up_proc,
98 };
99 
100 static struct proc *g_down_proc;
101 
102 static void
103 g_down_procbody(void)
104 {
105 	struct proc *p = g_down_proc;
106 	struct thread *tp = FIRST_THREAD_IN_PROC(p);
107 
108 	mtx_assert(&Giant, MA_NOTOWNED);
109 	tp->td_base_pri = PRIBIO;
110 	for(;;) {
111 		g_io_schedule_down(tp);
112 	}
113 }
114 
115 struct kproc_desc g_down_kp = {
116 	"g_down",
117 	g_down_procbody,
118 	&g_down_proc,
119 };
120 
121 static struct proc *g_event_proc;
122 
123 static void
124 g_event_procbody(void)
125 {
126 	struct proc *p = g_event_proc;
127 	struct thread *tp = FIRST_THREAD_IN_PROC(p);
128 
129 	mtx_assert(&Giant, MA_NOTOWNED);
130 	tp->td_base_pri = PRIBIO;
131 	for(;;) {
132 		g_run_events();
133 		tsleep(&g_wait_event, PRIBIO, "g_events", hz/10);
134 	}
135 }
136 
137 struct kproc_desc g_event_kp = {
138 	"g_event",
139 	g_event_procbody,
140 	&g_event_proc,
141 };
142 
143 void
144 g_init(void)
145 {
146 	sx_init(&topology_lock, "GEOM topology");
147 	g_stat_init();
148 	g_io_init();
149 	g_event_init();
150 	mtx_lock(&Giant);
151 	kproc_start(&g_event_kp);
152 	kproc_start(&g_up_kp);
153 	kproc_start(&g_down_kp);
154 	mtx_unlock(&Giant);
155 }
156 
157 static int
158 sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
159 {
160 	int error;
161 	struct sbuf *sb;
162 
163 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
164 	sbuf_clear(sb);
165 	g_call_me(g_conftxt, sb);
166 	do {
167 		tsleep(sb, PZERO, "g_conftxt", hz);
168 	} while(!sbuf_done(sb));
169 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
170 	sbuf_delete(sb);
171 	return error;
172 }
173 
174 static int
175 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
176 {
177 	int error;
178 	struct sbuf *sb;
179 
180 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
181 	sbuf_clear(sb);
182 	g_call_me(g_confdot, sb);
183 	do {
184 		tsleep(sb, PZERO, "g_confdot", hz);
185 	} while(!sbuf_done(sb));
186 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
187 	sbuf_delete(sb);
188 	return error;
189 }
190 
191 static int
192 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
193 {
194 	int error;
195 	struct sbuf *sb;
196 
197 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
198 	sbuf_clear(sb);
199 	g_call_me(g_confxml, sb);
200 	do {
201 		tsleep(sb, PZERO, "g_confxml", hz);
202 	} while(!sbuf_done(sb));
203 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
204 	sbuf_delete(sb);
205 	return error;
206 }
207 
208 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");
209 
210 SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD,
211 	0, 0, sysctl_kern_geom_confxml, "",
212 	"Dump the GEOM config in XML");
213 
214 SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD,
215 	0, 0, sysctl_kern_geom_confdot, "",
216 	"Dump the GEOM config in dot");
217 
218 SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD,
219 	0, 0, sysctl_kern_geom_conftxt, "",
220 	"Dump the GEOM config in txt");
221 
222 SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW,
223 	&g_debugflags, 0, "");
224 
225 SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
226 	&g_collectstats, 0, "");
227 
228 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
229 	0, sizeof(struct g_class), "");
230 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
231 	0, sizeof(struct g_geom), "");
232 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
233 	0, sizeof(struct g_provider), "");
234 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
235 	0, sizeof(struct g_consumer), "");
236 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
237 	0, sizeof(struct g_bioq), "");
238 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_event, CTLFLAG_RD,
239 	0, sizeof(struct g_event), "");
240 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_stat, CTLFLAG_RD,
241 	0, sizeof(struct g_stat), "");
242