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