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