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