1 /* vinum.c: vinum interface program */
3 * Copyright (c) 1997, 1998
4 * Nan Yang Computer Services Limited. All rights reserved.
6 * Written by Greg Lehey
8 * This software is distributed under the so-called ``Berkeley
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Company nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * This software is provided ``as is'', and any express or implied
24 * warranties, including, but not limited to, the implied warranties of
25 * merchantability and fitness for a particular purpose are disclaimed.
26 * In no event shall the company or contributors be liable for any
27 * direct, indirect, incidental, special, exemplary, or consequential
28 * damages (including, but not limited to, procurement of substitute
29 * goods or services; loss of use, data, or profits; or business
30 * interruption) however caused and on any theory of liability, whether
31 * in contract, strict liability, or tort (including negligence or
32 * otherwise) arising in any way out of the use of this software, even if
33 * advised of the possibility of such damage.
35 * $Id: v.c,v 1.31 2000/09/03 01:29:26 grog Exp grog $
36 * $FreeBSD: src/sbin/vinum/v.c,v 1.26.2.3 2001/03/13 03:04:06 grog Exp $
37 * $DragonFly: src/sbin/vinum/v.c,v 1.6 2007/07/22 22:46:09 corecode Exp $
53 #include <sys/ioctl.h>
54 #include <dev/raid/vinum/vinumhdr.h>
56 #include <sys/types.h>
58 #include <readline/readline.h>
59 #include <sys/linker.h>
60 #include <sys/module.h>
61 #include <sys/resource.h>
63 FILE *cf; /* config file handle */
64 FILE *hist; /* history file */
65 char *historyfile; /* and its name */
67 char *dateformat; /* format in which to store date */
69 char buffer[BUFSIZE]; /* buffer to read in to */
71 int line = 0; /* stdin line number for error messages */
72 int file_line = 0; /* and line in input file (yes, this is tacky) */
73 int inerror; /* set to 1 to exit after end of config file */
78 int debug = 0; /* debug flag, usage varies */
80 int force = 0; /* set to 1 to force some dangerous ops */
81 int interval = 0; /* interval in ms between init/revive */
82 int vflag = 0; /* set verbose operation or verify */
83 int Verbose = 0; /* set very verbose operation */
84 int recurse = 0; /* set recursion */
85 int sflag = 0; /* show statistics */
86 int SSize = 0; /* sector size for revive */
87 int dowait = 0; /* wait for completion */
88 char *objectname; /* name to be passed for -n flag */
90 /* Structures to read kernel data into */
91 struct _vinum_conf vinum_conf; /* configuration information */
98 jmp_buf command_fail; /* return on a failed command */
99 int superdev; /* vinum super device */
101 void start_daemon(void);
103 #define ofs(x) ((void *) (& ((struct confdata *) 0)->x)) /* offset of x in struct confdata */
105 char *token[MAXARGS]; /* pointers to individual tokens */
106 int tokens; /* number of tokens */
109 main(int argc, char *argv[], char *envp[])
111 struct stat histstat;
113 if (modfind(VINUMMOD) < 0) {
114 /* need to load the vinum module */
115 if (kldload(VINUMMOD) < 0 || modfind(VINUMMOD) < 0) {
116 perror(VINUMMOD ": Kernel module not available");
120 dateformat = getenv("VINUM_DATEFORMAT");
121 if (dateformat == NULL)
122 dateformat = "%e %b %Y %H:%M:%S";
123 historyfile = getenv("VINUM_HISTORY");
124 if (historyfile == NULL)
125 historyfile = DEFAULT_HISTORYFILE;
126 if (stat(historyfile, &histstat) == 0) { /* history file exists */
127 if ((histstat.st_mode & S_IFMT) != S_IFREG) {
129 "Vinum history file %s must be a regular file\n",
133 } else if ((errno != ENOENT) /* not "not there", */
134 &&(errno != EROFS)) { /* and not read-only file system */
136 "Can't open %s: %s (%d)\n",
142 hist = fopen(historyfile, "a+");
145 fprintf(hist, "*** " VINUMMOD " started ***\n");
146 fflush(hist); /* before we start the daemon */
148 superdev = open(VINUM_SUPERDEV_NAME, O_RDWR); /* open vinum superdevice */
149 if (superdev < 0) { /* no go */
150 if (errno == ENODEV) { /* not configured, */
151 superdev = open(VINUM_WRONGSUPERDEV_NAME, O_RDWR); /* do we have a debug mismatch? */
152 if (superdev >= 0) { /* yup! */
155 "This program is compiled with debug support, but the kernel module does\n"
156 "not have debug support. This program must be matched with the kernel\n"
157 "module. Please alter /usr/src/sbin/" VINUMMOD "/Makefile and remove\n"
158 "the option -DVINUMDEBUG from the CFLAGS definition, or alternatively\n"
159 "edit /usr/src/sys/modules/" VINUMMOD "/Makefile and add the option\n"
160 "-DVINUMDEBUG to the CFLAGS definition. Then rebuild the component\n"
161 "of your choice with 'make clean all install'. If you rebuild the kernel\n"
162 "module, you must stop " VINUMMOD " and restart it\n");
165 "This program is compiled without debug support, but the kernel module\n"
166 "includes debug support. This program must be matched with the kernel\n"
167 "module. Please alter /usr/src/sbin/" VINUMMOD "/Makefile and add\n"
168 "the option -DVINUMDEBUG to the CFLAGS definition, or alternatively\n"
169 "edit /usr/src/sys/modules/" VINUMMOD "/Makefile and remove the option\n"
170 "-DVINUMDEBUG from the CFLAGS definition. Then rebuild the component\n"
171 "of your choice with 'make clean all install'. If you rebuild the kernel\n"
172 "module, you must stop " VINUMMOD " and restart it\n");
176 } else if (errno == ENOENT) /* we don't have our node, */
177 make_devices(); /* create them first */
179 perror("Can't open " VINUM_SUPERDEV_NAME);
183 /* Check if the dæmon is running. If not, start it in the
187 if (argc > 1) { /* we have a command on the line */
188 if (setjmp(command_fail) != 0) /* long jumped out */
190 parseline(argc - 1, &argv[1]); /* do it */
193 * Catch a possible race condition which could cause us to
194 * longjmp() into nowhere if we receive a SIGINT in the next few
197 if (setjmp(command_fail)) /* come back here on catastrophic failure */
199 setsigs(); /* set signal handler */
202 int childstatus; /* from wait4 */
204 if (setjmp(command_fail) == 2) /* come back here on catastrophic failure */
205 fprintf(stderr, "*** interrupted ***\n"); /* interrupted */
207 while (wait4(-1, &childstatus, WNOHANG, NULL) > 0); /* wait for all dead children */
208 c = readline(VINUMMOD " -> "); /* get an input */
209 if (c == NULL) { /* EOF or error */
211 fprintf(stderr, "Can't read input: %s (%d)\n", strerror(errno), errno);
217 } else if (*c) { /* got something there */
218 add_history(c); /* save it in the history */
219 strcpy(buffer, c); /* put it where we can munge it */
221 line++; /* count the lines */
222 tokens = tokenize(buffer, token);
223 /* got something potentially worth parsing */
225 parseline(tokens, token); /* and do what he says */
231 return 0; /* normal completion */
234 /* stop the hard way */
236 vinum_quit(int argc, char *argv[], char *argv0[])
241 /* Set action on receiving a SIGINT */
245 struct sigaction act;
247 act.sa_handler = catchsig;
249 sigemptyset(&act.sa_mask);
250 sigaction(SIGINT, &act, NULL);
256 longjmp(command_fail, 2);
259 #define FUNKEY(x) { kw_##x, &vinum_##x } /* create pair "kw_foo", vinum_foo */
260 #define vinum_move vinum_mv /* synonym for 'mv' */
264 void (*fun) (int argc, char *argv[], char *arg0[]);
307 FUNKEY(rebuildparity),
311 /* Take args arguments at argv and attempt to perform the operation specified */
313 parseline(int args, char *argv[])
317 enum keyword command; /* command to execute */
319 if (hist != NULL) { /* save the command to history file */
321 for (i = 0; i < args; i++) /* all args */
322 fprintf(hist, "%s ", argv[i]);
325 if ((args == 0) /* empty line */
326 ||(*argv[0] == '#')) /* or a comment, */
328 if (args == MAXARGS) { /* too many arguments, */
329 fprintf(stderr, "Too many arguments to %s, this can't be right\n", argv[0]);
332 command = get_keyword(argv[0], &keyword_set);
333 dowait = 0; /* initialize flags */
334 force = 0; /* initialize flags */
335 vflag = 0; /* initialize flags */
336 Verbose = 0; /* initialize flags */
337 recurse = 0; /* initialize flags */
338 sflag = 0; /* initialize flags */
339 objectname = NULL; /* no name yet */
342 * first handle generic options
343 * We don't use getopt(3) because
344 * getopt doesn't allow merging flags
345 * (for example, -fr).
347 for (i = 1; (i < args) && (argv[i][0] == '-'); i++) { /* while we have flags */
348 for (j = 1; j < strlen(argv[i]); j++)
349 switch (argv[i][j]) {
351 case 'd': /* -d: debug */
356 case 'f': /* -f: force */
360 case 'i': /* interval */
362 if (argv[i][j + 1] != '\0') /* operand follows, */
363 interval = atoi(&argv[i][j + 1]); /* use it */
364 else if (args > (i + 1)) /* another following, */
365 interval = atoi(argv[++i]); /* use it */
366 if (interval == 0) /* nothing valid, */
367 fprintf(stderr, "-i: no interval specified\n");
370 case 'n': /* -n: get name */
371 if (i == args - 1) { /* last arg */
372 fprintf(stderr, "-n requires a name parameter\n");
375 objectname = argv[++i]; /* pick it up */
376 j = strlen(argv[i]); /* skip the next parm */
379 case 'r': /* -r: recurse */
383 case 's': /* -s: show statistics */
389 if (argv[i][j + 1] != '\0') /* operand follows, */
390 SSize = atoi(&argv[i][j + 1]); /* use it */
391 else if (args > (i + 1)) /* another following, */
392 SSize = atoi(argv[++i]); /* use it */
393 if (SSize == 0) /* nothing valid, */
394 fprintf(stderr, "-S: no size specified\n");
397 case 'v': /* -v: verbose */
401 case 'V': /* -V: Very verbose */
406 case 'w': /* -w: wait for completion */
411 fprintf(stderr, "Invalid flag: %s\n", argv[i]);
415 /* Pass what we have left to the command to handle it */
416 for (j = 0; j < (sizeof(funkeys) / sizeof(struct funkey)); j++) {
417 if (funkeys[j].kw == command) { /* found the command */
418 funkeys[j].fun(args - i, &argv[i], &argv[0]);
422 fprintf(stderr, "Unknown command: %s\n", argv[0]);
426 get_drive_info(struct drive *drive, int index)
428 *(int *) drive = index; /* put in drive to hand to driver */
429 if (ioctl(superdev, VINUM_DRIVECONFIG, drive) < 0) {
431 "Can't get config for drive %d: %s\n",
434 longjmp(command_fail, -1);
439 get_sd_info(struct sd *sd, int index)
441 *(int *) sd = index; /* put in sd to hand to driver */
442 if (ioctl(superdev, VINUM_SDCONFIG, sd) < 0) {
444 "Can't get config for subdisk %d: %s\n",
447 longjmp(command_fail, -1);
451 /* Get the contents of the sd entry for subdisk <sdno>
452 * of the specified plex. */
454 get_plex_sd_info(struct sd *sd, int plexno, int sdno)
456 ((int *) sd)[0] = plexno;
457 ((int *) sd)[1] = sdno; /* pass parameters */
458 if (ioctl(superdev, VINUM_PLEXSDCONFIG, sd) < 0) {
460 "Can't get config for subdisk %d (part of plex %d): %s\n",
464 longjmp(command_fail, -1);
469 get_plex_info(struct plex *plex, int index)
471 *(int *) plex = index; /* put in plex to hand to driver */
472 if (ioctl(superdev, VINUM_PLEXCONFIG, plex) < 0) {
474 "Can't get config for plex %d: %s\n",
477 longjmp(command_fail, -1);
482 get_volume_info(struct volume *volume, int index)
484 *(int *) volume = index; /* put in volume to hand to driver */
485 if (ioctl(superdev, VINUM_VOLCONFIG, volume) < 0) {
487 "Can't get config for volume %d: %s\n",
490 longjmp(command_fail, -1);
495 find_drive_by_devname(char *name)
499 struct drive *drivep = NULL;
501 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
502 perror("Can't get vinum config");
505 devpath = getdevpath(name, 0);
506 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
507 get_drive_info(&drive, driveno);
508 if (drive.state == drive_unallocated)
510 if (strcmp(drive.devicename, name) == 0) {
514 if (strcmp(drive.devicename, devpath) == 0) {
524 * Create the device nodes for vinum objects
526 * XXX - Obsolete, vinum kernel module now creates is own devices.
540 fprintf(hist, "*** Created devices ***\n");
544 system("rm -rf " VINUM_DIR); /* remove the old directories */
545 system("mkdir -p " VINUM_DIR "/drive " /* and make them again */
550 if (mknod(VINUM_SUPERDEV_NAME,
551 S_IRUSR | S_IWUSR | S_IFCHR, /* user only */
552 makedev(VINUM_CDEV_MAJOR, VINUM_SUPERDEV)) < 0)
553 fprintf(stderr, "Can't create %s: %s\n", VINUM_SUPERDEV_NAME, strerror(errno));
555 if (mknod(VINUM_WRONGSUPERDEV_NAME,
556 S_IRUSR | S_IWUSR | S_IFCHR, /* user only */
557 makedev(VINUM_CDEV_MAJOR, VINUM_WRONGSUPERDEV)) < 0)
558 fprintf(stderr, "Can't create %s: %s\n", VINUM_WRONGSUPERDEV_NAME, strerror(errno));
560 superdev = open(VINUM_SUPERDEV_NAME, O_RDWR); /* open the super device */
562 if (mknod(VINUM_DAEMON_DEV_NAME, /* daemon super device */
563 S_IRUSR | S_IWUSR | S_IFCHR, /* user only */
564 makedev(VINUM_CDEV_MAJOR, VINUM_DAEMON_DEV)) < 0)
565 fprintf(stderr, "Can't create %s: %s\n", VINUM_DAEMON_DEV_NAME, strerror(errno));
567 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
568 perror("Can't get vinum config");
572 for (volno = 0; volno < vinum_conf.volumes_allocated; volno++)
573 make_vol_dev(volno, 0);
575 for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++)
576 make_plex_dev(plexno, 0);
578 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++)
580 /* Drives. Do this later (both logical and physical names) XXX */
581 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
582 char filename[PATH_MAX]; /* for forming file names */
584 get_drive_info(&drive, driveno);
585 if (drive.state > drive_referenced) {
586 sprintf(filename, "ln -s %s " VINUM_DIR "/drive/%s", drive.devicename, drive.label.name);
595 /* make the devices for a volume */
597 make_vol_dev(int volno, int recurse)
600 char filename[PATH_MAX]; /* for forming file names */
603 get_volume_info(&vol, volno);
604 if (vol.state != volume_unallocated) { /* we could have holes in our lists */
605 voldev = VINUMDEV(volno, 0, 0, VINUM_VOLUME_TYPE); /* create a device number */
607 /* Create /dev/vinum/<myvol> */
608 sprintf(filename, VINUM_DIR "/%s", vol.name);
609 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, voldev) < 0)
610 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
612 /* Create /dev/vinum/vol/<myvol> */
613 sprintf(filename, VINUM_DIR "/vol/%s", vol.name);
614 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, voldev) < 0)
615 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
617 if (vol.plexes > 0) {
618 /* Create /dev/vinum/vol/<myvol>.plex/ */
619 sprintf(filename, VINUM_DIR "/vol/%s.plex", vol.name);
620 if (mkdir(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IXOTH) < 0)
621 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
624 for (plexno = 0; plexno < vol.plexes; plexno++)
625 make_plex_dev(plex.plexno, recurse);
630 * Create device entries for the plexes in
631 * /dev/vinum/<vol>.plex/ and /dev/vinum/plex.
634 make_plex_dev(int plexno, int recurse)
636 dev_t plexdev; /* device */
637 char filename[PATH_MAX]; /* for forming file names */
640 get_plex_info(&plex, plexno);
641 if (plex.state != plex_unallocated) {
642 plexdev = VINUM_PLEX(plexno);
644 /* /dev/vinum/plex/<plex> */
645 sprintf(filename, VINUM_DIR "/plex/%s", plex.name);
646 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, plexdev) < 0)
647 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
649 if (plex.volno >= 0) {
650 get_volume_info(&vol, plex.volno);
651 plexdev = VINUMDEV(plex.volno, plexno, 0, VINUM_PLEX_TYPE);
653 /* Create device /dev/vinum/vol/<vol>.plex/<plex> */
654 sprintf(filename, VINUM_DIR "/vol/%s.plex/%s", vol.name, plex.name);
655 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, plexdev) < 0)
656 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
658 /* Create directory /dev/vinum/vol/<vol>.plex/<plex>.sd */
659 sprintf(filename, VINUM_DIR "/vol/%s.plex/%s.sd", vol.name, plex.name);
660 if (mkdir(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IXOTH) < 0)
661 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
664 for (sdno = 0; sdno < plex.subdisks; sdno++) {
665 get_plex_sd_info(&sd, plex.plexno, sdno);
666 make_sd_dev(sd.sdno);
672 /* Create the contents of /dev/vinum/sd and /dev/vinum/rsd */
674 make_sd_dev(int sdno)
676 dev_t sddev; /* device */
677 char filename[PATH_MAX]; /* for forming file names */
679 get_sd_info(&sd, sdno);
680 if (sd.state != sd_unallocated) {
681 sddev = VINUM_SD(sdno);
683 /* /dev/vinum/sd/<sd> */
684 sprintf(filename, VINUM_DIR "/sd/%s", sd.name);
685 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, sddev) < 0)
686 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
692 /* command line interface for the 'makedev' command */
694 vinum_makedev(int argc, char *argv[], char *arg0[])
700 * Find the object "name". Return object type at type,
701 * and the index as the return value.
702 * If not found, return -1 and invalid_object.
705 find_object(const char *name, enum objecttype *type)
709 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
710 perror("Can't get vinum config");
711 *type = invalid_object;
714 /* Search the drive table */
715 for (object = 0; object < vinum_conf.drives_allocated; object++) {
716 get_drive_info(&drive, object);
717 if (strcmp(name, drive.label.name) == 0) {
718 *type = drive_object;
723 /* Search the subdisk table */
724 for (object = 0; object < vinum_conf.subdisks_allocated; object++) {
725 get_sd_info(&sd, object);
726 if (strcmp(name, sd.name) == 0) {
732 /* Search the plex table */
733 for (object = 0; object < vinum_conf.plexes_allocated; object++) {
734 get_plex_info(&plex, object);
735 if (strcmp(name, plex.name) == 0) {
741 /* Search the volume table */
742 for (object = 0; object < vinum_conf.volumes_allocated; object++) {
743 get_volume_info(&vol, object);
744 if (strcmp(name, vol.name) == 0) {
745 *type = volume_object;
750 /* Didn't find the name: invalid */
751 *type = invalid_object;
755 /* Continue reviving a subdisk in the background */
757 continue_revive(int sdno)
761 get_sd_info(&sd, sdno);
764 pid = fork(); /* do this in the background */
767 if (pid == 0) { /* we're the child */
768 struct _ioctl_reply reply;
769 struct vinum_ioctl_msg *message = (struct vinum_ioctl_msg *) &reply;
771 openlog(VINUMMOD, LOG_CONS | LOG_PERROR | LOG_PID, LOG_KERN);
772 syslog(LOG_INFO | LOG_KERN, "reviving %s", sd.name);
773 setproctitle("reviving %s", sd.name);
775 for (reply.error = EAGAIN; reply.error == EAGAIN;) { /* revive the subdisk */
777 usleep(interval * 1000); /* pause between each copy */
778 message->index = sdno; /* pass sd number */
779 message->type = sd_object; /* and type of object */
780 message->state = object_up;
781 if (SSize != 0) { /* specified a size for init */
783 SSize <<= DEV_BSHIFT;
784 message->blocksize = SSize;
786 message->blocksize = DEFAULT_REVIVE_BLOCKSIZE;
787 ioctl(superdev, VINUM_SETSTATE, message);
790 syslog(LOG_ERR | LOG_KERN,
791 "can't revive %s: %s",
793 reply.msg[0] ? reply.msg : strerror(reply.error));
797 get_sd_info(&sd, sdno); /* update the info */
798 syslog(LOG_INFO | LOG_KERN, "%s is %s", sd.name, sd_state(sd.state));
802 } else if (pid < 0) /* couldn't fork? */
803 fprintf(stderr, "Can't continue reviving %s: %s\n", sd.name, strerror(errno));
805 printf("Reviving %s in the background\n", sd.name);
809 * Check if the daemon is running,
810 * start it if it isn't. The check itself
811 * could take a while, so we do it as a separate
812 * process, which will become the daemon if one isn't
824 if (pid == 0) { /* We're the child, do the work */
826 * We have a problem when stopping the subsystem:
827 * The only way to know that we're idle is when
828 * all open superdevs close. But we want the
829 * daemon to clean up for us, and since we can't
830 * count the opens, we need to have the main device
831 * closed when we stop. We solve this conundrum
832 * by getting the daemon to open a separate device.
834 close(superdev); /* this is the wrong device */
835 superdev = open(VINUM_DAEMON_DEV_NAME, O_RDWR); /* open deamon superdevice */
837 perror("Can't open " VINUM_DAEMON_DEV_NAME);
840 error = daemon(0, 0); /* this will fork again, but who's counting? */
842 fprintf(stderr, "Can't start daemon: %s (%d)\n", strerror(errno), errno);
845 setproctitle(VINUMMOD " daemon"); /* show what we're doing */
846 status = ioctl(superdev, VINUM_FINDDAEMON, NULL);
847 if (status != 0) { /* no daemon, */
848 ioctl(superdev, VINUM_DAEMON, &vflag); /* we should hang here */
849 syslog(LOG_ERR | LOG_KERN, "%s", strerror(errno));
852 exit(0); /* when told to die */
853 } else if (pid < 0) /* couldn't fork */
854 printf("Can't fork to check daemon\n");
862 char datetext[MAXDATETEXT];
866 if (gettimeofday(&now, NULL) != 0) {
867 fprintf(stderr, "Can't get time: %s\n", strerror(errno));
871 date = localtime(&sec);
872 strftime(datetext, MAXDATETEXT, dateformat, date),