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