Whamcloud - gitweb
LU-737 utils: check device name for digit
[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 (c) 2003, 2010, Oracle and/or its affiliates. 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 <libcfs/libcfsutil.h>
74 #include <stdio.h>
75
76 static char * lcfg_devname;
77
78 int lcfg_set_devname(char *name)
79 {
80         char *ptr;
81         int digit = 1;
82
83         if (name) {
84                 if (lcfg_devname)
85                         free(lcfg_devname);
86                 /* quietly strip the unnecessary '$' */
87                 if (*name == '$' || *name == '%')
88                         name++;
89
90                 ptr = name;
91                 while (*ptr != '\0') {
92                         if (!isdigit(*ptr)) {
93                             digit = 0;
94                             break;
95                         }
96                         ptr++;
97                 }
98
99                 if (digit) {
100                         /* We can't translate from dev # to name */
101                         lcfg_devname = NULL;
102                 } else {
103                         lcfg_devname = strdup(name);
104                 }
105         } else {
106                 lcfg_devname = NULL;
107         }
108         return 0;
109 }
110
111 char * lcfg_get_devname(void)
112 {
113         return lcfg_devname;
114 }
115
116 int jt_lcfg_device(int argc, char **argv)
117 {
118         return jt_obd_device(argc, argv);
119 }
120
121 int jt_lcfg_attach(int argc, char **argv)
122 {
123         struct lustre_cfg_bufs bufs;
124         struct lustre_cfg *lcfg;
125         int rc;
126
127         if (argc != 4)
128                 return CMD_HELP;
129
130         lustre_cfg_bufs_reset(&bufs, NULL);
131
132         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
133         lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
134         lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
135
136         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
137         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
138         lustre_cfg_free(lcfg);
139         if (rc < 0) {
140                 fprintf(stderr, "error: %s: LCFG_ATTACH %s\n",
141                         jt_cmdname(argv[0]), strerror(rc = errno));
142         } else if (argc == 3) {
143                 char name[1024];
144
145                 lcfg_set_devname(argv[2]);
146                 if (strlen(argv[2]) > 128) {
147                         printf("Name too long to set environment\n");
148                         return -EINVAL;
149                 }
150                 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
151                 rc = setenv(name, argv[1], 1);
152                 if (rc) {
153                         printf("error setting env variable %s\n", name);
154                 }
155         } else {
156                 lcfg_set_devname(argv[2]);
157         }
158
159         return rc;
160 }
161
162 int jt_lcfg_setup(int argc, char **argv)
163 {
164         struct lustre_cfg_bufs bufs;
165         struct lustre_cfg *lcfg;
166         int i;
167         int rc;
168
169         if (lcfg_devname == NULL) {
170                 fprintf(stderr, "%s: please use 'device name' to set the "
171                         "device name for config commands.\n",
172                         jt_cmdname(argv[0]));
173                 return -EINVAL;
174         }
175
176         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
177
178         if (argc > 6)
179                 return CMD_HELP;
180
181         for (i = 1; i < argc; i++) {
182                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
183         }
184
185         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
186         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
187         lustre_cfg_free(lcfg);
188         if (rc < 0)
189                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
190                         strerror(rc = errno));
191
192         return rc;
193 }
194
195 int jt_obd_detach(int argc, char **argv)
196 {
197         struct lustre_cfg_bufs bufs;
198         struct lustre_cfg *lcfg;
199         int rc;
200
201         if (lcfg_devname == NULL) {
202                 fprintf(stderr, "%s: please use 'device name' to set the "
203                         "device name for config commands.\n",
204                         jt_cmdname(argv[0]));
205                 return -EINVAL;
206         }
207
208         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
209
210         if (argc != 1)
211                 return CMD_HELP;
212
213         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
214         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
215         lustre_cfg_free(lcfg);
216         if (rc < 0)
217                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
218                         strerror(rc = errno));
219
220         return rc;
221 }
222
223 int jt_obd_cleanup(int argc, char **argv)
224 {
225         struct lustre_cfg_bufs bufs;
226         struct lustre_cfg *lcfg;
227         char force = 'F';
228         char failover = 'A';
229         char flags[3] = { 0 };
230         int flag_cnt = 0, n;
231         int rc;
232
233         if (lcfg_devname == NULL) {
234                 fprintf(stderr, "%s: please use 'device name' to set the "
235                         "device name for config commands.\n",
236                         jt_cmdname(argv[0]));
237                 return -EINVAL;
238         }
239
240         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
241
242         if (argc < 1 || argc > 3)
243                 return CMD_HELP;
244
245         /* we are protected from overflowing our buffer by the argc
246          * check above
247          */
248         for (n = 1; n < argc; n++) {
249                 if (strcmp(argv[n], "force") == 0) {
250                         flags[flag_cnt++] = force;
251                 } else if (strcmp(argv[n], "failover") == 0) {
252                         flags[flag_cnt++] = failover;
253                 } else {
254                         fprintf(stderr, "unknown option: %s", argv[n]);
255                         return CMD_HELP;
256                 }
257         }
258
259         if (flag_cnt) {
260                 lustre_cfg_bufs_set_string(&bufs, 1, flags);
261         }
262
263         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
264         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
265         lustre_cfg_free(lcfg);
266         if (rc < 0)
267                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
268                         strerror(rc = errno));
269
270         return rc;
271 }
272
273 static
274 int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
275 {
276         int rc;
277         struct lustre_cfg_bufs bufs;
278         struct lustre_cfg *lcfg;
279
280         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
281         if (uuid)
282                 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
283
284         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
285         lcfg->lcfg_nid = nid;
286         /* Poison NAL -- pre 1.4.6 will LASSERT on 0 NAL, this way it
287            doesn't work without crashing (bz 10130) */
288         lcfg->lcfg_nal = 0x5a;
289
290 #if 0
291         fprintf(stderr, "adding\tnid: %d\tuuid: %s\n",
292                lcfg->lcfg_nid, uuid);
293 #endif
294         rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
295         lustre_cfg_free(lcfg);
296         if (rc) {
297                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
298                         strerror(errno));
299                 return -1;
300         }
301
302         printf ("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
303         return 0;
304 }
305
306 int jt_lcfg_add_uuid(int argc, char **argv)
307 {
308         lnet_nid_t nid;
309
310         if (argc != 3) {
311                 return CMD_HELP;
312         }
313
314         nid = libcfs_str2nid(argv[2]);
315         if (nid == LNET_NID_ANY) {
316                 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
317                 return (-1);
318         }
319
320         return do_add_uuid(argv[0], argv[1], nid);
321 }
322
323 int obd_add_uuid(char *uuid, lnet_nid_t nid)
324 {
325         return do_add_uuid("obd_add_uuid", uuid, nid);
326 }
327
328 int jt_lcfg_del_uuid(int argc, char **argv)
329 {
330         int rc;
331         struct lustre_cfg_bufs bufs;
332         struct lustre_cfg *lcfg;
333
334         if (argc != 2) {
335                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
336                 return 0;
337         }
338
339         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
340         if (strcmp (argv[1], "_all_"))
341                 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
342
343         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
344         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
345         lustre_cfg_free(lcfg);
346         if (rc) {
347                 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
348                         strerror(errno));
349                 return -1;
350         }
351         return 0;
352 }
353
354 int jt_lcfg_del_mount_option(int argc, char **argv)
355 {
356         int rc;
357         struct lustre_cfg_bufs bufs;
358         struct lustre_cfg *lcfg;
359
360         if (argc != 2)
361                 return CMD_HELP;
362
363         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
364
365         /* profile name */
366         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
367
368         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
369         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
370         lustre_cfg_free(lcfg);
371         if (rc < 0) {
372                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
373                         strerror(rc = errno));
374         }
375         return rc;
376 }
377
378 int jt_lcfg_set_timeout(int argc, char **argv)
379 {
380         int rc;
381         struct lustre_cfg_bufs bufs;
382         struct lustre_cfg *lcfg;
383
384         fprintf(stderr, "%s has been deprecated. Use conf_param instead.\n"
385                 "e.g. conf_param lustre-MDT0000 obd_timeout=50\n",
386                 jt_cmdname(argv[0]));
387         return CMD_HELP;
388
389
390         if (argc != 2)
391                 return CMD_HELP;
392
393         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
394         lcfg = lustre_cfg_new(LCFG_SET_TIMEOUT, &bufs);
395         lcfg->lcfg_num = atoi(argv[1]);
396
397         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
398         //rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
399
400         lustre_cfg_free(lcfg);
401         if (rc < 0) {
402                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
403                         strerror(rc = errno));
404         }
405         return rc;
406 }
407
408 int jt_lcfg_add_conn(int argc, char **argv)
409 {
410         struct lustre_cfg_bufs bufs;
411         struct lustre_cfg *lcfg;
412         int priority;
413         int rc;
414
415         if (argc == 2)
416                 priority = 0;
417         else if (argc == 3)
418                 priority = 1;
419         else
420                 return CMD_HELP;
421
422         if (lcfg_devname == NULL) {
423                 fprintf(stderr, "%s: please use 'device name' to set the "
424                         "device name for config commands.\n",
425                         jt_cmdname(argv[0]));
426                 return -EINVAL;
427         }
428
429         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
430
431         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
432
433         lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
434         lcfg->lcfg_num = priority;
435
436         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
437         lustre_cfg_free (lcfg);
438         if (rc < 0) {
439                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
440                         strerror(rc = errno));
441         }
442
443         return rc;
444 }
445
446 int jt_lcfg_del_conn(int argc, char **argv)
447 {
448         struct lustre_cfg_bufs bufs;
449         struct lustre_cfg *lcfg;
450         int rc;
451
452         if (argc != 2)
453                 return CMD_HELP;
454
455         if (lcfg_devname == NULL) {
456                 fprintf(stderr, "%s: please use 'device name' to set the "
457                         "device name for config commands.\n",
458                         jt_cmdname(argv[0]));
459                 return -EINVAL;
460         }
461
462         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
463
464         /* connection uuid */
465         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
466
467         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
468
469         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
470         lustre_cfg_free(lcfg);
471         if (rc < 0) {
472                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
473                         strerror(rc = errno));
474         }
475
476         return rc;
477 }
478
479 /* Param set locally, directly on target */
480 int jt_lcfg_param(int argc, char **argv)
481 {
482         int i, rc;
483         struct lustre_cfg_bufs bufs;
484         struct lustre_cfg *lcfg;
485
486         if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
487                 return CMD_HELP;
488
489         lustre_cfg_bufs_reset(&bufs, NULL);
490
491         for (i = 1; i < argc; i++) {
492                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
493         }
494
495         lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
496
497         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
498         lustre_cfg_free(lcfg);
499         if (rc < 0) {
500                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
501                         strerror(rc = errno));
502         }
503         return rc;
504 }
505
506 /* Param set in config log on MGS */
507 /* conf_param key=value */
508 /* Note we can actually send mgc conf_params from clients, but currently
509  * that's only done for default file striping (see ll_send_mgc_param),
510  * and not here. */
511 /* After removal of a parameter (-d) Lustre will use the default
512  * AT NEXT REBOOT, not immediately. */
513 int jt_lcfg_mgsparam(int argc, char **argv)
514 {
515         int rc;
516         int del = 0;
517         struct lustre_cfg_bufs bufs;
518         struct lustre_cfg *lcfg;
519         char *buf = NULL;
520
521         /* mgs_setparam processes only lctl buf #1 */
522         if ((argc > 3) || (argc <= 1))
523                 return CMD_HELP;
524
525         while ((rc = getopt(argc, argv, "d")) != -1) {
526                 switch (rc) {
527                         case 'd':
528                                 del = 1;
529                                 break;
530                         default:
531                                 return CMD_HELP;
532                 }
533         }
534
535         lustre_cfg_bufs_reset(&bufs, NULL);
536         if (del) {
537                 char *ptr;
538
539                 /* for delete, make it "<param>=\0" */
540                 buf = malloc(strlen(argv[optind]) + 2);
541                 /* put an '=' on the end in case it doesn't have one */
542                 sprintf(buf, "%s=", argv[optind]);
543                 /* then truncate after the first '=' */
544                 ptr = strchr(buf, '=');
545                 *(++ptr) = '\0';
546                 lustre_cfg_bufs_set_string(&bufs, 1, buf);
547         } else {
548                 lustre_cfg_bufs_set_string(&bufs, 1, argv[optind]);
549         }
550
551         /* We could put other opcodes here. */
552         lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
553
554         rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
555         lustre_cfg_free(lcfg);
556         if (buf)
557                 free(buf);
558         if (rc < 0) {
559                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
560                         strerror(rc = errno));
561         }
562
563         return rc;
564 }
565
566 /* Display the path in the same format as sysctl
567  * For eg. obdfilter.lustre-OST0000.stats */
568 static char *display_name(char *filename, int show_type)
569 {
570         char *tmp;
571         struct stat st;
572
573         if (show_type) {
574                 if (lstat(filename, &st) < 0)
575                         return NULL;
576         }
577
578         filename += strlen("/proc/");
579         if (strncmp(filename, "fs/", strlen("fs/")) == 0)
580                 filename += strlen("fs/");
581         else
582                 filename += strlen("sys/");
583
584         if (strncmp(filename, "lustre/", strlen("lustre/")) == 0)
585                 filename += strlen("lustre/");
586         else if (strncmp(filename, "lnet/", strlen("lnet/")) == 0)
587                 filename += strlen("lnet/");
588
589         /* replace '/' with '.' to match conf_param and sysctl */
590         tmp = filename;
591         while ((tmp = strchr(tmp, '/')) != NULL)
592                 *tmp = '.';
593
594         /* append the indicator to entries */
595         if (show_type) {
596                 if (S_ISDIR(st.st_mode))
597                         strcat(filename, "/");
598                 else if (S_ISLNK(st.st_mode))
599                         strcat(filename, "@");
600                 else if (st.st_mode & S_IWUSR)
601                         strcat(filename, "=");
602         }
603
604         return filename;
605 }
606
607 /* Find a character in a length limited string */
608 /* BEWARE - kernel definition of strnchr has args in different order! */
609 static char *strnchr(const char *p, char c, size_t n)
610 {
611        if (!p)
612                return (0);
613
614        while (n-- > 0) {
615                if (*p == c)
616                        return ((char *)p);
617                p++;
618        }
619        return (0);
620 }
621
622 static char *globerrstr(int glob_rc)
623 {
624         switch(glob_rc) {
625         case GLOB_NOSPACE:
626                 return "Out of memory";
627         case GLOB_ABORTED:
628                 return "Read error";
629         case GLOB_NOMATCH:
630                 return "Found no match";
631         }
632         return "Unknown error";
633 }
634
635 static void clean_path(char *path)
636 {
637         char *tmp;
638
639         /* If the input is in form Eg. obdfilter.*.stats */
640         if (strchr(path, '.')) {
641                 tmp = path;
642                 while (*tmp != '\0') {
643                         if ((*tmp == '.') &&
644                             (tmp != path) && (*(tmp - 1) != '\\'))
645                                 *tmp = '/';
646                         tmp ++;
647                 }
648         }
649         /* get rid of '\', glob doesn't like it */
650         if ((tmp = strrchr(path, '\\')) != NULL) {
651                 char *tail = path + strlen(path);
652                 while (tmp != path) {
653                         if (*tmp == '\\') {
654                                 memmove(tmp, tmp + 1, tail - tmp);
655                                 --tail;
656                         }
657                         --tmp;
658                 }
659         }
660 }
661
662 struct param_opts {
663         int only_path:1;
664         int show_path:1;
665         int show_type:1;
666         int recursive:1;
667 };
668
669 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
670 {
671         int ch;
672
673         popt->show_path = 1;
674         popt->only_path = 1;
675         popt->show_type = 0;
676         popt->recursive = 0;
677
678         while ((ch = getopt(argc, argv, "FR")) != -1) {
679                 switch (ch) {
680                 case 'F':
681                         popt->show_type = 1;
682                         break;
683                 case 'R':
684                         popt->recursive = 1;
685                         break;
686                 default:
687                         return -1;
688                 }
689         }
690
691         return optind;
692 }
693
694 static int listparam_display(struct param_opts *popt, char *pattern)
695 {
696         int rc;
697         int i;
698         glob_t glob_info;
699         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
700
701         rc = glob(pattern, GLOB_BRACE | (popt->recursive ? GLOB_MARK : 0),
702                   NULL, &glob_info);
703         if (rc) {
704                 fprintf(stderr, "error: list_param: %s: %s\n",
705                         pattern, globerrstr(rc));
706                 return -ESRCH;
707         }
708
709         for (i = 0; i  < glob_info.gl_pathc; i++) {
710                 char *valuename = NULL;
711                 int last;
712
713                 /* Trailing '/' will indicate recursion into directory */
714                 last = strlen(glob_info.gl_pathv[i]) - 1;
715
716                 /* Remove trailing '/' or it will be converted to '.' */
717                 if (last > 0 && glob_info.gl_pathv[i][last] == '/')
718                         glob_info.gl_pathv[i][last] = '\0';
719                 else
720                         last = 0;
721                 strcpy(filename, glob_info.gl_pathv[i]);
722                 valuename = display_name(filename, popt->show_type);
723                 if (valuename)
724                         printf("%s\n", valuename);
725                 if (last) {
726                         strcpy(filename, glob_info.gl_pathv[i]);
727                         strcat(filename, "/*");
728                         listparam_display(popt, filename);
729                 }
730         }
731
732         globfree(&glob_info);
733         return rc;
734 }
735
736 int jt_lcfg_listparam(int argc, char **argv)
737 {
738         int fp;
739         int rc = 0, i;
740         struct param_opts popt;
741         char pattern[PATH_MAX];
742         char *path;
743
744         rc = listparam_cmdline(argc, argv, &popt);
745         if (rc == argc && popt.recursive) {
746                 rc--;           /* we know at least "-R" is a parameter */
747                 argv[rc] = "*";
748         } else if (rc < 0 || rc >= argc) {
749                 return CMD_HELP;
750         }
751
752         for (i = rc; i < argc; i++) {
753                 path = argv[i];
754
755                 clean_path(path);
756
757                 /* If the entire path is specified as input */
758                 fp = open(path, O_RDONLY);
759                 if (fp < 0) {
760                         snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
761                                  path);
762                 } else {
763                         strcpy(pattern, path);
764                         close(fp);
765                 }
766
767                 rc = listparam_display(&popt, pattern);
768                 if (rc < 0)
769                         return rc;
770         }
771
772         return 0;
773 }
774
775 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
776 {
777         int ch;
778
779         popt->show_path = 1;
780         popt->only_path = 0;
781         popt->show_type = 0;
782         popt->recursive = 0;
783
784         while ((ch = getopt(argc, argv, "nNF")) != -1) {
785                 switch (ch) {
786                 case 'N':
787                         popt->only_path = 1;
788                         break;
789                 case 'n':
790                         popt->show_path = 0;
791                 case 'F':
792                         popt->show_type = 1;
793                         break;
794                 default:
795                         return -1;
796                 }
797         }
798
799         return optind;
800 }
801
802 static int getparam_display(struct param_opts *popt, char *pattern)
803 {
804         int rc;
805         int fd;
806         int i;
807         char *buf;
808         glob_t glob_info;
809         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
810
811         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
812         if (rc) {
813                 fprintf(stderr, "error: get_param: %s: %s\n",
814                         pattern, globerrstr(rc));
815                 return -ESRCH;
816         }
817
818         buf = malloc(CFS_PAGE_SIZE);
819         for (i = 0; i  < glob_info.gl_pathc; i++) {
820                 char *valuename = NULL;
821
822                 memset(buf, 0, CFS_PAGE_SIZE);
823                 /* As listparam_display is used to show param name (with type),
824                  * here "if (only_path)" is ignored.*/
825                 if (popt->show_path) {
826                         strcpy(filename, glob_info.gl_pathv[i]);
827                         valuename = display_name(filename, 0);
828                 }
829
830                 /* Write the contents of file to stdout */
831                 fd = open(glob_info.gl_pathv[i], O_RDONLY);
832                 if (fd < 0) {
833                         fprintf(stderr,
834                                 "error: get_param: opening('%s') failed: %s\n",
835                                 glob_info.gl_pathv[i], strerror(errno));
836                         continue;
837                 }
838
839                 do {
840                         rc = read(fd, buf, CFS_PAGE_SIZE);
841                         if (rc == 0)
842                                 break;
843                         if (rc < 0) {
844                                 fprintf(stderr, "error: get_param: "
845                                         "read('%s') failed: %s\n",
846                                         glob_info.gl_pathv[i], strerror(errno));
847                                 break;
848                         }
849                         /* Print the output in the format path=value if the
850                          * value contains no new line character or cab be
851                          * occupied in a line, else print value on new line */
852                         if (valuename && popt->show_path) {
853                                 int longbuf = strnchr(buf, rc - 1, '\n') != NULL
854                                               || rc > 60;
855                                 printf("%s=%s", valuename, longbuf ? "\n" : buf);
856                                 valuename = NULL;
857                                 if (!longbuf)
858                                         continue;
859                                 fflush(stdout);
860                         }
861                         rc = write(fileno(stdout), buf, rc);
862                         if (rc < 0) {
863                                 fprintf(stderr, "error: get_param: "
864                                         "write to stdout failed: %s\n",
865                                         strerror(errno));
866                                 break;
867                         }
868                 } while (1);
869                 close(fd);
870         }
871
872         globfree(&glob_info);
873         free(buf);
874         return rc;
875 }
876
877 int jt_lcfg_getparam(int argc, char **argv)
878 {
879         int fp;
880         int rc = 0, i;
881         struct param_opts popt;
882         char pattern[PATH_MAX];
883         char *path;
884
885         rc = getparam_cmdline(argc, argv, &popt);
886         if (rc < 0 || rc >= argc)
887                 return CMD_HELP;
888
889         for (i = rc; i < argc; i++) {
890                 path = argv[i];
891
892                 clean_path(path);
893
894                 /* If the entire path is specified as input */
895                 fp = open(path, O_RDONLY);
896                 if (fp < 0) {
897                         snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
898                                  path);
899                 } else {
900                         strcpy(pattern, path);
901                         close(fp);
902                 }
903
904                 if (popt.only_path)
905                         rc = listparam_display(&popt, pattern);
906                 else
907                         rc = getparam_display(&popt, pattern);
908                 if (rc < 0)
909                         return rc;
910         }
911
912         return 0;
913 }
914
915 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
916 {
917         int ch;
918
919         popt->show_path = 1;
920         popt->only_path = 0;
921         popt->show_type = 0;
922         popt->recursive = 0;
923
924         while ((ch = getopt(argc, argv, "n")) != -1) {
925                 switch (ch) {
926                 case 'n':
927                         popt->show_path = 0;
928                         break;
929                 default:
930                         return -1;
931                 }
932         }
933         return optind;
934 }
935
936 static int setparam_display(struct param_opts *popt, char *pattern, char *value)
937 {
938         int rc;
939         int fd;
940         int i;
941         glob_t glob_info;
942         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
943
944         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
945         if (rc) {
946                 fprintf(stderr, "error: set_param: %s: %s\n",
947                         pattern, globerrstr(rc));
948                 return -ESRCH;
949         }
950         for (i = 0; i  < glob_info.gl_pathc; i++) {
951                 char *valuename = NULL;
952
953                 if (popt->show_path) {
954                         strcpy(filename, glob_info.gl_pathv[i]);
955                         valuename = display_name(filename, 0);
956                         if (valuename)
957                                 printf("%s=%s\n", valuename, value);
958                 }
959                 /* Write the new value to the file */
960                 fd = open(glob_info.gl_pathv[i], O_WRONLY);
961                 if (fd > 0) {
962                         rc = write(fd, value, strlen(value));
963                         if (rc < 0)
964                                 fprintf(stderr, "error: set_param: "
965                                         "writing to file %s: %s\n",
966                                         glob_info.gl_pathv[i], strerror(errno));
967                         else
968                                 rc = 0;
969                         close(fd);
970                 } else {
971                         fprintf(stderr, "error: set_param: %s opening %s\n",
972                                 strerror(rc = errno), glob_info.gl_pathv[i]);
973                 }
974         }
975
976         globfree(&glob_info);
977         return rc;
978 }
979
980 int jt_lcfg_setparam(int argc, char **argv)
981 {
982         int fp;
983         int rc = 0, i;
984         struct param_opts popt;
985         char pattern[PATH_MAX];
986         char *path = NULL, *value = NULL;
987
988         rc = setparam_cmdline(argc, argv, &popt);
989         if (rc < 0 || rc >= argc)
990                 return CMD_HELP;
991
992         for (i = rc; i < argc; i++) {
993                 if ((value = strchr(argv[i], '=')) != NULL) {
994                         /* format: set_param a=b */
995                         *value = '\0';
996                         value ++;
997                         path = argv[i];
998                 } else {
999                         /* format: set_param a b */
1000                         if (path == NULL) {
1001                                 path = argv[i];
1002                                 continue;
1003                         } else {
1004                                 value = argv[i];
1005                         }
1006                 }
1007
1008                 clean_path(path);
1009
1010                 /* If the entire path is specified as input */
1011                 fp = open(path, O_RDONLY);
1012                 if (fp < 0) {
1013                         snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
1014                                  path);
1015                 } else {
1016                         strcpy(pattern, path);
1017                         close(fp);
1018                 }
1019
1020                 rc = setparam_display(&popt, pattern, value);
1021                 path = NULL;
1022                 value = NULL;
1023                 if (rc < 0)
1024                         return rc;
1025         }
1026
1027         return 0;
1028 }