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/
31 #include <linux/miscdevice.h>
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/debugfs.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 <libcfs/crypto/llcrypt.h>
57 #include "tracefile.h"
59 struct lnet_debugfs_symlink_def {
64 static struct dentry *lnet_debugfs_root;
66 BLOCKING_NOTIFIER_HEAD(libcfs_ioctl_list);
67 EXPORT_SYMBOL(libcfs_ioctl_list);
69 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
71 size_t len = sizeof(*data);
73 len += (data->ioc_inllen1 + 7) & ~7;
74 len += (data->ioc_inllen2 + 7) & ~7;
78 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
80 const int maxlen = 1 << 30;
81 if (data->ioc_hdr.ioc_len > maxlen)
84 if (data->ioc_inllen1 > maxlen)
87 if (data->ioc_inllen2 > maxlen)
90 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
93 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
96 if (data->ioc_pbuf1 && !data->ioc_plen1)
99 if (data->ioc_pbuf2 && !data->ioc_plen2)
102 if (data->ioc_plen1 && !data->ioc_pbuf1)
105 if (data->ioc_plen2 && !data->ioc_pbuf2)
108 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
111 if (data->ioc_inllen1 &&
112 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
113 data->ioc_inllen2 - 1] != '\0')
119 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
123 if (libcfs_ioctl_is_invalid(data)) {
124 CERROR("libcfs ioctl: parameter not correctly formatted\n");
128 if (data->ioc_inllen1 != 0)
129 data->ioc_inlbuf1 = &data->ioc_bulk[0];
131 if (data->ioc_inllen2 != 0)
132 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
133 cfs_size_round(data->ioc_inllen1);
138 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
139 struct libcfs_ioctl_hdr __user *uhdr)
141 struct libcfs_ioctl_hdr hdr;
145 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
148 if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
149 hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
150 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
151 LIBCFS_IOCTL_VERSION, hdr.ioc_version);
155 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
156 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
160 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
161 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
162 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
166 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
170 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
171 GOTO(free, err = -EFAULT);
173 if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
174 (*hdr_pp)->ioc_len != hdr.ioc_len) {
175 GOTO(free, err = -EINVAL);
181 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
185 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
187 struct libcfs_ioctl_data *data = NULL;
188 struct libcfs_ioctl_hdr *hdr;
192 /* 'cmd' and permissions get checked in our arch-specific caller */
193 err = libcfs_ioctl_getdata(&hdr, uparam);
195 CDEBUG_LIMIT(D_ERROR,
196 "libcfs ioctl: data header error %d\n", err);
200 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
201 /* The libcfs_ioctl_data_adjust() function performs adjustment
202 * operations on the libcfs_ioctl_data structure to make
203 * it usable by the code. This doesn't need to be called
204 * for new data structures added. */
205 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
206 err = libcfs_ioctl_data_adjust(data);
211 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
213 case IOC_LIBCFS_CLEAR_DEBUG:
214 libcfs_debug_clear_buffer();
216 case IOC_LIBCFS_MARK_DEBUG:
218 data->ioc_inlbuf1 == NULL ||
219 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
220 GOTO(out, err = -EINVAL);
222 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
226 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
228 if (!(err & NOTIFY_STOP_MASK))
229 /* No-one claimed the ioctl */
232 err = notifier_to_errno(err);
233 if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
238 LIBCFS_FREE(hdr, hdr->ioc_len);
243 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
245 if (!capable(CAP_SYS_ADMIN))
248 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
249 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
250 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
251 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
252 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
256 return libcfs_ioctl(cmd, (void __user *)arg);
259 static const struct file_operations libcfs_fops = {
260 .owner = THIS_MODULE,
261 .unlocked_ioctl = libcfs_psdev_ioctl,
264 static struct miscdevice libcfs_dev = {
265 .minor = MISC_DYNAMIC_MINOR,
267 .fops = &libcfs_fops,
270 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
271 void __user *buffer, size_t *lenp,
272 int (*handler)(void *data, int write, loff_t pos,
273 void __user *buffer, int len))
275 int rc = handler(data, write, *ppos, buffer, *lenp);
288 EXPORT_SYMBOL(lprocfs_call_handler);
290 static int __proc_dobitmasks(void *data, int write,
291 loff_t pos, void __user *buffer, int nob)
293 const int tmpstrlen = 512;
296 unsigned int *mask = data;
297 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
298 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
301 tmpstr = kmalloc(tmpstrlen, GFP_KERNEL | __GFP_ZERO);
304 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
310 rc = cfs_trace_copyout_string(buffer, nob,
314 tmpstr = memdup_user_nul(buffer, nob);
316 return PTR_ERR(tmpstr);
318 rc = libcfs_debug_str2mask(mask, strim(tmpstr), is_subsys);
319 /* Always print LBUG/LASSERT to console, so keep this mask */
328 static int proc_dobitmasks(struct ctl_table *table, int write,
329 void __user *buffer, size_t *lenp, loff_t *ppos)
331 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
335 static int min_watchdog_ratelimit; /* disable ratelimiting */
336 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
338 static int __proc_dump_kernel(void *data, int write,
339 loff_t pos, void __user *buffer, int nob)
344 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
347 static int proc_dump_kernel(struct ctl_table *table, int write,
348 void __user *buffer, size_t *lenp, loff_t *ppos)
350 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
354 static int __proc_daemon_file(void *data, int write,
355 loff_t pos, void __user *buffer, int nob)
358 int len = strlen(cfs_tracefile);
363 return cfs_trace_copyout_string(buffer, nob,
364 cfs_tracefile + pos, "\n");
367 return cfs_trace_daemon_command_usrstr(buffer, nob);
370 static int proc_daemon_file(struct ctl_table *table, int write,
371 void __user *buffer, size_t *lenp, loff_t *ppos)
373 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
377 static int libcfs_force_lbug(struct ctl_table *table, int write,
379 size_t *lenp, loff_t *ppos)
386 static int proc_fail_loc(struct ctl_table *table, int write,
387 void __user *buffer, size_t *lenp, loff_t *ppos)
390 long old_fail_loc = cfs_fail_loc;
392 if (!*lenp || *ppos) {
398 char *kbuf = memdup_user_nul(buffer, *lenp);
401 return PTR_ERR(kbuf);
402 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
408 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
409 if (copy_to_user(buffer, kbuf, rc))
417 if (old_fail_loc != cfs_fail_loc) {
419 wake_up(&cfs_race_waitq);
424 int debugfs_doint(struct ctl_table *table, int write,
425 void __user *buffer, size_t *lenp, loff_t *ppos)
429 if (!*lenp || *ppos) {
435 char *kbuf = memdup_user_nul(buffer, *lenp);
439 return PTR_ERR(kbuf);
441 rc = kstrtoint(kbuf, 0, &val);
444 if (table->extra1 && val < *(int *)table->extra1)
445 val = *(int *)table->extra1;
446 if (table->extra2 && val > *(int *)table->extra2)
447 val = *(int *)table->extra2;
448 *(int *)table->data = val;
454 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
455 if (copy_to_user(buffer, kbuf, rc))
465 EXPORT_SYMBOL(debugfs_doint);
467 static int debugfs_dou64(struct ctl_table *table, int write,
468 void __user *buffer, size_t *lenp, loff_t *ppos)
472 if (!*lenp || *ppos) {
478 char *kbuf = memdup_user_nul(buffer, *lenp);
479 unsigned long long val;
482 return PTR_ERR(kbuf);
484 rc = kstrtoull(kbuf, 0, &val);
487 *(u64 *)table->data = val;
492 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
493 (unsigned long long)*(u64 *)table->data);
494 if (copy_to_user(buffer, kbuf, rc))
505 static int debugfs_dostring(struct ctl_table *table, int write,
506 void __user *buffer, size_t *lenp, loff_t *ppos)
509 char *kbuf = table->data;
515 if (len > table->maxlen)
518 if (copy_from_user(kbuf, buffer, len))
520 memset(kbuf+len, 0, table->maxlen - len);
523 len = strnlen(kbuf, len);
524 if (copy_to_user(buffer, kbuf, len))
527 if (copy_to_user(buffer+len, "\n", 1))
537 static int __proc_cpt_table(void *data, int write,
538 loff_t pos, void __user *buffer, int nob)
548 LIBCFS_ALLOC(buf, len);
552 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
557 LIBCFS_FREE(buf, len);
569 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
572 LIBCFS_FREE(buf, len);
576 static int proc_cpt_table(struct ctl_table *table, int write,
577 void __user *buffer, size_t *lenp, loff_t *ppos)
579 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
583 static int __proc_cpt_distance(void *data, int write,
584 loff_t pos, void __user *buffer, int nob)
594 LIBCFS_ALLOC(buf, len);
598 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
603 LIBCFS_FREE(buf, len);
615 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
618 LIBCFS_FREE(buf, len);
622 static int proc_cpt_distance(struct ctl_table *table, int write,
623 void __user *buffer, size_t *lenp, loff_t *ppos)
625 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
626 __proc_cpt_distance);
629 static struct ctl_table lnet_table[] = {
632 .data = &libcfs_debug,
633 .maxlen = sizeof(int),
635 .proc_handler = &proc_dobitmasks,
638 .procname = "subsystem_debug",
639 .data = &libcfs_subsystem_debug,
640 .maxlen = sizeof(int),
642 .proc_handler = &proc_dobitmasks,
645 .procname = "printk",
646 .data = &libcfs_printk,
647 .maxlen = sizeof(int),
649 .proc_handler = &proc_dobitmasks,
652 .procname = "cpu_partition_table",
655 .proc_handler = &proc_cpt_table,
658 .procname = "cpu_partition_distance",
661 .proc_handler = &proc_cpt_distance,
664 .procname = "debug_log_upcall",
665 .data = lnet_debug_log_upcall,
666 .maxlen = sizeof(lnet_debug_log_upcall),
668 .proc_handler = &debugfs_dostring,
671 .procname = "lnet_memused",
672 .data = (u64 *)&libcfs_kmem.counter,
673 .maxlen = sizeof(u64),
675 .proc_handler = &debugfs_dou64,
678 .procname = "catastrophe",
679 .data = &libcfs_catastrophe,
680 .maxlen = sizeof(int),
682 .proc_handler = &debugfs_doint,
685 .procname = "dump_kernel",
688 .proc_handler = &proc_dump_kernel,
691 .procname = "daemon_file",
694 .proc_handler = &proc_daemon_file,
697 .procname = "watchdog_ratelimit",
698 .data = &libcfs_watchdog_ratelimit,
699 .maxlen = sizeof(int),
701 .proc_handler = &debugfs_doint,
702 .extra1 = &min_watchdog_ratelimit,
703 .extra2 = &max_watchdog_ratelimit,
706 .procname = "force_lbug",
710 .proc_handler = &libcfs_force_lbug
713 .procname = "fail_loc",
714 .data = &cfs_fail_loc,
715 .maxlen = sizeof(cfs_fail_loc),
717 .proc_handler = &proc_fail_loc
720 .procname = "fail_val",
721 .data = &cfs_fail_val,
722 .maxlen = sizeof(int),
724 .proc_handler = &debugfs_doint
727 .procname = "fail_err",
728 .data = &cfs_fail_err,
729 .maxlen = sizeof(cfs_fail_err),
731 .proc_handler = &debugfs_doint,
737 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
738 { .name = "console_ratelimit",
739 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
740 { .name = "debug_path",
741 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
742 { .name = "panic_on_lbug",
743 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
744 { .name = "console_backoff",
745 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
746 { .name = "debug_mb",
747 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
748 { .name = "console_min_delay_centisecs",
749 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
750 { .name = "console_max_delay_centisecs",
751 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
755 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
756 size_t count, loff_t *ppos)
758 struct ctl_table *table = filp->private_data;
759 ssize_t rc = -EINVAL;
762 rc = table->proc_handler(table, 0, buf, &count, ppos);
770 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
771 size_t count, loff_t *ppos)
773 struct ctl_table *table = filp->private_data;
774 ssize_t rc = -EINVAL;
777 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
786 static const struct file_operations lnet_debugfs_file_operations_rw = {
788 .read = lnet_debugfs_read,
789 .write = lnet_debugfs_write,
790 .llseek = default_llseek,
793 static const struct file_operations lnet_debugfs_file_operations_ro = {
795 .read = lnet_debugfs_read,
796 .llseek = default_llseek,
799 static const struct file_operations lnet_debugfs_file_operations_wo = {
801 .write = lnet_debugfs_write,
802 .llseek = default_llseek,
805 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
807 if (!(mode & S_IWUGO))
808 return &lnet_debugfs_file_operations_ro;
810 if (!(mode & S_IRUGO))
811 return &lnet_debugfs_file_operations_wo;
813 return &lnet_debugfs_file_operations_rw;
816 void lnet_insert_debugfs(struct ctl_table *table)
818 if (!lnet_debugfs_root)
819 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
821 /* Even if we cannot create, just ignore it altogether) */
822 if (IS_ERR_OR_NULL(lnet_debugfs_root))
825 /* We don't save the dentry returned in next two calls, because
826 * we don't call debugfs_remove() but rather remove_recursive()
828 for (; table && table->procname; table++)
829 debugfs_create_file(table->procname, table->mode,
830 lnet_debugfs_root, table,
831 lnet_debugfs_fops_select(table->mode));
833 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
835 static void lnet_insert_debugfs_links(
836 const struct lnet_debugfs_symlink_def *symlinks)
838 for (; symlinks && symlinks->name; symlinks++)
839 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
843 void lnet_remove_debugfs(struct ctl_table *table)
845 for (; table && table->procname; table++) {
846 struct qstr dname = QSTR_INIT(table->procname,
847 strlen(table->procname));
848 struct dentry *dentry;
850 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
851 debugfs_remove(dentry);
854 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
856 static int __init libcfs_init(void)
862 init_libcfs_vfree_atomic();
864 rc = libcfs_debug_init(5 * 1024 * 1024);
866 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
876 rc = misc_register(&libcfs_dev);
878 CERROR("misc_register: error %d\n", rc);
882 rc = cfs_wi_startup();
884 CERROR("initialize workitem: error %d\n", rc);
885 goto cleanup_deregister;
888 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
889 if (!cfs_rehash_wq) {
891 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
893 goto cleanup_deregister;
896 rc = cfs_crypto_register();
898 CERROR("cfs_crypto_regster: error %d\n", rc);
902 lnet_insert_debugfs(lnet_table);
903 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
904 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
908 CERROR("llcrypt_init: error %d\n", rc);
912 CDEBUG (D_OTHER, "portals setup OK\n");
917 misc_deregister(&libcfs_dev);
921 libcfs_debug_cleanup();
925 static void __exit libcfs_exit(void)
929 /* Remove everthing */
930 debugfs_remove_recursive(lnet_debugfs_root);
931 lnet_debugfs_root = NULL;
933 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
939 destroy_workqueue(cfs_rehash_wq);
940 cfs_rehash_wq = NULL;
943 cfs_crypto_unregister();
946 misc_deregister(&libcfs_dev);
950 /* the below message is checked in test-framework.sh check_mem_leak() */
951 if (libcfs_kmem_read() != 0)
952 CERROR("Portals memory leaked: %lld bytes\n",
955 rc = libcfs_debug_cleanup();
957 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
959 exit_libcfs_vfree_atomic();
962 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
963 MODULE_DESCRIPTION("Lustre helper library");
964 MODULE_VERSION(LIBCFS_VERSION);
965 MODULE_LICENSE("GPL");
967 module_init(libcfs_init);
968 module_exit(libcfs_exit);