Home | History | Annotate | Line # | Download | only in dkctl
dkctl.c revision 1.9
      1  1.9  thorpej /*	$NetBSD: dkctl.c,v 1.9 2004/09/25 03:31:35 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.1  thorpej  *
      9  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1  thorpej  * modification, are permitted provided that the following conditions
     11  1.1  thorpej  * are met:
     12  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1  thorpej  *    must display the following acknowledgement:
     19  1.1  thorpej  *	This product includes software developed for the NetBSD Project by
     20  1.1  thorpej  *	Wasabi Systems, Inc.
     21  1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  thorpej  *    or promote products derived from this software without specific prior
     23  1.1  thorpej  *    written permission.
     24  1.1  thorpej  *
     25  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  thorpej  */
     37  1.1  thorpej 
     38  1.1  thorpej /*
     39  1.1  thorpej  * dkctl(8) -- a program to manipulate disks.
     40  1.1  thorpej  */
     41  1.7      agc #include <sys/cdefs.h>
     42  1.7      agc 
     43  1.7      agc #ifndef lint
     44  1.9  thorpej __RCSID("$NetBSD: dkctl.c,v 1.9 2004/09/25 03:31:35 thorpej Exp $");
     45  1.7      agc #endif
     46  1.7      agc 
     47  1.1  thorpej 
     48  1.1  thorpej #include <sys/param.h>
     49  1.1  thorpej #include <sys/ioctl.h>
     50  1.1  thorpej #include <sys/dkio.h>
     51  1.3  darrenr #include <sys/disk.h>
     52  1.3  darrenr #include <sys/queue.h>
     53  1.1  thorpej #include <err.h>
     54  1.1  thorpej #include <errno.h>
     55  1.1  thorpej #include <fcntl.h>
     56  1.1  thorpej #include <stdio.h>
     57  1.1  thorpej #include <stdlib.h>
     58  1.1  thorpej #include <string.h>
     59  1.1  thorpej #include <unistd.h>
     60  1.1  thorpej #include <util.h>
     61  1.1  thorpej 
     62  1.2     yamt #define	YES	1
     63  1.2     yamt #define	NO	0
     64  1.2     yamt 
     65  1.2     yamt /* I don't think nl_langinfo is suitable in this case */
     66  1.2     yamt #define	YES_STR	"yes"
     67  1.2     yamt #define	NO_STR	"no"
     68  1.2     yamt #define YESNO_ARG	YES_STR " | " NO_STR
     69  1.2     yamt 
     70  1.3  darrenr #ifndef PRIdaddr
     71  1.3  darrenr #define PRIdaddr PRId64
     72  1.3  darrenr #endif
     73  1.3  darrenr 
     74  1.1  thorpej struct command {
     75  1.1  thorpej 	const char *cmd_name;
     76  1.1  thorpej 	const char *arg_names;
     77  1.1  thorpej 	void (*cmd_func)(int, char *[]);
     78  1.1  thorpej 	int open_flags;
     79  1.1  thorpej };
     80  1.1  thorpej 
     81  1.1  thorpej int	main(int, char *[]);
     82  1.1  thorpej void	usage(void);
     83  1.1  thorpej 
     84  1.1  thorpej int	fd;				/* file descriptor for device */
     85  1.1  thorpej const	char *dvname;			/* device name */
     86  1.1  thorpej char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     87  1.1  thorpej const	char *cmdname;			/* command user issued */
     88  1.1  thorpej const	char *argnames;			/* helpstring; expected arguments */
     89  1.1  thorpej 
     90  1.2     yamt int yesno(const char *);
     91  1.2     yamt 
     92  1.1  thorpej void	disk_getcache(int, char *[]);
     93  1.1  thorpej void	disk_setcache(int, char *[]);
     94  1.1  thorpej void	disk_synccache(int, char *[]);
     95  1.2     yamt void	disk_keeplabel(int, char *[]);
     96  1.3  darrenr void	disk_badsectors(int, char *[]);
     97  1.1  thorpej 
     98  1.9  thorpej void	disk_addwedge(int, char *[]);
     99  1.9  thorpej void	disk_delwedge(int, char *[]);
    100  1.9  thorpej void	disk_getwedgeinfo(int, char *[]);
    101  1.9  thorpej void	disk_listwedges(int, char *[]);
    102  1.9  thorpej 
    103  1.1  thorpej struct command commands[] = {
    104  1.1  thorpej 	{ "getcache",
    105  1.1  thorpej 	  "",
    106  1.1  thorpej 	  disk_getcache,
    107  1.1  thorpej 	  O_RDONLY },
    108  1.1  thorpej 
    109  1.1  thorpej 	{ "setcache",
    110  1.1  thorpej 	  "none | r | w | rw [save]",
    111  1.1  thorpej 	  disk_setcache,
    112  1.1  thorpej 	  O_RDWR },
    113  1.1  thorpej 
    114  1.1  thorpej 	{ "synccache",
    115  1.1  thorpej 	  "[force]",
    116  1.1  thorpej 	  disk_synccache,
    117  1.1  thorpej 	  O_RDWR },
    118  1.1  thorpej 
    119  1.2     yamt 	{ "keeplabel",
    120  1.2     yamt 	  YESNO_ARG,
    121  1.2     yamt 	  disk_keeplabel,
    122  1.2     yamt 	  O_RDWR },
    123  1.2     yamt 
    124  1.3  darrenr 	{ "badsector",
    125  1.3  darrenr 	  "flush | list | retry",
    126  1.3  darrenr 	   disk_badsectors,
    127  1.3  darrenr 	   O_RDWR },
    128  1.3  darrenr 
    129  1.9  thorpej 	{ "addwedge",
    130  1.9  thorpej 	  "name startblk blkcnt ptype",
    131  1.9  thorpej 	  disk_addwedge,
    132  1.9  thorpej 	  O_RDWR },
    133  1.9  thorpej 
    134  1.9  thorpej 	{ "delwedge",
    135  1.9  thorpej 	  "dk",
    136  1.9  thorpej 	  disk_delwedge,
    137  1.9  thorpej 	  O_RDWR },
    138  1.9  thorpej 
    139  1.9  thorpej 	{ "getwedgeinfo",
    140  1.9  thorpej 	  "",
    141  1.9  thorpej 	  disk_getwedgeinfo,
    142  1.9  thorpej 	  O_RDONLY },
    143  1.9  thorpej 
    144  1.9  thorpej 	{ "listwedges",
    145  1.9  thorpej 	  "",
    146  1.9  thorpej 	  disk_listwedges,
    147  1.9  thorpej 	  O_RDONLY },
    148  1.9  thorpej 
    149  1.1  thorpej 	{ NULL,
    150  1.1  thorpej 	  NULL,
    151  1.1  thorpej 	  NULL,
    152  1.1  thorpej 	  0 },
    153  1.1  thorpej };
    154  1.1  thorpej 
    155  1.1  thorpej int
    156  1.1  thorpej main(int argc, char *argv[])
    157  1.1  thorpej {
    158  1.1  thorpej 	int i;
    159  1.1  thorpej 
    160  1.1  thorpej 	/* Must have at least: device command */
    161  1.1  thorpej 	if (argc < 3)
    162  1.1  thorpej 		usage();
    163  1.1  thorpej 
    164  1.1  thorpej 	/* Skip program name, get and skip device name and command. */
    165  1.1  thorpej 	dvname = argv[1];
    166  1.1  thorpej 	cmdname = argv[2];
    167  1.1  thorpej 	argv += 3;
    168  1.1  thorpej 	argc -= 3;
    169  1.1  thorpej 
    170  1.1  thorpej 	/* Look up and call the command. */
    171  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    172  1.1  thorpej 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    173  1.1  thorpej 			break;
    174  1.1  thorpej 	if (commands[i].cmd_name == NULL)
    175  1.1  thorpej 		errx(1, "unknown command: %s", cmdname);
    176  1.1  thorpej 
    177  1.1  thorpej 	argnames = commands[i].arg_names;
    178  1.1  thorpej 
    179  1.1  thorpej 	/* Open the device. */
    180  1.1  thorpej 	fd = opendisk(dvname, commands[i].open_flags, dvname_store,
    181  1.1  thorpej 	    sizeof(dvname_store), 0);
    182  1.1  thorpej 	if (fd == -1)
    183  1.1  thorpej 		err(1, "%s", dvname);
    184  1.1  thorpej 
    185  1.1  thorpej 	dvname = dvname_store;
    186  1.1  thorpej 
    187  1.1  thorpej 	(*commands[i].cmd_func)(argc, argv);
    188  1.1  thorpej 	exit(0);
    189  1.1  thorpej }
    190  1.1  thorpej 
    191  1.1  thorpej void
    192  1.1  thorpej usage()
    193  1.1  thorpej {
    194  1.1  thorpej 	int i;
    195  1.1  thorpej 
    196  1.8     jmmv 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    197  1.1  thorpej 	    getprogname());
    198  1.1  thorpej 
    199  1.1  thorpej 	fprintf(stderr, "   Available commands:\n");
    200  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    201  1.1  thorpej 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    202  1.1  thorpej 		    commands[i].arg_names);
    203  1.1  thorpej 
    204  1.1  thorpej 	exit(1);
    205  1.1  thorpej }
    206  1.1  thorpej 
    207  1.1  thorpej void
    208  1.1  thorpej disk_getcache(int argc, char *argv[])
    209  1.1  thorpej {
    210  1.1  thorpej 	int bits;
    211  1.1  thorpej 
    212  1.1  thorpej 	if (ioctl(fd, DIOCGCACHE, &bits) == -1)
    213  1.1  thorpej 		err(1, "%s: getcache", dvname);
    214  1.1  thorpej 
    215  1.1  thorpej 	if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
    216  1.1  thorpej 		printf("%s: No caches enabled\n", dvname);
    217  1.1  thorpej 	else {
    218  1.1  thorpej 		if (bits & DKCACHE_READ)
    219  1.1  thorpej 			printf("%s: read cache enabled\n", dvname);
    220  1.1  thorpej 		if (bits & DKCACHE_WRITE)
    221  1.1  thorpej 			printf("%s: write-back cache enabled\n", dvname);
    222  1.1  thorpej 	}
    223  1.1  thorpej 
    224  1.1  thorpej 	printf("%s: read cache enable is %schangeable\n", dvname,
    225  1.1  thorpej 	    (bits & DKCACHE_RCHANGE) ? "" : "not ");
    226  1.1  thorpej 	printf("%s: write cache enable is %schangeable\n", dvname,
    227  1.1  thorpej 	    (bits & DKCACHE_WCHANGE) ? "" : "not ");
    228  1.1  thorpej 
    229  1.1  thorpej 	printf("%s: cache parameters are %ssavable\n", dvname,
    230  1.1  thorpej 	    (bits & DKCACHE_SAVE) ? "" : "not ");
    231  1.1  thorpej }
    232  1.1  thorpej 
    233  1.1  thorpej void
    234  1.1  thorpej disk_setcache(int argc, char *argv[])
    235  1.1  thorpej {
    236  1.1  thorpej 	int bits;
    237  1.1  thorpej 
    238  1.1  thorpej 	if (argc > 2 || argc == 0)
    239  1.1  thorpej 		usage();
    240  1.1  thorpej 
    241  1.1  thorpej 	if (strcmp(argv[0], "none") == 0)
    242  1.1  thorpej 		bits = 0;
    243  1.1  thorpej 	else if (strcmp(argv[0], "r") == 0)
    244  1.1  thorpej 		bits = DKCACHE_READ;
    245  1.1  thorpej 	else if (strcmp(argv[0], "w") == 0)
    246  1.1  thorpej 		bits = DKCACHE_WRITE;
    247  1.1  thorpej 	else if (strcmp(argv[0], "rw") == 0)
    248  1.1  thorpej 		bits = DKCACHE_READ|DKCACHE_WRITE;
    249  1.1  thorpej 	else
    250  1.1  thorpej 		usage();
    251  1.1  thorpej 
    252  1.1  thorpej 	if (argc == 2) {
    253  1.1  thorpej 		if (strcmp(argv[1], "save") == 0)
    254  1.1  thorpej 			bits |= DKCACHE_SAVE;
    255  1.1  thorpej 		else
    256  1.1  thorpej 			usage();
    257  1.1  thorpej 	}
    258  1.1  thorpej 
    259  1.1  thorpej 	if (ioctl(fd, DIOCSCACHE, &bits) == -1)
    260  1.1  thorpej 		err(1, "%s: setcache", dvname);
    261  1.1  thorpej }
    262  1.1  thorpej 
    263  1.1  thorpej void
    264  1.1  thorpej disk_synccache(int argc, char *argv[])
    265  1.1  thorpej {
    266  1.1  thorpej 	int force;
    267  1.1  thorpej 
    268  1.1  thorpej 	switch (argc) {
    269  1.1  thorpej 	case 0:
    270  1.1  thorpej 		force = 0;
    271  1.1  thorpej 		break;
    272  1.1  thorpej 
    273  1.1  thorpej 	case 1:
    274  1.1  thorpej 		if (strcmp(argv[0], "force") == 0)
    275  1.1  thorpej 			force = 1;
    276  1.1  thorpej 		else
    277  1.1  thorpej 			usage();
    278  1.1  thorpej 		break;
    279  1.1  thorpej 
    280  1.1  thorpej 	default:
    281  1.1  thorpej 		usage();
    282  1.1  thorpej 	}
    283  1.1  thorpej 
    284  1.1  thorpej 	if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
    285  1.1  thorpej 		err(1, "%s: sync cache", dvname);
    286  1.2     yamt }
    287  1.2     yamt 
    288  1.2     yamt void
    289  1.2     yamt disk_keeplabel(int argc, char *argv[])
    290  1.2     yamt {
    291  1.2     yamt 	int keep;
    292  1.2     yamt 	int yn;
    293  1.2     yamt 
    294  1.2     yamt 	if (argc != 1)
    295  1.2     yamt 		usage();
    296  1.2     yamt 
    297  1.2     yamt 	yn = yesno(argv[0]);
    298  1.2     yamt 	if (yn < 0)
    299  1.2     yamt 		usage();
    300  1.2     yamt 
    301  1.2     yamt 	keep = yn == YES;
    302  1.2     yamt 
    303  1.2     yamt 	if (ioctl(fd, DIOCKLABEL, &keep) == -1)
    304  1.2     yamt 		err(1, "%s: keep label", dvname);
    305  1.3  darrenr }
    306  1.3  darrenr 
    307  1.3  darrenr 
    308  1.3  darrenr void
    309  1.3  darrenr disk_badsectors(int argc, char *argv[])
    310  1.3  darrenr {
    311  1.3  darrenr 	struct disk_badsectors *dbs, *dbs2, buffer[200];
    312  1.3  darrenr 	SLIST_HEAD(, disk_badsectors) dbstop;
    313  1.3  darrenr 	struct disk_badsecinfo dbsi;
    314  1.3  darrenr 	daddr_t blk, totbad, bad;
    315  1.5      dsl 	u_int32_t count;
    316  1.3  darrenr 	struct stat sb;
    317  1.3  darrenr 	u_char *block;
    318  1.6   martin 	time_t tm;
    319  1.3  darrenr 
    320  1.3  darrenr 	if (argc != 1)
    321  1.3  darrenr 		usage();
    322  1.3  darrenr 
    323  1.3  darrenr 	if (strcmp(argv[0], "list") == 0) {
    324  1.3  darrenr 		/*
    325  1.3  darrenr 		 * Copy the list of kernel bad sectors out in chunks that fit
    326  1.3  darrenr 		 * into buffer[].  Updating dbsi_skip means we don't sit here
    327  1.3  darrenr 		 * forever only getting the first chunk that fit in buffer[].
    328  1.3  darrenr 		 */
    329  1.3  darrenr 		dbsi.dbsi_buffer = (caddr_t)buffer;
    330  1.3  darrenr 		dbsi.dbsi_bufsize = sizeof(buffer);
    331  1.3  darrenr 		dbsi.dbsi_skip = 0;
    332  1.3  darrenr 		dbsi.dbsi_copied = 0;
    333  1.3  darrenr 		dbsi.dbsi_left = 0;
    334  1.3  darrenr 
    335  1.3  darrenr 		do {
    336  1.3  darrenr 			if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
    337  1.3  darrenr 				err(1, "%s: badsectors list", dvname);
    338  1.3  darrenr 
    339  1.3  darrenr 			dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
    340  1.3  darrenr 			for (count = dbsi.dbsi_copied; count > 0; count--) {
    341  1.6   martin 				tm = dbs->dbs_failedat.tv_sec;
    342  1.5      dsl 				printf("%s: blocks %" PRIdaddr " - %" PRIdaddr " failed at %s",
    343  1.3  darrenr 					dvname, dbs->dbs_min, dbs->dbs_max,
    344  1.6   martin 					ctime(&tm));
    345  1.4  darrenr 				dbs++;
    346  1.3  darrenr 			}
    347  1.3  darrenr 			dbsi.dbsi_skip += dbsi.dbsi_copied;
    348  1.3  darrenr 		} while (dbsi.dbsi_left != 0);
    349  1.3  darrenr 
    350  1.3  darrenr 	} else if (strcmp(argv[0], "flush") == 0) {
    351  1.3  darrenr 		if (ioctl(fd, DIOCBSFLUSH) == -1)
    352  1.3  darrenr 			err(1, "%s: badsectors flush", dvname);
    353  1.3  darrenr 
    354  1.3  darrenr 	} else if (strcmp(argv[0], "retry") == 0) {
    355  1.3  darrenr 		/*
    356  1.3  darrenr 		 * Enforce use of raw device here because the block device
    357  1.3  darrenr 		 * causes access to blocks to be clustered in a larger group,
    358  1.3  darrenr 		 * making it impossible to determine which individual sectors
    359  1.3  darrenr 		 * are the cause of a problem.
    360  1.3  darrenr 		 */
    361  1.3  darrenr 		if (fstat(fd, &sb) == -1)
    362  1.3  darrenr 			err(1, "fstat");
    363  1.3  darrenr 
    364  1.3  darrenr 		if (!S_ISCHR(sb.st_mode)) {
    365  1.3  darrenr 			fprintf(stderr, "'badsector retry' must be used %s\n",
    366  1.3  darrenr 				"with character device");
    367  1.3  darrenr 			exit(1);
    368  1.3  darrenr 		}
    369  1.3  darrenr 
    370  1.3  darrenr 		SLIST_INIT(&dbstop);
    371  1.3  darrenr 
    372  1.3  darrenr 		/*
    373  1.3  darrenr 		 * Build up a copy of the in-kernel list in a number of stages.
    374  1.3  darrenr 		 * That the list we build up here is in the reverse order to
    375  1.3  darrenr 		 * the kernel's is of no concern.
    376  1.3  darrenr 		 */
    377  1.3  darrenr 		dbsi.dbsi_buffer = (caddr_t)buffer;
    378  1.3  darrenr 		dbsi.dbsi_bufsize = sizeof(buffer);
    379  1.3  darrenr 		dbsi.dbsi_skip = 0;
    380  1.3  darrenr 		dbsi.dbsi_copied = 0;
    381  1.3  darrenr 		dbsi.dbsi_left = 0;
    382  1.3  darrenr 
    383  1.3  darrenr 		do {
    384  1.3  darrenr 			if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
    385  1.3  darrenr 				err(1, "%s: badsectors list", dvname);
    386  1.3  darrenr 
    387  1.3  darrenr 			dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
    388  1.3  darrenr 			for (count = dbsi.dbsi_copied; count > 0; count--) {
    389  1.3  darrenr 				dbs2 = malloc(sizeof(*dbs2));
    390  1.3  darrenr 				*dbs2 = *dbs;
    391  1.3  darrenr 				SLIST_INSERT_HEAD(&dbstop, dbs2, dbs_next);
    392  1.4  darrenr 				dbs++;
    393  1.3  darrenr 			}
    394  1.3  darrenr 			dbsi.dbsi_skip += dbsi.dbsi_copied;
    395  1.3  darrenr 		} while (dbsi.dbsi_left != 0);
    396  1.3  darrenr 
    397  1.3  darrenr 		/*
    398  1.3  darrenr 		 * Just calculate and print out something that will hopefully
    399  1.3  darrenr 		 * provide some useful information about what's going to take
    400  1.3  darrenr 		 * place next (if anything.)
    401  1.3  darrenr 		 */
    402  1.3  darrenr 		bad = 0;
    403  1.3  darrenr 		totbad = 0;
    404  1.3  darrenr 		block = calloc(1, DEV_BSIZE);
    405  1.3  darrenr 		SLIST_FOREACH(dbs, &dbstop, dbs_next) {
    406  1.3  darrenr 			bad++;
    407  1.3  darrenr 			totbad += dbs->dbs_max - dbs->dbs_min + 1;
    408  1.3  darrenr 		}
    409  1.3  darrenr 
    410  1.3  darrenr 		printf("%s: bad sector clusters %"PRIdaddr
    411  1.3  darrenr 		    " total sectors %"PRIdaddr"\n", dvname, bad, totbad);
    412  1.3  darrenr 
    413  1.3  darrenr 		/*
    414  1.3  darrenr 		 * Clear out the kernel's list of bad sectors, ready for us
    415  1.3  darrenr 		 * to test all those it thought were bad.
    416  1.3  darrenr 		 */
    417  1.3  darrenr 		if (ioctl(fd, DIOCBSFLUSH) == -1)
    418  1.3  darrenr 			err(1, "%s: badsectors flush", dvname);
    419  1.3  darrenr 
    420  1.3  darrenr 		printf("%s: bad sectors flushed\n", dvname);
    421  1.3  darrenr 
    422  1.3  darrenr 		/*
    423  1.3  darrenr 		 * For each entry we obtained from the kernel, retry each
    424  1.3  darrenr 		 * individual sector recorded as bad by seeking to it and
    425  1.3  darrenr 		 * attempting to read it in.  Print out a line item for each
    426  1.3  darrenr 		 * bad block we verify.
    427  1.3  darrenr 		 *
    428  1.3  darrenr 		 * PRIdaddr is used here because the type of dbs_max is daddr_t
    429  1.3  darrenr 		 * and that may be either a 32bit or 64bit number(!)
    430  1.3  darrenr 		 */
    431  1.3  darrenr 		SLIST_FOREACH(dbs, &dbstop, dbs_next) {
    432  1.3  darrenr 			printf("%s: Retrying %"PRIdaddr" - %"
    433  1.3  darrenr 			    PRIdaddr"\n", dvname, dbs->dbs_min, dbs->dbs_max);
    434  1.3  darrenr 
    435  1.3  darrenr 			for (blk = dbs->dbs_min; blk <= dbs->dbs_max; blk++) {
    436  1.3  darrenr 				if (lseek(fd, (off_t)blk * DEV_BSIZE,
    437  1.3  darrenr 				    SEEK_SET) == -1) {
    438  1.5      dsl 					warn("%s: lseek block %" PRIdaddr "",
    439  1.5      dsl 					    dvname, blk);
    440  1.3  darrenr 					continue;
    441  1.3  darrenr 				}
    442  1.3  darrenr 				printf("%s: block %"PRIdaddr" - ", dvname, blk);
    443  1.3  darrenr 				if (read(fd, block, DEV_BSIZE) != DEV_BSIZE)
    444  1.3  darrenr 					printf("failed\n");
    445  1.3  darrenr 				else
    446  1.3  darrenr 					printf("ok\n");
    447  1.3  darrenr 				fflush(stdout);
    448  1.3  darrenr 			}
    449  1.3  darrenr 		}
    450  1.3  darrenr 	}
    451  1.2     yamt }
    452  1.2     yamt 
    453  1.9  thorpej void
    454  1.9  thorpej disk_addwedge(int argc, char *argv[])
    455  1.9  thorpej {
    456  1.9  thorpej 	struct dkwedge_info dkw;
    457  1.9  thorpej 	char *cp;
    458  1.9  thorpej 	daddr_t start;
    459  1.9  thorpej 	uint64_t size;
    460  1.9  thorpej 
    461  1.9  thorpej 	if (argc != 4)
    462  1.9  thorpej 		usage();
    463  1.9  thorpej 
    464  1.9  thorpej 	/* XXX Unicode. */
    465  1.9  thorpej 	if (strlen(argv[0]) > sizeof(dkw.dkw_wname) - 1)
    466  1.9  thorpej 		errx(1, "Wedge name too long; max %zd characters",
    467  1.9  thorpej 		    sizeof(dkw.dkw_wname) - 1);
    468  1.9  thorpej 	strcpy(dkw.dkw_wname, argv[0]);
    469  1.9  thorpej 
    470  1.9  thorpej 	if (strlen(argv[3]) > sizeof(dkw.dkw_ptype) - 1)
    471  1.9  thorpej 		errx(1, "Wedge partition type too long; max %zd characters",
    472  1.9  thorpej 		    sizeof(dkw.dkw_ptype) - 1);
    473  1.9  thorpej 	strcpy(dkw.dkw_ptype, argv[3]);
    474  1.9  thorpej 
    475  1.9  thorpej 	errno = 0;
    476  1.9  thorpej 	start = strtoll(argv[1], &cp, 0);
    477  1.9  thorpej 	if (*cp != '\0')
    478  1.9  thorpej 		errx(1, "Invalid start block: %s", argv[1]);
    479  1.9  thorpej 	if (errno == ERANGE && (start == LLONG_MAX ||
    480  1.9  thorpej 				start == LLONG_MIN))
    481  1.9  thorpej 		errx(1, "Start block out of range.");
    482  1.9  thorpej 	if (start < 0)
    483  1.9  thorpej 		errx(1, "Start block must be >= 0.");
    484  1.9  thorpej 
    485  1.9  thorpej 	errno = 0;
    486  1.9  thorpej 	size = strtoull(argv[2], &cp, 0);
    487  1.9  thorpej 	if (*cp != '\0')
    488  1.9  thorpej 		errx(1, "Invalid block count: %s", argv[2]);
    489  1.9  thorpej 	if (errno == ERANGE && (size == ULLONG_MAX))
    490  1.9  thorpej 		errx(1, "Block count out of range.");
    491  1.9  thorpej 
    492  1.9  thorpej 	dkw.dkw_offset = start;
    493  1.9  thorpej 	dkw.dkw_size = size;
    494  1.9  thorpej 
    495  1.9  thorpej 	if (ioctl(fd, DIOCAWEDGE, &dkw) == -1)
    496  1.9  thorpej 		err(1, "%s: addwedge", dvname);
    497  1.9  thorpej }
    498  1.9  thorpej 
    499  1.9  thorpej void
    500  1.9  thorpej disk_delwedge(int argc, char *argv[])
    501  1.9  thorpej {
    502  1.9  thorpej 	struct dkwedge_info dkw;
    503  1.9  thorpej 
    504  1.9  thorpej 	if (argc != 1)
    505  1.9  thorpej 		usage();
    506  1.9  thorpej 
    507  1.9  thorpej 	if (strlen(argv[0]) > sizeof(dkw.dkw_devname) - 1)
    508  1.9  thorpej 		errx(1, "Wedge dk name too long; max %zd characters",
    509  1.9  thorpej 		    sizeof(dkw.dkw_devname) - 1);
    510  1.9  thorpej 	strcpy(dkw.dkw_devname, argv[0]);
    511  1.9  thorpej 
    512  1.9  thorpej 	if (ioctl(fd, DIOCDWEDGE, &dkw) == -1)
    513  1.9  thorpej 		err(1, "%s: delwedge", dvname);
    514  1.9  thorpej }
    515  1.9  thorpej 
    516  1.9  thorpej void
    517  1.9  thorpej disk_getwedgeinfo(int argc, char *argv[])
    518  1.9  thorpej {
    519  1.9  thorpej 	struct dkwedge_info dkw;
    520  1.9  thorpej 
    521  1.9  thorpej 	if (argc != 0)
    522  1.9  thorpej 		usage();
    523  1.9  thorpej 
    524  1.9  thorpej 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1)
    525  1.9  thorpej 		err(1, "%s: getwedgeinfo", dvname);
    526  1.9  thorpej 
    527  1.9  thorpej 	printf("%s at %s: %s\n", dkw.dkw_devname, dkw.dkw_parent,
    528  1.9  thorpej 	    dkw.dkw_wname);	/* XXX Unicode */
    529  1.9  thorpej 	printf("%s: %llu blocks at %lld, type: %s\n",
    530  1.9  thorpej 	    dkw.dkw_devname, dkw.dkw_size, dkw.dkw_offset, dkw.dkw_ptype);
    531  1.9  thorpej }
    532  1.9  thorpej 
    533  1.9  thorpej void
    534  1.9  thorpej disk_listwedges(int argc, char *argv[])
    535  1.9  thorpej {
    536  1.9  thorpej 	struct dkwedge_info *dkw;
    537  1.9  thorpej 	struct dkwedge_list dkwl;
    538  1.9  thorpej 	size_t bufsize;
    539  1.9  thorpej 	u_int i;
    540  1.9  thorpej 
    541  1.9  thorpej 	if (argc != 0)
    542  1.9  thorpej 		usage();
    543  1.9  thorpej 
    544  1.9  thorpej 	dkw = NULL;
    545  1.9  thorpej 	dkwl.dkwl_buf = dkw;
    546  1.9  thorpej 	dkwl.dkwl_bufsize = 0;
    547  1.9  thorpej 
    548  1.9  thorpej 	for (;;) {
    549  1.9  thorpej 		if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
    550  1.9  thorpej 			err(1, "%s: listwedges", dvname);
    551  1.9  thorpej 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
    552  1.9  thorpej 			break;
    553  1.9  thorpej 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
    554  1.9  thorpej 		if (dkwl.dkwl_bufsize < bufsize) {
    555  1.9  thorpej 			dkw = realloc(dkwl.dkwl_buf, bufsize);
    556  1.9  thorpej 			if (dkw == NULL)
    557  1.9  thorpej 				errx(1, "%s: listwedges: unable to "
    558  1.9  thorpej 				    "allocate wedge info buffer", dvname);
    559  1.9  thorpej 			dkwl.dkwl_buf = dkw;
    560  1.9  thorpej 			dkwl.dkwl_bufsize = bufsize;
    561  1.9  thorpej 		}
    562  1.9  thorpej 	}
    563  1.9  thorpej 
    564  1.9  thorpej 	if (dkwl.dkwl_nwedges == 0) {
    565  1.9  thorpej 		printf("%s: no wedges configured\n", dvname);
    566  1.9  thorpej 		return;
    567  1.9  thorpej 	}
    568  1.9  thorpej 
    569  1.9  thorpej 	printf("%s: %u wedge%s:\n", dvname, dkwl.dkwl_nwedges,
    570  1.9  thorpej 	    dkwl.dkwl_nwedges == 1 ? "" : "s");
    571  1.9  thorpej 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
    572  1.9  thorpej 		printf("%s: %s, %llu blocks at %lld, type: %s\n",
    573  1.9  thorpej 		    dkw[i].dkw_devname,
    574  1.9  thorpej 		    dkw[i].dkw_wname,	/* XXX Unicode */
    575  1.9  thorpej 		    dkw[i].dkw_size, dkw[i].dkw_offset, dkw[i].dkw_ptype);
    576  1.9  thorpej 	}
    577  1.9  thorpej }
    578  1.9  thorpej 
    579  1.2     yamt /*
    580  1.2     yamt  * return YES, NO or -1.
    581  1.2     yamt  */
    582  1.2     yamt int
    583  1.2     yamt yesno(const char *p)
    584  1.2     yamt {
    585  1.2     yamt 
    586  1.2     yamt 	if (!strcmp(p, YES_STR))
    587  1.2     yamt 		return YES;
    588  1.2     yamt 	if (!strcmp(p, NO_STR))
    589  1.2     yamt 		return NO;
    590  1.2     yamt 	return -1;
    591  1.1  thorpej }
    592