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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Nan Yang Computer
23 * 4. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * This software is provided ``as is'', and any express or implied
28 * warranties, including, but not limited to, the implied warranties of
29 * merchantability and fitness for a particular purpose are disclaimed.
30 * In no event shall the company or contributors be liable for any
31 * direct, indirect, incidental, special, exemplary, or consequential
32 * damages (including, but not limited to, procurement of substitute
33 * goods or services; loss of use, data, or profits; or business
34 * interruption) however caused and on any theory of liability, whether
35 * in contract, strict liability, or tort (including negligence or
36 * otherwise) arising in any way out of the use of this software, even if
37 * advised of the possibility of such damage.
39 * $Id: v.c,v 1.31 2000/09/03 01:29:26 grog Exp grog $
56 #include <sys/ioctl.h>
58 #include <sys/types.h>
60 #include <readline/history.h>
61 #include <readline/readline.h>
62 #include <sys/linker.h>
63 #include <sys/module.h>
64 #include <sys/resource.h>
65 #include <sys/sysctl.h>
67 FILE *cf; /* config file handle */
68 FILE *history; /* history file */
69 char *historyfile; /* and its name */
71 char *dateformat; /* format in which to store date */
73 char buffer[BUFSIZE]; /* buffer to read in to */
75 int line = 0; /* stdin line number for error messages */
76 int file_line = 0; /* and line in input file (yes, this is tacky) */
77 int inerror; /* set to 1 to exit after end of config file */
81 int debug = 0; /* debug flag, usage varies */
82 int force = 0; /* set to 1 to force some dangerous ops */
83 int interval = 0; /* interval in ms between init/revive */
84 int vflag = 0; /* set verbose operation or verify */
85 int Verbose = 0; /* set very verbose operation */
86 int recurse = 0; /* set recursion */
87 int sflag = 0; /* show statistics */
88 int SSize = 0; /* sector size for revive */
89 int dowait = 0; /* wait for completion */
90 char *objectname; /* name to be passed for -n flag */
93 * Structures to read kernel data into. These are shortened versions
94 * of the kernel data structures, without the bits and pieces we
97 struct __vinum_conf vinum_conf; /* configuration information */
103 jmp_buf command_fail; /* return on a failed command */
104 int superdev; /* vinum super device */
105 int no_devfs = 1; /* set if we have no devfs active */
107 void start_daemon(void);
109 #define ofs(x) ((void *) (& ((struct confdata *) 0)->x)) /* offset of x in struct confdata */
111 char *token[MAXARGS]; /* pointers to individual tokens */
112 int tokens; /* number of tokens */
115 main(int argc, char *argv[], char *envp[])
117 struct stat histstat;
119 if (modfind(VINUMMOD) < 0) {
120 /* need to load the vinum module */
121 if (kldload(VINUMMOD) < 0 || modfind(VINUMMOD) < 0) {
122 perror(VINUMMOD ": Kernel module not available");
126 dateformat = getenv("VINUM_DATEFORMAT");
127 if (dateformat == NULL)
128 dateformat = "%e %b %Y %H:%M:%S";
129 historyfile = getenv("VINUM_HISTORY");
130 if (historyfile == NULL)
131 historyfile = DEFAULT_HISTORYFILE;
132 if (stat(historyfile, &histstat) == 0) { /* history file exists */
133 if ((histstat.st_mode & S_IFMT) != S_IFREG) {
135 "Vinum history file %s must be a regular file\n",
139 } else if ((errno != ENOENT) /* not "not there", */
140 &&(errno != EROFS)) { /* and not read-only file system */
142 "Can't open %s: %s (%d)\n",
148 history = fopen(historyfile, "a+");
149 if (history != NULL) {
151 fprintf(history, "*** " VINUMMOD " started ***\n");
152 fflush(history); /* before we start the daemon */
154 if (sysctlbyname("vfs.devfs.generation", NULL, NULL, NULL, 0) == 0)
156 superdev = open(VINUM_SUPERDEV_NAME, O_RDWR); /* open vinum superdevice */
157 if (superdev < 0) { /* no go */
158 if ((errno == ENOENT) && no_devfs) /* we don't have our node, */
159 make_devices(); /* create them first */
161 perror("Can't open " VINUM_SUPERDEV_NAME);
166 * Check that we match the kernel version. There are a number of
167 * possibilities here:
169 * 0: The versions are OK.
170 * 1: The kernel module could be a pre-version 1 module, which
171 * doesn't include this check. In that case, vinum_conf will be too
172 * short, and so we'll get an EINVAL back when trying to get it. In
173 * this case we'll fake a 0 in the version.
174 * 2: The module versions are different. Print appropriate messages
177 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
178 if (errno == EINVAL) /* wrong length, */
179 vinum_conf.version = 0; /* must be the old version */
181 perror("Can't get vinum config");
185 if (vinum_conf.version != VINUMVERSION) {
187 "Version mismatch. The kernel module is version %d of Vinum,\n"
188 "but this program is designed for version %d\n",
191 if (vinum_conf.version < VINUMVERSION)
192 fprintf(stderr, "Please upgrade your kernel module.\n");
194 fprintf(stderr, "Please upgrade vinum(8).\n");
197 /* Check if the dæmon is running. If not, start it in the
201 if (argc > 1) { /* we have a command on the line */
202 if (setjmp(command_fail) != 0) /* long jumped out */
204 parseline(argc - 1, &argv[1]); /* do it */
207 * Catch a possible race condition which could cause us to
208 * longjmp() into nowhere if we receive a SIGINT in the next few
211 if (setjmp(command_fail)) /* come back here on catastrophic failure */
213 setsigs(); /* set signal handler */
216 int childstatus; /* from wait4 */
218 if (setjmp(command_fail) == 2) /* come back here on catastrophic failure */
219 fprintf(stderr, "*** interrupted ***\n"); /* interrupted */
221 while (wait4(-1, &childstatus, WNOHANG, NULL) > 0); /* wait for all dead children */
222 c = readline(VINUMMOD " -> "); /* get an input */
223 if (c == NULL) { /* EOF or error */
225 fprintf(stderr, "Can't read input: %s (%d)\n", strerror(errno), errno);
231 } else if (*c) { /* got something there */
232 add_history(c); /* save it in the history */
233 strcpy(buffer, c); /* put it where we can munge it */
235 line++; /* count the lines */
236 tokens = tokenize(buffer, token, MAXARGS);
237 /* got something potentially worth parsing */
239 parseline(tokens, token); /* and do what he says */
245 return 0; /* normal completion */
248 /* stop the hard way */
250 vinum_quit(int argc, char *argv[], char *argv0[])
255 /* Set action on receiving a SIGINT */
259 struct sigaction act;
261 act.sa_handler = catchsig;
263 sigemptyset(&act.sa_mask);
264 sigaction(SIGINT, &act, NULL);
270 longjmp(command_fail, 2);
273 #define FUNKEY(x) { kw_##x, &vinum_##x } /* create pair "kw_foo", vinum_foo */
274 #define vinum_move vinum_mv /* synonym for 'mv' */
278 void (*fun) (int argc, char *argv[], char *arg0[]);
319 FUNKEY(rebuildparity),
323 /* Take args arguments at argv and attempt to perform the operation specified */
325 parseline(int args, char *argv[])
329 enum keyword command; /* command to execute */
331 if (history != NULL) { /* save the command to history file */
333 for (i = 0; i < args; i++) /* all args */
334 fprintf(history, "%s ", argv[i]);
335 fputs("\n", history);
337 if ((args == 0) /* empty line */
338 ||(*argv[0] == '#')) /* or a comment, */
340 if (args == MAXARGS) { /* too many arguments, */
341 fprintf(stderr, "Too many arguments to %s, this can't be right\n", argv[0]);
344 command = get_keyword(argv[0], &keyword_set);
345 dowait = 0; /* initialize flags */
346 force = 0; /* initialize flags */
347 vflag = 0; /* initialize flags */
348 Verbose = 0; /* initialize flags */
349 recurse = 0; /* initialize flags */
350 sflag = 0; /* initialize flags */
351 objectname = NULL; /* no name yet */
354 * first handle generic options
355 * We don't use getopt(3) because
356 * getopt doesn't allow merging flags
357 * (for example, -fr).
359 for (i = 1; (i < args) && (argv[i][0] == '-'); i++) { /* while we have flags */
360 for (j = 1; j < strlen(argv[i]); j++)
361 switch (argv[i][j]) {
362 case 'd': /* -d: debug */
366 case 'f': /* -f: force */
370 case 'i': /* interval */
372 if (argv[i][j + 1] != '\0') /* operand follows, */
373 interval = atoi(&argv[i][j + 1]); /* use it */
374 else if (args > (i + 1)) /* another following, */
375 interval = atoi(argv[++i]); /* use it */
376 if (interval == 0) /* nothing valid, */
377 fprintf(stderr, "-i: no interval specified\n");
380 case 'n': /* -n: get name */
381 if (i == args - 1) { /* last arg */
382 fprintf(stderr, "-n requires a name parameter\n");
385 objectname = argv[++i]; /* pick it up */
386 j = strlen(argv[i]); /* skip the next parm */
389 case 'r': /* -r: recurse */
393 case 's': /* -s: show statistics */
399 if (argv[i][j + 1] != '\0') /* operand follows, */
400 SSize = atoi(&argv[i][j + 1]); /* use it */
401 else if (args > (i + 1)) /* another following, */
402 SSize = atoi(argv[++i]); /* use it */
403 if (SSize == 0) /* nothing valid, */
404 fprintf(stderr, "-S: no size specified\n");
407 case 'v': /* -v: verbose */
411 case 'V': /* -V: Very verbose */
416 case 'w': /* -w: wait for completion */
421 fprintf(stderr, "Invalid flag: %s\n", argv[i]);
425 /* Pass what we have left to the command to handle it */
426 for (j = 0; j < (sizeof(funkeys) / sizeof(struct funkey)); j++) {
427 if (funkeys[j].kw == command) { /* found the command */
428 funkeys[j].fun(args - i, &argv[i], &argv[0]);
432 fprintf(stderr, "Unknown command: %s\n", argv[0]);
436 get_drive_info(struct _drive *drive, int index)
438 *(int *) drive = index; /* put in drive to hand to driver */
439 if (ioctl(superdev, VINUM_DRIVECONFIG, drive) < 0) {
441 "Can't get config for drive %d: %s\n",
444 longjmp(command_fail, -1);
449 get_sd_info(struct _sd *sd, int index)
451 *(int *) sd = index; /* put in sd to hand to driver */
452 if (ioctl(superdev, VINUM_SDCONFIG, sd) < 0) {
454 "Can't get config for subdisk %d: %s\n",
457 longjmp(command_fail, -1);
461 /* Get the contents of the sd entry for subdisk <sdno>
462 * of the specified plex. */
464 get_plex_sd_info(struct _sd *sd, int plexno, int sdno)
466 ((int *) sd)[0] = plexno;
467 ((int *) sd)[1] = sdno; /* pass parameters */
468 if (ioctl(superdev, VINUM_PLEXSDCONFIG, sd) < 0) {
470 "Can't get config for subdisk %d (part of plex %d): %s\n",
474 longjmp(command_fail, -1);
479 get_plex_info(struct _plex *plex, int index)
481 *(int *) plex = index; /* put in plex to hand to driver */
482 if (ioctl(superdev, VINUM_PLEXCONFIG, plex) < 0) {
484 "Can't get config for plex %d: %s\n",
487 longjmp(command_fail, -1);
492 get_volume_info(struct _volume *volume, int index)
494 *(int *) volume = index; /* put in volume to hand to driver */
495 if (ioctl(superdev, VINUM_VOLCONFIG, volume) < 0) {
497 "Can't get config for volume %d: %s\n",
500 longjmp(command_fail, -1);
505 find_drive_by_devname(char *name)
509 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
510 perror("Can't get vinum config");
513 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
514 get_drive_info(&drive, driveno);
515 if ((drive.state != drive_unallocated) /* real drive */
516 &&(!strcmp(drive.devicename, name))) /* and the name's right, */
517 return &drive; /* found it */
519 return NULL; /* no drive of that name */
522 /* Create the device nodes for vinum objects */
531 if (access(_PATH_DEV, W_OK) < 0) { /* can't access /dev to write? */
532 if (errno == EROFS) /* because it's read-only, */
533 fprintf(stderr, VINUMMOD ": " _PATH_DEV
534 " is mounted read-only, not rebuilding " VINUM_DIR "\n");
536 perror(VINUMMOD ": Can't write to " _PATH_DEV);
541 fprintf(history, "*** Created devices ***\n");
543 if (superdev >= 0) /* super device open */
546 system("rm -rf " VINUM_DIR); /* remove the old directories */
547 system("mkdir -p " VINUM_DIR "/drive " /* and make them again */
552 if (mknod(VINUM_SUPERDEV_NAME,
553 S_IRUSR | S_IWUSR | S_IFCHR, /* user only */
554 makedev(VINUM_CDEV_MAJOR, VINUM_SUPERDEV)) < 0)
555 fprintf(stderr, "Can't create %s: %s\n", VINUM_SUPERDEV_NAME, strerror(errno));
558 superdev = open(VINUM_SUPERDEV_NAME, O_RDWR); /* open the super device */
560 perror(VINUM_SUPERDEV_NAME);
563 if (mknod(VINUM_DAEMON_DEV_NAME, /* daemon super device */
564 S_IRUSR | S_IWUSR | S_IFCHR, /* user only */
565 makedev(VINUM_CDEV_MAJOR, VINUM_DAEMON_DEV)) < 0)
566 fprintf(stderr, "Can't create %s: %s\n", VINUM_DAEMON_DEV_NAME, strerror(errno));
568 if (ioctl(superdev, VINUM_GETCONFIG, &vinum_conf) < 0) {
569 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++)
581 /* Drives. Do this later (both logical and physical names) XXX */
582 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
583 char filename[PATH_MAX]; /* for forming file names */
585 get_drive_info(&drive, driveno);
586 if (drive.state > drive_referenced) {
587 sprintf(filename, "ln -s %s " VINUM_DIR "/drive/%s", drive.devicename, drive.label.name);
593 /* make the devices for a volume */
595 make_vol_dev(int volno, int recurse)
598 char filename[PATH_MAX]; /* for forming file names */
601 get_volume_info(&vol, volno);
602 if (vol.state != volume_unallocated) { /* we could have holes in our lists */
603 voldev = VINUMDEV(volno, 0, 0, VINUM_VOLUME_TYPE); /* create a device number */
605 /* Create /dev/vinum/<myvol> */
606 sprintf(filename, VINUM_DIR "/%s", vol.name);
607 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, voldev) < 0)
608 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
610 /* Create /dev/vinum/vol/<myvol> */
611 sprintf(filename, VINUM_DIR "/vol/%s", vol.name);
612 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, voldev) < 0)
613 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
615 if (vol.plexes > 0) {
616 /* Create /dev/vinum/vol/<myvol>.plex/ */
617 sprintf(filename, VINUM_DIR "/vol/%s.plex", vol.name);
618 if (mkdir(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IXOTH) < 0)
619 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
622 for (plexno = 0; plexno < vol.plexes; plexno++)
623 make_plex_dev(plex.plexno, recurse);
628 * Create device entries for the plexes in
629 * /dev/vinum/<vol>.plex/ and /dev/vinum/plex.
632 make_plex_dev(int plexno, int recurse)
634 dev_t plexdev; /* device */
635 char filename[PATH_MAX]; /* for forming file names */
638 get_plex_info(&plex, plexno);
639 if (plex.state != plex_unallocated) {
640 plexdev = VINUM_PLEX(plexno);
642 /* /dev/vinum/plex/<plex> */
643 sprintf(filename, VINUM_DIR "/plex/%s", plex.name);
644 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, plexdev) < 0)
645 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
647 if (plex.volno >= 0) {
648 get_volume_info(&vol, plex.volno);
649 plexdev = VINUMDEV(plex.volno, plexno, 0, VINUM_PLEX_TYPE);
651 /* Create device /dev/vinum/vol/<vol>.plex/<plex> */
652 sprintf(filename, VINUM_DIR "/vol/%s.plex/%s", vol.name, plex.name);
653 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, plexdev) < 0)
654 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
656 /* Create directory /dev/vinum/vol/<vol>.plex/<plex>.sd */
657 sprintf(filename, VINUM_DIR "/vol/%s.plex/%s.sd", vol.name, plex.name);
658 if (mkdir(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IXOTH) < 0)
659 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
662 for (sdno = 0; sdno < plex.subdisks; sdno++) {
663 get_plex_sd_info(&sd, plex.plexno, sdno);
664 make_sd_dev(sd.sdno);
670 /* Create the contents of /dev/vinum/sd and /dev/vinum/rsd */
672 make_sd_dev(int sdno)
674 dev_t sddev; /* device */
675 char filename[PATH_MAX]; /* for forming file names */
677 get_sd_info(&sd, sdno);
678 if (sd.state != sd_unallocated) {
679 sddev = VINUM_SD(sdno);
681 /* /dev/vinum/sd/<sd> */
682 sprintf(filename, VINUM_DIR "/sd/%s", sd.name);
683 if (mknod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IFCHR, sddev) < 0)
684 fprintf(stderr, "Can't create %s: %s\n", filename, strerror(errno));
689 /* command line interface for the 'makedev' command */
691 vinum_makedev(int argc, char *argv[], char *arg0[])
696 fprintf(stderr, "makedev is not needed for a DEVFS-based system\n");
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];
865 if (history != NULL) {
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),