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