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 * 3. 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>
98 #include <cam/cam_ccb.h>
99 #include <cam/cam_periph.h>
100 #include <cam/scsi/scsi_all.h>
101 #include <cam/scsi/scsi_sa.h>
103 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
105 #define NEED_2ARGS 0x01
106 #define ZERO_ALLOWED 0x02
107 #define IS_DENSITY 0x04
108 #define DISABLE_THIS 0x08
110 #define USE_GETOPT 0x20
119 #define MAX(a, b) (a > b) ? a : b
121 #define MT_PLURAL(a) (a == 1) ? "" : "s"
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)
388 " min blocksize %u byte%s\n"
389 " max blocksize %u byte%s\n"
390 " granularity %u byte%s\n",
391 tape, rblim.min_block_length,
392 MT_PLURAL(rblim.min_block_length),
393 rblim.max_block_length,
394 MT_PLURAL(rblim.max_block_length),
395 (1 << rblim.granularity),
396 MT_PLURAL((1 << rblim.granularity)));
403 case MT_CMD_GETDENSITY:
407 retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd,
416 retval = mt_locate(argc, argv, mtfd, tape);
423 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
424 err(1, "%s: %s", tape, comp->c_name);
426 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
434 static const struct tape_desc {
435 short t_type; /* type of magtape device */
436 const char *t_name; /* printing name */
437 const char *t_dsbits; /* "drive status" register */
438 const char *t_erbits; /* "error" register */
440 { MT_ISAR, "SCSI tape drive", 0, 0 },
445 * Interpret the status buffer returned
448 status(struct mtget *bp)
450 const struct tape_desc *mt;
452 for (mt = tapes;; mt++) {
453 if (mt->t_type == 0) {
454 (void)printf("%d: unknown tape drive type\n",
458 if (mt->t_type == bp->mt_type)
461 if(mt->t_type == MT_ISAR)
464 (void)printf("%s tape drive, residual=%d\n",
465 mt->t_name, bp->mt_resid);
466 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
467 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
473 * Print a register a la the %b format of the kernel's printf.
476 printreg(const char *s, u_int v, const char *bits)
481 if (bits && *bits == 8)
482 printf("%s=%o", s, v);
484 printf("%s=%x", s, v);
490 while ((i = *bits++)) {
491 if (v & (1 << (i-1))) {
495 for (; (c = *bits) > 32; bits++)
498 for (; *bits > 32; bits++)
508 (void)fprintf(stderr, "usage: mt [-f device] command [count]\n");
512 static const struct compression_types {
513 u_int32_t comp_number;
520 { 0xffffffff, "enable" },
521 { 0xffffffff, "on" },
529 const char *name = mt_density_name(d);
532 sprintf(buf, "0x%02x", d);
534 sprintf(buf, "0x%02x:%s", d, name);
545 sprintf(buf, "%d bytes", bs);
551 comptostring(u_int32_t comp)
554 const struct compression_types *ct;
556 if (comp == MT_COMP_DISABLED)
558 else if (comp == MT_COMP_UNSUPP)
559 return "unsupported";
561 for (ct = comp_types; ct->name; ct++)
562 if (ct->comp_number == comp)
565 if (ct->comp_number == 0xf0f0f0f0) {
566 sprintf(buf, "0x%x", comp);
573 stringtocomp(const char *s)
575 const struct compression_types *ct;
576 size_t l = strlen(s);
578 for (ct = comp_types; ct->name; ct++)
579 if (strncasecmp(ct->name, s, l) == 0)
582 return(ct->comp_number);
585 static struct driver_state {
588 } driver_states[] = {
589 { MTIO_DSREG_REST, "at rest" },
590 { MTIO_DSREG_RBSY, "Communicating with drive" },
591 { MTIO_DSREG_WR, "Writing" },
592 { MTIO_DSREG_FMK, "Writing Filemarks" },
593 { MTIO_DSREG_ZER, "Erasing" },
594 { MTIO_DSREG_RD, "Reading" },
595 { MTIO_DSREG_FWD, "Spacing Forward" },
596 { MTIO_DSREG_REV, "Spacing Reverse" },
597 { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" },
598 { MTIO_DSREG_REW, "Rewinding" },
599 { MTIO_DSREG_TEN, "Retensioning" },
600 { MTIO_DSREG_UNL, "Unloading" },
601 { MTIO_DSREG_LD, "Loading" },
605 get_driver_state_str(int dsreg)
609 for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) {
610 if (driver_states[i].dsreg == dsreg)
611 return (driver_states[i].desc);
618 st_status(struct mtget *bp)
620 printf("Mode Density Blocksize bpi "
622 "Current: %-17s %-12s %-7d %s\n"
623 "---------available modes---------\n"
624 "0: %-17s %-12s %-7d %s\n"
625 "1: %-17s %-12s %-7d %s\n"
626 "2: %-17s %-12s %-7d %s\n"
627 "3: %-17s %-12s %-7d %s\n",
628 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
629 mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
630 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
631 mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
632 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
633 mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
634 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
635 mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
636 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
637 mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
639 if (bp->mt_dsreg != MTIO_DSREG_NIL) {
640 const char sfmt[] = "Current Driver State: %s.\n";
641 printf("---------------------------------\n");
642 const char *state_str;
644 state_str = get_driver_state_str(bp->mt_dsreg);
645 if (state_str == NULL) {
647 (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
650 printf(sfmt, state_str);
653 if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
654 bp->mt_blkno == (daddr_t) -1)
656 printf("---------------------------------\n");
657 printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n",
658 bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
662 mt_locate(int argc, char **argv, int mtfd, const char *tape)
665 uint64_t logical_id = 0;
666 mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE;
667 int eod = 0, explicit = 0, immediate = 0;
668 int64_t partition = 0;
669 int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0;
673 bzero(&mtl, sizeof(mtl));
675 while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) {
679 logical_id = strtoull(optarg, NULL, 0);
680 dest_type = MT_LOCATE_DEST_OBJECT;
686 dest_type = MT_LOCATE_DEST_EOD;
690 * XXX KDM explicit address mode. Should we even
691 * allow this, since the driver doesn't operate in
692 * explicit address mode?
698 logical_id = strtoull(optarg, NULL, 0);
699 dest_type = MT_LOCATE_DEST_FILE;
704 * Immediate address mode. XXX KDM do we want to
705 * implement this? The other commands in the
706 * tape driver will need to be able to handle this.
712 * Change partition to the given partition.
714 partition = strtol(optarg, NULL, 0);
718 /* Go to the given set mark */
719 logical_id = strtoull(optarg, NULL, 0);
720 dest_type = MT_LOCATE_DEST_SET;
729 * These options are mutually exclusive. The user may only specify
732 if ((block_addr_set + file_set + eod + set_set) != 1)
733 errx(1, "You must specify only one of -b, -f, -e, or -s");
735 mtl.dest_type = dest_type;
737 case MT_LOCATE_DEST_OBJECT:
738 case MT_LOCATE_DEST_FILE:
739 case MT_LOCATE_DEST_SET:
740 mtl.logical_id = logical_id;
742 case MT_LOCATE_DEST_EOD:
747 mtl.flags |= MT_LOCATE_FLAG_IMMED;
749 if (partition_set != 0) {
750 mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART;
751 mtl.partition = partition;
755 mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT;
757 mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT;
759 if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1)
760 err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape);
771 MT_COMPRESSION_SUPPORTED = 5,
772 MT_COMPRESSION_ENABLED = 6,
773 MT_COMPRESSION_ALGORITHM = 7,
774 MT_MEDIA_DENSITY = 8,
775 MT_MEDIA_BLOCKSIZE = 9,
776 MT_CALCULATED_FILENO = 10,
777 MT_CALCULATED_REL_BLKNO = 11,
778 MT_REPORTED_FILENO = 12,
779 MT_REPORTED_BLKNO = 13,
793 MT_MAX_EFF_IOSIZE = 27
796 static struct mt_status_items {
798 struct mt_status_entry *entry;
799 } req_status_items[] = {
800 { "periph_name", NULL },
801 { "unit_number", NULL },
804 { "revision", NULL },
805 { "compression_supported", NULL },
806 { "compression_enabled", NULL },
807 { "compression_algorithm", NULL },
808 { "media_density", NULL },
809 { "media_blocksize", NULL },
810 { "calculated_fileno", NULL },
811 { "calculated_rel_blkno", NULL },
812 { "reported_fileno", NULL },
813 { "reported_blkno", NULL },
814 { "partition", NULL },
819 { "residual", NULL },
820 { "fixed_mode", NULL },
821 { "serial_num", NULL },
823 { "cpi_maxio", NULL },
826 { "blk_gran", NULL },
827 { "max_effective_iosize", NULL }
831 nstatus_print(int argc, char **argv, char *xml_str,
832 struct mt_status_data *status_data)
835 int64_t calculated_fileno, calculated_rel_blkno;
836 int64_t rep_fileno, rep_blkno, partition, resid;
838 const char *dens_str;
839 int dsreg, bop, eop, bpew;
842 unsigned int field_width;
846 while ((c = getopt(argc, argv, "xv")) != -1) {
860 printf("%s", xml_str);
864 for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0]));
868 name = __DECONST(char *, req_status_items[i].name);
869 req_status_items[i].entry = mt_status_entry_find(status_data,
871 if (req_status_items[i].entry == NULL) {
872 errx(1, "Cannot find status entry %s",
873 req_status_items[i].name);
877 printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n",
878 req_status_items[MT_PERIPH_NAME].entry->value,
879 (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned,
880 req_status_items[MT_VENDOR].entry->value,
881 req_status_items[MT_PRODUCT].entry->value,
882 req_status_items[MT_REVISION].entry->value,
883 (req_status_items[MT_SERIAL_NUM].entry->value) ?
884 req_status_items[MT_SERIAL_NUM].entry->value : "none");
885 printf("---------------------------------\n");
888 * We check to see whether we're in fixed mode or not, and don't
889 * just believe the blocksize. If the SILI bit is turned on, the
890 * blocksize will be set to 4, even though we're doing variable
891 * length (well, multiples of 4) blocks.
893 if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0)
894 snprintf(block_str, sizeof(block_str), "variable");
896 snprintf(block_str, sizeof(block_str), "%s",
897 getblksiz(req_status_items[
898 MT_MEDIA_BLOCKSIZE].entry->value_unsigned));
900 dens_str = denstostring(req_status_items[
901 MT_MEDIA_DENSITY].entry->value_unsigned);
902 if (dens_str == NULL)
905 dens_len = strlen(dens_str);
906 field_width = MAX(dens_len, 17);
907 printf("Mode %-*s Blocksize bpi Compression\n"
908 "Current: %-*s %-12s %-7d ",
909 field_width, "Density", field_width, dens_str, block_str,
910 mt_density_bp(req_status_items[
911 MT_MEDIA_DENSITY].entry->value_unsigned, TRUE));
913 if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0)
914 printf("unsupported\n");
915 else if (req_status_items[
916 MT_COMPRESSION_ENABLED].entry->value_signed == 0)
917 printf("disabled\n");
919 printf("enabled (%s)\n",
920 comptostring(req_status_items[
921 MT_COMPRESSION_ALGORITHM].entry->value_unsigned));
924 dsreg = req_status_items[MT_DSREG].entry->value_signed;
925 if (dsreg != MTIO_DSREG_NIL) {
926 const char sfmt[] = "Current Driver State: %s.\n";
927 printf("---------------------------------\n");
928 const char *state_str;
930 state_str = get_driver_state_str(dsreg);
931 if (state_str == NULL) {
933 (void) sprintf(foo, "Unknown state 0x%x", dsreg);
936 printf(sfmt, state_str);
939 resid = req_status_items[MT_RESID].entry->value_signed;
940 calculated_fileno = req_status_items[
941 MT_CALCULATED_FILENO].entry->value_signed;
942 calculated_rel_blkno = req_status_items[
943 MT_CALCULATED_REL_BLKNO].entry->value_signed;
944 rep_fileno = req_status_items[
945 MT_REPORTED_FILENO].entry->value_signed;
946 rep_blkno = req_status_items[
947 MT_REPORTED_BLKNO].entry->value_signed;
948 bop = req_status_items[MT_BOP].entry->value_signed;
949 eop = req_status_items[MT_EOP].entry->value_signed;
950 bpew = req_status_items[MT_BPEW].entry->value_signed;
951 partition = req_status_items[MT_PARTITION].entry->value_signed;
953 printf("---------------------------------\n");
954 printf("Partition: %3jd Calc File Number: %3jd "
955 " Calc Record Number: %jd\n"
956 "Residual: %3jd Reported File Number: %3jd "
957 "Reported Record Number: %jd\n", partition, calculated_fileno,
958 calculated_rel_blkno, resid, rep_fileno, rep_blkno);
961 if (bop > 0 || eop > 0 || bpew > 0) {
985 printf("---------------------------------\n");
986 printf("Tape I/O parameters:\n");
987 for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) {
988 printf(" %s (%s): %ju bytes\n",
989 req_status_items[i].entry->desc,
990 req_status_items[i].name,
991 req_status_items[i].entry->value_unsigned);
999 mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape)
1001 struct mt_status_data status_data;
1003 struct mt_status_entry *entry;
1007 unsigned long ioctl_cmd;
1010 case MT_CMD_PROTECT:
1012 ioctl_cmd = MTIOCPARAMGET;
1015 ioctl_cmd = MTIOCEXTGET;
1019 retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str);
1021 err(1, "Couldn't get mt XML string");
1023 retval = mt_get_status(xml_str, &status_data);
1024 if (retval != XML_STATUS_OK) {
1025 warn("Couldn't get mt status for %s", tape);
1030 * This gets set if there are memory allocation or other errors in
1031 * our parsing of the XML.
1033 if (status_data.error != 0) {
1034 warnx("%s", status_data.error_str);
1039 STAILQ_FOREACH(entry, &status_data.entries, links)
1040 mt_status_tree_print(entry, 0, NULL);
1045 retval = nstatus_print(argc, argv, xml_str, &status_data);
1048 retval = mt_param(argc, argv, mtfd, xml_str, &status_data);
1050 case MT_CMD_PROTECT:
1051 retval = mt_protect(argc, argv, mtfd, &status_data);
1053 case MT_CMD_GETDENSITY:
1054 retval = mt_getdensity(argc, argv, xml_str, &status_data);
1059 if (xml_str != NULL)
1062 mt_status_free(&status_data);
1068 mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name,
1071 struct mt_status_entry *entry;
1072 struct mtparamset param_set;
1074 entry = mt_status_entry_find(status_data,
1075 __DECONST(char *, "mtparamget"));
1077 errx(1, "Cannot find parameter root node");
1079 bzero(¶m_set, sizeof(param_set));
1080 entry = mt_entry_find(entry, param_name);
1082 errx(1, "Unknown parameter name \"%s\"", param_name);
1084 strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name));
1086 switch (entry->var_type) {
1088 param_set.value.value_signed = strtoll(param_value, NULL, 0);
1089 param_set.value_type = MT_PARAM_SET_SIGNED;
1090 param_set.value_len = entry->size;
1093 param_set.value.value_unsigned = strtoull(param_value, NULL, 0);
1094 param_set.value_type = MT_PARAM_SET_UNSIGNED;
1095 param_set.value_len = entry->size;
1097 case MT_TYPE_STRING: {
1100 param_len = strlen(param_value) + 1;
1101 if (param_len > sizeof(param_set.value.value_fixed_str)) {
1102 param_set.value_type = MT_PARAM_SET_VAR_STR;
1103 param_set.value.value_var_str = param_value;
1105 param_set.value_type = MT_PARAM_SET_FIXED_STR;
1106 strlcpy(param_set.value.value_fixed_str, param_value,
1107 sizeof(param_set.value.value_fixed_str));
1109 param_set.value_len = param_len;
1113 errx(1, "Unknown parameter type %d for %s", entry->var_type,
1118 if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1)
1119 err(1, "MTIOCPARAMSET");
1121 if (param_set.status != MT_PARAM_STATUS_OK)
1122 errx(1, "Failed to set %s: %s", param_name,
1123 param_set.error_str);
1137 static struct mt_protect_info {
1139 struct mt_status_entry *entry;
1141 } mt_protect_list[] = {
1142 { "lbp_r", NULL, 0 },
1143 { "lbp_w", NULL, 0 },
1144 { "rbdp", NULL, 0 },
1145 { "pi_length", NULL, 0 },
1146 { "prot_method", NULL, 0 }
1149 #define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0]))
1151 #define MT_PROT_NAME "protection"
1154 mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data)
1157 int do_enable = 0, do_disable = 0, do_list = 0;
1158 int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0;
1159 int prot_method_set = 0, pi_length_set = 0;
1161 uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0;
1162 uint32_t prot_method = 0, pi_length = 0;
1163 struct mt_status_entry *prot_entry, *supported_entry;
1164 struct mt_status_entry *entry;
1165 struct mtparamset params[MT_NUM_PROTECT_PARAMS];
1166 struct mtsetlist param_list;
1170 while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) {
1174 rbdp = strtoul(optarg, NULL, 0);
1175 if ((rbdp != 0) && (rbdp != 1))
1176 errx(1, "valid values for -b are 0 and 1");
1189 pi_length = strtoul(optarg, NULL, 0);
1190 if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK)
1191 errx(1, "PI length %u > maximum %u",
1192 pi_length, SA_CTRL_DP_PI_LENGTH_MASK);
1195 prot_method_set = 1;
1196 prot_method = strtoul(optarg, NULL, 0);
1197 if (prot_method > SA_CTRL_DP_METHOD_MAX)
1198 errx(1, "Method %u > maximum %u",
1199 prot_method, SA_CTRL_DP_METHOD_MAX);
1203 lbp_r = strtoul(optarg, NULL, 0);
1204 if ((lbp_r != 0) && (lbp_r != 1))
1205 errx(1, "valid values for -r are 0 and 1");
1212 lbp_w = strtoul(optarg, NULL, 0);
1213 if ((lbp_w != 0) && (lbp_r != 1))
1214 errx(1, "valid values for -r are 0 and 1");
1221 if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set +
1222 prot_method_set + lbp_r_set + lbp_w_set) == 0)
1223 errx(1, "Need an argument for protect");
1225 if ((do_disable + do_enable + do_list) != 1)
1226 errx(1, "You must specify only one of -e, -d or -l");
1229 retval = mt_protect_print(status_data, verbose);
1232 if (do_enable != 0) {
1234 * Enable protection, but allow the user to override
1235 * settings if he doesn't want everything turned on.
1244 * If the user doesn't override it, we default to enabling
1245 * Reed-Solomon checkums.
1247 if (prot_method_set == 0)
1248 prot_method = SA_CTRL_DP_REED_SOLOMON;
1249 if (pi_length_set == 0)
1250 pi_length = SA_CTRL_DP_RS_LENGTH;
1251 } else if (do_disable != 0) {
1253 * If the user wants to disable protection, we ignore any
1254 * other parameters he has set. Everything gets set to 0.
1256 rbdp = lbp_w = lbp_r = 0;
1257 prot_method = pi_length = 0;
1260 prot_entry = mt_status_entry_find(status_data,
1261 __DECONST(char *, MT_PROT_NAME));
1262 if (prot_entry == NULL)
1263 errx(1, "Unable to find protection information status");
1265 supported_entry = mt_entry_find(prot_entry,
1266 __DECONST(char *, "protection_supported"));
1267 if (supported_entry == NULL)
1268 errx(1, "Unable to find protection support information");
1270 if (((supported_entry->var_type == MT_TYPE_INT)
1271 && (supported_entry->value_signed == 0))
1272 || ((supported_entry->var_type == MT_TYPE_UINT)
1273 && (supported_entry->value_unsigned == 0)))
1274 errx(1, "This device does not support protection information");
1276 mt_protect_list[MT_PP_LBP_R].value = lbp_r;
1277 mt_protect_list[MT_PP_LBP_W].value = lbp_w;
1278 mt_protect_list[MT_PP_RBDP].value = rbdp;
1279 mt_protect_list[MT_PP_PI_LENGTH].value = pi_length;
1280 mt_protect_list[MT_PP_PROT_METHOD].value = prot_method;
1282 bzero(¶ms, sizeof(params));
1283 bzero(¶m_list, sizeof(param_list));
1286 * Go through the list and make sure that we have this parameter,
1287 * and that it is still an unsigned integer. If not, we've got a
1290 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1291 entry = mt_entry_find(prot_entry,
1292 __DECONST(char *, mt_protect_list[i].name));
1293 if (entry == NULL) {
1294 errx(1, "Unable to find parameter %s",
1295 mt_protect_list[i].name);
1297 mt_protect_list[i].entry = entry;
1299 if (entry->var_type != MT_TYPE_UINT)
1300 errx(1, "Parameter %s is type %d, not unsigned, "
1301 "cannot proceed", mt_protect_list[i].name,
1303 snprintf(params[i].value_name, sizeof(params[i].value_name),
1304 "%s.%s", MT_PROT_NAME, mt_protect_list[i].name);
1305 /* XXX KDM unify types here */
1306 params[i].value_type = MT_PARAM_SET_UNSIGNED;
1307 params[i].value_len = sizeof(mt_protect_list[i].value);
1308 params[i].value.value_unsigned = mt_protect_list[i].value;
1311 param_list.num_params = MT_NUM_PROTECT_PARAMS;
1312 param_list.param_len = sizeof(params);
1313 param_list.params = params;
1315 if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1)
1316 err(1, "error issuing MTIOCSETLIST ioctl");
1318 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1319 if (params[i].status != MT_PARAM_STATUS_OK) {
1320 warnx("%s", params[i].error_str);
1330 mt_param(int argc, char **argv, int mtfd, char *xml_str,
1331 struct mt_status_data *status_data)
1333 int list = 0, do_set = 0, xml_dump = 0;
1334 char *param_name = NULL, *param_value = NULL;
1335 int retval = 0, quiet = 0;
1338 while ((c = getopt(argc, argv, "lp:qs:x")) != -1) {
1344 if (param_name != NULL) {
1345 warnx("Only one parameter name may be "
1350 param_name = strdup(optarg);
1356 if (param_value != NULL) {
1357 warnx("Only one parameter value may be "
1362 param_value = strdup(optarg);
1373 if ((list + do_set + xml_dump) != 1) {
1374 warnx("You must specify only one of -s, -l or -x");
1379 if (xml_dump != 0) {
1380 printf("%s", xml_str);
1386 if (param_name == NULL)
1387 errx(1, "You must specify -p with -s");
1389 retval = mt_set_param(mtfd, status_data, param_name,
1391 } else if (list != 0)
1392 retval = mt_param_list(status_data, param_name, quiet);
1401 mt_print_density_entry(struct mt_status_entry *density_root, int indent)
1403 struct mt_status_entry *entry;
1406 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1407 if (entry->var_type == MT_TYPE_NODE) {
1408 retval = mt_print_density_entry(entry, indent + 2);
1414 if ((strcmp(entry->entry_name, "primary_density_code") == 0)
1415 || (strcmp(entry->entry_name, "secondary_density_code") == 0)
1416 || (strcmp(entry->entry_name, "density_code") == 0)) {
1418 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1419 entry->desc : "", entry->entry_name,
1420 denstostring(entry->value_unsigned));
1421 } else if (strcmp(entry->entry_name, "density_flags") == 0) {
1422 printf("%*sMedium Access: ", indent, "");
1423 if (entry->value_unsigned & MT_DENS_WRITE_OK) {
1424 printf("Read and Write\n");
1426 printf("Read Only\n");
1428 printf("%*sDefault Density: %s\n", indent, "",
1429 (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" :
1431 printf("%*sDuplicate Density: %s\n", indent, "",
1432 (entry->value_unsigned & MT_DENS_DUP) ? "Yes" :
1434 } else if (strcmp(entry->entry_name, "media_width") == 0) {
1435 printf("%*s%s (%s): %.1f mm\n", indent, "",
1436 entry->desc ? entry->desc : "", entry->entry_name,
1437 (double)((double)entry->value_unsigned / 10));
1438 } else if (strcmp(entry->entry_name, "medium_length") == 0) {
1439 printf("%*s%s (%s): %ju m\n", indent, "",
1440 entry->desc ? entry->desc : "", entry->entry_name,
1441 (uintmax_t)entry->value_unsigned);
1442 } else if (strcmp(entry->entry_name, "capacity") == 0) {
1443 printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ?
1444 entry->desc : "", entry->entry_name,
1445 (uintmax_t)entry->value_unsigned);
1447 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1448 entry->desc : "", entry->entry_name, entry->value);
1456 mt_print_density_report(struct mt_status_entry *report_root, int indent)
1458 struct mt_status_entry *mt_report, *media_report;
1459 struct mt_status_entry *entry;
1462 mt_report = mt_entry_find(report_root,
1463 __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME));
1464 if (mt_report == NULL)
1467 media_report = mt_entry_find(report_root,
1468 __DECONST(char *, MT_MEDIA_REPORT_NAME));
1469 if (media_report == NULL)
1472 if ((mt_report->value_signed == 0)
1473 && (media_report->value_signed == 0)) {
1474 printf("%*sThis tape drive supports the following "
1475 "media densities:\n", indent, "");
1476 } else if ((mt_report->value_signed == 0)
1477 && (media_report->value_signed != 0)) {
1478 printf("%*sThe tape currently in this drive supports "
1479 "the following media densities:\n", indent, "");
1480 } else if ((mt_report->value_signed != 0)
1481 && (media_report->value_signed == 0)) {
1482 printf("%*sThis tape drive supports the following "
1483 "media types:\n", indent, "");
1485 printf("%*sThis tape currently in this drive supports "
1486 "the following media types:\n", indent, "");
1489 STAILQ_FOREACH(entry, &report_root->child_entries, links) {
1490 struct mt_status_nv *nv;
1492 if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0)
1495 STAILQ_FOREACH(nv, &entry->nv_list, links) {
1496 if (strcmp(nv->name, "num") != 0)
1504 printf("%*sDensity Entry", indent, "");
1506 printf(" %s", nv->value);
1509 retval = mt_print_density_entry(entry, indent + 2);
1518 mt_print_density(struct mt_status_entry *density_root, int indent)
1520 struct mt_status_entry *entry;
1524 * We should have this entry for every tape drive. This particular
1525 * value is reported via the mode page block header, not the
1526 * SCSI REPORT DENSITY SUPPORT command.
1528 entry = mt_entry_find(density_root,
1529 __DECONST(char *, MT_MEDIA_DENSITY_NAME));
1531 errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME);
1533 printf("%*sCurrent density: %s\n", indent, "",
1534 denstostring(entry->value_unsigned));
1537 * It isn't an error if we don't have any density reports. Tape
1538 * drives that don't support the REPORT DENSITY SUPPORT command
1539 * won't have any; they will only have the current density entry
1542 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1543 if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0)
1546 retval = mt_print_density_report(entry, indent);
1553 mt_getdensity(int argc, char **argv, char *xml_str,
1554 struct mt_status_data *status_data)
1557 int verbose = 0, xml_dump = 0;
1558 struct mt_status_entry *density_root = NULL;
1561 while ((c = getopt(argc, argv, "vx")) != -1) {
1572 if (xml_dump != 0) {
1573 printf("%s", xml_str);
1577 density_root = mt_status_entry_find(status_data,
1578 __DECONST(char *, MT_DENSITY_ROOT_NAME));
1579 if (density_root == NULL)
1580 errx(1, "Cannot find density root node %s",
1581 MT_DENSITY_ROOT_NAME);
1583 retval = mt_print_density(density_root, 0);
1592 "The \"eof\" command has been disabled.\n"
1593 "Use \"weof\" if you really want to write end-of-file marks,\n"
1594 "or \"eom\" if you rather want to skip to the end of "
1595 "recorded medium.\n");