Whamcloud - gitweb
b=3026
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *   Author: Robert Read <rread@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27
28 #include <stdlib.h>
29 #include <sys/ioctl.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32
33 #ifndef __KERNEL__
34 #include <liblustre.h>
35 #endif
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_cfg.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_dlm.h>
40 #include <linux/obd.h>          /* for struct lov_stripe_md */
41 #include <linux/lustre_build_version.h>
42
43 #include <unistd.h>
44 #include <sys/un.h>
45 #include <time.h>
46 #include <sys/time.h>
47 #include <errno.h>
48 #include <string.h>
49
50
51 #include "obdctl.h"
52 #include <portals/ptlctl.h>
53 #include "parser.h"
54 #include <stdio.h>
55
56 static char * lcfg_devname;
57
58 void lcfg_set_devname(char *name)
59 {
60         if (lcfg_devname)
61                 free(lcfg_devname);
62         lcfg_devname = strdup(name);
63 }
64
65
66 int jt_lcfg_device(int argc, char **argv)
67 {
68         char *name;
69
70         if (argc == 1) {
71                 printf("current device is %s\n", lcfg_devname? : "not set");
72                 return 0;
73         } else if (argc != 2) {
74                 return CMD_HELP;
75         }
76
77         name = argv[1];
78
79         /* quietly strip the unnecessary '$' */
80         if (*name == '$')
81                 name++;
82
83         lcfg_set_devname(name);
84
85         return 0;
86 }
87
88 /* NOOP */
89 int jt_lcfg_newdev(int argc, char **argv)
90 {
91         return 0;
92 }
93
94 int jt_lcfg_attach(int argc, char **argv)
95 {
96         struct lustre_cfg_bufs bufs;
97         struct lustre_cfg *lcfg;
98         int rc;
99
100         if (argc != 2 && argc != 3 && argc != 4)
101                 return CMD_HELP;
102
103         lustre_cfg_bufs_reset(&bufs, NULL);
104
105         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
106         if (argc >= 3) {
107                 lustre_cfg_bufs_set_string(&bufs, 0, argv[2]);
108         } else {
109                 fprintf(stderr, "error: %s: LCFG_ATTACH requires a name\n",
110                         jt_cmdname(argv[0])); 
111                 return -EINVAL;
112         }
113
114         if (argc == 4) {
115                 lustre_cfg_bufs_set_string(&bufs, 2, argv[3]);
116         }
117
118         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
119         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
120         lustre_cfg_free(lcfg);
121         if (rc < 0) {
122                 fprintf(stderr, "error: %s: LCFG_ATTACH %s\n",
123                         jt_cmdname(argv[0]), strerror(rc = errno));
124         } else if (argc == 3) {
125                 char name[1024];
126
127                 lcfg_set_devname(argv[2]);
128                 if (strlen(argv[2]) > 128) {
129                         printf("Name too long to set environment\n");
130                         return -EINVAL;
131                 }
132                 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
133                 rc = setenv(name, argv[1], 1);
134                 if (rc) {
135                         printf("error setting env variable %s\n", name);
136                 }
137         } else {
138                 lcfg_set_devname(argv[2]);
139         }
140
141         return rc;
142 }
143
144 int jt_lcfg_setup(int argc, char **argv)
145 {
146         struct lustre_cfg_bufs bufs;
147         struct lustre_cfg *lcfg;
148         int i;
149         int rc;
150
151         if (lcfg_devname == NULL) {
152                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
153                         "device name for config commands.\n", 
154                         jt_cmdname(argv[0])); 
155                 return -EINVAL;
156         }
157
158         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
159
160         if (argc > 5)
161                 return CMD_HELP;
162
163         for (i = 1; i < argc; i++) {
164                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
165         }
166
167         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
168         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
169         lustre_cfg_free(lcfg);
170         if (rc < 0)
171                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
172                         strerror(rc = errno));
173
174         return rc;
175 }
176
177 int jt_obd_detach(int argc, char **argv)
178 {
179         struct lustre_cfg_bufs bufs;
180         struct lustre_cfg *lcfg;
181         int rc;
182
183         if (lcfg_devname == NULL) {
184                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
185                         "device name for config commands.\n", 
186                         jt_cmdname(argv[0])); 
187                 return -EINVAL;
188         }
189
190         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
191
192         if (argc != 1)
193                 return CMD_HELP;
194
195         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
196         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
197         lustre_cfg_free(lcfg);
198         if (rc < 0)
199                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
200                         strerror(rc = errno));
201
202         return rc;
203 }
204
205 int jt_obd_cleanup(int argc, char **argv)
206 {
207         struct lustre_cfg_bufs bufs;
208         struct lustre_cfg *lcfg;
209         char force = 'F';
210         char failover = 'A';
211         char flags[3] = { 0 };
212         int flag_cnt = 0, n;
213         int rc;
214
215         if (lcfg_devname == NULL) {
216                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
217                         "device name for config commands.\n", 
218                         jt_cmdname(argv[0])); 
219                 return -EINVAL;
220         }
221
222         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
223
224         if (argc < 1 || argc > 3)
225                 return CMD_HELP;
226
227         /* we are protected from overflowing our buffer by the argc
228          * check above
229          */
230         for (n = 1; n < argc; n++) {
231                 if (strcmp(argv[n], "force") == 0) {
232                         flags[flag_cnt++] = force;
233                 } else if (strcmp(argv[n], "failover") == 0) {
234                         flags[flag_cnt++] = failover;
235                 } else {
236                         fprintf(stderr, "unknown option: %s", argv[n]);
237                         return CMD_HELP;
238                 }
239         }
240
241         if (flag_cnt) {
242                 lustre_cfg_bufs_set_string(&bufs, 1, flags);
243         }
244
245         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
246         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
247         lustre_cfg_free(lcfg);
248         if (rc < 0)
249                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
250                         strerror(rc = errno));
251
252         return rc;
253 }
254
255 static 
256 int do_add_uuid(char * func, char *uuid, ptl_nid_t nid, int nal) 
257 {
258         char tmp[64];
259         int rc;
260         struct lustre_cfg_bufs bufs;
261         struct lustre_cfg *lcfg;
262
263         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
264         if (uuid)
265                 lustre_cfg_bufs_set_string(&bufs, 1, uuid);
266
267         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
268         lcfg->lcfg_nid = nid;
269         lcfg->lcfg_nal = nal;
270
271 #if 0
272         fprintf(stderr, "adding\tnal: %d\tnid: %d\tuuid: %s\n",
273                lcfg->lcfg_nid, lcfg->lcfg_nal, uuid);
274 #endif
275         rc = lcfg_ioctl(func, OBD_DEV_ID, lcfg);
276         lustre_cfg_free(lcfg);
277         if (rc) {
278                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
279                         strerror(errno));
280                 return -1;
281         }
282
283         printf ("Added uuid %s: %s\n", uuid, ptl_nid2str (tmp, nid));
284         return 0;
285 }
286
287 int jt_lcfg_add_uuid(int argc, char **argv)
288 {
289         ptl_nid_t nid = 0;
290         int nal;
291         
292         if (argc != 4) {                
293                 return CMD_HELP;
294         }
295
296         if (ptl_parse_nid (&nid, argv[2]) != 0) {
297                 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
298                         return (-1);
299         }
300
301         nal = ptl_name2nal(argv[3]);
302
303         if (nal <= 0) {
304                 fprintf (stderr, "Can't parse NAL %s\n", argv[3]);
305                 return -1;
306         }
307
308         return do_add_uuid(argv[0], argv[1], nid, nal);
309 }
310
311 int obd_add_uuid(char *uuid, ptl_nid_t nid, int nal)
312 {
313         return do_add_uuid("obd_add_uuid", uuid, nid, nal);
314 }
315
316 int jt_lcfg_del_uuid(int argc, char **argv)
317 {
318         int rc;
319         struct lustre_cfg_bufs bufs;
320         struct lustre_cfg *lcfg;
321
322         if (argc != 2) {
323                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
324                 return 0;
325         }
326
327         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
328         if (strcmp (argv[1], "_all_"))
329                 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
330         
331         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
332         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
333         lustre_cfg_free(lcfg);
334         if (rc) {
335                 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
336                         strerror(errno));
337                 return -1;
338         }
339         return 0;
340 }
341
342 int jt_lcfg_lov_setup(int argc, char **argv)
343 {
344         struct lustre_cfg_bufs bufs;
345         struct lustre_cfg *lcfg;
346         struct lov_desc desc;
347         int rc;
348         char *end;
349
350          /* argv: lov_setup <LOV uuid> <stripe count> <stripe size>
351           *                 <stripe offset> <pattern> [ <max tgt index> ]
352           */
353         if (argc < 6 || argc > 7)
354                 return CMD_HELP;
355
356         if (strlen(argv[1]) > sizeof(desc.ld_uuid) - 1) {
357                 fprintf(stderr,
358                         "error: %s: LOV uuid '%s' longer than "LPSZ" chars\n",
359                         jt_cmdname(argv[0]), argv[1], sizeof(desc.ld_uuid) - 1);
360                 return -EINVAL;
361         }
362
363         memset(&desc, 0, sizeof(desc));
364         obd_str2uuid(&desc.ld_uuid, argv[1]);
365         desc.ld_tgt_count = 0;
366         desc.ld_magic = LOV_DESC_MAGIC;
367         desc.ld_default_stripe_count = strtoul(argv[2], &end, 0);
368         if (*end) {
369                 fprintf(stderr, "error: %s: bad default stripe count '%s'\n",
370                         jt_cmdname(argv[0]), argv[2]);
371                 return CMD_HELP;
372         }
373
374         desc.ld_default_stripe_size = strtoull(argv[3], &end, 0);
375         if (*end) {
376                 fprintf(stderr, "error: %s: bad default stripe size '%s'\n",
377                         jt_cmdname(argv[0]), argv[3]);
378                 return CMD_HELP;
379         }
380         if (desc.ld_default_stripe_size < 4096) {
381                 fprintf(stderr,
382                         "error: %s: default stripe size "LPU64" too small\n",
383                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
384                 return -EINVAL;
385         } else if ((long)desc.ld_default_stripe_size <
386                    desc.ld_default_stripe_size) {
387                 fprintf(stderr,
388                         "error: %s: default stripe size "LPU64" too large\n",
389                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
390                 return -EINVAL;
391         }
392         desc.ld_default_stripe_offset = strtoull(argv[4], &end, 0);
393         if (*end) {
394                 fprintf(stderr, "error: %s: bad default stripe offset '%s'\n",
395                         jt_cmdname(argv[0]), argv[4]);
396                 return CMD_HELP;
397         }
398         desc.ld_pattern = strtoul(argv[5], &end, 0);
399         if (*end) {
400                 fprintf(stderr, "error: %s: bad stripe pattern '%s'\n",
401                         jt_cmdname(argv[0]), argv[5]);
402                 return CMD_HELP;
403         }
404
405         if (argc == 7) {
406                 desc.ld_tgt_count = strtoul(argv[6], &end, 0);
407                 if (*end) {
408                         fprintf(stderr, "error: %s: bad target count '%s'\n",
409                                 jt_cmdname(argv[0]), argv[6]);
410                         return CMD_HELP;
411                 }
412                 if (desc.ld_default_stripe_count > desc.ld_tgt_count) {
413                         fprintf(stderr,
414                                 "error: %s: default stripe count %u > "
415                                 "OST count %u\n", jt_cmdname(argv[0]),
416                                 desc.ld_default_stripe_count,
417                                 desc.ld_tgt_count);
418                         return -EINVAL;
419                 }
420         }
421
422         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
423         lustre_cfg_bufs_set(&bufs, 1, &desc, sizeof(desc));
424
425         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
426         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
427         lustre_cfg_free(lcfg);
428         if (rc)
429                 fprintf(stderr, "error: %s: ioctl error: %s\n",
430                         jt_cmdname(argv[0]), strerror(rc = errno));
431         return rc;
432 }
433
434 int jt_lcfg_lov_modify_tgts(int argc, char **argv)
435 {
436         struct lustre_cfg_bufs bufs;
437         struct lustre_cfg *lcfg;
438         char *end;
439         int cmd = 0;
440         int index;
441         int gen;
442         int rc;
443         
444         /* argv: lov_modify_tgts <op> <LOV name> <OBD uuid> <index> <gen> */
445         if (argc != 6)
446                 return CMD_HELP;
447         
448         if (!strncmp(argv[1], "add", 4)) {
449                 cmd = LCFG_LOV_ADD_OBD;
450         } else if (!strncmp(argv[1], "del", 4)) {
451                 cmd = LCFG_LOV_DEL_OBD;
452         } else {
453                 fprintf(stderr, "error: %s: bad operation '%s'\n",
454                         jt_cmdname(argv[0]), argv[1]);
455                 return CMD_HELP;
456         }
457         
458         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
459         
460         if (((index = strlen(argv[3]) + 1)) > sizeof(struct obd_uuid)) {
461                 fprintf(stderr,
462                         "error: %s: OBD uuid '%s' longer than "LPSZ" chars\n",
463                         jt_cmdname(argv[0]), argv[3],
464                         sizeof(struct obd_uuid) - 1);
465                 return -EINVAL;
466         }
467         lustre_cfg_bufs_set(&bufs, 1, argv[3], index);
468         
469         index = strtoul(argv[4], &end, 0);
470         if (*end) {
471                 fprintf(stderr, "error: %s: bad OBD index '%s'\n",
472                         jt_cmdname(argv[0]), argv[4]);
473                 return CMD_HELP;
474         }
475         lustre_cfg_bufs_set(&bufs, 2, argv[4], strlen(argv[4]));
476         
477         gen = strtoul(argv[5], &end, 0);
478         if (*end) {
479                 fprintf(stderr, "error: %s: bad OBD generation '%s'\n",
480                         jt_cmdname(argv[0]), argv[5]);
481                 return CMD_HELP;
482         }
483         lustre_cfg_bufs_set(&bufs, 3, argv[5], strlen(argv[5]));
484         
485         lcfg = lustre_cfg_new(cmd, &bufs);
486         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
487         lustre_cfg_free(lcfg);
488         if (rc)
489                 fprintf(stderr, "error: %s: ioctl error: %s\n",
490                         jt_cmdname(argv[0]), strerror(rc = errno));
491         
492         return rc;
493 }                                                     
494    
495
496
497 int jt_lcfg_mount_option(int argc, char **argv)
498 {
499         int rc;
500         struct lustre_cfg_bufs bufs;
501         struct lustre_cfg *lcfg;
502         int i;
503
504         if (argc < 3 || argc > 4)
505                 return CMD_HELP;
506
507         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
508
509         for (i = 1; i < argc; i++)
510                 lustre_cfg_bufs_set_string(&bufs, i, argv[i]);
511
512         lcfg = lustre_cfg_new(LCFG_MOUNTOPT, &bufs);
513         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
514         lustre_cfg_free(lcfg);
515         if (rc < 0) {
516                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
517                         strerror(rc = errno));
518         }
519         return rc;
520 }
521
522 int jt_lcfg_del_mount_option(int argc, char **argv)
523 {
524         int rc;
525         struct lustre_cfg_bufs bufs;
526         struct lustre_cfg *lcfg;
527
528         if (argc != 2)
529                 return CMD_HELP;
530
531         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
532
533         /* profile name */
534         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
535
536         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
537         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
538         lustre_cfg_free(lcfg);
539         if (rc < 0) {
540                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
541                         strerror(rc = errno));
542         }
543         return rc;
544 }
545
546 int jt_lcfg_set_timeout(int argc, char **argv)
547 {
548         int rc;
549         struct lustre_cfg_bufs bufs;
550         struct lustre_cfg *lcfg;
551
552         if (argc != 2)
553                 return CMD_HELP;
554
555         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
556         lcfg = lustre_cfg_new(LCFG_SET_TIMEOUT, &bufs);
557         lcfg->lcfg_num = atoi(argv[1]);
558         
559         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
560         lustre_cfg_free(lcfg);
561         if (rc < 0) {
562                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
563                         strerror(rc = errno));
564         }
565         return rc;
566 }
567
568
569 int jt_lcfg_set_lustre_upcall(int argc, char **argv)
570 {
571         int rc;
572         struct lustre_cfg_bufs bufs;
573         struct lustre_cfg *lcfg;
574
575         if (argc != 2)
576                 return CMD_HELP;
577
578         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
579
580         /* profile name */
581         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
582
583         lcfg = lustre_cfg_new(LCFG_SET_UPCALL, &bufs);
584         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
585         lustre_cfg_free(lcfg);
586         if (rc < 0) {
587                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
588                         strerror(rc = errno));
589         }
590         return rc;
591 }
592
593 int jt_lcfg_add_conn(int argc, char **argv)
594 {
595         struct lustre_cfg_bufs bufs;
596         struct lustre_cfg *lcfg;
597         int priority;
598         int rc;
599
600         if (argc == 2)
601                 priority = 0;
602         else if (argc == 3)
603                 priority = 1;
604         else
605                 return CMD_HELP;
606
607         if (lcfg_devname == NULL) {
608                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
609                         "device name for config commands.\n", 
610                         jt_cmdname(argv[0])); 
611                 return -EINVAL;
612         }
613
614         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
615
616         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
617
618         lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
619         lcfg->lcfg_num = priority;
620
621         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
622         lustre_cfg_free (lcfg);
623         if (rc < 0) {
624                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
625                         strerror(rc = errno));
626         }
627
628         return rc;
629 }
630
631 int jt_lcfg_del_conn(int argc, char **argv)
632 {
633         struct lustre_cfg_bufs bufs;
634         struct lustre_cfg *lcfg;
635         int rc;
636
637         if (argc != 2)
638                 return CMD_HELP;
639
640         if (lcfg_devname == NULL) {
641                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
642                         "device name for config commands.\n", 
643                         jt_cmdname(argv[0])); 
644                 return -EINVAL;
645         }
646
647         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
648
649         /* connection uuid */
650         lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
651
652         lcfg = lustre_cfg_new(LCFG_DEL_MOUNTOPT, &bufs);
653
654         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
655         lustre_cfg_free(lcfg);
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 }