]> granicus.if.org Git - zfs/blob - module/spl/spl-debug.c
Add Thread Specific Data (TSD) Implementation
[zfs] / module / spl / spl-debug.c
1 /*****************************************************************************\
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *  For details, see <http://github.com/behlendorf/spl/>.
10  *
11  *  The SPL is free software; you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License as published by the
13  *  Free Software Foundation; either version 2 of the License, or (at your
14  *  option) any later version.
15  *
16  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19  *  for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23  *****************************************************************************
24  *  Solaris Porting Layer (SPL) Debug Implementation.
25 \*****************************************************************************/
26
27 #include <linux/kmod.h>
28 #include <linux/mm.h>
29 #include <linux/vmalloc.h>
30 #include <linux/pagemap.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/kthread.h>
34 #include <linux/hardirq.h>
35 #include <linux/interrupt.h>
36 #include <linux/spinlock.h>
37 #include <linux/proc_compat.h>
38 #include <linux/file_compat.h>
39 #include <sys/sysmacros.h>
40 #include <spl-debug.h>
41 #include <spl-trace.h>
42 #include <spl-ctl.h>
43
44 #ifdef SS_DEBUG_SUBSYS
45 #undef SS_DEBUG_SUBSYS
46 #endif
47
48 #define SS_DEBUG_SUBSYS SS_DEBUG
49
50 unsigned long spl_debug_subsys = ~0;
51 EXPORT_SYMBOL(spl_debug_subsys);
52 module_param(spl_debug_subsys, ulong, 0644);
53 MODULE_PARM_DESC(spl_debug_subsys, "Subsystem debugging level mask.");
54
55 unsigned long spl_debug_mask = SD_CANTMASK;
56 EXPORT_SYMBOL(spl_debug_mask);
57 module_param(spl_debug_mask, ulong, 0644);
58 MODULE_PARM_DESC(spl_debug_mask, "Debugging level mask.");
59
60 unsigned long spl_debug_printk = SD_CANTMASK;
61 EXPORT_SYMBOL(spl_debug_printk);
62 module_param(spl_debug_printk, ulong, 0644);
63 MODULE_PARM_DESC(spl_debug_printk, "Console printk level mask.");
64
65 int spl_debug_mb = -1;
66 EXPORT_SYMBOL(spl_debug_mb);
67 module_param(spl_debug_mb, int, 0644);
68 MODULE_PARM_DESC(spl_debug_mb, "Total debug buffer size.");
69
70 unsigned int spl_debug_binary = 1;
71 EXPORT_SYMBOL(spl_debug_binary);
72
73 unsigned int spl_debug_catastrophe;
74 EXPORT_SYMBOL(spl_debug_catastrophe);
75
76 unsigned int spl_debug_panic_on_bug = 0;
77 EXPORT_SYMBOL(spl_debug_panic_on_bug);
78 module_param(spl_debug_panic_on_bug, uint, 0644);
79 MODULE_PARM_DESC(spl_debug_panic_on_bug, "Panic on BUG");
80
81 static char spl_debug_file_name[PATH_MAX];
82 char spl_debug_file_path[PATH_MAX] = "/tmp/spl-log";
83
84 unsigned int spl_console_ratelimit = 1;
85 EXPORT_SYMBOL(spl_console_ratelimit);
86
87 long spl_console_max_delay;
88 EXPORT_SYMBOL(spl_console_max_delay);
89
90 long spl_console_min_delay;
91 EXPORT_SYMBOL(spl_console_min_delay);
92
93 unsigned int spl_console_backoff = SPL_DEFAULT_BACKOFF;
94 EXPORT_SYMBOL(spl_console_backoff);
95
96 unsigned int spl_debug_stack;
97 EXPORT_SYMBOL(spl_debug_stack);
98
99 static int spl_panic_in_progress;
100
101 union trace_data_union (*trace_data[TCD_TYPE_MAX])[NR_CPUS] __cacheline_aligned;
102 char *trace_console_buffers[NR_CPUS][3];
103 struct rw_semaphore trace_sem;
104 atomic_t trace_tage_allocated = ATOMIC_INIT(0);
105
106 static int spl_debug_dump_all_pages(dumplog_priv_t *dp, char *);
107 static void trace_fini(void);
108
109
110 /* Memory percentage breakdown by type */
111 static unsigned int pages_factor[TCD_TYPE_MAX] = {
112        80,  /* 80% pages for TCD_TYPE_PROC */
113        10,  /* 10% pages for TCD_TYPE_SOFTIRQ */
114        10   /* 10% pages for TCD_TYPE_IRQ */
115 };
116
117 const char *
118 spl_debug_subsys2str(int subsys)
119 {
120         switch (subsys) {
121         default:
122                 return NULL;
123         case SS_UNDEFINED:
124                 return "undefined";
125         case SS_ATOMIC:
126                 return "atomic";
127         case SS_KOBJ:
128                 return "kobj";
129         case SS_VNODE:
130                 return "vnode";
131         case SS_TIME:
132                 return "time";
133         case SS_RWLOCK:
134                 return "rwlock";
135         case SS_THREAD:
136                 return "thread";
137         case SS_CONDVAR:
138                 return "condvar";
139         case SS_MUTEX:
140                 return "mutex";
141         case SS_RNG:
142                 return "rng";
143         case SS_TASKQ:
144                 return "taskq";
145         case SS_KMEM:
146                 return "kmem";
147         case SS_DEBUG:
148                 return "debug";
149         case SS_GENERIC:
150                 return "generic";
151         case SS_PROC:
152                 return "proc";
153         case SS_MODULE:
154                 return "module";
155         case SS_CRED:
156                 return "cred";
157         case SS_KSTAT:
158                 return "kstat";
159         case SS_XDR:
160                 return "xdr";
161         case SS_TSD:
162                 return "tsd";
163         case SS_USER1:
164                 return "user1";
165         case SS_USER2:
166                 return "user2";
167         case SS_USER3:
168                 return "user3";
169         case SS_USER4:
170                 return "user4";
171         case SS_USER5:
172                 return "user5";
173         case SS_USER6:
174                 return "user6";
175         case SS_USER7:
176                 return "user7";
177         case SS_USER8:
178                 return "user8";
179         }
180 }
181
182 const char *
183 spl_debug_dbg2str(int debug)
184 {
185         switch (debug) {
186         default:
187                 return NULL;
188         case SD_TRACE:
189                 return "trace";
190         case SD_INFO:
191                 return "info";
192         case SD_WARNING:
193                 return "warning";
194         case SD_ERROR:
195                 return "error";
196         case SD_EMERG:
197                 return "emerg";
198         case SD_CONSOLE:
199                 return "console";
200         case SD_IOCTL:
201                 return "ioctl";
202         case SD_DPRINTF:
203                 return "dprintf";
204         case SD_OTHER:
205                 return "other";
206         }
207 }
208
209 int
210 spl_debug_mask2str(char *str, int size, unsigned long mask, int is_subsys)
211 {
212         const char *(*fn)(int bit) = is_subsys ? spl_debug_subsys2str :
213                                                  spl_debug_dbg2str;
214         const char *token;
215         int i, bit, len = 0;
216
217         if (mask == 0) {                        /* "0" */
218                 if (size > 0)
219                         str[0] = '0';
220                 len = 1;
221         } else {                                /* space-separated tokens */
222                 for (i = 0; i < 32; i++) {
223                         bit = 1 << i;
224
225                         if ((mask & bit) == 0)
226                                 continue;
227
228                         token = fn(bit);
229                         if (token == NULL)              /* unused bit */
230                                 continue;
231
232                         if (len > 0) {                  /* separator? */
233                                 if (len < size)
234                                         str[len] = ' ';
235                                 len++;
236                         }
237
238                         while (*token != 0) {
239                                 if (len < size)
240                                         str[len] = *token;
241                                 token++;
242                                 len++;
243                         }
244                 }
245         }
246
247         /* terminate 'str' */
248         if (len < size)
249                 str[len] = 0;
250         else
251                 str[size - 1] = 0;
252
253         return len;
254 }
255
256 static int
257 spl_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
258 {
259         const char *(*fn)(int bit) = is_subsys ? spl_debug_subsys2str :
260                                                  spl_debug_dbg2str;
261         const char   *token;
262         int i, j, bit;
263
264         /* match against known tokens */
265         for (i = 0; i < 32; i++) {
266                 bit = 1 << i;
267
268                 token = fn(bit);
269                 if (token == NULL)              /* unused? */
270                         continue;
271
272                 /* strcasecmp */
273                 for (j = 0; ; j++) {
274                         if (j == len) {         /* end of token */
275                                 if (token[j] == 0) {
276                                         *mask = bit;
277                                         return 0;
278                                 }
279                                 break;
280                         }
281
282                         if (token[j] == 0)
283                                 break;
284
285                         if (str[j] == token[j])
286                                 continue;
287
288                         if (str[j] < 'A' || 'Z' < str[j])
289                                 break;
290
291                         if (str[j] - 'A' + 'a' != token[j])
292                                 break;
293                 }
294         }
295
296         return -EINVAL;                         /* no match */
297 }
298
299 int
300 spl_debug_str2mask(unsigned long *mask, const char *str, int is_subsys)
301 {
302         char op = 0;
303         int m = 0, matched, n, t;
304
305         /* Allow a number for backwards compatibility */
306         for (n = strlen(str); n > 0; n--)
307                 if (!isspace(str[n-1]))
308                         break;
309         matched = n;
310
311         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 && matched == n) {
312                 *mask = m;
313                 return 0;
314         }
315
316         /* <str> must be a list of debug tokens or numbers separated by
317          * whitespace and optionally an operator ('+' or '-').  If an operator
318          * appears first in <str>, '*mask' is used as the starting point
319          * (relative), otherwise 0 is used (absolute).  An operator applies to
320          * all following tokens up to the next operator. */
321         matched = 0;
322         while (*str != 0) {
323                 while (isspace(*str)) /* skip whitespace */
324                         str++;
325
326                 if (*str == 0)
327                         break;
328
329                 if (*str == '+' || *str == '-') {
330                         op = *str++;
331
332                         /* op on first token == relative */
333                         if (!matched)
334                                 m = *mask;
335
336                         while (isspace(*str)) /* skip whitespace */
337                                 str++;
338
339                         if (*str == 0)          /* trailing op */
340                                 return -EINVAL;
341                 }
342
343                 /* find token length */
344                 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
345
346                 /* match token */
347                 if (spl_debug_token2mask(&t, str, n, is_subsys) != 0)
348                         return -EINVAL;
349
350                 matched = 1;
351                 if (op == '-')
352                         m &= ~t;
353                 else
354                         m |= t;
355
356                 str += n;
357         }
358
359         if (!matched)
360                 return -EINVAL;
361
362         *mask = m;
363         return 0;
364 }
365
366 static void
367 spl_debug_dumplog_internal(dumplog_priv_t *dp)
368 {
369         void *journal_info;
370
371         journal_info = current->journal_info;
372         current->journal_info = NULL;
373
374         snprintf(spl_debug_file_name, sizeof(spl_debug_file_path) - 1,
375                  "%s.%ld.%ld", spl_debug_file_path,
376                  get_seconds(), (long)dp->dp_pid);
377         printk("SPL: Dumping log to %s\n", spl_debug_file_name);
378         spl_debug_dump_all_pages(dp, spl_debug_file_name);
379
380         current->journal_info = journal_info;
381 }
382
383 static int
384 spl_debug_dumplog_thread(void *arg)
385 {
386         dumplog_priv_t *dp = (dumplog_priv_t *)arg;
387
388         spl_debug_dumplog_internal(dp);
389         atomic_set(&dp->dp_done, 1);
390         wake_up(&dp->dp_waitq);
391         complete_and_exit(NULL, 0);
392
393         return 0; /* Unreachable */
394 }
395
396 /* When flag is set do not use a new thread for the debug dump */
397 int
398 spl_debug_dumplog(int flags)
399 {
400         struct task_struct *tsk;
401         dumplog_priv_t dp;
402
403         init_waitqueue_head(&dp.dp_waitq);
404         dp.dp_pid = current->pid;
405         dp.dp_flags = flags;
406         atomic_set(&dp.dp_done, 0);
407
408         if (dp.dp_flags & DL_NOTHREAD) {
409                 spl_debug_dumplog_internal(&dp);
410         } else {
411
412                 tsk = kthread_create(spl_debug_dumplog_thread,(void *)&dp,"spl_debug");
413                 if (tsk == NULL)
414                         return -ENOMEM;
415
416                 wake_up_process(tsk);
417                 wait_event(dp.dp_waitq, atomic_read(&dp.dp_done));
418         }
419
420         return 0;
421 }
422 EXPORT_SYMBOL(spl_debug_dumplog);
423
424 static char *
425 trace_get_console_buffer(void)
426 {
427         int  cpu = get_cpu();
428         int  idx;
429
430         if (in_irq()) {
431                 idx = 0;
432         } else if (in_softirq()) {
433                 idx = 1;
434         } else {
435                 idx = 2;
436         }
437
438         return trace_console_buffers[cpu][idx];
439 }
440
441 static void
442 trace_put_console_buffer(char *buffer)
443 {
444         put_cpu();
445 }
446
447 static int
448 trace_lock_tcd(struct trace_cpu_data *tcd)
449 {
450         __ASSERT(tcd->tcd_type < TCD_TYPE_MAX);
451
452         spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
453
454         return 1;
455 }
456
457 static void
458 trace_unlock_tcd(struct trace_cpu_data *tcd)
459 {
460         __ASSERT(tcd->tcd_type < TCD_TYPE_MAX);
461
462         spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
463 }
464
465 static struct trace_cpu_data *
466 trace_get_tcd(void)
467 {
468         int cpu;
469         struct trace_cpu_data *tcd;
470
471         cpu = get_cpu();
472         if (in_irq())
473                 tcd = &(*trace_data[TCD_TYPE_IRQ])[cpu].tcd;
474         else if (in_softirq())
475                 tcd = &(*trace_data[TCD_TYPE_SOFTIRQ])[cpu].tcd;
476         else
477                 tcd = &(*trace_data[TCD_TYPE_PROC])[cpu].tcd;
478
479         trace_lock_tcd(tcd);
480
481         return tcd;
482 }
483
484 static void
485 trace_put_tcd (struct trace_cpu_data *tcd)
486 {
487         trace_unlock_tcd(tcd);
488
489         put_cpu();
490 }
491
492 static void
493 trace_set_debug_header(struct spl_debug_header *header, int subsys,
494                        int mask, const int line, unsigned long stack)
495 {
496         struct timeval tv;
497
498         do_gettimeofday(&tv);
499
500         header->ph_subsys = subsys;
501         header->ph_mask = mask;
502         header->ph_cpu_id = smp_processor_id();
503         header->ph_sec = (__u32)tv.tv_sec;
504         header->ph_usec = tv.tv_usec;
505         header->ph_stack = stack;
506         header->ph_pid = current->pid;
507         header->ph_line_num = line;
508
509         return;
510 }
511
512 static void
513 trace_print_to_console(struct spl_debug_header *hdr, int mask, const char *buf,
514                        int len, const char *file, const char *fn)
515 {
516         char *prefix = "SPL", *ptype = NULL;
517
518         if ((mask & SD_EMERG) != 0) {
519                 prefix = "SPLError";
520                 ptype = KERN_EMERG;
521         } else if ((mask & SD_ERROR) != 0) {
522                 prefix = "SPLError";
523                 ptype = KERN_ERR;
524         } else if ((mask & SD_WARNING) != 0) {
525                 prefix = "SPL";
526                 ptype = KERN_WARNING;
527         } else if ((mask & (SD_CONSOLE | spl_debug_printk)) != 0) {
528                 prefix = "SPL";
529                 ptype = KERN_INFO;
530         }
531
532         if ((mask & SD_CONSOLE) != 0) {
533                 printk("%s%s: %.*s", ptype, prefix, len, buf);
534         } else {
535                 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix,
536                        hdr->ph_pid, hdr->ph_stack, file,
537                        hdr->ph_line_num, fn, len, buf);
538         }
539
540         return;
541 }
542
543 static int
544 trace_max_debug_mb(void)
545 {
546         return MAX(512, ((num_physpages >> (20 - PAGE_SHIFT)) * 80) / 100);
547 }
548
549 static struct trace_page *
550 tage_alloc(int gfp)
551 {
552         struct page *page;
553         struct trace_page *tage;
554
555         page = alloc_pages(gfp | __GFP_NOWARN, 0);
556         if (page == NULL)
557                 return NULL;
558
559         tage = kmalloc(sizeof(*tage), gfp);
560         if (tage == NULL) {
561                 __free_pages(page, 0);
562                 return NULL;
563         }
564
565         tage->page = page;
566         atomic_inc(&trace_tage_allocated);
567
568         return tage;
569 }
570
571 static void
572 tage_free(struct trace_page *tage)
573 {
574         __ASSERT(tage != NULL);
575         __ASSERT(tage->page != NULL);
576
577         __free_pages(tage->page, 0);
578         kfree(tage);
579         atomic_dec(&trace_tage_allocated);
580 }
581
582 static struct trace_page *
583 tage_from_list(struct list_head *list)
584 {
585         return list_entry(list, struct trace_page, linkage);
586 }
587
588 static void
589 tage_to_tail(struct trace_page *tage, struct list_head *queue)
590 {
591         __ASSERT(tage != NULL);
592         __ASSERT(queue != NULL);
593
594         list_move_tail(&tage->linkage, queue);
595 }
596
597 /* try to return a page that has 'len' bytes left at the end */
598 static struct trace_page *
599 trace_get_tage_try(struct trace_cpu_data *tcd, unsigned long len)
600 {
601         struct trace_page *tage;
602
603         if (tcd->tcd_cur_pages > 0) {
604                 __ASSERT(!list_empty(&tcd->tcd_pages));
605                 tage = tage_from_list(tcd->tcd_pages.prev);
606                 if (tage->used + len <= PAGE_SIZE)
607                         return tage;
608         }
609
610         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
611                 if (tcd->tcd_cur_stock_pages > 0) {
612                         tage = tage_from_list(tcd->tcd_stock_pages.prev);
613                         tcd->tcd_cur_stock_pages--;
614                         list_del_init(&tage->linkage);
615                 } else {
616                         tage = tage_alloc(GFP_ATOMIC);
617                         if (tage == NULL) {
618                                 printk(KERN_WARNING
619                                        "failure to allocate a tage (%ld)\n",
620                                        tcd->tcd_cur_pages);
621                                 return NULL;
622                         }
623                 }
624
625                 tage->used = 0;
626                 tage->cpu = smp_processor_id();
627                 tage->type = tcd->tcd_type;
628                 list_add_tail(&tage->linkage, &tcd->tcd_pages);
629                 tcd->tcd_cur_pages++;
630
631                 return tage;
632         }
633
634         return NULL;
635 }
636
637 /* return a page that has 'len' bytes left at the end */
638 static struct trace_page *
639 trace_get_tage(struct trace_cpu_data *tcd, unsigned long len)
640 {
641         struct trace_page *tage;
642
643         __ASSERT(len <= PAGE_SIZE);
644
645         tage = trace_get_tage_try(tcd, len);
646         if (tage)
647                 return tage;
648
649         if (tcd->tcd_cur_pages > 0) {
650                 tage = tage_from_list(tcd->tcd_pages.next);
651                 tage->used = 0;
652                 tage_to_tail(tage, &tcd->tcd_pages);
653         }
654
655         return tage;
656 }
657
658 int
659 spl_debug_msg(void *arg, int subsys, int mask, const char *file,
660     const char *fn, const int line, const char *format, ...)
661 {
662         spl_debug_limit_state_t *cdls = arg;
663         struct trace_cpu_data   *tcd = NULL;
664         struct spl_debug_header header = { 0, };
665         struct trace_page       *tage;
666         /* string_buf is used only if tcd != NULL, and is always set then */
667         char                    *string_buf = NULL;
668         char                    *debug_buf;
669         int                      known_size;
670         int                      needed = 85; /* average message length */
671         int                      max_nob;
672         va_list                  ap;
673         int                      i;
674
675         if (subsys == 0)
676                 subsys = SS_DEBUG_SUBSYS;
677
678         if (mask == 0)
679                 mask = SD_EMERG;
680
681         if (strchr(file, '/'))
682                 file = strrchr(file, '/') + 1;
683
684         trace_set_debug_header(&header, subsys, mask, line, 0);
685
686         tcd = trace_get_tcd();
687         if (tcd == NULL)
688                 goto console;
689
690         if (tcd->tcd_shutting_down) {
691                 trace_put_tcd(tcd);
692                 tcd = NULL;
693                 goto console;
694         }
695
696         known_size = strlen(file) + 1;
697         if (fn)
698                 known_size += strlen(fn) + 1;
699
700         if (spl_debug_binary)
701                 known_size += sizeof(header);
702
703         /* '2' used because vsnprintf returns real size required for output
704          * _without_ terminating NULL. */
705         for (i = 0; i < 2; i++) {
706                 tage = trace_get_tage(tcd, needed + known_size + 1);
707                 if (tage == NULL) {
708                         if (needed + known_size > PAGE_SIZE)
709                                 mask |= SD_ERROR;
710
711                         trace_put_tcd(tcd);
712                         tcd = NULL;
713                         goto console;
714                 }
715
716                 string_buf = (char *)page_address(tage->page) +
717                              tage->used + known_size;
718
719                 max_nob = PAGE_SIZE - tage->used - known_size;
720                 if (max_nob <= 0) {
721                         printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
722                         mask |= SD_ERROR;
723                         trace_put_tcd(tcd);
724                         tcd = NULL;
725                         goto console;
726                 }
727
728                 needed = 0;
729                 if (format) {
730                         va_start(ap, format);
731                         needed += vsnprintf(string_buf, max_nob, format, ap);
732                         va_end(ap);
733                 }
734
735                 if (needed < max_nob)
736                         break;
737         }
738
739         header.ph_len = known_size + needed;
740         debug_buf = (char *)page_address(tage->page) + tage->used;
741
742         if (spl_debug_binary) {
743                 memcpy(debug_buf, &header, sizeof(header));
744                 tage->used += sizeof(header);
745                 debug_buf += sizeof(header);
746         }
747
748         strcpy(debug_buf, file);
749         tage->used += strlen(file) + 1;
750         debug_buf += strlen(file) + 1;
751
752         if (fn) {
753                 strcpy(debug_buf, fn);
754                 tage->used += strlen(fn) + 1;
755                 debug_buf += strlen(fn) + 1;
756         }
757
758         __ASSERT(debug_buf == string_buf);
759
760         tage->used += needed;
761         __ASSERT (tage->used <= PAGE_SIZE);
762
763 console:
764         if ((mask & spl_debug_printk) == 0) {
765                 /* no console output requested */
766                 if (tcd != NULL)
767                         trace_put_tcd(tcd);
768                 return 1;
769         }
770
771         if (cdls != NULL) {
772                 if (spl_console_ratelimit && cdls->cdls_next != 0 &&
773                     !time_before(cdls->cdls_next, jiffies)) {
774                         /* skipping a console message */
775                         cdls->cdls_count++;
776                         if (tcd != NULL)
777                                 trace_put_tcd(tcd);
778                         return 1;
779                 }
780
781                 if (time_before(cdls->cdls_next + spl_console_max_delay +
782                                 (10 * HZ), jiffies)) {
783                         /* last timeout was a long time ago */
784                         cdls->cdls_delay /= spl_console_backoff * 4;
785                 } else {
786                         cdls->cdls_delay *= spl_console_backoff;
787
788                         if (cdls->cdls_delay < spl_console_min_delay)
789                                 cdls->cdls_delay = spl_console_min_delay;
790                         else if (cdls->cdls_delay > spl_console_max_delay)
791                                 cdls->cdls_delay = spl_console_max_delay;
792                 }
793
794                 /* ensure cdls_next is never zero after it's been seen */
795                 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
796         }
797
798         if (tcd != NULL) {
799                 trace_print_to_console(&header, mask, string_buf, needed, file, fn);
800                 trace_put_tcd(tcd);
801         } else {
802                 string_buf = trace_get_console_buffer();
803
804                 needed = 0;
805                 if (format != NULL) {
806                         va_start(ap, format);
807                         needed += vsnprintf(string_buf,
808                             TRACE_CONSOLE_BUFFER_SIZE, format, ap);
809                         va_end(ap);
810                 }
811                 trace_print_to_console(&header, mask,
812                                  string_buf, needed, file, fn);
813
814                 trace_put_console_buffer(string_buf);
815         }
816
817         if (cdls != NULL && cdls->cdls_count != 0) {
818                 string_buf = trace_get_console_buffer();
819
820                 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
821                          "Skipped %d previous similar message%s\n",
822                          cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
823
824                 trace_print_to_console(&header, mask,
825                                  string_buf, needed, file, fn);
826
827                 trace_put_console_buffer(string_buf);
828                 cdls->cdls_count = 0;
829         }
830
831         return 0;
832 }
833 EXPORT_SYMBOL(spl_debug_msg);
834
835 /* Do the collect_pages job on a single CPU: assumes that all other
836  * CPUs have been stopped during a panic.  If this isn't true for
837  * some arch, this will have to be implemented separately in each arch.
838  */
839 static void
840 collect_pages_from_single_cpu(struct page_collection *pc)
841 {
842         struct trace_cpu_data *tcd;
843         int i, j;
844
845         tcd_for_each(tcd, i, j) {
846                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
847                 tcd->tcd_cur_pages = 0;
848         }
849 }
850
851 static void
852 collect_pages_on_all_cpus(struct page_collection *pc)
853 {
854         struct trace_cpu_data *tcd;
855         int i, cpu;
856
857         spin_lock(&pc->pc_lock);
858         for_each_possible_cpu(cpu) {
859                 tcd_for_each_type_lock(tcd, i, cpu) {
860                         list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
861                         tcd->tcd_cur_pages = 0;
862                 }
863         }
864         spin_unlock(&pc->pc_lock);
865 }
866
867 static void
868 collect_pages(dumplog_priv_t *dp, struct page_collection *pc)
869 {
870         INIT_LIST_HEAD(&pc->pc_pages);
871
872         if (spl_panic_in_progress || dp->dp_flags & DL_SINGLE_CPU)
873                 collect_pages_from_single_cpu(pc);
874         else
875                 collect_pages_on_all_cpus(pc);
876 }
877
878 static void
879 put_pages_back_on_all_cpus(struct page_collection *pc)
880 {
881         struct trace_cpu_data *tcd;
882         struct list_head *cur_head;
883         struct trace_page *tage;
884         struct trace_page *tmp;
885         int i, cpu;
886
887         spin_lock(&pc->pc_lock);
888
889         for_each_possible_cpu(cpu) {
890                 tcd_for_each_type_lock(tcd, i, cpu) {
891                         cur_head = tcd->tcd_pages.next;
892
893                         list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
894                                                  linkage) {
895                                 if (tage->cpu != cpu || tage->type != i)
896                                         continue;
897
898                                 tage_to_tail(tage, cur_head);
899                                 tcd->tcd_cur_pages++;
900                         }
901                 }
902         }
903
904         spin_unlock(&pc->pc_lock);
905 }
906
907 static void
908 put_pages_back(struct page_collection *pc)
909 {
910         if (!spl_panic_in_progress)
911                 put_pages_back_on_all_cpus(pc);
912 }
913
914 static int
915 spl_debug_dump_all_pages(dumplog_priv_t *dp, char *filename)
916 {
917         struct page_collection pc;
918         struct file *filp;
919         struct trace_page *tage;
920         struct trace_page *tmp;
921         mm_segment_t oldfs;
922         int rc = 0;
923
924         down_write(&trace_sem);
925
926         filp = spl_filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE,
927                                0600, &rc);
928         if (filp == NULL) {
929                 if (rc != -EEXIST)
930                         printk(KERN_ERR "SPL: Can't open %s for dump: %d\n",
931                                filename, rc);
932                 goto out;
933         }
934
935         spin_lock_init(&pc.pc_lock);
936         collect_pages(dp, &pc);
937         if (list_empty(&pc.pc_pages)) {
938                 rc = 0;
939                 goto close;
940         }
941
942         oldfs = get_fs();
943         set_fs(get_ds());
944
945         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
946                 rc = spl_filp_write(filp, page_address(tage->page),
947                                     tage->used, spl_filp_poff(filp));
948                 if (rc != (int)tage->used) {
949                         printk(KERN_WARNING "SPL: Wanted to write %u "
950                                "but wrote %d\n", tage->used, rc);
951                         put_pages_back(&pc);
952                         __ASSERT(list_empty(&pc.pc_pages));
953                         break;
954                 }
955                 list_del(&tage->linkage);
956                 tage_free(tage);
957         }
958
959         set_fs(oldfs);
960
961         rc = spl_filp_fsync(filp, 1);
962         if (rc)
963                 printk(KERN_ERR "SPL: Unable to sync: %d\n", rc);
964  close:
965         spl_filp_close(filp);
966  out:
967         up_write(&trace_sem);
968
969         return rc;
970 }
971
972 static void
973 spl_debug_flush_pages(void)
974 {
975         dumplog_priv_t dp;
976         struct page_collection pc;
977         struct trace_page *tage;
978         struct trace_page *tmp;
979
980         spin_lock_init(&pc.pc_lock);
981         init_waitqueue_head(&dp.dp_waitq);
982         dp.dp_pid = current->pid;
983         dp.dp_flags = 0;
984         atomic_set(&dp.dp_done, 0);
985
986         collect_pages(&dp, &pc);
987         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
988                 list_del(&tage->linkage);
989                 tage_free(tage);
990         }
991 }
992
993 unsigned long
994 spl_debug_set_mask(unsigned long mask) {
995         spl_debug_mask = mask;
996         return 0;
997 }
998 EXPORT_SYMBOL(spl_debug_set_mask);
999
1000 unsigned long
1001 spl_debug_get_mask(void) {
1002         return spl_debug_mask;
1003 }
1004 EXPORT_SYMBOL(spl_debug_get_mask);
1005
1006 unsigned long
1007 spl_debug_set_subsys(unsigned long subsys) {
1008         spl_debug_subsys = subsys;
1009         return 0;
1010 }
1011 EXPORT_SYMBOL(spl_debug_set_subsys);
1012
1013 unsigned long
1014 spl_debug_get_subsys(void) {
1015         return spl_debug_subsys;
1016 }
1017 EXPORT_SYMBOL(spl_debug_get_subsys);
1018
1019 int
1020 spl_debug_set_mb(int mb)
1021 {
1022         int i, j, pages;
1023         int limit = trace_max_debug_mb();
1024         struct trace_cpu_data *tcd;
1025
1026         if (mb < num_possible_cpus()) {
1027                 printk(KERN_ERR "SPL: Refusing to set debug buffer size to "
1028                        "%dMB - lower limit is %d\n", mb, num_possible_cpus());
1029                 return -EINVAL;
1030         }
1031
1032         if (mb > limit) {
1033                 printk(KERN_ERR "SPL: Refusing to set debug buffer size to "
1034                        "%dMB - upper limit is %d\n", mb, limit);
1035                 return -EINVAL;
1036         }
1037
1038         mb /= num_possible_cpus();
1039         pages = mb << (20 - PAGE_SHIFT);
1040
1041         down_write(&trace_sem);
1042
1043         tcd_for_each(tcd, i, j)
1044                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1045
1046         up_write(&trace_sem);
1047
1048         return 0;
1049 }
1050 EXPORT_SYMBOL(spl_debug_set_mb);
1051
1052 int
1053 spl_debug_get_mb(void)
1054 {
1055         int i, j;
1056         struct trace_cpu_data *tcd;
1057         int total_pages = 0;
1058
1059         down_read(&trace_sem);
1060
1061         tcd_for_each(tcd, i, j)
1062                 total_pages += tcd->tcd_max_pages;
1063
1064         up_read(&trace_sem);
1065
1066         return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1067 }
1068 EXPORT_SYMBOL(spl_debug_get_mb);
1069
1070 void spl_debug_dumpstack(struct task_struct *tsk)
1071 {
1072         extern void show_task(struct task_struct *);
1073
1074         if (tsk == NULL)
1075                 tsk = current;
1076
1077         printk("SPL: Showing stack for process %d\n", tsk->pid);
1078         dump_stack();
1079 }
1080 EXPORT_SYMBOL(spl_debug_dumpstack);
1081
1082 void spl_debug_bug(char *file, const char *func, const int line, int flags)
1083 {
1084         spl_debug_catastrophe = 1;
1085         spl_debug_msg(NULL, 0, SD_EMERG, file, func, line, "SPL PANIC\n");
1086
1087         if (in_interrupt())
1088                 panic("SPL PANIC in interrupt.\n");
1089
1090         if (in_atomic() || irqs_disabled())
1091                 flags |= DL_NOTHREAD;
1092
1093         /* Ensure all debug pages and dumped by current cpu */
1094          if (spl_debug_panic_on_bug)
1095                 spl_panic_in_progress = 1;
1096
1097         spl_debug_dumpstack(NULL);
1098         spl_debug_dumplog(flags);
1099
1100         if (spl_debug_panic_on_bug)
1101                 panic("SPL PANIC");
1102
1103         set_task_state(current, TASK_UNINTERRUPTIBLE);
1104         while (1)
1105                 schedule();
1106 }
1107 EXPORT_SYMBOL(spl_debug_bug);
1108
1109 int
1110 spl_debug_clear_buffer(void)
1111 {
1112         spl_debug_flush_pages();
1113         return 0;
1114 }
1115 EXPORT_SYMBOL(spl_debug_clear_buffer);
1116
1117 int
1118 spl_debug_mark_buffer(char *text)
1119 {
1120         SDEBUG(SD_WARNING, "*************************************\n");
1121         SDEBUG(SD_WARNING, "DEBUG MARKER: %s\n", text);
1122         SDEBUG(SD_WARNING, "*************************************\n");
1123
1124         return 0;
1125 }
1126 EXPORT_SYMBOL(spl_debug_mark_buffer);
1127
1128 static int
1129 trace_init(int max_pages)
1130 {
1131         struct trace_cpu_data *tcd;
1132         int i, j;
1133
1134         init_rwsem(&trace_sem);
1135
1136         /* initialize trace_data */
1137         memset(trace_data, 0, sizeof(trace_data));
1138         for (i = 0; i < TCD_TYPE_MAX; i++) {
1139                 trace_data[i] = kmalloc(sizeof(union trace_data_union) *
1140                                         NR_CPUS, GFP_KERNEL);
1141                 if (trace_data[i] == NULL)
1142                         goto out;
1143         }
1144
1145         tcd_for_each(tcd, i, j) {
1146                 spin_lock_init(&tcd->tcd_lock);
1147                 tcd->tcd_pages_factor = pages_factor[i];
1148                 tcd->tcd_type = i;
1149                 tcd->tcd_cpu = j;
1150                 INIT_LIST_HEAD(&tcd->tcd_pages);
1151                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1152                 tcd->tcd_cur_pages = 0;
1153                 tcd->tcd_cur_stock_pages = 0;
1154                 tcd->tcd_max_pages = (max_pages * pages_factor[i]) / 100;
1155                 tcd->tcd_shutting_down = 0;
1156         }
1157
1158         for (i = 0; i < num_possible_cpus(); i++) {
1159                 for (j = 0; j < 3; j++) {
1160                         trace_console_buffers[i][j] =
1161                                 kmalloc(TRACE_CONSOLE_BUFFER_SIZE,
1162                                         GFP_KERNEL);
1163
1164                         if (trace_console_buffers[i][j] == NULL)
1165                                 goto out;
1166                 }
1167        }
1168
1169         return 0;
1170 out:
1171         trace_fini();
1172         printk(KERN_ERR "SPL: Insufficient memory for debug logs\n");
1173         return -ENOMEM;
1174 }
1175
1176 int
1177 debug_init(void)
1178 {
1179         int rc, max = spl_debug_mb;
1180
1181         spl_console_max_delay = SPL_DEFAULT_MAX_DELAY;
1182         spl_console_min_delay = SPL_DEFAULT_MIN_DELAY;
1183
1184         /* If spl_debug_mb is set to an invalid value or uninitialized
1185          * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1186         if (max > (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5 ||
1187             max >= 512 || max < 0) {
1188                 max = TCD_MAX_PAGES;
1189         } else {
1190                 max = (max / num_online_cpus()) << (20 - PAGE_SHIFT);
1191         }
1192
1193         rc = trace_init(max);
1194         if (rc)
1195                 return rc;
1196
1197         return rc;
1198 }
1199
1200 static void
1201 trace_cleanup_on_all_cpus(void)
1202 {
1203         struct trace_cpu_data *tcd;
1204         struct trace_page *tage;
1205         struct trace_page *tmp;
1206         int i, cpu;
1207
1208         for_each_possible_cpu(cpu) {
1209                 tcd_for_each_type_lock(tcd, i, cpu) {
1210                         tcd->tcd_shutting_down = 1;
1211
1212                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1213                                                  linkage) {
1214                                 list_del(&tage->linkage);
1215                                 tage_free(tage);
1216                         }
1217                         tcd->tcd_cur_pages = 0;
1218                 }
1219         }
1220 }
1221
1222 static void
1223 trace_fini(void)
1224 {
1225         int i, j;
1226
1227         trace_cleanup_on_all_cpus();
1228
1229         for (i = 0; i < num_possible_cpus(); i++) {
1230                 for (j = 0; j < 3; j++) {
1231                         if (trace_console_buffers[i][j] != NULL) {
1232                                 kfree(trace_console_buffers[i][j]);
1233                                 trace_console_buffers[i][j] = NULL;
1234                         }
1235                 }
1236         }
1237
1238         for (i = 0; i < TCD_TYPE_MAX && trace_data[i] != NULL; i++) {
1239                 kfree(trace_data[i]);
1240                 trace_data[i] = NULL;
1241         }
1242 }
1243
1244 void
1245 debug_fini(void)
1246 {
1247         trace_fini();
1248 }