Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/geom/geom_kern.c
39475 views
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/param.h>
39
#include <sys/systm.h>
40
#include <sys/kernel.h>
41
#include <sys/eventhandler.h>
42
#include <sys/malloc.h>
43
#include <sys/bio.h>
44
#include <sys/sysctl.h>
45
#include <sys/proc.h>
46
#include <sys/unistd.h>
47
#include <sys/kthread.h>
48
#include <sys/lock.h>
49
#include <sys/mutex.h>
50
#include <sys/sbuf.h>
51
#include <sys/sched.h>
52
#include <sys/sx.h>
53
#include <geom/geom.h>
54
#include <geom/geom_int.h>
55
56
MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
57
58
struct sx topology_lock;
59
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;
64
65
int __read_mostly g_debugflags;
66
int __read_mostly g_collectstats = G_STATS_PROVIDERS;
67
int g_shutdown;
68
int g_notaste;
69
70
/*
71
* G_UP and G_DOWN are the two threads which push I/O through the
72
* stack.
73
*
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
76
* in what order.
77
*
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.
81
*
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.
86
*/
87
88
static void
89
g_up_procbody(void *arg)
90
{
91
92
thread_lock(g_up_td);
93
sched_prio(g_up_td, PRIBIO);
94
thread_unlock(g_up_td);
95
for(;;) {
96
g_io_schedule_up(g_up_td);
97
}
98
}
99
100
static void
101
g_down_procbody(void *arg)
102
{
103
104
thread_lock(g_down_td);
105
sched_prio(g_down_td, PRIBIO);
106
thread_unlock(g_down_td);
107
for(;;) {
108
g_io_schedule_down(g_down_td);
109
}
110
}
111
112
static void
113
g_event_procbody(void *arg)
114
{
115
116
thread_lock(g_event_td);
117
sched_prio(g_event_td, PRIBIO);
118
thread_unlock(g_event_td);
119
g_run_events();
120
/* NOTREACHED */
121
}
122
123
int
124
g_is_geom_thread(struct thread *td)
125
{
126
127
return (td == g_up_td || td == g_down_td || td == g_event_td);
128
}
129
130
static void
131
geom_shutdown(void *foo __unused)
132
{
133
134
g_shutdown = 1;
135
}
136
137
void
138
g_init(void)
139
{
140
141
g_trace(G_T_TOPOLOGY, "g_ignition");
142
sx_init(&topology_lock, "GEOM topology");
143
g_io_init();
144
g_event_init();
145
g_ctl_init();
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,
153
SHUTDOWN_PRI_FIRST);
154
}
155
156
static int
157
sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
158
{
159
size_t len = 0;
160
int error = 0;
161
struct sbuf *sb;
162
163
if (req->oldptr == NULL) {
164
sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
165
SBUF_INCLUDENUL);
166
sbuf_set_drain(sb, sbuf_count_drain, &len);
167
g_waitfor_event(func, sb, M_WAITOK, NULL);
168
req->oldidx = *hint = len;
169
} else {
170
sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
171
SBUF_INCLUDENUL);
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));
175
}
176
sbuf_delete(sb);
177
return error;
178
}
179
180
static int
181
sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
182
{
183
static size_t hint = PAGE_SIZE;
184
185
return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
186
}
187
188
static int
189
sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
190
{
191
static size_t hint = PAGE_SIZE;
192
193
return (sysctl_kern_geom_confany(req, g_confdot, &hint));
194
}
195
196
static int
197
sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
198
{
199
static size_t hint = PAGE_SIZE;
200
201
return (sysctl_kern_geom_confany(req, g_confxml, &hint));
202
}
203
204
SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
205
"GEOMetry management");
206
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");
211
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");
216
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");
221
222
SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
223
&g_debugflags, 0, "Set various trace levels for GEOM debugging");
224
225
SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
226
&g_notaste, 0, "Prevent GEOM tasting");
227
228
SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
229
&g_collectstats, 0,
230
"Control statistics collection on GEOM providers and consumers");
231
232
SYSCTL_SIZEOF_STRUCT(g_class);
233
SYSCTL_SIZEOF_STRUCT(g_geom);
234
SYSCTL_SIZEOF_STRUCT(g_provider);
235
SYSCTL_SIZEOF_STRUCT(g_consumer);
236
SYSCTL_SIZEOF_STRUCT(g_bioq);
237
238