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 static int proc_dobitmasks(struct ctl_table *table, int write,
271 void __user *buffer, size_t *lenp, loff_t *ppos)
273 const int tmpstrlen = 512;
278 unsigned int *mask = table->data;
279 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
280 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
283 tmpstr = kmalloc(tmpstrlen, GFP_KERNEL | __GFP_ZERO);
286 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
292 rc = cfs_trace_copyout_string(buffer, nob,
296 tmpstr = memdup_user_nul(buffer, nob);
298 return PTR_ERR(tmpstr);
300 rc = libcfs_debug_str2mask(mask, strim(tmpstr), is_subsys);
301 /* Always print LBUG/LASSERT to console, so keep this mask */
310 static int min_watchdog_ratelimit; /* disable ratelimiting */
311 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
313 static int proc_dump_kernel(struct ctl_table *table, int write,
314 void __user *buffer, size_t *lenp, loff_t *ppos)
321 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
324 static int proc_daemon_file(struct ctl_table *table, int write,
325 void __user *buffer, size_t *lenp, loff_t *ppos)
331 int len = strlen(cfs_tracefile);
336 return cfs_trace_copyout_string(buffer, nob,
337 cfs_tracefile + pos, "\n");
340 return cfs_trace_daemon_command_usrstr(buffer, nob);
343 static int libcfs_force_lbug(struct ctl_table *table, int write,
345 size_t *lenp, loff_t *ppos)
352 static int proc_fail_loc(struct ctl_table *table, int write,
353 void __user *buffer, size_t *lenp, loff_t *ppos)
356 long old_fail_loc = cfs_fail_loc;
358 if (!*lenp || *ppos) {
364 char *kbuf = memdup_user_nul(buffer, *lenp);
367 return PTR_ERR(kbuf);
368 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
374 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
375 if (copy_to_user(buffer, kbuf, rc))
383 if (old_fail_loc != cfs_fail_loc) {
385 wake_up(&cfs_race_waitq);
390 int debugfs_doint(struct ctl_table *table, int write,
391 void __user *buffer, size_t *lenp, loff_t *ppos)
395 if (!*lenp || *ppos) {
401 char *kbuf = memdup_user_nul(buffer, *lenp);
405 return PTR_ERR(kbuf);
407 rc = kstrtoint(kbuf, 0, &val);
410 if (table->extra1 && val < *(int *)table->extra1)
411 val = *(int *)table->extra1;
412 if (table->extra2 && val > *(int *)table->extra2)
413 val = *(int *)table->extra2;
414 *(int *)table->data = val;
420 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
421 if (copy_to_user(buffer, kbuf, rc))
431 EXPORT_SYMBOL(debugfs_doint);
433 static int debugfs_dou64(struct ctl_table *table, int write,
434 void __user *buffer, size_t *lenp, loff_t *ppos)
438 if (!*lenp || *ppos) {
444 char *kbuf = memdup_user_nul(buffer, *lenp);
445 unsigned long long val;
448 return PTR_ERR(kbuf);
450 rc = kstrtoull(kbuf, 0, &val);
453 *(u64 *)table->data = val;
458 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
459 (unsigned long long)*(u64 *)table->data);
460 if (copy_to_user(buffer, kbuf, rc))
471 static int debugfs_dostring(struct ctl_table *table, int write,
472 void __user *buffer, size_t *lenp, loff_t *ppos)
475 char *kbuf = table->data;
481 if (len > table->maxlen)
484 if (copy_from_user(kbuf, buffer, len))
486 memset(kbuf+len, 0, table->maxlen - len);
489 len = strnlen(kbuf, len);
490 if (copy_to_user(buffer, kbuf, len))
493 if (copy_to_user(buffer+len, "\n", 1))
503 static int proc_cpt_table(struct ctl_table *table, int write,
504 void __user *buffer, size_t *lenp, loff_t *ppos)
516 LIBCFS_ALLOC(buf, len);
520 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
525 LIBCFS_FREE(buf, len);
537 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
540 LIBCFS_FREE(buf, len);
544 static int proc_cpt_distance(struct ctl_table *table, int write,
545 void __user *buffer, size_t *lenp, loff_t *ppos)
557 LIBCFS_ALLOC(buf, len);
561 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
566 LIBCFS_FREE(buf, len);
578 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
581 LIBCFS_FREE(buf, len);
585 static struct ctl_table lnet_table[] = {
588 .data = &libcfs_debug,
589 .maxlen = sizeof(int),
591 .proc_handler = &proc_dobitmasks,
594 .procname = "subsystem_debug",
595 .data = &libcfs_subsystem_debug,
596 .maxlen = sizeof(int),
598 .proc_handler = &proc_dobitmasks,
601 .procname = "printk",
602 .data = &libcfs_printk,
603 .maxlen = sizeof(int),
605 .proc_handler = &proc_dobitmasks,
608 .procname = "cpu_partition_table",
611 .proc_handler = &proc_cpt_table,
614 .procname = "cpu_partition_distance",
617 .proc_handler = &proc_cpt_distance,
620 .procname = "debug_log_upcall",
621 .data = lnet_debug_log_upcall,
622 .maxlen = sizeof(lnet_debug_log_upcall),
624 .proc_handler = &debugfs_dostring,
627 .procname = "lnet_memused",
628 .data = (u64 *)&libcfs_kmem.counter,
629 .maxlen = sizeof(u64),
631 .proc_handler = &debugfs_dou64,
634 .procname = "catastrophe",
635 .data = &libcfs_catastrophe,
636 .maxlen = sizeof(int),
638 .proc_handler = &debugfs_doint,
641 .procname = "dump_kernel",
644 .proc_handler = &proc_dump_kernel,
647 .procname = "daemon_file",
650 .proc_handler = &proc_daemon_file,
653 .procname = "watchdog_ratelimit",
654 .data = &libcfs_watchdog_ratelimit,
655 .maxlen = sizeof(int),
657 .proc_handler = &debugfs_doint,
658 .extra1 = &min_watchdog_ratelimit,
659 .extra2 = &max_watchdog_ratelimit,
662 .procname = "force_lbug",
666 .proc_handler = &libcfs_force_lbug
669 .procname = "fail_loc",
670 .data = &cfs_fail_loc,
671 .maxlen = sizeof(cfs_fail_loc),
673 .proc_handler = &proc_fail_loc
676 .procname = "fail_val",
677 .data = &cfs_fail_val,
678 .maxlen = sizeof(int),
680 .proc_handler = &debugfs_doint
683 .procname = "fail_err",
684 .data = &cfs_fail_err,
685 .maxlen = sizeof(cfs_fail_err),
687 .proc_handler = &debugfs_doint,
693 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
694 { .name = "console_ratelimit",
695 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
696 { .name = "debug_path",
697 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
698 { .name = "panic_on_lbug",
699 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
700 { .name = "console_backoff",
701 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
702 { .name = "debug_mb",
703 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
704 { .name = "console_min_delay_centisecs",
705 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
706 { .name = "console_max_delay_centisecs",
707 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
711 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
712 size_t count, loff_t *ppos)
714 struct ctl_table *table = filp->private_data;
715 loff_t old_pos = *ppos;
716 ssize_t rc = -EINVAL;
719 rc = table->proc_handler(table, 0, (void __user *)buf,
722 * On success, the length read is either in error or in count.
723 * If ppos changed, then use count, else use error
725 if (!rc && *ppos != old_pos)
733 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
734 size_t count, loff_t *ppos)
736 struct ctl_table *table = filp->private_data;
737 loff_t old_pos = *ppos;
738 ssize_t rc = -EINVAL;
741 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
746 if (*ppos == old_pos)
752 static const struct file_operations lnet_debugfs_file_operations_rw = {
754 .read = lnet_debugfs_read,
755 .write = lnet_debugfs_write,
756 .llseek = default_llseek,
759 static const struct file_operations lnet_debugfs_file_operations_ro = {
761 .read = lnet_debugfs_read,
762 .llseek = default_llseek,
765 static const struct file_operations lnet_debugfs_file_operations_wo = {
767 .write = lnet_debugfs_write,
768 .llseek = default_llseek,
771 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
773 if (!(mode & S_IWUGO))
774 return &lnet_debugfs_file_operations_ro;
776 if (!(mode & S_IRUGO))
777 return &lnet_debugfs_file_operations_wo;
779 return &lnet_debugfs_file_operations_rw;
782 void lnet_insert_debugfs(struct ctl_table *table)
784 if (!lnet_debugfs_root)
785 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
787 /* Even if we cannot create, just ignore it altogether) */
788 if (IS_ERR_OR_NULL(lnet_debugfs_root))
791 /* We don't save the dentry returned in next two calls, because
792 * we don't call debugfs_remove() but rather remove_recursive()
794 for (; table && table->procname; table++)
795 debugfs_create_file(table->procname, table->mode,
796 lnet_debugfs_root, table,
797 lnet_debugfs_fops_select(table->mode));
799 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
801 static void lnet_insert_debugfs_links(
802 const struct lnet_debugfs_symlink_def *symlinks)
804 for (; symlinks && symlinks->name; symlinks++)
805 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
809 void lnet_remove_debugfs(struct ctl_table *table)
811 for (; table && table->procname; table++) {
812 struct qstr dname = QSTR_INIT(table->procname,
813 strlen(table->procname));
814 struct dentry *dentry;
816 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
817 debugfs_remove(dentry);
820 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
822 static int __init libcfs_init(void)
828 init_libcfs_vfree_atomic();
830 rc = libcfs_debug_init(5 * 1024 * 1024);
832 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
842 rc = misc_register(&libcfs_dev);
844 CERROR("misc_register: error %d\n", rc);
848 rc = cfs_wi_startup();
850 CERROR("initialize workitem: error %d\n", rc);
851 goto cleanup_deregister;
854 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
855 if (!cfs_rehash_wq) {
857 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
859 goto cleanup_deregister;
862 rc = cfs_crypto_register();
864 CERROR("cfs_crypto_regster: error %d\n", rc);
868 lnet_insert_debugfs(lnet_table);
869 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
870 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
874 CERROR("llcrypt_init: error %d\n", rc);
878 CDEBUG (D_OTHER, "portals setup OK\n");
883 misc_deregister(&libcfs_dev);
887 libcfs_debug_cleanup();
891 static void __exit libcfs_exit(void)
895 /* Remove everthing */
896 debugfs_remove_recursive(lnet_debugfs_root);
897 lnet_debugfs_root = NULL;
899 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
905 destroy_workqueue(cfs_rehash_wq);
906 cfs_rehash_wq = NULL;
909 cfs_crypto_unregister();
912 misc_deregister(&libcfs_dev);
916 /* the below message is checked in test-framework.sh check_mem_leak() */
917 if (libcfs_kmem_read() != 0)
918 CERROR("Portals memory leaked: %lld bytes\n",
921 rc = libcfs_debug_cleanup();
923 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
925 exit_libcfs_vfree_atomic();
928 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
929 MODULE_DESCRIPTION("Lustre helper library");
930 MODULE_VERSION(LIBCFS_VERSION);
931 MODULE_LICENSE("GPL");
933 module_init(libcfs_init);
934 module_exit(libcfs_exit);