4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 #include <linux/module.h>
32 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/stat.h>
36 #include <linux/errno.h>
37 #include <linux/unistd.h>
39 #include <linux/uio.h>
40 #include <linux/uaccess.h>
43 #include <linux/file.h>
44 #include <linux/list.h>
46 #include <linux/sysctl.h>
47 #include <linux/debugfs.h>
48 #include <asm/div64.h>
50 #define DEBUG_SUBSYSTEM S_LNET
52 #include <libcfs/libcfs.h>
53 #include <libcfs/libcfs_crypto.h>
54 #include <lnet/lib-lnet.h>
55 #include <lustre_crypto.h>
56 #include "tracefile.h"
58 struct lnet_debugfs_symlink_def {
63 static struct dentry *lnet_debugfs_root;
65 int libcfs_ioctl(unsigned int cmd, struct libcfs_ioctl_data *data)
68 case IOC_LIBCFS_CLEAR_DEBUG:
69 libcfs_debug_clear_buffer();
71 case IOC_LIBCFS_MARK_DEBUG:
73 data->ioc_inlbuf1 == NULL ||
74 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
77 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
85 EXPORT_SYMBOL(libcfs_ioctl);
87 static int proc_dobitmasks(struct ctl_table *table, int write,
88 void __user *buffer, size_t *lenp, loff_t *ppos)
90 const int tmpstrlen = 512;
95 unsigned int *mask = table->data;
96 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
97 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
100 tmpstr = kmalloc(tmpstrlen, GFP_KERNEL | __GFP_ZERO);
103 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
109 rc = cfs_trace_copyout_string(buffer, nob,
113 tmpstr = memdup_user_nul(buffer, nob);
115 return PTR_ERR(tmpstr);
117 rc = libcfs_debug_str2mask(mask, strim(tmpstr), is_subsys);
118 /* Always print LBUG/LASSERT to console, so keep this mask */
127 static int min_watchdog_ratelimit; /* disable ratelimiting */
128 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
130 static int proc_dump_kernel(struct ctl_table *table, int write,
131 void __user *buffer, size_t *lenp, loff_t *ppos)
138 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
141 static int proc_daemon_file(struct ctl_table *table, int write,
142 void __user *buffer, size_t *lenp, loff_t *ppos)
148 int len = strlen(cfs_tracefile);
153 return cfs_trace_copyout_string(buffer, nob,
154 cfs_tracefile + pos, "\n");
157 return cfs_trace_daemon_command_usrstr(buffer, nob);
160 static int libcfs_force_lbug(struct ctl_table *table, int write,
162 size_t *lenp, loff_t *ppos)
169 static int proc_fail_loc(struct ctl_table *table, int write,
170 void __user *buffer, size_t *lenp, loff_t *ppos)
173 long old_fail_loc = cfs_fail_loc;
175 if (!*lenp || *ppos) {
181 char *kbuf = memdup_user_nul(buffer, *lenp);
184 return PTR_ERR(kbuf);
185 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
191 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
192 if (copy_to_user(buffer, kbuf, rc))
200 if (old_fail_loc != cfs_fail_loc) {
202 wake_up(&cfs_race_waitq);
207 int debugfs_doint(struct ctl_table *table, int write,
208 void __user *buffer, size_t *lenp, loff_t *ppos)
212 if (!*lenp || *ppos) {
218 char *kbuf = memdup_user_nul(buffer, *lenp);
222 return PTR_ERR(kbuf);
224 rc = kstrtoint(kbuf, 0, &val);
227 if (table->extra1 && val < *(int *)table->extra1)
228 val = *(int *)table->extra1;
229 if (table->extra2 && val > *(int *)table->extra2)
230 val = *(int *)table->extra2;
231 *(int *)table->data = val;
237 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
238 if (copy_to_user(buffer, kbuf, rc))
248 EXPORT_SYMBOL(debugfs_doint);
250 static int debugfs_dou64(struct ctl_table *table, int write,
251 void __user *buffer, size_t *lenp, loff_t *ppos)
255 if (!*lenp || *ppos) {
261 char *kbuf = memdup_user_nul(buffer, *lenp);
262 unsigned long long val;
265 return PTR_ERR(kbuf);
267 rc = kstrtoull(kbuf, 0, &val);
270 *(u64 *)table->data = val;
275 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
276 (unsigned long long)*(u64 *)table->data);
277 if (copy_to_user(buffer, kbuf, rc))
288 static int debugfs_dostring(struct ctl_table *table, int write,
289 void __user *buffer, size_t *lenp, loff_t *ppos)
292 char *kbuf = table->data;
298 if (len > table->maxlen)
301 if (copy_from_user(kbuf, buffer, len))
303 memset(kbuf+len, 0, table->maxlen - len);
306 len = strnlen(kbuf, len);
307 if (copy_to_user(buffer, kbuf, len))
310 if (copy_to_user(buffer+len, "\n", 1))
320 static int proc_cpt_table(struct ctl_table *table, int write,
321 void __user *buffer, size_t *lenp, loff_t *ppos)
333 LIBCFS_ALLOC(buf, len);
337 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
342 LIBCFS_FREE(buf, len);
354 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
357 LIBCFS_FREE(buf, len);
361 static int proc_cpt_distance(struct ctl_table *table, int write,
362 void __user *buffer, size_t *lenp, loff_t *ppos)
374 LIBCFS_ALLOC(buf, len);
378 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
383 LIBCFS_FREE(buf, len);
395 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
398 LIBCFS_FREE(buf, len);
402 static struct ctl_table lnet_table[] = {
405 .data = &libcfs_debug,
406 .maxlen = sizeof(int),
408 .proc_handler = &proc_dobitmasks,
411 .procname = "subsystem_debug",
412 .data = &libcfs_subsystem_debug,
413 .maxlen = sizeof(int),
415 .proc_handler = &proc_dobitmasks,
418 .procname = "printk",
419 .data = &libcfs_printk,
420 .maxlen = sizeof(int),
422 .proc_handler = &proc_dobitmasks,
425 .procname = "cpu_partition_table",
428 .proc_handler = &proc_cpt_table,
431 .procname = "cpu_partition_distance",
434 .proc_handler = &proc_cpt_distance,
437 .procname = "debug_log_upcall",
438 .data = lnet_debug_log_upcall,
439 .maxlen = sizeof(lnet_debug_log_upcall),
441 .proc_handler = &debugfs_dostring,
444 .procname = "lnet_memused",
445 .data = (u64 *)&libcfs_kmem.counter,
446 .maxlen = sizeof(u64),
448 .proc_handler = &debugfs_dou64,
451 .procname = "catastrophe",
452 .data = &libcfs_catastrophe,
453 .maxlen = sizeof(int),
455 .proc_handler = &debugfs_doint,
458 .procname = "dump_kernel",
461 .proc_handler = &proc_dump_kernel,
464 .procname = "daemon_file",
467 .proc_handler = &proc_daemon_file,
470 .procname = "watchdog_ratelimit",
471 .data = &libcfs_watchdog_ratelimit,
472 .maxlen = sizeof(int),
474 .proc_handler = &debugfs_doint,
475 .extra1 = &min_watchdog_ratelimit,
476 .extra2 = &max_watchdog_ratelimit,
479 .procname = "force_lbug",
483 .proc_handler = &libcfs_force_lbug
486 .procname = "fail_loc",
487 .data = &cfs_fail_loc,
488 .maxlen = sizeof(cfs_fail_loc),
490 .proc_handler = &proc_fail_loc
493 .procname = "fail_val",
494 .data = &cfs_fail_val,
495 .maxlen = sizeof(int),
497 .proc_handler = &debugfs_doint
500 .procname = "fail_err",
501 .data = &cfs_fail_err,
502 .maxlen = sizeof(cfs_fail_err),
504 .proc_handler = &debugfs_doint,
510 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
511 { .name = "console_ratelimit",
512 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
513 { .name = "debug_path",
514 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
515 { .name = "panic_on_lbug",
516 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
517 { .name = "console_backoff",
518 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
519 { .name = "debug_mb",
520 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
521 { .name = "console_min_delay_centisecs",
522 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
523 { .name = "console_max_delay_centisecs",
524 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
528 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
529 size_t count, loff_t *ppos)
531 struct ctl_table *table = filp->private_data;
532 loff_t old_pos = *ppos;
533 ssize_t rc = -EINVAL;
536 rc = table->proc_handler(table, 0, (void __user *)buf,
539 * On success, the length read is either in error or in count.
540 * If ppos changed, then use count, else use error
542 if (!rc && *ppos != old_pos)
550 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
551 size_t count, loff_t *ppos)
553 struct ctl_table *table = filp->private_data;
554 loff_t old_pos = *ppos;
555 ssize_t rc = -EINVAL;
558 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
563 if (*ppos == old_pos)
569 static const struct file_operations lnet_debugfs_file_operations_rw = {
571 .read = lnet_debugfs_read,
572 .write = lnet_debugfs_write,
573 .llseek = default_llseek,
576 static const struct file_operations lnet_debugfs_file_operations_ro = {
578 .read = lnet_debugfs_read,
579 .llseek = default_llseek,
582 static const struct file_operations lnet_debugfs_file_operations_wo = {
584 .write = lnet_debugfs_write,
585 .llseek = default_llseek,
588 static const struct file_operations *lnet_debugfs_fops_select(
589 umode_t mode, const struct file_operations state[3])
591 if (!(mode & S_IWUGO))
594 if (!(mode & S_IRUGO))
600 void lnet_insert_debugfs(struct ctl_table *table, struct module *mod,
603 struct file_operations *state = *statep;
604 if (!lnet_debugfs_root)
605 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
607 /* Even if we cannot create, just ignore it altogether) */
608 if (IS_ERR_OR_NULL(lnet_debugfs_root))
612 state = kmalloc(3 * sizeof(*state), GFP_KERNEL);
615 state[0] = lnet_debugfs_file_operations_ro;
616 state[0].owner = mod;
617 state[1] = lnet_debugfs_file_operations_wo;
618 state[1].owner = mod;
619 state[2] = lnet_debugfs_file_operations_rw;
620 state[2].owner = mod;
624 /* We don't save the dentry returned in next two calls, because
625 * we don't call debugfs_remove() but rather remove_recursive()
627 for (; table && table->procname; table++)
628 debugfs_create_file(table->procname, table->mode,
629 lnet_debugfs_root, table,
630 lnet_debugfs_fops_select(table->mode,
631 (const struct file_operations *)state));
633 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
635 void lnet_debugfs_fini(void **state)
640 EXPORT_SYMBOL_GPL(lnet_debugfs_fini);
642 static void lnet_insert_debugfs_links(
643 const struct lnet_debugfs_symlink_def *symlinks)
645 for (; symlinks && symlinks->name; symlinks++)
646 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
650 void lnet_remove_debugfs(struct ctl_table *table)
652 for (; table && table->procname; table++) {
653 struct qstr dname = QSTR_INIT(table->procname,
654 strlen(table->procname));
655 struct dentry *dentry;
657 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
658 debugfs_remove(dentry);
661 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
663 static DEFINE_MUTEX(libcfs_startup);
664 static int libcfs_active;
666 static void *debugfs_state;
668 int libcfs_setup(void)
672 mutex_lock(&libcfs_startup);
676 rc = libcfs_debug_init(5 * 1024 * 1024);
678 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
686 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
687 if (!cfs_rehash_wq) {
689 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
694 rc = cfs_crypto_register();
696 CERROR("cfs_crypto_register: error %d\n", rc);
700 CDEBUG(D_OTHER, "libcfs setup OK\n");
703 mutex_unlock(&libcfs_startup);
707 destroy_workqueue(cfs_rehash_wq);
708 cfs_rehash_wq = NULL;
712 libcfs_debug_cleanup();
714 mutex_unlock(&libcfs_startup);
717 EXPORT_SYMBOL(libcfs_setup);
719 static int __init libcfs_init(void)
723 rc = cfs_arch_init();
725 CERROR("cfs_arch_init: error %d\n", rc);
729 lnet_insert_debugfs(lnet_table, THIS_MODULE, &debugfs_state);
730 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
731 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
736 static void __exit libcfs_exit(void)
740 /* Remove everthing */
741 debugfs_remove_recursive(lnet_debugfs_root);
742 lnet_debugfs_root = NULL;
744 lnet_debugfs_fini(&debugfs_state);
746 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
750 destroy_workqueue(cfs_rehash_wq);
752 cfs_crypto_unregister();
756 /* the below message is checked in test-framework.sh check_mem_leak() */
757 if (libcfs_kmem_read() != 0)
758 CERROR("Portals memory leaked: %lld bytes\n",
761 rc = libcfs_debug_cleanup();
763 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
768 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
769 MODULE_DESCRIPTION("Lustre helper library");
770 MODULE_VERSION(LIBCFS_VERSION);
771 MODULE_LICENSE("GPL");
773 module_init(libcfs_init);
774 module_exit(libcfs_exit);