]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/lustre/lustre/libcfs/module.c
Merge 4.4-rc5 into staging-next
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / lustre / lustre / libcfs / module.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 */
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <net/sock.h>
44 #include <linux/uio.h>
45
46 #include <linux/uaccess.h>
47
48 #include <linux/fs.h>
49 #include <linux/file.h>
50 #include <linux/list.h>
51
52 #include <linux/sysctl.h>
53 #include <linux/debugfs.h>
54
55 # define DEBUG_SUBSYSTEM S_LNET
56
57 #include "../../include/linux/libcfs/libcfs.h"
58 #include <asm/div64.h>
59
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"
64
65 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
66 MODULE_DESCRIPTION("Portals v3.1");
67 MODULE_LICENSE("GPL");
68
69 static struct dentry *lnet_debugfs_root;
70
71 static void kportal_memhog_free(struct libcfs_device_userstate *ldu)
72 {
73 struct page **level0p = &ldu->ldu_memhog_root_page;
74 struct page **level1p;
75 struct page **level2p;
76 int count1;
77 int count2;
78
79 if (*level0p != NULL) {
80
81 level1p = (struct page **)page_address(*level0p);
82 count1 = 0;
83
84 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
85 *level1p != NULL) {
86
87 level2p = (struct page **)page_address(*level1p);
88 count2 = 0;
89
90 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
91 *level2p != NULL) {
92
93 __free_page(*level2p);
94 ldu->ldu_memhog_pages--;
95 level2p++;
96 count2++;
97 }
98
99 __free_page(*level1p);
100 ldu->ldu_memhog_pages--;
101 level1p++;
102 count1++;
103 }
104
105 __free_page(*level0p);
106 ldu->ldu_memhog_pages--;
107
108 *level0p = NULL;
109 }
110
111 LASSERT(ldu->ldu_memhog_pages == 0);
112 }
113
114 static int kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
115 gfp_t flags)
116 {
117 struct page **level0p;
118 struct page **level1p;
119 struct page **level2p;
120 int count1;
121 int count2;
122
123 LASSERT(ldu->ldu_memhog_pages == 0);
124 LASSERT(ldu->ldu_memhog_root_page == NULL);
125
126 if (npages < 0)
127 return -EINVAL;
128
129 if (npages == 0)
130 return 0;
131
132 level0p = &ldu->ldu_memhog_root_page;
133 *level0p = alloc_page(flags);
134 if (*level0p == NULL)
135 return -ENOMEM;
136 ldu->ldu_memhog_pages++;
137
138 level1p = (struct page **)page_address(*level0p);
139 count1 = 0;
140 memset(level1p, 0, PAGE_CACHE_SIZE);
141
142 while (ldu->ldu_memhog_pages < npages &&
143 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
144
145 if (cfs_signal_pending())
146 return -EINTR;
147
148 *level1p = alloc_page(flags);
149 if (*level1p == NULL)
150 return -ENOMEM;
151 ldu->ldu_memhog_pages++;
152
153 level2p = (struct page **)page_address(*level1p);
154 count2 = 0;
155 memset(level2p, 0, PAGE_CACHE_SIZE);
156
157 while (ldu->ldu_memhog_pages < npages &&
158 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
159
160 if (cfs_signal_pending())
161 return -EINTR;
162
163 *level2p = alloc_page(flags);
164 if (*level2p == NULL)
165 return -ENOMEM;
166 ldu->ldu_memhog_pages++;
167
168 level2p++;
169 count2++;
170 }
171
172 level1p++;
173 count1++;
174 }
175
176 return 0;
177 }
178
179 /* called when opening /dev/device */
180 static int libcfs_psdev_open(unsigned long flags, void *args)
181 {
182 struct libcfs_device_userstate *ldu;
183
184 try_module_get(THIS_MODULE);
185
186 LIBCFS_ALLOC(ldu, sizeof(*ldu));
187 if (ldu != NULL) {
188 ldu->ldu_memhog_pages = 0;
189 ldu->ldu_memhog_root_page = NULL;
190 }
191 *(struct libcfs_device_userstate **)args = ldu;
192
193 return 0;
194 }
195
196 /* called when closing /dev/device */
197 static int libcfs_psdev_release(unsigned long flags, void *args)
198 {
199 struct libcfs_device_userstate *ldu;
200
201 ldu = (struct libcfs_device_userstate *)args;
202 if (ldu != NULL) {
203 kportal_memhog_free(ldu);
204 LIBCFS_FREE(ldu, sizeof(*ldu));
205 }
206
207 module_put(THIS_MODULE);
208 return 0;
209 }
210
211 static DECLARE_RWSEM(ioctl_list_sem);
212 static LIST_HEAD(ioctl_list);
213
214 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
215 {
216 int rc = 0;
217
218 down_write(&ioctl_list_sem);
219 if (!list_empty(&hand->item))
220 rc = -EBUSY;
221 else
222 list_add_tail(&hand->item, &ioctl_list);
223 up_write(&ioctl_list_sem);
224
225 return rc;
226 }
227 EXPORT_SYMBOL(libcfs_register_ioctl);
228
229 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
230 {
231 int rc = 0;
232
233 down_write(&ioctl_list_sem);
234 if (list_empty(&hand->item))
235 rc = -ENOENT;
236 else
237 list_del_init(&hand->item);
238 up_write(&ioctl_list_sem);
239
240 return rc;
241 }
242 EXPORT_SYMBOL(libcfs_deregister_ioctl);
243
244 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile, unsigned long cmd,
245 void *arg, struct libcfs_ioctl_data *data)
246 {
247 int err = -EINVAL;
248
249 switch (cmd) {
250 case IOC_LIBCFS_CLEAR_DEBUG:
251 libcfs_debug_clear_buffer();
252 return 0;
253 /*
254 * case IOC_LIBCFS_PANIC:
255 * Handled in arch/cfs_module.c
256 */
257 case IOC_LIBCFS_MARK_DEBUG:
258 if (data->ioc_inlbuf1 == NULL ||
259 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
260 return -EINVAL;
261 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
262 return 0;
263 case IOC_LIBCFS_MEMHOG:
264 if (pfile->private_data == NULL) {
265 err = -EINVAL;
266 } else {
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,
270 data->ioc_count,
271 data->ioc_flags);
272 if (err != 0)
273 kportal_memhog_free(pfile->private_data);
274 }
275 break;
276
277 default: {
278 struct libcfs_ioctl_handler *hand;
279
280 err = -EINVAL;
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) {
285 if (err == 0)
286 err = libcfs_ioctl_popdata(arg,
287 data, sizeof(*data));
288 break;
289 }
290 }
291 up_read(&ioctl_list_sem);
292 break;
293 }
294 }
295
296 return err;
297 }
298
299 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
300 {
301 char *buf;
302 struct libcfs_ioctl_data *data;
303 int err = 0;
304
305 LIBCFS_ALLOC_GFP(buf, 1024, GFP_KERNEL);
306 if (buf == NULL)
307 return -ENOMEM;
308
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");
312 err = -EINVAL;
313 goto out;
314 }
315 data = (struct libcfs_ioctl_data *)buf;
316
317 err = libcfs_ioctl_int(pfile, cmd, arg, data);
318
319 out:
320 LIBCFS_FREE(buf, 1024);
321 return err;
322 }
323
324 struct cfs_psdev_ops libcfs_psdev_ops = {
325 libcfs_psdev_open,
326 libcfs_psdev_release,
327 NULL,
328 NULL,
329 libcfs_ioctl
330 };
331
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))
336 {
337 int rc = handler(data, write, *ppos, buffer, *lenp);
338
339 if (rc < 0)
340 return rc;
341
342 if (write) {
343 *ppos += *lenp;
344 } else {
345 *lenp = rc;
346 *ppos += rc;
347 }
348 return 0;
349 }
350
351 static int __proc_dobitmasks(void *data, int write,
352 loff_t pos, void __user *buffer, int nob)
353 {
354 const int tmpstrlen = 512;
355 char *tmpstr;
356 int rc;
357 unsigned int *mask = data;
358 int is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
359 int is_printk = (mask == &libcfs_printk) ? 1 : 0;
360
361 rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
362 if (rc < 0)
363 return rc;
364
365 if (!write) {
366 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
367 rc = strlen(tmpstr);
368
369 if (pos >= rc) {
370 rc = 0;
371 } else {
372 rc = cfs_trace_copyout_string(buffer, nob,
373 tmpstr + pos, "\n");
374 }
375 } else {
376 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
377 if (rc < 0) {
378 kfree(tmpstr);
379 return rc;
380 }
381
382 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
383 /* Always print LBUG/LASSERT to console, so keep this mask */
384 if (is_printk)
385 *mask |= D_EMERG;
386 }
387
388 kfree(tmpstr);
389 return rc;
390 }
391
392 static int proc_dobitmasks(struct ctl_table *table, int write,
393 void __user *buffer, size_t *lenp, loff_t *ppos)
394 {
395 return proc_call_handler(table->data, write, ppos, buffer, lenp,
396 __proc_dobitmasks);
397 }
398
399 static int __proc_dump_kernel(void *data, int write,
400 loff_t pos, void __user *buffer, int nob)
401 {
402 if (!write)
403 return 0;
404
405 return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
406 }
407
408 static int proc_dump_kernel(struct ctl_table *table, int write,
409 void __user *buffer, size_t *lenp, loff_t *ppos)
410 {
411 return proc_call_handler(table->data, write, ppos, buffer, lenp,
412 __proc_dump_kernel);
413 }
414
415 static int __proc_daemon_file(void *data, int write,
416 loff_t pos, void __user *buffer, int nob)
417 {
418 if (!write) {
419 int len = strlen(cfs_tracefile);
420
421 if (pos >= len)
422 return 0;
423
424 return cfs_trace_copyout_string(buffer, nob,
425 cfs_tracefile + pos, "\n");
426 }
427
428 return cfs_trace_daemon_command_usrstr(buffer, nob);
429 }
430
431 static int proc_daemon_file(struct ctl_table *table, int write,
432 void __user *buffer, size_t *lenp, loff_t *ppos)
433 {
434 return proc_call_handler(table->data, write, ppos, buffer, lenp,
435 __proc_daemon_file);
436 }
437
438 static int libcfs_force_lbug(struct ctl_table *table, int write,
439 void __user *buffer,
440 size_t *lenp, loff_t *ppos)
441 {
442 if (write)
443 LBUG();
444 return 0;
445 }
446
447 static int proc_fail_loc(struct ctl_table *table, int write,
448 void __user *buffer,
449 size_t *lenp, loff_t *ppos)
450 {
451 int rc;
452 long old_fail_loc = cfs_fail_loc;
453
454 rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
455 if (old_fail_loc != cfs_fail_loc)
456 wake_up(&cfs_race_waitq);
457 return rc;
458 }
459
460 static int __proc_cpt_table(void *data, int write,
461 loff_t pos, void __user *buffer, int nob)
462 {
463 char *buf = NULL;
464 int len = 4096;
465 int rc = 0;
466
467 if (write)
468 return -EPERM;
469
470 LASSERT(cfs_cpt_table != NULL);
471
472 while (1) {
473 LIBCFS_ALLOC(buf, len);
474 if (buf == NULL)
475 return -ENOMEM;
476
477 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
478 if (rc >= 0)
479 break;
480
481 if (rc == -EFBIG) {
482 LIBCFS_FREE(buf, len);
483 len <<= 1;
484 continue;
485 }
486 goto out;
487 }
488
489 if (pos >= rc) {
490 rc = 0;
491 goto out;
492 }
493
494 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
495 out:
496 if (buf != NULL)
497 LIBCFS_FREE(buf, len);
498 return rc;
499 }
500
501 static int proc_cpt_table(struct ctl_table *table, int write,
502 void __user *buffer, size_t *lenp, loff_t *ppos)
503 {
504 return proc_call_handler(table->data, write, ppos, buffer, lenp,
505 __proc_cpt_table);
506 }
507
508 static struct ctl_table lnet_table[] = {
509 /*
510 * NB No .strategy entries have been provided since sysctl(8) prefers
511 * to go via /proc for portability.
512 */
513 {
514 .procname = "debug",
515 .data = &libcfs_debug,
516 .maxlen = sizeof(int),
517 .mode = 0644,
518 .proc_handler = &proc_dobitmasks,
519 },
520 {
521 .procname = "subsystem_debug",
522 .data = &libcfs_subsystem_debug,
523 .maxlen = sizeof(int),
524 .mode = 0644,
525 .proc_handler = &proc_dobitmasks,
526 },
527 {
528 .procname = "printk",
529 .data = &libcfs_printk,
530 .maxlen = sizeof(int),
531 .mode = 0644,
532 .proc_handler = &proc_dobitmasks,
533 },
534 {
535 .procname = "cpu_partition_table",
536 .maxlen = 128,
537 .mode = 0444,
538 .proc_handler = &proc_cpt_table,
539 },
540
541 {
542 .procname = "upcall",
543 .data = lnet_upcall,
544 .maxlen = sizeof(lnet_upcall),
545 .mode = 0644,
546 .proc_handler = &proc_dostring,
547 },
548 {
549 .procname = "debug_log_upcall",
550 .data = lnet_debug_log_upcall,
551 .maxlen = sizeof(lnet_debug_log_upcall),
552 .mode = 0644,
553 .proc_handler = &proc_dostring,
554 },
555 {
556 .procname = "catastrophe",
557 .data = &libcfs_catastrophe,
558 .maxlen = sizeof(int),
559 .mode = 0444,
560 .proc_handler = &proc_dointvec,
561 },
562 {
563 .procname = "dump_kernel",
564 .maxlen = 256,
565 .mode = 0200,
566 .proc_handler = &proc_dump_kernel,
567 },
568 {
569 .procname = "daemon_file",
570 .mode = 0644,
571 .maxlen = 256,
572 .proc_handler = &proc_daemon_file,
573 },
574 {
575 .procname = "force_lbug",
576 .data = NULL,
577 .maxlen = 0,
578 .mode = 0200,
579 .proc_handler = &libcfs_force_lbug
580 },
581 {
582 .procname = "fail_loc",
583 .data = &cfs_fail_loc,
584 .maxlen = sizeof(cfs_fail_loc),
585 .mode = 0644,
586 .proc_handler = &proc_fail_loc
587 },
588 {
589 .procname = "fail_val",
590 .data = &cfs_fail_val,
591 .maxlen = sizeof(int),
592 .mode = 0644,
593 .proc_handler = &proc_dointvec
594 },
595 {
596 }
597 };
598
599 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
600 { "console_ratelimit",
601 "/sys/module/libcfs/parameters/libcfs_console_ratelimit"},
602 { "debug_path",
603 "/sys/module/libcfs/parameters/libcfs_debug_file_path"},
604 { "panic_on_lbug",
605 "/sys/module/libcfs/parameters/libcfs_panic_on_lbug"},
606 { "libcfs_console_backoff",
607 "/sys/module/libcfs/parameters/libcfs_console_backoff"},
608 { "debug_mb",
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"},
614 {},
615 };
616
617 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
618 size_t count, loff_t *ppos)
619 {
620 struct ctl_table *table = filp->private_data;
621 int error;
622
623 error = table->proc_handler(table, 0, (void __user *)buf, &count, ppos);
624 if (!error)
625 error = count;
626
627 return error;
628 }
629
630 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
631 size_t count, loff_t *ppos)
632 {
633 struct ctl_table *table = filp->private_data;
634 int error;
635
636 error = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
637 if (!error)
638 error = count;
639
640 return error;
641 }
642
643 static const struct file_operations lnet_debugfs_file_operations = {
644 .open = simple_open,
645 .read = lnet_debugfs_read,
646 .write = lnet_debugfs_write,
647 .llseek = default_llseek,
648 };
649
650 void lustre_insert_debugfs(struct ctl_table *table,
651 const struct lnet_debugfs_symlink_def *symlinks)
652 {
653 struct dentry *entry;
654
655 if (lnet_debugfs_root == NULL)
656 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
657
658 /* Even if we cannot create, just ignore it altogether) */
659 if (IS_ERR_OR_NULL(lnet_debugfs_root))
660 return;
661
662 for (; table->procname; table++)
663 entry = debugfs_create_file(table->procname, table->mode,
664 lnet_debugfs_root, table,
665 &lnet_debugfs_file_operations);
666
667 for (; symlinks && symlinks->name; symlinks++)
668 entry = debugfs_create_symlink(symlinks->name,
669 lnet_debugfs_root,
670 symlinks->target);
671
672 }
673 EXPORT_SYMBOL_GPL(lustre_insert_debugfs);
674
675 static void lustre_remove_debugfs(void)
676 {
677 if (lnet_debugfs_root != NULL)
678 debugfs_remove_recursive(lnet_debugfs_root);
679
680 lnet_debugfs_root = NULL;
681 }
682
683 static int init_libcfs_module(void)
684 {
685 int rc;
686
687 rc = libcfs_debug_init(5 * 1024 * 1024);
688 if (rc < 0) {
689 pr_err("LustreError: libcfs_debug_init: %d\n", rc);
690 return rc;
691 }
692
693 rc = cfs_cpu_init();
694 if (rc != 0)
695 goto cleanup_debug;
696
697 rc = misc_register(&libcfs_dev);
698 if (rc) {
699 CERROR("misc_register: error %d\n", rc);
700 goto cleanup_cpu;
701 }
702
703 rc = cfs_wi_startup();
704 if (rc) {
705 CERROR("initialize workitem: error %d\n", rc);
706 goto cleanup_deregister;
707 }
708
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);
713 if (rc != 0) {
714 CERROR("Startup workitem scheduler: error: %d\n", rc);
715 goto cleanup_deregister;
716 }
717
718 rc = cfs_crypto_register();
719 if (rc) {
720 CERROR("cfs_crypto_register: error %d\n", rc);
721 goto cleanup_wi;
722 }
723
724 lustre_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
725
726 CDEBUG(D_OTHER, "portals setup OK\n");
727 return 0;
728 cleanup_wi:
729 cfs_wi_shutdown();
730 cleanup_deregister:
731 misc_deregister(&libcfs_dev);
732 cleanup_cpu:
733 cfs_cpu_fini();
734 cleanup_debug:
735 libcfs_debug_cleanup();
736 return rc;
737 }
738
739 static void exit_libcfs_module(void)
740 {
741 int rc;
742
743 lustre_remove_debugfs();
744
745 if (cfs_sched_rehash) {
746 cfs_wi_sched_destroy(cfs_sched_rehash);
747 cfs_sched_rehash = NULL;
748 }
749
750 cfs_crypto_unregister();
751 cfs_wi_shutdown();
752
753 misc_deregister(&libcfs_dev);
754
755 cfs_cpu_fini();
756
757 rc = libcfs_debug_cleanup();
758 if (rc)
759 pr_err("LustreError: libcfs_debug_cleanup: %d\n", rc);
760 }
761
762 MODULE_VERSION("1.0.0");
763
764 module_init(init_libcfs_module);
765 module_exit(exit_libcfs_module);