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 == NULL ||
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 != NULL);
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[] = {
372 * NB No .strategy entries have been provided since sysctl(8) prefers
373 * to go via /proc for portability.
377 .data = &libcfs_debug,
378 .maxlen = sizeof(int),
380 .proc_handler = &proc_dobitmasks,
383 .procname = "subsystem_debug",
384 .data = &libcfs_subsystem_debug,
385 .maxlen = sizeof(int),
387 .proc_handler = &proc_dobitmasks,
390 .procname = "printk",
391 .data = &libcfs_printk,
392 .maxlen = sizeof(int),
394 .proc_handler = &proc_dobitmasks,
397 .procname = "cpu_partition_table",
400 .proc_handler = &proc_cpt_table,
404 .procname = "upcall",
406 .maxlen = sizeof(lnet_upcall),
408 .proc_handler = &proc_dostring,
411 .procname = "debug_log_upcall",
412 .data = lnet_debug_log_upcall,
413 .maxlen = sizeof(lnet_debug_log_upcall),
415 .proc_handler = &proc_dostring,
418 .procname = "catastrophe",
419 .data = &libcfs_catastrophe,
420 .maxlen = sizeof(int),
422 .proc_handler = &proc_dointvec,
425 .procname = "dump_kernel",
428 .proc_handler = &proc_dump_kernel,
431 .procname = "daemon_file",
434 .proc_handler = &proc_daemon_file,
437 .procname = "force_lbug",
441 .proc_handler = &libcfs_force_lbug
444 .procname = "fail_loc",
445 .data = &cfs_fail_loc,
446 .maxlen = sizeof(cfs_fail_loc),
448 .proc_handler = &proc_fail_loc
451 .procname = "fail_val",
452 .data = &cfs_fail_val,
453 .maxlen = sizeof(int),
455 .proc_handler = &proc_dointvec
461 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
462 { "console_ratelimit",
463 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
465 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
467 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
468 { "libcfs_console_backoff",
469 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
471 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
472 { "console_min_delay_centisecs",
473 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
474 { "console_max_delay_centisecs",
475 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
479 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
480 size_t count, loff_t *ppos)
482 struct ctl_table *table = filp->private_data;
485 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
492 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
493 size_t count, loff_t *ppos)
495 struct ctl_table *table = filp->private_data;
498 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
505 static const struct file_operations lnet_debugfs_file_operations_rw = {
507 .read = lnet_debugfs_read,
508 .write = lnet_debugfs_write,
509 .llseek = default_llseek,
512 static const struct file_operations lnet_debugfs_file_operations_ro = {
514 .read = lnet_debugfs_read,
515 .llseek = default_llseek,
518 static const struct file_operations lnet_debugfs_file_operations_wo = {
520 .write = lnet_debugfs_write,
521 .llseek = default_llseek,
524 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
526 if (!(mode & S_IWUGO))
527 return &lnet_debugfs_file_operations_ro;
529 if (!(mode & S_IRUGO))
530 return &lnet_debugfs_file_operations_wo;
532 return &lnet_debugfs_file_operations_rw;
535 void lustre_insert_debugfs(struct ctl_table *table,
536 const struct lnet_debugfs_symlink_def *symlinks)
538 if (lnet_debugfs_root == NULL)
539 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
541 /* Even if we cannot create, just ignore it altogether) */
542 if (IS_ERR_OR_NULL(lnet_debugfs_root))
545 /* We don't save the dentry returned in next two calls, because
546 * we don't call debugfs_remove() but rather remove_recursive()
548 for (; table->procname; table++)
549 debugfs_create_file(table->procname, table->mode,
550 lnet_debugfs_root, table,
551 lnet_debugfs_fops_select(table->mode));
553 for (; symlinks && symlinks->name; symlinks++)
554 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
558 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
560 static void lustre_remove_debugfs(void)
562 if (lnet_debugfs_root != NULL)
563 debugfs_remove_recursive(lnet_debugfs_root);
565 lnet_debugfs_root = NULL;
568 static int init_libcfs_module(void)
572 rc = libcfs_debug_init(5 * 1024 * 1024);
574 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
582 rc = misc_register(&libcfs_dev);
584 CERROR("misc_register: error %d\n", rc);
588 rc = cfs_wi_startup();
590 CERROR("initialize workitem: error %d\n", rc);
591 goto cleanup_deregister;
594 /* max to 4 threads, should be enough for rehash */
595 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
596 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
597 rc, &cfs_sched_rehash);
599 CERROR("Startup workitem scheduler: error: %d\n", rc);
600 goto cleanup_deregister;
603 rc = cfs_crypto_register();
605 CERROR("cfs_crypto_register: error %d\n", rc);
609 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
611 CDEBUG(D_OTHER, "portals setup OK\n");
616 misc_deregister(&libcfs_dev);
620 libcfs_debug_cleanup();
624 static void exit_libcfs_module(void)
628 lustre_remove_debugfs();
630 if (cfs_sched_rehash) {
631 cfs_wi_sched_destroy(cfs_sched_rehash);
632 cfs_sched_rehash = NULL;
635 cfs_crypto_unregister();
638 misc_deregister(&libcfs_dev);
642 rc = libcfs_debug_cleanup();
644 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
647 MODULE_VERSION("1.0.0");
649 module_init(init_libcfs_module);
650 module_exit(exit_libcfs_module);