2 * SPDX-License-Identifier: BSD-3-Clause
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions, and the following disclaimer,
39 * without modification.
40 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
41 * substantially similar to the "NO WARRANTY" disclaimer below
42 * ("Disclaimer") and any redistribution must be conditioned upon
43 * including a substantially similar Disclaimer requirement for further
44 * binary redistribution.
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
50 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
56 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGES.
59 * Authors: Ken Merry (Spectra Logic Corporation)
63 static const char copyright[] =
64 "@(#) Copyright (c) 1980, 1993\n\
65 The Regents of the University of California. All rights reserved.\n";
70 static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 5/4/95";
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
79 * magnetic tape manipulation program
81 #include <sys/types.h>
82 #include <sys/ioctl.h>
84 #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
122 #define MT_PLURAL(a) (a == 1) ? "" : "s"
125 MT_CMD_NONE = MTLOAD + 1,
130 static const struct commands {
132 unsigned long c_code;
136 { "bsf", MTBSF, 1, 0 },
137 { "bsr", MTBSR, 1, 0 },
138 /* XXX FreeBSD considered "eof" dangerous, since it's being
139 confused with "eom" (and is an alias for "weof" anyway) */
140 { "eof", MTWEOF, 0, DISABLE_THIS },
141 { "fsf", MTFSF, 1, 0 },
142 { "fsr", MTFSR, 1, 0 },
143 { "offline", MTOFFL, 1, 0 },
144 { "load", MTLOAD, 1, 0 },
145 { "rewind", MTREW, 1, 0 },
146 { "rewoffl", MTOFFL, 1, 0 },
147 { "ostatus", MTNOP, 1, 0 },
148 { "weof", MTWEOF, 0, ZERO_ALLOWED },
149 { "weofi", MTWEOFI, 0, ZERO_ALLOWED },
150 { "erase", MTERASE, 0, ZERO_ALLOWED},
151 { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
152 { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
153 { "eom", MTEOD, 1, 0 },
154 { "eod", MTEOD, 1, 0 },
155 { "smk", MTWSS, 0, 0 },
156 { "wss", MTWSS, 0, 0 },
157 { "fss", MTFSS, 1, 0 },
158 { "bss", MTBSS, 1, 0 },
159 { "comp", MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
160 { "retension", MTRETENS, 1, 0 },
161 { "rdhpos", MTIOCRDHPOS, 0, 0 },
162 { "rdspos", MTIOCRDSPOS, 0, 0 },
163 { "sethpos", MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
164 { "setspos", MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
165 { "errstat", MTIOCERRSTAT, 0, 0 },
166 { "setmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
167 { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
168 { "getmodel", MTIOCGETEOTMODEL, 0, 0 },
169 { "geteotmodel", MTIOCGETEOTMODEL, 0, 0 },
170 { "rblim", MTIOCRBLIM, 0, 0},
171 { "getdensity", MT_CMD_GETDENSITY, 0, USE_GETOPT},
172 { "status", MTIOCEXTGET, 0, USE_GETOPT },
173 { "locate", MTIOCEXTLOCATE, 0, USE_GETOPT },
174 { "param", MTIOCPARAMGET, 0, USE_GETOPT },
175 { "protect", MT_CMD_PROTECT, 0, USE_GETOPT },
180 static const char *getblksiz(int);
181 static void printreg(const char *, u_int, const char *);
182 static void status(struct mtget *);
183 static void usage(void);
184 const char *get_driver_state_str(int dsreg);
185 static void st_status (struct mtget *);
186 static int mt_locate(int argc, char **argv, int mtfd, const char *tape);
187 static int nstatus_print(int argc, char **argv, char *xml_str,
188 struct mt_status_data *status_data);
189 static int mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd,
191 static int mt_print_density_entry(struct mt_status_entry *density_root, int indent);
192 static int mt_print_density_report(struct mt_status_entry *report_root, int indent);
193 static int mt_print_density(struct mt_status_entry *density_root, int indent);
194 static int mt_getdensity(int argc, char **argv, char *xml_str,
195 struct mt_status_data *status_data);
196 static int mt_set_param(int mtfd, struct mt_status_data *status_data,
197 char *param_name, char *param_value);
198 static int mt_protect(int argc, char **argv, int mtfd,
199 struct mt_status_data *status_data);
200 static int mt_param(int argc, char **argv, int mtfd, char *xml_str,
201 struct mt_status_data *status_data);
202 static const char *denstostring (int d);
203 static u_int32_t stringtocomp(const char *s);
204 static const char *comptostring(u_int32_t comp);
205 static void warn_eof(void);
208 main(int argc, char *argv[])
210 const struct commands *comp;
211 struct mtget mt_status;
214 const char *p, *tape;
216 bzero(&mt_com, sizeof(mt_com));
218 if ((tape = getenv("TAPE")) == NULL)
221 while ((ch = getopt(argc, argv, "f:t:")) != -1)
239 len = strlen(p = *argv++);
240 for (comp = com;; comp++) {
241 if (comp->c_name == NULL)
242 errx(1, "%s: unknown command", p);
243 if (strncmp(p, comp->c_name, len) == 0)
246 if((comp->c_flags & NEED_2ARGS) && argc != 2)
248 if(comp->c_flags & DISABLE_THIS) {
251 if (comp->c_flags & USE_GETOPT) {
256 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
258 if (comp->c_code != MTNOP) {
259 mt_com.mt_op = comp->c_code;
261 if (!isdigit(**argv) &&
262 (comp->c_flags & IS_DENSITY)) {
264 mt_com.mt_count = mt_density_num(*argv);
265 if (mt_com.mt_count == 0)
266 errx(1, "%s: unknown density", *argv);
267 dcanon = denstostring(mt_com.mt_count);
268 if (strcmp(dcanon, *argv) != 0)
270 "Using \"%s\" as an alias for %s\n",
273 } else if (!isdigit(**argv) &&
274 (comp->c_flags & IS_COMP)) {
276 mt_com.mt_count = stringtocomp(*argv);
277 if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
278 errx(1, "%s: unknown compression",
281 } else if ((comp->c_flags & USE_GETOPT) == 0) {
283 /* allow for hex numbers; useful for density */
284 mt_com.mt_count = strtol(*argv, &q, 0);
287 if (((comp->c_flags & USE_GETOPT) == 0)
288 && (((mt_com.mt_count <=
289 ((comp->c_flags & ZERO_ALLOWED)? -1: 0))
290 && ((comp->c_flags & IS_COMP) == 0))
292 errx(1, "%s: illegal count", *argv);
296 switch (comp->c_code) {
301 struct scsi_tape_errors *s = &umn.scsi_errstat;
303 if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
305 (void)printf("Last I/O Residual: %u\n", s->io_resid);
306 (void)printf(" Last I/O Command:");
307 for (i = 0; i < sizeof (s->io_cdb); i++)
308 (void)printf(" %02X", s->io_cdb[i]);
310 (void)printf(" Last I/O Sense:\n\n\t");
311 for (i = 0; i < sizeof (s->io_sense); i++) {
312 (void)printf(" %02X", s->io_sense[i]);
313 if (((i + 1) & 0xf) == 0) {
314 (void)printf("\n\t");
318 (void)printf("Last Control Residual: %u\n",
320 (void)printf(" Last Control Command:");
321 for (i = 0; i < sizeof (s->ctl_cdb); i++)
322 (void)printf(" %02X", s->ctl_cdb[i]);
324 (void)printf(" Last Control Sense:\n\n\t");
325 for (i = 0; i < sizeof (s->ctl_sense); i++) {
326 (void)printf(" %02X", s->ctl_sense[i]);
327 if (((i + 1) & 0xf) == 0) {
328 (void)printf("\n\t");
331 (void)printf("\n\n");
339 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
341 (void)printf("%s: %s block location %u\n", tape,
342 (comp->c_code == MTIOCRDHPOS)? "hardware" :
350 u_int32_t block = (u_int32_t)mt_com.mt_count;
351 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
356 case MTIOCGETEOTMODEL:
359 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
361 (void)printf("%s: the model is %u filemar%s at EOT\n",
362 tape, om, (om > 1)? "ks" : "k");
366 case MTIOCSETEOTMODEL:
368 u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
369 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
371 if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
373 (void)printf("%s: old model was %u filemar%s at EOT\n",
374 tape, om, (om > 1)? "ks" : "k");
375 (void)printf("%s: new model is %u filemar%s at EOT\n",
376 tape, nm, (nm > 1)? "ks" : "k");
382 struct mtrblim rblim;
384 bzero(&rblim, sizeof(rblim));
386 if (ioctl(mtfd, MTIOCRBLIM, (caddr_t)&rblim) < 0)
389 " min blocksize %u byte%s\n"
390 " max blocksize %u byte%s\n"
391 " granularity %u byte%s\n",
392 tape, rblim.min_block_length,
393 MT_PLURAL(rblim.min_block_length),
394 rblim.max_block_length,
395 MT_PLURAL(rblim.max_block_length),
396 (1 << rblim.granularity),
397 MT_PLURAL((1 << rblim.granularity)));
404 case MT_CMD_GETDENSITY:
408 retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd,
417 retval = mt_locate(argc, argv, mtfd, tape);
424 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
425 err(1, "%s: %s", tape, comp->c_name);
427 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
435 static const struct tape_desc {
436 short t_type; /* type of magtape device */
437 const char *t_name; /* printing name */
438 const char *t_dsbits; /* "drive status" register */
439 const char *t_erbits; /* "error" register */
441 { MT_ISAR, "SCSI tape drive", 0, 0 },
446 * Interpret the status buffer returned
449 status(struct mtget *bp)
451 const struct tape_desc *mt;
453 for (mt = tapes;; mt++) {
454 if (mt->t_type == 0) {
455 (void)printf("%d: unknown tape drive type\n",
459 if (mt->t_type == bp->mt_type)
462 if(mt->t_type == MT_ISAR)
465 (void)printf("%s tape drive, residual=%d\n",
466 mt->t_name, bp->mt_resid);
467 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
468 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
474 * Print a register a la the %b format of the kernel's printf.
477 printreg(const char *s, u_int v, const char *bits)
482 if (bits && *bits == 8)
483 printf("%s=%o", s, v);
485 printf("%s=%x", s, v);
491 while ((i = *bits++)) {
492 if (v & (1 << (i-1))) {
496 for (; (c = *bits) > 32; bits++)
499 for (; *bits > 32; bits++)
509 (void)fprintf(stderr, "usage: mt [-f device] command [count]\n");
513 static const struct compression_types {
514 u_int32_t comp_number;
521 { 0xffffffff, "enable" },
522 { 0xffffffff, "on" },
530 const char *name = mt_density_name(d);
533 sprintf(buf, "0x%02x", d);
535 sprintf(buf, "0x%02x:%s", d, name);
546 sprintf(buf, "%d bytes", bs);
552 comptostring(u_int32_t comp)
555 const struct compression_types *ct;
557 if (comp == MT_COMP_DISABLED)
559 else if (comp == MT_COMP_UNSUPP)
560 return "unsupported";
562 for (ct = comp_types; ct->name; ct++)
563 if (ct->comp_number == comp)
566 if (ct->comp_number == 0xf0f0f0f0) {
567 sprintf(buf, "0x%x", comp);
574 stringtocomp(const char *s)
576 const struct compression_types *ct;
577 size_t l = strlen(s);
579 for (ct = comp_types; ct->name; ct++)
580 if (strncasecmp(ct->name, s, l) == 0)
583 return(ct->comp_number);
586 static struct driver_state {
589 } driver_states[] = {
590 { MTIO_DSREG_REST, "at rest" },
591 { MTIO_DSREG_RBSY, "Communicating with drive" },
592 { MTIO_DSREG_WR, "Writing" },
593 { MTIO_DSREG_FMK, "Writing Filemarks" },
594 { MTIO_DSREG_ZER, "Erasing" },
595 { MTIO_DSREG_RD, "Reading" },
596 { MTIO_DSREG_FWD, "Spacing Forward" },
597 { MTIO_DSREG_REV, "Spacing Reverse" },
598 { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" },
599 { MTIO_DSREG_REW, "Rewinding" },
600 { MTIO_DSREG_TEN, "Retensioning" },
601 { MTIO_DSREG_UNL, "Unloading" },
602 { MTIO_DSREG_LD, "Loading" },
606 get_driver_state_str(int dsreg)
610 for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) {
611 if (driver_states[i].dsreg == dsreg)
612 return (driver_states[i].desc);
619 st_status(struct mtget *bp)
621 printf("Mode Density Blocksize bpi "
623 "Current: %-17s %-12s %-7d %s\n"
624 "---------available modes---------\n"
625 "0: %-17s %-12s %-7d %s\n"
626 "1: %-17s %-12s %-7d %s\n"
627 "2: %-17s %-12s %-7d %s\n"
628 "3: %-17s %-12s %-7d %s\n",
629 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
630 mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
631 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
632 mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
633 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
634 mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
635 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
636 mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
637 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
638 mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
640 if (bp->mt_dsreg != MTIO_DSREG_NIL) {
641 const char sfmt[] = "Current Driver State: %s.\n";
642 printf("---------------------------------\n");
643 const char *state_str;
645 state_str = get_driver_state_str(bp->mt_dsreg);
646 if (state_str == NULL) {
648 (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
651 printf(sfmt, state_str);
654 if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
655 bp->mt_blkno == (daddr_t) -1)
657 printf("---------------------------------\n");
658 printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n",
659 bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
663 mt_locate(int argc, char **argv, int mtfd, const char *tape)
666 uint64_t logical_id = 0;
667 mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE;
668 int eod = 0, explicit = 0, immediate = 0;
669 int64_t partition = 0;
670 int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0;
674 bzero(&mtl, sizeof(mtl));
676 while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) {
680 logical_id = strtoull(optarg, NULL, 0);
681 dest_type = MT_LOCATE_DEST_OBJECT;
687 dest_type = MT_LOCATE_DEST_EOD;
691 * XXX KDM explicit address mode. Should we even
692 * allow this, since the driver doesn't operate in
693 * explicit address mode?
699 logical_id = strtoull(optarg, NULL, 0);
700 dest_type = MT_LOCATE_DEST_FILE;
705 * Immediate address mode. XXX KDM do we want to
706 * implement this? The other commands in the
707 * tape driver will need to be able to handle this.
713 * Change partition to the given partition.
715 partition = strtol(optarg, NULL, 0);
719 /* Go to the given set mark */
720 logical_id = strtoull(optarg, NULL, 0);
721 dest_type = MT_LOCATE_DEST_SET;
730 * These options are mutually exclusive. The user may only specify
733 if ((block_addr_set + file_set + eod + set_set) != 1)
734 errx(1, "You must specify only one of -b, -f, -e, or -s");
736 mtl.dest_type = dest_type;
738 case MT_LOCATE_DEST_OBJECT:
739 case MT_LOCATE_DEST_FILE:
740 case MT_LOCATE_DEST_SET:
741 mtl.logical_id = logical_id;
743 case MT_LOCATE_DEST_EOD:
748 mtl.flags |= MT_LOCATE_FLAG_IMMED;
750 if (partition_set != 0) {
751 mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART;
752 mtl.partition = partition;
756 mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT;
758 mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT;
760 if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1)
761 err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape);
772 MT_COMPRESSION_SUPPORTED = 5,
773 MT_COMPRESSION_ENABLED = 6,
774 MT_COMPRESSION_ALGORITHM = 7,
775 MT_MEDIA_DENSITY = 8,
776 MT_MEDIA_BLOCKSIZE = 9,
777 MT_CALCULATED_FILENO = 10,
778 MT_CALCULATED_REL_BLKNO = 11,
779 MT_REPORTED_FILENO = 12,
780 MT_REPORTED_BLKNO = 13,
794 MT_MAX_EFF_IOSIZE = 27
797 static struct mt_status_items {
799 struct mt_status_entry *entry;
800 } req_status_items[] = {
801 { "periph_name", NULL },
802 { "unit_number", NULL },
805 { "revision", NULL },
806 { "compression_supported", NULL },
807 { "compression_enabled", NULL },
808 { "compression_algorithm", NULL },
809 { "media_density", NULL },
810 { "media_blocksize", NULL },
811 { "calculated_fileno", NULL },
812 { "calculated_rel_blkno", NULL },
813 { "reported_fileno", NULL },
814 { "reported_blkno", NULL },
815 { "partition", NULL },
820 { "residual", NULL },
821 { "fixed_mode", NULL },
822 { "serial_num", NULL },
824 { "cpi_maxio", NULL },
827 { "blk_gran", NULL },
828 { "max_effective_iosize", NULL }
832 nstatus_print(int argc, char **argv, char *xml_str,
833 struct mt_status_data *status_data)
836 int64_t calculated_fileno, calculated_rel_blkno;
837 int64_t rep_fileno, rep_blkno, partition, resid;
839 const char *dens_str;
840 int dsreg, bop, eop, bpew;
843 unsigned int field_width;
847 while ((c = getopt(argc, argv, "xv")) != -1) {
861 printf("%s", xml_str);
865 for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0]));
869 name = __DECONST(char *, req_status_items[i].name);
870 req_status_items[i].entry = mt_status_entry_find(status_data,
872 if (req_status_items[i].entry == NULL) {
873 errx(1, "Cannot find status entry %s",
874 req_status_items[i].name);
878 printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n",
879 req_status_items[MT_PERIPH_NAME].entry->value,
880 (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned,
881 req_status_items[MT_VENDOR].entry->value,
882 req_status_items[MT_PRODUCT].entry->value,
883 req_status_items[MT_REVISION].entry->value,
884 (req_status_items[MT_SERIAL_NUM].entry->value) ?
885 req_status_items[MT_SERIAL_NUM].entry->value : "none");
886 printf("---------------------------------\n");
889 * We check to see whether we're in fixed mode or not, and don't
890 * just believe the blocksize. If the SILI bit is turned on, the
891 * blocksize will be set to 4, even though we're doing variable
892 * length (well, multiples of 4) blocks.
894 if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0)
895 snprintf(block_str, sizeof(block_str), "variable");
897 snprintf(block_str, sizeof(block_str), "%s",
898 getblksiz(req_status_items[
899 MT_MEDIA_BLOCKSIZE].entry->value_unsigned));
901 dens_str = denstostring(req_status_items[
902 MT_MEDIA_DENSITY].entry->value_unsigned);
903 if (dens_str == NULL)
906 dens_len = strlen(dens_str);
907 field_width = MAX(dens_len, 17);
908 printf("Mode %-*s Blocksize bpi Compression\n"
909 "Current: %-*s %-12s %-7d ",
910 field_width, "Density", field_width, dens_str, block_str,
911 mt_density_bp(req_status_items[
912 MT_MEDIA_DENSITY].entry->value_unsigned, TRUE));
914 if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0)
915 printf("unsupported\n");
916 else if (req_status_items[
917 MT_COMPRESSION_ENABLED].entry->value_signed == 0)
918 printf("disabled\n");
920 printf("enabled (%s)\n",
921 comptostring(req_status_items[
922 MT_COMPRESSION_ALGORITHM].entry->value_unsigned));
925 dsreg = req_status_items[MT_DSREG].entry->value_signed;
926 if (dsreg != MTIO_DSREG_NIL) {
927 const char sfmt[] = "Current Driver State: %s.\n";
928 printf("---------------------------------\n");
929 const char *state_str;
931 state_str = get_driver_state_str(dsreg);
932 if (state_str == NULL) {
934 (void) sprintf(foo, "Unknown state 0x%x", dsreg);
937 printf(sfmt, state_str);
940 resid = req_status_items[MT_RESID].entry->value_signed;
941 calculated_fileno = req_status_items[
942 MT_CALCULATED_FILENO].entry->value_signed;
943 calculated_rel_blkno = req_status_items[
944 MT_CALCULATED_REL_BLKNO].entry->value_signed;
945 rep_fileno = req_status_items[
946 MT_REPORTED_FILENO].entry->value_signed;
947 rep_blkno = req_status_items[
948 MT_REPORTED_BLKNO].entry->value_signed;
949 bop = req_status_items[MT_BOP].entry->value_signed;
950 eop = req_status_items[MT_EOP].entry->value_signed;
951 bpew = req_status_items[MT_BPEW].entry->value_signed;
952 partition = req_status_items[MT_PARTITION].entry->value_signed;
954 printf("---------------------------------\n");
955 printf("Partition: %3jd Calc File Number: %3jd "
956 " Calc Record Number: %jd\n"
957 "Residual: %3jd Reported File Number: %3jd "
958 "Reported Record Number: %jd\n", partition, calculated_fileno,
959 calculated_rel_blkno, resid, rep_fileno, rep_blkno);
962 if (bop > 0 || eop > 0 || bpew > 0) {
986 printf("---------------------------------\n");
987 printf("Tape I/O parameters:\n");
988 for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) {
989 printf(" %s (%s): %ju bytes\n",
990 req_status_items[i].entry->desc,
991 req_status_items[i].name,
992 req_status_items[i].entry->value_unsigned);
1000 mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape)
1002 struct mt_status_data status_data;
1004 struct mt_status_entry *entry;
1008 unsigned long ioctl_cmd;
1011 case MT_CMD_PROTECT:
1013 ioctl_cmd = MTIOCPARAMGET;
1016 ioctl_cmd = MTIOCEXTGET;
1020 retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str);
1022 err(1, "Couldn't get mt XML string");
1024 retval = mt_get_status(xml_str, &status_data);
1025 if (retval != XML_STATUS_OK) {
1026 warn("Couldn't get mt status for %s", tape);
1031 * This gets set if there are memory allocation or other errors in
1032 * our parsing of the XML.
1034 if (status_data.error != 0) {
1035 warnx("%s", status_data.error_str);
1040 STAILQ_FOREACH(entry, &status_data.entries, links)
1041 mt_status_tree_print(entry, 0, NULL);
1046 retval = nstatus_print(argc, argv, xml_str, &status_data);
1049 retval = mt_param(argc, argv, mtfd, xml_str, &status_data);
1051 case MT_CMD_PROTECT:
1052 retval = mt_protect(argc, argv, mtfd, &status_data);
1054 case MT_CMD_GETDENSITY:
1055 retval = mt_getdensity(argc, argv, xml_str, &status_data);
1060 if (xml_str != NULL)
1063 mt_status_free(&status_data);
1069 mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name,
1072 struct mt_status_entry *entry;
1073 struct mtparamset param_set;
1075 entry = mt_status_entry_find(status_data,
1076 __DECONST(char *, "mtparamget"));
1078 errx(1, "Cannot find parameter root node");
1080 bzero(¶m_set, sizeof(param_set));
1081 entry = mt_entry_find(entry, param_name);
1083 errx(1, "Unknown parameter name \"%s\"", param_name);
1085 strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name));
1087 switch (entry->var_type) {
1089 param_set.value.value_signed = strtoll(param_value, NULL, 0);
1090 param_set.value_type = MT_PARAM_SET_SIGNED;
1091 param_set.value_len = entry->size;
1094 param_set.value.value_unsigned = strtoull(param_value, NULL, 0);
1095 param_set.value_type = MT_PARAM_SET_UNSIGNED;
1096 param_set.value_len = entry->size;
1098 case MT_TYPE_STRING: {
1101 param_len = strlen(param_value) + 1;
1102 if (param_len > sizeof(param_set.value.value_fixed_str)) {
1103 param_set.value_type = MT_PARAM_SET_VAR_STR;
1104 param_set.value.value_var_str = param_value;
1106 param_set.value_type = MT_PARAM_SET_FIXED_STR;
1107 strlcpy(param_set.value.value_fixed_str, param_value,
1108 sizeof(param_set.value.value_fixed_str));
1110 param_set.value_len = param_len;
1114 errx(1, "Unknown parameter type %d for %s", entry->var_type,
1119 if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1)
1120 err(1, "MTIOCPARAMSET");
1122 if (param_set.status != MT_PARAM_STATUS_OK)
1123 errx(1, "Failed to set %s: %s", param_name,
1124 param_set.error_str);
1138 static struct mt_protect_info {
1140 struct mt_status_entry *entry;
1142 } mt_protect_list[] = {
1143 { "lbp_r", NULL, 0 },
1144 { "lbp_w", NULL, 0 },
1145 { "rbdp", NULL, 0 },
1146 { "pi_length", NULL, 0 },
1147 { "prot_method", NULL, 0 }
1150 #define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0]))
1152 #define MT_PROT_NAME "protection"
1155 mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data)
1158 int do_enable = 0, do_disable = 0, do_list = 0;
1159 int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0;
1160 int prot_method_set = 0, pi_length_set = 0;
1162 uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0;
1163 uint32_t prot_method = 0, pi_length = 0;
1164 struct mt_status_entry *prot_entry, *supported_entry;
1165 struct mt_status_entry *entry;
1166 struct mtparamset params[MT_NUM_PROTECT_PARAMS];
1167 struct mtsetlist param_list;
1171 while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) {
1175 rbdp = strtoul(optarg, NULL, 0);
1176 if ((rbdp != 0) && (rbdp != 1))
1177 errx(1, "valid values for -b are 0 and 1");
1190 pi_length = strtoul(optarg, NULL, 0);
1191 if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK)
1192 errx(1, "PI length %u > maximum %u",
1193 pi_length, SA_CTRL_DP_PI_LENGTH_MASK);
1196 prot_method_set = 1;
1197 prot_method = strtoul(optarg, NULL, 0);
1198 if (prot_method > SA_CTRL_DP_METHOD_MAX)
1199 errx(1, "Method %u > maximum %u",
1200 prot_method, SA_CTRL_DP_METHOD_MAX);
1204 lbp_r = strtoul(optarg, NULL, 0);
1205 if ((lbp_r != 0) && (lbp_r != 1))
1206 errx(1, "valid values for -r are 0 and 1");
1213 lbp_w = strtoul(optarg, NULL, 0);
1214 if ((lbp_w != 0) && (lbp_r != 1))
1215 errx(1, "valid values for -r are 0 and 1");
1222 if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set +
1223 prot_method_set + lbp_r_set + lbp_w_set) == 0)
1224 errx(1, "Need an argument for protect");
1226 if ((do_disable + do_enable + do_list) != 1)
1227 errx(1, "You must specify only one of -e, -d or -l");
1230 retval = mt_protect_print(status_data, verbose);
1233 if (do_enable != 0) {
1235 * Enable protection, but allow the user to override
1236 * settings if he doesn't want everything turned on.
1245 * If the user doesn't override it, we default to enabling
1246 * Reed-Solomon checkums.
1248 if (prot_method_set == 0)
1249 prot_method = SA_CTRL_DP_REED_SOLOMON;
1250 if (pi_length_set == 0)
1251 pi_length = SA_CTRL_DP_RS_LENGTH;
1252 } else if (do_disable != 0) {
1254 * If the user wants to disable protection, we ignore any
1255 * other parameters he has set. Everything gets set to 0.
1257 rbdp = lbp_w = lbp_r = 0;
1258 prot_method = pi_length = 0;
1261 prot_entry = mt_status_entry_find(status_data,
1262 __DECONST(char *, MT_PROT_NAME));
1263 if (prot_entry == NULL)
1264 errx(1, "Unable to find protection information status");
1266 supported_entry = mt_entry_find(prot_entry,
1267 __DECONST(char *, "protection_supported"));
1268 if (supported_entry == NULL)
1269 errx(1, "Unable to find protection support information");
1271 if (((supported_entry->var_type == MT_TYPE_INT)
1272 && (supported_entry->value_signed == 0))
1273 || ((supported_entry->var_type == MT_TYPE_UINT)
1274 && (supported_entry->value_unsigned == 0)))
1275 errx(1, "This device does not support protection information");
1277 mt_protect_list[MT_PP_LBP_R].value = lbp_r;
1278 mt_protect_list[MT_PP_LBP_W].value = lbp_w;
1279 mt_protect_list[MT_PP_RBDP].value = rbdp;
1280 mt_protect_list[MT_PP_PI_LENGTH].value = pi_length;
1281 mt_protect_list[MT_PP_PROT_METHOD].value = prot_method;
1283 bzero(¶ms, sizeof(params));
1284 bzero(¶m_list, sizeof(param_list));
1287 * Go through the list and make sure that we have this parameter,
1288 * and that it is still an unsigned integer. If not, we've got a
1291 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1292 entry = mt_entry_find(prot_entry,
1293 __DECONST(char *, mt_protect_list[i].name));
1294 if (entry == NULL) {
1295 errx(1, "Unable to find parameter %s",
1296 mt_protect_list[i].name);
1298 mt_protect_list[i].entry = entry;
1300 if (entry->var_type != MT_TYPE_UINT)
1301 errx(1, "Parameter %s is type %d, not unsigned, "
1302 "cannot proceed", mt_protect_list[i].name,
1304 snprintf(params[i].value_name, sizeof(params[i].value_name),
1305 "%s.%s", MT_PROT_NAME, mt_protect_list[i].name);
1306 /* XXX KDM unify types here */
1307 params[i].value_type = MT_PARAM_SET_UNSIGNED;
1308 params[i].value_len = sizeof(mt_protect_list[i].value);
1309 params[i].value.value_unsigned = mt_protect_list[i].value;
1312 param_list.num_params = MT_NUM_PROTECT_PARAMS;
1313 param_list.param_len = sizeof(params);
1314 param_list.params = params;
1316 if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1)
1317 err(1, "error issuing MTIOCSETLIST ioctl");
1319 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1320 if (params[i].status != MT_PARAM_STATUS_OK) {
1321 warnx("%s", params[i].error_str);
1331 mt_param(int argc, char **argv, int mtfd, char *xml_str,
1332 struct mt_status_data *status_data)
1334 int list = 0, do_set = 0, xml_dump = 0;
1335 char *param_name = NULL, *param_value = NULL;
1336 int retval = 0, quiet = 0;
1339 while ((c = getopt(argc, argv, "lp:qs:x")) != -1) {
1345 if (param_name != NULL) {
1346 warnx("Only one parameter name may be "
1351 param_name = strdup(optarg);
1357 if (param_value != NULL) {
1358 warnx("Only one parameter value may be "
1363 param_value = strdup(optarg);
1374 if ((list + do_set + xml_dump) != 1) {
1375 warnx("You must specify only one of -s, -l or -x");
1380 if (xml_dump != 0) {
1381 printf("%s", xml_str);
1387 if (param_name == NULL)
1388 errx(1, "You must specify -p with -s");
1390 retval = mt_set_param(mtfd, status_data, param_name,
1392 } else if (list != 0)
1393 retval = mt_param_list(status_data, param_name, quiet);
1402 mt_print_density_entry(struct mt_status_entry *density_root, int indent)
1404 struct mt_status_entry *entry;
1407 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1408 if (entry->var_type == MT_TYPE_NODE) {
1409 retval = mt_print_density_entry(entry, indent + 2);
1415 if ((strcmp(entry->entry_name, "primary_density_code") == 0)
1416 || (strcmp(entry->entry_name, "secondary_density_code") == 0)
1417 || (strcmp(entry->entry_name, "density_code") == 0)) {
1419 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1420 entry->desc : "", entry->entry_name,
1421 denstostring(entry->value_unsigned));
1422 } else if (strcmp(entry->entry_name, "density_flags") == 0) {
1423 printf("%*sMedium Access: ", indent, "");
1424 if (entry->value_unsigned & MT_DENS_WRITE_OK) {
1425 printf("Read and Write\n");
1427 printf("Read Only\n");
1429 printf("%*sDefault Density: %s\n", indent, "",
1430 (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" :
1432 printf("%*sDuplicate Density: %s\n", indent, "",
1433 (entry->value_unsigned & MT_DENS_DUP) ? "Yes" :
1435 } else if (strcmp(entry->entry_name, "media_width") == 0) {
1436 printf("%*s%s (%s): %.1f mm\n", indent, "",
1437 entry->desc ? entry->desc : "", entry->entry_name,
1438 (double)((double)entry->value_unsigned / 10));
1439 } else if (strcmp(entry->entry_name, "medium_length") == 0) {
1440 printf("%*s%s (%s): %ju m\n", indent, "",
1441 entry->desc ? entry->desc : "", entry->entry_name,
1442 (uintmax_t)entry->value_unsigned);
1443 } else if (strcmp(entry->entry_name, "capacity") == 0) {
1444 printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ?
1445 entry->desc : "", entry->entry_name,
1446 (uintmax_t)entry->value_unsigned);
1448 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1449 entry->desc : "", entry->entry_name, entry->value);
1457 mt_print_density_report(struct mt_status_entry *report_root, int indent)
1459 struct mt_status_entry *mt_report, *media_report;
1460 struct mt_status_entry *entry;
1463 mt_report = mt_entry_find(report_root,
1464 __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME));
1465 if (mt_report == NULL)
1468 media_report = mt_entry_find(report_root,
1469 __DECONST(char *, MT_MEDIA_REPORT_NAME));
1470 if (media_report == NULL)
1473 if ((mt_report->value_signed == 0)
1474 && (media_report->value_signed == 0)) {
1475 printf("%*sThis tape drive supports the following "
1476 "media densities:\n", indent, "");
1477 } else if ((mt_report->value_signed == 0)
1478 && (media_report->value_signed != 0)) {
1479 printf("%*sThe tape currently in this drive supports "
1480 "the following media densities:\n", indent, "");
1481 } else if ((mt_report->value_signed != 0)
1482 && (media_report->value_signed == 0)) {
1483 printf("%*sThis tape drive supports the following "
1484 "media types:\n", indent, "");
1486 printf("%*sThis tape currently in this drive supports "
1487 "the following media types:\n", indent, "");
1490 STAILQ_FOREACH(entry, &report_root->child_entries, links) {
1491 struct mt_status_nv *nv;
1493 if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0)
1496 STAILQ_FOREACH(nv, &entry->nv_list, links) {
1497 if (strcmp(nv->name, "num") != 0)
1505 printf("%*sDensity Entry", indent, "");
1507 printf(" %s", nv->value);
1510 retval = mt_print_density_entry(entry, indent + 2);
1519 mt_print_density(struct mt_status_entry *density_root, int indent)
1521 struct mt_status_entry *entry;
1525 * We should have this entry for every tape drive. This particular
1526 * value is reported via the mode page block header, not the
1527 * SCSI REPORT DENSITY SUPPORT command.
1529 entry = mt_entry_find(density_root,
1530 __DECONST(char *, MT_MEDIA_DENSITY_NAME));
1532 errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME);
1534 printf("%*sCurrent density: %s\n", indent, "",
1535 denstostring(entry->value_unsigned));
1538 * It isn't an error if we don't have any density reports. Tape
1539 * drives that don't support the REPORT DENSITY SUPPORT command
1540 * won't have any; they will only have the current density entry
1543 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1544 if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0)
1547 retval = mt_print_density_report(entry, indent);
1554 mt_getdensity(int argc, char **argv, char *xml_str,
1555 struct mt_status_data *status_data)
1558 int verbose = 0, xml_dump = 0;
1559 struct mt_status_entry *density_root = NULL;
1562 while ((c = getopt(argc, argv, "vx")) != -1) {
1573 if (xml_dump != 0) {
1574 printf("%s", xml_str);
1578 density_root = mt_status_entry_find(status_data,
1579 __DECONST(char *, MT_DENSITY_ROOT_NAME));
1580 if (density_root == NULL)
1581 errx(1, "Cannot find density root node %s",
1582 MT_DENSITY_ROOT_NAME);
1584 retval = mt_print_density(density_root, 0);
1593 "The \"eof\" command has been disabled.\n"
1594 "Use \"weof\" if you really want to write end-of-file marks,\n"
1595 "or \"eom\" if you rather want to skip to the end of "
1596 "recorded medium.\n");