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