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 static void insert_debugfs(void);
70 static void remove_debugfs(void);
72 static struct dentry
*lnet_debugfs_root
;
74 static void kportal_memhog_free(struct libcfs_device_userstate
*ldu
)
76 struct page
**level0p
= &ldu
->ldu_memhog_root_page
;
77 struct page
**level1p
;
78 struct page
**level2p
;
82 if (*level0p
!= NULL
) {
84 level1p
= (struct page
**)page_address(*level0p
);
87 while (count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
90 level2p
= (struct page
**)page_address(*level1p
);
93 while (count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
96 __free_page(*level2p
);
97 ldu
->ldu_memhog_pages
--;
102 __free_page(*level1p
);
103 ldu
->ldu_memhog_pages
--;
108 __free_page(*level0p
);
109 ldu
->ldu_memhog_pages
--;
114 LASSERT(ldu
->ldu_memhog_pages
== 0);
117 static int kportal_memhog_alloc(struct libcfs_device_userstate
*ldu
, int npages
,
120 struct page
**level0p
;
121 struct page
**level1p
;
122 struct page
**level2p
;
126 LASSERT(ldu
->ldu_memhog_pages
== 0);
127 LASSERT(ldu
->ldu_memhog_root_page
== NULL
);
135 level0p
= &ldu
->ldu_memhog_root_page
;
136 *level0p
= alloc_page(flags
);
137 if (*level0p
== NULL
)
139 ldu
->ldu_memhog_pages
++;
141 level1p
= (struct page
**)page_address(*level0p
);
143 memset(level1p
, 0, PAGE_CACHE_SIZE
);
145 while (ldu
->ldu_memhog_pages
< npages
&&
146 count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
148 if (cfs_signal_pending())
151 *level1p
= alloc_page(flags
);
152 if (*level1p
== NULL
)
154 ldu
->ldu_memhog_pages
++;
156 level2p
= (struct page
**)page_address(*level1p
);
158 memset(level2p
, 0, PAGE_CACHE_SIZE
);
160 while (ldu
->ldu_memhog_pages
< npages
&&
161 count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
163 if (cfs_signal_pending())
166 *level2p
= alloc_page(flags
);
167 if (*level2p
== NULL
)
169 ldu
->ldu_memhog_pages
++;
182 /* called when opening /dev/device */
183 static int libcfs_psdev_open(unsigned long flags
, void *args
)
185 struct libcfs_device_userstate
*ldu
;
187 try_module_get(THIS_MODULE
);
189 LIBCFS_ALLOC(ldu
, sizeof(*ldu
));
191 ldu
->ldu_memhog_pages
= 0;
192 ldu
->ldu_memhog_root_page
= NULL
;
194 *(struct libcfs_device_userstate
**)args
= ldu
;
199 /* called when closing /dev/device */
200 static int libcfs_psdev_release(unsigned long flags
, void *args
)
202 struct libcfs_device_userstate
*ldu
;
204 ldu
= (struct libcfs_device_userstate
*)args
;
206 kportal_memhog_free(ldu
);
207 LIBCFS_FREE(ldu
, sizeof(*ldu
));
210 module_put(THIS_MODULE
);
214 static DECLARE_RWSEM(ioctl_list_sem
);
215 static LIST_HEAD(ioctl_list
);
217 int libcfs_register_ioctl(struct libcfs_ioctl_handler
*hand
)
221 down_write(&ioctl_list_sem
);
222 if (!list_empty(&hand
->item
))
225 list_add_tail(&hand
->item
, &ioctl_list
);
226 up_write(&ioctl_list_sem
);
230 EXPORT_SYMBOL(libcfs_register_ioctl
);
232 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler
*hand
)
236 down_write(&ioctl_list_sem
);
237 if (list_empty(&hand
->item
))
240 list_del_init(&hand
->item
);
241 up_write(&ioctl_list_sem
);
245 EXPORT_SYMBOL(libcfs_deregister_ioctl
);
247 static int libcfs_ioctl_int(struct cfs_psdev_file
*pfile
, unsigned long cmd
,
248 void *arg
, struct libcfs_ioctl_data
*data
)
253 case IOC_LIBCFS_CLEAR_DEBUG
:
254 libcfs_debug_clear_buffer();
257 * case IOC_LIBCFS_PANIC:
258 * Handled in arch/cfs_module.c
260 case IOC_LIBCFS_MARK_DEBUG
:
261 if (data
->ioc_inlbuf1
== NULL
||
262 data
->ioc_inlbuf1
[data
->ioc_inllen1
- 1] != '\0')
264 libcfs_debug_mark_buffer(data
->ioc_inlbuf1
);
266 case IOC_LIBCFS_MEMHOG
:
267 if (pfile
->private_data
== NULL
) {
270 kportal_memhog_free(pfile
->private_data
);
271 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
272 err
= kportal_memhog_alloc(pfile
->private_data
,
276 kportal_memhog_free(pfile
->private_data
);
280 case IOC_LIBCFS_PING_TEST
: {
281 extern void (kping_client
)(struct libcfs_ioctl_data
*);
282 void (*ping
)(struct libcfs_ioctl_data
*);
284 CDEBUG(D_IOCTL
, "doing %d pings to nid %s (%s)\n",
285 data
->ioc_count
, libcfs_nid2str(data
->ioc_nid
),
286 libcfs_nid2str(data
->ioc_nid
));
287 ping
= symbol_get(kping_client
);
289 CERROR("symbol_get failed\n");
292 symbol_put(kping_client
);
298 struct libcfs_ioctl_handler
*hand
;
300 down_read(&ioctl_list_sem
);
301 list_for_each_entry(hand
, &ioctl_list
, item
) {
302 err
= hand
->handle_ioctl(cmd
, data
);
303 if (err
!= -EINVAL
) {
305 err
= libcfs_ioctl_popdata(arg
,
306 data
, sizeof(*data
));
310 up_read(&ioctl_list_sem
);
318 static int libcfs_ioctl(struct cfs_psdev_file
*pfile
, unsigned long cmd
, void *arg
)
321 struct libcfs_ioctl_data
*data
;
324 LIBCFS_ALLOC_GFP(buf
, 1024, GFP_IOFS
);
328 /* 'cmd' and permissions get checked in our arch-specific caller */
329 if (libcfs_ioctl_getdata(buf
, buf
+ 800, arg
)) {
330 CERROR("PORTALS ioctl: data error\n");
334 data
= (struct libcfs_ioctl_data
*)buf
;
336 err
= libcfs_ioctl_int(pfile
, cmd
, arg
, data
);
339 LIBCFS_FREE(buf
, 1024);
344 struct cfs_psdev_ops libcfs_psdev_ops
= {
346 libcfs_psdev_release
,
352 static int init_libcfs_module(void)
357 libcfs_init_nidstrings();
359 rc
= libcfs_debug_init(5 * 1024 * 1024);
361 pr_err("LustreError: libcfs_debug_init: %d\n", rc
);
369 rc
= misc_register(&libcfs_dev
);
371 CERROR("misc_register: error %d\n", rc
);
375 rc
= cfs_wi_startup();
377 CERROR("initialize workitem: error %d\n", rc
);
378 goto cleanup_deregister
;
381 /* max to 4 threads, should be enough for rehash */
382 rc
= min(cfs_cpt_weight(cfs_cpt_table
, CFS_CPT_ANY
), 4);
383 rc
= cfs_wi_sched_create("cfs_rh", cfs_cpt_table
, CFS_CPT_ANY
,
384 rc
, &cfs_sched_rehash
);
386 CERROR("Startup workitem scheduler: error: %d\n", rc
);
387 goto cleanup_deregister
;
390 rc
= cfs_crypto_register();
392 CERROR("cfs_crypto_register: error %d\n", rc
);
398 CDEBUG(D_OTHER
, "portals setup OK\n");
403 misc_deregister(&libcfs_dev
);
407 libcfs_debug_cleanup();
411 static void exit_libcfs_module(void)
417 if (cfs_sched_rehash
!= NULL
) {
418 cfs_wi_sched_destroy(cfs_sched_rehash
);
419 cfs_sched_rehash
= NULL
;
422 cfs_crypto_unregister();
425 misc_deregister(&libcfs_dev
);
429 rc
= libcfs_debug_cleanup();
431 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc
);
433 libcfs_arch_cleanup();
436 static int proc_call_handler(void *data
, int write
, loff_t
*ppos
,
437 void __user
*buffer
, size_t *lenp
,
438 int (*handler
)(void *data
, int write
,
439 loff_t pos
, void __user
*buffer
, int len
))
441 int rc
= handler(data
, write
, *ppos
, buffer
, *lenp
);
455 static int __proc_dobitmasks(void *data
, int write
,
456 loff_t pos
, void __user
*buffer
, int nob
)
458 const int tmpstrlen
= 512;
461 unsigned int *mask
= data
;
462 int is_subsys
= (mask
== &libcfs_subsystem_debug
) ? 1 : 0;
463 int is_printk
= (mask
== &libcfs_printk
) ? 1 : 0;
465 rc
= cfs_trace_allocate_string_buffer(&tmpstr
, tmpstrlen
);
470 libcfs_debug_mask2str(tmpstr
, tmpstrlen
, *mask
, is_subsys
);
476 rc
= cfs_trace_copyout_string(buffer
, nob
,
480 rc
= cfs_trace_copyin_string(tmpstr
, tmpstrlen
, buffer
, nob
);
482 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
486 rc
= libcfs_debug_str2mask(mask
, tmpstr
, is_subsys
);
487 /* Always print LBUG/LASSERT to console, so keep this mask */
492 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
496 static int proc_dobitmasks(struct ctl_table
*table
, int write
,
497 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
499 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
503 static int __proc_dump_kernel(void *data
, int write
,
504 loff_t pos
, void __user
*buffer
, int nob
)
509 return cfs_trace_dump_debug_buffer_usrstr(buffer
, nob
);
512 static int proc_dump_kernel(struct ctl_table
*table
, int write
,
513 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
515 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
519 static int __proc_daemon_file(void *data
, int write
,
520 loff_t pos
, void __user
*buffer
, int nob
)
523 int len
= strlen(cfs_tracefile
);
528 return cfs_trace_copyout_string(buffer
, nob
,
529 cfs_tracefile
+ pos
, "\n");
532 return cfs_trace_daemon_command_usrstr(buffer
, nob
);
535 static int proc_daemon_file(struct ctl_table
*table
, int write
,
536 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
538 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
542 static int libcfs_force_lbug(struct ctl_table
*table
, int write
,
544 size_t *lenp
, loff_t
*ppos
)
551 static int proc_fail_loc(struct ctl_table
*table
, int write
,
553 size_t *lenp
, loff_t
*ppos
)
556 long old_fail_loc
= cfs_fail_loc
;
558 rc
= proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
559 if (old_fail_loc
!= cfs_fail_loc
)
560 wake_up(&cfs_race_waitq
);
564 static int __proc_cpt_table(void *data
, int write
,
565 loff_t pos
, void __user
*buffer
, int nob
)
574 LASSERT(cfs_cpt_table
!= NULL
);
577 LIBCFS_ALLOC(buf
, len
);
581 rc
= cfs_cpt_table_print(cfs_cpt_table
, buf
, len
);
586 LIBCFS_FREE(buf
, len
);
598 rc
= cfs_trace_copyout_string(buffer
, nob
, buf
+ pos
, NULL
);
601 LIBCFS_FREE(buf
, len
);
605 static int proc_cpt_table(struct ctl_table
*table
, int write
,
606 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
608 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
612 static struct ctl_table lnet_table
[] = {
614 * NB No .strategy entries have been provided since sysctl(8) prefers
615 * to go via /proc for portability.
619 .data
= &libcfs_debug
,
620 .maxlen
= sizeof(int),
622 .proc_handler
= &proc_dobitmasks
,
625 .procname
= "subsystem_debug",
626 .data
= &libcfs_subsystem_debug
,
627 .maxlen
= sizeof(int),
629 .proc_handler
= &proc_dobitmasks
,
632 .procname
= "printk",
633 .data
= &libcfs_printk
,
634 .maxlen
= sizeof(int),
636 .proc_handler
= &proc_dobitmasks
,
639 .procname
= "cpu_partition_table",
642 .proc_handler
= &proc_cpt_table
,
646 .procname
= "upcall",
648 .maxlen
= sizeof(lnet_upcall
),
650 .proc_handler
= &proc_dostring
,
653 .procname
= "debug_log_upcall",
654 .data
= lnet_debug_log_upcall
,
655 .maxlen
= sizeof(lnet_debug_log_upcall
),
657 .proc_handler
= &proc_dostring
,
660 .procname
= "catastrophe",
661 .data
= &libcfs_catastrophe
,
662 .maxlen
= sizeof(int),
664 .proc_handler
= &proc_dointvec
,
667 .procname
= "dump_kernel",
670 .proc_handler
= &proc_dump_kernel
,
673 .procname
= "daemon_file",
676 .proc_handler
= &proc_daemon_file
,
679 .procname
= "force_lbug",
683 .proc_handler
= &libcfs_force_lbug
686 .procname
= "fail_loc",
687 .data
= &cfs_fail_loc
,
688 .maxlen
= sizeof(cfs_fail_loc
),
690 .proc_handler
= &proc_fail_loc
693 .procname
= "fail_val",
694 .data
= &cfs_fail_val
,
695 .maxlen
= sizeof(int),
697 .proc_handler
= &proc_dointvec
703 struct lnet_debugfs_symlink_def
{
708 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks
[] = {
709 { "console_ratelimit",
710 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
712 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
714 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
715 { "libcfs_console_backoff",
716 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
718 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
719 { "console_min_delay_centisecs",
720 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
721 { "console_max_delay_centisecs",
722 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
726 static ssize_t
lnet_debugfs_read(struct file
*filp
, char __user
*buf
,
727 size_t count
, loff_t
*ppos
)
729 struct ctl_table
*table
= filp
->private_data
;
732 error
= table
->proc_handler(table
, 0, (void __user
*)buf
, &count
, ppos
);
739 static ssize_t
lnet_debugfs_write(struct file
*filp
, const char __user
*buf
,
740 size_t count
, loff_t
*ppos
)
742 struct ctl_table
*table
= filp
->private_data
;
745 error
= table
->proc_handler(table
, 1, (void __user
*)buf
, &count
, ppos
);
752 static const struct file_operations lnet_debugfs_file_operations
= {
754 .read
= lnet_debugfs_read
,
755 .write
= lnet_debugfs_write
,
756 .llseek
= default_llseek
,
759 static void insert_debugfs(void)
761 struct ctl_table
*table
;
762 struct dentry
*entry
;
763 const struct lnet_debugfs_symlink_def
*symlinks
;
765 if (lnet_debugfs_root
== NULL
)
766 lnet_debugfs_root
= debugfs_create_dir("lnet", NULL
);
768 /* Even if we cannot create, just ignore it altogether) */
769 if (IS_ERR_OR_NULL(lnet_debugfs_root
))
772 for (table
= lnet_table
; table
->procname
; table
++)
773 entry
= debugfs_create_file(table
->procname
, table
->mode
,
774 lnet_debugfs_root
, table
,
775 &lnet_debugfs_file_operations
);
777 for (symlinks
= lnet_debugfs_symlinks
; symlinks
->name
; symlinks
++)
778 entry
= debugfs_create_symlink(symlinks
->name
,
784 static void remove_debugfs(void)
786 if (lnet_debugfs_root
!= NULL
)
787 debugfs_remove_recursive(lnet_debugfs_root
);
789 lnet_debugfs_root
= NULL
;
792 MODULE_VERSION("1.0.0");
794 module_init(init_libcfs_module
);
795 module_exit(exit_libcfs_module
);