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 struct dentry
*lnet_debugfs_root
;
71 static void kportal_memhog_free(struct libcfs_device_userstate
*ldu
)
73 struct page
**level0p
= &ldu
->ldu_memhog_root_page
;
74 struct page
**level1p
;
75 struct page
**level2p
;
79 if (*level0p
!= NULL
) {
81 level1p
= (struct page
**)page_address(*level0p
);
84 while (count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
87 level2p
= (struct page
**)page_address(*level1p
);
90 while (count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
93 __free_page(*level2p
);
94 ldu
->ldu_memhog_pages
--;
99 __free_page(*level1p
);
100 ldu
->ldu_memhog_pages
--;
105 __free_page(*level0p
);
106 ldu
->ldu_memhog_pages
--;
111 LASSERT(ldu
->ldu_memhog_pages
== 0);
114 static int kportal_memhog_alloc(struct libcfs_device_userstate
*ldu
, int npages
,
117 struct page
**level0p
;
118 struct page
**level1p
;
119 struct page
**level2p
;
123 LASSERT(ldu
->ldu_memhog_pages
== 0);
124 LASSERT(ldu
->ldu_memhog_root_page
== NULL
);
132 level0p
= &ldu
->ldu_memhog_root_page
;
133 *level0p
= alloc_page(flags
);
134 if (*level0p
== NULL
)
136 ldu
->ldu_memhog_pages
++;
138 level1p
= (struct page
**)page_address(*level0p
);
140 memset(level1p
, 0, PAGE_CACHE_SIZE
);
142 while (ldu
->ldu_memhog_pages
< npages
&&
143 count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
145 if (cfs_signal_pending())
148 *level1p
= alloc_page(flags
);
149 if (*level1p
== NULL
)
151 ldu
->ldu_memhog_pages
++;
153 level2p
= (struct page
**)page_address(*level1p
);
155 memset(level2p
, 0, PAGE_CACHE_SIZE
);
157 while (ldu
->ldu_memhog_pages
< npages
&&
158 count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
160 if (cfs_signal_pending())
163 *level2p
= alloc_page(flags
);
164 if (*level2p
== NULL
)
166 ldu
->ldu_memhog_pages
++;
179 /* called when opening /dev/device */
180 static int libcfs_psdev_open(unsigned long flags
, void *args
)
182 struct libcfs_device_userstate
*ldu
;
184 try_module_get(THIS_MODULE
);
186 LIBCFS_ALLOC(ldu
, sizeof(*ldu
));
188 ldu
->ldu_memhog_pages
= 0;
189 ldu
->ldu_memhog_root_page
= NULL
;
191 *(struct libcfs_device_userstate
**)args
= ldu
;
196 /* called when closing /dev/device */
197 static int libcfs_psdev_release(unsigned long flags
, void *args
)
199 struct libcfs_device_userstate
*ldu
;
201 ldu
= (struct libcfs_device_userstate
*)args
;
203 kportal_memhog_free(ldu
);
204 LIBCFS_FREE(ldu
, sizeof(*ldu
));
207 module_put(THIS_MODULE
);
211 static DECLARE_RWSEM(ioctl_list_sem
);
212 static LIST_HEAD(ioctl_list
);
214 int libcfs_register_ioctl(struct libcfs_ioctl_handler
*hand
)
218 down_write(&ioctl_list_sem
);
219 if (!list_empty(&hand
->item
))
222 list_add_tail(&hand
->item
, &ioctl_list
);
223 up_write(&ioctl_list_sem
);
227 EXPORT_SYMBOL(libcfs_register_ioctl
);
229 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler
*hand
)
233 down_write(&ioctl_list_sem
);
234 if (list_empty(&hand
->item
))
237 list_del_init(&hand
->item
);
238 up_write(&ioctl_list_sem
);
242 EXPORT_SYMBOL(libcfs_deregister_ioctl
);
244 static int libcfs_ioctl_int(struct cfs_psdev_file
*pfile
, unsigned long cmd
,
245 void *arg
, struct libcfs_ioctl_data
*data
)
250 case IOC_LIBCFS_CLEAR_DEBUG
:
251 libcfs_debug_clear_buffer();
254 * case IOC_LIBCFS_PANIC:
255 * Handled in arch/cfs_module.c
257 case IOC_LIBCFS_MARK_DEBUG
:
258 if (data
->ioc_inlbuf1
== NULL
||
259 data
->ioc_inlbuf1
[data
->ioc_inllen1
- 1] != '\0')
261 libcfs_debug_mark_buffer(data
->ioc_inlbuf1
);
263 case IOC_LIBCFS_MEMHOG
:
264 if (pfile
->private_data
== NULL
) {
267 kportal_memhog_free(pfile
->private_data
);
268 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
269 err
= kportal_memhog_alloc(pfile
->private_data
,
273 kportal_memhog_free(pfile
->private_data
);
277 case IOC_LIBCFS_PING_TEST
: {
278 extern void (kping_client
)(struct libcfs_ioctl_data
*);
279 void (*ping
)(struct libcfs_ioctl_data
*);
281 CDEBUG(D_IOCTL
, "doing %d pings to nid %s (%s)\n",
282 data
->ioc_count
, libcfs_nid2str(data
->ioc_nid
),
283 libcfs_nid2str(data
->ioc_nid
));
284 ping
= symbol_get(kping_client
);
286 CERROR("symbol_get failed\n");
289 symbol_put(kping_client
);
295 struct libcfs_ioctl_handler
*hand
;
298 down_read(&ioctl_list_sem
);
299 list_for_each_entry(hand
, &ioctl_list
, item
) {
300 err
= hand
->handle_ioctl(cmd
, data
);
301 if (err
!= -EINVAL
) {
303 err
= libcfs_ioctl_popdata(arg
,
304 data
, sizeof(*data
));
308 up_read(&ioctl_list_sem
);
316 static int libcfs_ioctl(struct cfs_psdev_file
*pfile
, unsigned long cmd
, void *arg
)
319 struct libcfs_ioctl_data
*data
;
322 LIBCFS_ALLOC_GFP(buf
, 1024, GFP_KERNEL
);
326 /* 'cmd' and permissions get checked in our arch-specific caller */
327 if (libcfs_ioctl_getdata(buf
, buf
+ 800, arg
)) {
328 CERROR("PORTALS ioctl: data error\n");
332 data
= (struct libcfs_ioctl_data
*)buf
;
334 err
= libcfs_ioctl_int(pfile
, cmd
, arg
, data
);
337 LIBCFS_FREE(buf
, 1024);
341 struct cfs_psdev_ops libcfs_psdev_ops
= {
343 libcfs_psdev_release
,
349 static int proc_call_handler(void *data
, int write
, loff_t
*ppos
,
350 void __user
*buffer
, size_t *lenp
,
351 int (*handler
)(void *data
, int write
,
352 loff_t pos
, void __user
*buffer
, int len
))
354 int rc
= handler(data
, write
, *ppos
, buffer
, *lenp
);
368 static int __proc_dobitmasks(void *data
, int write
,
369 loff_t pos
, void __user
*buffer
, int nob
)
371 const int tmpstrlen
= 512;
374 unsigned int *mask
= data
;
375 int is_subsys
= (mask
== &libcfs_subsystem_debug
) ? 1 : 0;
376 int is_printk
= (mask
== &libcfs_printk
) ? 1 : 0;
378 rc
= cfs_trace_allocate_string_buffer(&tmpstr
, tmpstrlen
);
383 libcfs_debug_mask2str(tmpstr
, tmpstrlen
, *mask
, is_subsys
);
389 rc
= cfs_trace_copyout_string(buffer
, nob
,
393 rc
= cfs_trace_copyin_string(tmpstr
, tmpstrlen
, buffer
, nob
);
395 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
399 rc
= libcfs_debug_str2mask(mask
, tmpstr
, is_subsys
);
400 /* Always print LBUG/LASSERT to console, so keep this mask */
405 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
409 static int proc_dobitmasks(struct ctl_table
*table
, int write
,
410 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
412 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
416 static int __proc_dump_kernel(void *data
, int write
,
417 loff_t pos
, void __user
*buffer
, int nob
)
422 return cfs_trace_dump_debug_buffer_usrstr(buffer
, nob
);
425 static int proc_dump_kernel(struct ctl_table
*table
, int write
,
426 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
428 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
432 static int __proc_daemon_file(void *data
, int write
,
433 loff_t pos
, void __user
*buffer
, int nob
)
436 int len
= strlen(cfs_tracefile
);
441 return cfs_trace_copyout_string(buffer
, nob
,
442 cfs_tracefile
+ pos
, "\n");
445 return cfs_trace_daemon_command_usrstr(buffer
, nob
);
448 static int proc_daemon_file(struct ctl_table
*table
, int write
,
449 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
451 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
455 static int libcfs_force_lbug(struct ctl_table
*table
, int write
,
457 size_t *lenp
, loff_t
*ppos
)
464 static int proc_fail_loc(struct ctl_table
*table
, int write
,
466 size_t *lenp
, loff_t
*ppos
)
469 long old_fail_loc
= cfs_fail_loc
;
471 rc
= proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
472 if (old_fail_loc
!= cfs_fail_loc
)
473 wake_up(&cfs_race_waitq
);
477 static int __proc_cpt_table(void *data
, int write
,
478 loff_t pos
, void __user
*buffer
, int nob
)
487 LASSERT(cfs_cpt_table
!= NULL
);
490 LIBCFS_ALLOC(buf
, len
);
494 rc
= cfs_cpt_table_print(cfs_cpt_table
, buf
, len
);
499 LIBCFS_FREE(buf
, len
);
511 rc
= cfs_trace_copyout_string(buffer
, nob
, buf
+ pos
, NULL
);
514 LIBCFS_FREE(buf
, len
);
518 static int proc_cpt_table(struct ctl_table
*table
, int write
,
519 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
521 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
525 static struct ctl_table lnet_table
[] = {
527 * NB No .strategy entries have been provided since sysctl(8) prefers
528 * to go via /proc for portability.
532 .data
= &libcfs_debug
,
533 .maxlen
= sizeof(int),
535 .proc_handler
= &proc_dobitmasks
,
538 .procname
= "subsystem_debug",
539 .data
= &libcfs_subsystem_debug
,
540 .maxlen
= sizeof(int),
542 .proc_handler
= &proc_dobitmasks
,
545 .procname
= "printk",
546 .data
= &libcfs_printk
,
547 .maxlen
= sizeof(int),
549 .proc_handler
= &proc_dobitmasks
,
552 .procname
= "cpu_partition_table",
555 .proc_handler
= &proc_cpt_table
,
559 .procname
= "upcall",
561 .maxlen
= sizeof(lnet_upcall
),
563 .proc_handler
= &proc_dostring
,
566 .procname
= "debug_log_upcall",
567 .data
= lnet_debug_log_upcall
,
568 .maxlen
= sizeof(lnet_debug_log_upcall
),
570 .proc_handler
= &proc_dostring
,
573 .procname
= "catastrophe",
574 .data
= &libcfs_catastrophe
,
575 .maxlen
= sizeof(int),
577 .proc_handler
= &proc_dointvec
,
580 .procname
= "dump_kernel",
583 .proc_handler
= &proc_dump_kernel
,
586 .procname
= "daemon_file",
589 .proc_handler
= &proc_daemon_file
,
592 .procname
= "force_lbug",
596 .proc_handler
= &libcfs_force_lbug
599 .procname
= "fail_loc",
600 .data
= &cfs_fail_loc
,
601 .maxlen
= sizeof(cfs_fail_loc
),
603 .proc_handler
= &proc_fail_loc
606 .procname
= "fail_val",
607 .data
= &cfs_fail_val
,
608 .maxlen
= sizeof(int),
610 .proc_handler
= &proc_dointvec
616 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks
[] = {
617 { "console_ratelimit",
618 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
620 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
622 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
623 { "libcfs_console_backoff",
624 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
626 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
627 { "console_min_delay_centisecs",
628 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
629 { "console_max_delay_centisecs",
630 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
634 static ssize_t
lnet_debugfs_read(struct file
*filp
, char __user
*buf
,
635 size_t count
, loff_t
*ppos
)
637 struct ctl_table
*table
= filp
->private_data
;
640 error
= table
->proc_handler(table
, 0, (void __user
*)buf
, &count
, ppos
);
647 static ssize_t
lnet_debugfs_write(struct file
*filp
, const char __user
*buf
,
648 size_t count
, loff_t
*ppos
)
650 struct ctl_table
*table
= filp
->private_data
;
653 error
= table
->proc_handler(table
, 1, (void __user
*)buf
, &count
, ppos
);
660 static const struct file_operations lnet_debugfs_file_operations
= {
662 .read
= lnet_debugfs_read
,
663 .write
= lnet_debugfs_write
,
664 .llseek
= default_llseek
,
667 void lustre_insert_debugfs(struct ctl_table
*table
,
668 const struct lnet_debugfs_symlink_def
*symlinks
)
670 struct dentry
*entry
;
672 if (lnet_debugfs_root
== NULL
)
673 lnet_debugfs_root
= debugfs_create_dir("lnet", NULL
);
675 /* Even if we cannot create, just ignore it altogether) */
676 if (IS_ERR_OR_NULL(lnet_debugfs_root
))
679 for (; table
->procname
; table
++)
680 entry
= debugfs_create_file(table
->procname
, table
->mode
,
681 lnet_debugfs_root
, table
,
682 &lnet_debugfs_file_operations
);
684 for (; symlinks
&& symlinks
->name
; symlinks
++)
685 entry
= debugfs_create_symlink(symlinks
->name
,
690 EXPORT_SYMBOL_GPL(lustre_insert_debugfs
);
692 static void lustre_remove_debugfs(void)
694 if (lnet_debugfs_root
!= NULL
)
695 debugfs_remove_recursive(lnet_debugfs_root
);
697 lnet_debugfs_root
= NULL
;
700 static int init_libcfs_module(void)
704 rc
= libcfs_debug_init(5 * 1024 * 1024);
706 pr_err("LustreError: libcfs_debug_init: %d\n", rc
);
714 rc
= misc_register(&libcfs_dev
);
716 CERROR("misc_register: error %d\n", rc
);
720 rc
= cfs_wi_startup();
722 CERROR("initialize workitem: error %d\n", rc
);
723 goto cleanup_deregister
;
726 /* max to 4 threads, should be enough for rehash */
727 rc
= min(cfs_cpt_weight(cfs_cpt_table
, CFS_CPT_ANY
), 4);
728 rc
= cfs_wi_sched_create("cfs_rh", cfs_cpt_table
, CFS_CPT_ANY
,
729 rc
, &cfs_sched_rehash
);
731 CERROR("Startup workitem scheduler: error: %d\n", rc
);
732 goto cleanup_deregister
;
735 rc
= cfs_crypto_register();
737 CERROR("cfs_crypto_register: error %d\n", rc
);
741 lustre_insert_debugfs(lnet_table
, lnet_debugfs_symlinks
);
743 CDEBUG(D_OTHER
, "portals setup OK\n");
748 misc_deregister(&libcfs_dev
);
752 libcfs_debug_cleanup();
756 static void exit_libcfs_module(void)
760 lustre_remove_debugfs();
762 if (cfs_sched_rehash
) {
763 cfs_wi_sched_destroy(cfs_sched_rehash
);
764 cfs_sched_rehash
= NULL
;
767 cfs_crypto_unregister();
770 misc_deregister(&libcfs_dev
);
774 rc
= libcfs_debug_cleanup();
776 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc
);
779 MODULE_VERSION("1.0.0");
781 module_init(init_libcfs_module
);
782 module_exit(exit_libcfs_module
);