4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/lustre_cfg.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Robert Read <rread@clusterfs.com>
46 #include <sys/ioctl.h>
53 #include <libcfs/util/ioctl.h>
54 #include <libcfs/util/string.h>
55 #include <libcfs/util/param.h>
56 #include <libcfs/util/parser.h>
57 #include <lustre/lustreapi.h>
58 #include <linux/lnet/nidstr.h>
59 #include <linux/lnet/lnetctl.h>
60 #include <linux/lustre/lustre_cfg.h>
61 #include <linux/lustre/lustre_ioctl.h>
62 #include <linux/lustre/lustre_ver.h>
74 static char * lcfg_devname;
76 int lcfg_set_devname(char *name)
84 /* quietly strip the unnecessary '$' */
85 if (*name == '$' || *name == '%')
89 while (*ptr != '\0') {
98 /* We can't translate from dev # to name */
101 lcfg_devname = strdup(name);
109 char * lcfg_get_devname(void)
114 int jt_lcfg_device(int argc, char **argv)
116 return jt_obd_device(argc, argv);
119 static int jt_lcfg_ioctl(struct lustre_cfg_bufs *bufs, char *arg, int cmd)
121 struct lustre_cfg *lcfg;
124 lcfg = malloc(lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
128 lustre_cfg_init(lcfg, cmd, bufs);
129 rc = lcfg_ioctl(arg, OBD_DEV_ID, lcfg);
133 fprintf(stderr, "error: %s: %s\n", jt_cmdname(arg),
134 strerror(rc = errno));
138 int jt_lcfg_attach(int argc, char **argv)
140 struct lustre_cfg_bufs bufs;
146 lustre_cfg_bufs_reset(&bufs, NULL);
148 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
149 lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
150 lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
152 rc = jt_lcfg_ioctl(&bufs, argv[0], LCFG_ATTACH);
154 lcfg_set_devname(argv[2]);
159 int jt_lcfg_setup(int argc, char **argv)
161 struct lustre_cfg_bufs bufs;
164 if (lcfg_devname == NULL) {
165 fprintf(stderr, "%s: please use 'device name' to set the "
166 "device name for config commands.\n",
167 jt_cmdname(argv[0]));
171 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
176 for (i = 1; i < argc; i++) {
177 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
180 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_SETUP);
183 int jt_obd_detach(int argc, char **argv)
185 struct lustre_cfg_bufs bufs;
187 if (lcfg_devname == NULL) {
188 fprintf(stderr, "%s: please use 'device name' to set the "
189 "device name for config commands.\n",
190 jt_cmdname(argv[0]));
194 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
199 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DETACH);
202 int jt_obd_cleanup(int argc, char **argv)
204 struct lustre_cfg_bufs bufs;
207 char flags[3] = { 0 };
210 if (lcfg_devname == NULL) {
211 fprintf(stderr, "%s: please use 'device name' to set the "
212 "device name for config commands.\n",
213 jt_cmdname(argv[0]));
217 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
219 if (argc < 1 || argc > 3)
222 /* we are protected from overflowing our buffer by the argc
225 for (n = 1; n < argc; n++) {
226 if (strcmp(argv[n], "force") == 0) {
227 flags[flag_cnt++] = force;
228 } else if (strcmp(argv[n], "failover") == 0) {
229 flags[flag_cnt++] = failover;
231 fprintf(stderr, "unknown option: %s\n", argv[n]);
237 lustre_cfg_bufs_set_string(&bufs, 1, flags);
240 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_CLEANUP);
244 int do_add_uuid(char *func, char *uuid, lnet_nid_t nid)
247 struct lustre_cfg_bufs bufs;
248 struct lustre_cfg *lcfg;
250 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
252 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
254 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
258 lustre_cfg_init(lcfg, LCFG_ADD_UUID, &bufs);
259 lcfg->lcfg_nid = nid;
261 rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
265 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
271 printf("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
276 int jt_lcfg_add_uuid(int argc, char **argv)
284 nid = libcfs_str2nid(argv[2]);
285 if (nid == LNET_NID_ANY) {
286 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
290 return do_add_uuid(argv[0], argv[1], nid);
293 int jt_lcfg_del_uuid(int argc, char **argv)
295 struct lustre_cfg_bufs bufs;
298 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
302 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
303 if (strcmp (argv[1], "_all_"))
304 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
306 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_UUID);
309 int jt_lcfg_del_mount_option(int argc, char **argv)
311 struct lustre_cfg_bufs bufs;
316 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
319 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
321 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
324 int jt_lcfg_set_timeout(int argc, char **argv)
327 struct lustre_cfg_bufs bufs;
328 struct lustre_cfg *lcfg;
330 fprintf(stderr, "%s has been deprecated. Use conf_param instead.\n"
331 "e.g. conf_param lustre-MDT0000 obd_timeout=50\n",
332 jt_cmdname(argv[0]));
339 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
341 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
345 lustre_cfg_init(lcfg, LCFG_SET_TIMEOUT, &bufs);
346 lcfg->lcfg_num = atoi(argv[1]);
348 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
352 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
353 strerror(rc = errno));
358 int jt_lcfg_add_conn(int argc, char **argv)
360 struct lustre_cfg_bufs bufs;
361 struct lustre_cfg *lcfg;
372 if (lcfg_devname == NULL) {
373 fprintf(stderr, "%s: please use 'device name' to set the "
374 "device name for config commands.\n",
375 jt_cmdname(argv[0]));
379 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
381 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
383 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
387 lustre_cfg_init(lcfg, LCFG_ADD_CONN, &bufs);
388 lcfg->lcfg_num = priority;
390 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
394 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
395 strerror(rc = errno));
401 int jt_lcfg_del_conn(int argc, char **argv)
403 struct lustre_cfg_bufs bufs;
408 if (lcfg_devname == NULL) {
409 fprintf(stderr, "%s: please use 'device name' to set the "
410 "device name for config commands.\n",
411 jt_cmdname(argv[0]));
415 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
417 /* connection uuid */
418 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
420 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
423 /* Param set locally, directly on target */
424 int jt_lcfg_param(int argc, char **argv)
426 struct lustre_cfg_bufs bufs;
429 if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
432 lustre_cfg_bufs_reset(&bufs, NULL);
434 for (i = 1; i < argc; i++) {
435 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
438 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_PARAM);
442 unsigned int po_only_path:1;
443 unsigned int po_show_path:1;
444 unsigned int po_show_type:1;
445 unsigned int po_recursive:1;
446 unsigned int po_perm:1;
447 unsigned int po_delete:1;
448 unsigned int po_only_dir:1;
449 unsigned int po_file:1;
452 int lcfg_setparam_perm(char *func, char *buf)
455 struct lustre_cfg_bufs bufs;
456 struct lustre_cfg *lcfg;
458 lustre_cfg_bufs_reset(&bufs, NULL);
459 /* This same command would be executed on all nodes, many
460 * of which should fail (silently) because they don't have
461 * that proc file existing locally. There would be no
462 * preprocessing on the MGS to try to figure out which
463 * parameter files to add this to, there would be nodes
464 * processing on the cluster nodes to try to figure out
465 * if they are the intended targets. They will blindly
466 * try to set the parameter, and ENOTFOUND means it wasn't
468 * Target name "general" means call on all targets. It is
469 * left here in case some filtering will be added in
472 lustre_cfg_bufs_set_string(&bufs, 0, "general");
474 lustre_cfg_bufs_set_string(&bufs, 1, buf);
477 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount,
481 fprintf(stderr, "error: allocating lcfg for %s: %s\n",
482 jt_cmdname(func), strerror(rc));
485 lustre_cfg_init(lcfg, LCFG_SET_PARAM, &bufs);
486 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
488 fprintf(stderr, "error: executing %s: %s\n",
489 jt_cmdname(func), strerror(errno));
496 /* Param set to single log file, used by all clients and servers.
497 * This should be loaded after the individual config logs.
498 * Called from set param with -P option.
500 static int jt_lcfg_setparam_perm(int argc, char **argv,
501 struct param_opts *popt)
509 first_param = optind;
510 if (first_param < 0 || first_param >= argc)
513 for (i = first_param, rc = 0; i < argc; i++) {
515 len = strlen(argv[i]);
519 /* put an '=' on the end in case it doesn't have one */
520 if (popt->po_delete && argv[i][len - 1] != '=') {
521 buf = malloc(len + 1);
526 sprintf(buf, "%s=", argv[i]);
529 rc = lcfg_setparam_perm(argv[0], buf);
538 int lcfg_conf_param(char *func, char *buf)
541 struct lustre_cfg_bufs bufs;
542 struct lustre_cfg *lcfg;
544 lustre_cfg_bufs_reset(&bufs, NULL);
545 lustre_cfg_bufs_set_string(&bufs, 1, buf);
547 /* We could put other opcodes here. */
548 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
552 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
553 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
562 /* Param set in config log on MGS */
563 /* conf_param key=value */
564 /* Note we can actually send mgc conf_params from clients, but currently
565 * that's only done for default file striping (see ll_send_mgc_param),
567 /* After removal of a parameter (-d) Lustre will use the default
568 * AT NEXT REBOOT, not immediately. */
569 int jt_lcfg_confparam(int argc, char **argv)
575 /* mgs_setparam processes only lctl buf #1 */
576 if ((argc > 3) || (argc <= 1))
579 while ((rc = getopt(argc, argv, "d")) != -1) {
594 /* for delete, make it "<param>=\0" */
595 buf = malloc(strlen(argv[optind]) + 2);
600 /* put an '=' on the end in case it doesn't have one */
601 sprintf(buf, "%s=", argv[optind]);
602 /* then truncate after the first '=' */
603 ptr = strchr(buf, '=');
607 rc = lcfg_conf_param(argv[0], buf);
609 if (buf != argv[optind])
613 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
621 * Display a parameter path in the same format as sysctl.
622 * E.g. obdfilter.lustre-OST0000.stats
624 * \param[in] filename file name of the parameter
625 * \param[in] st parameter file stats
626 * \param[in] popt set/get param options
628 * \retval allocated pointer containing modified filename
631 display_name(const char *filename, struct stat *st, struct param_opts *popt)
633 size_t suffix_len = 0;
638 if (popt->po_show_type) {
639 if (S_ISDIR(st->st_mode))
641 else if (S_ISLNK(st->st_mode))
643 else if (st->st_mode & S_IWUSR)
647 /* Take the original filename string and chop off the glob addition */
648 tmp = strstr(filename, "/lustre/");
650 tmp = strstr(filename, "/lnet/");
652 tmp += strlen("/lnet/");
654 tmp += strlen("/lustre/");
657 /* Allocate return string */
658 param_name = strdup(tmp);
659 if (param_name == NULL)
662 /* replace '/' with '.' to match conf_param and sysctl */
663 for (tmp = strchr(param_name, '/'); tmp != NULL; tmp = strchr(tmp, '/'))
666 /* Append the indicator to entries if needed. */
667 if (popt->po_show_type && suffix != NULL) {
668 suffix_len = strlen(suffix);
670 tmp = realloc(param_name, suffix_len + strlen(param_name) + 1);
673 strncat(param_name, suffix,
674 strlen(param_name) + suffix_len);
681 /* Find a character in a length limited string */
682 /* BEWARE - kernel definition of strnchr has args in different order! */
683 static char *strnchr(const char *p, char c, size_t n)
697 * Turns a lctl parameter string into a procfs/sysfs subdirectory path pattern.
699 * \param[in] popt Used to control parameter usage. For this
700 * function it is used to see if the path has
702 * \param[in,out] path lctl parameter string that is turned into
703 * the subdirectory path pattern that is used
704 * to search the procfs/sysfs tree.
706 * \retval -errno on error.
709 clean_path(struct param_opts *popt, char *path)
714 if (popt == NULL || path == NULL || strlen(path) == 0)
717 /* If path contains a suffix we need to remove it */
718 if (popt->po_show_type) {
719 size_t path_end = strlen(path) - 1;
721 tmp = path + path_end;
732 /* get rid of '\', glob doesn't like it */
733 tmp = strrchr(path, '\\');
735 char *tail = path + strlen(path);
737 while (tmp != path) {
739 memmove(tmp, tmp + 1, tail - tmp);
746 /* Does this path contain a NID string ? */
747 tmp = strchr(path, '@');
749 char *find_nid = strdup(path);
752 if (find_nid == NULL)
755 /* First we need to chop off rest after nid string.
756 * Since find_nid is a clone of path it better have
758 tmp = strchr(find_nid, '@');
759 tmp = strchr(tmp, '.');
763 /* Now chop off the front. */
764 for (tmp = strchr(find_nid, '.'); tmp != NULL;
765 tmp = strchr(tmp, '.')) {
766 /* Remove MGC to make it NID format */
767 if (!strncmp(++tmp, "MGC", 3))
770 nid = libcfs_str2nid(tmp);
771 if (nid != LNET_NID_ANY) {
772 nidstr = libcfs_nid2str(nid);
781 /* replace param '.' with '/' */
782 for (tmp = strchr(path, '.'); tmp != NULL; tmp = strchr(tmp, '.')) {
785 /* Remove MGC to make it NID format */
786 if (!strncmp(tmp, "MGC", 3))
789 /* There exist cases where some of the subdirectories of the
790 * the parameter tree has embedded in its name a NID string.
791 * This means that it is possible that these subdirectories
792 * could have actual '.' in its name. If this is the case we
793 * don't want to blindly replace the '.' with '/'. */
794 if (nidstr != NULL) {
795 char *match = strstr(tmp, nidstr);
798 tmp += strlen(nidstr);
806 * The application lctl can perform three operations for lustre
807 * tunables. This enum defines those three operations which are
809 * 1) LIST_PARAM - list available tunables
810 * 2) GET_PARAM - report the current setting of a tunable
811 * 3) SET_PARAM - set the tunable to a new value
813 enum parameter_operation {
819 char *parameter_opname[] = {
820 [LIST_PARAM] = "list_param",
821 [GET_PARAM] = "get_param",
822 [SET_PARAM] = "set_param",
826 * Read the value of parameter
828 * \param[in] path full path to the parameter
829 * \param[in] param_name lctl parameter format of the
831 * \param[in] popt set/get param options
833 * \retval 0 on success.
834 * \retval -errno on error.
837 read_param(const char *path, const char *param_name, struct param_opts *popt)
843 rc = llapi_param_get_value(path, &buf, &buflen);
846 "error: read_param: \'%s\': %s\n",
847 path, strerror(-rc));
850 /* don't print anything for empty files */
854 if (popt->po_show_path) {
857 longbuf = strnchr(buf, buflen - 1, '\n') != NULL ||
858 buflen + strlen(param_name) >= 80;
859 printf("%s=%s", param_name, longbuf ? "\n" : "");
869 * Set a parameter to a specified value
871 * \param[in] path full path to the parameter
872 * \param[in] param_name lctl parameter format of the parameter path
873 * \param[in] popt set/get param options
874 * \param[in] value value to set the parameter to
876 * \retval number of bytes written on success.
877 * \retval -errno on error.
880 write_param(const char *path, const char *param_name, struct param_opts *popt,
889 /* Write the new value to the file */
890 fd = open(path, O_WRONLY);
893 fprintf(stderr, "error: set_param: opening '%s': %s\n",
894 path, strerror(errno));
898 count = write(fd, value, strlen(value));
902 fprintf(stderr, "error: set_param: setting %s=%s: %s\n",
903 path, value, strerror(errno));
905 } else if (count < strlen(value)) { /* Truncate case */
907 fprintf(stderr, "error: set_param: setting %s=%s: "
908 "wrote only %zd\n", path, value, count);
909 } else if (popt->po_show_path) {
910 printf("%s=%s\n", param_name, value);
918 * Perform a read, write or just a listing of a parameter
920 * \param[in] popt list,set,get parameter options
921 * \param[in] pattern search filter for the path of the parameter
922 * \param[in] value value to set the parameter if write operation
923 * \param[in] mode what operation to perform with the parameter
925 * \retval number of bytes written on success.
926 * \retval -errno on error and prints error message.
929 param_display(struct param_opts *popt, char *pattern, char *value,
930 enum parameter_operation mode)
935 char *opname = parameter_opname[mode];
938 rc = llapi_param_get_paths(pattern, &paths);
941 if (!popt->po_recursive) {
942 fprintf(stderr, "error: %s: param_path '%s': %s\n",
943 opname, pattern, strerror(errno));
948 dup_cache = calloc(paths.gl_pathc, sizeof(char *));
949 if (dup_cache == NULL) {
952 "error: %s: allocating '%s' dup_cache[%zd]: %s\n",
953 opname, pattern, paths.gl_pathc, strerror(-rc));
957 for (i = 0; i < paths.gl_pathc; i++) {
958 char *param_name = NULL, *tmp;
959 char pathname[PATH_MAX], param_dir[PATH_MAX+2];
963 if (stat(paths.gl_pathv[i], &st) == -1) {
964 fprintf(stderr, "error: %s: stat '%s': %s\n",
965 opname, paths.gl_pathv[i], strerror(errno));
971 if (popt->po_only_dir && !S_ISDIR(st.st_mode))
974 param_name = display_name(paths.gl_pathv[i], &st, popt);
975 if (param_name == NULL) {
977 "error: %s: generating name for '%s': %s\n",
978 opname, paths.gl_pathv[i], strerror(ENOMEM));
986 /* Read the contents of file to stdout */
987 if (S_ISREG(st.st_mode)) {
988 rc2 = read_param(paths.gl_pathv[i], param_name,
990 if (rc2 < 0 && rc == 0)
995 if (S_ISREG(st.st_mode)) {
996 rc2 = write_param(paths.gl_pathv[i],
997 param_name, popt, value);
998 if (rc2 < 0 && rc == 0)
1004 * For the upstream client the parameter files locations
1005 * are split between under both /sys/kernel/debug/lustre
1006 * and /sys/fs/lustre. The parameter files containing
1007 * small amounts of data, less than a page in size, are
1008 * located under /sys/fs/lustre and in the case of large
1009 * parameter data files, think stats for example, are
1010 * located in the debugfs tree. Since the files are split
1011 * across two trees the directories are often duplicated
1012 * which means these directories are listed twice which
1013 * leads to duplicate output to the user. To avoid
1014 * scanning a directory twice we have to cache any
1015 * directory and check if a search has been requested
1018 for (j = 0; j < dup_count; j++) {
1019 if (!strcmp(dup_cache[j], param_name))
1022 if (j != dup_count) {
1027 dup_cache[dup_count++] = strdup(param_name);
1029 if (popt->po_show_path)
1030 printf("%s\n", param_name);
1034 /* Only directories are searched recursively if
1035 * requested by the user */
1036 if (!S_ISDIR(st.st_mode) || !popt->po_recursive) {
1042 /* Turn param_name into file path format */
1043 rc2 = clean_path(popt, param_name);
1045 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1046 opname, param_name, strerror(-rc2));
1054 /* Use param_name to grab subdirectory tree from full path */
1055 snprintf(param_dir, sizeof(param_dir), "/%s", param_name);
1056 tmp = strstr(paths.gl_pathv[i], param_dir);
1058 /* cleanup paramname now that we are done with it */
1061 memset(¶m_dir, '\0', sizeof(param_dir));
1063 /* Shouldn't happen but just in case */
1071 rc2 = snprintf(pathname, sizeof(pathname), "%s/*", tmp);
1073 /* snprintf() should never an error, and if it does
1074 * there isn't much point trying to use fprintf() */
1077 if (rc2 >= sizeof(pathname)) {
1078 fprintf(stderr, "error: %s: overflow processing '%s'\n",
1085 rc2 = param_display(popt, pathname, value, mode);
1086 if (rc2 != 0 && rc2 != -ENOENT) {
1087 /* errors will be printed by param_display() */
1094 for (i = 0; i < dup_count; i++)
1098 llapi_param_paths_free(&paths);
1102 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
1106 popt->po_show_path = 1;
1107 popt->po_only_path = 1;
1109 while ((ch = getopt(argc, argv, "FRD")) != -1) {
1112 popt->po_show_type = 1;
1115 popt->po_recursive = 1;
1118 popt->po_only_dir = 1;
1128 int jt_lcfg_listparam(int argc, char **argv)
1130 int rc = 0, index, i;
1131 struct param_opts popt;
1134 memset(&popt, 0, sizeof(popt));
1135 index = listparam_cmdline(argc, argv, &popt);
1136 if (index < 0 || index >= argc)
1139 for (i = index; i < argc; i++) {
1144 rc2 = clean_path(&popt, path);
1146 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1147 jt_cmdname(argv[0]), path, strerror(-rc2));
1153 rc2 = param_display(&popt, path, NULL, LIST_PARAM);
1155 fprintf(stderr, "error: %s: listing '%s': %s\n",
1156 jt_cmdname(argv[0]), path, strerror(-rc2));
1166 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
1170 popt->po_show_path = 1;
1172 while ((ch = getopt(argc, argv, "FnNR")) != -1) {
1175 popt->po_show_type = 1;
1178 popt->po_show_path = 0;
1181 popt->po_only_path = 1;
1184 popt->po_recursive = 1;
1194 int jt_lcfg_getparam(int argc, char **argv)
1196 int rc = 0, index, i;
1197 struct param_opts popt;
1200 memset(&popt, 0, sizeof(popt));
1201 index = getparam_cmdline(argc, argv, &popt);
1202 if (index < 0 || index >= argc)
1205 for (i = index; i < argc; i++) {
1210 rc2 = clean_path(&popt, path);
1212 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1213 jt_cmdname(argv[0]), path, strerror(-rc2));
1219 rc2 = param_display(&popt, path, NULL,
1220 popt.po_only_path ? LIST_PARAM : GET_PARAM);
1232 * Output information about nodemaps.
1233 * \param argc number of args
1234 * \param argv[] variable string arguments
1236 * [list|nodemap_name|all] \a list will list all nodemaps (default).
1237 * Specifying a \a nodemap_name will
1238 * display info about that specific nodemap.
1239 * \a all will display info for all nodemaps.
1240 * \retval 0 on success
1242 int jt_nodemap_info(int argc, char **argv)
1244 const char usage_str[] = "usage: nodemap_info "
1245 "[list|nodemap_name|all]\n";
1246 struct param_opts popt;
1249 memset(&popt, 0, sizeof(popt));
1250 popt.po_show_path = 1;
1253 fprintf(stderr, usage_str);
1257 if (argc == 1 || strcmp("list", argv[1]) == 0) {
1258 popt.po_only_path = 1;
1259 popt.po_only_dir = 1;
1260 rc = param_display(&popt, "nodemap/*", NULL, LIST_PARAM);
1261 } else if (strcmp("all", argv[1]) == 0) {
1262 rc = param_display(&popt, "nodemap/*/*", NULL, LIST_PARAM);
1264 char pattern[PATH_MAX];
1266 snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
1267 rc = param_display(&popt, pattern, NULL, LIST_PARAM);
1269 fprintf(stderr, "error: nodemap_info: cannot find "
1270 "nodemap %s\n", argv[1]);
1275 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
1279 popt->po_show_path = 1;
1280 popt->po_only_path = 0;
1281 popt->po_show_type = 0;
1282 popt->po_recursive = 0;
1284 popt->po_delete = 0;
1287 while ((ch = getopt(argc, argv, "nPdF")) != -1) {
1290 popt->po_show_path = 0;
1296 popt->po_delete = 1;
1316 #define PS_PARAM_FOUND 1
1317 #define PS_PARAM_SET 2
1318 #define PS_VAL_FOUND 4
1319 #define PS_VAL_SET 8
1320 #define PS_DEVICE_FOUND 16
1321 #define PS_DEVICE_SET 32
1323 #define PARAM_SZ 256
1325 static struct cfg_type_data {
1326 enum paramtype ptype;
1328 } cfg_type_table[] = {
1329 { PT_SETPARAM, "set_param" },
1330 { PT_CONFPARAM, "conf_param" },
1334 static struct cfg_stage_data {
1337 } cfg_stage_table[] = {
1338 { PS_PARAM_FOUND, "parameter" },
1339 { PS_VAL_FOUND, "value" },
1340 { PS_DEVICE_FOUND, "device" },
1345 void conf_to_set_param(enum paramtype confset, const char *param,
1346 const char *device, char *buf,
1351 if (confset == PT_SETPARAM) {
1352 strncpy(buf, param, bufsize);
1357 * sys.* params are top level, we just need to trim the sys.
1359 tmp = strstr(param, "sys.");
1362 strncpy(buf, tmp, bufsize);
1367 * parameters look like type.parameter, we need to stick the device
1368 * in the middle. Example combine mdt.identity_upcall with device
1369 * lustre-MDT0000 for mdt.lustre-MDT0000.identity_upcall
1372 tmp = strchrnul(param, '.');
1373 snprintf(buf, tmp - param + 1, "%s", param);
1375 bufsize -= tmp - param;
1376 snprintf(buf, bufsize, ".%s%s", device, tmp);
1379 int lcfg_setparam_yaml(char *func, char *filename)
1382 yaml_parser_t parser;
1386 enum paramtype confset = PT_NONE;
1387 int param = PS_NONE;
1389 char parameter[PARAM_SZ + 1];
1390 char value[PARAM_SZ + 1];
1391 char device[PARAM_SZ + 1];
1393 file = fopen(filename, "rb");
1394 yaml_parser_initialize(&parser);
1395 yaml_parser_set_input_file(&parser, file);
1398 * Search tokens for conf_param or set_param
1399 * The token after "parameter" goes into parameter
1400 * The token after "value" goes into value
1401 * when we have all 3, create param=val and call the
1402 * appropriate function for set/conf param
1404 while (token.type != YAML_STREAM_END_TOKEN && rc == 0) {
1407 yaml_token_delete(&token);
1408 if (!yaml_parser_scan(&parser, &token)) {
1413 if (token.type != YAML_SCALAR_TOKEN)
1416 for (i = 0; cfg_type_table[i].ptype != PT_NONE; i++) {
1417 if (!strncmp((char *)token.data.alias.value,
1418 cfg_type_table[i].type_name,
1419 strlen(cfg_type_table[i].type_name))) {
1420 confset = cfg_type_table[i].ptype;
1425 if (confset == PT_NONE)
1428 for (i = 0; cfg_stage_table[i].pstage != PS_NONE; i++) {
1429 if (!strncmp((char *)token.data.alias.value,
1430 cfg_stage_table[i].stage_name,
1431 strlen(cfg_stage_table[i].stage_name))) {
1432 param |= cfg_stage_table[i].pstage;
1437 if (cfg_stage_table[i].pstage != PS_NONE)
1440 if (param & PS_PARAM_FOUND) {
1441 conf_to_set_param(confset,
1442 (char *)token.data.alias.value,
1443 device, parameter, PARAM_SZ);
1444 param |= PS_PARAM_SET;
1445 param &= ~PS_PARAM_FOUND;
1448 * we're getting parameter: param=val
1449 * copy val and mark that we've got it in case
1450 * there is no value: tag
1452 tmp = strchrnul(parameter, '=');
1454 strncpy(value, tmp + 1, sizeof(value) - 1);
1456 param |= PS_VAL_SET;
1460 } else if (param & PS_VAL_FOUND) {
1461 strncpy(value, (char *)token.data.alias.value,
1463 param |= PS_VAL_SET;
1464 param &= ~PS_VAL_FOUND;
1465 } else if (param & PS_DEVICE_FOUND) {
1466 strncpy(device, (char *)token.data.alias.value,
1468 param |= PS_DEVICE_SET;
1469 param &= ~PS_DEVICE_FOUND;
1472 if (confset && param & PS_VAL_SET && param & PS_PARAM_SET) {
1473 int size = strlen(parameter) + strlen(value) + 2;
1474 char *buf = malloc(size);
1480 snprintf(buf, size, "%s=%s", parameter, value);
1482 printf("set_param: %s\n", buf);
1483 rc = lcfg_setparam_perm(func, buf);
1487 parameter[0] = '\0';
1494 yaml_parser_delete(&parser);
1500 int jt_lcfg_setparam(int argc, char **argv)
1502 int rc = 0, index, i;
1503 struct param_opts popt;
1504 char *path = NULL, *value = NULL;
1506 memset(&popt, 0, sizeof(popt));
1507 index = setparam_cmdline(argc, argv, &popt);
1508 if (index < 0 || index >= argc)
1512 /* We can't delete parameters that were
1513 * set with old conf_param interface */
1514 return jt_lcfg_setparam_perm(argc, argv, &popt);
1517 return lcfg_setparam_yaml(argv[0], argv[index]);
1519 for (i = index; i < argc; i++) {
1523 value = strchr(path, '=');
1524 if (value != NULL) {
1525 /* format: set_param a=b */
1528 if (*value == '\0') {
1530 "error: %s: setting %s: no value\n",
1531 jt_cmdname(argv[0]), path);
1537 /* format: set_param a b */
1541 "error: %s: setting %s: no value\n",
1542 jt_cmdname(argv[0]), path);
1551 rc2 = clean_path(&popt, path);
1553 fprintf(stderr, "error: %s: cleaning %s: %s\n",
1554 jt_cmdname(argv[0]), path, strerror(-rc2));
1560 rc2 = param_display(&popt, path, value, SET_PARAM);