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