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 struct ctl_table lnet_table[] = {
468 * NB No .strategy entries have been provided since sysctl(8) prefers
469 * to go via /proc for portability.
474 .data = &libcfs_debug,
475 .maxlen = sizeof(int),
477 .proc_handler = &proc_dobitmasks,
481 .procname = "subsystem_debug",
482 .data = &libcfs_subsystem_debug,
483 .maxlen = sizeof(int),
485 .proc_handler = &proc_dobitmasks,
489 .procname = "printk",
490 .data = &libcfs_printk,
491 .maxlen = sizeof(int),
493 .proc_handler = &proc_dobitmasks,
497 .procname = "console_ratelimit",
498 .data = &libcfs_console_ratelimit,
499 .maxlen = sizeof(int),
501 .proc_handler = &proc_dointvec
505 .procname = "console_max_delay_centisecs",
506 .maxlen = sizeof(int),
508 .proc_handler = &proc_console_max_delay_cs
512 .procname = "console_min_delay_centisecs",
513 .maxlen = sizeof(int),
515 .proc_handler = &proc_console_min_delay_cs
519 .procname = "console_backoff",
520 .maxlen = sizeof(int),
522 .proc_handler = &proc_console_backoff
526 .procname = "debug_path",
527 .data = libcfs_debug_file_path_arr,
528 .maxlen = sizeof(libcfs_debug_file_path_arr),
530 .proc_handler = &proc_dostring,
534 .procname = "cpu_partition_table",
537 .proc_handler = &proc_cpt_table,
541 .procname = "debug_log_upcall",
542 .data = lnet_debug_log_upcall,
543 .maxlen = sizeof(lnet_debug_log_upcall),
545 .proc_handler = &proc_dostring,
549 .procname = "lnet_memused",
550 .data = (int *)&libcfs_kmemory.counter,
551 .maxlen = sizeof(int),
553 .proc_handler = &proc_dointvec,
557 .procname = "catastrophe",
558 .data = &libcfs_catastrophe,
559 .maxlen = sizeof(int),
561 .proc_handler = &proc_dointvec,
565 .procname = "panic_on_lbug",
566 .data = &libcfs_panic_on_lbug,
567 .maxlen = sizeof(int),
569 .proc_handler = &proc_dointvec,
573 .procname = "dump_kernel",
576 .proc_handler = &proc_dump_kernel,
580 .procname = "daemon_file",
583 .proc_handler = &proc_daemon_file,
587 .procname = "debug_mb",
589 .proc_handler = &proc_debug_mb,
593 .procname = "watchdog_ratelimit",
594 .data = &libcfs_watchdog_ratelimit,
595 .maxlen = sizeof(int),
597 .proc_handler = &proc_dointvec_minmax,
598 .extra1 = &min_watchdog_ratelimit,
599 .extra2 = &max_watchdog_ratelimit,
603 .procname = "force_lbug",
607 .proc_handler = &libcfs_force_lbug
611 .procname = "fail_loc",
612 .data = &cfs_fail_loc,
613 .maxlen = sizeof(cfs_fail_loc),
615 .proc_handler = &proc_fail_loc
619 .procname = "fail_val",
620 .data = &cfs_fail_val,
621 .maxlen = sizeof(int),
623 .proc_handler = &proc_dointvec
627 .procname = "fail_err",
628 .data = &cfs_fail_err,
629 .maxlen = sizeof(cfs_fail_err),
631 .proc_handler = &proc_dointvec,
638 static struct ctl_table top_table[] = {
651 static int insert_proc(void)
654 if (lnet_table_header == NULL)
655 lnet_table_header = register_sysctl_table(top_table);
660 static void remove_proc(void)
663 if (lnet_table_header != NULL)
664 unregister_sysctl_table(lnet_table_header);
666 lnet_table_header = NULL;
670 static int __init libcfs_init(void)
674 rc = libcfs_debug_init(5 * 1024 * 1024);
676 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
684 rc = misc_register(&libcfs_dev);
686 CERROR("misc_register: error %d\n", rc);
690 rc = cfs_wi_startup();
692 CERROR("initialize workitem: error %d\n", rc);
693 goto cleanup_deregister;
696 /* max to 4 threads, should be enough for rehash */
697 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
698 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
699 rc, &cfs_sched_rehash);
701 CERROR("Startup workitem scheduler: error: %d\n", rc);
702 goto cleanup_deregister;
705 rc = cfs_crypto_register();
707 CERROR("cfs_crypto_regster: error %d\n", rc);
714 CERROR("insert_proc: error %d\n", rc);
718 CDEBUG (D_OTHER, "portals setup OK\n");
721 cfs_crypto_unregister();
725 misc_deregister(&libcfs_dev);
729 libcfs_debug_cleanup();
733 static void __exit libcfs_exit(void)
739 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
740 atomic_read(&libcfs_kmemory));
742 if (cfs_sched_rehash != NULL) {
743 cfs_wi_sched_destroy(cfs_sched_rehash);
744 cfs_sched_rehash = NULL;
747 cfs_crypto_unregister();
750 misc_deregister(&libcfs_dev);
754 if (atomic_read(&libcfs_kmemory) != 0)
755 CERROR("Portals memory leaked: %d bytes\n",
756 atomic_read(&libcfs_kmemory));
758 rc = libcfs_debug_cleanup();
760 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
764 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
765 MODULE_DESCRIPTION("Lustre helper library");
766 MODULE_VERSION(LIBCFS_VERSION);
767 MODULE_LICENSE("GPL");
769 module_init(libcfs_init);
770 module_exit(libcfs_exit);