xref: /freebsd/sys/kern/kern_shutdown.c (revision 6472ac3d8a86336899b6cfb789a4cd9897e3fab5)
1 /*-
2  * Copyright (c) 1986, 1988, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_ddb.h"
41 #include "opt_kdb.h"
42 #include "opt_panic.h"
43 #include "opt_sched.h"
44 #include "opt_watchdog.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/conf.h>
51 #include <sys/cons.h>
52 #include <sys/eventhandler.h>
53 #include <sys/jail.h>
54 #include <sys/kdb.h>
55 #include <sys/kernel.h>
56 #include <sys/kerneldump.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/sched.h>
65 #include <sys/smp.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysproto.h>
68 #include <sys/vnode.h>
69 #ifdef SW_WATCHDOG
70 #include <sys/watchdog.h>
71 #endif
72 
73 #include <ddb/ddb.h>
74 
75 #include <machine/cpu.h>
76 #include <machine/pcb.h>
77 #include <machine/smp.h>
78 
79 #include <security/mac/mac_framework.h>
80 
81 #include <vm/vm.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pager.h>
85 #include <vm/swap_pager.h>
86 
87 #include <sys/signalvar.h>
88 
89 #ifndef PANIC_REBOOT_WAIT_TIME
90 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
91 #endif
92 
93 /*
94  * Note that stdarg.h and the ANSI style va_start macro is used for both
95  * ANSI and traditional C compilers.
96  */
97 #include <machine/stdarg.h>
98 
99 #ifdef KDB
100 #ifdef KDB_UNATTENDED
101 int debugger_on_panic = 0;
102 #else
103 int debugger_on_panic = 1;
104 #endif
105 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
106 	&debugger_on_panic, 0, "Run debugger on kernel panic");
107 TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
108 
109 #ifdef KDB_TRACE
110 static int trace_on_panic = 1;
111 #else
112 static int trace_on_panic = 0;
113 #endif
114 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
115 	&trace_on_panic, 0, "Print stack trace on kernel panic");
116 TUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
117 #endif /* KDB */
118 
119 static int sync_on_panic = 0;
120 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
121 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
122 TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
123 
124 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
125     "Shutdown environment");
126 
127 #ifndef DIAGNOSTIC
128 static int show_busybufs;
129 #else
130 static int show_busybufs = 1;
131 #endif
132 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
133 	&show_busybufs, 0, "");
134 
135 /*
136  * Variable panicstr contains argument to first call to panic; used as flag
137  * to indicate that the kernel has already called panic.
138  */
139 const char *panicstr;
140 
141 int dumping;				/* system is dumping */
142 int rebooting;				/* system is rebooting */
143 static struct dumperinfo dumper;	/* our selected dumper */
144 
145 /* Context information for dump-debuggers. */
146 static struct pcb dumppcb;		/* Registers. */
147 static lwpid_t dumptid;			/* Thread ID. */
148 
149 static void poweroff_wait(void *, int);
150 static void shutdown_halt(void *junk, int howto);
151 static void shutdown_panic(void *junk, int howto);
152 static void shutdown_reset(void *junk, int howto);
153 
154 /* register various local shutdown events */
155 static void
156 shutdown_conf(void *unused)
157 {
158 
159 	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
160 	    SHUTDOWN_PRI_FIRST);
161 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
162 	    SHUTDOWN_PRI_LAST + 100);
163 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
164 	    SHUTDOWN_PRI_LAST + 100);
165 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
166 	    SHUTDOWN_PRI_LAST + 200);
167 }
168 
169 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
170 
171 /*
172  * The system call that results in a reboot.
173  */
174 /* ARGSUSED */
175 int
176 sys_reboot(struct thread *td, struct reboot_args *uap)
177 {
178 	int error;
179 
180 	error = 0;
181 #ifdef MAC
182 	error = mac_system_check_reboot(td->td_ucred, uap->opt);
183 #endif
184 	if (error == 0)
185 		error = priv_check(td, PRIV_REBOOT);
186 	if (error == 0) {
187 		mtx_lock(&Giant);
188 		kern_reboot(uap->opt);
189 		mtx_unlock(&Giant);
190 	}
191 	return (error);
192 }
193 
194 /*
195  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
196  */
197 static int shutdown_howto = 0;
198 
199 void
200 shutdown_nice(int howto)
201 {
202 
203 	shutdown_howto = howto;
204 
205 	/* Send a signal to init(8) and have it shutdown the world */
206 	if (initproc != NULL) {
207 		PROC_LOCK(initproc);
208 		kern_psignal(initproc, SIGINT);
209 		PROC_UNLOCK(initproc);
210 	} else {
211 		/* No init(8) running, so simply reboot */
212 		kern_reboot(RB_NOSYNC);
213 	}
214 	return;
215 }
216 static int	waittime = -1;
217 
218 static void
219 print_uptime(void)
220 {
221 	int f;
222 	struct timespec ts;
223 
224 	getnanouptime(&ts);
225 	printf("Uptime: ");
226 	f = 0;
227 	if (ts.tv_sec >= 86400) {
228 		printf("%ldd", (long)ts.tv_sec / 86400);
229 		ts.tv_sec %= 86400;
230 		f = 1;
231 	}
232 	if (f || ts.tv_sec >= 3600) {
233 		printf("%ldh", (long)ts.tv_sec / 3600);
234 		ts.tv_sec %= 3600;
235 		f = 1;
236 	}
237 	if (f || ts.tv_sec >= 60) {
238 		printf("%ldm", (long)ts.tv_sec / 60);
239 		ts.tv_sec %= 60;
240 		f = 1;
241 	}
242 	printf("%lds\n", (long)ts.tv_sec);
243 }
244 
245 int
246 doadump(boolean_t textdump)
247 {
248 	boolean_t coredump;
249 
250 	if (dumping)
251 		return (EBUSY);
252 	if (dumper.dumper == NULL)
253 		return (ENXIO);
254 
255 	savectx(&dumppcb);
256 	dumptid = curthread->td_tid;
257 	dumping++;
258 
259 	coredump = TRUE;
260 #ifdef DDB
261 	if (textdump && textdump_pending) {
262 		coredump = FALSE;
263 		textdump_dumpsys(&dumper);
264 	}
265 #endif
266 	if (coredump)
267 		dumpsys(&dumper);
268 
269 	dumping--;
270 	return (0);
271 }
272 
273 static int
274 isbufbusy(struct buf *bp)
275 {
276 	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
277 	    BUF_ISLOCKED(bp)) ||
278 	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
279 		return (1);
280 	return (0);
281 }
282 
283 /*
284  * Shutdown the system cleanly to prepare for reboot, halt, or power off.
285  */
286 void
287 kern_reboot(int howto)
288 {
289 	static int first_buf_printf = 1;
290 
291 #if defined(SMP)
292 	/*
293 	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
294 	 * systems don't shutdown properly (i.e., ACPI power off) if we
295 	 * run on another processor.
296 	 */
297 	thread_lock(curthread);
298 	sched_bind(curthread, 0);
299 	thread_unlock(curthread);
300 	KASSERT(PCPU_GET(cpuid) == 0, ("%s: not running on cpu 0", __func__));
301 #endif
302 	/* We're in the process of rebooting. */
303 	rebooting = 1;
304 
305 	/* collect extra flags that shutdown_nice might have set */
306 	howto |= shutdown_howto;
307 
308 	/* We are out of the debugger now. */
309 	kdb_active = 0;
310 
311 	/*
312 	 * Do any callouts that should be done BEFORE syncing the filesystems.
313 	 */
314 	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
315 
316 	/*
317 	 * Now sync filesystems
318 	 */
319 	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
320 		register struct buf *bp;
321 		int iter, nbusy, pbusy;
322 #ifndef PREEMPTION
323 		int subiter;
324 #endif
325 
326 		waittime = 0;
327 
328 #ifdef SW_WATCHDOG
329 		wdog_kern_pat(WD_LASTVAL);
330 #endif
331 		sys_sync(curthread, NULL);
332 
333 		/*
334 		 * With soft updates, some buffers that are
335 		 * written will be remarked as dirty until other
336 		 * buffers are written.
337 		 */
338 		for (iter = pbusy = 0; iter < 20; iter++) {
339 			nbusy = 0;
340 			for (bp = &buf[nbuf]; --bp >= buf; )
341 				if (isbufbusy(bp))
342 					nbusy++;
343 			if (nbusy == 0) {
344 				if (first_buf_printf)
345 					printf("All buffers synced.");
346 				break;
347 			}
348 			if (first_buf_printf) {
349 				printf("Syncing disks, buffers remaining... ");
350 				first_buf_printf = 0;
351 			}
352 			printf("%d ", nbusy);
353 			if (nbusy < pbusy)
354 				iter = 0;
355 			pbusy = nbusy;
356 #ifdef SW_WATCHDOG
357 			wdog_kern_pat(WD_LASTVAL);
358 #endif
359 			sys_sync(curthread, NULL);
360 
361 #ifdef PREEMPTION
362 			/*
363 			 * Drop Giant and spin for a while to allow
364 			 * interrupt threads to run.
365 			 */
366 			DROP_GIANT();
367 			DELAY(50000 * iter);
368 			PICKUP_GIANT();
369 #else
370 			/*
371 			 * Drop Giant and context switch several times to
372 			 * allow interrupt threads to run.
373 			 */
374 			DROP_GIANT();
375 			for (subiter = 0; subiter < 50 * iter; subiter++) {
376 				thread_lock(curthread);
377 				mi_switch(SW_VOL, NULL);
378 				thread_unlock(curthread);
379 				DELAY(1000);
380 			}
381 			PICKUP_GIANT();
382 #endif
383 		}
384 		printf("\n");
385 		/*
386 		 * Count only busy local buffers to prevent forcing
387 		 * a fsck if we're just a client of a wedged NFS server
388 		 */
389 		nbusy = 0;
390 		for (bp = &buf[nbuf]; --bp >= buf; ) {
391 			if (isbufbusy(bp)) {
392 #if 0
393 /* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
394 				if (bp->b_dev == NULL) {
395 					TAILQ_REMOVE(&mountlist,
396 					    bp->b_vp->v_mount, mnt_list);
397 					continue;
398 				}
399 #endif
400 				nbusy++;
401 				if (show_busybufs > 0) {
402 					printf(
403 	    "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
404 					    nbusy, bp, bp->b_vp, bp->b_flags,
405 					    (intmax_t)bp->b_blkno,
406 					    (intmax_t)bp->b_lblkno);
407 					BUF_LOCKPRINTINFO(bp);
408 					if (show_busybufs > 1)
409 						vn_printf(bp->b_vp,
410 						    "vnode content: ");
411 				}
412 			}
413 		}
414 		if (nbusy) {
415 			/*
416 			 * Failed to sync all blocks. Indicate this and don't
417 			 * unmount filesystems (thus forcing an fsck on reboot).
418 			 */
419 			printf("Giving up on %d buffers\n", nbusy);
420 			DELAY(5000000);	/* 5 seconds */
421 		} else {
422 			if (!first_buf_printf)
423 				printf("Final sync complete\n");
424 			/*
425 			 * Unmount filesystems
426 			 */
427 			if (panicstr == 0)
428 				vfs_unmountall();
429 		}
430 		swapoff_all();
431 		DELAY(100000);		/* wait for console output to finish */
432 	}
433 
434 	print_uptime();
435 
436 	/*
437 	 * Ok, now do things that assume all filesystem activity has
438 	 * been completed.
439 	 */
440 	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
441 
442 	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
443 		doadump(TRUE);
444 
445 	/* Now that we're going to really halt the system... */
446 	EVENTHANDLER_INVOKE(shutdown_final, howto);
447 
448 	for(;;) ;	/* safety against shutdown_reset not working */
449 	/* NOTREACHED */
450 }
451 
452 /*
453  * If the shutdown was a clean halt, behave accordingly.
454  */
455 static void
456 shutdown_halt(void *junk, int howto)
457 {
458 
459 	if (howto & RB_HALT) {
460 		printf("\n");
461 		printf("The operating system has halted.\n");
462 		printf("Please press any key to reboot.\n\n");
463 		switch (cngetc()) {
464 		case -1:		/* No console, just die */
465 			cpu_halt();
466 			/* NOTREACHED */
467 		default:
468 			howto &= ~RB_HALT;
469 			break;
470 		}
471 	}
472 }
473 
474 /*
475  * Check to see if the system paniced, pause and then reboot
476  * according to the specified delay.
477  */
478 static void
479 shutdown_panic(void *junk, int howto)
480 {
481 	int loop;
482 
483 	if (howto & RB_DUMP) {
484 		if (PANIC_REBOOT_WAIT_TIME != 0) {
485 			if (PANIC_REBOOT_WAIT_TIME != -1) {
486 				printf("Automatic reboot in %d seconds - "
487 				       "press a key on the console to abort\n",
488 					PANIC_REBOOT_WAIT_TIME);
489 				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
490 				     loop > 0; --loop) {
491 					DELAY(1000 * 100); /* 1/10th second */
492 					/* Did user type a key? */
493 					if (cncheckc() != -1)
494 						break;
495 				}
496 				if (!loop)
497 					return;
498 			}
499 		} else { /* zero time specified - reboot NOW */
500 			return;
501 		}
502 		printf("--> Press a key on the console to reboot,\n");
503 		printf("--> or switch off the system now.\n");
504 		cngetc();
505 	}
506 }
507 
508 /*
509  * Everything done, now reset
510  */
511 static void
512 shutdown_reset(void *junk, int howto)
513 {
514 
515 	printf("Rebooting...\n");
516 	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
517 
518 	/*
519 	 * Acquiring smp_ipi_mtx here has a double effect:
520 	 * - it disables interrupts avoiding CPU0 preemption
521 	 *   by fast handlers (thus deadlocking  against other CPUs)
522 	 * - it avoids deadlocks against smp_rendezvous() or, more
523 	 *   generally, threads busy-waiting, with this spinlock held,
524 	 *   and waiting for responses by threads on other CPUs
525 	 *   (ie. smp_tlb_shootdown()).
526 	 *
527 	 * For the !SMP case it just needs to handle the former problem.
528 	 */
529 #ifdef SMP
530 	mtx_lock_spin(&smp_ipi_mtx);
531 #else
532 	spinlock_enter();
533 #endif
534 
535 	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
536 	cpu_reset();
537 	/* NOTREACHED */ /* assuming reset worked */
538 }
539 
540 /*
541  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
542  * and then reboots.  If we are called twice, then we avoid trying to sync
543  * the disks as this often leads to recursive panics.
544  */
545 void
546 panic(const char *fmt, ...)
547 {
548 #ifdef SMP
549 	static volatile u_int panic_cpu = NOCPU;
550 #endif
551 	struct thread *td = curthread;
552 	int bootopt, newpanic;
553 	va_list ap;
554 	static char buf[256];
555 
556 	critical_enter();
557 #ifdef SMP
558 	/*
559 	 * We don't want multiple CPU's to panic at the same time, so we
560 	 * use panic_cpu as a simple spinlock.  We have to keep checking
561 	 * panic_cpu if we are spinning in case the panic on the first
562 	 * CPU is canceled.
563 	 */
564 	if (panic_cpu != PCPU_GET(cpuid))
565 		while (atomic_cmpset_int(&panic_cpu, NOCPU,
566 		    PCPU_GET(cpuid)) == 0)
567 			while (panic_cpu != NOCPU)
568 				; /* nothing */
569 #endif
570 
571 	bootopt = RB_AUTOBOOT;
572 	newpanic = 0;
573 	if (panicstr)
574 		bootopt |= RB_NOSYNC;
575 	else {
576 		bootopt |= RB_DUMP;
577 		panicstr = fmt;
578 		newpanic = 1;
579 	}
580 
581 	va_start(ap, fmt);
582 	if (newpanic) {
583 		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
584 		panicstr = buf;
585 		printf("panic: %s\n", buf);
586 	} else {
587 		printf("panic: ");
588 		vprintf(fmt, ap);
589 		printf("\n");
590 	}
591 	va_end(ap);
592 #ifdef SMP
593 	printf("cpuid = %d\n", PCPU_GET(cpuid));
594 #endif
595 
596 #ifdef KDB
597 	if (newpanic && trace_on_panic)
598 		kdb_backtrace();
599 	if (debugger_on_panic)
600 		kdb_enter(KDB_WHY_PANIC, "panic");
601 #endif
602 	/*thread_lock(td); */
603 	td->td_flags |= TDF_INPANIC;
604 	/* thread_unlock(td); */
605 	if (!sync_on_panic)
606 		bootopt |= RB_NOSYNC;
607 	critical_exit();
608 	kern_reboot(bootopt);
609 }
610 
611 /*
612  * Support for poweroff delay.
613  *
614  * Please note that setting this delay too short might power off your machine
615  * before the write cache on your hard disk has been flushed, leading to
616  * soft-updates inconsistencies.
617  */
618 #ifndef POWEROFF_DELAY
619 # define POWEROFF_DELAY 5000
620 #endif
621 static int poweroff_delay = POWEROFF_DELAY;
622 
623 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
624 	&poweroff_delay, 0, "");
625 
626 static void
627 poweroff_wait(void *junk, int howto)
628 {
629 
630 	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
631 		return;
632 	DELAY(poweroff_delay * 1000);
633 }
634 
635 /*
636  * Some system processes (e.g. syncer) need to be stopped at appropriate
637  * points in their main loops prior to a system shutdown, so that they
638  * won't interfere with the shutdown process (e.g. by holding a disk buf
639  * to cause sync to fail).  For each of these system processes, register
640  * shutdown_kproc() as a handler for one of shutdown events.
641  */
642 static int kproc_shutdown_wait = 60;
643 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
644     &kproc_shutdown_wait, 0, "");
645 
646 void
647 kproc_shutdown(void *arg, int howto)
648 {
649 	struct proc *p;
650 	int error;
651 
652 	if (panicstr)
653 		return;
654 
655 	p = (struct proc *)arg;
656 	printf("Waiting (max %d seconds) for system process `%s' to stop...",
657 	    kproc_shutdown_wait, p->p_comm);
658 	error = kproc_suspend(p, kproc_shutdown_wait * hz);
659 
660 	if (error == EWOULDBLOCK)
661 		printf("timed out\n");
662 	else
663 		printf("done\n");
664 }
665 
666 void
667 kthread_shutdown(void *arg, int howto)
668 {
669 	struct thread *td;
670 	int error;
671 
672 	if (panicstr)
673 		return;
674 
675 	td = (struct thread *)arg;
676 	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
677 	    kproc_shutdown_wait, td->td_name);
678 	error = kthread_suspend(td, kproc_shutdown_wait * hz);
679 
680 	if (error == EWOULDBLOCK)
681 		printf("timed out\n");
682 	else
683 		printf("done\n");
684 }
685 
686 /* Registration of dumpers */
687 int
688 set_dumper(struct dumperinfo *di)
689 {
690 
691 	if (di == NULL) {
692 		bzero(&dumper, sizeof dumper);
693 		return (0);
694 	}
695 	if (dumper.dumper != NULL)
696 		return (EBUSY);
697 	dumper = *di;
698 	return (0);
699 }
700 
701 /* Call dumper with bounds checking. */
702 int
703 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
704     off_t offset, size_t length)
705 {
706 
707 	if (length != 0 && (offset < di->mediaoffset ||
708 	    offset - di->mediaoffset + length > di->mediasize)) {
709 		printf("Attempt to write outside dump device boundaries.\n"
710 	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
711 		    (intmax_t)offset, (intmax_t)di->mediaoffset,
712 		    (uintmax_t)length, (intmax_t)di->mediasize);
713 		return (ENOSPC);
714 	}
715 	return (di->dumper(di->priv, virtual, physical, offset, length));
716 }
717 
718 void
719 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
720     uint64_t dumplen, uint32_t blksz)
721 {
722 
723 	bzero(kdh, sizeof(*kdh));
724 	strncpy(kdh->magic, magic, sizeof(kdh->magic));
725 	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
726 	kdh->version = htod32(KERNELDUMPVERSION);
727 	kdh->architectureversion = htod32(archver);
728 	kdh->dumplength = htod64(dumplen);
729 	kdh->dumptime = htod64(time_second);
730 	kdh->blocksize = htod32(blksz);
731 	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
732 	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
733 	if (panicstr != NULL)
734 		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
735 	kdh->parity = kerneldump_parity(kdh);
736 }
737