#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/eventhandler.h>
#include <sys/malloc.h>
#include <sys/bio.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/unistd.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sbuf.h>
#include <sys/sched.h>
#include <sys/sx.h>
#include <geom/geom.h>
#include <geom/geom_int.h>
MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
struct sx topology_lock;
static struct proc *g_proc;
struct thread __read_mostly *g_up_td;
struct thread __read_mostly *g_down_td;
static struct thread __read_mostly *g_event_td;
int __read_mostly g_debugflags;
int __read_mostly g_collectstats = G_STATS_PROVIDERS;
int g_shutdown;
int g_notaste;
static void
g_up_procbody(void *arg)
{
thread_lock(g_up_td);
sched_prio(g_up_td, PRIBIO);
thread_unlock(g_up_td);
for(;;) {
g_io_schedule_up(g_up_td);
}
}
static void
g_down_procbody(void *arg)
{
thread_lock(g_down_td);
sched_prio(g_down_td, PRIBIO);
thread_unlock(g_down_td);
for(;;) {
g_io_schedule_down(g_down_td);
}
}
static void
g_event_procbody(void *arg)
{
thread_lock(g_event_td);
sched_prio(g_event_td, PRIBIO);
thread_unlock(g_event_td);
g_run_events();
}
int
g_is_geom_thread(struct thread *td)
{
return (td == g_up_td || td == g_down_td || td == g_event_td);
}
static void
geom_shutdown(void *foo __unused)
{
g_shutdown = 1;
}
void
g_init(void)
{
g_trace(G_T_TOPOLOGY, "g_ignition");
sx_init(&topology_lock, "GEOM topology");
g_io_init();
g_event_init();
g_ctl_init();
kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
RFHIGHPID, 0, "geom", "g_event");
kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
RFHIGHPID, 0, "geom", "g_up");
kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
RFHIGHPID, 0, "geom", "g_down");
EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
SHUTDOWN_PRI_FIRST);
}
static int
sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
{
size_t len = 0;
int error = 0;
struct sbuf *sb;
if (req->oldptr == NULL) {
sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
SBUF_INCLUDENUL);
sbuf_set_drain(sb, sbuf_count_drain, &len);
g_waitfor_event(func, sb, M_WAITOK, NULL);
req->oldidx = *hint = len;
} else {
sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
SBUF_INCLUDENUL);
g_waitfor_event(func, sb, M_WAITOK, NULL);
*hint = sbuf_len(sb);
error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
}
sbuf_delete(sb);
return error;
}
static int
sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
{
static size_t hint = PAGE_SIZE;
return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
}
static int
sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
{
static size_t hint = PAGE_SIZE;
return (sysctl_kern_geom_confany(req, g_confdot, &hint));
}
static int
sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
{
static size_t hint = PAGE_SIZE;
return (sysctl_kern_geom_confany(req, g_confxml, &hint));
}
SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"GEOMetry management");
SYSCTL_PROC(_kern_geom, OID_AUTO, confxml,
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
sysctl_kern_geom_confxml, "",
"Dump the GEOM config in XML");
SYSCTL_PROC(_kern_geom, OID_AUTO, confdot,
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
sysctl_kern_geom_confdot, "",
"Dump the GEOM config in dot");
SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt,
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
sysctl_kern_geom_conftxt, "",
"Dump the GEOM config in txt");
SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
&g_debugflags, 0, "Set various trace levels for GEOM debugging");
SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
&g_notaste, 0, "Prevent GEOM tasting");
SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
&g_collectstats, 0,
"Control statistics collection on GEOM providers and consumers");
SYSCTL_SIZEOF_STRUCT(g_class);
SYSCTL_SIZEOF_STRUCT(g_geom);
SYSCTL_SIZEOF_STRUCT(g_provider);
SYSCTL_SIZEOF_STRUCT(g_consumer);
SYSCTL_SIZEOF_STRUCT(g_bioq);