Whamcloud - gitweb
b=14471
authorkomaln <komaln>
Fri, 22 Feb 2008 10:27:48 +0000 (10:27 +0000)
committerkomaln <komaln>
Fri, 22 Feb 2008 10:27:48 +0000 (10:27 +0000)
r=Adilger, Wangdi

to add lctl {get,set}_param functionality.

lustre/utils/lctl.c
lustre/utils/lustre_cfg.c
lustre/utils/obdctl.h

index af09fa8..1a713b8 100644 (file)
@@ -119,6 +119,14 @@ command_t cmdlist[] = {
          "usage: conf_param <target.keyword=val> ...\n"},
         {"local_param", jt_lcfg_param, 0, "set a temporary, local param\n"
          "usage: local_param <target.keyword=val> ...\n"},
+        {"get_param", jt_lcfg_getparam, 0, "get the Lustre or LNET parameter\n"
+         "usage: get_param [-n] path/to/param/file \n"
+         "Get the value of Lustre or LNET parameter from the specified path\n"
+         "Use '-n' to disable printing of the key name when printing values."},
+        {"set_param", jt_lcfg_setparam, 0, "set the Lustre or LNET parameter\n"
+         "usage: set_param [-n] path/to/param/file value\n"
+         "Set the value of the Lustre or LNET parameter at the specified path\n"
+         "Use '-n' to disable printing of the key name when printing values."},
 
         /* Debug commands */
         {"==== debugging control ====", jt_noop, 0, "debug"},
index b33f0e7..234b2e9 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <ctype.h>
+#include <glob.h>
 
 #ifndef __KERNEL__
 #include <liblustre.h>
@@ -505,4 +506,231 @@ int jt_lcfg_mgsparam(int argc, char **argv)
         return rc;
 }
 
+/* Display the path in the same format as sysctl
+ * For eg. obdfilter.lustre-OST0000.stats */
+static char *display_name(char *filename)
+{
+        char *tmp;
+
+        filename += strlen("/proc/");
+        if (strncmp(filename, "fs/", strlen("fs/")) == 0)
+                filename += strlen("fs/");
+        else
+                filename += strlen("sys/");
+
+        if (strncmp(filename, "lustre/", strlen("lustre/")) == 0)
+                filename += strlen("lustre/");
+
+        /* replace '/' with '.' to match conf_param and sysctl */
+        tmp = filename;
+        while ((tmp = strchr(tmp, '/')) != NULL)
+                *tmp = '.';
+
+        return filename;
+}
+
+/* Find a character in a length limited string */
+static char *strnchr(const char *p, char c, size_t n)
+{
+       if (!p)
+               return (0);
+
+       while (n-- > 0) {
+               if (*p == c)
+                       return ((char *)p);
+               p++;
+       }
+       return (0);
+}
+
+int jt_lcfg_getparam(int argc, char **argv)
+{
+        int fp;
+        int rc = 0, i, show_path = 0;
+        char buf[CFS_PAGE_SIZE] = {'\0'}, pattern[PATH_MAX];
+        char *path, *tmp;
+        glob_t glob_info;
+
+        if (argc == 3 && strcmp(argv[1], "-n") == 0) {
+                path = argv[2];
+        } else if (argc == 2) {
+                show_path = 1;
+                path = argv[1];
+        } else {
+                return CMD_HELP;
+        }
+
+        /* If the input is in form Eg. obdfilter.*.stats */
+        if (strchr(path, '.')) {
+                tmp = path;
+                while (*tmp != '\0') {
+                        if (*tmp == '.')
+                                *tmp = '/';
+                        tmp ++;
+                }
+        }
+
+        /* If the entire path is specified as input */
+        fp = open(path, O_RDONLY);
+        if (fp < 0)
+                snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
+                         path);
+        else {
+                strcpy(pattern, path);
+                close(fp);
+        }
+
+        rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
+        if (rc) {
+                fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
+                return rc;
+        }
+
+        for (i = 0; i  < glob_info.gl_pathc; i++) {
+                char *valuename = NULL;
+                if (show_path) {
+                        char *filename;
+                        filename = strdup(glob_info.gl_pathv[i]);
+                        valuename = display_name(filename);
+                }
+
+                /* Write the contents of file to stdout */
+                fp = open(glob_info.gl_pathv[i], O_RDONLY);
+                if (fp < 0) {
+                        fprintf(stderr, "error: %s: opening('%s') failed: %s\n",
+                                jt_cmdname(argv[0]), glob_info.gl_pathv[i],
+                                strerror(errno));
+                        continue;
+                }
+
+                do {
+                        rc = read(fp, buf, sizeof(buf));
+                        if (rc == 0)
+                                break;
+                        if (rc < 0) {
+                                fprintf(stderr, "error: %s: read('%s') "
+                                        "failed: %s\n", jt_cmdname(argv[0]),
+                                        glob_info.gl_pathv[i], strerror(errno));
+                                break;
+                        }
+                        /* Print the output in the format path=value if the
+                         * value contains no new line character or cab be
+                         * occupied in a line, else print value on new line */
+                        if (valuename && show_path) {
+                                int longbuf = strnchr(buf, rc - 1, '\n') != NULL
+                                              || rc > 60;
+                                printf("%s=%s", valuename, longbuf ? "\n" : buf);
+                                valuename = NULL;
+                                if (!longbuf)
+                                        continue;
+                                fflush(stdout);
+                        }
+                        rc = write(fileno(stdout), buf, rc);
+                        if (rc < 0) {
+                                fprintf(stderr, "error: %s: write to stdout "
+                                        "failed: %s\n", jt_cmdname(argv[0]),
+                                        strerror(errno));
+                                break;
+                        }
+                } while (1);
+                memset(buf, 0, sizeof(buf));
+                close(fp);
+        }
+
+        globfree(&glob_info);
+        return rc;
+}
+
+
+int jt_lcfg_setparam(int argc, char **argv)
+{
+        int rc = 0, i;
+        int fp, show_path = 0;
+        char pattern[PATH_MAX];
+        char *path, *value, *tmp;
+        glob_t glob_info;
+
+        path = argv[1];
+        if (argc == 4 && (strcmp(argv[1], "-n") == 0)) {
+                /* Format: lctl set_param -n param value */
+                path = argv[2];
+                value = argv[3];
+        } else if (argc == 3) {
+                if (strcmp(argv[1], "-n") != 0) {
+                        /* Format: lctl set_param param value */
+                        show_path = 1;
+                        value = argv[2];
+                } else if ((value = strchr(argv[2], '=')) != NULL) {
+                        /* Format: lctl set_param -n param=value */
+                        path = argv[2];
+                        *value = '\0';
+                        value ++;
+                } else {
+                        fprintf(stderr, "error: %s Incorrect arguments."
+                                        "See Usage\n",
+                                jt_cmdname(argv[0]));
+                        return CMD_HELP;
+                }
+        } else if (argc == 2 && ((value = strchr(argv[1], '=')) != NULL)) {
+                /* Format: lctl set_param param=value */
+                show_path = 1;
+                *value = '\0';
+                value++;
+        } else {
+                fprintf(stderr, "error: %s Incorrect arguments. See Usage\n",
+                        jt_cmdname(argv[0]));
+                return CMD_HELP;
+        }
 
+        /* If the input is in form Eg. obdfilter.*.stats */
+        if (strchr(path, '.')) {
+                tmp = path;
+                while (*tmp != '\0') {
+                        if (*tmp == '.')
+                                *tmp = '/';
+                        tmp ++;
+                }
+        }
+
+        fp = open(path, O_RDONLY);
+        if (fp < 0)
+                snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
+                         path);
+        else {
+                strcpy(pattern, path);
+                close(fp);
+        }
+
+        rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
+        if (rc) {
+                fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
+                return rc;
+        }
+        for (i = 0; i  < glob_info.gl_pathc; i++) {
+                if (show_path) {
+                        char *valuename, *filename;
+                        filename = strdup(glob_info.gl_pathv[i]);
+                        valuename = display_name(filename);
+                        printf("%s=%s\n", valuename, value);
+                }
+                /* Write the new value to the file */
+                fp = open(glob_info.gl_pathv[i], O_WRONLY);
+                if (fp > 0) {
+                        rc = write(fp, value, strlen(value));
+                        if (rc < 0)
+                                fprintf(stderr,
+                                        "error writing to file %s\n",
+                                        glob_info.gl_pathv[i]);
+                        else
+                                rc = 0;
+                        close(fp);
+                } else {
+                        fprintf(stderr, "error: %s: %s opening %s\n",
+                                jt_cmdname(argv[0]), strerror(rc = errno),
+                                glob_info.gl_pathv[i]);
+                }
+        }
+
+        globfree(&glob_info);
+        return rc;
+}
index 27e848b..cd7b430 100644 (file)
@@ -80,6 +80,8 @@ int jt_lcfg_add_conn(int argc, char **argv);
 int jt_lcfg_del_conn(int argc, char **argv);
 int jt_lcfg_param(int argc, char **argv);
 int jt_lcfg_mgsparam(int argc, char **argv);
+int jt_lcfg_getparam(int argc, char **argv);
+int jt_lcfg_setparam(int argc, char **argv);
 
 int obd_add_uuid(char *uuid, lnet_nid_t nid);