Whamcloud - gitweb
LU-12349 llite: console message for disabled flock call
[fs/lustre-release.git] / libcfs / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  *
36  */
37
38 # define DEBUG_SUBSYSTEM S_LNET
39
40 #include <linux/ctype.h>
41 #include <linux/kthread.h>
42 #include <libcfs/libcfs.h>
43 #include "tracefile.h"
44
45 static char debug_file_name[1024];
46
47 unsigned int libcfs_subsystem_debug = ~0;
48 module_param(libcfs_subsystem_debug, int, 0644);
49 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
50 EXPORT_SYMBOL(libcfs_subsystem_debug);
51
52 unsigned int libcfs_debug = (D_CANTMASK | D_NETERROR | D_HA | D_CONFIG |
53                              D_IOCTL | D_LFSCK | D_TTY);
54 module_param(libcfs_debug, int, 0644);
55 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
56 EXPORT_SYMBOL(libcfs_debug);
57
58 static int libcfs_param_debug_mb_set(const char *val,
59                                      cfs_kernel_param_arg_t *kp)
60 {
61         int rc;
62         unsigned int num;
63
64         rc = kstrtouint(val, 0, &num);
65         if (rc < 0)
66                 return rc;
67
68 /*
69  * RHEL6 does not support any kind of locking so we have to provide
70  * our own
71  */
72 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
73         kernel_param_lock(THIS_MODULE);
74 #endif
75         if (!*((unsigned int *)kp->arg)) {
76                 *((unsigned int *)kp->arg) = num;
77
78 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
79                 kernel_param_unlock(THIS_MODULE);
80 #endif
81                 return 0;
82         }
83
84         rc = cfs_trace_set_debug_mb(num);
85
86         if (!rc)
87                 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
88
89 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
90         kernel_param_unlock(THIS_MODULE);
91 #endif
92         return rc;
93 }
94
95 /*
96  * While debug_mb setting look like unsigned int, in fact
97  * it needs quite a bunch of extra processing, so we define special
98  * debug_mb parameter type with corresponding methods to handle this case
99  */
100 static struct kernel_param_ops param_ops_debug_mb = {
101         .set = libcfs_param_debug_mb_set,
102         .get = param_get_uint,
103 };
104
105 #define param_check_debug_mb(name, p) \
106                 __param_check(name, p, unsigned int)
107
108 static unsigned int libcfs_debug_mb;
109 #ifdef HAVE_KERNEL_PARAM_OPS
110 module_param(libcfs_debug_mb, debug_mb, 0644);
111 #else
112 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
113                   &param_ops_debug_mb, 0644);
114 #endif
115 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
116
117 unsigned int libcfs_printk = D_CANTMASK;
118 module_param(libcfs_printk, uint, 0644);
119 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
120
121 unsigned int libcfs_console_ratelimit = 1;
122 module_param(libcfs_console_ratelimit, uint, 0644);
123 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
124
125 static int param_set_delay_minmax(const char *val,
126                                   cfs_kernel_param_arg_t *kp,
127                                   long min, long max)
128 {
129         long d;
130         int sec;
131         int rc;
132
133         rc = kstrtoint(val, 0, &sec);
134         if (rc)
135                 return -EINVAL;
136
137         /* The sysfs setting is in centiseconds */
138         d = cfs_time_seconds(sec) / 100;
139         if (d < min || d > max)
140                 return -EINVAL;
141
142         *((unsigned int *)kp->arg) = d;
143
144         return 0;
145 }
146
147 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
148 {
149         unsigned int d = *(unsigned int *)kp->arg;
150
151         return sprintf(buffer, "%lu", jiffies_to_msecs(d * 10) / MSEC_PER_SEC);
152 }
153
154 unsigned int libcfs_console_max_delay;
155 unsigned int libcfs_console_min_delay;
156
157 static int param_set_console_max_delay(const char *val,
158                                        cfs_kernel_param_arg_t *kp)
159 {
160         return param_set_delay_minmax(val, kp,
161                                       libcfs_console_min_delay, INT_MAX);
162 }
163
164 static struct kernel_param_ops param_ops_console_max_delay = {
165         .set = param_set_console_max_delay,
166         .get = param_get_delay,
167 };
168
169 #define param_check_console_max_delay(name, p) \
170                 __param_check(name, p, unsigned int)
171
172 #ifdef HAVE_KERNEL_PARAM_OPS
173 module_param(libcfs_console_max_delay, console_max_delay, 0644);
174 #else
175 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
176                   param_get_delay, &param_ops_console_max_delay, 0644);
177 #endif
178 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
179
180 static int param_set_console_min_delay(const char *val,
181                                        cfs_kernel_param_arg_t *kp)
182 {
183         return param_set_delay_minmax(val, kp,
184                                       1, libcfs_console_max_delay);
185 }
186
187 static struct kernel_param_ops param_ops_console_min_delay = {
188         .set = param_set_console_min_delay,
189         .get = param_get_delay,
190 };
191
192 #define param_check_console_min_delay(name, p) \
193                 __param_check(name, p, unsigned int)
194
195 #ifdef HAVE_KERNEL_PARAM_OPS
196 module_param(libcfs_console_min_delay, console_min_delay, 0644);
197 #else
198 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
199                   param_get_delay, &param_ops_console_min_delay, 0644);
200 #endif
201 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
202
203 static int param_set_uint_minmax(const char *val,
204                                  cfs_kernel_param_arg_t *kp,
205                                  unsigned int min, unsigned int max)
206 {
207         unsigned int num;
208         int ret;
209
210         if (!val)
211                 return -EINVAL;
212
213         ret = kstrtouint(val, 0, &num);
214         if (ret < 0 || num < min || num > max)
215                 return -EINVAL;
216
217         *((unsigned int *)kp->arg) = num;
218         return 0;
219 }
220
221 static int param_set_uintpos(const char *val,
222                              cfs_kernel_param_arg_t *kp)
223 {
224         return param_set_uint_minmax(val, kp, 1, -1);
225 }
226
227 static struct kernel_param_ops param_ops_uintpos = {
228         .set = param_set_uintpos,
229         .get = param_get_uint,
230 };
231
232 #define param_check_uintpos(name, p) \
233                 __param_check(name, p, unsigned int)
234
235 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
236 #ifdef HAVE_KERNEL_PARAM_OPS
237 module_param(libcfs_console_backoff, uintpos, 0644);
238 #else
239 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
240                   &param_ops_uintpos, 0644);
241 #endif
242 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
243
244 unsigned int libcfs_debug_binary = 1;
245
246 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
247 EXPORT_SYMBOL(libcfs_stack);
248
249 unsigned int libcfs_catastrophe;
250 EXPORT_SYMBOL(libcfs_catastrophe);
251
252 unsigned int libcfs_watchdog_ratelimit = 300;
253 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
254
255 unsigned int libcfs_panic_on_lbug = 1;
256 module_param(libcfs_panic_on_lbug, uint, 0644);
257 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
258
259 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
260 EXPORT_SYMBOL(libcfs_kmemory);
261
262 static wait_queue_head_t debug_ctlwq;
263
264 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
265 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
266
267 /* We need to pass a pointer here, but elsewhere this must be a const */
268 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
269 module_param(libcfs_debug_file_path, charp, 0644);
270 MODULE_PARM_DESC(libcfs_debug_file_path,
271                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
272
273 int libcfs_panic_in_progress;
274
275 /*
276  * libcfs_debug_token2mask() expects the returned
277  * string in lower-case
278  */
279 static const char *libcfs_debug_subsys2str(int subsys)
280 {
281         static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
282
283         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
284                 return NULL;
285
286         return libcfs_debug_subsystems[subsys];
287 }
288
289 /*
290  * libcfs_debug_token2mask() expects the returned
291  * string in lower-case
292  */
293 static const char *libcfs_debug_dbg2str(int debug)
294 {
295         static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
296
297         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
298                 return NULL;
299
300         return libcfs_debug_masks[debug];
301 }
302
303 int
304 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
305 {
306         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
307                                                  libcfs_debug_dbg2str;
308         int len = 0;
309         const char *token;
310         int i;
311
312         if (mask == 0) {                        /* "0" */
313                 if (size > 0)
314                         str[0] = '0';
315                 len = 1;
316         } else {                                /* space-separated tokens */
317                 for (i = 0; i < 32; i++) {
318                         if ((mask & (1 << i)) == 0)
319                                 continue;
320
321                         token = fn(i);
322                         if (token == NULL)      /* unused bit */
323                                 continue;
324
325                         if (len > 0) {          /* separator? */
326                                 if (len < size)
327                                         str[len] = ' ';
328                                 len++;
329                         }
330
331                         while (*token != 0) {
332                                 if (len < size)
333                                         str[len] = *token;
334                                 token++;
335                                 len++;
336                         }
337                 }
338         }
339
340         /* terminate 'str' */
341         if (len < size)
342                 str[len] = 0;
343         else
344                 str[size - 1] = 0;
345
346         return len;
347 }
348
349 int
350 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
351 {
352         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
353                                                  libcfs_debug_dbg2str;
354         int m = 0;
355         int matched;
356         int n;
357         int t;
358
359         /* Allow a number for backwards compatibility */
360         for (n = strlen(str); n > 0; n--)
361                 if (!isspace(str[n-1]))
362                         break;
363         matched = n;
364
365         t = sscanf(str, "%i%n", &m, &matched);
366         if (t >= 1 && matched == n) {
367                 /* don't print warning for lctl set_param debug=0 or -1 */
368                 if (m != 0 && m != -1)
369                         CWARN("You are trying to use a numerical value for the "
370                               "mask - this will be deprecated in a future "
371                               "release.\n");
372                 *mask = m;
373                 return 0;
374         }
375
376         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
377                             0xffffffff);
378 }
379
380 /**
381  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
382  */
383 void libcfs_debug_dumplog_internal(void *arg)
384 {
385         static time64_t last_dump_time;
386         time64_t current_time;
387         void *journal_info;
388
389         journal_info = current->journal_info;
390         current->journal_info = NULL;
391         current_time = ktime_get_real_seconds();
392
393         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
394             current_time > last_dump_time) {
395                 last_dump_time = current_time;
396                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
397                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
398                          (s64)current_time, (uintptr_t)arg);
399                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
400                        debug_file_name);
401                 cfs_tracefile_dump_all_pages(debug_file_name);
402                 libcfs_run_debug_log_upcall(debug_file_name);
403         }
404         current->journal_info = journal_info;
405 }
406
407 static int libcfs_debug_dumplog_thread(void *arg)
408 {
409         libcfs_debug_dumplog_internal(arg);
410         wake_up(&debug_ctlwq);
411         return 0;
412 }
413
414 void libcfs_debug_dumplog(void)
415 {
416         wait_queue_entry_t wait;
417         struct task_struct *dumper;
418
419         ENTRY;
420
421         /*
422          * we're being careful to ensure that the kernel thread is
423          * able to set our state to running as it exits before we
424          * get to schedule()
425          */
426         init_waitqueue_entry(&wait, current);
427         set_current_state(TASK_INTERRUPTIBLE);
428         add_wait_queue(&debug_ctlwq, &wait);
429
430         dumper = kthread_run(libcfs_debug_dumplog_thread,
431                              (void *)(long)current_pid(),
432                              "libcfs_debug_dumper");
433         if (IS_ERR(dumper))
434                 printk(KERN_ERR "LustreError: cannot start log dump thread:"
435                        " %ld\n", PTR_ERR(dumper));
436         else
437                 schedule();
438
439         /* be sure to teardown if cfs_create_thread() failed */
440         remove_wait_queue(&debug_ctlwq, &wait);
441         set_current_state(TASK_RUNNING);
442 }
443 EXPORT_SYMBOL(libcfs_debug_dumplog);
444
445 int libcfs_debug_init(unsigned long bufsize)
446 {
447         int rc = 0;
448         unsigned int max = libcfs_debug_mb;
449
450         init_waitqueue_head(&debug_ctlwq);
451
452         if (libcfs_console_max_delay <= 0 || /* not set by user or */
453             libcfs_console_min_delay <= 0 || /* set to invalid values */
454             libcfs_console_min_delay >= libcfs_console_max_delay) {
455                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
456                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
457         }
458
459         if (libcfs_debug_file_path != NULL) {
460                 strlcpy(libcfs_debug_file_path_arr,
461                         libcfs_debug_file_path,
462                         sizeof(libcfs_debug_file_path_arr));
463         }
464
465         /*
466          * If libcfs_debug_mb is set to an invalid value or uninitialized
467          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES
468          */
469         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
470                 max = TCD_MAX_PAGES;
471         } else {
472                 max = (max / num_possible_cpus());
473                 max = (max << (20 - PAGE_SHIFT));
474         }
475
476         rc = cfs_tracefile_init(max);
477         if (rc)
478                 return rc;
479
480         libcfs_register_panic_notifier();
481         kernel_param_lock(THIS_MODULE);
482         if (libcfs_debug_mb == 0)
483                 libcfs_debug_mb = cfs_trace_get_debug_mb();
484         kernel_param_unlock(THIS_MODULE);
485         return rc;
486 }
487
488 int libcfs_debug_cleanup(void)
489 {
490         libcfs_unregister_panic_notifier();
491         kernel_param_lock(THIS_MODULE);
492         cfs_tracefile_exit();
493         kernel_param_unlock(THIS_MODULE);
494         return 0;
495 }
496
497 int libcfs_debug_clear_buffer(void)
498 {
499         cfs_trace_flush_pages();
500         return 0;
501 }
502
503 /*
504  * Debug markers, although printed by S_LNET
505  * should not be be marked as such.
506  */
507 #undef DEBUG_SUBSYSTEM
508 #define DEBUG_SUBSYSTEM S_UNDEFINED
509 int libcfs_debug_mark_buffer(const char *text)
510 {
511         CDEBUG(D_TRACE, "**************************************************\n");
512         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
513         CDEBUG(D_TRACE, "**************************************************\n");
514
515         return 0;
516 }
517 #undef DEBUG_SUBSYSTEM
518 #define DEBUG_SUBSYSTEM S_LNET
519
520 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
521 {
522         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
523                          rc, rc, rc);
524         return rc;
525 }
526 EXPORT_SYMBOL(libcfs_log_return);
527
528 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
529                      long rc)
530 {
531         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
532                          " : %#lx)\n", label, rc, rc, rc);
533 }
534 EXPORT_SYMBOL(libcfs_log_goto);