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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
36 #include <linux/string.h>
37 #include <linux/stat.h>
38 #include <linux/errno.h>
39 #include <linux/unistd.h>
41 #include <linux/uio.h>
42 #include <linux/uaccess.h>
45 #include <linux/file.h>
46 #include <linux/list.h>
48 #include <linux/sysctl.h>
49 #include <linux/debugfs.h>
50 #include <asm/div64.h>
52 #define DEBUG_SUBSYSTEM S_LNET
54 #include <libcfs/libcfs.h>
55 #include <libcfs/libcfs_crypto.h>
56 #include <lnet/lib-lnet.h>
57 #include "tracefile.h"
59 static struct dentry *lnet_debugfs_root;
61 static DECLARE_RWSEM(ioctl_list_sem);
62 static LIST_HEAD(ioctl_list);
64 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
68 down_write(&ioctl_list_sem);
69 if (!list_empty(&hand->item))
72 list_add_tail(&hand->item, &ioctl_list);
73 up_write(&ioctl_list_sem);
77 EXPORT_SYMBOL(libcfs_register_ioctl);
79 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
83 down_write(&ioctl_list_sem);
84 if (list_empty(&hand->item))
87 list_del_init(&hand->item);
88 up_write(&ioctl_list_sem);
92 EXPORT_SYMBOL(libcfs_deregister_ioctl);
94 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
96 struct libcfs_ioctl_data *data = NULL;
97 struct libcfs_ioctl_hdr *hdr;
101 /* 'cmd' and permissions get checked in our arch-specific caller */
102 err = libcfs_ioctl_getdata(&hdr, uparam);
104 CDEBUG_LIMIT(D_ERROR,
105 "libcfs ioctl: data header error %d\n", err);
109 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
110 /* The libcfs_ioctl_data_adjust() function performs adjustment
111 * operations on the libcfs_ioctl_data structure to make
112 * it usable by the code. This doesn't need to be called
113 * for new data structures added. */
114 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
115 err = libcfs_ioctl_data_adjust(data);
120 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
122 case IOC_LIBCFS_CLEAR_DEBUG:
123 libcfs_debug_clear_buffer();
125 case IOC_LIBCFS_MARK_DEBUG:
127 data->ioc_inlbuf1 == NULL ||
128 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
129 GOTO(out, err = -EINVAL);
131 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
135 struct libcfs_ioctl_handler *hand;
138 down_read(&ioctl_list_sem);
139 list_for_each_entry(hand, &ioctl_list, item) {
140 err = hand->handle_ioctl(cmd, hdr);
144 if (copy_to_user(uparam, hdr, hdr->ioc_len))
148 up_read(&ioctl_list_sem);
152 LIBCFS_FREE(hdr, hdr->ioc_len);
156 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
157 void __user *buffer, size_t *lenp,
158 int (*handler)(void *data, int write, loff_t pos,
159 void __user *buffer, int len))
161 int rc = handler(data, write, *ppos, buffer, *lenp);
174 EXPORT_SYMBOL(lprocfs_call_handler);
176 static int __proc_dobitmasks(void *data, int write,
177 loff_t pos, void __user *buffer, int nob)
179 const int tmpstrlen = 512;
182 unsigned int *mask = data;
183 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
184 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
186 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
191 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
197 rc = cfs_trace_copyout_string(buffer, nob,
201 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
207 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
208 /* Always print LBUG/LASSERT to console, so keep this mask */
217 static int proc_dobitmasks(struct ctl_table *table, int write,
218 void __user *buffer, size_t *lenp, loff_t *ppos)
220 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
224 static int min_watchdog_ratelimit; /* disable ratelimiting */
225 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
227 static int __proc_dump_kernel(void *data, int write,
228 loff_t pos, void __user *buffer, int nob)
233 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
236 static int proc_dump_kernel(struct ctl_table *table, int write,
237 void __user *buffer, size_t *lenp, loff_t *ppos)
239 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
243 static int __proc_daemon_file(void *data, int write,
244 loff_t pos, void __user *buffer, int nob)
247 int len = strlen(cfs_tracefile);
252 return cfs_trace_copyout_string(buffer, nob,
253 cfs_tracefile + pos, "\n");
256 return cfs_trace_daemon_command_usrstr(buffer, nob);
259 static int proc_daemon_file(struct ctl_table *table, int write,
260 void __user *buffer, size_t *lenp, loff_t *ppos)
262 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
266 static int libcfs_force_lbug(struct ctl_table *table, int write,
268 size_t *lenp, loff_t *ppos)
275 static int proc_fail_loc(struct ctl_table *table, int write,
276 void __user *buffer, size_t *lenp, loff_t *ppos)
279 long old_fail_loc = cfs_fail_loc;
281 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
282 if (old_fail_loc != cfs_fail_loc)
283 wake_up(&cfs_race_waitq);
287 static int __proc_cpt_table(void *data, int write,
288 loff_t pos, void __user *buffer, int nob)
297 LASSERT(cfs_cpt_table != NULL);
300 LIBCFS_ALLOC(buf, len);
304 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
309 LIBCFS_FREE(buf, len);
321 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
324 LIBCFS_FREE(buf, len);
328 static int proc_cpt_table(struct ctl_table *table, int write,
329 void __user *buffer, size_t *lenp, loff_t *ppos)
331 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
335 static int __proc_cpt_distance(void *data, int write,
336 loff_t pos, void __user *buffer, int nob)
345 LASSERT(cfs_cpt_table != NULL);
348 LIBCFS_ALLOC(buf, len);
352 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
357 LIBCFS_FREE(buf, len);
369 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
372 LIBCFS_FREE(buf, len);
376 static int proc_cpt_distance(struct ctl_table *table, int write,
377 void __user *buffer, size_t *lenp, loff_t *ppos)
379 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
380 __proc_cpt_distance);
383 static struct ctl_table lnet_table[] = {
387 .data = &libcfs_debug,
388 .maxlen = sizeof(int),
390 .proc_handler = &proc_dobitmasks,
394 .procname = "subsystem_debug",
395 .data = &libcfs_subsystem_debug,
396 .maxlen = sizeof(int),
398 .proc_handler = &proc_dobitmasks,
402 .procname = "printk",
403 .data = &libcfs_printk,
404 .maxlen = sizeof(int),
406 .proc_handler = &proc_dobitmasks,
410 .procname = "cpu_partition_table",
413 .proc_handler = &proc_cpt_table,
417 .procname = "cpu_partition_distance",
420 .proc_handler = &proc_cpt_distance,
424 .procname = "debug_log_upcall",
425 .data = lnet_debug_log_upcall,
426 .maxlen = sizeof(lnet_debug_log_upcall),
428 .proc_handler = &proc_dostring,
432 .procname = "lnet_memused",
433 .data = (int *)&libcfs_kmemory.counter,
434 .maxlen = sizeof(int),
436 .proc_handler = &proc_dointvec,
440 .procname = "catastrophe",
441 .data = &libcfs_catastrophe,
442 .maxlen = sizeof(int),
444 .proc_handler = &proc_dointvec,
448 .procname = "dump_kernel",
451 .proc_handler = &proc_dump_kernel,
455 .procname = "daemon_file",
458 .proc_handler = &proc_daemon_file,
462 .procname = "watchdog_ratelimit",
463 .data = &libcfs_watchdog_ratelimit,
464 .maxlen = sizeof(int),
466 .proc_handler = &proc_dointvec_minmax,
467 .extra1 = &min_watchdog_ratelimit,
468 .extra2 = &max_watchdog_ratelimit,
472 .procname = "force_lbug",
476 .proc_handler = &libcfs_force_lbug
480 .procname = "fail_loc",
481 .data = &cfs_fail_loc,
482 .maxlen = sizeof(cfs_fail_loc),
484 .proc_handler = &proc_fail_loc
488 .procname = "fail_val",
489 .data = &cfs_fail_val,
490 .maxlen = sizeof(int),
492 .proc_handler = &proc_dointvec
496 .procname = "fail_err",
497 .data = &cfs_fail_err,
498 .maxlen = sizeof(cfs_fail_err),
500 .proc_handler = &proc_dointvec,
506 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
507 { .name = "console_ratelimit",
508 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
509 { .name = "debug_path",
510 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
511 { .name = "panic_on_lbug",
512 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
513 { .name = "console_backoff",
514 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
515 { .name = "debug_mb",
516 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
517 { .name = "console_min_delay_centisecs",
518 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
519 { .name = "console_max_delay_centisecs",
520 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
524 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
525 size_t count, loff_t *ppos)
527 struct ctl_table *table = filp->private_data;
530 rc = table->proc_handler(table, 0, buf, &count, ppos);
537 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
538 size_t count, loff_t *ppos)
540 struct ctl_table *table = filp->private_data;
543 rc = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
550 static const struct file_operations lnet_debugfs_file_operations_rw = {
552 .read = lnet_debugfs_read,
553 .write = lnet_debugfs_write,
554 .llseek = default_llseek,
557 static const struct file_operations lnet_debugfs_file_operations_ro = {
559 .read = lnet_debugfs_read,
560 .llseek = default_llseek,
563 static const struct file_operations lnet_debugfs_file_operations_wo = {
565 .write = lnet_debugfs_write,
566 .llseek = default_llseek,
569 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
571 if (!(mode & S_IWUGO))
572 return &lnet_debugfs_file_operations_ro;
574 if (!(mode & S_IRUGO))
575 return &lnet_debugfs_file_operations_wo;
577 return &lnet_debugfs_file_operations_rw;
580 void lnet_insert_debugfs(struct ctl_table *table,
581 const struct lnet_debugfs_symlink_def *symlinks)
583 if (!lnet_debugfs_root)
584 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
586 /* Even if we cannot create, just ignore it altogether) */
587 if (IS_ERR_OR_NULL(lnet_debugfs_root))
590 /* We don't save the dentry returned in next two calls, because
591 * we don't call debugfs_remove() but rather remove_recursive()
593 for (; table && table->procname; table++)
594 debugfs_create_file(table->procname, table->mode,
595 lnet_debugfs_root, table,
596 lnet_debugfs_fops_select(table->mode));
598 for (; symlinks && symlinks->name; symlinks++)
599 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
602 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
604 static void lnet_remove_debugfs(void)
606 debugfs_remove_recursive(lnet_debugfs_root);
608 lnet_debugfs_root = NULL;
611 static int __init libcfs_init(void)
615 rc = libcfs_debug_init(5 * 1024 * 1024);
617 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
625 rc = misc_register(&libcfs_dev);
627 CERROR("misc_register: error %d\n", rc);
631 rc = cfs_wi_startup();
633 CERROR("initialize workitem: error %d\n", rc);
634 goto cleanup_deregister;
637 /* max to 4 threads, should be enough for rehash */
638 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
639 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
640 rc, &cfs_sched_rehash);
642 CERROR("Startup workitem scheduler: error: %d\n", rc);
643 goto cleanup_deregister;
646 rc = cfs_crypto_register();
648 CERROR("cfs_crypto_regster: error %d\n", rc);
652 lnet_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
654 CDEBUG (D_OTHER, "portals setup OK\n");
659 misc_deregister(&libcfs_dev);
663 libcfs_debug_cleanup();
667 static void __exit libcfs_exit(void)
671 lnet_remove_debugfs();
673 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
674 atomic_read(&libcfs_kmemory));
676 if (cfs_sched_rehash != NULL) {
677 cfs_wi_sched_destroy(cfs_sched_rehash);
678 cfs_sched_rehash = NULL;
681 cfs_crypto_unregister();
684 misc_deregister(&libcfs_dev);
688 if (atomic_read(&libcfs_kmemory) != 0)
689 CERROR("Portals memory leaked: %d bytes\n",
690 atomic_read(&libcfs_kmemory));
692 rc = libcfs_debug_cleanup();
694 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
698 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
699 MODULE_DESCRIPTION("Lustre helper library");
700 MODULE_VERSION(LIBCFS_VERSION);
701 MODULE_LICENSE("GPL");
703 module_init(libcfs_init);
704 module_exit(libcfs_exit);