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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
44 #include <linux/uio.h>
46 #include <linux/uaccess.h>
49 #include <linux/file.h>
50 #include <linux/list.h>
52 #include <linux/sysctl.h>
53 #include <linux/debugfs.h>
55 # define DEBUG_SUBSYSTEM S_LNET
57 #include "../../include/linux/libcfs/libcfs.h"
58 #include <asm/div64.h>
60 #include "../../include/linux/libcfs/libcfs_crypto.h"
61 #include "../../include/linux/lnet/lib-lnet.h"
62 #include "../../include/linux/lnet/lnet.h"
63 #include "tracefile.h"
65 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
66 MODULE_DESCRIPTION("Portals v3.1");
67 MODULE_LICENSE("GPL");
69 extern struct miscdevice libcfs_dev;
70 extern struct cfs_wi_sched *cfs_sched_rehash;
71 extern void libcfs_init_nidstrings(void);
73 static void insert_debugfs(void);
74 static void remove_debugfs(void);
76 static struct dentry *lnet_debugfs_root;
77 extern char lnet_upcall[1024];
79 * The path of debug log dump upcall script.
81 extern char lnet_debug_log_upcall[1024];
83 #define CTL_LNET (0x100)
86 PSDEV_DEBUG = 1, /* control debugging */
87 PSDEV_SUBSYSTEM_DEBUG, /* control debugging */
88 PSDEV_PRINTK, /* force all messages to console */
89 PSDEV_CONSOLE_RATELIMIT, /* ratelimit console messages */
90 PSDEV_CONSOLE_MAX_DELAY_CS, /* maximum delay over which we skip messages */
91 PSDEV_CONSOLE_MIN_DELAY_CS, /* initial delay over which we skip messages */
92 PSDEV_CONSOLE_BACKOFF, /* delay increase factor */
93 PSDEV_DEBUG_PATH, /* crashdump log location */
94 PSDEV_DEBUG_DUMP_PATH, /* crashdump tracelog location */
95 PSDEV_CPT_TABLE, /* information about cpu partitions */
96 PSDEV_LNET_UPCALL, /* User mode upcall script */
97 PSDEV_LNET_MEMUSED, /* bytes currently PORTAL_ALLOCated */
98 PSDEV_LNET_CATASTROPHE, /* if we have LBUGged or panic'd */
99 PSDEV_LNET_PANIC_ON_LBUG, /* flag to panic on LBUG */
100 PSDEV_LNET_DUMP_KERNEL, /* snapshot kernel debug buffer to file */
101 PSDEV_LNET_DAEMON_FILE, /* spool kernel debug buffer to file */
102 PSDEV_LNET_DEBUG_MB, /* size of debug buffer */
103 PSDEV_LNET_DEBUG_LOG_UPCALL, /* debug log upcall script */
104 PSDEV_LNET_WATCHDOG_RATELIMIT, /* ratelimit watchdog messages */
105 PSDEV_LNET_FORCE_LBUG, /* hook to force an LBUG */
106 PSDEV_LNET_FAIL_LOC, /* control test failures instrumentation */
107 PSDEV_LNET_FAIL_VAL, /* userdata for fail loc */
110 static void kportal_memhog_free (struct libcfs_device_userstate *ldu)
112 struct page **level0p = &ldu->ldu_memhog_root_page;
113 struct page **level1p;
114 struct page **level2p;
118 if (*level0p != NULL) {
120 level1p = (struct page **)page_address(*level0p);
123 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
126 level2p = (struct page **)page_address(*level1p);
129 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
132 __free_page(*level2p);
133 ldu->ldu_memhog_pages--;
138 __free_page(*level1p);
139 ldu->ldu_memhog_pages--;
144 __free_page(*level0p);
145 ldu->ldu_memhog_pages--;
150 LASSERT (ldu->ldu_memhog_pages == 0);
153 static int kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
156 struct page **level0p;
157 struct page **level1p;
158 struct page **level2p;
162 LASSERT (ldu->ldu_memhog_pages == 0);
163 LASSERT (ldu->ldu_memhog_root_page == NULL);
171 level0p = &ldu->ldu_memhog_root_page;
172 *level0p = alloc_page(flags);
173 if (*level0p == NULL)
175 ldu->ldu_memhog_pages++;
177 level1p = (struct page **)page_address(*level0p);
179 memset(level1p, 0, PAGE_CACHE_SIZE);
181 while (ldu->ldu_memhog_pages < npages &&
182 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
184 if (cfs_signal_pending())
187 *level1p = alloc_page(flags);
188 if (*level1p == NULL)
190 ldu->ldu_memhog_pages++;
192 level2p = (struct page **)page_address(*level1p);
194 memset(level2p, 0, PAGE_CACHE_SIZE);
196 while (ldu->ldu_memhog_pages < npages &&
197 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
199 if (cfs_signal_pending())
202 *level2p = alloc_page(flags);
203 if (*level2p == NULL)
205 ldu->ldu_memhog_pages++;
218 /* called when opening /dev/device */
219 static int libcfs_psdev_open(unsigned long flags, void *args)
221 struct libcfs_device_userstate *ldu;
223 try_module_get(THIS_MODULE);
225 LIBCFS_ALLOC(ldu, sizeof(*ldu));
227 ldu->ldu_memhog_pages = 0;
228 ldu->ldu_memhog_root_page = NULL;
230 *(struct libcfs_device_userstate **)args = ldu;
235 /* called when closing /dev/device */
236 static int libcfs_psdev_release(unsigned long flags, void *args)
238 struct libcfs_device_userstate *ldu;
240 ldu = (struct libcfs_device_userstate *)args;
242 kportal_memhog_free(ldu);
243 LIBCFS_FREE(ldu, sizeof(*ldu));
246 module_put(THIS_MODULE);
250 static DECLARE_RWSEM(ioctl_list_sem);
251 static LIST_HEAD(ioctl_list);
253 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
257 down_write(&ioctl_list_sem);
258 if (!list_empty(&hand->item))
261 list_add_tail(&hand->item, &ioctl_list);
262 up_write(&ioctl_list_sem);
266 EXPORT_SYMBOL(libcfs_register_ioctl);
268 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
272 down_write(&ioctl_list_sem);
273 if (list_empty(&hand->item))
276 list_del_init(&hand->item);
277 up_write(&ioctl_list_sem);
281 EXPORT_SYMBOL(libcfs_deregister_ioctl);
283 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile, unsigned long cmd,
284 void *arg, struct libcfs_ioctl_data *data)
289 case IOC_LIBCFS_CLEAR_DEBUG:
290 libcfs_debug_clear_buffer();
293 * case IOC_LIBCFS_PANIC:
294 * Handled in arch/cfs_module.c
296 case IOC_LIBCFS_MARK_DEBUG:
297 if (data->ioc_inlbuf1 == NULL ||
298 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
300 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
302 case IOC_LIBCFS_MEMHOG:
303 if (pfile->private_data == NULL) {
306 kportal_memhog_free(pfile->private_data);
307 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
308 err = kportal_memhog_alloc(pfile->private_data,
312 kportal_memhog_free(pfile->private_data);
316 case IOC_LIBCFS_PING_TEST: {
317 extern void (kping_client)(struct libcfs_ioctl_data *);
318 void (*ping)(struct libcfs_ioctl_data *);
320 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
321 data->ioc_count, libcfs_nid2str(data->ioc_nid),
322 libcfs_nid2str(data->ioc_nid));
323 ping = symbol_get(kping_client);
325 CERROR("symbol_get failed\n");
328 symbol_put(kping_client);
334 struct libcfs_ioctl_handler *hand;
336 down_read(&ioctl_list_sem);
337 list_for_each_entry(hand, &ioctl_list, item) {
338 err = hand->handle_ioctl(cmd, data);
339 if (err != -EINVAL) {
341 err = libcfs_ioctl_popdata(arg,
342 data, sizeof (*data));
346 up_read(&ioctl_list_sem);
354 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
357 struct libcfs_ioctl_data *data;
360 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
364 /* 'cmd' and permissions get checked in our arch-specific caller */
365 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
366 CERROR("PORTALS ioctl: data error\n");
370 data = (struct libcfs_ioctl_data *)buf;
372 err = libcfs_ioctl_int(pfile, cmd, arg, data);
375 LIBCFS_FREE(buf, 1024);
380 struct cfs_psdev_ops libcfs_psdev_ops = {
382 libcfs_psdev_release,
388 static int init_libcfs_module(void)
393 libcfs_init_nidstrings();
395 rc = libcfs_debug_init(5 * 1024 * 1024);
397 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
405 rc = misc_register(&libcfs_dev);
407 CERROR("misc_register: error %d\n", rc);
411 rc = cfs_wi_startup();
413 CERROR("initialize workitem: error %d\n", rc);
414 goto cleanup_deregister;
417 /* max to 4 threads, should be enough for rehash */
418 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
419 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
420 rc, &cfs_sched_rehash);
422 CERROR("Startup workitem scheduler: error: %d\n", rc);
423 goto cleanup_deregister;
426 rc = cfs_crypto_register();
428 CERROR("cfs_crypto_register: error %d\n", rc);
434 CDEBUG (D_OTHER, "portals setup OK\n");
439 misc_deregister(&libcfs_dev);
443 libcfs_debug_cleanup();
447 static void exit_libcfs_module(void)
453 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
454 atomic_read(&libcfs_kmemory));
456 if (cfs_sched_rehash != NULL) {
457 cfs_wi_sched_destroy(cfs_sched_rehash);
458 cfs_sched_rehash = NULL;
461 cfs_crypto_unregister();
464 rc = misc_deregister(&libcfs_dev);
466 CERROR("misc_deregister error %d\n", rc);
470 if (atomic_read(&libcfs_kmemory) != 0)
471 CERROR("Portals memory leaked: %d bytes\n",
472 atomic_read(&libcfs_kmemory));
474 rc = libcfs_debug_cleanup();
476 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
478 libcfs_arch_cleanup();
481 static int proc_call_handler(void *data, int write, loff_t *ppos,
482 void __user *buffer, size_t *lenp,
483 int (*handler)(void *data, int write,
484 loff_t pos, void __user *buffer, int len))
486 int rc = handler(data, write, *ppos, buffer, *lenp);
500 static int __proc_dobitmasks(void *data, int write,
501 loff_t pos, void __user *buffer, int nob)
503 const int tmpstrlen = 512;
506 unsigned int *mask = data;
507 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
508 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
510 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
515 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
521 rc = cfs_trace_copyout_string(buffer, nob,
525 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
527 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
531 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
532 /* Always print LBUG/LASSERT to console, so keep this mask */
537 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
541 static int proc_dobitmasks(struct ctl_table *table, int write,
542 void __user *buffer, size_t *lenp, loff_t *ppos)
544 return proc_call_handler(table->data, write, ppos, buffer, lenp,
548 static int min_watchdog_ratelimit; /* disable ratelimiting */
549 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
551 static int __proc_dump_kernel(void *data, int write,
552 loff_t pos, void __user *buffer, int nob)
557 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
560 static int proc_dump_kernel(struct ctl_table *table, int write,
561 void __user *buffer, size_t *lenp, loff_t *ppos)
563 return proc_call_handler(table->data, write, ppos, buffer, lenp,
567 static int __proc_daemon_file(void *data, int write,
568 loff_t pos, void __user *buffer, int nob)
571 int len = strlen(cfs_tracefile);
576 return cfs_trace_copyout_string(buffer, nob,
577 cfs_tracefile + pos, "\n");
580 return cfs_trace_daemon_command_usrstr(buffer, nob);
583 static int proc_daemon_file(struct ctl_table *table, int write,
584 void __user *buffer, size_t *lenp, loff_t *ppos)
586 return proc_call_handler(table->data, write, ppos, buffer, lenp,
590 static int __proc_debug_mb(void *data, int write,
591 loff_t pos, void __user *buffer, int nob)
595 int len = snprintf(tmpstr, sizeof(tmpstr), "%d",
596 cfs_trace_get_debug_mb());
601 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
605 return cfs_trace_set_debug_mb_usrstr(buffer, nob);
608 static int proc_debug_mb(struct ctl_table *table, int write,
609 void __user *buffer, size_t *lenp, loff_t *ppos)
611 return proc_call_handler(table->data, write, ppos, buffer, lenp,
615 static int proc_console_max_delay_cs(struct ctl_table *table, int write,
616 void __user *buffer, size_t *lenp,
619 int rc, max_delay_cs;
620 struct ctl_table dummy = *table;
623 dummy.data = &max_delay_cs;
624 dummy.proc_handler = &proc_dointvec;
626 if (!write) { /* read */
627 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
628 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
634 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
637 if (max_delay_cs <= 0)
640 d = cfs_time_seconds(max_delay_cs) / 100;
641 if (d == 0 || d < libcfs_console_min_delay)
643 libcfs_console_max_delay = d;
648 static int proc_console_min_delay_cs(struct ctl_table *table, int write,
649 void __user *buffer, size_t *lenp,
652 int rc, min_delay_cs;
653 struct ctl_table dummy = *table;
656 dummy.data = &min_delay_cs;
657 dummy.proc_handler = &proc_dointvec;
659 if (!write) { /* read */
660 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
661 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
667 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
670 if (min_delay_cs <= 0)
673 d = cfs_time_seconds(min_delay_cs) / 100;
674 if (d == 0 || d > libcfs_console_max_delay)
676 libcfs_console_min_delay = d;
681 static int proc_console_backoff(struct ctl_table *table, int write,
682 void __user *buffer, size_t *lenp, loff_t *ppos)
685 struct ctl_table dummy = *table;
687 dummy.data = &backoff;
688 dummy.proc_handler = &proc_dointvec;
690 if (!write) { /* read */
691 backoff = libcfs_console_backoff;
692 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
698 rc = proc_dointvec(&dummy, write, buffer, lenp, ppos);
704 libcfs_console_backoff = backoff;
709 static int libcfs_force_lbug(struct ctl_table *table, int write,
711 size_t *lenp, loff_t *ppos)
718 static int proc_fail_loc(struct ctl_table *table, int write,
720 size_t *lenp, loff_t *ppos)
723 long old_fail_loc = cfs_fail_loc;
725 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
726 if (old_fail_loc != cfs_fail_loc)
727 wake_up(&cfs_race_waitq);
731 static int __proc_cpt_table(void *data, int write,
732 loff_t pos, void __user *buffer, int nob)
741 LASSERT(cfs_cpt_table != NULL);
744 LIBCFS_ALLOC(buf, len);
748 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
753 LIBCFS_FREE(buf, len);
765 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
768 LIBCFS_FREE(buf, len);
772 static int proc_cpt_table(struct ctl_table *table, int write,
773 void __user *buffer, size_t *lenp, loff_t *ppos)
775 return proc_call_handler(table->data, write, ppos, buffer, lenp,
779 static struct ctl_table lnet_table[] = {
781 * NB No .strategy entries have been provided since sysctl(8) prefers
782 * to go via /proc for portability.
786 .data = &libcfs_debug,
787 .maxlen = sizeof(int),
789 .proc_handler = &proc_dobitmasks,
792 .procname = "subsystem_debug",
793 .data = &libcfs_subsystem_debug,
794 .maxlen = sizeof(int),
796 .proc_handler = &proc_dobitmasks,
799 .procname = "printk",
800 .data = &libcfs_printk,
801 .maxlen = sizeof(int),
803 .proc_handler = &proc_dobitmasks,
806 .procname = "console_max_delay_centisecs",
807 .maxlen = sizeof(int),
809 .proc_handler = &proc_console_max_delay_cs
812 .procname = "console_min_delay_centisecs",
813 .maxlen = sizeof(int),
815 .proc_handler = &proc_console_min_delay_cs
818 .procname = "console_backoff",
819 .maxlen = sizeof(int),
821 .proc_handler = &proc_console_backoff
825 .procname = "cpu_partition_table",
828 .proc_handler = &proc_cpt_table,
832 .procname = "upcall",
834 .maxlen = sizeof(lnet_upcall),
836 .proc_handler = &proc_dostring,
839 .procname = "debug_log_upcall",
840 .data = lnet_debug_log_upcall,
841 .maxlen = sizeof(lnet_debug_log_upcall),
843 .proc_handler = &proc_dostring,
846 .procname = "lnet_memused",
847 .data = (int *)&libcfs_kmemory.counter,
848 .maxlen = sizeof(int),
850 .proc_handler = &proc_dointvec,
853 .procname = "catastrophe",
854 .data = &libcfs_catastrophe,
855 .maxlen = sizeof(int),
857 .proc_handler = &proc_dointvec,
860 .procname = "dump_kernel",
863 .proc_handler = &proc_dump_kernel,
866 .procname = "daemon_file",
869 .proc_handler = &proc_daemon_file,
872 .procname = "debug_mb",
874 .proc_handler = &proc_debug_mb,
877 .procname = "watchdog_ratelimit",
878 .data = &libcfs_watchdog_ratelimit,
879 .maxlen = sizeof(int),
881 .proc_handler = &proc_dointvec_minmax,
882 .extra1 = &min_watchdog_ratelimit,
883 .extra2 = &max_watchdog_ratelimit,
886 .procname = "force_lbug",
890 .proc_handler = &libcfs_force_lbug
893 .procname = "fail_loc",
894 .data = &cfs_fail_loc,
895 .maxlen = sizeof(cfs_fail_loc),
897 .proc_handler = &proc_fail_loc
900 .procname = "fail_val",
901 .data = &cfs_fail_val,
902 .maxlen = sizeof(int),
904 .proc_handler = &proc_dointvec
910 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
911 size_t count, loff_t *ppos)
913 struct ctl_table *table = filp->private_data;
916 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
923 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
924 size_t count, loff_t *ppos)
926 struct ctl_table *table = filp->private_data;
929 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
936 static const struct file_operations lnet_debugfs_file_operations = {
938 .read = lnet_debugfs_read,
939 .write = lnet_debugfs_write,
940 .llseek = default_llseek,
943 static void insert_debugfs(void)
945 struct ctl_table *table;
946 struct dentry *entry;
948 if (lnet_debugfs_root == NULL)
949 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
951 /* Even if we cannot create, just ignore it altogether) */
952 if (IS_ERR_OR_NULL(lnet_debugfs_root))
955 for (table = lnet_table; table->procname; table++)
956 entry = debugfs_create_file(table->procname, table->mode,
957 lnet_debugfs_root, table,
958 &lnet_debugfs_file_operations);
961 static void remove_debugfs(void)
963 if (lnet_debugfs_root != NULL)
964 debugfs_remove_recursive(lnet_debugfs_root);
966 lnet_debugfs_root = NULL;
969 MODULE_VERSION("1.0.0");
971 module_init(init_libcfs_module);
972 module_exit(exit_libcfs_module);