diff options
Diffstat (limited to 'usr.bin/mt/mt.c')
-rw-r--r-- | usr.bin/mt/mt.c | 1266 |
1 files changed, 1109 insertions, 157 deletions
diff --git a/usr.bin/mt/mt.c b/usr.bin/mt/mt.c index 3fbbdf3..745766c 100644 --- a/usr.bin/mt/mt.c +++ b/usr.bin/mt/mt.c @@ -26,6 +26,37 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +/*- + * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation + * 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. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES. + * + * Authors: Ken Merry (Spectra Logic Corporation) + */ #ifndef lint static const char copyright[] = @@ -49,6 +80,8 @@ __FBSDID("$FreeBSD$"); #include <sys/types.h> #include <sys/ioctl.h> #include <sys/mtio.h> +#include <sys/queue.h> +#include <sys/sbuf.h> #include <ctype.h> #include <err.h> @@ -57,6 +90,16 @@ __FBSDID("$FreeBSD$"); #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <stdint.h> +#include <errno.h> +#include <bsdxml.h> +#include <mtlib.h> + +#include <cam/cam.h> +#include <cam/cam_ccb.h> +#include <cam/cam_periph.h> +#include <cam/scsi/scsi_all.h> +#include <cam/scsi/scsi_sa.h> /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */ /* c_flags */ @@ -65,6 +108,7 @@ __FBSDID("$FreeBSD$"); #define IS_DENSITY 0x04 #define DISABLE_THIS 0x08 #define IS_COMP 0x10 +#define USE_GETOPT 0x20 #ifndef TRUE #define TRUE 1 @@ -72,6 +116,16 @@ __FBSDID("$FreeBSD$"); #ifndef FALSE #define FALSE 0 #endif +#ifndef MAX +#define MAX(a, b) (a > b) ? a : b +#endif +#define MT_PLURAL(a) (a == 1) ? "" : "s" + +typedef enum { + MT_CMD_NONE = MTLOAD + 1, + MT_CMD_PROTECT, + MT_CMD_GETDENSITY +} mt_commands; static const struct commands { const char *c_name; @@ -87,10 +141,12 @@ static const struct commands { { "fsf", MTFSF, 1, 0 }, { "fsr", MTFSR, 1, 0 }, { "offline", MTOFFL, 1, 0 }, + { "load", MTLOAD, 1, 0 }, { "rewind", MTREW, 1, 0 }, { "rewoffl", MTOFFL, 1, 0 }, - { "status", MTNOP, 1, 0 }, + { "ostatus", MTNOP, 1, 0 }, { "weof", MTWEOF, 0, ZERO_ALLOWED }, + { "weofi", MTWEOFI, 0, ZERO_ALLOWED }, { "erase", MTERASE, 0, ZERO_ALLOWED}, { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED }, { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY }, @@ -111,17 +167,39 @@ static const struct commands { { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED }, { "getmodel", MTIOCGETEOTMODEL, 0, 0 }, { "geteotmodel", MTIOCGETEOTMODEL, 0, 0 }, + { "rblim", MTIOCRBLIM, 0, 0}, + { "getdensity", MT_CMD_GETDENSITY, 0, USE_GETOPT}, + { "status", MTIOCEXTGET, 0, USE_GETOPT }, + { "locate", MTIOCEXTLOCATE, 0, USE_GETOPT }, + { "param", MTIOCPARAMGET, 0, USE_GETOPT }, + { "protect", MT_CMD_PROTECT, 0, USE_GETOPT }, { NULL, 0, 0, 0 } }; + static const char *getblksiz(int); static void printreg(const char *, u_int, const char *); static void status(struct mtget *); static void usage(void); -static void st_status(struct mtget *); -static int stringtodens(const char *s); -static const char *denstostring(int d); -static int denstobp(int d, int bpi); +const char *get_driver_state_str(int dsreg); +static void st_status (struct mtget *); +static int mt_locate(int argc, char **argv, int mtfd, const char *tape); +static int nstatus_print(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data); +static int mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, + const char *tape); +static int mt_print_density_entry(struct mt_status_entry *density_root, int indent); +static int mt_print_density_report(struct mt_status_entry *report_root, int indent); +static int mt_print_density(struct mt_status_entry *density_root, int indent); +static int mt_getdensity(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data); +static int mt_set_param(int mtfd, struct mt_status_data *status_data, + char *param_name, char *param_value); +static int mt_protect(int argc, char **argv, int mtfd, + struct mt_status_data *status_data); +static int mt_param(int argc, char **argv, int mtfd, char *xml_str, + struct mt_status_data *status_data); +static const char *denstostring (int d); static u_int32_t stringtocomp(const char *s); static const char *comptostring(u_int32_t comp); static void warn_eof(void); @@ -135,6 +213,8 @@ main(int argc, char *argv[]) int ch, len, mtfd; const char *p, *tape; + bzero(&mt_com, sizeof(mt_com)); + if ((tape = getenv("TAPE")) == NULL) tape = DEFTAPE; @@ -145,13 +225,15 @@ main(int argc, char *argv[]) tape = optarg; break; case '?': - default: usage(); + break; + default: + break; } argc -= optind; argv += optind; - if (argc < 1 || argc > 2) + if (argc < 1) usage(); len = strlen(p = *argv++); @@ -166,6 +248,11 @@ main(int argc, char *argv[]) if(comp->c_flags & DISABLE_THIS) { warn_eof(); } + if (comp->c_flags & USE_GETOPT) { + argc--; + optind = 0; + } + if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) err(1, "%s", tape); if (comp->c_code != MTNOP) { @@ -174,7 +261,7 @@ main(int argc, char *argv[]) if (!isdigit(**argv) && (comp->c_flags & IS_DENSITY)) { const char *dcanon; - mt_com.mt_count = stringtodens(*argv); + mt_com.mt_count = mt_density_num(*argv); if (mt_com.mt_count == 0) errx(1, "%s: unknown density", *argv); dcanon = denstostring(mt_com.mt_count); @@ -191,16 +278,17 @@ main(int argc, char *argv[]) errx(1, "%s: unknown compression", *argv); p = ""; - } else { + } else if ((comp->c_flags & USE_GETOPT) == 0) { char *q; /* allow for hex numbers; useful for density */ mt_com.mt_count = strtol(*argv, &q, 0); p = q; } - if ((mt_com.mt_count <= - ((comp->c_flags & ZERO_ALLOWED)? -1: 0) - && ((comp->c_flags & IS_COMP) == 0) - ) || *p) + if (((comp->c_flags & USE_GETOPT) == 0) + && (((mt_com.mt_count <= + ((comp->c_flags & ZERO_ALLOWED)? -1: 0)) + && ((comp->c_flags & IS_COMP) == 0)) + || *p)) errx(1, "%s: illegal count", *argv); } else @@ -289,6 +377,47 @@ main(int argc, char *argv[]) exit(0); /* NOTREACHED */ } + case MTIOCRBLIM: + { + struct mtrblim rblim; + + bzero(&rblim, sizeof(rblim)); + + if (ioctl(mtfd, MTIOCRBLIM, (caddr_t)&rblim) < 0) + err(2, "%s", tape); + (void)printf("%s:\n" + " min blocksize %u byte%s\n" + " max blocksize %u byte%s\n" + " granularity %u byte%s\n", + tape, rblim.min_block_length, + MT_PLURAL(rblim.min_block_length), + rblim.max_block_length, + MT_PLURAL(rblim.max_block_length), + (1 << rblim.granularity), + MT_PLURAL((1 << rblim.granularity))); + exit(0); + /* NOTREACHED */ + } + case MTIOCPARAMGET: + case MTIOCEXTGET: + case MT_CMD_PROTECT: + case MT_CMD_GETDENSITY: + { + int retval = 0; + + retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd, + tape); + + exit(retval); + } + case MTIOCEXTLOCATE: + { + int retval = 0; + + retval = mt_locate(argc, argv, mtfd, tape); + + exit(retval); + } default: break; } @@ -381,62 +510,6 @@ usage(void) exit(1); } -static const struct densities { - int dens; - int bpmm; - int bpi; - const char *name; -} dens[] = { - /* - * Taken from T10 Project 997D - * SCSI-3 Stream Device Commands (SSC) - * Revision 11, 4-Nov-97 - */ - /*Num. bpmm bpi Reference */ - { 0x1, 32, 800, "X3.22-1983" }, - { 0x2, 63, 1600, "X3.39-1986" }, - { 0x3, 246, 6250, "X3.54-1986" }, - { 0x5, 315, 8000, "X3.136-1986" }, - { 0x6, 126, 3200, "X3.157-1987" }, - { 0x7, 252, 6400, "X3.116-1986" }, - { 0x8, 315, 8000, "X3.158-1987" }, - { 0x9, 491, 37871, "X3.180" }, - { 0xA, 262, 6667, "X3B5/86-199" }, - { 0xB, 63, 1600, "X3.56-1986" }, - { 0xC, 500, 12690, "HI-TC1" }, - { 0xD, 999, 25380, "HI-TC2" }, - { 0xF, 394, 10000, "QIC-120" }, - { 0x10, 394, 10000, "QIC-150" }, - { 0x11, 630, 16000, "QIC-320" }, - { 0x12, 2034, 51667, "QIC-1350" }, - { 0x13, 2400, 61000, "X3B5/88-185A" }, - { 0x14, 1703, 43245, "X3.202-1991" }, - { 0x15, 1789, 45434, "ECMA TC17" }, - { 0x16, 394, 10000, "X3.193-1990" }, - { 0x17, 1673, 42500, "X3B5/91-174" }, - { 0x18, 1673, 42500, "X3B5/92-50" }, - { 0x19, 2460, 62500, "DLTapeIII" }, - { 0x1A, 3214, 81633, "DLTapeIV(20GB)" }, - { 0x1B, 3383, 85937, "DLTapeIV(35GB)" }, - { 0x1C, 1654, 42000, "QIC-385M" }, - { 0x1D, 1512, 38400, "QIC-410M" }, - { 0x1E, 1385, 36000, "QIC-1000C" }, - { 0x1F, 2666, 67733, "QIC-2100C" }, - { 0x20, 2666, 67733, "QIC-6GB(M)" }, - { 0x21, 2666, 67733, "QIC-20GB(C)" }, - { 0x22, 1600, 40640, "QIC-2GB(C)" }, - { 0x23, 2666, 67733, "QIC-875M" }, - { 0x24, 2400, 61000, "DDS-2" }, - { 0x25, 3816, 97000, "DDS-3" }, - { 0x26, 3816, 97000, "DDS-4" }, - { 0x27, 3056, 77611, "Mammoth" }, - { 0x28, 1491, 37871, "X3.224" }, - { 0x41, 3868, 98250, "DLTapeIV(40GB)" }, - { 0x48, 5236, 133000, "SDLTapeI(110)" }, - { 0x49, 7598, 193000, "SDLTapeI(160)" }, - { 0, 0, 0, NULL } -}; - static const struct compression_types { u_int32_t comp_number; const char *name; @@ -454,58 +527,15 @@ static const char * denstostring(int d) { static char buf[20]; - const struct densities *sd; + const char *name = mt_density_name(d); - /* densities 0 and 0x7f are handled as special cases */ - if (d == 0) - return "default"; - if (d == 0x7f) - return "same"; - for (sd = dens; sd->dens; sd++) - if (sd->dens == d) - break; - if (sd->dens == 0) + if (name == NULL) sprintf(buf, "0x%02x", d); else - sprintf(buf, "0x%02x:%s", d, sd->name); + sprintf(buf, "0x%02x:%s", d, name); return buf; } -/* - * Given a specific density number, return either the bits per inch or bits - * per millimeter for the given density. - */ -static int -denstobp(int d, int bpi) -{ - const struct densities *sd; - - for (sd = dens; sd->dens; sd++) - if (sd->dens == d) - break; - if (sd->dens == 0) - return(0); - else { - if (bpi) - return(sd->bpi); - else - return(sd->bpmm); - } -} - -static int -stringtodens(const char *s) -{ - const struct densities *sd; - size_t l = strlen(s); - - for (sd = dens; sd->dens; sd++) - if (strncasecmp(sd->name, s, l) == 0) - break; - return sd->dens; -} - - static const char * getblksiz(int bs) { @@ -553,6 +583,38 @@ stringtocomp(const char *s) return(ct->comp_number); } +static struct driver_state { + int dsreg; + const char *desc; +} driver_states[] = { + { MTIO_DSREG_REST, "at rest" }, + { MTIO_DSREG_RBSY, "Communicating with drive" }, + { MTIO_DSREG_WR, "Writing" }, + { MTIO_DSREG_FMK, "Writing Filemarks" }, + { MTIO_DSREG_ZER, "Erasing" }, + { MTIO_DSREG_RD, "Reading" }, + { MTIO_DSREG_FWD, "Spacing Forward" }, + { MTIO_DSREG_REV, "Spacing Reverse" }, + { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" }, + { MTIO_DSREG_REW, "Rewinding" }, + { MTIO_DSREG_TEN, "Retensioning" }, + { MTIO_DSREG_UNL, "Unloading" }, + { MTIO_DSREG_LD, "Loading" }, +}; + +const char * +get_driver_state_str(int dsreg) +{ + unsigned int i; + + for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) { + if (driver_states[i].dsreg == dsreg) + return (driver_states[i].desc); + } + + return (NULL); +} + static void st_status(struct mtget *bp) { @@ -565,73 +627,963 @@ st_status(struct mtget *bp) "2: %-17s %-12s %-7d %s\n" "3: %-17s %-12s %-7d %s\n", denstostring(bp->mt_density), getblksiz(bp->mt_blksiz), - denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp), + mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp), denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0), - denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0), + mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0), denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1), - denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1), + mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1), denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2), - denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2), + mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2), denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3), - denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3)); + mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3)); if (bp->mt_dsreg != MTIO_DSREG_NIL) { - auto char foo[32]; const char sfmt[] = "Current Driver State: %s.\n"; printf("---------------------------------\n"); - switch (bp->mt_dsreg) { - case MTIO_DSREG_REST: - printf(sfmt, "at rest"); + const char *state_str; + + state_str = get_driver_state_str(bp->mt_dsreg); + if (state_str == NULL) { + char foo[32]; + (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg); + printf(sfmt, foo); + } else { + printf(sfmt, state_str); + } + } + if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 && + bp->mt_blkno == (daddr_t) -1) + return; + printf("---------------------------------\n"); + printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n", + bp->mt_fileno, bp->mt_blkno, bp->mt_resid); +} + +static int +mt_locate(int argc, char **argv, int mtfd, const char *tape) +{ + struct mtlocate mtl; + uint64_t logical_id = 0; + mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE; + int eod = 0, explicit = 0, immediate = 0; + int64_t partition = 0; + int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0; + int c, retval; + + retval = 0; + bzero(&mtl, sizeof(mtl)); + + while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) { + switch (c) { + case 'b': + /* Block address */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_OBJECT; + block_addr_set = 1; break; - case MTIO_DSREG_RBSY: - printf(sfmt, "Communicating with drive"); + case 'e': + /* end of data */ + eod = 1; + dest_type = MT_LOCATE_DEST_EOD; break; - case MTIO_DSREG_WR: - printf(sfmt, "Writing"); + case 'E': + /* + * XXX KDM explicit address mode. Should we even + * allow this, since the driver doesn't operate in + * explicit address mode? + */ + explicit = 1; break; - case MTIO_DSREG_FMK: - printf(sfmt, "Writing Filemarks"); + case 'f': + /* file number */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_FILE; + file_set = 1; break; - case MTIO_DSREG_ZER: - printf(sfmt, "Erasing"); + case 'i': + /* + * Immediate address mode. XXX KDM do we want to + * implement this? The other commands in the + * tape driver will need to be able to handle this. + */ + immediate = 1; break; - case MTIO_DSREG_RD: - printf(sfmt, "Reading"); + case 'p': + /* + * Change partition to the given partition. + */ + partition = strtol(optarg, NULL, 0); + partition_set = 1; break; - case MTIO_DSREG_FWD: - printf(sfmt, "Spacing Forward"); + case 's': + /* Go to the given set mark */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_SET; + set_set = 1; break; - case MTIO_DSREG_REV: - printf(sfmt, "Spacing Reverse"); + default: break; - case MTIO_DSREG_POS: - printf(sfmt, - "Hardware Positioning (direction unknown)"); + } + } + + /* + * These options are mutually exclusive. The user may only specify + * one. + */ + if ((block_addr_set + file_set + eod + set_set) != 1) + errx(1, "You must specify only one of -b, -f, -e, or -s"); + + mtl.dest_type = dest_type; + switch (dest_type) { + case MT_LOCATE_DEST_OBJECT: + case MT_LOCATE_DEST_FILE: + case MT_LOCATE_DEST_SET: + mtl.logical_id = logical_id; + break; + case MT_LOCATE_DEST_EOD: + break; + } + + if (immediate != 0) + mtl.flags |= MT_LOCATE_FLAG_IMMED; + + if (partition_set != 0) { + mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART; + mtl.partition = partition; + } + + if (explicit != 0) + mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT; + else + mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT; + + if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1) + err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape); + + return (retval); +} + +typedef enum { + MT_PERIPH_NAME = 0, + MT_UNIT_NUMBER = 1, + MT_VENDOR = 2, + MT_PRODUCT = 3, + MT_REVISION = 4, + MT_COMPRESSION_SUPPORTED = 5, + MT_COMPRESSION_ENABLED = 6, + MT_COMPRESSION_ALGORITHM = 7, + MT_MEDIA_DENSITY = 8, + MT_MEDIA_BLOCKSIZE = 9, + MT_CALCULATED_FILENO = 10, + MT_CALCULATED_REL_BLKNO = 11, + MT_REPORTED_FILENO = 12, + MT_REPORTED_BLKNO = 13, + MT_PARTITION = 14, + MT_BOP = 15, + MT_EOP = 16, + MT_BPEW = 17, + MT_DSREG = 18, + MT_RESID = 19, + MT_FIXED_MODE = 20, + MT_SERIAL_NUM = 21, + MT_MAXIO = 22, + MT_CPI_MAXIO = 23, + MT_MAX_BLK = 24, + MT_MIN_BLK = 25, + MT_BLK_GRAN = 26, + MT_MAX_EFF_IOSIZE = 27 +} status_item_index; + +static struct mt_status_items { + const char *name; + struct mt_status_entry *entry; +} req_status_items[] = { + { "periph_name", NULL }, + { "unit_number", NULL }, + { "vendor", NULL }, + { "product", NULL }, + { "revision", NULL }, + { "compression_supported", NULL }, + { "compression_enabled", NULL }, + { "compression_algorithm", NULL }, + { "media_density", NULL }, + { "media_blocksize", NULL }, + { "calculated_fileno", NULL }, + { "calculated_rel_blkno", NULL }, + { "reported_fileno", NULL }, + { "reported_blkno", NULL }, + { "partition", NULL }, + { "bop", NULL }, + { "eop", NULL }, + { "bpew", NULL }, + { "dsreg", NULL }, + { "residual", NULL }, + { "fixed_mode", NULL }, + { "serial_num", NULL }, + { "maxio", NULL }, + { "cpi_maxio", NULL }, + { "max_blk", NULL }, + { "min_blk", NULL }, + { "blk_gran", NULL }, + { "max_effective_iosize", NULL } +}; + +int +nstatus_print(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data) +{ + unsigned int i; + int64_t calculated_fileno, calculated_rel_blkno; + int64_t rep_fileno, rep_blkno, partition, resid; + char block_str[32]; + const char *dens_str; + int dsreg, bop, eop, bpew; + int xml_dump = 0; + size_t dens_len; + unsigned int field_width; + int verbose = 0; + int c; + + while ((c = getopt(argc, argv, "xv")) != -1) { + switch (c) { + case 'x': + xml_dump = 1; break; - case MTIO_DSREG_REW: - printf(sfmt, "Rewinding"); + case 'v': + verbose = 1; break; - case MTIO_DSREG_TEN: - printf(sfmt, "Retensioning"); + default: break; - case MTIO_DSREG_UNL: - printf(sfmt, "Unloading"); + } + } + + if (xml_dump != 0) { + printf("%s", xml_str); + return (0); + } + + for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0])); + i++) { + char *name; + + name = __DECONST(char *, req_status_items[i].name); + req_status_items[i].entry = mt_status_entry_find(status_data, + name); + if (req_status_items[i].entry == NULL) { + errx(1, "Cannot find status entry %s", + req_status_items[i].name); + } + } + + printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n", + req_status_items[MT_PERIPH_NAME].entry->value, + (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned, + req_status_items[MT_VENDOR].entry->value, + req_status_items[MT_PRODUCT].entry->value, + req_status_items[MT_REVISION].entry->value, + (req_status_items[MT_SERIAL_NUM].entry->value) ? + req_status_items[MT_SERIAL_NUM].entry->value : "none"); + printf("---------------------------------\n"); + + /* + * We check to see whether we're in fixed mode or not, and don't + * just believe the blocksize. If the SILI bit is turned on, the + * blocksize will be set to 4, even though we're doing variable + * length (well, multiples of 4) blocks. + */ + if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0) + snprintf(block_str, sizeof(block_str), "variable"); + else + snprintf(block_str, sizeof(block_str), "%s", + getblksiz(req_status_items[ + MT_MEDIA_BLOCKSIZE].entry->value_unsigned)); + + dens_str = denstostring(req_status_items[ + MT_MEDIA_DENSITY].entry->value_unsigned); + if (dens_str == NULL) + dens_len = 0; + else + dens_len = strlen(dens_str); + field_width = MAX(dens_len, 17); + printf("Mode %-*s Blocksize bpi Compression\n" + "Current: %-*s %-12s %-7d ", + field_width, "Density", field_width, dens_str, block_str, + mt_density_bp(req_status_items[ + MT_MEDIA_DENSITY].entry->value_unsigned, TRUE)); + + if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0) + printf("unsupported\n"); + else if (req_status_items[ + MT_COMPRESSION_ENABLED].entry->value_signed == 0) + printf("disabled\n"); + else { + printf("enabled (%s)\n", + comptostring(req_status_items[ + MT_COMPRESSION_ALGORITHM].entry->value_unsigned)); + } + + dsreg = req_status_items[MT_DSREG].entry->value_signed; + if (dsreg != MTIO_DSREG_NIL) { + const char sfmt[] = "Current Driver State: %s.\n"; + printf("---------------------------------\n"); + const char *state_str; + + state_str = get_driver_state_str(dsreg); + if (state_str == NULL) { + char foo[32]; + (void) sprintf(foo, "Unknown state 0x%x", dsreg); + printf(sfmt, foo); + } else { + printf(sfmt, state_str); + } + } + resid = req_status_items[MT_RESID].entry->value_signed; + calculated_fileno = req_status_items[ + MT_CALCULATED_FILENO].entry->value_signed; + calculated_rel_blkno = req_status_items[ + MT_CALCULATED_REL_BLKNO].entry->value_signed; + rep_fileno = req_status_items[ + MT_REPORTED_FILENO].entry->value_signed; + rep_blkno = req_status_items[ + MT_REPORTED_BLKNO].entry->value_signed; + bop = req_status_items[MT_BOP].entry->value_signed; + eop = req_status_items[MT_EOP].entry->value_signed; + bpew = req_status_items[MT_BPEW].entry->value_signed; + partition = req_status_items[MT_PARTITION].entry->value_signed; + + printf("---------------------------------\n"); + printf("Partition: %3jd Calc File Number: %3jd " + " Calc Record Number: %jd\n" + "Residual: %3jd Reported File Number: %3jd " + "Reported Record Number: %jd\n", partition, calculated_fileno, + calculated_rel_blkno, resid, rep_fileno, rep_blkno); + + printf("Flags: "); + if (bop > 0 || eop > 0 || bpew > 0) { + int need_comma = 0; + + if (bop > 0) { + printf("BOP"); + need_comma = 1; + } + if (eop > 0) { + if (need_comma != 0) + printf(","); + printf("EOP"); + need_comma = 1; + } + if (bpew > 0) { + if (need_comma != 0) + printf(","); + printf("BPEW"); + need_comma = 1; + } + } else { + printf("None"); + } + printf("\n"); + if (verbose != 0) { + printf("---------------------------------\n"); + printf("Tape I/O parameters:\n"); + for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) { + printf(" %s (%s): %ju bytes\n", + req_status_items[i].entry->desc, + req_status_items[i].name, + req_status_items[i].entry->value_unsigned); + } + } + + return (0); +} + +int +mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape) +{ + struct mt_status_data status_data; +#if 0 + struct mt_status_entry *entry; +#endif + char *xml_str; + int retval; + unsigned long ioctl_cmd; + + switch (cmd) { + case MT_CMD_PROTECT: + case MTIOCPARAMGET: + ioctl_cmd = MTIOCPARAMGET; + break; + default: + ioctl_cmd = MTIOCEXTGET; + break; + } + + retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str); + if (retval != 0) + err(1, "Couldn't get mt XML string"); + + retval = mt_get_status(xml_str, &status_data); + if (retval != XML_STATUS_OK) { + warn("Couldn't get mt status for %s", tape); + goto bailout; + } + + /* + * This gets set if there are memory allocation or other errors in + * our parsing of the XML. + */ + if (status_data.error != 0) { + warnx("%s", status_data.error_str); + retval = 1; + goto bailout; + } +#if 0 + STAILQ_FOREACH(entry, &status_data.entries, links) + mt_status_tree_print(entry, 0, NULL); +#endif + + switch (cmd) { + case MTIOCEXTGET: + retval = nstatus_print(argc, argv, xml_str, &status_data); + break; + case MTIOCPARAMGET: + retval = mt_param(argc, argv, mtfd, xml_str, &status_data); + break; + case MT_CMD_PROTECT: + retval = mt_protect(argc, argv, mtfd, &status_data); + break; + case MT_CMD_GETDENSITY: + retval = mt_getdensity(argc, argv, xml_str, &status_data); + break; + } + +bailout: + if (xml_str != NULL) + free(xml_str); + + mt_status_free(&status_data); + + return (retval); +} + +static int +mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name, + char *param_value) +{ + struct mt_status_entry *entry; + struct mtparamset param_set; + + entry = mt_status_entry_find(status_data, + __DECONST(char *, "mtparamget")); + if (entry == NULL) + errx(1, "Cannot find parameter root node"); + + bzero(¶m_set, sizeof(param_set)); + entry = mt_entry_find(entry, param_name); + if (entry == NULL) + errx(1, "Unknown parameter name \"%s\"", param_name); + + strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name)); + + switch (entry->var_type) { + case MT_TYPE_INT: + param_set.value.value_signed = strtoll(param_value, NULL, 0); + param_set.value_type = MT_PARAM_SET_SIGNED; + param_set.value_len = entry->size; + break; + case MT_TYPE_UINT: + param_set.value.value_unsigned = strtoull(param_value, NULL, 0); + param_set.value_type = MT_PARAM_SET_UNSIGNED; + param_set.value_len = entry->size; + break; + case MT_TYPE_STRING: { + size_t param_len; + + param_len = strlen(param_value) + 1; + if (param_len > sizeof(param_set.value.value_fixed_str)) { + param_set.value_type = MT_PARAM_SET_VAR_STR; + param_set.value.value_var_str = param_value; + } else { + param_set.value_type = MT_PARAM_SET_FIXED_STR; + strlcpy(param_set.value.value_fixed_str, param_value, + sizeof(param_set.value.value_fixed_str)); + } + param_set.value_len = param_len; + break; + } + default: + errx(1, "Unknown parameter type %d for %s", entry->var_type, + param_name); + break; + } + + if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1) + err(1, "MTIOCPARAMSET"); + + if (param_set.status != MT_PARAM_STATUS_OK) + errx(1, "Failed to set %s: %s", param_name, + param_set.error_str); + + return (0); +} + + +typedef enum { + MT_PP_LBP_R, + MT_PP_LBP_W, + MT_PP_RBDP, + MT_PP_PI_LENGTH, + MT_PP_PROT_METHOD +} mt_protect_param; + +static struct mt_protect_info { + const char *name; + struct mt_status_entry *entry; + uint32_t value; +} mt_protect_list[] = { + { "lbp_r", NULL, 0 }, + { "lbp_w", NULL, 0 }, + { "rbdp", NULL, 0 }, + { "pi_length", NULL, 0 }, + { "prot_method", NULL, 0 } +}; + +#define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0])) + +#define MT_PROT_NAME "protection" + +static int +mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data) +{ + int retval = 0; + int do_enable = 0, do_disable = 0, do_list = 0; + int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0; + int prot_method_set = 0, pi_length_set = 0; + int verbose = 0; + uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0; + uint32_t prot_method = 0, pi_length = 0; + struct mt_status_entry *prot_entry, *supported_entry; + struct mt_status_entry *entry; + struct mtparamset params[MT_NUM_PROTECT_PARAMS]; + struct mtsetlist param_list; + unsigned int i; + int c; + + while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) { + switch (c) { + case 'b': + rbdp_set = 1; + rbdp = strtoul(optarg, NULL, 0); + if ((rbdp != 0) && (rbdp != 1)) + errx(1, "valid values for -b are 0 and 1"); break; - case MTIO_DSREG_LD: - printf(sfmt, "Loading"); + case 'd': + do_disable = 1; + break; + case 'e': + do_enable = 1; + break; + case 'l': + do_list = 1; + break; + case 'L': + pi_length_set = 1; + pi_length = strtoul(optarg, NULL, 0); + if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK) + errx(1, "PI length %u > maximum %u", + pi_length, SA_CTRL_DP_PI_LENGTH_MASK); + break; + case 'm': + prot_method_set = 1; + prot_method = strtoul(optarg, NULL, 0); + if (prot_method > SA_CTRL_DP_METHOD_MAX) + errx(1, "Method %u > maximum %u", + prot_method, SA_CTRL_DP_METHOD_MAX); + break; + case 'r': + lbp_r_set = 1; + lbp_r = strtoul(optarg, NULL, 0); + if ((lbp_r != 0) && (lbp_r != 1)) + errx(1, "valid values for -r are 0 and 1"); + break; + case 'v': + verbose = 1; + break; + case 'w': + lbp_w_set = 1; + lbp_w = strtoul(optarg, NULL, 0); + if ((lbp_w != 0) && (lbp_r != 1)) + errx(1, "valid values for -r are 0 and 1"); break; default: - (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg); - printf(sfmt, foo); break; } } - if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 && - bp->mt_blkno == (daddr_t) -1) - return; - printf("---------------------------------\n"); - printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n", - bp->mt_fileno, bp->mt_blkno, bp->mt_resid); + + if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set + + prot_method_set + lbp_r_set + lbp_w_set) == 0) + errx(1, "Need an argument for protect"); + + if ((do_disable + do_enable + do_list) != 1) + errx(1, "You must specify only one of -e, -d or -l"); + + if (do_list != 0) { + retval = mt_protect_print(status_data, verbose); + goto bailout; + } + if (do_enable != 0) { + /* + * Enable protection, but allow the user to override + * settings if he doesn't want everything turned on. + */ + if (rbdp_set == 0) + rbdp = 1; + if (lbp_w_set == 0) + lbp_w = 1; + if (lbp_r_set == 0) + lbp_r = 1; + /* + * If the user doesn't override it, we default to enabling + * Reed-Solomon checkums. + */ + if (prot_method_set == 0) + prot_method = SA_CTRL_DP_REED_SOLOMON; + if (pi_length_set == 0) + pi_length = SA_CTRL_DP_RS_LENGTH; + } else if (do_disable != 0) { + /* + * If the user wants to disable protection, we ignore any + * other parameters he has set. Everything gets set to 0. + */ + rbdp = lbp_w = lbp_r = 0; + prot_method = pi_length = 0; + } + + prot_entry = mt_status_entry_find(status_data, + __DECONST(char *, MT_PROT_NAME)); + if (prot_entry == NULL) + errx(1, "Unable to find protection information status"); + + supported_entry = mt_entry_find(prot_entry, + __DECONST(char *, "protection_supported")); + if (supported_entry == NULL) + errx(1, "Unable to find protection support information"); + + if (((supported_entry->var_type == MT_TYPE_INT) + && (supported_entry->value_signed == 0)) + || ((supported_entry->var_type == MT_TYPE_UINT) + && (supported_entry->value_unsigned == 0))) + errx(1, "This device does not support protection information"); + + mt_protect_list[MT_PP_LBP_R].value = lbp_r; + mt_protect_list[MT_PP_LBP_W].value = lbp_w; + mt_protect_list[MT_PP_RBDP].value = rbdp; + mt_protect_list[MT_PP_PI_LENGTH].value = pi_length; + mt_protect_list[MT_PP_PROT_METHOD].value = prot_method; + + bzero(¶ms, sizeof(params)); + bzero(¶m_list, sizeof(param_list)); + + /* + * Go through the list and make sure that we have this parameter, + * and that it is still an unsigned integer. If not, we've got a + * problem. + */ + for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) { + entry = mt_entry_find(prot_entry, + __DECONST(char *, mt_protect_list[i].name)); + if (entry == NULL) { + errx(1, "Unable to find parameter %s", + mt_protect_list[i].name); + } + mt_protect_list[i].entry = entry; + + if (entry->var_type != MT_TYPE_UINT) + errx(1, "Parameter %s is type %d, not unsigned, " + "cannot proceed", mt_protect_list[i].name, + entry->var_type); + snprintf(params[i].value_name, sizeof(params[i].value_name), + "%s.%s", MT_PROT_NAME, mt_protect_list[i].name); + /* XXX KDM unify types here */ + params[i].value_type = MT_PARAM_SET_UNSIGNED; + params[i].value_len = sizeof(mt_protect_list[i].value); + params[i].value.value_unsigned = mt_protect_list[i].value; + + } + param_list.num_params = MT_NUM_PROTECT_PARAMS; + param_list.param_len = sizeof(params); + param_list.params = params; + + if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1) + err(1, "error issuing MTIOCSETLIST ioctl"); + + for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) { + if (params[i].status != MT_PARAM_STATUS_OK) { + warnx("%s", params[i].error_str); + retval = 1; + } + } +bailout: + + return (retval); +} + +static int +mt_param(int argc, char **argv, int mtfd, char *xml_str, + struct mt_status_data *status_data) +{ + int list = 0, do_set = 0, xml_dump = 0; + char *param_name = NULL, *param_value = NULL; + int retval = 0, quiet = 0; + int c; + + while ((c = getopt(argc, argv, "lp:qs:x")) != -1) { + switch (c) { + case 'l': + list = 1; + break; + case 'p': + if (param_name != NULL) { + warnx("Only one paramter name may be " + "specified"); + retval = 1; + goto bailout; + } + param_name = strdup(optarg); + break; + case 'q': + quiet = 1; + break; + case 's': + if (param_value != NULL) { + warnx("Only one paramter value may be " + "specified"); + retval = 1; + goto bailout; + } + param_value = strdup(optarg); + do_set = 1; + break; + case 'x': + xml_dump = 1; + break; + default: + break; + } + } + + if ((list + do_set + xml_dump) != 1) { + warnx("You must specify only one of -s, -l or -x"); + retval = 1; + goto bailout; + } + + if (xml_dump != 0) { + printf("%s", xml_str); + retval = 0; + goto bailout; + } + + if (do_set != 0) { + if (param_name == NULL) + errx(1, "You must specify -p with -s"); + + retval = mt_set_param(mtfd, status_data, param_name, + param_value); + } else if (list != 0) + retval = mt_param_list(status_data, param_name, quiet); + +bailout: + free(param_name); + free(param_value); + return (retval); +} + +int +mt_print_density_entry(struct mt_status_entry *density_root, int indent) +{ + struct mt_status_entry *entry; + int retval = 0; + + STAILQ_FOREACH(entry, &density_root->child_entries, links) { + if (entry->var_type == MT_TYPE_NODE) { + retval = mt_print_density_entry(entry, indent + 2); + if (retval != 0) + break; + else + continue; + } + if ((strcmp(entry->entry_name, "primary_density_code") == 0) + || (strcmp(entry->entry_name, "secondary_density_code") == 0) + || (strcmp(entry->entry_name, "density_code") == 0)) { + + printf("%*s%s (%s): %s\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, + denstostring(entry->value_unsigned)); + } else if (strcmp(entry->entry_name, "density_flags") == 0) { + printf("%*sMedium Access: ", indent, ""); + if (entry->value_unsigned & MT_DENS_WRITE_OK) { + printf("Read and Write\n"); + } else { + printf("Read Only\n"); + } + printf("%*sDefault Density: %s\n", indent, "", + (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" : + "No"); + printf("%*sDuplicate Density: %s\n", indent, "", + (entry->value_unsigned & MT_DENS_DUP) ? "Yes" : + "No"); + } else if (strcmp(entry->entry_name, "media_width") == 0) { + printf("%*s%s (%s): %.1f mm\n", indent, "", + entry->desc ? entry->desc : "", entry->entry_name, + (double)((double)entry->value_unsigned / 10)); + } else if (strcmp(entry->entry_name, "medium_length") == 0) { + printf("%*s%s (%s): %ju m\n", indent, "", + entry->desc ? entry->desc : "", entry->entry_name, + (uintmax_t)entry->value_unsigned); + } else if (strcmp(entry->entry_name, "capacity") == 0) { + printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, + (uintmax_t)entry->value_unsigned); + } else { + printf("%*s%s (%s): %s\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, entry->value); + } + } + + return (retval); +} + +int +mt_print_density_report(struct mt_status_entry *report_root, int indent) +{ + struct mt_status_entry *mt_report, *media_report; + struct mt_status_entry *entry; + int retval = 0; + + mt_report = mt_entry_find(report_root, + __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME)); + if (mt_report == NULL) + return (1); + + media_report = mt_entry_find(report_root, + __DECONST(char *, MT_MEDIA_REPORT_NAME)); + if (media_report == NULL) + return (1); + + if ((mt_report->value_signed == 0) + && (media_report->value_signed == 0)) { + printf("%*sThis tape drive supports the following " + "media densities:\n", indent, ""); + } else if ((mt_report->value_signed == 0) + && (media_report->value_signed != 0)) { + printf("%*sThe tape currently in this drive supports " + "the following media densities:\n", indent, ""); + } else if ((mt_report->value_signed != 0) + && (media_report->value_signed == 0)) { + printf("%*sThis tape drive supports the following " + "media types:\n", indent, ""); + } else { + printf("%*sThis tape currently in this drive supports " + "the following media types:\n", indent, ""); + } + + STAILQ_FOREACH(entry, &report_root->child_entries, links) { + struct mt_status_nv *nv; + + if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0) + continue; + + STAILQ_FOREACH(nv, &entry->nv_list, links) { + if (strcmp(nv->name, "num") != 0) + continue; + + break; + } + + indent += 2; + + printf("%*sDensity Entry", indent, ""); + if (nv != NULL) + printf(" %s", nv->value); + printf(":\n"); + + retval = mt_print_density_entry(entry, indent + 2); + + indent -= 2; + } + + return (retval); +} + +int +mt_print_density(struct mt_status_entry *density_root, int indent) +{ + struct mt_status_entry *entry; + int retval = 0; + + /* + * We should have this entry for every tape drive. This particular + * value is reported via the mode page block header, not the + * SCSI REPORT DENSITY SUPPORT command. + */ + entry = mt_entry_find(density_root, + __DECONST(char *, MT_MEDIA_DENSITY_NAME)); + if (entry == NULL) + errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME); + + printf("%*sCurrent density: %s\n", indent, "", + denstostring(entry->value_unsigned)); + + /* + * It isn't an error if we don't have any density reports. Tape + * drives that don't support the REPORT DENSITY SUPPORT command + * won't have any; they will only have the current density entry + * above. + */ + STAILQ_FOREACH(entry, &density_root->child_entries, links) { + if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0) + continue; + + retval = mt_print_density_report(entry, indent); + } + + return (retval); +} + +int +mt_getdensity(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data) +{ + int retval = 0; + int verbose = 0, xml_dump = 0; + struct mt_status_entry *density_root = NULL; + int c; + + while ((c = getopt(argc, argv, "vx")) != -1) { + switch (c) { + case 'v': + verbose = 1; + break; + case 'x': + xml_dump = 1; + break; + } + } + + if (xml_dump != 0) { + printf("%s", xml_str); + return (0); + } + + density_root = mt_status_entry_find(status_data, + __DECONST(char *, MT_DENSITY_ROOT_NAME)); + if (density_root == NULL) + errx(1, "Cannot find density root node %s", + MT_DENSITY_ROOT_NAME); + + retval = mt_print_density(density_root, 0); + + return (retval); } static void |