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