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>
41 #include <linux/uaccess.h>
44 #include <linux/file.h>
45 #include <linux/list.h>
47 #include <linux/sysctl.h>
48 #include <linux/proc_fs.h>
49 #include <asm/div64.h>
51 #define DEBUG_SUBSYSTEM S_LNET
53 #include <libcfs/libcfs.h>
54 #include <libcfs/libcfs_crypto.h>
55 #include <lnet/lib-lnet.h>
56 #include "tracefile.h"
59 static struct ctl_table_header *lnet_table_header;
62 static DECLARE_RWSEM(ioctl_list_sem);
63 static LIST_HEAD(ioctl_list);
65 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
69 down_write(&ioctl_list_sem);
70 if (!list_empty(&hand->item))
73 list_add_tail(&hand->item, &ioctl_list);
74 up_write(&ioctl_list_sem);
78 EXPORT_SYMBOL(libcfs_register_ioctl);
80 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
84 down_write(&ioctl_list_sem);
85 if (list_empty(&hand->item))
88 list_del_init(&hand->item);
89 up_write(&ioctl_list_sem);
93 EXPORT_SYMBOL(libcfs_deregister_ioctl);
95 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
97 struct libcfs_ioctl_data *data = NULL;
98 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) {
111 /* The libcfs_ioctl_data_adjust() function performs adjustment
112 * operations on the libcfs_ioctl_data structure to make
113 * it usable by the code. This doesn't need to be called
114 * for new data structures added. */
115 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
116 err = libcfs_ioctl_data_adjust(data);
121 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
123 case IOC_LIBCFS_CLEAR_DEBUG:
124 libcfs_debug_clear_buffer();
126 case IOC_LIBCFS_MARK_DEBUG:
128 data->ioc_inlbuf1 == NULL ||
129 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
130 GOTO(out, err = -EINVAL);
132 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
136 struct libcfs_ioctl_handler *hand;
139 down_read(&ioctl_list_sem);
140 list_for_each_entry(hand, &ioctl_list, item) {
141 err = hand->handle_ioctl(cmd, hdr);
146 if (copy_to_user(uparam, hdr, hdr->ioc_len))
151 up_read(&ioctl_list_sem);
155 LIBCFS_FREE(hdr, hdr->ioc_len);
160 lprocfs_call_handler(void *data, int write, loff_t *ppos,
161 void __user *buffer, size_t *lenp,
162 int (*handler)(void *data, int write, loff_t pos,
163 void __user *buffer, int len))
165 int rc = handler(data, write, *ppos, buffer, *lenp);
178 EXPORT_SYMBOL(lprocfs_call_handler);
180 static int __proc_dobitmasks(void *data, int write,
181 loff_t pos, void __user *buffer, int nob)
183 const int tmpstrlen = 512;
186 unsigned int *mask = data;
187 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
188 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
190 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
195 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
201 rc = cfs_trace_copyout_string(buffer, nob,
205 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
211 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
212 /* Always print LBUG/LASSERT to console, so keep this mask */
222 proc_dobitmasks(struct ctl_table *table, int write,
223 void __user *buffer, size_t *lenp, loff_t *ppos)
225 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
229 static int min_watchdog_ratelimit; /* disable ratelimiting */
230 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
232 static int __proc_dump_kernel(void *data, int write,
233 loff_t pos, void __user *buffer, int nob)
238 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
242 proc_dump_kernel(struct ctl_table *table, int write, void __user *buffer,
243 size_t *lenp, loff_t *ppos)
245 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
249 static int __proc_daemon_file(void *data, int write,
250 loff_t pos, void __user *buffer, int nob)
253 int len = strlen(cfs_tracefile);
258 return cfs_trace_copyout_string(buffer, nob,
259 cfs_tracefile + pos, "\n");
262 return cfs_trace_daemon_command_usrstr(buffer, nob);
266 proc_daemon_file(struct ctl_table *table, int write, void __user *buffer,
267 size_t *lenp, loff_t *ppos)
269 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
273 static int __proc_debug_mb(void *data, int write,
274 loff_t pos, void __user *buffer, int nob)
278 int len = snprintf(tmpstr, sizeof(tmpstr), "%d",
279 cfs_trace_get_debug_mb());
284 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
288 return cfs_trace_set_debug_mb_usrstr(buffer, nob);
292 proc_debug_mb(struct ctl_table *table, int write, void __user *buffer,
293 size_t *lenp, loff_t *ppos)
295 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
300 proc_console_max_delay_cs(struct ctl_table *table, int write,
301 void __user *buffer, size_t *lenp, loff_t *ppos)
303 int rc, max_delay_cs;
304 struct ctl_table dummy = *table;
307 dummy.data = &max_delay_cs;
308 dummy.proc_handler = &proc_dointvec;
310 if (!write) { /* read */
311 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
312 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
318 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
321 if (max_delay_cs <= 0)
324 d = cfs_time_seconds(max_delay_cs) / 100;
325 if (d == 0 || d < libcfs_console_min_delay)
327 libcfs_console_max_delay = d;
333 proc_console_min_delay_cs(struct ctl_table *table, int write,
334 void __user *buffer, size_t *lenp, loff_t *ppos)
336 int rc, min_delay_cs;
337 struct ctl_table dummy = *table;
340 dummy.data = &min_delay_cs;
341 dummy.proc_handler = &proc_dointvec;
343 if (!write) { /* read */
344 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
345 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
351 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
354 if (min_delay_cs <= 0)
357 d = cfs_time_seconds(min_delay_cs) / 100;
358 if (d == 0 || d > libcfs_console_max_delay)
360 libcfs_console_min_delay = d;
366 proc_console_backoff(struct ctl_table *table, int write, void __user *buffer,
367 size_t *lenp, loff_t *ppos)
370 struct ctl_table dummy = *table;
372 dummy.data = &backoff;
373 dummy.proc_handler = &proc_dointvec;
375 if (!write) { /* read */
376 backoff = libcfs_console_backoff;
377 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
383 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
390 libcfs_console_backoff = backoff;
396 libcfs_force_lbug(struct ctl_table *table, int write, void __user *buffer,
397 size_t *lenp, loff_t *ppos)
405 proc_fail_loc(struct ctl_table *table, int write, void __user *buffer,
406 size_t *lenp, loff_t *ppos)
409 long old_fail_loc = cfs_fail_loc;
411 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
412 if (old_fail_loc != cfs_fail_loc)
413 wake_up(&cfs_race_waitq);
417 static int __proc_cpt_table(void *data, int write,
418 loff_t pos, void __user *buffer, int nob)
427 LASSERT(cfs_cpt_table != NULL);
430 LIBCFS_ALLOC(buf, len);
434 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
439 LIBCFS_FREE(buf, len);
451 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
454 LIBCFS_FREE(buf, len);
459 proc_cpt_table(struct ctl_table *table, int write, void __user *buffer,
460 size_t *lenp, loff_t *ppos)
462 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
466 static int __proc_cpt_distance(void *data, int write,
467 loff_t pos, void __user *buffer, int nob)
476 LASSERT(cfs_cpt_table != NULL);
479 LIBCFS_ALLOC(buf, len);
483 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
488 LIBCFS_FREE(buf, len);
500 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
503 LIBCFS_FREE(buf, len);
508 proc_cpt_distance(struct ctl_table *table, int write, void __user *buffer,
509 size_t *lenp, loff_t *ppos)
511 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
512 __proc_cpt_distance);
515 static struct ctl_table lnet_table[] = {
517 * NB No .strategy entries have been provided since sysctl(8) prefers
518 * to go via /proc for portability.
523 .data = &libcfs_debug,
524 .maxlen = sizeof(int),
526 .proc_handler = &proc_dobitmasks,
530 .procname = "subsystem_debug",
531 .data = &libcfs_subsystem_debug,
532 .maxlen = sizeof(int),
534 .proc_handler = &proc_dobitmasks,
538 .procname = "printk",
539 .data = &libcfs_printk,
540 .maxlen = sizeof(int),
542 .proc_handler = &proc_dobitmasks,
546 .procname = "console_ratelimit",
547 .data = &libcfs_console_ratelimit,
548 .maxlen = sizeof(int),
550 .proc_handler = &proc_dointvec
554 .procname = "console_max_delay_centisecs",
555 .maxlen = sizeof(int),
557 .proc_handler = &proc_console_max_delay_cs
561 .procname = "console_min_delay_centisecs",
562 .maxlen = sizeof(int),
564 .proc_handler = &proc_console_min_delay_cs
568 .procname = "console_backoff",
569 .maxlen = sizeof(int),
571 .proc_handler = &proc_console_backoff
575 .procname = "debug_path",
576 .data = libcfs_debug_file_path_arr,
577 .maxlen = sizeof(libcfs_debug_file_path_arr),
579 .proc_handler = &proc_dostring,
583 .procname = "cpu_partition_table",
586 .proc_handler = &proc_cpt_table,
590 .procname = "cpu_partition_distance",
593 .proc_handler = &proc_cpt_distance,
597 .procname = "debug_log_upcall",
598 .data = lnet_debug_log_upcall,
599 .maxlen = sizeof(lnet_debug_log_upcall),
601 .proc_handler = &proc_dostring,
605 .procname = "lnet_memused",
606 .data = (int *)&libcfs_kmemory.counter,
607 .maxlen = sizeof(int),
609 .proc_handler = &proc_dointvec,
613 .procname = "catastrophe",
614 .data = &libcfs_catastrophe,
615 .maxlen = sizeof(int),
617 .proc_handler = &proc_dointvec,
621 .procname = "panic_on_lbug",
622 .data = &libcfs_panic_on_lbug,
623 .maxlen = sizeof(int),
625 .proc_handler = &proc_dointvec,
629 .procname = "dump_kernel",
632 .proc_handler = &proc_dump_kernel,
636 .procname = "daemon_file",
639 .proc_handler = &proc_daemon_file,
643 .procname = "debug_mb",
645 .proc_handler = &proc_debug_mb,
649 .procname = "watchdog_ratelimit",
650 .data = &libcfs_watchdog_ratelimit,
651 .maxlen = sizeof(int),
653 .proc_handler = &proc_dointvec_minmax,
654 .extra1 = &min_watchdog_ratelimit,
655 .extra2 = &max_watchdog_ratelimit,
659 .procname = "force_lbug",
663 .proc_handler = &libcfs_force_lbug
667 .procname = "fail_loc",
668 .data = &cfs_fail_loc,
669 .maxlen = sizeof(cfs_fail_loc),
671 .proc_handler = &proc_fail_loc
675 .procname = "fail_val",
676 .data = &cfs_fail_val,
677 .maxlen = sizeof(int),
679 .proc_handler = &proc_dointvec
683 .procname = "fail_err",
684 .data = &cfs_fail_err,
685 .maxlen = sizeof(cfs_fail_err),
687 .proc_handler = &proc_dointvec,
694 static struct ctl_table top_table[] = {
707 static int insert_proc(void)
710 if (lnet_table_header == NULL)
711 lnet_table_header = register_sysctl_table(top_table);
716 static void remove_proc(void)
719 if (lnet_table_header != NULL)
720 unregister_sysctl_table(lnet_table_header);
722 lnet_table_header = NULL;
726 static int __init libcfs_init(void)
730 rc = libcfs_debug_init(5 * 1024 * 1024);
732 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
740 rc = misc_register(&libcfs_dev);
742 CERROR("misc_register: error %d\n", rc);
746 rc = cfs_wi_startup();
748 CERROR("initialize workitem: error %d\n", rc);
749 goto cleanup_deregister;
752 /* max to 4 threads, should be enough for rehash */
753 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
754 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
755 rc, &cfs_sched_rehash);
757 CERROR("Startup workitem scheduler: error: %d\n", rc);
758 goto cleanup_deregister;
761 rc = cfs_crypto_register();
763 CERROR("cfs_crypto_regster: error %d\n", rc);
770 CERROR("insert_proc: error %d\n", rc);
774 CDEBUG (D_OTHER, "portals setup OK\n");
777 cfs_crypto_unregister();
781 misc_deregister(&libcfs_dev);
785 libcfs_debug_cleanup();
789 static void __exit libcfs_exit(void)
795 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
796 atomic_read(&libcfs_kmemory));
798 if (cfs_sched_rehash != NULL) {
799 cfs_wi_sched_destroy(cfs_sched_rehash);
800 cfs_sched_rehash = NULL;
803 cfs_crypto_unregister();
806 misc_deregister(&libcfs_dev);
810 if (atomic_read(&libcfs_kmemory) != 0)
811 CERROR("Portals memory leaked: %d bytes\n",
812 atomic_read(&libcfs_kmemory));
814 rc = libcfs_debug_cleanup();
816 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
820 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
821 MODULE_DESCRIPTION("Lustre helper library");
822 MODULE_VERSION(LIBCFS_VERSION);
823 MODULE_LICENSE("GPL");
825 module_init(libcfs_init);
826 module_exit(libcfs_exit);