Whamcloud - gitweb
LU-3789 mgs: Add deprecation warning for "lctl conf_param"
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
index 9079242..1c9ed42 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -77,13 +77,26 @@ static char * lcfg_devname;
 
 int lcfg_set_devname(char *name)
 {
+        char *ptr;
+        int digit = 1;
+
         if (name) {
                 if (lcfg_devname)
                         free(lcfg_devname);
                 /* quietly strip the unnecessary '$' */
                 if (*name == '$' || *name == '%')
                         name++;
-                if (isdigit(*name)) {
+
+                ptr = name;
+                while (*ptr != '\0') {
+                        if (!isdigit(*ptr)) {
+                            digit = 0;
+                            break;
+                        }
+                        ptr++;
+                }
+
+                if (digit) {
                         /* We can't translate from dev # to name */
                         lcfg_devname = NULL;
                 } else {
@@ -126,19 +139,6 @@ int jt_lcfg_attach(int argc, char **argv)
         if (rc < 0) {
                 fprintf(stderr, "error: %s: LCFG_ATTACH %s\n",
                         jt_cmdname(argv[0]), strerror(rc = errno));
-        } else if (argc == 3) {
-                char name[1024];
-
-                lcfg_set_devname(argv[2]);
-                if (strlen(argv[2]) > 128) {
-                        printf("Name too long to set environment\n");
-                        return -EINVAL;
-                }
-                snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
-                rc = setenv(name, argv[1], 1);
-                if (rc) {
-                        printf("error setting env variable %s\n", name);
-                }
         } else {
                 lcfg_set_devname(argv[2]);
         }
@@ -490,6 +490,83 @@ int jt_lcfg_param(int argc, char **argv)
         return rc;
 }
 
+struct param_opts {
+       unsigned int po_only_path:1;
+       unsigned int po_show_path:1;
+       unsigned int po_show_type:1;
+       unsigned int po_recursive:1;
+       unsigned int po_params2:1;
+       unsigned int po_delete:1;
+};
+
+/* Param set to single log file, used by all clients and servers.
+ * This should be loaded after the individual config logs.
+ * Called from set param with -P option.
+ */
+static int jt_lcfg_mgsparam2(int argc, char **argv, struct param_opts *popt)
+{
+       int     rc, i;
+       int     first_param;
+       struct  lustre_cfg_bufs bufs;
+       struct  lustre_cfg *lcfg;
+       char    *buf = NULL;
+       int     len;
+
+       first_param = optind;
+       if (first_param < 0 || first_param >= argc)
+               return CMD_HELP;
+
+       for (i = first_param, rc = 0; i < argc; i++) {
+               lustre_cfg_bufs_reset(&bufs, NULL);
+               /* This same command would be executed on all nodes, many
+                * of which should fail (silently) because they don't have
+                * that proc file existing locally. There would be no
+                * preprocessing on the MGS to try to figure out which
+                * parameter files to add this to, there would be nodes
+                * processing on the cluster nodes to try to figure out
+                * if they are the intended targets. They will blindly
+                * try to set the parameter, and ENOTFOUND means it wasn't
+                * for them.
+                * Target name "general" means call on all targets. It is
+                * left here in case some filtering will be added in
+                * future.
+                */
+               lustre_cfg_bufs_set_string(&bufs, 0, "general");
+
+               len = strlen(argv[i]);
+
+               /* put an '=' on the end in case it doesn't have one */
+               if (popt->po_delete && argv[i][len - 1] != '=') {
+                       buf = malloc(len + 1);
+                       sprintf(buf, "%s=", argv[i]);
+               } else {
+                       buf = argv[i];
+               }
+               lustre_cfg_bufs_set_string(&bufs, 1, buf);
+
+               lcfg = lustre_cfg_new(LCFG_SET_PARAM, &bufs);
+               if (IS_ERR(lcfg)) {
+                       fprintf(stderr, "error: allocating lcfg for %s: %s\n",
+                               jt_cmdname(argv[0]), strerror(PTR_ERR(lcfg)));
+                       if (rc == 0)
+                                rc = PTR_ERR(lcfg);
+               } else {
+                       int rc2 = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
+                       if (rc2 != 0) {
+                               fprintf(stderr, "error: executing %s: %s\n",
+                                       jt_cmdname(argv[0]), strerror(rc2));
+                               if (rc == 0)
+                                       rc = rc2;
+                       }
+                       lustre_cfg_free(lcfg);
+               }
+               if (buf != argv[i])
+                       free(buf);
+       }
+
+       return rc;
+}
+
 /* Param set in config log on MGS */
 /* conf_param key=value */
 /* Note we can actually send mgc conf_params from clients, but currently
@@ -505,6 +582,11 @@ int jt_lcfg_mgsparam(int argc, char **argv)
         struct lustre_cfg *lcfg;
         char *buf = NULL;
 
+#if LUSTRE_VERSION >= OBD_OCD_VERSION(2,7,53,0)
+       fprintf(stderr, "warning: 'lctl conf_param' is deprecated, "
+               "use 'lctl set_param -P' instead\n");
+#endif
+
         /* mgs_setparam processes only lctl buf #1 */
         if ((argc > 3) || (argc <= 1))
                 return CMD_HELP;
@@ -552,12 +634,12 @@ int jt_lcfg_mgsparam(int argc, char **argv)
 
 /* Display the path in the same format as sysctl
  * For eg. obdfilter.lustre-OST0000.stats */
-static char *display_name(char *filename, int show_type)
+static char *display_name(char *filename, unsigned int po_show_type)
 {
         char *tmp;
         struct stat st;
 
-        if (show_type) {
+        if (po_show_type) {
                 if (lstat(filename, &st) < 0)
                         return NULL;
         }
@@ -579,7 +661,7 @@ static char *display_name(char *filename, int show_type)
                 *tmp = '.';
 
         /* append the indicator to entries */
-        if (show_type) {
+        if (po_show_type) {
                 if (S_ISDIR(st.st_mode))
                         strcat(filename, "/");
                 else if (S_ISLNK(st.st_mode))
@@ -646,29 +728,46 @@ static void clean_path(char *path)
         }
 }
 
-struct param_opts {
-        int only_path:1;
-        int show_path:1;
-        int show_type:1;
-        int recursive:1;
-};
+/* Supporting file paths creates perilous behavoir: LU-888.
+ * Path support is deprecated.
+ * If a path is supplied it must begin with /proc.  */
+static void lprocfs_param_pattern(const char *cmd, const char *path, char *buf,
+        size_t buf_size)
+{
+        /* test path to see if it begins with '/proc/' */
+        if (strncmp(path, "/proc/", strlen("/proc/")) == 0) {
+                static int warned;
+                if (!warned) {
+                        fprintf(stderr, "%s: specifying parameters via "
+                                "full paths is deprecated.\n", cmd);
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 50, 0)
+#warning "remove deprecated full path tunable access"
+#endif
+                        warned = 1;
+                }
+                snprintf(buf, buf_size, "%s", path);
+        } else {
+                snprintf(buf, buf_size, "/proc/{fs,sys}/{lnet,lustre}/%s",
+                        path);
+        }
+}
 
 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
 {
         int ch;
 
-        popt->show_path = 1;
-        popt->only_path = 1;
-        popt->show_type = 0;
-        popt->recursive = 0;
+        popt->po_show_path = 1;
+        popt->po_only_path = 1;
+        popt->po_show_type = 0;
+        popt->po_recursive = 0;
 
         while ((ch = getopt(argc, argv, "FR")) != -1) {
                 switch (ch) {
                 case 'F':
-                        popt->show_type = 1;
+                        popt->po_show_type = 1;
                         break;
                 case 'R':
-                        popt->recursive = 1;
+                        popt->po_recursive = 1;
                         break;
                 default:
                         return -1;
@@ -685,7 +784,7 @@ static int listparam_display(struct param_opts *popt, char *pattern)
         glob_t glob_info;
         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
 
-        rc = glob(pattern, GLOB_BRACE | (popt->recursive ? GLOB_MARK : 0),
+        rc = glob(pattern, GLOB_BRACE | (popt->po_recursive ? GLOB_MARK : 0),
                   NULL, &glob_info);
         if (rc) {
                 fprintf(stderr, "error: list_param: %s: %s\n",
@@ -706,7 +805,7 @@ static int listparam_display(struct param_opts *popt, char *pattern)
                 else
                         last = 0;
                 strcpy(filename, glob_info.gl_pathv[i]);
-                valuename = display_name(filename, popt->show_type);
+                valuename = display_name(filename, popt->po_show_type);
                 if (valuename)
                         printf("%s\n", valuename);
                 if (last) {
@@ -722,14 +821,13 @@ static int listparam_display(struct param_opts *popt, char *pattern)
 
 int jt_lcfg_listparam(int argc, char **argv)
 {
-        int fp;
         int rc = 0, i;
         struct param_opts popt;
         char pattern[PATH_MAX];
         char *path;
 
         rc = listparam_cmdline(argc, argv, &popt);
-        if (rc == argc && popt.recursive) {
+        if (rc == argc && popt.po_recursive) {
                 rc--;           /* we know at least "-R" is a parameter */
                 argv[rc] = "*";
         } else if (rc < 0 || rc >= argc) {
@@ -741,15 +839,7 @@ int jt_lcfg_listparam(int argc, char **argv)
 
                 clean_path(path);
 
-                /* 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);
-                }
+                lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
 
                 rc = listparam_display(&popt, pattern);
                 if (rc < 0)
@@ -763,20 +853,20 @@ static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
 {
         int ch;
 
-        popt->show_path = 1;
-        popt->only_path = 0;
-        popt->show_type = 0;
-        popt->recursive = 0;
+        popt->po_show_path = 1;
+        popt->po_only_path = 0;
+        popt->po_show_type = 0;
+        popt->po_recursive = 0;
 
         while ((ch = getopt(argc, argv, "nNF")) != -1) {
                 switch (ch) {
                 case 'N':
-                        popt->only_path = 1;
+                        popt->po_only_path = 1;
                         break;
                 case 'n':
-                        popt->show_path = 0;
+                        popt->po_show_path = 0;
                 case 'F':
-                        popt->show_type = 1;
+                        popt->po_show_type = 1;
                         break;
                 default:
                         return -1;
@@ -802,14 +892,14 @@ static int getparam_display(struct param_opts *popt, char *pattern)
                 return -ESRCH;
         }
 
-        buf = malloc(CFS_PAGE_SIZE);
-        for (i = 0; i  < glob_info.gl_pathc; i++) {
-                char *valuename = NULL;
+       buf = malloc(PAGE_CACHE_SIZE);
+       for (i = 0; i  < glob_info.gl_pathc; i++) {
+               char *valuename = NULL;
 
-                memset(buf, 0, CFS_PAGE_SIZE);
+               memset(buf, 0, PAGE_CACHE_SIZE);
                 /* As listparam_display is used to show param name (with type),
                  * here "if (only_path)" is ignored.*/
-                if (popt->show_path) {
+                if (popt->po_show_path) {
                         strcpy(filename, glob_info.gl_pathv[i]);
                         valuename = display_name(filename, 0);
                 }
@@ -823,10 +913,10 @@ static int getparam_display(struct param_opts *popt, char *pattern)
                         continue;
                 }
 
-                do {
-                        rc = read(fd, buf, CFS_PAGE_SIZE);
-                        if (rc == 0)
-                                break;
+               do {
+                       rc = read(fd, buf, PAGE_CACHE_SIZE);
+                       if (rc == 0)
+                               break;
                         if (rc < 0) {
                                 fprintf(stderr, "error: get_param: "
                                         "read('%s') failed: %s\n",
@@ -836,7 +926,7 @@ static int getparam_display(struct param_opts *popt, char *pattern)
                         /* 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 && popt->show_path) {
+                        if (valuename && popt->po_show_path) {
                                 int longbuf = strnchr(buf, rc - 1, '\n') != NULL
                                               || rc > 60;
                                 printf("%s=%s", valuename, longbuf ? "\n" : buf);
@@ -863,7 +953,6 @@ static int getparam_display(struct param_opts *popt, char *pattern)
 
 int jt_lcfg_getparam(int argc, char **argv)
 {
-        int fp;
         int rc = 0, i;
         struct param_opts popt;
         char pattern[PATH_MAX];
@@ -873,46 +962,48 @@ int jt_lcfg_getparam(int argc, char **argv)
         if (rc < 0 || rc >= argc)
                 return CMD_HELP;
 
-        for (i = rc; i < argc; i++) {
-                path = argv[i];
+       for (i = rc, rc = 0; i < argc; i++) {
+               int rc2;
 
-                clean_path(path);
+               path = argv[i];
 
-                /* 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);
-                }
+               clean_path(path);
 
-                if (popt.only_path)
-                        rc = listparam_display(&popt, pattern);
-                else
-                        rc = getparam_display(&popt, pattern);
-                if (rc < 0)
-                        return rc;
-        }
+               lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
 
-        return 0;
+               if (popt.po_only_path)
+                       rc2 = listparam_display(&popt, pattern);
+               else
+                       rc2 = getparam_display(&popt, pattern);
+               if (rc2 < 0 && rc == 0)
+                       rc = rc2;
+       }
+
+       return rc;
 }
 
 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
 {
         int ch;
 
-        popt->show_path = 1;
-        popt->only_path = 0;
-        popt->show_type = 0;
-        popt->recursive = 0;
+        popt->po_show_path = 1;
+        popt->po_only_path = 0;
+        popt->po_show_type = 0;
+        popt->po_recursive = 0;
+       popt->po_params2 = 0;
+       popt->po_delete = 0;
 
-        while ((ch = getopt(argc, argv, "n")) != -1) {
+       while ((ch = getopt(argc, argv, "nPd")) != -1) {
                 switch (ch) {
                 case 'n':
-                        popt->show_path = 0;
+                        popt->po_show_path = 0;
                         break;
+               case 'P':
+                       popt->po_params2 = 1;
+                       break;
+               case 'd':
+                       popt->po_delete = 1;
+                       break;
                 default:
                         return -1;
                 }
@@ -934,39 +1025,38 @@ static int setparam_display(struct param_opts *popt, char *pattern, char *value)
                         pattern, globerrstr(rc));
                 return -ESRCH;
         }
-        for (i = 0; i  < glob_info.gl_pathc; i++) {
-                char *valuename = NULL;
-
-                if (popt->show_path) {
-                        strcpy(filename, glob_info.gl_pathv[i]);
-                        valuename = display_name(filename, 0);
-                        if (valuename)
-                                printf("%s=%s\n", valuename, value);
-                }
-                /* Write the new value to the file */
-                fd = open(glob_info.gl_pathv[i], O_WRONLY);
-                if (fd > 0) {
-                        rc = write(fd, value, strlen(value));
-                        if (rc < 0)
-                                fprintf(stderr, "error: set_param: "
-                                        "writing to file %s: %s\n",
-                                        glob_info.gl_pathv[i], strerror(errno));
-                        else
-                                rc = 0;
-                        close(fd);
-                } else {
-                        fprintf(stderr, "error: set_param: %s opening %s\n",
-                                strerror(rc = errno), glob_info.gl_pathv[i]);
-                }
-        }
-
-        globfree(&glob_info);
-        return rc;
+       for (i = 0; i  < glob_info.gl_pathc; i++) {
+               char *valuename = NULL;
+
+               if (popt->po_show_path) {
+                       strcpy(filename, glob_info.gl_pathv[i]);
+                       valuename = display_name(filename, 0);
+                       if (valuename)
+                               printf("%s=%s\n", valuename, value);
+               }
+               /* Write the new value to the file */
+               fd = open(glob_info.gl_pathv[i], O_WRONLY);
+               if (fd >= 0) {
+                       rc = write(fd, value, strlen(value));
+                       if (rc < 0)
+                               fprintf(stderr, "error: set_param: setting "
+                                       "%s=%s: %s\n", glob_info.gl_pathv[i],
+                                       value, strerror(errno));
+                       else
+                               rc = 0;
+                       close(fd);
+               } else {
+                       fprintf(stderr, "error: set_param: %s opening %s\n",
+                               strerror(rc = errno), glob_info.gl_pathv[i]);
+               }
+       }
+
+       globfree(&glob_info);
+       return rc;
 }
 
 int jt_lcfg_setparam(int argc, char **argv)
 {
-        int fp;
         int rc = 0, i;
         struct param_opts popt;
         char pattern[PATH_MAX];
@@ -976,7 +1066,14 @@ int jt_lcfg_setparam(int argc, char **argv)
         if (rc < 0 || rc >= argc)
                 return CMD_HELP;
 
-        for (i = rc; i < argc; i++) {
+       if (popt.po_params2)
+               /* We can't delete parameters that were
+                * set with old conf_param interface */
+               return jt_lcfg_mgsparam2(argc, argv, &popt);
+
+       for (i = rc, rc = 0; i < argc; i++) {
+               int rc2;
+
                 if ((value = strchr(argv[i], '=')) != NULL) {
                         /* format: set_param a=b */
                         *value = '\0';
@@ -994,22 +1091,15 @@ int jt_lcfg_setparam(int argc, char **argv)
 
                 clean_path(path);
 
-                /* 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);
-                }
+                lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
 
-                rc = setparam_display(&popt, pattern, value);
+                rc2 = setparam_display(&popt, pattern, value);
                 path = NULL;
                 value = NULL;
-                if (rc < 0)
-                        return rc;
-        }
+               if (rc2 < 0 && rc == 0)
+                       rc = rc2;
+       }
 
-        return 0;
+       return rc;
 }
+