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 <libcfs/crypto/llcrypt.h>
58 #include "tracefile.h"
60 static struct dentry *lnet_debugfs_root;
62 BLOCKING_NOTIFIER_HEAD(libcfs_ioctl_list);
63 EXPORT_SYMBOL(libcfs_ioctl_list);
65 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
67 size_t len = sizeof(*data);
69 len += (data->ioc_inllen1 + 7) & ~7;
70 len += (data->ioc_inllen2 + 7) & ~7;
74 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
76 const int maxlen = 1 << 30;
77 if (data->ioc_hdr.ioc_len > maxlen)
80 if (data->ioc_inllen1 > maxlen)
83 if (data->ioc_inllen2 > maxlen)
86 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
89 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
92 if (data->ioc_pbuf1 && !data->ioc_plen1)
95 if (data->ioc_pbuf2 && !data->ioc_plen2)
98 if (data->ioc_plen1 && !data->ioc_pbuf1)
101 if (data->ioc_plen2 && !data->ioc_pbuf2)
104 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
107 if (data->ioc_inllen1 &&
108 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
109 data->ioc_inllen2 - 1] != '\0')
115 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
119 if (libcfs_ioctl_is_invalid(data)) {
120 CERROR("libcfs ioctl: parameter not correctly formatted\n");
124 if (data->ioc_inllen1 != 0)
125 data->ioc_inlbuf1 = &data->ioc_bulk[0];
127 if (data->ioc_inllen2 != 0)
128 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
129 cfs_size_round(data->ioc_inllen1);
134 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
135 struct libcfs_ioctl_hdr __user *uhdr)
137 struct libcfs_ioctl_hdr hdr;
141 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
144 if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
145 hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
146 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
147 LIBCFS_IOCTL_VERSION, hdr.ioc_version);
151 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
152 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
156 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
157 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
158 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
162 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
166 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
167 GOTO(free, err = -EFAULT);
169 if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
170 (*hdr_pp)->ioc_len != hdr.ioc_len) {
171 GOTO(free, err = -EINVAL);
177 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
181 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
183 struct libcfs_ioctl_data *data = NULL;
184 struct libcfs_ioctl_hdr *hdr;
188 /* 'cmd' and permissions get checked in our arch-specific caller */
189 err = libcfs_ioctl_getdata(&hdr, uparam);
191 CDEBUG_LIMIT(D_ERROR,
192 "libcfs ioctl: data header error %d\n", err);
196 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
197 /* The libcfs_ioctl_data_adjust() function performs adjustment
198 * operations on the libcfs_ioctl_data structure to make
199 * it usable by the code. This doesn't need to be called
200 * for new data structures added. */
201 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
202 err = libcfs_ioctl_data_adjust(data);
207 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
209 case IOC_LIBCFS_CLEAR_DEBUG:
210 libcfs_debug_clear_buffer();
212 case IOC_LIBCFS_MARK_DEBUG:
214 data->ioc_inlbuf1 == NULL ||
215 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
216 GOTO(out, err = -EINVAL);
218 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
222 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
224 if (!(err & NOTIFY_STOP_MASK))
225 /* No-one claimed the ioctl */
228 err = notifier_to_errno(err);
229 if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
234 LIBCFS_FREE(hdr, hdr->ioc_len);
239 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
241 if (!capable(CAP_SYS_ADMIN))
244 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
245 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
246 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
247 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
248 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
252 return libcfs_ioctl(cmd, (void __user *)arg);
255 static const struct file_operations libcfs_fops = {
256 .owner = THIS_MODULE,
257 .unlocked_ioctl = libcfs_psdev_ioctl,
260 static struct miscdevice libcfs_dev = {
261 .minor = MISC_DYNAMIC_MINOR,
263 .fops = &libcfs_fops,
266 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
267 void __user *buffer, size_t *lenp,
268 int (*handler)(void *data, int write, loff_t pos,
269 void __user *buffer, int len))
271 int rc = handler(data, write, *ppos, buffer, *lenp);
284 EXPORT_SYMBOL(lprocfs_call_handler);
286 static int __proc_dobitmasks(void *data, int write,
287 loff_t pos, void __user *buffer, int nob)
289 const int tmpstrlen = 512;
292 unsigned int *mask = data;
293 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
294 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
296 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
301 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
307 rc = cfs_trace_copyout_string(buffer, nob,
311 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
317 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
318 /* Always print LBUG/LASSERT to console, so keep this mask */
327 static int proc_dobitmasks(struct ctl_table *table, int write,
328 void __user *buffer, size_t *lenp, loff_t *ppos)
330 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
334 static int min_watchdog_ratelimit; /* disable ratelimiting */
335 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
337 static int __proc_dump_kernel(void *data, int write,
338 loff_t pos, void __user *buffer, int nob)
343 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
346 static int proc_dump_kernel(struct ctl_table *table, int write,
347 void __user *buffer, size_t *lenp, loff_t *ppos)
349 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
353 static int __proc_daemon_file(void *data, int write,
354 loff_t pos, void __user *buffer, int nob)
357 int len = strlen(cfs_tracefile);
362 return cfs_trace_copyout_string(buffer, nob,
363 cfs_tracefile + pos, "\n");
366 return cfs_trace_daemon_command_usrstr(buffer, nob);
369 static int proc_daemon_file(struct ctl_table *table, int write,
370 void __user *buffer, size_t *lenp, loff_t *ppos)
372 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
376 static int libcfs_force_lbug(struct ctl_table *table, int write,
378 size_t *lenp, loff_t *ppos)
385 static int proc_fail_loc(struct ctl_table *table, int write,
386 void __user *buffer, size_t *lenp, loff_t *ppos)
389 long old_fail_loc = cfs_fail_loc;
391 if (!*lenp || *ppos) {
397 char *kbuf = memdup_user_nul(buffer, *lenp);
400 return PTR_ERR(kbuf);
401 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
407 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
408 if (copy_to_user(buffer, kbuf, rc))
416 if (old_fail_loc != cfs_fail_loc) {
418 wake_up(&cfs_race_waitq);
423 int debugfs_doint(struct ctl_table *table, int write,
424 void __user *buffer, size_t *lenp, loff_t *ppos)
428 if (!*lenp || *ppos) {
434 char *kbuf = memdup_user_nul(buffer, *lenp);
438 return PTR_ERR(kbuf);
440 rc = kstrtoint(kbuf, 0, &val);
443 if (table->extra1 && val < *(int *)table->extra1)
444 val = *(int *)table->extra1;
445 if (table->extra2 && val > *(int *)table->extra2)
446 val = *(int *)table->extra2;
447 *(int *)table->data = val;
453 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
454 if (copy_to_user(buffer, kbuf, rc))
464 EXPORT_SYMBOL(debugfs_doint);
466 static int debugfs_dou64(struct ctl_table *table, int write,
467 void __user *buffer, size_t *lenp, loff_t *ppos)
471 if (!*lenp || *ppos) {
477 char *kbuf = memdup_user_nul(buffer, *lenp);
478 unsigned long long val;
481 return PTR_ERR(kbuf);
483 rc = kstrtoull(kbuf, 0, &val);
486 *(u64 *)table->data = val;
491 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
492 (unsigned long long)*(u64 *)table->data);
493 if (copy_to_user(buffer, kbuf, rc))
504 static int debugfs_dostring(struct ctl_table *table, int write,
505 void __user *buffer, size_t *lenp, loff_t *ppos)
508 char *kbuf = table->data;
514 if (len > table->maxlen)
517 if (copy_from_user(kbuf, buffer, len))
519 memset(kbuf+len, 0, table->maxlen - len);
522 len = strnlen(kbuf, len);
523 if (copy_to_user(buffer, kbuf, len))
526 if (copy_to_user(buffer+len, "\n", 1))
536 static int __proc_cpt_table(void *data, int write,
537 loff_t pos, void __user *buffer, int nob)
547 LIBCFS_ALLOC(buf, len);
551 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
556 LIBCFS_FREE(buf, len);
568 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
571 LIBCFS_FREE(buf, len);
575 static int proc_cpt_table(struct ctl_table *table, int write,
576 void __user *buffer, size_t *lenp, loff_t *ppos)
578 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
582 static int __proc_cpt_distance(void *data, int write,
583 loff_t pos, void __user *buffer, int nob)
593 LIBCFS_ALLOC(buf, len);
597 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
602 LIBCFS_FREE(buf, len);
614 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
617 LIBCFS_FREE(buf, len);
621 static int proc_cpt_distance(struct ctl_table *table, int write,
622 void __user *buffer, size_t *lenp, loff_t *ppos)
624 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
625 __proc_cpt_distance);
628 static struct ctl_table lnet_table[] = {
631 .data = &libcfs_debug,
632 .maxlen = sizeof(int),
634 .proc_handler = &proc_dobitmasks,
637 .procname = "subsystem_debug",
638 .data = &libcfs_subsystem_debug,
639 .maxlen = sizeof(int),
641 .proc_handler = &proc_dobitmasks,
644 .procname = "printk",
645 .data = &libcfs_printk,
646 .maxlen = sizeof(int),
648 .proc_handler = &proc_dobitmasks,
651 .procname = "cpu_partition_table",
654 .proc_handler = &proc_cpt_table,
657 .procname = "cpu_partition_distance",
660 .proc_handler = &proc_cpt_distance,
663 .procname = "debug_log_upcall",
664 .data = lnet_debug_log_upcall,
665 .maxlen = sizeof(lnet_debug_log_upcall),
667 .proc_handler = &debugfs_dostring,
670 .procname = "lnet_memused",
671 .data = (u64 *)&libcfs_kmem.counter,
672 .maxlen = sizeof(u64),
674 .proc_handler = &debugfs_dou64,
677 .procname = "catastrophe",
678 .data = &libcfs_catastrophe,
679 .maxlen = sizeof(int),
681 .proc_handler = &debugfs_doint,
684 .procname = "dump_kernel",
687 .proc_handler = &proc_dump_kernel,
690 .procname = "daemon_file",
693 .proc_handler = &proc_daemon_file,
696 .procname = "watchdog_ratelimit",
697 .data = &libcfs_watchdog_ratelimit,
698 .maxlen = sizeof(int),
700 .proc_handler = &debugfs_doint,
701 .extra1 = &min_watchdog_ratelimit,
702 .extra2 = &max_watchdog_ratelimit,
705 .procname = "force_lbug",
709 .proc_handler = &libcfs_force_lbug
712 .procname = "fail_loc",
713 .data = &cfs_fail_loc,
714 .maxlen = sizeof(cfs_fail_loc),
716 .proc_handler = &proc_fail_loc
719 .procname = "fail_val",
720 .data = &cfs_fail_val,
721 .maxlen = sizeof(int),
723 .proc_handler = &debugfs_doint
726 .procname = "fail_err",
727 .data = &cfs_fail_err,
728 .maxlen = sizeof(cfs_fail_err),
730 .proc_handler = &debugfs_doint,
736 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
737 { .name = "console_ratelimit",
738 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
739 { .name = "debug_path",
740 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
741 { .name = "panic_on_lbug",
742 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
743 { .name = "console_backoff",
744 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
745 { .name = "debug_mb",
746 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
747 { .name = "console_min_delay_centisecs",
748 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
749 { .name = "console_max_delay_centisecs",
750 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
754 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
755 size_t count, loff_t *ppos)
757 struct ctl_table *table = filp->private_data;
758 ssize_t rc = -EINVAL;
761 rc = table->proc_handler(table, 0, buf, &count, ppos);
769 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
770 size_t count, loff_t *ppos)
772 struct ctl_table *table = filp->private_data;
773 ssize_t rc = -EINVAL;
776 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
785 static const struct file_operations lnet_debugfs_file_operations_rw = {
787 .read = lnet_debugfs_read,
788 .write = lnet_debugfs_write,
789 .llseek = default_llseek,
792 static const struct file_operations lnet_debugfs_file_operations_ro = {
794 .read = lnet_debugfs_read,
795 .llseek = default_llseek,
798 static const struct file_operations lnet_debugfs_file_operations_wo = {
800 .write = lnet_debugfs_write,
801 .llseek = default_llseek,
804 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
806 if (!(mode & S_IWUGO))
807 return &lnet_debugfs_file_operations_ro;
809 if (!(mode & S_IRUGO))
810 return &lnet_debugfs_file_operations_wo;
812 return &lnet_debugfs_file_operations_rw;
815 void lnet_insert_debugfs(struct ctl_table *table)
817 if (!lnet_debugfs_root)
818 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
820 /* Even if we cannot create, just ignore it altogether) */
821 if (IS_ERR_OR_NULL(lnet_debugfs_root))
824 /* We don't save the dentry returned in next two calls, because
825 * we don't call debugfs_remove() but rather remove_recursive()
827 for (; table && table->procname; table++)
828 debugfs_create_file(table->procname, table->mode,
829 lnet_debugfs_root, table,
830 lnet_debugfs_fops_select(table->mode));
832 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
834 static void lnet_insert_debugfs_links(
835 const struct lnet_debugfs_symlink_def *symlinks)
837 for (; symlinks && symlinks->name; symlinks++)
838 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
842 void lnet_remove_debugfs(struct ctl_table *table)
844 for (; table && table->procname; table++) {
845 struct qstr dname = QSTR_INIT(table->procname,
846 strlen(table->procname));
847 struct dentry *dentry;
849 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
850 debugfs_remove(dentry);
853 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
855 static int __init libcfs_init(void)
861 init_libcfs_vfree_atomic();
863 rc = libcfs_debug_init(5 * 1024 * 1024);
865 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
875 rc = misc_register(&libcfs_dev);
877 CERROR("misc_register: error %d\n", rc);
881 rc = cfs_wi_startup();
883 CERROR("initialize workitem: error %d\n", rc);
884 goto cleanup_deregister;
887 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
888 if (!cfs_rehash_wq) {
890 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
892 goto cleanup_deregister;
895 rc = cfs_crypto_register();
897 CERROR("cfs_crypto_regster: error %d\n", rc);
901 lnet_insert_debugfs(lnet_table);
902 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
903 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
907 CERROR("llcrypt_init: error %d\n", rc);
911 CDEBUG (D_OTHER, "portals setup OK\n");
916 misc_deregister(&libcfs_dev);
920 libcfs_debug_cleanup();
924 static void __exit libcfs_exit(void)
928 /* Remove everthing */
929 debugfs_remove_recursive(lnet_debugfs_root);
930 lnet_debugfs_root = NULL;
932 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
938 destroy_workqueue(cfs_rehash_wq);
939 cfs_rehash_wq = NULL;
942 cfs_crypto_unregister();
945 misc_deregister(&libcfs_dev);
949 /* the below message is checked in test-framework.sh check_mem_leak() */
950 if (libcfs_kmem_read() != 0)
951 CERROR("Portals memory leaked: %lld bytes\n",
954 rc = libcfs_debug_cleanup();
956 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
958 exit_libcfs_vfree_atomic();
961 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
962 MODULE_DESCRIPTION("Lustre helper library");
963 MODULE_VERSION(LIBCFS_VERSION);
964 MODULE_LICENSE("GPL");
966 module_init(libcfs_init);
967 module_exit(libcfs_exit);