diff --git a/sbin/camcontrol/modeedit.c b/sbin/camcontrol/modeedit.c index 3fb9587e206a..eaf87d632957 100644 --- a/sbin/camcontrol/modeedit.c +++ b/sbin/camcontrol/modeedit.c @@ -1,909 +1,910 @@ /*- * Copyright (c) 2000 Kelly Yancey * Derived from work done by Julian Elischer , 1993, and Peter Dufault , 1994. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer, * without modification, immediately at the beginning of the file. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "camcontrol.h" int verbose = 0; #define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes" #define DEFAULT_EDITOR "vi" #define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */ #define MAX_PAGENUM_LEN 10 /* Max characters in page num. */ #define MAX_PAGENAME_LEN 64 /* Max characters in page name. */ #define PAGEDEF_START '{' /* Page definition delimiter. */ #define PAGEDEF_END '}' /* Page definition delimiter. */ #define PAGENAME_START '"' /* Page name delimiter. */ #define PAGENAME_END '"' /* Page name delimiter. */ #define PAGEENTRY_END ';' /* Page entry terminator (optional). */ #define MAX_COMMAND_SIZE 255 /* Mode/Log sense data buffer size. */ #define PAGE_CTRL_SHIFT 6 /* Bit offset to page control field. */ /* Macros for working with mode pages. */ #define MODE_PAGE_HEADER(mh) \ (struct scsi_mode_page_header *)find_mode_page_6(mh) #define MODE_PAGE_DATA(mph) \ (u_int8_t *)(mph) + sizeof(struct scsi_mode_page_header) struct editentry { STAILQ_ENTRY(editentry) link; char *name; char type; int editable; int size; union { int ivalue; char *svalue; } value; }; STAILQ_HEAD(, editentry) editlist; /* List of page entries. */ int editlist_changed = 0; /* Whether any entries were changed. */ struct pagename { SLIST_ENTRY(pagename) link; int pagenum; char *name; }; SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */ static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */ static FILE *edit_file = NULL; /* File handle for edit file. */ static char edit_path[] = "/tmp/camXXXXXX"; /* Function prototypes. */ static void editentry_create(void *hook, int letter, void *arg, int count, char *name); static void editentry_update(void *hook, int letter, void *arg, int count, char *name); static int editentry_save(void *hook, char *name); static struct editentry *editentry_lookup(char *name); static int editentry_set(char *name, char *newvalue, int editonly); static void editlist_populate(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout); static void editlist_save(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout); static void nameentry_create(int pagenum, char *name); static struct pagename *nameentry_lookup(int pagenum); static int load_format(const char *pagedb_path, int page); static int modepage_write(FILE *file, int editonly); static int modepage_read(FILE *file); static void modepage_edit(void); static void modepage_dump(struct cam_device *device, int page, int page_control, int dbd, int retries, int timeout); static void cleanup_editfile(void); #define returnerr(code) do { \ errno = code; \ return (-1); \ } while (0) #define RTRIM(string) do { \ int _length; \ while (isspace(string[_length = strlen(string) - 1])) \ string[_length] = '\0'; \ } while (0) static void editentry_create(void *hook __unused, int letter, void *arg, int count, char *name) { struct editentry *newentry; /* Buffer to hold new entry. */ /* Allocate memory for the new entry and a copy of the entry name. */ if ((newentry = malloc(sizeof(struct editentry))) == NULL || (newentry->name = strdup(name)) == NULL) err(EX_OSERR, NULL); /* Trim any trailing whitespace for the entry name. */ RTRIM(newentry->name); newentry->editable = (arg != NULL); newentry->type = letter; newentry->size = count; /* Placeholder; not accurate. */ newentry->value.svalue = NULL; STAILQ_INSERT_TAIL(&editlist, newentry, link); } static void editentry_update(void *hook __unused, int letter, void *arg, int count, char *name) { struct editentry *dest; /* Buffer to hold entry to update. */ dest = editentry_lookup(name); assert(dest != NULL); dest->type = letter; dest->size = count; /* We get the real size now. */ switch (dest->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': dest->value.ivalue = (intptr_t)arg; break; case 'c': /* Character array. */ case 'z': /* Null-padded string. */ editentry_set(name, (char *)arg, 0); break; default: ; /* NOTREACHED */ } } static int editentry_save(void *hook __unused, char *name) { struct editentry *src; /* Entry value to save. */ src = editentry_lookup(name); assert(src != NULL); switch (src->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': return (src->value.ivalue); /* NOTREACHED */ case 'c': /* Character array. */ case 'z': /* Null-padded string. */ return ((intptr_t)src->value.svalue); /* NOTREACHED */ default: ; /* NOTREACHED */ } return (0); /* This should never happen. */ } static struct editentry * editentry_lookup(char *name) { struct editentry *scan; assert(name != NULL); STAILQ_FOREACH(scan, &editlist, link) { if (strcasecmp(scan->name, name) == 0) return (scan); } /* Not found during list traversal. */ return (NULL); } static int editentry_set(char *name, char *newvalue, int editonly) { struct editentry *dest; /* Modepage entry to update. */ char *cval; /* Pointer to new string value. */ char *convertend; /* End-of-conversion pointer. */ int ival; /* New integral value. */ int resolution; /* Resolution in bits for integer conversion. */ /* * Macro to determine the maximum value of the given size for the current * resolution. * XXX Lovely x86's optimize out the case of shifting by 32 and gcc doesn't * currently workaround it (even for int64's), so we have to kludge it. */ #define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \ (int)0xffffffff: (1 << (resolution * (size))) - 1) assert(newvalue != NULL); if (*newvalue == '\0') return (0); /* Nothing to do. */ if ((dest = editentry_lookup(name)) == NULL) returnerr(ENOENT); if (!dest->editable && editonly) returnerr(EPERM); switch (dest->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': /* Convert the value string to an integer. */ resolution = (dest->type == 'i')? 8: 1; ival = (int)strtol(newvalue, &convertend, 0); if (*convertend != '\0') returnerr(EINVAL); if (ival > RESOLUTION_MAX(dest->size) || ival < 0) { int newival = (ival < 0)? 0: RESOLUTION_MAX(dest->size); warnx("value %d is out of range for entry %s; clipping " "to %d", ival, name, newival); ival = newival; } if (dest->value.ivalue != ival) editlist_changed = 1; dest->value.ivalue = ival; break; case 'c': /* Character array. */ case 'z': /* Null-padded string. */ if ((cval = malloc(dest->size + 1)) == NULL) err(EX_OSERR, NULL); bzero(cval, dest->size + 1); strncpy(cval, newvalue, dest->size); if (dest->type == 'z') { /* Convert trailing spaces to nulls. */ char *convertend2; for (convertend2 = cval + dest->size; convertend2 >= cval; convertend2--) { if (*convertend2 == ' ') *convertend2 = '\0'; else if (*convertend2 != '\0') break; } } if (strncmp(dest->value.svalue, cval, dest->size) == 0) { /* Nothing changed, free the newly allocated string. */ free(cval); break; } if (dest->value.svalue != NULL) { /* Free the current string buffer. */ free(dest->value.svalue); dest->value.svalue = NULL; } dest->value.svalue = cval; editlist_changed = 1; break; default: ; /* NOTREACHED */ } return (0); #undef RESOLUTION_MAX } static void nameentry_create(int pagenum, char *name) { struct pagename *newentry; if (pagenum < 0 || name == NULL || name[0] == '\0') return; /* Allocate memory for the new entry and a copy of the entry name. */ if ((newentry = malloc(sizeof(struct pagename))) == NULL || (newentry->name = strdup(name)) == NULL) err(EX_OSERR, NULL); /* Trim any trailing whitespace for the page name. */ RTRIM(newentry->name); newentry->pagenum = pagenum; SLIST_INSERT_HEAD(&namelist, newentry, link); } static struct pagename * nameentry_lookup(int pagenum) { struct pagename *scan; SLIST_FOREACH(scan, &namelist, link) { if (pagenum == scan->pagenum) return (scan); } /* Not found during list traversal. */ return (NULL); } static int load_format(const char *pagedb_path, int page) { FILE *pagedb; char str_pagenum[MAX_PAGENUM_LEN]; char str_pagename[MAX_PAGENAME_LEN]; int pagenum; int depth; /* Quoting depth. */ int found; int lineno; enum { LOCATE, PAGENAME, PAGEDEF } state; int ch; char c; #define SETSTATE_LOCATE do { \ str_pagenum[0] = '\0'; \ str_pagename[0] = '\0'; \ pagenum = -1; \ state = LOCATE; \ } while (0) #define SETSTATE_PAGENAME do { \ str_pagename[0] = '\0'; \ state = PAGENAME; \ } while (0) #define SETSTATE_PAGEDEF do { \ format[0] = '\0'; \ state = PAGEDEF; \ } while (0) #define UPDATE_LINENO do { \ if (c == '\n') \ lineno++; \ } while (0) #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer)) if ((pagedb = fopen(pagedb_path, "r")) == NULL) returnerr(ENOENT); SLIST_INIT(&namelist); + c = '\0'; depth = 0; lineno = 0; found = 0; SETSTATE_LOCATE; while ((ch = fgetc(pagedb)) != EOF) { /* Keep a line count to make error messages more useful. */ UPDATE_LINENO; /* Skip over comments anywhere in the mode database. */ if (ch == '#') { do { ch = fgetc(pagedb); } while (ch != '\n' && ch != EOF); UPDATE_LINENO; continue; } c = ch; /* Strip out newline characters. */ if (c == '\n') continue; /* Keep track of the nesting depth for braces. */ if (c == PAGEDEF_START) depth++; else if (c == PAGEDEF_END) { depth--; if (depth < 0) { errx(EX_OSFILE, "%s:%d: %s", pagedb_path, lineno, "mismatched bracket"); } } switch (state) { case LOCATE: /* * Locate the page the user is interested in, skipping * all others. */ if (isspace(c)) { /* Ignore all whitespace between pages. */ break; } else if (depth == 0 && c == PAGEENTRY_END) { /* * A page entry terminator will reset page * scanning (useful for assigning names to * modes without providing a mode definition). */ /* Record the name of this page. */ pagenum = strtol(str_pagenum, NULL, 0); nameentry_create(pagenum, str_pagename); SETSTATE_LOCATE; } else if (depth == 0 && c == PAGENAME_START) { SETSTATE_PAGENAME; } else if (c == PAGEDEF_START) { pagenum = strtol(str_pagenum, NULL, 0); if (depth == 1) { /* Record the name of this page. */ nameentry_create(pagenum, str_pagename); /* * Only record the format if this is * the page we are interested in. */ if (page == pagenum && !found) SETSTATE_PAGEDEF; } } else if (c == PAGEDEF_END) { /* Reset the processor state. */ SETSTATE_LOCATE; } else if (depth == 0 && ! BUFFERFULL(str_pagenum)) { strncat(str_pagenum, &c, 1); } else if (depth == 0) { errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page identifier exceeds", sizeof(str_pagenum) - 1, "characters"); } break; case PAGENAME: if (c == PAGENAME_END) { /* * Return to LOCATE state without resetting the * page number buffer. */ state = LOCATE; } else if (! BUFFERFULL(str_pagename)) { strncat(str_pagename, &c, 1); } else { errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page name exceeds", sizeof(str_pagenum) - 1, "characters"); } break; case PAGEDEF: /* * Transfer the page definition into a format buffer * suitable for use with CDB encoding/decoding routines. */ if (depth == 0) { found = 1; SETSTATE_LOCATE; } else if (! BUFFERFULL(format)) { strncat(format, &c, 1); } else { errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page definition exceeds", sizeof(format) - 1, "characters"); } break; default: ; /* NOTREACHED */ } /* Repeat processing loop with next character. */ } if (ferror(pagedb)) err(EX_OSFILE, "%s", pagedb_path); /* Close the SCSI page database. */ fclose(pagedb); if (!found) /* Never found a matching page. */ returnerr(ESRCH); return (0); } static void editlist_populate(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; STAILQ_INIT(&editlist); /* Fetch changeable values; use to build initial editlist. */ mode_sense(device, modepage, 1, dbd, retries, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Decode the value data, creating edit_entries for each value. */ buff_decode_visit(mode_pars, mh->data_length, format, editentry_create, 0); /* Fetch the current/saved values; use to set editentry values. */ mode_sense(device, modepage, page_control, dbd, retries, timeout, data, sizeof(data)); buff_decode_visit(mode_pars, mh->data_length, format, editentry_update, 0); } static void editlist_save(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; /* Make sure that something changed before continuing. */ if (! editlist_changed) return; /* * Preload the CDB buffer with the current mode page data. * XXX If buff_encode_visit would return the number of bytes encoded * we *should* use that to build a header from scratch. As it is * now, we need mode_sense to find out the page length. */ mode_sense(device, modepage, page_control, dbd, retries, timeout, data, sizeof(data)); /* Initial headers & offsets. */ mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Encode the value data to be passed back to the device. */ buff_encode_visit(mode_pars, mh->data_length, format, editentry_save, 0); /* Eliminate block descriptors. */ bcopy(mph, ((u_int8_t *)mh) + sizeof(*mh), sizeof(*mph) + mph->page_length); /* Recalculate headers & offsets. */ mh->blk_desc_len = 0; /* No block descriptors. */ mh->dev_spec = 0; /* Clear device-specific parameters. */ mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); mph->page_code &= SMS_PAGE_CODE;/* Isolate just the page code. */ mh->data_length = 0; /* Reserved for MODE SELECT command. */ /* * Write the changes back to the device. If the user editted control * page 3 (saved values) then request the changes be permanently * recorded. */ mode_select(device, (page_control << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED), retries, timeout, (u_int8_t *)mh, sizeof(*mh) + mh->blk_desc_len + sizeof(*mph) + mph->page_length); } static int modepage_write(FILE *file, int editonly) { struct editentry *scan; int written = 0; STAILQ_FOREACH(scan, &editlist, link) { if (scan->editable || !editonly) { written++; if (scan->type == 'c' || scan->type == 'z') { fprintf(file, "%s: %s\n", scan->name, scan->value.svalue); } else { fprintf(file, "%s: %d\n", scan->name, scan->value.ivalue); } } } return (written); } static int modepage_read(FILE *file) { char *buffer; /* Pointer to dynamic line buffer. */ char *line; /* Pointer to static fgetln buffer. */ char *name; /* Name portion of the line buffer. */ char *value; /* Value portion of line buffer. */ size_t length; /* Length of static fgetln buffer. */ #define ABORT_READ(message, param) do { \ warnx(message, param); \ free(buffer); \ returnerr(EAGAIN); \ } while (0) while ((line = fgetln(file, &length)) != NULL) { /* Trim trailing whitespace (including optional newline). */ while (length > 0 && isspace(line[length - 1])) length--; /* Allocate a buffer to hold the line + terminating null. */ if ((buffer = malloc(length + 1)) == NULL) err(EX_OSERR, NULL); memcpy(buffer, line, length); buffer[length] = '\0'; /* Strip out comments. */ if ((value = strchr(buffer, '#')) != NULL) *value = '\0'; /* The name is first in the buffer. Trim whitespace.*/ name = buffer; RTRIM(name); while (isspace(*name)) name++; /* Skip empty lines. */ if (strlen(name) == 0) continue; /* The name ends at the colon; the value starts there. */ if ((value = strrchr(buffer, ':')) == NULL) ABORT_READ("no value associated with %s", name); *value = '\0'; /* Null-terminate name. */ value++; /* Value starts afterwards. */ /* Trim leading and trailing whitespace. */ RTRIM(value); while (isspace(*value)) value++; /* Make sure there is a value left. */ if (strlen(value) == 0) ABORT_READ("no value associated with %s", name); /* Update our in-memory copy of the modepage entry value. */ if (editentry_set(name, value, 1) != 0) { if (errno == ENOENT) { /* No entry by the name. */ ABORT_READ("no such modepage entry \"%s\"", name); } else if (errno == EINVAL) { /* Invalid value. */ ABORT_READ("Invalid value for entry \"%s\"", name); } else if (errno == ERANGE) { /* Value out of range for entry type. */ ABORT_READ("value out of range for %s", name); } else if (errno == EPERM) { /* Entry is not editable; not fatal. */ warnx("modepage entry \"%s\" is read-only; " "skipping.", name); } } free(buffer); } return (ferror(file)? -1: 0); #undef ABORT_READ } static void modepage_edit(void) { const char *editor; char *commandline; int fd; int written; if (!isatty(fileno(stdin))) { /* Not a tty, read changes from stdin. */ modepage_read(stdin); return; } /* Lookup editor to invoke. */ if ((editor = getenv("EDITOR")) == NULL) editor = DEFAULT_EDITOR; /* Create temp file for editor to modify. */ if ((fd = mkstemp(edit_path)) == -1) errx(EX_CANTCREAT, "mkstemp failed"); atexit(cleanup_editfile); if ((edit_file = fdopen(fd, "w")) == NULL) err(EX_NOINPUT, "%s", edit_path); written = modepage_write(edit_file, 1); fclose(edit_file); edit_file = NULL; if (written == 0) { warnx("no editable entries"); cleanup_editfile(); return; } /* * Allocate memory to hold the command line (the 2 extra characters * are to hold the argument separator (a space), and the terminating * null character. */ commandline = malloc(strlen(editor) + strlen(edit_path) + 2); if (commandline == NULL) err(EX_OSERR, NULL); sprintf(commandline, "%s %s", editor, edit_path); /* Invoke the editor on the temp file. */ if (system(commandline) == -1) err(EX_UNAVAILABLE, "could not invoke %s", editor); free(commandline); if ((edit_file = fopen(edit_path, "r")) == NULL) err(EX_NOINPUT, "%s", edit_path); /* Read any changes made to the temp file. */ modepage_read(edit_file); cleanup_editfile(); } static void modepage_dump(struct cam_device *device, int page, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; int indx; /* Index for scanning mode params. */ mode_sense(device, page, page_control, dbd, retries, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Print the raw mode page data with newlines each 8 bytes. */ for (indx = 0; indx < mph->page_length; indx++) { printf("%02x%c",mode_pars[indx], (((indx + 1) % 8) == 0) ? '\n' : ' '); } putchar('\n'); } static void cleanup_editfile(void) { if (edit_file == NULL) return; if (fclose(edit_file) != 0 || unlink(edit_path) != 0) warn("%s", edit_path); edit_file = NULL; } void mode_edit(struct cam_device *device, int page, int page_control, int dbd, int edit, int binary, int retry_count, int timeout) { const char *pagedb_path; /* Path to modepage database. */ if (edit && binary) errx(EX_USAGE, "cannot edit in binary mode."); if (! binary) { if ((pagedb_path = getenv("SCSI_MODES")) == NULL) pagedb_path = DEFAULT_SCSI_MODE_DB; if (load_format(pagedb_path, page) != 0 && (edit || verbose)) { if (errno == ENOENT) { /* Modepage database file not found. */ warn("cannot open modepage database \"%s\"", pagedb_path); } else if (errno == ESRCH) { /* Modepage entry not found in database. */ warnx("modepage %d not found in database" "\"%s\"", page, pagedb_path); } /* We can recover in display mode, otherwise we exit. */ if (!edit) { warnx("reverting to binary display only"); binary = 1; } else exit(EX_OSFILE); } editlist_populate(device, page, page_control, dbd, retry_count, timeout); } if (edit) { if (page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT && page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED) errx(EX_USAGE, "it only makes sense to edit page 0 " "(current) or page 3 (saved values)"); modepage_edit(); editlist_save(device, page, page_control, dbd, retry_count, timeout); } else if (binary || STAILQ_EMPTY(&editlist)) { /* Display without formatting information. */ modepage_dump(device, page, page_control, dbd, retry_count, timeout); } else { /* Display with format. */ modepage_write(stdout, 0); } } void mode_list(struct cam_device *device, int page_control, int dbd, int retry_count, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; struct pagename *nameentry; const char *pagedb_path; int len; if ((pagedb_path = getenv("SCSI_MODES")) == NULL) pagedb_path = DEFAULT_SCSI_MODE_DB; if (load_format(pagedb_path, 0) != 0 && verbose && errno == ENOENT) { /* Modepage database file not found. */ warn("cannot open modepage database \"%s\"", pagedb_path); } /* Build the list of all mode pages by querying the "all pages" page. */ mode_sense(device, SMS_ALL_PAGES_PAGE, page_control, dbd, retry_count, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; len = mh->blk_desc_len; /* Skip block descriptors. */ /* Iterate through the pages in the reply. */ while (len < mh->data_length) { /* Locate the next mode page header. */ mph = (struct scsi_mode_page_header *) ((intptr_t)mh + sizeof(*mh) + len); mode_pars = MODE_PAGE_DATA(mph); mph->page_code &= SMS_PAGE_CODE; nameentry = nameentry_lookup(mph->page_code); if (nameentry == NULL || nameentry->name == NULL) printf("0x%02x\n", mph->page_code); else printf("0x%02x\t%s\n", mph->page_code, nameentry->name); len += mph->page_length + sizeof(*mph); } } diff --git a/sbin/gvinum/gvinum.c b/sbin/gvinum/gvinum.c index 041f1409ccde..3b350f8151ac 100644 --- a/sbin/gvinum/gvinum.c +++ b/sbin/gvinum/gvinum.c @@ -1,1430 +1,1431 @@ /* * Copyright (c) 2004 Lukas Ertl * Copyright (c) 2005 Chris Jones * Copyright (c) 2007 Ulf Lilleengen * All rights reserved. * * Portions of this software were developed for the FreeBSD Project * by Chris Jones thanks to the support of Google's Summer of Code * program and mentoring by Lukas Ertl. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "gvinum.h" void gvinum_attach(int, char **); void gvinum_concat(int, char **); void gvinum_create(int, char **); void gvinum_detach(int, char **); void gvinum_grow(int, char **); void gvinum_help(void); void gvinum_list(int, char **); void gvinum_move(int, char **); void gvinum_mirror(int, char **); void gvinum_parityop(int, char **, int); void gvinum_printconfig(int, char **); void gvinum_raid5(int, char **); void gvinum_rename(int, char **); void gvinum_resetconfig(void); void gvinum_rm(int, char **); void gvinum_saveconfig(void); void gvinum_setstate(int, char **); void gvinum_start(int, char **); void gvinum_stop(int, char **); void gvinum_stripe(int, char **); void parseline(int, char **); void printconfig(FILE *, char *); char *create_drive(char *); void create_volume(int, char **, char *); char *find_name(const char *, int, int); char *find_pattern(char *, char *); void copy_device(struct gv_drive *, const char *); #define find_drive() find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME) int main(int argc, char **argv) { int line, tokens; char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS]; /* Load the module if necessary. */ if (kldfind(GVINUMMOD) < 0 && kldload(GVINUMMOD) < 0) err(1, GVINUMMOD ": Kernel module not available"); /* Arguments given on the command line. */ if (argc > 1) { argc--; argv++; parseline(argc, argv); /* Interactive mode. */ } else { for (;;) { inputline = readline("gvinum -> "); if (inputline == NULL) { if (ferror(stdin)) { err(1, "can't read input"); } else { printf("\n"); exit(0); } } else if (*inputline) { add_history(inputline); strcpy(buffer, inputline); free(inputline); line++; /* count the lines */ tokens = gv_tokenize(buffer, token, GV_MAXARGS); if (tokens) parseline(tokens, token); } } } exit(0); } /* Attach a plex to a volume or a subdisk to a plex. */ void gvinum_attach(int argc, char **argv) { struct gctl_req *req; const char *errstr; int rename; off_t offset; rename = 0; offset = -1; if (argc < 3) { warnx("usage:\tattach [rename] " "[]\n" "\tattach [rename]"); return; } if (argc > 3) { if (!strcmp(argv[3], "rename")) { rename = 1; if (argc == 5) offset = strtol(argv[4], NULL, 0); } else offset = strtol(argv[3], NULL, 0); } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "attach"); gctl_ro_param(req, "child", -1, argv[1]); gctl_ro_param(req, "parent", -1, argv[2]); gctl_ro_param(req, "offset", sizeof(off_t), &offset); gctl_ro_param(req, "rename", sizeof(int), &rename); errstr = gctl_issue(req); if (errstr != NULL) warnx("attach failed: %s", errstr); gctl_free(req); } void gvinum_create(int argc, char **argv) { struct gctl_req *req; struct gv_drive *d; struct gv_plex *p; struct gv_sd *s; struct gv_volume *v; FILE *tmp; int drives, errors, fd, flags, i, line, plexes, plex_in_volume; int sd_in_plex, status, subdisks, tokens, undeffd, volumes; const char *errstr; char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname; char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS]; char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME]; tmp = NULL; flags = 0; for (i = 1; i < argc; i++) { /* Force flag used to ignore already created drives. */ if (!strcmp(argv[i], "-f")) { flags |= GV_FLAG_F; /* Else it must be a file. */ } else { if ((tmp = fopen(argv[1], "r")) == NULL) { warn("can't open '%s' for reading", argv[1]); return; } } } /* We didn't get a file. */ if (tmp == NULL) { snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX"); if ((fd = mkstemp(tmpfile)) == -1) { warn("temporary file not accessible"); return; } if ((tmp = fdopen(fd, "w")) == NULL) { warn("can't open '%s' for writing", tmpfile); return; } printconfig(tmp, "# "); fclose(tmp); ed = getenv("EDITOR"); if (ed == NULL) ed = _PATH_VI; snprintf(commandline, sizeof(commandline), "%s %s", ed, tmpfile); status = system(commandline); if (status != 0) { warn("couldn't exec %s; status: %d", ed, status); return; } if ((tmp = fopen(tmpfile, "r")) == NULL) { warn("can't open '%s' for reading", tmpfile); return; } } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "create"); gctl_ro_param(req, "flags", sizeof(int), &flags); drives = volumes = plexes = subdisks = 0; plex_in_volume = sd_in_plex = undeffd = 0; plex[0] = '\0'; errors = 0; line = 1; while ((fgets(buf, BUFSIZ, tmp)) != NULL) { /* Skip empty lines and comments. */ if (*buf == '\0' || *buf == '#') { line++; continue; } /* Kill off the newline. */ buf[strlen(buf) - 1] = '\0'; /* * Copy the original input line in case we need it for error * output. */ strlcpy(original, buf, sizeof(original)); tokens = gv_tokenize(buf, token, GV_MAXARGS); if (tokens <= 0) { line++; continue; } /* Volume definition. */ if (!strcmp(token[0], "volume")) { v = gv_new_volume(tokens, token); if (v == NULL) { warnx("line %d: invalid volume definition", line); warnx("line %d: '%s'", line, original); errors++; line++; continue; } /* Reset plex count for this volume. */ plex_in_volume = 0; /* * Set default volume name for following plex * definitions. */ strlcpy(volume, v->name, sizeof(volume)); snprintf(buf1, sizeof(buf1), "volume%d", volumes); gctl_ro_param(req, buf1, sizeof(*v), v); volumes++; /* Plex definition. */ } else if (!strcmp(token[0], "plex")) { p = gv_new_plex(tokens, token); if (p == NULL) { warnx("line %d: invalid plex definition", line); warnx("line %d: '%s'", line, original); errors++; line++; continue; } /* Reset subdisk count for this plex. */ sd_in_plex = 0; /* Default name. */ if (strlen(p->name) == 0) { snprintf(p->name, sizeof(p->name), "%s.p%d", volume, plex_in_volume++); } /* Default volume. */ if (strlen(p->volume) == 0) { snprintf(p->volume, sizeof(p->volume), "%s", volume); } /* * Set default plex name for following subdisk * definitions. */ strlcpy(plex, p->name, sizeof(plex)); snprintf(buf1, sizeof(buf1), "plex%d", plexes); gctl_ro_param(req, buf1, sizeof(*p), p); plexes++; /* Subdisk definition. */ } else if (!strcmp(token[0], "sd")) { s = gv_new_sd(tokens, token); if (s == NULL) { warnx("line %d: invalid subdisk " "definition:", line); warnx("line %d: '%s'", line, original); errors++; line++; continue; } /* Default name. */ if (strlen(s->name) == 0) { if (strlen(plex) == 0) { sdname = find_name("gvinumsubdisk.p", GV_TYPE_SD, GV_MAXSDNAME); snprintf(s->name, sizeof(s->name), "%s.s%d", sdname, undeffd++); free(sdname); } else { snprintf(s->name, sizeof(s->name), "%s.s%d",plex, sd_in_plex++); } } /* Default plex. */ if (strlen(s->plex) == 0) snprintf(s->plex, sizeof(s->plex), "%s", plex); snprintf(buf1, sizeof(buf1), "sd%d", subdisks); gctl_ro_param(req, buf1, sizeof(*s), s); subdisks++; /* Subdisk definition. */ } else if (!strcmp(token[0], "drive")) { d = gv_new_drive(tokens, token); if (d == NULL) { warnx("line %d: invalid drive definition:", line); warnx("line %d: '%s'", line, original); errors++; line++; continue; } snprintf(buf1, sizeof(buf1), "drive%d", drives); gctl_ro_param(req, buf1, sizeof(*d), d); drives++; /* Everything else is bogus. */ } else { warnx("line %d: invalid definition:", line); warnx("line %d: '%s'", line, original); errors++; } line++; } fclose(tmp); unlink(tmpfile); if (!errors && (volumes || plexes || subdisks || drives)) { gctl_ro_param(req, "volumes", sizeof(int), &volumes); gctl_ro_param(req, "plexes", sizeof(int), &plexes); gctl_ro_param(req, "subdisks", sizeof(int), &subdisks); gctl_ro_param(req, "drives", sizeof(int), &drives); errstr = gctl_issue(req); if (errstr != NULL) warnx("create failed: %s", errstr); } gctl_free(req); } /* Create a concatenated volume. */ void gvinum_concat(int argc, char **argv) { if (argc < 2) { warnx("usage:\tconcat [-fv] [-n name] drives\n"); return; } create_volume(argc, argv, "concat"); } /* Create a drive quick and dirty. */ char * create_drive(char *device) { struct gv_drive *d; struct gctl_req *req; const char *errstr; char *drivename, *dname; int drives, i, flags, volumes, subdisks, plexes; flags = plexes = subdisks = volumes = 0; drives = 1; dname = NULL; drivename = find_drive(); if (drivename == NULL) return (NULL); req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "create"); d = gv_alloc_drive(); if (d == NULL) err(1, "unable to allocate for gv_drive object"); strlcpy(d->name, drivename, sizeof(d->name)); copy_device(d, device); gctl_ro_param(req, "drive0", sizeof(*d), d); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "drives", sizeof(int), &drives); gctl_ro_param(req, "volumes", sizeof(int), &volumes); gctl_ro_param(req, "plexes", sizeof(int), &plexes); gctl_ro_param(req, "subdisks", sizeof(int), &subdisks); errstr = gctl_issue(req); if (errstr != NULL) { warnx("error creating drive: %s", errstr); gctl_free(req); return (NULL); } else { gctl_free(req); /* XXX: This is needed because we have to make sure the drives * are created before we return. */ /* Loop until it's in the config. */ for (i = 0; i < 100000; i++) { dname = find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME); /* If we got a different name, quit. */ if (dname == NULL) continue; if (strcmp(dname, drivename)) { free(dname); return (drivename); } free(dname); dname = NULL; usleep(100000); /* Sleep for 0.1s */ } } gctl_free(req); return (drivename); } /* * General routine for creating a volume. Mainly for use by concat, mirror, * raid5 and stripe commands. */ void create_volume(int argc, char **argv, char *verb) { struct gctl_req *req; const char *errstr; char buf[BUFSIZ], *drivename, *volname; int drives, flags, i; off_t stripesize; flags = 0; drives = 0; volname = NULL; stripesize = 262144; /* XXX: Should we check for argument length? */ req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-f")) { flags |= GV_FLAG_F; } else if (!strcmp(argv[i], "-n")) { volname = argv[++i]; } else if (!strcmp(argv[i], "-v")) { flags |= GV_FLAG_V; } else if (!strcmp(argv[i], "-s")) { flags |= GV_FLAG_S; if (!strcmp(verb, "raid5")) stripesize = gv_sizespec(argv[++i]); } else { /* Assume it's a drive. */ snprintf(buf, sizeof(buf), "drive%d", drives++); /* First we create the drive. */ drivename = create_drive(argv[i]); if (drivename == NULL) goto bad; /* Then we add it to the request. */ gctl_ro_param(req, buf, -1, drivename); } } gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize); /* Find a free volume name. */ if (volname == NULL) volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME); /* Then we send a request to actually create the volumes. */ gctl_ro_param(req, "verb", -1, verb); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "drives", sizeof(int), &drives); gctl_ro_param(req, "name", -1, volname); errstr = gctl_issue(req); if (errstr != NULL) warnx("creating %s volume failed: %s", verb, errstr); bad: gctl_free(req); } /* Parse a line of the config, return the word after . */ char * find_pattern(char *line, char *pattern) { char *ptr; ptr = strsep(&line, " "); while (ptr != NULL) { if (!strcmp(ptr, pattern)) { /* Return the next. */ ptr = strsep(&line, " "); return (ptr); } ptr = strsep(&line, " "); } return (NULL); } /* Find a free name for an object given a a prefix. */ char * find_name(const char *prefix, int type, int namelen) { struct gctl_req *req; char comment[1], buf[GV_CFG_LEN - 1], *name, *sname, *ptr; const char *errstr; int i, n, begin, len, conflict; char line[1024]; comment[0] = '\0'; /* Find a name. Fetch out configuration first. */ req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "getconfig"); gctl_ro_param(req, "comment", -1, comment); gctl_rw_param(req, "config", sizeof(buf), buf); errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't get configuration: %s", errstr); return (NULL); } gctl_free(req); begin = 0; len = strlen(buf); i = 0; sname = malloc(namelen + 1); /* XXX: Max object setting? */ for (n = 0; n < 10000; n++) { snprintf(sname, namelen, "%s%d", prefix, n); conflict = 0; begin = 0; /* Loop through the configuration line by line. */ for (i = 0; i < len; i++) { if (buf[i] == '\n' || buf[i] == '\0') { ptr = buf + begin; strlcpy(line, ptr, (i - begin) + 1); begin = i + 1; switch (type) { case GV_TYPE_DRIVE: name = find_pattern(line, "drive"); break; case GV_TYPE_VOL: name = find_pattern(line, "volume"); break; case GV_TYPE_PLEX: case GV_TYPE_SD: name = find_pattern(line, "name"); break; default: printf("Invalid type given\n"); continue; } if (name == NULL) continue; if (!strcmp(sname, name)) { conflict = 1; /* XXX: Could quit the loop earlier. */ } } } if (!conflict) return (sname); } free(sname); return (NULL); } void copy_device(struct gv_drive *d, const char *device) { if (strncmp(device, "/dev/", 5) == 0) strlcpy(d->device, (device + 5), sizeof(d->device)); else strlcpy(d->device, device, sizeof(d->device)); } /* Detach a plex or subdisk from its parent. */ void gvinum_detach(int argc, char **argv) { const char *errstr; struct gctl_req *req; int flags, i; + flags = 0; optreset = 1; optind = 1; while ((i = getopt(argc, argv, "f")) != -1) { switch(i) { case 'f': flags |= GV_FLAG_F; break; default: warn("invalid flag: %c", i); return; } } argc -= optind; argv += optind; if (argc != 1) { warnx("usage: detach [-f] | "); return; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "detach"); gctl_ro_param(req, "object", -1, argv[0]); gctl_ro_param(req, "flags", sizeof(int), &flags); errstr = gctl_issue(req); if (errstr != NULL) warnx("detach failed: %s", errstr); gctl_free(req); } void gvinum_help(void) { printf("COMMANDS\n" "checkparity [-f] plex\n" " Check the parity blocks of a RAID-5 plex.\n" "create [-f] description-file\n" " Create as per description-file or open editor.\n" "attach plex volume [rename]\n" "attach subdisk plex [offset] [rename]\n" " Attach a plex to a volume, or a subdisk to a plex\n" "concat [-fv] [-n name] drives\n" " Create a concatenated volume from the specified drives.\n" "detach [-f] [plex | subdisk]\n" " Detach a plex or a subdisk from the volume or plex to\n" " which it is attached.\n" "grow plex drive\n" " Grow plex by creating a properly sized subdisk on drive\n" "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n" " List information about specified objects.\n" "ld [-r] [-v] [-V] [volume]\n" " List information about drives.\n" "ls [-r] [-v] [-V] [subdisk]\n" " List information about subdisks.\n" "lp [-r] [-v] [-V] [plex]\n" " List information about plexes.\n" "lv [-r] [-v] [-V] [volume]\n" " List information about volumes.\n" "mirror [-fsv] [-n name] drives\n" " Create a mirrored volume from the specified drives.\n" "move | mv -f drive object ...\n" " Move the object(s) to the specified drive.\n" "quit Exit the vinum program when running in interactive mode." " Nor-\n" " mally this would be done by entering the EOF character.\n" "raid5 [-fv] [-s stripesize] [-n name] drives\n" " Create a RAID-5 volume from the specified drives.\n" "rename [-r] [drive | subdisk | plex | volume] newname\n" " Change the name of the specified object.\n" "rebuildparity plex [-f]\n" " Rebuild the parity blocks of a RAID-5 plex.\n" "resetconfig\n" " Reset the complete gvinum configuration\n" "rm [-r] [-f] volume | plex | subdisk | drive\n" " Remove an object.\n" "saveconfig\n" " Save vinum configuration to disk after configuration" " failures.\n" "setstate [-f] state [volume | plex | subdisk | drive]\n" " Set state without influencing other objects, for" " diagnostic pur-\n" " poses only.\n" "start [-S size] volume | plex | subdisk\n" " Allow the system to access the objects.\n" "stripe [-fv] [-n name] drives\n" " Create a striped volume from the specified drives.\n" ); return; } void gvinum_setstate(int argc, char **argv) { struct gctl_req *req; int flags, i; const char *errstr; flags = 0; optreset = 1; optind = 1; while ((i = getopt(argc, argv, "f")) != -1) { switch (i) { case 'f': flags |= GV_FLAG_F; break; case '?': default: warn("invalid flag: %c", i); return; } } argc -= optind; argv += optind; if (argc != 2) { warnx("usage: setstate [-f] "); return; } /* * XXX: This hack is needed to avoid tripping over (now) invalid * 'classic' vinum states and will go away later. */ if (strcmp(argv[0], "up") && strcmp(argv[0], "down") && strcmp(argv[0], "stale")) { warnx("invalid state '%s'", argv[0]); return; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "setstate"); gctl_ro_param(req, "state", -1, argv[0]); gctl_ro_param(req, "object", -1, argv[1]); gctl_ro_param(req, "flags", sizeof(int), &flags); errstr = gctl_issue(req); if (errstr != NULL) warnx("%s", errstr); gctl_free(req); } void gvinum_list(int argc, char **argv) { struct gctl_req *req; int flags, i, j; const char *errstr; char buf[20], *cmd, config[GV_CFG_LEN + 1]; flags = 0; cmd = "list"; if (argc) { optreset = 1; optind = 1; cmd = argv[0]; while ((j = getopt(argc, argv, "rsvV")) != -1) { switch (j) { case 'r': flags |= GV_FLAG_R; break; case 's': flags |= GV_FLAG_S; break; case 'v': flags |= GV_FLAG_V; break; case 'V': flags |= GV_FLAG_V; flags |= GV_FLAG_VV; break; case '?': default: return; } } argc -= optind; argv += optind; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "list"); gctl_ro_param(req, "cmd", -1, cmd); gctl_ro_param(req, "argc", sizeof(int), &argc); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_rw_param(req, "config", sizeof(config), config); if (argc) { for (i = 0; i < argc; i++) { snprintf(buf, sizeof(buf), "argv%d", i); gctl_ro_param(req, buf, -1, argv[i]); } } errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't get configuration: %s", errstr); gctl_free(req); return; } printf("%s", config); gctl_free(req); return; } /* Create a mirrored volume. */ void gvinum_mirror(int argc, char **argv) { if (argc < 2) { warnx("usage\tmirror [-fsv] [-n name] drives\n"); return; } create_volume(argc, argv, "mirror"); } /* Note that move is currently of form '[-r] target object [...]' */ void gvinum_move(int argc, char **argv) { struct gctl_req *req; const char *errstr; char buf[20]; int flags, i, j; flags = 0; if (argc) { optreset = 1; optind = 1; while ((j = getopt(argc, argv, "f")) != -1) { switch (j) { case 'f': flags |= GV_FLAG_F; break; case '?': default: return; } } argc -= optind; argv += optind; } switch (argc) { case 0: warnx("no destination or object(s) to move specified"); return; case 1: warnx("no object(s) to move specified"); return; default: break; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "move"); gctl_ro_param(req, "argc", sizeof(int), &argc); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "destination", -1, argv[0]); for (i = 1; i < argc; i++) { snprintf(buf, sizeof(buf), "argv%d", i); gctl_ro_param(req, buf, -1, argv[i]); } errstr = gctl_issue(req); if (errstr != NULL) warnx("can't move object(s): %s", errstr); gctl_free(req); return; } void gvinum_printconfig(int argc, char **argv) { printconfig(stdout, ""); } void gvinum_parityop(int argc, char **argv, int rebuild) { struct gctl_req *req; int flags, i; const char *errstr; char *op, *msg; if (rebuild) { op = "rebuildparity"; msg = "Rebuilding"; } else { op = "checkparity"; msg = "Checking"; } optreset = 1; optind = 1; flags = 0; while ((i = getopt(argc, argv, "fv")) != -1) { switch (i) { case 'f': flags |= GV_FLAG_F; break; case 'v': flags |= GV_FLAG_V; break; case '?': default: warnx("invalid flag '%c'", i); return; } } argc -= optind; argv += optind; if (argc != 1) { warn("usage: %s [-f] [-v] ", op); return; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, op); gctl_ro_param(req, "rebuild", sizeof(int), &rebuild); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "plex", -1, argv[0]); errstr = gctl_issue(req); if (errstr) warnx("%s\n", errstr); gctl_free(req); } /* Create a RAID-5 volume. */ void gvinum_raid5(int argc, char **argv) { if (argc < 2) { warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n"); return; } create_volume(argc, argv, "raid5"); } void gvinum_rename(int argc, char **argv) { struct gctl_req *req; const char *errstr; int flags, j; flags = 0; if (argc) { optreset = 1; optind = 1; while ((j = getopt(argc, argv, "r")) != -1) { switch (j) { case 'r': flags |= GV_FLAG_R; break; case '?': default: return; } } argc -= optind; argv += optind; } switch (argc) { case 0: warnx("no object to rename specified"); return; case 1: warnx("no new name specified"); return; case 2: break; default: warnx("more than one new name specified"); return; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "rename"); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "object", -1, argv[0]); gctl_ro_param(req, "newname", -1, argv[1]); errstr = gctl_issue(req); if (errstr != NULL) warnx("can't rename object: %s", errstr); gctl_free(req); return; } void gvinum_rm(int argc, char **argv) { struct gctl_req *req; int flags, i, j; const char *errstr; char buf[20], *cmd; cmd = argv[0]; flags = 0; optreset = 1; optind = 1; while ((j = getopt(argc, argv, "rf")) != -1) { switch (j) { case 'f': flags |= GV_FLAG_F; break; case 'r': flags |= GV_FLAG_R; break; case '?': default: return; } } argc -= optind; argv += optind; req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "remove"); gctl_ro_param(req, "argc", sizeof(int), &argc); gctl_ro_param(req, "flags", sizeof(int), &flags); if (argc) { for (i = 0; i < argc; i++) { snprintf(buf, sizeof(buf), "argv%d", i); gctl_ro_param(req, buf, -1, argv[i]); } } errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't remove: %s", errstr); gctl_free(req); return; } gctl_free(req); } void gvinum_resetconfig(void) { struct gctl_req *req; const char *errstr; char reply[32]; if (!isatty(STDIN_FILENO)) { warn("Please enter this command from a tty device\n"); return; } printf(" WARNING! This command will completely wipe out your gvinum" "configuration.\n" " All data will be lost. If you really want to do this," " enter the text\n\n" " NO FUTURE\n" " Enter text -> "); fgets(reply, sizeof(reply), stdin); if (strcmp(reply, "NO FUTURE\n")) { printf("\n No change\n"); return; } req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "resetconfig"); errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't reset config: %s", errstr); gctl_free(req); return; } gctl_free(req); printf("gvinum configuration obliterated\n"); } void gvinum_saveconfig(void) { struct gctl_req *req; const char *errstr; req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "saveconfig"); errstr = gctl_issue(req); if (errstr != NULL) warnx("can't save configuration: %s", errstr); gctl_free(req); } void gvinum_start(int argc, char **argv) { struct gctl_req *req; int i, initsize, j; const char *errstr; char buf[20]; /* 'start' with no arguments is a no-op. */ if (argc == 1) return; initsize = 0; optreset = 1; optind = 1; while ((j = getopt(argc, argv, "S")) != -1) { switch (j) { case 'S': initsize = atoi(optarg); break; case '?': default: return; } } argc -= optind; argv += optind; if (!initsize) initsize = 512; req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "start"); gctl_ro_param(req, "argc", sizeof(int), &argc); gctl_ro_param(req, "initsize", sizeof(int), &initsize); if (argc) { for (i = 0; i < argc; i++) { snprintf(buf, sizeof(buf), "argv%d", i); gctl_ro_param(req, buf, -1, argv[i]); } } errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't start: %s", errstr); gctl_free(req); return; } gctl_free(req); } void gvinum_stop(int argc, char **argv) { int err, fileid; fileid = kldfind(GVINUMMOD); if (fileid == -1) { warn("cannot find " GVINUMMOD); return; } /* * This little hack prevents that we end up in an infinite loop in * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM * event thread will be free for the g_wither_geom() call from * gv_unload(). It's silly, but it works. */ printf("unloading " GVINUMMOD " kernel module... "); fflush(stdout); if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) { sleep(1); err = kldunload(fileid); } if (err != 0) { printf(" failed!\n"); warn("cannot unload " GVINUMMOD); return; } printf("done\n"); exit(0); } /* Create a striped volume. */ void gvinum_stripe(int argc, char **argv) { if (argc < 2) { warnx("usage:\tstripe [-fv] [-n name] drives\n"); return; } create_volume(argc, argv, "stripe"); } /* Grow a subdisk by adding disk backed by provider. */ void gvinum_grow(int argc, char **argv) { struct gctl_req *req; char *drive, *sdname; char sdprefix[GV_MAXSDNAME]; struct gv_drive *d; struct gv_sd *s; const char *errstr; int drives, volumes, plexes, subdisks, flags; drives = volumes = plexes = subdisks = 0; if (argc < 3) { warnx("usage:\tgrow plex drive\n"); return; } s = gv_alloc_sd(); if (s == NULL) { warn("unable to create subdisk"); return; } d = gv_alloc_drive(); if (d == NULL) { warn("unable to create drive"); free(s); return; } /* Lookup device and set an appropriate drive name. */ drive = find_drive(); if (drive == NULL) { warn("unable to find an appropriate drive name"); free(s); free(d); return; } strlcpy(d->name, drive, sizeof(d->name)); copy_device(d, argv[2]); drives = 1; /* We try to use the plex name as basis for the subdisk name. */ snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]); sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME); if (sdname == NULL) { warn("unable to find an appropriate subdisk name"); free(s); free(d); free(drive); return; } strlcpy(s->name, sdname, sizeof(s->name)); free(sdname); strlcpy(s->plex, argv[1], sizeof(s->plex)); strlcpy(s->drive, d->name, sizeof(s->drive)); subdisks = 1; req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "create"); gctl_ro_param(req, "flags", sizeof(int), &flags); gctl_ro_param(req, "volumes", sizeof(int), &volumes); gctl_ro_param(req, "plexes", sizeof(int), &plexes); gctl_ro_param(req, "subdisks", sizeof(int), &subdisks); gctl_ro_param(req, "drives", sizeof(int), &drives); gctl_ro_param(req, "drive0", sizeof(*d), d); gctl_ro_param(req, "sd0", sizeof(*s), s); errstr = gctl_issue(req); free(drive); if (errstr != NULL) { warnx("unable to grow plex: %s", errstr); free(s); free(d); return; } gctl_free(req); } void parseline(int argc, char **argv) { if (argc <= 0) return; if (!strcmp(argv[0], "create")) gvinum_create(argc, argv); else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit")) exit(0); else if (!strcmp(argv[0], "attach")) gvinum_attach(argc, argv); else if (!strcmp(argv[0], "detach")) gvinum_detach(argc, argv); else if (!strcmp(argv[0], "concat")) gvinum_concat(argc, argv); else if (!strcmp(argv[0], "grow")) gvinum_grow(argc, argv); else if (!strcmp(argv[0], "help")) gvinum_help(); else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l")) gvinum_list(argc, argv); else if (!strcmp(argv[0], "ld")) gvinum_list(argc, argv); else if (!strcmp(argv[0], "lp")) gvinum_list(argc, argv); else if (!strcmp(argv[0], "ls")) gvinum_list(argc, argv); else if (!strcmp(argv[0], "lv")) gvinum_list(argc, argv); else if (!strcmp(argv[0], "mirror")) gvinum_mirror(argc, argv); else if (!strcmp(argv[0], "move")) gvinum_move(argc, argv); else if (!strcmp(argv[0], "mv")) gvinum_move(argc, argv); else if (!strcmp(argv[0], "printconfig")) gvinum_printconfig(argc, argv); else if (!strcmp(argv[0], "raid5")) gvinum_raid5(argc, argv); else if (!strcmp(argv[0], "rename")) gvinum_rename(argc, argv); else if (!strcmp(argv[0], "resetconfig")) gvinum_resetconfig(); else if (!strcmp(argv[0], "rm")) gvinum_rm(argc, argv); else if (!strcmp(argv[0], "saveconfig")) gvinum_saveconfig(); else if (!strcmp(argv[0], "setstate")) gvinum_setstate(argc, argv); else if (!strcmp(argv[0], "start")) gvinum_start(argc, argv); else if (!strcmp(argv[0], "stop")) gvinum_stop(argc, argv); else if (!strcmp(argv[0], "stripe")) gvinum_stripe(argc, argv); else if (!strcmp(argv[0], "checkparity")) gvinum_parityop(argc, argv, 0); else if (!strcmp(argv[0], "rebuildparity")) gvinum_parityop(argc, argv, 1); else printf("unknown command '%s'\n", argv[0]); return; } /* * The guts of printconfig. This is called from gvinum_printconfig and from * gvinum_create when called without an argument, in order to give the user * something to edit. */ void printconfig(FILE *of, char *comment) { struct gctl_req *req; struct utsname uname_s; const char *errstr; time_t now; char buf[GV_CFG_LEN + 1]; uname(&uname_s); time(&now); req = gctl_get_handle(); gctl_ro_param(req, "class", -1, "VINUM"); gctl_ro_param(req, "verb", -1, "getconfig"); gctl_ro_param(req, "comment", -1, comment); gctl_rw_param(req, "config", sizeof(buf), buf); errstr = gctl_issue(req); if (errstr != NULL) { warnx("can't get configuration: %s", errstr); return; } gctl_free(req); fprintf(of, "# Vinum configuration of %s, saved at %s", uname_s.nodename, ctime(&now)); if (*comment != '\0') fprintf(of, "# Current configuration:\n"); fprintf(of, buf); }