geom_kern.c revision 135151
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 135151 2004-09-13 14:58:27Z pjd $"); 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/kthread.h> 48#include <sys/lock.h> 49#include <sys/mutex.h> 50#include <sys/sx.h> 51#include <sys/sbuf.h> 52#include <geom/geom.h> 53#include <geom/geom_int.h> 54 55MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures"); 56 57struct sx topology_lock; 58 59static struct proc *g_up_proc; 60 61int g_debugflags; 62int g_collectstats = 1; 63int g_shutdown; 64 65/* 66 * G_UP and G_DOWN are the two threads which push I/O through the 67 * stack. 68 * 69 * Things are procesed in a FIFO order, but these threads could be 70 * part of I/O prioritization by deciding which bios/bioqs to service 71 * in what order. 72 * 73 * We have only one thread in each direction, it is belived that until 74 * a very non-trivial workload in the UP/DOWN path this will be enough, 75 * but more than one can actually be run without problems. 76 * 77 * Holding the "mymutex" is a debugging feature: It prevents people 78 * from sleeping in the UP/DOWN I/O path by mistake or design (doing 79 * so almost invariably result in deadlocks since it stalls all I/O 80 * processing in the given direction. 81 */ 82 83static void 84g_up_procbody(void) 85{ 86 struct proc *p = g_up_proc; 87 struct thread *tp = FIRST_THREAD_IN_PROC(p); 88 89 mtx_assert(&Giant, MA_NOTOWNED); 90 tp->td_base_pri = PRIBIO; 91 for(;;) { 92 g_io_schedule_up(tp); 93 } 94} 95 96struct kproc_desc g_up_kp = { 97 "g_up", 98 g_up_procbody, 99 &g_up_proc, 100}; 101 102static struct proc *g_down_proc; 103 104static void 105g_down_procbody(void) 106{ 107 struct proc *p = g_down_proc; 108 struct thread *tp = FIRST_THREAD_IN_PROC(p); 109 110 mtx_assert(&Giant, MA_NOTOWNED); 111 tp->td_base_pri = PRIBIO; 112 for(;;) { 113 g_io_schedule_down(tp); 114 } 115} 116 117struct kproc_desc g_down_kp = { 118 "g_down", 119 g_down_procbody, 120 &g_down_proc, 121}; 122 123static struct proc *g_event_proc; 124 125static void 126g_event_procbody(void) 127{ 128 struct proc *p = g_event_proc; 129 struct thread *tp = FIRST_THREAD_IN_PROC(p); 130 131 mtx_assert(&Giant, MA_NOTOWNED); 132 tp->td_base_pri = PRIBIO; 133 for(;;) { 134 g_run_events(); 135 tsleep(&g_wait_event, PRIBIO, "-", hz/10); 136 } 137} 138 139static struct kproc_desc g_event_kp = { 140 "g_event", 141 g_event_procbody, 142 &g_event_proc, 143}; 144 145static void 146geom_shutdown(void *foo __unused) 147{ 148 149 g_shutdown = 1; 150} 151 152void 153g_init(void) 154{ 155 156 g_trace(G_T_TOPOLOGY, "g_ignition"); 157 sx_init(&topology_lock, "GEOM topology"); 158 g_io_init(); 159 g_event_init(); 160 g_ctl_init(); 161 mtx_lock(&Giant); 162 kproc_start(&g_event_kp); 163 kproc_start(&g_up_kp); 164 kproc_start(&g_down_kp); 165 mtx_unlock(&Giant); 166 EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL, 167 SHUTDOWN_PRI_FIRST); 168} 169 170static int 171sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS) 172{ 173 int error; 174 struct sbuf *sb; 175 176 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 177 g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL); 178 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 179 sbuf_delete(sb); 180 return error; 181} 182 183static int 184sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS) 185{ 186 int error; 187 struct sbuf *sb; 188 189 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 190 g_waitfor_event(g_confdot, sb, M_WAITOK, NULL); 191 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 192 sbuf_delete(sb); 193 return error; 194} 195 196static int 197sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS) 198{ 199 int error; 200 struct sbuf *sb; 201 202 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 203 g_waitfor_event(g_confxml, sb, M_WAITOK, NULL); 204 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 205 sbuf_delete(sb); 206 return error; 207} 208 209SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management"); 210 211SYSCTL_PROC(_kern_geom, OID_AUTO, confxml, CTLTYPE_STRING|CTLFLAG_RD, 212 0, 0, sysctl_kern_geom_confxml, "", 213 "Dump the GEOM config in XML"); 214 215SYSCTL_PROC(_kern_geom, OID_AUTO, confdot, CTLTYPE_STRING|CTLFLAG_RD, 216 0, 0, sysctl_kern_geom_confdot, "", 217 "Dump the GEOM config in dot"); 218 219SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt, CTLTYPE_STRING|CTLFLAG_RD, 220 0, 0, sysctl_kern_geom_conftxt, "", 221 "Dump the GEOM config in txt"); 222 223TUNABLE_INT("kern.geom.debugflags", &g_debugflags); 224SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW, 225 &g_debugflags, 0, ""); 226 227SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW, 228 &g_collectstats, 0, ""); 229 230SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD, 231 0, sizeof(struct g_class), ""); 232SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD, 233 0, sizeof(struct g_geom), ""); 234SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD, 235 0, sizeof(struct g_provider), ""); 236SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD, 237 0, sizeof(struct g_consumer), ""); 238SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD, 239 0, sizeof(struct g_bioq), ""); 240