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/
31 * lustre/utils/lustre_cfg.c
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Andreas Dilger <adilger@clusterfs.com>
36 * 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>
64 #include <linux/lustre/lustre_kernelcomm.h>
65 #include <lnetconfig/liblnetconfig.h>
66 #include "lustreapi_internal.h"
78 static char *lcfg_devname;
80 int lcfg_set_devname(char *name)
88 /* quietly strip the unnecessary '$' */
89 if (*name == '$' || *name == '%')
93 while (*ptr != '\0') {
102 /* We can't translate from dev # to name */
105 lcfg_devname = strdup(name);
113 char *lcfg_get_devname(void)
118 int jt_lcfg_device(int argc, char **argv)
120 return jt_obd_device(argc, argv);
123 static int jt_lcfg_ioctl(struct lustre_cfg_bufs *bufs, char *arg, int cmd)
125 struct lustre_cfg *lcfg;
128 lcfg = malloc(lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
132 lustre_cfg_init(lcfg, cmd, bufs);
133 rc = lcfg_ioctl(arg, OBD_DEV_ID, lcfg);
137 fprintf(stderr, "error: %s: %s\n", jt_cmdname(arg),
138 strerror(rc = errno));
142 int jt_lcfg_attach(int argc, char **argv)
144 struct lustre_cfg_bufs bufs;
150 lustre_cfg_bufs_reset(&bufs, NULL);
152 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
153 lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
154 lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
156 rc = jt_lcfg_ioctl(&bufs, argv[0], LCFG_ATTACH);
158 lcfg_set_devname(argv[2]);
163 int jt_lcfg_setup(int argc, char **argv)
165 struct lustre_cfg_bufs bufs;
170 "%s: please use 'device name' to set the device name for config commands.\n",
171 jt_cmdname(argv[0]));
175 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
180 for (i = 1; i < argc; i++)
181 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
183 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_SETUP);
186 int jt_obd_detach(int argc, char **argv)
188 struct lustre_cfg_bufs bufs;
192 "%s: please use 'device name' to set the device name for config commands.\n",
193 jt_cmdname(argv[0]));
197 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
202 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DETACH);
205 int jt_obd_cleanup(int argc, char **argv)
207 struct lustre_cfg_bufs bufs;
210 char flags[3] = { 0 };
215 "%s: please use 'device name' to set the device name for config commands.\n",
216 jt_cmdname(argv[0]));
220 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
222 if (argc < 1 || argc > 3)
226 * we are protected from overflowing our buffer by the argc
229 for (n = 1; n < argc; n++) {
230 if (strcmp(argv[n], "force") == 0) {
231 flags[flag_cnt++] = force;
232 } else if (strcmp(argv[n], "failover") == 0) {
233 flags[flag_cnt++] = failover;
235 fprintf(stderr, "unknown option: %s\n", argv[n]);
241 lustre_cfg_bufs_set_string(&bufs, 1, flags);
243 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_CLEANUP);
247 int do_add_uuid(char *func, char *uuid, lnet_nid_t nid)
250 struct lustre_cfg_bufs bufs;
251 struct lustre_cfg *lcfg;
253 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
255 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
257 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
261 lustre_cfg_init(lcfg, LCFG_ADD_UUID, &bufs);
262 lcfg->lcfg_nid = nid;
264 rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
268 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
274 printf("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
279 int jt_lcfg_add_uuid(int argc, char **argv)
286 nid = libcfs_str2nid(argv[2]);
287 if (nid == LNET_NID_ANY) {
288 fprintf(stderr, "Can't parse NID %s\n", argv[2]);
292 return do_add_uuid(argv[0], argv[1], nid);
295 int jt_lcfg_del_uuid(int argc, char **argv)
297 struct lustre_cfg_bufs bufs;
300 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
304 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
305 if (strcmp(argv[1], "_all_"))
306 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
308 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_UUID);
311 int jt_lcfg_del_mount_option(int argc, char **argv)
313 struct lustre_cfg_bufs bufs;
318 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
321 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
323 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
326 int jt_lcfg_set_timeout(int argc, char **argv)
329 struct lustre_cfg_bufs bufs;
330 struct lustre_cfg *lcfg;
333 "%s has been deprecated. Use conf_param instead.\ne.g. conf_param lustre-MDT0000 obd_timeout=50\n",
334 jt_cmdname(argv[0]));
340 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
342 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
346 lustre_cfg_init(lcfg, LCFG_SET_TIMEOUT, &bufs);
347 lcfg->lcfg_num = atoi(argv[1]);
349 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
353 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
354 strerror(rc = errno));
359 int jt_lcfg_add_conn(int argc, char **argv)
361 struct lustre_cfg_bufs bufs;
362 struct lustre_cfg *lcfg;
375 "%s: please use 'device name' to set the device name for config commands.\n",
376 jt_cmdname(argv[0]));
380 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
382 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
384 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
388 lustre_cfg_init(lcfg, LCFG_ADD_CONN, &bufs);
389 lcfg->lcfg_num = priority;
391 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
395 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
396 strerror(rc = errno));
402 int jt_lcfg_del_conn(int argc, char **argv)
404 struct lustre_cfg_bufs bufs;
411 "%s: please use 'device name' to set the device name for config commands.\n",
412 jt_cmdname(argv[0]));
416 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
418 /* connection uuid */
419 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
421 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
424 /* Param set locally, directly on target */
425 int jt_lcfg_param(int argc, char **argv)
427 struct lustre_cfg_bufs bufs;
430 if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
433 lustre_cfg_bufs_reset(&bufs, NULL);
435 for (i = 1; i < argc; i++)
436 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;
450 unsigned int po_yaml:1;
451 unsigned int po_detail:1;
454 int lcfg_setparam_perm(char *func, char *buf)
457 struct lustre_cfg_bufs bufs;
458 struct lustre_cfg *lcfg;
460 lustre_cfg_bufs_reset(&bufs, NULL);
462 * This same command would be executed on all nodes, many
463 * of which should fail (silently) because they don't have
464 * that proc file existing locally. There would be no
465 * preprocessing on the MGS to try to figure out which
466 * parameter files to add this to, there would be nodes
467 * processing on the cluster nodes to try to figure out
468 * if they are the intended targets. They will blindly
469 * try to set the parameter, and ENOTFOUND means it wasn't
471 * Target name "general" means call on all targets. It is
472 * left here in case some filtering will be added in
475 lustre_cfg_bufs_set_string(&bufs, 0, "general");
477 lustre_cfg_bufs_set_string(&bufs, 1, buf);
479 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount,
483 fprintf(stderr, "error: allocating lcfg for %s: %s\n",
484 jt_cmdname(func), strerror(rc));
487 lustre_cfg_init(lcfg, LCFG_SET_PARAM, &bufs);
488 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
490 fprintf(stderr, "error: executing %s: %s\n",
491 jt_cmdname(func), strerror(errno));
499 * Param set to single log file, used by all clients and servers.
500 * This should be loaded after the individual config logs.
501 * Called from set param with -P option.
503 static int jt_lcfg_setparam_perm(int argc, char **argv,
504 struct param_opts *popt)
511 first_param = optind;
512 if (first_param < 0 || first_param >= argc)
515 for (i = first_param, rc = 0; i < argc; i++) {
517 if (popt->po_delete) {
522 /* Consider param ends at the first '=' in the buffer
523 * and make sure it always ends with '=' as well
525 end_pos = memchr(buf, '=', len - 1);
528 } else if (buf[len - 1] != '=') {
529 buf = malloc(len + 2);
532 sprintf(buf, "%s=", argv[i]);
536 rc = lcfg_setparam_perm(argv[0], buf);
544 int lcfg_conf_param(char *func, char *buf)
547 struct lustre_cfg_bufs bufs;
548 struct lustre_cfg *lcfg;
550 lustre_cfg_bufs_reset(&bufs, NULL);
551 lustre_cfg_bufs_set_string(&bufs, 1, buf);
553 /* We could put other opcodes here. */
554 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
558 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
559 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
569 * Param set in config log on MGS
570 * conf_param key=value
572 * Note we can actually send mgc conf_params from clients, but currently
573 * that's only done for default file striping (see ll_send_mgc_param),
576 * After removal of a parameter (-d) Lustre will use the default
577 * AT NEXT REBOOT, not immediately.
579 int jt_lcfg_confparam(int argc, char **argv)
585 /* mgs_setparam processes only lctl buf #1 */
586 if ((argc > 3) || (argc <= 1))
589 while ((rc = getopt(argc, argv, "d")) != -1) {
604 /* for delete, make it "<param>=\0" */
605 buf = malloc(strlen(argv[optind]) + 2);
610 /* put an '=' on the end in case it doesn't have one */
611 sprintf(buf, "%s=", argv[optind]);
612 /* then truncate after the first '=' */
613 ptr = strchr(buf, '=');
617 rc = lcfg_conf_param(argv[0], buf);
619 if (buf != argv[optind])
623 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
631 * Display a parameter path in the same format as sysctl.
632 * E.g. obdfilter.lustre-OST0000.stats
634 * \param[in] filename file name of the parameter
635 * \param[in] st parameter file stats
636 * \param[in] popt set/get param options
638 * \retval allocated pointer containing modified filename
641 display_name(const char *filename, struct stat *st, struct param_opts *popt)
643 size_t suffix_len = 0;
648 if (popt->po_show_type) {
649 if (S_ISDIR(st->st_mode))
651 else if (S_ISLNK(st->st_mode))
653 else if (st->st_mode & S_IWUSR)
657 /* Take the original filename string and chop off the glob addition */
658 tmp = strstr(filename, "/lustre/");
660 tmp = strstr(filename, "/lnet/");
662 tmp += strlen("/lnet/");
664 tmp += strlen("/lustre/");
667 /* Allocate return string */
668 param_name = strdup(tmp);
672 /* replace '/' with '.' to match conf_param and sysctl */
673 for (tmp = strchr(param_name, '/'); tmp != NULL; tmp = strchr(tmp, '/'))
676 /* Append the indicator to entries if needed. */
677 if (popt->po_show_type && suffix != NULL) {
678 suffix_len = strlen(suffix);
680 tmp = realloc(param_name, suffix_len + strlen(param_name) + 1);
683 strncat(param_name, suffix,
684 strlen(param_name) + suffix_len);
692 * Turns a lctl parameter string into a procfs/sysfs subdirectory path pattern.
694 * \param[in] popt Used to control parameter usage. For this
695 * function it is used to see if the path has
697 * \param[in,out] path lctl parameter string that is turned into
698 * the subdirectory path pattern that is used
699 * to search the procfs/sysfs tree.
701 * \retval -errno on error.
704 clean_path(struct param_opts *popt, char *path)
706 char *nidstart = NULL;
710 if (popt == NULL || path == NULL || strlen(path) == 0)
713 /* If path contains a suffix we need to remove it */
714 if (popt->po_show_type) {
715 size_t path_end = strlen(path) - 1;
717 tmp = path + path_end;
728 /* get rid of '\', glob doesn't like it */
729 tmp = strrchr(path, '\\');
731 char *tail = path + strlen(path);
733 while (tmp != path) {
735 memmove(tmp, tmp + 1, tail - tmp);
742 /* Does path contain a NID string? Skip '.->/' replacement for it. */
743 tmp = strchr(path, '@');
745 /* First find the NID start. NIDs may have variable (0-4) '.',
746 * so find the common NID prefixes instead of trying to count
747 * the dots. Not great, but there are only two, and faster
748 * than multiple speculative NID parses and bad DNS lookups.
750 if ((tmp = strstr(path, ".exports.")))
751 nidstart = tmp + strlen(".exports.");
752 else if ((tmp = strstr(path, ".MGC")))
755 /* Next, find the end of the NID string. */
757 nidend = strchrnul(strchr(nidstart, '@'), '.');
760 /* replace param '.' with '/' */
761 for (tmp = strchr(path, '.'); tmp != NULL; tmp = strchr(tmp, '.')) {
765 * There exist cases where some of the subdirectories of the
766 * the parameter tree has embedded in its name a NID string.
767 * This means that it is possible that these subdirectories
768 * could have actual '.' in its name. If this is the case we
769 * don't want to blindly replace the '.' with '/', so skip
770 * over the part of the parameter containing the NID.
780 * The application lctl can perform three operations for lustre
781 * tunables. This enum defines those three operations which are
783 * 1) LIST_PARAM - list available tunables
784 * 2) GET_PARAM - report the current setting of a tunable
785 * 3) SET_PARAM - set the tunable to a new value
787 enum parameter_operation {
793 char *parameter_opname[] = {
794 [LIST_PARAM] = "list_param",
795 [GET_PARAM] = "get_param",
796 [SET_PARAM] = "set_param",
800 * Read the value of parameter
802 * \param[in] path full path to the parameter
803 * \param[in] param_name lctl parameter format of the
805 * \param[in] popt set/get param options
807 * \retval 0 on success.
808 * \retval -errno on error.
811 read_param(const char *path, const char *param_name, struct param_opts *popt)
817 rc = llapi_param_get_value(path, &buf, &buflen);
820 "error: read_param: \'%s\': %s\n",
821 path, strerror(-rc));
824 /* don't print anything for empty files */
828 if (popt->po_show_path) {
831 longbuf = memchr(buf, '\n', buflen - 1) ||
832 buflen + strlen(param_name) >= 80;
833 printf("%s=%s", param_name, longbuf ? "\n" : "");
843 * Set a parameter to a specified value
845 * \param[in] path full path to the parameter
846 * \param[in] param_name lctl parameter format of the parameter path
847 * \param[in] popt set/get param options
848 * \param[in] value value to set the parameter to
850 * \retval number of bytes written on success.
851 * \retval -errno on error.
854 write_param(const char *path, const char *param_name, struct param_opts *popt,
863 /* Write the new value to the file */
864 fd = open(path, O_WRONLY);
867 fprintf(stderr, "error: set_param: opening '%s': %s\n",
868 path, strerror(errno));
872 count = write(fd, value, strlen(value));
876 fprintf(stderr, "error: set_param: setting %s=%s: %s\n",
877 path, value, strerror(errno));
879 } else if (count < strlen(value)) { /* Truncate case */
882 "error: set_param: setting %s=%s: wrote only %zd\n",
884 } else if (popt->po_show_path) {
885 printf("%s=%s\n", param_name, value);
892 void print_obd_line(char *s)
894 const char *param = "osc/%s/ost_conn_uuid";
895 char obd_name[MAX_OBD_NAME];
896 char buf[MAX_OBD_NAME];
901 /* obd device type is the first 3 characters of param name */
902 snprintf(buf, sizeof(buf), " %%*d %%*s %.3s %%%zus %%*s %%*d ",
903 param, sizeof(obd_name) - 1);
904 if (sscanf(s, buf, obd_name) == 0)
906 if (cfs_get_param_paths(&path, param, obd_name) != 0)
908 fp = fopen(path.gl_pathv[0], "r");
910 /* need to free path data before retry */
911 cfs_free_param_data(&path);
913 if (param[0] == 'o') { /* failed with osc, try mdc */
914 param = "mdc/%s/mds_conn_uuid";
921 /* should not ignore fgets(3)'s return value */
922 if (!fgets(buf, sizeof(buf), fp)) {
923 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
929 cfs_free_param_data(&path);
931 /* trim trailing newlines */
932 ptr = strrchr(buf, '\n');
936 ptr = strrchr(s, '\n');
939 printf("%s%s%s\n", s, buf[0] ? " " : "", buf);
942 int yaml_get_device_index(char *source)
944 yaml_emitter_t request;
951 sk = nl_socket_alloc();
955 /* Setup parser to recieve Netlink packets */
956 rc = yaml_parser_initialize(&reply);
960 rc = yaml_parser_set_input_netlink(&reply, sk, false);
964 /* Create Netlink emitter to send request to kernel */
965 yaml_emitter_initialize(&request);
966 rc = yaml_emitter_set_output_netlink(&request, sk, "lustre",
968 LUSTRE_CMD_DEVICES, NLM_F_DUMP);
972 yaml_emitter_open(&request);
974 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
975 rc = yaml_emitter_emit(&request, &event);
979 yaml_mapping_start_event_initialize(&event, NULL,
980 (yaml_char_t *)YAML_MAP_TAG,
981 1, YAML_ANY_MAPPING_STYLE);
982 rc = yaml_emitter_emit(&request, &event);
986 yaml_scalar_event_initialize(&event, NULL,
987 (yaml_char_t *)YAML_STR_TAG,
988 (yaml_char_t *)"devices",
989 strlen("devices"), 1, 0,
990 YAML_PLAIN_SCALAR_STYLE);
991 rc = yaml_emitter_emit(&request, &event);
995 yaml_sequence_start_event_initialize(&event, NULL,
996 (yaml_char_t *)YAML_SEQ_TAG,
997 1, YAML_ANY_SEQUENCE_STYLE);
998 rc = yaml_emitter_emit(&request, &event);
1002 yaml_mapping_start_event_initialize(&event, NULL,
1003 (yaml_char_t *)YAML_MAP_TAG,
1004 1, YAML_ANY_MAPPING_STYLE);
1005 rc = yaml_emitter_emit(&request, &event);
1009 yaml_scalar_event_initialize(&event, NULL,
1010 (yaml_char_t *)YAML_STR_TAG,
1011 (yaml_char_t *)"name",
1013 1, 0, YAML_PLAIN_SCALAR_STYLE);
1014 rc = yaml_emitter_emit(&request, &event);
1018 yaml_scalar_event_initialize(&event, NULL,
1019 (yaml_char_t *)YAML_STR_TAG,
1020 (yaml_char_t *)source,
1021 strlen(source), 1, 0,
1022 YAML_PLAIN_SCALAR_STYLE);
1023 rc = yaml_emitter_emit(&request, &event);
1027 yaml_mapping_end_event_initialize(&event);
1028 rc = yaml_emitter_emit(&request, &event);
1032 yaml_sequence_end_event_initialize(&event);
1033 rc = yaml_emitter_emit(&request, &event);
1037 yaml_mapping_end_event_initialize(&event);
1038 rc = yaml_emitter_emit(&request, &event);
1042 yaml_document_end_event_initialize(&event, 0);
1043 rc = yaml_emitter_emit(&request, &event);
1047 yaml_emitter_close(&request);
1050 yaml_emitter_log_error(&request, stderr);
1053 yaml_emitter_delete(&request);
1056 rc = yaml_parser_parse(&reply, &event);
1058 yaml_parser_log_error(&reply, stdout, "lctl: ");
1063 if (event.type == YAML_SCALAR_EVENT) {
1064 char *value = (char *)event.data.scalar.value;
1066 if (strcmp(value, "index") == 0) {
1067 yaml_event_delete(&event);
1068 rc = yaml_parser_parse(&reply, &event);
1070 value = (char *)event.data.scalar.value;
1072 rc = strtoul(value, NULL, 10);
1074 yaml_event_delete(&event);
1081 done = (event.type == YAML_STREAM_END_EVENT);
1082 yaml_event_delete(&event);
1091 * Perform a read, write or just a listing of a parameter
1093 * \param[in] popt list,set,get parameter options
1094 * \param[in] pattern search filter for the path of the parameter
1095 * \param[in] value value to set the parameter if write operation
1096 * \param[in] mode what operation to perform with the parameter
1098 * \retval number of bytes written on success.
1099 * \retval -errno on error and prints error message.
1102 param_display(struct param_opts *popt, char *pattern, char *value,
1103 enum parameter_operation mode)
1108 char *opname = parameter_opname[mode];
1111 rc = llapi_param_get_paths(pattern, &paths);
1114 if (!popt->po_recursive && !(rc == -ENOENT && getuid() != 0)) {
1115 fprintf(stderr, "error: %s: param_path '%s': %s\n",
1116 opname, pattern, strerror(errno));
1121 dup_cache = calloc(paths.gl_pathc, sizeof(char *));
1125 "error: %s: allocating '%s' dup_cache[%zd]: %s\n",
1126 opname, pattern, paths.gl_pathc, strerror(-rc));
1130 for (i = 0; i < paths.gl_pathc; i++) {
1131 char *param_name = NULL, *tmp;
1132 char pathname[PATH_MAX], param_dir[PATH_MAX + 2];
1136 if (stat(paths.gl_pathv[i], &st) == -1) {
1137 fprintf(stderr, "error: %s: stat '%s': %s\n",
1138 opname, paths.gl_pathv[i], strerror(errno));
1144 if (popt->po_only_dir && !S_ISDIR(st.st_mode))
1147 param_name = display_name(paths.gl_pathv[i], &st, popt);
1150 "error: %s: generating name for '%s': %s\n",
1151 opname, paths.gl_pathv[i], strerror(ENOMEM));
1159 /* Read the contents of file to stdout */
1160 if (S_ISREG(st.st_mode)) {
1161 rc2 = read_param(paths.gl_pathv[i], param_name,
1163 if (rc2 < 0 && rc == 0)
1168 if (S_ISREG(st.st_mode)) {
1169 rc2 = write_param(paths.gl_pathv[i],
1170 param_name, popt, value);
1171 if (rc2 < 0 && rc == 0)
1177 * For the upstream client the parameter files locations
1178 * are split between under both /sys/kernel/debug/lustre
1179 * and /sys/fs/lustre. The parameter files containing
1180 * small amounts of data, less than a page in size, are
1181 * located under /sys/fs/lustre and in the case of large
1182 * parameter data files, think stats for example, are
1183 * located in the debugfs tree. Since the files are
1184 * split across two trees the directories are often
1185 * duplicated which means these directories are listed
1186 * twice which leads to duplicate output to the user.
1187 * To avoid scanning a directory twice we have to cache
1188 * any directory and check if a search has been
1191 for (j = 0; j < dup_count; j++) {
1192 if (!strcmp(dup_cache[j], param_name))
1195 if (j != dup_count) {
1200 dup_cache[dup_count++] = strdup(param_name);
1202 if (popt->po_show_path)
1203 printf("%s\n", param_name);
1208 * Only directories are searched recursively if
1209 * requested by the user
1211 if (!S_ISDIR(st.st_mode) || !popt->po_recursive) {
1217 /* Turn param_name into file path format */
1218 rc2 = clean_path(popt, param_name);
1220 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1221 opname, param_name, strerror(-rc2));
1229 /* Use param_name to grab subdirectory tree from full path */
1230 snprintf(param_dir, sizeof(param_dir), "/%s", param_name);
1231 tmp = strstr(paths.gl_pathv[i], param_dir);
1233 /* cleanup paramname now that we are done with it */
1236 memset(¶m_dir, '\0', sizeof(param_dir));
1238 /* Shouldn't happen but just in case */
1246 rc2 = snprintf(pathname, sizeof(pathname), "%s/*", tmp);
1249 * snprintf() should never an error, and if it does
1250 * there isn't much point trying to use fprintf()
1254 if (rc2 >= sizeof(pathname)) {
1255 fprintf(stderr, "error: %s: overflow processing '%s'\n",
1262 rc2 = param_display(popt, pathname, value, mode);
1263 if (rc2 != 0 && rc2 != -ENOENT) {
1264 /* errors will be printed by param_display() */
1271 for (i = 0; i < dup_count; i++)
1275 llapi_param_paths_free(&paths);
1279 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
1283 popt->po_show_path = 1;
1284 popt->po_only_path = 1;
1286 while ((ch = getopt(argc, argv, "FRD")) != -1) {
1289 popt->po_show_type = 1;
1292 popt->po_recursive = 1;
1295 popt->po_only_dir = 1;
1305 int jt_lcfg_listparam(int argc, char **argv)
1307 int rc = 0, index, i;
1308 struct param_opts popt;
1311 memset(&popt, 0, sizeof(popt));
1312 index = listparam_cmdline(argc, argv, &popt);
1313 if (index < 0 || index >= argc)
1316 for (i = index; i < argc; i++) {
1321 rc2 = clean_path(&popt, path);
1323 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1324 jt_cmdname(argv[0]), path, strerror(-rc2));
1330 rc2 = param_display(&popt, path, NULL, LIST_PARAM);
1335 if (rc2 == -ENOENT && getuid() != 0)
1336 rc2 = llapi_param_display_value(path, 0, 0,
1339 fprintf(stderr, "error: %s: listing '%s': %s\n",
1340 jt_cmdname(argv[0]), path,
1350 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
1354 popt->po_show_path = 1;
1356 while ((ch = getopt(argc, argv, "FnNRy")) != -1) {
1359 popt->po_show_type = 1;
1362 popt->po_show_path = 0;
1365 popt->po_only_path = 1;
1368 popt->po_recursive = 1;
1381 int jt_lcfg_getparam(int argc, char **argv)
1383 int version = LUSTRE_GENL_VERSION;
1384 enum parameter_operation mode;
1385 int rc = 0, index, i;
1386 struct param_opts popt;
1390 memset(&popt, 0, sizeof(popt));
1391 index = getparam_cmdline(argc, argv, &popt);
1392 if (index < 0 || index >= argc)
1395 mode = popt.po_only_path ? LIST_PARAM : GET_PARAM;
1396 if (mode == LIST_PARAM)
1400 flags |= PARAM_FLAGS_YAML_FORMAT;
1401 if (popt.po_show_path)
1402 flags |= PARAM_FLAGS_SHOW_SOURCE;
1404 for (i = index; i < argc; i++) {
1409 rc2 = clean_path(&popt, path);
1411 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1412 jt_cmdname(argv[0]), path, strerror(-rc2));
1418 rc2 = param_display(&popt, path, NULL, mode);
1423 if (rc2 == -ENOENT && getuid() != 0)
1424 rc2 = llapi_param_display_value(path, version,
1433 /* get device list by netlink or debugfs */
1434 int jt_device_list(int argc, char **argv)
1436 static const struct option long_opts[] = {
1437 { .name = "target", .has_arg = no_argument, .val = 't' },
1438 { .name = "yaml", .has_arg = no_argument, .val = 'y' },
1441 struct param_opts opts;
1442 char buf[MAX_OBD_NAME];
1448 if (optind + 1 < argc)
1451 memset(&opts, 0, sizeof(opts));
1453 while ((c = getopt_long(argc, argv, "ty", long_opts, NULL)) != -1) {
1456 flags |= PARAM_FLAGS_EXTRA_DETAILS;
1457 opts.po_detail = true;
1460 flags |= PARAM_FLAGS_YAML_FORMAT;
1461 opts.po_yaml = true;
1468 if (optind < argc) {
1474 /* Use YAML to list all devices */
1475 rc = llapi_param_display_value("devices", LUSTRE_GENL_VERSION, flags,
1480 rc = llapi_param_get_paths("devices", &path);
1484 fp = fopen(path.gl_pathv[0], "r");
1486 cfs_free_param_data(&path);
1490 while (fgets(buf, sizeof(buf), fp) != NULL)
1492 print_obd_line(buf);
1496 cfs_free_param_data(&path);
1501 static int do_name2dev(char *func, char *name, int dev_id)
1503 struct obd_ioctl_data data;
1504 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1507 /* Use YAML to find device index */
1508 rc = yaml_get_device_index(name);
1509 if (rc >= 0 || rc != -EOPNOTSUPP)
1512 memset(&data, 0, sizeof(data));
1513 data.ioc_dev = dev_id;
1514 data.ioc_inllen1 = strlen(name) + 1;
1515 data.ioc_inlbuf1 = name;
1517 memset(buf, 0, sizeof(rawbuf));
1518 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1520 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
1523 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
1526 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1528 fprintf(stderr, "error: %s: invalid reply\n", jt_cmdname(func));
1532 return data.ioc_dev;
1536 * resolve a device name to a device number.
1537 * supports a number, $name or %uuid.
1539 int parse_devname(char *func, char *name, int dev_id)
1547 /* Test if its a pure number string */
1548 if (strspn(name, "0123456789") != strlen(name)) {
1549 if (name[0] == '$' || name[0] == '%')
1552 rc = do_name2dev(func, name, dev_id);
1557 ret = strtoul(name, NULL, 10);
1563 fprintf(stderr, "No device found for name %s: %s\n",
1564 name, strerror(rc));
1568 #ifdef HAVE_SERVER_SUPPORT
1570 * Output information about nodemaps.
1571 * \param argc number of args
1572 * \param argv[] variable string arguments
1574 * [list|nodemap_name|all] \a list will list all nodemaps (default).
1575 * Specifying a \a nodemap_name will
1576 * display info about that specific nodemap.
1577 * \a all will display info for all nodemaps.
1578 * \retval 0 on success
1580 int jt_nodemap_info(int argc, char **argv)
1582 const char usage_str[] = "usage: nodemap_info [list|nodemap_name|all]\n";
1583 struct param_opts popt;
1586 memset(&popt, 0, sizeof(popt));
1587 popt.po_show_path = 1;
1590 fprintf(stderr, usage_str);
1594 if (argc == 1 || strcmp("list", argv[1]) == 0) {
1595 popt.po_only_path = 1;
1596 popt.po_only_dir = 1;
1597 rc = param_display(&popt, "nodemap/*", NULL, LIST_PARAM);
1598 } else if (strcmp("all", argv[1]) == 0) {
1599 rc = param_display(&popt, "nodemap/*/*", NULL, LIST_PARAM);
1601 char pattern[PATH_MAX];
1603 snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
1604 rc = param_display(&popt, pattern, NULL, LIST_PARAM);
1607 "error: nodemap_info: cannot find nodemap %s\n",
1614 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
1618 popt->po_show_path = 1;
1619 popt->po_only_path = 0;
1620 popt->po_show_type = 0;
1621 popt->po_recursive = 0;
1623 popt->po_delete = 0;
1626 while ((ch = getopt(argc, argv, "nPdF")) != -1) {
1629 popt->po_show_path = 0;
1635 popt->po_delete = 1;
1654 #define PS_PARAM_FOUND 1
1655 #define PS_PARAM_SET 2
1656 #define PS_VAL_FOUND 4
1657 #define PS_VAL_SET 8
1658 #define PS_DEVICE_FOUND 16
1659 #define PS_DEVICE_SET 32
1661 #define PARAM_SZ 256
1663 static struct cfg_type_data {
1664 enum paramtype ptype;
1666 } cfg_type_table[] = {
1667 { PT_SETPARAM, "set_param" },
1668 { PT_CONFPARAM, "conf_param" },
1672 static struct cfg_stage_data {
1675 } cfg_stage_table[] = {
1676 { PS_PARAM_FOUND, "parameter" },
1677 { PS_VAL_FOUND, "value" },
1678 { PS_DEVICE_FOUND, "device" },
1682 void conf_to_set_param(enum paramtype confset, const char *param,
1683 const char *device, char *buf,
1688 if (confset == PT_SETPARAM) {
1689 strncpy(buf, param, bufsize);
1694 * sys.* params are top level, we just need to trim the sys.
1696 tmp = strstr(param, "sys.");
1699 strncpy(buf, tmp, bufsize);
1704 * parameters look like type.parameter, we need to stick the device
1705 * in the middle. Example combine mdt.identity_upcall with device
1706 * lustre-MDT0000 for mdt.lustre-MDT0000.identity_upcall
1709 tmp = strchrnul(param, '.');
1710 snprintf(buf, tmp - param + 1, "%s", param);
1712 bufsize -= tmp - param;
1713 snprintf(buf, bufsize, ".%s%s", device, tmp);
1716 int lcfg_setparam_yaml(char *func, char *filename)
1719 yaml_parser_t parser;
1723 enum paramtype confset = PT_NONE;
1724 int param = PS_NONE;
1726 char parameter[PARAM_SZ + 1];
1727 char value[PARAM_SZ + 1];
1728 char device[PARAM_SZ + 1];
1730 file = fopen(filename, "rb");
1731 yaml_parser_initialize(&parser);
1732 yaml_parser_set_input_file(&parser, file);
1735 * Search tokens for conf_param or set_param
1736 * The token after "parameter" goes into parameter
1737 * The token after "value" goes into value
1738 * when we have all 3, create param=val and call the
1739 * appropriate function for set/conf param
1741 while (token.type != YAML_STREAM_END_TOKEN && rc == 0) {
1744 yaml_token_delete(&token);
1745 if (!yaml_parser_scan(&parser, &token)) {
1750 if (token.type != YAML_SCALAR_TOKEN)
1753 for (i = 0; cfg_type_table[i].ptype != PT_NONE; i++) {
1754 if (!strncmp((char *)token.data.alias.value,
1755 cfg_type_table[i].type_name,
1756 strlen(cfg_type_table[i].type_name))) {
1757 confset = cfg_type_table[i].ptype;
1762 if (confset == PT_NONE)
1765 for (i = 0; cfg_stage_table[i].pstage != PS_NONE; i++) {
1766 if (!strncmp((char *)token.data.alias.value,
1767 cfg_stage_table[i].stage_name,
1768 strlen(cfg_stage_table[i].stage_name))) {
1769 param |= cfg_stage_table[i].pstage;
1774 if (cfg_stage_table[i].pstage != PS_NONE)
1777 if (param & PS_PARAM_FOUND) {
1778 conf_to_set_param(confset,
1779 (char *)token.data.alias.value,
1780 device, parameter, PARAM_SZ);
1781 param |= PS_PARAM_SET;
1782 param &= ~PS_PARAM_FOUND;
1785 * we're getting parameter: param=val
1786 * copy val and mark that we've got it in case
1787 * there is no value: tag
1789 tmp = strchrnul(parameter, '=');
1791 strncpy(value, tmp + 1, sizeof(value) - 1);
1793 param |= PS_VAL_SET;
1797 } else if (param & PS_VAL_FOUND) {
1798 strncpy(value, (char *)token.data.alias.value,
1800 param |= PS_VAL_SET;
1801 param &= ~PS_VAL_FOUND;
1802 } else if (param & PS_DEVICE_FOUND) {
1803 strncpy(device, (char *)token.data.alias.value,
1805 param |= PS_DEVICE_SET;
1806 param &= ~PS_DEVICE_FOUND;
1809 if (confset && param & PS_VAL_SET && param & PS_PARAM_SET) {
1810 int size = strlen(parameter) + strlen(value) + 2;
1811 char *buf = malloc(size);
1817 snprintf(buf, size, "%s=%s", parameter, value);
1819 printf("set_param: %s\n", buf);
1820 rc = lcfg_setparam_perm(func, buf);
1824 parameter[0] = '\0';
1831 yaml_parser_delete(&parser);
1837 int jt_lcfg_setparam(int argc, char **argv)
1839 int rc = 0, index, i;
1840 struct param_opts popt;
1841 char *path = NULL, *value = NULL;
1843 memset(&popt, 0, sizeof(popt));
1844 index = setparam_cmdline(argc, argv, &popt);
1845 if (index < 0 || index >= argc)
1850 * We can't delete parameters that were
1851 * set with old conf_param interface
1853 return jt_lcfg_setparam_perm(argc, argv, &popt);
1856 return lcfg_setparam_yaml(argv[0], argv[index]);
1858 for (i = index; i < argc; i++) {
1862 value = strchr(path, '=');
1864 /* format: set_param a=b */
1867 if (*value == '\0') {
1869 "error: %s: setting %s: no value\n",
1870 jt_cmdname(argv[0]), path);
1876 /* format: set_param a b */
1880 "error: %s: setting %s: no value\n",
1881 jt_cmdname(argv[0]), path);
1889 rc2 = clean_path(&popt, path);
1891 fprintf(stderr, "error: %s: cleaning %s: %s\n",
1892 jt_cmdname(argv[0]), path, strerror(-rc2));
1898 rc2 = param_display(&popt, path, value, SET_PARAM);