1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *  For details, see <http://zfsonlinux.org/>.
10  *
11  *  The SPL is free software; you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License as published by the
13  *  Free Software Foundation; either version 2 of the License, or (at your
14  *  option) any later version.
15  *
16  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19  *  for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  *  Solaris Porting Layer (SPL) Error Implementation.
25  */
26 
27 #include <sys/sysmacros.h>
28 #include <sys/cmn_err.h>
29 
30 /*
31  * It is often useful to actually have the panic crash the node so you
32  * can then get notified of the event, get the crashdump for later
33  * analysis and other such goodies.
34  * But we would still default to the current default of not to do that.
35  */
36 /* BEGIN CSTYLED */
37 unsigned int spl_panic_halt;
38 module_param(spl_panic_halt, uint, 0644);
39 MODULE_PARM_DESC(spl_panic_halt, "Cause kernel panic on assertion failures");
40 /* END CSTYLED */
41 
42 void
43 spl_dumpstack(void)
44 {
45 	printk("Showing stack for process %d\n", current->pid);
46 	dump_stack();
47 }
48 EXPORT_SYMBOL(spl_dumpstack);
49 
50 int
51 spl_panic(const char *file, const char *func, int line, const char *fmt, ...)
52 {
53 	const char *newfile;
54 	char msg[MAXMSGLEN];
55 	va_list ap;
56 
57 	newfile = strrchr(file, '/');
58 	if (newfile != NULL)
59 		newfile = newfile + 1;
60 	else
61 		newfile = file;
62 
63 	va_start(ap, fmt);
64 	(void) vsnprintf(msg, sizeof (msg), fmt, ap);
65 	va_end(ap);
66 
67 	printk(KERN_EMERG "%s", msg);
68 	printk(KERN_EMERG "PANIC at %s:%d:%s()\n", newfile, line, func);
69 	if (spl_panic_halt)
70 		panic("%s", msg);
71 
72 	spl_dumpstack();
73 
74 	/* Halt the thread to facilitate further debugging */
75 	set_current_state(TASK_UNINTERRUPTIBLE);
76 	while (1)
77 		schedule();
78 
79 	/* Unreachable */
80 	return (1);
81 }
82 EXPORT_SYMBOL(spl_panic);
83 
84 void
85 vcmn_err(int ce, const char *fmt, va_list ap)
86 {
87 	char msg[MAXMSGLEN];
88 
89 	vsnprintf(msg, MAXMSGLEN, fmt, ap);
90 
91 	switch (ce) {
92 	case CE_IGNORE:
93 		break;
94 	case CE_CONT:
95 		printk("%s", msg);
96 		break;
97 	case CE_NOTE:
98 		printk(KERN_NOTICE "NOTICE: %s\n", msg);
99 		break;
100 	case CE_WARN:
101 		printk(KERN_WARNING "WARNING: %s\n", msg);
102 		break;
103 	case CE_PANIC:
104 		printk(KERN_EMERG "PANIC: %s\n", msg);
105 		spl_dumpstack();
106 
107 		/* Halt the thread to facilitate further debugging */
108 		set_current_state(TASK_UNINTERRUPTIBLE);
109 		while (1)
110 			schedule();
111 	}
112 } /* vcmn_err() */
113 EXPORT_SYMBOL(vcmn_err);
114 
115 void
116 cmn_err(int ce, const char *fmt, ...)
117 {
118 	va_list ap;
119 
120 	va_start(ap, fmt);
121 	vcmn_err(ce, fmt, ap);
122 	va_end(ap);
123 } /* cmn_err() */
124 EXPORT_SYMBOL(cmn_err);
125