Home | History | Annotate | Line # | Download | only in dkctl
dkctl.c revision 1.2
      1  1.2     yamt /*	$NetBSD: dkctl.c,v 1.2 2002/07/01 18:49:57 yamt 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.1  thorpej 
     42  1.1  thorpej #include <sys/param.h>
     43  1.1  thorpej #include <sys/ioctl.h>
     44  1.1  thorpej #include <sys/dkio.h>
     45  1.1  thorpej #include <err.h>
     46  1.1  thorpej #include <errno.h>
     47  1.1  thorpej #include <fcntl.h>
     48  1.1  thorpej #include <stdio.h>
     49  1.1  thorpej #include <stdlib.h>
     50  1.1  thorpej #include <string.h>
     51  1.1  thorpej #include <unistd.h>
     52  1.1  thorpej #include <util.h>
     53  1.1  thorpej 
     54  1.2     yamt #define	YES	1
     55  1.2     yamt #define	NO	0
     56  1.2     yamt 
     57  1.2     yamt /* I don't think nl_langinfo is suitable in this case */
     58  1.2     yamt #define	YES_STR	"yes"
     59  1.2     yamt #define	NO_STR	"no"
     60  1.2     yamt #define YESNO_ARG	YES_STR " | " NO_STR
     61  1.2     yamt 
     62  1.1  thorpej struct command {
     63  1.1  thorpej 	const char *cmd_name;
     64  1.1  thorpej 	const char *arg_names;
     65  1.1  thorpej 	void (*cmd_func)(int, char *[]);
     66  1.1  thorpej 	int open_flags;
     67  1.1  thorpej };
     68  1.1  thorpej 
     69  1.1  thorpej int	main(int, char *[]);
     70  1.1  thorpej void	usage(void);
     71  1.1  thorpej 
     72  1.1  thorpej int	fd;				/* file descriptor for device */
     73  1.1  thorpej const	char *dvname;			/* device name */
     74  1.1  thorpej char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     75  1.1  thorpej const	char *cmdname;			/* command user issued */
     76  1.1  thorpej const	char *argnames;			/* helpstring; expected arguments */
     77  1.1  thorpej 
     78  1.2     yamt int yesno(const char *);
     79  1.2     yamt 
     80  1.1  thorpej void	disk_getcache(int, char *[]);
     81  1.1  thorpej void	disk_setcache(int, char *[]);
     82  1.1  thorpej void	disk_synccache(int, char *[]);
     83  1.2     yamt void	disk_keeplabel(int, char *[]);
     84  1.1  thorpej 
     85  1.1  thorpej struct command commands[] = {
     86  1.1  thorpej 	{ "getcache",
     87  1.1  thorpej 	  "",
     88  1.1  thorpej 	  disk_getcache,
     89  1.1  thorpej 	  O_RDONLY },
     90  1.1  thorpej 
     91  1.1  thorpej 	{ "setcache",
     92  1.1  thorpej 	  "none | r | w | rw [save]",
     93  1.1  thorpej 	  disk_setcache,
     94  1.1  thorpej 	  O_RDWR },
     95  1.1  thorpej 
     96  1.1  thorpej 	{ "synccache",
     97  1.1  thorpej 	  "[force]",
     98  1.1  thorpej 	  disk_synccache,
     99  1.1  thorpej 	  O_RDWR },
    100  1.1  thorpej 
    101  1.2     yamt 	{ "keeplabel",
    102  1.2     yamt 	  YESNO_ARG,
    103  1.2     yamt 	  disk_keeplabel,
    104  1.2     yamt 	  O_RDWR },
    105  1.2     yamt 
    106  1.1  thorpej 	{ NULL,
    107  1.1  thorpej 	  NULL,
    108  1.1  thorpej 	  NULL,
    109  1.1  thorpej 	  0 },
    110  1.1  thorpej };
    111  1.1  thorpej 
    112  1.1  thorpej int
    113  1.1  thorpej main(int argc, char *argv[])
    114  1.1  thorpej {
    115  1.1  thorpej 	int i;
    116  1.1  thorpej 
    117  1.1  thorpej 	/* Must have at least: device command */
    118  1.1  thorpej 	if (argc < 3)
    119  1.1  thorpej 		usage();
    120  1.1  thorpej 
    121  1.1  thorpej 	/* Skip program name, get and skip device name and command. */
    122  1.1  thorpej 	dvname = argv[1];
    123  1.1  thorpej 	cmdname = argv[2];
    124  1.1  thorpej 	argv += 3;
    125  1.1  thorpej 	argc -= 3;
    126  1.1  thorpej 
    127  1.1  thorpej 	/* Look up and call the command. */
    128  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    129  1.1  thorpej 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    130  1.1  thorpej 			break;
    131  1.1  thorpej 	if (commands[i].cmd_name == NULL)
    132  1.1  thorpej 		errx(1, "unknown command: %s", cmdname);
    133  1.1  thorpej 
    134  1.1  thorpej 	argnames = commands[i].arg_names;
    135  1.1  thorpej 
    136  1.1  thorpej 	/* Open the device. */
    137  1.1  thorpej 	fd = opendisk(dvname, commands[i].open_flags, dvname_store,
    138  1.1  thorpej 	    sizeof(dvname_store), 0);
    139  1.1  thorpej 	if (fd == -1)
    140  1.1  thorpej 		err(1, "%s", dvname);
    141  1.1  thorpej 
    142  1.1  thorpej 	dvname = dvname_store;
    143  1.1  thorpej 
    144  1.1  thorpej 	(*commands[i].cmd_func)(argc, argv);
    145  1.1  thorpej 	exit(0);
    146  1.1  thorpej }
    147  1.1  thorpej 
    148  1.1  thorpej void
    149  1.1  thorpej usage()
    150  1.1  thorpej {
    151  1.1  thorpej 	int i;
    152  1.1  thorpej 
    153  1.1  thorpej 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    154  1.1  thorpej 	    getprogname());
    155  1.1  thorpej 
    156  1.1  thorpej 	fprintf(stderr, "   Available commands:\n");
    157  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    158  1.1  thorpej 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    159  1.1  thorpej 		    commands[i].arg_names);
    160  1.1  thorpej 
    161  1.1  thorpej 	exit(1);
    162  1.1  thorpej }
    163  1.1  thorpej 
    164  1.1  thorpej void
    165  1.1  thorpej disk_getcache(int argc, char *argv[])
    166  1.1  thorpej {
    167  1.1  thorpej 	int bits;
    168  1.1  thorpej 
    169  1.1  thorpej 	if (ioctl(fd, DIOCGCACHE, &bits) == -1)
    170  1.1  thorpej 		err(1, "%s: getcache", dvname);
    171  1.1  thorpej 
    172  1.1  thorpej 	if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
    173  1.1  thorpej 		printf("%s: No caches enabled\n", dvname);
    174  1.1  thorpej 	else {
    175  1.1  thorpej 		if (bits & DKCACHE_READ)
    176  1.1  thorpej 			printf("%s: read cache enabled\n", dvname);
    177  1.1  thorpej 		if (bits & DKCACHE_WRITE)
    178  1.1  thorpej 			printf("%s: write-back cache enabled\n", dvname);
    179  1.1  thorpej 	}
    180  1.1  thorpej 
    181  1.1  thorpej 	printf("%s: read cache enable is %schangeable\n", dvname,
    182  1.1  thorpej 	    (bits & DKCACHE_RCHANGE) ? "" : "not ");
    183  1.1  thorpej 	printf("%s: write cache enable is %schangeable\n", dvname,
    184  1.1  thorpej 	    (bits & DKCACHE_WCHANGE) ? "" : "not ");
    185  1.1  thorpej 
    186  1.1  thorpej 	printf("%s: cache parameters are %ssavable\n", dvname,
    187  1.1  thorpej 	    (bits & DKCACHE_SAVE) ? "" : "not ");
    188  1.1  thorpej }
    189  1.1  thorpej 
    190  1.1  thorpej void
    191  1.1  thorpej disk_setcache(int argc, char *argv[])
    192  1.1  thorpej {
    193  1.1  thorpej 	int bits;
    194  1.1  thorpej 
    195  1.1  thorpej 	if (argc > 2 || argc == 0)
    196  1.1  thorpej 		usage();
    197  1.1  thorpej 
    198  1.1  thorpej 	if (strcmp(argv[0], "none") == 0)
    199  1.1  thorpej 		bits = 0;
    200  1.1  thorpej 	else if (strcmp(argv[0], "r") == 0)
    201  1.1  thorpej 		bits = DKCACHE_READ;
    202  1.1  thorpej 	else if (strcmp(argv[0], "w") == 0)
    203  1.1  thorpej 		bits = DKCACHE_WRITE;
    204  1.1  thorpej 	else if (strcmp(argv[0], "rw") == 0)
    205  1.1  thorpej 		bits = DKCACHE_READ|DKCACHE_WRITE;
    206  1.1  thorpej 	else
    207  1.1  thorpej 		usage();
    208  1.1  thorpej 
    209  1.1  thorpej 	if (argc == 2) {
    210  1.1  thorpej 		if (strcmp(argv[1], "save") == 0)
    211  1.1  thorpej 			bits |= DKCACHE_SAVE;
    212  1.1  thorpej 		else
    213  1.1  thorpej 			usage();
    214  1.1  thorpej 	}
    215  1.1  thorpej 
    216  1.1  thorpej 	if (ioctl(fd, DIOCSCACHE, &bits) == -1)
    217  1.1  thorpej 		err(1, "%s: setcache", dvname);
    218  1.1  thorpej }
    219  1.1  thorpej 
    220  1.1  thorpej void
    221  1.1  thorpej disk_synccache(int argc, char *argv[])
    222  1.1  thorpej {
    223  1.1  thorpej 	int force;
    224  1.1  thorpej 
    225  1.1  thorpej 	switch (argc) {
    226  1.1  thorpej 	case 0:
    227  1.1  thorpej 		force = 0;
    228  1.1  thorpej 		break;
    229  1.1  thorpej 
    230  1.1  thorpej 	case 1:
    231  1.1  thorpej 		if (strcmp(argv[0], "force") == 0)
    232  1.1  thorpej 			force = 1;
    233  1.1  thorpej 		else
    234  1.1  thorpej 			usage();
    235  1.1  thorpej 		break;
    236  1.1  thorpej 
    237  1.1  thorpej 	default:
    238  1.1  thorpej 		usage();
    239  1.1  thorpej 	}
    240  1.1  thorpej 
    241  1.1  thorpej 	if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
    242  1.1  thorpej 		err(1, "%s: sync cache", dvname);
    243  1.2     yamt }
    244  1.2     yamt 
    245  1.2     yamt void
    246  1.2     yamt disk_keeplabel(int argc, char *argv[])
    247  1.2     yamt {
    248  1.2     yamt 	int keep;
    249  1.2     yamt 	int yn;
    250  1.2     yamt 
    251  1.2     yamt 	if (argc != 1)
    252  1.2     yamt 		usage();
    253  1.2     yamt 
    254  1.2     yamt 	yn = yesno(argv[0]);
    255  1.2     yamt 	if (yn < 0)
    256  1.2     yamt 		usage();
    257  1.2     yamt 
    258  1.2     yamt 	keep = yn == YES;
    259  1.2     yamt 
    260  1.2     yamt 	if (ioctl(fd, DIOCKLABEL, &keep) == -1)
    261  1.2     yamt 		err(1, "%s: keep label", dvname);
    262  1.2     yamt }
    263  1.2     yamt 
    264  1.2     yamt /*
    265  1.2     yamt  * return YES, NO or -1.
    266  1.2     yamt  */
    267  1.2     yamt int
    268  1.2     yamt yesno(const char *p)
    269  1.2     yamt {
    270  1.2     yamt 
    271  1.2     yamt 	if (!strcmp(p, YES_STR))
    272  1.2     yamt 		return YES;
    273  1.2     yamt 	if (!strcmp(p, NO_STR))
    274  1.2     yamt 		return NO;
    275  1.2     yamt 	return -1;
    276  1.1  thorpej }
    277