1999-01-21 00:31:31 +00:00
|
|
|
|
/* daemon.c: kernel part of Vinum daemon */
|
|
|
|
|
/*-
|
|
|
|
|
* Copyright (c) 1997, 1998
|
|
|
|
|
* Nan Yang Computer Services Limited. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed under the so-called ``Berkeley
|
|
|
|
|
* License'':
|
|
|
|
|
*
|
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software developed by Nan Yang Computer
|
|
|
|
|
* Services Limited.
|
|
|
|
|
* 4. Neither the name of the Company nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
1999-08-15 02:31:19 +00:00
|
|
|
|
*
|
1999-01-21 00:31:31 +00:00
|
|
|
|
* This software is provided ``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 company 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.
|
|
|
|
|
*
|
2000-01-05 06:03:56 +00:00
|
|
|
|
* $Id: vinumdaemon.c,v 1.8 2000/01/03 05:22:03 grog Exp grog $
|
1999-08-28 01:08:13 +00:00
|
|
|
|
* $FreeBSD$
|
1999-01-21 00:31:31 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1999-01-23 01:29:05 +00:00
|
|
|
|
#include <dev/vinum/vinumhdr.h>
|
|
|
|
|
#include <dev/vinum/request.h>
|
1999-01-21 00:31:31 +00:00
|
|
|
|
|
|
|
|
|
#ifdef VINUMDEBUG
|
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* declarations */
|
|
|
|
|
void recover_io(struct request *rq);
|
|
|
|
|
|
|
|
|
|
int daemon_options = 0; /* options */
|
1999-02-11 06:42:39 +00:00
|
|
|
|
int daemonpid; /* PID of daemon */
|
1999-10-13 03:16:05 +00:00
|
|
|
|
struct daemonq *daemonq; /* daemon's work queue */
|
|
|
|
|
struct daemonq *dqend; /* and the end of the queue */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We normally call Malloc to get a queue element. In interrupt
|
|
|
|
|
* context, we can't guarantee that we'll get one, since we're not
|
|
|
|
|
* allowed to wait. If malloc fails, use one of these elements.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define INTQSIZE 4
|
|
|
|
|
struct daemonq intq[INTQSIZE]; /* queue elements for interrupt context */
|
|
|
|
|
struct daemonq *intqp; /* and pointer in it */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
|
1999-08-15 02:31:19 +00:00
|
|
|
|
void
|
1999-01-21 00:31:31 +00:00
|
|
|
|
vinum_daemon(void)
|
|
|
|
|
{
|
|
|
|
|
int s;
|
1999-10-13 03:16:05 +00:00
|
|
|
|
struct daemonq *request;
|
1999-01-21 00:31:31 +00:00
|
|
|
|
|
2001-01-24 10:28:19 +00:00
|
|
|
|
PROC_LOCK(curproc);
|
|
|
|
|
curproc->p_flag |= P_SYSTEM; /* we're a system process */
|
|
|
|
|
PROC_UNLOCK(curproc);
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2001-01-24 10:28:19 +00:00
|
|
|
|
curproc->p_sflag |= PS_INMEM;
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
1999-01-21 00:31:31 +00:00
|
|
|
|
daemon_save_config(); /* start by saving the configuration */
|
1999-02-11 06:42:39 +00:00
|
|
|
|
daemonpid = curproc->p_pid; /* mark our territory */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
while (1) {
|
|
|
|
|
tsleep(&vinum_daemon, PRIBIO, "vinum", 0); /* wait for something to happen */
|
|
|
|
|
|
1999-02-11 06:42:39 +00:00
|
|
|
|
/*
|
|
|
|
|
* It's conceivable that, as the result of an
|
|
|
|
|
* I/O error, we'll be out of action long
|
|
|
|
|
* enough that another daemon gets started.
|
|
|
|
|
* That's OK, just give up gracefully.
|
|
|
|
|
*/
|
|
|
|
|
if (curproc->p_pid != daemonpid) { /* we've been ousted in our sleep */
|
|
|
|
|
if (daemon_options & daemon_verbose)
|
2000-01-05 06:03:56 +00:00
|
|
|
|
log(LOG_INFO, "vinum: abdicating\n");
|
1999-02-11 06:42:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
1999-01-21 00:31:31 +00:00
|
|
|
|
while (daemonq != NULL) { /* we have work to do, */
|
|
|
|
|
s = splhigh(); /* don't get interrupted here */
|
|
|
|
|
request = daemonq; /* get the request */
|
|
|
|
|
daemonq = daemonq->next; /* and detach it */
|
|
|
|
|
if (daemonq == NULL) /* got to the end, */
|
|
|
|
|
dqend = NULL; /* no end any more */
|
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
|
|
switch (request->type) {
|
1999-01-29 01:17:54 +00:00
|
|
|
|
/*
|
|
|
|
|
* We had an I/O error on a request. Go through the
|
1999-08-15 02:31:19 +00:00
|
|
|
|
* request and try to salvage it
|
1999-01-29 01:17:54 +00:00
|
|
|
|
*/
|
1999-01-21 00:31:31 +00:00
|
|
|
|
case daemonrq_ioerror:
|
|
|
|
|
if (daemon_options & daemon_verbose) {
|
|
|
|
|
struct request *rq = request->info.rq;
|
|
|
|
|
|
1999-03-02 06:53:15 +00:00
|
|
|
|
log(LOG_WARNING,
|
2002-03-15 18:49:47 +00:00
|
|
|
|
"vinum: recovering I/O request: %p\n%s dev %d.%d, offset 0x%llx, length %ld\n",
|
2000-01-05 06:03:56 +00:00
|
|
|
|
rq,
|
2000-03-20 10:44:49 +00:00
|
|
|
|
rq->bp->b_iocmd == BIO_READ ? "Read" : "Write",
|
1999-05-15 05:49:21 +00:00
|
|
|
|
major(rq->bp->b_dev),
|
|
|
|
|
minor(rq->bp->b_dev),
|
2002-11-07 21:52:51 +00:00
|
|
|
|
(long long)rq->bp->b_blkno,
|
1999-01-21 00:31:31 +00:00
|
|
|
|
rq->bp->b_bcount);
|
|
|
|
|
}
|
|
|
|
|
recover_io(request->info.rq); /* the failed request */
|
|
|
|
|
break;
|
|
|
|
|
|
1999-01-29 01:17:54 +00:00
|
|
|
|
/*
|
|
|
|
|
* Write the config to disk. We could end up with
|
1999-01-21 00:31:31 +00:00
|
|
|
|
* quite a few of these in a row. Only honour the
|
1999-08-15 02:31:19 +00:00
|
|
|
|
* last one
|
1999-01-29 01:17:54 +00:00
|
|
|
|
*/
|
1999-01-21 00:31:31 +00:00
|
|
|
|
case daemonrq_saveconfig:
|
|
|
|
|
if ((daemonq == NULL) /* no more requests */
|
|
|
|
|
||(daemonq->type != daemonrq_saveconfig)) { /* or the next isn't the same */
|
1999-03-02 06:53:15 +00:00
|
|
|
|
if (((daemon_options & daemon_noupdate) == 0) /* we're allowed to do it */
|
|
|
|
|
&&((vinum_conf.flags & VF_READING_CONFIG) == 0)) { /* and we're not building the config now */
|
|
|
|
|
/*
|
|
|
|
|
* We obviously don't want to save a
|
|
|
|
|
* partial configuration. Less obviously,
|
|
|
|
|
* we don't need to do anything if we're
|
|
|
|
|
* asked to write the config when we're
|
|
|
|
|
* building it up, because we save it at
|
|
|
|
|
* the end.
|
|
|
|
|
*/
|
1999-01-21 00:31:31 +00:00
|
|
|
|
if (daemon_options & daemon_verbose)
|
2000-01-05 06:03:56 +00:00
|
|
|
|
log(LOG_INFO, "vinum: saving config\n");
|
1999-01-21 00:31:31 +00:00
|
|
|
|
daemon_save_config(); /* save it */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case daemonrq_return: /* been told to stop */
|
|
|
|
|
if (daemon_options & daemon_verbose)
|
2000-01-05 06:03:56 +00:00
|
|
|
|
log(LOG_INFO, "vinum: stopping\n");
|
1999-01-21 00:31:31 +00:00
|
|
|
|
daemon_options |= daemon_stopped; /* note that we've stopped */
|
1999-03-31 08:42:52 +00:00
|
|
|
|
Free(request);
|
|
|
|
|
while (daemonq != NULL) { /* backed up requests, */
|
|
|
|
|
request = daemonq; /* get the request */
|
|
|
|
|
daemonq = daemonq->next; /* and detach it */
|
|
|
|
|
Free(request); /* then free it */
|
|
|
|
|
}
|
|
|
|
|
wakeup(&vinumclose); /* and wake any waiting vinum(8)s */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case daemonrq_ping: /* tell the caller we're here */
|
|
|
|
|
if (daemon_options & daemon_verbose)
|
2000-01-05 06:03:56 +00:00
|
|
|
|
log(LOG_INFO, "vinum: ping reply\n");
|
1999-01-21 00:31:31 +00:00
|
|
|
|
wakeup(&vinum_finddaemon); /* wake up the caller */
|
|
|
|
|
break;
|
|
|
|
|
|
1999-06-24 08:54:09 +00:00
|
|
|
|
case daemonrq_closedrive: /* close a drive */
|
|
|
|
|
close_drive(request->info.drive); /* do it */
|
|
|
|
|
break;
|
|
|
|
|
|
1999-01-21 00:31:31 +00:00
|
|
|
|
case daemonrq_init: /* initialize a plex */
|
|
|
|
|
/* XXX */
|
|
|
|
|
case daemonrq_revive: /* revive a subdisk */
|
|
|
|
|
/* XXX */
|
1999-06-24 08:54:09 +00:00
|
|
|
|
/* FALLTHROUGH */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
default:
|
1999-03-02 06:53:15 +00:00
|
|
|
|
log(LOG_WARNING, "Invalid request\n");
|
1999-01-21 00:31:31 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
1999-10-13 03:16:05 +00:00
|
|
|
|
if (request->privateinuse) /* one of ours, */
|
|
|
|
|
request->privateinuse = 0; /* no longer in use */
|
|
|
|
|
else
|
|
|
|
|
Free(request); /* return it */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-01-29 01:17:54 +00:00
|
|
|
|
/*
|
|
|
|
|
* Recover a failed I/O operation.
|
|
|
|
|
*
|
1999-01-21 00:31:31 +00:00
|
|
|
|
* The correct way to do this is to examine the request and determine
|
|
|
|
|
* how to recover each individual failure. In the case of a write,
|
|
|
|
|
* this could be as simple as doing nothing: the defective drives may
|
|
|
|
|
* already be down, and there may be nothing else to do. In case of
|
|
|
|
|
* a read, it will be necessary to retry if there are alternative
|
|
|
|
|
* copies of the data.
|
|
|
|
|
*
|
|
|
|
|
* The easy way (here) is just to reissue the request. This will take
|
|
|
|
|
* a little longer, but nothing like as long as the failure will have
|
|
|
|
|
* taken.
|
|
|
|
|
*
|
|
|
|
|
*/
|
1999-08-15 02:31:19 +00:00
|
|
|
|
void
|
1999-01-21 00:31:31 +00:00
|
|
|
|
recover_io(struct request *rq)
|
|
|
|
|
{
|
1999-08-15 02:31:19 +00:00
|
|
|
|
/*
|
|
|
|
|
* This should read:
|
|
|
|
|
*
|
|
|
|
|
* vinumstrategy(rq->bp);
|
|
|
|
|
*
|
|
|
|
|
* Negotiate with phk to get it fixed.
|
|
|
|
|
*/
|
2003-01-03 05:57:35 +00:00
|
|
|
|
DEV_STRATEGY(rq->bp); /* reissue the command */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Functions called to interface with the daemon */
|
|
|
|
|
|
|
|
|
|
/* queue a request for the daemon */
|
1999-08-15 02:31:19 +00:00
|
|
|
|
void
|
1999-01-21 00:31:31 +00:00
|
|
|
|
queue_daemon_request(enum daemonrq type, union daemoninfo info)
|
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
|
|
|
|
|
struct daemonq *qelt = (struct daemonq *) Malloc(sizeof(struct daemonq));
|
1999-10-13 03:16:05 +00:00
|
|
|
|
|
|
|
|
|
if (qelt == NULL) { /* malloc failed, we're prepared for that */
|
|
|
|
|
/*
|
|
|
|
|
* Take one of our spares. Give up if it's still in use; the only
|
|
|
|
|
* message we're likely to get here is a 'drive failed' message,
|
|
|
|
|
* and that'll come by again if we miss it.
|
|
|
|
|
*/
|
|
|
|
|
if (intqp->privateinuse) /* still in use? */
|
|
|
|
|
return; /* yes, give up */
|
|
|
|
|
qelt = intqp++;
|
|
|
|
|
if (intqp == &intq[INTQSIZE]) /* got to the end, */
|
|
|
|
|
intqp = intq; /* wrap around */
|
|
|
|
|
qelt->privateinuse = 1; /* it's ours, and it's in use */
|
|
|
|
|
} else
|
|
|
|
|
qelt->privateinuse = 0;
|
|
|
|
|
|
1999-01-21 00:31:31 +00:00
|
|
|
|
qelt->next = NULL; /* end of the chain */
|
|
|
|
|
qelt->type = type;
|
|
|
|
|
qelt->info = info;
|
|
|
|
|
s = splhigh();
|
|
|
|
|
if (daemonq) { /* something queued already */
|
|
|
|
|
dqend->next = qelt;
|
|
|
|
|
dqend = qelt;
|
|
|
|
|
} else { /* queue is empty, */
|
|
|
|
|
daemonq = qelt; /* this is the whole queue */
|
|
|
|
|
dqend = qelt;
|
|
|
|
|
}
|
|
|
|
|
splx(s);
|
|
|
|
|
wakeup(&vinum_daemon); /* and give the d<>mon a kick */
|
|
|
|
|
}
|
|
|
|
|
|
1999-01-29 01:17:54 +00:00
|
|
|
|
/*
|
|
|
|
|
* see if the daemon is running. Return 0 (no error)
|
1999-08-15 02:31:19 +00:00
|
|
|
|
* if it is, ESRCH otherwise
|
1999-01-29 01:17:54 +00:00
|
|
|
|
*/
|
1999-08-15 02:31:19 +00:00
|
|
|
|
int
|
1999-01-21 00:31:31 +00:00
|
|
|
|
vinum_finddaemon()
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
1999-03-02 06:53:15 +00:00
|
|
|
|
if (daemonpid != 0) { /* we think we have a daemon, */
|
2002-08-21 23:39:52 +00:00
|
|
|
|
queue_daemon_request(daemonrq_ping, (union daemoninfo) 0); /* queue a ping */
|
1999-03-02 06:53:15 +00:00
|
|
|
|
result = tsleep(&vinum_finddaemon, PUSER, "reap", 2 * hz);
|
|
|
|
|
if (result == 0) /* yup, the daemon's up and running */
|
|
|
|
|
return 0;
|
1999-01-21 00:31:31 +00:00
|
|
|
|
}
|
1999-03-02 06:53:15 +00:00
|
|
|
|
/* no daemon, or we couldn't talk to it: start it */
|
|
|
|
|
vinum_daemon(); /* start the daemon */
|
1999-01-21 00:31:31 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-15 02:31:19 +00:00
|
|
|
|
int
|
1999-01-21 00:31:31 +00:00
|
|
|
|
vinum_setdaemonopts(int options)
|
|
|
|
|
{
|
|
|
|
|
daemon_options = options;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|