xref: /freebsd/sys/geom/geom_kern.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
44 #include <sys/bio.h>
45 #include <sys/sysctl.h>
46 #include <sys/proc.h>
47 #include <sys/unistd.h>
48 #include <sys/kthread.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/sbuf.h>
52 #include <sys/sched.h>
53 #include <sys/sx.h>
54 #include <geom/geom.h>
55 #include <geom/geom_int.h>
56 
57 MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
58 
59 struct sx topology_lock;
60 
61 static struct proc *g_proc;
62 struct thread __read_mostly *g_up_td;
63 struct thread __read_mostly *g_down_td;
64 static struct thread __read_mostly *g_event_td;
65 
66 int __read_mostly g_debugflags;
67 int __read_mostly g_collectstats = G_STATS_PROVIDERS;
68 int g_shutdown;
69 int g_notaste;
70 
71 /*
72  * G_UP and G_DOWN are the two threads which push I/O through the
73  * stack.
74  *
75  * Things are procesed in a FIFO order, but these threads could be
76  * part of I/O prioritization by deciding which bios/bioqs to service
77  * in what order.
78  *
79  * We have only one thread in each direction, it is believed that until
80  * a very non-trivial workload in the UP/DOWN path this will be enough,
81  * but more than one can actually be run without problems.
82  *
83  * Holding the "mymutex" is a debugging feature:  It prevents people
84  * from sleeping in the UP/DOWN I/O path by mistake or design (doing
85  * so almost invariably result in deadlocks since it stalls all I/O
86  * processing in the given direction.
87  */
88 
89 static void
90 g_up_procbody(void *arg)
91 {
92 
93 	thread_lock(g_up_td);
94 	sched_prio(g_up_td, PRIBIO);
95 	thread_unlock(g_up_td);
96 	for(;;) {
97 		g_io_schedule_up(g_up_td);
98 	}
99 }
100 
101 static void
102 g_down_procbody(void *arg)
103 {
104 
105 	thread_lock(g_down_td);
106 	sched_prio(g_down_td, PRIBIO);
107 	thread_unlock(g_down_td);
108 	for(;;) {
109 		g_io_schedule_down(g_down_td);
110 	}
111 }
112 
113 static void
114 g_event_procbody(void *arg)
115 {
116 
117 	thread_lock(g_event_td);
118 	sched_prio(g_event_td, PRIBIO);
119 	thread_unlock(g_event_td);
120 	g_run_events();
121 	/* NOTREACHED */
122 }
123 
124 int
125 g_is_geom_thread(struct thread *td)
126 {
127 
128 	return (td == g_up_td || td == g_down_td || td == g_event_td);
129 }
130 
131 static void
132 geom_shutdown(void *foo __unused)
133 {
134 
135 	g_shutdown = 1;
136 }
137 
138 void
139 g_init(void)
140 {
141 
142 	g_trace(G_T_TOPOLOGY, "g_ignition");
143 	sx_init(&topology_lock, "GEOM topology");
144 	g_io_init();
145 	g_event_init();
146 	g_ctl_init();
147 	kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
148 	    RFHIGHPID, 0, "geom", "g_event");
149 	kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
150 	    RFHIGHPID, 0, "geom", "g_up");
151 	kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
152 	    RFHIGHPID, 0, "geom", "g_down");
153 	EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
154 		SHUTDOWN_PRI_FIRST);
155 }
156 
157 static int
158 sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
159 {
160 	size_t len = 0;
161 	int error = 0;
162 	struct sbuf *sb;
163 
164 	if (req->oldptr == NULL) {
165 		sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
166 		    SBUF_INCLUDENUL);
167 		sbuf_set_drain(sb, sbuf_count_drain, &len);
168 		g_waitfor_event(func, sb, M_WAITOK, NULL);
169 		req->oldidx = *hint = len;
170 	} else {
171 		sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
172 		    SBUF_INCLUDENUL);
173 		g_waitfor_event(func, sb, M_WAITOK, NULL);
174 		*hint = sbuf_len(sb);
175 		error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
176 	}
177 	sbuf_delete(sb);
178 	return error;
179 }
180 
181 static int
182 sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
183 {
184 	static size_t hint = PAGE_SIZE;
185 
186 	return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
187 }
188 
189 static int
190 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
191 {
192 	static size_t hint = PAGE_SIZE;
193 
194 	return (sysctl_kern_geom_confany(req, g_confdot, &hint));
195 }
196 
197 static int
198 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
199 {
200 	static size_t hint = PAGE_SIZE;
201 
202 	return (sysctl_kern_geom_confany(req, g_confxml, &hint));
203 }
204 
205 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
206     "GEOMetry management");
207 
208 SYSCTL_PROC(_kern_geom, OID_AUTO, confxml,
209     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
210     sysctl_kern_geom_confxml, "",
211     "Dump the GEOM config in XML");
212 
213 SYSCTL_PROC(_kern_geom, OID_AUTO, confdot,
214     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
215     sysctl_kern_geom_confdot, "",
216     "Dump the GEOM config in dot");
217 
218 SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt,
219     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
220     sysctl_kern_geom_conftxt, "",
221     "Dump the GEOM config in txt");
222 
223 SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
224 	&g_debugflags, 0, "Set various trace levels for GEOM debugging");
225 
226 SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
227 	&g_notaste, 0, "Prevent GEOM tasting");
228 
229 SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
230 	&g_collectstats, 0,
231 	"Control statistics collection on GEOM providers and consumers");
232 
233 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
234 	SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
235 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
236 	SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
237 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
238 	SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
239 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
240 	SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
241 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
242 	SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");
243