Whamcloud - gitweb
LU-12275 sec: add llcrypt as file encryption library
[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 <libcfs/crypto/llcrypt.h>
58 #include "tracefile.h"
59
60 static struct dentry *lnet_debugfs_root;
61
62 BLOCKING_NOTIFIER_HEAD(libcfs_ioctl_list);
63 EXPORT_SYMBOL(libcfs_ioctl_list);
64
65 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
66 {
67         size_t len = sizeof(*data);
68
69         len += (data->ioc_inllen1 + 7) & ~7;
70         len += (data->ioc_inllen2 + 7) & ~7;
71         return len;
72 }
73
74 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
75 {
76         if (data->ioc_hdr.ioc_len > BIT(30))
77                 return true;
78
79         if (data->ioc_inllen1 > BIT(30))
80                 return true;
81
82         if (data->ioc_inllen2 > BIT(30))
83                 return true;
84
85         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
86                 return true;
87
88         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
89                 return true;
90
91         if (data->ioc_pbuf1 && !data->ioc_plen1)
92                 return true;
93
94         if (data->ioc_pbuf2 && !data->ioc_plen2)
95                 return true;
96
97         if (data->ioc_plen1 && !data->ioc_pbuf1)
98                 return true;
99
100         if (data->ioc_plen2 && !data->ioc_pbuf2)
101                 return true;
102
103         if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
104                 return true;
105
106         if (data->ioc_inllen1 &&
107                 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
108                                data->ioc_inllen2 - 1] != '\0')
109                 return true;
110
111         return false;
112 }
113
114 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
115 {
116         ENTRY;
117
118         if (libcfs_ioctl_is_invalid(data)) {
119                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
120                 RETURN(-EINVAL);
121         }
122
123         if (data->ioc_inllen1 != 0)
124                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
125
126         if (data->ioc_inllen2 != 0)
127                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
128                                     cfs_size_round(data->ioc_inllen1);
129
130         RETURN(0);
131 }
132
133 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
134                          struct libcfs_ioctl_hdr __user *uhdr)
135 {
136         struct libcfs_ioctl_hdr hdr;
137         int err;
138
139         ENTRY;
140         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
141                 RETURN(-EFAULT);
142
143         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
144             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
145                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
146                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
147                 RETURN(-EINVAL);
148         }
149
150         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
151                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
152                 RETURN(-EINVAL);
153         }
154
155         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
156                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
157                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
158                 RETURN(-EINVAL);
159         }
160
161         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
162         if (*hdr_pp == NULL)
163                 RETURN(-ENOMEM);
164
165         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
166                 GOTO(free, err = -EFAULT);
167
168         if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
169                 (*hdr_pp)->ioc_len != hdr.ioc_len) {
170                 GOTO(free, err = -EINVAL);
171         }
172
173         RETURN(0);
174
175 free:
176         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
177         RETURN(err);
178 }
179
180 static int libcfs_ioctl(unsigned long cmd, void __user *uparam)
181 {
182         struct libcfs_ioctl_data *data = NULL;
183         struct libcfs_ioctl_hdr  *hdr;
184         int                       err;
185         ENTRY;
186
187         /* 'cmd' and permissions get checked in our arch-specific caller */
188         err = libcfs_ioctl_getdata(&hdr, uparam);
189         if (err != 0) {
190                 CDEBUG_LIMIT(D_ERROR,
191                              "libcfs ioctl: data header error %d\n", err);
192                 RETURN(err);
193         }
194
195         if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
196                 /* The libcfs_ioctl_data_adjust() function performs adjustment
197                  * operations on the libcfs_ioctl_data structure to make
198                  * it usable by the code.  This doesn't need to be called
199                  * for new data structures added. */
200                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
201                 err = libcfs_ioctl_data_adjust(data);
202                 if (err != 0)
203                         GOTO(out, err);
204         }
205
206         CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
207         switch (cmd) {
208         case IOC_LIBCFS_CLEAR_DEBUG:
209                 libcfs_debug_clear_buffer();
210                 break;
211         case IOC_LIBCFS_MARK_DEBUG:
212                 if (data == NULL ||
213                     data->ioc_inlbuf1 == NULL ||
214                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
215                         GOTO(out, err = -EINVAL);
216
217                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
218                 break;
219
220         default:
221                 err = blocking_notifier_call_chain(&libcfs_ioctl_list,
222                                                    cmd, hdr);
223                 if (!(err & NOTIFY_STOP_MASK))
224                         /* No-one claimed the ioctl */
225                         err = -EINVAL;
226                 else
227                         err = notifier_to_errno(err);
228                 if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
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                 cfs_race_state = 1;
393                 wake_up(&cfs_race_waitq);
394         }
395         return rc;
396 }
397
398 static int __proc_cpt_table(void *data, int write,
399                             loff_t pos, void __user *buffer, int nob)
400 {
401         char *buf = NULL;
402         int   len = 4096;
403         int   rc  = 0;
404
405         if (write)
406                 return -EPERM;
407
408         LASSERT(cfs_cpt_tab);
409
410         while (1) {
411                 LIBCFS_ALLOC(buf, len);
412                 if (buf == NULL)
413                         return -ENOMEM;
414
415                 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
416                 if (rc >= 0)
417                         break;
418
419                 if (rc == -EFBIG) {
420                         LIBCFS_FREE(buf, len);
421                         len <<= 1;
422                         continue;
423                 }
424                 goto out;
425         }
426
427         if (pos >= rc) {
428                 rc = 0;
429                 goto out;
430         }
431
432         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
433 out:
434         if (buf != NULL)
435                 LIBCFS_FREE(buf, len);
436         return rc;
437 }
438
439 static int proc_cpt_table(struct ctl_table *table, int write,
440                           void __user *buffer, size_t *lenp, loff_t *ppos)
441 {
442         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
443                                     __proc_cpt_table);
444 }
445
446 static int __proc_cpt_distance(void *data, int write,
447                                loff_t pos, void __user *buffer, int nob)
448 {
449         char *buf = NULL;
450         int   len = 4096;
451         int   rc  = 0;
452
453         if (write)
454                 return -EPERM;
455
456         LASSERT(cfs_cpt_tab);
457
458         while (1) {
459                 LIBCFS_ALLOC(buf, len);
460                 if (buf == NULL)
461                         return -ENOMEM;
462
463                 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
464                 if (rc >= 0)
465                         break;
466
467                 if (rc == -EFBIG) {
468                         LIBCFS_FREE(buf, len);
469                         len <<= 1;
470                         continue;
471                 }
472                 goto out;
473         }
474
475         if (pos >= rc) {
476                 rc = 0;
477                 goto out;
478         }
479
480         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
481  out:
482         if (buf != NULL)
483                 LIBCFS_FREE(buf, len);
484         return rc;
485 }
486
487 static int proc_cpt_distance(struct ctl_table *table, int write,
488                              void __user *buffer, size_t *lenp, loff_t *ppos)
489 {
490         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
491                                      __proc_cpt_distance);
492 }
493
494 static struct ctl_table lnet_table[] = {
495         {
496                 .procname       = "debug",
497                 .data           = &libcfs_debug,
498                 .maxlen         = sizeof(int),
499                 .mode           = 0644,
500                 .proc_handler   = &proc_dobitmasks,
501         },
502         {
503                 .procname       = "subsystem_debug",
504                 .data           = &libcfs_subsystem_debug,
505                 .maxlen         = sizeof(int),
506                 .mode           = 0644,
507                 .proc_handler   = &proc_dobitmasks,
508         },
509         {
510                 .procname       = "printk",
511                 .data           = &libcfs_printk,
512                 .maxlen         = sizeof(int),
513                 .mode           = 0644,
514                 .proc_handler   = &proc_dobitmasks,
515         },
516         {
517                 .procname       = "cpu_partition_table",
518                 .maxlen         = 128,
519                 .mode           = 0444,
520                 .proc_handler   = &proc_cpt_table,
521         },
522         {
523                 .procname       = "cpu_partition_distance",
524                 .maxlen         = 128,
525                 .mode           = 0444,
526                 .proc_handler   = &proc_cpt_distance,
527         },
528         {
529                 .procname       = "debug_log_upcall",
530                 .data           = lnet_debug_log_upcall,
531                 .maxlen         = sizeof(lnet_debug_log_upcall),
532                 .mode           = 0644,
533                 .proc_handler   = &proc_dostring,
534         },
535         {
536                 .procname       = "lnet_memused",
537                 .data           = (int *)&libcfs_kmemory.counter,
538                 .maxlen         = sizeof(int),
539                 .mode           = 0444,
540                 .proc_handler   = &proc_dointvec,
541         },
542         {
543                 .procname       = "catastrophe",
544                 .data           = &libcfs_catastrophe,
545                 .maxlen         = sizeof(int),
546                 .mode           = 0444,
547                 .proc_handler   = &proc_dointvec,
548         },
549         {
550                 .procname       = "dump_kernel",
551                 .maxlen         = 256,
552                 .mode           = 0200,
553                 .proc_handler   = &proc_dump_kernel,
554         },
555         {
556                 .procname       = "daemon_file",
557                 .mode           = 0644,
558                 .maxlen         = 256,
559                 .proc_handler   = &proc_daemon_file,
560         },
561         {
562                 .procname       = "watchdog_ratelimit",
563                 .data           = &libcfs_watchdog_ratelimit,
564                 .maxlen         = sizeof(int),
565                 .mode           = 0644,
566                 .proc_handler   = &proc_dointvec_minmax,
567                 .extra1         = &min_watchdog_ratelimit,
568                 .extra2         = &max_watchdog_ratelimit,
569         },
570         {
571                 .procname       = "force_lbug",
572                 .data           = NULL,
573                 .maxlen         = 0,
574                 .mode           = 0200,
575                 .proc_handler   = &libcfs_force_lbug
576         },
577         {
578                 .procname       = "fail_loc",
579                 .data           = &cfs_fail_loc,
580                 .maxlen         = sizeof(cfs_fail_loc),
581                 .mode           = 0644,
582                 .proc_handler   = &proc_fail_loc
583         },
584         {
585                 .procname       = "fail_val",
586                 .data           = &cfs_fail_val,
587                 .maxlen         = sizeof(int),
588                 .mode           = 0644,
589                 .proc_handler   = &proc_dointvec
590         },
591         {
592                 .procname       = "fail_err",
593                 .data           = &cfs_fail_err,
594                 .maxlen         = sizeof(cfs_fail_err),
595                 .mode           = 0644,
596                 .proc_handler   = &proc_dointvec,
597         },
598         {
599         }
600 };
601
602 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
603         { .name         = "console_ratelimit",
604           .target       = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
605         { .name         = "debug_path",
606           .target       = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
607         { .name         = "panic_on_lbug",
608           .target       = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
609         { .name         = "console_backoff",
610           .target       = "../../../module/libcfs/parameters/libcfs_console_backoff" },
611         { .name         = "debug_mb",
612           .target       = "../../../module/libcfs/parameters/libcfs_debug_mb" },
613         { .name         = "console_min_delay_centisecs",
614           .target       = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
615         { .name         = "console_max_delay_centisecs",
616           .target       = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
617         { .name         = NULL },
618 };
619
620 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
621                                  size_t count, loff_t *ppos)
622 {
623         struct ctl_table *table = filp->private_data;
624         ssize_t rc = -EINVAL;
625
626         if (table) {
627                 rc = table->proc_handler(table, 0, buf, &count, ppos);
628                 if (!rc)
629                         rc = count;
630         }
631
632         return rc;
633 }
634
635 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
636                                   size_t count, loff_t *ppos)
637 {
638         struct ctl_table *table = filp->private_data;
639         ssize_t rc = -EINVAL;
640
641         if (table) {
642                 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
643                                          ppos);
644                 if (!rc)
645                         rc = count;
646         }
647
648         return rc;
649 }
650
651 static const struct file_operations lnet_debugfs_file_operations_rw = {
652         .open           = simple_open,
653         .read           = lnet_debugfs_read,
654         .write          = lnet_debugfs_write,
655         .llseek         = default_llseek,
656 };
657
658 static const struct file_operations lnet_debugfs_file_operations_ro = {
659         .open           = simple_open,
660         .read           = lnet_debugfs_read,
661         .llseek         = default_llseek,
662 };
663
664 static const struct file_operations lnet_debugfs_file_operations_wo = {
665         .open           = simple_open,
666         .write          = lnet_debugfs_write,
667         .llseek         = default_llseek,
668 };
669
670 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
671 {
672         if (!(mode & S_IWUGO))
673                 return &lnet_debugfs_file_operations_ro;
674
675         if (!(mode & S_IRUGO))
676                 return &lnet_debugfs_file_operations_wo;
677
678         return &lnet_debugfs_file_operations_rw;
679 }
680
681 void lnet_insert_debugfs(struct ctl_table *table)
682 {
683         if (!lnet_debugfs_root)
684                 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
685
686         /* Even if we cannot create, just ignore it altogether) */
687         if (IS_ERR_OR_NULL(lnet_debugfs_root))
688                 return;
689
690         /* We don't save the dentry returned in next two calls, because
691          * we don't call debugfs_remove() but rather remove_recursive()
692          */
693         for (; table && table->procname; table++)
694                 debugfs_create_file(table->procname, table->mode,
695                                     lnet_debugfs_root, table,
696                                     lnet_debugfs_fops_select(table->mode));
697 }
698 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
699
700 static void lnet_insert_debugfs_links(
701                 const struct lnet_debugfs_symlink_def *symlinks)
702 {
703         for (; symlinks && symlinks->name; symlinks++)
704                 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
705                                        symlinks->target);
706 }
707
708 void lnet_remove_debugfs(struct ctl_table *table)
709 {
710         for (; table && table->procname; table++) {
711                 struct qstr dname = QSTR_INIT(table->procname,
712                                               strlen(table->procname));
713                 struct dentry *dentry;
714
715                 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
716                 debugfs_remove(dentry);
717         }
718 }
719 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
720
721 static int __init libcfs_init(void)
722 {
723         int rc;
724
725         cfs_arch_init();
726
727         rc = libcfs_debug_init(5 * 1024 * 1024);
728         if (rc < 0) {
729                 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
730                 return (rc);
731         }
732
733         rc = cfs_cpu_init();
734         if (rc != 0)
735                 goto cleanup_debug;
736
737         rc = misc_register(&libcfs_dev);
738         if (rc) {
739                 CERROR("misc_register: error %d\n", rc);
740                 goto cleanup_cpu;
741         }
742
743         rc = cfs_wi_startup();
744         if (rc) {
745                 CERROR("initialize workitem: error %d\n", rc);
746                 goto cleanup_deregister;
747         }
748
749         cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
750         if (!cfs_rehash_wq) {
751                 rc = -ENOMEM;
752                 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
753                        rc);
754                 goto cleanup_deregister;
755         }
756
757         rc = cfs_crypto_register();
758         if (rc) {
759                 CERROR("cfs_crypto_regster: error %d\n", rc);
760                 goto cleanup_wi;
761         }
762
763         lnet_insert_debugfs(lnet_table);
764         if (!IS_ERR_OR_NULL(lnet_debugfs_root))
765                 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
766
767         rc = llcrypt_init();
768         if (rc) {
769                 CERROR("llcrypt_init: error %d\n", rc);
770                 goto cleanup_wi;
771         }
772
773         CDEBUG (D_OTHER, "portals setup OK\n");
774         return 0;
775 cleanup_wi:
776         cfs_wi_shutdown();
777 cleanup_deregister:
778         misc_deregister(&libcfs_dev);
779 cleanup_cpu:
780         cfs_cpu_fini();
781 cleanup_debug:
782         libcfs_debug_cleanup();
783         return rc;
784 }
785
786 static void __exit libcfs_exit(void)
787 {
788         int rc;
789
790         /* Remove everthing */
791         debugfs_remove_recursive(lnet_debugfs_root);
792         lnet_debugfs_root = NULL;
793
794         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
795                atomic_read(&libcfs_kmemory));
796
797         llcrypt_exit();
798
799         if (cfs_rehash_wq) {
800                 destroy_workqueue(cfs_rehash_wq);
801                 cfs_rehash_wq = NULL;
802         }
803
804         cfs_crypto_unregister();
805         cfs_wi_shutdown();
806
807         misc_deregister(&libcfs_dev);
808
809         cfs_cpu_fini();
810
811         /* the below message is checked in test-framework.sh check_mem_leak() */
812         if (atomic_read(&libcfs_kmemory) != 0)
813                 CERROR("Portals memory leaked: %d bytes\n",
814                        atomic_read(&libcfs_kmemory));
815
816         rc = libcfs_debug_cleanup();
817         if (rc)
818                 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
819 }
820
821 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
822 MODULE_DESCRIPTION("Lustre helper library");
823 MODULE_VERSION(LIBCFS_VERSION);
824 MODULE_LICENSE("GPL");
825
826 module_init(libcfs_init);
827 module_exit(libcfs_exit);