Whamcloud - gitweb
LU-15142 lctl: fixes for set_param -P and llog_print
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lustre_cfg.c
32  *
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>
37  */
38
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <stdbool.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <stdarg.h>
50 #include <ctype.h>
51
52 #include <libcfs/util/ioctl.h>
53 #include <libcfs/util/string.h>
54 #include <libcfs/util/param.h>
55 #include <libcfs/util/parser.h>
56 #include <lustre/lustreapi.h>
57 #include <linux/lnet/nidstr.h>
58 #include <linux/lnet/lnetctl.h>
59 #include <linux/lustre/lustre_cfg.h>
60 #include <linux/lustre/lustre_ioctl.h>
61 #include <linux/lustre/lustre_ver.h>
62
63 #include <sys/un.h>
64 #include <time.h>
65 #include <sys/time.h>
66 #include <errno.h>
67 #include <string.h>
68
69 #include "obdctl.h"
70 #include <stdio.h>
71 #include <yaml.h>
72
73 static char *lcfg_devname;
74
75 int lcfg_set_devname(char *name)
76 {
77         char *ptr;
78         int digit = 1;
79
80         if (name) {
81                 if (lcfg_devname)
82                         free(lcfg_devname);
83                 /* quietly strip the unnecessary '$' */
84                 if (*name == '$' || *name == '%')
85                         name++;
86
87                 ptr = name;
88                 while (*ptr != '\0') {
89                         if (!isdigit(*ptr)) {
90                                 digit = 0;
91                                 break;
92                         }
93                         ptr++;
94                 }
95
96                 if (digit) {
97                         /* We can't translate from dev # to name */
98                         lcfg_devname = NULL;
99                 } else {
100                         lcfg_devname = strdup(name);
101                 }
102         } else {
103                 lcfg_devname = NULL;
104         }
105         return 0;
106 }
107
108 char *lcfg_get_devname(void)
109 {
110         return lcfg_devname;
111 }
112
113 int jt_lcfg_device(int argc, char **argv)
114 {
115         return jt_obd_device(argc, argv);
116 }
117
118 static int jt_lcfg_ioctl(struct lustre_cfg_bufs *bufs, char *arg, int cmd)
119 {
120         struct lustre_cfg *lcfg;
121         int rc;
122
123         lcfg = malloc(lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
124         if (!lcfg) {
125                 rc = -ENOMEM;
126         } else {
127                 lustre_cfg_init(lcfg, cmd, bufs);
128                 rc = lcfg_ioctl(arg, OBD_DEV_ID, lcfg);
129                 free(lcfg);
130         }
131         if (rc < 0)
132                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(arg),
133                         strerror(rc = errno));
134         return rc;
135 }
136
137 int jt_lcfg_attach(int argc, char **argv)
138 {
139         struct lustre_cfg_bufs bufs;
140         int rc;
141
142         if (argc != 4)
143                 return CMD_HELP;
144
145         lustre_cfg_bufs_reset(&bufs, NULL);
146
147         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
148         lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
149         lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
150
151         rc = jt_lcfg_ioctl(&bufs, argv[0], LCFG_ATTACH);
152         if (rc == 0)
153                 lcfg_set_devname(argv[2]);
154
155         return rc;
156 }
157
158 int jt_lcfg_setup(int argc, char **argv)
159 {
160         struct lustre_cfg_bufs bufs;
161         int i;
162
163         if (!lcfg_devname) {
164                 fprintf(stderr,
165                         "%s: please use 'device name' to set the device name for config commands.\n",
166                         jt_cmdname(argv[0]));
167                 return -EINVAL;
168         }
169
170         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
171
172         if (argc > 6)
173                 return CMD_HELP;
174
175         for (i = 1; i < argc; i++)
176                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
177
178         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_SETUP);
179 }
180
181 int jt_obd_detach(int argc, char **argv)
182 {
183         struct lustre_cfg_bufs bufs;
184
185         if (!lcfg_devname) {
186                 fprintf(stderr,
187                         "%s: please use 'device name' to set the device name for config commands.\n",
188                         jt_cmdname(argv[0]));
189                 return -EINVAL;
190         }
191
192         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
193
194         if (argc != 1)
195                 return CMD_HELP;
196
197         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DETACH);
198 }
199
200 int jt_obd_cleanup(int argc, char **argv)
201 {
202         struct lustre_cfg_bufs bufs;
203         char force = 'F';
204         char failover = 'A';
205         char flags[3] = { 0 };
206         int flag_cnt = 0, n;
207
208         if (!lcfg_devname) {
209                 fprintf(stderr,
210                         "%s: please use 'device name' to set the device name for config commands.\n",
211                         jt_cmdname(argv[0]));
212                 return -EINVAL;
213         }
214
215         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
216
217         if (argc < 1 || argc > 3)
218                 return CMD_HELP;
219
220         /*
221          * we are protected from overflowing our buffer by the argc
222          * check above
223          */
224         for (n = 1; n < argc; n++) {
225                 if (strcmp(argv[n], "force") == 0) {
226                         flags[flag_cnt++] = force;
227                 } else if (strcmp(argv[n], "failover") == 0) {
228                         flags[flag_cnt++] = failover;
229                 } else {
230                         fprintf(stderr, "unknown option: %s\n", argv[n]);
231                         return CMD_HELP;
232                 }
233         }
234
235         if (flag_cnt)
236                 lustre_cfg_bufs_set_string(&bufs, 1, flags);
237
238         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_CLEANUP);
239 }
240
241 static
242 int do_add_uuid(char *func, char *uuid, lnet_nid_t nid)
243 {
244         int rc;
245         struct lustre_cfg_bufs bufs;
246         struct lustre_cfg *lcfg;
247
248         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
249         if (uuid)
250                 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
251
252         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
253         if (!lcfg) {
254                 rc = -ENOMEM;
255         } else {
256                 lustre_cfg_init(lcfg, LCFG_ADD_UUID, &bufs);
257                 lcfg->lcfg_nid = nid;
258
259                 rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
260                 free(lcfg);
261         }
262         if (rc) {
263                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
264                         strerror(errno));
265                 return -1;
266         }
267
268         if (uuid)
269                 printf("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
270
271         return 0;
272 }
273
274 int jt_lcfg_add_uuid(int argc, char **argv)
275 {
276         lnet_nid_t nid;
277
278         if (argc != 3)
279                 return CMD_HELP;
280
281         nid = libcfs_str2nid(argv[2]);
282         if (nid == LNET_NID_ANY) {
283                 fprintf(stderr, "Can't parse NID %s\n", argv[2]);
284                 return (-1);
285         }
286
287         return do_add_uuid(argv[0], argv[1], nid);
288 }
289
290 int jt_lcfg_del_uuid(int argc, char **argv)
291 {
292         struct lustre_cfg_bufs bufs;
293
294         if (argc != 2) {
295                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
296                 return 0;
297         }
298
299         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
300         if (strcmp(argv[1], "_all_"))
301                 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
302
303         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_UUID);
304 }
305
306 int jt_lcfg_del_mount_option(int argc, char **argv)
307 {
308         struct lustre_cfg_bufs bufs;
309
310         if (argc != 2)
311                 return CMD_HELP;
312
313         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
314
315         /* profile name */
316         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
317
318         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
319 }
320
321 int jt_lcfg_set_timeout(int argc, char **argv)
322 {
323         int rc;
324         struct lustre_cfg_bufs bufs;
325         struct lustre_cfg *lcfg;
326
327         fprintf(stderr,
328                 "%s has been deprecated. Use conf_param instead.\ne.g. conf_param lustre-MDT0000 obd_timeout=50\n",
329                 jt_cmdname(argv[0]));
330         return CMD_HELP;
331
332         if (argc != 2)
333                 return CMD_HELP;
334
335         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
336
337         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
338         if (!lcfg) {
339                 rc = -ENOMEM;
340         } else {
341                 lustre_cfg_init(lcfg, LCFG_SET_TIMEOUT, &bufs);
342                 lcfg->lcfg_num = atoi(argv[1]);
343
344                 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
345                 free(lcfg);
346         }
347         if (rc < 0) {
348                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
349                         strerror(rc = errno));
350         }
351         return rc;
352 }
353
354 int jt_lcfg_add_conn(int argc, char **argv)
355 {
356         struct lustre_cfg_bufs bufs;
357         struct lustre_cfg *lcfg;
358         int priority;
359         int rc;
360
361         if (argc == 2)
362                 priority = 0;
363         else if (argc == 3)
364                 priority = 1;
365         else
366                 return CMD_HELP;
367
368         if (!lcfg_devname) {
369                 fprintf(stderr,
370                         "%s: please use 'device name' to set the device name for config commands.\n",
371                         jt_cmdname(argv[0]));
372                 return -EINVAL;
373         }
374
375         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
376
377         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
378
379         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
380         if (!lcfg) {
381                 rc = -ENOMEM;
382         } else {
383                 lustre_cfg_init(lcfg, LCFG_ADD_CONN, &bufs);
384                 lcfg->lcfg_num = priority;
385
386                 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
387                 free(lcfg);
388         }
389         if (rc < 0) {
390                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
391                         strerror(rc = errno));
392         }
393
394         return rc;
395 }
396
397 int jt_lcfg_del_conn(int argc, char **argv)
398 {
399         struct lustre_cfg_bufs bufs;
400
401         if (argc != 2)
402                 return CMD_HELP;
403
404         if (!lcfg_devname) {
405                 fprintf(stderr,
406                         "%s: please use 'device name' to set the device name for config commands.\n",
407                         jt_cmdname(argv[0]));
408                 return -EINVAL;
409         }
410
411         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
412
413         /* connection uuid */
414         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
415
416         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_DEL_MOUNTOPT);
417 }
418
419 /* Param set locally, directly on target */
420 int jt_lcfg_param(int argc, char **argv)
421 {
422         struct lustre_cfg_bufs bufs;
423         int i;
424
425         if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
426                 return CMD_HELP;
427
428         lustre_cfg_bufs_reset(&bufs, NULL);
429
430         for (i = 1; i < argc; i++)
431                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
432
433         return jt_lcfg_ioctl(&bufs, argv[0], LCFG_PARAM);
434 }
435
436 struct param_opts {
437         unsigned int po_only_path:1;
438         unsigned int po_show_path:1;
439         unsigned int po_show_type:1;
440         unsigned int po_recursive:1;
441         unsigned int po_perm:1;
442         unsigned int po_delete:1;
443         unsigned int po_only_dir:1;
444         unsigned int po_file:1;
445 };
446
447 int lcfg_setparam_perm(char *func, char *buf)
448 {
449         int rc = 0;
450         struct lustre_cfg_bufs bufs;
451         struct lustre_cfg *lcfg;
452
453         lustre_cfg_bufs_reset(&bufs, NULL);
454         /*
455          * This same command would be executed on all nodes, many
456          * of which should fail (silently) because they don't have
457          * that proc file existing locally. There would be no
458          * preprocessing on the MGS to try to figure out which
459          * parameter files to add this to, there would be nodes
460          * processing on the cluster nodes to try to figure out
461          * if they are the intended targets. They will blindly
462          * try to set the parameter, and ENOTFOUND means it wasn't
463          * for them.
464          * Target name "general" means call on all targets. It is
465          * left here in case some filtering will be added in
466          * future.
467          */
468         lustre_cfg_bufs_set_string(&bufs, 0, "general");
469
470         lustre_cfg_bufs_set_string(&bufs, 1, buf);
471
472         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount,
473                                      bufs.lcfg_buflen));
474         if (!lcfg) {
475                 rc = -ENOMEM;
476                 fprintf(stderr, "error: allocating lcfg for %s: %s\n",
477                         jt_cmdname(func), strerror(rc));
478
479         } else {
480                 lustre_cfg_init(lcfg, LCFG_SET_PARAM, &bufs);
481                 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
482                 if (rc != 0)
483                         fprintf(stderr, "error: executing %s: %s\n",
484                                 jt_cmdname(func), strerror(errno));
485                 free(lcfg);
486         }
487
488         return rc;
489 }
490
491 /*
492  * Param set to single log file, used by all clients and servers.
493  * This should be loaded after the individual config logs.
494  * Called from set param with -P option.
495  */
496 static int jt_lcfg_setparam_perm(int argc, char **argv,
497                                  struct param_opts *popt)
498 {
499         int rc;
500         int i;
501         int first_param;
502         char *buf = NULL;
503
504         first_param = optind;
505         if (first_param < 0 || first_param >= argc)
506                 return CMD_HELP;
507
508         for (i = first_param, rc = 0; i < argc; i++) {
509                 buf = argv[i];
510                 if (popt->po_delete) {
511                         char *end_pos;
512                         size_t len;
513
514                         len = strlen(buf);
515                         /* Consider param ends at the first '=' in the buffer
516                          * and make sure it always ends with '=' as well
517                          */
518                         end_pos = memchr(buf, '=', len - 1);
519                         if (end_pos) {
520                                 *(++end_pos) = '\0';
521                         } else if (buf[len - 1] != '=') {
522                                 buf = malloc(len + 1);
523                                 if (buf == NULL)
524                                         return -ENOMEM;
525                                 sprintf(buf, "%s=", argv[i]);
526                         }
527                 }
528
529                 rc = lcfg_setparam_perm(argv[0], buf);
530                 if (buf != argv[i])
531                         free(buf);
532         }
533
534         return rc;
535 }
536
537 int lcfg_conf_param(char *func, char *buf)
538 {
539         int rc;
540         struct lustre_cfg_bufs bufs;
541         struct lustre_cfg *lcfg;
542
543         lustre_cfg_bufs_reset(&bufs, NULL);
544         lustre_cfg_bufs_set_string(&bufs, 1, buf);
545
546         /* We could put other opcodes here. */
547         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
548         if (!lcfg) {
549                 rc = -ENOMEM;
550         } else {
551                 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
552                 rc = lcfg_mgs_ioctl(func, OBD_DEV_ID, lcfg);
553                 if (rc < 0)
554                         rc = -errno;
555                 free(lcfg);
556         }
557
558         return rc;
559 }
560
561 /*
562  * Param set in config log on MGS
563  * conf_param key=value
564  *
565  * Note we can actually send mgc conf_params from clients, but currently
566  * that's only done for default file striping (see ll_send_mgc_param),
567  * and not here.
568  *
569  * After removal of a parameter (-d) Lustre will use the default
570  * AT NEXT REBOOT, not immediately.
571  */
572 int jt_lcfg_confparam(int argc, char **argv)
573 {
574         int rc;
575         int del = 0;
576         char *buf = NULL;
577
578         /* mgs_setparam processes only lctl buf #1 */
579         if ((argc > 3) || (argc <= 1))
580                 return CMD_HELP;
581
582         while ((rc = getopt(argc, argv, "d")) != -1) {
583                 switch (rc) {
584                 case 'd':
585                         del = 1;
586                         break;
587                 default:
588                         return CMD_HELP;
589                 }
590         }
591
592         buf = argv[optind];
593
594         if (del) {
595                 char *ptr;
596
597                 /* for delete, make it "<param>=\0" */
598                 buf = malloc(strlen(argv[optind]) + 2);
599                 if (!buf) {
600                         rc = -ENOMEM;
601                         goto out;
602                 }
603                 /* put an '=' on the end in case it doesn't have one */
604                 sprintf(buf, "%s=", argv[optind]);
605                 /* then truncate after the first '=' */
606                 ptr = strchr(buf, '=');
607                 *(++ptr) = '\0';
608         }
609
610         rc = lcfg_conf_param(argv[0], buf);
611
612         if (buf != argv[optind])
613                 free(buf);
614 out:
615         if (rc < 0) {
616                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
617                         strerror(-rc));
618         }
619
620         return rc;
621 }
622
623 /**
624  * Display a parameter path in the same format as sysctl.
625  * E.g. obdfilter.lustre-OST0000.stats
626  *
627  * \param[in] filename  file name of the parameter
628  * \param[in] st        parameter file stats
629  * \param[in] popt      set/get param options
630  *
631  * \retval allocated pointer containing modified filename
632  */
633 static char *
634 display_name(const char *filename, struct stat *st, struct param_opts *popt)
635 {
636         size_t suffix_len = 0;
637         char *suffix = NULL;
638         char *param_name;
639         char *tmp;
640
641         if (popt->po_show_type) {
642                 if (S_ISDIR(st->st_mode))
643                         suffix = "/";
644                 else if (S_ISLNK(st->st_mode))
645                         suffix = "@";
646                 else if (st->st_mode & S_IWUSR)
647                         suffix = "=";
648         }
649
650         /* Take the original filename string and chop off the glob addition */
651         tmp = strstr(filename, "/lustre/");
652         if (!tmp) {
653                 tmp = strstr(filename, "/lnet/");
654                 if (tmp)
655                         tmp += strlen("/lnet/");
656         } else {
657                 tmp += strlen("/lustre/");
658         }
659
660         /* Allocate return string */
661         param_name = strdup(tmp);
662         if (!param_name)
663                 return NULL;
664
665         /* replace '/' with '.' to match conf_param and sysctl */
666         for (tmp = strchr(param_name, '/'); tmp != NULL; tmp = strchr(tmp, '/'))
667                 *tmp = '.';
668
669         /* Append the indicator to entries if needed. */
670         if (popt->po_show_type && suffix != NULL) {
671                 suffix_len = strlen(suffix);
672
673                 tmp = realloc(param_name, suffix_len + strlen(param_name) + 1);
674                 if (tmp) {
675                         param_name = tmp;
676                         strncat(param_name, suffix,
677                                 strlen(param_name) + suffix_len);
678                 }
679         }
680
681         return param_name;
682 }
683
684 /**
685  * Turns a lctl parameter string into a procfs/sysfs subdirectory path pattern.
686  *
687  * \param[in] popt              Used to control parameter usage. For this
688  *                              function it is used to see if the path has
689  *                              a added suffix.
690  * \param[in,out] path          lctl parameter string that is turned into
691  *                              the subdirectory path pattern that is used
692  *                              to search the procfs/sysfs tree.
693  *
694  * \retval -errno on error.
695  */
696 static int
697 clean_path(struct param_opts *popt, char *path)
698 {
699         char *nidstart = NULL;
700         char *nidend = NULL;
701         char *tmp;
702
703         if (popt == NULL || path == NULL || strlen(path) == 0)
704                 return -EINVAL;
705
706         /* If path contains a suffix we need to remove it */
707         if (popt->po_show_type) {
708                 size_t path_end = strlen(path) - 1;
709
710                 tmp = path + path_end;
711                 switch (*tmp) {
712                 case '@':
713                 case '=':
714                 case '/':
715                         *tmp = '\0';
716                 default:
717                         break;
718                 }
719         }
720
721         /* get rid of '\', glob doesn't like it */
722         tmp = strrchr(path, '\\');
723         if (tmp) {
724                 char *tail = path + strlen(path);
725
726                 while (tmp != path) {
727                         if (*tmp == '\\') {
728                                 memmove(tmp, tmp + 1, tail - tmp);
729                                 --tail;
730                         }
731                         --tmp;
732                 }
733         }
734
735         /* Does path contain a NID string?  Skip '.->/' replacement for it. */
736         tmp = strchr(path, '@');
737         if (tmp) {
738                 /* First find the NID start.  NIDs may have variable (0-4) '.',
739                  * so find the common NID prefixes instead of trying to count
740                  * the dots.  Not great, but there are only two, and faster
741                  * than multiple speculative NID parses and bad DNS lookups.
742                  */
743                 if ((tmp = strstr(path, ".exports.")))
744                         nidstart = tmp + strlen(".exports.");
745                 else if ((tmp = strstr(path, ".MGC")))
746                         nidstart = tmp + 1;
747
748                 /* Next, find the end of the NID string. */
749                 if (nidstart)
750                         nidend = strchrnul(strchr(nidstart, '@'), '.');
751         }
752
753         /* replace param '.' with '/' */
754         for (tmp = strchr(path, '.'); tmp != NULL; tmp = strchr(tmp, '.')) {
755                 *tmp++ = '/';
756
757                 /*
758                  * There exist cases where some of the subdirectories of the
759                  * the parameter tree has embedded in its name a NID string.
760                  * This means that it is possible that these subdirectories
761                  * could have actual '.' in its name. If this is the case we
762                  * don't want to blindly replace the '.' with '/', so skip
763                  * over the part of the parameter containing the NID.
764                  */
765                 if (tmp == nidstart)
766                         tmp = nidend;
767         }
768
769         return 0;
770 }
771
772 /**
773  * The application lctl can perform three operations for lustre
774  * tunables. This enum defines those three operations which are
775  *
776  * 1) LIST_PARAM        - list available tunables
777  * 2) GET_PARAM         - report the current setting of a tunable
778  * 3) SET_PARAM         - set the tunable to a new value
779  */
780 enum parameter_operation {
781         LIST_PARAM,
782         GET_PARAM,
783         SET_PARAM,
784 };
785
786 char *parameter_opname[] = {
787         [LIST_PARAM] = "list_param",
788         [GET_PARAM] = "get_param",
789         [SET_PARAM] = "set_param",
790 };
791
792 /**
793  * Read the value of parameter
794  *
795  * \param[in]   path            full path to the parameter
796  * \param[in]   param_name      lctl parameter format of the
797  *                              parameter path
798  * \param[in]   popt            set/get param options
799  *
800  * \retval 0 on success.
801  * \retval -errno on error.
802  */
803 static int
804 read_param(const char *path, const char *param_name, struct param_opts *popt)
805 {
806         int rc = 0;
807         char *buf = NULL;
808         size_t buflen;
809
810         rc = llapi_param_get_value(path, &buf, &buflen);
811         if (rc != 0) {
812                 fprintf(stderr,
813                         "error: read_param: \'%s\': %s\n",
814                         path, strerror(-rc));
815                 goto free_buf;
816         }
817         /* don't print anything for empty files */
818         if (buf[0] == '\0')
819                 goto free_buf;
820
821         if (popt->po_show_path) {
822                 bool longbuf;
823
824                 longbuf = memchr(buf, '\n', buflen - 1) ||
825                           buflen + strlen(param_name) >= 80;
826                 printf("%s=%s", param_name, longbuf ? "\n" : "");
827         }
828         printf("%s", buf);
829
830 free_buf:
831         free(buf);
832         return rc;
833 }
834
835 /**
836  * Set a parameter to a specified value
837  *
838  * \param[in] path              full path to the parameter
839  * \param[in] param_name        lctl parameter format of the parameter path
840  * \param[in] popt              set/get param options
841  * \param[in] value             value to set the parameter to
842  *
843  * \retval number of bytes written on success.
844  * \retval -errno on error.
845  */
846 static int
847 write_param(const char *path, const char *param_name, struct param_opts *popt,
848             const char *value)
849 {
850         int fd, rc = 0;
851         ssize_t count;
852
853         if (!value)
854                 return -EINVAL;
855
856         /* Write the new value to the file */
857         fd = open(path, O_WRONLY);
858         if (fd < 0) {
859                 rc = -errno;
860                 fprintf(stderr, "error: set_param: opening '%s': %s\n",
861                         path, strerror(errno));
862                 return rc;
863         }
864
865         count = write(fd, value, strlen(value));
866         if (count < 0) {
867                 rc = -errno;
868                 if (errno != EIO) {
869                         fprintf(stderr, "error: set_param: setting %s=%s: %s\n",
870                                 path, value, strerror(errno));
871                 }
872         } else if (count < strlen(value)) { /* Truncate case */
873                 rc = -EINVAL;
874                 fprintf(stderr,
875                         "error: set_param: setting %s=%s: wrote only %zd\n",
876                         path, value, count);
877         } else if (popt->po_show_path) {
878                 printf("%s=%s\n", param_name, value);
879         }
880         close(fd);
881
882         return rc;
883 }
884
885 /**
886  * Perform a read, write or just a listing of a parameter
887  *
888  * \param[in] popt              list,set,get parameter options
889  * \param[in] pattern           search filter for the path of the parameter
890  * \param[in] value             value to set the parameter if write operation
891  * \param[in] mode              what operation to perform with the parameter
892  *
893  * \retval number of bytes written on success.
894  * \retval -errno on error and prints error message.
895  */
896 static int
897 param_display(struct param_opts *popt, char *pattern, char *value,
898               enum parameter_operation mode)
899 {
900         int dup_count = 0;
901         char **dup_cache;
902         glob_t paths;
903         char *opname = parameter_opname[mode];
904         int rc, i;
905
906         rc = llapi_param_get_paths(pattern, &paths);
907         if (rc != 0) {
908                 rc = -errno;
909                 if (!popt->po_recursive) {
910                         fprintf(stderr, "error: %s: param_path '%s': %s\n",
911                                 opname, pattern, strerror(errno));
912                 }
913                 return rc;
914         }
915
916         dup_cache = calloc(paths.gl_pathc, sizeof(char *));
917         if (!dup_cache) {
918                 rc = -ENOMEM;
919                 fprintf(stderr,
920                         "error: %s: allocating '%s' dup_cache[%zd]: %s\n",
921                         opname, pattern, paths.gl_pathc, strerror(-rc));
922                 goto out_param;
923         }
924
925         for (i = 0; i < paths.gl_pathc; i++) {
926                 char *param_name = NULL, *tmp;
927                 char pathname[PATH_MAX], param_dir[PATH_MAX + 2];
928                 struct stat st;
929                 int rc2, j;
930
931                 if (stat(paths.gl_pathv[i], &st) == -1) {
932                         fprintf(stderr, "error: %s: stat '%s': %s\n",
933                                 opname, paths.gl_pathv[i], strerror(errno));
934                         if (rc == 0)
935                                 rc = -errno;
936                         continue;
937                 }
938
939                 if (popt->po_only_dir && !S_ISDIR(st.st_mode))
940                         continue;
941
942                 param_name = display_name(paths.gl_pathv[i], &st, popt);
943                 if (!param_name) {
944                         fprintf(stderr,
945                                 "error: %s: generating name for '%s': %s\n",
946                                 opname, paths.gl_pathv[i], strerror(ENOMEM));
947                         if (rc == 0)
948                                 rc = -ENOMEM;
949                         continue;
950                 }
951
952                 switch (mode) {
953                 case GET_PARAM:
954                         /* Read the contents of file to stdout */
955                         if (S_ISREG(st.st_mode)) {
956                                 rc2 = read_param(paths.gl_pathv[i], param_name,
957                                                  popt);
958                                 if (rc2 < 0 && rc == 0)
959                                         rc = rc2;
960                         }
961                         break;
962                 case SET_PARAM:
963                         if (S_ISREG(st.st_mode)) {
964                                 rc2 = write_param(paths.gl_pathv[i],
965                                                   param_name, popt, value);
966                                 if (rc2 < 0 && rc == 0)
967                                         rc = rc2;
968                         }
969                         break;
970                 case LIST_PARAM:
971                         /**
972                          * For the upstream client the parameter files locations
973                          * are split between under both /sys/kernel/debug/lustre
974                          * and /sys/fs/lustre. The parameter files containing
975                          * small amounts of data, less than a page in size, are
976                          * located under /sys/fs/lustre and in the case of large
977                          * parameter data files, think stats for example, are
978                          * located in the debugfs tree. Since the files are
979                          * split across two trees the directories are often
980                          * duplicated which means these directories are listed
981                          * twice which leads to duplicate output to the user.
982                          * To avoid scanning a directory twice we have to cache
983                          * any directory and check if a search has been
984                          * requested twice.
985                          */
986                         for (j = 0; j < dup_count; j++) {
987                                 if (!strcmp(dup_cache[j], param_name))
988                                         break;
989                         }
990                         if (j != dup_count) {
991                                 free(param_name);
992                                 param_name = NULL;
993                                 continue;
994                         }
995                         dup_cache[dup_count++] = strdup(param_name);
996
997                         if (popt->po_show_path)
998                                 printf("%s\n", param_name);
999                         break;
1000                 }
1001
1002                 /*
1003                  * Only directories are searched recursively if
1004                  * requested by the user
1005                  */
1006                 if (!S_ISDIR(st.st_mode) || !popt->po_recursive) {
1007                         free(param_name);
1008                         param_name = NULL;
1009                         continue;
1010                 }
1011
1012                 /* Turn param_name into file path format */
1013                 rc2 = clean_path(popt, param_name);
1014                 if (rc2 < 0) {
1015                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1016                                 opname, param_name, strerror(-rc2));
1017                         free(param_name);
1018                         param_name = NULL;
1019                         if (rc == 0)
1020                                 rc = rc2;
1021                         continue;
1022                 }
1023
1024                 /* Use param_name to grab subdirectory tree from full path */
1025                 snprintf(param_dir, sizeof(param_dir), "/%s", param_name);
1026                 tmp = strstr(paths.gl_pathv[i], param_dir);
1027
1028                 /* cleanup paramname now that we are done with it */
1029                 free(param_name);
1030                 param_name = NULL;
1031                 memset(&param_dir, '\0', sizeof(param_dir));
1032
1033                 /* Shouldn't happen but just in case */
1034                 if (!tmp) {
1035                         if (rc == 0)
1036                                 rc = -EINVAL;
1037                         continue;
1038                 }
1039                 tmp++;
1040
1041                 rc2 = snprintf(pathname, sizeof(pathname), "%s/*", tmp);
1042                 if (rc2 < 0) {
1043                         /*
1044                          * snprintf() should never an error, and if it does
1045                          * there isn't much point trying to use fprintf()
1046                          */
1047                         continue;
1048                 }
1049                 if (rc2 >= sizeof(pathname)) {
1050                         fprintf(stderr, "error: %s: overflow processing '%s'\n",
1051                                 opname, pathname);
1052                         if (rc == 0)
1053                                 rc = -EINVAL;
1054                         continue;
1055                 }
1056
1057                 rc2 = param_display(popt, pathname, value, mode);
1058                 if (rc2 != 0 && rc2 != -ENOENT) {
1059                         /* errors will be printed by param_display() */
1060                         if (rc == 0)
1061                                 rc = rc2;
1062                         continue;
1063                 }
1064         }
1065
1066         for (i = 0; i < dup_count; i++)
1067                 free(dup_cache[i]);
1068         free(dup_cache);
1069 out_param:
1070         llapi_param_paths_free(&paths);
1071         return rc;
1072 }
1073
1074 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
1075 {
1076         int ch;
1077
1078         popt->po_show_path = 1;
1079         popt->po_only_path = 1;
1080
1081         while ((ch = getopt(argc, argv, "FRD")) != -1) {
1082                 switch (ch) {
1083                 case 'F':
1084                         popt->po_show_type = 1;
1085                         break;
1086                 case 'R':
1087                         popt->po_recursive = 1;
1088                         break;
1089                 case 'D':
1090                         popt->po_only_dir = 1;
1091                         break;
1092                 default:
1093                         return -1;
1094                 }
1095         }
1096
1097         return optind;
1098 }
1099
1100 int jt_lcfg_listparam(int argc, char **argv)
1101 {
1102         int rc = 0, index, i;
1103         struct param_opts popt;
1104         char *path;
1105
1106         memset(&popt, 0, sizeof(popt));
1107         index = listparam_cmdline(argc, argv, &popt);
1108         if (index < 0 || index >= argc)
1109                 return CMD_HELP;
1110
1111         for (i = index; i < argc; i++) {
1112                 int rc2;
1113
1114                 path = argv[i];
1115
1116                 rc2 = clean_path(&popt, path);
1117                 if (rc2 < 0) {
1118                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1119                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1120                         if (rc == 0)
1121                                 rc = rc2;
1122                         continue;
1123                 }
1124
1125                 rc2 = param_display(&popt, path, NULL, LIST_PARAM);
1126                 if (rc2 < 0) {
1127                         fprintf(stderr, "error: %s: listing '%s': %s\n",
1128                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1129                         if (rc == 0)
1130                                 rc = rc2;
1131                         continue;
1132                 }
1133         }
1134
1135         return rc;
1136 }
1137
1138 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
1139 {
1140         int ch;
1141
1142         popt->po_show_path = 1;
1143
1144         while ((ch = getopt(argc, argv, "FnNR")) != -1) {
1145                 switch (ch) {
1146                 case 'F':
1147                         popt->po_show_type = 1;
1148                         break;
1149                 case 'n':
1150                         popt->po_show_path = 0;
1151                         break;
1152                 case 'N':
1153                         popt->po_only_path = 1;
1154                         break;
1155                 case 'R':
1156                         popt->po_recursive = 1;
1157                         break;
1158                 default:
1159                         return -1;
1160                 }
1161         }
1162
1163         return optind;
1164 }
1165
1166 int jt_lcfg_getparam(int argc, char **argv)
1167 {
1168         int rc = 0, index, i;
1169         struct param_opts popt;
1170         char *path;
1171
1172         memset(&popt, 0, sizeof(popt));
1173         index = getparam_cmdline(argc, argv, &popt);
1174         if (index < 0 || index >= argc)
1175                 return CMD_HELP;
1176
1177         for (i = index; i < argc; i++) {
1178                 int rc2;
1179
1180                 path = argv[i];
1181
1182                 rc2 = clean_path(&popt, path);
1183                 if (rc2 < 0) {
1184                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1185                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1186                         if (rc == 0)
1187                                 rc = rc2;
1188                         continue;
1189                 }
1190
1191                 rc2 = param_display(&popt, path, NULL,
1192                                     popt.po_only_path ? LIST_PARAM : GET_PARAM);
1193                 if (rc2 < 0) {
1194                         if (rc == 0)
1195                                 rc = rc2;
1196                         continue;
1197                 }
1198         }
1199
1200         return rc;
1201 }
1202
1203 #ifdef HAVE_SERVER_SUPPORT
1204 /**
1205  * Output information about nodemaps.
1206  * \param       argc            number of args
1207  * \param       argv[]          variable string arguments
1208  *
1209  * [list|nodemap_name|all]      \a list will list all nodemaps (default).
1210  *                              Specifying a \a nodemap_name will
1211  *                              display info about that specific nodemap.
1212  *                              \a all will display info for all nodemaps.
1213  * \retval                      0 on success
1214  */
1215 int jt_nodemap_info(int argc, char **argv)
1216 {
1217         const char usage_str[] = "usage: nodemap_info [list|nodemap_name|all]\n";
1218         struct param_opts popt;
1219         int rc = 0;
1220
1221         memset(&popt, 0, sizeof(popt));
1222         popt.po_show_path = 1;
1223
1224         if (argc > 2) {
1225                 fprintf(stderr, usage_str);
1226                 return -1;
1227         }
1228
1229         if (argc == 1 || strcmp("list", argv[1]) == 0) {
1230                 popt.po_only_path = 1;
1231                 popt.po_only_dir = 1;
1232                 rc = param_display(&popt, "nodemap/*", NULL, LIST_PARAM);
1233         } else if (strcmp("all", argv[1]) == 0) {
1234                 rc = param_display(&popt, "nodemap/*/*", NULL, LIST_PARAM);
1235         } else {
1236                 char    pattern[PATH_MAX];
1237
1238                 snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
1239                 rc = param_display(&popt, pattern, NULL, LIST_PARAM);
1240                 if (rc == -ESRCH)
1241                         fprintf(stderr,
1242                                 "error: nodemap_info: cannot find nodemap %s\n",
1243                                 argv[1]);
1244         }
1245         return rc;
1246 }
1247 #endif
1248
1249 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
1250 {
1251         int ch;
1252
1253         popt->po_show_path = 1;
1254         popt->po_only_path = 0;
1255         popt->po_show_type = 0;
1256         popt->po_recursive = 0;
1257         popt->po_perm = 0;
1258         popt->po_delete = 0;
1259         popt->po_file = 0;
1260
1261         while ((ch = getopt(argc, argv, "nPdF")) != -1) {
1262                 switch (ch) {
1263                 case 'n':
1264                         popt->po_show_path = 0;
1265                         break;
1266                 case 'P':
1267                         popt->po_perm = 1;
1268                         break;
1269                 case 'd':
1270                         popt->po_delete = 1;
1271                         break;
1272                 case 'F':
1273                         popt->po_file = 1;
1274                         break;
1275                 default:
1276                         return -1;
1277                 }
1278         }
1279         return optind;
1280 }
1281
1282 enum paramtype {
1283         PT_NONE = 0,
1284         PT_SETPARAM,
1285         PT_CONFPARAM
1286 };
1287
1288 #define PS_NONE 0
1289 #define PS_PARAM_FOUND 1
1290 #define PS_PARAM_SET 2
1291 #define PS_VAL_FOUND 4
1292 #define PS_VAL_SET 8
1293 #define PS_DEVICE_FOUND 16
1294 #define PS_DEVICE_SET 32
1295
1296 #define PARAM_SZ 256
1297
1298 static struct cfg_type_data {
1299         enum paramtype ptype;
1300         char *type_name;
1301 } cfg_type_table[] = {
1302         { PT_SETPARAM, "set_param" },
1303         { PT_CONFPARAM, "conf_param" },
1304         { PT_NONE, "none" }
1305 };
1306
1307 static struct cfg_stage_data {
1308         int pstage;
1309         char *stage_name;
1310 } cfg_stage_table[] = {
1311         { PS_PARAM_FOUND, "parameter" },
1312         { PS_VAL_FOUND, "value" },
1313         { PS_DEVICE_FOUND, "device" },
1314         { PS_NONE, "none" }
1315 };
1316
1317 void conf_to_set_param(enum paramtype confset, const char *param,
1318                        const char *device, char *buf,
1319                        int bufsize)
1320 {
1321         char *tmp;
1322
1323         if (confset == PT_SETPARAM) {
1324                 strncpy(buf, param, bufsize);
1325                 return;
1326         }
1327
1328         /*
1329          * sys.* params are top level, we just need to trim the sys.
1330          */
1331         tmp = strstr(param, "sys.");
1332         if (tmp) {
1333                 tmp += 4;
1334                 strncpy(buf, tmp, bufsize);
1335                 return;
1336         }
1337
1338         /*
1339          * parameters look like type.parameter, we need to stick the device
1340          * in the middle.  Example combine mdt.identity_upcall with device
1341          * lustre-MDT0000 for mdt.lustre-MDT0000.identity_upcall
1342          */
1343
1344         tmp = strchrnul(param, '.');
1345         snprintf(buf, tmp - param + 1, "%s", param);
1346         buf += tmp - param;
1347         bufsize -= tmp - param;
1348         snprintf(buf, bufsize, ".%s%s", device, tmp);
1349 }
1350
1351 int lcfg_setparam_yaml(char *func, char *filename)
1352 {
1353         FILE *file;
1354         yaml_parser_t parser;
1355         yaml_token_t token;
1356         int rc = 0;
1357
1358         enum paramtype confset = PT_NONE;
1359         int param = PS_NONE;
1360         char *tmp;
1361         char parameter[PARAM_SZ + 1];
1362         char value[PARAM_SZ + 1];
1363         char device[PARAM_SZ + 1];
1364
1365         file = fopen(filename, "rb");
1366         yaml_parser_initialize(&parser);
1367         yaml_parser_set_input_file(&parser, file);
1368
1369         /*
1370          * Search tokens for conf_param or set_param
1371          * The token after "parameter" goes into parameter
1372          * The token after "value" goes into value
1373          * when we have all 3, create param=val and call the
1374          * appropriate function for set/conf param
1375          */
1376         while (token.type != YAML_STREAM_END_TOKEN && rc == 0) {
1377                 int i;
1378
1379                 yaml_token_delete(&token);
1380                 if (!yaml_parser_scan(&parser, &token)) {
1381                         rc = 1;
1382                         break;
1383                 }
1384
1385                 if (token.type != YAML_SCALAR_TOKEN)
1386                         continue;
1387
1388                 for (i = 0; cfg_type_table[i].ptype != PT_NONE; i++) {
1389                         if (!strncmp((char *)token.data.alias.value,
1390                                      cfg_type_table[i].type_name,
1391                                      strlen(cfg_type_table[i].type_name))) {
1392                                 confset = cfg_type_table[i].ptype;
1393                                 break;
1394                         }
1395                 }
1396
1397                 if (confset == PT_NONE)
1398                         continue;
1399
1400                 for (i = 0; cfg_stage_table[i].pstage != PS_NONE; i++) {
1401                         if (!strncmp((char *)token.data.alias.value,
1402                                      cfg_stage_table[i].stage_name,
1403                                      strlen(cfg_stage_table[i].stage_name))) {
1404                                 param |= cfg_stage_table[i].pstage;
1405                                 break;
1406                         }
1407                 }
1408
1409                 if (cfg_stage_table[i].pstage != PS_NONE)
1410                         continue;
1411
1412                 if (param & PS_PARAM_FOUND) {
1413                         conf_to_set_param(confset,
1414                                           (char *)token.data.alias.value,
1415                                           device, parameter, PARAM_SZ);
1416                         param |= PS_PARAM_SET;
1417                         param &= ~PS_PARAM_FOUND;
1418
1419                         /*
1420                          * we're getting parameter: param=val
1421                          * copy val and mark that we've got it in case
1422                          * there is no value: tag
1423                          */
1424                         tmp = strchrnul(parameter, '=');
1425                         if (*tmp == '=') {
1426                                 strncpy(value, tmp + 1, sizeof(value) - 1);
1427                                 *tmp = '\0';
1428                                 param |= PS_VAL_SET;
1429                         } else {
1430                                 continue;
1431                         }
1432                 } else if (param & PS_VAL_FOUND) {
1433                         strncpy(value, (char *)token.data.alias.value,
1434                                 PARAM_SZ);
1435                         param |= PS_VAL_SET;
1436                         param &= ~PS_VAL_FOUND;
1437                 } else if (param & PS_DEVICE_FOUND) {
1438                         strncpy(device, (char *)token.data.alias.value,
1439                                 PARAM_SZ);
1440                         param |= PS_DEVICE_SET;
1441                         param &= ~PS_DEVICE_FOUND;
1442                 }
1443
1444                 if (confset && param & PS_VAL_SET && param & PS_PARAM_SET) {
1445                         int size = strlen(parameter) + strlen(value) + 2;
1446                         char *buf = malloc(size);
1447
1448                         if (!buf) {
1449                                 rc = 2;
1450                                 break;
1451                         }
1452                         snprintf(buf, size, "%s=%s", parameter, value);
1453
1454                         printf("set_param: %s\n", buf);
1455                         rc = lcfg_setparam_perm(func, buf);
1456
1457                         confset = PT_NONE;
1458                         param = PS_NONE;
1459                         parameter[0] = '\0';
1460                         value[0] = '\0';
1461                         device[0] = '\0';
1462                         free(buf);
1463                 }
1464         }
1465
1466         yaml_parser_delete(&parser);
1467         fclose(file);
1468
1469         return rc;
1470 }
1471
1472 int jt_lcfg_setparam(int argc, char **argv)
1473 {
1474         int rc = 0, index, i;
1475         struct param_opts popt;
1476         char *path = NULL, *value = NULL;
1477
1478         memset(&popt, 0, sizeof(popt));
1479         index = setparam_cmdline(argc, argv, &popt);
1480         if (index < 0 || index >= argc)
1481                 return CMD_HELP;
1482
1483         if (popt.po_perm)
1484                 /*
1485                  * We can't delete parameters that were
1486                  * set with old conf_param interface
1487                  */
1488                 return jt_lcfg_setparam_perm(argc, argv, &popt);
1489
1490         if (popt.po_file)
1491                 return lcfg_setparam_yaml(argv[0], argv[index]);
1492
1493         for (i = index; i < argc; i++) {
1494                 int rc2;
1495
1496                 path = argv[i];
1497                 value = strchr(path, '=');
1498                 if (value) {
1499                         /* format: set_param a=b */
1500                         *value = '\0';
1501                         value++;
1502                         if (*value == '\0') {
1503                                 fprintf(stderr,
1504                                         "error: %s: setting %s: no value\n",
1505                                         jt_cmdname(argv[0]), path);
1506                                 if (rc == 0)
1507                                         rc = -EINVAL;
1508                                 continue;
1509                         }
1510                 } else {
1511                         /* format: set_param a b */
1512                         i++;
1513                         if (i >= argc) {
1514                                 fprintf(stderr,
1515                                         "error: %s: setting %s: no value\n",
1516                                         jt_cmdname(argv[0]), path);
1517                                 if (rc == 0)
1518                                         rc = -EINVAL;
1519                                 break;
1520                         }
1521                         value = argv[i];
1522                 }
1523
1524                 rc2 = clean_path(&popt, path);
1525                 if (rc2 < 0) {
1526                         fprintf(stderr, "error: %s: cleaning %s: %s\n",
1527                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1528                         if (rc == 0)
1529                                 rc = rc2;
1530                         continue;
1531                 }
1532
1533                 rc2 = param_display(&popt, path, value, SET_PARAM);
1534                 if (rc == 0)
1535                         rc = rc2;
1536         }
1537
1538         return rc;
1539 }