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