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