Whamcloud - gitweb
LU-1884 build: fix 'resource leak' errors
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-proc.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-proc.c
37  *
38  * Author: Zach Brown <zab@zabbo.net>
39  * Author: Peter J. Braam <braam@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/unistd.h>
50 #include <net/sock.h>
51 #include <linux/uio.h>
52
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55
56 #include <linux/fs.h>
57 #include <linux/file.h>
58 #include <linux/stat.h>
59 #include <linux/list.h>
60 #include <asm/uaccess.h>
61
62 #include <linux/proc_fs.h>
63 #include <linux/sysctl.h>
64
65 # define DEBUG_SUBSYSTEM S_LNET
66
67 #include <libcfs/libcfs.h>
68 #include <asm/div64.h>
69 #include "tracefile.h"
70
71 #ifdef CONFIG_SYSCTL
72 static cfs_sysctl_table_header_t *lnet_table_header = NULL;
73 #endif
74 extern char lnet_upcall[1024];
75 /**
76  * The path of debug log dump upcall script.
77  */
78 extern char lnet_debug_log_upcall[1024];
79
80 #ifndef HAVE_SYSCTL_UNNUMBERED
81 #define CTL_LNET        (0x100)
82 enum {
83         PSDEV_DEBUG = 1,          /* control debugging */
84         PSDEV_SUBSYSTEM_DEBUG,    /* control debugging */
85         PSDEV_PRINTK,             /* force all messages to console */
86         PSDEV_CONSOLE_RATELIMIT,  /* ratelimit console messages */
87         PSDEV_CONSOLE_MAX_DELAY_CS, /* maximum delay over which we skip messages */
88         PSDEV_CONSOLE_MIN_DELAY_CS, /* initial delay over which we skip messages */
89         PSDEV_CONSOLE_BACKOFF,    /* delay increase factor */
90         PSDEV_DEBUG_PATH,         /* crashdump log location */
91         PSDEV_DEBUG_DUMP_PATH,    /* crashdump tracelog location */
92         PSDEV_CPT_TABLE,          /* information about cpu partitions */
93         PSDEV_LNET_UPCALL,        /* User mode upcall script  */
94         PSDEV_LNET_MEMUSED,       /* bytes currently PORTAL_ALLOCated */
95         PSDEV_LNET_CATASTROPHE,   /* if we have LBUGged or panic'd */
96         PSDEV_LNET_PANIC_ON_LBUG, /* flag to panic on LBUG */
97         PSDEV_LNET_DUMP_KERNEL,   /* snapshot kernel debug buffer to file */
98         PSDEV_LNET_DAEMON_FILE,   /* spool kernel debug buffer to file */
99         PSDEV_LNET_DEBUG_MB,      /* size of debug buffer */
100         PSDEV_LNET_DEBUG_LOG_UPCALL, /* debug log upcall script */
101         PSDEV_LNET_WATCHDOG_RATELIMIT,  /* ratelimit watchdog messages  */
102         PSDEV_LNET_FORCE_LBUG,    /* hook to force an LBUG */
103         PSDEV_LNET_FAIL_LOC,      /* control test failures instrumentation */
104         PSDEV_LNET_FAIL_VAL,      /* userdata for fail loc */
105 };
106 #else
107 #define CTL_LNET                        CTL_UNNUMBERED
108 #define PSDEV_DEBUG                     CTL_UNNUMBERED
109 #define PSDEV_SUBSYSTEM_DEBUG           CTL_UNNUMBERED
110 #define PSDEV_PRINTK                    CTL_UNNUMBERED
111 #define PSDEV_CONSOLE_RATELIMIT         CTL_UNNUMBERED
112 #define PSDEV_CONSOLE_MAX_DELAY_CS      CTL_UNNUMBERED
113 #define PSDEV_CONSOLE_MIN_DELAY_CS      CTL_UNNUMBERED
114 #define PSDEV_CONSOLE_BACKOFF           CTL_UNNUMBERED
115 #define PSDEV_DEBUG_PATH                CTL_UNNUMBERED
116 #define PSDEV_DEBUG_DUMP_PATH           CTL_UNNUMBERED
117 #define PSDEV_CPT_TABLE                 CTL_UNNUMBERED
118 #define PSDEV_LNET_UPCALL               CTL_UNNUMBERED
119 #define PSDEV_LNET_MEMUSED              CTL_UNNUMBERED
120 #define PSDEV_LNET_CATASTROPHE          CTL_UNNUMBERED
121 #define PSDEV_LNET_PANIC_ON_LBUG        CTL_UNNUMBERED
122 #define PSDEV_LNET_DUMP_KERNEL          CTL_UNNUMBERED
123 #define PSDEV_LNET_DAEMON_FILE          CTL_UNNUMBERED
124 #define PSDEV_LNET_DEBUG_MB             CTL_UNNUMBERED
125 #define PSDEV_LNET_DEBUG_LOG_UPCALL     CTL_UNNUMBERED
126 #define PSDEV_LNET_WATCHDOG_RATELIMIT   CTL_UNNUMBERED
127 #define PSDEV_LNET_FORCE_LBUG           CTL_UNNUMBERED
128 #define PSDEV_LNET_FAIL_LOC             CTL_UNNUMBERED
129 #define PSDEV_LNET_FAIL_VAL             CTL_UNNUMBERED
130 #endif
131
132 int
133 proc_call_handler(void *data, int write,
134                   loff_t *ppos, void *buffer, size_t *lenp,
135                   int (*handler)(void *data, int write,
136                                  loff_t pos, void *buffer, int len))
137 {
138         int rc = handler(data, write, *ppos, buffer, *lenp);
139
140         if (rc < 0)
141                 return rc;
142
143         if (write) {
144                 *ppos += *lenp;
145         } else {
146                 *lenp = rc;
147                 *ppos += rc;
148         }
149         return 0;
150 }
151 EXPORT_SYMBOL(proc_call_handler);
152
153 static int __proc_dobitmasks(void *data, int write,
154                              loff_t pos, void *buffer, int nob)
155 {
156         const int     tmpstrlen = 512;
157         char         *tmpstr;
158         int           rc;
159         unsigned int *mask = data;
160         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
161         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
162
163         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
164         if (rc < 0)
165                 return rc;
166
167         if (!write) {
168                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
169                 rc = strlen(tmpstr);
170
171                 if (pos >= rc) {
172                         rc = 0;
173                 } else {
174                         rc = cfs_trace_copyout_string(buffer, nob,
175                                                       tmpstr + pos, "\n");
176                 }
177         } else {
178                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
179                 if (rc < 0) {
180                         cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
181                         return rc;
182                 }
183
184                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
185                 /* Always print LBUG/LASSERT to console, so keep this mask */
186                 if (is_printk)
187                         *mask |= D_EMERG;
188         }
189
190         cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
191         return rc;
192 }
193
194 DECLARE_PROC_HANDLER(proc_dobitmasks)
195
196 static int min_watchdog_ratelimit = 0;          /* disable ratelimiting */
197 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
198
199 static int __proc_dump_kernel(void *data, int write,
200                               loff_t pos, void *buffer, int nob)
201 {
202         if (!write)
203                 return 0;
204
205         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
206 }
207
208 DECLARE_PROC_HANDLER(proc_dump_kernel)
209
210 static int __proc_daemon_file(void *data, int write,
211                               loff_t pos, void *buffer, int nob)
212 {
213         if (!write) {
214                 int len = strlen(cfs_tracefile);
215
216                 if (pos >= len)
217                         return 0;
218
219                 return cfs_trace_copyout_string(buffer, nob,
220                                                 cfs_tracefile + pos, "\n");
221         }
222
223         return cfs_trace_daemon_command_usrstr(buffer, nob);
224 }
225
226 DECLARE_PROC_HANDLER(proc_daemon_file)
227
228 static int __proc_debug_mb(void *data, int write,
229                            loff_t pos, void *buffer, int nob)
230 {
231         if (!write) {
232                 char tmpstr[32];
233                 int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
234                                     cfs_trace_get_debug_mb());
235
236                 if (pos >= len)
237                         return 0;
238
239                 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
240                        "\n");
241         }
242
243         return cfs_trace_set_debug_mb_usrstr(buffer, nob);
244 }
245
246 DECLARE_PROC_HANDLER(proc_debug_mb)
247
248 int LL_PROC_PROTO(proc_console_max_delay_cs)
249 {
250         int rc, max_delay_cs;
251         cfs_sysctl_table_t dummy = *table;
252         cfs_duration_t d;
253
254         dummy.data = &max_delay_cs;
255         dummy.proc_handler = &proc_dointvec;
256
257         if (!write) { /* read */
258                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
259                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
260                 return rc;
261         }
262
263         /* write */
264         max_delay_cs = 0;
265         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
266         if (rc < 0)
267                 return rc;
268         if (max_delay_cs <= 0)
269                 return -EINVAL;
270
271         d = cfs_time_seconds(max_delay_cs) / 100;
272         if (d == 0 || d < libcfs_console_min_delay)
273                 return -EINVAL;
274         libcfs_console_max_delay = d;
275
276         return rc;
277 }
278
279 int LL_PROC_PROTO(proc_console_min_delay_cs)
280 {
281         int rc, min_delay_cs;
282         cfs_sysctl_table_t dummy = *table;
283         cfs_duration_t d;
284
285         dummy.data = &min_delay_cs;
286         dummy.proc_handler = &proc_dointvec;
287
288         if (!write) { /* read */
289                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
290                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
291                 return rc;
292         }
293
294         /* write */
295         min_delay_cs = 0;
296         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
297         if (rc < 0)
298                 return rc;
299         if (min_delay_cs <= 0)
300                 return -EINVAL;
301
302         d = cfs_time_seconds(min_delay_cs) / 100;
303         if (d == 0 || d > libcfs_console_max_delay)
304                 return -EINVAL;
305         libcfs_console_min_delay = d;
306
307         return rc;
308 }
309
310 int LL_PROC_PROTO(proc_console_backoff)
311 {
312         int rc, backoff;
313         cfs_sysctl_table_t dummy = *table;
314
315         dummy.data = &backoff;
316         dummy.proc_handler = &proc_dointvec;
317
318         if (!write) { /* read */
319                 backoff= libcfs_console_backoff;
320                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
321                 return rc;
322         }
323
324         /* write */
325         backoff = 0;
326         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
327         if (rc < 0)
328                 return rc;
329         if (backoff <= 0)
330                 return -EINVAL;
331
332         libcfs_console_backoff = backoff;
333
334         return rc;
335 }
336
337 int LL_PROC_PROTO(libcfs_force_lbug)
338 {
339         if (write)
340                 LBUG();
341         return 0;
342 }
343
344 int LL_PROC_PROTO(proc_fail_loc)
345 {
346         int rc;
347         long old_fail_loc = cfs_fail_loc;
348
349         rc = ll_proc_dolongvec(table, write, filp, buffer, lenp, ppos);
350         if (old_fail_loc != cfs_fail_loc)
351                 cfs_waitq_signal(&cfs_race_waitq);
352         return rc;
353 }
354
355 static int __proc_cpt_table(void *data, int write,
356                             loff_t pos, void *buffer, int nob)
357 {
358         char *buf = NULL;
359         int   len = 4096;
360         int   rc  = 0;
361
362         if (write)
363                 return -EPERM;
364
365         LASSERT(cfs_cpt_table != NULL);
366
367         while (1) {
368                 LIBCFS_ALLOC(buf, len);
369                 if (buf == NULL)
370                         return -ENOMEM;
371
372                 rc = cfs_cpt_table_print(cfs_cpt_table, buf, len);
373                 if (rc >= 0)
374                         break;
375
376                 LIBCFS_FREE(buf, len);
377                 if (rc == -EFBIG) {
378                         len <<= 1;
379                         continue;
380                 }
381                 goto out;
382         }
383
384         if (pos >= rc) {
385                 rc = 0;
386                 goto out;
387         }
388
389         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
390  out:
391         if (buf != NULL)
392                 LIBCFS_FREE(buf, len);
393         return rc;
394 }
395 DECLARE_PROC_HANDLER(proc_cpt_table)
396
397 static cfs_sysctl_table_t lnet_table[] = {
398         /*
399          * NB No .strategy entries have been provided since sysctl(8) prefers
400          * to go via /proc for portability.
401          */
402         {
403                 INIT_CTL_NAME(PSDEV_DEBUG)
404                 .procname = "debug",
405                 .data     = &libcfs_debug,
406                 .maxlen   = sizeof(int),
407                 .mode     = 0644,
408                 .proc_handler = &proc_dobitmasks,
409         },
410         {
411                 INIT_CTL_NAME(PSDEV_SUBSYSTEM_DEBUG)
412                 .procname = "subsystem_debug",
413                 .data     = &libcfs_subsystem_debug,
414                 .maxlen   = sizeof(int),
415                 .mode     = 0644,
416                 .proc_handler = &proc_dobitmasks,
417         },
418         {
419                 INIT_CTL_NAME(PSDEV_PRINTK)
420                 .procname = "printk",
421                 .data     = &libcfs_printk,
422                 .maxlen   = sizeof(int),
423                 .mode     = 0644,
424                 .proc_handler = &proc_dobitmasks,
425         },
426         {
427                 INIT_CTL_NAME(PSDEV_CONSOLE_RATELIMIT)
428                 .procname = "console_ratelimit",
429                 .data     = &libcfs_console_ratelimit,
430                 .maxlen   = sizeof(int),
431                 .mode     = 0644,
432                 .proc_handler = &proc_dointvec
433         },
434         {
435                 INIT_CTL_NAME(PSDEV_CONSOLE_MAX_DELAY_CS)
436                 .procname = "console_max_delay_centisecs",
437                 .maxlen   = sizeof(int),
438                 .mode     = 0644,
439                 .proc_handler = &proc_console_max_delay_cs
440         },
441         {
442                 INIT_CTL_NAME(PSDEV_CONSOLE_MIN_DELAY_CS)
443                 .procname = "console_min_delay_centisecs",
444                 .maxlen   = sizeof(int),
445                 .mode     = 0644,
446                 .proc_handler = &proc_console_min_delay_cs
447         },
448         {
449                 INIT_CTL_NAME(PSDEV_CONSOLE_BACKOFF)
450                 .procname = "console_backoff",
451                 .maxlen   = sizeof(int),
452                 .mode     = 0644,
453                 .proc_handler = &proc_console_backoff
454         },
455
456         {
457                 INIT_CTL_NAME(PSDEV_DEBUG_PATH)
458                 .procname = "debug_path",
459                 .data     = libcfs_debug_file_path_arr,
460                 .maxlen   = sizeof(libcfs_debug_file_path_arr),
461                 .mode     = 0644,
462                 .proc_handler = &proc_dostring,
463         },
464
465         {
466                 INIT_CTL_NAME(PSDEV_CPT_TABLE)
467                 .procname = "cpu_partition_table",
468                 .maxlen   = 128,
469                 .mode     = 0444,
470                 .proc_handler = &proc_cpt_table,
471         },
472
473         {
474                 INIT_CTL_NAME(PSDEV_LNET_UPCALL)
475                 .procname = "upcall",
476                 .data     = lnet_upcall,
477                 .maxlen   = sizeof(lnet_upcall),
478                 .mode     = 0644,
479                 .proc_handler = &proc_dostring,
480         },
481         {
482                 INIT_CTL_NAME(PSDEV_LNET_DEBUG_LOG_UPCALL)
483                 .procname = "debug_log_upcall",
484                 .data     = lnet_debug_log_upcall,
485                 .maxlen   = sizeof(lnet_debug_log_upcall),
486                 .mode     = 0644,
487                 .proc_handler = &proc_dostring,
488         },
489         {
490                 INIT_CTL_NAME(PSDEV_LNET_MEMUSED)
491                 .procname = "lnet_memused",
492                 .data     = (int *)&libcfs_kmemory.counter,
493                 .maxlen   = sizeof(int),
494                 .mode     = 0444,
495                 .proc_handler = &proc_dointvec,
496                 INIT_STRATEGY(&sysctl_intvec)
497         },
498         {
499                 INIT_CTL_NAME(PSDEV_LNET_CATASTROPHE)
500                 .procname = "catastrophe",
501                 .data     = &libcfs_catastrophe,
502                 .maxlen   = sizeof(int),
503                 .mode     = 0444,
504                 .proc_handler = &proc_dointvec,
505                 INIT_STRATEGY(&sysctl_intvec)
506         },
507         {
508                 INIT_CTL_NAME(PSDEV_LNET_PANIC_ON_LBUG)
509                 .procname = "panic_on_lbug",
510                 .data     = &libcfs_panic_on_lbug,
511                 .maxlen   = sizeof(int),
512                 .mode     = 0644,
513                 .proc_handler = &proc_dointvec,
514                 INIT_STRATEGY(&sysctl_intvec)
515         },
516         {
517                 INIT_CTL_NAME(PSDEV_LNET_DUMP_KERNEL)
518                 .procname = "dump_kernel",
519                 .maxlen   = 256,
520                 .mode     = 0200,
521                 .proc_handler = &proc_dump_kernel,
522         },
523         {
524                 INIT_CTL_NAME(PSDEV_LNET_DAEMON_FILE)
525                 .procname = "daemon_file",
526                 .mode     = 0644,
527                 .maxlen   = 256,
528                 .proc_handler = &proc_daemon_file,
529         },
530         {
531                 INIT_CTL_NAME(PSDEV_LNET_DEBUG_MB)
532                 .procname = "debug_mb",
533                 .mode     = 0644,
534                 .proc_handler = &proc_debug_mb,
535         },
536         {
537                 INIT_CTL_NAME(PSDEV_LNET_WATCHDOG_RATELIMIT)
538                 .procname = "watchdog_ratelimit",
539                 .data     = &libcfs_watchdog_ratelimit,
540                 .maxlen   = sizeof(int),
541                 .mode     = 0644,
542                 .proc_handler = &proc_dointvec_minmax,
543                 .extra1   = &min_watchdog_ratelimit,
544                 .extra2   = &max_watchdog_ratelimit,
545         },
546         {       INIT_CTL_NAME(PSDEV_LNET_FORCE_LBUG)
547                 .procname = "force_lbug",
548                 .data     = NULL,
549                 .maxlen   = 0,
550                 .mode     = 0200,
551                 .proc_handler = &libcfs_force_lbug
552         },
553         {
554                 INIT_CTL_NAME(PSDEV_LNET_FAIL_LOC)
555                 .procname = "fail_loc",
556                 .data     = &cfs_fail_loc,
557                 .maxlen   = sizeof(cfs_fail_loc),
558                 .mode     = 0644,
559                 .proc_handler = &proc_fail_loc
560         },
561         {
562                 INIT_CTL_NAME(PSDEV_LNET_FAIL_VAL)
563                 .procname = "fail_val",
564                 .data     = &cfs_fail_val,
565                 .maxlen   = sizeof(int),
566                 .mode     = 0644,
567                 .proc_handler = &proc_dointvec
568         },
569         {
570                 INIT_CTL_NAME(0)
571         }
572 };
573
574 #ifdef CONFIG_SYSCTL
575 static cfs_sysctl_table_t top_table[] = {
576         {
577                 INIT_CTL_NAME(CTL_LNET)
578                 .procname = "lnet",
579                 .mode     = 0555,
580                 .data     = NULL,
581                 .maxlen   = 0,
582                 .child    = lnet_table,
583         },
584         {
585                 INIT_CTL_NAME(0)
586         }
587 };
588
589 int insert_proc(void)
590 {
591         if (lnet_table_header == NULL)
592                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
593 #endif
594         return 0;
595 }
596
597 void remove_proc(void)
598 {
599 #ifdef CONFIG_SYSCTL
600         if (lnet_table_header != NULL)
601                 cfs_unregister_sysctl_table(lnet_table_header);
602
603         lnet_table_header = NULL;
604 #endif
605 }