geom_kern.c revision 125651
125745Speter/*- 225745Speter * Copyright (c) 2002 Poul-Henning Kamp 325745Speter * Copyright (c) 2002 Networks Associates Technology, Inc. 425745Speter * All rights reserved. 525745Speter * 625745Speter * This software was developed for the FreeBSD Project by Poul-Henning Kamp 725745Speter * and NAI Labs, the Security Research Division of Network Associates, Inc. 825745Speter * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 925745Speter * DARPA CHATS research program. 1025745Speter * 1125745Speter * Redistribution and use in source and binary forms, with or without 1225745Speter * modification, are permitted provided that the following conditions 1325745Speter * are met: 1425745Speter * 1. Redistributions of source code must retain the above copyright 1525745Speter * notice, this list of conditions and the following disclaimer. 1625745Speter * 2. Redistributions in binary form must reproduce the above copyright 1725745Speter * notice, this list of conditions and the following disclaimer in the 1825745Speter * documentation and/or other materials provided with the distribution. 1925745Speter * 3. The names of the authors may not be used to endorse or promote 2025745Speter * products derived from this software without specific prior written 2125745Speter * permission. 2225745Speter * 2325745Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2425745Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2525745Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2625745Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2725745Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2825745Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2925745Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3025745Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3125745Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32108028Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3325745Speter * SUCH DAMAGE. 3425745Speter */ 3525745Speter 3679531Sru#include <sys/cdefs.h> 3725745Speter__FBSDID("$FreeBSD: head/sys/geom/geom_kern.c 125651 2004-02-10 10:54:19Z phk $"); 3836037Speter 3950946Sphantom#include <sys/param.h> 4059460Sphantom#include <sys/systm.h> 4159460Sphantom#include <sys/kernel.h> 4225745Speter#include <sys/eventhandler.h> 4384306Sru#include <sys/malloc.h> 4425745Speter#include <sys/bio.h> 4525745Speter#include <sys/sysctl.h> 4626339Speter#include <sys/proc.h> 47108028Sru#include <sys/kthread.h> 48108028Sru#include <sys/lock.h> 49108028Sru#include <sys/mutex.h> 50197958Skib#include <sys/sx.h> 51197958Skib#include <sys/sbuf.h> 52197958Skib#include <geom/geom.h> 53131504Sru#include <geom/geom_int.h> 5426339Speter 5526339SpeterMALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures"); 5626339Speter 5750946Sphantomstruct sx topology_lock; 5879754Sdd 5925745Speterstatic struct proc *g_up_proc; 60108028Sru 6179754Sddint g_debugflags; 6225745Speterint g_collectstats = 1; 6379754Sddint g_shutdown; 6425745Speter 65108028Sru/* 66108105Sceri * G_UP and G_DOWN are the two threads which push I/O through the 6725745Speter * stack. 6825745Speter * 6979754Sdd * Things are procesed in a FIFO order, but these threads could be 7025745Speter * part of I/O prioritization by deciding which bios/bioqs to service 7179754Sdd * in what order. 72108028Sru * 7325745Speter * We have only one thread in each direction, it is belived that until 7425745Speter * a very non-trivial workload in the UP/DOWN path this will be enough, 7581627Syar * but more than one can actually be run without problems. 7681627Syar * 7779754Sdd * Holding the "mymutex" is a debugging feature: It prevents people 78108028Sru * from sleeping in the UP/DOWN I/O path by mistake or design (doing 7925745Speter * so almost invariably result in deadlocks since it stalls all I/O 8025745Speter * processing in the given direction. 8125745Speter */ 8225745Speter 8379754Sddstatic void 8425745Speterg_up_procbody(void) 8525745Speter{ 8625745Speter struct proc *p = g_up_proc; 8725745Speter struct thread *tp = FIRST_THREAD_IN_PROC(p); 88108028Sru 8950946Sphantom mtx_assert(&Giant, MA_NOTOWNED); 90108028Sru tp->td_base_pri = PRIBIO; 9125745Speter for(;;) { 9225745Speter g_io_schedule_up(tp); 93108087Sru } 9425745Speter} 95108087Sru 9628545Sbdestruct kproc_desc g_up_kp = { 9728545Sbde "g_up", 9825745Speter g_up_procbody, 99108028Sru &g_up_proc, 10050946Sphantom}; 101108028Sru 10225745Speterstatic struct proc *g_down_proc; 10325745Speter 10425745Speterstatic void 10529988Swoschg_down_procbody(void) 10625745Speter{ 10725745Speter struct proc *p = g_down_proc; 10825745Speter struct thread *tp = FIRST_THREAD_IN_PROC(p); 10925745Speter 110108028Sru mtx_assert(&Giant, MA_NOTOWNED); 11125745Speter 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 223SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RW, 224 &g_debugflags, 0, ""); 225 226SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW, 227 &g_collectstats, 0, ""); 228 229SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD, 230 0, sizeof(struct g_class), ""); 231SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD, 232 0, sizeof(struct g_geom), ""); 233SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD, 234 0, sizeof(struct g_provider), ""); 235SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD, 236 0, sizeof(struct g_consumer), ""); 237SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD, 238 0, sizeof(struct g_bioq), ""); 239