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>
54 # define DEBUG_SUBSYSTEM S_LNET
56 #include "../../include/linux/libcfs/libcfs.h"
57 #include <asm/div64.h>
59 #include "../../include/linux/libcfs/libcfs_crypto.h"
60 #include "../../include/linux/lnet/lib-lnet.h"
61 #include "../../include/linux/lnet/lnet.h"
62 #include "tracefile.h"
64 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
65 MODULE_DESCRIPTION("Portals v3.1");
66 MODULE_LICENSE("GPL");
68 extern struct miscdevice libcfs_dev
;
69 extern struct cfs_wi_sched
*cfs_sched_rehash
;
70 extern void libcfs_init_nidstrings(void);
72 static int insert_proc(void);
73 static void remove_proc(void);
75 static struct ctl_table_header
*lnet_table_header
;
76 extern char lnet_upcall
[1024];
78 * The path of debug log dump upcall script.
80 extern char lnet_debug_log_upcall
[1024];
82 #define CTL_LNET (0x100)
85 PSDEV_DEBUG
= 1, /* control debugging */
86 PSDEV_SUBSYSTEM_DEBUG
, /* control debugging */
87 PSDEV_PRINTK
, /* force all messages to console */
88 PSDEV_CONSOLE_RATELIMIT
, /* ratelimit console messages */
89 PSDEV_CONSOLE_MAX_DELAY_CS
, /* maximum delay over which we skip messages */
90 PSDEV_CONSOLE_MIN_DELAY_CS
, /* initial delay over which we skip messages */
91 PSDEV_CONSOLE_BACKOFF
, /* delay increase factor */
92 PSDEV_DEBUG_PATH
, /* crashdump log location */
93 PSDEV_DEBUG_DUMP_PATH
, /* crashdump tracelog location */
94 PSDEV_CPT_TABLE
, /* information about cpu partitions */
95 PSDEV_LNET_UPCALL
, /* User mode upcall script */
96 PSDEV_LNET_MEMUSED
, /* bytes currently PORTAL_ALLOCated */
97 PSDEV_LNET_CATASTROPHE
, /* if we have LBUGged or panic'd */
98 PSDEV_LNET_PANIC_ON_LBUG
, /* flag to panic on LBUG */
99 PSDEV_LNET_DUMP_KERNEL
, /* snapshot kernel debug buffer to file */
100 PSDEV_LNET_DAEMON_FILE
, /* spool kernel debug buffer to file */
101 PSDEV_LNET_DEBUG_MB
, /* size of debug buffer */
102 PSDEV_LNET_DEBUG_LOG_UPCALL
, /* debug log upcall script */
103 PSDEV_LNET_WATCHDOG_RATELIMIT
, /* ratelimit watchdog messages */
104 PSDEV_LNET_FORCE_LBUG
, /* hook to force an LBUG */
105 PSDEV_LNET_FAIL_LOC
, /* control test failures instrumentation */
106 PSDEV_LNET_FAIL_VAL
, /* userdata for fail loc */
109 static void kportal_memhog_free (struct libcfs_device_userstate
*ldu
)
111 struct page
**level0p
= &ldu
->ldu_memhog_root_page
;
112 struct page
**level1p
;
113 struct page
**level2p
;
117 if (*level0p
!= NULL
) {
119 level1p
= (struct page
**)page_address(*level0p
);
122 while (count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
125 level2p
= (struct page
**)page_address(*level1p
);
128 while (count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*) &&
131 __free_page(*level2p
);
132 ldu
->ldu_memhog_pages
--;
137 __free_page(*level1p
);
138 ldu
->ldu_memhog_pages
--;
143 __free_page(*level0p
);
144 ldu
->ldu_memhog_pages
--;
149 LASSERT (ldu
->ldu_memhog_pages
== 0);
152 static int kportal_memhog_alloc(struct libcfs_device_userstate
*ldu
, int npages
,
155 struct page
**level0p
;
156 struct page
**level1p
;
157 struct page
**level2p
;
161 LASSERT (ldu
->ldu_memhog_pages
== 0);
162 LASSERT (ldu
->ldu_memhog_root_page
== NULL
);
170 level0p
= &ldu
->ldu_memhog_root_page
;
171 *level0p
= alloc_page(flags
);
172 if (*level0p
== NULL
)
174 ldu
->ldu_memhog_pages
++;
176 level1p
= (struct page
**)page_address(*level0p
);
178 memset(level1p
, 0, PAGE_CACHE_SIZE
);
180 while (ldu
->ldu_memhog_pages
< npages
&&
181 count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
183 if (cfs_signal_pending())
186 *level1p
= alloc_page(flags
);
187 if (*level1p
== NULL
)
189 ldu
->ldu_memhog_pages
++;
191 level2p
= (struct page
**)page_address(*level1p
);
193 memset(level2p
, 0, PAGE_CACHE_SIZE
);
195 while (ldu
->ldu_memhog_pages
< npages
&&
196 count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
198 if (cfs_signal_pending())
201 *level2p
= alloc_page(flags
);
202 if (*level2p
== NULL
)
204 ldu
->ldu_memhog_pages
++;
217 /* called when opening /dev/device */
218 static int libcfs_psdev_open(unsigned long flags
, void *args
)
220 struct libcfs_device_userstate
*ldu
;
222 try_module_get(THIS_MODULE
);
224 LIBCFS_ALLOC(ldu
, sizeof(*ldu
));
226 ldu
->ldu_memhog_pages
= 0;
227 ldu
->ldu_memhog_root_page
= NULL
;
229 *(struct libcfs_device_userstate
**)args
= ldu
;
234 /* called when closing /dev/device */
235 static int libcfs_psdev_release(unsigned long flags
, void *args
)
237 struct libcfs_device_userstate
*ldu
;
239 ldu
= (struct libcfs_device_userstate
*)args
;
241 kportal_memhog_free(ldu
);
242 LIBCFS_FREE(ldu
, sizeof(*ldu
));
245 module_put(THIS_MODULE
);
249 static DECLARE_RWSEM(ioctl_list_sem
);
250 static LIST_HEAD(ioctl_list
);
252 int libcfs_register_ioctl(struct libcfs_ioctl_handler
*hand
)
256 down_write(&ioctl_list_sem
);
257 if (!list_empty(&hand
->item
))
260 list_add_tail(&hand
->item
, &ioctl_list
);
261 up_write(&ioctl_list_sem
);
265 EXPORT_SYMBOL(libcfs_register_ioctl
);
267 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler
*hand
)
271 down_write(&ioctl_list_sem
);
272 if (list_empty(&hand
->item
))
275 list_del_init(&hand
->item
);
276 up_write(&ioctl_list_sem
);
280 EXPORT_SYMBOL(libcfs_deregister_ioctl
);
282 static int libcfs_ioctl_int(struct cfs_psdev_file
*pfile
, unsigned long cmd
,
283 void *arg
, struct libcfs_ioctl_data
*data
)
288 case IOC_LIBCFS_CLEAR_DEBUG
:
289 libcfs_debug_clear_buffer();
292 * case IOC_LIBCFS_PANIC:
293 * Handled in arch/cfs_module.c
295 case IOC_LIBCFS_MARK_DEBUG
:
296 if (data
->ioc_inlbuf1
== NULL
||
297 data
->ioc_inlbuf1
[data
->ioc_inllen1
- 1] != '\0')
299 libcfs_debug_mark_buffer(data
->ioc_inlbuf1
);
301 case IOC_LIBCFS_MEMHOG
:
302 if (pfile
->private_data
== NULL
) {
305 kportal_memhog_free(pfile
->private_data
);
306 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
307 err
= kportal_memhog_alloc(pfile
->private_data
,
311 kportal_memhog_free(pfile
->private_data
);
315 case IOC_LIBCFS_PING_TEST
: {
316 extern void (kping_client
)(struct libcfs_ioctl_data
*);
317 void (*ping
)(struct libcfs_ioctl_data
*);
319 CDEBUG(D_IOCTL
, "doing %d pings to nid %s (%s)\n",
320 data
->ioc_count
, libcfs_nid2str(data
->ioc_nid
),
321 libcfs_nid2str(data
->ioc_nid
));
322 ping
= symbol_get(kping_client
);
324 CERROR("symbol_get failed\n");
327 symbol_put(kping_client
);
333 struct libcfs_ioctl_handler
*hand
;
335 down_read(&ioctl_list_sem
);
336 list_for_each_entry(hand
, &ioctl_list
, item
) {
337 err
= hand
->handle_ioctl(cmd
, data
);
338 if (err
!= -EINVAL
) {
340 err
= libcfs_ioctl_popdata(arg
,
341 data
, sizeof (*data
));
345 up_read(&ioctl_list_sem
);
353 static int libcfs_ioctl(struct cfs_psdev_file
*pfile
, unsigned long cmd
, void *arg
)
356 struct libcfs_ioctl_data
*data
;
359 LIBCFS_ALLOC_GFP(buf
, 1024, GFP_IOFS
);
363 /* 'cmd' and permissions get checked in our arch-specific caller */
364 if (libcfs_ioctl_getdata(buf
, buf
+ 800, (void *)arg
)) {
365 CERROR("PORTALS ioctl: data error\n");
369 data
= (struct libcfs_ioctl_data
*)buf
;
371 err
= libcfs_ioctl_int(pfile
, cmd
, arg
, data
);
374 LIBCFS_FREE(buf
, 1024);
379 struct cfs_psdev_ops libcfs_psdev_ops
= {
381 libcfs_psdev_release
,
387 static int init_libcfs_module(void)
392 libcfs_init_nidstrings();
394 rc
= libcfs_debug_init(5 * 1024 * 1024);
396 pr_err("LustreError: libcfs_debug_init: %d\n", rc
);
404 rc
= misc_register(&libcfs_dev
);
406 CERROR("misc_register: error %d\n", rc
);
410 rc
= cfs_wi_startup();
412 CERROR("initialize workitem: error %d\n", rc
);
413 goto cleanup_deregister
;
416 /* max to 4 threads, should be enough for rehash */
417 rc
= min(cfs_cpt_weight(cfs_cpt_table
, CFS_CPT_ANY
), 4);
418 rc
= cfs_wi_sched_create("cfs_rh", cfs_cpt_table
, CFS_CPT_ANY
,
419 rc
, &cfs_sched_rehash
);
421 CERROR("Startup workitem scheduler: error: %d\n", rc
);
422 goto cleanup_deregister
;
425 rc
= cfs_crypto_register();
427 CERROR("cfs_crypto_register: error %d\n", rc
);
434 CERROR("insert_proc: error %d\n", rc
);
438 CDEBUG (D_OTHER
, "portals setup OK\n");
441 cfs_crypto_unregister();
445 misc_deregister(&libcfs_dev
);
449 libcfs_debug_cleanup();
453 static void exit_libcfs_module(void)
459 CDEBUG(D_MALLOC
, "before Portals cleanup: kmem %d\n",
460 atomic_read(&libcfs_kmemory
));
462 if (cfs_sched_rehash
!= NULL
) {
463 cfs_wi_sched_destroy(cfs_sched_rehash
);
464 cfs_sched_rehash
= NULL
;
467 cfs_crypto_unregister();
470 rc
= misc_deregister(&libcfs_dev
);
472 CERROR("misc_deregister error %d\n", rc
);
476 if (atomic_read(&libcfs_kmemory
) != 0)
477 CERROR("Portals memory leaked: %d bytes\n",
478 atomic_read(&libcfs_kmemory
));
480 rc
= libcfs_debug_cleanup();
482 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc
);
484 libcfs_arch_cleanup();
487 static int proc_call_handler(void *data
, int write
, loff_t
*ppos
,
488 void __user
*buffer
, size_t *lenp
,
489 int (*handler
)(void *data
, int write
,
490 loff_t pos
, void __user
*buffer
, int len
))
492 int rc
= handler(data
, write
, *ppos
, buffer
, *lenp
);
506 static int __proc_dobitmasks(void *data
, int write
,
507 loff_t pos
, void __user
*buffer
, int nob
)
509 const int tmpstrlen
= 512;
512 unsigned int *mask
= data
;
513 int is_subsys
= (mask
== &libcfs_subsystem_debug
) ? 1 : 0;
514 int is_printk
= (mask
== &libcfs_printk
) ? 1 : 0;
516 rc
= cfs_trace_allocate_string_buffer(&tmpstr
, tmpstrlen
);
521 libcfs_debug_mask2str(tmpstr
, tmpstrlen
, *mask
, is_subsys
);
527 rc
= cfs_trace_copyout_string(buffer
, nob
,
531 rc
= cfs_trace_copyin_string(tmpstr
, tmpstrlen
, buffer
, nob
);
533 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
537 rc
= libcfs_debug_str2mask(mask
, tmpstr
, is_subsys
);
538 /* Always print LBUG/LASSERT to console, so keep this mask */
543 cfs_trace_free_string_buffer(tmpstr
, tmpstrlen
);
547 static int proc_dobitmasks(struct ctl_table
*table
, int write
,
548 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
550 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
554 static int min_watchdog_ratelimit
; /* disable ratelimiting */
555 static int max_watchdog_ratelimit
= (24*60*60); /* limit to once per day */
557 static int __proc_dump_kernel(void *data
, int write
,
558 loff_t pos
, void __user
*buffer
, int nob
)
563 return cfs_trace_dump_debug_buffer_usrstr(buffer
, nob
);
566 static int proc_dump_kernel(struct ctl_table
*table
, int write
,
567 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
569 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
573 static int __proc_daemon_file(void *data
, int write
,
574 loff_t pos
, void __user
*buffer
, int nob
)
577 int len
= strlen(cfs_tracefile
);
582 return cfs_trace_copyout_string(buffer
, nob
,
583 cfs_tracefile
+ pos
, "\n");
586 return cfs_trace_daemon_command_usrstr(buffer
, nob
);
589 static int proc_daemon_file(struct ctl_table
*table
, int write
,
590 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
592 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
596 static int __proc_debug_mb(void *data
, int write
,
597 loff_t pos
, void __user
*buffer
, int nob
)
601 int len
= snprintf(tmpstr
, sizeof(tmpstr
), "%d",
602 cfs_trace_get_debug_mb());
607 return cfs_trace_copyout_string(buffer
, nob
, tmpstr
+ pos
,
611 return cfs_trace_set_debug_mb_usrstr(buffer
, nob
);
614 static int proc_debug_mb(struct ctl_table
*table
, int write
,
615 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
617 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
621 static int proc_console_max_delay_cs(struct ctl_table
*table
, int write
,
622 void __user
*buffer
, size_t *lenp
,
625 int rc
, max_delay_cs
;
626 struct ctl_table dummy
= *table
;
629 dummy
.data
= &max_delay_cs
;
630 dummy
.proc_handler
= &proc_dointvec
;
632 if (!write
) { /* read */
633 max_delay_cs
= cfs_duration_sec(libcfs_console_max_delay
* 100);
634 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
640 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
643 if (max_delay_cs
<= 0)
646 d
= cfs_time_seconds(max_delay_cs
) / 100;
647 if (d
== 0 || d
< libcfs_console_min_delay
)
649 libcfs_console_max_delay
= d
;
654 static int proc_console_min_delay_cs(struct ctl_table
*table
, int write
,
655 void __user
*buffer
, size_t *lenp
,
658 int rc
, min_delay_cs
;
659 struct ctl_table dummy
= *table
;
662 dummy
.data
= &min_delay_cs
;
663 dummy
.proc_handler
= &proc_dointvec
;
665 if (!write
) { /* read */
666 min_delay_cs
= cfs_duration_sec(libcfs_console_min_delay
* 100);
667 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
673 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
676 if (min_delay_cs
<= 0)
679 d
= cfs_time_seconds(min_delay_cs
) / 100;
680 if (d
== 0 || d
> libcfs_console_max_delay
)
682 libcfs_console_min_delay
= d
;
687 static int proc_console_backoff(struct ctl_table
*table
, int write
,
688 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
691 struct ctl_table dummy
= *table
;
693 dummy
.data
= &backoff
;
694 dummy
.proc_handler
= &proc_dointvec
;
696 if (!write
) { /* read */
697 backoff
= libcfs_console_backoff
;
698 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
704 rc
= proc_dointvec(&dummy
, write
, buffer
, lenp
, ppos
);
710 libcfs_console_backoff
= backoff
;
715 static int libcfs_force_lbug(struct ctl_table
*table
, int write
,
717 size_t *lenp
, loff_t
*ppos
)
724 static int proc_fail_loc(struct ctl_table
*table
, int write
,
726 size_t *lenp
, loff_t
*ppos
)
729 long old_fail_loc
= cfs_fail_loc
;
731 rc
= proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
732 if (old_fail_loc
!= cfs_fail_loc
)
733 wake_up(&cfs_race_waitq
);
737 static int __proc_cpt_table(void *data
, int write
,
738 loff_t pos
, void __user
*buffer
, int nob
)
747 LASSERT(cfs_cpt_table
!= NULL
);
750 LIBCFS_ALLOC(buf
, len
);
754 rc
= cfs_cpt_table_print(cfs_cpt_table
, buf
, len
);
759 LIBCFS_FREE(buf
, len
);
771 rc
= cfs_trace_copyout_string(buffer
, nob
, buf
+ pos
, NULL
);
774 LIBCFS_FREE(buf
, len
);
778 static int proc_cpt_table(struct ctl_table
*table
, int write
,
779 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
781 return proc_call_handler(table
->data
, write
, ppos
, buffer
, lenp
,
785 static struct ctl_table lnet_table
[] = {
787 * NB No .strategy entries have been provided since sysctl(8) prefers
788 * to go via /proc for portability.
792 .data
= &libcfs_debug
,
793 .maxlen
= sizeof(int),
795 .proc_handler
= &proc_dobitmasks
,
798 .procname
= "subsystem_debug",
799 .data
= &libcfs_subsystem_debug
,
800 .maxlen
= sizeof(int),
802 .proc_handler
= &proc_dobitmasks
,
805 .procname
= "printk",
806 .data
= &libcfs_printk
,
807 .maxlen
= sizeof(int),
809 .proc_handler
= &proc_dobitmasks
,
812 .procname
= "console_ratelimit",
813 .data
= &libcfs_console_ratelimit
,
814 .maxlen
= sizeof(int),
816 .proc_handler
= &proc_dointvec
819 .procname
= "console_max_delay_centisecs",
820 .maxlen
= sizeof(int),
822 .proc_handler
= &proc_console_max_delay_cs
825 .procname
= "console_min_delay_centisecs",
826 .maxlen
= sizeof(int),
828 .proc_handler
= &proc_console_min_delay_cs
831 .procname
= "console_backoff",
832 .maxlen
= sizeof(int),
834 .proc_handler
= &proc_console_backoff
838 .procname
= "debug_path",
839 .data
= libcfs_debug_file_path_arr
,
840 .maxlen
= sizeof(libcfs_debug_file_path_arr
),
842 .proc_handler
= &proc_dostring
,
846 .procname
= "cpu_partition_table",
849 .proc_handler
= &proc_cpt_table
,
853 .procname
= "upcall",
855 .maxlen
= sizeof(lnet_upcall
),
857 .proc_handler
= &proc_dostring
,
860 .procname
= "debug_log_upcall",
861 .data
= lnet_debug_log_upcall
,
862 .maxlen
= sizeof(lnet_debug_log_upcall
),
864 .proc_handler
= &proc_dostring
,
867 .procname
= "lnet_memused",
868 .data
= (int *)&libcfs_kmemory
.counter
,
869 .maxlen
= sizeof(int),
871 .proc_handler
= &proc_dointvec
,
874 .procname
= "catastrophe",
875 .data
= &libcfs_catastrophe
,
876 .maxlen
= sizeof(int),
878 .proc_handler
= &proc_dointvec
,
881 .procname
= "panic_on_lbug",
882 .data
= &libcfs_panic_on_lbug
,
883 .maxlen
= sizeof(int),
885 .proc_handler
= &proc_dointvec
,
888 .procname
= "dump_kernel",
891 .proc_handler
= &proc_dump_kernel
,
894 .procname
= "daemon_file",
897 .proc_handler
= &proc_daemon_file
,
900 .procname
= "debug_mb",
902 .proc_handler
= &proc_debug_mb
,
905 .procname
= "watchdog_ratelimit",
906 .data
= &libcfs_watchdog_ratelimit
,
907 .maxlen
= sizeof(int),
909 .proc_handler
= &proc_dointvec_minmax
,
910 .extra1
= &min_watchdog_ratelimit
,
911 .extra2
= &max_watchdog_ratelimit
,
914 .procname
= "force_lbug",
918 .proc_handler
= &libcfs_force_lbug
921 .procname
= "fail_loc",
922 .data
= &cfs_fail_loc
,
923 .maxlen
= sizeof(cfs_fail_loc
),
925 .proc_handler
= &proc_fail_loc
928 .procname
= "fail_val",
929 .data
= &cfs_fail_val
,
930 .maxlen
= sizeof(int),
932 .proc_handler
= &proc_dointvec
938 static struct ctl_table top_table
[] = {
950 static int insert_proc(void)
952 if (lnet_table_header
== NULL
)
953 lnet_table_header
= register_sysctl_table(top_table
);
957 static void remove_proc(void)
959 if (lnet_table_header
!= NULL
)
960 unregister_sysctl_table(lnet_table_header
);
962 lnet_table_header
= NULL
;
965 MODULE_VERSION("1.0.0");
967 module_init(init_libcfs_module
);
968 module_exit(exit_libcfs_module
);