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