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