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