These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / lustre / lustre / libcfs / debug.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  *
40  */
41
42 # define DEBUG_SUBSYSTEM S_LNET
43
44 #include "../../include/linux/libcfs/libcfs.h"
45 #include "tracefile.h"
46
47 static char debug_file_name[1024];
48
49 unsigned int libcfs_subsystem_debug = ~0;
50 module_param(libcfs_subsystem_debug, int, 0644);
51 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
52 EXPORT_SYMBOL(libcfs_subsystem_debug);
53
54 unsigned int libcfs_debug = (D_CANTMASK |
55                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
56 module_param(libcfs_debug, int, 0644);
57 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
58 EXPORT_SYMBOL(libcfs_debug);
59
60 static int libcfs_param_debug_mb_set(const char *val,
61                                      const struct kernel_param *kp)
62 {
63         int rc;
64         unsigned num;
65
66         rc = kstrtouint(val, 0, &num);
67         if (rc < 0)
68                 return rc;
69
70         if (!*((unsigned int *)kp->arg)) {
71                 *((unsigned int *)kp->arg) = num;
72                 return 0;
73         }
74
75         rc = cfs_trace_set_debug_mb(num);
76
77         if (!rc)
78                 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
79
80         return rc;
81 }
82
83 /* While debug_mb setting look like unsigned int, in fact
84  * it needs quite a bunch of extra processing, so we define special
85  * debugmb parameter type with corresponding methods to handle this case */
86 static struct kernel_param_ops param_ops_debugmb = {
87         .set = libcfs_param_debug_mb_set,
88         .get = param_get_uint,
89 };
90
91 #define param_check_debugmb(name, p) \
92                 __param_check(name, p, unsigned int)
93
94 static unsigned int libcfs_debug_mb;
95 module_param(libcfs_debug_mb, debugmb, 0644);
96 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
97 EXPORT_SYMBOL(libcfs_debug_mb);
98
99 unsigned int libcfs_printk = D_CANTMASK;
100 module_param(libcfs_printk, uint, 0644);
101 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
102 EXPORT_SYMBOL(libcfs_printk);
103
104 unsigned int libcfs_console_ratelimit = 1;
105 module_param(libcfs_console_ratelimit, uint, 0644);
106 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
107 EXPORT_SYMBOL(libcfs_console_ratelimit);
108
109 static int param_set_delay_minmax(const char *val,
110                                   const struct kernel_param *kp,
111                                   long min, long max)
112 {
113         long d;
114         int sec;
115         int rc;
116
117         rc = kstrtoint(val, 0, &sec);
118         if (rc)
119                 return -EINVAL;
120
121         d = cfs_time_seconds(sec) / 100;
122         if (d < min || d > max)
123                 return -EINVAL;
124
125         *((unsigned int *)kp->arg) = d;
126
127         return 0;
128 }
129
130 static int param_get_delay(char *buffer, const struct kernel_param *kp)
131 {
132         unsigned int d = *(unsigned int *)kp->arg;
133
134         return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
135 }
136
137 unsigned int libcfs_console_max_delay;
138 EXPORT_SYMBOL(libcfs_console_max_delay);
139 unsigned int libcfs_console_min_delay;
140 EXPORT_SYMBOL(libcfs_console_min_delay);
141
142 static int param_set_console_max_delay(const char *val,
143                                        const struct kernel_param *kp)
144 {
145         return param_set_delay_minmax(val, kp,
146                                       libcfs_console_min_delay, INT_MAX);
147 }
148
149 static struct kernel_param_ops param_ops_console_max_delay = {
150         .set = param_set_console_max_delay,
151         .get = param_get_delay,
152 };
153
154 #define param_check_console_max_delay(name, p) \
155                 __param_check(name, p, unsigned int)
156
157 module_param(libcfs_console_max_delay, console_max_delay, 0644);
158 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
159
160 static int param_set_console_min_delay(const char *val,
161                                        const struct kernel_param *kp)
162 {
163         return param_set_delay_minmax(val, kp,
164                                       1, libcfs_console_max_delay);
165 }
166
167 static struct kernel_param_ops param_ops_console_min_delay = {
168         .set = param_set_console_min_delay,
169         .get = param_get_delay,
170 };
171
172 #define param_check_console_min_delay(name, p) \
173                 __param_check(name, p, unsigned int)
174
175 module_param(libcfs_console_min_delay, console_min_delay, 0644);
176 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
177
178 static int param_set_uint_minmax(const char *val,
179                                  const struct kernel_param *kp,
180                                  unsigned int min, unsigned int max)
181 {
182         unsigned int num;
183         int ret;
184
185         if (!val)
186                 return -EINVAL;
187         ret = kstrtouint(val, 0, &num);
188         if (ret < 0 || num < min || num > max)
189                 return -EINVAL;
190         *((unsigned int *)kp->arg) = num;
191         return 0;
192 }
193
194 static int param_set_uintpos(const char *val, const struct kernel_param *kp)
195 {
196         return param_set_uint_minmax(val, kp, 1, -1);
197 }
198
199 static struct kernel_param_ops param_ops_uintpos = {
200         .set = param_set_uintpos,
201         .get = param_get_uint,
202 };
203
204 #define param_check_uintpos(name, p) \
205                 __param_check(name, p, unsigned int)
206
207 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
208 module_param(libcfs_console_backoff, uintpos, 0644);
209 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
210 EXPORT_SYMBOL(libcfs_console_backoff);
211
212 unsigned int libcfs_debug_binary = 1;
213 EXPORT_SYMBOL(libcfs_debug_binary);
214
215 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
216 EXPORT_SYMBOL(libcfs_stack);
217
218 unsigned int libcfs_catastrophe;
219 EXPORT_SYMBOL(libcfs_catastrophe);
220
221 unsigned int libcfs_panic_on_lbug = 1;
222 module_param(libcfs_panic_on_lbug, uint, 0644);
223 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
224 EXPORT_SYMBOL(libcfs_panic_on_lbug);
225
226 static wait_queue_head_t debug_ctlwq;
227
228 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
229
230 /* We need to pass a pointer here, but elsewhere this must be a const */
231 static char *libcfs_debug_file_path;
232 module_param(libcfs_debug_file_path, charp, 0644);
233 MODULE_PARM_DESC(libcfs_debug_file_path,
234                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
235
236 int libcfs_panic_in_progress;
237
238 /* libcfs_debug_token2mask() expects the returned
239  * string in lower-case */
240 static const char *
241 libcfs_debug_subsys2str(int subsys)
242 {
243         switch (1 << subsys) {
244         default:
245                 return NULL;
246         case S_UNDEFINED:
247                 return "undefined";
248         case S_MDC:
249                 return "mdc";
250         case S_MDS:
251                 return "mds";
252         case S_OSC:
253                 return "osc";
254         case S_OST:
255                 return "ost";
256         case S_CLASS:
257                 return "class";
258         case S_LOG:
259                 return "log";
260         case S_LLITE:
261                 return "llite";
262         case S_RPC:
263                 return "rpc";
264         case S_LNET:
265                 return "lnet";
266         case S_LND:
267                 return "lnd";
268         case S_PINGER:
269                 return "pinger";
270         case S_FILTER:
271                 return "filter";
272         case S_ECHO:
273                 return "echo";
274         case S_LDLM:
275                 return "ldlm";
276         case S_LOV:
277                 return "lov";
278         case S_LQUOTA:
279                 return "lquota";
280         case S_OSD:
281                 return "osd";
282         case S_LMV:
283                 return "lmv";
284         case S_SEC:
285                 return "sec";
286         case S_GSS:
287                 return "gss";
288         case S_MGC:
289                 return "mgc";
290         case S_MGS:
291                 return "mgs";
292         case S_FID:
293                 return "fid";
294         case S_FLD:
295                 return "fld";
296         }
297 }
298
299 /* libcfs_debug_token2mask() expects the returned
300  * string in lower-case */
301 static const char *
302 libcfs_debug_dbg2str(int debug)
303 {
304         switch (1 << debug) {
305         default:
306                 return NULL;
307         case D_TRACE:
308                 return "trace";
309         case D_INODE:
310                 return "inode";
311         case D_SUPER:
312                 return "super";
313         case D_EXT2:
314                 return "ext2";
315         case D_MALLOC:
316                 return "malloc";
317         case D_CACHE:
318                 return "cache";
319         case D_INFO:
320                 return "info";
321         case D_IOCTL:
322                 return "ioctl";
323         case D_NETERROR:
324                 return "neterror";
325         case D_NET:
326                 return "net";
327         case D_WARNING:
328                 return "warning";
329         case D_BUFFS:
330                 return "buffs";
331         case D_OTHER:
332                 return "other";
333         case D_DENTRY:
334                 return "dentry";
335         case D_NETTRACE:
336                 return "nettrace";
337         case D_PAGE:
338                 return "page";
339         case D_DLMTRACE:
340                 return "dlmtrace";
341         case D_ERROR:
342                 return "error";
343         case D_EMERG:
344                 return "emerg";
345         case D_HA:
346                 return "ha";
347         case D_RPCTRACE:
348                 return "rpctrace";
349         case D_VFSTRACE:
350                 return "vfstrace";
351         case D_READA:
352                 return "reada";
353         case D_MMAP:
354                 return "mmap";
355         case D_CONFIG:
356                 return "config";
357         case D_CONSOLE:
358                 return "console";
359         case D_QUOTA:
360                 return "quota";
361         case D_SEC:
362                 return "sec";
363         case D_LFSCK:
364                 return "lfsck";
365         }
366 }
367
368 int
369 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
370 {
371         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
372                                                  libcfs_debug_dbg2str;
373         int        len = 0;
374         const char   *token;
375         int        i;
376
377         if (mask == 0) {                        /* "0" */
378                 if (size > 0)
379                         str[0] = '0';
380                 len = 1;
381         } else {                                /* space-separated tokens */
382                 for (i = 0; i < 32; i++) {
383                         if ((mask & (1 << i)) == 0)
384                                 continue;
385
386                         token = fn(i);
387                         if (token == NULL)            /* unused bit */
388                                 continue;
389
390                         if (len > 0) {            /* separator? */
391                                 if (len < size)
392                                         str[len] = ' ';
393                                 len++;
394                         }
395
396                         while (*token != 0) {
397                                 if (len < size)
398                                         str[len] = *token;
399                                 token++;
400                                 len++;
401                         }
402                 }
403         }
404
405         /* terminate 'str' */
406         if (len < size)
407                 str[len] = 0;
408         else
409                 str[size - 1] = 0;
410
411         return len;
412 }
413
414 int
415 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
416 {
417         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
418                                                  libcfs_debug_dbg2str;
419         int      m = 0;
420         int      matched;
421         int      n;
422         int      t;
423
424         /* Allow a number for backwards compatibility */
425
426         for (n = strlen(str); n > 0; n--)
427                 if (!isspace(str[n-1]))
428                         break;
429         matched = n;
430         t = sscanf(str, "%i%n", &m, &matched);
431         if (t >= 1 && matched == n) {
432                 /* don't print warning for lctl set_param debug=0 or -1 */
433                 if (m != 0 && m != -1)
434                         CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
435                 *mask = m;
436                 return 0;
437         }
438
439         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
440                             0xffffffff);
441 }
442
443 /**
444  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
445  */
446 void libcfs_debug_dumplog_internal(void *arg)
447 {
448         void *journal_info;
449
450         journal_info = current->journal_info;
451         current->journal_info = NULL;
452
453         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
454                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
455                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
456                          (s64)ktime_get_real_seconds(), (long_ptr_t)arg);
457                 pr_alert("LustreError: dumping log to %s\n",
458                        debug_file_name);
459                 cfs_tracefile_dump_all_pages(debug_file_name);
460                 libcfs_run_debug_log_upcall(debug_file_name);
461         }
462
463         current->journal_info = journal_info;
464 }
465
466 static int libcfs_debug_dumplog_thread(void *arg)
467 {
468         libcfs_debug_dumplog_internal(arg);
469         wake_up(&debug_ctlwq);
470         return 0;
471 }
472
473 void libcfs_debug_dumplog(void)
474 {
475         wait_queue_t wait;
476         struct task_struct *dumper;
477
478         /* we're being careful to ensure that the kernel thread is
479          * able to set our state to running as it exits before we
480          * get to schedule() */
481         init_waitqueue_entry(&wait, current);
482         set_current_state(TASK_INTERRUPTIBLE);
483         add_wait_queue(&debug_ctlwq, &wait);
484
485         dumper = kthread_run(libcfs_debug_dumplog_thread,
486                              (void *)(long)current_pid(),
487                              "libcfs_debug_dumper");
488         if (IS_ERR(dumper))
489                 pr_err("LustreError: cannot start log dump thread: %ld\n",
490                        PTR_ERR(dumper));
491         else
492                 schedule();
493
494         /* be sure to teardown if cfs_create_thread() failed */
495         remove_wait_queue(&debug_ctlwq, &wait);
496         set_current_state(TASK_RUNNING);
497 }
498 EXPORT_SYMBOL(libcfs_debug_dumplog);
499
500 int libcfs_debug_init(unsigned long bufsize)
501 {
502         int    rc = 0;
503         unsigned int max = libcfs_debug_mb;
504
505         init_waitqueue_head(&debug_ctlwq);
506
507         if (libcfs_console_max_delay <= 0 || /* not set by user or */
508             libcfs_console_min_delay <= 0 || /* set to invalid values */
509             libcfs_console_min_delay >= libcfs_console_max_delay) {
510                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
511                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
512         }
513
514         if (libcfs_debug_file_path != NULL) {
515                 strncpy(libcfs_debug_file_path_arr,
516                         libcfs_debug_file_path, PATH_MAX-1);
517                 libcfs_debug_file_path_arr[PATH_MAX - 1] = '\0';
518         }
519
520         /* If libcfs_debug_mb is set to an invalid value or uninitialized
521          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
522         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
523                 max = TCD_MAX_PAGES;
524         } else {
525                 max = max / num_possible_cpus();
526                 max <<= (20 - PAGE_CACHE_SHIFT);
527         }
528         rc = cfs_tracefile_init(max);
529
530         if (rc == 0) {
531                 libcfs_register_panic_notifier();
532                 libcfs_debug_mb = cfs_trace_get_debug_mb();
533         }
534
535         return rc;
536 }
537
538 int libcfs_debug_cleanup(void)
539 {
540         libcfs_unregister_panic_notifier();
541         cfs_tracefile_exit();
542         return 0;
543 }
544
545 int libcfs_debug_clear_buffer(void)
546 {
547         cfs_trace_flush_pages();
548         return 0;
549 }
550
551 /* Debug markers, although printed by S_LNET
552  * should not be be marked as such. */
553 #undef DEBUG_SUBSYSTEM
554 #define DEBUG_SUBSYSTEM S_UNDEFINED
555 int libcfs_debug_mark_buffer(const char *text)
556 {
557         CDEBUG(D_TRACE,
558                "***************************************************\n");
559         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
560         CDEBUG(D_TRACE,
561                "***************************************************\n");
562
563         return 0;
564 }
565
566 #undef DEBUG_SUBSYSTEM
567 #define DEBUG_SUBSYSTEM S_LNET
568
569 void libcfs_debug_set_level(unsigned int debug_level)
570 {
571         pr_warn("Lustre: Setting portals debug level to %08x\n",
572                debug_level);
573         libcfs_debug = debug_level;
574 }
575
576 EXPORT_SYMBOL(libcfs_debug_set_level);