proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / kern / kern_shutdown.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_shutdown.c,v 1.72.2.12 2002/02/21 19:15:10 dillon Exp $
41c20dac 40 * $DragonFly: src/sys/kern/kern_shutdown.c,v 1.5 2003/06/23 17:55:41 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ddb.h"
44#include "opt_hw_wdog.h"
45#include "opt_panic.h"
46#include "opt_show_busybufs.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/eventhandler.h>
51#include <sys/buf.h>
52#include <sys/disklabel.h>
53#include <sys/reboot.h>
54#include <sys/proc.h>
55#include <sys/vnode.h>
56#include <sys/kernel.h>
57#include <sys/kthread.h>
58#include <sys/malloc.h>
59#include <sys/mount.h>
60#include <sys/queue.h>
61#include <sys/sysctl.h>
62#include <sys/conf.h>
63#include <sys/sysproto.h>
64#include <sys/cons.h>
3020e3be 65#include <sys/buf2.h>
984263bc
MD
66
67#include <machine/pcb.h>
68#include <machine/clock.h>
69#include <machine/md_var.h>
70#include <machine/smp.h> /* smp_active, cpuid */
71
72#include <sys/signalvar.h>
73
74#ifndef PANIC_REBOOT_WAIT_TIME
75#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
76#endif
77
78/*
79 * Note that stdarg.h and the ANSI style va_start macro is used for both
80 * ANSI and traditional C compilers.
81 */
82#include <machine/stdarg.h>
83
84#ifdef DDB
85#ifdef DDB_UNATTENDED
86int debugger_on_panic = 0;
87#else
88int debugger_on_panic = 1;
89#endif
90SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
91 &debugger_on_panic, 0, "Run debugger on kernel panic");
92#endif
93
94SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
95
96#ifdef HW_WDOG
97/*
98 * If there is a hardware watchdog, point this at the function needed to
99 * hold it off.
100 * It's needed when the kernel needs to do some lengthy operations.
101 * e.g. in wd.c when dumping core.. It's most annoying to have
102 * your precious core-dump only half written because the wdog kicked in.
103 */
104watchdog_tickle_fn wdog_tickler = NULL;
105#endif /* HW_WDOG */
106
107/*
108 * Variable panicstr contains argument to first call to panic; used as flag
109 * to indicate that the kernel has already called panic.
110 */
111const char *panicstr;
112
113int dumping; /* system is dumping */
114
115static void boot __P((int)) __dead2;
116static void dumpsys __P((void));
117static int setdumpdev __P((dev_t dev));
118static void poweroff_wait __P((void *, int));
119static void print_uptime __P((void));
120static void shutdown_halt __P((void *junk, int howto));
121static void shutdown_panic __P((void *junk, int howto));
122static void shutdown_reset __P((void *junk, int howto));
123
124/* register various local shutdown events */
125static void
126shutdown_conf(void *unused)
127{
128 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST);
129 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100);
130 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100);
131 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200);
132}
133
134SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL)
135
136/* ARGSUSED */
137
138/*
139 * The system call that results in a reboot
140 */
141int
41c20dac 142reboot(struct reboot_args *uap)
984263bc
MD
143{
144 int error;
145
41c20dac 146 if ((error = suser()))
984263bc
MD
147 return (error);
148
149 boot(uap->opt);
150 return (0);
151}
152
153/*
154 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
155 */
156static int shutdown_howto = 0;
157
158void
159shutdown_nice(int howto)
160{
161 shutdown_howto = howto;
162
163 /* Send a signal to init(8) and have it shutdown the world */
164 if (initproc != NULL) {
165 psignal(initproc, SIGINT);
166 } else {
167 /* No init(8) running, so simply reboot */
168 boot(RB_NOSYNC);
169 }
170 return;
171}
172static int waittime = -1;
173static struct pcb dumppcb;
174
175static void
176print_uptime()
177{
178 int f;
179 struct timespec ts;
180
181 getnanouptime(&ts);
182 printf("Uptime: ");
183 f = 0;
184 if (ts.tv_sec >= 86400) {
185 printf("%ldd", ts.tv_sec / 86400);
186 ts.tv_sec %= 86400;
187 f = 1;
188 }
189 if (f || ts.tv_sec >= 3600) {
190 printf("%ldh", ts.tv_sec / 3600);
191 ts.tv_sec %= 3600;
192 f = 1;
193 }
194 if (f || ts.tv_sec >= 60) {
195 printf("%ldm", ts.tv_sec / 60);
196 ts.tv_sec %= 60;
197 f = 1;
198 }
199 printf("%lds\n", ts.tv_sec);
200}
201
202/*
203 * Go through the rigmarole of shutting down..
204 * this used to be in machdep.c but I'll be dammned if I could see
205 * anything machine dependant in it.
206 */
207static void
41c20dac 208boot(int howto)
984263bc
MD
209{
210
211 /* collect extra flags that shutdown_nice might have set */
212 howto |= shutdown_howto;
213
214#ifdef SMP
215 if (smp_active) {
216 printf("boot() called on cpu#%d\n", cpuid);
217 }
218#endif
219 /*
220 * Do any callouts that should be done BEFORE syncing the filesystems.
221 */
222 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
223
224 /*
225 * Now sync filesystems
226 */
227 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
228 register struct buf *bp;
229 int iter, nbusy, pbusy;
230
231 waittime = 0;
232 printf("\nsyncing disks... ");
233
41c20dac 234 sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
984263bc
MD
235
236 /*
237 * With soft updates, some buffers that are
238 * written will be remarked as dirty until other
239 * buffers are written.
240 */
241 for (iter = pbusy = 0; iter < 20; iter++) {
242 nbusy = 0;
243 for (bp = &buf[nbuf]; --bp >= buf; ) {
244 if ((bp->b_flags & B_INVAL) == 0 &&
245 BUF_REFCNT(bp) > 0) {
246 nbusy++;
247 } else if ((bp->b_flags & (B_DELWRI | B_INVAL))
248 == B_DELWRI) {
249 /* bawrite(bp);*/
250 nbusy++;
251 }
252 }
253 if (nbusy == 0)
254 break;
255 printf("%d ", nbusy);
256 if (nbusy < pbusy)
257 iter = 0;
258 pbusy = nbusy;
259 if (iter > 5 && bioops.io_sync)
260 (*bioops.io_sync)(NULL);
41c20dac 261 sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
984263bc
MD
262 DELAY(50000 * iter);
263 }
264 printf("\n");
265 /*
266 * Count only busy local buffers to prevent forcing
267 * a fsck if we're just a client of a wedged NFS server
268 */
269 nbusy = 0;
270 for (bp = &buf[nbuf]; --bp >= buf; ) {
271 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
272 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
273 if (bp->b_dev == NODEV) {
274 TAILQ_REMOVE(&mountlist,
275 bp->b_vp->v_mount, mnt_list);
276 continue;
277 }
278 nbusy++;
279#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
280 printf(
281 "%p %d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
282 bp, nbusy, devtoname(bp->b_dev),
283 bp->b_flags, (long)bp->b_blkno,
284 (long)bp->b_lblkno);
285#endif
286 }
287 }
288 if (nbusy) {
289 /*
290 * Failed to sync all blocks. Indicate this and don't
291 * unmount filesystems (thus forcing an fsck on reboot).
292 */
293 printf("giving up on %d buffers\n", nbusy);
294 DELAY(5000000); /* 5 seconds */
295 } else {
296 printf("done\n");
297 /*
298 * Unmount filesystems
299 */
300 if (panicstr == 0)
301 vfs_unmountall();
302 }
303 DELAY(100000); /* wait for console output to finish */
304 }
305
306 print_uptime();
307
308 /*
309 * Ok, now do things that assume all filesystem activity has
310 * been completed.
311 */
312 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
313 splhigh();
314 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold)
315 dumpsys();
316
317 /* Now that we're going to really halt the system... */
318 EVENTHANDLER_INVOKE(shutdown_final, howto);
319
320 for(;;) ; /* safety against shutdown_reset not working */
321 /* NOTREACHED */
322}
323
324/*
325 * If the shutdown was a clean halt, behave accordingly.
326 */
327static void
328shutdown_halt(void *junk, int howto)
329{
330 if (howto & RB_HALT) {
331 printf("\n");
332 printf("The operating system has halted.\n");
333 printf("Please press any key to reboot.\n\n");
334 switch (cngetc()) {
335 case -1: /* No console, just die */
336 cpu_halt();
337 /* NOTREACHED */
338 default:
339 howto &= ~RB_HALT;
340 break;
341 }
342 }
343}
344
345/*
346 * Check to see if the system paniced, pause and then reboot
347 * according to the specified delay.
348 */
349static void
350shutdown_panic(void *junk, int howto)
351{
352 int loop;
353
354 if (howto & RB_DUMP) {
355 if (PANIC_REBOOT_WAIT_TIME != 0) {
356 if (PANIC_REBOOT_WAIT_TIME != -1) {
357 printf("Automatic reboot in %d seconds - "
358 "press a key on the console to abort\n",
359 PANIC_REBOOT_WAIT_TIME);
360 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
361 loop > 0; --loop) {
362 DELAY(1000 * 100); /* 1/10th second */
363 /* Did user type a key? */
364 if (cncheckc() != -1)
365 break;
366 }
367 if (!loop)
368 return;
369 }
370 } else { /* zero time specified - reboot NOW */
371 return;
372 }
373 printf("--> Press a key on the console to reboot,\n");
374 printf("--> or switch off the system now.\n");
375 cngetc();
376 }
377}
378
379/*
380 * Everything done, now reset
381 */
382static void
383shutdown_reset(void *junk, int howto)
384{
385 printf("Rebooting...\n");
386 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
387 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
388 cpu_reset();
389 /* NOTREACHED */ /* assuming reset worked */
390}
391
392/*
393 * Magic number for savecore
394 *
395 * exported (symorder) and used at least by savecore(8)
396 *
397 */
398static u_long const dumpmag = 0x8fca0101UL;
399
400static int dumpsize = 0; /* also for savecore */
401
402static int dodump = 1;
403
404SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
405 "Try to perform coredump on kernel panic");
406
407static int
408setdumpdev(dev)
409 dev_t dev;
410{
411 int psize;
412 long newdumplo;
413
414 if (dev == NODEV) {
415 dumpdev = dev;
416 return (0);
417 }
418 if (devsw(dev) == NULL)
419 return (ENXIO); /* XXX is this right? */
420 if (devsw(dev)->d_psize == NULL)
421 return (ENXIO); /* XXX should be ENODEV ? */
422 psize = devsw(dev)->d_psize(dev);
423 if (psize == -1)
424 return (ENXIO); /* XXX should be ENODEV ? */
425 /*
426 * XXX should clean up checking in dumpsys() to be more like this.
427 */
428 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE);
429 if (newdumplo <= LABELSECTOR)
430 return (ENOSPC);
431 dumpdev = dev;
432 dumplo = newdumplo;
433 return (0);
434}
435
436
437/* ARGSUSED */
438static void dump_conf __P((void *dummy));
439static void
440dump_conf(dummy)
441 void *dummy;
442{
443 char *path;
444 dev_t dev;
445
446 path = malloc(MNAMELEN, M_TEMP, M_WAITOK);
447 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) {
448 dev = getdiskbyname(path);
449 if (dev != NODEV)
450 dumpdev = dev;
451 }
452 free(path, M_TEMP);
453 if (setdumpdev(dumpdev) != 0)
454 dumpdev = NODEV;
455}
456
457SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
458
459static int
460sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
461{
462 int error;
463 udev_t ndumpdev;
464
465 ndumpdev = dev2udev(dumpdev);
466 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
467 if (error == 0 && req->newptr != NULL)
468 error = setdumpdev(udev2dev(ndumpdev, 0));
469 return (error);
470}
471
472SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
473 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
474
475/*
476 * Doadump comes here after turning off memory management and
477 * getting on the dump stack, either when called above, or by
478 * the auto-restart code.
479 */
480static void
481dumpsys(void)
482{
483 int error;
484
485 savectx(&dumppcb);
486 if (dumping++) {
487 printf("Dump already in progress, bailing...\n");
488 return;
489 }
490 if (!dodump)
491 return;
492 if (dumpdev == NODEV)
493 return;
494 if (!(devsw(dumpdev)))
495 return;
496 if (!(devsw(dumpdev)->d_dump))
497 return;
498 dumpsize = Maxmem;
499 printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
500 printf("dump ");
501 error = (*devsw(dumpdev)->d_dump)(dumpdev);
502 if (error == 0) {
503 printf("succeeded\n");
504 return;
505 }
506 printf("failed, reason: ");
507 switch (error) {
508 case ENODEV:
509 printf("device doesn't support a dump routine\n");
510 break;
511
512 case ENXIO:
513 printf("device bad\n");
514 break;
515
516 case EFAULT:
517 printf("device not ready\n");
518 break;
519
520 case EINVAL:
521 printf("area improper\n");
522 break;
523
524 case EIO:
525 printf("i/o error\n");
526 break;
527
528 case EINTR:
529 printf("aborted from console\n");
530 break;
531
532 default:
533 printf("unknown, error = %d\n", error);
534 break;
535 }
536}
537
538int
539dumpstatus(vm_offset_t addr, off_t count)
540{
541 int c;
542
543 if (addr % (1024 * 1024) == 0) {
544#ifdef HW_WDOG
545 if (wdog_tickler)
546 (*wdog_tickler)();
547#endif
548 printf("%ld ", (long)(count / (1024 * 1024)));
549 }
550
551 if ((c = cncheckc()) == 0x03)
552 return -1;
553 else if (c != -1)
554 printf("[CTRL-C to abort] ");
555
556 return 0;
557}
558
559/*
560 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
561 * and then reboots. If we are called twice, then we avoid trying to sync
562 * the disks as this often leads to recursive panics.
563 */
564void
565panic(const char *fmt, ...)
566{
567 int bootopt;
568 va_list ap;
569 static char buf[256];
570
571 bootopt = RB_AUTOBOOT | RB_DUMP;
572 if (panicstr)
573 bootopt |= RB_NOSYNC;
574 else
575 panicstr = fmt;
576
577 va_start(ap, fmt);
578 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
579 if (panicstr == fmt)
580 panicstr = buf;
581 va_end(ap);
582 printf("panic: %s\n", buf);
583#ifdef SMP
584 /* three seperate prints in case of an unmapped page and trap */
585 printf("mp_lock = %08x; ", mp_lock);
586 printf("cpuid = %d; ", cpuid);
587 printf("lapic.id = %08x\n", lapic.id);
588#endif
589
590#if defined(DDB)
591 if (debugger_on_panic)
592 Debugger ("panic");
593#endif
594 boot(bootopt);
595}
596
597/*
598 * Support for poweroff delay.
599 */
600#ifndef POWEROFF_DELAY
601# define POWEROFF_DELAY 5000
602#endif
603static int poweroff_delay = POWEROFF_DELAY;
604
605SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
606 &poweroff_delay, 0, "");
607
608static void
609poweroff_wait(void *junk, int howto)
610{
611 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
612 return;
613 DELAY(poweroff_delay * 1000);
614}
615
616/*
617 * Some system processes (e.g. syncer) need to be stopped at appropriate
618 * points in their main loops prior to a system shutdown, so that they
619 * won't interfere with the shutdown process (e.g. by holding a disk buf
620 * to cause sync to fail). For each of these system processes, register
621 * shutdown_kproc() as a handler for one of shutdown events.
622 */
623static int kproc_shutdown_wait = 60;
624SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
625 &kproc_shutdown_wait, 0, "");
626
627void
628shutdown_kproc(void *arg, int howto)
629{
bc6dffab 630 struct thread *td;
984263bc
MD
631 struct proc *p;
632 int error;
633
634 if (panicstr)
635 return;
636
bc6dffab
MD
637 td = (struct thread *)arg;
638 if ((p = td->td_proc) != NULL) {
639 printf("Waiting (max %d seconds) for system process `%s' to stop...",
640 kproc_shutdown_wait, p->p_comm);
641 } else {
642 printf("Waiting (max %d seconds) for system thread %p to stop...",
643 kproc_shutdown_wait, td);
644 }
645 error = suspend_kproc(td, kproc_shutdown_wait * hz);
984263bc
MD
646
647 if (error == EWOULDBLOCK)
648 printf("timed out\n");
649 else
650 printf("stopped\n");
651}