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>
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>
51 # define DEBUG_SUBSYSTEM S_LNET
53 #include "../../include/linux/libcfs/libcfs.h"
54 #include <asm/div64.h>
56 #include "../../include/linux/libcfs/libcfs_crypto.h"
57 #include "../../include/linux/lnet/lib-lnet.h"
58 #include "../../include/linux/lnet/lib-dlc.h"
59 #include "../../include/linux/lnet/lnet.h"
60 #include "tracefile.h"
62 static struct dentry *lnet_debugfs_root;
64 static DECLARE_RWSEM(ioctl_list_sem);
65 static LIST_HEAD(ioctl_list);
67 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
71 down_write(&ioctl_list_sem);
72 if (!list_empty(&hand->item))
75 list_add_tail(&hand->item, &ioctl_list);
76 up_write(&ioctl_list_sem);
80 EXPORT_SYMBOL(libcfs_register_ioctl);
82 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
86 down_write(&ioctl_list_sem);
87 if (list_empty(&hand->item))
90 list_del_init(&hand->item);
91 up_write(&ioctl_list_sem);
95 EXPORT_SYMBOL(libcfs_deregister_ioctl);
97 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
99 struct libcfs_ioctl_data *data = NULL;
100 struct libcfs_ioctl_hdr *hdr;
103 /* 'cmd' and permissions get checked in our arch-specific caller */
104 err = libcfs_ioctl_getdata(&hdr, uparam);
106 CDEBUG_LIMIT(D_ERROR,
107 "libcfs ioctl: data header error %d\n", err);
111 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
113 * The libcfs_ioctl_data_adjust() function performs adjustment
114 * operations on the libcfs_ioctl_data structure to make
115 * it usable by the code. This doesn't need to be called
116 * for new data structures added.
118 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
119 err = libcfs_ioctl_data_adjust(data);
124 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
126 case IOC_LIBCFS_CLEAR_DEBUG:
127 libcfs_debug_clear_buffer();
130 case IOC_LIBCFS_MARK_DEBUG:
131 if (!data || !data->ioc_inlbuf1 ||
132 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0') {
136 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
140 struct libcfs_ioctl_handler *hand;
143 down_read(&ioctl_list_sem);
144 list_for_each_entry(hand, &ioctl_list, item) {
145 err = hand->handle_ioctl(cmd, hdr);
150 if (copy_to_user(uparam, hdr, hdr->ioc_len))
155 up_read(&ioctl_list_sem);
159 LIBCFS_FREE(hdr, hdr->ioc_len);
163 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
164 void __user *buffer, size_t *lenp,
165 int (*handler)(void *data, int write, loff_t pos,
166 void __user *buffer, int len))
168 int rc = handler(data, write, *ppos, buffer, *lenp);
181 EXPORT_SYMBOL(lprocfs_call_handler);
183 static int __proc_dobitmasks(void *data, int write,
184 loff_t pos, void __user *buffer, int nob)
186 const int tmpstrlen = 512;
189 unsigned int *mask = data;
190 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
191 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
193 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
198 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
204 rc = cfs_trace_copyout_string(buffer, nob,
208 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
214 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
215 /* Always print LBUG/LASSERT to console, so keep this mask */
224 static int proc_dobitmasks(struct ctl_table *table, int write,
225 void __user *buffer, size_t *lenp, loff_t *ppos)
227 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
231 static int __proc_dump_kernel(void *data, int write,
232 loff_t pos, void __user *buffer, int nob)
237 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
240 static int proc_dump_kernel(struct ctl_table *table, int write,
241 void __user *buffer, size_t *lenp, loff_t *ppos)
243 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
247 static int __proc_daemon_file(void *data, int write,
248 loff_t pos, void __user *buffer, int nob)
251 int len = strlen(cfs_tracefile);
256 return cfs_trace_copyout_string(buffer, nob,
257 cfs_tracefile + pos, "\n");
260 return cfs_trace_daemon_command_usrstr(buffer, nob);
263 static int proc_daemon_file(struct ctl_table *table, int write,
264 void __user *buffer, size_t *lenp, loff_t *ppos)
266 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
270 static int libcfs_force_lbug(struct ctl_table *table, int write,
272 size_t *lenp, loff_t *ppos)
279 static int proc_fail_loc(struct ctl_table *table, int write,
281 size_t *lenp, loff_t *ppos)
284 long old_fail_loc = cfs_fail_loc;
286 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
287 if (old_fail_loc != cfs_fail_loc)
288 wake_up(&cfs_race_waitq);
292 static int __proc_cpt_table(void *data, int write,
293 loff_t pos, void __user *buffer, int nob)
302 LASSERT(cfs_cpt_table);
305 LIBCFS_ALLOC(buf, len);
309 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
314 LIBCFS_FREE(buf, len);
326 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
329 LIBCFS_FREE(buf, len);
333 static int proc_cpt_table(struct ctl_table *table, int write,
334 void __user *buffer, size_t *lenp, loff_t *ppos)
336 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
340 static struct ctl_table lnet_table[] = {
343 .data = &libcfs_debug,
344 .maxlen = sizeof(int),
346 .proc_handler = &proc_dobitmasks,
349 .procname = "subsystem_debug",
350 .data = &libcfs_subsystem_debug,
351 .maxlen = sizeof(int),
353 .proc_handler = &proc_dobitmasks,
356 .procname = "printk",
357 .data = &libcfs_printk,
358 .maxlen = sizeof(int),
360 .proc_handler = &proc_dobitmasks,
363 .procname = "cpu_partition_table",
366 .proc_handler = &proc_cpt_table,
370 .procname = "upcall",
372 .maxlen = sizeof(lnet_upcall),
374 .proc_handler = &proc_dostring,
377 .procname = "debug_log_upcall",
378 .data = lnet_debug_log_upcall,
379 .maxlen = sizeof(lnet_debug_log_upcall),
381 .proc_handler = &proc_dostring,
384 .procname = "catastrophe",
385 .data = &libcfs_catastrophe,
386 .maxlen = sizeof(int),
388 .proc_handler = &proc_dointvec,
391 .procname = "dump_kernel",
394 .proc_handler = &proc_dump_kernel,
397 .procname = "daemon_file",
400 .proc_handler = &proc_daemon_file,
403 .procname = "force_lbug",
407 .proc_handler = &libcfs_force_lbug
410 .procname = "fail_loc",
411 .data = &cfs_fail_loc,
412 .maxlen = sizeof(cfs_fail_loc),
414 .proc_handler = &proc_fail_loc
417 .procname = "fail_val",
418 .data = &cfs_fail_val,
419 .maxlen = sizeof(int),
421 .proc_handler = &proc_dointvec
424 .procname = "fail_err",
425 .data = &cfs_fail_err,
426 .maxlen = sizeof(cfs_fail_err),
428 .proc_handler = &proc_dointvec,
434 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
435 { "console_ratelimit",
436 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
438 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
440 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
441 { "libcfs_console_backoff",
442 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
444 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
445 { "console_min_delay_centisecs",
446 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
447 { "console_max_delay_centisecs",
448 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
452 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
453 size_t count, loff_t *ppos)
455 struct ctl_table *table = filp->private_data;
458 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
465 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
466 size_t count, loff_t *ppos)
468 struct ctl_table *table = filp->private_data;
471 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
478 static const struct file_operations lnet_debugfs_file_operations_rw = {
480 .read = lnet_debugfs_read,
481 .write = lnet_debugfs_write,
482 .llseek = default_llseek,
485 static const struct file_operations lnet_debugfs_file_operations_ro = {
487 .read = lnet_debugfs_read,
488 .llseek = default_llseek,
491 static const struct file_operations lnet_debugfs_file_operations_wo = {
493 .write = lnet_debugfs_write,
494 .llseek = default_llseek,
497 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
499 if (!(mode & S_IWUGO))
500 return &lnet_debugfs_file_operations_ro;
502 if (!(mode & S_IRUGO))
503 return &lnet_debugfs_file_operations_wo;
505 return &lnet_debugfs_file_operations_rw;
508 void lustre_insert_debugfs(struct ctl_table *table,
509 const struct lnet_debugfs_symlink_def *symlinks)
511 if (!lnet_debugfs_root)
512 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
514 /* Even if we cannot create, just ignore it altogether) */
515 if (IS_ERR_OR_NULL(lnet_debugfs_root))
518 /* We don't save the dentry returned in next two calls, because
519 * we don't call debugfs_remove() but rather remove_recursive()
521 for (; table->procname; table++)
522 debugfs_create_file(table->procname, table->mode,
523 lnet_debugfs_root, table,
524 lnet_debugfs_fops_select(table->mode));
526 for (; symlinks && symlinks->name; symlinks++)
527 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
530 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
532 static void lustre_remove_debugfs(void)
534 debugfs_remove_recursive(lnet_debugfs_root);
536 lnet_debugfs_root = NULL;
539 static int libcfs_init(void)
543 rc = libcfs_debug_init(5 * 1024 * 1024);
545 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
553 rc = misc_register(&libcfs_dev);
555 CERROR("misc_register: error %d\n", rc);
559 rc = cfs_wi_startup();
561 CERROR("initialize workitem: error %d\n", rc);
562 goto cleanup_deregister;
565 /* max to 4 threads, should be enough for rehash */
566 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
567 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
568 rc, &cfs_sched_rehash);
570 CERROR("Startup workitem scheduler: error: %d\n", rc);
571 goto cleanup_deregister;
574 rc = cfs_crypto_register();
576 CERROR("cfs_crypto_register: error %d\n", rc);
580 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
582 CDEBUG(D_OTHER, "portals setup OK\n");
587 misc_deregister(&libcfs_dev);
591 libcfs_debug_cleanup();
595 static void libcfs_exit(void)
599 lustre_remove_debugfs();
601 if (cfs_sched_rehash) {
602 cfs_wi_sched_destroy(cfs_sched_rehash);
603 cfs_sched_rehash = NULL;
606 cfs_crypto_unregister();
609 misc_deregister(&libcfs_dev);
613 rc = libcfs_debug_cleanup();
615 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
618 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
619 MODULE_DESCRIPTION("Lustre helper library");
620 MODULE_VERSION(LIBCFS_VERSION);
621 MODULE_LICENSE("GPL");
623 module_init(libcfs_init);
624 module_exit(libcfs_exit);