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>
45 #include <linux/uaccess.h>
48 #include <linux/file.h>
49 #include <linux/list.h>
51 #include <linux/sysctl.h>
52 #include <linux/proc_fs.h>
53 #include <asm/div64.h>
55 #define DEBUG_SUBSYSTEM S_LNET
57 #include <libcfs/libcfs.h>
58 #include <libcfs/libcfs_crypto.h>
59 #include <lnet/lib-lnet.h>
60 #include "tracefile.h"
63 static struct ctl_table_header *lnet_table_header;
66 static DECLARE_RWSEM(ioctl_list_sem);
67 static LIST_HEAD(ioctl_list);
69 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
73 down_write(&ioctl_list_sem);
74 if (!list_empty(&hand->item))
77 list_add_tail(&hand->item, &ioctl_list);
78 up_write(&ioctl_list_sem);
82 EXPORT_SYMBOL(libcfs_register_ioctl);
84 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
88 down_write(&ioctl_list_sem);
89 if (list_empty(&hand->item))
92 list_del_init(&hand->item);
93 up_write(&ioctl_list_sem);
97 EXPORT_SYMBOL(libcfs_deregister_ioctl);
99 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
101 struct libcfs_ioctl_data *data = NULL;
102 struct libcfs_ioctl_hdr *hdr;
106 /* 'cmd' and permissions get checked in our arch-specific caller */
107 err = libcfs_ioctl_getdata(&hdr, uparam);
109 CDEBUG_LIMIT(D_ERROR,
110 "libcfs ioctl: data header error %d\n", err);
114 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
115 /* The libcfs_ioctl_data_adjust() function performs adjustment
116 * operations on the libcfs_ioctl_data structure to make
117 * it usable by the code. This doesn't need to be called
118 * for new data structures added. */
119 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
120 err = libcfs_ioctl_data_adjust(data);
125 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
127 case IOC_LIBCFS_CLEAR_DEBUG:
128 libcfs_debug_clear_buffer();
130 case IOC_LIBCFS_MARK_DEBUG:
132 data->ioc_inlbuf1 == NULL ||
133 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
134 GOTO(out, err = -EINVAL);
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);
164 lprocfs_call_handler(void *data, int write, loff_t *ppos,
165 void __user *buffer, size_t *lenp,
166 int (*handler)(void *data, int write, loff_t pos,
167 void __user *buffer, int len))
169 int rc = handler(data, write, *ppos, buffer, *lenp);
182 EXPORT_SYMBOL(lprocfs_call_handler);
184 static int __proc_dobitmasks(void *data, int write,
185 loff_t pos, void __user *buffer, int nob)
187 const int tmpstrlen = 512;
190 unsigned int *mask = data;
191 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
192 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
194 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
199 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
205 rc = cfs_trace_copyout_string(buffer, nob,
209 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
215 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
216 /* Always print LBUG/LASSERT to console, so keep this mask */
226 proc_dobitmasks(struct ctl_table *table, int write,
227 void __user *buffer, size_t *lenp, loff_t *ppos)
229 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
233 static int min_watchdog_ratelimit; /* disable ratelimiting */
234 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
236 static int __proc_dump_kernel(void *data, int write,
237 loff_t pos, void __user *buffer, int nob)
242 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
246 proc_dump_kernel(struct ctl_table *table, int write, void __user *buffer,
247 size_t *lenp, loff_t *ppos)
249 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
253 static int __proc_daemon_file(void *data, int write,
254 loff_t pos, void __user *buffer, int nob)
257 int len = strlen(cfs_tracefile);
262 return cfs_trace_copyout_string(buffer, nob,
263 cfs_tracefile + pos, "\n");
266 return cfs_trace_daemon_command_usrstr(buffer, nob);
270 proc_daemon_file(struct ctl_table *table, int write, void __user *buffer,
271 size_t *lenp, loff_t *ppos)
273 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
277 static int __proc_debug_mb(void *data, int write,
278 loff_t pos, void __user *buffer, int nob)
282 int len = snprintf(tmpstr, sizeof(tmpstr), "%d",
283 cfs_trace_get_debug_mb());
288 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
292 return cfs_trace_set_debug_mb_usrstr(buffer, nob);
296 proc_debug_mb(struct ctl_table *table, int write, void __user *buffer,
297 size_t *lenp, loff_t *ppos)
299 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
304 proc_console_max_delay_cs(struct ctl_table *table, int write,
305 void __user *buffer, size_t *lenp, loff_t *ppos)
307 int rc, max_delay_cs;
308 struct ctl_table dummy = *table;
311 dummy.data = &max_delay_cs;
312 dummy.proc_handler = &proc_dointvec;
314 if (!write) { /* read */
315 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
316 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
322 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
325 if (max_delay_cs <= 0)
328 d = cfs_time_seconds(max_delay_cs) / 100;
329 if (d == 0 || d < libcfs_console_min_delay)
331 libcfs_console_max_delay = d;
337 proc_console_min_delay_cs(struct ctl_table *table, int write,
338 void __user *buffer, size_t *lenp, loff_t *ppos)
340 int rc, min_delay_cs;
341 struct ctl_table dummy = *table;
344 dummy.data = &min_delay_cs;
345 dummy.proc_handler = &proc_dointvec;
347 if (!write) { /* read */
348 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
349 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
355 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
358 if (min_delay_cs <= 0)
361 d = cfs_time_seconds(min_delay_cs) / 100;
362 if (d == 0 || d > libcfs_console_max_delay)
364 libcfs_console_min_delay = d;
370 proc_console_backoff(struct ctl_table *table, int write, void __user *buffer,
371 size_t *lenp, loff_t *ppos)
374 struct ctl_table dummy = *table;
376 dummy.data = &backoff;
377 dummy.proc_handler = &proc_dointvec;
379 if (!write) { /* read */
380 backoff = libcfs_console_backoff;
381 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
387 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
394 libcfs_console_backoff = backoff;
400 libcfs_force_lbug(struct ctl_table *table, int write, void __user *buffer,
401 size_t *lenp, loff_t *ppos)
409 proc_fail_loc(struct ctl_table *table, int write, void __user *buffer,
410 size_t *lenp, loff_t *ppos)
413 long old_fail_loc = cfs_fail_loc;
415 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
416 if (old_fail_loc != cfs_fail_loc)
417 wake_up(&cfs_race_waitq);
421 static int __proc_cpt_table(void *data, int write,
422 loff_t pos, void __user *buffer, int nob)
431 LASSERT(cfs_cpt_table != NULL);
434 LIBCFS_ALLOC(buf, len);
438 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
443 LIBCFS_FREE(buf, len);
455 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
458 LIBCFS_FREE(buf, len);
463 proc_cpt_table(struct ctl_table *table, int write, void __user *buffer,
464 size_t *lenp, loff_t *ppos)
466 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
470 static struct ctl_table lnet_table[] = {
472 * NB No .strategy entries have been provided since sysctl(8) prefers
473 * to go via /proc for portability.
478 .data = &libcfs_debug,
479 .maxlen = sizeof(int),
481 .proc_handler = &proc_dobitmasks,
485 .procname = "subsystem_debug",
486 .data = &libcfs_subsystem_debug,
487 .maxlen = sizeof(int),
489 .proc_handler = &proc_dobitmasks,
493 .procname = "printk",
494 .data = &libcfs_printk,
495 .maxlen = sizeof(int),
497 .proc_handler = &proc_dobitmasks,
501 .procname = "console_ratelimit",
502 .data = &libcfs_console_ratelimit,
503 .maxlen = sizeof(int),
505 .proc_handler = &proc_dointvec
509 .procname = "console_max_delay_centisecs",
510 .maxlen = sizeof(int),
512 .proc_handler = &proc_console_max_delay_cs
516 .procname = "console_min_delay_centisecs",
517 .maxlen = sizeof(int),
519 .proc_handler = &proc_console_min_delay_cs
523 .procname = "console_backoff",
524 .maxlen = sizeof(int),
526 .proc_handler = &proc_console_backoff
530 .procname = "debug_path",
531 .data = libcfs_debug_file_path_arr,
532 .maxlen = sizeof(libcfs_debug_file_path_arr),
534 .proc_handler = &proc_dostring,
538 .procname = "cpu_partition_table",
541 .proc_handler = &proc_cpt_table,
545 .procname = "upcall",
547 .maxlen = sizeof(lnet_upcall),
549 .proc_handler = &proc_dostring,
553 .procname = "debug_log_upcall",
554 .data = lnet_debug_log_upcall,
555 .maxlen = sizeof(lnet_debug_log_upcall),
557 .proc_handler = &proc_dostring,
561 .procname = "lnet_memused",
562 .data = (int *)&libcfs_kmemory.counter,
563 .maxlen = sizeof(int),
565 .proc_handler = &proc_dointvec,
569 .procname = "catastrophe",
570 .data = &libcfs_catastrophe,
571 .maxlen = sizeof(int),
573 .proc_handler = &proc_dointvec,
577 .procname = "panic_on_lbug",
578 .data = &libcfs_panic_on_lbug,
579 .maxlen = sizeof(int),
581 .proc_handler = &proc_dointvec,
585 .procname = "dump_kernel",
588 .proc_handler = &proc_dump_kernel,
592 .procname = "daemon_file",
595 .proc_handler = &proc_daemon_file,
599 .procname = "debug_mb",
601 .proc_handler = &proc_debug_mb,
605 .procname = "watchdog_ratelimit",
606 .data = &libcfs_watchdog_ratelimit,
607 .maxlen = sizeof(int),
609 .proc_handler = &proc_dointvec_minmax,
610 .extra1 = &min_watchdog_ratelimit,
611 .extra2 = &max_watchdog_ratelimit,
615 .procname = "force_lbug",
619 .proc_handler = &libcfs_force_lbug
623 .procname = "fail_loc",
624 .data = &cfs_fail_loc,
625 .maxlen = sizeof(cfs_fail_loc),
627 .proc_handler = &proc_fail_loc
631 .procname = "fail_val",
632 .data = &cfs_fail_val,
633 .maxlen = sizeof(int),
635 .proc_handler = &proc_dointvec
639 .procname = "fail_err",
640 .data = &cfs_fail_err,
641 .maxlen = sizeof(cfs_fail_err),
643 .proc_handler = &proc_dointvec,
650 static struct ctl_table top_table[] = {
663 static int insert_proc(void)
666 if (lnet_table_header == NULL)
667 lnet_table_header = register_sysctl_table(top_table);
672 static void remove_proc(void)
675 if (lnet_table_header != NULL)
676 unregister_sysctl_table(lnet_table_header);
678 lnet_table_header = NULL;
682 static int __init libcfs_init(void)
686 rc = libcfs_debug_init(5 * 1024 * 1024);
688 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
696 rc = misc_register(&libcfs_dev);
698 CERROR("misc_register: error %d\n", rc);
702 rc = cfs_wi_startup();
704 CERROR("initialize workitem: error %d\n", rc);
705 goto cleanup_deregister;
708 /* max to 4 threads, should be enough for rehash */
709 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
710 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
711 rc, &cfs_sched_rehash);
713 CERROR("Startup workitem scheduler: error: %d\n", rc);
714 goto cleanup_deregister;
717 rc = cfs_crypto_register();
719 CERROR("cfs_crypto_regster: error %d\n", rc);
726 CERROR("insert_proc: error %d\n", rc);
730 CDEBUG (D_OTHER, "portals setup OK\n");
733 cfs_crypto_unregister();
737 misc_deregister(&libcfs_dev);
741 libcfs_debug_cleanup();
745 static void __exit libcfs_exit(void)
751 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
752 atomic_read(&libcfs_kmemory));
754 if (cfs_sched_rehash != NULL) {
755 cfs_wi_sched_destroy(cfs_sched_rehash);
756 cfs_sched_rehash = NULL;
759 cfs_crypto_unregister();
762 misc_deregister(&libcfs_dev);
766 if (atomic_read(&libcfs_kmemory) != 0)
767 CERROR("Portals memory leaked: %d bytes\n",
768 atomic_read(&libcfs_kmemory));
770 rc = libcfs_debug_cleanup();
772 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
776 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
777 MODULE_DESCRIPTION("Lustre helper library");
778 MODULE_VERSION(LIBCFS_VERSION);
779 MODULE_LICENSE("GPL");
781 module_init(libcfs_init);
782 module_exit(libcfs_exit);