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 static void kportal_memhog_free(struct libcfs_device_userstate *ldu)
85 struct page **level0p = &ldu->ldu_memhog_root_page;
86 struct page **level1p;
87 struct page **level2p;
91 if (*level0p != NULL) {
93 level1p = (struct page **)page_address(*level0p);
96 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
99 level2p = (struct page **)page_address(*level1p);
102 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
105 __free_page(*level2p);
106 ldu->ldu_memhog_pages--;
111 __free_page(*level1p);
112 ldu->ldu_memhog_pages--;
117 __free_page(*level0p);
118 ldu->ldu_memhog_pages--;
123 LASSERT(ldu->ldu_memhog_pages == 0);
126 static int kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
129 struct page **level0p;
130 struct page **level1p;
131 struct page **level2p;
135 LASSERT(ldu->ldu_memhog_pages == 0);
136 LASSERT(ldu->ldu_memhog_root_page == NULL);
144 level0p = &ldu->ldu_memhog_root_page;
145 *level0p = alloc_page(flags);
146 if (*level0p == NULL)
148 ldu->ldu_memhog_pages++;
150 level1p = (struct page **)page_address(*level0p);
152 memset(level1p, 0, PAGE_CACHE_SIZE);
154 while (ldu->ldu_memhog_pages < npages &&
155 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
157 if (cfs_signal_pending())
160 *level1p = alloc_page(flags);
161 if (*level1p == NULL)
163 ldu->ldu_memhog_pages++;
165 level2p = (struct page **)page_address(*level1p);
167 memset(level2p, 0, PAGE_CACHE_SIZE);
169 while (ldu->ldu_memhog_pages < npages &&
170 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
172 if (cfs_signal_pending())
175 *level2p = alloc_page(flags);
176 if (*level2p == NULL)
178 ldu->ldu_memhog_pages++;
191 /* called when opening /dev/device */
192 static int libcfs_psdev_open(unsigned long flags, void *args)
194 struct libcfs_device_userstate *ldu;
196 try_module_get(THIS_MODULE);
198 LIBCFS_ALLOC(ldu, sizeof(*ldu));
200 ldu->ldu_memhog_pages = 0;
201 ldu->ldu_memhog_root_page = NULL;
203 *(struct libcfs_device_userstate **)args = ldu;
208 /* called when closing /dev/device */
209 static int libcfs_psdev_release(unsigned long flags, void *args)
211 struct libcfs_device_userstate *ldu;
213 ldu = (struct libcfs_device_userstate *)args;
215 kportal_memhog_free(ldu);
216 LIBCFS_FREE(ldu, sizeof(*ldu));
219 module_put(THIS_MODULE);
223 static DECLARE_RWSEM(ioctl_list_sem);
224 static LIST_HEAD(ioctl_list);
226 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
230 down_write(&ioctl_list_sem);
231 if (!list_empty(&hand->item))
234 list_add_tail(&hand->item, &ioctl_list);
235 up_write(&ioctl_list_sem);
239 EXPORT_SYMBOL(libcfs_register_ioctl);
241 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
245 down_write(&ioctl_list_sem);
246 if (list_empty(&hand->item))
249 list_del_init(&hand->item);
250 up_write(&ioctl_list_sem);
254 EXPORT_SYMBOL(libcfs_deregister_ioctl);
256 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile, unsigned long cmd,
257 void *arg, struct libcfs_ioctl_data *data)
262 case IOC_LIBCFS_CLEAR_DEBUG:
263 libcfs_debug_clear_buffer();
266 * case IOC_LIBCFS_PANIC:
267 * Handled in arch/cfs_module.c
269 case IOC_LIBCFS_MARK_DEBUG:
270 if (data->ioc_inlbuf1 == NULL ||
271 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
273 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
275 case IOC_LIBCFS_MEMHOG:
276 if (pfile->private_data == NULL) {
279 kportal_memhog_free(pfile->private_data);
280 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
281 err = kportal_memhog_alloc(pfile->private_data,
285 kportal_memhog_free(pfile->private_data);
289 case IOC_LIBCFS_PING_TEST: {
290 extern void (kping_client)(struct libcfs_ioctl_data *);
291 void (*ping)(struct libcfs_ioctl_data *);
293 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
294 data->ioc_count, libcfs_nid2str(data->ioc_nid),
295 libcfs_nid2str(data->ioc_nid));
296 ping = symbol_get(kping_client);
298 CERROR("symbol_get failed\n");
301 symbol_put(kping_client);
307 struct libcfs_ioctl_handler *hand;
309 down_read(&ioctl_list_sem);
310 list_for_each_entry(hand, &ioctl_list, item) {
311 err = hand->handle_ioctl(cmd, data);
312 if (err != -EINVAL) {
314 err = libcfs_ioctl_popdata(arg,
315 data, sizeof(*data));
319 up_read(&ioctl_list_sem);
327 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
330 struct libcfs_ioctl_data *data;
333 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
337 /* 'cmd' and permissions get checked in our arch-specific caller */
338 if (libcfs_ioctl_getdata(buf, buf + 800, arg)) {
339 CERROR("PORTALS ioctl: data error\n");
343 data = (struct libcfs_ioctl_data *)buf;
345 err = libcfs_ioctl_int(pfile, cmd, arg, data);
348 LIBCFS_FREE(buf, 1024);
353 struct cfs_psdev_ops libcfs_psdev_ops = {
355 libcfs_psdev_release,
361 static int init_libcfs_module(void)
366 libcfs_init_nidstrings();
368 rc = libcfs_debug_init(5 * 1024 * 1024);
370 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
378 rc = misc_register(&libcfs_dev);
380 CERROR("misc_register: error %d\n", rc);
384 rc = cfs_wi_startup();
386 CERROR("initialize workitem: error %d\n", rc);
387 goto cleanup_deregister;
390 /* max to 4 threads, should be enough for rehash */
391 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
392 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
393 rc, &cfs_sched_rehash);
395 CERROR("Startup workitem scheduler: error: %d\n", rc);
396 goto cleanup_deregister;
399 rc = cfs_crypto_register();
401 CERROR("cfs_crypto_register: error %d\n", rc);
407 CDEBUG(D_OTHER, "portals setup OK\n");
412 misc_deregister(&libcfs_dev);
416 libcfs_debug_cleanup();
420 static void exit_libcfs_module(void)
426 if (cfs_sched_rehash != NULL) {
427 cfs_wi_sched_destroy(cfs_sched_rehash);
428 cfs_sched_rehash = NULL;
431 cfs_crypto_unregister();
434 rc = misc_deregister(&libcfs_dev);
436 CERROR("misc_deregister error %d\n", rc);
440 rc = libcfs_debug_cleanup();
442 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
444 libcfs_arch_cleanup();
447 static int proc_call_handler(void *data, int write, loff_t *ppos,
448 void __user *buffer, size_t *lenp,
449 int (*handler)(void *data, int write,
450 loff_t pos, void __user *buffer, int len))
452 int rc = handler(data, write, *ppos, buffer, *lenp);
466 static int __proc_dobitmasks(void *data, int write,
467 loff_t pos, void __user *buffer, int nob)
469 const int tmpstrlen = 512;
472 unsigned int *mask = data;
473 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
474 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
476 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
481 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
487 rc = cfs_trace_copyout_string(buffer, nob,
491 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
493 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
497 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
498 /* Always print LBUG/LASSERT to console, so keep this mask */
503 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
507 static int proc_dobitmasks(struct ctl_table *table, int write,
508 void __user *buffer, size_t *lenp, loff_t *ppos)
510 return proc_call_handler(table->data, write, ppos, buffer, lenp,
514 static int __proc_dump_kernel(void *data, int write,
515 loff_t pos, void __user *buffer, int nob)
520 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
523 static int proc_dump_kernel(struct ctl_table *table, int write,
524 void __user *buffer, size_t *lenp, loff_t *ppos)
526 return proc_call_handler(table->data, write, ppos, buffer, lenp,
530 static int __proc_daemon_file(void *data, int write,
531 loff_t pos, void __user *buffer, int nob)
534 int len = strlen(cfs_tracefile);
539 return cfs_trace_copyout_string(buffer, nob,
540 cfs_tracefile + pos, "\n");
543 return cfs_trace_daemon_command_usrstr(buffer, nob);
546 static int proc_daemon_file(struct ctl_table *table, int write,
547 void __user *buffer, size_t *lenp, loff_t *ppos)
549 return proc_call_handler(table->data, write, ppos, buffer, lenp,
553 static int libcfs_force_lbug(struct ctl_table *table, int write,
555 size_t *lenp, loff_t *ppos)
562 static int proc_fail_loc(struct ctl_table *table, int write,
564 size_t *lenp, loff_t *ppos)
567 long old_fail_loc = cfs_fail_loc;
569 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
570 if (old_fail_loc != cfs_fail_loc)
571 wake_up(&cfs_race_waitq);
575 static int __proc_cpt_table(void *data, int write,
576 loff_t pos, void __user *buffer, int nob)
585 LASSERT(cfs_cpt_table != NULL);
588 LIBCFS_ALLOC(buf, len);
592 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
597 LIBCFS_FREE(buf, len);
609 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
612 LIBCFS_FREE(buf, len);
616 static int proc_cpt_table(struct ctl_table *table, int write,
617 void __user *buffer, size_t *lenp, loff_t *ppos)
619 return proc_call_handler(table->data, write, ppos, buffer, lenp,
623 static struct ctl_table lnet_table[] = {
625 * NB No .strategy entries have been provided since sysctl(8) prefers
626 * to go via /proc for portability.
630 .data = &libcfs_debug,
631 .maxlen = sizeof(int),
633 .proc_handler = &proc_dobitmasks,
636 .procname = "subsystem_debug",
637 .data = &libcfs_subsystem_debug,
638 .maxlen = sizeof(int),
640 .proc_handler = &proc_dobitmasks,
643 .procname = "printk",
644 .data = &libcfs_printk,
645 .maxlen = sizeof(int),
647 .proc_handler = &proc_dobitmasks,
650 .procname = "cpu_partition_table",
653 .proc_handler = &proc_cpt_table,
657 .procname = "upcall",
659 .maxlen = sizeof(lnet_upcall),
661 .proc_handler = &proc_dostring,
664 .procname = "debug_log_upcall",
665 .data = lnet_debug_log_upcall,
666 .maxlen = sizeof(lnet_debug_log_upcall),
668 .proc_handler = &proc_dostring,
671 .procname = "catastrophe",
672 .data = &libcfs_catastrophe,
673 .maxlen = sizeof(int),
675 .proc_handler = &proc_dointvec,
678 .procname = "dump_kernel",
681 .proc_handler = &proc_dump_kernel,
684 .procname = "daemon_file",
687 .proc_handler = &proc_daemon_file,
690 .procname = "force_lbug",
694 .proc_handler = &libcfs_force_lbug
697 .procname = "fail_loc",
698 .data = &cfs_fail_loc,
699 .maxlen = sizeof(cfs_fail_loc),
701 .proc_handler = &proc_fail_loc
704 .procname = "fail_val",
705 .data = &cfs_fail_val,
706 .maxlen = sizeof(int),
708 .proc_handler = &proc_dointvec
714 struct lnet_debugfs_symlink_def {
719 struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
720 { "console_ratelimit",
721 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
723 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
725 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
726 { "libcfs_console_backoff",
727 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
729 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
730 { "console_min_delay_centisecs",
731 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
732 { "console_max_delay_centisecs",
733 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
737 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
738 size_t count, loff_t *ppos)
740 struct ctl_table *table = filp->private_data;
743 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
750 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
751 size_t count, loff_t *ppos)
753 struct ctl_table *table = filp->private_data;
756 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
763 static const struct file_operations lnet_debugfs_file_operations = {
765 .read = lnet_debugfs_read,
766 .write = lnet_debugfs_write,
767 .llseek = default_llseek,
770 static void insert_debugfs(void)
772 struct ctl_table *table;
773 struct dentry *entry;
774 struct lnet_debugfs_symlink_def *symlinks;
776 if (lnet_debugfs_root == NULL)
777 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
779 /* Even if we cannot create, just ignore it altogether) */
780 if (IS_ERR_OR_NULL(lnet_debugfs_root))
783 for (table = lnet_table; table->procname; table++)
784 entry = debugfs_create_file(table->procname, table->mode,
785 lnet_debugfs_root, table,
786 &lnet_debugfs_file_operations);
788 for (symlinks = lnet_debugfs_symlinks; symlinks->name; symlinks++)
789 entry = debugfs_create_symlink(symlinks->name,
795 static void remove_debugfs(void)
797 if (lnet_debugfs_root != NULL)
798 debugfs_remove_recursive(lnet_debugfs_root);
800 lnet_debugfs_root = NULL;
803 MODULE_VERSION("1.0.0");
805 module_init(init_libcfs_module);
806 module_exit(exit_libcfs_module);