Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.66
      1 /*	$NetBSD: kern_subr.c,v 1.66 2000/05/10 02:16:15 enami Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 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 "opt_md.h"
     93 
     94 #include <sys/param.h>
     95 #include <sys/systm.h>
     96 #include <sys/proc.h>
     97 #include <sys/malloc.h>
     98 #include <sys/mount.h>
     99 #include <sys/device.h>
    100 #include <sys/reboot.h>
    101 #include <sys/conf.h>
    102 #include <sys/disklabel.h>
    103 #include <sys/queue.h>
    104 
    105 #include <dev/cons.h>
    106 
    107 #include <net/if.h>
    108 
    109 /* XXX these should eventually move to subr_autoconf.c */
    110 static int findblkmajor __P((const char *));
    111 static const char *findblkname __P((int));
    112 static struct device *finddevice __P((const char *));
    113 static struct device *getdisk __P((char *, int, int, dev_t *, int));
    114 static struct device *parsedisk __P((char *, int, int, dev_t *));
    115 
    116 int
    117 uiomove(buf, n, uio)
    118 	void *buf;
    119 	int n;
    120 	struct uio *uio;
    121 {
    122 	struct iovec *iov;
    123 	u_int cnt;
    124 	int error = 0;
    125 	char *cp = buf;
    126 	struct proc *p = uio->uio_procp;
    127 
    128 #ifdef DIAGNOSTIC
    129 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    130 		panic("uiomove: mode");
    131 	if (uio->uio_segflg == UIO_USERSPACE && p != curproc)
    132 		panic("uiomove proc");
    133 #endif
    134 	while (n > 0 && uio->uio_resid) {
    135 		iov = uio->uio_iov;
    136 		cnt = iov->iov_len;
    137 		if (cnt == 0) {
    138 			uio->uio_iov++;
    139 			uio->uio_iovcnt--;
    140 			continue;
    141 		}
    142 		if (cnt > n)
    143 			cnt = n;
    144 		switch (uio->uio_segflg) {
    145 
    146 		case UIO_USERSPACE:
    147 			if (p->p_schedflags & PSCHED_SHOULDYIELD)
    148 				preempt(NULL);
    149 			if (uio->uio_rw == UIO_READ)
    150 				error = copyout(cp, iov->iov_base, cnt);
    151 			else
    152 				error = copyin(iov->iov_base, cp, cnt);
    153 			if (error)
    154 				return (error);
    155 			break;
    156 
    157 		case UIO_SYSSPACE:
    158 			if (uio->uio_rw == UIO_READ)
    159 				error = kcopy(cp, iov->iov_base, cnt);
    160 			else
    161 				error = kcopy(iov->iov_base, cp, cnt);
    162 			if (error)
    163 				return (error);
    164 			break;
    165 		}
    166 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    167 		iov->iov_len -= cnt;
    168 		uio->uio_resid -= cnt;
    169 		uio->uio_offset += cnt;
    170 		cp += cnt;
    171 		n -= cnt;
    172 	}
    173 	return (error);
    174 }
    175 
    176 /*
    177  * Give next character to user as result of read.
    178  */
    179 int
    180 ureadc(c, uio)
    181 	int c;
    182 	struct uio *uio;
    183 {
    184 	struct iovec *iov;
    185 
    186 	if (uio->uio_resid <= 0)
    187 		panic("ureadc: non-positive resid");
    188 again:
    189 	if (uio->uio_iovcnt <= 0)
    190 		panic("ureadc: non-positive iovcnt");
    191 	iov = uio->uio_iov;
    192 	if (iov->iov_len <= 0) {
    193 		uio->uio_iovcnt--;
    194 		uio->uio_iov++;
    195 		goto again;
    196 	}
    197 	switch (uio->uio_segflg) {
    198 
    199 	case UIO_USERSPACE:
    200 		if (subyte(iov->iov_base, c) < 0)
    201 			return (EFAULT);
    202 		break;
    203 
    204 	case UIO_SYSSPACE:
    205 		*(char *)iov->iov_base = c;
    206 		break;
    207 	}
    208 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    209 	iov->iov_len--;
    210 	uio->uio_resid--;
    211 	uio->uio_offset++;
    212 	return (0);
    213 }
    214 
    215 /*
    216  * General routine to allocate a hash table.
    217  * Allocate enough memory to hold at least `elements' list-head pointers.
    218  * Return a pointer to the allocated space and set *hashmask to a pattern
    219  * suitable for masking a value to use as an index into the returned array.
    220  */
    221 void *
    222 hashinit(elements, type, flags, hashmask)
    223 	int elements, type, flags;
    224 	u_long *hashmask;
    225 {
    226 	long hashsize;
    227 	LIST_HEAD(generic, generic) *hashtbl;
    228 	int i;
    229 
    230 	if (elements <= 0)
    231 		panic("hashinit: bad cnt");
    232 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    233 		continue;
    234 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
    235 	if (hashtbl == NULL)
    236 		return (NULL);
    237 	for (i = 0; i < hashsize; i++)
    238 		LIST_INIT(&hashtbl[i]);
    239 	*hashmask = hashsize - 1;
    240 	return (hashtbl);
    241 }
    242 
    243 /*
    244  * Free memory from hash table previosly allocated via hashinit().
    245  */
    246 void
    247 hashdone(hashtbl, type)
    248 	void *hashtbl;
    249 	int type;
    250 {
    251 
    252 	free(hashtbl, type);
    253 }
    254 
    255 /*
    256  * "Shutdown hook" types, functions, and variables.
    257  */
    258 
    259 struct shutdownhook_desc {
    260 	LIST_ENTRY(shutdownhook_desc) sfd_list;
    261 	void	(*sfd_fn) __P((void *));
    262 	void	*sfd_arg;
    263 };
    264 
    265 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
    266 
    267 void *
    268 shutdownhook_establish(fn, arg)
    269 	void (*fn) __P((void *));
    270 	void *arg;
    271 {
    272 	struct shutdownhook_desc *ndp;
    273 
    274 	ndp = (struct shutdownhook_desc *)
    275 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    276 	if (ndp == NULL)
    277 		return (NULL);
    278 
    279 	ndp->sfd_fn = fn;
    280 	ndp->sfd_arg = arg;
    281 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
    282 
    283 	return (ndp);
    284 }
    285 
    286 void
    287 shutdownhook_disestablish(vhook)
    288 	void *vhook;
    289 {
    290 #ifdef DIAGNOSTIC
    291 	struct shutdownhook_desc *dp;
    292 
    293 	for (dp = shutdownhook_list.lh_first; dp != NULL;
    294 	    dp = dp->sfd_list.le_next)
    295                 if (dp == vhook)
    296 			break;
    297 	if (dp == NULL)
    298 		panic("shutdownhook_disestablish: hook not established");
    299 #endif
    300 
    301 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
    302 	free(vhook, M_DEVBUF);
    303 }
    304 
    305 /*
    306  * Run shutdown hooks.  Should be invoked immediately before the
    307  * system is halted or rebooted, i.e. after file systems unmounted,
    308  * after crash dump done, etc.
    309  *
    310  * Each shutdown hook is removed from the list before it's run, so that
    311  * it won't be run again.
    312  */
    313 void
    314 doshutdownhooks()
    315 {
    316 	struct shutdownhook_desc *dp;
    317 
    318 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    319 		LIST_REMOVE(dp, sfd_list);
    320 		(*dp->sfd_fn)(dp->sfd_arg);
    321 #if 0
    322 		/*
    323 		 * Don't bother freeing the hook structure,, since we may
    324 		 * be rebooting because of a memory corruption problem,
    325 		 * and this might only make things worse.  It doesn't
    326 		 * matter, anyway, since the system is just about to
    327 		 * reboot.
    328 		 */
    329 		free(dp, M_DEVBUF);
    330 #endif
    331 	}
    332 }
    333 
    334 /*
    335  * "Power hook" types, functions, and variables.
    336  */
    337 
    338 struct powerhook_desc {
    339 	LIST_ENTRY(powerhook_desc) sfd_list;
    340 	void	(*sfd_fn) __P((int, void *));
    341 	void	*sfd_arg;
    342 };
    343 
    344 LIST_HEAD(, powerhook_desc) powerhook_list;
    345 
    346 void *
    347 powerhook_establish(fn, arg)
    348 	void (*fn) __P((int, void *));
    349 	void *arg;
    350 {
    351 	struct powerhook_desc *ndp;
    352 
    353 	ndp = (struct powerhook_desc *)
    354 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    355 	if (ndp == NULL)
    356 		return (NULL);
    357 
    358 	ndp->sfd_fn = fn;
    359 	ndp->sfd_arg = arg;
    360 	LIST_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
    361 
    362 	return (ndp);
    363 }
    364 
    365 void
    366 powerhook_disestablish(vhook)
    367 	void *vhook;
    368 {
    369 #ifdef DIAGNOSTIC
    370 	struct powerhook_desc *dp;
    371 
    372 	for (dp = powerhook_list.lh_first; dp != NULL;
    373 	    dp = dp->sfd_list.le_next)
    374                 if (dp == vhook)
    375 			break;
    376 	if (dp == NULL)
    377 		panic("powerhook_disestablish: hook not established");
    378 #endif
    379 
    380 	LIST_REMOVE((struct powerhook_desc *)vhook, sfd_list);
    381 	free(vhook, M_DEVBUF);
    382 }
    383 
    384 /*
    385  * Run power hooks.
    386  */
    387 void
    388 dopowerhooks(why)
    389 	int why;
    390 {
    391 	struct powerhook_desc *dp;
    392 
    393 	for (dp = LIST_FIRST(&powerhook_list);
    394 	     dp != NULL;
    395 	     dp = LIST_NEXT(dp, sfd_list)) {
    396 		(*dp->sfd_fn)(why, dp->sfd_arg);
    397 	}
    398 }
    399 
    400 /*
    401  * "Mountroot hook" types, functions, and variables.
    402  */
    403 
    404 struct mountroothook_desc {
    405 	LIST_ENTRY(mountroothook_desc) mrd_list;
    406 	struct	device *mrd_device;
    407 	void 	(*mrd_func) __P((struct device *));
    408 };
    409 
    410 LIST_HEAD(, mountroothook_desc) mountroothook_list;
    411 
    412 void *
    413 mountroothook_establish(func, dev)
    414 	void (*func) __P((struct device *));
    415 	struct device *dev;
    416 {
    417 	struct mountroothook_desc *mrd;
    418 
    419 	mrd = (struct mountroothook_desc *)
    420 	    malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
    421 	if (mrd == NULL)
    422 		return (NULL);
    423 
    424 	mrd->mrd_device = dev;
    425 	mrd->mrd_func = func;
    426 	LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
    427 
    428 	return (mrd);
    429 }
    430 
    431 void
    432 mountroothook_disestablish(vhook)
    433 	void *vhook;
    434 {
    435 #ifdef DIAGNOSTIC
    436 	struct mountroothook_desc *mrd;
    437 
    438 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    439 	    mrd = mrd->mrd_list.le_next)
    440                 if (mrd == vhook)
    441 			break;
    442 	if (mrd == NULL)
    443 		panic("mountroothook_disestablish: hook not established");
    444 #endif
    445 
    446 	LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
    447 	free(vhook, M_DEVBUF);
    448 }
    449 
    450 void
    451 mountroothook_destroy()
    452 {
    453 	struct mountroothook_desc *mrd;
    454 
    455 	while ((mrd = mountroothook_list.lh_first) != NULL) {
    456 		LIST_REMOVE(mrd, mrd_list);
    457 		free(mrd, M_DEVBUF);
    458 	}
    459 }
    460 
    461 void
    462 domountroothook()
    463 {
    464 	struct mountroothook_desc *mrd;
    465 
    466 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    467 	    mrd = mrd->mrd_list.le_next) {
    468 		if (mrd->mrd_device == root_device) {
    469 			(*mrd->mrd_func)(root_device);
    470 			return;
    471 		}
    472 	}
    473 }
    474 
    475 /*
    476  * Exec hook code.
    477  */
    478 
    479 struct exechook_desc {
    480 	LIST_ENTRY(exechook_desc) ehk_list;
    481 	void	(*ehk_fn) __P((struct proc *, void *));
    482 	void	*ehk_arg;
    483 };
    484 
    485 LIST_HEAD(, exechook_desc) exechook_list;
    486 
    487 void *
    488 exechook_establish(fn, arg)
    489 	void (*fn) __P((struct proc *, void *));
    490 	void *arg;
    491 {
    492 	struct exechook_desc *edp;
    493 
    494 	edp = (struct exechook_desc *)
    495 	    malloc(sizeof(*edp), M_DEVBUF, M_NOWAIT);
    496 	if (edp == NULL)
    497 		return (NULL);
    498 
    499 	edp->ehk_fn = fn;
    500 	edp->ehk_arg = arg;
    501 	LIST_INSERT_HEAD(&exechook_list, edp, ehk_list);
    502 
    503 	return (edp);
    504 }
    505 
    506 void
    507 exechook_disestablish(vhook)
    508 	void *vhook;
    509 {
    510 #ifdef DIAGNOSTIC
    511 	struct exechook_desc *edp;
    512 
    513 	for (edp = exechook_list.lh_first; edp != NULL;
    514 	    edp = edp->ehk_list.le_next)
    515                 if (edp == vhook)
    516 			break;
    517 	if (edp == NULL)
    518 		panic("exechook_disestablish: hook not established");
    519 #endif
    520 
    521 	LIST_REMOVE((struct exechook_desc *)vhook, ehk_list);
    522 	free(vhook, M_DEVBUF);
    523 }
    524 
    525 /*
    526  * Run exec hooks.
    527  */
    528 void
    529 doexechooks(p)
    530 	struct proc *p;
    531 {
    532 	struct exechook_desc *edp;
    533 
    534 	for (edp = LIST_FIRST(&exechook_list);
    535 	     edp != NULL;
    536 	     edp = LIST_NEXT(edp, ehk_list)) {
    537 		(*edp->ehk_fn)(p, edp->ehk_arg);
    538 	}
    539 }
    540 
    541 /*
    542  * Determine the root device and, if instructed to, the root file system.
    543  */
    544 
    545 #include "md.h"
    546 #if NMD == 0
    547 #undef MEMORY_DISK_HOOKS
    548 #endif
    549 
    550 #ifdef MEMORY_DISK_HOOKS
    551 static struct device fakemdrootdev[NMD];
    552 #endif
    553 
    554 #include "raid.h"
    555 #if NRAID == 1
    556 #define BOOT_FROM_RAID_HOOKS 1
    557 #endif
    558 
    559 #ifdef BOOT_FROM_RAID_HOOKS
    560 extern int numraid;
    561 extern struct device *raidrootdev;
    562 #endif
    563 
    564 void
    565 setroot(bootdv, bootpartition)
    566 	struct device *bootdv;
    567 	int bootpartition;
    568 {
    569 	struct device *dv;
    570 	int len;
    571 #ifdef MEMORY_DISK_HOOKS
    572 	int i;
    573 #endif
    574 	dev_t nrootdev;
    575 	dev_t ndumpdev = NODEV;
    576 	char buf[128];
    577 	const char *rootdevname;
    578 	const char *dumpdevname;
    579 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    580 	struct device *dumpdv = NULL;
    581 	struct ifnet *ifp;
    582 	const char *deffsname;
    583 	struct vfsops *vops;
    584 	extern int (*mountroot) __P((void));
    585 
    586 #ifdef MEMORY_DISK_HOOKS
    587 	for (i = 0; i < NMD; i++) {
    588 		fakemdrootdev[i].dv_class  = DV_DISK;
    589 		fakemdrootdev[i].dv_cfdata = NULL;
    590 		fakemdrootdev[i].dv_unit   = i;
    591 		fakemdrootdev[i].dv_parent = NULL;
    592 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    593 	}
    594 #endif /* MEMORY_DISK_HOOKS */
    595 
    596 #ifdef MEMORY_DISK_IS_ROOT
    597 	bootdv = &fakemdrootdev[0];
    598 	bootpartition = 0;
    599 #endif
    600 
    601 	/*
    602 	 * If NFS is specified as the file system, and we found
    603 	 * a DV_DISK boot device (or no boot device at all), then
    604 	 * find a reasonable network interface for "rootspec".
    605 	 */
    606 	vops = vfs_getopsbyname("nfs");
    607 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    608 	    rootspec == NULL &&
    609 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    610 		for (ifp = ifnet.tqh_first; ifp != NULL;
    611 		    ifp = ifp->if_list.tqe_next)
    612 			if ((ifp->if_flags &
    613 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    614 				break;
    615 		if (ifp == NULL) {
    616 			/*
    617 			 * Can't find a suitable interface; ask the
    618 			 * user.
    619 			 */
    620 			boothowto |= RB_ASKNAME;
    621 		} else {
    622 			/*
    623 			 * Have a suitable interface; behave as if
    624 			 * the user specified this interface.
    625 			 */
    626 			rootspec = (const char *)ifp->if_xname;
    627 		}
    628 	}
    629 
    630 	/*
    631 	 * If wildcarded root and we the boot device wasn't determined,
    632 	 * ask the user.
    633 	 */
    634 	if (rootspec == NULL && bootdv == NULL)
    635 		boothowto |= RB_ASKNAME;
    636 
    637  top:
    638 	if (boothowto & RB_ASKNAME) {
    639 		struct device *defdumpdv;
    640 
    641 		for (;;) {
    642 			printf("root device");
    643 			if (bootdv != NULL) {
    644 				printf(" (default %s", bootdv->dv_xname);
    645 				if (bootdv->dv_class == DV_DISK)
    646 					printf("%c", bootpartition + 'a');
    647 				printf(")");
    648 			}
    649 			printf(": ");
    650 			len = cngetsn(buf, sizeof(buf));
    651 			if (len == 0 && bootdv != NULL) {
    652 				strcpy(buf, bootdv->dv_xname);
    653 				len = strlen(buf);
    654 			}
    655 			if (len > 0 && buf[len - 1] == '*') {
    656 				buf[--len] = '\0';
    657 				dv = getdisk(buf, len, 1, &nrootdev, 0);
    658 				if (dv != NULL) {
    659 					rootdv = dv;
    660 					break;
    661 				}
    662 			}
    663 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    664 			if (dv != NULL) {
    665 				rootdv = dv;
    666 				break;
    667 			}
    668 		}
    669 
    670 		/*
    671 		 * Set up the default dump device.  If root is on
    672 		 * a network device, there is no default dump
    673 		 * device, since we don't support dumps to the
    674 		 * network.
    675 		 */
    676 		if (rootdv->dv_class == DV_IFNET)
    677 			defdumpdv = NULL;
    678 		else
    679 			defdumpdv = rootdv;
    680 
    681 		for (;;) {
    682 			printf("dump device");
    683 			if (defdumpdv != NULL) {
    684 				/*
    685 				 * Note, we know it's a disk if we get here.
    686 				 */
    687 				printf(" (default %sb)", defdumpdv->dv_xname);
    688 			}
    689 			printf(": ");
    690 			len = cngetsn(buf, sizeof(buf));
    691 			if (len == 0) {
    692 				if (defdumpdv != NULL) {
    693 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    694 					    DISKUNIT(nrootdev), 1);
    695 				}
    696 				dumpdv = defdumpdv;
    697 				break;
    698 			}
    699 			if (len == 4 && strcmp(buf, "none") == 0) {
    700 				dumpdv = NULL;
    701 				break;
    702 			}
    703 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
    704 			if (dv != NULL) {
    705 				dumpdv = dv;
    706 				break;
    707 			}
    708 		}
    709 
    710 		rootdev = nrootdev;
    711 		dumpdev = ndumpdev;
    712 
    713 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    714 		     vops = LIST_NEXT(vops, vfs_list)) {
    715 			if (vops->vfs_mountroot != NULL &&
    716 			    vops->vfs_mountroot == mountroot)
    717 			break;
    718 		}
    719 
    720 		if (vops == NULL) {
    721 			mountroot = NULL;
    722 			deffsname = "generic";
    723 		} else
    724 			deffsname = vops->vfs_name;
    725 
    726 		for (;;) {
    727 			printf("file system (default %s): ", deffsname);
    728 			len = cngetsn(buf, sizeof(buf));
    729 			if (len == 0)
    730 				break;
    731 			if (len == 4 && strcmp(buf, "halt") == 0)
    732 				cpu_reboot(RB_HALT, NULL);
    733 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    734 				mountroot = NULL;
    735 				break;
    736 			}
    737 			vops = vfs_getopsbyname(buf);
    738 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    739 				printf("use one of: generic");
    740 				for (vops = LIST_FIRST(&vfs_list);
    741 				     vops != NULL;
    742 				     vops = LIST_NEXT(vops, vfs_list)) {
    743 					if (vops->vfs_mountroot != NULL)
    744 						printf(" %s", vops->vfs_name);
    745 				}
    746 				printf(" halt\n");
    747 			} else {
    748 				mountroot = vops->vfs_mountroot;
    749 				break;
    750 			}
    751 		}
    752 
    753 	} else if (rootspec == NULL) {
    754 		int majdev;
    755 
    756 		/*
    757 		 * Wildcarded root; use the boot device.
    758 		 */
    759 		rootdv = bootdv;
    760 
    761 		majdev = findblkmajor(bootdv->dv_xname);
    762 		if (majdev >= 0) {
    763 			/*
    764 			 * Root is on a disk.  `bootpartition' is root.
    765 			 */
    766 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    767 			    bootpartition);
    768 		}
    769 	} else {
    770 
    771 		/*
    772 		 * `root on <dev> ...'
    773 		 */
    774 
    775 		/*
    776 		 * If it's a network interface, we can bail out
    777 		 * early.
    778 		 */
    779 		dv = finddevice(rootspec);
    780 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    781 			rootdv = dv;
    782 			goto haveroot;
    783 		}
    784 
    785 		rootdevname = findblkname(major(rootdev));
    786 		if (rootdevname == NULL) {
    787 			printf("unknown device major 0x%x\n", rootdev);
    788 			boothowto |= RB_ASKNAME;
    789 			goto top;
    790 		}
    791 		memset(buf, 0, sizeof(buf));
    792 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    793 
    794 		rootdv = finddevice(buf);
    795 		if (rootdv == NULL) {
    796 			printf("device %s (0x%x) not configured\n",
    797 			    buf, rootdev);
    798 			boothowto |= RB_ASKNAME;
    799 			goto top;
    800 		}
    801 	}
    802 
    803  haveroot:
    804 
    805 	root_device = rootdv;
    806 
    807 	switch (rootdv->dv_class) {
    808 	case DV_IFNET:
    809 		printf("root on %s", rootdv->dv_xname);
    810 		break;
    811 
    812 	case DV_DISK:
    813 		printf("root on %s%c", rootdv->dv_xname,
    814 		    DISKPART(rootdev) + 'a');
    815 		break;
    816 
    817 	default:
    818 		printf("can't determine root device\n");
    819 		boothowto |= RB_ASKNAME;
    820 		goto top;
    821 	}
    822 
    823 	/*
    824 	 * Now configure the dump device.
    825 	 *
    826 	 * If we haven't figured out the dump device, do so, with
    827 	 * the following rules:
    828 	 *
    829 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    830 	 *
    831 	 *	(b) If dumpspec is set, try to use it.  If the device
    832 	 *	    is not available, punt.
    833 	 *
    834 	 *	(c) If dumpspec is not set, the dump device is
    835 	 *	    wildcarded or unspecified.  If the root device
    836 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    837 	 *	    of the root device.
    838 	 */
    839 
    840 	if (boothowto & RB_ASKNAME) {		/* (a) */
    841 		if (dumpdv == NULL)
    842 			goto nodumpdev;
    843 	} else if (dumpspec != NULL) {		/* (b) */
    844 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    845 			/*
    846 			 * Operator doesn't want a dump device.
    847 			 * Or looks like they tried to pick a network
    848 			 * device.  Oops.
    849 			 */
    850 			goto nodumpdev;
    851 		}
    852 
    853 		dumpdevname = findblkname(major(dumpdev));
    854 		if (dumpdevname == NULL)
    855 			goto nodumpdev;
    856 		memset(buf, 0, sizeof(buf));
    857 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
    858 
    859 		dumpdv = finddevice(buf);
    860 		if (dumpdv == NULL) {
    861 			/*
    862 			 * Device not configured.
    863 			 */
    864 			goto nodumpdev;
    865 		}
    866 	} else {				/* (c) */
    867 		if (rootdv->dv_class == DV_IFNET)
    868 			goto nodumpdev;
    869 		else {
    870 			dumpdv = rootdv;
    871 			dumpdev = MAKEDISKDEV(major(rootdev),
    872 			    dumpdv->dv_unit, 1);
    873 		}
    874 	}
    875 
    876 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
    877 	return;
    878 
    879  nodumpdev:
    880 	dumpdev = NODEV;
    881 	printf("\n");
    882 }
    883 
    884 static int
    885 findblkmajor(name)
    886 	const char *name;
    887 {
    888 	int i;
    889 
    890 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
    891 		if (strncmp(name, dev_name2blk[i].d_name,
    892 		    strlen(dev_name2blk[i].d_name)) == 0)
    893 			return (dev_name2blk[i].d_maj);
    894 	return (-1);
    895 }
    896 
    897 const char *
    898 findblkname(maj)
    899 	int maj;
    900 {
    901 	int i;
    902 
    903 	for (i = 0; dev_name2blk[i].d_name != NULL; i++)
    904 		if (dev_name2blk[i].d_maj == maj)
    905 			return (dev_name2blk[i].d_name);
    906 	return (NULL);
    907 }
    908 
    909 static struct device *
    910 finddevice(name)
    911 	const char *name;
    912 {
    913 	struct device *dv;
    914 #ifdef BOOT_FROM_RAID_HOOKS
    915 	int j;
    916 
    917 	for (j = 0; j < numraid; j++) {
    918 		if (strcmp(name, raidrootdev[j].dv_xname) == 0) {
    919 			dv = &raidrootdev[j];
    920 			return (dv);
    921 		}
    922 	}
    923 #endif;
    924 
    925 	for (dv = TAILQ_FIRST(&alldevs); dv != NULL;
    926 	    dv = TAILQ_NEXT(dv, dv_list))
    927 		if (strcmp(dv->dv_xname, name) == 0)
    928 			break;
    929 	return (dv);
    930 }
    931 
    932 static struct device *
    933 getdisk(str, len, defpart, devp, isdump)
    934 	char *str;
    935 	int len, defpart;
    936 	dev_t *devp;
    937 	int isdump;
    938 {
    939 	struct device	*dv;
    940 #ifdef MEMORY_DISK_HOOKS
    941 	int		i;
    942 #endif
    943 #ifdef BOOT_FROM_RAID_HOOKS
    944 	int 		j;
    945 #endif
    946 
    947 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
    948 		printf("use one of:");
    949 #ifdef MEMORY_DISK_HOOKS
    950 		if (isdump == 0)
    951 			for (i = 0; i < NMD; i++)
    952 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
    953 				    'a' + MAXPARTITIONS - 1);
    954 #endif
    955 #ifdef BOOT_FROM_RAID_HOOKS
    956 		if (isdump == 0)
    957 			for (j = 0; j < numraid; j++)
    958 				printf(" %s[a-%c]", raidrootdev[j].dv_xname,
    959 				    'a' + MAXPARTITIONS - 1);
    960 #endif
    961 		for (dv = alldevs.tqh_first; dv != NULL;
    962 		    dv = dv->dv_list.tqe_next) {
    963 			if (dv->dv_class == DV_DISK)
    964 				printf(" %s[a-%c]", dv->dv_xname,
    965 				    'a' + MAXPARTITIONS - 1);
    966 			if (isdump == 0 && dv->dv_class == DV_IFNET)
    967 				printf(" %s", dv->dv_xname);
    968 		}
    969 		if (isdump)
    970 			printf(" none");
    971 		printf(" halt\n");
    972 	}
    973 	return (dv);
    974 }
    975 
    976 static struct device *
    977 parsedisk(str, len, defpart, devp)
    978 	char *str;
    979 	int len, defpart;
    980 	dev_t *devp;
    981 {
    982 	struct device *dv;
    983 	char *cp, c;
    984 	int majdev, part;
    985 #ifdef MEMORY_DISK_HOOKS
    986 	int i;
    987 #endif
    988 	if (len == 0)
    989 		return (NULL);
    990 
    991 	if (len == 4 && strcmp(str, "halt") == 0)
    992 		cpu_reboot(RB_HALT, NULL);
    993 
    994 	cp = str + len - 1;
    995 	c = *cp;
    996 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    997 		part = c - 'a';
    998 		*cp = '\0';
    999 	} else
   1000 		part = defpart;
   1001 
   1002 #ifdef MEMORY_DISK_HOOKS
   1003 	for (i = 0; i < NMD; i++)
   1004 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
   1005 			dv = &fakemdrootdev[i];
   1006 			goto gotdisk;
   1007 		}
   1008 #endif
   1009 
   1010 	dv = finddevice(str);
   1011 	if (dv != NULL) {
   1012 		if (dv->dv_class == DV_DISK) {
   1013 #ifdef MEMORY_DISK_HOOKS
   1014  gotdisk:
   1015 #endif
   1016 			majdev = findblkmajor(dv->dv_xname);
   1017 			if (majdev < 0)
   1018 				panic("parsedisk");
   1019 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
   1020 		}
   1021 
   1022 		if (dv->dv_class == DV_IFNET)
   1023 			*devp = NODEV;
   1024 	}
   1025 
   1026 	*cp = c;
   1027 	return (dv);
   1028 }
   1029 
   1030 /*
   1031  * snprintf() `bytes' into `buf', reformatting it so that the number,
   1032  * plus a possible `x' + suffix extension) fits into len bytes (including
   1033  * the terminating NUL).
   1034  * Returns the number of bytes stored in buf, or -1 if there was a problem.
   1035  * E.g, given a len of 9 and a suffix of `B':
   1036  *	bytes		result
   1037  *	-----		------
   1038  *	99999		`99999 B'
   1039  *	100000		`97 KB'
   1040  *	66715648	`65152 KB'
   1041  *	252215296	`240 MB'
   1042  */
   1043 int
   1044 humanize_number(buf, len, bytes, suffix, divisor)
   1045 	char		*buf;
   1046 	size_t		 len;
   1047 	u_int64_t	 bytes;
   1048 	const char	*suffix;
   1049 	int 		divisor;
   1050 {
   1051 		/* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
   1052 	static const char prefixes[] = " KMGTPE";
   1053 
   1054 	int		i, r;
   1055 	u_int64_t	max;
   1056 	size_t		suffixlen;
   1057 
   1058 	if (buf == NULL || suffix == NULL)
   1059 		return (-1);
   1060 	if (len > 0)
   1061 		buf[0] = '\0';
   1062 	suffixlen = strlen(suffix);
   1063 			/* check if enough room for `x y' + suffix + `\0' */
   1064 	if (len < 4 + suffixlen)
   1065 		return (-1);
   1066 
   1067 	max = 1;
   1068 	for (i = 0; i < len - suffixlen - 3; i++)
   1069 		max *= 10;
   1070 	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
   1071 		bytes /= divisor;
   1072 
   1073 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
   1074 	    i == 0 ? "" : " ", prefixes[i], suffix);
   1075 
   1076 	return (r);
   1077 }
   1078 
   1079 int
   1080 format_bytes(buf, len, bytes)
   1081 	char		*buf;
   1082 	size_t		 len;
   1083 	u_int64_t	 bytes;
   1084 {
   1085 	int	rv;
   1086 	size_t	nlen;
   1087 
   1088 	rv = humanize_number(buf, len, bytes, "B", 1024);
   1089 	if (rv != -1) {
   1090 			/* nuke the trailing ` B' if it exists */
   1091 		nlen = strlen(buf) - 2;
   1092 		if (strcmp(&buf[nlen], " B") == 0)
   1093 			buf[nlen] = '\0';
   1094 	}
   1095 	return (rv);
   1096 }
   1097