GNU Linux-libre 4.9.296-gnu1
[releases.git] / drivers / staging / lustre / lnet / libcfs / linux / linux-debug.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/linux/linux-debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #include <linux/module.h>
38 #include <linux/kmod.h>
39 #include <linux/notifier.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #include <linux/unistd.h>
46 #include <linux/interrupt.h>
47 #include <linux/completion.h>
48 #include <linux/fs.h>
49 #include <linux/uaccess.h>
50 #include <linux/miscdevice.h>
51
52 # define DEBUG_SUBSYSTEM S_LNET
53
54 #include "../../../include/linux/libcfs/libcfs.h"
55
56 #include "../tracefile.h"
57
58 #include <linux/kallsyms.h>
59
60 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
61 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
62
63 /**
64  * Upcall function once a Lustre log has been dumped.
65  *
66  * \param file  path of the dumped log
67  */
68 void libcfs_run_debug_log_upcall(char *file)
69 {
70         char *argv[3];
71         int   rc;
72         char *envp[] = {
73                 "HOME=/",
74                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
75                 NULL};
76
77         argv[0] = lnet_debug_log_upcall;
78
79         LASSERTF(file, "called on a null filename\n");
80         argv[1] = file; /* only need to pass the path of the file */
81
82         argv[2] = NULL;
83
84         rc = call_usermodehelper(argv[0], argv, envp, 1);
85         if (rc < 0 && rc != -ENOENT) {
86                 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
87                        rc, argv[0], argv[1]);
88         } else {
89                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
90                        argv[0], argv[1]);
91         }
92 }
93
94 void libcfs_run_upcall(char **argv)
95 {
96         int   rc;
97         int   argc;
98         char *envp[] = {
99                 "HOME=/",
100                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
101                 NULL};
102
103         argv[0] = lnet_upcall;
104         argc = 1;
105         while (argv[argc])
106                 argc++;
107
108         LASSERT(argc >= 2);
109
110         rc = call_usermodehelper(argv[0], argv, envp, 1);
111         if (rc < 0 && rc != -ENOENT) {
112                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; check /sys/kernel/debug/lnet/upcall\n",
113                        rc, argv[0], argv[1],
114                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
115                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
116                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
117                        argc < 6 ? "" : ",...");
118         } else {
119                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
120                        argv[0], argv[1],
121                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
122                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
123                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
124                        argc < 6 ? "" : ",...");
125         }
126 }
127
128 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
129 {
130         char *argv[6];
131         char buf[32];
132
133         snprintf(buf, sizeof(buf), "%d", msgdata->msg_line);
134
135         argv[1] = "LBUG";
136         argv[2] = (char *)msgdata->msg_file;
137         argv[3] = (char *)msgdata->msg_fn;
138         argv[4] = buf;
139         argv[5] = NULL;
140
141         libcfs_run_upcall(argv);
142 }
143 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
144
145 /* coverity[+kill] */
146 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
147 {
148         libcfs_catastrophe = 1;
149         libcfs_debug_msg(msgdata, "LBUG\n");
150
151         if (in_interrupt()) {
152                 panic("LBUG in interrupt.\n");
153                 /* not reached */
154         }
155
156         dump_stack();
157         if (!libcfs_panic_on_lbug)
158                 libcfs_debug_dumplog();
159         libcfs_run_lbug_upcall(msgdata);
160         if (libcfs_panic_on_lbug)
161                 panic("LBUG");
162         set_task_state(current, TASK_UNINTERRUPTIBLE);
163         while (1)
164                 schedule();
165 }
166 EXPORT_SYMBOL(lbug_with_loc);
167
168 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
169                           void *unused2)
170 {
171         if (libcfs_panic_in_progress)
172                 return 0;
173
174         libcfs_panic_in_progress = 1;
175         mb();
176
177         return 0;
178 }
179
180 static struct notifier_block libcfs_panic_notifier = {
181         .notifier_call  = panic_notifier,
182         .next           = NULL,
183         .priority       = 10000,
184 };
185
186 void libcfs_register_panic_notifier(void)
187 {
188         atomic_notifier_chain_register(&panic_notifier_list,
189                                        &libcfs_panic_notifier);
190 }
191
192 void libcfs_unregister_panic_notifier(void)
193 {
194         atomic_notifier_chain_unregister(&panic_notifier_list,
195                                          &libcfs_panic_notifier);
196 }