summaryrefslogtreecommitdiff
path: root/usr/src/cmd/emul64ioctl/emul64ioctl.c
blob: cb2fcfe5f24116cbb157732df758317544bd7ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This program provides a command line interface to the
 * three new ioctls for the emul64 driver - EMUL64_WRITE_OFF,
 * EMUL64_WRITE_ON and EMUL64_ZERO_RANGE. All three of these
 * ioctls require the range of blocks to be specified. The
 * range is specified by starting block number and block count
 * both of which are 64 bit.
 *
 * Returns 0 on success, >0 on failure.
 *
 */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/emul64.h>

#define	DEBUG	1
#define	ADMIN_DIR	"/dev/cfg/"

char *Pname;

static int	get_disk_addr(char *path, emul64_tgt_range_t *tr, char **admin);

static void
usage(void)
{
	(void) fprintf(stderr, "Usage: emul64ioctl -s start_block "
	    "-b block_count -c write_off | write_on | zero emul64_dev\n");
	exit(1);
}

int
main(int argc, char **argv)
{
	extern char	*optarg;
	extern int	optind;
	char		*admin;
	int		count_seen = 0;
	int 		fd, retval;
	int		cmd = -1;
	int		do_usage = 0;
	char		*slice;
	int		sb_seen = 0;
	emul64_tgt_range_t tr;

	Pname = strrchr(argv[0], '/');
	if (Pname == NULL)
		Pname = argv[0];
	else
		Pname++;

	while ((retval = getopt(argc, argv, "s:b:c:")) != -1) {
		switch (retval) {
		case 's':
			sb_seen = 1;
			tr.emul64_blkrange.emul64_sb = atoll(optarg);
			break;
		case 'b':
			count_seen = 1;
			tr.emul64_blkrange.emul64_blkcnt = atoll(optarg);
			break;
		case 'c':
			if (strncmp(optarg, "write_off",
			    strlen("write_off")) == 0) {
				cmd = EMUL64_WRITE_OFF;
			} else if (strncmp(optarg, "write_on",
			    strlen("write_on")) == 0) {
				cmd = EMUL64_WRITE_ON;
			} else if (strncmp(optarg, "zero",
			    strlen("zero")) == 0) {
				cmd = EMUL64_ZERO_RANGE;
			} else {
			    do_usage = 1;
			}
			break;
		    default:
			break;
		}
	}

	if (do_usage || (optind != argc - 1)) {
		usage();
	}
	if ((sb_seen == 0) || (count_seen == 0) || (cmd == -1))
		usage();

	slice = argv[optind];

	/*
	 * Get admin device, target and lun
	 */
	if (get_disk_addr(slice, &tr, &admin) != 0)
		exit(1);

	/*
	 * open the specified emul64_dev.
	 */
	if ((fd = open(admin, O_RDONLY, 0444)) != -1)	{

		retval = ioctl(fd, cmd, &tr);
		(void) close(fd);

		if (retval != -1) {
			free(admin);
			return (0);
		}
		(void) printf("emul64ioctl: %s: ioctl %s\n",
		    admin, strerror(errno));
	} else {
		(void) printf("emul64ioctl: %s: open %s\n",
		    admin, strerror(errno));
	}
	free(admin);
	return (1);
}

#define	TOK_CHECK(s)	if (token == NULL) {\
						bogus = (s);\
						goto err_out;\
			}

static int
get_disk_addr(char *path, emul64_tgt_range_t *tr, char **admin)
{
	size_t		admin_size;
	int		ctlr_num;
	int		conversions;
	char		*ctds;

	*admin = NULL;
	ctds = strrchr(path, '/');
	if (ctds == NULL)
		ctds = path;
	else
		ctds++;
	conversions = sscanf(ctds, "c%dt%hud%hu", &ctlr_num,
				&tr->emul64_target, &tr->emul64_lun);
	if (conversions != 3) {
		(void) fprintf(stderr, "%s: \"%s\" is invalid disk name.  "
			"%d conversions\n", Pname, ctds, conversions);
		return (-1);
	}

	/* Build controller name */
	admin_size = strlen(ADMIN_DIR) +
		10 +		/* enough digits for an int */
		1 +		/* c */
		1;		/* Null terminator */
	*admin = malloc(admin_size);
	if (*admin == NULL) {
		(void) fprintf(stderr, "%s: out of memory\n", Pname);
		return (-1);
	}
	(void) snprintf(*admin, admin_size, "%sc%d", ADMIN_DIR, ctlr_num);
	return (0);
}