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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015 Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
44 #include <linux/uio.h>
46 #include <linux/uaccess.h>
49 #include <linux/file.h>
50 #include <linux/list.h>
52 #include <linux/sysctl.h>
53 #include <linux/debugfs.h>
55 # define DEBUG_SUBSYSTEM S_LNET
57 #include "../../include/linux/libcfs/libcfs.h"
58 #include <asm/div64.h>
60 #include "../../include/linux/libcfs/libcfs_crypto.h"
61 #include "../../include/linux/lnet/lib-lnet.h"
62 #include "../../include/linux/lnet/lnet.h"
63 #include "tracefile.h"
65 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
66 MODULE_DESCRIPTION("Portals v3.1");
67 MODULE_LICENSE("GPL");
69 static struct dentry *lnet_debugfs_root;
71 /* called when opening /dev/device */
72 static int libcfs_psdev_open(unsigned long flags, void *args)
74 try_module_get(THIS_MODULE);
78 /* called when closing /dev/device */
79 static int libcfs_psdev_release(unsigned long flags, void *args)
81 module_put(THIS_MODULE);
85 static DECLARE_RWSEM(ioctl_list_sem);
86 static LIST_HEAD(ioctl_list);
88 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
92 down_write(&ioctl_list_sem);
93 if (!list_empty(&hand->item))
96 list_add_tail(&hand->item, &ioctl_list);
97 up_write(&ioctl_list_sem);
101 EXPORT_SYMBOL(libcfs_register_ioctl);
103 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
107 down_write(&ioctl_list_sem);
108 if (list_empty(&hand->item))
111 list_del_init(&hand->item);
112 up_write(&ioctl_list_sem);
116 EXPORT_SYMBOL(libcfs_deregister_ioctl);
118 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile, unsigned long cmd,
119 void __user *arg, struct libcfs_ioctl_data *data)
124 case IOC_LIBCFS_CLEAR_DEBUG:
125 libcfs_debug_clear_buffer();
128 * case IOC_LIBCFS_PANIC:
129 * Handled in arch/cfs_module.c
131 case IOC_LIBCFS_MARK_DEBUG:
132 if (!data->ioc_inlbuf1 ||
133 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
135 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
139 struct libcfs_ioctl_handler *hand;
142 down_read(&ioctl_list_sem);
143 list_for_each_entry(hand, &ioctl_list, item) {
144 err = hand->handle_ioctl(cmd, data);
145 if (err != -EINVAL) {
147 err = libcfs_ioctl_popdata(arg,
148 data, sizeof(*data));
152 up_read(&ioctl_list_sem);
160 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd,
164 struct libcfs_ioctl_data *data;
167 LIBCFS_ALLOC_GFP(buf, 1024, GFP_KERNEL);
171 /* 'cmd' and permissions get checked in our arch-specific caller */
172 if (libcfs_ioctl_getdata(buf, buf + 800, arg)) {
173 CERROR("PORTALS ioctl: data error\n");
177 data = (struct libcfs_ioctl_data *)buf;
179 err = libcfs_ioctl_int(pfile, cmd, arg, data);
182 LIBCFS_FREE(buf, 1024);
186 struct cfs_psdev_ops libcfs_psdev_ops = {
188 libcfs_psdev_release,
194 static int proc_call_handler(void *data, int write, loff_t *ppos,
195 void __user *buffer, size_t *lenp,
196 int (*handler)(void *data, int write,
197 loff_t pos, void __user *buffer, int len))
199 int rc = handler(data, write, *ppos, buffer, *lenp);
213 static int __proc_dobitmasks(void *data, int write,
214 loff_t pos, void __user *buffer, int nob)
216 const int tmpstrlen = 512;
219 unsigned int *mask = data;
220 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
221 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
223 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
228 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
234 rc = cfs_trace_copyout_string(buffer, nob,
238 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
244 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
245 /* Always print LBUG/LASSERT to console, so keep this mask */
254 static int proc_dobitmasks(struct ctl_table *table, int write,
255 void __user *buffer, size_t *lenp, loff_t *ppos)
257 return proc_call_handler(table->data, write, ppos, buffer, lenp,
261 static int __proc_dump_kernel(void *data, int write,
262 loff_t pos, void __user *buffer, int nob)
267 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
270 static int proc_dump_kernel(struct ctl_table *table, int write,
271 void __user *buffer, size_t *lenp, loff_t *ppos)
273 return proc_call_handler(table->data, write, ppos, buffer, lenp,
277 static int __proc_daemon_file(void *data, int write,
278 loff_t pos, void __user *buffer, int nob)
281 int len = strlen(cfs_tracefile);
286 return cfs_trace_copyout_string(buffer, nob,
287 cfs_tracefile + pos, "\n");
290 return cfs_trace_daemon_command_usrstr(buffer, nob);
293 static int proc_daemon_file(struct ctl_table *table, int write,
294 void __user *buffer, size_t *lenp, loff_t *ppos)
296 return proc_call_handler(table->data, write, ppos, buffer, lenp,
300 static int libcfs_force_lbug(struct ctl_table *table, int write,
302 size_t *lenp, loff_t *ppos)
309 static int proc_fail_loc(struct ctl_table *table, int write,
311 size_t *lenp, loff_t *ppos)
314 long old_fail_loc = cfs_fail_loc;
316 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
317 if (old_fail_loc != cfs_fail_loc)
318 wake_up(&cfs_race_waitq);
322 static int __proc_cpt_table(void *data, int write,
323 loff_t pos, void __user *buffer, int nob)
332 LASSERT(cfs_cpt_table);
335 LIBCFS_ALLOC(buf, len);
339 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
344 LIBCFS_FREE(buf, len);
356 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
359 LIBCFS_FREE(buf, len);
363 static int proc_cpt_table(struct ctl_table *table, int write,
364 void __user *buffer, size_t *lenp, loff_t *ppos)
366 return proc_call_handler(table->data, write, ppos, buffer, lenp,
370 static struct ctl_table lnet_table[] = {
373 .data = &libcfs_debug,
374 .maxlen = sizeof(int),
376 .proc_handler = &proc_dobitmasks,
379 .procname = "subsystem_debug",
380 .data = &libcfs_subsystem_debug,
381 .maxlen = sizeof(int),
383 .proc_handler = &proc_dobitmasks,
386 .procname = "printk",
387 .data = &libcfs_printk,
388 .maxlen = sizeof(int),
390 .proc_handler = &proc_dobitmasks,
393 .procname = "cpu_partition_table",
396 .proc_handler = &proc_cpt_table,
400 .procname = "upcall",
402 .maxlen = sizeof(lnet_upcall),
404 .proc_handler = &proc_dostring,
407 .procname = "debug_log_upcall",
408 .data = lnet_debug_log_upcall,
409 .maxlen = sizeof(lnet_debug_log_upcall),
411 .proc_handler = &proc_dostring,
414 .procname = "catastrophe",
415 .data = &libcfs_catastrophe,
416 .maxlen = sizeof(int),
418 .proc_handler = &proc_dointvec,
421 .procname = "dump_kernel",
424 .proc_handler = &proc_dump_kernel,
427 .procname = "daemon_file",
430 .proc_handler = &proc_daemon_file,
433 .procname = "force_lbug",
437 .proc_handler = &libcfs_force_lbug
440 .procname = "fail_loc",
441 .data = &cfs_fail_loc,
442 .maxlen = sizeof(cfs_fail_loc),
444 .proc_handler = &proc_fail_loc
447 .procname = "fail_val",
448 .data = &cfs_fail_val,
449 .maxlen = sizeof(int),
451 .proc_handler = &proc_dointvec
457 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
458 { "console_ratelimit",
459 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
461 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
463 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
464 { "libcfs_console_backoff",
465 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
467 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
468 { "console_min_delay_centisecs",
469 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
470 { "console_max_delay_centisecs",
471 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
475 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
476 size_t count, loff_t *ppos)
478 struct ctl_table *table = filp->private_data;
481 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
488 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
489 size_t count, loff_t *ppos)
491 struct ctl_table *table = filp->private_data;
494 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
501 static const struct file_operations lnet_debugfs_file_operations_rw = {
503 .read = lnet_debugfs_read,
504 .write = lnet_debugfs_write,
505 .llseek = default_llseek,
508 static const struct file_operations lnet_debugfs_file_operations_ro = {
510 .read = lnet_debugfs_read,
511 .llseek = default_llseek,
514 static const struct file_operations lnet_debugfs_file_operations_wo = {
516 .write = lnet_debugfs_write,
517 .llseek = default_llseek,
520 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
522 if (!(mode & S_IWUGO))
523 return &lnet_debugfs_file_operations_ro;
525 if (!(mode & S_IRUGO))
526 return &lnet_debugfs_file_operations_wo;
528 return &lnet_debugfs_file_operations_rw;
531 void lustre_insert_debugfs(struct ctl_table *table,
532 const struct lnet_debugfs_symlink_def *symlinks)
534 if (!lnet_debugfs_root)
535 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
537 /* Even if we cannot create, just ignore it altogether) */
538 if (IS_ERR_OR_NULL(lnet_debugfs_root))
541 /* We don't save the dentry returned in next two calls, because
542 * we don't call debugfs_remove() but rather remove_recursive()
544 for (; table->procname; table++)
545 debugfs_create_file(table->procname, table->mode,
546 lnet_debugfs_root, table,
547 lnet_debugfs_fops_select(table->mode));
549 for (; symlinks && symlinks->name; symlinks++)
550 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
554 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
556 static void lustre_remove_debugfs(void)
558 debugfs_remove_recursive(lnet_debugfs_root);
560 lnet_debugfs_root = NULL;
563 static int init_libcfs_module(void)
567 rc = libcfs_debug_init(5 * 1024 * 1024);
569 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
577 rc = misc_register(&libcfs_dev);
579 CERROR("misc_register: error %d\n", rc);
583 rc = cfs_wi_startup();
585 CERROR("initialize workitem: error %d\n", rc);
586 goto cleanup_deregister;
589 /* max to 4 threads, should be enough for rehash */
590 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
591 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
592 rc, &cfs_sched_rehash);
594 CERROR("Startup workitem scheduler: error: %d\n", rc);
595 goto cleanup_deregister;
598 rc = cfs_crypto_register();
600 CERROR("cfs_crypto_register: error %d\n", rc);
604 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
606 CDEBUG(D_OTHER, "portals setup OK\n");
611 misc_deregister(&libcfs_dev);
615 libcfs_debug_cleanup();
619 static void exit_libcfs_module(void)
623 lustre_remove_debugfs();
625 if (cfs_sched_rehash) {
626 cfs_wi_sched_destroy(cfs_sched_rehash);
627 cfs_sched_rehash = NULL;
630 cfs_crypto_unregister();
633 misc_deregister(&libcfs_dev);
637 rc = libcfs_debug_cleanup();
639 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
642 MODULE_VERSION("1.0.0");
644 module_init(init_libcfs_module);
645 module_exit(exit_libcfs_module);