kern_subr.c revision 1.51 1 /* $NetBSD: kern_subr.c,v 1.51 1999/06/07 20:16:09 thorpej 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 * "Mountroot hook" types, functions, and variables.
319 */
320
321 struct mountroothook_desc {
322 LIST_ENTRY(mountroothook_desc) mrd_list;
323 struct device *mrd_device;
324 void (*mrd_func) __P((struct device *));
325 };
326
327 LIST_HEAD(, mountroothook_desc) mountroothook_list;
328
329 void *
330 mountroothook_establish(func, dev)
331 void (*func) __P((struct device *));
332 struct device *dev;
333 {
334 struct mountroothook_desc *mrd;
335
336 mrd = (struct mountroothook_desc *)
337 malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
338 if (mrd == NULL)
339 return (NULL);
340
341 mrd->mrd_device = dev;
342 mrd->mrd_func = func;
343 LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
344
345 return (mrd);
346 }
347
348 void
349 mountroothook_disestablish(vhook)
350 void *vhook;
351 {
352 #ifdef DIAGNOSTIC
353 struct mountroothook_desc *mrd;
354
355 for (mrd = mountroothook_list.lh_first; mrd != NULL;
356 mrd = mrd->mrd_list.le_next)
357 if (mrd == vhook)
358 break;
359 if (mrd == NULL)
360 panic("mountroothook_disestablish: hook not established");
361 #endif
362
363 LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
364 free(vhook, M_DEVBUF);
365 }
366
367 void
368 mountroothook_destroy()
369 {
370 struct mountroothook_desc *mrd;
371
372 while ((mrd = mountroothook_list.lh_first) != NULL) {
373 LIST_REMOVE(mrd, mrd_list);
374 free(mrd, M_DEVBUF);
375 }
376 }
377
378 void
379 domountroothook()
380 {
381 struct mountroothook_desc *mrd;
382
383 for (mrd = mountroothook_list.lh_first; mrd != NULL;
384 mrd = mrd->mrd_list.le_next) {
385 if (mrd->mrd_device == root_device) {
386 (*mrd->mrd_func)(root_device);
387 return;
388 }
389 }
390 }
391
392 /*
393 * Determine the root device and, if instructed to, the root file system.
394 */
395
396 #include "md.h"
397 #if NMD == 0
398 #undef MEMORY_DISK_HOOKS
399 #endif
400
401 #ifdef MEMORY_DISK_HOOKS
402 static struct device fakemdrootdev[NMD];
403 #endif
404
405 void
406 setroot(bootdv, bootpartition)
407 struct device *bootdv;
408 int bootpartition;
409 {
410 struct device *dv;
411 int len, print_newline = 0;
412 #ifdef MEMORY_DISK_HOOKS
413 int i;
414 #endif
415 dev_t nrootdev;
416 dev_t ndumpdev = NODEV;
417 char buf[128];
418 const char *rootdevname;
419 const char *dumpdevname;
420 struct device *rootdv = NULL; /* XXX gcc -Wuninitialized */
421 struct device *dumpdv = NULL;
422 struct ifnet *ifp;
423 const char *deffsname;
424 struct vfsops *vops;
425 extern int (*mountroot) __P((void));
426
427 #ifdef MEMORY_DISK_HOOKS
428 for (i = 0; i < NMD; i++) {
429 fakemdrootdev[i].dv_class = DV_DISK;
430 fakemdrootdev[i].dv_cfdata = NULL;
431 fakemdrootdev[i].dv_unit = i;
432 fakemdrootdev[i].dv_parent = NULL;
433 sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
434 }
435 #endif /* MEMORY_DISK_HOOKS */
436
437 #ifdef MEMORY_DISK_IS_ROOT
438 bootdv = &fakemdrootdev[0];
439 bootpartition = 0;
440 #endif
441
442 /*
443 * If NFS is specified as the file system, and we found
444 * a DV_DISK boot device (or no boot device at all), then
445 * find a reasonable network interface for "rootspec".
446 */
447 vops = vfs_getopsbyname("nfs");
448 if (vops != NULL && vops->vfs_mountroot == mountroot &&
449 rootspec == NULL &&
450 (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
451 for (ifp = ifnet.tqh_first; ifp != NULL;
452 ifp = ifp->if_list.tqe_next)
453 if ((ifp->if_flags &
454 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
455 break;
456 if (ifp == NULL) {
457 /*
458 * Can't find a suitable interface; ask the
459 * user.
460 */
461 boothowto |= RB_ASKNAME;
462 } else {
463 /*
464 * Have a suitable interface; behave as if
465 * the user specified this interface.
466 */
467 rootspec = (const char *)ifp->if_xname;
468 }
469 }
470
471 /*
472 * If wildcarded root and we the boot device wasn't determined,
473 * ask the user.
474 */
475 if (rootspec == NULL && bootdv == NULL)
476 boothowto |= RB_ASKNAME;
477
478 top:
479 if (boothowto & RB_ASKNAME) {
480 struct device *defdumpdv;
481
482 for (;;) {
483 printf("root device");
484 if (bootdv != NULL) {
485 printf(" (default %s", bootdv->dv_xname);
486 if (bootdv->dv_class == DV_DISK)
487 printf("%c", bootpartition + 'a');
488 printf(")");
489 }
490 printf(": ");
491 len = getstr(buf, sizeof(buf));
492 if (len == 0 && bootdv != NULL) {
493 strcpy(buf, bootdv->dv_xname);
494 len = strlen(buf);
495 }
496 if (len > 0 && buf[len - 1] == '*') {
497 buf[--len] = '\0';
498 dv = getdisk(buf, len, 1, &nrootdev, 0);
499 if (dv != NULL) {
500 rootdv = dv;
501 break;
502 }
503 }
504 dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
505 if (dv != NULL) {
506 rootdv = dv;
507 break;
508 }
509 }
510
511 /*
512 * Set up the default dump device. If root is on
513 * a network device, there is no default dump
514 * device, since we don't support dumps to the
515 * network.
516 */
517 if (rootdv->dv_class == DV_IFNET)
518 defdumpdv = NULL;
519 else
520 defdumpdv = rootdv;
521
522 for (;;) {
523 printf("dump device");
524 if (defdumpdv != NULL) {
525 /*
526 * Note, we know it's a disk if we get here.
527 */
528 printf(" (default %sb)", defdumpdv->dv_xname);
529 }
530 printf(": ");
531 len = getstr(buf, sizeof(buf));
532 if (len == 0) {
533 if (defdumpdv != NULL) {
534 ndumpdev = MAKEDISKDEV(major(nrootdev),
535 DISKUNIT(nrootdev), 1);
536 }
537 if (rootdv->dv_class == DV_IFNET)
538 dumpdv = NULL;
539 else
540 dumpdv = rootdv;
541 break;
542 }
543 if (len == 4 && strcmp(buf, "none") == 0) {
544 dumpspec = "none";
545 goto havedump;
546 }
547 dv = getdisk(buf, len, 1, &ndumpdev, 1);
548 if (dv) {
549 dumpdv = dv;
550 break;
551 }
552 }
553
554 havedump:
555 rootdev = nrootdev;
556 dumpdev = ndumpdev;
557
558 for (vops = LIST_FIRST(&vfs_list); vops != NULL;
559 vops = LIST_NEXT(vops, vfs_list)) {
560 if (vops->vfs_mountroot != NULL &&
561 vops->vfs_mountroot == mountroot)
562 break;
563 }
564
565 if (vops == NULL) {
566 mountroot = NULL;
567 deffsname = "generic";
568 } else
569 deffsname = vops->vfs_name;
570
571 for (;;) {
572 printf("file system (default %s): ", deffsname);
573 len = getstr(buf, sizeof(buf));
574 if (len == 0)
575 break;
576 if (len == 4 && strcmp(buf, "halt") == 0)
577 cpu_reboot(RB_HALT, NULL);
578 else if (len == 7 && strcmp(buf, "generic") == 0) {
579 mountroot = NULL;
580 break;
581 }
582 vops = vfs_getopsbyname(buf);
583 if (vops == NULL || vops->vfs_mountroot == NULL) {
584 printf("use one of: generic");
585 for (vops = LIST_FIRST(&vfs_list);
586 vops != NULL;
587 vops = LIST_NEXT(vops, vfs_list)) {
588 if (vops->vfs_mountroot != NULL)
589 printf(" %s", vops->vfs_name);
590 }
591 printf(" halt\n");
592 } else {
593 mountroot = vops->vfs_mountroot;
594 break;
595 }
596 }
597
598 } else if (rootspec == NULL) {
599 int majdev;
600
601 /*
602 * Wildcarded root; use the boot device.
603 */
604 rootdv = bootdv;
605
606 majdev = findblkmajor(bootdv->dv_xname);
607 if (majdev >= 0) {
608 /*
609 * Root is on a disk. `bootpartition' is root.
610 */
611 rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
612 bootpartition);
613 }
614 } else {
615
616 /*
617 * `root on <dev> ...'
618 */
619
620 /*
621 * If it's a network interface, we can bail out
622 * early.
623 */
624 for (dv = alldevs.tqh_first; dv != NULL;
625 dv = dv->dv_list.tqe_next)
626 if (strcmp(dv->dv_xname, rootspec) == 0)
627 break;
628 if (dv != NULL && dv->dv_class == DV_IFNET) {
629 rootdv = dv;
630 goto haveroot;
631 }
632
633 rootdevname = findblkname(major(rootdev));
634 if (rootdevname == NULL) {
635 printf("unknown device major 0x%x\n", rootdev);
636 boothowto |= RB_ASKNAME;
637 goto top;
638 }
639 memset(buf, 0, sizeof(buf));
640 sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
641
642 for (dv = alldevs.tqh_first; dv != NULL;
643 dv = dv->dv_list.tqe_next) {
644 if (strcmp(buf, dv->dv_xname) == 0) {
645 rootdv = dv;
646 break;
647 }
648 }
649 if (rootdv == NULL) {
650 printf("device %s (0x%x) not configured\n",
651 buf, rootdev);
652 boothowto |= RB_ASKNAME;
653 goto top;
654 }
655 }
656
657 haveroot:
658
659 root_device = rootdv;
660
661 switch (rootdv->dv_class) {
662 case DV_IFNET:
663 /* Nothing. */
664 break;
665
666 case DV_DISK:
667 printf("root on %s%c", rootdv->dv_xname,
668 DISKPART(rootdev) + 'a');
669 print_newline = 1;
670 break;
671
672 default:
673 printf("can't determine root device\n");
674 boothowto |= RB_ASKNAME;
675 goto top;
676 }
677
678 /*
679 * Now configure the dump device.
680 */
681
682 if (dumpspec != NULL && strcmp(dumpspec, "none") == 0) {
683 /*
684 * Operator doesn't want a dump device.
685 */
686 goto nodumpdev;
687 }
688
689 /*
690 * If we haven't figured out the dump device, do so, with
691 * the following rules:
692 *
693 * (a) We already know dumpdv in the RB_ASKNAME case.
694 *
695 * (b) If dumpspec is set, try to use it. If the device
696 * is not available, punt.
697 *
698 * (c) If dumpspec is not set, the dump device is
699 * wildcarded or unspecified. If the root device
700 * is DV_IFNET, punt. Otherwise, use partition b
701 * of the root device.
702 */
703
704 if (boothowto & RB_ASKNAME) {
705 if (dumpdv == NULL) {
706 /*
707 * Just return; dumpdev is already set to NODEV
708 * and we don't want to print a newline in this
709 * case.
710 */
711 return;
712 }
713 goto out;
714 }
715
716 if (dumpspec != NULL) {
717 if (dumpdev == NODEV) {
718 /*
719 * Looks like they tried to pick a network
720 * device. Oops.
721 */
722 goto nodumpdev;
723 }
724
725 dumpdevname = findblkname(major(dumpdev));
726 if (dumpdevname == NULL)
727 goto nodumpdev;
728 memset(buf, 0, sizeof(buf));
729 sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
730
731 for (dv = alldevs.tqh_first; dv != NULL;
732 dv = dv->dv_list.tqe_next) {
733 if (strcmp(buf, dv->dv_xname) == 0) {
734 dumpdv = dv;
735 break;
736 }
737 }
738 if (dv == NULL) {
739 /*
740 * Device not configured.
741 */
742 goto nodumpdev;
743 }
744 } else if (rootdv->dv_class == DV_IFNET)
745 goto nodumpdev;
746 else {
747 dumpdv = rootdv;
748 dumpdev = MAKEDISKDEV(major(rootdev), dumpdv->dv_unit, 1);
749 }
750
751 out:
752 printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
753 return;
754
755 nodumpdev:
756 dumpdev = NODEV;
757 if (print_newline)
758 printf("\n");
759 }
760
761 static int
762 findblkmajor(name)
763 const char *name;
764 {
765 int i;
766
767 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
768 if (strncmp(name, dev_name2blk[i].d_name,
769 strlen(dev_name2blk[i].d_name)) == 0)
770 return (dev_name2blk[i].d_maj);
771 return (-1);
772 }
773
774 const char *
775 findblkname(maj)
776 int maj;
777 {
778 int i;
779
780 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
781 if (dev_name2blk[i].d_maj == maj)
782 return (dev_name2blk[i].d_name);
783 return (NULL);
784 }
785
786 static struct device *
787 getdisk(str, len, defpart, devp, isdump)
788 char *str;
789 int len, defpart;
790 dev_t *devp;
791 int isdump;
792 {
793 struct device *dv;
794 #ifdef MEMORY_DISK_HOOKS
795 int i;
796 #endif
797
798 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
799 printf("use one of:");
800 #ifdef MEMORY_DISK_HOOKS
801 if (isdump == 0)
802 for (i = 0; i < NMD; i++)
803 printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
804 'a' + MAXPARTITIONS - 1);
805 #endif
806 for (dv = alldevs.tqh_first; dv != NULL;
807 dv = dv->dv_list.tqe_next) {
808 if (dv->dv_class == DV_DISK)
809 printf(" %s[a-%c]", dv->dv_xname,
810 'a' + MAXPARTITIONS - 1);
811 if (isdump == 0 && dv->dv_class == DV_IFNET)
812 printf(" %s", dv->dv_xname);
813 }
814 if (isdump)
815 printf(" none");
816 printf(" halt\n");
817 }
818 return (dv);
819 }
820
821 static struct device *
822 parsedisk(str, len, defpart, devp)
823 char *str;
824 int len, defpart;
825 dev_t *devp;
826 {
827 struct device *dv;
828 char *cp, c;
829 int majdev, part;
830 #ifdef MEMORY_DISK_HOOKS
831 int i;
832 #endif
833
834 if (len == 0)
835 return (NULL);
836
837 if (len == 4 && strcmp(str, "halt") == 0)
838 cpu_reboot(RB_HALT, NULL);
839
840 cp = str + len - 1;
841 c = *cp;
842 if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
843 part = c - 'a';
844 *cp = '\0';
845 } else
846 part = defpart;
847
848 #ifdef MEMORY_DISK_HOOKS
849 for (i = 0; i < NMD; i++)
850 if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
851 dv = &fakemdrootdev[i];
852 goto gotdisk;
853 }
854 #endif
855
856 for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
857 if (dv->dv_class == DV_DISK &&
858 strcmp(str, dv->dv_xname) == 0) {
859 #ifdef MEMORY_DISK_HOOKS
860 gotdisk:
861 #endif
862 majdev = findblkmajor(dv->dv_xname);
863 if (majdev < 0)
864 panic("parsedisk");
865 *devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
866 break;
867 }
868
869 if (dv->dv_class == DV_IFNET &&
870 strcmp(str, dv->dv_xname) == 0) {
871 *devp = NODEV;
872 break;
873 }
874 }
875
876 *cp = c;
877 return (dv);
878 }
879
880 /*
881 * XXX shouldn't this be a common function?
882 */
883 static int
884 getstr(cp, size)
885 char *cp;
886 int size;
887 {
888 char *lp;
889 int c, len;
890
891 cnpollc(1);
892
893 lp = cp;
894 len = 0;
895 for (;;) {
896 c = cngetc();
897 switch (c) {
898 case '\n':
899 case '\r':
900 printf("\n");
901 *lp++ = '\0';
902 cnpollc(0);
903 return (len);
904 case '\b':
905 case '\177':
906 case '#':
907 if (len) {
908 --len;
909 --lp;
910 printf("\b \b");
911 }
912 continue;
913 case '@':
914 case 'u'&037:
915 len = 0;
916 lp = cp;
917 printf("\n");
918 continue;
919 default:
920 if (len + 1 >= size || c < ' ') {
921 printf("\007");
922 continue;
923 }
924 printf("%c", c);
925 ++len;
926 *lp++ = c;
927 }
928 }
929 }
930
931 /*
932 * snprintf() `bytes' into `buf', reformatting it so that the number,
933 * plus a possible `x' + suffix extension) fits into len bytes (including
934 * the terminating NUL).
935 * Returns the number of bytes stored in buf, or -1 * if there was a problem.
936 * E.g, given a len of 9 and a suffix of `B':
937 * bytes result
938 * ----- ------
939 * 99999 `99999 B'
940 * 100000 `97 KB'
941 * 66715648 `65152 KB'
942 * 252215296 `240 MB'
943 */
944 int
945 humanize_number(buf, len, bytes, suffix)
946 char *buf;
947 size_t len;
948 u_int64_t bytes;
949 const char *suffix;
950 {
951 /* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
952 static const char prefixes[] = " KMGTPE";
953
954 int i, r;
955 u_int64_t max;
956 size_t suffixlen;
957
958 if (buf == NULL || suffix == NULL)
959 return (-1);
960 if (len > 0)
961 buf[0] = '\0';
962 suffixlen = strlen(suffix);
963 /* check if enough room for `x y' + suffix + `\0' */
964 if (len < 4 + suffixlen)
965 return (-1);
966
967 max = 1;
968 for (i = 0; i < len - suffixlen - 3; i++)
969 max *= 10;
970 for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
971 bytes /= 1024;
972
973 r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
974 i == 0 ? "" : " ", prefixes[i], suffix);
975
976 return (r);
977 }
978
979 int
980 format_bytes(buf, len, bytes)
981 char *buf;
982 size_t len;
983 u_int64_t bytes;
984 {
985 int rv;
986 size_t nlen;
987
988 rv = humanize_number(buf, len, bytes, "B");
989 if (rv != -1) {
990 /* nuke the trailing ` B' if it exists */
991 nlen = strlen(buf) - 2;
992 if (strcmp(&buf[nlen], " B") == 0)
993 buf[nlen] = '\0';
994 }
995 return (rv);
996 }
997