Whamcloud - gitweb
Quiet compiler warnings related to passing "const" pointers to non-const
[fs/lustre-release.git] / lnet / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/libcfs/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 # define DEBUG_SUBSYSTEM S_LNET
46
47 #include <stdarg.h>
48 #include <libcfs/kp30.h>
49 #include <libcfs/libcfs.h>
50 #include "tracefile.h"
51
52 static char debug_file_name[1024];
53
54 #ifdef __KERNEL__
55 unsigned int libcfs_subsystem_debug = ~0;
56 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
57                 "Lustre kernel debug subsystem mask");
58 EXPORT_SYMBOL(libcfs_subsystem_debug);
59
60 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
61                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
62 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
63                 "Lustre kernel debug mask");
64 EXPORT_SYMBOL(libcfs_debug);
65
66 int libcfs_debug_mb = -1;
67 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
68                 "Total debug buffer size.");
69 EXPORT_SYMBOL(libcfs_debug_mb);
70
71 unsigned int libcfs_printk = D_CANTMASK;
72 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
73                 "Lustre kernel debug console mask");
74 EXPORT_SYMBOL(libcfs_printk);
75
76 unsigned int libcfs_console_ratelimit = 1;
77 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
78                 "Lustre kernel debug console ratelimit (0 to disable)");
79 EXPORT_SYMBOL(libcfs_console_ratelimit);
80
81 cfs_duration_t libcfs_console_max_delay;
82 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
83                 "Lustre kernel debug console max delay (jiffies)");
84 EXPORT_SYMBOL(libcfs_console_max_delay);
85
86 cfs_duration_t libcfs_console_min_delay;
87 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
88                 "Lustre kernel debug console min delay (jiffies)");
89 EXPORT_SYMBOL(libcfs_console_min_delay);
90
91 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
92 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
93                 "Lustre kernel debug console backoff factor");
94 EXPORT_SYMBOL(libcfs_console_backoff);
95
96 unsigned int libcfs_debug_binary = 1;
97 EXPORT_SYMBOL(libcfs_debug_binary);
98
99 unsigned int libcfs_stack;
100 EXPORT_SYMBOL(libcfs_stack);
101
102 unsigned int portal_enter_debugger;
103 EXPORT_SYMBOL(portal_enter_debugger);
104
105 unsigned int libcfs_catastrophe;
106 EXPORT_SYMBOL(libcfs_catastrophe);
107
108 unsigned int libcfs_panic_on_lbug = 0;
109 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
110                 "Lustre kernel panic on LBUG");
111 EXPORT_SYMBOL(libcfs_panic_on_lbug);
112
113 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
114 EXPORT_SYMBOL(libcfs_kmemory);
115
116 static cfs_waitq_t debug_ctlwq;
117
118 #ifdef HAVE_BGL_SUPPORT
119 char debug_file_path_arr[1024] = "/bgl/ion/tmp/lustre-log";
120 #elif defined(__arch_um__)
121 char debug_file_path_arr[1024] = "/r/tmp/lustre-log";
122 #else
123 char debug_file_path_arr[1024] = "/tmp/lustre-log";
124 #endif
125 /* We need to pass a pointer here, but elsewhere this must be a const */
126 static char *debug_file_path = &debug_file_path_arr[0];
127 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
128                 "Path for dumping debug logs, "
129                 "set 'NONE' to prevent log dumping");
130
131 int libcfs_panic_in_progress;
132
133 /* libcfs_debug_token2mask() expects the returned
134  * string in lower-case */
135 const char *
136 libcfs_debug_subsys2str(int subsys)
137 {
138         switch (subsys) {
139         default:
140                 return NULL;
141         case S_UNDEFINED:
142                 return "undefined";
143         case S_MDC:
144                 return "mdc";
145         case S_MDS:
146                 return "mds";
147         case S_OSC:
148                 return "osc";
149         case S_OST:
150                 return "ost";
151         case S_CLASS:
152                 return "class";
153         case S_LOG:
154                 return "log";
155         case S_LLITE:
156                 return "llite";
157         case S_RPC:
158                 return "rpc";
159         case S_LNET:
160                 return "lnet";
161         case S_LND:
162                 return "lnd";
163         case S_PINGER:
164                 return "pinger";
165         case S_FILTER:
166                 return "filter";
167         case S_ECHO:
168                 return "echo";
169         case S_LDLM:
170                 return "ldlm";
171         case S_LOV:
172                 return "lov";
173         case S_LQUOTA:
174                 return "lquota";
175         case S_LMV:
176                 return "lmv";
177         case S_SEC:
178                 return "sec";
179         case S_GSS:
180                 return "gss";
181         case S_MGC:
182                 return "mgc";
183         case S_MGS:
184                 return "mgs";
185         case S_FID:
186                 return "fid";
187         case S_FLD:
188                 return "fld";
189         }
190 }
191
192 /* libcfs_debug_token2mask() expects the returned
193  * string in lower-case */
194 const char *
195 libcfs_debug_dbg2str(int debug)
196 {
197         switch (debug) {
198         default:
199                 return NULL;
200         case D_TRACE:
201                 return "trace";
202         case D_INODE:
203                 return "inode";
204         case D_SUPER:
205                 return "super";
206         case D_EXT2:
207                 return "ext2";
208         case D_MALLOC:
209                 return "malloc";
210         case D_CACHE:
211                 return "cache";
212         case D_INFO:
213                 return "info";
214         case D_IOCTL:
215                 return "ioctl";
216         case D_NETERROR:
217                 return "neterror";
218         case D_NET:
219                 return "net";
220         case D_WARNING:
221                 return "warning";
222         case D_BUFFS:
223                 return "buffs";
224         case D_OTHER:
225                 return "other";
226         case D_DENTRY:
227                 return "dentry";
228         case D_NETTRACE:
229                 return "nettrace";
230         case D_PAGE:
231                 return "page";
232         case D_DLMTRACE:
233                 return "dlmtrace";
234         case D_ERROR:
235                 return "error";
236         case D_EMERG:
237                 return "emerg";
238         case D_HA:
239                 return "ha";
240         case D_RPCTRACE:
241                 return "rpctrace";
242         case D_VFSTRACE:
243                 return "vfstrace";
244         case D_READA:
245                 return "reada";
246         case D_MMAP:
247                 return "mmap";
248         case D_CONFIG:
249                 return "config";
250         case D_CONSOLE:
251                 return "console";
252         case D_QUOTA:
253                 return "quota";
254         case D_SEC:
255                 return "sec";
256         }
257 }
258
259 int
260 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
261 {
262         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
263                                                  libcfs_debug_dbg2str;
264         int           len = 0;
265         const char   *token;
266         int           bit;
267         int           i;
268
269         if (mask == 0) {                        /* "0" */
270                 if (size > 0)
271                         str[0] = '0';
272                 len = 1;
273         } else {                                /* space-separated tokens */
274                 for (i = 0; i < 32; i++) {
275                         bit = 1 << i;
276
277                         if ((mask & bit) == 0)
278                                 continue;
279
280                         token = fn(bit);
281                         if (token == NULL)              /* unused bit */
282                                 continue;
283
284                         if (len > 0) {                  /* separator? */
285                                 if (len < size)
286                                         str[len] = ' ';
287                                 len++;
288                         }
289
290                         while (*token != 0) {
291                                 if (len < size)
292                                         str[len] = *token;
293                                 token++;
294                                 len++;
295                         }
296                 }
297         }
298
299         /* terminate 'str' */
300         if (len < size)
301                 str[len] = 0;
302         else
303                 str[size - 1] = 0;
304
305         return len;
306 }
307
308 int
309 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
310 {
311         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
312                                                  libcfs_debug_dbg2str;
313         int           i;
314         int           j;
315         int           bit;
316         const char   *token;
317
318         /* match against known tokens */
319         for (i = 0; i < 32; i++) {
320                 bit = 1 << i;
321
322                 token = fn(bit);
323                 if (token == NULL)              /* unused? */
324                         continue;
325
326                 /* strcasecmp */
327                 for (j = 0; ; j++) {
328                         if (j == len) {         /* end of token */
329                                 if (token[j] == 0) {
330                                         *mask = bit;
331                                         return 0;
332                                 }
333                                 break;
334                         }
335
336                         if (token[j] == 0)
337                                 break;
338
339                         if (str[j] == token[j])
340                                 continue;
341
342                         if (str[j] < 'A' || 'Z' < str[j])
343                                 break;
344
345                         if (str[j] - 'A' + 'a' != token[j])
346                                 break;
347                 }
348         }
349
350         return -EINVAL;                         /* no match */
351 }
352
353 int
354 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
355 {
356         int         m = 0;
357         char        op = 0;
358         int         matched;
359         int         n;
360         int         t;
361
362         /* Allow a number for backwards compatibility */
363
364         for (n = strlen(str); n > 0; n--)
365                 if (!isspace(str[n-1]))
366                         break;
367         matched = n;
368
369         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
370             matched == n) {
371                 *mask = m;
372                 return 0;
373         }
374
375         /* <str> must be a list of debug tokens or numbers separated by
376          * whitespace and optionally an operator ('+' or '-').  If an operator
377          * appears first in <str>, '*mask' is used as the starting point
378          * (relative), otherwise 0 is used (absolute).  An operator applies to
379          * all following tokens up to the next operator. */
380
381         matched = 0;
382         while (*str != 0) {
383                 while (isspace(*str)) /* skip whitespace */
384                         str++;
385
386                 if (*str == 0)
387                         break;
388
389                 if (*str == '+' || *str == '-') {
390                         op = *str++;
391
392                         /* op on first token == relative */
393                         if (!matched)
394                                 m = *mask;
395
396                         while (isspace(*str)) /* skip whitespace */
397                                 str++;
398
399                         if (*str == 0)          /* trailing op */
400                                 return -EINVAL;
401                 }
402
403                 /* find token length */
404                 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
405
406                 /* match token */
407                 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
408                         return -EINVAL;
409
410                 matched = 1;
411                 if (op == '-')
412                         m &= ~t;
413                 else
414                         m |= t;
415
416                 str += n;
417         }
418
419         if (!matched)
420                 return -EINVAL;
421
422         *mask = m;
423         return 0;
424 }
425
426 /**
427  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
428  */
429 void libcfs_debug_dumplog_internal(void *arg)
430 {
431         CFS_DECL_JOURNAL_DATA;
432
433         CFS_PUSH_JOURNAL;
434
435         if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
436                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
437                          "%s.%ld.%ld", debug_file_path_arr,
438                          cfs_time_current_sec(), (long)arg);
439                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
440                        debug_file_name);
441                 tracefile_dump_all_pages(debug_file_name);
442                 libcfs_run_debug_log_upcall(debug_file_name);
443         }
444         CFS_POP_JOURNAL;
445 }
446
447 int libcfs_debug_dumplog_thread(void *arg)
448 {
449         cfs_daemonize("");
450         libcfs_debug_dumplog_internal(arg);
451         cfs_waitq_signal(&debug_ctlwq);
452         return 0;
453 }
454
455 void libcfs_debug_dumplog(void)
456 {
457         int            rc;
458         cfs_waitlink_t wait;
459         ENTRY;
460
461         /* we're being careful to ensure that the kernel thread is
462          * able to set our state to running as it exits before we
463          * get to schedule() */
464         cfs_waitlink_init(&wait);
465         set_current_state(TASK_INTERRUPTIBLE);
466         cfs_waitq_add(&debug_ctlwq, &wait);
467
468         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
469                                (void *)(long)cfs_curproc_pid(),
470                                CLONE_VM | CLONE_FS | CLONE_FILES);
471         if (rc < 0)
472                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
473                        "%d\n", rc);
474         else
475                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
476
477         /* be sure to teardown if kernel_thread() failed */
478         cfs_waitq_del(&debug_ctlwq, &wait);
479         set_current_state(TASK_RUNNING);
480 }
481
482 int libcfs_debug_init(unsigned long bufsize)
483 {
484         int    rc = 0;
485         int    max = libcfs_debug_mb;
486
487         cfs_waitq_init(&debug_ctlwq);
488         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
489         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
490         /* If libcfs_debug_mb is set to an invalid value or uninitialized
491          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
492         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
493                 max = TCD_MAX_PAGES;
494         } else {
495                 max = (max / num_possible_cpus());
496                 max = (max << (20 - CFS_PAGE_SHIFT));
497         }
498         rc = tracefile_init(max);
499
500         if (rc == 0)
501                 libcfs_register_panic_notifier();
502
503         return rc;
504 }
505
506 int libcfs_debug_cleanup(void)
507 {
508         libcfs_unregister_panic_notifier();
509         tracefile_exit();
510         return 0;
511 }
512
513 int libcfs_debug_clear_buffer(void)
514 {
515         trace_flush_pages();
516         return 0;
517 }
518
519 /* Debug markers, although printed by S_LNET
520  * should not be be marked as such. */
521 #undef DEBUG_SUBSYSTEM
522 #define DEBUG_SUBSYSTEM S_UNDEFINED
523 int libcfs_debug_mark_buffer(const char *text)
524 {
525         CDEBUG(D_TRACE,"***************************************************\n");
526         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
527         CDEBUG(D_TRACE,"***************************************************\n");
528
529         return 0;
530 }
531 #undef DEBUG_SUBSYSTEM
532 #define DEBUG_SUBSYSTEM S_LNET
533
534 void libcfs_debug_set_level(unsigned int debug_level)
535 {
536         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
537                debug_level);
538         libcfs_debug = debug_level;
539 }
540
541 EXPORT_SYMBOL(libcfs_debug_dumplog);
542 EXPORT_SYMBOL(libcfs_debug_set_level);
543
544
545 #else /* !__KERNEL__ */
546
547 #include <libcfs/libcfs.h>
548
549 #ifdef HAVE_CATAMOUNT_DATA_H
550 #include <catamount/data.h>
551 #include <catamount/lputs.h>
552
553 static char source_nid[16];
554 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
555 static int toconsole = 1;
556 unsigned int libcfs_console_ratelimit = 1;
557 cfs_duration_t libcfs_console_max_delay;
558 cfs_duration_t libcfs_console_min_delay;
559 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
560 #else /* !HAVE_CATAMOUNT_DATA_H */
561 #ifdef HAVE_NETDB_H
562 #include <sys/utsname.h>
563 #endif /* HAVE_NETDB_H */
564 struct utsname *tmp_utsname;
565 static char source_nid[sizeof(tmp_utsname->nodename)];
566 #endif /* HAVE_CATAMOUNT_DATA_H */
567
568 static int source_pid;
569 int smp_processor_id = 1;
570 char debug_file_path[1024];
571 FILE *debug_file_fd;
572
573 int portals_do_debug_dumplog(void *arg)
574 {
575         printf("Look in %s\n", debug_file_name);
576         return 0;
577 }
578
579
580 void portals_debug_print(void)
581 {
582         return;
583 }
584
585
586 void libcfs_debug_dumplog(void)
587 {
588         printf("Look in %s\n", debug_file_name);
589         return;
590 }
591
592 int libcfs_debug_init(unsigned long bufsize)
593 {
594         char *debug_mask = NULL;
595         char *debug_subsys = NULL;
596         char *debug_filename;
597
598 #ifdef HAVE_CATAMOUNT_DATA_H
599         char *debug_console = NULL;
600         char *debug_ratelimit = NULL;
601         char *debug_max_delay = NULL;
602         char *debug_min_delay = NULL;
603         char *debug_backoff = NULL;
604
605         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
606         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
607
608         snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
609         source_pid = _my_pid;
610
611         debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
612         if (debug_console != NULL) {
613                 toconsole = strtoul(debug_console, NULL, 0);
614                 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
615         }
616         debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
617         if (debug_ratelimit != NULL) {
618                 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
619                 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
620                                 libcfs_console_ratelimit);
621         }
622         debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
623         if (debug_max_delay != NULL)
624                 libcfs_console_max_delay =
625                             cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
626         debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
627         if (debug_min_delay != NULL)
628                 libcfs_console_min_delay =
629                             cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
630         if (debug_min_delay || debug_max_delay) {
631                 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
632                     libcfs_console_max_delay < libcfs_console_min_delay) {
633                         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
634                         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
635                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
636                                        "should be greater than "
637                                        "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
638                                        "and both parameters should be non-null"
639                                        ": restore default values\n");
640                 } else {
641                         CDEBUG(D_INFO, "set liblustre console max delay to %lus"
642                                        " and min delay to %lus\n",
643                                (cfs_duration_t)
644                                      cfs_duration_sec(libcfs_console_max_delay),
645                                (cfs_duration_t)
646                                     cfs_duration_sec(libcfs_console_min_delay));
647                 }
648         }
649         debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
650         if (debug_backoff != NULL) {
651                 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
652                 if (libcfs_console_backoff <= 0) {
653                         libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
654                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
655                                        "restore default value\n");
656                 } else {
657                         CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
658                                libcfs_console_backoff);
659                 }
660         }
661 #else
662         struct utsname myname;
663
664         if (uname(&myname) == 0)
665                 strcpy(source_nid, myname.nodename);
666         source_pid = getpid();
667 #endif
668         /* debug masks */
669         debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
670         if (debug_mask)
671                 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
672
673         debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
674         if (debug_subsys)
675                 libcfs_subsystem_debug =
676                                 (unsigned int) strtol(debug_subsys, NULL, 0);
677
678         debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
679         if (debug_filename)
680                 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
681
682         debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
683         if (debug_filename)
684                 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
685
686         if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
687                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
688                          "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
689
690         if (strcmp(debug_file_name, "stdout") == 0 ||
691             strcmp(debug_file_name, "-") == 0) {
692                 debug_file_fd = stdout;
693         } else if (strcmp(debug_file_name, "stderr") == 0) {
694                 debug_file_fd = stderr;
695         } else if (debug_file_name[0] != '\0') {
696                 debug_file_fd = fopen(debug_file_name, "w");
697                 if (debug_file_fd == NULL)
698                         fprintf(stderr, "%s: unable to open '%s': %s\n",
699                                 source_nid, debug_file_name, strerror(errno));
700         }
701
702         if (debug_file_fd == NULL)
703                 debug_file_fd = stdout;
704
705         return 0;
706 }
707
708 int libcfs_debug_cleanup(void)
709 {
710         if (debug_file_fd != stdout && debug_file_fd != stderr)
711                 fclose(debug_file_fd);
712         return 0;
713 }
714
715 int libcfs_debug_clear_buffer(void)
716 {
717         return 0;
718 }
719
720 int libcfs_debug_mark_buffer(char *text)
721 {
722
723         fprintf(debug_file_fd, "*******************************************************************************\n");
724         fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
725         fprintf(debug_file_fd, "*******************************************************************************\n");
726
727         return 0;
728 }
729
730 #ifdef HAVE_CATAMOUNT_DATA_H
731 #define CATAMOUNT_MAXLINE (256-4)
732 void catamount_printline(char *buf, size_t size)
733 {
734     char *pos = buf;
735     int prsize = size;
736
737     while (prsize > 0){
738         lputs(pos);
739         pos += CATAMOUNT_MAXLINE;
740         prsize -= CATAMOUNT_MAXLINE;
741     }
742 }
743 #endif
744
745 int
746 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
747                    int subsys, int mask,
748                    const char *file, const char *fn, const int line,
749                    const char *format1, va_list args,
750                    const char *format2, ...)
751 {
752         struct timeval tv;
753         int            nob;
754         int            remain;
755         va_list        ap;
756         char           buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
757                                             * with linux, where message can`t
758                                             * be exceed PAGE_SIZE */
759         int            console = 0;
760         char *prefix = "Lustre";
761
762 #ifdef HAVE_CATAMOUNT_DATA_H
763         /* toconsole == 0 - all messages to debug_file_fd
764          * toconsole == 1 - warnings to console, all to debug_file_fd
765          * toconsole >  1 - all debug to console */
766         if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
767                 console = 1;
768 #endif
769
770         if ((!console) && (!debug_file_fd)) {
771                 return 0;
772         }
773
774         if (mask & (D_EMERG | D_ERROR))
775                prefix = "LustreError";
776
777         nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
778                        source_pid, source_nid, file, line, fn);
779
780         remain = sizeof(buf) - nob;
781         if (format1) {
782                 nob += vsnprintf(&buf[nob], remain, format1, args);
783         }
784
785         remain = sizeof(buf) - nob;
786         if ((format2) && (remain > 0)) {
787                 va_start(ap, format2);
788                 nob += vsnprintf(&buf[nob], remain, format2, ap);
789                 va_end(ap);
790         }
791
792 #ifdef HAVE_CATAMOUNT_DATA_H
793         if (console) {
794                 /* check rate limit for console */
795                 if (cdls != NULL) {
796                         if (libcfs_console_ratelimit &&
797                                 cdls->cdls_next != 0 &&     /* not first time ever */
798                                 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
799
800                                 /* skipping a console message */
801                                 cdls->cdls_count++;
802                                 goto out_file;
803                         }
804
805                         if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
806                                            libcfs_console_max_delay +
807                                            cfs_time_seconds(10))) {
808                                 /* last timeout was a long time ago */
809                                 cdls->cdls_delay /= libcfs_console_backoff * 4;
810                         } else {
811                                 cdls->cdls_delay *= libcfs_console_backoff;
812
813                                 if (cdls->cdls_delay <
814                                                 libcfs_console_min_delay)
815                                         cdls->cdls_delay =
816                                                 libcfs_console_min_delay;
817                                 else if (cdls->cdls_delay >
818                                                 libcfs_console_max_delay)
819                                         cdls->cdls_delay =
820                                                 libcfs_console_max_delay;
821                         }
822
823                         /* ensure cdls_next is never zero after it's been seen */
824                         cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
825                 }
826
827                 if (cdls != NULL && cdls->cdls_count != 0) {
828                         char buf2[100];
829
830                         nob = snprintf(buf2, sizeof(buf2),
831                                        "Skipped %d previous similar message%s\n",
832                                        cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
833
834                         catamount_printline(buf2, nob);
835                         cdls->cdls_count = 0;
836                         goto out_file;
837                 }
838                 catamount_printline(buf, nob);
839        }
840 out_file:
841         /* return on toconsole > 1, as we don't want the user getting
842         * spammed by the debug data */
843         if (toconsole > 1)
844                 return 0;
845 #endif
846         if (debug_file_fd == NULL)
847                 return 0;
848
849         gettimeofday(&tv, NULL);
850
851         fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
852                 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
853                 file, line, fn, buf);
854
855         return 0;
856 }
857
858 void
859 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
860                         const int line)
861 {
862         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
863                          "ASSERTION(%s) failed\n", expr);
864         abort();
865 }
866
867 #endif /* __KERNEL__ */