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