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