Merge branch 'opw-next' into staging-next
[linux-2.6-microblaze.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                 .procname = "debug",
375                 .data     = &libcfs_debug,
376                 .maxlen   = sizeof(int),
377                 .mode     = 0644,
378                 .proc_handler = &proc_dobitmasks,
379         },
380         {
381                 .procname = "subsystem_debug",
382                 .data     = &libcfs_subsystem_debug,
383                 .maxlen   = sizeof(int),
384                 .mode     = 0644,
385                 .proc_handler = &proc_dobitmasks,
386         },
387         {
388                 .procname = "printk",
389                 .data     = &libcfs_printk,
390                 .maxlen   = sizeof(int),
391                 .mode     = 0644,
392                 .proc_handler = &proc_dobitmasks,
393         },
394         {
395                 .procname = "console_ratelimit",
396                 .data     = &libcfs_console_ratelimit,
397                 .maxlen   = sizeof(int),
398                 .mode     = 0644,
399                 .proc_handler = &proc_dointvec
400         },
401         {
402                 .procname = "console_max_delay_centisecs",
403                 .maxlen   = sizeof(int),
404                 .mode     = 0644,
405                 .proc_handler = &proc_console_max_delay_cs
406         },
407         {
408                 .procname = "console_min_delay_centisecs",
409                 .maxlen   = sizeof(int),
410                 .mode     = 0644,
411                 .proc_handler = &proc_console_min_delay_cs
412         },
413         {
414                 .procname = "console_backoff",
415                 .maxlen   = sizeof(int),
416                 .mode     = 0644,
417                 .proc_handler = &proc_console_backoff
418         },
419
420         {
421                 .procname = "debug_path",
422                 .data     = libcfs_debug_file_path_arr,
423                 .maxlen   = sizeof(libcfs_debug_file_path_arr),
424                 .mode     = 0644,
425                 .proc_handler = &proc_dostring,
426         },
427
428         {
429                 .procname = "cpu_partition_table",
430                 .maxlen   = 128,
431                 .mode     = 0444,
432                 .proc_handler = &proc_cpt_table,
433         },
434
435         {
436                 .procname = "upcall",
437                 .data     = lnet_upcall,
438                 .maxlen   = sizeof(lnet_upcall),
439                 .mode     = 0644,
440                 .proc_handler = &proc_dostring,
441         },
442         {
443                 .procname = "debug_log_upcall",
444                 .data     = lnet_debug_log_upcall,
445                 .maxlen   = sizeof(lnet_debug_log_upcall),
446                 .mode     = 0644,
447                 .proc_handler = &proc_dostring,
448         },
449         {
450                 .procname = "lnet_memused",
451                 .data     = (int *)&libcfs_kmemory.counter,
452                 .maxlen   = sizeof(int),
453                 .mode     = 0444,
454                 .proc_handler = &proc_dointvec,
455         },
456         {
457                 .procname = "catastrophe",
458                 .data     = &libcfs_catastrophe,
459                 .maxlen   = sizeof(int),
460                 .mode     = 0444,
461                 .proc_handler = &proc_dointvec,
462         },
463         {
464                 .procname = "panic_on_lbug",
465                 .data     = &libcfs_panic_on_lbug,
466                 .maxlen   = sizeof(int),
467                 .mode     = 0644,
468                 .proc_handler = &proc_dointvec,
469         },
470         {
471                 .procname = "dump_kernel",
472                 .maxlen   = 256,
473                 .mode     = 0200,
474                 .proc_handler = &proc_dump_kernel,
475         },
476         {
477                 .procname = "daemon_file",
478                 .mode     = 0644,
479                 .maxlen   = 256,
480                 .proc_handler = &proc_daemon_file,
481         },
482         {
483                 .procname = "debug_mb",
484                 .mode     = 0644,
485                 .proc_handler = &proc_debug_mb,
486         },
487         {
488                 .procname = "watchdog_ratelimit",
489                 .data     = &libcfs_watchdog_ratelimit,
490                 .maxlen   = sizeof(int),
491                 .mode     = 0644,
492                 .proc_handler = &proc_dointvec_minmax,
493                 .extra1   = &min_watchdog_ratelimit,
494                 .extra2   = &max_watchdog_ratelimit,
495         },
496         {
497                 .procname = "force_lbug",
498                 .data     = NULL,
499                 .maxlen   = 0,
500                 .mode     = 0200,
501                 .proc_handler = &libcfs_force_lbug
502         },
503         {
504                 .procname = "fail_loc",
505                 .data     = &cfs_fail_loc,
506                 .maxlen   = sizeof(cfs_fail_loc),
507                 .mode     = 0644,
508                 .proc_handler = &proc_fail_loc
509         },
510         {
511                 .procname = "fail_val",
512                 .data     = &cfs_fail_val,
513                 .maxlen   = sizeof(int),
514                 .mode     = 0644,
515                 .proc_handler = &proc_dointvec
516         },
517         {
518         }
519 };
520
521 #ifdef CONFIG_SYSCTL
522 static ctl_table_t top_table[] = {
523         {
524                 .procname = "lnet",
525                 .mode     = 0555,
526                 .data     = NULL,
527                 .maxlen   = 0,
528                 .child    = lnet_table,
529         },
530         {
531         }
532 };
533 #endif
534
535 int insert_proc(void)
536 {
537 #ifdef CONFIG_SYSCTL
538         if (lnet_table_header == NULL)
539                 lnet_table_header = register_sysctl_table(top_table);
540 #endif
541         return 0;
542 }
543
544 void remove_proc(void)
545 {
546 #ifdef CONFIG_SYSCTL
547         if (lnet_table_header != NULL)
548                 unregister_sysctl_table(lnet_table_header);
549
550         lnet_table_header = NULL;
551 #endif
552 }