Whamcloud - gitweb
LU-10086 libcfs: use dynamic minors for /dev/{lnet,obd}
[fs/lustre-release.git] / libcfs / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/mm.h>
36 #include <linux/string.h>
37 #include <linux/stat.h>
38 #include <linux/errno.h>
39 #include <linux/unistd.h>
40 #include <net/sock.h>
41 #include <linux/uio.h>
42 #include <linux/uaccess.h>
43
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/list.h>
47
48 #include <linux/sysctl.h>
49 #include <linux/debugfs.h>
50 #include <asm/div64.h>
51
52 #define DEBUG_SUBSYSTEM S_LNET
53
54 #include <libcfs/libcfs.h>
55 #include <libcfs/libcfs_crypto.h>
56 #include <lnet/lib-lnet.h>
57 #include "tracefile.h"
58
59 static struct dentry *lnet_debugfs_root;
60
61 static DECLARE_RWSEM(ioctl_list_sem);
62 static LIST_HEAD(ioctl_list);
63
64 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
65 {
66         int rc = 0;
67
68         down_write(&ioctl_list_sem);
69         if (!list_empty(&hand->item))
70                 rc = -EBUSY;
71         else
72                 list_add_tail(&hand->item, &ioctl_list);
73         up_write(&ioctl_list_sem);
74
75         return rc;
76 }
77 EXPORT_SYMBOL(libcfs_register_ioctl);
78
79 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
80 {
81         int rc = 0;
82
83         down_write(&ioctl_list_sem);
84         if (list_empty(&hand->item))
85                 rc = -ENOENT;
86         else
87                 list_del_init(&hand->item);
88         up_write(&ioctl_list_sem);
89
90         return rc;
91 }
92 EXPORT_SYMBOL(libcfs_deregister_ioctl);
93
94 int libcfs_ioctl(unsigned long cmd, void __user *uparam)
95 {
96         struct libcfs_ioctl_data *data = NULL;
97         struct libcfs_ioctl_hdr  *hdr;
98         int                       err;
99         ENTRY;
100
101         /* 'cmd' and permissions get checked in our arch-specific caller */
102         err = libcfs_ioctl_getdata(&hdr, uparam);
103         if (err != 0) {
104                 CDEBUG_LIMIT(D_ERROR,
105                              "libcfs ioctl: data header error %d\n", err);
106                 RETURN(err);
107         }
108
109         if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
110                 /* The libcfs_ioctl_data_adjust() function performs adjustment
111                  * operations on the libcfs_ioctl_data structure to make
112                  * it usable by the code.  This doesn't need to be called
113                  * for new data structures added. */
114                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
115                 err = libcfs_ioctl_data_adjust(data);
116                 if (err != 0)
117                         GOTO(out, err);
118         }
119
120         CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
121         switch (cmd) {
122         case IOC_LIBCFS_CLEAR_DEBUG:
123                 libcfs_debug_clear_buffer();
124                 break;
125         case IOC_LIBCFS_MARK_DEBUG:
126                 if (data == NULL ||
127                     data->ioc_inlbuf1 == NULL ||
128                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
129                         GOTO(out, err = -EINVAL);
130
131                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
132                 break;
133
134         default: {
135                 struct libcfs_ioctl_handler *hand;
136
137                 err = -EINVAL;
138                 down_read(&ioctl_list_sem);
139                 list_for_each_entry(hand, &ioctl_list, item) {
140                         err = hand->handle_ioctl(cmd, hdr);
141                         if (err == -EINVAL)
142                                 continue;
143
144                         if (copy_to_user(uparam, hdr, hdr->ioc_len))
145                                 err = -EFAULT;
146                         break;
147                 }
148                 up_read(&ioctl_list_sem);
149                 break; }
150         }
151 out:
152         LIBCFS_FREE(hdr, hdr->ioc_len);
153         RETURN(err);
154 }
155
156 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
157                          void __user *buffer, size_t *lenp,
158                          int (*handler)(void *data, int write, loff_t pos,
159                                         void __user *buffer, int len))
160 {
161         int rc = handler(data, write, *ppos, buffer, *lenp);
162
163         if (rc < 0)
164                 return rc;
165
166         if (write) {
167                 *ppos += *lenp;
168         } else {
169                 *lenp = rc;
170                 *ppos += rc;
171         }
172         return 0;
173 }
174 EXPORT_SYMBOL(lprocfs_call_handler);
175
176 static int __proc_dobitmasks(void *data, int write,
177                              loff_t pos, void __user *buffer, int nob)
178 {
179         const int     tmpstrlen = 512;
180         char         *tmpstr;
181         int           rc;
182         unsigned int *mask = data;
183         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
184         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
185
186         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
187         if (rc < 0)
188                 return rc;
189
190         if (!write) {
191                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
192                 rc = strlen(tmpstr);
193
194                 if (pos >= rc) {
195                         rc = 0;
196                 } else {
197                         rc = cfs_trace_copyout_string(buffer, nob,
198                                                       tmpstr + pos, "\n");
199                 }
200         } else {
201                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
202                 if (rc < 0) {
203                         kfree(tmpstr);
204                         return rc;
205                 }
206
207                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
208                 /* Always print LBUG/LASSERT to console, so keep this mask */
209                 if (is_printk)
210                         *mask |= D_EMERG;
211         }
212
213         kfree(tmpstr);
214         return rc;
215 }
216
217 static int proc_dobitmasks(struct ctl_table *table, int write,
218                            void __user *buffer, size_t *lenp, loff_t *ppos)
219 {
220         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
221                                     __proc_dobitmasks);
222 }
223
224 static int min_watchdog_ratelimit;              /* disable ratelimiting */
225 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
226
227 static int __proc_dump_kernel(void *data, int write,
228                               loff_t pos, void __user *buffer, int nob)
229 {
230         if (!write)
231                 return 0;
232
233         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
234 }
235
236 static int proc_dump_kernel(struct ctl_table *table, int write,
237                             void __user *buffer, size_t *lenp, loff_t *ppos)
238 {
239         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
240                                     __proc_dump_kernel);
241 }
242
243 static int __proc_daemon_file(void *data, int write,
244                               loff_t pos, void __user *buffer, int nob)
245 {
246         if (!write) {
247                 int len = strlen(cfs_tracefile);
248
249                 if (pos >= len)
250                         return 0;
251
252                 return cfs_trace_copyout_string(buffer, nob,
253                                                 cfs_tracefile + pos, "\n");
254         }
255
256         return cfs_trace_daemon_command_usrstr(buffer, nob);
257 }
258
259 static int proc_daemon_file(struct ctl_table *table, int write,
260                             void __user *buffer, size_t *lenp, loff_t *ppos)
261 {
262         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
263                                     __proc_daemon_file);
264 }
265
266 static int libcfs_force_lbug(struct ctl_table *table, int write,
267                              void __user *buffer,
268                              size_t *lenp, loff_t *ppos)
269 {
270         if (write)
271                 LBUG();
272         return 0;
273 }
274
275 static int proc_fail_loc(struct ctl_table *table, int write,
276                          void __user *buffer, size_t *lenp, loff_t *ppos)
277 {
278         int rc;
279         long old_fail_loc = cfs_fail_loc;
280
281         rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
282         if (old_fail_loc != cfs_fail_loc)
283                 wake_up(&cfs_race_waitq);
284         return rc;
285 }
286
287 static int __proc_cpt_table(void *data, int write,
288                             loff_t pos, void __user *buffer, int nob)
289 {
290         char *buf = NULL;
291         int   len = 4096;
292         int   rc  = 0;
293
294         if (write)
295                 return -EPERM;
296
297         LASSERT(cfs_cpt_table != NULL);
298
299         while (1) {
300                 LIBCFS_ALLOC(buf, len);
301                 if (buf == NULL)
302                         return -ENOMEM;
303
304                 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
305                 if (rc >= 0)
306                         break;
307
308                 if (rc == -EFBIG) {
309                         LIBCFS_FREE(buf, len);
310                         len <<= 1;
311                         continue;
312                 }
313                 goto out;
314         }
315
316         if (pos >= rc) {
317                 rc = 0;
318                 goto out;
319         }
320
321         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
322 out:
323         if (buf != NULL)
324                 LIBCFS_FREE(buf, len);
325         return rc;
326 }
327
328 static int proc_cpt_table(struct ctl_table *table, int write,
329                           void __user *buffer, size_t *lenp, loff_t *ppos)
330 {
331         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
332                                     __proc_cpt_table);
333 }
334
335 static int __proc_cpt_distance(void *data, int write,
336                                loff_t pos, void __user *buffer, int nob)
337 {
338         char *buf = NULL;
339         int   len = 4096;
340         int   rc  = 0;
341
342         if (write)
343                 return -EPERM;
344
345         LASSERT(cfs_cpt_table != NULL);
346
347         while (1) {
348                 LIBCFS_ALLOC(buf, len);
349                 if (buf == NULL)
350                         return -ENOMEM;
351
352                 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
353                 if (rc >= 0)
354                         break;
355
356                 if (rc == -EFBIG) {
357                         LIBCFS_FREE(buf, len);
358                         len <<= 1;
359                         continue;
360                 }
361                 goto out;
362         }
363
364         if (pos >= rc) {
365                 rc = 0;
366                 goto out;
367         }
368
369         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
370  out:
371         if (buf != NULL)
372                 LIBCFS_FREE(buf, len);
373         return rc;
374 }
375
376 static int proc_cpt_distance(struct ctl_table *table, int write,
377                              void __user *buffer, size_t *lenp, loff_t *ppos)
378 {
379         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
380                                      __proc_cpt_distance);
381 }
382
383 static struct ctl_table lnet_table[] = {
384         {
385                 INIT_CTL_NAME
386                 .procname       = "debug",
387                 .data           = &libcfs_debug,
388                 .maxlen         = sizeof(int),
389                 .mode           = 0644,
390                 .proc_handler   = &proc_dobitmasks,
391         },
392         {
393                 INIT_CTL_NAME
394                 .procname       = "subsystem_debug",
395                 .data           = &libcfs_subsystem_debug,
396                 .maxlen         = sizeof(int),
397                 .mode           = 0644,
398                 .proc_handler   = &proc_dobitmasks,
399         },
400         {
401                 INIT_CTL_NAME
402                 .procname       = "printk",
403                 .data           = &libcfs_printk,
404                 .maxlen         = sizeof(int),
405                 .mode           = 0644,
406                 .proc_handler   = &proc_dobitmasks,
407         },
408         {
409                 INIT_CTL_NAME
410                 .procname       = "cpu_partition_table",
411                 .maxlen         = 128,
412                 .mode           = 0444,
413                 .proc_handler   = &proc_cpt_table,
414         },
415         {
416                 INIT_CTL_NAME
417                 .procname       = "cpu_partition_distance",
418                 .maxlen         = 128,
419                 .mode           = 0444,
420                 .proc_handler   = &proc_cpt_distance,
421         },
422         {
423                 INIT_CTL_NAME
424                 .procname       = "debug_log_upcall",
425                 .data           = lnet_debug_log_upcall,
426                 .maxlen         = sizeof(lnet_debug_log_upcall),
427                 .mode           = 0644,
428                 .proc_handler   = &proc_dostring,
429         },
430         {
431                 INIT_CTL_NAME
432                 .procname       = "lnet_memused",
433                 .data           = (int *)&libcfs_kmemory.counter,
434                 .maxlen         = sizeof(int),
435                 .mode           = 0444,
436                 .proc_handler   = &proc_dointvec,
437         },
438         {
439                 INIT_CTL_NAME
440                 .procname       = "catastrophe",
441                 .data           = &libcfs_catastrophe,
442                 .maxlen         = sizeof(int),
443                 .mode           = 0444,
444                 .proc_handler   = &proc_dointvec,
445         },
446         {
447                 INIT_CTL_NAME
448                 .procname       = "dump_kernel",
449                 .maxlen         = 256,
450                 .mode           = 0200,
451                 .proc_handler   = &proc_dump_kernel,
452         },
453         {
454                 INIT_CTL_NAME
455                 .procname       = "daemon_file",
456                 .mode           = 0644,
457                 .maxlen         = 256,
458                 .proc_handler   = &proc_daemon_file,
459         },
460         {
461                 INIT_CTL_NAME
462                 .procname       = "watchdog_ratelimit",
463                 .data           = &libcfs_watchdog_ratelimit,
464                 .maxlen         = sizeof(int),
465                 .mode           = 0644,
466                 .proc_handler   = &proc_dointvec_minmax,
467                 .extra1         = &min_watchdog_ratelimit,
468                 .extra2         = &max_watchdog_ratelimit,
469         },
470         {
471                 INIT_CTL_NAME
472                 .procname       = "force_lbug",
473                 .data           = NULL,
474                 .maxlen         = 0,
475                 .mode           = 0200,
476                 .proc_handler   = &libcfs_force_lbug
477         },
478         {
479                 INIT_CTL_NAME
480                 .procname       = "fail_loc",
481                 .data           = &cfs_fail_loc,
482                 .maxlen         = sizeof(cfs_fail_loc),
483                 .mode           = 0644,
484                 .proc_handler   = &proc_fail_loc
485         },
486         {
487                 INIT_CTL_NAME
488                 .procname       = "fail_val",
489                 .data           = &cfs_fail_val,
490                 .maxlen         = sizeof(int),
491                 .mode           = 0644,
492                 .proc_handler   = &proc_dointvec
493         },
494         {
495                 INIT_CTL_NAME
496                 .procname       = "fail_err",
497                 .data           = &cfs_fail_err,
498                 .maxlen         = sizeof(cfs_fail_err),
499                 .mode           = 0644,
500                 .proc_handler   = &proc_dointvec,
501         },
502         {
503         }
504 };
505
506 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
507         { .name         = "console_ratelimit",
508           .target       = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
509         { .name         = "debug_path",
510           .target       = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
511         { .name         = "panic_on_lbug",
512           .target       = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
513         { .name         = "console_backoff",
514           .target       = "../../../module/libcfs/parameters/libcfs_console_backoff" },
515         { .name         = "debug_mb",
516           .target       = "../../../module/libcfs/parameters/libcfs_debug_mb" },
517         { .name         = "console_min_delay_centisecs",
518           .target       = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
519         { .name         = "console_max_delay_centisecs",
520           .target       = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
521         { .name         = NULL },
522 };
523
524 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
525                                  size_t count, loff_t *ppos)
526 {
527         struct ctl_table *table = filp->private_data;
528         ssize_t rc;
529
530         rc = table->proc_handler(table, 0, buf, &count, ppos);
531         if (!rc)
532                 rc = count;
533
534         return rc;
535 }
536
537 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
538                                   size_t count, loff_t *ppos)
539 {
540         struct ctl_table *table = filp->private_data;
541         ssize_t rc;
542
543         rc = table->proc_handler(table, 1, (void __user *)buf, &count, ppos);
544         if (!rc)
545                 rc = count;
546
547         return rc;
548 }
549
550 static const struct file_operations lnet_debugfs_file_operations_rw = {
551         .open           = simple_open,
552         .read           = lnet_debugfs_read,
553         .write          = lnet_debugfs_write,
554         .llseek         = default_llseek,
555 };
556
557 static const struct file_operations lnet_debugfs_file_operations_ro = {
558         .open           = simple_open,
559         .read           = lnet_debugfs_read,
560         .llseek         = default_llseek,
561 };
562
563 static const struct file_operations lnet_debugfs_file_operations_wo = {
564         .open           = simple_open,
565         .write          = lnet_debugfs_write,
566         .llseek         = default_llseek,
567 };
568
569 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
570 {
571         if (!(mode & S_IWUGO))
572                 return &lnet_debugfs_file_operations_ro;
573
574         if (!(mode & S_IRUGO))
575                 return &lnet_debugfs_file_operations_wo;
576
577         return &lnet_debugfs_file_operations_rw;
578 }
579
580 void lnet_insert_debugfs(struct ctl_table *table,
581                          const struct lnet_debugfs_symlink_def *symlinks)
582 {
583         if (!lnet_debugfs_root)
584                 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
585
586         /* Even if we cannot create, just ignore it altogether) */
587         if (IS_ERR_OR_NULL(lnet_debugfs_root))
588                 return;
589
590         /* We don't save the dentry returned in next two calls, because
591          * we don't call debugfs_remove() but rather remove_recursive()
592          */
593         for (; table && table->procname; table++)
594                 debugfs_create_file(table->procname, table->mode,
595                                     lnet_debugfs_root, table,
596                                     lnet_debugfs_fops_select(table->mode));
597
598         for (; symlinks && symlinks->name; symlinks++)
599                 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
600                                        symlinks->target);
601 }
602 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
603
604 static void lnet_remove_debugfs(void)
605 {
606         debugfs_remove_recursive(lnet_debugfs_root);
607
608         lnet_debugfs_root = NULL;
609 }
610
611 static int __init libcfs_init(void)
612 {
613         int rc;
614
615         rc = libcfs_debug_init(5 * 1024 * 1024);
616         if (rc < 0) {
617                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
618                 return (rc);
619         }
620
621         rc = cfs_cpu_init();
622         if (rc != 0)
623                 goto cleanup_debug;
624
625         rc = misc_register(&libcfs_dev);
626         if (rc) {
627                 CERROR("misc_register: error %d\n", rc);
628                 goto cleanup_cpu;
629         }
630
631         rc = cfs_wi_startup();
632         if (rc) {
633                 CERROR("initialize workitem: error %d\n", rc);
634                 goto cleanup_deregister;
635         }
636
637         /* max to 4 threads, should be enough for rehash */
638         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
639         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
640                                  rc, &cfs_sched_rehash);
641         if (rc != 0) {
642                 CERROR("Startup workitem scheduler: error: %d\n", rc);
643                 goto cleanup_deregister;
644         }
645
646         rc = cfs_crypto_register();
647         if (rc) {
648                 CERROR("cfs_crypto_regster: error %d\n", rc);
649                 goto cleanup_wi;
650         }
651
652         lnet_insert_debugfs(lnet_table, lnet_debugfs_symlinks);
653
654         CDEBUG (D_OTHER, "portals setup OK\n");
655         return 0;
656 cleanup_wi:
657         cfs_wi_shutdown();
658 cleanup_deregister:
659         misc_deregister(&libcfs_dev);
660 cleanup_cpu:
661         cfs_cpu_fini();
662 cleanup_debug:
663         libcfs_debug_cleanup();
664         return rc;
665 }
666
667 static void __exit libcfs_exit(void)
668 {
669         int rc;
670
671         lnet_remove_debugfs();
672
673         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
674                atomic_read(&libcfs_kmemory));
675
676         if (cfs_sched_rehash != NULL) {
677                 cfs_wi_sched_destroy(cfs_sched_rehash);
678                 cfs_sched_rehash = NULL;
679         }
680
681         cfs_crypto_unregister();
682         cfs_wi_shutdown();
683
684         misc_deregister(&libcfs_dev);
685
686         cfs_cpu_fini();
687
688         if (atomic_read(&libcfs_kmemory) != 0)
689                 CERROR("Portals memory leaked: %d bytes\n",
690                        atomic_read(&libcfs_kmemory));
691
692         rc = libcfs_debug_cleanup();
693         if (rc)
694                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
695                        rc);
696 }
697
698 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
699 MODULE_DESCRIPTION("Lustre helper library");
700 MODULE_VERSION(LIBCFS_VERSION);
701 MODULE_LICENSE("GPL");
702
703 module_init(libcfs_init);
704 module_exit(libcfs_exit);