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