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