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