Whamcloud - gitweb
b=20580
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lustre_cfg.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  * Author: Robert Read <rread@clusterfs.com>
42  */
43
44 #include <stdlib.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <stdio.h>
48 #include <stdarg.h>
49 #include <ctype.h>
50 #include <glob.h>
51
52 #ifndef __KERNEL__
53 #include <liblustre.h>
54 #endif
55 #include <lustre_lib.h>
56 #include <lustre_cfg.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_dlm.h>
59 #include <obd.h>          /* for struct lov_stripe_md */
60 #include <obd_lov.h>
61 #include <lustre/lustre_build_version.h>
62
63 #include <unistd.h>
64 #include <sys/un.h>
65 #include <time.h>
66 #include <sys/time.h>
67 #include <errno.h>
68 #include <string.h>
69
70
71 #include "obdctl.h"
72 #include <lnet/lnetctl.h>
73 #include "parser.h"
74 #include <stdio.h>
75
76 static char * lcfg_devname;
77
78 int lcfg_set_devname(char *name)
79 {
80         if (name) {
81                 if (lcfg_devname)
82                         free(lcfg_devname);
83                 /* quietly strip the unnecessary '$' */
84                 if (*name == '$' || *name == '%')
85                         name++;
86                 if (isdigit(*name)) {
87                         /* We can't translate from dev # to name */
88                         lcfg_devname = NULL;
89                 } else {
90                         lcfg_devname = strdup(name);
91                 }
92         } else {
93                 lcfg_devname = NULL;
94         }
95         return 0;
96 }
97
98 char * lcfg_get_devname(void)
99 {
100         return lcfg_devname;
101 }
102
103 int jt_lcfg_device(int argc, char **argv)
104 {
105         return jt_obd_device(argc, argv);
106 }
107
108 int jt_lcfg_attach(int argc, char **argv)
109 {
110         struct lustre_cfg_bufs bufs;
111         struct lustre_cfg *lcfg;
112         int rc;
113
114         if (argc != 4)
115                 return CMD_HELP;
116
117         lustre_cfg_bufs_reset(&bufs, NULL);
118
119         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
120         lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
121         lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
122
123         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
124         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
125         lustre_cfg_free(lcfg);
126         if (rc < 0) {
127                 fprintf(stderr, "error: %s: LCFG_ATTACH %s\n",
128                         jt_cmdname(argv[0]), strerror(rc = errno));
129         } else if (argc == 3) {
130                 char name[1024];
131
132                 lcfg_set_devname(argv[2]);
133                 if (strlen(argv[2]) > 128) {
134                         printf("Name too long to set environment\n");
135                         return -EINVAL;
136                 }
137                 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
138                 rc = setenv(name, argv[1], 1);
139                 if (rc) {
140                         printf("error setting env variable %s\n", name);
141                 }
142         } else {
143                 lcfg_set_devname(argv[2]);
144         }
145
146         return rc;
147 }
148
149 int jt_lcfg_setup(int argc, char **argv)
150 {
151         struct lustre_cfg_bufs bufs;
152         struct lustre_cfg *lcfg;
153         int i;
154         int rc;
155
156         if (lcfg_devname == NULL) {
157                 fprintf(stderr, "%s: please use 'device name' to set the "
158                         "device name for config commands.\n",
159                         jt_cmdname(argv[0]));
160                 return -EINVAL;
161         }
162
163         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
164
165         if (argc > 6)
166                 return CMD_HELP;
167
168         for (i = 1; i < argc; i++) {
169                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
170         }
171
172         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
173         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
174         lustre_cfg_free(lcfg);
175         if (rc < 0)
176                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
177                         strerror(rc = errno));
178
179         return rc;
180 }
181
182 int jt_obd_detach(int argc, char **argv)
183 {
184         struct lustre_cfg_bufs bufs;
185         struct lustre_cfg *lcfg;
186         int rc;
187
188         if (lcfg_devname == NULL) {
189                 fprintf(stderr, "%s: please use 'device name' to set the "
190                         "device name for config commands.\n",
191                         jt_cmdname(argv[0]));
192                 return -EINVAL;
193         }
194
195         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
196
197         if (argc != 1)
198                 return CMD_HELP;
199
200         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
201         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
202         lustre_cfg_free(lcfg);
203         if (rc < 0)
204                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
205                         strerror(rc = errno));
206
207         return rc;
208 }
209
210 int jt_obd_cleanup(int argc, char **argv)
211 {
212         struct lustre_cfg_bufs bufs;
213         struct lustre_cfg *lcfg;
214         char force = 'F';
215         char failover = 'A';
216         char flags[3] = { 0 };
217         int flag_cnt = 0, n;
218         int rc;
219
220         if (lcfg_devname == NULL) {
221                 fprintf(stderr, "%s: please use 'device name' to set the "
222                         "device name for config commands.\n",
223                         jt_cmdname(argv[0]));
224                 return -EINVAL;
225         }
226
227         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
228
229         if (argc < 1 || argc > 3)
230                 return CMD_HELP;
231
232         /* we are protected from overflowing our buffer by the argc
233          * check above
234          */
235         for (n = 1; n < argc; n++) {
236                 if (strcmp(argv[n], "force") == 0) {
237                         flags[flag_cnt++] = force;
238                 } else if (strcmp(argv[n], "failover") == 0) {
239                         flags[flag_cnt++] = failover;
240                 } else {
241                         fprintf(stderr, "unknown option: %s", argv[n]);
242                         return CMD_HELP;
243                 }
244         }
245
246         if (flag_cnt) {
247                 lustre_cfg_bufs_set_string(&bufs, 1, flags);
248         }
249
250         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
251         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
252         lustre_cfg_free(lcfg);
253         if (rc < 0)
254                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
255                         strerror(rc = errno));
256
257         return rc;
258 }
259
260 static
261 int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
262 {
263         int rc;
264         struct lustre_cfg_bufs bufs;
265         struct lustre_cfg *lcfg;
266
267         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
268         if (uuid)
269                 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
270
271         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
272         lcfg->lcfg_nid = nid;
273         /* Poison NAL -- pre 1.4.6 will LASSERT on 0 NAL, this way it
274            doesn't work without crashing (bz 10130) */
275         lcfg->lcfg_nal = 0x5a;
276
277 #if 0
278         fprintf(stderr, "adding\tnid: %d\tuuid: %s\n",
279                lcfg->lcfg_nid, uuid);
280 #endif
281         rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
282         lustre_cfg_free(lcfg);
283         if (rc) {
284                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
285                         strerror(errno));
286                 return -1;
287         }
288
289         printf ("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
290         return 0;
291 }
292
293 int jt_lcfg_add_uuid(int argc, char **argv)
294 {
295         lnet_nid_t nid;
296
297         if (argc != 3) {
298                 return CMD_HELP;
299         }
300
301         nid = libcfs_str2nid(argv[2]);
302         if (nid == LNET_NID_ANY) {
303                 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
304                 return (-1);
305         }
306
307         return do_add_uuid(argv[0], argv[1], nid);
308 }
309
310 int obd_add_uuid(char *uuid, lnet_nid_t nid)
311 {
312         return do_add_uuid("obd_add_uuid", uuid, nid);
313 }
314
315 int jt_lcfg_del_uuid(int argc, char **argv)
316 {
317         int rc;
318         struct lustre_cfg_bufs bufs;
319         struct lustre_cfg *lcfg;
320
321         if (argc != 2) {
322                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
323                 return 0;
324         }
325
326         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
327         if (strcmp (argv[1], "_all_"))
328                 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
329
330         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
331         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
332         lustre_cfg_free(lcfg);
333         if (rc) {
334                 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
335                         strerror(errno));
336                 return -1;
337         }
338         return 0;
339 }
340
341 int jt_lcfg_del_mount_option(int argc, char **argv)
342 {
343         int rc;
344         struct lustre_cfg_bufs bufs;
345         struct lustre_cfg *lcfg;
346
347         if (argc != 2)
348                 return CMD_HELP;
349
350         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
351
352         /* profile name */
353         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
354
355         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
356         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
357         lustre_cfg_free(lcfg);
358         if (rc < 0) {
359                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
360                         strerror(rc = errno));
361         }
362         return rc;
363 }
364
365 int jt_lcfg_set_timeout(int argc, char **argv)
366 {
367         fprintf(stderr, "%s has been deprecated. Use conf_param instead.\n"
368                 "e.g. conf_param lustre-MDT0000 obd_timeout=50\n",
369                 jt_cmdname(argv[0]));
370         return CMD_HELP;
371 }
372
373 int jt_lcfg_add_conn(int argc, char **argv)
374 {
375         struct lustre_cfg_bufs bufs;
376         struct lustre_cfg *lcfg;
377         int priority;
378         int rc;
379
380         if (argc == 2)
381                 priority = 0;
382         else if (argc == 3)
383                 priority = 1;
384         else
385                 return CMD_HELP;
386
387         if (lcfg_devname == NULL) {
388                 fprintf(stderr, "%s: please use 'device name' to set the "
389                         "device name for config commands.\n",
390                         jt_cmdname(argv[0]));
391                 return -EINVAL;
392         }
393
394         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
395
396         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
397
398         lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
399         lcfg->lcfg_num = priority;
400
401         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
402         lustre_cfg_free (lcfg);
403         if (rc < 0) {
404                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
405                         strerror(rc = errno));
406         }
407
408         return rc;
409 }
410
411 int jt_lcfg_del_conn(int argc, char **argv)
412 {
413         struct lustre_cfg_bufs bufs;
414         struct lustre_cfg *lcfg;
415         int rc;
416
417         if (argc != 2)
418                 return CMD_HELP;
419
420         if (lcfg_devname == NULL) {
421                 fprintf(stderr, "%s: please use 'device name' to set the "
422                         "device name for config commands.\n",
423                         jt_cmdname(argv[0]));
424                 return -EINVAL;
425         }
426
427         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
428
429         /* connection uuid */
430         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
431
432         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
433
434         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
435         lustre_cfg_free(lcfg);
436         if (rc < 0) {
437                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
438                         strerror(rc = errno));
439         }
440
441         return rc;
442 }
443
444 /* Param set locally, directly on target */
445 int jt_lcfg_param(int argc, char **argv)
446 {
447         int i, rc;
448         struct lustre_cfg_bufs bufs;
449         struct lustre_cfg *lcfg;
450
451         if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
452                 return CMD_HELP;
453
454         lustre_cfg_bufs_reset(&bufs, NULL);
455
456         for (i = 1; i < argc; i++) {
457                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
458         }
459
460         lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
461
462         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
463         lustre_cfg_free(lcfg);
464         if (rc < 0) {
465                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
466                         strerror(rc = errno));
467         }
468         return rc;
469 }
470
471 /* Param set in config log on MGS */
472 /* conf_param key1=value1 [key2=value2...] */
473 int jt_lcfg_mgsparam(int argc, char **argv)
474 {
475         int i, rc;
476         struct lustre_cfg_bufs bufs;
477         struct lustre_cfg *lcfg;
478
479         if ((argc >= LUSTRE_CFG_MAX_BUFCOUNT) || (argc <= 1))
480                 return CMD_HELP;
481
482         lustre_cfg_bufs_reset(&bufs, NULL);
483
484         for (i = 1; i < argc; i++) {
485                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
486         }
487
488         /* We could put other opcodes here. */
489         lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
490
491         rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
492         lustre_cfg_free(lcfg);
493         if (rc < 0) {
494                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
495                         strerror(rc = errno));
496         }
497
498         return rc;
499 }
500
501 /* Display the path in the same format as sysctl
502  * For eg. obdfilter.lustre-OST0000.stats */
503 static char *display_name(char *filename, int show_type)
504 {
505         char *tmp;
506         struct stat st;
507
508         if (show_type) {
509                 if (stat(filename, &st) < 0)
510                         return NULL;
511         }
512
513         filename += strlen("/proc/");
514         if (strncmp(filename, "fs/", strlen("fs/")) == 0)
515                 filename += strlen("fs/");
516         else
517                 filename += strlen("sys/");
518
519         if (strncmp(filename, "lustre/", strlen("lustre/")) == 0)
520                 filename += strlen("lustre/");
521
522         /* replace '/' with '.' to match conf_param and sysctl */
523         tmp = filename;
524         while ((tmp = strchr(tmp, '/')) != NULL)
525                 *tmp = '.';
526
527         /* append the indicator to entries*/
528         if (show_type) {
529                 if (S_ISDIR(st.st_mode))
530                         filename[strlen(filename)] = '/';
531                 else if (S_ISLNK(st.st_mode))
532                         filename[strlen(filename)] = '@';
533                 else if (st.st_mode & S_IWUSR)
534                         filename[strlen(filename)] = '=';
535         }
536
537         return filename;
538 }
539
540 /* Find a character in a length limited string */
541 static char *strnchr(const char *p, char c, size_t n)
542 {
543        if (!p)
544                return (0);
545
546        while (n-- > 0) {
547                if (*p == c)
548                        return ((char *)p);
549                p++;
550        }
551        return (0);
552 }
553
554 static char *globerrstr(int glob_rc)
555 {
556         switch(glob_rc) {
557         case GLOB_NOSPACE:
558                 return "Out of memory";
559         case GLOB_ABORTED:
560                 return "Read error";
561         case GLOB_NOMATCH:
562                 return "Found no match";
563         }
564         return "Unknown error";
565 }
566
567 static void clean_path(char *path)
568 {
569         char *tmp;
570
571         /* If the input is in form Eg. obdfilter.*.stats */
572         if (strchr(path, '.')) {
573                 tmp = path;
574                 while (*tmp != '\0') {
575                         if ((*tmp == '.') &&
576                             (tmp != path) && (*(tmp - 1) != '\\'))
577                                 *tmp = '/';
578                         tmp ++;
579                 }
580         }
581         /* get rid of '\', glob doesn't like it */
582         if ((tmp = strrchr(path, '\\')) != NULL) {
583                 char *tail = path + strlen(path);
584                 while (tmp != path) {
585                         if (*tmp == '\\') {
586                                 memmove(tmp, tmp + 1, tail - tmp);
587                                 --tail;
588                         }
589                         --tmp;
590                 }
591         }
592 }
593
594 struct param_opts {
595         int only_path;
596         int show_path;
597         int show_type;
598 };
599
600 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
601 {
602         int ch;
603
604         popt->show_path = 1;
605         popt->only_path = 0;
606         popt->show_type = 0;
607
608         while ((ch = getopt(argc, argv, "nNF")) != -1) {
609                 switch (ch) {
610                 case 'N':
611                         popt->only_path = 1;
612                         break;
613                 case 'n':
614                         popt->show_path = 0;
615                 case 'F':
616                         popt->show_type = 1;
617                         break;
618                 default:
619                         return -1;
620                 }
621         }
622         return optind;
623 }
624
625 static int getparam_display(struct param_opts *popt, char *pattern)
626 {
627         int rc;
628         int fd;
629         int i;
630         char *buf;
631         glob_t glob_info;
632         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
633
634         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
635         if (rc) {
636                 fprintf(stderr, "error: get_param: %s: %s\n",
637                         pattern, globerrstr(rc));
638                 return -ESRCH;
639         }
640
641         buf = malloc(CFS_PAGE_SIZE);
642         for (i = 0; i  < glob_info.gl_pathc; i++) {
643                 char *valuename = NULL;
644
645                 memset(buf, 0, CFS_PAGE_SIZE);
646                 memset(filename, 0, PATH_MAX + 1);
647                 if (popt->show_path) {
648                         strcpy(filename, glob_info.gl_pathv[i]);
649                         if (popt->only_path) {
650                                 valuename = display_name(filename,
651                                                          popt->show_type);
652                                 if (valuename)
653                                         printf("%s\n", valuename);
654                                 continue;
655                         }
656                         valuename = display_name(filename, 0);
657                 }
658
659                 /* Write the contents of file to stdout */
660                 fd = open(glob_info.gl_pathv[i], O_RDONLY);
661                 if (fd < 0) {
662                         fprintf(stderr,
663                                 "error: get_param: opening('%s') failed: %s\n",
664                                 glob_info.gl_pathv[i], strerror(errno));
665                         continue;
666                 }
667
668                 do {
669                         rc = read(fd, buf, CFS_PAGE_SIZE);
670                         if (rc == 0)
671                                 break;
672                         if (rc < 0) {
673                                 fprintf(stderr, "error: get_param: "
674                                         "read('%s') failed: %s\n",
675                                         glob_info.gl_pathv[i], strerror(errno));
676                                 break;
677                         }
678                         /* Print the output in the format path=value if the
679                          * value contains no new line character or cab be
680                          * occupied in a line, else print value on new line */
681                         if (valuename && popt->show_path) {
682                                 int longbuf = strnchr(buf, rc - 1, '\n') != NULL
683                                               || rc > 60;
684                                 printf("%s=%s", valuename, longbuf ? "\n" : buf);
685                                 valuename = NULL;
686                                 if (!longbuf)
687                                         continue;
688                                 fflush(stdout);
689                         }
690                         rc = write(fileno(stdout), buf, rc);
691                         if (rc < 0) {
692                                 fprintf(stderr, "error: get_param: "
693                                         "write to stdout failed: %s\n",
694                                         strerror(errno));
695                                 break;
696                         }
697                 } while (1);
698                 close(fd);
699         }
700
701         globfree(&glob_info);
702         free(buf);
703         return rc;
704 }
705
706 int jt_lcfg_getparam(int argc, char **argv)
707 {
708         int fp;
709         int rc = 0, i;
710         struct param_opts popt;
711         char pattern[PATH_MAX];
712         char *path;
713
714         rc = getparam_cmdline(argc, argv, &popt);
715         if (rc < 0 || rc >= argc)
716                 return CMD_HELP;
717
718         for (i = rc; i < argc; i++) {
719                 path = argv[i];
720
721                 clean_path(path);
722
723                 /* If the entire path is specified as input */
724                 fp = open(path, O_RDONLY);
725                 if (fp < 0) {
726                         snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
727                                  path);
728                 } else {
729                         strcpy(pattern, path);
730                         close(fp);
731                 }
732
733                 rc = getparam_display(&popt, pattern);
734                 if (rc < 0)
735                         return rc;
736         }
737
738         return 0;
739 }
740
741 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
742 {
743         int ch;
744
745         popt->show_path = 1;
746         popt->only_path = 0;
747         popt->show_type = 0;
748
749         while ((ch = getopt(argc, argv, "n")) != -1) {
750                 switch (ch) {
751                 case 'n':
752                         popt->show_path = 0;
753                         break;
754                 default:
755                         return -1;
756                 }
757         }
758         return optind;
759 }
760
761 static int setparam_display(struct param_opts *popt, char *pattern, char *value)
762 {
763         int rc;
764         int fd;
765         int i;
766         glob_t glob_info;
767         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
768
769         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
770         if (rc) {
771                 fprintf(stderr, "error: set_param: %s: %s\n",
772                         pattern, globerrstr(rc));
773                 return -ESRCH;
774         }
775         for (i = 0; i  < glob_info.gl_pathc; i++) {
776                 char *valuename = NULL;
777
778                 memset(filename, 0, PATH_MAX + 1);
779                 if (popt->show_path) {
780                         strcpy(filename, glob_info.gl_pathv[i]);
781                         valuename = display_name(filename, 0);
782                         if (valuename)
783                                 printf("%s=%s\n", valuename, value);
784                 }
785                 /* Write the new value to the file */
786                 fd = open(glob_info.gl_pathv[i], O_WRONLY);
787                 if (fd > 0) {
788                         rc = write(fd, value, strlen(value));
789                         if (rc < 0)
790                                 fprintf(stderr, "error: set_param: "
791                                         "writing to file %s: %s\n",
792                                         glob_info.gl_pathv[i], strerror(errno));
793                         else
794                                 rc = 0;
795                         close(fd);
796                 } else {
797                         fprintf(stderr, "error: set_param: %s opening %s\n",
798                                 strerror(rc = errno), glob_info.gl_pathv[i]);
799                 }
800         }
801
802         globfree(&glob_info);
803         return rc;
804 }
805
806 int jt_lcfg_setparam(int argc, char **argv)
807 {
808         int fp;
809         int rc = 0, i;
810         struct param_opts popt;
811         char pattern[PATH_MAX];
812         char *path = NULL, *value = NULL;
813
814         rc = setparam_cmdline(argc, argv, &popt);
815         if (rc < 0 || rc >= argc)
816                 return CMD_HELP;
817
818         for (i = rc; i < argc; i++) {
819                 if ((value = strchr(argv[i], '=')) != NULL) {
820                         /* format: set_param a=b */
821                         *value = '\0';
822                         value ++;
823                         path = argv[i];
824                 } else {
825                         /* format: set_param a b */
826                         if (path == NULL) {
827                                 path = argv[i];
828                                 continue;
829                         } else {
830                                 value = argv[i];
831                         }
832                 }
833
834                 clean_path(path);
835
836                 /* If the entire path is specified as input */
837                 fp = open(path, O_RDONLY);
838                 if (fp < 0) {
839                         snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
840                                  path);
841                 } else {
842                         strcpy(pattern, path);
843                         close(fp);
844                 }
845
846                 rc = setparam_display(&popt, pattern, value);
847                 path = NULL;
848                 value = NULL;
849                 if (rc < 0)
850                         return rc;
851         }
852
853         return 0;
854 }