Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.81
      1 /*	$NetBSD: kern_subr.c,v 1.81 2002/06/17 16:22:51 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Luke Mewburn.
     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. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * (c) UNIX System Laboratories, Inc.
     44  * All or some portions of this file are derived from material licensed
     45  * to the University of California by American Telephone and Telegraph
     46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  * the permission of UNIX System Laboratories, Inc.
     48  *
     49  * Copyright (c) 1992, 1993
     50  *	The Regents of the University of California.  All rights reserved.
     51  *
     52  * This software was developed by the Computer Systems Engineering group
     53  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     54  * contributed to Berkeley.
     55  *
     56  * All advertising materials mentioning features or use of this software
     57  * must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Lawrence Berkeley Laboratory.
     60  *
     61  * Redistribution and use in source and binary forms, with or without
     62  * modification, are permitted provided that the following conditions
     63  * are met:
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  * 2. Redistributions in binary form must reproduce the above copyright
     67  *    notice, this list of conditions and the following disclaimer in the
     68  *    documentation and/or other materials provided with the distribution.
     69  * 3. All advertising materials mentioning features or use of this software
     70  *    must display the following acknowledgement:
     71  *	This product includes software developed by the University of
     72  *	California, Berkeley and its contributors.
     73  * 4. Neither the name of the University nor the names of its contributors
     74  *    may be used to endorse or promote products derived from this software
     75  *    without specific prior written permission.
     76  *
     77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87  * SUCH DAMAGE.
     88  *
     89  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     90  */
     91 
     92 #include <sys/cdefs.h>
     93 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.81 2002/06/17 16:22:51 christos Exp $");
     94 
     95 #include "opt_ddb.h"
     96 #include "opt_md.h"
     97 #include "opt_syscall_debug.h"
     98 #include "opt_ktrace.h"
     99 #include "opt_systrace.h"
    100 
    101 #include <sys/param.h>
    102 #include <sys/systm.h>
    103 #include <sys/proc.h>
    104 #include <sys/malloc.h>
    105 #include <sys/mount.h>
    106 #include <sys/device.h>
    107 #include <sys/reboot.h>
    108 #include <sys/conf.h>
    109 #include <sys/disklabel.h>
    110 #include <sys/queue.h>
    111 #include <sys/systrace.h>
    112 #include <sys/ktrace.h>
    113 
    114 #include <dev/cons.h>
    115 
    116 #include <net/if.h>
    117 
    118 /* XXX these should eventually move to subr_autoconf.c */
    119 static int findblkmajor __P((const char *));
    120 const char *findblkname __P((int));
    121 static struct device *finddevice __P((const char *));
    122 static struct device *getdisk __P((char *, int, int, dev_t *, int));
    123 static struct device *parsedisk __P((char *, int, int, dev_t *));
    124 
    125 /*
    126  * A generic linear hook.
    127  */
    128 struct hook_desc {
    129 	LIST_ENTRY(hook_desc) hk_list;
    130 	void	(*hk_fn) __P((void *));
    131 	void	*hk_arg;
    132 };
    133 typedef LIST_HEAD(, hook_desc) hook_list_t;
    134 
    135 static void *hook_establish __P((hook_list_t *, void (*)(void *), void *));
    136 static void hook_disestablish __P((hook_list_t *, void *));
    137 static void hook_destroy __P((hook_list_t *));
    138 static void hook_proc_run __P((hook_list_t *, struct proc *));
    139 
    140 int
    141 uiomove(buf, n, uio)
    142 	void *buf;
    143 	int n;
    144 	struct uio *uio;
    145 {
    146 	struct iovec *iov;
    147 	u_int cnt;
    148 	int error = 0;
    149 	char *cp = buf;
    150 	struct proc *p = uio->uio_procp;
    151 
    152 #ifdef DIAGNOSTIC
    153 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    154 		panic("uiomove: mode");
    155 	if (uio->uio_segflg == UIO_USERSPACE && p != curproc)
    156 		panic("uiomove proc");
    157 #endif
    158 	while (n > 0 && uio->uio_resid) {
    159 		iov = uio->uio_iov;
    160 		cnt = iov->iov_len;
    161 		if (cnt == 0) {
    162 			uio->uio_iov++;
    163 			uio->uio_iovcnt--;
    164 			continue;
    165 		}
    166 		if (cnt > n)
    167 			cnt = n;
    168 		switch (uio->uio_segflg) {
    169 
    170 		case UIO_USERSPACE:
    171 			KDASSERT(p->p_cpu != NULL);
    172 			KDASSERT(p->p_cpu == curcpu());
    173 			if (p->p_cpu->ci_schedstate.spc_flags &
    174 			    SPCF_SHOULDYIELD)
    175 				preempt(NULL);
    176 			if (uio->uio_rw == UIO_READ)
    177 				error = copyout(cp, iov->iov_base, cnt);
    178 			else
    179 				error = copyin(iov->iov_base, cp, cnt);
    180 			if (error)
    181 				return (error);
    182 			break;
    183 
    184 		case UIO_SYSSPACE:
    185 			if (uio->uio_rw == UIO_READ)
    186 				error = kcopy(cp, iov->iov_base, cnt);
    187 			else
    188 				error = kcopy(iov->iov_base, cp, cnt);
    189 			if (error)
    190 				return (error);
    191 			break;
    192 		}
    193 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    194 		iov->iov_len -= cnt;
    195 		uio->uio_resid -= cnt;
    196 		uio->uio_offset += cnt;
    197 		cp += cnt;
    198 		n -= cnt;
    199 	}
    200 	return (error);
    201 }
    202 
    203 /*
    204  * Give next character to user as result of read.
    205  */
    206 int
    207 ureadc(c, uio)
    208 	int c;
    209 	struct uio *uio;
    210 {
    211 	struct iovec *iov;
    212 
    213 	if (uio->uio_resid <= 0)
    214 		panic("ureadc: non-positive resid");
    215 again:
    216 	if (uio->uio_iovcnt <= 0)
    217 		panic("ureadc: non-positive iovcnt");
    218 	iov = uio->uio_iov;
    219 	if (iov->iov_len <= 0) {
    220 		uio->uio_iovcnt--;
    221 		uio->uio_iov++;
    222 		goto again;
    223 	}
    224 	switch (uio->uio_segflg) {
    225 
    226 	case UIO_USERSPACE:
    227 		if (subyte(iov->iov_base, c) < 0)
    228 			return (EFAULT);
    229 		break;
    230 
    231 	case UIO_SYSSPACE:
    232 		*(char *)iov->iov_base = c;
    233 		break;
    234 	}
    235 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    236 	iov->iov_len--;
    237 	uio->uio_resid--;
    238 	uio->uio_offset++;
    239 	return (0);
    240 }
    241 
    242 /*
    243  * General routine to allocate a hash table.
    244  * Allocate enough memory to hold at least `elements' list-head pointers.
    245  * Return a pointer to the allocated space and set *hashmask to a pattern
    246  * suitable for masking a value to use as an index into the returned array.
    247  */
    248 void *
    249 hashinit(elements, htype, mtype, mflags, hashmask)
    250 	int elements;
    251 	enum hashtype htype;
    252 	int mtype, mflags;
    253 	u_long *hashmask;
    254 {
    255 	long hashsize;
    256 	LIST_HEAD(, generic) *hashtbl_list;
    257 	TAILQ_HEAD(, generic) *hashtbl_tailq;
    258 	int i, esize;
    259 	void *p;
    260 
    261 	if (elements <= 0)
    262 		panic("hashinit: bad cnt");
    263 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    264 		continue;
    265 
    266 	switch (htype) {
    267 	case HASH_LIST:
    268 		esize = sizeof(*hashtbl_list);
    269 		break;
    270 	case HASH_TAILQ:
    271 		esize = sizeof(*hashtbl_tailq);
    272 		break;
    273 #ifdef DIAGNOSTIC
    274 	default:
    275 		panic("hashinit: invalid table type");
    276 #endif
    277 	}
    278 
    279 	if ((p = malloc((u_long)hashsize * esize, mtype, mflags)) == NULL)
    280 		return (NULL);
    281 
    282 	switch (htype) {
    283 	case HASH_LIST:
    284 		hashtbl_list = p;
    285 		for (i = 0; i < hashsize; i++)
    286 			LIST_INIT(&hashtbl_list[i]);
    287 		break;
    288 	case HASH_TAILQ:
    289 		hashtbl_tailq = p;
    290 		for (i = 0; i < hashsize; i++)
    291 			TAILQ_INIT(&hashtbl_tailq[i]);
    292 		break;
    293 	}
    294 	*hashmask = hashsize - 1;
    295 	return (p);
    296 }
    297 
    298 /*
    299  * Free memory from hash table previosly allocated via hashinit().
    300  */
    301 void
    302 hashdone(hashtbl, mtype)
    303 	void *hashtbl;
    304 	int mtype;
    305 {
    306 
    307 	free(hashtbl, mtype);
    308 }
    309 
    310 
    311 static void *
    312 hook_establish(list, fn, arg)
    313 	hook_list_t *list;
    314 	void (*fn) __P((void *));
    315 	void *arg;
    316 {
    317 	struct hook_desc *hd;
    318 
    319 	hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
    320 	if (hd == NULL)
    321 		return (NULL);
    322 
    323 	hd->hk_fn = fn;
    324 	hd->hk_arg = arg;
    325 	LIST_INSERT_HEAD(list, hd, hk_list);
    326 
    327 	return (hd);
    328 }
    329 
    330 static void
    331 hook_disestablish(list, vhook)
    332 	hook_list_t *list;
    333 	void *vhook;
    334 {
    335 #ifdef DIAGNOSTIC
    336 	struct hook_desc *hd;
    337 
    338 	for (hd = list->lh_first; hd != NULL; hd = hd->hk_list.le_next)
    339                 if (hd == vhook)
    340 			break;
    341 	if (hd == NULL)
    342 		panic("hook_disestablish: hook not established");
    343 #endif
    344 	LIST_REMOVE((struct hook_desc *)vhook, hk_list);
    345 	free(vhook, M_DEVBUF);
    346 }
    347 
    348 static void
    349 hook_destroy(list)
    350 	hook_list_t *list;
    351 {
    352 	struct hook_desc *hd;
    353 
    354 	while ((hd = list->lh_first) != NULL) {
    355 		LIST_REMOVE(hd, hk_list);
    356 		free(hd, M_DEVBUF);
    357 	}
    358 }
    359 
    360 static void
    361 hook_proc_run(list, p)
    362 	hook_list_t *list;
    363 	struct proc *p;
    364 {
    365 	struct hook_desc *hd;
    366 
    367 	for (hd = LIST_FIRST(list); hd != NULL; hd = LIST_NEXT(hd, hk_list)) {
    368 		((void (*) __P((struct proc *, void *)))*hd->hk_fn)(p,
    369 		    hd->hk_arg);
    370 	}
    371 }
    372 
    373 /*
    374  * "Shutdown hook" types, functions, and variables.
    375  *
    376  * Should be invoked immediately before the
    377  * system is halted or rebooted, i.e. after file systems unmounted,
    378  * after crash dump done, etc.
    379  *
    380  * Each shutdown hook is removed from the list before it's run, so that
    381  * it won't be run again.
    382  */
    383 
    384 hook_list_t shutdownhook_list;
    385 
    386 void *
    387 shutdownhook_establish(fn, arg)
    388 	void (*fn) __P((void *));
    389 	void *arg;
    390 {
    391 	return hook_establish(&shutdownhook_list, fn, arg);
    392 }
    393 
    394 void
    395 shutdownhook_disestablish(vhook)
    396 	void *vhook;
    397 {
    398 	return hook_disestablish(&shutdownhook_list, vhook);
    399 }
    400 
    401 /*
    402  * Run shutdown hooks.  Should be invoked immediately before the
    403  * system is halted or rebooted, i.e. after file systems unmounted,
    404  * after crash dump done, etc.
    405  *
    406  * Each shutdown hook is removed from the list before it's run, so that
    407  * it won't be run again.
    408  */
    409 void
    410 doshutdownhooks()
    411 {
    412 	struct hook_desc *dp;
    413 
    414 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    415 		LIST_REMOVE(dp, hk_list);
    416 		(*dp->hk_fn)(dp->hk_arg);
    417 #if 0
    418 		/*
    419 		 * Don't bother freeing the hook structure,, since we may
    420 		 * be rebooting because of a memory corruption problem,
    421 		 * and this might only make things worse.  It doesn't
    422 		 * matter, anyway, since the system is just about to
    423 		 * reboot.
    424 		 */
    425 		free(dp, M_DEVBUF);
    426 #endif
    427 	}
    428 }
    429 
    430 /*
    431  * "Mountroot hook" types, functions, and variables.
    432  */
    433 
    434 hook_list_t mountroothook_list;
    435 
    436 void *
    437 mountroothook_establish(fn, dev)
    438 	void (*fn) __P((struct device *));
    439 	struct device *dev;
    440 {
    441 	return hook_establish(&mountroothook_list, (void (*)__P((void *)))fn,
    442 	    dev);
    443 }
    444 
    445 void
    446 mountroothook_disestablish(vhook)
    447 	void *vhook;
    448 {
    449 	return hook_disestablish(&mountroothook_list, vhook);
    450 }
    451 
    452 void
    453 mountroothook_destroy()
    454 {
    455 	hook_destroy(&mountroothook_list);
    456 }
    457 
    458 void
    459 domountroothook()
    460 {
    461 	struct hook_desc *hd;
    462 
    463 	for (hd = mountroothook_list.lh_first; hd != NULL;
    464 	    hd = hd->hk_list.le_next) {
    465 		if (hd->hk_arg == (void *)root_device) {
    466 			(*hd->hk_fn)(hd->hk_arg);
    467 			return;
    468 		}
    469 	}
    470 }
    471 
    472 hook_list_t exechook_list;
    473 
    474 void *
    475 exechook_establish(fn, arg)
    476 	void (*fn) __P((struct proc *, void *));
    477 	void *arg;
    478 {
    479 	return hook_establish(&exechook_list, (void (*) __P((void *)))fn, arg);
    480 }
    481 
    482 void
    483 exechook_disestablish(vhook)
    484 	void *vhook;
    485 {
    486 	hook_disestablish(&exechook_list, vhook);
    487 }
    488 
    489 /*
    490  * Run exec hooks.
    491  */
    492 void
    493 doexechooks(p)
    494 	struct proc *p;
    495 {
    496 	hook_proc_run(&exechook_list, p);
    497 }
    498 
    499 hook_list_t exithook_list;
    500 
    501 void *
    502 exithook_establish(fn, arg)
    503 	void (*fn) __P((struct proc *, void *));
    504 	void *arg;
    505 {
    506 	return hook_establish(&exithook_list, (void (*) __P((void *)))fn, arg);
    507 }
    508 
    509 void
    510 exithook_disestablish(vhook)
    511 	void *vhook;
    512 {
    513 	hook_disestablish(&exithook_list, vhook);
    514 }
    515 
    516 /*
    517  * Run exit hooks.
    518  */
    519 void
    520 doexithooks(p)
    521 	struct proc *p;
    522 {
    523 	hook_proc_run(&exithook_list, p);
    524 }
    525 
    526 /*
    527  * "Power hook" types, functions, and variables.
    528  * The list of power hooks is kept ordered with the last registered hook
    529  * first.
    530  * When running the hooks on power down the hooks are called in reverse
    531  * registration order, when powering up in registration order.
    532  */
    533 struct powerhook_desc {
    534 	CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
    535 	void	(*sfd_fn) __P((int, void *));
    536 	void	*sfd_arg;
    537 };
    538 
    539 CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
    540 	CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
    541 
    542 void *
    543 powerhook_establish(fn, arg)
    544 	void (*fn) __P((int, void *));
    545 	void *arg;
    546 {
    547 	struct powerhook_desc *ndp;
    548 
    549 	ndp = (struct powerhook_desc *)
    550 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    551 	if (ndp == NULL)
    552 		return (NULL);
    553 
    554 	ndp->sfd_fn = fn;
    555 	ndp->sfd_arg = arg;
    556 	CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
    557 
    558 	return (ndp);
    559 }
    560 
    561 void
    562 powerhook_disestablish(vhook)
    563 	void *vhook;
    564 {
    565 #ifdef DIAGNOSTIC
    566 	struct powerhook_desc *dp;
    567 
    568 	CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
    569                 if (dp == vhook)
    570 			goto found;
    571 	panic("powerhook_disestablish: hook not established");
    572  found:
    573 #endif
    574 
    575 	CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
    576 	    sfd_list);
    577 	free(vhook, M_DEVBUF);
    578 }
    579 
    580 /*
    581  * Run power hooks.
    582  */
    583 void
    584 dopowerhooks(why)
    585 	int why;
    586 {
    587 	struct powerhook_desc *dp;
    588 
    589 	if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
    590 		CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
    591 			(*dp->sfd_fn)(why, dp->sfd_arg);
    592 		}
    593 	} else {
    594 		CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
    595 			(*dp->sfd_fn)(why, dp->sfd_arg);
    596 		}
    597 	}
    598 }
    599 
    600 /*
    601  * Determine the root device and, if instructed to, the root file system.
    602  */
    603 
    604 #include "md.h"
    605 #if NMD == 0
    606 #undef MEMORY_DISK_HOOKS
    607 #endif
    608 
    609 #ifdef MEMORY_DISK_HOOKS
    610 static struct device fakemdrootdev[NMD];
    611 #endif
    612 
    613 #include "raid.h"
    614 #if NRAID == 1
    615 #define BOOT_FROM_RAID_HOOKS 1
    616 #endif
    617 
    618 #ifdef BOOT_FROM_RAID_HOOKS
    619 extern int numraid;
    620 extern struct device *raidrootdev;
    621 #endif
    622 
    623 void
    624 setroot(bootdv, bootpartition)
    625 	struct device *bootdv;
    626 	int bootpartition;
    627 {
    628 	struct device *dv;
    629 	int len;
    630 #ifdef MEMORY_DISK_HOOKS
    631 	int i;
    632 #endif
    633 	dev_t nrootdev;
    634 	dev_t ndumpdev = NODEV;
    635 	char buf[128];
    636 	const char *rootdevname;
    637 	const char *dumpdevname;
    638 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    639 	struct device *dumpdv = NULL;
    640 	struct ifnet *ifp;
    641 	const char *deffsname;
    642 	struct vfsops *vops;
    643 
    644 #ifdef MEMORY_DISK_HOOKS
    645 	for (i = 0; i < NMD; i++) {
    646 		fakemdrootdev[i].dv_class  = DV_DISK;
    647 		fakemdrootdev[i].dv_cfdata = NULL;
    648 		fakemdrootdev[i].dv_unit   = i;
    649 		fakemdrootdev[i].dv_parent = NULL;
    650 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    651 	}
    652 #endif /* MEMORY_DISK_HOOKS */
    653 
    654 #ifdef MEMORY_DISK_IS_ROOT
    655 	bootdv = &fakemdrootdev[0];
    656 	bootpartition = 0;
    657 #endif
    658 
    659 	/*
    660 	 * If NFS is specified as the file system, and we found
    661 	 * a DV_DISK boot device (or no boot device at all), then
    662 	 * find a reasonable network interface for "rootspec".
    663 	 */
    664 	vops = vfs_getopsbyname("nfs");
    665 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    666 	    rootspec == NULL &&
    667 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    668 		for (ifp = ifnet.tqh_first; ifp != NULL;
    669 		    ifp = ifp->if_list.tqe_next)
    670 			if ((ifp->if_flags &
    671 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    672 				break;
    673 		if (ifp == NULL) {
    674 			/*
    675 			 * Can't find a suitable interface; ask the
    676 			 * user.
    677 			 */
    678 			boothowto |= RB_ASKNAME;
    679 		} else {
    680 			/*
    681 			 * Have a suitable interface; behave as if
    682 			 * the user specified this interface.
    683 			 */
    684 			rootspec = (const char *)ifp->if_xname;
    685 		}
    686 	}
    687 
    688 	/*
    689 	 * If wildcarded root and we the boot device wasn't determined,
    690 	 * ask the user.
    691 	 */
    692 	if (rootspec == NULL && bootdv == NULL)
    693 		boothowto |= RB_ASKNAME;
    694 
    695  top:
    696 	if (boothowto & RB_ASKNAME) {
    697 		struct device *defdumpdv;
    698 
    699 		for (;;) {
    700 			printf("root device");
    701 			if (bootdv != NULL) {
    702 				printf(" (default %s", bootdv->dv_xname);
    703 				if (bootdv->dv_class == DV_DISK)
    704 					printf("%c", bootpartition + 'a');
    705 				printf(")");
    706 			}
    707 			printf(": ");
    708 			len = cngetsn(buf, sizeof(buf));
    709 			if (len == 0 && bootdv != NULL) {
    710 				strcpy(buf, bootdv->dv_xname);
    711 				len = strlen(buf);
    712 			}
    713 			if (len > 0 && buf[len - 1] == '*') {
    714 				buf[--len] = '\0';
    715 				dv = getdisk(buf, len, 1, &nrootdev, 0);
    716 				if (dv != NULL) {
    717 					rootdv = dv;
    718 					break;
    719 				}
    720 			}
    721 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    722 			if (dv != NULL) {
    723 				rootdv = dv;
    724 				break;
    725 			}
    726 		}
    727 
    728 		/*
    729 		 * Set up the default dump device.  If root is on
    730 		 * a network device, there is no default dump
    731 		 * device, since we don't support dumps to the
    732 		 * network.
    733 		 */
    734 		if (rootdv->dv_class == DV_IFNET)
    735 			defdumpdv = NULL;
    736 		else
    737 			defdumpdv = rootdv;
    738 
    739 		for (;;) {
    740 			printf("dump device");
    741 			if (defdumpdv != NULL) {
    742 				/*
    743 				 * Note, we know it's a disk if we get here.
    744 				 */
    745 				printf(" (default %sb)", defdumpdv->dv_xname);
    746 			}
    747 			printf(": ");
    748 			len = cngetsn(buf, sizeof(buf));
    749 			if (len == 0) {
    750 				if (defdumpdv != NULL) {
    751 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    752 					    DISKUNIT(nrootdev), 1);
    753 				}
    754 				dumpdv = defdumpdv;
    755 				break;
    756 			}
    757 			if (len == 4 && strcmp(buf, "none") == 0) {
    758 				dumpdv = NULL;
    759 				break;
    760 			}
    761 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
    762 			if (dv != NULL) {
    763 				dumpdv = dv;
    764 				break;
    765 			}
    766 		}
    767 
    768 		rootdev = nrootdev;
    769 		dumpdev = ndumpdev;
    770 
    771 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    772 		     vops = LIST_NEXT(vops, vfs_list)) {
    773 			if (vops->vfs_mountroot != NULL &&
    774 			    vops->vfs_mountroot == mountroot)
    775 			break;
    776 		}
    777 
    778 		if (vops == NULL) {
    779 			mountroot = NULL;
    780 			deffsname = "generic";
    781 		} else
    782 			deffsname = vops->vfs_name;
    783 
    784 		for (;;) {
    785 			printf("file system (default %s): ", deffsname);
    786 			len = cngetsn(buf, sizeof(buf));
    787 			if (len == 0)
    788 				break;
    789 			if (len == 4 && strcmp(buf, "halt") == 0)
    790 				cpu_reboot(RB_HALT, NULL);
    791 			else if (len == 6 && strcmp(buf, "reboot") == 0)
    792 				cpu_reboot(0, NULL);
    793 #if defined(DDB)
    794 			else if (len == 3 && strcmp(buf, "ddb") == 0) {
    795 				console_debugger();
    796 			}
    797 #endif
    798 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    799 				mountroot = NULL;
    800 				break;
    801 			}
    802 			vops = vfs_getopsbyname(buf);
    803 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    804 				printf("use one of: generic");
    805 				for (vops = LIST_FIRST(&vfs_list);
    806 				     vops != NULL;
    807 				     vops = LIST_NEXT(vops, vfs_list)) {
    808 					if (vops->vfs_mountroot != NULL)
    809 						printf(" %s", vops->vfs_name);
    810 				}
    811 #if defined(DDB)
    812 				printf(" ddb");
    813 #endif
    814 				printf(" halt reboot\n");
    815 			} else {
    816 				mountroot = vops->vfs_mountroot;
    817 				break;
    818 			}
    819 		}
    820 
    821 	} else if (rootspec == NULL) {
    822 		int majdev;
    823 
    824 		/*
    825 		 * Wildcarded root; use the boot device.
    826 		 */
    827 		rootdv = bootdv;
    828 
    829 		majdev = findblkmajor(bootdv->dv_xname);
    830 		if (majdev >= 0) {
    831 			/*
    832 			 * Root is on a disk.  `bootpartition' is root.
    833 			 */
    834 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    835 			    bootpartition);
    836 		}
    837 	} else {
    838 
    839 		/*
    840 		 * `root on <dev> ...'
    841 		 */
    842 
    843 		/*
    844 		 * If it's a network interface, we can bail out
    845 		 * early.
    846 		 */
    847 		dv = finddevice(rootspec);
    848 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    849 			rootdv = dv;
    850 			goto haveroot;
    851 		}
    852 
    853 		rootdevname = findblkname(major(rootdev));
    854 		if (rootdevname == NULL) {
    855 			printf("unknown device major 0x%x\n", rootdev);
    856 			boothowto |= RB_ASKNAME;
    857 			goto top;
    858 		}
    859 		memset(buf, 0, sizeof(buf));
    860 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    861 
    862 		rootdv = finddevice(buf);
    863 		if (rootdv == NULL) {
    864 			printf("device %s (0x%x) not configured\n",
    865 			    buf, rootdev);
    866 			boothowto |= RB_ASKNAME;
    867 			goto top;
    868 		}
    869 	}
    870 
    871  haveroot:
    872 
    873 	root_device = rootdv;
    874 
    875 	switch (rootdv->dv_class) {
    876 	case DV_IFNET:
    877 		printf("root on %s", rootdv->dv_xname);
    878 		break;
    879 
    880 	case DV_DISK:
    881 		printf("root on %s%c", rootdv->dv_xname,
    882 		    DISKPART(rootdev) + 'a');
    883 		break;
    884 
    885 	default:
    886 		printf("can't determine root device\n");
    887 		boothowto |= RB_ASKNAME;
    888 		goto top;
    889 	}
    890 
    891 	/*
    892 	 * Now configure the dump device.
    893 	 *
    894 	 * If we haven't figured out the dump device, do so, with
    895 	 * the following rules:
    896 	 *
    897 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    898 	 *
    899 	 *	(b) If dumpspec is set, try to use it.  If the device
    900 	 *	    is not available, punt.
    901 	 *
    902 	 *	(c) If dumpspec is not set, the dump device is
    903 	 *	    wildcarded or unspecified.  If the root device
    904 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    905 	 *	    of the root device.
    906 	 */
    907 
    908 	if (boothowto & RB_ASKNAME) {		/* (a) */
    909 		if (dumpdv == NULL)
    910 			goto nodumpdev;
    911 	} else if (dumpspec != NULL) {		/* (b) */
    912 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    913 			/*
    914 			 * Operator doesn't want a dump device.
    915 			 * Or looks like they tried to pick a network
    916 			 * device.  Oops.
    917 			 */
    918 			goto nodumpdev;
    919 		}
    920 
    921 		dumpdevname = findblkname(major(dumpdev));
    922 		if (dumpdevname == NULL)
    923 			goto nodumpdev;
    924 		memset(buf, 0, sizeof(buf));
    925 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
    926 
    927 		dumpdv = finddevice(buf);
    928 		if (dumpdv == NULL) {
    929 			/*
    930 			 * Device not configured.
    931 			 */
    932 			goto nodumpdev;
    933 		}
    934 	} else {				/* (c) */
    935 		if (rootdv->dv_class == DV_IFNET)
    936 			goto nodumpdev;
    937 		else {
    938 			dumpdv = rootdv;
    939 			dumpdev = MAKEDISKDEV(major(rootdev),
    940 			    dumpdv->dv_unit, 1);
    941 		}
    942 	}
    943 
    944 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
    945 	return;
    946 
    947  nodumpdev:
    948 	dumpdev = NODEV;
    949 	printf("\n");
    950 }
    951 
    952 static int
    953 findblkmajor(name)
    954 	const char *name;
    955 {
    956 	int i;
    957 
    958 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
    959 		if (strncmp(name, dev_name2blk[i].d_name,
    960 		    strlen(dev_name2blk[i].d_name)) == 0)
    961 			return (dev_name2blk[i].d_maj);
    962 	return (-1);
    963 }
    964 
    965 const char *
    966 findblkname(maj)
    967 	int maj;
    968 {
    969 	int i;
    970 
    971 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
    972 		if (dev_name2blk[i].d_maj == maj)
    973 			return (dev_name2blk[i].d_name);
    974 	return (NULL);
    975 }
    976 
    977 static struct device *
    978 finddevice(name)
    979 	const char *name;
    980 {
    981 	struct device *dv;
    982 #ifdef BOOT_FROM_RAID_HOOKS
    983 	int j;
    984 
    985 	for (j = 0; j < numraid; j++) {
    986 		if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
    987 			dv = &raidrootdev[j];
    988 			return (dv);
    989 		}
    990 	}
    991 #endif
    992 
    993 	for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
    994 	    dv = TAILQ_NEXT(dv, dv_list))
    995 		if (strcmp(dv->dv_xname, name) == 0)
    996 			break;
    997 	return (dv);
    998 }
    999 
   1000 static struct device *
   1001 getdisk(str, len, defpart, devp, isdump)
   1002 	char *str;
   1003 	int len, defpart;
   1004 	dev_t *devp;
   1005 	int isdump;
   1006 {
   1007 	struct device	*dv;
   1008 #ifdef MEMORY_DISK_HOOKS
   1009 	int		i;
   1010 #endif
   1011 #ifdef BOOT_FROM_RAID_HOOKS
   1012 	int 		j;
   1013 #endif
   1014 
   1015 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
   1016 		printf("use one of:");
   1017 #ifdef MEMORY_DISK_HOOKS
   1018 		if (isdump == 0)
   1019 			for (i = 0; i < NMD; i++)
   1020 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
   1021 				    'a' + MAXPARTITIONS - 1);
   1022 #endif
   1023 #ifdef BOOT_FROM_RAID_HOOKS
   1024 		if (isdump == 0)
   1025 			for (j = 0; j < numraid; j++)
   1026 				printf(" %s[a-%c]", raidrootdev[j].dv_xname,
   1027 				    'a' + MAXPARTITIONS - 1);
   1028 #endif
   1029 		for (dv = alldevs.tqh_first; dv != NULL;
   1030 		    dv = dv->dv_list.tqe_next) {
   1031 			if (dv->dv_class == DV_DISK)
   1032 				printf(" %s[a-%c]", dv->dv_xname,
   1033 				    'a' + MAXPARTITIONS - 1);
   1034 			if (isdump == 0 && dv->dv_class == DV_IFNET)
   1035 				printf(" %s", dv->dv_xname);
   1036 		}
   1037 		if (isdump)
   1038 			printf(" none");
   1039 #if defined(DDB)
   1040 		printf(" ddb");
   1041 #endif
   1042 		printf(" halt reboot\n");
   1043 	}
   1044 	return (dv);
   1045 }
   1046 
   1047 static struct device *
   1048 parsedisk(str, len, defpart, devp)
   1049 	char *str;
   1050 	int len, defpart;
   1051 	dev_t *devp;
   1052 {
   1053 	struct device *dv;
   1054 	char *cp, c;
   1055 	int majdev, part;
   1056 #ifdef MEMORY_DISK_HOOKS
   1057 	int i;
   1058 #endif
   1059 	if (len == 0)
   1060 		return (NULL);
   1061 
   1062 	if (len == 4 && strcmp(str, "halt") == 0)
   1063 		cpu_reboot(RB_HALT, NULL);
   1064 	else if (len == 6 && strcmp(str, "reboot") == 0)
   1065 		cpu_reboot(0, NULL);
   1066 #if defined(DDB)
   1067 	else if (len == 3 && strcmp(str, "ddb") == 0)
   1068 		console_debugger();
   1069 #endif
   1070 
   1071 	cp = str + len - 1;
   1072 	c = *cp;
   1073 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
   1074 		part = c - 'a';
   1075 		*cp = '\0';
   1076 	} else
   1077 		part = defpart;
   1078 
   1079 #ifdef MEMORY_DISK_HOOKS
   1080 	for (i = 0; i < NMD; i++)
   1081 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
   1082 			dv = &fakemdrootdev[i];
   1083 			goto gotdisk;
   1084 		}
   1085 #endif
   1086 
   1087 	dv = finddevice(str);
   1088 	if (dv != NULL) {
   1089 		if (dv->dv_class == DV_DISK) {
   1090 #ifdef MEMORY_DISK_HOOKS
   1091  gotdisk:
   1092 #endif
   1093 			majdev = findblkmajor(dv->dv_xname);
   1094 			if (majdev < 0)
   1095 				panic("parsedisk");
   1096 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
   1097 		}
   1098 
   1099 		if (dv->dv_class == DV_IFNET)
   1100 			*devp = NODEV;
   1101 	}
   1102 
   1103 	*cp = c;
   1104 	return (dv);
   1105 }
   1106 
   1107 /*
   1108  * snprintf() `bytes' into `buf', reformatting it so that the number,
   1109  * plus a possible `x' + suffix extension) fits into len bytes (including
   1110  * the terminating NUL).
   1111  * Returns the number of bytes stored in buf, or -1 if there was a problem.
   1112  * E.g, given a len of 9 and a suffix of `B':
   1113  *	bytes		result
   1114  *	-----		------
   1115  *	99999		`99999 B'
   1116  *	100000		`97 KB'
   1117  *	66715648	`65152 KB'
   1118  *	252215296	`240 MB'
   1119  */
   1120 int
   1121 humanize_number(buf, len, bytes, suffix, divisor)
   1122 	char		*buf;
   1123 	size_t		 len;
   1124 	u_int64_t	 bytes;
   1125 	const char	*suffix;
   1126 	int 		divisor;
   1127 {
   1128 		/* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
   1129 	static const char prefixes[] = " KMGTPE";
   1130 
   1131 	int		i, r;
   1132 	u_int64_t	max;
   1133 	size_t		suffixlen;
   1134 
   1135 	if (buf == NULL || suffix == NULL)
   1136 		return (-1);
   1137 	if (len > 0)
   1138 		buf[0] = '\0';
   1139 	suffixlen = strlen(suffix);
   1140 			/* check if enough room for `x y' + suffix + `\0' */
   1141 	if (len < 4 + suffixlen)
   1142 		return (-1);
   1143 
   1144 	max = 1;
   1145 	for (i = 0; i < len - suffixlen - 3; i++)
   1146 		max *= 10;
   1147 	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
   1148 		bytes /= divisor;
   1149 
   1150 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
   1151 	    i == 0 ? "" : " ", prefixes[i], suffix);
   1152 
   1153 	return (r);
   1154 }
   1155 
   1156 int
   1157 format_bytes(buf, len, bytes)
   1158 	char		*buf;
   1159 	size_t		 len;
   1160 	u_int64_t	 bytes;
   1161 {
   1162 	int	rv;
   1163 	size_t	nlen;
   1164 
   1165 	rv = humanize_number(buf, len, bytes, "B", 1024);
   1166 	if (rv != -1) {
   1167 			/* nuke the trailing ` B' if it exists */
   1168 		nlen = strlen(buf) - 2;
   1169 		if (strcmp(&buf[nlen], " B") == 0)
   1170 			buf[nlen] = '\0';
   1171 	}
   1172 	return (rv);
   1173 }
   1174 
   1175 int
   1176 trace_enter(struct proc *p, register_t code, void *args, register_t rval[])
   1177 {
   1178 #ifdef SYSCALL_DEBUG
   1179 	scdebug_call(p, code, args);
   1180 #endif /* SYSCALL_DEBUG */
   1181 
   1182 #ifdef KTRACE
   1183 	if (KTRPOINT(p, KTR_SYSCALL))
   1184 		ktrsyscall(p, code, args);
   1185 #endif /* KTRACE */
   1186 
   1187 #ifdef SYSTRACE
   1188 	if (ISSET(p->p_flag, P_SYSTRACE))
   1189 		return systrace_enter(p, code, args, rval);
   1190 #endif
   1191 	return 0;
   1192 }
   1193 
   1194 void
   1195 trace_exit(struct proc *p, register_t code, void *args, register_t rval[],
   1196     int error)
   1197 {
   1198 #ifdef SYSCALL_DEBUG
   1199 	scdebug_ret(p, code, error, rval);
   1200 #endif /* SYSCALL_DEBUG */
   1201 
   1202 #ifdef KTRACE
   1203 	if (KTRPOINT(p, KTR_SYSRET)) {
   1204 		KERNEL_PROC_LOCK(p);
   1205 		ktrsysret(p, code, error, rval[0]);
   1206 		KERNEL_PROC_UNLOCK(p);
   1207 	}
   1208 #endif /* KTRACE */
   1209 
   1210 #ifdef SYSTRACE
   1211 	if (ISSET(p->p_flag, P_SYSTRACE))
   1212 		systrace_exit(p, code, args, rval, error);
   1213 #endif
   1214 }
   1215