Whamcloud - gitweb
*** empty log message ***
[fs/lustre-release.git] / lnet / libcfs / linux / linux-proc.c
index 77277ba..92cc8fe 100644 (file)
@@ -26,7 +26,9 @@
 # define EXPORT_SYMTAB
 #endif
 
+#ifndef AUTOCONF_INCLUDED
 #include <linux/config.h>
+#endif
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/proc_fs.h>
 #include <linux/sysctl.h>
 
-# define DEBUG_SUBSYSTEM S_PORTALS
+# define DEBUG_SUBSYSTEM S_LNET
 
 #include <libcfs/kp30.h>
 #include <asm/div64.h>
 #include "tracefile.h"
 
-static struct ctl_table_header *portals_table_header = NULL;
-extern char debug_file_path[1024];
-extern char portals_upcall[1024];
+static cfs_sysctl_table_header_t *lnet_table_header = NULL;
+extern char lnet_upcall[1024];
 
-#define PSDEV_PORTALS  (0x100)
+#define PSDEV_LNET  (0x100)
 enum {
         PSDEV_DEBUG = 1,          /* control debugging */
         PSDEV_SUBSYSTEM_DEBUG,    /* control debugging */
-        PSDEV_PRINTK,             /* force all errors to console */
-        PSDEV_CONSOLE,            /* allow _any_ messages to console */
+        PSDEV_PRINTK,             /* force all messages to console */
+        PSDEV_CONSOLE_RATELIMIT,  /* ratelimit console messages */
         PSDEV_DEBUG_PATH,         /* crashdump log location */
         PSDEV_DEBUG_DUMP_PATH,    /* crashdump tracelog location */
-        PSDEV_PORTALS_UPCALL,     /* User mode upcall script  */
-        PSDEV_PORTALS_MEMUSED,    /* bytes currently PORTAL_ALLOCated */
-        PSDEV_PORTALS_CATASTROPHE,/* if we have LBUGged or panic'd */
-};
-
-static struct ctl_table portals_table[] = {
-        {PSDEV_DEBUG, "debug", &portal_debug, sizeof(int), 0644, NULL,
-         &proc_dointvec},
-        {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &portal_subsystem_debug,
-         sizeof(int), 0644, NULL, &proc_dointvec},
-        {PSDEV_PRINTK, "printk", &portal_printk, sizeof(int), 0644, NULL,
-         &proc_dointvec},
-        {PSDEV_DEBUG_PATH, "debug_path", debug_file_path,
-         sizeof(debug_file_path), 0644, NULL, &proc_dostring, &sysctl_string},
-        {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
-         sizeof(portals_upcall), 0644, NULL, &proc_dostring,
-         &sysctl_string},
-        {PSDEV_PORTALS_MEMUSED, "memused", (int *)&portal_kmemory.counter,
-         sizeof(int), 0444, NULL, &proc_dointvec},
-        {PSDEV_PORTALS_CATASTROPHE, "catastrophe", &portals_catastrophe,
-         sizeof(int), 0444, NULL, &proc_dointvec},
-        {0}
+        PSDEV_LNET_UPCALL,        /* User mode upcall script  */
+        PSDEV_LNET_MEMUSED,       /* bytes currently PORTAL_ALLOCated */
+        PSDEV_LNET_CATASTROPHE,   /* if we have LBUGged or panic'd */
+        PSDEV_LNET_PANIC_ON_LBUG, /* flag to panic on LBUG */
+        PSDEV_LNET_DUMP_KERNEL,   /* snapshot kernel debug buffer to file */
+        PSDEV_LNET_DAEMON_FILE,   /* spool kernel debug buffer to file */
+        PSDEV_LNET_DEBUG_MB,      /* size of debug buffer */
 };
 
-static struct ctl_table top_table[2] = {
-        {PSDEV_PORTALS, "portals", NULL, 0, 0555, portals_table},
-        {0}
-};
-
-
-#ifdef PORTALS_PROFILING
-/*
- * profiling stuff.  we do this statically for now 'cause its simple,
- * but we could do some tricks with elf sections to have this array
- * automatically built.
- */
-#define def_prof(FOO) [PROF__##FOO] = {#FOO, 0, }
-
-struct prof_ent prof_ents[] = {
-        def_prof(our_recvmsg),
-        def_prof(our_sendmsg),
-        def_prof(socknal_recv),
-        def_prof(lib_parse),
-        def_prof(conn_list_walk),
-        def_prof(memcpy),
-        def_prof(lib_finalize),
-        def_prof(pingcli_time),
-        def_prof(gmnal_send),
-        def_prof(gmnal_recv),
-};
-
-EXPORT_SYMBOL(prof_ents);
+static int 
+proc_call_handler(void *data, int write, 
+                  loff_t *ppos, void *buffer, size_t *lenp, 
+                  int (*handler)(void *data, int write,
+                                 loff_t pos, void *buffer, int len))
+{
+        int rc = handler(data, write, *ppos, buffer, *lenp);
 
-/*
- * this function is as crazy as the proc filling api
- * requires.
- *
- * buffer: page allocated for us to scribble in.  the
- *  data returned to the user will be taken from here.
- * *start: address of the pointer that will tell the 
- *  caller where in buffer the data the user wants is.
- * ppos: offset in the entire /proc file that the user
- *  currently wants.
- * wanted: the amount of data the user wants.
- *
- * while going, 'curpos' is the offset in the entire
- * file where we currently are.  We only actually
- * start filling buffer when we get to a place in
- * the file that the user cares about.
- *
- * we take care to only sprintf when the user cares because
- * we're holding a lock while we do this.
- *
- * we're smart and know that we generate fixed size lines.
- * we only start writing to the buffer when the user cares.
- * This is unpredictable because we don't snapshot the
- * list between calls that are filling in a file from
- * the list.  The list could change mid read and the
- * output will look very weird indeed.  oh well.
- */
+        if (rc < 0)
+                return rc;
 
-static int prof_read_proc(char *buffer, char **start, off_t ppos, int wanted,
-                          int *eof, void *data)
-{
-        int len = 0, i;
-        int curpos;
-        char *header = "Interval        Cycles_per (Starts Finishes Total)\n";
-        int header_len = strlen(header);
-        char *format = "%-15s %.12Ld (%.12d %.12d %.12Ld)";
-        int line_len = (15 + 1 + 12 + 2 + 12 + 1 + 12 + 1 + 12 + 1);
-
-        *start = buffer;
-
-        if (ppos < header_len) {
-                int diff = MIN(header_len, wanted);
-                memcpy(buffer, header + ppos, diff);
-                len += diff;
-                ppos += diff;
+        if (write) {
+                *ppos += *lenp;
+        } else {
+                *lenp = rc;
+                *ppos += rc;
         }
+        return 0;
+}
 
-        if (len >= wanted)
-                goto out;
-
-        curpos = header_len;
-
-        for ( i = 0; i < MAX_PROFS ; i++) {
-                int copied;
-                struct prof_ent *pe = &prof_ents[i];
-                long long cycles_per;
-                /*
-                 * find the part of the array that the buffer wants
-                 */
-                if (ppos >= (curpos + line_len))  {
-                        curpos += line_len;
-                        continue;
-                }
-                /* the clever caller split a line */
-                if (ppos > curpos) {
-                        *start = buffer + (ppos - curpos);
-                }
+#define DECLARE_PROC_HANDLER(name)                      \
+static int                                              \
+LL_PROC_PROTO(name)                                     \
+{                                                       \
+        DECLARE_LL_PROC_PPOS_DECL;                      \
+                                                        \
+        return proc_call_handler(table->data, write,    \
+                                 ppos, buffer, lenp,    \
+                                 __##name);             \
+}
 
-                if (pe->finishes == 0)
-                        cycles_per = 0;
-                else
-                {
-                        cycles_per = pe->total_cycles;
-                        do_div (cycles_per, pe->finishes);
+static int __proc_dobitmasks(void *data, int write, 
+                             loff_t pos, void *buffer, int nob)
+{
+        const int     tmpstrlen = 512;
+        char         *tmpstr;
+        int           rc;
+        unsigned int *mask = data;
+        int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
+
+        rc = trace_allocate_string_buffer(&tmpstr, tmpstrlen);
+        if (rc < 0)
+                return rc;
+        
+        if (!write) {
+                libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
+                rc = strlen(tmpstr);
+
+                if (pos >= rc) {
+                        rc = 0;
+                } else {
+                        rc = trace_copyout_string(buffer, nob,
+                                                  tmpstr + pos, "\n");
                 }
+        } else {
+                rc = trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
+                if (rc < 0)
+                        return rc;
+                
+                rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
+        }
 
-                copied = sprintf(buffer + len, format, pe->str, cycles_per,
-                                 pe->starts, pe->finishes, pe->total_cycles);
+        trace_free_string_buffer(tmpstr, tmpstrlen);
+        return rc;
+}
 
-                len += copied;
+DECLARE_PROC_HANDLER(proc_dobitmasks)
 
-                /* pad to line len, -1 for \n */
-                if ((copied < line_len-1)) {
-                        int diff = (line_len-1) - copied;
-                        memset(buffer + len, ' ', diff);
-                        len += diff;
-                        copied += diff;
-                }
-
-                buffer[len++]= '\n';
+static int __proc_dump_kernel(void *data, int write,
+                              loff_t pos, void *buffer, int nob)
+{
+        if (!write)
+                return 0;
+        
+        return trace_dump_debug_buffer_usrstr(buffer, nob);
+}
 
-                /* bail if we have enough */
-                if (((buffer + len) - *start) >= wanted)
-                        break;
+DECLARE_PROC_HANDLER(proc_dump_kernel)
 
-                curpos += line_len;
+static int __proc_daemon_file(void *data, int write,
+                              loff_t pos, void *buffer, int nob)
+{
+        if (!write) {
+                int len = strlen(tracefile);
+                
+                if (pos >= len)
+                        return 0;
+                
+                return trace_copyout_string(buffer, nob, 
+                                            tracefile + pos, "\n");
         }
-
-        /* lameness */
-        if (i == MAX_PROFS)
-                *eof = 1;
- out:
-
-        return MIN(((buffer + len) - *start), wanted);
+        
+        return trace_daemon_command_usrstr(buffer, nob);
 }
 
-/*
- * all kids love /proc :/
- */
-static unsigned char basedir[]="net/portals";
-#endif /* PORTALS_PROFILING */
+DECLARE_PROC_HANDLER(proc_daemon_file)
 
-int insert_proc(void)
+static int __proc_debug_mb(void *data, int write,
+                           loff_t pos, void *buffer, int nob)
 {
-        struct proc_dir_entry *ent;
-#if PORTALS_PROFILING
-        unsigned char dir[128];
-
-        if (ARRAY_SIZE(prof_ents) != MAX_PROFS) {
-                CERROR("profiling enum and array are out of sync.\n");
-                return -1;
+        if (!write) {
+                char tmpstr[32];
+                int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
+                                    trace_get_debug_mb());
+
+                if (pos >= len)
+                        return 0;
+                
+                return trace_copyout_string(buffer, nob, tmpstr + pos, "\n");
         }
+        
+        return trace_set_debug_mb_usrstr(buffer, nob);
+}
+
+DECLARE_PROC_HANDLER(proc_debug_mb)
 
+static cfs_sysctl_table_t lnet_table[] = {
         /*
-         * This is pretty lame.  assuming that failure just
-         * means that they already existed.
+         * NB No .strategy entries have been provided since sysctl(8) prefers
+         * to go via /proc for portability.
          */
-        strcat(dir, basedir);
-        create_proc_entry(dir, S_IFDIR, 0);
-
-        strcat(dir, "/cycles");
-        ent = create_proc_entry(dir, 0, 0);
-        if (!ent) {
-                CERROR("couldn't register %s?\n", dir);
-                return -1;
-        }
+        {
+                .ctl_name = PSDEV_DEBUG,
+                .procname = "debug",
+                .data     = &libcfs_debug,
+                .maxlen   = sizeof(int),
+                .mode     = 0644,
+                .proc_handler = &proc_dobitmasks
+        },
+        {
+                .ctl_name = PSDEV_SUBSYSTEM_DEBUG,
+                .procname = "subsystem_debug",
+                .data     = &libcfs_subsystem_debug,
+                .maxlen   = sizeof(int),
+                .mode     = 0644,
+                .proc_handler = &proc_dobitmasks
+        },
+        {
+                .ctl_name = PSDEV_PRINTK,
+                .procname = "printk",
+                .data     = &libcfs_printk,
+                .maxlen   = sizeof(int),
+                .mode     = 0644,
+                .proc_handler = &proc_dobitmasks
+        },
+        {
+                .ctl_name = PSDEV_CONSOLE_RATELIMIT,
+                .procname = "console_ratelimit",
+                .data     = &libcfs_console_ratelimit,
+                .maxlen   = sizeof(int),
+                .mode     = 0644,
+                .proc_handler = &proc_dointvec
+        },
+
+        {
+                .ctl_name = PSDEV_DEBUG_PATH,
+                .procname = "debug_path",
+                .data     = debug_file_path,
+                .maxlen   = sizeof(debug_file_path),
+                .mode     = 0644,
+                .proc_handler = &proc_dostring,
+        },
+
+        {
+                .ctl_name = PSDEV_LNET_UPCALL,
+                .procname = "upcall",
+                .data     = lnet_upcall,
+                .maxlen   = sizeof(lnet_upcall),
+                .mode     = 0644,
+                .proc_handler = &proc_dostring,
+        },
+        {
+                .ctl_name = PSDEV_LNET_MEMUSED,
+                .procname = "memused",
+                .data     = (int *)&libcfs_kmemory.counter,
+                .maxlen   = sizeof(int),
+                .mode     = 0444,
+                .proc_handler = &proc_dointvec
+        },
+        {
+                .ctl_name = PSDEV_LNET_CATASTROPHE,
+                .procname = "catastrophe",
+                .data     = &libcfs_catastrophe,
+                .maxlen   = sizeof(int),
+                .mode     = 0444,
+                .proc_handler = &proc_dointvec
+        },
+        {
+                .ctl_name = PSDEV_LNET_PANIC_ON_LBUG,
+                .procname = "panic_on_lbug",
+                .data     = &libcfs_panic_on_lbug,
+                .maxlen   = sizeof(int),
+                .mode     = 0644,
+                .proc_handler = &proc_dointvec
+        },
+        {
+                .ctl_name = PSDEV_LNET_DUMP_KERNEL,
+                .procname = "dump_kernel",
+                .mode     = 0200,
+                .proc_handler = &proc_dump_kernel,
+        },
+        {
+                .ctl_name = PSDEV_LNET_DAEMON_FILE,
+                .procname = "daemon_file",
+                .mode     = 0644,
+                .proc_handler = &proc_daemon_file,
+        },
+        {
+                .ctl_name = PSDEV_LNET_DEBUG_MB,
+                .procname = "debug_mb",
+                .mode     = 0644,
+                .proc_handler = &proc_debug_mb,
+        },
+        {0}
+};
 
-        ent->data = NULL;
-        ent->read_proc = prof_read_proc;
-#endif /* PORTALS_PROFILING */
+static cfs_sysctl_table_t top_table[2] = {
+        {
+                .ctl_name = PSDEV_LNET,
+                .procname = "lnet",
+                .data     = NULL,
+                .maxlen   = 0,
+                .mode     = 0555,
+                .child    = lnet_table
+        },
+        {0}
+};
 
+int insert_proc(void)
+{
 #ifdef CONFIG_SYSCTL
-        if (!portals_table_header)
-                portals_table_header = register_sysctl_table(top_table, 0);
+        if (lnet_table_header == NULL)
+                lnet_table_header = cfs_register_sysctl_table(top_table, 0);
 #endif
-
-        ent = create_proc_entry("sys/portals/dump_kernel", 0, NULL);
-        if (ent == NULL) {
-                CERROR("couldn't register dump_kernel\n");
-                return -1;
-        }
-        ent->write_proc = trace_dk;
-
-        ent = create_proc_entry("sys/portals/daemon_file", 0, NULL);
-        if (ent == NULL) {
-                CERROR("couldn't register daemon_file\n");
-                return -1;
-        }
-        ent->write_proc = trace_write_daemon_file;
-        ent->read_proc = trace_read_daemon_file;
-
-        ent = create_proc_entry("sys/portals/debug_mb", 0, NULL);
-        if (ent == NULL) {
-                CERROR("couldn't register debug_mb\n");
-                return -1;
-        }
-        ent->write_proc = trace_write_debug_mb;
-        ent->read_proc = trace_read_debug_mb;
-
         return 0;
 }
 
 void remove_proc(void)
 {
-#if PORTALS_PROFILING
-        unsigned char dir[128];
-        int end;
-
-        dir[0]='\0';
-        strcat(dir, basedir);
-
-        end = strlen(dir);
-
-        strcat(dir, "/cycles");
-        remove_proc_entry(dir, 0);
-
-        dir[end] = '\0';
-        remove_proc_entry(dir, 0);
-#endif /* PORTALS_PROFILING */
-
-        remove_proc_entry("sys/portals/dump_kernel", NULL);
-        remove_proc_entry("sys/portals/daemon_file", NULL);
-        remove_proc_entry("sys/portals/debug_mb", NULL);
-
 #ifdef CONFIG_SYSCTL
-        if (portals_table_header)
-                unregister_sysctl_table(portals_table_header);
-        portals_table_header = NULL;
+        if (lnet_table_header != NULL)
+                cfs_unregister_sysctl_table(lnet_table_header);
+
+        lnet_table_header = NULL;
 #endif
 }