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__FBSDID("$FreeBSD$");
40
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>
46#include <sys/bio.h>
47#include <sys/sysctl.h>
48#include <sys/proc.h>
49#include <sys/unistd.h>
50#include <sys/kthread.h>
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#include <sys/sbuf.h>
54#include <sys/sched.h>
55#include <sys/sx.h>
56#include <geom/geom.h>
57#include <geom/geom_int.h>
58
59MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
60
61struct sx topology_lock;
62
63static struct proc *g_proc;
64static struct thread __read_mostly *g_up_td;
65static struct thread __read_mostly *g_down_td;
66static struct thread __read_mostly *g_event_td;
67
68int __read_mostly g_debugflags;
69int __read_mostly g_collectstats = G_STATS_PROVIDERS;
70int g_shutdown;
71int g_notaste;
72
73/*
74 * G_UP and G_DOWN are the two threads which push I/O through the
75 * stack.
76 *
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
79 * in what order.
80 *
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.
84 *
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.
89 */
90
91static void
92g_up_procbody(void *arg)
93{
94
95	thread_lock(g_up_td);
96	sched_prio(g_up_td, PRIBIO);
97	thread_unlock(g_up_td);
98	for(;;) {
99		g_io_schedule_up(g_up_td);
100	}
101}
102
103static void
104g_down_procbody(void *arg)
105{
106
107	thread_lock(g_down_td);
108	sched_prio(g_down_td, PRIBIO);
109	thread_unlock(g_down_td);
110	for(;;) {
111		g_io_schedule_down(g_down_td);
112	}
113}
114
115static void
116g_event_procbody(void *arg)
117{
118
119	thread_lock(g_event_td);
120	sched_prio(g_event_td, PRIBIO);
121	thread_unlock(g_event_td);
122	g_run_events();
123	/* NOTREACHED */
124}
125
126int
127g_is_geom_thread(struct thread *td)
128{
129
130	return (td == g_up_td || td == g_down_td || td == g_event_td);
131}
132
133static void
134geom_shutdown(void *foo __unused)
135{
136
137	g_shutdown = 1;
138}
139
140void
141g_init(void)
142{
143
144	g_trace(G_T_TOPOLOGY, "g_ignition");
145	sx_init(&topology_lock, "GEOM topology");
146	g_io_init();
147	g_event_init();
148	g_ctl_init();
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,
156		SHUTDOWN_PRI_FIRST);
157}
158
159static int
160sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
161{
162	size_t len = 0;
163	int error = 0;
164	struct sbuf *sb;
165
166	if (req->oldptr == NULL) {
167		sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
168		    SBUF_INCLUDENUL);
169		sbuf_set_drain(sb, sbuf_count_drain, &len);
170		g_waitfor_event(func, sb, M_WAITOK, NULL);
171		req->oldidx = *hint = len;
172	} else {
173		sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
174		    SBUF_INCLUDENUL);
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));
178	}
179	sbuf_delete(sb);
180	return error;
181}
182
183static int
184sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
185{
186	static size_t hint = PAGE_SIZE;
187
188	return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
189}
190
191static int
192sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
193{
194	static size_t hint = PAGE_SIZE;
195
196	return (sysctl_kern_geom_confany(req, g_confdot, &hint));
197}
198
199static int
200sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
201{
202	static size_t hint = PAGE_SIZE;
203
204	return (sysctl_kern_geom_confany(req, g_confxml, &hint));
205}
206
207SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
208    "GEOMetry management");
209
210SYSCTL_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");
214
215SYSCTL_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");
219
220SYSCTL_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");
224
225SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
226	&g_debugflags, 0, "Set various trace levels for GEOM debugging");
227
228SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
229	&g_notaste, 0, "Prevent GEOM tasting");
230
231SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
232	&g_collectstats, 0,
233	"Control statistics collection on GEOM providers and consumers");
234
235SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
236	SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
237SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
238	SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
239SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
240	SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
241SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
242	SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
243SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
244	SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");
245