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);
278 struct libcfs_ioctl_handler *hand;
281 down_read(&ioctl_list_sem);
282 list_for_each_entry(hand, &ioctl_list, item) {
283 err = hand->handle_ioctl(cmd, data);
284 if (err != -EINVAL) {
286 err = libcfs_ioctl_popdata(arg,
287 data, sizeof(*data));
291 up_read(&ioctl_list_sem);
299 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
302 struct libcfs_ioctl_data *data;
305 LIBCFS_ALLOC_GFP(buf, 1024, GFP_KERNEL);
309 /* 'cmd' and permissions get checked in our arch-specific caller */
310 if (libcfs_ioctl_getdata(buf, buf + 800, arg)) {
311 CERROR("PORTALS ioctl: data error\n");
315 data = (struct libcfs_ioctl_data *)buf;
317 err = libcfs_ioctl_int(pfile, cmd, arg, data);
320 LIBCFS_FREE(buf, 1024);
324 struct cfs_psdev_ops libcfs_psdev_ops = {
326 libcfs_psdev_release,
332 static int proc_call_handler(void *data, int write, loff_t *ppos,
333 void __user *buffer, size_t *lenp,
334 int (*handler)(void *data, int write,
335 loff_t pos, void __user *buffer, int len))
337 int rc = handler(data, write, *ppos, buffer, *lenp);
351 static int __proc_dobitmasks(void *data, int write,
352 loff_t pos, void __user *buffer, int nob)
354 const int tmpstrlen = 512;
357 unsigned int *mask = data;
358 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
359 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
361 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
366 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
372 rc = cfs_trace_copyout_string(buffer, nob,
376 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
378 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
382 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
383 /* Always print LBUG/LASSERT to console, so keep this mask */
388 cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
392 static int proc_dobitmasks(struct ctl_table *table, int write,
393 void __user *buffer, size_t *lenp, loff_t *ppos)
395 return proc_call_handler(table->data, write, ppos, buffer, lenp,
399 static int __proc_dump_kernel(void *data, int write,
400 loff_t pos, void __user *buffer, int nob)
405 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
408 static int proc_dump_kernel(struct ctl_table *table, int write,
409 void __user *buffer, size_t *lenp, loff_t *ppos)
411 return proc_call_handler(table->data, write, ppos, buffer, lenp,
415 static int __proc_daemon_file(void *data, int write,
416 loff_t pos, void __user *buffer, int nob)
419 int len = strlen(cfs_tracefile);
424 return cfs_trace_copyout_string(buffer, nob,
425 cfs_tracefile + pos, "\n");
428 return cfs_trace_daemon_command_usrstr(buffer, nob);
431 static int proc_daemon_file(struct ctl_table *table, int write,
432 void __user *buffer, size_t *lenp, loff_t *ppos)
434 return proc_call_handler(table->data, write, ppos, buffer, lenp,
438 static int libcfs_force_lbug(struct ctl_table *table, int write,
440 size_t *lenp, loff_t *ppos)
447 static int proc_fail_loc(struct ctl_table *table, int write,
449 size_t *lenp, loff_t *ppos)
452 long old_fail_loc = cfs_fail_loc;
454 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
455 if (old_fail_loc != cfs_fail_loc)
456 wake_up(&cfs_race_waitq);
460 static int __proc_cpt_table(void *data, int write,
461 loff_t pos, void __user *buffer, int nob)
470 LASSERT(cfs_cpt_table != NULL);
473 LIBCFS_ALLOC(buf, len);
477 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
482 LIBCFS_FREE(buf, len);
494 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
497 LIBCFS_FREE(buf, len);
501 static int proc_cpt_table(struct ctl_table *table, int write,
502 void __user *buffer, size_t *lenp, loff_t *ppos)
504 return proc_call_handler(table->data, write, ppos, buffer, lenp,
508 static struct ctl_table lnet_table[] = {
510 * NB No .strategy entries have been provided since sysctl(8) prefers
511 * to go via /proc for portability.
515 .data = &libcfs_debug,
516 .maxlen = sizeof(int),
518 .proc_handler = &proc_dobitmasks,
521 .procname = "subsystem_debug",
522 .data = &libcfs_subsystem_debug,
523 .maxlen = sizeof(int),
525 .proc_handler = &proc_dobitmasks,
528 .procname = "printk",
529 .data = &libcfs_printk,
530 .maxlen = sizeof(int),
532 .proc_handler = &proc_dobitmasks,
535 .procname = "cpu_partition_table",
538 .proc_handler = &proc_cpt_table,
542 .procname = "upcall",
544 .maxlen = sizeof(lnet_upcall),
546 .proc_handler = &proc_dostring,
549 .procname = "debug_log_upcall",
550 .data = lnet_debug_log_upcall,
551 .maxlen = sizeof(lnet_debug_log_upcall),
553 .proc_handler = &proc_dostring,
556 .procname = "catastrophe",
557 .data = &libcfs_catastrophe,
558 .maxlen = sizeof(int),
560 .proc_handler = &proc_dointvec,
563 .procname = "dump_kernel",
566 .proc_handler = &proc_dump_kernel,
569 .procname = "daemon_file",
572 .proc_handler = &proc_daemon_file,
575 .procname = "force_lbug",
579 .proc_handler = &libcfs_force_lbug
582 .procname = "fail_loc",
583 .data = &cfs_fail_loc,
584 .maxlen = sizeof(cfs_fail_loc),
586 .proc_handler = &proc_fail_loc
589 .procname = "fail_val",
590 .data = &cfs_fail_val,
591 .maxlen = sizeof(int),
593 .proc_handler = &proc_dointvec
599 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
600 { "console_ratelimit",
601 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
603 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
605 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
606 { "libcfs_console_backoff",
607 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
609 "/sys/module/libcfs/parameters/libcfs_debug_mb"},
610 { "console_min_delay_centisecs",
611 "/sys/module/libcfs/parameters/libcfs_console_min_delay"},
612 { "console_max_delay_centisecs",
613 "/sys/module/libcfs/parameters/libcfs_console_max_delay"},
617 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
618 size_t count, loff_t *ppos)
620 struct ctl_table *table = filp->private_data;
623 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
630 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
631 size_t count, loff_t *ppos)
633 struct ctl_table *table = filp->private_data;
636 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
643 static const struct file_operations lnet_debugfs_file_operations = {
645 .read = lnet_debugfs_read,
646 .write = lnet_debugfs_write,
647 .llseek = default_llseek,
650 void lustre_insert_debugfs(struct ctl_table *table,
651 const struct lnet_debugfs_symlink_def *symlinks)
653 struct dentry *entry;
655 if (lnet_debugfs_root == NULL)
656 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
658 /* Even if we cannot create, just ignore it altogether) */
659 if (IS_ERR_OR_NULL(lnet_debugfs_root))
662 for (; table->procname; table++)
663 entry = debugfs_create_file(table->procname, table->mode,
664 lnet_debugfs_root, table,
665 &lnet_debugfs_file_operations);
667 for (; symlinks && symlinks->name; symlinks++)
668 entry = debugfs_create_symlink(symlinks->name,
673 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
675 static void lustre_remove_debugfs(void)
677 if (lnet_debugfs_root != NULL)
678 debugfs_remove_recursive(lnet_debugfs_root);
680 lnet_debugfs_root = NULL;
683 static int init_libcfs_module(void)
687 rc = libcfs_debug_init(5 * 1024 * 1024);
689 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
697 rc = misc_register(&libcfs_dev);
699 CERROR("misc_register: error %d\n", rc);
703 rc = cfs_wi_startup();
705 CERROR("initialize workitem: error %d\n", rc);
706 goto cleanup_deregister;
709 /* max to 4 threads, should be enough for rehash */
710 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
711 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
712 rc, &cfs_sched_rehash);
714 CERROR("Startup workitem scheduler: error: %d\n", rc);
715 goto cleanup_deregister;
718 rc = cfs_crypto_register();
720 CERROR("cfs_crypto_register: error %d\n", rc);
724 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
726 CDEBUG(D_OTHER, "portals setup OK\n");
731 misc_deregister(&libcfs_dev);
735 libcfs_debug_cleanup();
739 static void exit_libcfs_module(void)
743 lustre_remove_debugfs();
745 if (cfs_sched_rehash) {
746 cfs_wi_sched_destroy(cfs_sched_rehash);
747 cfs_sched_rehash = NULL;
750 cfs_crypto_unregister();
753 misc_deregister(&libcfs_dev);
757 rc = libcfs_debug_cleanup();
759 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
762 MODULE_VERSION("1.0.0");
764 module_init(init_libcfs_module);
765 module_exit(exit_libcfs_module);