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