cleanup some odd uses of curproc. Remove PHOLD/PRELE around physical I/O
[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 $
dadab5e9 40 * $DragonFly: src/sys/kern/kern_shutdown.c,v 1.6 2003/06/25 03:55:57 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 143{
dadab5e9 144 struct thread *td = curthread;
984263bc
MD
145 int error;
146
dadab5e9 147 if ((error = suser(td)))
984263bc
MD
148 return (error);
149
150 boot(uap->opt);
151 return (0);
152}
153
154/*
155 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
156 */
157static int shutdown_howto = 0;
158
159void
160shutdown_nice(int howto)
161{
162 shutdown_howto = howto;
163
164 /* Send a signal to init(8) and have it shutdown the world */
165 if (initproc != NULL) {
166 psignal(initproc, SIGINT);
167 } else {
168 /* No init(8) running, so simply reboot */
169 boot(RB_NOSYNC);
170 }
171 return;
172}
173static int waittime = -1;
174static struct pcb dumppcb;
175
176static void
177print_uptime()
178{
179 int f;
180 struct timespec ts;
181
182 getnanouptime(&ts);
183 printf("Uptime: ");
184 f = 0;
185 if (ts.tv_sec >= 86400) {
186 printf("%ldd", ts.tv_sec / 86400);
187 ts.tv_sec %= 86400;
188 f = 1;
189 }
190 if (f || ts.tv_sec >= 3600) {
191 printf("%ldh", ts.tv_sec / 3600);
192 ts.tv_sec %= 3600;
193 f = 1;
194 }
195 if (f || ts.tv_sec >= 60) {
196 printf("%ldm", ts.tv_sec / 60);
197 ts.tv_sec %= 60;
198 f = 1;
199 }
200 printf("%lds\n", ts.tv_sec);
201}
202
203/*
204 * Go through the rigmarole of shutting down..
205 * this used to be in machdep.c but I'll be dammned if I could see
206 * anything machine dependant in it.
207 */
208static void
41c20dac 209boot(int howto)
984263bc
MD
210{
211
212 /* collect extra flags that shutdown_nice might have set */
213 howto |= shutdown_howto;
214
215#ifdef SMP
216 if (smp_active) {
217 printf("boot() called on cpu#%d\n", cpuid);
218 }
219#endif
220 /*
221 * Do any callouts that should be done BEFORE syncing the filesystems.
222 */
223 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
224
225 /*
226 * Now sync filesystems
227 */
228 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
229 register struct buf *bp;
230 int iter, nbusy, pbusy;
231
232 waittime = 0;
233 printf("\nsyncing disks... ");
234
41c20dac 235 sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
984263bc
MD
236
237 /*
238 * With soft updates, some buffers that are
239 * written will be remarked as dirty until other
240 * buffers are written.
241 */
242 for (iter = pbusy = 0; iter < 20; iter++) {
243 nbusy = 0;
244 for (bp = &buf[nbuf]; --bp >= buf; ) {
245 if ((bp->b_flags & B_INVAL) == 0 &&
246 BUF_REFCNT(bp) > 0) {
247 nbusy++;
248 } else if ((bp->b_flags & (B_DELWRI | B_INVAL))
249 == B_DELWRI) {
250 /* bawrite(bp);*/
251 nbusy++;
252 }
253 }
254 if (nbusy == 0)
255 break;
256 printf("%d ", nbusy);
257 if (nbusy < pbusy)
258 iter = 0;
259 pbusy = nbusy;
260 if (iter > 5 && bioops.io_sync)
261 (*bioops.io_sync)(NULL);
41c20dac 262 sync(NULL); /* YYY was sync(&proc0, NULL). why proc0 ? */
984263bc
MD
263 DELAY(50000 * iter);
264 }
265 printf("\n");
266 /*
267 * Count only busy local buffers to prevent forcing
268 * a fsck if we're just a client of a wedged NFS server
269 */
270 nbusy = 0;
271 for (bp = &buf[nbuf]; --bp >= buf; ) {
272 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
273 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
274 if (bp->b_dev == NODEV) {
275 TAILQ_REMOVE(&mountlist,
276 bp->b_vp->v_mount, mnt_list);
277 continue;
278 }
279 nbusy++;
280#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
281 printf(
282 "%p %d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
283 bp, nbusy, devtoname(bp->b_dev),
284 bp->b_flags, (long)bp->b_blkno,
285 (long)bp->b_lblkno);
286#endif
287 }
288 }
289 if (nbusy) {
290 /*
291 * Failed to sync all blocks. Indicate this and don't
292 * unmount filesystems (thus forcing an fsck on reboot).
293 */
294 printf("giving up on %d buffers\n", nbusy);
295 DELAY(5000000); /* 5 seconds */
296 } else {
297 printf("done\n");
298 /*
299 * Unmount filesystems
300 */
301 if (panicstr == 0)
302 vfs_unmountall();
303 }
304 DELAY(100000); /* wait for console output to finish */
305 }
306
307 print_uptime();
308
309 /*
310 * Ok, now do things that assume all filesystem activity has
311 * been completed.
312 */
313 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
314 splhigh();
315 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold)
316 dumpsys();
317
318 /* Now that we're going to really halt the system... */
319 EVENTHANDLER_INVOKE(shutdown_final, howto);
320
321 for(;;) ; /* safety against shutdown_reset not working */
322 /* NOTREACHED */
323}
324
325/*
326 * If the shutdown was a clean halt, behave accordingly.
327 */
328static void
329shutdown_halt(void *junk, int howto)
330{
331 if (howto & RB_HALT) {
332 printf("\n");
333 printf("The operating system has halted.\n");
334 printf("Please press any key to reboot.\n\n");
335 switch (cngetc()) {
336 case -1: /* No console, just die */
337 cpu_halt();
338 /* NOTREACHED */
339 default:
340 howto &= ~RB_HALT;
341 break;
342 }
343 }
344}
345
346/*
347 * Check to see if the system paniced, pause and then reboot
348 * according to the specified delay.
349 */
350static void
351shutdown_panic(void *junk, int howto)
352{
353 int loop;
354
355 if (howto & RB_DUMP) {
356 if (PANIC_REBOOT_WAIT_TIME != 0) {
357 if (PANIC_REBOOT_WAIT_TIME != -1) {
358 printf("Automatic reboot in %d seconds - "
359 "press a key on the console to abort\n",
360 PANIC_REBOOT_WAIT_TIME);
361 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
362 loop > 0; --loop) {
363 DELAY(1000 * 100); /* 1/10th second */
364 /* Did user type a key? */
365 if (cncheckc() != -1)
366 break;
367 }
368 if (!loop)
369 return;
370 }
371 } else { /* zero time specified - reboot NOW */
372 return;
373 }
374 printf("--> Press a key on the console to reboot,\n");
375 printf("--> or switch off the system now.\n");
376 cngetc();
377 }
378}
379
380/*
381 * Everything done, now reset
382 */
383static void
384shutdown_reset(void *junk, int howto)
385{
386 printf("Rebooting...\n");
387 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
388 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
389 cpu_reset();
390 /* NOTREACHED */ /* assuming reset worked */
391}
392
393/*
394 * Magic number for savecore
395 *
396 * exported (symorder) and used at least by savecore(8)
397 *
398 */
399static u_long const dumpmag = 0x8fca0101UL;
400
401static int dumpsize = 0; /* also for savecore */
402
403static int dodump = 1;
404
405SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
406 "Try to perform coredump on kernel panic");
407
408static int
409setdumpdev(dev)
410 dev_t dev;
411{
412 int psize;
413 long newdumplo;
414
415 if (dev == NODEV) {
416 dumpdev = dev;
417 return (0);
418 }
419 if (devsw(dev) == NULL)
420 return (ENXIO); /* XXX is this right? */
421 if (devsw(dev)->d_psize == NULL)
422 return (ENXIO); /* XXX should be ENODEV ? */
423 psize = devsw(dev)->d_psize(dev);
424 if (psize == -1)
425 return (ENXIO); /* XXX should be ENODEV ? */
426 /*
427 * XXX should clean up checking in dumpsys() to be more like this.
428 */
429 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE);
430 if (newdumplo <= LABELSECTOR)
431 return (ENOSPC);
432 dumpdev = dev;
433 dumplo = newdumplo;
434 return (0);
435}
436
437
438/* ARGSUSED */
439static void dump_conf __P((void *dummy));
440static void
441dump_conf(dummy)
442 void *dummy;
443{
444 char *path;
445 dev_t dev;
446
447 path = malloc(MNAMELEN, M_TEMP, M_WAITOK);
448 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) {
449 dev = getdiskbyname(path);
450 if (dev != NODEV)
451 dumpdev = dev;
452 }
453 free(path, M_TEMP);
454 if (setdumpdev(dumpdev) != 0)
455 dumpdev = NODEV;
456}
457
458SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
459
460static int
461sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS)
462{
463 int error;
464 udev_t ndumpdev;
465
466 ndumpdev = dev2udev(dumpdev);
467 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
468 if (error == 0 && req->newptr != NULL)
469 error = setdumpdev(udev2dev(ndumpdev, 0));
470 return (error);
471}
472
473SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
474 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
475
476/*
477 * Doadump comes here after turning off memory management and
478 * getting on the dump stack, either when called above, or by
479 * the auto-restart code.
480 */
481static void
482dumpsys(void)
483{
484 int error;
485
486 savectx(&dumppcb);
487 if (dumping++) {
488 printf("Dump already in progress, bailing...\n");
489 return;
490 }
491 if (!dodump)
492 return;
493 if (dumpdev == NODEV)
494 return;
495 if (!(devsw(dumpdev)))
496 return;
497 if (!(devsw(dumpdev)->d_dump))
498 return;
499 dumpsize = Maxmem;
500 printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
501 printf("dump ");
502 error = (*devsw(dumpdev)->d_dump)(dumpdev);
503 if (error == 0) {
504 printf("succeeded\n");
505 return;
506 }
507 printf("failed, reason: ");
508 switch (error) {
509 case ENODEV:
510 printf("device doesn't support a dump routine\n");
511 break;
512
513 case ENXIO:
514 printf("device bad\n");
515 break;
516
517 case EFAULT:
518 printf("device not ready\n");
519 break;
520
521 case EINVAL:
522 printf("area improper\n");
523 break;
524
525 case EIO:
526 printf("i/o error\n");
527 break;
528
529 case EINTR:
530 printf("aborted from console\n");
531 break;
532
533 default:
534 printf("unknown, error = %d\n", error);
535 break;
536 }
537}
538
539int
540dumpstatus(vm_offset_t addr, off_t count)
541{
542 int c;
543
544 if (addr % (1024 * 1024) == 0) {
545#ifdef HW_WDOG
546 if (wdog_tickler)
547 (*wdog_tickler)();
548#endif
549 printf("%ld ", (long)(count / (1024 * 1024)));
550 }
551
552 if ((c = cncheckc()) == 0x03)
553 return -1;
554 else if (c != -1)
555 printf("[CTRL-C to abort] ");
556
557 return 0;
558}
559
560/*
561 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
562 * and then reboots. If we are called twice, then we avoid trying to sync
563 * the disks as this often leads to recursive panics.
564 */
565void
566panic(const char *fmt, ...)
567{
568 int bootopt;
569 va_list ap;
570 static char buf[256];
571
572 bootopt = RB_AUTOBOOT | RB_DUMP;
573 if (panicstr)
574 bootopt |= RB_NOSYNC;
575 else
576 panicstr = fmt;
577
578 va_start(ap, fmt);
579 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
580 if (panicstr == fmt)
581 panicstr = buf;
582 va_end(ap);
583 printf("panic: %s\n", buf);
584#ifdef SMP
585 /* three seperate prints in case of an unmapped page and trap */
586 printf("mp_lock = %08x; ", mp_lock);
587 printf("cpuid = %d; ", cpuid);
588 printf("lapic.id = %08x\n", lapic.id);
589#endif
590
591#if defined(DDB)
592 if (debugger_on_panic)
593 Debugger ("panic");
594#endif
595 boot(bootopt);
596}
597
598/*
599 * Support for poweroff delay.
600 */
601#ifndef POWEROFF_DELAY
602# define POWEROFF_DELAY 5000
603#endif
604static int poweroff_delay = POWEROFF_DELAY;
605
606SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
607 &poweroff_delay, 0, "");
608
609static void
610poweroff_wait(void *junk, int howto)
611{
612 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
613 return;
614 DELAY(poweroff_delay * 1000);
615}
616
617/*
618 * Some system processes (e.g. syncer) need to be stopped at appropriate
619 * points in their main loops prior to a system shutdown, so that they
620 * won't interfere with the shutdown process (e.g. by holding a disk buf
621 * to cause sync to fail). For each of these system processes, register
622 * shutdown_kproc() as a handler for one of shutdown events.
623 */
624static int kproc_shutdown_wait = 60;
625SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
626 &kproc_shutdown_wait, 0, "");
627
628void
629shutdown_kproc(void *arg, int howto)
630{
bc6dffab 631 struct thread *td;
984263bc
MD
632 struct proc *p;
633 int error;
634
635 if (panicstr)
636 return;
637
bc6dffab
MD
638 td = (struct thread *)arg;
639 if ((p = td->td_proc) != NULL) {
640 printf("Waiting (max %d seconds) for system process `%s' to stop...",
641 kproc_shutdown_wait, p->p_comm);
642 } else {
643 printf("Waiting (max %d seconds) for system thread %p to stop...",
644 kproc_shutdown_wait, td);
645 }
646 error = suspend_kproc(td, kproc_shutdown_wait * hz);
984263bc
MD
647
648 if (error == EWOULDBLOCK)
649 printf("timed out\n");
650 else
651 printf("stopped\n");
652}