1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/lustre_cfg.c
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>
45 #include <sys/ioctl.h>
52 #include <liblustre.h>
54 #include <lustre_lib.h>
55 #include <lustre_cfg.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_dlm.h>
58 #include <obd.h> /* for struct lov_stripe_md */
60 #include <lustre/lustre_build_version.h>
71 #include <lnet/lnetctl.h>
72 #include <libcfs/libcfsutil.h>
75 static char * lcfg_devname;
77 int lcfg_set_devname(char *name)
82 /* quietly strip the unnecessary '$' */
83 if (*name == '$' || *name == '%')
86 /* We can't translate from dev # to name */
89 lcfg_devname = strdup(name);
97 char * lcfg_get_devname(void)
102 int jt_lcfg_device(int argc, char **argv)
104 return jt_obd_device(argc, argv);
107 int jt_lcfg_attach(int argc, char **argv)
109 struct lustre_cfg_bufs bufs;
110 struct lustre_cfg *lcfg;
116 lustre_cfg_bufs_reset(&bufs, NULL);
118 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
119 lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
120 lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
122 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
123 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
124 lustre_cfg_free(lcfg);
126 fprintf(stderr, "error: %s: LCFG_ATTACH %s\n",
127 jt_cmdname(argv[0]), strerror(rc = errno));
128 } else if (argc == 3) {
131 lcfg_set_devname(argv[2]);
132 if (strlen(argv[2]) > 128) {
133 printf("Name too long to set environment\n");
136 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
137 rc = setenv(name, argv[1], 1);
139 printf("error setting env variable %s\n", name);
142 lcfg_set_devname(argv[2]);
148 int jt_lcfg_setup(int argc, char **argv)
150 struct lustre_cfg_bufs bufs;
151 struct lustre_cfg *lcfg;
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]));
162 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
167 for (i = 1; i < argc; i++) {
168 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
171 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
172 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
173 lustre_cfg_free(lcfg);
175 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
176 strerror(rc = errno));
181 int jt_obd_detach(int argc, char **argv)
183 struct lustre_cfg_bufs bufs;
184 struct lustre_cfg *lcfg;
187 if (lcfg_devname == NULL) {
188 fprintf(stderr, "%s: please use 'device name' to set the "
189 "device name for config commands.\n",
190 jt_cmdname(argv[0]));
194 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
199 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
200 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
201 lustre_cfg_free(lcfg);
203 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
204 strerror(rc = errno));
209 int jt_obd_cleanup(int argc, char **argv)
211 struct lustre_cfg_bufs bufs;
212 struct lustre_cfg *lcfg;
215 char flags[3] = { 0 };
219 if (lcfg_devname == NULL) {
220 fprintf(stderr, "%s: please use 'device name' to set the "
221 "device name for config commands.\n",
222 jt_cmdname(argv[0]));
226 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
228 if (argc < 1 || argc > 3)
231 /* we are protected from overflowing our buffer by the argc
234 for (n = 1; n < argc; n++) {
235 if (strcmp(argv[n], "force") == 0) {
236 flags[flag_cnt++] = force;
237 } else if (strcmp(argv[n], "failover") == 0) {
238 flags[flag_cnt++] = failover;
240 fprintf(stderr, "unknown option: %s", argv[n]);
246 lustre_cfg_bufs_set_string(&bufs, 1, flags);
249 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
250 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
251 lustre_cfg_free(lcfg);
253 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
254 strerror(rc = errno));
260 int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
263 struct lustre_cfg_bufs bufs;
264 struct lustre_cfg *lcfg;
266 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
268 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
270 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
271 lcfg->lcfg_nid = nid;
272 /* Poison NAL -- pre 1.4.6 will LASSERT on 0 NAL, this way it
273 doesn't work without crashing (bz 10130) */
274 lcfg->lcfg_nal = 0x5a;
277 fprintf(stderr, "adding\tnid: %d\tuuid: %s\n",
278 lcfg->lcfg_nid, uuid);
280 rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
281 lustre_cfg_free(lcfg);
283 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
288 printf ("Added uuid %s: %s\n", uuid, libcfs_nid2str(nid));
292 int jt_lcfg_add_uuid(int argc, char **argv)
300 nid = libcfs_str2nid(argv[2]);
301 if (nid == LNET_NID_ANY) {
302 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
306 return do_add_uuid(argv[0], argv[1], nid);
309 int obd_add_uuid(char *uuid, lnet_nid_t nid)
311 return do_add_uuid("obd_add_uuid", uuid, nid);
314 int jt_lcfg_del_uuid(int argc, char **argv)
317 struct lustre_cfg_bufs bufs;
318 struct lustre_cfg *lcfg;
321 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
325 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
326 if (strcmp (argv[1], "_all_"))
327 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
329 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
330 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
331 lustre_cfg_free(lcfg);
333 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
343 int jt_lcfg_del_mount_option(int argc, char **argv)
346 struct lustre_cfg_bufs bufs;
347 struct lustre_cfg *lcfg;
352 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
355 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
357 lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
358 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
359 lustre_cfg_free(lcfg);
361 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
362 strerror(rc = errno));
367 int jt_lcfg_set_timeout(int argc, char **argv)
370 struct lustre_cfg_bufs bufs;
371 struct lustre_cfg *lcfg;
373 fprintf(stderr, "%s has been deprecated. Use conf_param instead.\n"
374 "e.g. conf_param lustre-MDT0000 obd_timeout=50\n",
375 jt_cmdname(argv[0]));
382 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
383 lcfg = lustre_cfg_new(LCFG_SET_TIMEOUT, &bufs);
384 lcfg->lcfg_num = atoi(argv[1]);
386 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
387 //rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
389 lustre_cfg_free(lcfg);
391 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
392 strerror(rc = errno));
399 int jt_lcfg_add_conn(int argc, char **argv)
401 struct lustre_cfg_bufs bufs;
402 struct lustre_cfg *lcfg;
413 if (lcfg_devname == NULL) {
414 fprintf(stderr, "%s: please use 'device name' to set the "
415 "device name for config commands.\n",
416 jt_cmdname(argv[0]));
420 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
422 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
424 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
425 lcfg->lcfg_num = priority;
427 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
428 lustre_cfg_free (lcfg);
430 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
431 strerror(rc = errno));
437 int jt_lcfg_del_conn(int argc, char **argv)
439 struct lustre_cfg_bufs bufs;
440 struct lustre_cfg *lcfg;
446 if (lcfg_devname == NULL) {
447 fprintf(stderr, "%s: please use 'device name' to set the "
448 "device name for config commands.\n",
449 jt_cmdname(argv[0]));
453 lustre_cfg_bufs_reset(&bufs, lcfg_devname);
455 /* connection uuid */
456 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
458 lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
460 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
461 lustre_cfg_free(lcfg);
463 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
464 strerror(rc = errno));
470 /* Param set locally, directly on target */
471 int jt_lcfg_param(int argc, char **argv)
474 struct lustre_cfg_bufs bufs;
475 struct lustre_cfg *lcfg;
477 if (argc >= LUSTRE_CFG_MAX_BUFCOUNT)
480 lustre_cfg_bufs_reset(&bufs, NULL);
482 for (i = 1; i < argc; i++) {
483 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
486 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
488 rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
489 lustre_cfg_free(lcfg);
491 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
492 strerror(rc = errno));
497 /* Param set in config log on MGS */
498 /* conf_param key1=value1 [key2=value2...] */
499 int jt_lcfg_mgsparam(int argc, char **argv)
502 struct lustre_cfg_bufs bufs;
503 struct lustre_cfg *lcfg;
505 if ((argc >= LUSTRE_CFG_MAX_BUFCOUNT) || (argc <= 1))
508 lustre_cfg_bufs_reset(&bufs, NULL);
509 for (i = 1; i < argc; i++) {
510 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
513 /* We could put other opcodes here. */
514 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
516 rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
517 lustre_cfg_free(lcfg);
519 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
520 strerror(rc = errno));
526 /* Display the path in the same format as sysctl
527 * For eg. obdfilter.lustre-OST0000.stats */
528 static char *display_name(char *filename)
532 filename += strlen("/proc/");
533 if (strncmp(filename, "fs/", strlen("fs/")) == 0)
534 filename += strlen("fs/");
536 filename += strlen("sys/");
538 if (strncmp(filename, "lustre/", strlen("lustre/")) == 0)
539 filename += strlen("lustre/");
541 /* replace '/' with '.' to match conf_param and sysctl */
543 while ((tmp = strchr(tmp, '/')) != NULL)
549 /* Find a character in a length limited string */
550 /* BEWARE - kernel definition of strnchr has args in different order! */
551 static char *strnchr(const char *p, char c, size_t n)
564 int jt_lcfg_getparam(int argc, char **argv)
567 int rc = 0, i, show_path = 0, only_path = 0;
568 char pattern[PATH_MAX];
569 char *path, *tmp, *buf;
572 if (argc == 3 && (strcmp(argv[1], "-n") == 0 || strcmp(argv[1], "-N") == 0)) {
574 if (strcmp(argv[1], "-N") == 0) {
578 } else if (argc == 2) {
585 /* If the input is in form Eg. obdfilter.*.stats */
586 if (strchr(path, '.')) {
588 while (*tmp != '\0') {
595 /* If the entire path is specified as input */
596 fp = open(path, O_RDONLY);
598 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
601 strcpy(pattern, path);
605 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
607 fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
611 buf = malloc(CFS_PAGE_SIZE);
612 for (i = 0; i < glob_info.gl_pathc; i++) {
613 char *valuename = NULL;
615 memset(buf, 0, CFS_PAGE_SIZE);
618 filename = strdup(glob_info.gl_pathv[i]);
619 valuename = display_name(filename);
620 if (valuename && only_path) {
621 printf("%s\n", valuename);
626 /* Write the contents of file to stdout */
627 fp = open(glob_info.gl_pathv[i], O_RDONLY);
629 fprintf(stderr, "error: %s: opening('%s') failed: %s\n",
630 jt_cmdname(argv[0]), glob_info.gl_pathv[i],
636 rc = read(fp, buf, CFS_PAGE_SIZE);
640 fprintf(stderr, "error: %s: read('%s') "
641 "failed: %s\n", jt_cmdname(argv[0]),
642 glob_info.gl_pathv[i], strerror(errno));
645 /* Print the output in the format path=value if the
646 * value contains no new line character or cab be
647 * occupied in a line, else print value on new line */
648 if (valuename && show_path) {
649 int longbuf = strnchr(buf, rc - 1, '\n') != NULL
651 printf("%s=%s", valuename, longbuf ? "\n" : buf);
657 rc = write(fileno(stdout), buf, rc);
659 fprintf(stderr, "error: %s: write to stdout "
660 "failed: %s\n", jt_cmdname(argv[0]),
668 globfree(&glob_info);
674 int jt_lcfg_setparam(int argc, char **argv)
677 int fp, show_path = 0;
678 char pattern[PATH_MAX];
679 char *path, *value, *tmp;
683 if (argc == 4 && (strcmp(argv[1], "-n") == 0)) {
684 /* Format: lctl set_param -n param value */
687 } else if (argc == 3) {
688 if (strcmp(argv[1], "-n") != 0) {
689 /* Format: lctl set_param param value */
692 } else if ((value = strchr(argv[2], '=')) != NULL) {
693 /* Format: lctl set_param -n param=value */
698 fprintf(stderr, "error: %s Incorrect arguments."
700 jt_cmdname(argv[0]));
703 } else if (argc == 2 && ((value = strchr(argv[1], '=')) != NULL)) {
704 /* Format: lctl set_param param=value */
709 fprintf(stderr, "error: %s Incorrect arguments. See Usage\n",
710 jt_cmdname(argv[0]));
714 /* If the input is in form Eg. obdfilter.*.stats */
715 if (strchr(path, '.')) {
717 while (*tmp != '\0') {
724 fp = open(path, O_RDONLY);
726 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
729 strcpy(pattern, path);
733 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
735 fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
738 for (i = 0; i < glob_info.gl_pathc; i++) {
740 char *valuename, *filename;
741 filename = strdup(glob_info.gl_pathv[i]);
742 valuename = display_name(filename);
743 printf("%s=%s\n", valuename, value);
745 /* Write the new value to the file */
746 fp = open(glob_info.gl_pathv[i], O_WRONLY);
748 rc = write(fp, value, strlen(value));
751 "error writing to file %s\n",
752 glob_info.gl_pathv[i]);
757 fprintf(stderr, "error: %s: %s opening %s\n",
758 jt_cmdname(argv[0]), strerror(rc = errno),
759 glob_info.gl_pathv[i]);
763 globfree(&glob_info);