Whamcloud - gitweb
d82b2c19acce4ffd14d05f442ca08bb12cabd3da
[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/module.h>
41 #include <linux/ctype.h>
42 #include <libcfs/libcfs_string.h>
43 #include <linux/kthread.h>
44 #include <linux/stacktrace.h>
45 #include <linux/utsname.h>
46 #include <linux/kallsyms.h>
47 #include "tracefile.h"
48
49 static char debug_file_name[1024];
50
51 unsigned int libcfs_subsystem_debug = ~0;
52 EXPORT_SYMBOL(libcfs_subsystem_debug);
53 module_param(libcfs_subsystem_debug, int, 0644);
54 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
55
56 unsigned int libcfs_debug = (D_CANTMASK | D_NETERROR | D_HA | D_CONFIG |
57                              D_IOCTL | D_LFSCK | D_TTY);
58 EXPORT_SYMBOL(libcfs_debug);
59 module_param(libcfs_debug, int, 0644);
60 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
61
62 static int libcfs_param_debug_mb_set(const char *val,
63                                      cfs_kernel_param_arg_t *kp)
64 {
65         int rc;
66         unsigned int num;
67
68         rc = kstrtouint(val, 0, &num);
69         if (rc < 0)
70                 return rc;
71
72         num = cfs_trace_set_debug_mb(num);
73
74         *((unsigned int *)kp->arg) = num;
75         num = cfs_trace_get_debug_mb();
76         if (num)
77                 /* This value is more precise */
78                 *((unsigned int *)kp->arg) = num;
79
80         return 0;
81 }
82
83 /* While debug_mb setting look like unsigned int, in fact
84  * it needs quite a bunch of extra processing, so we define special
85  * debug_mb parameter type with corresponding methods to handle this case
86  */
87 static const struct kernel_param_ops param_ops_debug_mb = {
88         .set = libcfs_param_debug_mb_set,
89         .get = param_get_uint,
90 };
91
92 #define param_check_debug_mb(name, p) \
93                 __param_check(name, p, unsigned int)
94
95 static unsigned int libcfs_debug_mb;
96 #ifdef HAVE_KERNEL_PARAM_OPS
97 module_param(libcfs_debug_mb, debug_mb, 0644);
98 #else
99 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
100                   &param_ops_debug_mb, 0644);
101 #endif
102 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
103
104 unsigned int libcfs_printk = D_CANTMASK;
105 module_param(libcfs_printk, uint, 0644);
106 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
107
108 unsigned int libcfs_console_ratelimit = 1;
109 module_param(libcfs_console_ratelimit, uint, 0644);
110 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
111
112 static int param_set_delay_minmax(const char *val,
113                                   cfs_kernel_param_arg_t *kp,
114                                   long min, long max)
115 {
116         long d;
117         int sec;
118         int rc;
119
120         rc = kstrtoint(val, 0, &sec);
121         if (rc)
122                 return -EINVAL;
123
124         /* The sysfs setting is in centiseconds */
125         d = cfs_time_seconds(sec) / 100;
126         if (d < min || d > max)
127                 return -EINVAL;
128
129         *((unsigned int *)kp->arg) = d;
130
131         return 0;
132 }
133
134 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
135 {
136         unsigned int d = *(unsigned int *)kp->arg;
137
138         param_get_byte(buffer, kp);
139         return sprintf(buffer, "%lu%c", jiffies_to_msecs(d * 10) / MSEC_PER_SEC,
140                        strnchr(buffer, PAGE_SIZE, '\n') ? '\n' : '\0');
141 }
142
143 unsigned int libcfs_console_max_delay;
144 unsigned int libcfs_console_min_delay;
145
146 static int param_set_console_max_delay(const char *val,
147                                        cfs_kernel_param_arg_t *kp)
148 {
149         return param_set_delay_minmax(val, kp,
150                                       libcfs_console_min_delay, INT_MAX);
151 }
152
153 static const struct kernel_param_ops param_ops_console_max_delay = {
154         .set = param_set_console_max_delay,
155         .get = param_get_delay,
156 };
157
158 #define param_check_console_max_delay(name, p) \
159                 __param_check(name, p, unsigned int)
160
161 #ifdef HAVE_KERNEL_PARAM_OPS
162 module_param(libcfs_console_max_delay, console_max_delay, 0644);
163 #else
164 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
165                   param_get_delay, &param_ops_console_max_delay, 0644);
166 #endif
167 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
168
169 static int param_set_console_min_delay(const char *val,
170                                        cfs_kernel_param_arg_t *kp)
171 {
172         return param_set_delay_minmax(val, kp,
173                                       1, libcfs_console_max_delay);
174 }
175
176 static const struct kernel_param_ops param_ops_console_min_delay = {
177         .set = param_set_console_min_delay,
178         .get = param_get_delay,
179 };
180
181 #define param_check_console_min_delay(name, p) \
182                 __param_check(name, p, unsigned int)
183
184 #ifdef HAVE_KERNEL_PARAM_OPS
185 module_param(libcfs_console_min_delay, console_min_delay, 0644);
186 #else
187 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
188                   param_get_delay, &param_ops_console_min_delay, 0644);
189 #endif
190 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
191
192 static int param_set_uint_minmax(const char *val,
193                                  cfs_kernel_param_arg_t *kp,
194                                  unsigned int min, unsigned int max)
195 {
196         unsigned int num;
197         int ret;
198
199         if (!val)
200                 return -EINVAL;
201
202         ret = kstrtouint(val, 0, &num);
203         if (ret < 0 || num < min || num > max)
204                 return -EINVAL;
205
206         *((unsigned int *)kp->arg) = num;
207         return 0;
208 }
209
210 static int param_set_uintpos(const char *val,
211                              cfs_kernel_param_arg_t *kp)
212 {
213         return param_set_uint_minmax(val, kp, 1, -1);
214 }
215
216 static const struct kernel_param_ops param_ops_uintpos = {
217         .set = param_set_uintpos,
218         .get = param_get_uint,
219 };
220
221 #define param_check_uintpos(name, p) \
222                 __param_check(name, p, unsigned int)
223
224 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
225 #ifdef HAVE_KERNEL_PARAM_OPS
226 module_param(libcfs_console_backoff, uintpos, 0644);
227 #else
228 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
229                   &param_ops_uintpos, 0644);
230 #endif
231 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
232
233 unsigned int libcfs_debug_binary = 1;
234
235 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
236 EXPORT_SYMBOL(libcfs_stack);
237
238 unsigned int libcfs_catastrophe;
239 EXPORT_SYMBOL(libcfs_catastrophe);
240
241 unsigned int libcfs_watchdog_ratelimit = 300;
242 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
243
244 unsigned int libcfs_panic_on_lbug = 1;
245 module_param(libcfs_panic_on_lbug, uint, 0644);
246 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
247
248 atomic64_t libcfs_kmem = ATOMIC64_INIT(0);
249 EXPORT_SYMBOL(libcfs_kmem);
250
251 static DECLARE_COMPLETION(debug_complete);
252
253 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
254 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
255
256 /* We need to pass a pointer here, but elsewhere this must be a const */
257 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
258 module_param(libcfs_debug_file_path, charp, 0644);
259 MODULE_PARM_DESC(libcfs_debug_file_path,
260                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
261
262 int libcfs_panic_in_progress;
263
264 /* libcfs_debug_token2mask() expects the returned string in lower-case */
265 static const char *libcfs_debug_subsys2str(int subsys)
266 {
267         static const char * const libcfs_debug_subsystems[] =
268                 LIBCFS_DEBUG_SUBSYS_NAMES;
269
270         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
271                 return NULL;
272
273         return libcfs_debug_subsystems[subsys];
274 }
275
276 /* libcfs_debug_token2mask() expects the returned string in lower-case */
277 static const char *libcfs_debug_dbg2str(int debug)
278 {
279         static const char * const libcfs_debug_masks[] =
280                 LIBCFS_DEBUG_MASKS_NAMES;
281
282         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
283                 return NULL;
284
285         return libcfs_debug_masks[debug];
286 }
287
288 int
289 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
290 {
291         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
292                                                  libcfs_debug_dbg2str;
293         int len = 0;
294         const char *token;
295         int i;
296
297         if (mask == 0) {                        /* "0" */
298                 if (size > 0)
299                         str[0] = '0';
300                 len = 1;
301         } else {                                /* space-separated tokens */
302                 for (i = 0; i < 32; i++) {
303                         if ((mask & BIT(i)) == 0)
304                                 continue;
305
306                         token = fn(i);
307                         if (!token)     /* unused bit */
308                                 continue;
309
310                         if (len > 0) {          /* separator? */
311                                 if (len < size)
312                                         str[len] = ' ';
313                                 len++;
314                         }
315
316                         while (*token != 0) {
317                                 if (len < size)
318                                         str[len] = *token;
319                                 token++;
320                                 len++;
321                         }
322                 }
323         }
324
325         /* terminate 'str' */
326         if (len < size)
327                 str[len] = 0;
328         else
329                 str[size - 1] = 0;
330
331         return len;
332 }
333
334 int
335 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
336 {
337         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
338                                                  libcfs_debug_dbg2str;
339         int m = 0;
340         int matched;
341         int n;
342         int t;
343
344         /* Allow a number for backwards compatibility */
345         for (n = strlen(str); n > 0; n--)
346                 if (!isspace(str[n-1]))
347                         break;
348         matched = n;
349         t = sscanf(str, "%i%n", &m, &matched);
350         if (t >= 1 && matched == n) {
351                 /* don't print warning for lctl set_param debug=0 or -1 */
352                 if (m != 0 && m != -1)
353                         CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
354                 *mask = m;
355                 return 0;
356         }
357
358         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
359                             0xffffffff);
360 }
361
362 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
363
364 /* Upcall function once a Lustre log has been dumped.
365  *
366  * @file        path of the dumped log
367  */
368 static void libcfs_run_debug_log_upcall(char *file)
369 {
370         char *argv[3];
371         int rc;
372         static const char * const envp[] = {
373                 "HOME=/",
374                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
375                 NULL
376         };
377
378         ENTRY;
379         argv[0] = lnet_debug_log_upcall;
380
381         LASSERTF(file, "called on a null filename\n");
382         argv[1] = file; /* only need to pass the path of the file */
383
384         argv[2] = NULL;
385
386         rc = call_usermodehelper(argv[0], argv, (char **)envp, 1);
387         if (rc < 0 && rc != -ENOENT) {
388                 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
389                        rc, argv[0], argv[1]);
390         } else {
391                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
392                        argv[0], argv[1]);
393         }
394 }
395
396 /**
397  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
398  */
399 static void libcfs_debug_dumplog_internal(void *arg)
400 {
401         static time64_t last_dump_time;
402         time64_t current_time;
403
404         current_time = ktime_get_real_seconds();
405
406         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
407             current_time > last_dump_time) {
408                 last_dump_time = current_time;
409                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
410                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
411                          (s64)current_time, (uintptr_t)arg);
412                 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
413                 cfs_tracefile_dump_all_pages(debug_file_name);
414                 libcfs_run_debug_log_upcall(debug_file_name);
415         }
416 }
417
418 static int libcfs_debug_dumplog_thread(void *arg)
419 {
420         libcfs_debug_dumplog_internal(arg);
421         complete(&debug_complete);
422         return 0;
423 }
424
425 static DEFINE_MUTEX(libcfs_debug_dumplog_lock);
426
427 void libcfs_debug_dumplog(void)
428 {
429         struct task_struct *dumper;
430
431         ENTRY;
432
433         if (mutex_trylock(&libcfs_debug_dumplog_lock) == 0)
434                 return;
435
436         /* If a previous call was interrupted, debug_complete->done
437          * might be elevated, and so we won't actually wait here.
438          * So we reinit the completion to ensure we wait for
439          * one thread to complete, though it might not be the one
440          * we start if there are overlaping thread.
441          */
442         reinit_completion(&debug_complete);
443         dumper = kthread_run(libcfs_debug_dumplog_thread,
444                              (void *)(long)current->pid,
445                              "libcfs_debug_dumper");
446         if (IS_ERR(dumper))
447                 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
448                        PTR_ERR(dumper));
449         else
450                 wait_for_completion_interruptible(&debug_complete);
451
452         mutex_unlock(&libcfs_debug_dumplog_lock);
453 }
454 EXPORT_SYMBOL(libcfs_debug_dumplog);
455
456 /* coverity[+kill] */
457 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
458 {
459         libcfs_catastrophe = 1;
460         libcfs_debug_msg(msgdata, "LBUG\n");
461
462         if (in_interrupt()) {
463                 panic("LBUG in interrupt.\n");
464                 /* not reached */
465         }
466
467         libcfs_debug_dumpstack(NULL);
468         if (libcfs_panic_on_lbug)
469                 panic("LBUG");
470         else
471                 libcfs_debug_dumplog();
472         set_current_state(TASK_UNINTERRUPTIBLE);
473         while (1)
474                 schedule();
475 }
476 EXPORT_SYMBOL(lbug_with_loc);
477
478 #ifdef CONFIG_STACKTRACE
479
480 #ifndef HAVE_SAVE_STACK_TRACE_TSK
481 #define save_stack_trace_tsk(tsk, trace)                                       \
482 do {                                                                           \
483         if (tsk == current)                                                    \
484                 save_stack_trace(trace);                                       \
485         else                                                                   \
486                 pr_info("No stack, save_stack_trace_tsk() not exported\n");    \
487 } while (0)
488 #endif
489
490 static void cfs_print_stack_trace(unsigned long *entries, unsigned int nr)
491 {
492         unsigned int i;
493
494         /* Prefer %pB for backtraced symbolic names since it was added in:
495          * Linux v2.6.38-6557-g0f77a8d37825
496          * vsprintf: Introduce %pB format specifier
497          */
498         for (i = 0; i < nr; i++)
499                 pr_info("[<0>] %pB\n", (void *)entries[i]);
500 }
501
502 #define MAX_ST_ENTRIES  100
503 static DEFINE_SPINLOCK(st_lock);
504
505 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
506  * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
507  * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
508  */
509 #ifdef CONFIG_ARCH_STACKWALK
510 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
511                                               unsigned long *store,
512                                               unsigned int size,
513                                               unsigned int skipnr);
514 static stack_trace_save_tsk_t *task_dump_stack;
515 #endif
516
517 void __init cfs_debug_init(void)
518 {
519 #ifdef CONFIG_ARCH_STACKWALK
520         task_dump_stack = (void *)
521                         kallsyms_lookup_name("stack_trace_save_tsk");
522
523 #endif
524 }
525
526 static void libcfs_call_trace(struct task_struct *tsk)
527 {
528         static unsigned long entries[MAX_ST_ENTRIES];
529 #ifdef CONFIG_ARCH_STACKWALK
530         unsigned int nr_entries;
531
532         spin_lock(&st_lock);
533         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
534                 init_utsname()->release, init_utsname()->version);
535         pr_info("Call Trace TBD:\n");
536         if (task_dump_stack) {
537                 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
538                 cfs_print_stack_trace(entries, nr_entries);
539         }
540         spin_unlock(&st_lock);
541 #else
542         struct stack_trace trace;
543
544         trace.nr_entries = 0;
545         trace.max_entries = MAX_ST_ENTRIES;
546         trace.entries = entries;
547         trace.skip = 0;
548
549         spin_lock(&st_lock);
550         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
551                 init_utsname()->release, init_utsname()->version);
552         pr_info("Call Trace:\n");
553         save_stack_trace_tsk(tsk, &trace);
554         cfs_print_stack_trace(trace.entries, trace.nr_entries);
555         spin_unlock(&st_lock);
556 #endif
557 }
558
559 #else /* !CONFIG_STACKTRACE */
560
561 #ifdef CONFIG_X86
562 #include <linux/nmi.h>
563 #include <asm/stacktrace.h>
564
565 #ifdef HAVE_STACKTRACE_OPS
566 static int print_trace_stack(void *data, char *name)
567 {
568         printk(" <%s> ", name);
569         return 0;
570 }
571
572 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
573 static int
574 #else
575 static void
576 #endif
577 print_trace_address(void *data, unsigned long addr, int reliable)
578 {
579         char fmt[32];
580
581         touch_nmi_watchdog();
582         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "" : "? ");
583         __print_symbol(fmt, addr);
584 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
585         return 0;
586 #endif
587 }
588
589 static const struct stacktrace_ops print_trace_ops = {
590         .stack          = print_trace_stack,
591         .address        = print_trace_address,
592         .walk_stack     = print_context_stack,
593 };
594 #endif /* HAVE_STACKTRACE_OPS */
595
596 static void libcfs_call_trace(struct task_struct *tsk)
597 {
598 #ifdef HAVE_STACKTRACE_OPS
599         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
600         printk("\nCall Trace:\n");
601         dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
602         printk("\n");
603 #else /* !HAVE_STACKTRACE_OPS */
604         if (tsk == current)
605                 dump_stack();
606         else
607                 CWARN("can't show stack: kernel doesn't export show_task\n");
608 #endif /* HAVE_STACKTRACE_OPS */
609 }
610
611 #else /* !CONFIG_X86 */
612
613 static void libcfs_call_trace(struct task_struct *tsk)
614 {
615         if (tsk == current)
616                 dump_stack();
617         else
618                 CWARN("can't show stack: kernel doesn't export show_task\n");
619 }
620
621 #endif /* CONFIG_X86 */
622
623 #endif /* CONFIG_STACKTRACE */
624
625 void libcfs_debug_dumpstack(struct task_struct *tsk)
626 {
627         libcfs_call_trace(tsk ?: current);
628 }
629 EXPORT_SYMBOL(libcfs_debug_dumpstack);
630
631 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
632                           void *unused2)
633 {
634         if (libcfs_panic_in_progress)
635                 return 0;
636
637         libcfs_panic_in_progress = 1;
638         mb();
639
640 #ifdef LNET_DUMP_ON_PANIC
641         /* This is currently disabled because it spews far too much to the
642          * console on the rare cases it is ever triggered. */
643
644         if (in_interrupt()) {
645                 cfs_trace_debug_print();
646         } else {
647                 libcfs_debug_dumplog_internal((void *)(long)current->pid);
648         }
649 #endif
650         return 0;
651 }
652
653 static struct notifier_block libcfs_panic_notifier = {
654         .notifier_call          = panic_notifier,
655         .next                   = NULL,
656         .priority               = 10000,
657 };
658
659 static void libcfs_register_panic_notifier(void)
660 {
661         atomic_notifier_chain_register(&panic_notifier_list,
662                                        &libcfs_panic_notifier);
663 }
664
665 static void libcfs_unregister_panic_notifier(void)
666 {
667         atomic_notifier_chain_unregister(&panic_notifier_list,
668                                          &libcfs_panic_notifier);
669 }
670
671 int libcfs_debug_init(unsigned long bufsize)
672 {
673         unsigned int max = libcfs_debug_mb;
674         int rc = 0;
675
676         if (libcfs_console_max_delay <= 0 || /* not set by user or */
677             libcfs_console_min_delay <= 0 || /* set to invalid values */
678             libcfs_console_min_delay >= libcfs_console_max_delay) {
679                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
680                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
681         }
682
683         if (libcfs_debug_file_path) {
684                 strlcpy(libcfs_debug_file_path_arr,
685                         libcfs_debug_file_path,
686                         sizeof(libcfs_debug_file_path_arr));
687         }
688
689         /* If libcfs_debug_mb is uninitialized then just make the
690          * total buffers smp_num_cpus * TCD_MAX_PAGES
691          */
692         if (max < num_possible_cpus()) {
693                 max = TCD_MAX_PAGES;
694         } else {
695                 max = (max / num_possible_cpus());
696                 max <<= (20 - PAGE_SHIFT);
697         }
698
699         rc = cfs_tracefile_init(max);
700         if (rc)
701                 return rc;
702
703         libcfs_register_panic_notifier();
704         kernel_param_lock(THIS_MODULE);
705         if (libcfs_debug_mb == 0)
706                 libcfs_debug_mb = cfs_trace_get_debug_mb();
707         kernel_param_unlock(THIS_MODULE);
708         return rc;
709 }
710
711 int libcfs_debug_cleanup(void)
712 {
713         libcfs_unregister_panic_notifier();
714         kernel_param_lock(THIS_MODULE);
715         cfs_tracefile_exit();
716         kernel_param_unlock(THIS_MODULE);
717         return 0;
718 }
719
720 int libcfs_debug_clear_buffer(void)
721 {
722         cfs_trace_flush_pages();
723         return 0;
724 }
725
726 /* Debug markers, although printed by S_LNET should not be be marked as such. */
727 #undef DEBUG_SUBSYSTEM
728 #define DEBUG_SUBSYSTEM S_UNDEFINED
729 int libcfs_debug_mark_buffer(const char *text)
730 {
731         CDEBUG(D_TRACE,
732                "**************************************************\n");
733         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
734         CDEBUG(D_TRACE,
735                "**************************************************\n");
736
737         return 0;
738 }
739
740 #undef DEBUG_SUBSYSTEM
741 #define DEBUG_SUBSYSTEM S_LNET
742
743 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
744 {
745         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
746                          rc, rc, rc);
747         return rc;
748 }
749 EXPORT_SYMBOL(libcfs_log_return);
750
751 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
752                      long rc)
753 {
754         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
755                          " : %#lx)\n", label, rc, rc, rc);
756 }
757 EXPORT_SYMBOL(libcfs_log_goto);