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