Home | History | Annotate | Line # | Download | only in dkctl
dkctl.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: dkctl.c,v 1.1 2002/01/09 22:30:14 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.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.1  thorpej struct command {
     55  1.1  thorpej 	const char *cmd_name;
     56  1.1  thorpej 	const char *arg_names;
     57  1.1  thorpej 	void (*cmd_func)(int, char *[]);
     58  1.1  thorpej 	int open_flags;
     59  1.1  thorpej };
     60  1.1  thorpej 
     61  1.1  thorpej int	main(int, char *[]);
     62  1.1  thorpej void	usage(void);
     63  1.1  thorpej 
     64  1.1  thorpej int	fd;				/* file descriptor for device */
     65  1.1  thorpej const	char *dvname;			/* device name */
     66  1.1  thorpej char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     67  1.1  thorpej const	char *cmdname;			/* command user issued */
     68  1.1  thorpej const	char *argnames;			/* helpstring; expected arguments */
     69  1.1  thorpej 
     70  1.1  thorpej void	disk_getcache(int, char *[]);
     71  1.1  thorpej void	disk_setcache(int, char *[]);
     72  1.1  thorpej void	disk_synccache(int, char *[]);
     73  1.1  thorpej 
     74  1.1  thorpej struct command commands[] = {
     75  1.1  thorpej 	{ "getcache",
     76  1.1  thorpej 	  "",
     77  1.1  thorpej 	  disk_getcache,
     78  1.1  thorpej 	  O_RDONLY },
     79  1.1  thorpej 
     80  1.1  thorpej 	{ "setcache",
     81  1.1  thorpej 	  "none | r | w | rw [save]",
     82  1.1  thorpej 	  disk_setcache,
     83  1.1  thorpej 	  O_RDWR },
     84  1.1  thorpej 
     85  1.1  thorpej 	{ "synccache",
     86  1.1  thorpej 	  "[force]",
     87  1.1  thorpej 	  disk_synccache,
     88  1.1  thorpej 	  O_RDWR },
     89  1.1  thorpej 
     90  1.1  thorpej 	{ NULL,
     91  1.1  thorpej 	  NULL,
     92  1.1  thorpej 	  NULL,
     93  1.1  thorpej 	  0 },
     94  1.1  thorpej };
     95  1.1  thorpej 
     96  1.1  thorpej int
     97  1.1  thorpej main(int argc, char *argv[])
     98  1.1  thorpej {
     99  1.1  thorpej 	int i;
    100  1.1  thorpej 
    101  1.1  thorpej 	/* Must have at least: device command */
    102  1.1  thorpej 	if (argc < 3)
    103  1.1  thorpej 		usage();
    104  1.1  thorpej 
    105  1.1  thorpej 	/* Skip program name, get and skip device name and command. */
    106  1.1  thorpej 	dvname = argv[1];
    107  1.1  thorpej 	cmdname = argv[2];
    108  1.1  thorpej 	argv += 3;
    109  1.1  thorpej 	argc -= 3;
    110  1.1  thorpej 
    111  1.1  thorpej 	/* Look up and call the command. */
    112  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    113  1.1  thorpej 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    114  1.1  thorpej 			break;
    115  1.1  thorpej 	if (commands[i].cmd_name == NULL)
    116  1.1  thorpej 		errx(1, "unknown command: %s", cmdname);
    117  1.1  thorpej 
    118  1.1  thorpej 	argnames = commands[i].arg_names;
    119  1.1  thorpej 
    120  1.1  thorpej 	/* Open the device. */
    121  1.1  thorpej 	fd = opendisk(dvname, commands[i].open_flags, dvname_store,
    122  1.1  thorpej 	    sizeof(dvname_store), 0);
    123  1.1  thorpej 	if (fd == -1)
    124  1.1  thorpej 		err(1, "%s", dvname);
    125  1.1  thorpej 
    126  1.1  thorpej 	dvname = dvname_store;
    127  1.1  thorpej 
    128  1.1  thorpej 	(*commands[i].cmd_func)(argc, argv);
    129  1.1  thorpej 	exit(0);
    130  1.1  thorpej }
    131  1.1  thorpej 
    132  1.1  thorpej void
    133  1.1  thorpej usage()
    134  1.1  thorpej {
    135  1.1  thorpej 	int i;
    136  1.1  thorpej 
    137  1.1  thorpej 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    138  1.1  thorpej 	    getprogname());
    139  1.1  thorpej 
    140  1.1  thorpej 	fprintf(stderr, "   Available commands:\n");
    141  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    142  1.1  thorpej 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    143  1.1  thorpej 		    commands[i].arg_names);
    144  1.1  thorpej 
    145  1.1  thorpej 	exit(1);
    146  1.1  thorpej }
    147  1.1  thorpej 
    148  1.1  thorpej void
    149  1.1  thorpej disk_getcache(int argc, char *argv[])
    150  1.1  thorpej {
    151  1.1  thorpej 	int bits;
    152  1.1  thorpej 
    153  1.1  thorpej 	if (ioctl(fd, DIOCGCACHE, &bits) == -1)
    154  1.1  thorpej 		err(1, "%s: getcache", dvname);
    155  1.1  thorpej 
    156  1.1  thorpej 	if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
    157  1.1  thorpej 		printf("%s: No caches enabled\n", dvname);
    158  1.1  thorpej 	else {
    159  1.1  thorpej 		if (bits & DKCACHE_READ)
    160  1.1  thorpej 			printf("%s: read cache enabled\n", dvname);
    161  1.1  thorpej 		if (bits & DKCACHE_WRITE)
    162  1.1  thorpej 			printf("%s: write-back cache enabled\n", dvname);
    163  1.1  thorpej 	}
    164  1.1  thorpej 
    165  1.1  thorpej 	printf("%s: read cache enable is %schangeable\n", dvname,
    166  1.1  thorpej 	    (bits & DKCACHE_RCHANGE) ? "" : "not ");
    167  1.1  thorpej 	printf("%s: write cache enable is %schangeable\n", dvname,
    168  1.1  thorpej 	    (bits & DKCACHE_WCHANGE) ? "" : "not ");
    169  1.1  thorpej 
    170  1.1  thorpej 	printf("%s: cache parameters are %ssavable\n", dvname,
    171  1.1  thorpej 	    (bits & DKCACHE_SAVE) ? "" : "not ");
    172  1.1  thorpej }
    173  1.1  thorpej 
    174  1.1  thorpej void
    175  1.1  thorpej disk_setcache(int argc, char *argv[])
    176  1.1  thorpej {
    177  1.1  thorpej 	int bits;
    178  1.1  thorpej 
    179  1.1  thorpej 	if (argc > 2 || argc == 0)
    180  1.1  thorpej 		usage();
    181  1.1  thorpej 
    182  1.1  thorpej 	if (strcmp(argv[0], "none") == 0)
    183  1.1  thorpej 		bits = 0;
    184  1.1  thorpej 	else if (strcmp(argv[0], "r") == 0)
    185  1.1  thorpej 		bits = DKCACHE_READ;
    186  1.1  thorpej 	else if (strcmp(argv[0], "w") == 0)
    187  1.1  thorpej 		bits = DKCACHE_WRITE;
    188  1.1  thorpej 	else if (strcmp(argv[0], "rw") == 0)
    189  1.1  thorpej 		bits = DKCACHE_READ|DKCACHE_WRITE;
    190  1.1  thorpej 	else
    191  1.1  thorpej 		usage();
    192  1.1  thorpej 
    193  1.1  thorpej 	if (argc == 2) {
    194  1.1  thorpej 		if (strcmp(argv[1], "save") == 0)
    195  1.1  thorpej 			bits |= DKCACHE_SAVE;
    196  1.1  thorpej 		else
    197  1.1  thorpej 			usage();
    198  1.1  thorpej 	}
    199  1.1  thorpej 
    200  1.1  thorpej 	if (ioctl(fd, DIOCSCACHE, &bits) == -1)
    201  1.1  thorpej 		err(1, "%s: setcache", dvname);
    202  1.1  thorpej }
    203  1.1  thorpej 
    204  1.1  thorpej void
    205  1.1  thorpej disk_synccache(int argc, char *argv[])
    206  1.1  thorpej {
    207  1.1  thorpej 	int force;
    208  1.1  thorpej 
    209  1.1  thorpej 	switch (argc) {
    210  1.1  thorpej 	case 0:
    211  1.1  thorpej 		force = 0;
    212  1.1  thorpej 		break;
    213  1.1  thorpej 
    214  1.1  thorpej 	case 1:
    215  1.1  thorpej 		if (strcmp(argv[0], "force") == 0)
    216  1.1  thorpej 			force = 1;
    217  1.1  thorpej 		else
    218  1.1  thorpej 			usage();
    219  1.1  thorpej 		break;
    220  1.1  thorpej 
    221  1.1  thorpej 	default:
    222  1.1  thorpej 		usage();
    223  1.1  thorpej 	}
    224  1.1  thorpej 
    225  1.1  thorpej 	if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
    226  1.1  thorpej 		err(1, "%s: sync cache", dvname);
    227  1.1  thorpej }
    228