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, struct lnet_nid *nid)
250 char nidstr[LNET_NIDSTR_SIZE];
251 struct lustre_cfg_bufs bufs;
252 struct lustre_cfg *lcfg;
254 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
256 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
257 if (!nid_is_nid4(nid)) {
258 libcfs_nidstr_r(nid, nidstr, sizeof(nidstr));
259 lustre_cfg_bufs_set_string(&bufs, 2, nidstr);
262 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
266 lustre_cfg_init(lcfg, LCFG_ADD_UUID, &bufs);
267 if (nid_is_nid4(nid))
268 lcfg->lcfg_nid = lnet_nid_to_nid4(nid);
272 rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
276 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
282 printf("Added uuid %s: %s\n", uuid, libcfs_nidstr(nid));
287 int jt_lcfg_add_uuid(int argc, char **argv)
294 if (libcfs_strnid(&nid, argv[2]) < 0) {
295 fprintf(stderr, "Can't parse NID %s\n", argv[2]);
299 return do_add_uuid(argv[0], argv[1], &nid);
302 int jt_lcfg_del_uuid(int argc, char **argv)
304 struct lustre_cfg_bufs bufs;
307 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
311 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
312 if (strcmp(argv[1], "_all_"))
313 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
315 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_UUID);
318 int jt_lcfg_del_mount_option(int argc, char **argv)
320 struct lustre_cfg_bufs bufs;
325 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
328 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
330 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
333 int jt_lcfg_set_timeout(int argc, char **argv)
336 struct lustre_cfg_bufs bufs;
337 struct lustre_cfg *lcfg;
340 "%s has been deprecated. Use conf_param instead.\ne.g. conf_param lustre-MDT0000 obd_timeout=50\n",
341 jt_cmdname(argv[0]));
347 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
349 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
353 lustre_cfg_init(lcfg, LCFG_SET_TIMEOUT, &bufs);
354 lcfg->lcfg_num = atoi(argv[1]);
356 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
360 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
361 strerror(rc = errno));
366 int jt_lcfg_add_conn(int argc, char **argv)
368 struct lustre_cfg_bufs bufs;
369 struct lustre_cfg *lcfg;
382 "%s: please use 'device name' to set the device name for config commands.\n",
383 jt_cmdname(argv[0]));
387 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
389 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
391 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
395 lustre_cfg_init(lcfg, LCFG_ADD_CONN, &bufs);
396 lcfg->lcfg_num = priority;
398 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
402 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
403 strerror(rc = errno));
409 int jt_lcfg_del_conn(int argc, char **argv)
411 struct lustre_cfg_bufs bufs;
418 "%s: please use 'device name' to set the device name for config commands.\n",
419 jt_cmdname(argv[0]));
423 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
425 /* connection uuid */
426 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
428 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
431 /* Param set locally, directly on target */
432 int jt_lcfg_param(int argc, char **argv)
434 struct lustre_cfg_bufs bufs;
437 if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
440 lustre_cfg_bufs_reset(&bufs, NULL);
442 for (i = 1; i < argc; i++)
443 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
445 return jt_lcfg_ioctl(&bufs, argv[0], LCFG_PARAM);
449 unsigned int po_only_path:1;
450 unsigned int po_show_path:1;
451 unsigned int po_show_type:1;
452 unsigned int po_recursive:1;
453 unsigned int po_perm:1;
454 unsigned int po_delete:1;
455 unsigned int po_only_dir:1;
456 unsigned int po_file:1;
457 unsigned int po_yaml:1;
458 unsigned int po_detail:1;
461 int lcfg_setparam_perm(char *func, char *buf)
464 struct lustre_cfg_bufs bufs;
465 struct lustre_cfg *lcfg;
467 lustre_cfg_bufs_reset(&bufs, NULL);
469 * This same command would be executed on all nodes, many
470 * of which should fail (silently) because they don't have
471 * that proc file existing locally. There would be no
472 * preprocessing on the MGS to try to figure out which
473 * parameter files to add this to, there would be nodes
474 * processing on the cluster nodes to try to figure out
475 * if they are the intended targets. They will blindly
476 * try to set the parameter, and ENOTFOUND means it wasn't
478 * Target name "general" means call on all targets. It is
479 * left here in case some filtering will be added in
482 lustre_cfg_bufs_set_string(&bufs, 0, "general");
484 lustre_cfg_bufs_set_string(&bufs, 1, buf);
486 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount,
490 fprintf(stderr, "error: allocating lcfg for %s: %s\n",
491 jt_cmdname(func), strerror(rc));
494 lustre_cfg_init(lcfg, LCFG_SET_PARAM, &bufs);
495 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
497 fprintf(stderr, "error: executing %s: %s\n",
498 jt_cmdname(func), strerror(errno));
506 * Param set to single log file, used by all clients and servers.
507 * This should be loaded after the individual config logs.
508 * Called from set param with -P option.
510 static int jt_lcfg_setparam_perm(int argc, char **argv,
511 struct param_opts *popt)
518 first_param = optind;
519 if (first_param < 0 || first_param >= argc)
522 for (i = first_param, rc = 0; i < argc; i++) {
524 if (popt->po_delete) {
529 /* Consider param ends at the first '=' in the buffer
530 * and make sure it always ends with '=' as well
532 end_pos = memchr(buf, '=', len - 1);
535 } else if (buf[len - 1] != '=') {
536 buf = malloc(len + 2);
539 sprintf(buf, "%s=", argv[i]);
543 rc = lcfg_setparam_perm(argv[0], buf);
551 int lcfg_conf_param(char *func, char *buf)
554 struct lustre_cfg_bufs bufs;
555 struct lustre_cfg *lcfg;
557 lustre_cfg_bufs_reset(&bufs, NULL);
558 lustre_cfg_bufs_set_string(&bufs, 1, buf);
560 /* We could put other opcodes here. */
561 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
565 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
566 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
576 * Param set in config log on MGS
577 * conf_param key=value
579 * Note we can actually send mgc conf_params from clients, but currently
580 * that's only done for default file striping (see ll_send_mgc_param),
583 * After removal of a parameter (-d) Lustre will use the default
584 * AT NEXT REBOOT, not immediately.
586 int jt_lcfg_confparam(int argc, char **argv)
592 /* mgs_setparam processes only lctl buf #1 */
593 if ((argc > 3) || (argc <= 1))
596 while ((rc = getopt(argc, argv, "d")) != -1) {
611 /* for delete, make it "<param>=\0" */
612 buf = malloc(strlen(argv[optind]) + 2);
617 /* put an '=' on the end in case it doesn't have one */
618 sprintf(buf, "%s=", argv[optind]);
619 /* then truncate after the first '=' */
620 ptr = strchr(buf, '=');
624 rc = lcfg_conf_param(argv[0], buf);
626 if (buf != argv[optind])
630 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
638 * Display a parameter path in the same format as sysctl.
639 * E.g. obdfilter.lustre-OST0000.stats
641 * \param[in] filename file name of the parameter
642 * \param[in] st parameter file stats
643 * \param[in] popt set/get param options
645 * \retval allocated pointer containing modified filename
648 display_name(const char *filename, struct stat *st, struct param_opts *popt)
650 size_t suffix_len = 0;
655 if (popt->po_show_type) {
656 if (S_ISDIR(st->st_mode))
658 else if (S_ISLNK(st->st_mode))
660 else if (st->st_mode & S_IWUSR)
664 /* Take the original filename string and chop off the glob addition */
665 tmp = strstr(filename, "/lustre/");
667 tmp = strstr(filename, "/lnet/");
669 tmp += strlen("/lnet/");
671 tmp += strlen("/lustre/");
674 /* Allocate return string */
675 param_name = strdup(tmp);
679 /* replace '/' with '.' to match conf_param and sysctl */
680 for (tmp = strchr(param_name, '/'); tmp != NULL; tmp = strchr(tmp, '/'))
683 /* Append the indicator to entries if needed. */
684 if (popt->po_show_type && suffix != NULL) {
685 suffix_len = strlen(suffix);
687 tmp = realloc(param_name, suffix_len + strlen(param_name) + 1);
690 strncat(param_name, suffix,
691 strlen(param_name) + suffix_len);
699 * Turns a lctl parameter string into a procfs/sysfs subdirectory path pattern.
701 * \param[in] popt Used to control parameter usage. For this
702 * function it is used to see if the path has
704 * \param[in,out] path lctl parameter string that is turned into
705 * the subdirectory path pattern that is used
706 * to search the procfs/sysfs tree.
708 * \retval -errno on error.
711 clean_path(struct param_opts *popt, char *path)
713 char *nidstart = NULL;
717 if (popt == NULL || path == NULL || strlen(path) == 0)
720 /* If path contains a suffix we need to remove it */
721 if (popt->po_show_type) {
722 size_t path_end = strlen(path) - 1;
724 tmp = path + path_end;
735 /* get rid of '\', glob doesn't like it */
736 tmp = strrchr(path, '\\');
738 char *tail = path + strlen(path);
740 while (tmp != path) {
742 memmove(tmp, tmp + 1, tail - tmp);
749 /* Does path contain a NID string? Skip '.->/' replacement for it. */
750 tmp = strchr(path, '@');
752 /* First find the NID start. NIDs may have variable (0-4) '.',
753 * so find the common NID prefixes instead of trying to count
754 * the dots. Not great, but there are only two, and faster
755 * than multiple speculative NID parses and bad DNS lookups.
757 if ((tmp = strstr(path, ".exports.")))
758 nidstart = tmp + strlen(".exports.");
759 else if ((tmp = strstr(path, ".MGC")))
762 /* Next, find the end of the NID string. */
764 nidend = strchrnul(strchr(nidstart, '@'), '.');
767 /* replace param '.' with '/' */
768 for (tmp = strchr(path, '.'); tmp != NULL; tmp = strchr(tmp, '.')) {
772 * There exist cases where some of the subdirectories of the
773 * the parameter tree has embedded in its name a NID string.
774 * This means that it is possible that these subdirectories
775 * could have actual '.' in its name. If this is the case we
776 * don't want to blindly replace the '.' with '/', so skip
777 * over the part of the parameter containing the NID.
787 * The application lctl can perform three operations for lustre
788 * tunables. This enum defines those three operations which are
790 * 1) LIST_PARAM - list available tunables
791 * 2) GET_PARAM - report the current setting of a tunable
792 * 3) SET_PARAM - set the tunable to a new value
794 enum parameter_operation {
800 char *parameter_opname[] = {
801 [LIST_PARAM] = "list_param",
802 [GET_PARAM] = "get_param",
803 [SET_PARAM] = "set_param",
807 * Read the value of parameter
809 * \param[in] path full path to the parameter
810 * \param[in] param_name lctl parameter format of the
812 * \param[in] popt set/get param options
814 * \retval 0 on success.
815 * \retval -errno on error.
818 read_param(const char *path, const char *param_name, struct param_opts *popt)
824 rc = llapi_param_get_value(path, &buf, &buflen);
827 "error: read_param: \'%s\': %s\n",
828 path, strerror(-rc));
831 /* don't print anything for empty files */
835 if (popt->po_show_path) {
838 longbuf = memchr(buf, '\n', buflen - 1) ||
839 buflen + strlen(param_name) >= 80;
840 printf("%s=%s", param_name, longbuf ? "\n" : "");
850 * Set a parameter to a specified value
852 * \param[in] path full path to the parameter
853 * \param[in] param_name lctl parameter format of the parameter path
854 * \param[in] popt set/get param options
855 * \param[in] value value to set the parameter to
857 * \retval number of bytes written on success.
858 * \retval -errno on error.
861 write_param(const char *path, const char *param_name, struct param_opts *popt,
870 /* Write the new value to the file */
871 fd = open(path, O_WRONLY);
874 fprintf(stderr, "error: set_param: opening '%s': %s\n",
875 path, strerror(errno));
879 count = write(fd, value, strlen(value));
883 fprintf(stderr, "error: set_param: setting %s=%s: %s\n",
884 path, value, strerror(errno));
886 } else if (count < strlen(value)) { /* Truncate case */
889 "error: set_param: setting %s=%s: wrote only %zd\n",
891 } else if (popt->po_show_path) {
892 printf("%s=%s\n", param_name, value);
899 void print_obd_line(char *s)
901 const char *param = "osc/%s/ost_conn_uuid";
902 char obd_name[MAX_OBD_NAME];
903 char buf[MAX_OBD_NAME];
908 /* obd device type is the first 3 characters of param name */
909 snprintf(buf, sizeof(buf), " %%*d %%*s %.3s %%%zus %%*s %%*d ",
910 param, sizeof(obd_name) - 1);
911 if (sscanf(s, buf, obd_name) == 0)
913 if (cfs_get_param_paths(&path, param, obd_name) != 0)
915 fp = fopen(path.gl_pathv[0], "r");
917 /* need to free path data before retry */
918 cfs_free_param_data(&path);
920 if (param[0] == 'o') { /* failed with osc, try mdc */
921 param = "mdc/%s/mds_conn_uuid";
928 /* should not ignore fgets(3)'s return value */
929 if (!fgets(buf, sizeof(buf), fp)) {
930 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
936 cfs_free_param_data(&path);
938 /* trim trailing newlines */
939 ptr = strrchr(buf, '\n');
943 ptr = strrchr(s, '\n');
946 printf("%s%s%s\n", s, buf[0] ? " " : "", buf);
949 int yaml_get_device_index(char *source)
951 yaml_emitter_t request;
958 sk = nl_socket_alloc();
962 /* Setup parser to recieve Netlink packets */
963 rc = yaml_parser_initialize(&reply);
967 rc = yaml_parser_set_input_netlink(&reply, sk, false);
971 /* Create Netlink emitter to send request to kernel */
972 yaml_emitter_initialize(&request);
973 rc = yaml_emitter_set_output_netlink(&request, sk, "lustre",
975 LUSTRE_CMD_DEVICES, NLM_F_DUMP);
979 yaml_emitter_open(&request);
981 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
982 rc = yaml_emitter_emit(&request, &event);
986 yaml_mapping_start_event_initialize(&event, NULL,
987 (yaml_char_t *)YAML_MAP_TAG,
988 1, YAML_ANY_MAPPING_STYLE);
989 rc = yaml_emitter_emit(&request, &event);
993 yaml_scalar_event_initialize(&event, NULL,
994 (yaml_char_t *)YAML_STR_TAG,
995 (yaml_char_t *)"devices",
996 strlen("devices"), 1, 0,
997 YAML_PLAIN_SCALAR_STYLE);
998 rc = yaml_emitter_emit(&request, &event);
1002 yaml_sequence_start_event_initialize(&event, NULL,
1003 (yaml_char_t *)YAML_SEQ_TAG,
1004 1, YAML_ANY_SEQUENCE_STYLE);
1005 rc = yaml_emitter_emit(&request, &event);
1009 yaml_mapping_start_event_initialize(&event, NULL,
1010 (yaml_char_t *)YAML_MAP_TAG,
1011 1, YAML_ANY_MAPPING_STYLE);
1012 rc = yaml_emitter_emit(&request, &event);
1016 yaml_scalar_event_initialize(&event, NULL,
1017 (yaml_char_t *)YAML_STR_TAG,
1018 (yaml_char_t *)"name",
1020 1, 0, YAML_PLAIN_SCALAR_STYLE);
1021 rc = yaml_emitter_emit(&request, &event);
1025 yaml_scalar_event_initialize(&event, NULL,
1026 (yaml_char_t *)YAML_STR_TAG,
1027 (yaml_char_t *)source,
1028 strlen(source), 1, 0,
1029 YAML_PLAIN_SCALAR_STYLE);
1030 rc = yaml_emitter_emit(&request, &event);
1034 yaml_mapping_end_event_initialize(&event);
1035 rc = yaml_emitter_emit(&request, &event);
1039 yaml_sequence_end_event_initialize(&event);
1040 rc = yaml_emitter_emit(&request, &event);
1044 yaml_mapping_end_event_initialize(&event);
1045 rc = yaml_emitter_emit(&request, &event);
1049 yaml_document_end_event_initialize(&event, 0);
1050 rc = yaml_emitter_emit(&request, &event);
1054 yaml_emitter_close(&request);
1057 yaml_emitter_log_error(&request, stderr);
1060 yaml_emitter_delete(&request);
1063 rc = yaml_parser_parse(&reply, &event);
1065 yaml_parser_log_error(&reply, stdout, "lctl: ");
1070 if (event.type == YAML_SCALAR_EVENT) {
1071 char *value = (char *)event.data.scalar.value;
1073 if (strcmp(value, "index") == 0) {
1074 yaml_event_delete(&event);
1075 rc = yaml_parser_parse(&reply, &event);
1077 value = (char *)event.data.scalar.value;
1079 rc = strtoul(value, NULL, 10);
1081 yaml_event_delete(&event);
1088 done = (event.type == YAML_STREAM_END_EVENT);
1089 yaml_event_delete(&event);
1098 * Perform a read, write or just a listing of a parameter
1100 * \param[in] popt list,set,get parameter options
1101 * \param[in] pattern search filter for the path of the parameter
1102 * \param[in] value value to set the parameter if write operation
1103 * \param[in] mode what operation to perform with the parameter
1105 * \retval number of bytes written on success.
1106 * \retval -errno on error and prints error message.
1109 param_display(struct param_opts *popt, char *pattern, char *value,
1110 enum parameter_operation mode)
1115 char *opname = parameter_opname[mode];
1118 rc = llapi_param_get_paths(pattern, &paths);
1121 if (!popt->po_recursive && !(rc == -ENOENT && getuid() != 0)) {
1122 fprintf(stderr, "error: %s: param_path '%s': %s\n",
1123 opname, pattern, strerror(errno));
1128 dup_cache = calloc(paths.gl_pathc, sizeof(char *));
1132 "error: %s: allocating '%s' dup_cache[%zd]: %s\n",
1133 opname, pattern, paths.gl_pathc, strerror(-rc));
1137 for (i = 0; i < paths.gl_pathc; i++) {
1138 char *param_name = NULL, *tmp;
1139 char pathname[PATH_MAX], param_dir[PATH_MAX + 2];
1143 if (stat(paths.gl_pathv[i], &st) == -1) {
1144 fprintf(stderr, "error: %s: stat '%s': %s\n",
1145 opname, paths.gl_pathv[i], strerror(errno));
1151 if (popt->po_only_dir && !S_ISDIR(st.st_mode))
1154 param_name = display_name(paths.gl_pathv[i], &st, popt);
1157 "error: %s: generating name for '%s': %s\n",
1158 opname, paths.gl_pathv[i], strerror(ENOMEM));
1166 /* Read the contents of file to stdout */
1167 if (S_ISREG(st.st_mode)) {
1168 rc2 = read_param(paths.gl_pathv[i], param_name,
1170 if (rc2 < 0 && rc == 0)
1175 if (S_ISREG(st.st_mode)) {
1176 rc2 = write_param(paths.gl_pathv[i],
1177 param_name, popt, value);
1178 if (rc2 < 0 && rc == 0)
1184 * For the upstream client the parameter files locations
1185 * are split between under both /sys/kernel/debug/lustre
1186 * and /sys/fs/lustre. The parameter files containing
1187 * small amounts of data, less than a page in size, are
1188 * located under /sys/fs/lustre and in the case of large
1189 * parameter data files, think stats for example, are
1190 * located in the debugfs tree. Since the files are
1191 * split across two trees the directories are often
1192 * duplicated which means these directories are listed
1193 * twice which leads to duplicate output to the user.
1194 * To avoid scanning a directory twice we have to cache
1195 * any directory and check if a search has been
1198 for (j = 0; j < dup_count; j++) {
1199 if (!strcmp(dup_cache[j], param_name))
1202 if (j != dup_count) {
1207 dup_cache[dup_count++] = strdup(param_name);
1209 if (popt->po_show_path)
1210 printf("%s\n", param_name);
1215 * Only directories are searched recursively if
1216 * requested by the user
1218 if (!S_ISDIR(st.st_mode) || !popt->po_recursive) {
1224 /* Turn param_name into file path format */
1225 rc2 = clean_path(popt, param_name);
1227 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1228 opname, param_name, strerror(-rc2));
1236 /* Use param_name to grab subdirectory tree from full path */
1237 snprintf(param_dir, sizeof(param_dir), "/%s", param_name);
1238 tmp = strstr(paths.gl_pathv[i], param_dir);
1240 /* cleanup paramname now that we are done with it */
1243 memset(¶m_dir, '\0', sizeof(param_dir));
1245 /* Shouldn't happen but just in case */
1253 rc2 = snprintf(pathname, sizeof(pathname), "%s/*", tmp);
1256 * snprintf() should never an error, and if it does
1257 * there isn't much point trying to use fprintf()
1261 if (rc2 >= sizeof(pathname)) {
1262 fprintf(stderr, "error: %s: overflow processing '%s'\n",
1269 rc2 = param_display(popt, pathname, value, mode);
1270 if (rc2 != 0 && rc2 != -ENOENT) {
1271 /* errors will be printed by param_display() */
1278 for (i = 0; i < dup_count; i++)
1282 llapi_param_paths_free(&paths);
1286 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
1290 popt->po_show_path = 1;
1291 popt->po_only_path = 1;
1293 while ((ch = getopt(argc, argv, "FRD")) != -1) {
1296 popt->po_show_type = 1;
1299 popt->po_recursive = 1;
1302 popt->po_only_dir = 1;
1312 int jt_lcfg_listparam(int argc, char **argv)
1314 int rc = 0, index, i;
1315 struct param_opts popt;
1318 memset(&popt, 0, sizeof(popt));
1319 index = listparam_cmdline(argc, argv, &popt);
1320 if (index < 0 || index >= argc)
1323 for (i = index; i < argc; i++) {
1328 rc2 = clean_path(&popt, path);
1330 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1331 jt_cmdname(argv[0]), path, strerror(-rc2));
1337 rc2 = param_display(&popt, path, NULL, LIST_PARAM);
1342 if (rc2 == -ENOENT && getuid() != 0)
1343 rc2 = llapi_param_display_value(path, 0, 0,
1346 fprintf(stderr, "error: %s: listing '%s': %s\n",
1347 jt_cmdname(argv[0]), path,
1357 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
1361 popt->po_show_path = 1;
1363 while ((ch = getopt(argc, argv, "FnNRy")) != -1) {
1366 popt->po_show_type = 1;
1369 popt->po_show_path = 0;
1372 popt->po_only_path = 1;
1375 popt->po_recursive = 1;
1388 int jt_lcfg_getparam(int argc, char **argv)
1390 int version = LUSTRE_GENL_VERSION;
1391 enum parameter_operation mode;
1392 int rc = 0, index, i;
1393 struct param_opts popt;
1397 memset(&popt, 0, sizeof(popt));
1398 index = getparam_cmdline(argc, argv, &popt);
1399 if (index < 0 || index >= argc)
1402 mode = popt.po_only_path ? LIST_PARAM : GET_PARAM;
1403 if (mode == LIST_PARAM)
1407 flags |= PARAM_FLAGS_YAML_FORMAT;
1408 if (popt.po_show_path)
1409 flags |= PARAM_FLAGS_SHOW_SOURCE;
1411 for (i = index; i < argc; i++) {
1416 rc2 = clean_path(&popt, path);
1418 fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1419 jt_cmdname(argv[0]), path, strerror(-rc2));
1425 rc2 = param_display(&popt, path, NULL, mode);
1430 if (rc2 == -ENOENT && getuid() != 0)
1431 rc2 = llapi_param_display_value(path, version,
1440 /* get device list by netlink or debugfs */
1441 int jt_device_list(int argc, char **argv)
1443 static const struct option long_opts[] = {
1444 { .name = "target", .has_arg = no_argument, .val = 't' },
1445 { .name = "yaml", .has_arg = no_argument, .val = 'y' },
1448 struct param_opts opts;
1449 char buf[MAX_OBD_NAME];
1455 if (optind + 1 < argc)
1458 memset(&opts, 0, sizeof(opts));
1460 while ((c = getopt_long(argc, argv, "ty", long_opts, NULL)) != -1) {
1463 flags |= PARAM_FLAGS_EXTRA_DETAILS;
1464 opts.po_detail = true;
1467 flags |= PARAM_FLAGS_YAML_FORMAT;
1468 opts.po_yaml = true;
1475 if (optind < argc) {
1481 /* Use YAML to list all devices */
1482 rc = llapi_param_display_value("devices", LUSTRE_GENL_VERSION, flags,
1487 rc = llapi_param_get_paths("devices", &path);
1491 fp = fopen(path.gl_pathv[0], "r");
1493 cfs_free_param_data(&path);
1497 while (fgets(buf, sizeof(buf), fp) != NULL)
1499 print_obd_line(buf);
1503 cfs_free_param_data(&path);
1508 static int do_name2dev(char *func, char *name, int dev_id)
1510 struct obd_ioctl_data data;
1511 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1514 /* Use YAML to find device index */
1515 rc = yaml_get_device_index(name);
1516 if (rc >= 0 || rc != -EOPNOTSUPP)
1519 memset(&data, 0, sizeof(data));
1520 data.ioc_dev = dev_id;
1521 data.ioc_inllen1 = strlen(name) + 1;
1522 data.ioc_inlbuf1 = name;
1524 memset(buf, 0, sizeof(rawbuf));
1525 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1527 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
1530 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
1533 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1535 fprintf(stderr, "error: %s: invalid reply\n", jt_cmdname(func));
1539 return data.ioc_dev;
1543 * resolve a device name to a device number.
1544 * supports a number, $name or %uuid.
1546 int parse_devname(char *func, char *name, int dev_id)
1554 /* Test if its a pure number string */
1555 if (strspn(name, "0123456789") != strlen(name)) {
1556 if (name[0] == '$' || name[0] == '%')
1559 rc = do_name2dev(func, name, dev_id);
1564 ret = strtoul(name, NULL, 10);
1570 fprintf(stderr, "No device found for name %s: %s\n",
1571 name, strerror(rc));
1575 #ifdef HAVE_SERVER_SUPPORT
1577 * Output information about nodemaps.
1578 * \param argc number of args
1579 * \param argv[] variable string arguments
1581 * [list|nodemap_name|all] \a list will list all nodemaps (default).
1582 * Specifying a \a nodemap_name will
1583 * display info about that specific nodemap.
1584 * \a all will display info for all nodemaps.
1585 * \retval 0 on success
1587 int jt_nodemap_info(int argc, char **argv)
1589 const char usage_str[] = "usage: nodemap_info [list|nodemap_name|all]\n";
1590 struct param_opts popt;
1593 memset(&popt, 0, sizeof(popt));
1594 popt.po_show_path = 1;
1597 fprintf(stderr, usage_str);
1601 if (argc == 1 || strcmp("list", argv[1]) == 0) {
1602 popt.po_only_dir = 1;
1603 rc = param_display(&popt, "nodemap/*", NULL, LIST_PARAM);
1604 } else if (strcmp("all", argv[1]) == 0) {
1605 rc = param_display(&popt, "nodemap/*/*", NULL, GET_PARAM);
1607 char pattern[PATH_MAX];
1609 snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
1610 rc = param_display(&popt, pattern, NULL, GET_PARAM);
1613 "error: nodemap_info: cannot find nodemap %s\n",
1620 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
1624 popt->po_show_path = 1;
1625 popt->po_only_path = 0;
1626 popt->po_show_type = 0;
1627 popt->po_recursive = 0;
1629 popt->po_delete = 0;
1632 while ((ch = getopt(argc, argv, "nPdF")) != -1) {
1635 popt->po_show_path = 0;
1641 popt->po_delete = 1;
1660 #define PS_PARAM_FOUND 1
1661 #define PS_PARAM_SET 2
1662 #define PS_VAL_FOUND 4
1663 #define PS_VAL_SET 8
1664 #define PS_DEVICE_FOUND 16
1665 #define PS_DEVICE_SET 32
1667 #define PARAM_SZ 256
1669 static struct cfg_type_data {
1670 enum paramtype ptype;
1672 } cfg_type_table[] = {
1673 { PT_SETPARAM, "set_param" },
1674 { PT_CONFPARAM, "conf_param" },
1678 static struct cfg_stage_data {
1681 } cfg_stage_table[] = {
1682 { PS_PARAM_FOUND, "parameter" },
1683 { PS_VAL_FOUND, "value" },
1684 { PS_DEVICE_FOUND, "device" },
1688 void conf_to_set_param(enum paramtype confset, const char *param,
1689 const char *device, char *buf,
1694 if (confset == PT_SETPARAM) {
1695 strncpy(buf, param, bufsize);
1700 * sys.* params are top level, we just need to trim the sys.
1702 tmp = strstr(param, "sys.");
1705 strncpy(buf, tmp, bufsize);
1710 * parameters look like type.parameter, we need to stick the device
1711 * in the middle. Example combine mdt.identity_upcall with device
1712 * lustre-MDT0000 for mdt.lustre-MDT0000.identity_upcall
1715 tmp = strchrnul(param, '.');
1716 snprintf(buf, tmp - param + 1, "%s", param);
1718 bufsize -= tmp - param;
1719 snprintf(buf, bufsize, ".%s%s", device, tmp);
1722 int lcfg_setparam_yaml(char *func, char *filename)
1725 yaml_parser_t parser;
1729 enum paramtype confset = PT_NONE;
1730 int param = PS_NONE;
1732 char parameter[PARAM_SZ + 1];
1733 char value[PARAM_SZ + 1];
1734 char device[PARAM_SZ + 1];
1736 file = fopen(filename, "rb");
1737 yaml_parser_initialize(&parser);
1738 yaml_parser_set_input_file(&parser, file);
1741 * Search tokens for conf_param or set_param
1742 * The token after "parameter" goes into parameter
1743 * The token after "value" goes into value
1744 * when we have all 3, create param=val and call the
1745 * appropriate function for set/conf param
1747 while (token.type != YAML_STREAM_END_TOKEN && rc == 0) {
1750 yaml_token_delete(&token);
1751 if (!yaml_parser_scan(&parser, &token)) {
1756 if (token.type != YAML_SCALAR_TOKEN)
1759 for (i = 0; cfg_type_table[i].ptype != PT_NONE; i++) {
1760 if (!strncmp((char *)token.data.alias.value,
1761 cfg_type_table[i].type_name,
1762 strlen(cfg_type_table[i].type_name))) {
1763 confset = cfg_type_table[i].ptype;
1768 if (confset == PT_NONE)
1771 for (i = 0; cfg_stage_table[i].pstage != PS_NONE; i++) {
1772 if (!strncmp((char *)token.data.alias.value,
1773 cfg_stage_table[i].stage_name,
1774 strlen(cfg_stage_table[i].stage_name))) {
1775 param |= cfg_stage_table[i].pstage;
1780 if (cfg_stage_table[i].pstage != PS_NONE)
1783 if (param & PS_PARAM_FOUND) {
1784 conf_to_set_param(confset,
1785 (char *)token.data.alias.value,
1786 device, parameter, PARAM_SZ);
1787 param |= PS_PARAM_SET;
1788 param &= ~PS_PARAM_FOUND;
1791 * we're getting parameter: param=val
1792 * copy val and mark that we've got it in case
1793 * there is no value: tag
1795 tmp = strchrnul(parameter, '=');
1797 strncpy(value, tmp + 1, sizeof(value) - 1);
1799 param |= PS_VAL_SET;
1803 } else if (param & PS_VAL_FOUND) {
1804 strncpy(value, (char *)token.data.alias.value,
1806 param |= PS_VAL_SET;
1807 param &= ~PS_VAL_FOUND;
1808 } else if (param & PS_DEVICE_FOUND) {
1809 strncpy(device, (char *)token.data.alias.value,
1811 param |= PS_DEVICE_SET;
1812 param &= ~PS_DEVICE_FOUND;
1815 if (confset && param & PS_VAL_SET && param & PS_PARAM_SET) {
1816 int size = strlen(parameter) + strlen(value) + 2;
1817 char *buf = malloc(size);
1823 snprintf(buf, size, "%s=%s", parameter, value);
1825 printf("set_param: %s\n", buf);
1826 rc = lcfg_setparam_perm(func, buf);
1830 parameter[0] = '\0';
1837 yaml_parser_delete(&parser);
1843 int jt_lcfg_setparam(int argc, char **argv)
1845 int rc = 0, index, i;
1846 struct param_opts popt;
1847 char *path = NULL, *value = NULL;
1849 memset(&popt, 0, sizeof(popt));
1850 index = setparam_cmdline(argc, argv, &popt);
1851 if (index < 0 || index >= argc)
1856 * We can't delete parameters that were
1857 * set with old conf_param interface
1859 return jt_lcfg_setparam_perm(argc, argv, &popt);
1862 return lcfg_setparam_yaml(argv[0], argv[index]);
1864 for (i = index; i < argc; i++) {
1868 value = strchr(path, '=');
1870 /* format: set_param a=b */
1873 if (*value == '\0') {
1875 "error: %s: setting %s: no value\n",
1876 jt_cmdname(argv[0]), path);
1882 /* format: set_param a b */
1886 "error: %s: setting %s: no value\n",
1887 jt_cmdname(argv[0]), path);
1895 rc2 = clean_path(&popt, path);
1897 fprintf(stderr, "error: %s: cleaning %s: %s\n",
1898 jt_cmdname(argv[0]), path, strerror(-rc2));
1904 rc2 = param_display(&popt, path, value, SET_PARAM);