Whamcloud - gitweb
LU-1866 lfsck: enhance otable-based iteration
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 /* Supporting file paths creates perilous behavoir: LU-888.
663  * Path support is deprecated.
664  * If a path is supplied it must begin with /proc.  */
665 static void lprocfs_param_pattern(const char *cmd, const char *path, char *buf,
666         size_t buf_size)
667 {
668         /* test path to see if it begins with '/proc/' */
669         if (strncmp(path, "/proc/", strlen("/proc/")) == 0) {
670                 static int warned;
671                 if (!warned) {
672                         fprintf(stderr, "%s: specifying parameters via "
673                                 "full paths is deprecated.\n", cmd);
674 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 50, 0)
675 #warning "remove deprecated full path tunable access"
676 #endif
677                         warned = 1;
678                 }
679                 snprintf(buf, buf_size, "%s", path);
680         } else {
681                 snprintf(buf, buf_size, "/proc/{fs,sys}/{lnet,lustre}/%s",
682                         path);
683         }
684 }
685
686 struct param_opts {
687         int only_path:1;
688         int show_path:1;
689         int show_type:1;
690         int recursive:1;
691 };
692
693 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
694 {
695         int ch;
696
697         popt->show_path = 1;
698         popt->only_path = 1;
699         popt->show_type = 0;
700         popt->recursive = 0;
701
702         while ((ch = getopt(argc, argv, "FR")) != -1) {
703                 switch (ch) {
704                 case 'F':
705                         popt->show_type = 1;
706                         break;
707                 case 'R':
708                         popt->recursive = 1;
709                         break;
710                 default:
711                         return -1;
712                 }
713         }
714
715         return optind;
716 }
717
718 static int listparam_display(struct param_opts *popt, char *pattern)
719 {
720         int rc;
721         int i;
722         glob_t glob_info;
723         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
724
725         rc = glob(pattern, GLOB_BRACE | (popt->recursive ? GLOB_MARK : 0),
726                   NULL, &glob_info);
727         if (rc) {
728                 fprintf(stderr, "error: list_param: %s: %s\n",
729                         pattern, globerrstr(rc));
730                 return -ESRCH;
731         }
732
733         for (i = 0; i  < glob_info.gl_pathc; i++) {
734                 char *valuename = NULL;
735                 int last;
736
737                 /* Trailing '/' will indicate recursion into directory */
738                 last = strlen(glob_info.gl_pathv[i]) - 1;
739
740                 /* Remove trailing '/' or it will be converted to '.' */
741                 if (last > 0 && glob_info.gl_pathv[i][last] == '/')
742                         glob_info.gl_pathv[i][last] = '\0';
743                 else
744                         last = 0;
745                 strcpy(filename, glob_info.gl_pathv[i]);
746                 valuename = display_name(filename, popt->show_type);
747                 if (valuename)
748                         printf("%s\n", valuename);
749                 if (last) {
750                         strcpy(filename, glob_info.gl_pathv[i]);
751                         strcat(filename, "/*");
752                         listparam_display(popt, filename);
753                 }
754         }
755
756         globfree(&glob_info);
757         return rc;
758 }
759
760 int jt_lcfg_listparam(int argc, char **argv)
761 {
762         int rc = 0, i;
763         struct param_opts popt;
764         char pattern[PATH_MAX];
765         char *path;
766
767         rc = listparam_cmdline(argc, argv, &popt);
768         if (rc == argc && popt.recursive) {
769                 rc--;           /* we know at least "-R" is a parameter */
770                 argv[rc] = "*";
771         } else if (rc < 0 || rc >= argc) {
772                 return CMD_HELP;
773         }
774
775         for (i = rc; i < argc; i++) {
776                 path = argv[i];
777
778                 clean_path(path);
779
780                 lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
781
782                 rc = listparam_display(&popt, pattern);
783                 if (rc < 0)
784                         return rc;
785         }
786
787         return 0;
788 }
789
790 static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
791 {
792         int ch;
793
794         popt->show_path = 1;
795         popt->only_path = 0;
796         popt->show_type = 0;
797         popt->recursive = 0;
798
799         while ((ch = getopt(argc, argv, "nNF")) != -1) {
800                 switch (ch) {
801                 case 'N':
802                         popt->only_path = 1;
803                         break;
804                 case 'n':
805                         popt->show_path = 0;
806                 case 'F':
807                         popt->show_type = 1;
808                         break;
809                 default:
810                         return -1;
811                 }
812         }
813
814         return optind;
815 }
816
817 static int getparam_display(struct param_opts *popt, char *pattern)
818 {
819         int rc;
820         int fd;
821         int i;
822         char *buf;
823         glob_t glob_info;
824         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
825
826         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
827         if (rc) {
828                 fprintf(stderr, "error: get_param: %s: %s\n",
829                         pattern, globerrstr(rc));
830                 return -ESRCH;
831         }
832
833         buf = malloc(CFS_PAGE_SIZE);
834         for (i = 0; i  < glob_info.gl_pathc; i++) {
835                 char *valuename = NULL;
836
837                 memset(buf, 0, CFS_PAGE_SIZE);
838                 /* As listparam_display is used to show param name (with type),
839                  * here "if (only_path)" is ignored.*/
840                 if (popt->show_path) {
841                         strcpy(filename, glob_info.gl_pathv[i]);
842                         valuename = display_name(filename, 0);
843                 }
844
845                 /* Write the contents of file to stdout */
846                 fd = open(glob_info.gl_pathv[i], O_RDONLY);
847                 if (fd < 0) {
848                         fprintf(stderr,
849                                 "error: get_param: opening('%s') failed: %s\n",
850                                 glob_info.gl_pathv[i], strerror(errno));
851                         continue;
852                 }
853
854                 do {
855                         rc = read(fd, buf, CFS_PAGE_SIZE);
856                         if (rc == 0)
857                                 break;
858                         if (rc < 0) {
859                                 fprintf(stderr, "error: get_param: "
860                                         "read('%s') failed: %s\n",
861                                         glob_info.gl_pathv[i], strerror(errno));
862                                 break;
863                         }
864                         /* Print the output in the format path=value if the
865                          * value contains no new line character or cab be
866                          * occupied in a line, else print value on new line */
867                         if (valuename && popt->show_path) {
868                                 int longbuf = strnchr(buf, rc - 1, '\n') != NULL
869                                               || rc > 60;
870                                 printf("%s=%s", valuename, longbuf ? "\n" : buf);
871                                 valuename = NULL;
872                                 if (!longbuf)
873                                         continue;
874                                 fflush(stdout);
875                         }
876                         rc = write(fileno(stdout), buf, rc);
877                         if (rc < 0) {
878                                 fprintf(stderr, "error: get_param: "
879                                         "write to stdout failed: %s\n",
880                                         strerror(errno));
881                                 break;
882                         }
883                 } while (1);
884                 close(fd);
885         }
886
887         globfree(&glob_info);
888         free(buf);
889         return rc;
890 }
891
892 int jt_lcfg_getparam(int argc, char **argv)
893 {
894         int rc = 0, i;
895         struct param_opts popt;
896         char pattern[PATH_MAX];
897         char *path;
898
899         rc = getparam_cmdline(argc, argv, &popt);
900         if (rc < 0 || rc >= argc)
901                 return CMD_HELP;
902
903         for (i = rc, rc = 0; i < argc; i++) {
904                 int rc2;
905
906                 path = argv[i];
907
908                 clean_path(path);
909
910                 lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
911
912                 if (popt.only_path)
913                         rc2 = listparam_display(&popt, pattern);
914                 else
915                         rc2 = getparam_display(&popt, pattern);
916                 if (rc2 < 0 && rc == 0)
917                         rc = rc2;
918         }
919
920         return rc;
921 }
922
923 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
924 {
925         int ch;
926
927         popt->show_path = 1;
928         popt->only_path = 0;
929         popt->show_type = 0;
930         popt->recursive = 0;
931
932         while ((ch = getopt(argc, argv, "n")) != -1) {
933                 switch (ch) {
934                 case 'n':
935                         popt->show_path = 0;
936                         break;
937                 default:
938                         return -1;
939                 }
940         }
941         return optind;
942 }
943
944 static int setparam_display(struct param_opts *popt, char *pattern, char *value)
945 {
946         int rc;
947         int fd;
948         int i;
949         glob_t glob_info;
950         char filename[PATH_MAX + 1];    /* extra 1 byte for file type */
951
952         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
953         if (rc) {
954                 fprintf(stderr, "error: set_param: %s: %s\n",
955                         pattern, globerrstr(rc));
956                 return -ESRCH;
957         }
958         for (i = 0; i  < glob_info.gl_pathc; i++) {
959                 char *valuename = NULL;
960
961                 if (popt->show_path) {
962                         strcpy(filename, glob_info.gl_pathv[i]);
963                         valuename = display_name(filename, 0);
964                         if (valuename)
965                                 printf("%s=%s\n", valuename, value);
966                 }
967                 /* Write the new value to the file */
968                 fd = open(glob_info.gl_pathv[i], O_WRONLY);
969                 if (fd > 0) {
970                         rc = write(fd, value, strlen(value));
971                         if (rc < 0)
972                                 fprintf(stderr, "error: set_param: "
973                                         "writing to file %s: %s\n",
974                                         glob_info.gl_pathv[i], strerror(errno));
975                         else
976                                 rc = 0;
977                         close(fd);
978                 } else {
979                         fprintf(stderr, "error: set_param: %s opening %s\n",
980                                 strerror(rc = errno), glob_info.gl_pathv[i]);
981                 }
982         }
983
984         globfree(&glob_info);
985         return rc;
986 }
987
988 int jt_lcfg_setparam(int argc, char **argv)
989 {
990         int rc = 0, i;
991         struct param_opts popt;
992         char pattern[PATH_MAX];
993         char *path = NULL, *value = NULL;
994
995         rc = setparam_cmdline(argc, argv, &popt);
996         if (rc < 0 || rc >= argc)
997                 return CMD_HELP;
998
999         for (i = rc, rc = 0; i < argc; i++) {
1000                 int rc2;
1001
1002                 if ((value = strchr(argv[i], '=')) != NULL) {
1003                         /* format: set_param a=b */
1004                         *value = '\0';
1005                         value ++;
1006                         path = argv[i];
1007                 } else {
1008                         /* format: set_param a b */
1009                         if (path == NULL) {
1010                                 path = argv[i];
1011                                 continue;
1012                         } else {
1013                                 value = argv[i];
1014                         }
1015                 }
1016
1017                 clean_path(path);
1018
1019                 lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
1020
1021                 rc2 = setparam_display(&popt, pattern, value);
1022                 path = NULL;
1023                 value = NULL;
1024                 if (rc2 < 0 && rc == 0)
1025                         rc = rc2;
1026         }
1027
1028         return rc;
1029 }
1030