2002-03-11 21:42:35 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: How do we in general know that objects referenced in events
|
|
|
|
* have not been destroyed before we get around to handle the event ?
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifndef _KERNEL
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <err.h>
|
|
|
|
#else
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2002-09-27 20:23:35 +00:00
|
|
|
#include <sys/eventhandler.h>
|
2003-04-02 20:41:18 +00:00
|
|
|
#include <machine/stdarg.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <geom/geom.h>
|
2002-03-26 22:07:38 +00:00
|
|
|
#include <geom/geom_int.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
|
|
|
|
static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
|
2003-01-13 08:46:32 +00:00
|
|
|
static u_int g_pending_events;
|
2002-03-26 22:07:38 +00:00
|
|
|
static void g_do_event(struct g_event *ep);
|
2002-03-11 21:42:35 +00:00
|
|
|
static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
|
2002-09-27 20:18:16 +00:00
|
|
|
static struct mtx g_eventlock;
|
2003-03-23 21:58:09 +00:00
|
|
|
static struct sx g_eventstall;
|
2002-09-27 20:23:35 +00:00
|
|
|
static int g_shutdown;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
|
|
|
void
|
2002-06-09 10:57:34 +00:00
|
|
|
g_waitidle(void)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
while (g_pending_events)
|
2002-06-09 10:57:34 +00:00
|
|
|
tsleep(&g_pending_events, PPAUSE, "g_waitidle", hz/5);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
2003-03-23 21:58:09 +00:00
|
|
|
void
|
|
|
|
g_stall_events(void)
|
|
|
|
{
|
|
|
|
|
2003-03-24 13:35:06 +00:00
|
|
|
sx_xlock(&g_eventstall);
|
2003-03-23 21:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_release_events(void)
|
|
|
|
{
|
|
|
|
|
2003-03-24 13:35:06 +00:00
|
|
|
sx_xunlock(&g_eventstall);
|
2003-03-23 21:58:09 +00:00
|
|
|
}
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
void
|
|
|
|
g_orphan_provider(struct g_provider *pp, int error)
|
|
|
|
{
|
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
|
|
|
|
pp, pp->name, error);
|
|
|
|
KASSERT(error != 0,
|
|
|
|
("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
|
|
|
|
pp, pp->name));
|
|
|
|
pp->error = error;
|
2002-09-27 20:18:16 +00:00
|
|
|
mtx_lock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
|
2002-09-27 20:18:16 +00:00
|
|
|
mtx_unlock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
wakeup(&g_wait_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called once on each provider which the event handler
|
|
|
|
* finds on its g_doorstep.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2002-03-26 22:07:38 +00:00
|
|
|
g_orphan_register(struct g_provider *pp)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct g_consumer *cp, *cp2;
|
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
|
|
|
|
g_topology_assert();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell all consumers the bad news.
|
2003-03-09 10:02:31 +00:00
|
|
|
* Don't be surprised if they self-destruct.
|
2002-03-11 21:42:35 +00:00
|
|
|
*/
|
|
|
|
cp = LIST_FIRST(&pp->consumers);
|
|
|
|
while (cp != NULL) {
|
|
|
|
cp2 = LIST_NEXT(cp, consumers);
|
2002-04-04 09:54:13 +00:00
|
|
|
KASSERT(cp->geom->orphan != NULL,
|
|
|
|
("geom %s has no orphan, class %s",
|
|
|
|
cp->geom->name, cp->geom->class->name));
|
|
|
|
cp->geom->orphan(cp);
|
2002-03-11 21:42:35 +00:00
|
|
|
cp = cp2;
|
|
|
|
}
|
2003-03-16 16:29:04 +00:00
|
|
|
#ifdef notyet
|
2003-03-10 23:41:41 +00:00
|
|
|
cp = LIST_FIRST(&pp->consumers);
|
|
|
|
if (cp != NULL)
|
|
|
|
return;
|
|
|
|
if (pp->geom->flags & G_GEOM_WITHER)
|
|
|
|
g_destroy_provider(pp);
|
2003-03-16 16:29:04 +00:00
|
|
|
#endif
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_destroy_event(struct g_event *ep)
|
|
|
|
{
|
|
|
|
|
|
|
|
g_free(ep);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-03-26 22:07:38 +00:00
|
|
|
g_do_event(struct g_event *ep)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
2002-03-26 21:40:06 +00:00
|
|
|
struct g_class *mp, *mp2;
|
2002-03-11 21:42:35 +00:00
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_consumer *cp, *cp2;
|
|
|
|
struct g_provider *pp;
|
|
|
|
int i;
|
|
|
|
|
2003-04-02 20:41:18 +00:00
|
|
|
g_trace(G_T_TOPOLOGY, "g_do_event(%p) %d - ", ep, ep->event);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_assert();
|
|
|
|
switch (ep->event) {
|
2002-09-27 20:38:36 +00:00
|
|
|
case EV_CALL_ME:
|
2003-04-02 20:41:18 +00:00
|
|
|
ep->func(ep->arg, 0);
|
2002-09-30 08:27:29 +00:00
|
|
|
g_topology_assert();
|
2002-09-27 20:38:36 +00:00
|
|
|
break;
|
2002-03-26 21:40:06 +00:00
|
|
|
case EV_NEW_CLASS:
|
2002-09-27 20:38:36 +00:00
|
|
|
if (g_shutdown)
|
|
|
|
break;
|
2003-04-02 20:41:18 +00:00
|
|
|
mp2 = ep->ref[0];
|
2002-03-11 21:42:35 +00:00
|
|
|
if (mp2->taste == NULL)
|
|
|
|
break;
|
2002-04-04 09:41:47 +00:00
|
|
|
LIST_FOREACH(mp, &g_classes, class) {
|
2002-03-11 21:42:35 +00:00
|
|
|
if (mp2 == mp)
|
|
|
|
continue;
|
|
|
|
LIST_FOREACH(gp, &mp->geom, geom) {
|
|
|
|
LIST_FOREACH(pp, &gp->provider, provider) {
|
2003-04-02 20:41:18 +00:00
|
|
|
mp2->taste(mp2, pp, 0);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_assert();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_NEW_PROVIDER:
|
2002-09-27 20:30:27 +00:00
|
|
|
if (g_shutdown)
|
|
|
|
break;
|
2003-04-02 20:41:18 +00:00
|
|
|
pp = ep->ref[0];
|
|
|
|
g_trace(G_T_TOPOLOGY, "EV_NEW_PROVIDER(%s)", pp->name);
|
2002-04-04 09:41:47 +00:00
|
|
|
LIST_FOREACH(mp, &g_classes, class) {
|
2002-03-11 21:42:35 +00:00
|
|
|
if (mp->taste == NULL)
|
|
|
|
continue;
|
|
|
|
i = 1;
|
2003-04-02 20:41:18 +00:00
|
|
|
LIST_FOREACH(cp, &pp->consumers, consumers)
|
2002-03-26 21:40:06 +00:00
|
|
|
if(cp->geom->class == mp)
|
2002-03-11 21:42:35 +00:00
|
|
|
i = 0;
|
|
|
|
if (i) {
|
2003-04-02 20:41:18 +00:00
|
|
|
mp->taste(mp, pp, 0);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_assert();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_SPOILED:
|
2003-04-02 20:41:18 +00:00
|
|
|
pp = ep->ref[0];
|
2002-03-11 21:42:35 +00:00
|
|
|
g_trace(G_T_TOPOLOGY, "EV_SPOILED(%p(%s),%p)",
|
2003-04-02 20:41:18 +00:00
|
|
|
pp, pp->name, ep->ref[1]);
|
|
|
|
cp = LIST_FIRST(&pp->consumers);
|
2002-03-11 21:42:35 +00:00
|
|
|
while (cp != NULL) {
|
|
|
|
cp2 = LIST_NEXT(cp, consumers);
|
|
|
|
if (cp->spoiled) {
|
|
|
|
g_trace(G_T_TOPOLOGY, "spoiling %p (%s) (%p)",
|
|
|
|
cp, cp->geom->name, cp->geom->spoiled);
|
|
|
|
if (cp->geom->spoiled != NULL)
|
|
|
|
cp->geom->spoiled(cp);
|
2002-05-26 17:17:25 +00:00
|
|
|
else
|
|
|
|
cp->spoiled = 0;
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
cp = cp2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_LAST:
|
|
|
|
default:
|
|
|
|
KASSERT(1 == 0, ("Unknown event %d", ep->event));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-03-26 22:07:38 +00:00
|
|
|
one_event(void)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct g_event *ep;
|
|
|
|
struct g_provider *pp;
|
|
|
|
|
2003-03-23 21:58:09 +00:00
|
|
|
sx_xlock(&g_eventstall);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_lock();
|
|
|
|
for (;;) {
|
2002-09-27 20:18:16 +00:00
|
|
|
mtx_lock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
pp = TAILQ_FIRST(&g_doorstep);
|
|
|
|
if (pp != NULL)
|
|
|
|
TAILQ_REMOVE(&g_doorstep, pp, orphan);
|
2002-09-27 20:18:16 +00:00
|
|
|
mtx_unlock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
if (pp == NULL)
|
|
|
|
break;
|
2002-03-26 22:07:38 +00:00
|
|
|
g_orphan_register(pp);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
2002-09-27 20:38:36 +00:00
|
|
|
mtx_lock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
ep = TAILQ_FIRST(&g_events);
|
|
|
|
if (ep == NULL) {
|
2002-09-27 20:38:36 +00:00
|
|
|
mtx_unlock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_unlock();
|
2003-03-23 21:58:09 +00:00
|
|
|
sx_xunlock(&g_eventstall);
|
2002-03-11 21:42:35 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&g_events, ep, events);
|
2002-09-27 20:38:36 +00:00
|
|
|
mtx_unlock(&g_eventlock);
|
2002-03-26 22:07:38 +00:00
|
|
|
g_do_event(ep);
|
2002-11-04 09:31:02 +00:00
|
|
|
g_destroy_event(ep);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_pending_events--;
|
2002-11-04 09:31:02 +00:00
|
|
|
if (g_pending_events == 0)
|
2002-03-11 21:42:35 +00:00
|
|
|
wakeup(&g_pending_events);
|
|
|
|
g_topology_unlock();
|
2003-03-23 21:58:09 +00:00
|
|
|
sx_xunlock(&g_eventstall);
|
2002-03-11 21:42:35 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-26 22:07:38 +00:00
|
|
|
g_run_events()
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
|
2002-03-26 22:07:38 +00:00
|
|
|
while (one_event())
|
2002-03-11 21:42:35 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-02 20:41:18 +00:00
|
|
|
g_post_event(enum g_events ev, ...)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct g_event *ep;
|
2003-04-02 20:41:18 +00:00
|
|
|
va_list ap;
|
|
|
|
void *p;
|
|
|
|
int n;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2003-04-02 20:41:18 +00:00
|
|
|
g_trace(G_T_TOPOLOGY, "g_post_event(%d)", ev);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_topology_assert();
|
2003-02-19 05:47:46 +00:00
|
|
|
ep = g_malloc(sizeof *ep, M_WAITOK | M_ZERO);
|
2002-03-11 21:42:35 +00:00
|
|
|
ep->event = ev;
|
2003-04-02 20:41:18 +00:00
|
|
|
va_start(ap, ev);
|
|
|
|
for (n = 0; n < G_N_EVENTREFS; n++) {
|
|
|
|
p = va_arg(ap, void *);
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
g_trace(G_T_TOPOLOGY, " ref %p", p);
|
|
|
|
ep->ref[n++] = p;
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
2003-04-02 20:41:18 +00:00
|
|
|
va_end(ap);
|
|
|
|
KASSERT(p == NULL, ("Too many references to event"));
|
2002-09-27 20:38:36 +00:00
|
|
|
mtx_lock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_pending_events++;
|
|
|
|
TAILQ_INSERT_TAIL(&g_events, ep, events);
|
2002-09-27 20:38:36 +00:00
|
|
|
mtx_unlock(&g_eventlock);
|
|
|
|
wakeup(&g_wait_event);
|
|
|
|
}
|
|
|
|
|
2003-03-23 23:01:40 +00:00
|
|
|
void
|
2003-04-02 20:41:18 +00:00
|
|
|
g_cancel_event(void *ref)
|
2003-03-23 23:01:40 +00:00
|
|
|
{
|
|
|
|
struct g_event *ep, *epn;
|
2003-04-02 20:41:18 +00:00
|
|
|
u_int n;
|
2003-03-23 23:01:40 +00:00
|
|
|
|
|
|
|
mtx_lock(&g_eventlock);
|
2003-04-02 20:41:18 +00:00
|
|
|
for (ep = TAILQ_FIRST(&g_events); ep != NULL; ep = epn) {
|
2003-03-23 23:01:40 +00:00
|
|
|
epn = TAILQ_NEXT(ep, events);
|
2003-04-02 20:41:18 +00:00
|
|
|
for (n = 0; n < G_N_EVENTREFS; n++) {
|
|
|
|
if (ep->ref[n] == NULL)
|
|
|
|
break;
|
|
|
|
if (ep->ref[n] == ref) {
|
|
|
|
TAILQ_REMOVE(&g_events, ep, events);
|
2003-04-02 21:10:04 +00:00
|
|
|
if (ep->event == EV_CALL_ME)
|
|
|
|
ep->func(ep->arg, EV_CANCEL);
|
2003-04-02 20:41:18 +00:00
|
|
|
g_free(ep);
|
|
|
|
break;
|
|
|
|
}
|
2003-03-23 23:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock(&g_eventlock);
|
|
|
|
}
|
|
|
|
|
2002-09-27 20:38:36 +00:00
|
|
|
int
|
2003-04-02 20:41:18 +00:00
|
|
|
g_call_me(g_call_me_t *func, void *arg, ...)
|
2002-09-27 20:38:36 +00:00
|
|
|
{
|
|
|
|
struct g_event *ep;
|
2003-04-02 20:41:18 +00:00
|
|
|
va_list ap;
|
|
|
|
void *p;
|
|
|
|
u_int n;
|
2002-09-27 20:38:36 +00:00
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_call_me(%p, %p", func, arg);
|
|
|
|
ep = g_malloc(sizeof *ep, M_NOWAIT | M_ZERO);
|
|
|
|
if (ep == NULL)
|
|
|
|
return (ENOMEM);
|
2003-04-02 20:41:18 +00:00
|
|
|
va_start(ap, arg);
|
|
|
|
for (n = 0; n < G_N_EVENTREFS; n++) {
|
|
|
|
p = va_arg(ap, void *);
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
g_trace(G_T_TOPOLOGY, " ref %p", p);
|
|
|
|
ep->ref[n++] = p;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
KASSERT(p == NULL, ("Too many references to event"));
|
2002-09-27 20:38:36 +00:00
|
|
|
ep->event = EV_CALL_ME;
|
|
|
|
ep->func = func;
|
|
|
|
ep->arg = arg;
|
|
|
|
mtx_lock(&g_eventlock);
|
|
|
|
g_pending_events++;
|
|
|
|
TAILQ_INSERT_TAIL(&g_events, ep, events);
|
|
|
|
mtx_unlock(&g_eventlock);
|
2002-03-11 21:42:35 +00:00
|
|
|
wakeup(&g_wait_event);
|
2002-09-27 20:38:36 +00:00
|
|
|
return (0);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
2002-09-27 20:23:35 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
static void
|
|
|
|
geom_shutdown(void *foo __unused)
|
|
|
|
{
|
|
|
|
|
|
|
|
g_shutdown = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
void
|
|
|
|
g_event_init()
|
|
|
|
{
|
|
|
|
|
2002-09-27 20:23:35 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
|
|
|
|
EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
|
|
|
|
SHUTDOWN_PRI_FIRST);
|
|
|
|
#endif
|
2002-09-27 20:18:16 +00:00
|
|
|
mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);
|
2003-03-23 21:58:09 +00:00
|
|
|
sx_init(&g_eventstall, "GEOM event stalling");
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|