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