Whamcloud - gitweb
LU-9859 libcfs: move remaining code from linux-module.c to module.c
[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, 2017, 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 BLOCKING_NOTIFIER_HEAD(libcfs_ioctl_list);
62 EXPORT_SYMBOL(libcfs_ioctl_list);
63
64 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
65 {
66         size_t len = sizeof(*data);
67
68         len += (data->ioc_inllen1 + 7) & ~7;
69         len += (data->ioc_inllen2 + 7) & ~7;
70         return len;
71 }
72
73 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
74 {
75         if (data->ioc_hdr.ioc_len > BIT(30))
76                 return true;
77
78         if (data->ioc_inllen1 > BIT(30))
79                 return true;
80
81         if (data->ioc_inllen2 > BIT(30))
82                 return true;
83
84         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
85                 return true;
86
87         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
88                 return true;
89
90         if (data->ioc_pbuf1 && !data->ioc_plen1)
91                 return true;
92
93         if (data->ioc_pbuf2 && !data->ioc_plen2)
94                 return true;
95
96         if (data->ioc_plen1 && !data->ioc_pbuf1)
97                 return true;
98
99         if (data->ioc_plen2 && !data->ioc_pbuf2)
100                 return true;
101
102         if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
103                 return true;
104
105         if (data->ioc_inllen1 &&
106                 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
107                                data->ioc_inllen2 - 1] != '\0')
108                 return true;
109
110         return false;
111 }
112
113 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
114 {
115         ENTRY;
116
117         if (libcfs_ioctl_is_invalid(data)) {
118                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
119                 RETURN(-EINVAL);
120         }
121
122         if (data->ioc_inllen1 != 0)
123                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
124
125         if (data->ioc_inllen2 != 0)
126                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
127                                     cfs_size_round(data->ioc_inllen1);
128
129         RETURN(0);
130 }
131
132 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
133                          struct libcfs_ioctl_hdr __user *uhdr)
134 {
135         struct libcfs_ioctl_hdr hdr;
136         int err;
137
138         ENTRY;
139         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
140                 RETURN(-EFAULT);
141
142         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
143             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
144                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
145                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
146                 RETURN(-EINVAL);
147         }
148
149         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
150                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
151                 RETURN(-EINVAL);
152         }
153
154         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
155                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
156                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
157                 RETURN(-EINVAL);
158         }
159
160         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
161         if (*hdr_pp == NULL)
162                 RETURN(-ENOMEM);
163
164         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
165                 GOTO(free, err = -EFAULT);
166
167         if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
168                 (*hdr_pp)->ioc_len != hdr.ioc_len) {
169                 GOTO(free, err = -EINVAL);
170         }
171
172         RETURN(0);
173
174 free:
175         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
176         RETURN(err);
177 }
178
179 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
180 {
181         struct libcfs_ioctl_data *data = NULL;
182         struct libcfs_ioctl_hdr  *hdr;
183         int                       err;
184         ENTRY;
185
186         /* 'cmd' and permissions get checked in our arch-specific caller */
187         err = libcfs_ioctl_getdata(&hdr, uparam);
188         if (err != 0) {
189                 CDEBUG_LIMIT(D_ERROR,
190                              "libcfs ioctl: data header error %d\n", err);
191                 RETURN(err);
192         }
193
194         if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
195                 /* The libcfs_ioctl_data_adjust() function performs adjustment
196                  * operations on the libcfs_ioctl_data structure to make
197                  * it usable by the code.  This doesn't need to be called
198                  * for new data structures added. */
199                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
200                 err = libcfs_ioctl_data_adjust(data);
201                 if (err != 0)
202                         GOTO(out, err);
203         }
204
205         CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
206         switch (cmd) {
207         case IOC_LIBCFS_CLEAR_DEBUG:
208                 libcfs_debug_clear_buffer();
209                 break;
210         case IOC_LIBCFS_MARK_DEBUG:
211                 if (data == NULL ||
212                     data->ioc_inlbuf1 == NULL ||
213                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
214                         GOTO(out, err = -EINVAL);
215
216                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
217                 break;
218
219         default:
220                 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
221                                                    cmd, hdr);
222                 if (!(err & NOTIFY_STOP_MASK))
223                         /* No-one claimed the ioctl */
224                         err = -EINVAL;
225                 else
226                         err = notifier_to_errno(err);
227                 if (!err)
228                         if (copy_to_user(uparam, hdr, hdr->ioc_len))
229                                 err = -EFAULT;
230                 break;
231         }
232 out:
233         LIBCFS_FREE(hdr, hdr->ioc_len);
234         RETURN(err);
235 }
236
237 static long
238 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
239 {
240         if (!capable(CAP_SYS_ADMIN))
241                 return -EACCES;
242
243         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
244             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
245             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
246                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
247                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
248                 return -EINVAL;
249         }
250
251         return libcfs_ioctl(cmd, (void __user *)arg);
252 }
253
254 static const struct file_operations libcfs_fops = {
255         .owner                  = THIS_MODULE,
256         .unlocked_ioctl         = libcfs_psdev_ioctl,
257 };
258
259 static struct miscdevice libcfs_dev = {
260         .minor                  = MISC_DYNAMIC_MINOR,
261         .name                   = "lnet",
262         .fops                   = &libcfs_fops,
263 };
264
265 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
266                          void __user *buffer, size_t *lenp,
267                          int (*handler)(void *data, int write, loff_t pos,
268                                         void __user *buffer, int len))
269 {
270         int rc = handler(data, write, *ppos, buffer, *lenp);
271
272         if (rc < 0)
273                 return rc;
274
275         if (write) {
276                 *ppos += *lenp;
277         } else {
278                 *lenp = rc;
279                 *ppos += rc;
280         }
281         return 0;
282 }
283 EXPORT_SYMBOL(lprocfs_call_handler);
284
285 static int __proc_dobitmasks(void *data, int write,
286                              loff_t pos, void __user *buffer, int nob)
287 {
288         const int     tmpstrlen = 512;
289         char         *tmpstr;
290         int           rc;
291         unsigned int *mask = data;
292         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
293         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
294
295         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
296         if (rc < 0)
297                 return rc;
298
299         if (!write) {
300                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
301                 rc = strlen(tmpstr);
302
303                 if (pos >= rc) {
304                         rc = 0;
305                 } else {
306                         rc = cfs_trace_copyout_string(buffer, nob,
307                                                       tmpstr + pos, "\n");
308                 }
309         } else {
310                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
311                 if (rc < 0) {
312                         kfree(tmpstr);
313                         return rc;
314                 }
315
316                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
317                 /* Always print LBUG/LASSERT to console, so keep this mask */
318                 if (is_printk)
319                         *mask |= D_EMERG;
320         }
321
322         kfree(tmpstr);
323         return rc;
324 }
325
326 static int proc_dobitmasks(struct ctl_table *table, int write,
327                            void __user *buffer, size_t *lenp, loff_t *ppos)
328 {
329         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
330                                     __proc_dobitmasks);
331 }
332
333 static int min_watchdog_ratelimit;              /* disable ratelimiting */
334 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
335
336 static int __proc_dump_kernel(void *data, int write,
337                               loff_t pos, void __user *buffer, int nob)
338 {
339         if (!write)
340                 return 0;
341
342         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
343 }
344
345 static int proc_dump_kernel(struct ctl_table *table, int write,
346                             void __user *buffer, size_t *lenp, loff_t *ppos)
347 {
348         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
349                                     __proc_dump_kernel);
350 }
351
352 static int __proc_daemon_file(void *data, int write,
353                               loff_t pos, void __user *buffer, int nob)
354 {
355         if (!write) {
356                 int len = strlen(cfs_tracefile);
357
358                 if (pos >= len)
359                         return 0;
360
361                 return cfs_trace_copyout_string(buffer, nob,
362                                                 cfs_tracefile + pos, "\n");
363         }
364
365         return cfs_trace_daemon_command_usrstr(buffer, nob);
366 }
367
368 static int proc_daemon_file(struct ctl_table *table, int write,
369                             void __user *buffer, size_t *lenp, loff_t *ppos)
370 {
371         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
372                                     __proc_daemon_file);
373 }
374
375 static int libcfs_force_lbug(struct ctl_table *table, int write,
376                              void __user *buffer,
377                              size_t *lenp, loff_t *ppos)
378 {
379         if (write)
380                 LBUG();
381         return 0;
382 }
383
384 static int proc_fail_loc(struct ctl_table *table, int write,
385                          void __user *buffer, size_t *lenp, loff_t *ppos)
386 {
387         int rc;
388         long old_fail_loc = cfs_fail_loc;
389
390         rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
391         if (old_fail_loc != cfs_fail_loc)
392                 wake_up(&cfs_race_waitq);
393         return rc;
394 }
395
396 static int __proc_cpt_table(void *data, int write,
397                             loff_t pos, void __user *buffer, int nob)
398 {
399         char *buf = NULL;
400         int   len = 4096;
401         int   rc  = 0;
402
403         if (write)
404                 return -EPERM;
405
406         LASSERT(cfs_cpt_table != NULL);
407
408         while (1) {
409                 LIBCFS_ALLOC(buf, len);
410                 if (buf == NULL)
411                         return -ENOMEM;
412
413                 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
414                 if (rc >= 0)
415                         break;
416
417                 if (rc == -EFBIG) {
418                         LIBCFS_FREE(buf, len);
419                         len <<= 1;
420                         continue;
421                 }
422                 goto out;
423         }
424
425         if (pos >= rc) {
426                 rc = 0;
427                 goto out;
428         }
429
430         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
431 out:
432         if (buf != NULL)
433                 LIBCFS_FREE(buf, len);
434         return rc;
435 }
436
437 static int proc_cpt_table(struct ctl_table *table, int write,
438                           void __user *buffer, size_t *lenp, loff_t *ppos)
439 {
440         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
441                                     __proc_cpt_table);
442 }
443
444 static int __proc_cpt_distance(void *data, int write,
445                                loff_t pos, void __user *buffer, int nob)
446 {
447         char *buf = NULL;
448         int   len = 4096;
449         int   rc  = 0;
450
451         if (write)
452                 return -EPERM;
453
454         LASSERT(cfs_cpt_table != NULL);
455
456         while (1) {
457                 LIBCFS_ALLOC(buf, len);
458                 if (buf == NULL)
459                         return -ENOMEM;
460
461                 rc = cfs_cpt_distance_print(cfs_cpt_table, buf, len);
462                 if (rc >= 0)
463                         break;
464
465                 if (rc == -EFBIG) {
466                         LIBCFS_FREE(buf, len);
467                         len <<= 1;
468                         continue;
469                 }
470                 goto out;
471         }
472
473         if (pos >= rc) {
474                 rc = 0;
475                 goto out;
476         }
477
478         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
479  out:
480         if (buf != NULL)
481                 LIBCFS_FREE(buf, len);
482         return rc;
483 }
484
485 static int proc_cpt_distance(struct ctl_table *table, int write,
486                              void __user *buffer, size_t *lenp, loff_t *ppos)
487 {
488         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
489                                      __proc_cpt_distance);
490 }
491
492 static struct ctl_table lnet_table[] = {
493         {
494                 INIT_CTL_NAME
495                 .procname       = "debug",
496                 .data           = &libcfs_debug,
497                 .maxlen         = sizeof(int),
498                 .mode           = 0644,
499                 .proc_handler   = &proc_dobitmasks,
500         },
501         {
502                 INIT_CTL_NAME
503                 .procname       = "subsystem_debug",
504                 .data           = &libcfs_subsystem_debug,
505                 .maxlen         = sizeof(int),
506                 .mode           = 0644,
507                 .proc_handler   = &proc_dobitmasks,
508         },
509         {
510                 INIT_CTL_NAME
511                 .procname       = "printk",
512                 .data           = &libcfs_printk,
513                 .maxlen         = sizeof(int),
514                 .mode           = 0644,
515                 .proc_handler   = &proc_dobitmasks,
516         },
517         {
518                 INIT_CTL_NAME
519                 .procname       = "cpu_partition_table",
520                 .maxlen         = 128,
521                 .mode           = 0444,
522                 .proc_handler   = &proc_cpt_table,
523         },
524         {
525                 INIT_CTL_NAME
526                 .procname       = "cpu_partition_distance",
527                 .maxlen         = 128,
528                 .mode           = 0444,
529                 .proc_handler   = &proc_cpt_distance,
530         },
531         {
532                 INIT_CTL_NAME
533                 .procname       = "debug_log_upcall",
534                 .data           = lnet_debug_log_upcall,
535                 .maxlen         = sizeof(lnet_debug_log_upcall),
536                 .mode           = 0644,
537                 .proc_handler   = &proc_dostring,
538         },
539         {
540                 INIT_CTL_NAME
541                 .procname       = "lnet_memused",
542                 .data           = (int *)&libcfs_kmemory.counter,
543                 .maxlen         = sizeof(int),
544                 .mode           = 0444,
545                 .proc_handler   = &proc_dointvec,
546         },
547         {
548                 INIT_CTL_NAME
549                 .procname       = "catastrophe",
550                 .data           = &libcfs_catastrophe,
551                 .maxlen         = sizeof(int),
552                 .mode           = 0444,
553                 .proc_handler   = &proc_dointvec,
554         },
555         {
556                 INIT_CTL_NAME
557                 .procname       = "dump_kernel",
558                 .maxlen         = 256,
559                 .mode           = 0200,
560                 .proc_handler   = &proc_dump_kernel,
561         },
562         {
563                 INIT_CTL_NAME
564                 .procname       = "daemon_file",
565                 .mode           = 0644,
566                 .maxlen         = 256,
567                 .proc_handler   = &proc_daemon_file,
568         },
569         {
570                 INIT_CTL_NAME
571                 .procname       = "watchdog_ratelimit",
572                 .data           = &libcfs_watchdog_ratelimit,
573                 .maxlen         = sizeof(int),
574                 .mode           = 0644,
575                 .proc_handler   = &proc_dointvec_minmax,
576                 .extra1         = &min_watchdog_ratelimit,
577                 .extra2         = &max_watchdog_ratelimit,
578         },
579         {
580                 INIT_CTL_NAME
581                 .procname       = "force_lbug",
582                 .data           = NULL,
583                 .maxlen         = 0,
584                 .mode           = 0200,
585                 .proc_handler   = &libcfs_force_lbug
586         },
587         {
588                 INIT_CTL_NAME
589                 .procname       = "fail_loc",
590                 .data           = &cfs_fail_loc,
591                 .maxlen         = sizeof(cfs_fail_loc),
592                 .mode           = 0644,
593                 .proc_handler   = &proc_fail_loc
594         },
595         {
596                 INIT_CTL_NAME
597                 .procname       = "fail_val",
598                 .data           = &cfs_fail_val,
599                 .maxlen         = sizeof(int),
600                 .mode           = 0644,
601                 .proc_handler   = &proc_dointvec
602         },
603         {
604                 INIT_CTL_NAME
605                 .procname       = "fail_err",
606                 .data           = &cfs_fail_err,
607                 .maxlen         = sizeof(cfs_fail_err),
608                 .mode           = 0644,
609                 .proc_handler   = &proc_dointvec,
610         },
611         {
612         }
613 };
614
615 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
616         { .name         = "console_ratelimit",
617           .target       = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
618         { .name         = "debug_path",
619           .target       = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
620         { .name         = "panic_on_lbug",
621           .target       = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
622         { .name         = "console_backoff",
623           .target       = "../../../module/libcfs/parameters/libcfs_console_backoff" },
624         { .name         = "debug_mb",
625           .target       = "../../../module/libcfs/parameters/libcfs_debug_mb" },
626         { .name         = "console_min_delay_centisecs",
627           .target       = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
628         { .name         = "console_max_delay_centisecs",
629           .target       = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
630         { .name         = NULL },
631 };
632
633 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
634                                  size_t count, loff_t *ppos)
635 {
636         struct ctl_table *table = filp->private_data;
637         ssize_t rc = -EINVAL;
638
639         if (table) {
640                 rc = table->proc_handler(table, 0, buf, &count, ppos);
641                 if (!rc)
642                         rc = count;
643         }
644
645         return rc;
646 }
647
648 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
649                                   size_t count, loff_t *ppos)
650 {
651         struct ctl_table *table = filp->private_data;
652         ssize_t rc = -EINVAL;
653
654         if (table) {
655                 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
656                                          ppos);
657                 if (!rc)
658                         rc = count;
659         }
660
661         return rc;
662 }
663
664 static const struct file_operations lnet_debugfs_file_operations_rw = {
665         .open           = simple_open,
666         .read           = lnet_debugfs_read,
667         .write          = lnet_debugfs_write,
668         .llseek         = default_llseek,
669 };
670
671 static const struct file_operations lnet_debugfs_file_operations_ro = {
672         .open           = simple_open,
673         .read           = lnet_debugfs_read,
674         .llseek         = default_llseek,
675 };
676
677 static const struct file_operations lnet_debugfs_file_operations_wo = {
678         .open           = simple_open,
679         .write          = lnet_debugfs_write,
680         .llseek         = default_llseek,
681 };
682
683 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
684 {
685         if (!(mode & S_IWUGO))
686                 return &lnet_debugfs_file_operations_ro;
687
688         if (!(mode & S_IRUGO))
689                 return &lnet_debugfs_file_operations_wo;
690
691         return &lnet_debugfs_file_operations_rw;
692 }
693
694 void lnet_insert_debugfs(struct ctl_table *table)
695 {
696         if (!lnet_debugfs_root)
697                 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
698
699         /* Even if we cannot create, just ignore it altogether) */
700         if (IS_ERR_OR_NULL(lnet_debugfs_root))
701                 return;
702
703         /* We don't save the dentry returned in next two calls, because
704          * we don't call debugfs_remove() but rather remove_recursive()
705          */
706         for (; table && table->procname; table++)
707                 debugfs_create_file(table->procname, table->mode,
708                                     lnet_debugfs_root, table,
709                                     lnet_debugfs_fops_select(table->mode));
710 }
711 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
712
713 static void lnet_insert_debugfs_links(
714                 const struct lnet_debugfs_symlink_def *symlinks)
715 {
716         for (; symlinks && symlinks->name; symlinks++)
717                 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
718                                        symlinks->target);
719 }
720
721 void lnet_remove_debugfs(struct ctl_table *table)
722 {
723         for (; table && table->procname; table++) {
724                 struct qstr dname = QSTR_INIT(table->procname,
725                                               strlen(table->procname));
726                 struct dentry *dentry;
727
728                 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
729                 debugfs_remove(dentry);
730         }
731 }
732 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
733
734 static int __init libcfs_init(void)
735 {
736         int rc;
737
738 #ifndef HAVE_WAIT_VAR_EVENT
739         wait_bit_init();
740 #endif
741         rc = libcfs_debug_init(5 * 1024 * 1024);
742         if (rc < 0) {
743                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
744                 return (rc);
745         }
746
747         rc = cfs_cpu_init();
748         if (rc != 0)
749                 goto cleanup_debug;
750
751         rc = misc_register(&libcfs_dev);
752         if (rc) {
753                 CERROR("misc_register: error %d\n", rc);
754                 goto cleanup_cpu;
755         }
756
757         rc = cfs_wi_startup();
758         if (rc) {
759                 CERROR("initialize workitem: error %d\n", rc);
760                 goto cleanup_deregister;
761         }
762
763         cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
764         if (!cfs_rehash_wq) {
765                 rc = -ENOMEM;
766                 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
767                        rc);
768                 goto cleanup_deregister;
769         }
770
771         rc = cfs_crypto_register();
772         if (rc) {
773                 CERROR("cfs_crypto_regster: error %d\n", rc);
774                 goto cleanup_wi;
775         }
776
777         lnet_insert_debugfs(lnet_table);
778         if (!IS_ERR_OR_NULL(lnet_debugfs_root))
779                 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
780
781         CDEBUG (D_OTHER, "portals setup OK\n");
782         return 0;
783 cleanup_wi:
784         cfs_wi_shutdown();
785 cleanup_deregister:
786         misc_deregister(&libcfs_dev);
787 cleanup_cpu:
788         cfs_cpu_fini();
789 cleanup_debug:
790         libcfs_debug_cleanup();
791         return rc;
792 }
793
794 static void __exit libcfs_exit(void)
795 {
796         int rc;
797
798         /* Remove everthing */
799         debugfs_remove_recursive(lnet_debugfs_root);
800         lnet_debugfs_root = NULL;
801
802         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
803                atomic_read(&libcfs_kmemory));
804
805         if (cfs_rehash_wq) {
806                 destroy_workqueue(cfs_rehash_wq);
807                 cfs_rehash_wq = NULL;
808         }
809
810         cfs_crypto_unregister();
811         cfs_wi_shutdown();
812
813         misc_deregister(&libcfs_dev);
814
815         cfs_cpu_fini();
816
817         /* the below message is checked in test-framework.sh check_mem_leak() */
818         if (atomic_read(&libcfs_kmemory) != 0)
819                 CERROR("Portals memory leaked: %d bytes\n",
820                        atomic_read(&libcfs_kmemory));
821
822         rc = libcfs_debug_cleanup();
823         if (rc)
824                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
825                        rc);
826 }
827
828 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
829 MODULE_DESCRIPTION("Lustre helper library");
830 MODULE_VERSION(LIBCFS_VERSION);
831 MODULE_LICENSE("GPL");
832
833 module_init(libcfs_init);
834 module_exit(libcfs_exit);