Whamcloud - gitweb
LU-14550 libcfs: fix setting of debug_path
[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 /* We need to pass a pointer here, but elsewhere this must be a const */
254 char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
255 EXPORT_SYMBOL(libcfs_debug_file_path);
256 module_param(libcfs_debug_file_path, charp, 0644);
257 MODULE_PARM_DESC(libcfs_debug_file_path,
258                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
259
260 int libcfs_panic_in_progress;
261
262 /* libcfs_debug_token2mask() expects the returned string in lower-case */
263 static const char *libcfs_debug_subsys2str(int subsys)
264 {
265         static const char * const libcfs_debug_subsystems[] =
266                 LIBCFS_DEBUG_SUBSYS_NAMES;
267
268         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
269                 return NULL;
270
271         return libcfs_debug_subsystems[subsys];
272 }
273
274 /* libcfs_debug_token2mask() expects the returned string in lower-case */
275 static const char *libcfs_debug_dbg2str(int debug)
276 {
277         static const char * const libcfs_debug_masks[] =
278                 LIBCFS_DEBUG_MASKS_NAMES;
279
280         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
281                 return NULL;
282
283         return libcfs_debug_masks[debug];
284 }
285
286 int
287 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
288 {
289         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
290                                                  libcfs_debug_dbg2str;
291         int len = 0;
292         const char *token;
293         int i;
294
295         if (mask == 0) {                        /* "0" */
296                 if (size > 0)
297                         str[0] = '0';
298                 len = 1;
299         } else {                                /* space-separated tokens */
300                 for (i = 0; i < 32; i++) {
301                         if ((mask & BIT(i)) == 0)
302                                 continue;
303
304                         token = fn(i);
305                         if (!token)     /* unused bit */
306                                 continue;
307
308                         if (len > 0) {          /* separator? */
309                                 if (len < size)
310                                         str[len] = ' ';
311                                 len++;
312                         }
313
314                         while (*token != 0) {
315                                 if (len < size)
316                                         str[len] = *token;
317                                 token++;
318                                 len++;
319                         }
320                 }
321         }
322
323         /* terminate 'str' */
324         if (len < size)
325                 str[len] = 0;
326         else
327                 str[size - 1] = 0;
328
329         return len;
330 }
331
332 int
333 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
334 {
335         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
336                                                  libcfs_debug_dbg2str;
337         int m = 0;
338         int matched;
339         int n;
340         int t;
341
342         /* Allow a number for backwards compatibility */
343         for (n = strlen(str); n > 0; n--)
344                 if (!isspace(str[n-1]))
345                         break;
346         matched = n;
347         t = sscanf(str, "%i%n", &m, &matched);
348         if (t >= 1 && matched == n) {
349                 /* don't print warning for lctl set_param debug=0 or -1 */
350                 if (m != 0 && m != -1)
351                         CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
352                 *mask = m;
353                 return 0;
354         }
355
356         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
357                             0xffffffff);
358 }
359
360 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
361
362 /* Upcall function once a Lustre log has been dumped.
363  *
364  * @file        path of the dumped log
365  */
366 static void libcfs_run_debug_log_upcall(char *file)
367 {
368         char *argv[3];
369         int rc;
370         static const char * const envp[] = {
371                 "HOME=/",
372                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
373                 NULL
374         };
375
376         ENTRY;
377         argv[0] = lnet_debug_log_upcall;
378
379         LASSERTF(file, "called on a null filename\n");
380         argv[1] = file; /* only need to pass the path of the file */
381
382         argv[2] = NULL;
383
384         rc = call_usermodehelper(argv[0], argv, (char **)envp, 1);
385         if (rc < 0 && rc != -ENOENT) {
386                 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
387                        rc, argv[0], argv[1]);
388         } else {
389                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
390                        argv[0], argv[1]);
391         }
392 }
393
394 /**
395  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
396  */
397 static void libcfs_debug_dumplog_internal(void *arg)
398 {
399         static time64_t last_dump_time;
400         time64_t current_time;
401
402         current_time = ktime_get_real_seconds();
403
404         if (strncmp(libcfs_debug_file_path, "NONE", 4) != 0 &&
405             current_time > last_dump_time) {
406                 last_dump_time = current_time;
407                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
408                          "%s.%lld.%ld", libcfs_debug_file_path,
409                          (s64)current_time, (uintptr_t)arg);
410                 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
411                 cfs_tracefile_dump_all_pages(debug_file_name);
412                 libcfs_run_debug_log_upcall(debug_file_name);
413         }
414 }
415
416 static int libcfs_debug_dumplog_thread(void *arg)
417 {
418         libcfs_debug_dumplog_internal(arg);
419         complete(&debug_complete);
420         return 0;
421 }
422
423 static DEFINE_MUTEX(libcfs_debug_dumplog_lock);
424
425 void libcfs_debug_dumplog(void)
426 {
427         struct task_struct *dumper;
428
429         ENTRY;
430
431         if (mutex_trylock(&libcfs_debug_dumplog_lock) == 0)
432                 return;
433
434         /* If a previous call was interrupted, debug_complete->done
435          * might be elevated, and so we won't actually wait here.
436          * So we reinit the completion to ensure we wait for
437          * one thread to complete, though it might not be the one
438          * we start if there are overlaping thread.
439          */
440         reinit_completion(&debug_complete);
441         dumper = kthread_run(libcfs_debug_dumplog_thread,
442                              (void *)(long)current->pid,
443                              "libcfs_debug_dumper");
444         if (IS_ERR(dumper))
445                 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
446                        PTR_ERR(dumper));
447         else
448                 wait_for_completion_interruptible(&debug_complete);
449
450         mutex_unlock(&libcfs_debug_dumplog_lock);
451 }
452 EXPORT_SYMBOL(libcfs_debug_dumplog);
453
454 /* coverity[+kill] */
455 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
456 {
457         libcfs_catastrophe = 1;
458         libcfs_debug_msg(msgdata, "LBUG\n");
459
460         if (in_interrupt()) {
461                 panic("LBUG in interrupt.\n");
462                 /* not reached */
463         }
464
465         libcfs_debug_dumpstack(NULL);
466         if (libcfs_panic_on_lbug)
467                 panic("LBUG");
468         else
469                 libcfs_debug_dumplog();
470         set_current_state(TASK_UNINTERRUPTIBLE);
471         while (1)
472                 schedule();
473 }
474 EXPORT_SYMBOL(lbug_with_loc);
475
476 #ifdef CONFIG_STACKTRACE
477
478 #ifndef HAVE_SAVE_STACK_TRACE_TSK
479 #define save_stack_trace_tsk(tsk, trace)                                       \
480 do {                                                                           \
481         if (tsk == current)                                                    \
482                 save_stack_trace(trace);                                       \
483         else                                                                   \
484                 pr_info("No stack, save_stack_trace_tsk() not exported\n");    \
485 } while (0)
486 #endif
487
488 static void cfs_print_stack_trace(unsigned long *entries, unsigned int nr)
489 {
490         unsigned int i;
491
492         /* Prefer %pB for backtraced symbolic names since it was added in:
493          * Linux v2.6.38-6557-g0f77a8d37825
494          * vsprintf: Introduce %pB format specifier
495          */
496         for (i = 0; i < nr; i++)
497                 pr_info("[<0>] %pB\n", (void *)entries[i]);
498 }
499
500 #define MAX_ST_ENTRIES  100
501 static DEFINE_SPINLOCK(st_lock);
502
503 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
504  * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
505  * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
506  */
507 #ifdef CONFIG_ARCH_STACKWALK
508 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
509                                               unsigned long *store,
510                                               unsigned int size,
511                                               unsigned int skipnr);
512 static stack_trace_save_tsk_t *task_dump_stack;
513 #endif
514
515 void __init cfs_debug_init(void)
516 {
517 #ifdef CONFIG_ARCH_STACKWALK
518         task_dump_stack = (void *)
519                         kallsyms_lookup_name("stack_trace_save_tsk");
520
521 #endif
522 }
523
524 static void libcfs_call_trace(struct task_struct *tsk)
525 {
526         static unsigned long entries[MAX_ST_ENTRIES];
527 #ifdef CONFIG_ARCH_STACKWALK
528         unsigned int nr_entries;
529
530         spin_lock(&st_lock);
531         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
532                 init_utsname()->release, init_utsname()->version);
533         pr_info("Call Trace TBD:\n");
534         if (task_dump_stack) {
535                 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
536                 cfs_print_stack_trace(entries, nr_entries);
537         }
538         spin_unlock(&st_lock);
539 #else
540         struct stack_trace trace;
541
542         trace.nr_entries = 0;
543         trace.max_entries = MAX_ST_ENTRIES;
544         trace.entries = entries;
545         trace.skip = 0;
546
547         spin_lock(&st_lock);
548         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
549                 init_utsname()->release, init_utsname()->version);
550         pr_info("Call Trace:\n");
551         save_stack_trace_tsk(tsk, &trace);
552         cfs_print_stack_trace(trace.entries, trace.nr_entries);
553         spin_unlock(&st_lock);
554 #endif
555 }
556
557 #else /* !CONFIG_STACKTRACE */
558
559 #ifdef CONFIG_X86
560 #include <linux/nmi.h>
561 #include <asm/stacktrace.h>
562
563 #ifdef HAVE_STACKTRACE_OPS
564 static int print_trace_stack(void *data, char *name)
565 {
566         printk(" <%s> ", name);
567         return 0;
568 }
569
570 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
571 static int
572 #else
573 static void
574 #endif
575 print_trace_address(void *data, unsigned long addr, int reliable)
576 {
577         char fmt[32];
578
579         touch_nmi_watchdog();
580         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "" : "? ");
581         __print_symbol(fmt, addr);
582 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
583         return 0;
584 #endif
585 }
586
587 static const struct stacktrace_ops print_trace_ops = {
588         .stack          = print_trace_stack,
589         .address        = print_trace_address,
590         .walk_stack     = print_context_stack,
591 };
592 #endif /* HAVE_STACKTRACE_OPS */
593
594 static void libcfs_call_trace(struct task_struct *tsk)
595 {
596 #ifdef HAVE_STACKTRACE_OPS
597         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
598         printk("\nCall Trace:\n");
599         dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
600         printk("\n");
601 #else /* !HAVE_STACKTRACE_OPS */
602         if (tsk == current)
603                 dump_stack();
604         else
605                 CWARN("can't show stack: kernel doesn't export show_task\n");
606 #endif /* HAVE_STACKTRACE_OPS */
607 }
608
609 #else /* !CONFIG_X86 */
610
611 static void libcfs_call_trace(struct task_struct *tsk)
612 {
613         if (tsk == current)
614                 dump_stack();
615         else
616                 CWARN("can't show stack: kernel doesn't export show_task\n");
617 }
618
619 #endif /* CONFIG_X86 */
620
621 #endif /* CONFIG_STACKTRACE */
622
623 void libcfs_debug_dumpstack(struct task_struct *tsk)
624 {
625         libcfs_call_trace(tsk ?: current);
626 }
627 EXPORT_SYMBOL(libcfs_debug_dumpstack);
628
629 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
630                           void *unused2)
631 {
632         if (libcfs_panic_in_progress)
633                 return 0;
634
635         libcfs_panic_in_progress = 1;
636         mb();
637
638 #ifdef LNET_DUMP_ON_PANIC
639         /* This is currently disabled because it spews far too much to the
640          * console on the rare cases it is ever triggered. */
641
642         if (in_interrupt()) {
643                 cfs_trace_debug_print();
644         } else {
645                 libcfs_debug_dumplog_internal((void *)(long)current->pid);
646         }
647 #endif
648         return 0;
649 }
650
651 static struct notifier_block libcfs_panic_notifier = {
652         .notifier_call          = panic_notifier,
653         .next                   = NULL,
654         .priority               = 10000,
655 };
656
657 static void libcfs_register_panic_notifier(void)
658 {
659         atomic_notifier_chain_register(&panic_notifier_list,
660                                        &libcfs_panic_notifier);
661 }
662
663 static void libcfs_unregister_panic_notifier(void)
664 {
665         atomic_notifier_chain_unregister(&panic_notifier_list,
666                                          &libcfs_panic_notifier);
667 }
668
669 int libcfs_debug_init(unsigned long bufsize)
670 {
671         unsigned int max = libcfs_debug_mb;
672         int rc = 0;
673
674         if (libcfs_console_max_delay <= 0 || /* not set by user or */
675             libcfs_console_min_delay <= 0 || /* set to invalid values */
676             libcfs_console_min_delay >= libcfs_console_max_delay) {
677                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
678                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
679         }
680
681         /* If libcfs_debug_mb is uninitialized then just make the
682          * total buffers smp_num_cpus * TCD_MAX_PAGES
683          */
684         if (max < num_possible_cpus()) {
685                 max = TCD_MAX_PAGES;
686         } else {
687                 max = (max / num_possible_cpus());
688                 max <<= (20 - PAGE_SHIFT);
689         }
690
691         rc = cfs_tracefile_init(max);
692         if (rc)
693                 return rc;
694
695         libcfs_register_panic_notifier();
696         kernel_param_lock(THIS_MODULE);
697         if (libcfs_debug_mb == 0)
698                 libcfs_debug_mb = cfs_trace_get_debug_mb();
699         kernel_param_unlock(THIS_MODULE);
700         return rc;
701 }
702
703 int libcfs_debug_cleanup(void)
704 {
705         libcfs_unregister_panic_notifier();
706         kernel_param_lock(THIS_MODULE);
707         cfs_tracefile_exit();
708         kernel_param_unlock(THIS_MODULE);
709         return 0;
710 }
711
712 int libcfs_debug_clear_buffer(void)
713 {
714         cfs_trace_flush_pages();
715         return 0;
716 }
717
718 /* Debug markers, although printed by S_LNET should not be be marked as such. */
719 #undef DEBUG_SUBSYSTEM
720 #define DEBUG_SUBSYSTEM S_UNDEFINED
721 int libcfs_debug_mark_buffer(const char *text)
722 {
723         CDEBUG(D_TRACE,
724                "**************************************************\n");
725         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
726         CDEBUG(D_TRACE,
727                "**************************************************\n");
728
729         return 0;
730 }
731
732 #undef DEBUG_SUBSYSTEM
733 #define DEBUG_SUBSYSTEM S_LNET
734
735 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
736 {
737         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
738                          rc, rc, rc);
739         return rc;
740 }
741 EXPORT_SYMBOL(libcfs_log_return);
742
743 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
744                      long rc)
745 {
746         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
747                          " : %#lx)\n", label, rc, rc, rc);
748 }
749 EXPORT_SYMBOL(libcfs_log_goto);