Whamcloud - gitweb
LU-3544 nfs: writing to new files will return ENOENT
[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.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 <libcfs/libcfs.h>
45 #include "tracefile.h"
46
47 static char debug_file_name[1024];
48
49 unsigned int libcfs_subsystem_debug = ~0;
50 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
51                 "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 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
57                 "Lustre kernel debug mask");
58 EXPORT_SYMBOL(libcfs_debug);
59
60 unsigned int libcfs_debug_mb = 0;
61 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
62                 "Total debug buffer size.");
63 EXPORT_SYMBOL(libcfs_debug_mb);
64
65 unsigned int libcfs_printk = D_CANTMASK;
66 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
67                 "Lustre kernel debug console mask");
68 EXPORT_SYMBOL(libcfs_printk);
69
70 unsigned int libcfs_console_ratelimit = 1;
71 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
72                 "Lustre kernel debug console ratelimit (0 to disable)");
73 EXPORT_SYMBOL(libcfs_console_ratelimit);
74
75 unsigned int libcfs_console_max_delay;
76 CFS_MODULE_PARM(libcfs_console_max_delay, "l", uint, 0644,
77                 "Lustre kernel debug console max delay (jiffies)");
78 EXPORT_SYMBOL(libcfs_console_max_delay);
79
80 unsigned int libcfs_console_min_delay;
81 CFS_MODULE_PARM(libcfs_console_min_delay, "l", uint, 0644,
82                 "Lustre kernel debug console min delay (jiffies)");
83 EXPORT_SYMBOL(libcfs_console_min_delay);
84
85 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
86 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
87                 "Lustre kernel debug console backoff factor");
88 EXPORT_SYMBOL(libcfs_console_backoff);
89
90 unsigned int libcfs_debug_binary = 1;
91 EXPORT_SYMBOL(libcfs_debug_binary);
92
93 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
94 EXPORT_SYMBOL(libcfs_stack);
95
96 unsigned int portal_enter_debugger;
97 EXPORT_SYMBOL(portal_enter_debugger);
98
99 unsigned int libcfs_catastrophe;
100 EXPORT_SYMBOL(libcfs_catastrophe);
101
102 unsigned int libcfs_watchdog_ratelimit = 300;
103 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
104
105 unsigned int libcfs_panic_on_lbug = 1;
106 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
107                 "Lustre kernel panic on LBUG");
108 EXPORT_SYMBOL(libcfs_panic_on_lbug);
109
110 cfs_atomic_t libcfs_kmemory = CFS_ATOMIC_INIT(0);
111 EXPORT_SYMBOL(libcfs_kmemory);
112
113 static cfs_waitq_t debug_ctlwq;
114
115 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
116
117 /* We need to pass a pointer here, but elsewhere this must be a const */
118 char *libcfs_debug_file_path;
119 CFS_MODULE_PARM(libcfs_debug_file_path, "s", charp, 0644,
120                 "Path for dumping debug logs, "
121                 "set 'NONE' to prevent log dumping");
122
123 int libcfs_panic_in_progress;
124
125 /* libcfs_debug_token2mask() expects the returned
126  * string in lower-case */
127 const char *
128 libcfs_debug_subsys2str(int subsys)
129 {
130         switch (1 << subsys) {
131         default:
132                 return NULL;
133         case S_UNDEFINED:
134                 return "undefined";
135         case S_MDC:
136                 return "mdc";
137         case S_MDS:
138                 return "mds";
139         case S_OSC:
140                 return "osc";
141         case S_OST:
142                 return "ost";
143         case S_CLASS:
144                 return "class";
145         case S_LOG:
146                 return "log";
147         case S_LLITE:
148                 return "llite";
149         case S_RPC:
150                 return "rpc";
151         case S_LNET:
152                 return "lnet";
153         case S_LND:
154                 return "lnd";
155         case S_PINGER:
156                 return "pinger";
157         case S_FILTER:
158                 return "filter";
159         case S_ECHO:
160                 return "echo";
161         case S_LDLM:
162                 return "ldlm";
163         case S_LOV:
164                 return "lov";
165         case S_LQUOTA:
166                 return "lquota";
167         case S_OSD:
168                 return "osd";
169         case S_LFSCK:
170                 return "lfsck";
171         case S_LMV:
172                 return "lmv";
173         case S_SEC:
174                 return "sec";
175         case S_GSS:
176                 return "gss";
177         case S_MGC:
178                 return "mgc";
179         case S_MGS:
180                 return "mgs";
181         case S_FID:
182                 return "fid";
183         case S_FLD:
184                 return "fld";
185         }
186 }
187
188 /* libcfs_debug_token2mask() expects the returned
189  * string in lower-case */
190 const char *
191 libcfs_debug_dbg2str(int debug)
192 {
193         switch (1 << debug) {
194         default:
195                 return NULL;
196         case D_TRACE:
197                 return "trace";
198         case D_INODE:
199                 return "inode";
200         case D_SUPER:
201                 return "super";
202         case D_EXT2:
203                 return "ext2";
204         case D_MALLOC:
205                 return "malloc";
206         case D_CACHE:
207                 return "cache";
208         case D_INFO:
209                 return "info";
210         case D_IOCTL:
211                 return "ioctl";
212         case D_NETERROR:
213                 return "neterror";
214         case D_NET:
215                 return "net";
216         case D_WARNING:
217                 return "warning";
218         case D_BUFFS:
219                 return "buffs";
220         case D_OTHER:
221                 return "other";
222         case D_DENTRY:
223                 return "dentry";
224         case D_NETTRACE:
225                 return "nettrace";
226         case D_PAGE:
227                 return "page";
228         case D_DLMTRACE:
229                 return "dlmtrace";
230         case D_ERROR:
231                 return "error";
232         case D_EMERG:
233                 return "emerg";
234         case D_HA:
235                 return "ha";
236         case D_RPCTRACE:
237                 return "rpctrace";
238         case D_VFSTRACE:
239                 return "vfstrace";
240         case D_READA:
241                 return "reada";
242         case D_MMAP:
243                 return "mmap";
244         case D_CONFIG:
245                 return "config";
246         case D_CONSOLE:
247                 return "console";
248         case D_QUOTA:
249                 return "quota";
250         case D_SEC:
251                 return "sec";
252         case D_LFSCK:
253                 return "lfsck";
254         }
255 }
256
257 int
258 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
259 {
260         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
261                                                  libcfs_debug_dbg2str;
262         int           len = 0;
263         const char   *token;
264         int           i;
265
266         if (mask == 0) {                        /* "0" */
267                 if (size > 0)
268                         str[0] = '0';
269                 len = 1;
270         } else {                                /* space-separated tokens */
271                 for (i = 0; i < 32; i++) {
272                         if ((mask & (1 << i)) == 0)
273                                 continue;
274
275                         token = fn(i);
276                         if (token == NULL)              /* unused bit */
277                                 continue;
278
279                         if (len > 0) {                  /* separator? */
280                                 if (len < size)
281                                         str[len] = ' ';
282                                 len++;
283                         }
284
285                         while (*token != 0) {
286                                 if (len < size)
287                                         str[len] = *token;
288                                 token++;
289                                 len++;
290                         }
291                 }
292         }
293
294         /* terminate 'str' */
295         if (len < size)
296                 str[len] = 0;
297         else
298                 str[size - 1] = 0;
299
300         return len;
301 }
302
303 int
304 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
305 {
306         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
307                                                  libcfs_debug_dbg2str;
308         int         m = 0;
309         int         matched;
310         int         n;
311         int         t;
312
313         /* Allow a number for backwards compatibility */
314
315         for (n = strlen(str); n > 0; n--)
316                 if (!isspace(str[n-1]))
317                         break;
318         matched = n;
319
320         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
321             matched == n) {
322                 /* don't print warning for lctl set_param debug=0 or -1 */
323                 if (m != 0 && m != -1)
324                         CWARN("You are trying to use a numerical value for the "
325                               "mask - this will be deprecated in a future "
326                               "release.\n");
327                 *mask = m;
328                 return 0;
329         }
330
331         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
332                             0xffffffff);
333 }
334
335 /**
336  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
337  */
338 void libcfs_debug_dumplog_internal(void *arg)
339 {
340         CFS_DECL_JOURNAL_DATA;
341
342         CFS_PUSH_JOURNAL;
343
344         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
345                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
346                          "%s.%ld." LPLD, libcfs_debug_file_path_arr,
347                          cfs_time_current_sec(), (long_ptr_t)arg);
348                 printk(CFS_KERN_ALERT "LustreError: dumping log to %s\n",
349                        debug_file_name);
350                 cfs_tracefile_dump_all_pages(debug_file_name);
351                 libcfs_run_debug_log_upcall(debug_file_name);
352         }
353         CFS_POP_JOURNAL;
354 }
355
356 int libcfs_debug_dumplog_thread(void *arg)
357 {
358         libcfs_debug_dumplog_internal(arg);
359         cfs_waitq_signal(&debug_ctlwq);
360         return 0;
361 }
362
363 void libcfs_debug_dumplog(void)
364 {
365         cfs_waitlink_t wait;
366         cfs_task_t    *dumper;
367         ENTRY;
368
369         /* we're being careful to ensure that the kernel thread is
370          * able to set our state to running as it exits before we
371          * get to schedule() */
372         cfs_waitlink_init(&wait);
373         cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
374         cfs_waitq_add(&debug_ctlwq, &wait);
375
376         dumper = kthread_run(libcfs_debug_dumplog_thread,
377                              (void *)(long)cfs_curproc_pid(),
378                              "libcfs_debug_dumper");
379         if (IS_ERR(dumper))
380                 printk(CFS_KERN_ERR "LustreError: cannot start log dump thread:"
381                        " %ld\n", PTR_ERR(dumper));
382         else
383                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
384
385         /* be sure to teardown if cfs_create_thread() failed */
386         cfs_waitq_del(&debug_ctlwq, &wait);
387         cfs_set_current_state(CFS_TASK_RUNNING);
388 }
389 EXPORT_SYMBOL(libcfs_debug_dumplog);
390
391 int libcfs_debug_init(unsigned long bufsize)
392 {
393         int    rc = 0;
394         unsigned int max = libcfs_debug_mb;
395
396         cfs_waitq_init(&debug_ctlwq);
397
398         if (libcfs_console_max_delay <= 0 || /* not set by user or */
399             libcfs_console_min_delay <= 0 || /* set to invalid values */
400             libcfs_console_min_delay >= libcfs_console_max_delay) {
401                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
402                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
403         }
404
405         if (libcfs_debug_file_path != NULL) {
406                 memset(libcfs_debug_file_path_arr, 0, PATH_MAX);
407                 strncpy(libcfs_debug_file_path_arr, 
408                         libcfs_debug_file_path, PATH_MAX-1);
409         }
410
411         /* If libcfs_debug_mb is set to an invalid value or uninitialized
412          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
413         if (max > cfs_trace_max_debug_mb() || max < cfs_num_possible_cpus()) {
414                 max = TCD_MAX_PAGES;
415         } else {
416                 max = (max / cfs_num_possible_cpus());
417                 max = (max << (20 - PAGE_CACHE_SHIFT));
418         }
419         rc = cfs_tracefile_init(max);
420
421         if (rc == 0)
422                 libcfs_register_panic_notifier();
423
424         return rc;
425 }
426
427 int libcfs_debug_cleanup(void)
428 {
429         libcfs_unregister_panic_notifier();
430         cfs_tracefile_exit();
431         return 0;
432 }
433
434 int libcfs_debug_clear_buffer(void)
435 {
436         cfs_trace_flush_pages();
437         return 0;
438 }
439
440 /* Debug markers, although printed by S_LNET
441  * should not be be marked as such. */
442 #undef DEBUG_SUBSYSTEM
443 #define DEBUG_SUBSYSTEM S_UNDEFINED
444 int libcfs_debug_mark_buffer(const char *text)
445 {
446         CDEBUG(D_TRACE,"***************************************************\n");
447         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
448         CDEBUG(D_TRACE,"***************************************************\n");
449
450         return 0;
451 }
452 #undef DEBUG_SUBSYSTEM
453 #define DEBUG_SUBSYSTEM S_LNET
454
455 void libcfs_debug_set_level(unsigned int debug_level)
456 {
457         printk(CFS_KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
458                debug_level);
459         libcfs_debug = debug_level;
460 }
461
462 EXPORT_SYMBOL(libcfs_debug_set_level);
463
464 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
465 {
466         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
467                          rc, rc, rc);
468         return rc;
469 }
470 EXPORT_SYMBOL(libcfs_log_return);
471
472 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
473                      long_ptr_t rc)
474 {
475         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=" LPLU " : " LPLD
476                          " : " LPLX ")\n", label, (ulong_ptr_t)rc, rc, rc);
477 }
478 EXPORT_SYMBOL(libcfs_log_goto);