Whamcloud - gitweb
fd6f34b59f598cb005ab134a7ae36d872e83fee8
[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 (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
228                         err = -EFAULT;
229                 break;
230         }
231 out:
232         LIBCFS_FREE(hdr, hdr->ioc_len);
233         RETURN(err);
234 }
235
236 static long
237 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
238 {
239         if (!capable(CAP_SYS_ADMIN))
240                 return -EACCES;
241
242         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
243             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
244             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
245                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
246                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
247                 return -EINVAL;
248         }
249
250         return libcfs_ioctl(cmd, (void __user *)arg);
251 }
252
253 static const struct file_operations libcfs_fops = {
254         .owner                  = THIS_MODULE,
255         .unlocked_ioctl         = libcfs_psdev_ioctl,
256 };
257
258 static struct miscdevice libcfs_dev = {
259         .minor                  = MISC_DYNAMIC_MINOR,
260         .name                   = "lnet",
261         .fops                   = &libcfs_fops,
262 };
263
264 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
265                          void __user *buffer, size_t *lenp,
266                          int (*handler)(void *data, int write, loff_t pos,
267                                         void __user *buffer, int len))
268 {
269         int rc = handler(data, write, *ppos, buffer, *lenp);
270
271         if (rc < 0)
272                 return rc;
273
274         if (write) {
275                 *ppos += *lenp;
276         } else {
277                 *lenp = rc;
278                 *ppos += rc;
279         }
280         return 0;
281 }
282 EXPORT_SYMBOL(lprocfs_call_handler);
283
284 static int __proc_dobitmasks(void *data, int write,
285                              loff_t pos, void __user *buffer, int nob)
286 {
287         const int     tmpstrlen = 512;
288         char         *tmpstr;
289         int           rc;
290         unsigned int *mask = data;
291         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
292         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
293
294         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
295         if (rc < 0)
296                 return rc;
297
298         if (!write) {
299                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
300                 rc = strlen(tmpstr);
301
302                 if (pos >= rc) {
303                         rc = 0;
304                 } else {
305                         rc = cfs_trace_copyout_string(buffer, nob,
306                                                       tmpstr + pos, "\n");
307                 }
308         } else {
309                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
310                 if (rc < 0) {
311                         kfree(tmpstr);
312                         return rc;
313                 }
314
315                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
316                 /* Always print LBUG/LASSERT to console, so keep this mask */
317                 if (is_printk)
318                         *mask |= D_EMERG;
319         }
320
321         kfree(tmpstr);
322         return rc;
323 }
324
325 static int proc_dobitmasks(struct ctl_table *table, int write,
326                            void __user *buffer, size_t *lenp, loff_t *ppos)
327 {
328         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
329                                     __proc_dobitmasks);
330 }
331
332 static int min_watchdog_ratelimit;              /* disable ratelimiting */
333 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
334
335 static int __proc_dump_kernel(void *data, int write,
336                               loff_t pos, void __user *buffer, int nob)
337 {
338         if (!write)
339                 return 0;
340
341         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
342 }
343
344 static int proc_dump_kernel(struct ctl_table *table, int write,
345                             void __user *buffer, size_t *lenp, loff_t *ppos)
346 {
347         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
348                                     __proc_dump_kernel);
349 }
350
351 static int __proc_daemon_file(void *data, int write,
352                               loff_t pos, void __user *buffer, int nob)
353 {
354         if (!write) {
355                 int len = strlen(cfs_tracefile);
356
357                 if (pos >= len)
358                         return 0;
359
360                 return cfs_trace_copyout_string(buffer, nob,
361                                                 cfs_tracefile + pos, "\n");
362         }
363
364         return cfs_trace_daemon_command_usrstr(buffer, nob);
365 }
366
367 static int proc_daemon_file(struct ctl_table *table, int write,
368                             void __user *buffer, size_t *lenp, loff_t *ppos)
369 {
370         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
371                                     __proc_daemon_file);
372 }
373
374 static int libcfs_force_lbug(struct ctl_table *table, int write,
375                              void __user *buffer,
376                              size_t *lenp, loff_t *ppos)
377 {
378         if (write)
379                 LBUG();
380         return 0;
381 }
382
383 static int proc_fail_loc(struct ctl_table *table, int write,
384                          void __user *buffer, size_t *lenp, loff_t *ppos)
385 {
386         int rc;
387         long old_fail_loc = cfs_fail_loc;
388
389         rc = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
390         if (old_fail_loc != cfs_fail_loc) {
391                 cfs_race_state = 1;
392                 wake_up(&cfs_race_waitq);
393         }
394         return rc;
395 }
396
397 static int __proc_cpt_table(void *data, int write,
398                             loff_t pos, void __user *buffer, int nob)
399 {
400         char *buf = NULL;
401         int   len = 4096;
402         int   rc  = 0;
403
404         if (write)
405                 return -EPERM;
406
407         LASSERT(cfs_cpt_tab);
408
409         while (1) {
410                 LIBCFS_ALLOC(buf, len);
411                 if (buf == NULL)
412                         return -ENOMEM;
413
414                 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
415                 if (rc >= 0)
416                         break;
417
418                 if (rc == -EFBIG) {
419                         LIBCFS_FREE(buf, len);
420                         len <<= 1;
421                         continue;
422                 }
423                 goto out;
424         }
425
426         if (pos >= rc) {
427                 rc = 0;
428                 goto out;
429         }
430
431         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
432 out:
433         if (buf != NULL)
434                 LIBCFS_FREE(buf, len);
435         return rc;
436 }
437
438 static int proc_cpt_table(struct ctl_table *table, int write,
439                           void __user *buffer, size_t *lenp, loff_t *ppos)
440 {
441         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
442                                     __proc_cpt_table);
443 }
444
445 static int __proc_cpt_distance(void *data, int write,
446                                loff_t pos, void __user *buffer, int nob)
447 {
448         char *buf = NULL;
449         int   len = 4096;
450         int   rc  = 0;
451
452         if (write)
453                 return -EPERM;
454
455         LASSERT(cfs_cpt_tab);
456
457         while (1) {
458                 LIBCFS_ALLOC(buf, len);
459                 if (buf == NULL)
460                         return -ENOMEM;
461
462                 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
463                 if (rc >= 0)
464                         break;
465
466                 if (rc == -EFBIG) {
467                         LIBCFS_FREE(buf, len);
468                         len <<= 1;
469                         continue;
470                 }
471                 goto out;
472         }
473
474         if (pos >= rc) {
475                 rc = 0;
476                 goto out;
477         }
478
479         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
480  out:
481         if (buf != NULL)
482                 LIBCFS_FREE(buf, len);
483         return rc;
484 }
485
486 static int proc_cpt_distance(struct ctl_table *table, int write,
487                              void __user *buffer, size_t *lenp, loff_t *ppos)
488 {
489         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
490                                      __proc_cpt_distance);
491 }
492
493 static struct ctl_table lnet_table[] = {
494         {
495                 .procname       = "debug",
496                 .data           = &libcfs_debug,
497                 .maxlen         = sizeof(int),
498                 .mode           = 0644,
499                 .proc_handler   = &proc_dobitmasks,
500         },
501         {
502                 .procname       = "subsystem_debug",
503                 .data           = &libcfs_subsystem_debug,
504                 .maxlen         = sizeof(int),
505                 .mode           = 0644,
506                 .proc_handler   = &proc_dobitmasks,
507         },
508         {
509                 .procname       = "printk",
510                 .data           = &libcfs_printk,
511                 .maxlen         = sizeof(int),
512                 .mode           = 0644,
513                 .proc_handler   = &proc_dobitmasks,
514         },
515         {
516                 .procname       = "cpu_partition_table",
517                 .maxlen         = 128,
518                 .mode           = 0444,
519                 .proc_handler   = &proc_cpt_table,
520         },
521         {
522                 .procname       = "cpu_partition_distance",
523                 .maxlen         = 128,
524                 .mode           = 0444,
525                 .proc_handler   = &proc_cpt_distance,
526         },
527         {
528                 .procname       = "debug_log_upcall",
529                 .data           = lnet_debug_log_upcall,
530                 .maxlen         = sizeof(lnet_debug_log_upcall),
531                 .mode           = 0644,
532                 .proc_handler   = &proc_dostring,
533         },
534         {
535                 .procname       = "lnet_memused",
536                 .data           = (int *)&libcfs_kmemory.counter,
537                 .maxlen         = sizeof(int),
538                 .mode           = 0444,
539                 .proc_handler   = &proc_dointvec,
540         },
541         {
542                 .procname       = "catastrophe",
543                 .data           = &libcfs_catastrophe,
544                 .maxlen         = sizeof(int),
545                 .mode           = 0444,
546                 .proc_handler   = &proc_dointvec,
547         },
548         {
549                 .procname       = "dump_kernel",
550                 .maxlen         = 256,
551                 .mode           = 0200,
552                 .proc_handler   = &proc_dump_kernel,
553         },
554         {
555                 .procname       = "daemon_file",
556                 .mode           = 0644,
557                 .maxlen         = 256,
558                 .proc_handler   = &proc_daemon_file,
559         },
560         {
561                 .procname       = "watchdog_ratelimit",
562                 .data           = &libcfs_watchdog_ratelimit,
563                 .maxlen         = sizeof(int),
564                 .mode           = 0644,
565                 .proc_handler   = &proc_dointvec_minmax,
566                 .extra1         = &min_watchdog_ratelimit,
567                 .extra2         = &max_watchdog_ratelimit,
568         },
569         {
570                 .procname       = "force_lbug",
571                 .data           = NULL,
572                 .maxlen         = 0,
573                 .mode           = 0200,
574                 .proc_handler   = &libcfs_force_lbug
575         },
576         {
577                 .procname       = "fail_loc",
578                 .data           = &cfs_fail_loc,
579                 .maxlen         = sizeof(cfs_fail_loc),
580                 .mode           = 0644,
581                 .proc_handler   = &proc_fail_loc
582         },
583         {
584                 .procname       = "fail_val",
585                 .data           = &cfs_fail_val,
586                 .maxlen         = sizeof(int),
587                 .mode           = 0644,
588                 .proc_handler   = &proc_dointvec
589         },
590         {
591                 .procname       = "fail_err",
592                 .data           = &cfs_fail_err,
593                 .maxlen         = sizeof(cfs_fail_err),
594                 .mode           = 0644,
595                 .proc_handler   = &proc_dointvec,
596         },
597         {
598         }
599 };
600
601 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
602         { .name         = "console_ratelimit",
603           .target       = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
604         { .name         = "debug_path",
605           .target       = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
606         { .name         = "panic_on_lbug",
607           .target       = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
608         { .name         = "console_backoff",
609           .target       = "../../../module/libcfs/parameters/libcfs_console_backoff" },
610         { .name         = "debug_mb",
611           .target       = "../../../module/libcfs/parameters/libcfs_debug_mb" },
612         { .name         = "console_min_delay_centisecs",
613           .target       = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
614         { .name         = "console_max_delay_centisecs",
615           .target       = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
616         { .name         = NULL },
617 };
618
619 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
620                                  size_t count, loff_t *ppos)
621 {
622         struct ctl_table *table = filp->private_data;
623         ssize_t rc = -EINVAL;
624
625         if (table) {
626                 rc = table->proc_handler(table, 0, buf, &count, ppos);
627                 if (!rc)
628                         rc = count;
629         }
630
631         return rc;
632 }
633
634 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
635                                   size_t count, loff_t *ppos)
636 {
637         struct ctl_table *table = filp->private_data;
638         ssize_t rc = -EINVAL;
639
640         if (table) {
641                 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
642                                          ppos);
643                 if (!rc)
644                         rc = count;
645         }
646
647         return rc;
648 }
649
650 static const struct file_operations lnet_debugfs_file_operations_rw = {
651         .open           = simple_open,
652         .read           = lnet_debugfs_read,
653         .write          = lnet_debugfs_write,
654         .llseek         = default_llseek,
655 };
656
657 static const struct file_operations lnet_debugfs_file_operations_ro = {
658         .open           = simple_open,
659         .read           = lnet_debugfs_read,
660         .llseek         = default_llseek,
661 };
662
663 static const struct file_operations lnet_debugfs_file_operations_wo = {
664         .open           = simple_open,
665         .write          = lnet_debugfs_write,
666         .llseek         = default_llseek,
667 };
668
669 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
670 {
671         if (!(mode & S_IWUGO))
672                 return &lnet_debugfs_file_operations_ro;
673
674         if (!(mode & S_IRUGO))
675                 return &lnet_debugfs_file_operations_wo;
676
677         return &lnet_debugfs_file_operations_rw;
678 }
679
680 void lnet_insert_debugfs(struct ctl_table *table)
681 {
682         if (!lnet_debugfs_root)
683                 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
684
685         /* Even if we cannot create, just ignore it altogether) */
686         if (IS_ERR_OR_NULL(lnet_debugfs_root))
687                 return;
688
689         /* We don't save the dentry returned in next two calls, because
690          * we don't call debugfs_remove() but rather remove_recursive()
691          */
692         for (; table && table->procname; table++)
693                 debugfs_create_file(table->procname, table->mode,
694                                     lnet_debugfs_root, table,
695                                     lnet_debugfs_fops_select(table->mode));
696 }
697 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
698
699 static void lnet_insert_debugfs_links(
700                 const struct lnet_debugfs_symlink_def *symlinks)
701 {
702         for (; symlinks && symlinks->name; symlinks++)
703                 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
704                                        symlinks->target);
705 }
706
707 void lnet_remove_debugfs(struct ctl_table *table)
708 {
709         for (; table && table->procname; table++) {
710                 struct qstr dname = QSTR_INIT(table->procname,
711                                               strlen(table->procname));
712                 struct dentry *dentry;
713
714                 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
715                 debugfs_remove(dentry);
716         }
717 }
718 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
719
720 static int __init libcfs_init(void)
721 {
722         int rc;
723
724         cfs_arch_init();
725
726         rc = libcfs_debug_init(5 * 1024 * 1024);
727         if (rc < 0) {
728                 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
729                 return (rc);
730         }
731
732         rc = cfs_cpu_init();
733         if (rc != 0)
734                 goto cleanup_debug;
735
736         rc = misc_register(&libcfs_dev);
737         if (rc) {
738                 CERROR("misc_register: error %d\n", rc);
739                 goto cleanup_cpu;
740         }
741
742         rc = cfs_wi_startup();
743         if (rc) {
744                 CERROR("initialize workitem: error %d\n", rc);
745                 goto cleanup_deregister;
746         }
747
748         cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
749         if (!cfs_rehash_wq) {
750                 rc = -ENOMEM;
751                 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
752                        rc);
753                 goto cleanup_deregister;
754         }
755
756         rc = cfs_crypto_register();
757         if (rc) {
758                 CERROR("cfs_crypto_regster: error %d\n", rc);
759                 goto cleanup_wi;
760         }
761
762         lnet_insert_debugfs(lnet_table);
763         if (!IS_ERR_OR_NULL(lnet_debugfs_root))
764                 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
765
766         CDEBUG (D_OTHER, "portals setup OK\n");
767         return 0;
768 cleanup_wi:
769         cfs_wi_shutdown();
770 cleanup_deregister:
771         misc_deregister(&libcfs_dev);
772 cleanup_cpu:
773         cfs_cpu_fini();
774 cleanup_debug:
775         libcfs_debug_cleanup();
776         return rc;
777 }
778
779 static void __exit libcfs_exit(void)
780 {
781         int rc;
782
783         /* Remove everthing */
784         debugfs_remove_recursive(lnet_debugfs_root);
785         lnet_debugfs_root = NULL;
786
787         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
788                atomic_read(&libcfs_kmemory));
789
790         if (cfs_rehash_wq) {
791                 destroy_workqueue(cfs_rehash_wq);
792                 cfs_rehash_wq = NULL;
793         }
794
795         cfs_crypto_unregister();
796         cfs_wi_shutdown();
797
798         misc_deregister(&libcfs_dev);
799
800         cfs_cpu_fini();
801
802         /* the below message is checked in test-framework.sh check_mem_leak() */
803         if (atomic_read(&libcfs_kmemory) != 0)
804                 CERROR("Portals memory leaked: %d bytes\n",
805                        atomic_read(&libcfs_kmemory));
806
807         rc = libcfs_debug_cleanup();
808         if (rc)
809                 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
810 }
811
812 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
813 MODULE_DESCRIPTION("Lustre helper library");
814 MODULE_VERSION(LIBCFS_VERSION);
815 MODULE_LICENSE("GPL");
816
817 module_init(libcfs_init);
818 module_exit(libcfs_exit);