Whamcloud - gitweb
LU-12477 libcfs: Remove obsolete config checks
[fs/lustre-release.git] / libcfs / libcfs / 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) 2011, 2017, 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/debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  *
36  */
37
38 # define DEBUG_SUBSYSTEM S_LNET
39
40 #include <linux/ctype.h>
41 #include <linux/kthread.h>
42 #include <libcfs/libcfs.h>
43 #include "tracefile.h"
44
45 static char debug_file_name[1024];
46
47 unsigned int libcfs_subsystem_debug = ~0;
48 module_param(libcfs_subsystem_debug, int, 0644);
49 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
50 EXPORT_SYMBOL(libcfs_subsystem_debug);
51
52 unsigned int libcfs_debug = (D_CANTMASK | D_NETERROR | D_HA | D_CONFIG |
53                              D_IOCTL | D_LFSCK | D_TTY);
54 module_param(libcfs_debug, int, 0644);
55 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
56 EXPORT_SYMBOL(libcfs_debug);
57
58 static int libcfs_param_debug_mb_set(const char *val,
59                                      cfs_kernel_param_arg_t *kp)
60 {
61         int rc;
62         unsigned int num;
63
64         rc = kstrtouint(val, 0, &num);
65         if (rc < 0)
66                 return rc;
67
68 /*
69  * RHEL6 does not support any kind of locking so we have to provide
70  * our own
71  */
72         if (!*((unsigned int *)kp->arg)) {
73                 *((unsigned int *)kp->arg) = num;
74                 return 0;
75         }
76
77         rc = cfs_trace_set_debug_mb(num);
78
79         if (!rc)
80                 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
81
82         return rc;
83 }
84
85 /*
86  * While debug_mb setting look like unsigned int, in fact
87  * it needs quite a bunch of extra processing, so we define special
88  * debug_mb parameter type with corresponding methods to handle this case
89  */
90 static struct kernel_param_ops param_ops_debug_mb = {
91         .set = libcfs_param_debug_mb_set,
92         .get = param_get_uint,
93 };
94
95 #define param_check_debug_mb(name, p) \
96                 __param_check(name, p, unsigned int)
97
98 static unsigned int libcfs_debug_mb;
99 #ifdef HAVE_KERNEL_PARAM_OPS
100 module_param(libcfs_debug_mb, debug_mb, 0644);
101 #else
102 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
103                   &param_ops_debug_mb, 0644);
104 #endif
105 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
106
107 unsigned int libcfs_printk = D_CANTMASK;
108 module_param(libcfs_printk, uint, 0644);
109 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
110
111 unsigned int libcfs_console_ratelimit = 1;
112 module_param(libcfs_console_ratelimit, uint, 0644);
113 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
114
115 static int param_set_delay_minmax(const char *val,
116                                   cfs_kernel_param_arg_t *kp,
117                                   long min, long max)
118 {
119         long d;
120         int sec;
121         int rc;
122
123         rc = kstrtoint(val, 0, &sec);
124         if (rc)
125                 return -EINVAL;
126
127         /* The sysfs setting is in centiseconds */
128         d = cfs_time_seconds(sec) / 100;
129         if (d < min || d > max)
130                 return -EINVAL;
131
132         *((unsigned int *)kp->arg) = d;
133
134         return 0;
135 }
136
137 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
138 {
139         unsigned int d = *(unsigned int *)kp->arg;
140
141         return sprintf(buffer, "%lu", jiffies_to_msecs(d * 10) / MSEC_PER_SEC);
142 }
143
144 unsigned int libcfs_console_max_delay;
145 unsigned int libcfs_console_min_delay;
146
147 static int param_set_console_max_delay(const char *val,
148                                        cfs_kernel_param_arg_t *kp)
149 {
150         return param_set_delay_minmax(val, kp,
151                                       libcfs_console_min_delay, INT_MAX);
152 }
153
154 static struct kernel_param_ops param_ops_console_max_delay = {
155         .set = param_set_console_max_delay,
156         .get = param_get_delay,
157 };
158
159 #define param_check_console_max_delay(name, p) \
160                 __param_check(name, p, unsigned int)
161
162 #ifdef HAVE_KERNEL_PARAM_OPS
163 module_param(libcfs_console_max_delay, console_max_delay, 0644);
164 #else
165 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
166                   param_get_delay, &param_ops_console_max_delay, 0644);
167 #endif
168 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
169
170 static int param_set_console_min_delay(const char *val,
171                                        cfs_kernel_param_arg_t *kp)
172 {
173         return param_set_delay_minmax(val, kp,
174                                       1, libcfs_console_max_delay);
175 }
176
177 static struct kernel_param_ops param_ops_console_min_delay = {
178         .set = param_set_console_min_delay,
179         .get = param_get_delay,
180 };
181
182 #define param_check_console_min_delay(name, p) \
183                 __param_check(name, p, unsigned int)
184
185 #ifdef HAVE_KERNEL_PARAM_OPS
186 module_param(libcfs_console_min_delay, console_min_delay, 0644);
187 #else
188 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
189                   param_get_delay, &param_ops_console_min_delay, 0644);
190 #endif
191 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
192
193 static int param_set_uint_minmax(const char *val,
194                                  cfs_kernel_param_arg_t *kp,
195                                  unsigned int min, unsigned int max)
196 {
197         unsigned int num;
198         int ret;
199
200         if (!val)
201                 return -EINVAL;
202
203         ret = kstrtouint(val, 0, &num);
204         if (ret < 0 || num < min || num > max)
205                 return -EINVAL;
206
207         *((unsigned int *)kp->arg) = num;
208         return 0;
209 }
210
211 static int param_set_uintpos(const char *val,
212                              cfs_kernel_param_arg_t *kp)
213 {
214         return param_set_uint_minmax(val, kp, 1, -1);
215 }
216
217 static struct kernel_param_ops param_ops_uintpos = {
218         .set = param_set_uintpos,
219         .get = param_get_uint,
220 };
221
222 #define param_check_uintpos(name, p) \
223                 __param_check(name, p, unsigned int)
224
225 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
226 #ifdef HAVE_KERNEL_PARAM_OPS
227 module_param(libcfs_console_backoff, uintpos, 0644);
228 #else
229 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
230                   &param_ops_uintpos, 0644);
231 #endif
232 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
233
234 unsigned int libcfs_debug_binary = 1;
235
236 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
237 EXPORT_SYMBOL(libcfs_stack);
238
239 unsigned int libcfs_catastrophe;
240 EXPORT_SYMBOL(libcfs_catastrophe);
241
242 unsigned int libcfs_watchdog_ratelimit = 300;
243 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
244
245 unsigned int libcfs_panic_on_lbug = 1;
246 module_param(libcfs_panic_on_lbug, uint, 0644);
247 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
248
249 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
250 EXPORT_SYMBOL(libcfs_kmemory);
251
252 static wait_queue_head_t debug_ctlwq;
253
254 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
255 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
256
257 /* We need to pass a pointer here, but elsewhere this must be a const */
258 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
259 module_param(libcfs_debug_file_path, charp, 0644);
260 MODULE_PARM_DESC(libcfs_debug_file_path,
261                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
262
263 int libcfs_panic_in_progress;
264
265 /*
266  * libcfs_debug_token2mask() expects the returned
267  * string in lower-case
268  */
269 static const char *libcfs_debug_subsys2str(int subsys)
270 {
271         static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
272
273         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
274                 return NULL;
275
276         return libcfs_debug_subsystems[subsys];
277 }
278
279 /*
280  * libcfs_debug_token2mask() expects the returned
281  * string in lower-case
282  */
283 static const char *libcfs_debug_dbg2str(int debug)
284 {
285         static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
286
287         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
288                 return NULL;
289
290         return libcfs_debug_masks[debug];
291 }
292
293 int
294 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
295 {
296         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
297                                                  libcfs_debug_dbg2str;
298         int len = 0;
299         const char *token;
300         int i;
301
302         if (mask == 0) {                        /* "0" */
303                 if (size > 0)
304                         str[0] = '0';
305                 len = 1;
306         } else {                                /* space-separated tokens */
307                 for (i = 0; i < 32; i++) {
308                         if ((mask & (1 << i)) == 0)
309                                 continue;
310
311                         token = fn(i);
312                         if (token == NULL)      /* unused bit */
313                                 continue;
314
315                         if (len > 0) {          /* separator? */
316                                 if (len < size)
317                                         str[len] = ' ';
318                                 len++;
319                         }
320
321                         while (*token != 0) {
322                                 if (len < size)
323                                         str[len] = *token;
324                                 token++;
325                                 len++;
326                         }
327                 }
328         }
329
330         /* terminate 'str' */
331         if (len < size)
332                 str[len] = 0;
333         else
334                 str[size - 1] = 0;
335
336         return len;
337 }
338
339 int
340 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
341 {
342         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
343                                                  libcfs_debug_dbg2str;
344         int m = 0;
345         int matched;
346         int n;
347         int t;
348
349         /* Allow a number for backwards compatibility */
350         for (n = strlen(str); n > 0; n--)
351                 if (!isspace(str[n-1]))
352                         break;
353         matched = n;
354
355         t = sscanf(str, "%i%n", &m, &matched);
356         if (t >= 1 && matched == n) {
357                 /* don't print warning for lctl set_param debug=0 or -1 */
358                 if (m != 0 && m != -1)
359                         CWARN("You are trying to use a numerical value for the "
360                               "mask - this will be deprecated in a future "
361                               "release.\n");
362                 *mask = m;
363                 return 0;
364         }
365
366         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
367                             0xffffffff);
368 }
369
370 /**
371  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
372  */
373 void libcfs_debug_dumplog_internal(void *arg)
374 {
375         static time64_t last_dump_time;
376         time64_t current_time;
377         void *journal_info;
378
379         journal_info = current->journal_info;
380         current->journal_info = NULL;
381         current_time = ktime_get_real_seconds();
382
383         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
384             current_time > last_dump_time) {
385                 last_dump_time = current_time;
386                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
387                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
388                          (s64)current_time, (uintptr_t)arg);
389                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
390                        debug_file_name);
391                 cfs_tracefile_dump_all_pages(debug_file_name);
392                 libcfs_run_debug_log_upcall(debug_file_name);
393         }
394         current->journal_info = journal_info;
395 }
396
397 static int libcfs_debug_dumplog_thread(void *arg)
398 {
399         libcfs_debug_dumplog_internal(arg);
400         wake_up(&debug_ctlwq);
401         return 0;
402 }
403
404 void libcfs_debug_dumplog(void)
405 {
406         wait_queue_entry_t wait;
407         struct task_struct *dumper;
408
409         ENTRY;
410
411         /*
412          * we're being careful to ensure that the kernel thread is
413          * able to set our state to running as it exits before we
414          * get to schedule()
415          */
416         init_waitqueue_entry(&wait, current);
417         set_current_state(TASK_INTERRUPTIBLE);
418         add_wait_queue(&debug_ctlwq, &wait);
419
420         dumper = kthread_run(libcfs_debug_dumplog_thread,
421                              (void *)(long)current_pid(),
422                              "libcfs_debug_dumper");
423         if (IS_ERR(dumper))
424                 printk(KERN_ERR "LustreError: cannot start log dump thread:"
425                        " %ld\n", PTR_ERR(dumper));
426         else
427                 schedule();
428
429         /* be sure to teardown if cfs_create_thread() failed */
430         remove_wait_queue(&debug_ctlwq, &wait);
431         set_current_state(TASK_RUNNING);
432 }
433 EXPORT_SYMBOL(libcfs_debug_dumplog);
434
435 int libcfs_debug_init(unsigned long bufsize)
436 {
437         int rc = 0;
438         unsigned int max = libcfs_debug_mb;
439
440         init_waitqueue_head(&debug_ctlwq);
441
442         if (libcfs_console_max_delay <= 0 || /* not set by user or */
443             libcfs_console_min_delay <= 0 || /* set to invalid values */
444             libcfs_console_min_delay >= libcfs_console_max_delay) {
445                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
446                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
447         }
448
449         if (libcfs_debug_file_path != NULL) {
450                 strlcpy(libcfs_debug_file_path_arr,
451                         libcfs_debug_file_path,
452                         sizeof(libcfs_debug_file_path_arr));
453         }
454
455         /*
456          * If libcfs_debug_mb is set to an invalid value or uninitialized
457          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES
458          */
459         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
460                 max = TCD_MAX_PAGES;
461         } else {
462                 max = (max / num_possible_cpus());
463                 max = (max << (20 - PAGE_SHIFT));
464         }
465
466         rc = cfs_tracefile_init(max);
467         if (rc)
468                 return rc;
469
470         libcfs_register_panic_notifier();
471         kernel_param_lock(THIS_MODULE);
472         if (libcfs_debug_mb == 0)
473                 libcfs_debug_mb = cfs_trace_get_debug_mb();
474         kernel_param_unlock(THIS_MODULE);
475         return rc;
476 }
477
478 int libcfs_debug_cleanup(void)
479 {
480         libcfs_unregister_panic_notifier();
481         kernel_param_lock(THIS_MODULE);
482         cfs_tracefile_exit();
483         kernel_param_unlock(THIS_MODULE);
484         return 0;
485 }
486
487 int libcfs_debug_clear_buffer(void)
488 {
489         cfs_trace_flush_pages();
490         return 0;
491 }
492
493 /*
494  * Debug markers, although printed by S_LNET
495  * should not be be marked as such.
496  */
497 #undef DEBUG_SUBSYSTEM
498 #define DEBUG_SUBSYSTEM S_UNDEFINED
499 int libcfs_debug_mark_buffer(const char *text)
500 {
501         CDEBUG(D_TRACE, "**************************************************\n");
502         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
503         CDEBUG(D_TRACE, "**************************************************\n");
504
505         return 0;
506 }
507 #undef DEBUG_SUBSYSTEM
508 #define DEBUG_SUBSYSTEM S_LNET
509
510 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
511 {
512         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
513                          rc, rc, rc);
514         return rc;
515 }
516 EXPORT_SYMBOL(libcfs_log_return);
517
518 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
519                      long rc)
520 {
521         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
522                          " : %#lx)\n", label, rc, rc, rc);
523 }
524 EXPORT_SYMBOL(libcfs_log_goto);