Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[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 lcfg;
97         int rc;
98
99         LCFG_INIT(lcfg, LCFG_ATTACH, lcfg_devname);
100
101         if (argc != 2 && argc != 3 && argc != 4)
102                 return CMD_HELP;
103
104         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
105         lcfg.lcfg_inlbuf1 = argv[1];
106         if (argc >= 3) {
107                 lcfg.lcfg_dev_namelen = strlen(argv[2]) + 1;
108                 lcfg.lcfg_dev_name = argv[2];
109         } else {
110                 fprintf(stderr, "error: %s: LCFG_ATTACH requires a name\n",
111                         jt_cmdname(argv[0])); 
112                 return -EINVAL;
113         }
114
115         if (argc == 4) {
116                 lcfg.lcfg_inllen2 = strlen(argv[3]) + 1;
117                 lcfg.lcfg_inlbuf2 = argv[3];
118         }
119
120         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &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 lcfg;
147         int rc;
148
149         if (lcfg_devname == NULL) {
150                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
151                         "device name for config commands.\n", 
152                         jt_cmdname(argv[0])); 
153                 return -EINVAL;
154         }
155
156         LCFG_INIT(lcfg, LCFG_SETUP, lcfg_devname);
157
158         if (argc > 7)
159                 return CMD_HELP;
160
161         if (argc > 1) {
162                 lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
163                 lcfg.lcfg_inlbuf1 = argv[1];
164         }
165         if (argc > 2) {
166                 lcfg.lcfg_inllen2 = strlen(argv[2]) + 1;
167                 lcfg.lcfg_inlbuf2 = argv[2];
168         }
169         if (argc > 3) {
170                 lcfg.lcfg_inllen3 = strlen(argv[3]) + 1;
171                 lcfg.lcfg_inlbuf3 = argv[3];
172         }
173         if (argc > 4) {
174                 lcfg.lcfg_inllen4 = strlen(argv[4]) + 1;
175                 lcfg.lcfg_inlbuf4 = argv[4];
176         }
177         if (argc > 5) {
178                 lcfg.lcfg_inllen5 = strlen(argv[5]) + 1;
179                 lcfg.lcfg_inlbuf5 = argv[5];
180         }
181         if (argc > 6) {
182                 lcfg.lcfg_inllen6 = strlen(argv[6]) + 1;
183                 lcfg.lcfg_inlbuf6 = argv[6];
184         }
185         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
186         if (rc < 0)
187                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
188                         strerror(rc = errno));
189
190         return rc;
191 }
192
193 int jt_obd_detach(int argc, char **argv)
194 {
195         struct lustre_cfg lcfg;
196         int rc;
197
198         if (lcfg_devname == NULL) {
199                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
200                         "device name for config commands.\n", 
201                         jt_cmdname(argv[0])); 
202                 return -EINVAL;
203         }
204
205         LCFG_INIT(lcfg, LCFG_DETACH, lcfg_devname);
206
207         if (argc != 1)
208                 return CMD_HELP;
209
210         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
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 lcfg;
221         char force = 'F';
222         char failover = 'A';
223         char flags[3];
224         int flag_cnt = 0, n;
225         int rc;
226
227         if (lcfg_devname == NULL) {
228                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
229                         "device name for config commands.\n", 
230                         jt_cmdname(argv[0])); 
231                 return -EINVAL;
232         }
233
234         LCFG_INIT(lcfg, LCFG_CLEANUP, lcfg_devname);
235
236         if (argc < 1 || argc > 3)
237                 return CMD_HELP;
238
239         for (n = 1; n < argc; n++) 
240                 if (strcmp(argv[n], "force") == 0) {
241                         flags[flag_cnt++] = force;
242                 } else if (strcmp(argv[n], "failover") == 0) {
243                         flags[flag_cnt++] = failover;
244                 } else {
245                         fprintf(stderr, "unknown option: %s", argv[n]);
246                         return CMD_HELP;
247                 }
248
249         lcfg.lcfg_inllen1 = flag_cnt;
250         if (flag_cnt)
251                 lcfg.lcfg_inlbuf1 = flags;
252
253         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
254         if (rc < 0)
255                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
256                         strerror(rc = errno));
257
258         return rc;
259 }
260
261 static 
262 int do_add_uuid(char * func, char *uuid, ptl_nid_t nid, int nal) 
263 {
264         char tmp[64];
265         int rc;
266         struct lustre_cfg lcfg;
267
268         LCFG_INIT(lcfg, LCFG_ADD_UUID, lcfg_devname);
269         lcfg.lcfg_nid = nid;
270         lcfg.lcfg_inllen1 = strlen(uuid) + 1;
271         lcfg.lcfg_inlbuf1 = uuid;
272         lcfg.lcfg_nal = nal;
273
274         rc = lcfg_ioctl(func, OBD_DEV_ID, &lcfg);
275         if (rc) {
276                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
277                         strerror(errno));
278                 return -1;
279         }
280
281         printf ("Added uuid %s: %s\n", uuid, ptl_nid2str (tmp, nid));
282         return 0;
283 }
284
285 int jt_lcfg_add_uuid(int argc, char **argv)
286 {
287         ptl_nid_t nid = 0;
288         int nal;
289         
290         if (argc != 4) {                
291                 return CMD_HELP;
292         }
293
294         if (ptl_parse_nid (&nid, argv[2]) != 0) {
295                 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
296                         return (-1);
297         }
298
299         nal = ptl_name2nal(argv[3]);
300
301         if (nal <= 0) {
302                 fprintf (stderr, "Can't parse NAL %s\n", argv[3]);
303                 return -1;
304         }
305
306         return do_add_uuid(argv[0], argv[1], nid, nal);
307 }
308
309 int obd_add_uuid(char *uuid, ptl_nid_t nid, int nal)
310 {
311         return do_add_uuid("obd_add_uuid", uuid, nid, nal);
312 }
313
314 int jt_lcfg_del_uuid(int argc, char **argv)
315 {
316         int rc;
317         struct lustre_cfg lcfg;
318
319         if (argc != 2) {
320                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
321                 return 0;
322         }
323
324         LCFG_INIT(lcfg, LCFG_DEL_UUID, lcfg_devname);
325
326         if (strcmp (argv[1], "_all_"))
327         {
328                 lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
329                 lcfg.lcfg_inlbuf1 = argv[1];
330         }
331         
332         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
333         if (rc) {
334                 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
335                         strerror(errno));
336                 return -1;
337         }
338         return 0;
339 }
340
341 int jt_lcfg_lov_setup(int argc, char **argv)
342 {
343         struct lustre_cfg lcfg;
344         struct lov_desc desc;
345         struct obd_uuid *uuidarray, *ptr;
346         int rc, i;
347         char *end;
348
349         LCFG_INIT(lcfg, LCFG_SETUP, lcfg_devname);
350
351         if (argc <= 6)
352                 return CMD_HELP;
353
354         if (strlen(argv[1]) > sizeof(desc.ld_uuid) - 1) {
355                 fprintf(stderr,
356                         "error: %s: LOV uuid '%s' longer than "LPSZ" chars\n",
357                         jt_cmdname(argv[0]), argv[1], sizeof(desc.ld_uuid) - 1);
358                 return -EINVAL;
359         }
360
361         memset(&desc, 0, sizeof(desc));
362         obd_str2uuid(&desc.ld_uuid, argv[1]);
363         desc.ld_tgt_count = argc - 6;
364         desc.ld_default_stripe_count = strtoul(argv[2], &end, 0);
365         if (*end) {
366                 fprintf(stderr, "error: %s: bad default stripe count '%s'\n",
367                         jt_cmdname(argv[0]), argv[2]);
368                 return CMD_HELP;
369         }
370         if (desc.ld_default_stripe_count > desc.ld_tgt_count) {
371                 fprintf(stderr,
372                         "error: %s: default stripe count %u > OST count %u\n",
373                         jt_cmdname(argv[0]), desc.ld_default_stripe_count,
374                         desc.ld_tgt_count);
375                 return -EINVAL;
376         }
377
378         desc.ld_default_stripe_size = strtoull(argv[3], &end, 0);
379         if (*end) {
380                 fprintf(stderr, "error: %s: bad default stripe size '%s'\n",
381                         jt_cmdname(argv[0]), argv[3]);
382                 return CMD_HELP;
383         }
384         if (desc.ld_default_stripe_size < 4096) {
385                 fprintf(stderr,
386                         "error: %s: default stripe size "LPU64" too small\n",
387                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
388                 return -EINVAL;
389         } else if ((long)desc.ld_default_stripe_size <
390                    desc.ld_default_stripe_size) {
391                 fprintf(stderr,
392                         "error: %s: default stripe size "LPU64" too large\n",
393                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
394                 return -EINVAL;
395         }
396         desc.ld_default_stripe_offset = strtoull(argv[4], &end, 0);
397         if (*end) {
398                 fprintf(stderr, "error: %s: bad default stripe offset '%s'\n",
399                         jt_cmdname(argv[0]), argv[4]);
400                 return CMD_HELP;
401         }
402         desc.ld_pattern = strtoul(argv[5], &end, 0);
403         if (*end) {
404                 fprintf(stderr, "error: %s: bad stripe pattern '%s'\n",
405                         jt_cmdname(argv[0]), argv[5]);
406                 return CMD_HELP;
407         }
408
409         /* NOTE: it is possible to overwrite the default striping parameters,
410          *       but EXTREME care must be taken when saving the OST UUID list.
411          *       It must be EXACTLY the same, or have only additions at the
412          *       end of the list, or only overwrite individual OST entries
413          *       that are restored from backups of the previous OST.
414          */
415         uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
416         if (!uuidarray) {
417                 fprintf(stderr, "error: %s: no memory for %d UUIDs\n",
418                         jt_cmdname(argv[0]), desc.ld_tgt_count);
419                 rc = -ENOMEM;
420                 goto out;
421         }
422         for (i = 6, ptr = uuidarray; i < argc; i++, ptr++) {
423                 if (strlen(argv[i]) >= sizeof(*ptr)) {
424                         fprintf(stderr, "error: %s: arg %d (%s) too long\n",
425                                 jt_cmdname(argv[0]), i, argv[i]);
426                         rc = -EINVAL;
427                         goto out;
428                 }
429                 strcpy((char *)ptr, argv[i]);
430         }
431
432         lcfg.lcfg_inllen1 = sizeof(desc);
433         lcfg.lcfg_inlbuf1 = (char *)&desc;
434         lcfg.lcfg_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
435         lcfg.lcfg_inlbuf2 = (char *)uuidarray;
436
437         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
438         if (rc)
439                 fprintf(stderr, "error: %s: ioctl error: %s\n",
440                         jt_cmdname(argv[0]), strerror(rc = errno));
441 out:
442         free(uuidarray);
443         return rc;
444 }
445
446 int jt_lcfg_lmv_setup(int argc, char **argv)
447 {
448         struct lustre_cfg lcfg;
449         struct lmv_desc desc;
450         struct obd_uuid *uuidarray, *ptr;
451         int rc, i;
452
453         LCFG_INIT(lcfg, LCFG_SETUP, lcfg_devname);
454
455         if (argc <= 2)
456                 return CMD_HELP;
457
458         if (strlen(argv[1]) > sizeof(desc.ld_uuid) - 1) {
459                 fprintf(stderr,
460                         "error: %s: LMV uuid '%s' longer than "LPSZ" chars\n",
461                         jt_cmdname(argv[0]), argv[1], sizeof(desc.ld_uuid) - 1);
462                 return -EINVAL;
463         }
464
465         memset(&desc, 0, sizeof(desc));
466         obd_str2uuid(&desc.ld_uuid, argv[1]);
467         desc.ld_count = argc - 2;
468         printf("LMV: %d uuids:\n", desc.ld_count);
469
470         /* NOTE: it is possible to overwrite the default striping parameters,
471          *       but EXTREME care must be taken when saving the OST UUID list.
472          *       It must be EXACTLY the same, or have only additions at the
473          *       end of the list, or only overwrite individual OST entries
474          *       that are restored from backups of the previous OST.
475          */
476         uuidarray = calloc(desc.ld_count, sizeof(*uuidarray));
477         if (!uuidarray) {
478                 fprintf(stderr, "error: %s: no memory for %d UUIDs\n",
479                         jt_cmdname(argv[0]), desc.ld_count);
480                 rc = -ENOMEM;
481                 goto out;
482         }
483         for (i = 2, ptr = uuidarray; i < argc; i++, ptr++) {
484                 if (strlen(argv[i]) >= sizeof(*ptr)) {
485                         fprintf(stderr, "error: %s: arg %d (%s) too long\n",
486                                 jt_cmdname(argv[0]), i, argv[i]);
487                         rc = -EINVAL;
488                         goto out;
489                 }
490                 printf("  %s\n", argv[i]);
491                 strcpy((char *)ptr, argv[i]);
492         }
493
494         lcfg.lcfg_inllen1 = sizeof(desc);
495         lcfg.lcfg_inlbuf1 = (char *)&desc;
496         lcfg.lcfg_inllen2 = desc.ld_count * sizeof(*uuidarray);
497         lcfg.lcfg_inlbuf2 = (char *)uuidarray;
498
499         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
500         if (rc)
501                 fprintf(stderr, "error: %s: ioctl error: %s\n",
502                         jt_cmdname(argv[0]), strerror(rc = errno));
503 out:
504         free(uuidarray);
505         return rc;
506 }
507
508 int jt_lcfg_mount_option(int argc, char **argv)
509 {
510         int rc;
511         struct lustre_cfg lcfg;
512
513         LCFG_INIT(lcfg, LCFG_MOUNTOPT, lcfg_devname);
514
515         if (argc < 3 || argc > 4)
516                 return CMD_HELP;
517
518         /* profile name */
519         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
520         lcfg.lcfg_inlbuf1 = argv[1];
521         /* osc name */
522         lcfg.lcfg_inllen2 = strlen(argv[2]) + 1;
523         lcfg.lcfg_inlbuf2 = argv[2];
524         if (argc == 4) {
525                 /* mdc name */
526                 lcfg.lcfg_inllen3 = strlen(argv[3]) + 1;
527                 lcfg.lcfg_inlbuf3 = argv[3];
528         }
529         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
530         if (rc < 0) {
531                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
532                         strerror(rc = errno));
533         }
534
535         return rc;
536 }
537
538 int jt_lcfg_del_mount_option(int argc, char **argv)
539 {
540         int rc;
541         struct lustre_cfg lcfg;
542
543         LCFG_INIT(lcfg, LCFG_DEL_MOUNTOPT, lcfg_devname);
544
545         if (argc != 2)
546                 return CMD_HELP;
547
548         /* profile name */
549         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
550         lcfg.lcfg_inlbuf1 = argv[1];
551
552         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
553         if (rc < 0) {
554                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
555                         strerror(rc = errno));
556         }
557
558         return rc;
559 }
560
561 int jt_lcfg_set_timeout(int argc, char **argv)
562 {
563         int rc;
564         struct lustre_cfg lcfg;
565
566         LCFG_INIT(lcfg, LCFG_SET_TIMEOUT, lcfg_devname);
567
568         if (argc != 2)
569                 return CMD_HELP;
570
571         lcfg.lcfg_num = atoi(argv[1]);
572         
573         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
574         if (rc < 0) {
575                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
576                         strerror(rc = errno));
577         }
578
579         return rc;
580 }
581
582
583 int jt_lcfg_set_lustre_upcall(int argc, char **argv)
584 {
585         int rc;
586         struct lustre_cfg lcfg;
587
588         LCFG_INIT(lcfg, LCFG_SET_UPCALL, lcfg_devname);
589
590         if (argc != 2)
591                 return CMD_HELP;
592
593         /* profile name */
594         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
595         lcfg.lcfg_inlbuf1 = argv[1];
596
597         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
598         if (rc < 0) {
599                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
600                         strerror(rc = errno));
601         }
602
603         return rc;
604 }
605