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