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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 #include <linux/module.h>
33 #include <linux/kernel.h>
35 #include <linux/string.h>
36 #include <linux/stat.h>
37 #include <linux/errno.h>
38 #include <linux/unistd.h>
40 #include <linux/uio.h>
41 #include <linux/uaccess.h>
44 #include <linux/file.h>
45 #include <linux/list.h>
47 #include <linux/sysctl.h>
48 #include <linux/debugfs.h>
49 #include <asm/div64.h>
51 #define DEBUG_SUBSYSTEM S_LNET
53 #include <libcfs/libcfs.h>
54 #include <libcfs/libcfs_crypto.h>
55 #include <lnet/lib-lnet.h>
56 #include "tracefile.h"
58 static struct dentry *lnet_debugfs_root;
60 static DECLARE_RWSEM(ioctl_list_sem);
61 static LIST_HEAD(ioctl_list);
63 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
67 down_write(&ioctl_list_sem);
68 if (!list_empty(&hand->item))
71 list_add_tail(&hand->item, &ioctl_list);
72 up_write(&ioctl_list_sem);
76 EXPORT_SYMBOL(libcfs_register_ioctl);
78 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
82 down_write(&ioctl_list_sem);
83 if (list_empty(&hand->item))
86 list_del_init(&hand->item);
87 up_write(&ioctl_list_sem);
91 EXPORT_SYMBOL(libcfs_deregister_ioctl);
93 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
95 struct libcfs_ioctl_data *data = NULL;
96 struct libcfs_ioctl_hdr *hdr;
100 /* 'cmd' and permissions get checked in our arch-specific caller */
101 err = libcfs_ioctl_getdata(&hdr, uparam);
103 CDEBUG_LIMIT(D_ERROR,
104 "libcfs ioctl: data header error %d\n", err);
108 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
109 /* The libcfs_ioctl_data_adjust() function performs adjustment
110 * operations on the libcfs_ioctl_data structure to make
111 * it usable by the code. This doesn't need to be called
112 * for new data structures added. */
113 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
114 err = libcfs_ioctl_data_adjust(data);
119 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
121 case IOC_LIBCFS_CLEAR_DEBUG:
122 libcfs_debug_clear_buffer();
124 case IOC_LIBCFS_MARK_DEBUG:
126 data->ioc_inlbuf1 == NULL ||
127 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
128 GOTO(out, err = -EINVAL);
130 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
134 struct libcfs_ioctl_handler *hand;
137 down_read(&ioctl_list_sem);
138 list_for_each_entry(hand, &ioctl_list, item) {
139 err = hand->handle_ioctl(cmd, hdr);
143 if (copy_to_user(uparam, hdr, hdr->ioc_len))
147 up_read(&ioctl_list_sem);
151 LIBCFS_FREE(hdr, hdr->ioc_len);
155 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
156 void __user *buffer, size_t *lenp,
157 int (*handler)(void *data, int write, loff_t pos,
158 void __user *buffer, int len))
160 int rc = handler(data, write, *ppos, buffer, *lenp);
173 EXPORT_SYMBOL(lprocfs_call_handler);
175 static int __proc_dobitmasks(void *data, int write,
176 loff_t pos, void __user *buffer, int nob)
178 const int tmpstrlen = 512;
181 unsigned int *mask = data;
182 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
183 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
185 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
190 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
196 rc = cfs_trace_copyout_string(buffer, nob,
200 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
206 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
207 /* Always print LBUG/LASSERT to console, so keep this mask */
216 static int proc_dobitmasks(struct ctl_table *table, int write,
217 void __user *buffer, size_t *lenp, loff_t *ppos)
219 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
223 static int min_watchdog_ratelimit; /* disable ratelimiting */
224 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
226 static int __proc_dump_kernel(void *data, int write,
227 loff_t pos, void __user *buffer, int nob)
232 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
235 static int proc_dump_kernel(struct ctl_table *table, int write,
236 void __user *buffer, size_t *lenp, loff_t *ppos)
238 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
242 static int __proc_daemon_file(void *data, int write,
243 loff_t pos, void __user *buffer, int nob)
246 int len = strlen(cfs_tracefile);
251 return cfs_trace_copyout_string(buffer, nob,
252 cfs_tracefile + pos, "\n");
255 return cfs_trace_daemon_command_usrstr(buffer, nob);
258 static int proc_daemon_file(struct ctl_table *table, int write,
259 void __user *buffer, size_t *lenp, loff_t *ppos)
261 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
265 static int libcfs_force_lbug(struct ctl_table *table, int write,
267 size_t *lenp, loff_t *ppos)
274 static int proc_fail_loc(struct ctl_table *table, int write,
275 void __user *buffer, size_t *lenp, loff_t *ppos)
278 long old_fail_loc = cfs_fail_loc;
280 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
281 if (old_fail_loc != cfs_fail_loc)
282 wake_up(&cfs_race_waitq);
286 static int __proc_cpt_table(void *data, int write,
287 loff_t pos, void __user *buffer, int nob)
296 LASSERT(cfs_cpt_table != NULL);
299 LIBCFS_ALLOC(buf, len);
303 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
308 LIBCFS_FREE(buf, len);
320 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
323 LIBCFS_FREE(buf, len);
327 static int proc_cpt_table(struct ctl_table *table, int write,
328 void __user *buffer, size_t *lenp, loff_t *ppos)
330 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
334 static int __proc_cpt_distance(void *data, int write,
335 loff_t pos, void __user *buffer, int nob)
344 LASSERT(cfs_cpt_table != NULL);
347 LIBCFS_ALLOC(buf, len);
351 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
356 LIBCFS_FREE(buf, len);
368 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
371 LIBCFS_FREE(buf, len);
375 static int proc_cpt_distance(struct ctl_table *table, int write,
376 void __user *buffer, size_t *lenp, loff_t *ppos)
378 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
379 __proc_cpt_distance);
382 static struct ctl_table lnet_table[] = {
386 .data = &libcfs_debug,
387 .maxlen = sizeof(int),
389 .proc_handler = &proc_dobitmasks,
393 .procname = "subsystem_debug",
394 .data = &libcfs_subsystem_debug,
395 .maxlen = sizeof(int),
397 .proc_handler = &proc_dobitmasks,
401 .procname = "printk",
402 .data = &libcfs_printk,
403 .maxlen = sizeof(int),
405 .proc_handler = &proc_dobitmasks,
409 .procname = "cpu_partition_table",
412 .proc_handler = &proc_cpt_table,
416 .procname = "cpu_partition_distance",
419 .proc_handler = &proc_cpt_distance,
423 .procname = "debug_log_upcall",
424 .data = lnet_debug_log_upcall,
425 .maxlen = sizeof(lnet_debug_log_upcall),
427 .proc_handler = &proc_dostring,
431 .procname = "lnet_memused",
432 .data = (int *)&libcfs_kmemory.counter,
433 .maxlen = sizeof(int),
435 .proc_handler = &proc_dointvec,
439 .procname = "catastrophe",
440 .data = &libcfs_catastrophe,
441 .maxlen = sizeof(int),
443 .proc_handler = &proc_dointvec,
447 .procname = "dump_kernel",
450 .proc_handler = &proc_dump_kernel,
454 .procname = "daemon_file",
457 .proc_handler = &proc_daemon_file,
461 .procname = "watchdog_ratelimit",
462 .data = &libcfs_watchdog_ratelimit,
463 .maxlen = sizeof(int),
465 .proc_handler = &proc_dointvec_minmax,
466 .extra1 = &min_watchdog_ratelimit,
467 .extra2 = &max_watchdog_ratelimit,
471 .procname = "force_lbug",
475 .proc_handler = &libcfs_force_lbug
479 .procname = "fail_loc",
480 .data = &cfs_fail_loc,
481 .maxlen = sizeof(cfs_fail_loc),
483 .proc_handler = &proc_fail_loc
487 .procname = "fail_val",
488 .data = &cfs_fail_val,
489 .maxlen = sizeof(int),
491 .proc_handler = &proc_dointvec
495 .procname = "fail_err",
496 .data = &cfs_fail_err,
497 .maxlen = sizeof(cfs_fail_err),
499 .proc_handler = &proc_dointvec,
505 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
506 { .name = "console_ratelimit",
507 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
508 { .name = "debug_path",
509 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
510 { .name = "panic_on_lbug",
511 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
512 { .name = "console_backoff",
513 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
514 { .name = "debug_mb",
515 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
516 { .name = "console_min_delay_centisecs",
517 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
518 { .name = "console_max_delay_centisecs",
519 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
523 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
524 size_t count, loff_t *ppos)
526 struct ctl_table *table = filp->private_data;
529 rc = table->proc_handler(table, 0, buf, &count, ppos);
536 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
537 size_t count, loff_t *ppos)
539 struct ctl_table *table = filp->private_data;
542 rc = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
549 static const struct file_operations lnet_debugfs_file_operations_rw = {
551 .read = lnet_debugfs_read,
552 .write = lnet_debugfs_write,
553 .llseek = default_llseek,
556 static const struct file_operations lnet_debugfs_file_operations_ro = {
558 .read = lnet_debugfs_read,
559 .llseek = default_llseek,
562 static const struct file_operations lnet_debugfs_file_operations_wo = {
564 .write = lnet_debugfs_write,
565 .llseek = default_llseek,
568 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
570 if (!(mode & S_IWUGO))
571 return &lnet_debugfs_file_operations_ro;
573 if (!(mode & S_IRUGO))
574 return &lnet_debugfs_file_operations_wo;
576 return &lnet_debugfs_file_operations_rw;
579 void lnet_insert_debugfs(struct ctl_table *table,
580 const struct lnet_debugfs_symlink_def *symlinks)
582 if (!lnet_debugfs_root)
583 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
585 /* Even if we cannot create, just ignore it altogether) */
586 if (IS_ERR_OR_NULL(lnet_debugfs_root))
589 /* We don't save the dentry returned in next two calls, because
590 * we don't call debugfs_remove() but rather remove_recursive()
592 for (; table && table->procname; table++)
593 debugfs_create_file(table->procname, table->mode,
594 lnet_debugfs_root, table,
595 lnet_debugfs_fops_select(table->mode));
597 for (; symlinks && symlinks->name; symlinks++)
598 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
601 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
603 static void lnet_remove_debugfs(void)
605 debugfs_remove_recursive(lnet_debugfs_root);
607 lnet_debugfs_root = NULL;
610 static int __init libcfs_init(void)
614 rc = libcfs_debug_init(5 * 1024 * 1024);
616 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
624 rc = misc_register(&libcfs_dev);
626 CERROR("misc_register: error %d\n", rc);
630 rc = cfs_wi_startup();
632 CERROR("initialize workitem: error %d\n", rc);
633 goto cleanup_deregister;
636 /* max to 4 threads, should be enough for rehash */
637 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
638 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
639 rc, &cfs_sched_rehash);
641 CERROR("Startup workitem scheduler: error: %d\n", rc);
642 goto cleanup_deregister;
645 rc = cfs_crypto_register();
647 CERROR("cfs_crypto_regster: error %d\n", rc);
651 lnet_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
653 CDEBUG (D_OTHER, "portals setup OK\n");
658 misc_deregister(&libcfs_dev);
662 libcfs_debug_cleanup();
666 static void __exit libcfs_exit(void)
670 lnet_remove_debugfs();
672 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
673 atomic_read(&libcfs_kmemory));
675 if (cfs_sched_rehash != NULL) {
676 cfs_wi_sched_destroy(cfs_sched_rehash);
677 cfs_sched_rehash = NULL;
680 cfs_crypto_unregister();
683 misc_deregister(&libcfs_dev);
687 if (atomic_read(&libcfs_kmemory) != 0)
688 CERROR("Portals memory leaked: %d bytes\n",
689 atomic_read(&libcfs_kmemory));
691 rc = libcfs_debug_cleanup();
693 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
697 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
698 MODULE_DESCRIPTION("Lustre helper library");
699 MODULE_VERSION(LIBCFS_VERSION);
700 MODULE_LICENSE("GPL");
702 module_init(libcfs_init);
703 module_exit(libcfs_exit);