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>
77 * magnetic tape manipulation program
79 #include <sys/types.h>
80 #include <sys/ioctl.h>
82 #include <sys/queue.h>
97 #include <cam/cam_ccb.h>
98 #include <cam/cam_periph.h>
99 #include <cam/scsi/scsi_all.h>
100 #include <cam/scsi/scsi_sa.h>
102 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
104 #define NEED_2ARGS 0x01
105 #define ZERO_ALLOWED 0x02
106 #define IS_DENSITY 0x04
107 #define DISABLE_THIS 0x08
109 #define USE_GETOPT 0x20
118 #define MAX(a, b) (a > b) ? a : b
120 #define MT_PLURAL(a) (a == 1) ? "" : "s"
123 MT_CMD_NONE = MTLOAD + 1,
128 static const struct commands {
130 unsigned long c_code;
134 { "bsf", MTBSF, 1, 0 },
135 { "bsr", MTBSR, 1, 0 },
136 /* XXX FreeBSD considered "eof" dangerous, since it's being
137 confused with "eom" (and is an alias for "weof" anyway) */
138 { "eof", MTWEOF, 0, DISABLE_THIS },
139 { "fsf", MTFSF, 1, 0 },
140 { "fsr", MTFSR, 1, 0 },
141 { "offline", MTOFFL, 1, 0 },
142 { "load", MTLOAD, 1, 0 },
143 { "rewind", MTREW, 1, 0 },
144 { "rewoffl", MTOFFL, 1, 0 },
145 { "ostatus", MTNOP, 1, 0 },
146 { "weof", MTWEOF, 0, ZERO_ALLOWED },
147 { "weofi", MTWEOFI, 0, ZERO_ALLOWED },
148 { "erase", MTERASE, 0, ZERO_ALLOWED},
149 { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
150 { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
151 { "eom", MTEOD, 1, 0 },
152 { "eod", MTEOD, 1, 0 },
153 { "smk", MTWSS, 0, 0 },
154 { "wss", MTWSS, 0, 0 },
155 { "fss", MTFSS, 1, 0 },
156 { "bss", MTBSS, 1, 0 },
157 { "comp", MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
158 { "retension", MTRETENS, 1, 0 },
159 { "rdhpos", MTIOCRDHPOS, 0, 0 },
160 { "rdspos", MTIOCRDSPOS, 0, 0 },
161 { "sethpos", MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
162 { "setspos", MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
163 { "errstat", MTIOCERRSTAT, 0, 0 },
164 { "setmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
165 { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
166 { "getmodel", MTIOCGETEOTMODEL, 0, 0 },
167 { "geteotmodel", MTIOCGETEOTMODEL, 0, 0 },
168 { "rblim", MTIOCRBLIM, 0, 0},
169 { "getdensity", MT_CMD_GETDENSITY, 0, USE_GETOPT},
170 { "status", MTIOCEXTGET, 0, USE_GETOPT },
171 { "locate", MTIOCEXTLOCATE, 0, USE_GETOPT },
172 { "param", MTIOCPARAMGET, 0, USE_GETOPT },
173 { "protect", MT_CMD_PROTECT, 0, USE_GETOPT },
178 static const char *getblksiz(int);
179 static void printreg(const char *, u_int, const char *);
180 static void status(struct mtget *);
181 static void usage(void) __dead2;
182 const char *get_driver_state_str(int dsreg);
183 static void st_status (struct mtget *);
184 static int mt_locate(int argc, char **argv, int mtfd, const char *tape);
185 static int nstatus_print(int argc, char **argv, char *xml_str,
186 struct mt_status_data *status_data);
187 static int mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd,
189 static int mt_print_density_entry(struct mt_status_entry *density_root, int indent);
190 static int mt_print_density_report(struct mt_status_entry *report_root, int indent);
191 static int mt_print_density(struct mt_status_entry *density_root, int indent);
192 static int mt_getdensity(int argc, char **argv, char *xml_str,
193 struct mt_status_data *status_data);
194 static int mt_set_param(int mtfd, struct mt_status_data *status_data,
195 char *param_name, char *param_value);
196 static int mt_protect(int argc, char **argv, int mtfd,
197 struct mt_status_data *status_data);
198 static int mt_param(int argc, char **argv, int mtfd, char *xml_str,
199 struct mt_status_data *status_data);
200 static const char *denstostring (int d);
201 static u_int32_t stringtocomp(const char *s);
202 static const char *comptostring(u_int32_t comp);
203 static void warn_eof(void);
206 main(int argc, char *argv[])
208 const struct commands *comp;
209 struct mtget mt_status;
212 const char *p, *tape;
214 bzero(&mt_com, sizeof(mt_com));
216 if ((tape = getenv("TAPE")) == NULL)
219 while ((ch = getopt(argc, argv, "f:t:")) != -1)
237 len = strlen(p = *argv++);
238 for (comp = com;; comp++) {
239 if (comp->c_name == NULL)
240 errx(1, "%s: unknown command", p);
241 if (strncmp(p, comp->c_name, len) == 0)
244 if((comp->c_flags & NEED_2ARGS) && argc != 2)
246 if(comp->c_flags & DISABLE_THIS) {
249 if (comp->c_flags & USE_GETOPT) {
254 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
256 if (comp->c_code != MTNOP) {
257 mt_com.mt_op = comp->c_code;
259 if (!isdigit(**argv) &&
260 (comp->c_flags & IS_DENSITY)) {
262 mt_com.mt_count = mt_density_num(*argv);
263 if (mt_com.mt_count == 0)
264 errx(1, "%s: unknown density", *argv);
265 dcanon = denstostring(mt_com.mt_count);
266 if (strcmp(dcanon, *argv) != 0)
268 "Using \"%s\" as an alias for %s\n",
271 } else if (!isdigit(**argv) &&
272 (comp->c_flags & IS_COMP)) {
274 mt_com.mt_count = stringtocomp(*argv);
275 if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
276 errx(1, "%s: unknown compression",
279 } else if ((comp->c_flags & USE_GETOPT) == 0) {
281 /* allow for hex numbers; useful for density */
282 mt_com.mt_count = strtol(*argv, &q, 0);
285 if (((comp->c_flags & USE_GETOPT) == 0)
286 && (((mt_com.mt_count <=
287 ((comp->c_flags & ZERO_ALLOWED)? -1: 0))
288 && ((comp->c_flags & IS_COMP) == 0))
290 errx(1, "%s: illegal count", *argv);
294 switch (comp->c_code) {
299 struct scsi_tape_errors *s = &umn.scsi_errstat;
301 if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
303 (void)printf("Last I/O Residual: %u\n", s->io_resid);
304 (void)printf(" Last I/O Command:");
305 for (i = 0; i < sizeof (s->io_cdb); i++)
306 (void)printf(" %02X", s->io_cdb[i]);
308 (void)printf(" Last I/O Sense:\n\n\t");
309 for (i = 0; i < sizeof (s->io_sense); i++) {
310 (void)printf(" %02X", s->io_sense[i]);
311 if (((i + 1) & 0xf) == 0) {
312 (void)printf("\n\t");
316 (void)printf("Last Control Residual: %u\n",
318 (void)printf(" Last Control Command:");
319 for (i = 0; i < sizeof (s->ctl_cdb); i++)
320 (void)printf(" %02X", s->ctl_cdb[i]);
322 (void)printf(" Last Control Sense:\n\n\t");
323 for (i = 0; i < sizeof (s->ctl_sense); i++) {
324 (void)printf(" %02X", s->ctl_sense[i]);
325 if (((i + 1) & 0xf) == 0) {
326 (void)printf("\n\t");
329 (void)printf("\n\n");
337 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
339 (void)printf("%s: %s block location %u\n", tape,
340 (comp->c_code == MTIOCRDHPOS)? "hardware" :
348 u_int32_t block = (u_int32_t)mt_com.mt_count;
349 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
354 case MTIOCGETEOTMODEL:
357 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
359 (void)printf("%s: the model is %u filemar%s at EOT\n",
360 tape, om, (om > 1)? "ks" : "k");
364 case MTIOCSETEOTMODEL:
366 u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
367 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
369 if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
371 (void)printf("%s: old model was %u filemar%s at EOT\n",
372 tape, om, (om > 1)? "ks" : "k");
373 (void)printf("%s: new model is %u filemar%s at EOT\n",
374 tape, nm, (nm > 1)? "ks" : "k");
380 struct mtrblim rblim;
382 bzero(&rblim, sizeof(rblim));
384 if (ioctl(mtfd, MTIOCRBLIM, (caddr_t)&rblim) < 0)
387 " min blocksize %u byte%s\n"
388 " max blocksize %u byte%s\n"
389 " granularity %u byte%s\n",
390 tape, rblim.min_block_length,
391 MT_PLURAL(rblim.min_block_length),
392 rblim.max_block_length,
393 MT_PLURAL(rblim.max_block_length),
394 (1 << rblim.granularity),
395 MT_PLURAL((1 << rblim.granularity)));
402 case MT_CMD_GETDENSITY:
406 retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd,
415 retval = mt_locate(argc, argv, mtfd, tape);
422 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
423 err(1, "%s: %s", tape, comp->c_name);
425 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
433 static const struct tape_desc {
434 short t_type; /* type of magtape device */
435 const char *t_name; /* printing name */
436 const char *t_dsbits; /* "drive status" register */
437 const char *t_erbits; /* "error" register */
439 { MT_ISAR, "SCSI tape drive", 0, 0 },
444 * Interpret the status buffer returned
447 status(struct mtget *bp)
449 const struct tape_desc *mt;
451 for (mt = tapes;; mt++) {
452 if (mt->t_type == 0) {
453 (void)printf("%d: unknown tape drive type\n",
457 if (mt->t_type == bp->mt_type)
460 if(mt->t_type == MT_ISAR)
463 (void)printf("%s tape drive, residual=%d\n",
464 mt->t_name, bp->mt_resid);
465 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
466 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
472 * Print a register a la the %b format of the kernel's printf.
475 printreg(const char *s, u_int v, const char *bits)
480 if (bits && *bits == 8)
481 printf("%s=%o", s, v);
483 printf("%s=%x", s, v);
489 while ((i = *bits++)) {
490 if (v & (1 << (i-1))) {
494 for (; (c = *bits) > 32; bits++)
497 for (; *bits > 32; bits++)
507 (void)fprintf(stderr, "usage: mt [-f device] command [count]\n");
511 static const struct compression_types {
512 u_int32_t comp_number;
519 { 0xffffffff, "enable" },
520 { 0xffffffff, "on" },
528 const char *name = mt_density_name(d);
531 sprintf(buf, "0x%02x", d);
533 sprintf(buf, "0x%02x:%s", d, name);
544 sprintf(buf, "%d bytes", bs);
550 comptostring(u_int32_t comp)
553 const struct compression_types *ct;
555 if (comp == MT_COMP_DISABLED)
557 else if (comp == MT_COMP_UNSUPP)
558 return "unsupported";
560 for (ct = comp_types; ct->name; ct++)
561 if (ct->comp_number == comp)
564 if (ct->comp_number == 0xf0f0f0f0) {
565 sprintf(buf, "0x%x", comp);
572 stringtocomp(const char *s)
574 const struct compression_types *ct;
575 size_t l = strlen(s);
577 for (ct = comp_types; ct->name; ct++)
578 if (strncasecmp(ct->name, s, l) == 0)
581 return(ct->comp_number);
584 static struct driver_state {
587 } driver_states[] = {
588 { MTIO_DSREG_REST, "at rest" },
589 { MTIO_DSREG_RBSY, "Communicating with drive" },
590 { MTIO_DSREG_WR, "Writing" },
591 { MTIO_DSREG_FMK, "Writing Filemarks" },
592 { MTIO_DSREG_ZER, "Erasing" },
593 { MTIO_DSREG_RD, "Reading" },
594 { MTIO_DSREG_FWD, "Spacing Forward" },
595 { MTIO_DSREG_REV, "Spacing Reverse" },
596 { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" },
597 { MTIO_DSREG_REW, "Rewinding" },
598 { MTIO_DSREG_TEN, "Retensioning" },
599 { MTIO_DSREG_UNL, "Unloading" },
600 { MTIO_DSREG_LD, "Loading" },
604 get_driver_state_str(int dsreg)
608 for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) {
609 if (driver_states[i].dsreg == dsreg)
610 return (driver_states[i].desc);
617 st_status(struct mtget *bp)
619 printf("Mode Density Blocksize bpi "
621 "Current: %-17s %-12s %-7d %s\n"
622 "---------available modes---------\n"
623 "0: %-17s %-12s %-7d %s\n"
624 "1: %-17s %-12s %-7d %s\n"
625 "2: %-17s %-12s %-7d %s\n"
626 "3: %-17s %-12s %-7d %s\n",
627 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
628 mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
629 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
630 mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
631 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
632 mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
633 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
634 mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
635 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
636 mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
638 if (bp->mt_dsreg != MTIO_DSREG_NIL) {
639 const char sfmt[] = "Current Driver State: %s.\n";
640 printf("---------------------------------\n");
641 const char *state_str;
643 state_str = get_driver_state_str(bp->mt_dsreg);
644 if (state_str == NULL) {
646 (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
649 printf(sfmt, state_str);
652 if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
653 bp->mt_blkno == (daddr_t) -1)
655 printf("---------------------------------\n");
656 printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n",
657 bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
661 mt_locate(int argc, char **argv, int mtfd, const char *tape)
664 uint64_t logical_id = 0;
665 mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE;
666 int eod = 0, explicit = 0, immediate = 0;
667 int64_t partition = 0;
668 int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0;
672 bzero(&mtl, sizeof(mtl));
674 while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) {
678 logical_id = strtoull(optarg, NULL, 0);
679 dest_type = MT_LOCATE_DEST_OBJECT;
685 dest_type = MT_LOCATE_DEST_EOD;
689 * XXX KDM explicit address mode. Should we even
690 * allow this, since the driver doesn't operate in
691 * explicit address mode?
697 logical_id = strtoull(optarg, NULL, 0);
698 dest_type = MT_LOCATE_DEST_FILE;
703 * Immediate address mode. XXX KDM do we want to
704 * implement this? The other commands in the
705 * tape driver will need to be able to handle this.
711 * Change partition to the given partition.
713 partition = strtol(optarg, NULL, 0);
717 /* Go to the given set mark */
718 logical_id = strtoull(optarg, NULL, 0);
719 dest_type = MT_LOCATE_DEST_SET;
728 * These options are mutually exclusive. The user may only specify
731 if ((block_addr_set + file_set + eod + set_set) != 1)
732 errx(1, "You must specify only one of -b, -f, -e, or -s");
734 mtl.dest_type = dest_type;
736 case MT_LOCATE_DEST_OBJECT:
737 case MT_LOCATE_DEST_FILE:
738 case MT_LOCATE_DEST_SET:
739 mtl.logical_id = logical_id;
741 case MT_LOCATE_DEST_EOD:
746 mtl.flags |= MT_LOCATE_FLAG_IMMED;
748 if (partition_set != 0) {
749 mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART;
750 mtl.partition = partition;
754 mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT;
756 mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT;
758 if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1)
759 err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape);
770 MT_COMPRESSION_SUPPORTED = 5,
771 MT_COMPRESSION_ENABLED = 6,
772 MT_COMPRESSION_ALGORITHM = 7,
773 MT_MEDIA_DENSITY = 8,
774 MT_MEDIA_BLOCKSIZE = 9,
775 MT_CALCULATED_FILENO = 10,
776 MT_CALCULATED_REL_BLKNO = 11,
777 MT_REPORTED_FILENO = 12,
778 MT_REPORTED_BLKNO = 13,
792 MT_MAX_EFF_IOSIZE = 27
795 static struct mt_status_items {
797 struct mt_status_entry *entry;
798 } req_status_items[] = {
799 { "periph_name", NULL },
800 { "unit_number", NULL },
803 { "revision", NULL },
804 { "compression_supported", NULL },
805 { "compression_enabled", NULL },
806 { "compression_algorithm", NULL },
807 { "media_density", NULL },
808 { "media_blocksize", NULL },
809 { "calculated_fileno", NULL },
810 { "calculated_rel_blkno", NULL },
811 { "reported_fileno", NULL },
812 { "reported_blkno", NULL },
813 { "partition", NULL },
818 { "residual", NULL },
819 { "fixed_mode", NULL },
820 { "serial_num", NULL },
822 { "cpi_maxio", NULL },
825 { "blk_gran", NULL },
826 { "max_effective_iosize", NULL }
830 nstatus_print(int argc, char **argv, char *xml_str,
831 struct mt_status_data *status_data)
834 int64_t calculated_fileno, calculated_rel_blkno;
835 int64_t rep_fileno, rep_blkno, partition, resid;
837 const char *dens_str;
838 int dsreg, bop, eop, bpew;
841 unsigned int field_width;
845 while ((c = getopt(argc, argv, "xv")) != -1) {
859 printf("%s", xml_str);
863 for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0]));
867 name = __DECONST(char *, req_status_items[i].name);
868 req_status_items[i].entry = mt_status_entry_find(status_data,
870 if (req_status_items[i].entry == NULL) {
871 errx(1, "Cannot find status entry %s",
872 req_status_items[i].name);
876 printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n",
877 req_status_items[MT_PERIPH_NAME].entry->value,
878 (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned,
879 req_status_items[MT_VENDOR].entry->value,
880 req_status_items[MT_PRODUCT].entry->value,
881 req_status_items[MT_REVISION].entry->value,
882 (req_status_items[MT_SERIAL_NUM].entry->value) ?
883 req_status_items[MT_SERIAL_NUM].entry->value : "none");
884 printf("---------------------------------\n");
887 * We check to see whether we're in fixed mode or not, and don't
888 * just believe the blocksize. If the SILI bit is turned on, the
889 * blocksize will be set to 4, even though we're doing variable
890 * length (well, multiples of 4) blocks.
892 if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0)
893 snprintf(block_str, sizeof(block_str), "variable");
895 snprintf(block_str, sizeof(block_str), "%s",
896 getblksiz(req_status_items[
897 MT_MEDIA_BLOCKSIZE].entry->value_unsigned));
899 dens_str = denstostring(req_status_items[
900 MT_MEDIA_DENSITY].entry->value_unsigned);
901 if (dens_str == NULL)
904 dens_len = strlen(dens_str);
905 field_width = MAX(dens_len, 17);
906 printf("Mode %-*s Blocksize bpi Compression\n"
907 "Current: %-*s %-12s %-7d ",
908 field_width, "Density", field_width, dens_str, block_str,
909 mt_density_bp(req_status_items[
910 MT_MEDIA_DENSITY].entry->value_unsigned, TRUE));
912 if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0)
913 printf("unsupported\n");
914 else if (req_status_items[
915 MT_COMPRESSION_ENABLED].entry->value_signed == 0)
916 printf("disabled\n");
918 printf("enabled (%s)\n",
919 comptostring(req_status_items[
920 MT_COMPRESSION_ALGORITHM].entry->value_unsigned));
923 dsreg = req_status_items[MT_DSREG].entry->value_signed;
924 if (dsreg != MTIO_DSREG_NIL) {
925 const char sfmt[] = "Current Driver State: %s.\n";
926 printf("---------------------------------\n");
927 const char *state_str;
929 state_str = get_driver_state_str(dsreg);
930 if (state_str == NULL) {
932 (void) sprintf(foo, "Unknown state 0x%x", dsreg);
935 printf(sfmt, state_str);
938 resid = req_status_items[MT_RESID].entry->value_signed;
939 calculated_fileno = req_status_items[
940 MT_CALCULATED_FILENO].entry->value_signed;
941 calculated_rel_blkno = req_status_items[
942 MT_CALCULATED_REL_BLKNO].entry->value_signed;
943 rep_fileno = req_status_items[
944 MT_REPORTED_FILENO].entry->value_signed;
945 rep_blkno = req_status_items[
946 MT_REPORTED_BLKNO].entry->value_signed;
947 bop = req_status_items[MT_BOP].entry->value_signed;
948 eop = req_status_items[MT_EOP].entry->value_signed;
949 bpew = req_status_items[MT_BPEW].entry->value_signed;
950 partition = req_status_items[MT_PARTITION].entry->value_signed;
952 printf("---------------------------------\n");
953 printf("Partition: %3jd Calc File Number: %3jd "
954 " Calc Record Number: %jd\n"
955 "Residual: %3jd Reported File Number: %3jd "
956 "Reported Record Number: %jd\n", partition, calculated_fileno,
957 calculated_rel_blkno, resid, rep_fileno, rep_blkno);
960 if (bop > 0 || eop > 0 || bpew > 0) {
984 printf("---------------------------------\n");
985 printf("Tape I/O parameters:\n");
986 for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) {
987 printf(" %s (%s): %ju bytes\n",
988 req_status_items[i].entry->desc,
989 req_status_items[i].name,
990 req_status_items[i].entry->value_unsigned);
998 mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape)
1000 struct mt_status_data status_data;
1002 struct mt_status_entry *entry;
1006 unsigned long ioctl_cmd;
1009 case MT_CMD_PROTECT:
1011 ioctl_cmd = MTIOCPARAMGET;
1014 ioctl_cmd = MTIOCEXTGET;
1018 retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str);
1020 err(1, "Couldn't get mt XML string");
1022 retval = mt_get_status(xml_str, &status_data);
1023 if (retval != XML_STATUS_OK) {
1024 warn("Couldn't get mt status for %s", tape);
1029 * This gets set if there are memory allocation or other errors in
1030 * our parsing of the XML.
1032 if (status_data.error != 0) {
1033 warnx("%s", status_data.error_str);
1038 STAILQ_FOREACH(entry, &status_data.entries, links)
1039 mt_status_tree_print(entry, 0, NULL);
1044 retval = nstatus_print(argc, argv, xml_str, &status_data);
1047 retval = mt_param(argc, argv, mtfd, xml_str, &status_data);
1049 case MT_CMD_PROTECT:
1050 retval = mt_protect(argc, argv, mtfd, &status_data);
1052 case MT_CMD_GETDENSITY:
1053 retval = mt_getdensity(argc, argv, xml_str, &status_data);
1058 if (xml_str != NULL)
1061 mt_status_free(&status_data);
1067 mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name,
1070 struct mt_status_entry *entry;
1071 struct mtparamset param_set;
1073 entry = mt_status_entry_find(status_data,
1074 __DECONST(char *, "mtparamget"));
1076 errx(1, "Cannot find parameter root node");
1078 bzero(¶m_set, sizeof(param_set));
1079 entry = mt_entry_find(entry, param_name);
1081 errx(1, "Unknown parameter name \"%s\"", param_name);
1083 strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name));
1085 switch (entry->var_type) {
1087 param_set.value.value_signed = strtoll(param_value, NULL, 0);
1088 param_set.value_type = MT_PARAM_SET_SIGNED;
1089 param_set.value_len = entry->size;
1092 param_set.value.value_unsigned = strtoull(param_value, NULL, 0);
1093 param_set.value_type = MT_PARAM_SET_UNSIGNED;
1094 param_set.value_len = entry->size;
1096 case MT_TYPE_STRING: {
1099 param_len = strlen(param_value) + 1;
1100 if (param_len > sizeof(param_set.value.value_fixed_str)) {
1101 param_set.value_type = MT_PARAM_SET_VAR_STR;
1102 param_set.value.value_var_str = param_value;
1104 param_set.value_type = MT_PARAM_SET_FIXED_STR;
1105 strlcpy(param_set.value.value_fixed_str, param_value,
1106 sizeof(param_set.value.value_fixed_str));
1108 param_set.value_len = param_len;
1112 errx(1, "Unknown parameter type %d for %s", entry->var_type,
1117 if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1)
1118 err(1, "MTIOCPARAMSET");
1120 if (param_set.status != MT_PARAM_STATUS_OK)
1121 errx(1, "Failed to set %s: %s", param_name,
1122 param_set.error_str);
1136 static struct mt_protect_info {
1138 struct mt_status_entry *entry;
1140 } mt_protect_list[] = {
1141 { "lbp_r", NULL, 0 },
1142 { "lbp_w", NULL, 0 },
1143 { "rbdp", NULL, 0 },
1144 { "pi_length", NULL, 0 },
1145 { "prot_method", NULL, 0 }
1148 #define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0]))
1150 #define MT_PROT_NAME "protection"
1153 mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data)
1156 int do_enable = 0, do_disable = 0, do_list = 0;
1157 int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0;
1158 int prot_method_set = 0, pi_length_set = 0;
1160 uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0;
1161 uint32_t prot_method = 0, pi_length = 0;
1162 struct mt_status_entry *prot_entry, *supported_entry;
1163 struct mt_status_entry *entry;
1164 struct mtparamset params[MT_NUM_PROTECT_PARAMS];
1165 struct mtsetlist param_list;
1169 while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) {
1173 rbdp = strtoul(optarg, NULL, 0);
1174 if ((rbdp != 0) && (rbdp != 1))
1175 errx(1, "valid values for -b are 0 and 1");
1188 pi_length = strtoul(optarg, NULL, 0);
1189 if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK)
1190 errx(1, "PI length %u > maximum %u",
1191 pi_length, SA_CTRL_DP_PI_LENGTH_MASK);
1194 prot_method_set = 1;
1195 prot_method = strtoul(optarg, NULL, 0);
1196 if (prot_method > SA_CTRL_DP_METHOD_MAX)
1197 errx(1, "Method %u > maximum %u",
1198 prot_method, SA_CTRL_DP_METHOD_MAX);
1202 lbp_r = strtoul(optarg, NULL, 0);
1203 if ((lbp_r != 0) && (lbp_r != 1))
1204 errx(1, "valid values for -r are 0 and 1");
1211 lbp_w = strtoul(optarg, NULL, 0);
1212 if ((lbp_w != 0) && (lbp_r != 1))
1213 errx(1, "valid values for -r are 0 and 1");
1220 if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set +
1221 prot_method_set + lbp_r_set + lbp_w_set) == 0)
1222 errx(1, "Need an argument for protect");
1224 if ((do_disable + do_enable + do_list) != 1)
1225 errx(1, "You must specify only one of -e, -d or -l");
1228 retval = mt_protect_print(status_data, verbose);
1231 if (do_enable != 0) {
1233 * Enable protection, but allow the user to override
1234 * settings if he doesn't want everything turned on.
1243 * If the user doesn't override it, we default to enabling
1244 * Reed-Solomon checkums.
1246 if (prot_method_set == 0)
1247 prot_method = SA_CTRL_DP_REED_SOLOMON;
1248 if (pi_length_set == 0)
1249 pi_length = SA_CTRL_DP_RS_LENGTH;
1250 } else if (do_disable != 0) {
1252 * If the user wants to disable protection, we ignore any
1253 * other parameters he has set. Everything gets set to 0.
1255 rbdp = lbp_w = lbp_r = 0;
1256 prot_method = pi_length = 0;
1259 prot_entry = mt_status_entry_find(status_data,
1260 __DECONST(char *, MT_PROT_NAME));
1261 if (prot_entry == NULL)
1262 errx(1, "Unable to find protection information status");
1264 supported_entry = mt_entry_find(prot_entry,
1265 __DECONST(char *, "protection_supported"));
1266 if (supported_entry == NULL)
1267 errx(1, "Unable to find protection support information");
1269 if (((supported_entry->var_type == MT_TYPE_INT)
1270 && (supported_entry->value_signed == 0))
1271 || ((supported_entry->var_type == MT_TYPE_UINT)
1272 && (supported_entry->value_unsigned == 0)))
1273 errx(1, "This device does not support protection information");
1275 mt_protect_list[MT_PP_LBP_R].value = lbp_r;
1276 mt_protect_list[MT_PP_LBP_W].value = lbp_w;
1277 mt_protect_list[MT_PP_RBDP].value = rbdp;
1278 mt_protect_list[MT_PP_PI_LENGTH].value = pi_length;
1279 mt_protect_list[MT_PP_PROT_METHOD].value = prot_method;
1281 bzero(¶ms, sizeof(params));
1282 bzero(¶m_list, sizeof(param_list));
1285 * Go through the list and make sure that we have this parameter,
1286 * and that it is still an unsigned integer. If not, we've got a
1289 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1290 entry = mt_entry_find(prot_entry,
1291 __DECONST(char *, mt_protect_list[i].name));
1292 if (entry == NULL) {
1293 errx(1, "Unable to find parameter %s",
1294 mt_protect_list[i].name);
1296 mt_protect_list[i].entry = entry;
1298 if (entry->var_type != MT_TYPE_UINT)
1299 errx(1, "Parameter %s is type %d, not unsigned, "
1300 "cannot proceed", mt_protect_list[i].name,
1302 snprintf(params[i].value_name, sizeof(params[i].value_name),
1303 "%s.%s", MT_PROT_NAME, mt_protect_list[i].name);
1304 /* XXX KDM unify types here */
1305 params[i].value_type = MT_PARAM_SET_UNSIGNED;
1306 params[i].value_len = sizeof(mt_protect_list[i].value);
1307 params[i].value.value_unsigned = mt_protect_list[i].value;
1310 param_list.num_params = MT_NUM_PROTECT_PARAMS;
1311 param_list.param_len = sizeof(params);
1312 param_list.params = params;
1314 if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1)
1315 err(1, "error issuing MTIOCSETLIST ioctl");
1317 for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) {
1318 if (params[i].status != MT_PARAM_STATUS_OK) {
1319 warnx("%s", params[i].error_str);
1329 mt_param(int argc, char **argv, int mtfd, char *xml_str,
1330 struct mt_status_data *status_data)
1332 int list = 0, do_set = 0, xml_dump = 0;
1333 char *param_name = NULL, *param_value = NULL;
1334 int retval = 0, quiet = 0;
1337 while ((c = getopt(argc, argv, "lp:qs:x")) != -1) {
1343 if (param_name != NULL) {
1344 warnx("Only one parameter name may be "
1349 param_name = strdup(optarg);
1355 if (param_value != NULL) {
1356 warnx("Only one parameter value may be "
1361 param_value = strdup(optarg);
1372 if ((list + do_set + xml_dump) != 1) {
1373 warnx("You must specify only one of -s, -l or -x");
1378 if (xml_dump != 0) {
1379 printf("%s", xml_str);
1385 if (param_name == NULL)
1386 errx(1, "You must specify -p with -s");
1388 retval = mt_set_param(mtfd, status_data, param_name,
1390 } else if (list != 0)
1391 retval = mt_param_list(status_data, param_name, quiet);
1400 mt_print_density_entry(struct mt_status_entry *density_root, int indent)
1402 struct mt_status_entry *entry;
1405 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1406 if (entry->var_type == MT_TYPE_NODE) {
1407 retval = mt_print_density_entry(entry, indent + 2);
1413 if ((strcmp(entry->entry_name, "primary_density_code") == 0)
1414 || (strcmp(entry->entry_name, "secondary_density_code") == 0)
1415 || (strcmp(entry->entry_name, "density_code") == 0)) {
1417 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1418 entry->desc : "", entry->entry_name,
1419 denstostring(entry->value_unsigned));
1420 } else if (strcmp(entry->entry_name, "density_flags") == 0) {
1421 printf("%*sMedium Access: ", indent, "");
1422 if (entry->value_unsigned & MT_DENS_WRITE_OK) {
1423 printf("Read and Write\n");
1425 printf("Read Only\n");
1427 printf("%*sDefault Density: %s\n", indent, "",
1428 (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" :
1430 printf("%*sDuplicate Density: %s\n", indent, "",
1431 (entry->value_unsigned & MT_DENS_DUP) ? "Yes" :
1433 } else if (strcmp(entry->entry_name, "media_width") == 0) {
1434 printf("%*s%s (%s): %.1f mm\n", indent, "",
1435 entry->desc ? entry->desc : "", entry->entry_name,
1436 (double)((double)entry->value_unsigned / 10));
1437 } else if (strcmp(entry->entry_name, "medium_length") == 0) {
1438 printf("%*s%s (%s): %ju m\n", indent, "",
1439 entry->desc ? entry->desc : "", entry->entry_name,
1440 (uintmax_t)entry->value_unsigned);
1441 } else if (strcmp(entry->entry_name, "capacity") == 0) {
1442 printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ?
1443 entry->desc : "", entry->entry_name,
1444 (uintmax_t)entry->value_unsigned);
1446 printf("%*s%s (%s): %s\n", indent, "", entry->desc ?
1447 entry->desc : "", entry->entry_name, entry->value);
1455 mt_print_density_report(struct mt_status_entry *report_root, int indent)
1457 struct mt_status_entry *mt_report, *media_report;
1458 struct mt_status_entry *entry;
1461 mt_report = mt_entry_find(report_root,
1462 __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME));
1463 if (mt_report == NULL)
1466 media_report = mt_entry_find(report_root,
1467 __DECONST(char *, MT_MEDIA_REPORT_NAME));
1468 if (media_report == NULL)
1471 if ((mt_report->value_signed == 0)
1472 && (media_report->value_signed == 0)) {
1473 printf("%*sThis tape drive supports the following "
1474 "media densities:\n", indent, "");
1475 } else if ((mt_report->value_signed == 0)
1476 && (media_report->value_signed != 0)) {
1477 printf("%*sThe tape currently in this drive supports "
1478 "the following media densities:\n", indent, "");
1479 } else if ((mt_report->value_signed != 0)
1480 && (media_report->value_signed == 0)) {
1481 printf("%*sThis tape drive supports the following "
1482 "media types:\n", indent, "");
1484 printf("%*sThis tape currently in this drive supports "
1485 "the following media types:\n", indent, "");
1488 STAILQ_FOREACH(entry, &report_root->child_entries, links) {
1489 struct mt_status_nv *nv;
1491 if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0)
1494 STAILQ_FOREACH(nv, &entry->nv_list, links) {
1495 if (strcmp(nv->name, "num") != 0)
1503 printf("%*sDensity Entry", indent, "");
1505 printf(" %s", nv->value);
1508 retval = mt_print_density_entry(entry, indent + 2);
1517 mt_print_density(struct mt_status_entry *density_root, int indent)
1519 struct mt_status_entry *entry;
1523 * We should have this entry for every tape drive. This particular
1524 * value is reported via the mode page block header, not the
1525 * SCSI REPORT DENSITY SUPPORT command.
1527 entry = mt_entry_find(density_root,
1528 __DECONST(char *, MT_MEDIA_DENSITY_NAME));
1530 errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME);
1532 printf("%*sCurrent density: %s\n", indent, "",
1533 denstostring(entry->value_unsigned));
1536 * It isn't an error if we don't have any density reports. Tape
1537 * drives that don't support the REPORT DENSITY SUPPORT command
1538 * won't have any; they will only have the current density entry
1541 STAILQ_FOREACH(entry, &density_root->child_entries, links) {
1542 if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0)
1545 retval = mt_print_density_report(entry, indent);
1552 mt_getdensity(int argc, char **argv, char *xml_str,
1553 struct mt_status_data *status_data)
1557 struct mt_status_entry *density_root = NULL;
1560 while ((c = getopt(argc, argv, "vx")) != -1) {
1571 if (xml_dump != 0) {
1572 printf("%s", xml_str);
1576 density_root = mt_status_entry_find(status_data,
1577 __DECONST(char *, MT_DENSITY_ROOT_NAME));
1578 if (density_root == NULL)
1579 errx(1, "Cannot find density root node %s",
1580 MT_DENSITY_ROOT_NAME);
1582 retval = mt_print_density(density_root, 0);
1591 "The \"eof\" command has been disabled.\n"
1592 "Use \"weof\" if you really want to write end-of-file marks,\n"
1593 "or \"eom\" if you rather want to skip to the end of "
1594 "recorded medium.\n");