Whamcloud - gitweb
landing smfs.
[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 > 5)
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         
174         if (argc > 4) {
175                 lcfg.lcfg_inllen4 = strlen(argv[4]) + 1;
176                 lcfg.lcfg_inlbuf4 = argv[4];
177         }
178
179         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
180         if (rc < 0)
181                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
182                         strerror(rc = errno));
183
184         return rc;
185 }
186
187 int jt_obd_detach(int argc, char **argv)
188 {
189         struct lustre_cfg lcfg;
190         int rc;
191
192         if (lcfg_devname == NULL) {
193                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
194                         "device name for config commands.\n", 
195                         jt_cmdname(argv[0])); 
196                 return -EINVAL;
197         }
198
199         LCFG_INIT(lcfg, LCFG_DETACH, lcfg_devname);
200
201         if (argc != 1)
202                 return CMD_HELP;
203
204         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
205         if (rc < 0)
206                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
207                         strerror(rc = errno));
208
209         return rc;
210 }
211
212 int jt_obd_cleanup(int argc, char **argv)
213 {
214         struct lustre_cfg lcfg;
215         char force = 'F';
216         char failover = 'A';
217         char flags[3];
218         int flag_cnt = 0, n;
219         int rc;
220
221         if (lcfg_devname == NULL) {
222                 fprintf(stderr, "%s: please use 'cfg_device name' to set the "
223                         "device name for config commands.\n", 
224                         jt_cmdname(argv[0])); 
225                 return -EINVAL;
226         }
227
228         LCFG_INIT(lcfg, LCFG_CLEANUP, lcfg_devname);
229
230         if (argc < 1 || argc > 3)
231                 return CMD_HELP;
232
233         for (n = 1; n < argc; n++) 
234                 if (strcmp(argv[n], "force") == 0) {
235                         flags[flag_cnt++] = force;
236                 } else if (strcmp(argv[n], "failover") == 0) {
237                         flags[flag_cnt++] = failover;
238                 } else {
239                         fprintf(stderr, "unknown option: %s", argv[n]);
240                         return CMD_HELP;
241                 }
242
243         lcfg.lcfg_inllen1 = flag_cnt;
244         if (flag_cnt)
245                 lcfg.lcfg_inlbuf1 = flags;
246
247         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &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 lcfg;
261
262         LCFG_INIT(lcfg, LCFG_ADD_UUID, lcfg_devname);
263         lcfg.lcfg_nid = nid;
264         lcfg.lcfg_inllen1 = strlen(uuid) + 1;
265         lcfg.lcfg_inlbuf1 = uuid;
266         lcfg.lcfg_nal = nal;
267
268         rc = lcfg_ioctl(func, OBD_DEV_ID, &lcfg);
269         if (rc) {
270                 fprintf(stderr, "IOC_PORTAL_ADD_UUID failed: %s\n",
271                         strerror(errno));
272                 return -1;
273         }
274
275         printf ("Added uuid %s: %s\n", uuid, ptl_nid2str (tmp, nid));
276         return 0;
277 }
278
279 int jt_lcfg_add_uuid(int argc, char **argv)
280 {
281         ptl_nid_t nid = 0;
282         int nal;
283         
284         if (argc != 4) {                
285                 return CMD_HELP;
286         }
287
288         if (ptl_parse_nid (&nid, argv[2]) != 0) {
289                 fprintf (stderr, "Can't parse NID %s\n", argv[2]);
290                         return (-1);
291         }
292
293         nal = ptl_name2nal(argv[3]);
294
295         if (nal <= 0) {
296                 fprintf (stderr, "Can't parse NAL %s\n", argv[3]);
297                 return -1;
298         }
299
300         return do_add_uuid(argv[0], argv[1], nid, nal);
301 }
302
303 int obd_add_uuid(char *uuid, ptl_nid_t nid, int nal)
304 {
305         return do_add_uuid("obd_add_uuid", uuid, nid, nal);
306 }
307
308 int jt_lcfg_del_uuid(int argc, char **argv)
309 {
310         int rc;
311         struct lustre_cfg lcfg;
312
313         if (argc != 2) {
314                 fprintf(stderr, "usage: %s <uuid>\n", argv[0]);
315                 return 0;
316         }
317
318         LCFG_INIT(lcfg, LCFG_DEL_UUID, lcfg_devname);
319
320         if (strcmp (argv[1], "_all_"))
321         {
322                 lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
323                 lcfg.lcfg_inlbuf1 = argv[1];
324         }
325         
326         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
327         if (rc) {
328                 fprintf(stderr, "IOC_PORTAL_DEL_UUID failed: %s\n",
329                         strerror(errno));
330                 return -1;
331         }
332         return 0;
333 }
334
335 int jt_lcfg_lov_setup(int argc, char **argv)
336 {
337         struct lustre_cfg lcfg;
338         struct lov_desc desc;
339         struct obd_uuid *uuidarray, *ptr;
340         int rc, i;
341         char *end;
342
343         LCFG_INIT(lcfg, LCFG_SETUP, lcfg_devname);
344
345         if (argc <= 6)
346                 return CMD_HELP;
347
348         if (strlen(argv[1]) > sizeof(desc.ld_uuid) - 1) {
349                 fprintf(stderr,
350                         "error: %s: LOV uuid '%s' longer than "LPSZ" chars\n",
351                         jt_cmdname(argv[0]), argv[1], sizeof(desc.ld_uuid) - 1);
352                 return -EINVAL;
353         }
354
355         memset(&desc, 0, sizeof(desc));
356         obd_str2uuid(&desc.ld_uuid, argv[1]);
357         desc.ld_tgt_count = argc - 6;
358         desc.ld_default_stripe_count = strtoul(argv[2], &end, 0);
359         if (*end) {
360                 fprintf(stderr, "error: %s: bad default stripe count '%s'\n",
361                         jt_cmdname(argv[0]), argv[2]);
362                 return CMD_HELP;
363         }
364         if (desc.ld_default_stripe_count > desc.ld_tgt_count) {
365                 fprintf(stderr,
366                         "error: %s: default stripe count %u > OST count %u\n",
367                         jt_cmdname(argv[0]), desc.ld_default_stripe_count,
368                         desc.ld_tgt_count);
369                 return -EINVAL;
370         }
371
372         desc.ld_default_stripe_size = strtoull(argv[3], &end, 0);
373         if (*end) {
374                 fprintf(stderr, "error: %s: bad default stripe size '%s'\n",
375                         jt_cmdname(argv[0]), argv[3]);
376                 return CMD_HELP;
377         }
378         if (desc.ld_default_stripe_size < 4096) {
379                 fprintf(stderr,
380                         "error: %s: default stripe size "LPU64" too small\n",
381                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
382                 return -EINVAL;
383         } else if ((long)desc.ld_default_stripe_size <
384                    desc.ld_default_stripe_size) {
385                 fprintf(stderr,
386                         "error: %s: default stripe size "LPU64" too large\n",
387                         jt_cmdname(argv[0]), desc.ld_default_stripe_size);
388                 return -EINVAL;
389         }
390         desc.ld_default_stripe_offset = strtoull(argv[4], &end, 0);
391         if (*end) {
392                 fprintf(stderr, "error: %s: bad default stripe offset '%s'\n",
393                         jt_cmdname(argv[0]), argv[4]);
394                 return CMD_HELP;
395         }
396         desc.ld_pattern = strtoul(argv[5], &end, 0);
397         if (*end) {
398                 fprintf(stderr, "error: %s: bad stripe pattern '%s'\n",
399                         jt_cmdname(argv[0]), argv[5]);
400                 return CMD_HELP;
401         }
402
403         /* NOTE: it is possible to overwrite the default striping parameters,
404          *       but EXTREME care must be taken when saving the OST UUID list.
405          *       It must be EXACTLY the same, or have only additions at the
406          *       end of the list, or only overwrite individual OST entries
407          *       that are restored from backups of the previous OST.
408          */
409         uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
410         if (!uuidarray) {
411                 fprintf(stderr, "error: %s: no memory for %d UUIDs\n",
412                         jt_cmdname(argv[0]), desc.ld_tgt_count);
413                 rc = -ENOMEM;
414                 goto out;
415         }
416         for (i = 6, ptr = uuidarray; i < argc; i++, ptr++) {
417                 if (strlen(argv[i]) >= sizeof(*ptr)) {
418                         fprintf(stderr, "error: %s: arg %d (%s) too long\n",
419                                 jt_cmdname(argv[0]), i, argv[i]);
420                         rc = -EINVAL;
421                         goto out;
422                 }
423                 strcpy((char *)ptr, argv[i]);
424         }
425
426         lcfg.lcfg_inllen1 = sizeof(desc);
427         lcfg.lcfg_inlbuf1 = (char *)&desc;
428         lcfg.lcfg_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
429         lcfg.lcfg_inlbuf2 = (char *)uuidarray;
430
431         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
432         if (rc)
433                 fprintf(stderr, "error: %s: ioctl error: %s\n",
434                         jt_cmdname(argv[0]), strerror(rc = errno));
435 out:
436         free(uuidarray);
437         return rc;
438 }
439
440 int jt_lcfg_mount_option(int argc, char **argv)
441 {
442         int rc;
443         struct lustre_cfg lcfg;
444
445         LCFG_INIT(lcfg, LCFG_MOUNTOPT, lcfg_devname);
446
447         if (argc < 3 || argc > 4)
448                 return CMD_HELP;
449
450         /* profile name */
451         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
452         lcfg.lcfg_inlbuf1 = argv[1];
453         /* osc name */
454         lcfg.lcfg_inllen2 = strlen(argv[2]) + 1;
455         lcfg.lcfg_inlbuf2 = argv[2];
456         if (argc == 4) {
457                 /* mdc name */
458                 lcfg.lcfg_inllen3 = strlen(argv[3]) + 1;
459                 lcfg.lcfg_inlbuf3 = argv[3];
460         }
461         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
462         if (rc < 0) {
463                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
464                         strerror(rc = errno));
465         }
466
467         return rc;
468 }
469
470 int jt_lcfg_del_mount_option(int argc, char **argv)
471 {
472         int rc;
473         struct lustre_cfg lcfg;
474
475         LCFG_INIT(lcfg, LCFG_DEL_MOUNTOPT, lcfg_devname);
476
477         if (argc != 2)
478                 return CMD_HELP;
479
480         /* profile name */
481         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
482         lcfg.lcfg_inlbuf1 = argv[1];
483
484         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
485         if (rc < 0) {
486                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
487                         strerror(rc = errno));
488         }
489
490         return rc;
491 }
492
493 int jt_lcfg_set_timeout(int argc, char **argv)
494 {
495         int rc;
496         struct lustre_cfg lcfg;
497
498         LCFG_INIT(lcfg, LCFG_SET_TIMEOUT, lcfg_devname);
499
500         if (argc != 2)
501                 return CMD_HELP;
502
503         lcfg.lcfg_num = atoi(argv[1]);
504         
505         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, &lcfg);
506         if (rc < 0) {
507                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
508                         strerror(rc = errno));
509         }
510
511         return rc;
512 }
513
514
515 int jt_lcfg_set_lustre_upcall(int argc, char **argv)
516 {
517         int rc;
518         struct lustre_cfg lcfg;
519
520         LCFG_INIT(lcfg, LCFG_SET_UPCALL, lcfg_devname);
521
522         if (argc != 2)
523                 return CMD_HELP;
524
525         /* profile name */
526         lcfg.lcfg_inllen1 = strlen(argv[1]) + 1;
527         lcfg.lcfg_inlbuf1 = argv[1];
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