2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions, and the following disclaimer,
38 * without modification.
39 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
40 * substantially similar to the "NO WARRANTY" disclaimer below
41 * ("Disclaimer") and any redistribution must be conditioned upon
42 * including a substantially similar Disclaimer requirement for further
43 * binary redistribution.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
49 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGES.
58 * Authors: Ken Merry (Spectra Logic Corporation)
62 static const char copyright[] =
63 "@(#) Copyright (c) 1980, 1993\n\
64 The Regents of the University of California. All rights reserved.\n";
69 static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 5/4/95";
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
78 * magnetic tape manipulation program
80 #include <sys/types.h>
81 #include <sys/ioctl.h>
83 #include <sys/queue.h>
99 #include <cam/cam_ccb.h>
100 #include <cam/cam_periph.h>
101 #include <cam/scsi/scsi_all.h>
102 #include <cam/scsi/scsi_sa.h>
104 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
106 #define NEED_2ARGS 0x01
107 #define ZERO_ALLOWED 0x02
108 #define IS_DENSITY 0x04
109 #define DISABLE_THIS 0x08
111 #define USE_GETOPT 0x20
120 #define MAX(a, b) (a > b) ? a : b
124 MT_CMD_NONE = MTLOAD + 1,
129 static const struct commands {
131 unsigned long c_code;
135 { "bsf", MTBSF, 1, 0 },
136 { "bsr", MTBSR, 1, 0 },
137 /* XXX FreeBSD considered "eof" dangerous, since it's being
138 confused with "eom" (and is an alias for "weof" anyway) */
139 { "eof", MTWEOF, 0, DISABLE_THIS },
140 { "fsf", MTFSF, 1, 0 },
141 { "fsr", MTFSR, 1, 0 },
142 { "offline", MTOFFL, 1, 0 },
143 { "load", MTLOAD, 1, 0 },
144 { "rewind", MTREW, 1, 0 },
145 { "rewoffl", MTOFFL, 1, 0 },
146 { "ostatus", MTNOP, 1, 0 },
147 { "weof", MTWEOF, 0, ZERO_ALLOWED },
148 { "weofi", MTWEOFI, 0, ZERO_ALLOWED },
149 { "erase", MTERASE, 0, ZERO_ALLOWED},
150 { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
151 { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
152 { "eom", MTEOD, 1, 0 },
153 { "eod", MTEOD, 1, 0 },
154 { "smk", MTWSS, 0, 0 },
155 { "wss", MTWSS, 0, 0 },
156 { "fss", MTFSS, 1, 0 },
157 { "bss", MTBSS, 1, 0 },
158 { "comp", MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
159 { "retension", MTRETENS, 1, 0 },
160 { "rdhpos", MTIOCRDHPOS, 0, 0 },
161 { "rdspos", MTIOCRDSPOS, 0, 0 },
162 { "sethpos", MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
163 { "setspos", MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
164 { "errstat", MTIOCERRSTAT, 0, 0 },
165 { "setmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
166 { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
167 { "getmodel", MTIOCGETEOTMODEL, 0, 0 },
168 { "geteotmodel", MTIOCGETEOTMODEL, 0, 0 },
169 { "rblim", MTIOCRBLIM, 0, 0},
170 { "getdensity", MT_CMD_GETDENSITY, 0, USE_GETOPT},
171 { "status", MTIOCEXTGET, 0, USE_GETOPT },
172 { "locate", MTIOCEXTLOCATE, 0, USE_GETOPT },
173 { "param", MTIOCPARAMGET, 0, USE_GETOPT },
174 { "protect", MT_CMD_PROTECT, 0, USE_GETOPT },
179 static const char *getblksiz(int);
180 static void printreg(const char *, u_int, const char *);
181 static void status(struct mtget *);
182 static void usage(void);
183 const char *get_driver_state_str(int dsreg);
184 static void st_status (struct mtget *);
185 static int mt_locate(int argc, char **argv, int mtfd, const char *tape);
186 static int nstatus_print(int argc, char **argv, char *xml_str,
187 struct mt_status_data *status_data);
188 static int mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd,
190 static int mt_print_density_entry(struct mt_status_entry *density_root, int indent);
191 static int mt_print_density_report(struct mt_status_entry *report_root, int indent);
192 static int mt_print_density(struct mt_status_entry *density_root, int indent);
193 static int mt_getdensity(int argc, char **argv, char *xml_str,
194 struct mt_status_data *status_data);
195 static int mt_set_param(int mtfd, struct mt_status_data *status_data,
196 char *param_name, char *param_value);
197 static int mt_protect(int argc, char **argv, int mtfd,
198 struct mt_status_data *status_data);
199 static int mt_param(int argc, char **argv, int mtfd, char *xml_str,
200 struct mt_status_data *status_data);
201 static const char *denstostring (int d);
202 static u_int32_t stringtocomp(const char *s);
203 static const char *comptostring(u_int32_t comp);
204 static void warn_eof(void);
207 main(int argc, char *argv[])
209 const struct commands *comp;
210 struct mtget mt_status;
213 const char *p, *tape;
215 bzero(&mt_com, sizeof(mt_com));
217 if ((tape = getenv("TAPE")) == NULL)
220 while ((ch = getopt(argc, argv, "f:t:")) != -1)
238 len = strlen(p = *argv++);
239 for (comp = com;; comp++) {
240 if (comp->c_name == NULL)
241 errx(1, "%s: unknown command", p);
242 if (strncmp(p, comp->c_name, len) == 0)
245 if((comp->c_flags & NEED_2ARGS) && argc != 2)
247 if(comp->c_flags & DISABLE_THIS) {
250 if (comp->c_flags & USE_GETOPT) {
255 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
257 if (comp->c_code != MTNOP) {
258 mt_com.mt_op = comp->c_code;
260 if (!isdigit(**argv) &&
261 (comp->c_flags & IS_DENSITY)) {
263 mt_com.mt_count = mt_density_num(*argv);
264 if (mt_com.mt_count == 0)
265 errx(1, "%s: unknown density", *argv);
266 dcanon = denstostring(mt_com.mt_count);
267 if (strcmp(dcanon, *argv) != 0)
269 "Using \"%s\" as an alias for %s\n",
272 } else if (!isdigit(**argv) &&
273 (comp->c_flags & IS_COMP)) {
275 mt_com.mt_count = stringtocomp(*argv);
276 if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
277 errx(1, "%s: unknown compression",
280 } else if ((comp->c_flags & USE_GETOPT) == 0) {
282 /* allow for hex numbers; useful for density */
283 mt_com.mt_count = strtol(*argv, &q, 0);
286 if (((comp->c_flags & USE_GETOPT) == 0)
287 && (((mt_com.mt_count <=
288 ((comp->c_flags & ZERO_ALLOWED)? -1: 0))
289 && ((comp->c_flags & IS_COMP) == 0))
291 errx(1, "%s: illegal count", *argv);
295 switch (comp->c_code) {
300 struct scsi_tape_errors *s = &umn.scsi_errstat;
302 if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
304 (void)printf("Last I/O Residual: %u\n", s->io_resid);
305 (void)printf(" Last I/O Command:");
306 for (i = 0; i < sizeof (s->io_cdb); i++)
307 (void)printf(" %02X", s->io_cdb[i]);
309 (void)printf(" Last I/O Sense:\n\n\t");
310 for (i = 0; i < sizeof (s->io_sense); i++) {
311 (void)printf(" %02X", s->io_sense[i]);
312 if (((i + 1) & 0xf) == 0) {
313 (void)printf("\n\t");
317 (void)printf("Last Control Residual: %u\n",
319 (void)printf(" Last Control Command:");
320 for (i = 0; i < sizeof (s->ctl_cdb); i++)
321 (void)printf(" %02X", s->ctl_cdb[i]);
323 (void)printf(" Last Control Sense:\n\n\t");
324 for (i = 0; i < sizeof (s->ctl_sense); i++) {
325 (void)printf(" %02X", s->ctl_sense[i]);
326 if (((i + 1) & 0xf) == 0) {
327 (void)printf("\n\t");
330 (void)printf("\n\n");
338 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
340 (void)printf("%s: %s block location %u\n", tape,
341 (comp->c_code == MTIOCRDHPOS)? "hardware" :
349 u_int32_t block = (u_int32_t)mt_com.mt_count;
350 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
355 case MTIOCGETEOTMODEL:
358 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
360 (void)printf("%s: the model is %u filemar%s at EOT\n",
361 tape, om, (om > 1)? "ks" : "k");
365 case MTIOCSETEOTMODEL:
367 u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
368 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
370 if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
372 (void)printf("%s: old model was %u filemar%s at EOT\n",
373 tape, om, (om > 1)? "ks" : "k");
374 (void)printf("%s: new model is %u filemar%s at EOT\n",
375 tape, nm, (nm > 1)? "ks" : "k");
381 struct mtrblim rblim;
383 bzero(&rblim, sizeof(rblim));
385 if (ioctl(mtfd, MTIOCRBLIM, (caddr_t)&rblim) < 0)
387 (void)printf("%s: min blocksize %u bytes, "
388 "max blocksize %u bytes, granularity %u bytes\n",
389 tape, rblim.min_block_length,
390 rblim.max_block_length, rblim.granularity);
397 case MT_CMD_GETDENSITY:
401 retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd,
410 retval = mt_locate(argc, argv, mtfd, tape);
417 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
418 err(1, "%s: %s", tape, comp->c_name);
420 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
428 static const struct tape_desc {
429 short t_type; /* type of magtape device */
430 const char *t_name; /* printing name */
431 const char *t_dsbits; /* "drive status" register */
432 const char *t_erbits; /* "error" register */
434 { MT_ISAR, "SCSI tape drive", 0, 0 },
439 * Interpret the status buffer returned
442 status(struct mtget *bp)
444 const struct tape_desc *mt;
446 for (mt = tapes;; mt++) {
447 if (mt->t_type == 0) {
448 (void)printf("%d: unknown tape drive type\n",
452 if (mt->t_type == bp->mt_type)
455 if(mt->t_type == MT_ISAR)
458 (void)printf("%s tape drive, residual=%d\n",
459 mt->t_name, bp->mt_resid);
460 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
461 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
467 * Print a register a la the %b format of the kernel's printf.
470 printreg(const char *s, u_int v, const char *bits)
475 if (bits && *bits == 8)
476 printf("%s=%o", s, v);
478 printf("%s=%x", s, v);
484 while ((i = *bits++)) {
485 if (v & (1 << (i-1))) {
489 for (; (c = *bits) > 32; bits++)
492 for (; *bits > 32; bits++)
502 (void)fprintf(stderr, "usage: mt [-f device] command [count]\n");
506 static const struct compression_types {
507 u_int32_t comp_number;
514 { 0xffffffff, "enable" },
515 { 0xffffffff, "on" },
523 const char *name = mt_density_name(d);
526 sprintf(buf, "0x%02x", d);
528 sprintf(buf, "0x%02x:%s", d, name);
539 sprintf(buf, "%d bytes", bs);
545 comptostring(u_int32_t comp)
548 const struct compression_types *ct;
550 if (comp == MT_COMP_DISABLED)
552 else if (comp == MT_COMP_UNSUPP)
553 return "unsupported";
555 for (ct = comp_types; ct->name; ct++)
556 if (ct->comp_number == comp)
559 if (ct->comp_number == 0xf0f0f0f0) {
560 sprintf(buf, "0x%x", comp);
567 stringtocomp(const char *s)
569 const struct compression_types *ct;
570 size_t l = strlen(s);
572 for (ct = comp_types; ct->name; ct++)
573 if (strncasecmp(ct->name, s, l) == 0)
576 return(ct->comp_number);
579 static struct driver_state {
582 } driver_states[] = {
583 { MTIO_DSREG_REST, "at rest" },
584 { MTIO_DSREG_RBSY, "Communicating with drive" },
585 { MTIO_DSREG_WR, "Writing" },
586 { MTIO_DSREG_FMK, "Writing Filemarks" },
587 { MTIO_DSREG_ZER, "Erasing" },
588 { MTIO_DSREG_RD, "Reading" },
589 { MTIO_DSREG_FWD, "Spacing Forward" },
590 { MTIO_DSREG_REV, "Spacing Reverse" },
591 { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" },
592 { MTIO_DSREG_REW, "Rewinding" },
593 { MTIO_DSREG_TEN, "Retensioning" },
594 { MTIO_DSREG_UNL, "Unloading" },
595 { MTIO_DSREG_LD, "Loading" },
599 get_driver_state_str(int dsreg)
603 for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) {
604 if (driver_states[i].dsreg == dsreg)
605 return (driver_states[i].desc);
612 st_status(struct mtget *bp)
614 printf("Mode Density Blocksize bpi "
616 "Current: %-17s %-12s %-7d %s\n"
617 "---------available modes---------\n"
618 "0: %-17s %-12s %-7d %s\n"
619 "1: %-17s %-12s %-7d %s\n"
620 "2: %-17s %-12s %-7d %s\n"
621 "3: %-17s %-12s %-7d %s\n",
622 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
623 mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
624 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
625 mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
626 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
627 mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
628 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
629 mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
630 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
631 mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
633 if (bp->mt_dsreg != MTIO_DSREG_NIL) {
634 const char sfmt[] = "Current Driver State: %s.\n";
635 printf("---------------------------------\n");
636 const char *state_str;
638 state_str = get_driver_state_str(bp->mt_dsreg);
639 if (state_str == NULL) {
641 (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
644 printf(sfmt, state_str);
647 if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
648 bp->mt_blkno == (daddr_t) -1)
650 printf("---------------------------------\n");
651 printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n",
652 bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
656 mt_locate(int argc, char **argv, int mtfd, const char *tape)
659 uint64_t logical_id = 0;
660 mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE;
661 int eod = 0, explicit = 0, immediate = 0;
662 int64_t partition = 0;
663 int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0;
667 bzero(&mtl, sizeof(mtl));
669 while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) {
673 logical_id = strtoull(optarg, NULL, 0);
674 dest_type = MT_LOCATE_DEST_OBJECT;
680 dest_type = MT_LOCATE_DEST_EOD;
684 * XXX KDM explicit address mode. Should we even
685 * allow this, since the driver doesn't operate in
686 * explicit address mode?
692 logical_id = strtoull(optarg, NULL, 0);
693 dest_type = MT_LOCATE_DEST_FILE;
698 * Immediate address mode. XXX KDM do we want to
699 * implement this? The other commands in the
700 * tape driver will need to be able to handle this.
706 * Change partition to the given partition.
708 partition = strtol(optarg, NULL, 0);
712 /* Go to the given set mark */
713 logical_id = strtoull(optarg, NULL, 0);
714 dest_type = MT_LOCATE_DEST_SET;
723 * These options are mutually exclusive. The user may only specify
726 if ((block_addr_set + file_set + eod + set_set) != 1)
727 errx(1, "You must specify only one of -b, -f, -e, or -s");
729 mtl.dest_type = dest_type;
731 case MT_LOCATE_DEST_OBJECT:
732 case MT_LOCATE_DEST_FILE:
733 case MT_LOCATE_DEST_SET:
734 mtl.logical_id = logical_id;
736 case MT_LOCATE_DEST_EOD:
741 mtl.flags |= MT_LOCATE_FLAG_IMMED;
743 if (partition_set != 0) {
744 mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART;
745 mtl.partition = partition;
749 mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT;
751 mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT;
753 if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1)
754 err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape);
765 MT_COMPRESSION_SUPPORTED = 5,
766 MT_COMPRESSION_ENABLED = 6,
767 MT_COMPRESSION_ALGORITHM = 7,
768 MT_MEDIA_DENSITY = 8,
769 MT_MEDIA_BLOCKSIZE = 9,
770 MT_CALCULATED_FILENO = 10,
771 MT_CALCULATED_REL_BLKNO = 11,
772 MT_REPORTED_FILENO = 12,
773 MT_REPORTED_BLKNO = 13,
787 MT_MAX_EFF_IOSIZE = 27
790 static struct mt_status_items {
792 struct mt_status_entry *entry;
793 } req_status_items[] = {
794 { "periph_name", NULL },
795 { "unit_number", NULL },
798 { "revision", NULL },
799 { "compression_supported", NULL },
800 { "compression_enabled", NULL },
801 { "compression_algorithm", NULL },
802 { "media_density", NULL },
803 { "media_blocksize", NULL },
804 { "calculated_fileno", NULL },
805 { "calculated_rel_blkno", NULL },
806 { "reported_fileno", NULL },
807 { "reported_blkno", NULL },
808 { "partition", NULL },
813 { "residual", NULL },
814 { "fixed_mode", NULL },
815 { "serial_num", NULL },
817 { "cpi_maxio", NULL },
820 { "blk_gran", NULL },
821 { "max_effective_iosize", NULL }
825 nstatus_print(int argc, char **argv, char *xml_str,
826 struct mt_status_data *status_data)
829 int64_t calculated_fileno, calculated_rel_blkno;
830 int64_t rep_fileno, rep_blkno, partition, resid;
832 const char *dens_str;
833 int dsreg, bop, eop, bpew;
836 unsigned int field_width;
840 while ((c = getopt(argc, argv, "xv")) != -1) {
854 printf("%s", xml_str);
858 for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0]));
862 name = __DECONST(char *, req_status_items[i].name);
863 req_status_items[i].entry = mt_status_entry_find(status_data,
865 if (req_status_items[i].entry == NULL) {
866 errx(1, "Cannot find status entry %s",
867 req_status_items[i].name);
871 printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n",
872 req_status_items[MT_PERIPH_NAME].entry->value,
873 (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned,
874 req_status_items[MT_VENDOR].entry->value,
875 req_status_items[MT_PRODUCT].entry->value,
876 req_status_items[MT_REVISION].entry->value,
877 (req_status_items[MT_SERIAL_NUM].entry->value) ?
878 req_status_items[MT_SERIAL_NUM].entry->value : "none");
879 printf("---------------------------------\n");
882 * We check to see whether we're in fixed mode or not, and don't
883 * just believe the blocksize. If the SILI bit is turned on, the
884 * blocksize will be set to 4, even though we're doing variable
885 * length (well, multiples of 4) blocks.
887 if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0)
888 snprintf(block_str, sizeof(block_str), "variable");
890 snprintf(block_str, sizeof(block_str), "%s",
891 getblksiz(req_status_items[
892 MT_MEDIA_BLOCKSIZE].entry->value_unsigned));
894 dens_str = denstostring(req_status_items[
895 MT_MEDIA_DENSITY].entry->value_unsigned);
896 if (dens_str == NULL)
899 dens_len = strlen(dens_str);
900 field_width = MAX(dens_len, 17);
901 printf("Mode %-*s Blocksize bpi Compression\n"
902 "Current: %-*s %-12s %-7d ",
903 field_width, "Density", field_width, dens_str, block_str,
904 mt_density_bp(req_status_items[
905 MT_MEDIA_DENSITY].entry->value_unsigned, TRUE));
907 if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0)
908 printf("unsupported\n");
909 else if (req_status_items[
910 MT_COMPRESSION_ENABLED].entry->value_signed == 0)
911 printf("disabled\n");
913 printf("enabled (%s)\n",
914 comptostring(req_status_items[
915 MT_COMPRESSION_ALGORITHM].entry->value_unsigned));
918 dsreg = req_status_items[MT_DSREG].entry->value_signed;
919 if (dsreg != MTIO_DSREG_NIL) {
920 const char sfmt[] = "Current Driver State: %s.\n";
921 printf("---------------------------------\n");
922 const char *state_str;
924 state_str = get_driver_state_str(dsreg);
925 if (state_str == NULL) {
927 (void) sprintf(foo, "Unknown state 0x%x", dsreg);
930 printf(sfmt, state_str);
933 resid = req_status_items[MT_RESID].entry->value_signed;
934 calculated_fileno = req_status_items[
935 MT_CALCULATED_FILENO].entry->value_signed;
936 calculated_rel_blkno = req_status_items[
937 MT_CALCULATED_REL_BLKNO].entry->value_signed;
938 rep_fileno = req_status_items[
939 MT_REPORTED_FILENO].entry->value_signed;
940 rep_blkno = req_status_items[
941 MT_REPORTED_BLKNO].entry->value_signed;
942 bop = req_status_items[MT_BOP].entry->value_signed;
943 eop = req_status_items[MT_EOP].entry->value_signed;
944 bpew = req_status_items[MT_BPEW].entry->value_signed;
945 partition = req_status_items[MT_PARTITION].entry->value_signed;
947 printf("---------------------------------\n");
948 printf("Partition: %3jd Calc File Number: %3jd "
949 " Calc Record Number: %jd\n"
950 "Residual: %3jd Reported File Number: %3jd "
951 "Reported Record Number: %jd\n", partition, calculated_fileno,
952 calculated_rel_blkno, resid, rep_fileno, rep_blkno);
955 if (bop > 0 || eop > 0 || bpew > 0) {
979 printf("---------------------------------\n");
980 printf("Tape I/O parameters:\n");
981 for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) {
982 printf(" %s (%s): %ju bytes\n",
983 req_status_items[i].entry->desc,
984 req_status_items[i].name,
985 req_status_items[i].entry->value_unsigned);
993 mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape)
995 struct mt_status_data status_data;
997 struct mt_status_entry *entry;
1001 unsigned long ioctl_cmd;
1004 case MT_CMD_PROTECT:
1006 ioctl_cmd = MTIOCPARAMGET;
1009 ioctl_cmd = MTIOCEXTGET;
1013 retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str);
1015 err(1, "Couldn't get mt XML string");
1017 retval = mt_get_status(xml_str, &status_data);
1018 if (retval != XML_STATUS_OK) {
1019 warn("Couldn't get mt status for %s", tape);
1024 * This gets set if there are memory allocation or other errors in
1025 * our parsing of the XML.
1027 if (status_data.error != 0) {
1028 warnx("%s", status_data.error_str);
1033 STAILQ_FOREACH(entry, &status_data.entries, links)
1034 mt_status_tree_print(entry, 0, NULL);
1039 retval = nstatus_print(argc, argv, xml_str, &status_data);
1042 retval = mt_param(argc, argv, mtfd, xml_str, &status_data);
1044 case MT_CMD_PROTECT:
1045 retval = mt_protect(argc, argv, mtfd, &status_data);
1047 case MT_CMD_GETDENSITY:
1048 retval = mt_getdensity(argc, argv, xml_str, &status_data);
1053 if (xml_str != NULL)
1056 mt_status_free(&status_data);
1062 mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name,
1065 struct mt_status_entry *entry;
1066 struct mtparamset param_set;
1068 entry = mt_status_entry_find(status_data,
1069 __DECONST(char *, "mtparamget"));
1071 errx(1, "Cannot find parameter root node");
1073 bzero(¶m_set, sizeof(param_set));
1074 entry = mt_entry_find(entry, param_name);
1076 errx(1, "Unknown parameter name \"%s\"", param_name);
1078 strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name));
1080 switch (entry->var_type) {
1082 param_set.value.value_signed = strtoll(param_value, NULL, 0);
1083 param_set.value_type = MT_PARAM_SET_SIGNED;
1084 param_set.value_len = entry->size;
1087 param_set.value.value_unsigned = strtoull(param_value, NULL, 0);
1088 param_set.value_type = MT_PARAM_SET_UNSIGNED;
1089 param_set.value_len = entry->size;
1091 case MT_TYPE_STRING: {
1094 param_len = strlen(param_value) + 1;
1095 if (param_len > sizeof(param_set.value.value_fixed_str)) {
1096 param_set.value_type = MT_PARAM_SET_VAR_STR;
1097 param_set.value.value_var_str = param_value;
1099 param_set.value_type = MT_PARAM_SET_FIXED_STR;
1100 strlcpy(param_set.value.value_fixed_str, param_value,
1101 sizeof(param_set.value.value_fixed_str));
1103 param_set.value_len = param_len;
1107 errx(1, "Unknown parameter type %d for %s", entry->var_type,
1112 if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1)
1113 err(1, "MTIOCPARAMSET");
1115 if (param_set.status != MT_PARAM_STATUS_OK)
1116 errx(1, "Failed to set %s: %s", param_name,
1117 param_set.error_str);
1131 static struct mt_protect_info {
1133 struct mt_status_entry *entry;
1135 } mt_protect_list[] = {
1136 { "lbp_r", NULL, 0 },
1137 { "lbp_w", NULL, 0 },
1138 { "rbdp", NULL, 0 },
1139 { "pi_length", NULL, 0 },
1140 { "prot_method", NULL, 0 }
1143 #define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0]))
1145 #define MT_PROT_NAME "protection"
1148 mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data)
1151 int do_enable = 0, do_disable = 0, do_list = 0;
1152 int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0;
1153 int prot_method_set = 0, pi_length_set = 0;
1155 uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0;
1156 uint32_t prot_method = 0, pi_length = 0;
1157 struct mt_status_entry *prot_entry, *supported_entry;
1158 struct mt_status_entry *entry;
1159 struct mtparamset params[MT_NUM_PROTECT_PARAMS];
1160 struct mtsetlist param_list;
1164 while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) {
1168 rbdp = strtoul(optarg, NULL, 0);
1169 if ((rbdp != 0) && (rbdp != 1))
1170 errx(1, "valid values for -b are 0 and 1");
1183 pi_length = strtoul(optarg, NULL, 0);
1184 if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK)
1185 errx(1, "PI length %u > maximum %u",
1186 pi_length, SA_CTRL_DP_PI_LENGTH_MASK);
1189 prot_method_set = 1;
1190 prot_method = strtoul(optarg, NULL, 0);
1191 if (prot_method > SA_CTRL_DP_METHOD_MAX)
1192 errx(1, "Method %u > maximum %u",
1193 prot_method, SA_CTRL_DP_METHOD_MAX);
1197 lbp_r = strtoul(optarg, NULL, 0);
1198 if ((lbp_r != 0) && (lbp_r != 1))
1199 errx(1, "valid values for -r are 0 and 1");
1206 lbp_w = strtoul(optarg, NULL, 0);
1207 if ((lbp_w != 0) && (lbp_r != 1))
1208 errx(1, "valid values for -r are 0 and 1");
1215 if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set +
1216 prot_method_set + lbp_r_set + lbp_w_set) == 0)
1217 errx(1, "Need an argument for protect");
1219 if ((do_disable + do_enable + do_list) != 1)
1220 errx(1, "You must specify only one of -e, -d or -l");
1223 retval = mt_protect_print(status_data, verbose);
1226 if (do_enable != 0) {
1228 * Enable protection, but allow the user to override
1229 * settings if he doesn't want everything turned on.
1238 * If the user doesn't override it, we default to enabling
1239 * Reed-Solomon checkums.
1241 if (prot_method_set == 0)
1242 prot_method = SA_CTRL_DP_REED_SOLOMON;
1243 if (pi_length_set == 0)
1244 pi_length = SA_CTRL_DP_RS_LENGTH;
1245 } else if (do_disable != 0) {
1247 * If the user wants to disable protection, we ignore any
1248 * other parameters he has set. Everything gets set to 0.
1250 rbdp = lbp_w = lbp_r = 0;
1251 prot_method = pi_length = 0;
1254 prot_entry = mt_status_entry_find(status_data,
1255 __DECONST(char *, MT_PROT_NAME));
1256 if (prot_entry == NULL)
1257 errx(1, "Unable to find protection information status");
1259 supported_entry = mt_entry_find(prot_entry,
1260 __DECONST(char *, "protection_supported"));
1261 if (supported_entry == NULL)
1262 errx(1, "Unable to find protection support information");
1264 if (((supported_entry->var_type == MT_TYPE_INT)
1265 && (supported_entry->value_signed == 0))
1266 || ((supported_entry->var_type == MT_TYPE_UINT)
1267 && (supported_entry->value_unsigned == 0)))
1268 errx(1, "This device does not support protection information");
1270 mt_protect_list[MT_PP_LBP_R].value = lbp_r;
1271 mt_protect_list[MT_PP_LBP_W].value = lbp_w;
1272 mt_protect_list[MT_PP_RBDP].value = rbdp;
1273 mt_protect_list[MT_PP_PI_LENGTH].value = pi_length;
1274 mt_protect_list[MT_PP_PROT_METHOD].value = prot_method;
1276 bzero(¶ms, sizeof(params));
1277 bzero(¶m_list, sizeof(param_list));
1280 * Go through the list and make sure that we have this parameter,
1281 * and that it is still an unsigned integer. If not, we've got a
1284 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1285 entry = mt_entry_find(prot_entry,
1286 __DECONST(char *, mt_protect_list[i].name));
1287 if (entry == NULL) {
1288 errx(1, "Unable to find parameter %s",
1289 mt_protect_list[i].name);
1291 mt_protect_list[i].entry = entry;
1293 if (entry->var_type != MT_TYPE_UINT)
1294 errx(1, "Parameter %s is type %d, not unsigned, "
1295 "cannot proceed", mt_protect_list[i].name,
1297 snprintf(params[i].value_name, sizeof(params[i].value_name),
1298 "%s.%s", MT_PROT_NAME, mt_protect_list[i].name);
1299 /* XXX KDM unify types here */
1300 params[i].value_type = MT_PARAM_SET_UNSIGNED;
1301 params[i].value_len = sizeof(mt_protect_list[i].value);
1302 params[i].value.value_unsigned = mt_protect_list[i].value;
1305 param_list.num_params = MT_NUM_PROTECT_PARAMS;
1306 param_list.param_len = sizeof(params);
1307 param_list.params = params;
1309 if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1)
1310 err(1, "error issuing MTIOCSETLIST ioctl");
1312 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1313 if (params[i].status != MT_PARAM_STATUS_OK) {
1314 warnx("%s", params[i].error_str);
1324 mt_param(int argc, char **argv, int mtfd, char *xml_str,
1325 struct mt_status_data *status_data)
1327 int list = 0, do_set = 0, xml_dump = 0;
1328 char *param_name = NULL, *param_value = NULL;
1329 int retval = 0, quiet = 0;
1332 while ((c = getopt(argc, argv, "lp:qs:x")) != -1) {
1338 if (param_name != NULL) {
1339 warnx("Only one paramter name may be "
1344 param_name = strdup(optarg);
1350 if (param_value != NULL) {
1351 warnx("Only one paramter value may be "
1356 param_value = strdup(optarg);
1367 if ((list + do_set + xml_dump) != 1) {
1368 warnx("You must specify only one of -s, -l or -x");
1373 if (xml_dump != 0) {
1374 printf("%s", xml_str);
1380 if (param_name == NULL)
1381 errx(1, "You must specify -p with -s");
1383 retval = mt_set_param(mtfd, status_data, param_name,
1385 } else if (list != 0)
1386 retval = mt_param_list(status_data, param_name, quiet);
1395 mt_print_density_entry(struct mt_status_entry *density_root, int indent)
1397 struct mt_status_entry *entry;
1400 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1401 if (entry->var_type == MT_TYPE_NODE) {
1402 retval = mt_print_density_entry(entry, indent + 2);
1408 if ((strcmp(entry->entry_name, "primary_density_code") == 0)
1409 || (strcmp(entry->entry_name, "secondary_density_code") == 0)){
1411 /* XXX KDM this should really be unsigned */
1412 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1413 entry->desc : "", entry->entry_name,
1414 denstostring(entry->value_unsigned));
1415 } else if (strcmp(entry->entry_name, "density_flags") == 0) {
1416 printf("%*sMedium Access: ", indent, "");
1417 if (entry->value_unsigned & MT_DENS_WRITE_OK) {
1418 printf("Read and Write\n");
1420 printf("Read Only\n");
1422 printf("%*sDefault Density: %s\n", indent, "",
1423 (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" :
1425 printf("%*sDuplicate Density: %s\n", indent, "",
1426 (entry->value_unsigned & MT_DENS_DUP) ? "Yes" :
1428 } else if (strcmp(entry->entry_name, "media_width") == 0) {
1429 printf("%*s%s (%s): %.1f mm\n", indent, "",
1430 entry->desc ? entry->desc : "", entry->entry_name,
1431 (double)((double)entry->value_unsigned / 10));
1432 } else if (strcmp(entry->entry_name, "medium_length") == 0) {
1433 printf("%*s%s (%s): %ju m\n", indent, "",
1434 entry->desc ? entry->desc : "", entry->entry_name,
1435 (uintmax_t)entry->value_unsigned);
1436 } else if (strcmp(entry->entry_name, "capacity") == 0) {
1437 printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ?
1438 entry->desc : "", entry->entry_name,
1439 (uintmax_t)entry->value_unsigned);
1441 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1442 entry->desc : "", entry->entry_name, entry->value);
1450 mt_print_density_report(struct mt_status_entry *report_root, int indent)
1452 struct mt_status_entry *mt_report, *media_report;
1453 struct mt_status_entry *entry;
1456 mt_report = mt_entry_find(report_root,
1457 __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME));
1458 if (mt_report == NULL)
1461 media_report = mt_entry_find(report_root,
1462 __DECONST(char *, MT_MEDIA_REPORT_NAME));
1463 if (media_report == NULL)
1466 if ((mt_report->value_signed == 0)
1467 && (media_report->value_signed == 0)) {
1468 printf("%*sThis tape drive supports the following "
1469 "media densities:\n", indent, "");
1470 } else if ((mt_report->value_signed == 0)
1471 && (media_report->value_signed != 0)) {
1472 printf("%*sThe tape currently in this drive supports "
1473 "the following media densities:\n", indent, "");
1474 } else if ((mt_report->value_signed != 0)
1475 && (media_report->value_signed == 0)) {
1476 printf("%*sThis tape drive supports the following "
1477 "media types:\n", indent, "");
1479 printf("%*sThis tape currently in this drive supports "
1480 "the following media types:\n", indent, "");
1483 STAILQ_FOREACH(entry, &report_root->child_entries, links) {
1484 struct mt_status_nv *nv;
1486 if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0)
1489 STAILQ_FOREACH(nv, &entry->nv_list, links) {
1490 if (strcmp(nv->name, "num") != 0)
1498 printf("%*sDensity Entry", indent, "");
1500 printf(" %s", nv->value);
1503 retval = mt_print_density_entry(entry, indent + 2);
1512 mt_print_density(struct mt_status_entry *density_root, int indent)
1514 struct mt_status_entry *entry;
1518 * We should have this entry for every tape drive. This particular
1519 * value is reported via the mode page block header, not the
1520 * SCSI REPORT DENSITY SUPPORT command.
1522 entry = mt_entry_find(density_root,
1523 __DECONST(char *, MT_MEDIA_DENSITY_NAME));
1525 errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME);
1527 printf("%*sCurrent density: %s\n", indent, "",
1528 denstostring(entry->value_unsigned));
1531 * It isn't an error if we don't have any density reports. Tape
1532 * drives that don't support the REPORT DENSITY SUPPORT command
1533 * won't have any; they will only have the current density entry
1536 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1537 if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0)
1540 retval = mt_print_density_report(entry, indent);
1547 mt_getdensity(int argc, char **argv, char *xml_str,
1548 struct mt_status_data *status_data)
1551 int verbose = 0, xml_dump = 0;
1552 struct mt_status_entry *density_root = NULL;
1555 while ((c = getopt(argc, argv, "vx")) != -1) {
1566 if (xml_dump != 0) {
1567 printf("%s", xml_str);
1571 density_root = mt_status_entry_find(status_data,
1572 __DECONST(char *, MT_DENSITY_ROOT_NAME));
1573 if (density_root == NULL)
1574 errx(1, "Cannot find density root node %s",
1575 MT_DENSITY_ROOT_NAME);
1577 retval = mt_print_density(density_root, 0);
1586 "The \"eof\" command has been disabled.\n"
1587 "Use \"weof\" if you really want to write end-of-file marks,\n"
1588 "or \"eom\" if you rather want to skip to the end of "
1589 "recorded medium.\n");