geom_kern.c revision 255860
1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/geom/geom_kern.c 255860 2013-09-24 20:05:16Z des $"); 38 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 57MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures"); 58 59struct sx topology_lock; 60 61static struct proc *g_proc; 62static struct thread *g_up_td; 63static struct thread *g_down_td; 64static struct thread *g_event_td; 65 66int g_debugflags; 67int g_collectstats = 1; 68int g_shutdown; 69int 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 belived 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 89static void 90g_up_procbody(void *arg) 91{ 92 93 mtx_assert(&Giant, MA_NOTOWNED); 94 thread_lock(g_up_td); 95 sched_prio(g_up_td, PRIBIO); 96 thread_unlock(g_up_td); 97 for(;;) { 98 g_io_schedule_up(g_up_td); 99 } 100} 101 102static void 103g_down_procbody(void *arg) 104{ 105 106 mtx_assert(&Giant, MA_NOTOWNED); 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 mtx_assert(&Giant, MA_NOTOWNED); 120 thread_lock(g_event_td); 121 sched_prio(g_event_td, PRIBIO); 122 thread_unlock(g_event_td); 123 g_run_events(); 124 /* NOTREACHED */ 125} 126 127static void 128geom_shutdown(void *foo __unused) 129{ 130 131 g_shutdown = 1; 132} 133 134void 135g_init(void) 136{ 137 138 g_trace(G_T_TOPOLOGY, "g_ignition"); 139 sx_init(&topology_lock, "GEOM topology"); 140 g_io_init(); 141 g_event_init(); 142 g_ctl_init(); 143 mtx_lock(&Giant); 144 kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td, 145 RFHIGHPID, 0, "geom", "g_event"); 146 kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td, 147 RFHIGHPID, 0, "geom", "g_up"); 148 kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td, 149 RFHIGHPID, 0, "geom", "g_down"); 150 mtx_unlock(&Giant); 151 EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL, 152 SHUTDOWN_PRI_FIRST); 153} 154 155static int 156sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS) 157{ 158 int error; 159 struct sbuf *sb; 160 161 sb = sbuf_new_auto(); 162 g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL); 163 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 164 sbuf_delete(sb); 165 return error; 166} 167 168static int 169sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS) 170{ 171 int error; 172 struct sbuf *sb; 173 174 sb = sbuf_new_auto(); 175 g_waitfor_event(g_confdot, sb, M_WAITOK, NULL); 176 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 177 sbuf_delete(sb); 178 return error; 179} 180 181static int 182sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS) 183{ 184 int error; 185 struct sbuf *sb; 186 187 sb = sbuf_new_auto(); 188 g_waitfor_event(g_confxml, sb, M_WAITOK, NULL); 189 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 190 sbuf_delete(sb); 191 return error; 192} 193 194SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management"); 195 196SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD, 197 0, 0, sysctl_kern_geom_confxml, "", 198 "Dump the GEOM config in XML"); 199 200SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD, 201 0, 0, sysctl_kern_geom_confdot, "", 202 "Dump the GEOM config in dot"); 203 204SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD, 205 0, 0, sysctl_kern_geom_conftxt, "", 206 "Dump the GEOM config in txt"); 207 208TUNABLE_INT("kern.geom.debugflags", &g_debugflags); 209SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW, 210 &g_debugflags, 0, "Set various trace levels for GEOM debugging"); 211 212SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW, 213 &g_notaste, 0, "Prevent GEOM tasting"); 214 215SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW, 216 &g_collectstats, 0, 217 "Control statistics collection on GEOM providers and consumers"); 218 219SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD, 220 0, sizeof(struct g_class), "sizeof(struct g_class)"); 221SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD, 222 0, sizeof(struct g_geom), "sizeof(struct g_geom)"); 223SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD, 224 0, sizeof(struct g_provider), "sizeof(struct g_provider)"); 225SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD, 226 0, sizeof(struct g_consumer), "sizeof(struct g_consumer)"); 227SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD, 228 0, sizeof(struct g_bioq), "sizeof(struct g_bioq)"); 229