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 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 const int maxlen = 1 << 30;
76 if (data->ioc_hdr.ioc_len > maxlen)
79 if (data->ioc_inllen1 > maxlen)
82 if (data->ioc_inllen2 > maxlen)
85 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
88 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
91 if (data->ioc_pbuf1 && !data->ioc_plen1)
94 if (data->ioc_pbuf2 && !data->ioc_plen2)
97 if (data->ioc_plen1 && !data->ioc_pbuf1)
100 if (data->ioc_plen2 && !data->ioc_pbuf2)
103 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
106 if (data->ioc_inllen1 &&
107 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
108 data->ioc_inllen2 - 1] != '\0')
114 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
118 if (libcfs_ioctl_is_invalid(data)) {
119 CERROR("libcfs ioctl: parameter not correctly formatted\n");
123 if (data->ioc_inllen1 != 0)
124 data->ioc_inlbuf1 = &data->ioc_bulk[0];
126 if (data->ioc_inllen2 != 0)
127 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
128 cfs_size_round(data->ioc_inllen1);
133 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
134 struct libcfs_ioctl_hdr __user *uhdr)
136 struct libcfs_ioctl_hdr hdr;
140 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
143 if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
144 hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
145 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
146 LIBCFS_IOCTL_VERSION, hdr.ioc_version);
150 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
151 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
155 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
156 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
157 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
161 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
165 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
166 GOTO(free, err = -EFAULT);
168 if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
169 (*hdr_pp)->ioc_len != hdr.ioc_len) {
170 GOTO(free, err = -EINVAL);
176 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
180 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
182 struct libcfs_ioctl_data *data = NULL;
183 struct libcfs_ioctl_hdr *hdr;
187 /* 'cmd' and permissions get checked in our arch-specific caller */
188 err = libcfs_ioctl_getdata(&hdr, uparam);
190 CDEBUG_LIMIT(D_ERROR,
191 "libcfs ioctl: data header error %d\n", err);
195 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
196 /* The libcfs_ioctl_data_adjust() function performs adjustment
197 * operations on the libcfs_ioctl_data structure to make
198 * it usable by the code. This doesn't need to be called
199 * for new data structures added. */
200 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
201 err = libcfs_ioctl_data_adjust(data);
206 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
208 case IOC_LIBCFS_CLEAR_DEBUG:
209 libcfs_debug_clear_buffer();
211 case IOC_LIBCFS_MARK_DEBUG:
213 data->ioc_inlbuf1 == NULL ||
214 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
215 GOTO(out, err = -EINVAL);
217 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
221 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
223 if (!(err & NOTIFY_STOP_MASK))
224 /* No-one claimed the ioctl */
227 err = notifier_to_errno(err);
228 if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
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;
296 tmpstr = kmalloc(tmpstrlen, GFP_KERNEL | __GFP_ZERO);
299 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
305 rc = cfs_trace_copyout_string(buffer, nob,
309 tmpstr = memdup_user_nul(buffer, nob);
313 rc = libcfs_debug_str2mask(mask, strim(tmpstr), is_subsys);
314 /* Always print LBUG/LASSERT to console, so keep this mask */
323 static int proc_dobitmasks(struct ctl_table *table, int write,
324 void __user *buffer, size_t *lenp, loff_t *ppos)
326 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
330 static int min_watchdog_ratelimit; /* disable ratelimiting */
331 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
333 static int __proc_dump_kernel(void *data, int write,
334 loff_t pos, void __user *buffer, int nob)
339 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
342 static int proc_dump_kernel(struct ctl_table *table, int write,
343 void __user *buffer, size_t *lenp, loff_t *ppos)
345 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
349 static int __proc_daemon_file(void *data, int write,
350 loff_t pos, void __user *buffer, int nob)
353 int len = strlen(cfs_tracefile);
358 return cfs_trace_copyout_string(buffer, nob,
359 cfs_tracefile + pos, "\n");
362 return cfs_trace_daemon_command_usrstr(buffer, nob);
365 static int proc_daemon_file(struct ctl_table *table, int write,
366 void __user *buffer, size_t *lenp, loff_t *ppos)
368 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
372 static int libcfs_force_lbug(struct ctl_table *table, int write,
374 size_t *lenp, loff_t *ppos)
381 static int proc_fail_loc(struct ctl_table *table, int write,
382 void __user *buffer, size_t *lenp, loff_t *ppos)
385 long old_fail_loc = cfs_fail_loc;
387 if (!*lenp || *ppos) {
393 char *kbuf = memdup_user_nul(buffer, *lenp);
396 return PTR_ERR(kbuf);
397 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
403 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
404 if (copy_to_user(buffer, kbuf, rc))
412 if (old_fail_loc != cfs_fail_loc) {
414 wake_up(&cfs_race_waitq);
419 int debugfs_doint(struct ctl_table *table, int write,
420 void __user *buffer, size_t *lenp, loff_t *ppos)
424 if (!*lenp || *ppos) {
430 char *kbuf = memdup_user_nul(buffer, *lenp);
434 return PTR_ERR(kbuf);
436 rc = kstrtoint(kbuf, 0, &val);
439 if (table->extra1 && val < *(int *)table->extra1)
440 val = *(int *)table->extra1;
441 if (table->extra2 && val > *(int *)table->extra2)
442 val = *(int *)table->extra2;
443 *(int *)table->data = val;
449 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
450 if (copy_to_user(buffer, kbuf, rc))
460 EXPORT_SYMBOL(debugfs_doint);
462 static int debugfs_dou64(struct ctl_table *table, int write,
463 void __user *buffer, size_t *lenp, loff_t *ppos)
467 if (!*lenp || *ppos) {
473 char *kbuf = memdup_user_nul(buffer, *lenp);
474 unsigned long long val;
477 return PTR_ERR(kbuf);
479 rc = kstrtoull(kbuf, 0, &val);
482 *(u64 *)table->data = val;
487 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
488 (unsigned long long)*(u64 *)table->data);
489 if (copy_to_user(buffer, kbuf, rc))
500 static int debugfs_dostring(struct ctl_table *table, int write,
501 void __user *buffer, size_t *lenp, loff_t *ppos)
504 char *kbuf = table->data;
510 if (len > table->maxlen)
513 if (copy_from_user(kbuf, buffer, len))
515 memset(kbuf+len, 0, table->maxlen - len);
518 len = strnlen(kbuf, len);
519 if (copy_to_user(buffer, kbuf, len))
522 if (copy_to_user(buffer+len, "\n", 1))
532 static int __proc_cpt_table(void *data, int write,
533 loff_t pos, void __user *buffer, int nob)
543 LIBCFS_ALLOC(buf, len);
547 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
552 LIBCFS_FREE(buf, len);
564 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
567 LIBCFS_FREE(buf, len);
571 static int proc_cpt_table(struct ctl_table *table, int write,
572 void __user *buffer, size_t *lenp, loff_t *ppos)
574 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
578 static int __proc_cpt_distance(void *data, int write,
579 loff_t pos, void __user *buffer, int nob)
589 LIBCFS_ALLOC(buf, len);
593 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
598 LIBCFS_FREE(buf, len);
610 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
613 LIBCFS_FREE(buf, len);
617 static int proc_cpt_distance(struct ctl_table *table, int write,
618 void __user *buffer, size_t *lenp, loff_t *ppos)
620 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
621 __proc_cpt_distance);
624 static struct ctl_table lnet_table[] = {
627 .data = &libcfs_debug,
628 .maxlen = sizeof(int),
630 .proc_handler = &proc_dobitmasks,
633 .procname = "subsystem_debug",
634 .data = &libcfs_subsystem_debug,
635 .maxlen = sizeof(int),
637 .proc_handler = &proc_dobitmasks,
640 .procname = "printk",
641 .data = &libcfs_printk,
642 .maxlen = sizeof(int),
644 .proc_handler = &proc_dobitmasks,
647 .procname = "cpu_partition_table",
650 .proc_handler = &proc_cpt_table,
653 .procname = "cpu_partition_distance",
656 .proc_handler = &proc_cpt_distance,
659 .procname = "debug_log_upcall",
660 .data = lnet_debug_log_upcall,
661 .maxlen = sizeof(lnet_debug_log_upcall),
663 .proc_handler = &debugfs_dostring,
666 .procname = "lnet_memused",
667 .data = (u64 *)&libcfs_kmem.counter,
668 .maxlen = sizeof(u64),
670 .proc_handler = &debugfs_dou64,
673 .procname = "catastrophe",
674 .data = &libcfs_catastrophe,
675 .maxlen = sizeof(int),
677 .proc_handler = &debugfs_doint,
680 .procname = "dump_kernel",
683 .proc_handler = &proc_dump_kernel,
686 .procname = "daemon_file",
689 .proc_handler = &proc_daemon_file,
692 .procname = "watchdog_ratelimit",
693 .data = &libcfs_watchdog_ratelimit,
694 .maxlen = sizeof(int),
696 .proc_handler = &debugfs_doint,
697 .extra1 = &min_watchdog_ratelimit,
698 .extra2 = &max_watchdog_ratelimit,
701 .procname = "force_lbug",
705 .proc_handler = &libcfs_force_lbug
708 .procname = "fail_loc",
709 .data = &cfs_fail_loc,
710 .maxlen = sizeof(cfs_fail_loc),
712 .proc_handler = &proc_fail_loc
715 .procname = "fail_val",
716 .data = &cfs_fail_val,
717 .maxlen = sizeof(int),
719 .proc_handler = &debugfs_doint
722 .procname = "fail_err",
723 .data = &cfs_fail_err,
724 .maxlen = sizeof(cfs_fail_err),
726 .proc_handler = &debugfs_doint,
732 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
733 { .name = "console_ratelimit",
734 .target = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
735 { .name = "debug_path",
736 .target = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
737 { .name = "panic_on_lbug",
738 .target = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
739 { .name = "console_backoff",
740 .target = "../../../module/libcfs/parameters/libcfs_console_backoff" },
741 { .name = "debug_mb",
742 .target = "../../../module/libcfs/parameters/libcfs_debug_mb" },
743 { .name = "console_min_delay_centisecs",
744 .target = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
745 { .name = "console_max_delay_centisecs",
746 .target = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
750 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
751 size_t count, loff_t *ppos)
753 struct ctl_table *table = filp->private_data;
754 ssize_t rc = -EINVAL;
757 rc = table->proc_handler(table, 0, buf, &count, ppos);
765 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
766 size_t count, loff_t *ppos)
768 struct ctl_table *table = filp->private_data;
769 ssize_t rc = -EINVAL;
772 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
781 static const struct file_operations lnet_debugfs_file_operations_rw = {
783 .read = lnet_debugfs_read,
784 .write = lnet_debugfs_write,
785 .llseek = default_llseek,
788 static const struct file_operations lnet_debugfs_file_operations_ro = {
790 .read = lnet_debugfs_read,
791 .llseek = default_llseek,
794 static const struct file_operations lnet_debugfs_file_operations_wo = {
796 .write = lnet_debugfs_write,
797 .llseek = default_llseek,
800 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
802 if (!(mode & S_IWUGO))
803 return &lnet_debugfs_file_operations_ro;
805 if (!(mode & S_IRUGO))
806 return &lnet_debugfs_file_operations_wo;
808 return &lnet_debugfs_file_operations_rw;
811 void lnet_insert_debugfs(struct ctl_table *table)
813 if (!lnet_debugfs_root)
814 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
816 /* Even if we cannot create, just ignore it altogether) */
817 if (IS_ERR_OR_NULL(lnet_debugfs_root))
820 /* We don't save the dentry returned in next two calls, because
821 * we don't call debugfs_remove() but rather remove_recursive()
823 for (; table && table->procname; table++)
824 debugfs_create_file(table->procname, table->mode,
825 lnet_debugfs_root, table,
826 lnet_debugfs_fops_select(table->mode));
828 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
830 static void lnet_insert_debugfs_links(
831 const struct lnet_debugfs_symlink_def *symlinks)
833 for (; symlinks && symlinks->name; symlinks++)
834 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
838 void lnet_remove_debugfs(struct ctl_table *table)
840 for (; table && table->procname; table++) {
841 struct qstr dname = QSTR_INIT(table->procname,
842 strlen(table->procname));
843 struct dentry *dentry;
845 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
846 debugfs_remove(dentry);
849 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
851 static int __init libcfs_init(void)
857 init_libcfs_vfree_atomic();
859 rc = libcfs_debug_init(5 * 1024 * 1024);
861 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
871 rc = misc_register(&libcfs_dev);
873 CERROR("misc_register: error %d\n", rc);
877 rc = cfs_wi_startup();
879 CERROR("initialize workitem: error %d\n", rc);
880 goto cleanup_deregister;
883 cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
884 if (!cfs_rehash_wq) {
886 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
888 goto cleanup_deregister;
891 rc = cfs_crypto_register();
893 CERROR("cfs_crypto_regster: error %d\n", rc);
897 lnet_insert_debugfs(lnet_table);
898 if (!IS_ERR_OR_NULL(lnet_debugfs_root))
899 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
903 CERROR("llcrypt_init: error %d\n", rc);
907 CDEBUG (D_OTHER, "portals setup OK\n");
912 misc_deregister(&libcfs_dev);
916 libcfs_debug_cleanup();
920 static void __exit libcfs_exit(void)
924 /* Remove everthing */
925 debugfs_remove_recursive(lnet_debugfs_root);
926 lnet_debugfs_root = NULL;
928 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
934 destroy_workqueue(cfs_rehash_wq);
935 cfs_rehash_wq = NULL;
938 cfs_crypto_unregister();
941 misc_deregister(&libcfs_dev);
945 /* the below message is checked in test-framework.sh check_mem_leak() */
946 if (libcfs_kmem_read() != 0)
947 CERROR("Portals memory leaked: %lld bytes\n",
950 rc = libcfs_debug_cleanup();
952 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
954 exit_libcfs_vfree_atomic();
957 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
958 MODULE_DESCRIPTION("Lustre helper library");
959 MODULE_VERSION(LIBCFS_VERSION);
960 MODULE_LICENSE("GPL");
962 module_init(libcfs_init);
963 module_exit(libcfs_exit);