2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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
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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/kthread.h>
52 #include <sys/mutex.h>
54 #include <sys/sched.h>
56 #include <geom/geom.h>
57 #include <geom/geom_int.h>
59 MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
61 struct sx topology_lock;
63 static struct proc *g_proc;
64 static struct thread __read_mostly *g_up_td;
65 static struct thread __read_mostly *g_down_td;
66 static struct thread __read_mostly *g_event_td;
68 int __read_mostly g_debugflags;
69 int __read_mostly g_collectstats = G_STATS_PROVIDERS;
74 * G_UP and G_DOWN are the two threads which push I/O through the
77 * Things are procesed in a FIFO order, but these threads could be
78 * part of I/O prioritization by deciding which bios/bioqs to service
81 * We have only one thread in each direction, it is believed that until
82 * a very non-trivial workload in the UP/DOWN path this will be enough,
83 * but more than one can actually be run without problems.
85 * Holding the "mymutex" is a debugging feature: It prevents people
86 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
87 * so almost invariably result in deadlocks since it stalls all I/O
88 * processing in the given direction.
92 g_up_procbody(void *arg)
96 sched_prio(g_up_td, PRIBIO);
97 thread_unlock(g_up_td);
99 g_io_schedule_up(g_up_td);
104 g_down_procbody(void *arg)
107 thread_lock(g_down_td);
108 sched_prio(g_down_td, PRIBIO);
109 thread_unlock(g_down_td);
111 g_io_schedule_down(g_down_td);
116 g_event_procbody(void *arg)
119 thread_lock(g_event_td);
120 sched_prio(g_event_td, PRIBIO);
121 thread_unlock(g_event_td);
127 g_is_geom_thread(struct thread *td)
130 return (td == g_up_td || td == g_down_td || td == g_event_td);
134 geom_shutdown(void *foo __unused)
144 g_trace(G_T_TOPOLOGY, "g_ignition");
145 sx_init(&topology_lock, "GEOM topology");
149 kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
150 RFHIGHPID, 0, "geom", "g_event");
151 kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
152 RFHIGHPID, 0, "geom", "g_up");
153 kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
154 RFHIGHPID, 0, "geom", "g_down");
155 EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
160 sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
166 if (req->oldptr == NULL) {
167 sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
169 sbuf_set_drain(sb, sbuf_count_drain, &len);
170 g_waitfor_event(func, sb, M_WAITOK, NULL);
171 req->oldidx = *hint = len;
173 sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
175 g_waitfor_event(func, sb, M_WAITOK, NULL);
176 *hint = sbuf_len(sb);
177 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
184 sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
186 static size_t hint = PAGE_SIZE;
188 return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
192 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
194 static size_t hint = PAGE_SIZE;
196 return (sysctl_kern_geom_confany(req, g_confdot, &hint));
200 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
202 static size_t hint = PAGE_SIZE;
204 return (sysctl_kern_geom_confany(req, g_confxml, &hint));
207 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
208 "GEOMetry management");
210 SYSCTL_PROC(_kern_geom, OID_AUTO, confxml,
211 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
212 sysctl_kern_geom_confxml, "",
213 "Dump the GEOM config in XML");
215 SYSCTL_PROC(_kern_geom, OID_AUTO, confdot,
216 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
217 sysctl_kern_geom_confdot, "",
218 "Dump the GEOM config in dot");
220 SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt,
221 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
222 sysctl_kern_geom_conftxt, "",
223 "Dump the GEOM config in txt");
225 SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
226 &g_debugflags, 0, "Set various trace levels for GEOM debugging");
228 SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
229 &g_notaste, 0, "Prevent GEOM tasting");
231 SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
233 "Control statistics collection on GEOM providers and consumers");
235 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
236 SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
237 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
238 SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
239 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
240 SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
241 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
242 SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
243 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
244 SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");