Whamcloud - gitweb
LU-14428 libcfs: discard cfs_trace_allocate_string_buffer
[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 = NULL;
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         if (!write) {
297                 tmpstr = kmalloc(tmpstrlen, GFP_KERNEL | __GFP_ZERO);
298                 if (!tmpstr)
299                         return -ENOMEM;
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                 tmpstr = memdup_user_nul(buffer, nob);
311                 if (!tmpstr)
312                         return -ENOMEM;
313
314                 rc = libcfs_debug_str2mask(mask, strim(tmpstr), is_subsys);
315                 /* Always print LBUG/LASSERT to console, so keep this mask */
316                 if (is_printk)
317                         *mask |= D_EMERG;
318         }
319
320         kfree(tmpstr);
321         return rc;
322 }
323
324 static int proc_dobitmasks(struct ctl_table *table, int write,
325                            void __user *buffer, size_t *lenp, loff_t *ppos)
326 {
327         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
328                                     __proc_dobitmasks);
329 }
330
331 static int min_watchdog_ratelimit;              /* disable ratelimiting */
332 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
333
334 static int __proc_dump_kernel(void *data, int write,
335                               loff_t pos, void __user *buffer, int nob)
336 {
337         if (!write)
338                 return 0;
339
340         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
341 }
342
343 static int proc_dump_kernel(struct ctl_table *table, int write,
344                             void __user *buffer, size_t *lenp, loff_t *ppos)
345 {
346         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
347                                     __proc_dump_kernel);
348 }
349
350 static int __proc_daemon_file(void *data, int write,
351                               loff_t pos, void __user *buffer, int nob)
352 {
353         if (!write) {
354                 int len = strlen(cfs_tracefile);
355
356                 if (pos >= len)
357                         return 0;
358
359                 return cfs_trace_copyout_string(buffer, nob,
360                                                 cfs_tracefile + pos, "\n");
361         }
362
363         return cfs_trace_daemon_command_usrstr(buffer, nob);
364 }
365
366 static int proc_daemon_file(struct ctl_table *table, int write,
367                             void __user *buffer, size_t *lenp, loff_t *ppos)
368 {
369         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
370                                     __proc_daemon_file);
371 }
372
373 static int libcfs_force_lbug(struct ctl_table *table, int write,
374                              void __user *buffer,
375                              size_t *lenp, loff_t *ppos)
376 {
377         if (write)
378                 LBUG();
379         return 0;
380 }
381
382 static int proc_fail_loc(struct ctl_table *table, int write,
383                          void __user *buffer, size_t *lenp, loff_t *ppos)
384 {
385         int rc;
386         long old_fail_loc = cfs_fail_loc;
387
388         if (!*lenp || *ppos) {
389                 *lenp = 0;
390                 return 0;
391         }
392
393         if (write) {
394                 char *kbuf = memdup_user_nul(buffer, *lenp);
395
396                 if (IS_ERR(kbuf))
397                         return PTR_ERR(kbuf);
398                 rc = kstrtoul(kbuf, 0, &cfs_fail_loc);
399                 kfree(kbuf);
400                 *ppos += *lenp;
401         } else {
402                 char kbuf[64/3+3];
403
404                 rc = scnprintf(kbuf, sizeof(kbuf), "%lu\n", cfs_fail_loc);
405                 if (copy_to_user(buffer, kbuf, rc))
406                         rc = -EFAULT;
407                 else {
408                         *lenp = rc;
409                         *ppos += rc;
410                 }
411         }
412
413         if (old_fail_loc != cfs_fail_loc) {
414                 cfs_race_state = 1;
415                 wake_up(&cfs_race_waitq);
416         }
417         return rc;
418 }
419
420 int debugfs_doint(struct ctl_table *table, int write,
421                   void __user *buffer, size_t *lenp, loff_t *ppos)
422 {
423         int rc;
424
425         if (!*lenp || *ppos) {
426                 *lenp = 0;
427                 return 0;
428         }
429
430         if (write) {
431                 char *kbuf = memdup_user_nul(buffer, *lenp);
432                 int val;
433
434                 if (IS_ERR(kbuf))
435                         return PTR_ERR(kbuf);
436
437                 rc = kstrtoint(kbuf, 0, &val);
438                 kfree(kbuf);
439                 if (!rc) {
440                         if (table->extra1 && val < *(int *)table->extra1)
441                                 val = *(int *)table->extra1;
442                         if (table->extra2 && val > *(int *)table->extra2)
443                                 val = *(int *)table->extra2;
444                         *(int *)table->data = val;
445                 }
446                 *ppos += *lenp;
447         } else {
448                 char kbuf[64/3+3];
449
450                 rc = scnprintf(kbuf, sizeof(kbuf), "%u\n", *(int *)table->data);
451                 if (copy_to_user(buffer, kbuf, rc))
452                         rc = -EFAULT;
453                 else {
454                         *lenp = rc;
455                         *ppos += rc;
456                 }
457         }
458
459         return rc;
460 }
461 EXPORT_SYMBOL(debugfs_doint);
462
463 static int debugfs_dou64(struct ctl_table *table, int write,
464                          void __user *buffer, size_t *lenp, loff_t *ppos)
465 {
466         int rc;
467
468         if (!*lenp || *ppos) {
469                 *lenp = 0;
470                 return 0;
471         }
472
473         if (write) {
474                 char *kbuf = memdup_user_nul(buffer, *lenp);
475                 unsigned long long val;
476
477                 if (IS_ERR(kbuf))
478                         return PTR_ERR(kbuf);
479
480                 rc = kstrtoull(kbuf, 0, &val);
481                 kfree(kbuf);
482                 if (!rc)
483                         *(u64 *)table->data = val;
484                 *ppos += *lenp;
485         } else {
486                 char kbuf[64/3+3];
487
488                 rc = scnprintf(kbuf, sizeof(kbuf), "%llu\n",
489                                (unsigned long long)*(u64 *)table->data);
490                 if (copy_to_user(buffer, kbuf, rc))
491                         rc = -EFAULT;
492                 else {
493                         *lenp = rc;
494                         *ppos += rc;
495                 }
496         }
497
498         return rc;
499 }
500
501 static int debugfs_dostring(struct ctl_table *table, int write,
502                             void __user *buffer, size_t *lenp, loff_t *ppos)
503 {
504         int len = *lenp;
505         char *kbuf = table->data;
506
507         if (!len || *ppos) {
508                 *lenp = 0;
509                 return 0;
510         }
511         if (len > table->maxlen)
512                 len = table->maxlen;
513         if (write) {
514                 if (copy_from_user(kbuf, buffer, len))
515                         return -EFAULT;
516                 memset(kbuf+len, 0, table->maxlen - len);
517                 *ppos = *lenp;
518         } else {
519                 len = strnlen(kbuf, len);
520                 if (copy_to_user(buffer, kbuf, len))
521                         return -EFAULT;
522                 if (len < *lenp) {
523                         if (copy_to_user(buffer+len, "\n", 1))
524                                 return -EFAULT;
525                         len += 1;
526                 }
527                 *ppos += len;
528                 *lenp -= len;
529         }
530         return len;
531 }
532
533 static int __proc_cpt_table(void *data, int write,
534                             loff_t pos, void __user *buffer, int nob)
535 {
536         char *buf = NULL;
537         int   len = 4096;
538         int   rc  = 0;
539
540         if (write)
541                 return -EPERM;
542
543         while (1) {
544                 LIBCFS_ALLOC(buf, len);
545                 if (buf == NULL)
546                         return -ENOMEM;
547
548                 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
549                 if (rc >= 0)
550                         break;
551
552                 if (rc == -EFBIG) {
553                         LIBCFS_FREE(buf, len);
554                         len <<= 1;
555                         continue;
556                 }
557                 goto out;
558         }
559
560         if (pos >= rc) {
561                 rc = 0;
562                 goto out;
563         }
564
565         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
566 out:
567         if (buf != NULL)
568                 LIBCFS_FREE(buf, len);
569         return rc;
570 }
571
572 static int proc_cpt_table(struct ctl_table *table, int write,
573                           void __user *buffer, size_t *lenp, loff_t *ppos)
574 {
575         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
576                                     __proc_cpt_table);
577 }
578
579 static int __proc_cpt_distance(void *data, int write,
580                                loff_t pos, void __user *buffer, int nob)
581 {
582         char *buf = NULL;
583         int   len = 4096;
584         int   rc  = 0;
585
586         if (write)
587                 return -EPERM;
588
589         while (1) {
590                 LIBCFS_ALLOC(buf, len);
591                 if (buf == NULL)
592                         return -ENOMEM;
593
594                 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
595                 if (rc >= 0)
596                         break;
597
598                 if (rc == -EFBIG) {
599                         LIBCFS_FREE(buf, len);
600                         len <<= 1;
601                         continue;
602                 }
603                 goto out;
604         }
605
606         if (pos >= rc) {
607                 rc = 0;
608                 goto out;
609         }
610
611         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
612  out:
613         if (buf != NULL)
614                 LIBCFS_FREE(buf, len);
615         return rc;
616 }
617
618 static int proc_cpt_distance(struct ctl_table *table, int write,
619                              void __user *buffer, size_t *lenp, loff_t *ppos)
620 {
621         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
622                                      __proc_cpt_distance);
623 }
624
625 static struct ctl_table lnet_table[] = {
626         {
627                 .procname       = "debug",
628                 .data           = &libcfs_debug,
629                 .maxlen         = sizeof(int),
630                 .mode           = 0644,
631                 .proc_handler   = &proc_dobitmasks,
632         },
633         {
634                 .procname       = "subsystem_debug",
635                 .data           = &libcfs_subsystem_debug,
636                 .maxlen         = sizeof(int),
637                 .mode           = 0644,
638                 .proc_handler   = &proc_dobitmasks,
639         },
640         {
641                 .procname       = "printk",
642                 .data           = &libcfs_printk,
643                 .maxlen         = sizeof(int),
644                 .mode           = 0644,
645                 .proc_handler   = &proc_dobitmasks,
646         },
647         {
648                 .procname       = "cpu_partition_table",
649                 .maxlen         = 128,
650                 .mode           = 0444,
651                 .proc_handler   = &proc_cpt_table,
652         },
653         {
654                 .procname       = "cpu_partition_distance",
655                 .maxlen         = 128,
656                 .mode           = 0444,
657                 .proc_handler   = &proc_cpt_distance,
658         },
659         {
660                 .procname       = "debug_log_upcall",
661                 .data           = lnet_debug_log_upcall,
662                 .maxlen         = sizeof(lnet_debug_log_upcall),
663                 .mode           = 0644,
664                 .proc_handler   = &debugfs_dostring,
665         },
666         {
667                 .procname       = "lnet_memused",
668                 .data           = (u64 *)&libcfs_kmem.counter,
669                 .maxlen         = sizeof(u64),
670                 .mode           = 0444,
671                 .proc_handler   = &debugfs_dou64,
672         },
673         {
674                 .procname       = "catastrophe",
675                 .data           = &libcfs_catastrophe,
676                 .maxlen         = sizeof(int),
677                 .mode           = 0444,
678                 .proc_handler   = &debugfs_doint,
679         },
680         {
681                 .procname       = "dump_kernel",
682                 .maxlen         = 256,
683                 .mode           = 0200,
684                 .proc_handler   = &proc_dump_kernel,
685         },
686         {
687                 .procname       = "daemon_file",
688                 .mode           = 0644,
689                 .maxlen         = 256,
690                 .proc_handler   = &proc_daemon_file,
691         },
692         {
693                 .procname       = "watchdog_ratelimit",
694                 .data           = &libcfs_watchdog_ratelimit,
695                 .maxlen         = sizeof(int),
696                 .mode           = 0644,
697                 .proc_handler   = &debugfs_doint,
698                 .extra1         = &min_watchdog_ratelimit,
699                 .extra2         = &max_watchdog_ratelimit,
700         },
701         {
702                 .procname       = "force_lbug",
703                 .data           = NULL,
704                 .maxlen         = 0,
705                 .mode           = 0200,
706                 .proc_handler   = &libcfs_force_lbug
707         },
708         {
709                 .procname       = "fail_loc",
710                 .data           = &cfs_fail_loc,
711                 .maxlen         = sizeof(cfs_fail_loc),
712                 .mode           = 0644,
713                 .proc_handler   = &proc_fail_loc
714         },
715         {
716                 .procname       = "fail_val",
717                 .data           = &cfs_fail_val,
718                 .maxlen         = sizeof(int),
719                 .mode           = 0644,
720                 .proc_handler   = &debugfs_doint
721         },
722         {
723                 .procname       = "fail_err",
724                 .data           = &cfs_fail_err,
725                 .maxlen         = sizeof(cfs_fail_err),
726                 .mode           = 0644,
727                 .proc_handler   = &debugfs_doint,
728         },
729         {
730         }
731 };
732
733 static const struct lnet_debugfs_symlink_def lnet_debugfs_symlinks[] = {
734         { .name         = "console_ratelimit",
735           .target       = "../../../module/libcfs/parameters/libcfs_console_ratelimit" },
736         { .name         = "debug_path",
737           .target       = "../../../module/libcfs/parameters/libcfs_debug_file_path" },
738         { .name         = "panic_on_lbug",
739           .target       = "../../../module/libcfs/parameters/libcfs_panic_on_lbug" },
740         { .name         = "console_backoff",
741           .target       = "../../../module/libcfs/parameters/libcfs_console_backoff" },
742         { .name         = "debug_mb",
743           .target       = "../../../module/libcfs/parameters/libcfs_debug_mb" },
744         { .name         = "console_min_delay_centisecs",
745           .target       = "../../../module/libcfs/parameters/libcfs_console_min_delay" },
746         { .name         = "console_max_delay_centisecs",
747           .target       = "../../../module/libcfs/parameters/libcfs_console_max_delay" },
748         { .name         = NULL },
749 };
750
751 static ssize_t lnet_debugfs_read(struct file *filp, char __user *buf,
752                                  size_t count, loff_t *ppos)
753 {
754         struct ctl_table *table = filp->private_data;
755         ssize_t rc = -EINVAL;
756
757         if (table) {
758                 rc = table->proc_handler(table, 0, buf, &count, ppos);
759                 if (!rc)
760                         rc = count;
761         }
762
763         return rc;
764 }
765
766 static ssize_t lnet_debugfs_write(struct file *filp, const char __user *buf,
767                                   size_t count, loff_t *ppos)
768 {
769         struct ctl_table *table = filp->private_data;
770         ssize_t rc = -EINVAL;
771
772         if (table) {
773                 rc = table->proc_handler(table, 1, (void __user *)buf, &count,
774                                          ppos);
775                 if (!rc)
776                         rc = count;
777         }
778
779         return rc;
780 }
781
782 static const struct file_operations lnet_debugfs_file_operations_rw = {
783         .open           = simple_open,
784         .read           = lnet_debugfs_read,
785         .write          = lnet_debugfs_write,
786         .llseek         = default_llseek,
787 };
788
789 static const struct file_operations lnet_debugfs_file_operations_ro = {
790         .open           = simple_open,
791         .read           = lnet_debugfs_read,
792         .llseek         = default_llseek,
793 };
794
795 static const struct file_operations lnet_debugfs_file_operations_wo = {
796         .open           = simple_open,
797         .write          = lnet_debugfs_write,
798         .llseek         = default_llseek,
799 };
800
801 static const struct file_operations *lnet_debugfs_fops_select(umode_t mode)
802 {
803         if (!(mode & S_IWUGO))
804                 return &lnet_debugfs_file_operations_ro;
805
806         if (!(mode & S_IRUGO))
807                 return &lnet_debugfs_file_operations_wo;
808
809         return &lnet_debugfs_file_operations_rw;
810 }
811
812 void lnet_insert_debugfs(struct ctl_table *table)
813 {
814         if (!lnet_debugfs_root)
815                 lnet_debugfs_root = debugfs_create_dir("lnet", NULL);
816
817         /* Even if we cannot create, just ignore it altogether) */
818         if (IS_ERR_OR_NULL(lnet_debugfs_root))
819                 return;
820
821         /* We don't save the dentry returned in next two calls, because
822          * we don't call debugfs_remove() but rather remove_recursive()
823          */
824         for (; table && table->procname; table++)
825                 debugfs_create_file(table->procname, table->mode,
826                                     lnet_debugfs_root, table,
827                                     lnet_debugfs_fops_select(table->mode));
828 }
829 EXPORT_SYMBOL_GPL(lnet_insert_debugfs);
830
831 static void lnet_insert_debugfs_links(
832                 const struct lnet_debugfs_symlink_def *symlinks)
833 {
834         for (; symlinks && symlinks->name; symlinks++)
835                 debugfs_create_symlink(symlinks->name, lnet_debugfs_root,
836                                        symlinks->target);
837 }
838
839 void lnet_remove_debugfs(struct ctl_table *table)
840 {
841         for (; table && table->procname; table++) {
842                 struct qstr dname = QSTR_INIT(table->procname,
843                                               strlen(table->procname));
844                 struct dentry *dentry;
845
846                 dentry = d_hash_and_lookup(lnet_debugfs_root, &dname);
847                 debugfs_remove(dentry);
848         }
849 }
850 EXPORT_SYMBOL_GPL(lnet_remove_debugfs);
851
852 static int __init libcfs_init(void)
853 {
854         int rc;
855
856         cfs_arch_init();
857
858         init_libcfs_vfree_atomic();
859
860         rc = libcfs_debug_init(5 * 1024 * 1024);
861         if (rc < 0) {
862                 pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc);
863                 return (rc);
864         }
865
866         cfs_debug_init();
867
868         rc = cfs_cpu_init();
869         if (rc != 0)
870                 goto cleanup_debug;
871
872         rc = misc_register(&libcfs_dev);
873         if (rc) {
874                 CERROR("misc_register: error %d\n", rc);
875                 goto cleanup_cpu;
876         }
877
878         rc = cfs_wi_startup();
879         if (rc) {
880                 CERROR("initialize workitem: error %d\n", rc);
881                 goto cleanup_deregister;
882         }
883
884         cfs_rehash_wq = alloc_workqueue("cfs_rh", WQ_SYSFS, 4);
885         if (!cfs_rehash_wq) {
886                 rc = -ENOMEM;
887                 CERROR("libcfs: failed to start rehash workqueue: rc = %d\n",
888                        rc);
889                 goto cleanup_deregister;
890         }
891
892         rc = cfs_crypto_register();
893         if (rc) {
894                 CERROR("cfs_crypto_regster: error %d\n", rc);
895                 goto cleanup_wi;
896         }
897
898         lnet_insert_debugfs(lnet_table);
899         if (!IS_ERR_OR_NULL(lnet_debugfs_root))
900                 lnet_insert_debugfs_links(lnet_debugfs_symlinks);
901
902         rc = llcrypt_init();
903         if (rc) {
904                 CERROR("llcrypt_init: error %d\n", rc);
905                 goto cleanup_wi;
906         }
907
908         CDEBUG (D_OTHER, "portals setup OK\n");
909         return 0;
910 cleanup_wi:
911         cfs_wi_shutdown();
912 cleanup_deregister:
913         misc_deregister(&libcfs_dev);
914 cleanup_cpu:
915         cfs_cpu_fini();
916 cleanup_debug:
917         libcfs_debug_cleanup();
918         return rc;
919 }
920
921 static void __exit libcfs_exit(void)
922 {
923         int rc;
924
925         /* Remove everthing */
926         debugfs_remove_recursive(lnet_debugfs_root);
927         lnet_debugfs_root = NULL;
928
929         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %lld\n",
930                libcfs_kmem_read());
931
932         llcrypt_exit();
933
934         if (cfs_rehash_wq) {
935                 destroy_workqueue(cfs_rehash_wq);
936                 cfs_rehash_wq = NULL;
937         }
938
939         cfs_crypto_unregister();
940         cfs_wi_shutdown();
941
942         misc_deregister(&libcfs_dev);
943
944         cfs_cpu_fini();
945
946         /* the below message is checked in test-framework.sh check_mem_leak() */
947         if (libcfs_kmem_read() != 0)
948                 CERROR("Portals memory leaked: %lld bytes\n",
949                        libcfs_kmem_read());
950
951         rc = libcfs_debug_cleanup();
952         if (rc)
953                 pr_err("LustreError: libcfs_debug_cleanup: rc = %d\n", rc);
954
955         exit_libcfs_vfree_atomic();
956 }
957
958 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
959 MODULE_DESCRIPTION("Lustre helper library");
960 MODULE_VERSION(LIBCFS_VERSION);
961 MODULE_LICENSE("GPL");
962
963 module_init(libcfs_init);
964 module_exit(libcfs_exit);