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/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/kthread.h>
49 #include <sys/mutex.h>
51 #include <sys/sched.h>
53 #include <geom/geom.h>
54 #include <geom/geom_int.h>
56 MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
58 struct sx topology_lock;
60 static struct proc *g_proc;
61 struct thread __read_mostly *g_up_td;
62 struct thread __read_mostly *g_down_td;
63 static struct thread __read_mostly *g_event_td;
65 int __read_mostly g_debugflags;
66 int __read_mostly g_collectstats = G_STATS_PROVIDERS;
71 * G_UP and G_DOWN are the two threads which push I/O through the
74 * Things are procesed in a FIFO order, but these threads could be
75 * part of I/O prioritization by deciding which bios/bioqs to service
78 * We have only one thread in each direction, it is believed that until
79 * a very non-trivial workload in the UP/DOWN path this will be enough,
80 * but more than one can actually be run without problems.
82 * Holding the "mymutex" is a debugging feature: It prevents people
83 * from sleeping in the UP/DOWN I/O path by mistake or design (doing
84 * so almost invariably result in deadlocks since it stalls all I/O
85 * processing in the given direction.
89 g_up_procbody(void *arg)
93 sched_prio(g_up_td, PRIBIO);
94 thread_unlock(g_up_td);
96 g_io_schedule_up(g_up_td);
101 g_down_procbody(void *arg)
104 thread_lock(g_down_td);
105 sched_prio(g_down_td, PRIBIO);
106 thread_unlock(g_down_td);
108 g_io_schedule_down(g_down_td);
113 g_event_procbody(void *arg)
116 thread_lock(g_event_td);
117 sched_prio(g_event_td, PRIBIO);
118 thread_unlock(g_event_td);
124 g_is_geom_thread(struct thread *td)
127 return (td == g_up_td || td == g_down_td || td == g_event_td);
131 geom_shutdown(void *foo __unused)
141 g_trace(G_T_TOPOLOGY, "g_ignition");
142 sx_init(&topology_lock, "GEOM topology");
146 kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
147 RFHIGHPID, 0, "geom", "g_event");
148 kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
149 RFHIGHPID, 0, "geom", "g_up");
150 kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
151 RFHIGHPID, 0, "geom", "g_down");
152 EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
157 sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
163 if (req->oldptr == NULL) {
164 sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
166 sbuf_set_drain(sb, sbuf_count_drain, &len);
167 g_waitfor_event(func, sb, M_WAITOK, NULL);
168 req->oldidx = *hint = len;
170 sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
172 g_waitfor_event(func, sb, M_WAITOK, NULL);
173 *hint = sbuf_len(sb);
174 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
181 sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
183 static size_t hint = PAGE_SIZE;
185 return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
189 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
191 static size_t hint = PAGE_SIZE;
193 return (sysctl_kern_geom_confany(req, g_confdot, &hint));
197 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
199 static size_t hint = PAGE_SIZE;
201 return (sysctl_kern_geom_confany(req, g_confxml, &hint));
204 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
205 "GEOMetry management");
207 SYSCTL_PROC(_kern_geom, OID_AUTO, confxml,
208 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
209 sysctl_kern_geom_confxml, "",
210 "Dump the GEOM config in XML");
212 SYSCTL_PROC(_kern_geom, OID_AUTO, confdot,
213 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
214 sysctl_kern_geom_confdot, "",
215 "Dump the GEOM config in dot");
217 SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt,
218 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
219 sysctl_kern_geom_conftxt, "",
220 "Dump the GEOM config in txt");
222 SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
223 &g_debugflags, 0, "Set various trace levels for GEOM debugging");
225 SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
226 &g_notaste, 0, "Prevent GEOM tasting");
228 SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
230 "Control statistics collection on GEOM providers and consumers");
232 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
233 SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
234 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
235 SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
236 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
237 SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
238 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
239 SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
240 SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
241 SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");