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, 2017, 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/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
36 #include <linux/string.h>
37 #include <linux/stat.h>
38 #include <linux/errno.h>
39 #include <linux/unistd.h>
41 #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>
50 #include <asm/div64.h>
52 #define DEBUG_SUBSYSTEM S_LNET
54 #include <libcfs/libcfs.h>
55 #include <libcfs/libcfs_crypto.h>
56 #include <lnet/lib-lnet.h>
57 #include "tracefile.h"
59 static struct dentry *lnet_debugfs_root;
61 BLOCKING_NOTIFIER_HEAD(libcfs_ioctl_list);
62 EXPORT_SYMBOL(libcfs_ioctl_list);
64 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
66 size_t len = sizeof(*data);
68 len += (data->ioc_inllen1 + 7) & ~7;
69 len += (data->ioc_inllen2 + 7) & ~7;
73 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
75 if (data->ioc_hdr.ioc_len > BIT(30))
78 if (data->ioc_inllen1 > BIT(30))
81 if (data->ioc_inllen2 > BIT(30))
84 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
87 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
90 if (data->ioc_pbuf1 && !data->ioc_plen1)
93 if (data->ioc_pbuf2 && !data->ioc_plen2)
96 if (data->ioc_plen1 && !data->ioc_pbuf1)
99 if (data->ioc_plen2 && !data->ioc_pbuf2)
102 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
105 if (data->ioc_inllen1 &&
106 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
107 data->ioc_inllen2 - 1] != '\0')
113 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
117 if (libcfs_ioctl_is_invalid(data)) {
118 CERROR("libcfs ioctl: parameter not correctly formatted\n");
122 if (data->ioc_inllen1 != 0)
123 data->ioc_inlbuf1 = &data->ioc_bulk[0];
125 if (data->ioc_inllen2 != 0)
126 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
127 cfs_size_round(data->ioc_inllen1);
132 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
133 struct libcfs_ioctl_hdr __user *uhdr)
135 struct libcfs_ioctl_hdr hdr;
139 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
142 if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
143 hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
144 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
145 LIBCFS_IOCTL_VERSION, hdr.ioc_version);
149 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
150 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
154 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
155 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
156 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
160 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
164 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
165 GOTO(free, err = -EFAULT);
167 if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
168 (*hdr_pp)->ioc_len != hdr.ioc_len) {
169 GOTO(free, err = -EINVAL);
175 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
179 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
181 struct libcfs_ioctl_data *data = NULL;
182 struct libcfs_ioctl_hdr *hdr;
186 /* 'cmd' and permissions get checked in our arch-specific caller */
187 err = libcfs_ioctl_getdata(&hdr, uparam);
189 CDEBUG_LIMIT(D_ERROR,
190 "libcfs ioctl: data header error %d\n", err);
194 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
195 /* The libcfs_ioctl_data_adjust() function performs adjustment
196 * operations on the libcfs_ioctl_data structure to make
197 * it usable by the code. This doesn't need to be called
198 * for new data structures added. */
199 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
200 err = libcfs_ioctl_data_adjust(data);
205 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
207 case IOC_LIBCFS_CLEAR_DEBUG:
208 libcfs_debug_clear_buffer();
210 case IOC_LIBCFS_MARK_DEBUG:
212 data->ioc_inlbuf1 == NULL ||
213 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
214 GOTO(out, err = -EINVAL);
216 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
220 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
222 if (!(err & NOTIFY_STOP_MASK))
223 /* No-one claimed the ioctl */
226 err = notifier_to_errno(err);
228 if (copy_to_user(uparam, hdr, hdr->ioc_len))
233 LIBCFS_FREE(hdr, hdr->ioc_len);
238 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
240 if (!capable(CAP_SYS_ADMIN))
243 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
244 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
245 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
246 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
247 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
251 return libcfs_ioctl(cmd, (void __user *)arg);
254 static const struct file_operations libcfs_fops = {
255 .owner = THIS_MODULE,
256 .unlocked_ioctl = libcfs_psdev_ioctl,
259 static struct miscdevice libcfs_dev = {
260 .minor = MISC_DYNAMIC_MINOR,
262 .fops = &libcfs_fops,
265 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
266 void __user *buffer, size_t *lenp,
267 int (*handler)(void *data, int write, loff_t pos,
268 void __user *buffer, int len))
270 int rc = handler(data, write, *ppos, buffer, *lenp);
283 EXPORT_SYMBOL(lprocfs_call_handler);
285 static int __proc_dobitmasks(void *data, int write,
286 loff_t pos, void __user *buffer, int nob)
288 const int tmpstrlen = 512;
291 unsigned int *mask = data;
292 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
293 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
295 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
300 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
306 rc = cfs_trace_copyout_string(buffer, nob,
310 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
316 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
317 /* Always print LBUG/LASSERT to console, so keep this mask */
326 static int proc_dobitmasks(struct ctl_table *table, int write,
327 void __user *buffer, size_t *lenp, loff_t *ppos)
329 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
333 static int min_watchdog_ratelimit; /* disable ratelimiting */
334 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
336 static int __proc_dump_kernel(void *data, int write,
337 loff_t pos, void __user *buffer, int nob)
342 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
345 static int proc_dump_kernel(struct ctl_table *table, int write,
346 void __user *buffer, size_t *lenp, loff_t *ppos)
348 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
352 static int __proc_daemon_file(void *data, int write,
353 loff_t pos, void __user *buffer, int nob)
356 int len = strlen(cfs_tracefile);
361 return cfs_trace_copyout_string(buffer, nob,
362 cfs_tracefile + pos, "\n");
365 return cfs_trace_daemon_command_usrstr(buffer, nob);
368 static int proc_daemon_file(struct ctl_table *table, int write,
369 void __user *buffer, size_t *lenp, loff_t *ppos)
371 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
375 static int libcfs_force_lbug(struct ctl_table *table, int write,
377 size_t *lenp, loff_t *ppos)
384 static int proc_fail_loc(struct ctl_table *table, int write,
385 void __user *buffer, size_t *lenp, loff_t *ppos)
388 long old_fail_loc = cfs_fail_loc;
390 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
391 if (old_fail_loc != cfs_fail_loc)
392 wake_up(&cfs_race_waitq);
396 static int __proc_cpt_table(void *data, int write,
397 loff_t pos, void __user *buffer, int nob)
406 LASSERT(cfs_cpt_table != NULL);
409 LIBCFS_ALLOC(buf, len);
413 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
418 LIBCFS_FREE(buf, len);
430 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
433 LIBCFS_FREE(buf, len);
437 static int proc_cpt_table(struct ctl_table *table, int write,
438 void __user *buffer, size_t *lenp, loff_t *ppos)
440 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
444 static int __proc_cpt_distance(void *data, int write,
445 loff_t pos, void __user *buffer, int nob)
454 LASSERT(cfs_cpt_table != NULL);
457 LIBCFS_ALLOC(buf, len);
461 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
466 LIBCFS_FREE(buf, len);
478 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
481 LIBCFS_FREE(buf, len);
485 static int proc_cpt_distance(struct ctl_table *table, int write,
486 void __user *buffer, size_t *lenp, loff_t *ppos)
488 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
489 __proc_cpt_distance);
492 static struct ctl_table lnet_table[] = {
495 .data = &libcfs_debug,
496 .maxlen = sizeof(int),
498 .proc_handler = &proc_dobitmasks,
501 .procname = "subsystem_debug",
502 .data = &libcfs_subsystem_debug,
503 .maxlen = sizeof(int),
505 .proc_handler = &proc_dobitmasks,
508 .procname = "printk",
509 .data = &libcfs_printk,
510 .maxlen = sizeof(int),
512 .proc_handler = &proc_dobitmasks,
515 .procname = "cpu_partition_table",
518 .proc_handler = &proc_cpt_table,
521 .procname = "cpu_partition_distance",
524 .proc_handler = &proc_cpt_distance,
527 .procname = "debug_log_upcall",
528 .data = lnet_debug_log_upcall,
529 .maxlen = sizeof(lnet_debug_log_upcall),
531 .proc_handler = &proc_dostring,
534 .procname = "lnet_memused",
535 .data = (int *)&libcfs_kmemory.counter,
536 .maxlen = sizeof(int),
538 .proc_handler = &proc_dointvec,
541 .procname = "catastrophe",
542 .data = &libcfs_catastrophe,
543 .maxlen = sizeof(int),
545 .proc_handler = &proc_dointvec,
548 .procname = "dump_kernel",
551 .proc_handler = &proc_dump_kernel,
554 .procname = "daemon_file",
557 .proc_handler = &proc_daemon_file,
560 .procname = "watchdog_ratelimit",
561 .data = &libcfs_watchdog_ratelimit,
562 .maxlen = sizeof(int),
564 .proc_handler = &proc_dointvec_minmax,
565 .extra1 = &min_watchdog_ratelimit,
566 .extra2 = &max_watchdog_ratelimit,
569 .procname = "force_lbug",
573 .proc_handler = &libcfs_force_lbug
576 .procname = "fail_loc",
577 .data = &cfs_fail_loc,
578 .maxlen = sizeof(cfs_fail_loc),
580 .proc_handler = &proc_fail_loc
583 .procname = "fail_val",
584 .data = &cfs_fail_val,
585 .maxlen = sizeof(int),
587 .proc_handler = &proc_dointvec
590 .procname = "fail_err",
591 .data = &cfs_fail_err,
592 .maxlen = sizeof(cfs_fail_err),
594 .proc_handler = &proc_dointvec,
600 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
601 { .name = "console_ratelimit",
602 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
603 { .name = "debug_path",
604 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
605 { .name = "panic_on_lbug",
606 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
607 { .name = "console_backoff",
608 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
609 { .name = "debug_mb",
610 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
611 { .name = "console_min_delay_centisecs",
612 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
613 { .name = "console_max_delay_centisecs",
614 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
618 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
619 size_t count, loff_t *ppos)
621 struct ctl_table *table = filp->private_data;
622 ssize_t rc = -EINVAL;
625 rc = table->proc_handler(table, 0, buf, &count, ppos);
633 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
634 size_t count, loff_t *ppos)
636 struct ctl_table *table = filp->private_data;
637 ssize_t rc = -EINVAL;
640 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
649 static const struct file_operations lnet_debugfs_file_operations_rw = {
651 .read = lnet_debugfs_read,
652 .write = lnet_debugfs_write,
653 .llseek = default_llseek,
656 static const struct file_operations lnet_debugfs_file_operations_ro = {
658 .read = lnet_debugfs_read,
659 .llseek = default_llseek,
662 static const struct file_operations lnet_debugfs_file_operations_wo = {
664 .write = lnet_debugfs_write,
665 .llseek = default_llseek,
668 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
670 if (!(mode & S_IWUGO))
671 return &lnet_debugfs_file_operations_ro;
673 if (!(mode & S_IRUGO))
674 return &lnet_debugfs_file_operations_wo;
676 return &lnet_debugfs_file_operations_rw;
679 void lnet_insert_debugfs(struct ctl_table *table)
681 if (!lnet_debugfs_root)
682 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
684 /* Even if we cannot create, just ignore it altogether) */
685 if (IS_ERR_OR_NULL(lnet_debugfs_root))
688 /* We don't save the dentry returned in next two calls, because
689 * we don't call debugfs_remove() but rather remove_recursive()
691 for (; table && table->procname; table++)
692 debugfs_create_file(table->procname, table->mode,
693 lnet_debugfs_root, table,
694 lnet_debugfs_fops_select(table->mode));
696 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
698 static void lnet_insert_debugfs_links(
699 const struct lnet_debugfs_symlink_def *symlinks)
701 for (; symlinks && symlinks->name; symlinks++)
702 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
706 void lnet_remove_debugfs(struct ctl_table *table)
708 for (; table && table->procname; table++) {
709 struct qstr dname = QSTR_INIT(table->procname,
710 strlen(table->procname));
711 struct dentry *dentry;
713 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
714 debugfs_remove(dentry);
717 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
719 static int __init libcfs_init(void)
723 #ifndef HAVE_WAIT_VAR_EVENT
726 rc = libcfs_debug_init(5 * 1024 * 1024);
728 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
736 rc = misc_register(&libcfs_dev);
738 CERROR("misc_register: error %d\n", rc);
742 rc = cfs_wi_startup();
744 CERROR("initialize workitem: error %d\n", rc);
745 goto cleanup_deregister;
748 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
749 if (!cfs_rehash_wq) {
751 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
753 goto cleanup_deregister;
756 rc = cfs_crypto_register();
758 CERROR("cfs_crypto_regster: error %d\n", rc);
762 lnet_insert_debugfs(lnet_table);
763 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
764 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
766 CDEBUG (D_OTHER, "portals setup OK\n");
771 misc_deregister(&libcfs_dev);
775 libcfs_debug_cleanup();
779 static void __exit libcfs_exit(void)
783 /* Remove everthing */
784 debugfs_remove_recursive(lnet_debugfs_root);
785 lnet_debugfs_root = NULL;
787 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
788 atomic_read(&libcfs_kmemory));
791 destroy_workqueue(cfs_rehash_wq);
792 cfs_rehash_wq = NULL;
795 cfs_crypto_unregister();
798 misc_deregister(&libcfs_dev);
802 /* the below message is checked in test-framework.sh check_mem_leak() */
803 if (atomic_read(&libcfs_kmemory) != 0)
804 CERROR("Portals memory leaked: %d bytes\n",
805 atomic_read(&libcfs_kmemory));
807 rc = libcfs_debug_cleanup();
809 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
812 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
813 MODULE_DESCRIPTION("Lustre helper library");
814 MODULE_VERSION(LIBCFS_VERSION);
815 MODULE_LICENSE("GPL");
817 module_init(libcfs_init);
818 module_exit(libcfs_exit);