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 <linux/libcfs/libcfs.h>
54 #include <asm/div64.h>
56 #include <linux/libcfs/libcfs_crypto.h>
57 #include <linux/lnet/lib-lnet.h>
58 #include <uapi/linux/lnet/lnet-dlc.h>
59 #include "tracefile.h"
61 static struct dentry *lnet_debugfs_root;
63 static DECLARE_RWSEM(ioctl_list_sem);
64 static LIST_HEAD(ioctl_list);
66 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
70 down_write(&ioctl_list_sem);
71 if (!list_empty(&hand->item))
74 list_add_tail(&hand->item, &ioctl_list);
75 up_write(&ioctl_list_sem);
79 EXPORT_SYMBOL(libcfs_register_ioctl);
81 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
85 down_write(&ioctl_list_sem);
86 if (list_empty(&hand->item))
89 list_del_init(&hand->item);
90 up_write(&ioctl_list_sem);
94 EXPORT_SYMBOL(libcfs_deregister_ioctl);
96 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
98 struct libcfs_ioctl_data *data = NULL;
99 struct libcfs_ioctl_hdr *hdr;
102 /* 'cmd' and permissions get checked in our arch-specific caller */
103 err = libcfs_ioctl_getdata(&hdr, uparam);
105 CDEBUG_LIMIT(D_ERROR,
106 "libcfs ioctl: data header error %d\n", err);
110 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
112 * The libcfs_ioctl_data_adjust() function performs adjustment
113 * operations on the libcfs_ioctl_data structure to make
114 * it usable by the code. This doesn't need to be called
115 * for new data structures added.
117 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
118 err = libcfs_ioctl_data_adjust(data);
123 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
125 case IOC_LIBCFS_CLEAR_DEBUG:
126 libcfs_debug_clear_buffer();
129 case IOC_LIBCFS_MARK_DEBUG:
130 if (!data || !data->ioc_inlbuf1 ||
131 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, hdr);
149 if (copy_to_user(uparam, hdr, hdr->ioc_len))
154 up_read(&ioctl_list_sem);
158 LIBCFS_FREE(hdr, hdr->ioc_len);
162 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
163 void __user *buffer, size_t *lenp,
164 int (*handler)(void *data, int write, loff_t pos,
165 void __user *buffer, int len))
167 int rc = handler(data, write, *ppos, buffer, *lenp);
180 EXPORT_SYMBOL(lprocfs_call_handler);
182 static int __proc_dobitmasks(void *data, int write,
183 loff_t pos, void __user *buffer, int nob)
185 const int tmpstrlen = 512;
188 unsigned int *mask = data;
189 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
190 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
192 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
197 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
203 rc = cfs_trace_copyout_string(buffer, nob,
207 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
213 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
214 /* Always print LBUG/LASSERT to console, so keep this mask */
223 static int proc_dobitmasks(struct ctl_table *table, int write,
224 void __user *buffer, size_t *lenp, loff_t *ppos)
226 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
230 static int __proc_dump_kernel(void *data, int write,
231 loff_t pos, void __user *buffer, int nob)
236 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
239 static int proc_dump_kernel(struct ctl_table *table, int write,
240 void __user *buffer, size_t *lenp, loff_t *ppos)
242 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
246 static int __proc_daemon_file(void *data, int write,
247 loff_t pos, void __user *buffer, int nob)
250 int len = strlen(cfs_tracefile);
255 return cfs_trace_copyout_string(buffer, nob,
256 cfs_tracefile + pos, "\n");
259 return cfs_trace_daemon_command_usrstr(buffer, nob);
262 static int proc_daemon_file(struct ctl_table *table, int write,
263 void __user *buffer, size_t *lenp, loff_t *ppos)
265 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
269 static int libcfs_force_lbug(struct ctl_table *table, int write,
271 size_t *lenp, loff_t *ppos)
278 static int proc_fail_loc(struct ctl_table *table, int write,
280 size_t *lenp, loff_t *ppos)
283 long old_fail_loc = cfs_fail_loc;
285 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
286 if (old_fail_loc != cfs_fail_loc)
287 wake_up(&cfs_race_waitq);
291 static int __proc_cpt_table(void *data, int write,
292 loff_t pos, void __user *buffer, int nob)
301 LASSERT(cfs_cpt_table);
304 LIBCFS_ALLOC(buf, len);
308 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
313 LIBCFS_FREE(buf, len);
325 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
328 LIBCFS_FREE(buf, len);
332 static int proc_cpt_table(struct ctl_table *table, int write,
333 void __user *buffer, size_t *lenp, loff_t *ppos)
335 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
339 static struct ctl_table lnet_table[] = {
342 .data = &libcfs_debug,
343 .maxlen = sizeof(int),
345 .proc_handler = &proc_dobitmasks,
348 .procname = "subsystem_debug",
349 .data = &libcfs_subsystem_debug,
350 .maxlen = sizeof(int),
352 .proc_handler = &proc_dobitmasks,
355 .procname = "printk",
356 .data = &libcfs_printk,
357 .maxlen = sizeof(int),
359 .proc_handler = &proc_dobitmasks,
362 .procname = "cpu_partition_table",
365 .proc_handler = &proc_cpt_table,
368 .procname = "debug_log_upcall",
369 .data = lnet_debug_log_upcall,
370 .maxlen = sizeof(lnet_debug_log_upcall),
372 .proc_handler = &proc_dostring,
375 .procname = "catastrophe",
376 .data = &libcfs_catastrophe,
377 .maxlen = sizeof(int),
379 .proc_handler = &proc_dointvec,
382 .procname = "dump_kernel",
385 .proc_handler = &proc_dump_kernel,
388 .procname = "daemon_file",
391 .proc_handler = &proc_daemon_file,
394 .procname = "force_lbug",
398 .proc_handler = &libcfs_force_lbug
401 .procname = "fail_loc",
402 .data = &cfs_fail_loc,
403 .maxlen = sizeof(cfs_fail_loc),
405 .proc_handler = &proc_fail_loc
408 .procname = "fail_val",
409 .data = &cfs_fail_val,
410 .maxlen = sizeof(int),
412 .proc_handler = &proc_dointvec
415 .procname = "fail_err",
416 .data = &cfs_fail_err,
417 .maxlen = sizeof(cfs_fail_err),
419 .proc_handler = &proc_dointvec,
425 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
426 { "console_ratelimit",
427 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
429 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
431 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
432 { "libcfs_console_backoff",
433 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
435 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
436 { "console_min_delay_centisecs",
437 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
438 { "console_max_delay_centisecs",
439 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
443 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
444 size_t count, loff_t *ppos)
446 struct ctl_table *table = filp->private_data;
449 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
456 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
457 size_t count, loff_t *ppos)
459 struct ctl_table *table = filp->private_data;
462 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
469 static const struct file_operations lnet_debugfs_file_operations_rw = {
471 .read = lnet_debugfs_read,
472 .write = lnet_debugfs_write,
473 .llseek = default_llseek,
476 static const struct file_operations lnet_debugfs_file_operations_ro = {
478 .read = lnet_debugfs_read,
479 .llseek = default_llseek,
482 static const struct file_operations lnet_debugfs_file_operations_wo = {
484 .write = lnet_debugfs_write,
485 .llseek = default_llseek,
488 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
491 return &lnet_debugfs_file_operations_ro;
494 return &lnet_debugfs_file_operations_wo;
496 return &lnet_debugfs_file_operations_rw;
499 void lustre_insert_debugfs(struct ctl_table *table,
500 const struct lnet_debugfs_symlink_def *symlinks)
502 if (!lnet_debugfs_root)
503 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
505 /* Even if we cannot create, just ignore it altogether) */
506 if (IS_ERR_OR_NULL(lnet_debugfs_root))
509 /* We don't save the dentry returned in next two calls, because
510 * we don't call debugfs_remove() but rather remove_recursive()
512 for (; table->procname; table++)
513 debugfs_create_file(table->procname, table->mode,
514 lnet_debugfs_root, table,
515 lnet_debugfs_fops_select(table->mode));
517 for (; symlinks && symlinks->name; symlinks++)
518 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
521 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
523 static void lustre_remove_debugfs(void)
525 debugfs_remove_recursive(lnet_debugfs_root);
527 lnet_debugfs_root = NULL;
530 static int libcfs_init(void)
534 rc = libcfs_debug_init(5 * 1024 * 1024);
536 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
544 rc = misc_register(&libcfs_dev);
546 CERROR("misc_register: error %d\n", rc);
550 rc = cfs_wi_startup();
552 CERROR("initialize workitem: error %d\n", rc);
553 goto cleanup_deregister;
556 /* max to 4 threads, should be enough for rehash */
557 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
558 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
559 rc, &cfs_sched_rehash);
561 CERROR("Startup workitem scheduler: error: %d\n", rc);
562 goto cleanup_deregister;
565 rc = cfs_crypto_register();
567 CERROR("cfs_crypto_register: error %d\n", rc);
571 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
573 CDEBUG(D_OTHER, "portals setup OK\n");
578 misc_deregister(&libcfs_dev);
582 libcfs_debug_cleanup();
586 static void libcfs_exit(void)
590 lustre_remove_debugfs();
592 if (cfs_sched_rehash) {
593 cfs_wi_sched_destroy(cfs_sched_rehash);
594 cfs_sched_rehash = NULL;
597 cfs_crypto_unregister();
600 misc_deregister(&libcfs_dev);
604 rc = libcfs_debug_cleanup();
606 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
609 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
610 MODULE_DESCRIPTION("Lustre helper library");
611 MODULE_VERSION(LIBCFS_VERSION);
612 MODULE_LICENSE("GPL");
614 module_init(libcfs_init);
615 module_exit(libcfs_exit);