Whamcloud - gitweb
LU-4939 utils: allow configuration through yaml files
[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 dir_count = 0;
973         char **dir_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         dir_cache = calloc(paths.gl_pathc, sizeof(char *));
989         if (dir_cache == NULL) {
990                 rc = -ENOMEM;
991                 fprintf(stderr,
992                         "error: %s: allocating '%s' dir_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;
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                 /**
1025                  * For the upstream client the parameter files locations
1026                  * are split between under both /sys/kernel/debug/lustre
1027                  * and /sys/fs/lustre. The parameter files containing
1028                  * small amounts of data, less than a page in size, are
1029                  * located under /sys/fs/lustre and in the case of large
1030                  * parameter data files, think stats for example, are
1031                  * located in the debugfs tree. Since the files are split
1032                  * across two trees the directories are often duplicated
1033                  * which means these directories are listed twice which
1034                  * leads to duplicate output to the user. To avoid scanning
1035                  * a directory twice we have to cache any directory and
1036                  * check if a search has been requested twice.
1037                  */
1038                 if (S_ISDIR(st.st_mode)) {
1039                         int j;
1040
1041                         for (j = 0; j < dir_count; j++) {
1042                                 if (!strcmp(dir_cache[j], param_name))
1043                                         break;
1044                         }
1045                         if (j != dir_count) {
1046                                 free(param_name);
1047                                 param_name = NULL;
1048                                 continue;
1049                         }
1050                         dir_cache[dir_count++] = strdup(param_name);
1051                 }
1052
1053                 switch (mode) {
1054                 case GET_PARAM:
1055                         /* Read the contents of file to stdout */
1056                         if (S_ISREG(st.st_mode)) {
1057                                 rc2 = read_param(paths.gl_pathv[i], param_name,
1058                                                  popt);
1059                                 if (rc2 < 0 && rc == 0)
1060                                         rc = rc2;
1061                         }
1062                         break;
1063                 case SET_PARAM:
1064                         if (S_ISREG(st.st_mode)) {
1065                                 rc2 = write_param(paths.gl_pathv[i],
1066                                                   param_name, popt, value);
1067                                 if (rc2 < 0 && rc == 0)
1068                                         rc = rc2;
1069                         }
1070                         break;
1071                 case LIST_PARAM:
1072                         if (popt->po_show_path)
1073                                 printf("%s\n", param_name);
1074                         break;
1075                 }
1076
1077                 /* Only directories are searched recursively if
1078                  * requested by the user */
1079                 if (!S_ISDIR(st.st_mode) || !popt->po_recursive) {
1080                         free(param_name);
1081                         param_name = NULL;
1082                         continue;
1083                 }
1084
1085                 /* Turn param_name into file path format */
1086                 rc2 = clean_path(popt, param_name);
1087                 if (rc2 < 0) {
1088                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1089                                 opname, param_name, strerror(-rc2));
1090                         free(param_name);
1091                         param_name = NULL;
1092                         if (rc == 0)
1093                                 rc = rc2;
1094                         continue;
1095                 }
1096
1097                 /* Use param_name to grab subdirectory tree from full path */
1098                 tmp = strstr(paths.gl_pathv[i], param_name);
1099
1100                 /* cleanup paramname now that we are done with it */
1101                 free(param_name);
1102                 param_name = NULL;
1103
1104                 /* Shouldn't happen but just in case */
1105                 if (tmp == NULL) {
1106                         if (rc == 0)
1107                                 rc = -EINVAL;
1108                         continue;
1109                 }
1110
1111                 rc2 = snprintf(pathname, sizeof(pathname), "%s/*", tmp);
1112                 if (rc2 < 0) {
1113                         /* snprintf() should never an error, and if it does
1114                          * there isn't much point trying to use fprintf() */
1115                         continue;
1116                 }
1117                 if (rc2 >= sizeof(pathname)) {
1118                         fprintf(stderr, "error: %s: overflow processing '%s'\n",
1119                                 opname, pathname);
1120                         if (rc == 0)
1121                                 rc = -EINVAL;
1122                         continue;
1123                 }
1124
1125                 rc2 = param_display(popt, pathname, value, mode);
1126                 if (rc2 != 0 && rc2 != -ENOENT) {
1127                         /* errors will be printed by param_display() */
1128                         if (rc == 0)
1129                                 rc = rc2;
1130                         continue;
1131                 }
1132         }
1133
1134         for (i = 0; i < dir_count; i++)
1135                 free(dir_cache[i]);
1136         free(dir_cache);
1137 out_param:
1138         cfs_free_param_data(&paths);
1139         return rc;
1140 }
1141
1142 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
1143 {
1144         int ch;
1145
1146         popt->po_show_path = 1;
1147         popt->po_only_path = 1;
1148
1149         while ((ch = getopt(argc, argv, "FRD")) != -1) {
1150                 switch (ch) {
1151                 case 'F':
1152                         popt->po_show_type = 1;
1153                         break;
1154                 case 'R':
1155                         popt->po_recursive = 1;
1156                         break;
1157                 case 'D':
1158                         popt->po_only_dir = 1;
1159                         break;
1160                 default:
1161                         return -1;
1162                 }
1163         }
1164
1165         return optind;
1166 }
1167
1168 int jt_lcfg_listparam(int argc, char **argv)
1169 {
1170         int rc = 0, index, i;
1171         struct param_opts popt;
1172         char *path;
1173
1174         memset(&popt, 0, sizeof(popt));
1175         index = listparam_cmdline(argc, argv, &popt);
1176         if (index < 0 || index >= argc)
1177                 return CMD_HELP;
1178
1179         for (i = index; i < argc; i++) {
1180                 int rc2;
1181
1182                 path = argv[i];
1183
1184                 rc2 = clean_path(&popt, path);
1185                 if (rc2 < 0) {
1186                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1187                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1188                         if (rc == 0)
1189                                 rc = rc2;
1190                         continue;
1191                 }
1192
1193                 rc2 = param_display(&popt, path, NULL, LIST_PARAM);
1194                 if (rc2 < 0) {
1195                         fprintf(stderr, "error: %s: listing '%s': %s\n",
1196                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1197                         if (rc == 0)
1198                                 rc = rc2;
1199                         continue;
1200                 }
1201         }
1202
1203         return rc;
1204 }
1205
1206 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
1207 {
1208         int ch;
1209
1210         popt->po_show_path = 1;
1211
1212         while ((ch = getopt(argc, argv, "FnNR")) != -1) {
1213                 switch (ch) {
1214                 case 'F':
1215                         popt->po_show_type = 1;
1216                         break;
1217                 case 'n':
1218                         popt->po_show_path = 0;
1219                         break;
1220                 case 'N':
1221                         popt->po_only_path = 1;
1222                         break;
1223                 case 'R':
1224                         popt->po_recursive = 1;
1225                         break;
1226                 default:
1227                         return -1;
1228                 }
1229         }
1230
1231         return optind;
1232 }
1233
1234 int jt_lcfg_getparam(int argc, char **argv)
1235 {
1236         int rc = 0, index, i;
1237         struct param_opts popt;
1238         char *path;
1239
1240         memset(&popt, 0, sizeof(popt));
1241         index = getparam_cmdline(argc, argv, &popt);
1242         if (index < 0 || index >= argc)
1243                 return CMD_HELP;
1244
1245         for (i = index; i < argc; i++) {
1246                 int rc2;
1247
1248                 path = argv[i];
1249
1250                 rc2 = clean_path(&popt, path);
1251                 if (rc2 < 0) {
1252                         fprintf(stderr, "error: %s: cleaning '%s': %s\n",
1253                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1254                         if (rc == 0)
1255                                 rc = rc2;
1256                         continue;
1257                 }
1258
1259                 rc2 = param_display(&popt, path, NULL,
1260                                    popt.po_only_path ? LIST_PARAM : GET_PARAM);
1261                 if (rc2 < 0) {
1262                         if (rc == 0)
1263                                 rc = rc2;
1264                         continue;
1265                 }
1266         }
1267
1268         return rc;
1269 }
1270
1271 /**
1272  * Output information about nodemaps.
1273  * \param       argc            number of args
1274  * \param       argv[]          variable string arguments
1275  *
1276  * [list|nodemap_name|all]      \a list will list all nodemaps (default).
1277  *                              Specifying a \a nodemap_name will
1278  *                              display info about that specific nodemap.
1279  *                              \a all will display info for all nodemaps.
1280  * \retval                      0 on success
1281  */
1282 int jt_nodemap_info(int argc, char **argv)
1283 {
1284         const char              usage_str[] = "usage: nodemap_info "
1285                                               "[list|nodemap_name|all]\n";
1286         struct param_opts       popt;
1287         int                     rc = 0;
1288
1289         memset(&popt, 0, sizeof(popt));
1290         popt.po_show_path = 1;
1291
1292         if (argc > 2) {
1293                 fprintf(stderr, usage_str);
1294                 return -1;
1295         }
1296
1297         if (argc == 1 || strcmp("list", argv[1]) == 0) {
1298                 popt.po_only_path = 1;
1299                 popt.po_only_dir = 1;
1300                 rc = param_display(&popt, "nodemap/*", NULL, LIST_PARAM);
1301         } else if (strcmp("all", argv[1]) == 0) {
1302                 rc = param_display(&popt, "nodemap/*/*", NULL, LIST_PARAM);
1303         } else {
1304                 char    pattern[PATH_MAX];
1305
1306                 snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
1307                 rc = param_display(&popt, pattern, NULL, LIST_PARAM);
1308                 if (rc == -ESRCH)
1309                         fprintf(stderr, "error: nodemap_info: cannot find "
1310                                         "nodemap %s\n", argv[1]);
1311         }
1312         return rc;
1313 }
1314
1315 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
1316 {
1317         int ch;
1318
1319         popt->po_show_path = 1;
1320         popt->po_only_path = 0;
1321         popt->po_show_type = 0;
1322         popt->po_recursive = 0;
1323         popt->po_perm = 0;
1324         popt->po_delete = 0;
1325         popt->po_file = 0;
1326
1327         while ((ch = getopt(argc, argv, "nPdF")) != -1) {
1328                 switch (ch) {
1329                 case 'n':
1330                         popt->po_show_path = 0;
1331                         break;
1332                 case 'P':
1333                         popt->po_perm = 1;
1334                         break;
1335                 case 'd':
1336                         popt->po_delete = 1;
1337                         break;
1338                 case 'F':
1339                         popt->po_file = 1;
1340                         break;
1341                 default:
1342                         return -1;
1343                 }
1344         }
1345         return optind;
1346 }
1347
1348 enum paramtype {
1349         PT_NONE = 0,
1350         PT_SETPARAM,
1351         PT_CONFPARAM
1352 };
1353
1354
1355 #define PS_NONE 0
1356 #define PS_PARAM_FOUND 1
1357 #define PS_PARAM_SET 2
1358 #define PS_VAL_FOUND 4
1359 #define PS_VAL_SET 8
1360 #define PS_DEVICE_FOUND 16
1361 #define PS_DEVICE_SET 32
1362
1363 #define PARAM_SZ 256
1364
1365 static struct cfg_type_data {
1366         enum paramtype ptype;
1367         char *type_name;
1368 } cfg_type_table[] = {
1369         { PT_SETPARAM, "set_param" },
1370         { PT_CONFPARAM, "conf_param" },
1371         { PT_NONE, "none" }
1372 };
1373
1374 static struct cfg_stage_data {
1375         int pstage;
1376         char *stage_name;
1377 } cfg_stage_table[] = {
1378         { PS_PARAM_FOUND, "parameter" },
1379         { PS_VAL_FOUND, "value" },
1380         { PS_DEVICE_FOUND, "device" },
1381         { PS_NONE, "none" }
1382 };
1383
1384
1385 void conf_to_set_param(enum paramtype confset, const char *param,
1386                        const char *device, char *buf,
1387                        int bufsize)
1388 {
1389         char *tmp;
1390
1391         if (confset == PT_SETPARAM) {
1392                 strncpy(buf, param, bufsize);
1393                 return;
1394         }
1395
1396         /*
1397          * sys.* params are top level, we just need to trim the sys.
1398          */
1399         tmp = strstr(param, "sys.");
1400         if (tmp != NULL) {
1401                 tmp += 4;
1402                 strncpy(buf, tmp, bufsize);
1403                 return;
1404         }
1405
1406         /*
1407          * parameters look like type.parameter, we need to stick the device
1408          * in the middle.  Example combine mdt.identity_upcall with device
1409          * lustre-MDT0000 for mdt.lustre-MDT0000.identity_upcall
1410          */
1411
1412         tmp = strchrnul(param, '.');
1413         snprintf(buf, tmp - param + 1, "%s", param);
1414         buf += tmp - param;
1415         bufsize -= tmp - param;
1416         snprintf(buf, bufsize, ".%s%s", device, tmp);
1417 }
1418
1419 int lcfg_setparam_yaml(char *func, char *filename)
1420 {
1421         FILE *file;
1422         yaml_parser_t parser;
1423         yaml_token_t token;
1424         int rc = 0;
1425
1426         enum paramtype confset = PT_NONE;
1427         int param = PS_NONE;
1428         char *tmp;
1429         char parameter[PARAM_SZ];
1430         char value[PARAM_SZ];
1431         char device[PARAM_SZ];
1432
1433         file = fopen(filename, "rb");
1434         yaml_parser_initialize(&parser);
1435         yaml_parser_set_input_file(&parser, file);
1436
1437         /*
1438          * Search tokens for conf_param or set_param
1439          * The token after "parameter" goes into parameter
1440          * The token after "value" goes into value
1441          * when we have all 3, create param=val and call the
1442          * appropriate function for set/conf param
1443          */
1444         while (token.type != YAML_STREAM_END_TOKEN && rc == 0) {
1445                 int i;
1446
1447                 yaml_token_delete(&token);
1448                 if (!yaml_parser_scan(&parser, &token)) {
1449                         rc = 1;
1450                         break;
1451                 }
1452
1453                 if (token.type != YAML_SCALAR_TOKEN)
1454                         continue;
1455
1456                 for (i = 0; cfg_type_table[i].ptype != PT_NONE; i++) {
1457                         if (!strncmp((char *)token.data.alias.value,
1458                                      cfg_type_table[i].type_name,
1459                                      strlen(cfg_type_table[i].type_name))) {
1460                                 confset = cfg_type_table[i].ptype;
1461                                 break;
1462                         }
1463                 }
1464
1465                 if (confset == PT_NONE)
1466                         continue;
1467
1468                 for (i = 0; cfg_stage_table[i].pstage != PS_NONE; i++) {
1469                         if (!strncmp((char *)token.data.alias.value,
1470                                      cfg_stage_table[i].stage_name,
1471                                      strlen(cfg_stage_table[i].stage_name))) {
1472                                 param |= cfg_stage_table[i].pstage;
1473                                 break;
1474                         }
1475                 }
1476
1477                 if (cfg_stage_table[i].pstage != PS_NONE)
1478                         continue;
1479
1480                 if (param & PS_PARAM_FOUND) {
1481                         conf_to_set_param(confset,
1482                                           (char *)token.data.alias.value,
1483                                           device, parameter, PARAM_SZ);
1484                         param |= PS_PARAM_SET;
1485                         param &= ~PS_PARAM_FOUND;
1486
1487                         /*
1488                          * we're getting parameter: param=val
1489                          * copy val and mark that we've got it in case
1490                          * there is no value: tag
1491                          */
1492                         tmp = strchrnul(parameter, '=');
1493                         if (*tmp == '=') {
1494                                 strncpy(value, tmp+1, sizeof(value));
1495                                 *tmp = '\0';
1496                                 param |= PS_VAL_SET;
1497                         } else {
1498                                 continue;
1499                         }
1500                 } else if (param & PS_VAL_FOUND) {
1501                         strncpy(value, (char *)token.data.alias.value,
1502                                 PARAM_SZ);
1503                         param |= PS_VAL_SET;
1504                         param &= ~PS_VAL_FOUND;
1505                 } else if (param & PS_DEVICE_FOUND) {
1506                         strncpy(device, (char *)token.data.alias.value,
1507                                 PARAM_SZ);
1508                         param |= PS_DEVICE_SET;
1509                         param &= ~PS_DEVICE_FOUND;
1510                 }
1511
1512                 if (confset && param & PS_VAL_SET && param & PS_PARAM_SET) {
1513                         int size = strlen(parameter) + strlen(value) + 2;
1514                         char *buf = malloc(size);
1515
1516                         if (buf == NULL) {
1517                                 rc = 2;
1518                                 break;
1519                         }
1520                         snprintf(buf, size, "%s=%s", parameter, value);
1521
1522                         printf("set_param: %s\n", buf);
1523                         rc = lcfg_setparam_perm(func, buf);
1524
1525                         confset = PT_NONE;
1526                         param = PS_NONE;
1527                         parameter[0] = '\0';
1528                         value[0] = '\0';
1529                         device[0] = '\0';
1530                         free(buf);
1531                 }
1532         }
1533
1534         yaml_parser_delete(&parser);
1535         fclose(file);
1536
1537         return rc;
1538 }
1539
1540 int jt_lcfg_setparam(int argc, char **argv)
1541 {
1542         int rc = 0, index, i;
1543         struct param_opts popt;
1544         char *path = NULL, *value = NULL;
1545
1546         memset(&popt, 0, sizeof(popt));
1547         index = setparam_cmdline(argc, argv, &popt);
1548         if (index < 0 || index >= argc)
1549                 return CMD_HELP;
1550
1551         if (popt.po_perm)
1552                 /* We can't delete parameters that were
1553                  * set with old conf_param interface */
1554                 return jt_lcfg_setparam_perm(argc, argv, &popt);
1555
1556         if (popt.po_file)
1557                 return lcfg_setparam_yaml(argv[0], argv[index]);
1558
1559         for (i = index; i < argc; i++) {
1560                 int rc2;
1561                 path = argv[i];
1562
1563                 value = strchr(path, '=');
1564                 if (value != NULL) {
1565                         /* format: set_param a=b */
1566                         *value = '\0';
1567                         value++;
1568                         if (*value == '\0') {
1569                                 fprintf(stderr,
1570                                         "error: %s: setting %s: no value\n",
1571                                         jt_cmdname(argv[0]), path);
1572                                 if (rc == 0)
1573                                         rc = -EINVAL;
1574                                 continue;
1575                         }
1576                 } else {
1577                         /* format: set_param a b */
1578                         i++;
1579                         if (i >= argc) {
1580                                 fprintf(stderr,
1581                                         "error: %s: setting %s: no value\n",
1582                                         jt_cmdname(argv[0]), path);
1583                                 if (rc == 0)
1584                                         rc = -EINVAL;
1585                                 break;
1586                         } else {
1587                                 value = argv[i];
1588                         }
1589                 }
1590
1591                 rc2 = clean_path(&popt, path);
1592                 if (rc2 < 0) {
1593                         fprintf(stderr, "error: %s: cleaning %s: %s\n",
1594                                 jt_cmdname(argv[0]), path, strerror(-rc2));
1595                         if (rc == 0)
1596                                 rc = rc2;
1597                         continue;
1598                 }
1599
1600                 rc2 = param_display(&popt, path, value, SET_PARAM);
1601                 if (rc == 0)
1602                         rc = rc2;
1603         }
1604
1605         return rc;
1606 }