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