xref: /freebsd/sys/kern/kern_shutdown.c (revision 3eb9ab5255a574471bb487091b09881b0fbec1ba)
1ad4240feSJulian Elischer /*-
2ad4240feSJulian Elischer  * Copyright (c) 1986, 1988, 1991, 1993
3ad4240feSJulian Elischer  *	The Regents of the University of California.  All rights reserved.
4ad4240feSJulian Elischer  * (c) UNIX System Laboratories, Inc.
5ad4240feSJulian Elischer  * All or some portions of this file are derived from material licensed
6ad4240feSJulian Elischer  * to the University of California by American Telephone and Telegraph
7ad4240feSJulian Elischer  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8ad4240feSJulian Elischer  * the permission of UNIX System Laboratories, Inc.
9ad4240feSJulian Elischer  *
10ad4240feSJulian Elischer  * Redistribution and use in source and binary forms, with or without
11ad4240feSJulian Elischer  * modification, are permitted provided that the following conditions
12ad4240feSJulian Elischer  * are met:
13ad4240feSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
14ad4240feSJulian Elischer  *    notice, this list of conditions and the following disclaimer.
15ad4240feSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
16ad4240feSJulian Elischer  *    notice, this list of conditions and the following disclaimer in the
17ad4240feSJulian Elischer  *    documentation and/or other materials provided with the distribution.
18ad4240feSJulian Elischer  * 4. Neither the name of the University nor the names of its contributors
19ad4240feSJulian Elischer  *    may be used to endorse or promote products derived from this software
20ad4240feSJulian Elischer  *    without specific prior written permission.
21ad4240feSJulian Elischer  *
22ad4240feSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23ad4240feSJulian Elischer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ad4240feSJulian Elischer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ad4240feSJulian Elischer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26ad4240feSJulian Elischer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ad4240feSJulian Elischer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ad4240feSJulian Elischer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ad4240feSJulian Elischer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ad4240feSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ad4240feSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ad4240feSJulian Elischer  * SUCH DAMAGE.
33ad4240feSJulian Elischer  *
34ad4240feSJulian Elischer  *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
35ad4240feSJulian Elischer  */
36ad4240feSJulian Elischer 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40618c7db3SRobert Watson #include "opt_ddb.h"
412d50560aSMarcel Moolenaar #include "opt_kdb.h"
426d58e6cbSBruce Evans #include "opt_panic.h"
439923b511SScott Long #include "opt_sched.h"
442be767e0SAttilio Rao #include "opt_watchdog.h"
45ad4240feSJulian Elischer 
46ad4240feSJulian Elischer #include <sys/param.h>
47ad4240feSJulian Elischer #include <sys/systm.h>
489626b608SPoul-Henning Kamp #include <sys/bio.h>
49fc8f7066SBruce Evans #include <sys/buf.h>
501d79f1bbSJohn Baldwin #include <sys/conf.h>
511d79f1bbSJohn Baldwin #include <sys/cons.h>
521d79f1bbSJohn Baldwin #include <sys/eventhandler.h>
5376ca6f88SJamie Gritton #include <sys/jail.h>
542d50560aSMarcel Moolenaar #include <sys/kdb.h>
55ad4240feSJulian Elischer #include <sys/kernel.h>
56e6592ee5SPeter Wemm #include <sys/kerneldump.h>
575e950839SLuoqi Chen #include <sys/kthread.h>
58dcd7d9b7SMaxim Sobolev #include <sys/malloc.h>
59ac0ad63fSBruce Evans #include <sys/mount.h>
60acd3428bSRobert Watson #include <sys/priv.h>
611d79f1bbSJohn Baldwin #include <sys/proc.h>
621d79f1bbSJohn Baldwin #include <sys/reboot.h>
631d79f1bbSJohn Baldwin #include <sys/resourcevar.h>
6420e25d7dSPeter Wemm #include <sys/sched.h>
65248bb937SAttilio Rao #include <sys/smp.h>
66ad4240feSJulian Elischer #include <sys/sysctl.h>
67ad4240feSJulian Elischer #include <sys/sysproto.h>
68fa2b39a1SAttilio Rao #include <sys/vnode.h>
692be767e0SAttilio Rao #ifdef SW_WATCHDOG
702be767e0SAttilio Rao #include <sys/watchdog.h>
712be767e0SAttilio Rao #endif
72ad4240feSJulian Elischer 
73618c7db3SRobert Watson #include <ddb/ddb.h>
74618c7db3SRobert Watson 
7526502503SMarcel Moolenaar #include <machine/cpu.h>
76d39e457bSPoul-Henning Kamp #include <machine/pcb.h>
77752dff3dSJake Burkholder #include <machine/smp.h>
78ad4240feSJulian Elischer 
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
810909f38aSPawel Jakub Dawidek #include <vm/vm.h>
820909f38aSPawel Jakub Dawidek #include <vm/vm_object.h>
830909f38aSPawel Jakub Dawidek #include <vm/vm_page.h>
840909f38aSPawel Jakub Dawidek #include <vm/vm_pager.h>
850909f38aSPawel Jakub Dawidek #include <vm/swap_pager.h>
860909f38aSPawel Jakub Dawidek 
87ad4240feSJulian Elischer #include <sys/signalvar.h>
88ad4240feSJulian Elischer 
89ad4240feSJulian Elischer #ifndef PANIC_REBOOT_WAIT_TIME
90ad4240feSJulian Elischer #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
91ad4240feSJulian Elischer #endif
92ad4240feSJulian Elischer 
93ad4240feSJulian Elischer /*
94ad4240feSJulian Elischer  * Note that stdarg.h and the ANSI style va_start macro is used for both
95ad4240feSJulian Elischer  * ANSI and traditional C compilers.
96ad4240feSJulian Elischer  */
97ad4240feSJulian Elischer #include <machine/stdarg.h>
98ad4240feSJulian Elischer 
992d50560aSMarcel Moolenaar #ifdef KDB
1002d50560aSMarcel Moolenaar #ifdef KDB_UNATTENDED
1019959b1a8SMike Smith int debugger_on_panic = 0;
102ad4240feSJulian Elischer #else
1039959b1a8SMike Smith int debugger_on_panic = 1;
104ad4240feSJulian Elischer #endif
10508a9c205SAndriy Gapon SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
1063d177f46SBill Fumerola 	&debugger_on_panic, 0, "Run debugger on kernel panic");
10708a9c205SAndriy Gapon TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
108e485b64bSJohn Baldwin 
1092d50560aSMarcel Moolenaar #ifdef KDB_TRACE
11008a9c205SAndriy Gapon static int trace_on_panic = 1;
111e485b64bSJohn Baldwin #else
11208a9c205SAndriy Gapon static int trace_on_panic = 0;
113e485b64bSJohn Baldwin #endif
11408a9c205SAndriy Gapon SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
115e485b64bSJohn Baldwin 	&trace_on_panic, 0, "Print stack trace on kernel panic");
11608a9c205SAndriy Gapon TUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
1172d50560aSMarcel Moolenaar #endif /* KDB */
118ad4240feSJulian Elischer 
11908a9c205SAndriy Gapon static int sync_on_panic = 0;
12008a9c205SAndriy Gapon SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
121259ed917SPeter Wemm 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
12208a9c205SAndriy Gapon TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
123259ed917SPeter Wemm 
12435370593SAndriy Gapon static int stop_scheduler_on_panic = 0;
12535370593SAndriy Gapon SYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
12635370593SAndriy Gapon     &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic");
12735370593SAndriy Gapon TUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic);
12835370593SAndriy Gapon 
1296472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
1306472ac3dSEd Schouten     "Shutdown environment");
131db82a982SMike Smith 
132fa2b39a1SAttilio Rao #ifndef DIAGNOSTIC
133fa2b39a1SAttilio Rao static int show_busybufs;
134fa2b39a1SAttilio Rao #else
135fa2b39a1SAttilio Rao static int show_busybufs = 1;
136fa2b39a1SAttilio Rao #endif
137fa2b39a1SAttilio Rao SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
138fa2b39a1SAttilio Rao 	&show_busybufs, 0, "");
139fa2b39a1SAttilio Rao 
1405230cfd2SJulian Elischer /*
141ad4240feSJulian Elischer  * Variable panicstr contains argument to first call to panic; used as flag
142ad4240feSJulian Elischer  * to indicate that the kernel has already called panic.
143ad4240feSJulian Elischer  */
144ad4240feSJulian Elischer const char *panicstr;
145ad4240feSJulian Elischer 
14635370593SAndriy Gapon int stop_scheduler;			/* system stopped CPUs for panic */
14716a011f9SPaul Saab int dumping;				/* system is dumping */
14836a52c3cSJeff Roberson int rebooting;				/* system is rebooting */
14981661c94SPoul-Henning Kamp static struct dumperinfo dumper;	/* our selected dumper */
1502d50560aSMarcel Moolenaar 
1512d50560aSMarcel Moolenaar /* Context information for dump-debuggers. */
1522d50560aSMarcel Moolenaar static struct pcb dumppcb;		/* Registers. */
1532d50560aSMarcel Moolenaar static lwpid_t dumptid;			/* Thread ID. */
15416a011f9SPaul Saab 
15582acbcf5SPeter Wemm static void poweroff_wait(void *, int);
15682acbcf5SPeter Wemm static void shutdown_halt(void *junk, int howto);
15782acbcf5SPeter Wemm static void shutdown_panic(void *junk, int howto);
15882acbcf5SPeter Wemm static void shutdown_reset(void *junk, int howto);
159f06a54f0SPoul-Henning Kamp 
160fcb893a8SMike Smith /* register various local shutdown events */
161fcb893a8SMike Smith static void
162fcb893a8SMike Smith shutdown_conf(void *unused)
163fcb893a8SMike Smith {
164e95499bdSAlfred Perlstein 
165e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
166fd104c15SRebecca Cran 	    SHUTDOWN_PRI_FIRST);
167e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
168e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 100);
169e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
170e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 100);
171e95499bdSAlfred Perlstein 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
172e95499bdSAlfred Perlstein 	    SHUTDOWN_PRI_LAST + 200);
173fcb893a8SMike Smith }
174ad4240feSJulian Elischer 
175237fdd78SRobert Watson SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
176fcb893a8SMike Smith 
177ad4240feSJulian Elischer /*
1780c14ff0eSRobert Watson  * The system call that results in a reboot.
179ad4240feSJulian Elischer  */
180835a82eeSMatthew Dillon /* ARGSUSED */
181ad4240feSJulian Elischer int
1828451d0ddSKip Macy sys_reboot(struct thread *td, struct reboot_args *uap)
183ad4240feSJulian Elischer {
184ad4240feSJulian Elischer 	int error;
185ad4240feSJulian Elischer 
186a2ecb9b7SRobert Watson 	error = 0;
187a2ecb9b7SRobert Watson #ifdef MAC
18830d239bcSRobert Watson 	error = mac_system_check_reboot(td->td_ucred, uap->opt);
189a2ecb9b7SRobert Watson #endif
190a2ecb9b7SRobert Watson 	if (error == 0)
191acd3428bSRobert Watson 		error = priv_check(td, PRIV_REBOOT);
192a2ecb9b7SRobert Watson 	if (error == 0) {
193835a82eeSMatthew Dillon 		mtx_lock(&Giant);
19476e18b25SMarcel Moolenaar 		kern_reboot(uap->opt);
195835a82eeSMatthew Dillon 		mtx_unlock(&Giant);
196a2ecb9b7SRobert Watson 	}
197835a82eeSMatthew Dillon 	return (error);
198ad4240feSJulian Elischer }
199ad4240feSJulian Elischer 
200ad4240feSJulian Elischer /*
201ad4240feSJulian Elischer  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
202ad4240feSJulian Elischer  */
2033e755f76SMike Smith static int shutdown_howto = 0;
2043e755f76SMike Smith 
205ad4240feSJulian Elischer void
2063e755f76SMike Smith shutdown_nice(int howto)
207ad4240feSJulian Elischer {
208e95499bdSAlfred Perlstein 
2093e755f76SMike Smith 	shutdown_howto = howto;
2103e755f76SMike Smith 
211ad4240feSJulian Elischer 	/* Send a signal to init(8) and have it shutdown the world */
212ad4240feSJulian Elischer 	if (initproc != NULL) {
21387729a2bSJohn Baldwin 		PROC_LOCK(initproc);
2148451d0ddSKip Macy 		kern_psignal(initproc, SIGINT);
21587729a2bSJohn Baldwin 		PROC_UNLOCK(initproc);
216ad4240feSJulian Elischer 	} else {
217ad4240feSJulian Elischer 		/* No init(8) running, so simply reboot */
21876e18b25SMarcel Moolenaar 		kern_reboot(RB_NOSYNC);
219ad4240feSJulian Elischer 	}
220ad4240feSJulian Elischer 	return;
221ad4240feSJulian Elischer }
222ad4240feSJulian Elischer static int	waittime = -1;
223ad4240feSJulian Elischer 
22472dfe7a3SPoul-Henning Kamp static void
22582acbcf5SPeter Wemm print_uptime(void)
22672dfe7a3SPoul-Henning Kamp {
22772dfe7a3SPoul-Henning Kamp 	int f;
22872dfe7a3SPoul-Henning Kamp 	struct timespec ts;
22972dfe7a3SPoul-Henning Kamp 
23072dfe7a3SPoul-Henning Kamp 	getnanouptime(&ts);
23172dfe7a3SPoul-Henning Kamp 	printf("Uptime: ");
23272dfe7a3SPoul-Henning Kamp 	f = 0;
23372dfe7a3SPoul-Henning Kamp 	if (ts.tv_sec >= 86400) {
2344a6404dfSJohn Baldwin 		printf("%ldd", (long)ts.tv_sec / 86400);
23572dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 86400;
23672dfe7a3SPoul-Henning Kamp 		f = 1;
23772dfe7a3SPoul-Henning Kamp 	}
23872dfe7a3SPoul-Henning Kamp 	if (f || ts.tv_sec >= 3600) {
2394a6404dfSJohn Baldwin 		printf("%ldh", (long)ts.tv_sec / 3600);
24072dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 3600;
24172dfe7a3SPoul-Henning Kamp 		f = 1;
24272dfe7a3SPoul-Henning Kamp 	}
24372dfe7a3SPoul-Henning Kamp 	if (f || ts.tv_sec >= 60) {
2444a6404dfSJohn Baldwin 		printf("%ldm", (long)ts.tv_sec / 60);
24572dfe7a3SPoul-Henning Kamp 		ts.tv_sec %= 60;
24672dfe7a3SPoul-Henning Kamp 		f = 1;
24772dfe7a3SPoul-Henning Kamp 	}
2484a6404dfSJohn Baldwin 	printf("%lds\n", (long)ts.tv_sec);
24972dfe7a3SPoul-Henning Kamp }
25072dfe7a3SPoul-Henning Kamp 
251299cceefSMarcel Moolenaar int
252299cceefSMarcel Moolenaar doadump(boolean_t textdump)
253d39e457bSPoul-Henning Kamp {
254299cceefSMarcel Moolenaar 	boolean_t coredump;
255e95499bdSAlfred Perlstein 
256299cceefSMarcel Moolenaar 	if (dumping)
257299cceefSMarcel Moolenaar 		return (EBUSY);
258299cceefSMarcel Moolenaar 	if (dumper.dumper == NULL)
259299cceefSMarcel Moolenaar 		return (ENXIO);
260f6449d9dSJulian Elischer 
261d39e457bSPoul-Henning Kamp 	savectx(&dumppcb);
2622d50560aSMarcel Moolenaar 	dumptid = curthread->td_tid;
263d39e457bSPoul-Henning Kamp 	dumping++;
264299cceefSMarcel Moolenaar 
265299cceefSMarcel Moolenaar 	coredump = TRUE;
266618c7db3SRobert Watson #ifdef DDB
267299cceefSMarcel Moolenaar 	if (textdump && textdump_pending) {
268299cceefSMarcel Moolenaar 		coredump = FALSE;
269618c7db3SRobert Watson 		textdump_dumpsys(&dumper);
270299cceefSMarcel Moolenaar 	}
271618c7db3SRobert Watson #endif
272299cceefSMarcel Moolenaar 	if (coredump)
273d39e457bSPoul-Henning Kamp 		dumpsys(&dumper);
274299cceefSMarcel Moolenaar 
2759e473363SRuslan Ermilov 	dumping--;
276299cceefSMarcel Moolenaar 	return (0);
277d39e457bSPoul-Henning Kamp }
278d39e457bSPoul-Henning Kamp 
279d07f87a2SDon Lewis static int
280d07f87a2SDon Lewis isbufbusy(struct buf *bp)
281d07f87a2SDon Lewis {
282d07f87a2SDon Lewis 	if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
283d638e093SAttilio Rao 	    BUF_ISLOCKED(bp)) ||
284d07f87a2SDon Lewis 	    ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
285d07f87a2SDon Lewis 		return (1);
286d07f87a2SDon Lewis 	return (0);
287d07f87a2SDon Lewis }
288d07f87a2SDon Lewis 
289ad4240feSJulian Elischer /*
29070ce93f4SNate Lawson  * Shutdown the system cleanly to prepare for reboot, halt, or power off.
291ad4240feSJulian Elischer  */
29276e18b25SMarcel Moolenaar void
29376e18b25SMarcel Moolenaar kern_reboot(int howto)
294ad4240feSJulian Elischer {
295b6915bdbSDon Lewis 	static int first_buf_printf = 1;
296ad4240feSJulian Elischer 
297f7ebc7ceSMarcel Moolenaar #if defined(SMP)
29870ce93f4SNate Lawson 	/*
29970ce93f4SNate Lawson 	 * Bind us to CPU 0 so that all shutdown code runs there.  Some
30070ce93f4SNate Lawson 	 * systems don't shutdown properly (i.e., ACPI power off) if we
30170ce93f4SNate Lawson 	 * run on another processor.
30270ce93f4SNate Lawson 	 */
30335370593SAndriy Gapon 	if (!SCHEDULER_STOPPED()) {
304982d11f8SJeff Roberson 		thread_lock(curthread);
30520e25d7dSPeter Wemm 		sched_bind(curthread, 0);
306982d11f8SJeff Roberson 		thread_unlock(curthread);
30735370593SAndriy Gapon 		KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
30835370593SAndriy Gapon 	}
30920e25d7dSPeter Wemm #endif
31036a52c3cSJeff Roberson 	/* We're in the process of rebooting. */
31136a52c3cSJeff Roberson 	rebooting = 1;
31220e25d7dSPeter Wemm 
3133e755f76SMike Smith 	/* collect extra flags that shutdown_nice might have set */
3143e755f76SMike Smith 	howto |= shutdown_howto;
3153e755f76SMike Smith 
31661e96500SJohn Baldwin 	/* We are out of the debugger now. */
3172d50560aSMarcel Moolenaar 	kdb_active = 0;
31861e96500SJohn Baldwin 
3195230cfd2SJulian Elischer 	/*
3205230cfd2SJulian Elischer 	 * Do any callouts that should be done BEFORE syncing the filesystems.
3215230cfd2SJulian Elischer 	 */
322fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
3235230cfd2SJulian Elischer 
3245230cfd2SJulian Elischer 	/*
3255230cfd2SJulian Elischer 	 * Now sync filesystems
3265230cfd2SJulian Elischer 	 */
327ad4240feSJulian Elischer 	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
328ad4240feSJulian Elischer 		register struct buf *bp;
32962820f25SJason Evans 		int iter, nbusy, pbusy;
3300c0b25aeSJohn Baldwin #ifndef PREEMPTION
33162820f25SJason Evans 		int subiter;
3320c0b25aeSJohn Baldwin #endif
333ad4240feSJulian Elischer 
334ad4240feSJulian Elischer 		waittime = 0;
335ad4240feSJulian Elischer 
3362be767e0SAttilio Rao #ifdef SW_WATCHDOG
3372be767e0SAttilio Rao 		wdog_kern_pat(WD_LASTVAL);
3382be767e0SAttilio Rao #endif
3398451d0ddSKip Macy 		sys_sync(curthread, NULL);
340ad4240feSJulian Elischer 
341b1897c19SJulian Elischer 		/*
342b1897c19SJulian Elischer 		 * With soft updates, some buffers that are
343b1897c19SJulian Elischer 		 * written will be remarked as dirty until other
344b1897c19SJulian Elischer 		 * buffers are written.
345b1897c19SJulian Elischer 		 */
34662820f25SJason Evans 		for (iter = pbusy = 0; iter < 20; iter++) {
347ad4240feSJulian Elischer 			nbusy = 0;
348d07f87a2SDon Lewis 			for (bp = &buf[nbuf]; --bp >= buf; )
349d07f87a2SDon Lewis 				if (isbufbusy(bp))
350ad4240feSJulian Elischer 					nbusy++;
351b6915bdbSDon Lewis 			if (nbusy == 0) {
352b6915bdbSDon Lewis 				if (first_buf_printf)
35337abb77fSPoul-Henning Kamp 					printf("All buffers synced.");
354ad4240feSJulian Elischer 				break;
355b6915bdbSDon Lewis 			}
356b6915bdbSDon Lewis 			if (first_buf_printf) {
357b6915bdbSDon Lewis 				printf("Syncing disks, buffers remaining... ");
358b6915bdbSDon Lewis 				first_buf_printf = 0;
359b6915bdbSDon Lewis 			}
360ad4240feSJulian Elischer 			printf("%d ", nbusy);
36162820f25SJason Evans 			if (nbusy < pbusy)
36262820f25SJason Evans 				iter = 0;
36362820f25SJason Evans 			pbusy = nbusy;
3642be767e0SAttilio Rao #ifdef SW_WATCHDOG
3652be767e0SAttilio Rao 			wdog_kern_pat(WD_LASTVAL);
3662be767e0SAttilio Rao #endif
3678451d0ddSKip Macy 			sys_sync(curthread, NULL);
3680c0b25aeSJohn Baldwin 
3690c0b25aeSJohn Baldwin #ifdef PREEMPTION
3700c0b25aeSJohn Baldwin 			/*
3710c0b25aeSJohn Baldwin 			 * Drop Giant and spin for a while to allow
3720c0b25aeSJohn Baldwin 			 * interrupt threads to run.
3730c0b25aeSJohn Baldwin 			 */
3740c0b25aeSJohn Baldwin 			DROP_GIANT();
3750c0b25aeSJohn Baldwin 			DELAY(50000 * iter);
3760c0b25aeSJohn Baldwin 			PICKUP_GIANT();
3770c0b25aeSJohn Baldwin #else
3780c0b25aeSJohn Baldwin 			/*
3790c0b25aeSJohn Baldwin 			 * Drop Giant and context switch several times to
3800c0b25aeSJohn Baldwin 			 * allow interrupt threads to run.
3810c0b25aeSJohn Baldwin 			 */
382c86b6ff5SJohn Baldwin 			DROP_GIANT();
38362820f25SJason Evans 			for (subiter = 0; subiter < 50 * iter; subiter++) {
384982d11f8SJeff Roberson 				thread_lock(curthread);
385bf0acc27SJohn Baldwin 				mi_switch(SW_VOL, NULL);
386982d11f8SJeff Roberson 				thread_unlock(curthread);
38762820f25SJason Evans 				DELAY(1000);
38862820f25SJason Evans 			}
38920cdcc5bSJohn Baldwin 			PICKUP_GIANT();
3900c0b25aeSJohn Baldwin #endif
391ad4240feSJulian Elischer 		}
392c8c216d5SNate Lawson 		printf("\n");
393d02d6d04SMike Smith 		/*
394d02d6d04SMike Smith 		 * Count only busy local buffers to prevent forcing
395d02d6d04SMike Smith 		 * a fsck if we're just a client of a wedged NFS server
396d02d6d04SMike Smith 		 */
397d02d6d04SMike Smith 		nbusy = 0;
398d02d6d04SMike Smith 		for (bp = &buf[nbuf]; --bp >= buf; ) {
399d07f87a2SDon Lewis 			if (isbufbusy(bp)) {
400c5690651SPoul-Henning Kamp #if 0
401c5690651SPoul-Henning Kamp /* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
402f3732fd1SPoul-Henning Kamp 				if (bp->b_dev == NULL) {
4030429e37aSPoul-Henning Kamp 					TAILQ_REMOVE(&mountlist,
40467812eacSKirk McKusick 					    bp->b_vp->v_mount, mnt_list);
4059c111b31SPoul-Henning Kamp 					continue;
406dfd5dee1SPeter Wemm 				}
407c5690651SPoul-Henning Kamp #endif
4089c111b31SPoul-Henning Kamp 				nbusy++;
409fa2b39a1SAttilio Rao 				if (show_busybufs > 0) {
4109c111b31SPoul-Henning Kamp 					printf(
411fa2b39a1SAttilio Rao 	    "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
412fa2b39a1SAttilio Rao 					    nbusy, bp, bp->b_vp, bp->b_flags,
413fa2b39a1SAttilio Rao 					    (intmax_t)bp->b_blkno,
414fa2b39a1SAttilio Rao 					    (intmax_t)bp->b_lblkno);
415fa2b39a1SAttilio Rao 					BUF_LOCKPRINTINFO(bp);
416fa2b39a1SAttilio Rao 					if (show_busybufs > 1)
417fa2b39a1SAttilio Rao 						vn_printf(bp->b_vp,
418fa2b39a1SAttilio Rao 						    "vnode content: ");
419fa2b39a1SAttilio Rao 				}
4209c111b31SPoul-Henning Kamp 			}
421d02d6d04SMike Smith 		}
422ad4240feSJulian Elischer 		if (nbusy) {
423ad4240feSJulian Elischer 			/*
424ad4240feSJulian Elischer 			 * Failed to sync all blocks. Indicate this and don't
425ad4240feSJulian Elischer 			 * unmount filesystems (thus forcing an fsck on reboot).
426ad4240feSJulian Elischer 			 */
427b6915bdbSDon Lewis 			printf("Giving up on %d buffers\n", nbusy);
428ad4240feSJulian Elischer 			DELAY(5000000);	/* 5 seconds */
429ad4240feSJulian Elischer 		} else {
430b6915bdbSDon Lewis 			if (!first_buf_printf)
431b6915bdbSDon Lewis 				printf("Final sync complete\n");
432ad4240feSJulian Elischer 			/*
433ad4240feSJulian Elischer 			 * Unmount filesystems
434ad4240feSJulian Elischer 			 */
435ad4240feSJulian Elischer 			if (panicstr == 0)
436ad4240feSJulian Elischer 				vfs_unmountall();
437ad4240feSJulian Elischer 		}
4380909f38aSPawel Jakub Dawidek 		swapoff_all();
439ad4240feSJulian Elischer 		DELAY(100000);		/* wait for console output to finish */
440ad4240feSJulian Elischer 	}
4415230cfd2SJulian Elischer 
44272dfe7a3SPoul-Henning Kamp 	print_uptime();
44372dfe7a3SPoul-Henning Kamp 
4445230cfd2SJulian Elischer 	/*
4455230cfd2SJulian Elischer 	 * Ok, now do things that assume all filesystem activity has
4465230cfd2SJulian Elischer 	 * been completed.
4475230cfd2SJulian Elischer 	 */
448fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
44970ce93f4SNate Lawson 
450f6449d9dSJulian Elischer 	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
451299cceefSMarcel Moolenaar 		doadump(TRUE);
4522cfa0a03SJustin T. Gibbs 
4532cfa0a03SJustin T. Gibbs 	/* Now that we're going to really halt the system... */
454fcb893a8SMike Smith 	EVENTHANDLER_INVOKE(shutdown_final, howto);
4552cfa0a03SJustin T. Gibbs 
456fcb893a8SMike Smith 	for(;;) ;	/* safety against shutdown_reset not working */
457fcb893a8SMike Smith 	/* NOTREACHED */
458fcb893a8SMike Smith }
459fcb893a8SMike Smith 
460fcb893a8SMike Smith /*
461fcb893a8SMike Smith  * If the shutdown was a clean halt, behave accordingly.
462fcb893a8SMike Smith  */
463fcb893a8SMike Smith static void
464fcb893a8SMike Smith shutdown_halt(void *junk, int howto)
465fcb893a8SMike Smith {
466e95499bdSAlfred Perlstein 
467ad4240feSJulian Elischer 	if (howto & RB_HALT) {
468ad4240feSJulian Elischer 		printf("\n");
469ad4240feSJulian Elischer 		printf("The operating system has halted.\n");
470ad4240feSJulian Elischer 		printf("Please press any key to reboot.\n\n");
471d13d3630SJulian Elischer 		switch (cngetc()) {
472d13d3630SJulian Elischer 		case -1:		/* No console, just die */
473d13d3630SJulian Elischer 			cpu_halt();
474d13d3630SJulian Elischer 			/* NOTREACHED */
475d13d3630SJulian Elischer 		default:
4762cfa0a03SJustin T. Gibbs 			howto &= ~RB_HALT;
477d13d3630SJulian Elischer 			break;
478d13d3630SJulian Elischer 		}
479fcb893a8SMike Smith 	}
480fcb893a8SMike Smith }
481ad4240feSJulian Elischer 
482fcb893a8SMike Smith /*
483fcb893a8SMike Smith  * Check to see if the system paniced, pause and then reboot
484fcb893a8SMike Smith  * according to the specified delay.
485fcb893a8SMike Smith  */
486fcb893a8SMike Smith static void
487fcb893a8SMike Smith shutdown_panic(void *junk, int howto)
488fcb893a8SMike Smith {
489fcb893a8SMike Smith 	int loop;
490fcb893a8SMike Smith 
491fcb893a8SMike Smith 	if (howto & RB_DUMP) {
492ad4240feSJulian Elischer 		if (PANIC_REBOOT_WAIT_TIME != 0) {
493ad4240feSJulian Elischer 			if (PANIC_REBOOT_WAIT_TIME != -1) {
4942cfa0a03SJustin T. Gibbs 				printf("Automatic reboot in %d seconds - "
4952cfa0a03SJustin T. Gibbs 				       "press a key on the console to abort\n",
496ad4240feSJulian Elischer 					PANIC_REBOOT_WAIT_TIME);
4972cfa0a03SJustin T. Gibbs 				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
4982cfa0a03SJustin T. Gibbs 				     loop > 0; --loop) {
499ad4240feSJulian Elischer 					DELAY(1000 * 100); /* 1/10th second */
500a7f8f2abSBruce Evans 					/* Did user type a key? */
501a7f8f2abSBruce Evans 					if (cncheckc() != -1)
502ad4240feSJulian Elischer 						break;
503ad4240feSJulian Elischer 				}
504ad4240feSJulian Elischer 				if (!loop)
505fcb893a8SMike Smith 					return;
506ad4240feSJulian Elischer 			}
507ad4240feSJulian Elischer 		} else { /* zero time specified - reboot NOW */
508fcb893a8SMike Smith 			return;
509ad4240feSJulian Elischer 		}
510422702e9SNik Clayton 		printf("--> Press a key on the console to reboot,\n");
511422702e9SNik Clayton 		printf("--> or switch off the system now.\n");
512ad4240feSJulian Elischer 		cngetc();
513ad4240feSJulian Elischer 	}
514fcb893a8SMike Smith }
515fcb893a8SMike Smith 
516fcb893a8SMike Smith /*
517fcb893a8SMike Smith  * Everything done, now reset
518fcb893a8SMike Smith  */
519fcb893a8SMike Smith static void
520fcb893a8SMike Smith shutdown_reset(void *junk, int howto)
521fcb893a8SMike Smith {
522e95499bdSAlfred Perlstein 
523ad4240feSJulian Elischer 	printf("Rebooting...\n");
524ad4240feSJulian Elischer 	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
525248bb937SAttilio Rao 
526248bb937SAttilio Rao 	/*
527248bb937SAttilio Rao 	 * Acquiring smp_ipi_mtx here has a double effect:
528248bb937SAttilio Rao 	 * - it disables interrupts avoiding CPU0 preemption
529248bb937SAttilio Rao 	 *   by fast handlers (thus deadlocking  against other CPUs)
530248bb937SAttilio Rao 	 * - it avoids deadlocks against smp_rendezvous() or, more
531248bb937SAttilio Rao 	 *   generally, threads busy-waiting, with this spinlock held,
532248bb937SAttilio Rao 	 *   and waiting for responses by threads on other CPUs
533248bb937SAttilio Rao 	 *   (ie. smp_tlb_shootdown()).
5340a2d5feaSAttilio Rao 	 *
5350a2d5feaSAttilio Rao 	 * For the !SMP case it just needs to handle the former problem.
536248bb937SAttilio Rao 	 */
5370a2d5feaSAttilio Rao #ifdef SMP
538248bb937SAttilio Rao 	mtx_lock_spin(&smp_ipi_mtx);
5390a2d5feaSAttilio Rao #else
5400a2d5feaSAttilio Rao 	spinlock_enter();
5410a2d5feaSAttilio Rao #endif
542248bb937SAttilio Rao 
543269fb9d7SJulian Elischer 	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
544ad4240feSJulian Elischer 	cpu_reset();
545fcb893a8SMike Smith 	/* NOTREACHED */ /* assuming reset worked */
546ad4240feSJulian Elischer }
547ad4240feSJulian Elischer 
548ad4240feSJulian Elischer /*
549ad4240feSJulian Elischer  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
550ad4240feSJulian Elischer  * and then reboots.  If we are called twice, then we avoid trying to sync
551ad4240feSJulian Elischer  * the disks as this often leads to recursive panics.
552ad4240feSJulian Elischer  */
553ad4240feSJulian Elischer void
5549a6dc4b6SPoul-Henning Kamp panic(const char *fmt, ...)
555ad4240feSJulian Elischer {
55664dd590eSAndriy Gapon #ifdef SMP
55764dd590eSAndriy Gapon 	static volatile u_int panic_cpu = NOCPU;
55835370593SAndriy Gapon 	cpuset_t other_cpus;
55964dd590eSAndriy Gapon #endif
560fe799533SAndrew Gallatin 	struct thread *td = curthread;
561e485b64bSJohn Baldwin 	int bootopt, newpanic;
562ad4240feSJulian Elischer 	va_list ap;
56399237364SAndrey A. Chernov 	static char buf[256];
564ad4240feSJulian Elischer 
56535370593SAndriy Gapon 	if (stop_scheduler_on_panic)
56635370593SAndriy Gapon 		spinlock_enter();
56735370593SAndriy Gapon 	else
56841a4e90eSKonstantin Belousov 		critical_enter();
56935370593SAndriy Gapon 
5700384fff8SJason Evans #ifdef SMP
5711a5333c3SJohn Baldwin 	/*
5721a5333c3SJohn Baldwin 	 * We don't want multiple CPU's to panic at the same time, so we
5730711ca46SJohn Baldwin 	 * use panic_cpu as a simple spinlock.  We have to keep checking
5740711ca46SJohn Baldwin 	 * panic_cpu if we are spinning in case the panic on the first
5751a5333c3SJohn Baldwin 	 * CPU is canceled.
5761a5333c3SJohn Baldwin 	 */
5770711ca46SJohn Baldwin 	if (panic_cpu != PCPU_GET(cpuid))
5780711ca46SJohn Baldwin 		while (atomic_cmpset_int(&panic_cpu, NOCPU,
5790711ca46SJohn Baldwin 		    PCPU_GET(cpuid)) == 0)
5800711ca46SJohn Baldwin 			while (panic_cpu != NOCPU)
5810711ca46SJohn Baldwin 				; /* nothing */
58235370593SAndriy Gapon 
58335370593SAndriy Gapon 	if (stop_scheduler_on_panic) {
58435370593SAndriy Gapon 		if (panicstr == NULL && !kdb_active) {
58535370593SAndriy Gapon 			other_cpus = all_cpus;
58635370593SAndriy Gapon 			CPU_CLR(PCPU_GET(cpuid), &other_cpus);
58735370593SAndriy Gapon 			stop_cpus_hard(other_cpus);
58835370593SAndriy Gapon 		}
58935370593SAndriy Gapon 
59035370593SAndriy Gapon 		/*
59135370593SAndriy Gapon 		 * We set stop_scheduler here and not in the block above,
59235370593SAndriy Gapon 		 * because we want to ensure that if panic has been called and
59335370593SAndriy Gapon 		 * stop_scheduler_on_panic is true, then stop_scheduler will
59435370593SAndriy Gapon 		 * always be set.  Even if panic has been entered from kdb.
59535370593SAndriy Gapon 		 */
59635370593SAndriy Gapon 		stop_scheduler = 1;
59735370593SAndriy Gapon 	}
5980384fff8SJason Evans #endif
5990384fff8SJason Evans 
600e3adb685SAttilio Rao 	bootopt = RB_AUTOBOOT;
601e485b64bSJohn Baldwin 	newpanic = 0;
602ad4240feSJulian Elischer 	if (panicstr)
603ad4240feSJulian Elischer 		bootopt |= RB_NOSYNC;
604e485b64bSJohn Baldwin 	else {
605e3adb685SAttilio Rao 		bootopt |= RB_DUMP;
606ad4240feSJulian Elischer 		panicstr = fmt;
607e485b64bSJohn Baldwin 		newpanic = 1;
608e485b64bSJohn Baldwin 	}
609ad4240feSJulian Elischer 
610ad4240feSJulian Elischer 	va_start(ap, fmt);
6114f1b4577SIan Dowse 	if (newpanic) {
6122127f260SArchie Cobbs 		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
61399237364SAndrey A. Chernov 		panicstr = buf;
6149a6dc4b6SPoul-Henning Kamp 		printf("panic: %s\n", buf);
6154f1b4577SIan Dowse 	} else {
6164f1b4577SIan Dowse 		printf("panic: ");
6174f1b4577SIan Dowse 		vprintf(fmt, ap);
6189a6dc4b6SPoul-Henning Kamp 		printf("\n");
6194f1b4577SIan Dowse 	}
6204f1b4577SIan Dowse 	va_end(ap);
62147d81897SSteve Passe #ifdef SMP
62255c45354SJohn Baldwin 	printf("cpuid = %d\n", PCPU_GET(cpuid));
6232bcc63c5SJohn Baldwin #endif
624ad4240feSJulian Elischer 
6252d50560aSMarcel Moolenaar #ifdef KDB
626e485b64bSJohn Baldwin 	if (newpanic && trace_on_panic)
6272d50560aSMarcel Moolenaar 		kdb_backtrace();
628ad4240feSJulian Elischer 	if (debugger_on_panic)
6293de213ccSRobert Watson 		kdb_enter(KDB_WHY_PANIC, "panic");
6301432aa0cSJohn Baldwin #endif
631982d11f8SJeff Roberson 	/*thread_lock(td); */
632fe799533SAndrew Gallatin 	td->td_flags |= TDF_INPANIC;
633982d11f8SJeff Roberson 	/* thread_unlock(td); */
634259ed917SPeter Wemm 	if (!sync_on_panic)
635259ed917SPeter Wemm 		bootopt |= RB_NOSYNC;
63635370593SAndriy Gapon 	if (!stop_scheduler_on_panic)
63741a4e90eSKonstantin Belousov 		critical_exit();
63876e18b25SMarcel Moolenaar 	kern_reboot(bootopt);
639ad4240feSJulian Elischer }
640ad4240feSJulian Elischer 
641e0d898b4SJulian Elischer /*
642db82a982SMike Smith  * Support for poweroff delay.
643b22692bdSNick Hibma  *
644b22692bdSNick Hibma  * Please note that setting this delay too short might power off your machine
645b22692bdSNick Hibma  * before the write cache on your hard disk has been flushed, leading to
646b22692bdSNick Hibma  * soft-updates inconsistencies.
647db82a982SMike Smith  */
6489eec6969SMike Smith #ifndef POWEROFF_DELAY
6499eec6969SMike Smith # define POWEROFF_DELAY 5000
6509eec6969SMike Smith #endif
6519eec6969SMike Smith static int poweroff_delay = POWEROFF_DELAY;
6529eec6969SMike Smith 
653db82a982SMike Smith SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
654*3eb9ab52SEitan Adler     &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
655db82a982SMike Smith 
656fcb893a8SMike Smith static void
657fcb893a8SMike Smith poweroff_wait(void *junk, int howto)
658db82a982SMike Smith {
659e95499bdSAlfred Perlstein 
660db82a982SMike Smith 	if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
661db82a982SMike Smith 		return;
662db82a982SMike Smith 	DELAY(poweroff_delay * 1000);
663db82a982SMike Smith }
6645e950839SLuoqi Chen 
6655e950839SLuoqi Chen /*
6665e950839SLuoqi Chen  * Some system processes (e.g. syncer) need to be stopped at appropriate
6675e950839SLuoqi Chen  * points in their main loops prior to a system shutdown, so that they
6685e950839SLuoqi Chen  * won't interfere with the shutdown process (e.g. by holding a disk buf
6695e950839SLuoqi Chen  * to cause sync to fail).  For each of these system processes, register
6705e950839SLuoqi Chen  * shutdown_kproc() as a handler for one of shutdown events.
6715e950839SLuoqi Chen  */
6725e950839SLuoqi Chen static int kproc_shutdown_wait = 60;
6735e950839SLuoqi Chen SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
674*3eb9ab52SEitan Adler     &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
6755e950839SLuoqi Chen 
6765e950839SLuoqi Chen void
677ffc831daSJohn Baldwin kproc_shutdown(void *arg, int howto)
6785e950839SLuoqi Chen {
6795e950839SLuoqi Chen 	struct proc *p;
6805e950839SLuoqi Chen 	int error;
6815e950839SLuoqi Chen 
6825e950839SLuoqi Chen 	if (panicstr)
6835e950839SLuoqi Chen 		return;
6845e950839SLuoqi Chen 
6855e950839SLuoqi Chen 	p = (struct proc *)arg;
686b1c81391SNate Lawson 	printf("Waiting (max %d seconds) for system process `%s' to stop...",
6874f9d48e4SJohn Baldwin 	    kproc_shutdown_wait, p->p_comm);
6883745c395SJulian Elischer 	error = kproc_suspend(p, kproc_shutdown_wait * hz);
6895e950839SLuoqi Chen 
6905e950839SLuoqi Chen 	if (error == EWOULDBLOCK)
691b1c81391SNate Lawson 		printf("timed out\n");
6925e950839SLuoqi Chen 	else
693b1c81391SNate Lawson 		printf("done\n");
6945e950839SLuoqi Chen }
69581661c94SPoul-Henning Kamp 
6967ab24ea3SJulian Elischer void
6977ab24ea3SJulian Elischer kthread_shutdown(void *arg, int howto)
6987ab24ea3SJulian Elischer {
6997ab24ea3SJulian Elischer 	struct thread *td;
7007ab24ea3SJulian Elischer 	int error;
7017ab24ea3SJulian Elischer 
7027ab24ea3SJulian Elischer 	if (panicstr)
7037ab24ea3SJulian Elischer 		return;
7047ab24ea3SJulian Elischer 
7057ab24ea3SJulian Elischer 	td = (struct thread *)arg;
7067ab24ea3SJulian Elischer 	printf("Waiting (max %d seconds) for system thread `%s' to stop...",
7074f9d48e4SJohn Baldwin 	    kproc_shutdown_wait, td->td_name);
7087ab24ea3SJulian Elischer 	error = kthread_suspend(td, kproc_shutdown_wait * hz);
7097ab24ea3SJulian Elischer 
7107ab24ea3SJulian Elischer 	if (error == EWOULDBLOCK)
7117ab24ea3SJulian Elischer 		printf("timed out\n");
7127ab24ea3SJulian Elischer 	else
7137ab24ea3SJulian Elischer 		printf("done\n");
7147ab24ea3SJulian Elischer }
7157ab24ea3SJulian Elischer 
71681661c94SPoul-Henning Kamp /* Registration of dumpers */
71781661c94SPoul-Henning Kamp int
71881661c94SPoul-Henning Kamp set_dumper(struct dumperinfo *di)
71981661c94SPoul-Henning Kamp {
720e95499bdSAlfred Perlstein 
72181661c94SPoul-Henning Kamp 	if (di == NULL) {
72281661c94SPoul-Henning Kamp 		bzero(&dumper, sizeof dumper);
72381661c94SPoul-Henning Kamp 		return (0);
72481661c94SPoul-Henning Kamp 	}
72581661c94SPoul-Henning Kamp 	if (dumper.dumper != NULL)
72681661c94SPoul-Henning Kamp 		return (EBUSY);
72781661c94SPoul-Henning Kamp 	dumper = *di;
72881661c94SPoul-Henning Kamp 	return (0);
72981661c94SPoul-Henning Kamp }
73081661c94SPoul-Henning Kamp 
731007b1b7bSRuslan Ermilov /* Call dumper with bounds checking. */
732007b1b7bSRuslan Ermilov int
733007b1b7bSRuslan Ermilov dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
734007b1b7bSRuslan Ermilov     off_t offset, size_t length)
735007b1b7bSRuslan Ermilov {
736007b1b7bSRuslan Ermilov 
737007b1b7bSRuslan Ermilov 	if (length != 0 && (offset < di->mediaoffset ||
738007b1b7bSRuslan Ermilov 	    offset - di->mediaoffset + length > di->mediasize)) {
73958379067SAttilio Rao 		printf("Attempt to write outside dump device boundaries.\n"
74058379067SAttilio Rao 	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
74158379067SAttilio Rao 		    (intmax_t)offset, (intmax_t)di->mediaoffset,
74258379067SAttilio Rao 		    (uintmax_t)length, (intmax_t)di->mediasize);
74358379067SAttilio Rao 		return (ENOSPC);
744007b1b7bSRuslan Ermilov 	}
745007b1b7bSRuslan Ermilov 	return (di->dumper(di->priv, virtual, physical, offset, length));
746007b1b7bSRuslan Ermilov }
747007b1b7bSRuslan Ermilov 
748e6592ee5SPeter Wemm void
749e6592ee5SPeter Wemm mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
750e6592ee5SPeter Wemm     uint64_t dumplen, uint32_t blksz)
751e6592ee5SPeter Wemm {
752e6592ee5SPeter Wemm 
753e6592ee5SPeter Wemm 	bzero(kdh, sizeof(*kdh));
754e6592ee5SPeter Wemm 	strncpy(kdh->magic, magic, sizeof(kdh->magic));
755e6592ee5SPeter Wemm 	strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
756e6592ee5SPeter Wemm 	kdh->version = htod32(KERNELDUMPVERSION);
757e6592ee5SPeter Wemm 	kdh->architectureversion = htod32(archver);
758e6592ee5SPeter Wemm 	kdh->dumplength = htod64(dumplen);
759e6592ee5SPeter Wemm 	kdh->dumptime = htod64(time_second);
760e6592ee5SPeter Wemm 	kdh->blocksize = htod32(blksz);
761c1f19219SJamie Gritton 	strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
762e6592ee5SPeter Wemm 	strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
763e6592ee5SPeter Wemm 	if (panicstr != NULL)
764e6592ee5SPeter Wemm 		strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
765e6592ee5SPeter Wemm 	kdh->parity = kerneldump_parity(kdh);
766e6592ee5SPeter Wemm }
767