xref: /freebsd/usr.sbin/ctladm/ctladm.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3  * Copyright (c) 1997-2007 Kenneth D. Merry
4  * Copyright (c) 2012 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
36  */
37 /*
38  * CAM Target Layer exercise program.
39  *
40  * Author: Ken Merry <ken@FreeBSD.org>
41  */
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/callout.h>
48 #include <sys/ioctl.h>
49 #include <sys/linker.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/sbuf.h>
53 #include <sys/stat.h>
54 #include <bsdxml.h>
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <getopt.h>
60 #include <stdlib.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/ctl/ctl.h>
68 #include <cam/ctl/ctl_io.h>
69 #include <cam/ctl/ctl_backend.h>
70 #include <cam/ctl/ctl_ioctl.h>
71 #include <cam/ctl/ctl_util.h>
72 #include <cam/ctl/ctl_scsi_all.h>
73 #include <camlib.h>
74 #include <libutil.h>
75 #include "ctladm.h"
76 
77 #ifdef min
78 #undef min
79 #endif
80 #define min(x,y) (x < y) ? x : y
81 
82 typedef enum {
83 	CTLADM_CMD_TUR,
84 	CTLADM_CMD_INQUIRY,
85 	CTLADM_CMD_REQ_SENSE,
86 	CTLADM_CMD_ARRAYLIST,
87 	CTLADM_CMD_REPORT_LUNS,
88 	CTLADM_CMD_HELP,
89 	CTLADM_CMD_DEVLIST,
90 	CTLADM_CMD_ADDDEV,
91 	CTLADM_CMD_RM,
92 	CTLADM_CMD_CREATE,
93 	CTLADM_CMD_READ,
94 	CTLADM_CMD_WRITE,
95 	CTLADM_CMD_PORT,
96 	CTLADM_CMD_PORTLIST,
97 	CTLADM_CMD_READCAPACITY,
98 	CTLADM_CMD_MODESENSE,
99 	CTLADM_CMD_DUMPOOA,
100 	CTLADM_CMD_DUMPSTRUCTS,
101 	CTLADM_CMD_START,
102 	CTLADM_CMD_STOP,
103 	CTLADM_CMD_SYNC_CACHE,
104 	CTLADM_CMD_LUNLIST,
105 	CTLADM_CMD_DELAY,
106 	CTLADM_CMD_ERR_INJECT,
107 	CTLADM_CMD_PRES_IN,
108 	CTLADM_CMD_PRES_OUT,
109 	CTLADM_CMD_INQ_VPD_DEVID,
110 	CTLADM_CMD_RTPG,
111 	CTLADM_CMD_MODIFY,
112 	CTLADM_CMD_ISLIST,
113 	CTLADM_CMD_ISLOGOUT,
114 	CTLADM_CMD_ISTERMINATE,
115 	CTLADM_CMD_LUNMAP
116 } ctladm_cmdfunction;
117 
118 typedef enum {
119 	CTLADM_ARG_NONE		= 0x0000000,
120 	CTLADM_ARG_AUTOSENSE	= 0x0000001,
121 	CTLADM_ARG_DEVICE	= 0x0000002,
122 	CTLADM_ARG_ARRAYSIZE	= 0x0000004,
123 	CTLADM_ARG_BACKEND	= 0x0000008,
124 	CTLADM_ARG_CDBSIZE	= 0x0000010,
125 	CTLADM_ARG_DATALEN	= 0x0000020,
126 	CTLADM_ARG_FILENAME	= 0x0000040,
127 	CTLADM_ARG_LBA		= 0x0000080,
128 	CTLADM_ARG_PC		= 0x0000100,
129 	CTLADM_ARG_PAGE_CODE	= 0x0000200,
130 	CTLADM_ARG_PAGE_LIST	= 0x0000400,
131 	CTLADM_ARG_SUBPAGE	= 0x0000800,
132 	CTLADM_ARG_PAGELIST	= 0x0001000,
133 	CTLADM_ARG_DBD		= 0x0002000,
134 	CTLADM_ARG_TARG_LUN	= 0x0004000,
135 	CTLADM_ARG_BLOCKSIZE	= 0x0008000,
136 	CTLADM_ARG_IMMED	= 0x0010000,
137 	CTLADM_ARG_RELADR	= 0x0020000,
138 	CTLADM_ARG_RETRIES	= 0x0040000,
139 	CTLADM_ARG_ONOFFLINE	= 0x0080000,
140 	CTLADM_ARG_ONESHOT	= 0x0100000,
141 	CTLADM_ARG_TIMEOUT	= 0x0200000,
142 	CTLADM_ARG_INITIATOR	= 0x0400000,
143 	CTLADM_ARG_NOCOPY	= 0x0800000,
144 	CTLADM_ARG_NEED_TL	= 0x1000000
145 } ctladm_cmdargs;
146 
147 struct ctladm_opts {
148 	const char	*optname;
149 	uint32_t	cmdnum;
150 	ctladm_cmdargs	argnum;
151 	const char	*subopt;
152 };
153 
154 typedef enum {
155 	CC_OR_NOT_FOUND,
156 	CC_OR_AMBIGUOUS,
157 	CC_OR_FOUND
158 } ctladm_optret;
159 
160 static const char rw_opts[] = "Nb:c:d:f:l:";
161 static const char startstop_opts[] = "i";
162 
163 static struct ctladm_opts option_table[] = {
164 	{"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
165 	{"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
166 	{"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
167 	{"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
168 	{"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
169 	{"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
170 	{"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
171 	{"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
172 	{"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
173 	{"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
174 	{"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
175 	{"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
176 	{"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
177 	{"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
178 	{"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
179 	{"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
180 	{"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
181 	{"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
182 	{"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
183 	{"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
184 	{"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
185 	{"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
186 	{"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
187 	{"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
188 	{"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
189 	{"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
190 	{"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
191 	{"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
192 	{"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
193 	{"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
194 	{"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
195 	{"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
196 	{"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
197 	{"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
198 	{NULL, 0, 0, NULL}
199 };
200 
201 
202 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
203 			ctladm_cmdargs *argnum, const char **subopt);
204 static int cctl_dump_ooa(int fd, int argc, char **argv);
205 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
206 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
207 static int cctl_delay(int fd, int lun, int argc, char **argv,
208 		      char *combinedopt);
209 static int cctl_lunlist(int fd);
210 static int cctl_sync_cache(int fd, int lun, int iid, int retries,
211 			   int argc, char **argv, char *combinedopt);
212 static int cctl_start_stop(int fd, int lun, int iid, int retries,
213 			   int start, int argc, char **argv, char *combinedopt);
214 static int cctl_mode_sense(int fd, int lun, int iid, int retries,
215 			   int argc, char **argv, char *combinedopt);
216 static int cctl_read_capacity(int fd, int lun, int iid,
217 			      int retries, int argc, char **argv,
218 			      char *combinedopt);
219 static int cctl_read_write(int fd, int lun, int iid, int retries,
220 			   int argc, char **argv, char *combinedopt,
221 			   ctladm_cmdfunction command);
222 static int cctl_get_luns(int fd, int lun, int iid, int retries,
223 			 struct scsi_report_luns_data **lun_data,
224 			 uint32_t *num_luns);
225 static int cctl_report_luns(int fd, int lun, int iid, int retries);
226 static int cctl_tur(int fd, int lun, int iid, int retries);
227 static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
228 			    char *path_str, int path_len,
229 			    struct scsi_inquiry_data *inq_data);
230 static int cctl_inquiry(int fd, int lun, int iid, int retries);
231 static int cctl_req_sense(int fd, int lun, int iid, int retries);
232 static int cctl_persistent_reserve_in(int fd, int lun,
233 				      int initiator, int argc, char **argv,
234 				      char *combinedopt, int retry_count);
235 static int cctl_persistent_reserve_out(int fd, int lun,
236 				       int initiator, int argc, char **argv,
237 				       char *combinedopt, int retry_count);
238 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
239 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
240 static int cctl_report_target_port_group(int fd, int lun, int initiator);
241 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
242 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
243 
244 ctladm_optret
245 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
246 	  ctladm_cmdargs *argnum, const char **subopt)
247 {
248 	struct ctladm_opts *opts;
249 	int num_matches = 0;
250 
251 	for (opts = table; (opts != NULL) && (opts->optname != NULL);
252 	     opts++) {
253 		if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
254 			*cmdnum = opts->cmdnum;
255 			*argnum = opts->argnum;
256 			*subopt = opts->subopt;
257 
258 			if (strcmp(opts->optname, arg) == 0)
259 				return (CC_OR_FOUND);
260 
261 			if (++num_matches > 1)
262 				return(CC_OR_AMBIGUOUS);
263 		}
264 	}
265 
266 	if (num_matches > 0)
267 		return(CC_OR_FOUND);
268 	else
269 		return(CC_OR_NOT_FOUND);
270 }
271 
272 static int
273 cctl_dump_ooa(int fd, int argc, char **argv)
274 {
275 	struct ctl_ooa ooa;
276 	long double cmd_latency;
277 	int num_entries, len, lun = -1, retval = 0;
278 	unsigned int i;
279 
280 	num_entries = 104;
281 
282 	if ((argc > 2) && (isdigit(argv[2][0])))
283 		lun = strtol(argv[2], NULL, 0);
284 retry:
285 
286 	len = num_entries * sizeof(struct ctl_ooa_entry);
287 	bzero(&ooa, sizeof(ooa));
288 	ooa.entries = malloc(len);
289 	if (ooa.entries == NULL) {
290 		warn("%s: error mallocing %d bytes", __func__, len);
291 		return (1);
292 	}
293 	if (lun >= 0) {
294 		ooa.lun_num = lun;
295 	} else
296 		ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
297 	ooa.alloc_len = len;
298 	ooa.alloc_num = num_entries;
299 	if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
300 		warn("%s: CTL_GET_OOA ioctl failed", __func__);
301 		retval = 1;
302 		goto bailout;
303 	}
304 
305 	if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
306 		num_entries = num_entries * 2;
307 		free(ooa.entries);
308 		ooa.entries = NULL;
309 		goto retry;
310 	}
311 
312 	if (ooa.status != CTL_OOA_OK) {
313 		warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
314 		      ooa.status);
315 		retval = 1;
316 		goto bailout;
317 	}
318 
319 	fprintf(stdout, "Dumping OOA queues\n");
320 	for (i = 0; i < ooa.fill_num; i++) {
321 		struct ctl_ooa_entry *entry;
322 		char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
323 		struct bintime delta_bt;
324 		struct timespec ts;
325 
326 		entry = &ooa.entries[i];
327 
328 		delta_bt = ooa.cur_bt;
329 		bintime_sub(&delta_bt, &entry->start_bt);
330 		bintime2timespec(&delta_bt, &ts);
331 		cmd_latency = ts.tv_sec * 1000;
332 		if (ts.tv_nsec > 0)
333 			cmd_latency += ts.tv_nsec / 1000000;
334 
335 		fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
336 			"(%0.0Lf ms)\n",
337 			(intmax_t)entry->lun_num, entry->tag_num,
338 			(entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
339 			 " BLOCKED" : "",
340 			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
341 			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
342 			 " DMAQUEUED" : "",
343 			(entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
344 			 " ABORT" : "",
345 			(entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
346 			scsi_op_desc(entry->cdb[0], NULL),
347 			scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
348 			cmd_latency);
349 	}
350 	fprintf(stdout, "OOA queues dump done\n");
351 
352 bailout:
353 	free(ooa.entries);
354 	return (retval);
355 }
356 
357 static int
358 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
359 {
360 	if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
361 		warn(__func__);
362 		return (1);
363 	}
364 	return (0);
365 }
366 
367 typedef enum {
368 	CCTL_PORT_MODE_NONE,
369 	CCTL_PORT_MODE_LIST,
370 	CCTL_PORT_MODE_SET,
371 	CCTL_PORT_MODE_ON,
372 	CCTL_PORT_MODE_OFF
373 } cctl_port_mode;
374 
375 static struct ctladm_opts cctl_fe_table[] = {
376 	{"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
377 	{"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
378 	{"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
379 	{"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
380 	{"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
381 	{"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
382 	{NULL, 0, 0, NULL}
383 };
384 
385 static int
386 cctl_port(int fd, int argc, char **argv, char *combinedopt)
387 {
388 	int c;
389 	int32_t targ_port = -1;
390 	int retval = 0;
391 	int wwnn_set = 0, wwpn_set = 0;
392 	uint64_t wwnn = 0, wwpn = 0;
393 	cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
394 	struct ctl_port_entry entry;
395 	ctl_port_type port_type = CTL_PORT_NONE;
396 	int quiet = 0, xml = 0;
397 
398 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
399 		switch (c) {
400 		case 'l':
401 			if (port_mode != CCTL_PORT_MODE_NONE)
402 				goto bailout_badarg;
403 
404 			port_mode = CCTL_PORT_MODE_LIST;
405 			break;
406 		case 'o':
407 			if (port_mode != CCTL_PORT_MODE_NONE)
408 				goto bailout_badarg;
409 
410 			if (strcasecmp(optarg, "on") == 0)
411 				port_mode = CCTL_PORT_MODE_ON;
412 			else if (strcasecmp(optarg, "off") == 0)
413 				port_mode = CCTL_PORT_MODE_OFF;
414 			else {
415 				warnx("Invalid -o argument %s, \"on\" or "
416 				      "\"off\" are the only valid args",
417 				      optarg);
418 				retval = 1;
419 				goto bailout;
420 			}
421 			break;
422 		case 'p':
423 			targ_port = strtol(optarg, NULL, 0);
424 			break;
425 		case 'q':
426 			quiet = 1;
427 			break;
428 		case 't': {
429 			ctladm_optret optret;
430 			ctladm_cmdargs argnum;
431 			const char *subopt;
432 			ctl_port_type tmp_port_type;
433 
434 			optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
435 					   &argnum, &subopt);
436 			if (optret == CC_OR_AMBIGUOUS) {
437 				warnx("%s: ambiguous frontend type %s",
438 				      __func__, optarg);
439 				retval = 1;
440 				goto bailout;
441 			} else if (optret == CC_OR_NOT_FOUND) {
442 				warnx("%s: invalid frontend type %s",
443 				      __func__, optarg);
444 				retval = 1;
445 				goto bailout;
446 			}
447 
448 			port_type |= tmp_port_type;
449 			break;
450 		}
451 		case 'w':
452 			if ((port_mode != CCTL_PORT_MODE_NONE)
453 			 && (port_mode != CCTL_PORT_MODE_SET))
454 				goto bailout_badarg;
455 
456 			port_mode = CCTL_PORT_MODE_SET;
457 
458 			wwnn = strtoull(optarg, NULL, 0);
459 			wwnn_set = 1;
460 			break;
461 		case 'W':
462 			if ((port_mode != CCTL_PORT_MODE_NONE)
463 			 && (port_mode != CCTL_PORT_MODE_SET))
464 				goto bailout_badarg;
465 
466 			port_mode = CCTL_PORT_MODE_SET;
467 
468 			wwpn = strtoull(optarg, NULL, 0);
469 			wwpn_set = 1;
470 			break;
471 		case 'x':
472 			xml = 1;
473 			break;
474 		}
475 	}
476 
477 	/*
478 	 * The user can specify either one or more frontend types (-t), or
479 	 * a specific frontend, but not both.
480 	 *
481 	 * If the user didn't specify a frontend type or number, set it to
482 	 * all.  This is primarily needed for the enable/disable ioctls.
483 	 * This will be a no-op for the listing code.  For the set ioctl,
484 	 * we'll throw an error, since that only works on one port at a time.
485 	 */
486 	if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
487 		warnx("%s: can only specify one of -t or -n", __func__);
488 		retval = 1;
489 		goto bailout;
490 	} else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
491 		port_type = CTL_PORT_ALL;
492 
493 	bzero(&entry, sizeof(entry));
494 
495 	/*
496 	 * These are needed for all but list/dump mode.
497 	 */
498 	entry.port_type = port_type;
499 	entry.targ_port = targ_port;
500 
501 	switch (port_mode) {
502 	case CCTL_PORT_MODE_LIST: {
503 		char opts[] = "xq";
504 		char argx[] = "-x";
505 		char argq[] = "-q";
506 		char *argvx[2];
507 		int argcx = 0;
508 
509 		optind = 0;
510 		optreset = 1;
511 		if (xml)
512 			argvx[argcx++] = argx;
513 		if (quiet)
514 			argvx[argcx++] = argq;
515 		cctl_portlist(fd, argcx, argvx, opts);
516 		break;
517 	}
518 	case CCTL_PORT_MODE_SET:
519 		if (targ_port == -1) {
520 			warnx("%s: -w and -W require -n", __func__);
521 			retval = 1;
522 			goto bailout;
523 		}
524 
525 		if (wwnn_set) {
526 			entry.flags |= CTL_PORT_WWNN_VALID;
527 			entry.wwnn = wwnn;
528 		}
529 		if (wwpn_set) {
530 			entry.flags |= CTL_PORT_WWPN_VALID;
531 			entry.wwpn = wwpn;
532 		}
533 
534 		if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
535 			warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
536 			retval = 1;
537 			goto bailout;
538 		}
539 		break;
540 	case CCTL_PORT_MODE_ON:
541 		if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
542 			warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
543 			retval = 1;
544 			goto bailout;
545 		}
546 		fprintf(stdout, "Front End Ports enabled\n");
547 		break;
548 	case CCTL_PORT_MODE_OFF:
549 		if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
550 			warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
551 			retval = 1;
552 			goto bailout;
553 		}
554 		fprintf(stdout, "Front End Ports disabled\n");
555 		break;
556 	default:
557 		warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
558 		retval = 1;
559 		goto bailout;
560 		break;
561 	}
562 
563 bailout:
564 
565 	return (retval);
566 
567 bailout_badarg:
568 	warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
569 	return (1);
570 }
571 
572 static int
573 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
574 {
575 	do {
576 		if (ioctl(fd, CTL_IO, io) == -1) {
577 			warn("%s: error sending CTL_IO ioctl", func);
578 			return (-1);
579 		}
580 	} while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
581 	      && (retries-- > 0));
582 
583 	return (0);
584 }
585 
586 static int
587 cctl_delay(int fd, int lun, int argc, char **argv,
588 	   char *combinedopt)
589 {
590 	struct ctl_io_delay_info delay_info;
591 	char *delayloc = NULL;
592 	char *delaytype = NULL;
593 	int delaytime = -1;
594 	int retval;
595 	int c;
596 
597 	retval = 0;
598 
599 	memset(&delay_info, 0, sizeof(delay_info));
600 
601 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
602 		switch (c) {
603 		case 'T':
604 			delaytype = strdup(optarg);
605 			break;
606 		case 'l':
607 			delayloc = strdup(optarg);
608 			break;
609 		case 't':
610 			delaytime = strtoul(optarg, NULL, 0);
611 			break;
612 		}
613 	}
614 
615 	if (delaytime == -1) {
616 		warnx("%s: you must specify the delaytime with -t", __func__);
617 		retval = 1;
618 		goto bailout;
619 	}
620 
621 	if (strcasecmp(delayloc, "datamove") == 0)
622 		delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
623 	else if (strcasecmp(delayloc, "done") == 0)
624 		delay_info.delay_loc = CTL_DELAY_LOC_DONE;
625 	else {
626 		warnx("%s: invalid delay location %s", __func__, delayloc);
627 		retval = 1;
628 		goto bailout;
629 	}
630 
631 	if ((delaytype == NULL)
632 	 || (strcmp(delaytype, "oneshot") == 0))
633 		delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
634 	else if (strcmp(delaytype, "cont") == 0)
635 		delay_info.delay_type = CTL_DELAY_TYPE_CONT;
636 	else {
637 		warnx("%s: invalid delay type %s", __func__, delaytype);
638 		retval = 1;
639 		goto bailout;
640 	}
641 
642 	delay_info.lun_id = lun;
643 	delay_info.delay_secs = delaytime;
644 
645 	if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
646 		warn("%s: CTL_DELAY_IO ioctl failed", __func__);
647 		retval = 1;
648 		goto bailout;
649 	}
650 	switch (delay_info.status) {
651 	case CTL_DELAY_STATUS_NONE:
652 		warnx("%s: no delay status??", __func__);
653 		retval = 1;
654 		break;
655 	case CTL_DELAY_STATUS_OK:
656 		break;
657 	case CTL_DELAY_STATUS_INVALID_LUN:
658 		warnx("%s: invalid lun %d", __func__, lun);
659 		retval = 1;
660 		break;
661 	case CTL_DELAY_STATUS_INVALID_TYPE:
662 		warnx("%s: invalid delay type %d", __func__,
663 		      delay_info.delay_type);
664 		retval = 1;
665 		break;
666 	case CTL_DELAY_STATUS_INVALID_LOC:
667 		warnx("%s: delay location %s not implemented?", __func__,
668 		      delayloc);
669 		retval = 1;
670 		break;
671 	case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
672 		warnx("%s: delay not implemented in the kernel", __func__);
673 		warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
674 		retval = 1;
675 		break;
676 	default:
677 		warnx("%s: unknown delay return status %d", __func__,
678 		      delay_info.status);
679 		retval = 1;
680 		break;
681 	}
682 
683 bailout:
684 	free(delayloc);
685 	free(delaytype);
686 	return (retval);
687 }
688 
689 static struct ctladm_opts cctl_err_types[] = {
690 	{"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
691 	{"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
692 	{"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
693 	{"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
694 	{NULL, 0, 0, NULL}
695 
696 };
697 
698 static struct ctladm_opts cctl_err_patterns[] = {
699 	{"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
700 	{"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
701 	{"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
702 	{"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
703 	{"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
704 	{"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
705 	{"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
706 #if 0
707 	{"cmd", CTL_LUN_PAT_CMD,  CTLADM_ARG_NONE, NULL},
708 #endif
709 	{NULL, 0, 0, NULL}
710 };
711 
712 static int
713 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
714 		  char *combinedopt)
715 {
716 	int retval = 0;
717 	struct ctl_error_desc err_desc;
718 	uint64_t lba = 0;
719 	uint32_t len = 0;
720 	uint64_t delete_id = 0;
721 	int delete_id_set = 0;
722 	int continuous = 0;
723 	int sense_len = 0;
724 	int fd_sense = 0;
725 	int c;
726 
727 	bzero(&err_desc, sizeof(err_desc));
728 	err_desc.lun_id = lun;
729 
730 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
731 		switch (c) {
732 		case 'c':
733 			continuous = 1;
734 			break;
735 		case 'd':
736 			delete_id = strtoull(optarg, NULL, 0);
737 			delete_id_set = 1;
738 			break;
739 		case 'i':
740 		case 'p': {
741 			ctladm_optret optret;
742 			ctladm_cmdargs argnum;
743 			const char *subopt;
744 
745 			if (c == 'i') {
746 				ctl_lun_error err_type;
747 
748 				if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
749 					warnx("%s: can't specify multiple -i "
750 					      "arguments", __func__);
751 					retval = 1;
752 					goto bailout;
753 				}
754 				optret = getoption(cctl_err_types, optarg,
755 						   &err_type, &argnum, &subopt);
756 				err_desc.lun_error = err_type;
757 			} else {
758 				ctl_lun_error_pattern pattern;
759 
760 				optret = getoption(cctl_err_patterns, optarg,
761 						   &pattern, &argnum, &subopt);
762 				err_desc.error_pattern |= pattern;
763 			}
764 
765 			if (optret == CC_OR_AMBIGUOUS) {
766 				warnx("%s: ambiguous argument %s", __func__,
767 				      optarg);
768 				retval = 1;
769 				goto bailout;
770 			} else if (optret == CC_OR_NOT_FOUND) {
771 				warnx("%s: argument %s not found", __func__,
772 				      optarg);
773 				retval = 1;
774 				goto bailout;
775 			}
776 			break;
777 		}
778 		case 'r': {
779 			char *tmpstr, *tmpstr2;
780 
781 			tmpstr = strdup(optarg);
782 			if (tmpstr == NULL) {
783 				warn("%s: error duplicating string %s",
784 				     __func__, optarg);
785 				retval = 1;
786 				goto bailout;
787 			}
788 
789 			tmpstr2 = strsep(&tmpstr, ",");
790 			if (tmpstr2 == NULL) {
791 				warnx("%s: invalid -r argument %s", __func__,
792 				      optarg);
793 				retval = 1;
794 				free(tmpstr);
795 				goto bailout;
796 			}
797 			lba = strtoull(tmpstr2, NULL, 0);
798 			tmpstr2 = strsep(&tmpstr, ",");
799 			if (tmpstr2 == NULL) {
800 				warnx("%s: no len argument for -r lba,len, got"
801 				      " %s", __func__, optarg);
802 				retval = 1;
803 				free(tmpstr);
804 				goto bailout;
805 			}
806 			len = strtoul(tmpstr2, NULL, 0);
807 			free(tmpstr);
808 			break;
809 		}
810 		case 's': {
811 			struct get_hook hook;
812 			char *sensestr;
813 
814 			sense_len = strtol(optarg, NULL, 0);
815 			if (sense_len <= 0) {
816 				warnx("invalid number of sense bytes %d",
817 				      sense_len);
818 				retval = 1;
819 				goto bailout;
820 			}
821 
822 			sense_len = MIN(sense_len, SSD_FULL_SIZE);
823 
824 			hook.argc = argc - optind;
825 			hook.argv = argv + optind;
826 			hook.got = 0;
827 
828 			sensestr = cget(&hook, NULL);
829 			if ((sensestr != NULL)
830 			 && (sensestr[0] == '-')) {
831 				fd_sense = 1;
832 			} else {
833 				buff_encode_visit(
834 				    (uint8_t *)&err_desc.custom_sense,
835 				    sense_len, sensestr, iget, &hook);
836 			}
837 			optind += hook.got;
838 			break;
839 		}
840 		default:
841 			break;
842 		}
843 	}
844 
845 	if (delete_id_set != 0) {
846 		err_desc.serial = delete_id;
847 		if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
848 			warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
849 			     __func__);
850 			retval = 1;
851 		}
852 		goto bailout;
853 	}
854 
855 	if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
856 		warnx("%s: error injection command (-i) needed",
857 		      __func__);
858 		retval = 1;
859 		goto bailout;
860 	} else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
861 		&& (sense_len == 0)) {
862 		warnx("%s: custom error requires -s", __func__);
863 		retval = 1;
864 		goto bailout;
865 	}
866 
867 	if (continuous != 0)
868 		err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
869 
870 	/*
871 	 * If fd_sense is set, we need to read the sense data the user
872 	 * wants returned from stdin.
873 	 */
874         if (fd_sense == 1) {
875 		ssize_t amt_read;
876 		int amt_to_read = sense_len;
877 		u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
878 
879 		for (amt_read = 0; amt_to_read > 0;
880 		     amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
881 			if (amt_read == -1) {
882 				warn("error reading sense data from stdin");
883 				retval = 1;
884 				goto bailout;
885 			}
886 			amt_to_read -= amt_read;
887 			buf_ptr += amt_read;
888 		}
889 	}
890 
891 	if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
892 		warnx("%s: command pattern (-p) needed", __func__);
893 		retval = 1;
894 		goto bailout;
895 	}
896 
897 	if (len != 0) {
898 		err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
899 		/*
900 		 * We could check here to see whether it's a read/write
901 		 * command, but that will be pointless once we allow
902 		 * custom patterns.  At that point, the user could specify
903 		 * a READ(6) CDB type, and we wouldn't have an easy way here
904 		 * to verify whether range checking is possible there.  The
905 		 * user will just figure it out when his error never gets
906 		 * executed.
907 		 */
908 #if 0
909 		if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
910 			warnx("%s: need read and/or write pattern if range "
911 			      "is specified", __func__);
912 			retval = 1;
913 			goto bailout;
914 		}
915 #endif
916 		err_desc.lba_range.lba = lba;
917 		err_desc.lba_range.len = len;
918 	}
919 
920 	if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
921 		warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
922 		retval = 1;
923 	} else {
924 		printf("Error injection succeeded, serial number is %ju\n",
925 		       (uintmax_t)err_desc.serial);
926 	}
927 bailout:
928 
929 	return (retval);
930 }
931 
932 static int
933 cctl_lunlist(int fd)
934 {
935 	struct scsi_report_luns_data *lun_data;
936 	struct scsi_inquiry_data *inq_data;
937 	uint32_t num_luns;
938 	int initid;
939 	unsigned int i;
940 	int retval;
941 
942 	inq_data = NULL;
943 	initid = 7;
944 
945 	/*
946 	 * XXX KDM assuming LUN 0 is fine, but we may need to change this
947 	 * if we ever acquire the ability to have multiple targets.
948 	 */
949 	if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
950 				    /*retries*/ 2, &lun_data, &num_luns)) != 0)
951 		goto bailout;
952 
953 	inq_data = malloc(sizeof(*inq_data));
954 	if (inq_data == NULL) {
955 		warn("%s: couldn't allocate memory for inquiry data\n",
956 		     __func__);
957 		retval = 1;
958 		goto bailout;
959 	}
960 	for (i = 0; i < num_luns; i++) {
961 		char scsi_path[40];
962 		int lun_val;
963 
964 		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
965 		case RPL_LUNDATA_ATYP_PERIPH:
966 			lun_val = lun_data->luns[i].lundata[1];
967 			break;
968 		case RPL_LUNDATA_ATYP_FLAT:
969 			lun_val = (lun_data->luns[i].lundata[0] &
970 				RPL_LUNDATA_FLAT_LUN_MASK) |
971 				(lun_data->luns[i].lundata[1] <<
972 				RPL_LUNDATA_FLAT_LUN_BITS);
973 			break;
974 		case RPL_LUNDATA_ATYP_LUN:
975 		case RPL_LUNDATA_ATYP_EXTLUN:
976 		default:
977 			fprintf(stdout, "Unsupported LUN format %d\n",
978 				lun_data->luns[i].lundata[0] &
979 				RPL_LUNDATA_ATYP_MASK);
980 			lun_val = -1;
981 			break;
982 		}
983 		if (lun_val == -1)
984 			continue;
985 
986 		if ((retval = cctl_get_inquiry(fd, lun_val, initid,
987 					       /*retries*/ 2, scsi_path,
988 					       sizeof(scsi_path),
989 					       inq_data)) != 0) {
990 			goto bailout;
991 		}
992 		printf("%s", scsi_path);
993 		scsi_print_inquiry(inq_data);
994 	}
995 bailout:
996 
997 	if (lun_data != NULL)
998 		free(lun_data);
999 
1000 	if (inq_data != NULL)
1001 		free(inq_data);
1002 
1003 	return (retval);
1004 }
1005 
1006 static int
1007 cctl_sync_cache(int fd, int lun, int iid, int retries,
1008 		int argc, char **argv, char *combinedopt)
1009 {
1010 	union ctl_io *io;
1011 	int cdb_size = -1;
1012 	int retval;
1013 	uint64_t our_lba = 0;
1014 	uint32_t our_block_count = 0;
1015 	int reladr = 0, immed = 0;
1016 	int c;
1017 
1018 	retval = 0;
1019 
1020 	io = ctl_scsi_alloc_io(iid);
1021 	if (io == NULL) {
1022 		warnx("%s: can't allocate memory", __func__);
1023 		return (1);
1024 	}
1025 
1026 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1027 		switch (c) {
1028 		case 'b':
1029 			our_block_count = strtoul(optarg, NULL, 0);
1030 			break;
1031 		case 'c':
1032 			cdb_size = strtol(optarg, NULL, 0);
1033 			break;
1034 		case 'i':
1035 			immed = 1;
1036 			break;
1037 		case 'l':
1038 			our_lba = strtoull(optarg, NULL, 0);
1039 			break;
1040 		case 'r':
1041 			reladr = 1;
1042 			break;
1043 		default:
1044 			break;
1045 		}
1046 	}
1047 
1048 	if (cdb_size != -1) {
1049 		switch (cdb_size) {
1050 		case 10:
1051 		case 16:
1052 			break;
1053 		default:
1054 			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1055 			      "and 16", __func__, cdb_size);
1056 			retval = 1;
1057 			goto bailout;
1058 			break; /* NOTREACHED */
1059 		}
1060 	} else
1061 		cdb_size = 10;
1062 
1063 	ctl_scsi_sync_cache(/*io*/ io,
1064 			    /*immed*/ immed,
1065 			    /*reladr*/ reladr,
1066 			    /*minimum_cdb_size*/ cdb_size,
1067 			    /*starting_lba*/ our_lba,
1068 			    /*block_count*/ our_block_count,
1069 			    /*tag_type*/ CTL_TAG_SIMPLE,
1070 			    /*control*/ 0);
1071 
1072 	io->io_hdr.nexus.targ_lun = lun;
1073 	io->io_hdr.nexus.initid = iid;
1074 
1075 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1076 		retval = 1;
1077 		goto bailout;
1078 	}
1079 
1080 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1081 		fprintf(stdout, "Cache synchronized successfully\n");
1082 	} else
1083 		ctl_io_error_print(io, NULL, stderr);
1084 bailout:
1085 	ctl_scsi_free_io(io);
1086 
1087 	return (retval);
1088 }
1089 
1090 static int
1091 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1092 		int argc, char **argv, char *combinedopt)
1093 {
1094 	union ctl_io *io;
1095 	char scsi_path[40];
1096 	int immed = 0;
1097 	int retval, c;
1098 
1099 	retval = 0;
1100 
1101 	io = ctl_scsi_alloc_io(iid);
1102 	if (io == NULL) {
1103 		warnx("%s: can't allocate memory", __func__);
1104 		return (1);
1105 	}
1106 
1107 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1108 		switch (c) {
1109 		case 'i':
1110 			immed = 1;
1111 			break;
1112 		default:
1113 			break;
1114 		}
1115 	}
1116 	/*
1117 	 * Use an ordered tag for the stop command, to guarantee that any
1118 	 * pending I/O will finish before the stop command executes.  This
1119 	 * would normally be the case anyway, since CTL will basically
1120 	 * treat the start/stop command as an ordered command with respect
1121 	 * to any other command except an INQUIRY.  (See ctl_ser_table.c.)
1122 	 */
1123 	ctl_scsi_start_stop(/*io*/ io,
1124 			    /*start*/ start,
1125 			    /*load_eject*/ 0,
1126 			    /*immediate*/ immed,
1127 			    /*power_conditions*/ SSS_PC_START_VALID,
1128 			    /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1129 						     CTL_TAG_ORDERED,
1130 			    /*control*/ 0);
1131 
1132 	io->io_hdr.nexus.targ_lun = lun;
1133 	io->io_hdr.nexus.initid = iid;
1134 
1135 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1136 		retval = 1;
1137 		goto bailout;
1138 	}
1139 
1140 	ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1141 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1142 		fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1143 			(start) ?  "started" : "stopped");
1144 	} else
1145 		ctl_io_error_print(io, NULL, stderr);
1146 
1147 bailout:
1148 	ctl_scsi_free_io(io);
1149 
1150 	return (retval);
1151 }
1152 
1153 static int
1154 cctl_mode_sense(int fd, int lun, int iid, int retries,
1155 		int argc, char **argv, char *combinedopt)
1156 {
1157 	union ctl_io *io;
1158 	uint32_t datalen;
1159 	uint8_t *dataptr;
1160 	int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1161 	int list = 0;
1162 	int page_code = -1;
1163 	int c;
1164 
1165 	cdbsize = 0;
1166 	retval = 0;
1167 	dataptr = NULL;
1168 
1169 	io = ctl_scsi_alloc_io(iid);
1170 	if (io == NULL) {
1171 		warn("%s: can't allocate memory", __func__);
1172 		return (1);
1173 	}
1174 
1175 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1176 		switch (c) {
1177 		case 'P':
1178 			pc = strtoul(optarg, NULL, 0);
1179 			break;
1180 		case 'S':
1181 			subpage = strtoul(optarg, NULL, 0);
1182 			break;
1183 		case 'd':
1184 			dbd = 1;
1185 			break;
1186 		case 'l':
1187 			list = 1;
1188 			break;
1189 		case 'm':
1190 			page_code = strtoul(optarg, NULL, 0);
1191 			break;
1192 		case 'c':
1193 			cdbsize = strtol(optarg, NULL, 0);
1194 			break;
1195 		default:
1196 			break;
1197 		}
1198 	}
1199 
1200 	if (((list == 0) && (page_code == -1))
1201 	 || ((list != 0) && (page_code != -1))) {
1202 		warnx("%s: you must specify either a page code (-m) or -l",
1203 		      __func__);
1204 		retval = 1;
1205 		goto bailout;
1206 	}
1207 
1208 	if ((page_code != -1)
1209 	 && ((page_code > SMS_ALL_PAGES_PAGE)
1210 	  || (page_code < 0))) {
1211 		warnx("%s: page code %d is out of range", __func__,
1212 		      page_code);
1213 		retval = 1;
1214 		goto bailout;
1215 	}
1216 
1217 	if (list == 1) {
1218 		page_code = SMS_ALL_PAGES_PAGE;
1219 		if (pc != -1) {
1220 			warnx("%s: arg -P makes no sense with -l",
1221 			      __func__);
1222 			retval = 1;
1223 			goto bailout;
1224 		}
1225 		if (subpage != -1) {
1226 			warnx("%s: arg -S makes no sense with -l", __func__);
1227 			retval = 1;
1228 			goto bailout;
1229 		}
1230 	}
1231 
1232 	if (pc == -1)
1233 		pc = SMS_PAGE_CTRL_CURRENT;
1234 	else {
1235 		if ((pc > 3)
1236 		 || (pc < 0)) {
1237 			warnx("%s: page control value %d is out of range: 0-3",
1238 			      __func__, pc);
1239 			retval = 1;
1240 			goto bailout;
1241 		}
1242 	}
1243 
1244 
1245 	if ((subpage != -1)
1246 	 && ((subpage > 255)
1247 	  || (subpage < 0))) {
1248 		warnx("%s: subpage code %d is out of range: 0-255", __func__,
1249 		      subpage);
1250 		retval = 1;
1251 		goto bailout;
1252 	}
1253 	if (cdbsize != 0) {
1254 		switch (cdbsize) {
1255 		case 6:
1256 		case 10:
1257 			break;
1258 		default:
1259 			warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1260 			      "and 10", __func__, cdbsize);
1261 			retval = 1;
1262 			goto bailout;
1263 			break;
1264 		}
1265 	} else
1266 		cdbsize = 6;
1267 
1268 	if (subpage == -1)
1269 		subpage = 0;
1270 
1271 	if (cdbsize == 6)
1272 		datalen = 255;
1273 	else
1274 		datalen = 65535;
1275 
1276 	dataptr = (uint8_t *)malloc(datalen);
1277 	if (dataptr == NULL) {
1278 		warn("%s: can't allocate %d bytes", __func__, datalen);
1279 		retval = 1;
1280 		goto bailout;
1281 	}
1282 
1283 	memset(dataptr, 0, datalen);
1284 
1285 	ctl_scsi_mode_sense(io,
1286 			    /*data_ptr*/ dataptr,
1287 			    /*data_len*/ datalen,
1288 			    /*dbd*/ dbd,
1289 			    /*llbaa*/ 0,
1290 			    /*page_code*/ page_code,
1291 			    /*pc*/ pc << 6,
1292 			    /*subpage*/ subpage,
1293 			    /*minimum_cdb_size*/ cdbsize,
1294 			    /*tag_type*/ CTL_TAG_SIMPLE,
1295 			    /*control*/ 0);
1296 
1297 	io->io_hdr.nexus.targ_lun = lun;
1298 	io->io_hdr.nexus.initid = iid;
1299 
1300 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1301 		retval = 1;
1302 		goto bailout;
1303 	}
1304 
1305 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1306 		int pages_len, used_len;
1307 		uint32_t returned_len;
1308 		uint8_t *ndataptr;
1309 
1310 		if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1311 			struct scsi_mode_hdr_6 *hdr6;
1312 			int bdlen;
1313 
1314 			hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1315 
1316 			returned_len = hdr6->datalen + 1;
1317 			bdlen = hdr6->block_descr_len;
1318 
1319 			ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1320 		} else {
1321 			struct scsi_mode_hdr_10 *hdr10;
1322 			int bdlen;
1323 
1324 			hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1325 
1326 			returned_len = scsi_2btoul(hdr10->datalen) + 2;
1327 			bdlen = scsi_2btoul(hdr10->block_descr_len);
1328 
1329 			ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1330 		}
1331 		/* just in case they can give us more than we allocated for */
1332 		returned_len = min(returned_len, datalen);
1333 		pages_len = returned_len - (ndataptr - dataptr);
1334 #if 0
1335 		fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1336 			returned_len, pages_len);
1337 #endif
1338 		if (list == 1) {
1339 			fprintf(stdout, "Supported mode pages:\n");
1340 			for (used_len = 0; used_len < pages_len;) {
1341 				struct scsi_mode_page_header *header;
1342 
1343 				header = (struct scsi_mode_page_header *)
1344 					&ndataptr[used_len];
1345 				fprintf(stdout, "%d\n", header->page_code);
1346 				used_len += header->page_length + 2;
1347 			}
1348 		} else {
1349 			for (used_len = 0; used_len < pages_len; used_len++) {
1350 				fprintf(stdout, "0x%x ", ndataptr[used_len]);
1351 				if (((used_len+1) % 16) == 0)
1352 					fprintf(stdout, "\n");
1353 			}
1354 			fprintf(stdout, "\n");
1355 		}
1356 	} else
1357 		ctl_io_error_print(io, NULL, stderr);
1358 bailout:
1359 
1360 	ctl_scsi_free_io(io);
1361 
1362 	if (dataptr != NULL)
1363 		free(dataptr);
1364 
1365 	return (retval);
1366 }
1367 
1368 static int
1369 cctl_read_capacity(int fd, int lun, int iid, int retries,
1370 		   int argc, char **argv, char *combinedopt)
1371 {
1372 	union ctl_io *io;
1373 	struct scsi_read_capacity_data *data;
1374 	struct scsi_read_capacity_data_long *longdata;
1375 	int cdbsize = -1, retval;
1376 	uint8_t *dataptr;
1377 	int c;
1378 
1379 	cdbsize = 10;
1380 	dataptr = NULL;
1381 	retval = 0;
1382 
1383 	io = ctl_scsi_alloc_io(iid);
1384 	if (io == NULL) {
1385 		warn("%s: can't allocate memory\n", __func__);
1386 		return (1);
1387 	}
1388 
1389 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1390 		switch (c) {
1391 		case 'c':
1392 			cdbsize = strtol(optarg, NULL, 0);
1393 			break;
1394 		default:
1395 			break;
1396 		}
1397 	}
1398 	if (cdbsize != -1) {
1399 		switch (cdbsize) {
1400 		case 10:
1401 		case 16:
1402 			break;
1403 		default:
1404 			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1405 			      "and 16", __func__, cdbsize);
1406 			retval = 1;
1407 			goto bailout;
1408 			break; /* NOTREACHED */
1409 		}
1410 	} else
1411 		cdbsize = 10;
1412 
1413 	dataptr = (uint8_t *)malloc(sizeof(*longdata));
1414 	if (dataptr == NULL) {
1415 		warn("%s: can't allocate %zd bytes\n", __func__,
1416 		     sizeof(*longdata));
1417 		retval = 1;
1418 		goto bailout;
1419 	}
1420 	memset(dataptr, 0, sizeof(*longdata));
1421 
1422 retry:
1423 
1424 	switch (cdbsize) {
1425 	case 10:
1426 		ctl_scsi_read_capacity(io,
1427 				       /*data_ptr*/ dataptr,
1428 				       /*data_len*/ sizeof(*longdata),
1429 				       /*addr*/ 0,
1430 				       /*reladr*/ 0,
1431 				       /*pmi*/ 0,
1432 				       /*tag_type*/ CTL_TAG_SIMPLE,
1433 				       /*control*/ 0);
1434 		break;
1435 	case 16:
1436 		ctl_scsi_read_capacity_16(io,
1437 					  /*data_ptr*/ dataptr,
1438 					  /*data_len*/ sizeof(*longdata),
1439 					  /*addr*/ 0,
1440 					  /*reladr*/ 0,
1441 					  /*pmi*/ 0,
1442 					  /*tag_type*/ CTL_TAG_SIMPLE,
1443 					  /*control*/ 0);
1444 		break;
1445 	}
1446 
1447 	io->io_hdr.nexus.initid = iid;
1448 	io->io_hdr.nexus.targ_lun = lun;
1449 
1450 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1451 		retval = 1;
1452 		goto bailout;
1453 	}
1454 
1455 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1456 		uint64_t maxlba;
1457 		uint32_t blocksize;
1458 
1459 		if (cdbsize == 10) {
1460 
1461 			data = (struct scsi_read_capacity_data *)dataptr;
1462 
1463 			maxlba = scsi_4btoul(data->addr);
1464 			blocksize = scsi_4btoul(data->length);
1465 
1466 			if (maxlba == 0xffffffff) {
1467 				cdbsize = 16;
1468 				goto retry;
1469 			}
1470 		} else {
1471 			longdata=(struct scsi_read_capacity_data_long *)dataptr;
1472 
1473 			maxlba = scsi_8btou64(longdata->addr);
1474 			blocksize = scsi_4btoul(longdata->length);
1475 		}
1476 
1477 		fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1478 			(uintmax_t)maxlba, blocksize);
1479 	} else {
1480 		ctl_io_error_print(io, NULL, stderr);
1481 	}
1482 bailout:
1483 	ctl_scsi_free_io(io);
1484 
1485 	if (dataptr != NULL)
1486 		free(dataptr);
1487 
1488 	return (retval);
1489 }
1490 
1491 static int
1492 cctl_read_write(int fd, int lun, int iid, int retries,
1493 		int argc, char **argv, char *combinedopt,
1494 		ctladm_cmdfunction command)
1495 {
1496 	union ctl_io *io;
1497 	int file_fd, do_stdio;
1498 	int cdbsize = -1, databytes;
1499 	uint8_t *dataptr;
1500 	char *filename = NULL;
1501 	int datalen = -1, blocksize = -1;
1502 	uint64_t lba = 0;
1503 	int lba_set = 0;
1504 	int retval;
1505 	int c;
1506 
1507 	retval = 0;
1508 	do_stdio = 0;
1509 	dataptr = NULL;
1510 	file_fd = -1;
1511 
1512 	io = ctl_scsi_alloc_io(iid);
1513 	if (io == NULL) {
1514 		warn("%s: can't allocate memory\n", __func__);
1515 		return (1);
1516 	}
1517 
1518 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1519 		switch (c) {
1520 		case 'N':
1521 			io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1522 			break;
1523 		case 'b':
1524 			blocksize = strtoul(optarg, NULL, 0);
1525 			break;
1526 		case 'c':
1527 			cdbsize = strtoul(optarg, NULL, 0);
1528 			break;
1529 		case 'd':
1530 			datalen = strtoul(optarg, NULL, 0);
1531 			break;
1532 		case 'f':
1533 			filename = strdup(optarg);
1534 			break;
1535 		case 'l':
1536 			lba = strtoull(optarg, NULL, 0);
1537 			lba_set = 1;
1538 			break;
1539 		default:
1540 			break;
1541 		}
1542 	}
1543 	if (filename == NULL) {
1544 		warnx("%s: you must supply a filename using -f", __func__);
1545 		retval = 1;
1546 		goto bailout;
1547 	}
1548 
1549 	if (datalen == -1) {
1550 		warnx("%s: you must specify the data length with -d", __func__);
1551 		retval = 1;
1552 		goto bailout;
1553 	}
1554 
1555 	if (lba_set == 0) {
1556 		warnx("%s: you must specify the LBA with -l", __func__);
1557 		retval = 1;
1558 		goto bailout;
1559 	}
1560 
1561 	if (blocksize == -1) {
1562 		warnx("%s: you must specify the blocksize with -b", __func__);
1563 		retval = 1;
1564 		goto bailout;
1565 	}
1566 
1567 	if (cdbsize != -1) {
1568 		switch (cdbsize) {
1569 		case 6:
1570 		case 10:
1571 		case 12:
1572 		case 16:
1573 			break;
1574 		default:
1575 			warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1576 			      "10, 12 or 16", __func__, cdbsize);
1577 			retval = 1;
1578 			goto bailout;
1579 			break; /* NOTREACHED */
1580 		}
1581 	} else
1582 		cdbsize = 6;
1583 
1584 	databytes = datalen * blocksize;
1585 	dataptr = (uint8_t *)malloc(databytes);
1586 
1587 	if (dataptr == NULL) {
1588 		warn("%s: can't allocate %d bytes\n", __func__, databytes);
1589 		retval = 1;
1590 		goto bailout;
1591 	}
1592 	if (strcmp(filename, "-") == 0) {
1593 		if (command == CTLADM_CMD_READ)
1594 			file_fd = STDOUT_FILENO;
1595 		else
1596 			file_fd = STDIN_FILENO;
1597 		do_stdio = 1;
1598 	} else {
1599 		file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1600 		if (file_fd == -1) {
1601 			warn("%s: can't open file %s", __func__, filename);
1602 			retval = 1;
1603 			goto bailout;
1604 		}
1605 	}
1606 
1607 	memset(dataptr, 0, databytes);
1608 
1609 	if (command == CTLADM_CMD_WRITE) {
1610 		int bytes_read;
1611 
1612 		bytes_read = read(file_fd, dataptr, databytes);
1613 		if (bytes_read == -1) {
1614 			warn("%s: error reading file %s", __func__, filename);
1615 			retval = 1;
1616 			goto bailout;
1617 		}
1618 		if (bytes_read != databytes) {
1619 			warnx("%s: only read %d bytes from file %s",
1620 			      __func__, bytes_read, filename);
1621 			retval = 1;
1622 			goto bailout;
1623 		}
1624 	}
1625 	ctl_scsi_read_write(io,
1626 			    /*data_ptr*/ dataptr,
1627 			    /*data_len*/ databytes,
1628 			    /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1629 			    /*byte2*/ 0,
1630 			    /*minimum_cdb_size*/ cdbsize,
1631 			    /*lba*/ lba,
1632 			    /*num_blocks*/ datalen,
1633 			    /*tag_type*/ CTL_TAG_SIMPLE,
1634 			    /*control*/ 0);
1635 
1636 	io->io_hdr.nexus.targ_lun = lun;
1637 	io->io_hdr.nexus.initid = iid;
1638 
1639 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1640 		retval = 1;
1641 		goto bailout;
1642 	}
1643 
1644 	if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1645 	 && (command == CTLADM_CMD_READ)) {
1646 		int bytes_written;
1647 
1648 		bytes_written = write(file_fd, dataptr, databytes);
1649 		if (bytes_written == -1) {
1650 			warn("%s: can't write to %s", __func__, filename);
1651 			goto bailout;
1652 		}
1653 	} else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1654 		ctl_io_error_print(io, NULL, stderr);
1655 
1656 
1657 bailout:
1658 
1659 	ctl_scsi_free_io(io);
1660 
1661 	if (dataptr != NULL)
1662 		free(dataptr);
1663 
1664 	if ((do_stdio == 0)
1665 	 && (file_fd != -1))
1666 		close(file_fd);
1667 
1668 	return (retval);
1669 }
1670 
1671 static int
1672 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1673 	      scsi_report_luns_data **lun_data, uint32_t *num_luns)
1674 {
1675 	union ctl_io *io;
1676 	uint32_t nluns;
1677 	int lun_datalen;
1678 	int retval;
1679 
1680 	retval = 0;
1681 
1682 	io = ctl_scsi_alloc_io(iid);
1683 	if (io == NULL) {
1684 		warnx("%s: can't allocate memory", __func__);
1685 		return (1);
1686 	}
1687 
1688 	/*
1689 	 * lun_data includes space for 1 lun, allocate space for 4 initially.
1690 	 * If that isn't enough, we'll allocate more.
1691 	 */
1692 	nluns = 4;
1693 retry:
1694 	lun_datalen = sizeof(*lun_data) +
1695 		(nluns * sizeof(struct scsi_report_luns_lundata));
1696 	*lun_data = malloc(lun_datalen);
1697 
1698 	if (*lun_data == NULL) {
1699 		warnx("%s: can't allocate memory", __func__);
1700 		ctl_scsi_free_io(io);
1701 		return (1);
1702 	}
1703 
1704 	ctl_scsi_report_luns(io,
1705 			     /*data_ptr*/ (uint8_t *)*lun_data,
1706 			     /*data_len*/ lun_datalen,
1707 			     /*select_report*/ RPL_REPORT_ALL,
1708 			     /*tag_type*/ CTL_TAG_SIMPLE,
1709 			     /*control*/ 0);
1710 
1711 	io->io_hdr.nexus.initid = iid;
1712 	io->io_hdr.nexus.targ_lun = lun;
1713 
1714 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1715 		retval = 1;
1716 		goto bailout;
1717 	}
1718 
1719 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1720 		uint32_t returned_len, returned_luns;
1721 
1722 		returned_len = scsi_4btoul((*lun_data)->length);
1723 		returned_luns = returned_len / 8;
1724 		if (returned_luns > nluns) {
1725 			nluns = returned_luns;
1726 			free(*lun_data);
1727 			goto retry;
1728 		}
1729 		/* These should be the same */
1730 		*num_luns = MIN(returned_luns, nluns);
1731 	} else {
1732 		ctl_io_error_print(io, NULL, stderr);
1733 		retval = 1;
1734 	}
1735 bailout:
1736 	ctl_scsi_free_io(io);
1737 
1738 	return (retval);
1739 }
1740 
1741 static int
1742 cctl_report_luns(int fd, int lun, int iid, int retries)
1743 {
1744 	struct scsi_report_luns_data *lun_data;
1745 	uint32_t num_luns, i;
1746 	int retval;
1747 
1748 	lun_data = NULL;
1749 
1750 	if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1751 				   &num_luns)) != 0)
1752 		goto bailout;
1753 
1754 	fprintf(stdout, "%u LUNs returned\n", num_luns);
1755 	for (i = 0; i < num_luns; i++) {
1756 		int lun_val;
1757 
1758 		/*
1759 		 * XXX KDM figure out a way to share this code with
1760 		 * cctl_lunlist()?
1761 		 */
1762 		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1763 		case RPL_LUNDATA_ATYP_PERIPH:
1764 			lun_val = lun_data->luns[i].lundata[1];
1765 			break;
1766 		case RPL_LUNDATA_ATYP_FLAT:
1767 			lun_val = (lun_data->luns[i].lundata[0] &
1768 				RPL_LUNDATA_FLAT_LUN_MASK) |
1769 				(lun_data->luns[i].lundata[1] <<
1770 				RPL_LUNDATA_FLAT_LUN_BITS);
1771 			break;
1772 		case RPL_LUNDATA_ATYP_LUN:
1773 		case RPL_LUNDATA_ATYP_EXTLUN:
1774 		default:
1775 			fprintf(stdout, "Unsupported LUN format %d\n",
1776 				lun_data->luns[i].lundata[0] &
1777 				RPL_LUNDATA_ATYP_MASK);
1778 			lun_val = -1;
1779 			break;
1780 		}
1781 		if (lun_val == -1)
1782 			continue;
1783 
1784 		fprintf(stdout, "%d\n", lun_val);
1785 	}
1786 
1787 bailout:
1788 	if (lun_data != NULL)
1789 		free(lun_data);
1790 
1791 	return (retval);
1792 }
1793 
1794 static int
1795 cctl_tur(int fd, int lun, int iid, int retries)
1796 {
1797 	union ctl_io *io;
1798 
1799 	io = ctl_scsi_alloc_io(iid);
1800 	if (io == NULL) {
1801 		fprintf(stderr, "can't allocate memory\n");
1802 		return (1);
1803 	}
1804 
1805 	ctl_scsi_tur(io,
1806 		     /* tag_type */ CTL_TAG_SIMPLE,
1807 		     /* control */ 0);
1808 
1809 	io->io_hdr.nexus.targ_lun = lun;
1810 	io->io_hdr.nexus.initid = iid;
1811 
1812 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1813 		ctl_scsi_free_io(io);
1814 		return (1);
1815 	}
1816 
1817 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1818 		fprintf(stdout, "Unit is ready\n");
1819 	else
1820 		ctl_io_error_print(io, NULL, stderr);
1821 
1822 	return (0);
1823 }
1824 
1825 static int
1826 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1827 		 char *path_str, int path_len,
1828 		 struct scsi_inquiry_data *inq_data)
1829 {
1830 	union ctl_io *io;
1831 	int retval;
1832 
1833 	retval = 0;
1834 
1835 	io = ctl_scsi_alloc_io(iid);
1836 	if (io == NULL) {
1837 		warnx("cctl_inquiry: can't allocate memory\n");
1838 		return (1);
1839 	}
1840 
1841 	ctl_scsi_inquiry(/*io*/ io,
1842 			 /*data_ptr*/ (uint8_t *)inq_data,
1843 			 /*data_len*/ sizeof(*inq_data),
1844 			 /*byte2*/ 0,
1845 			 /*page_code*/ 0,
1846 			 /*tag_type*/ CTL_TAG_SIMPLE,
1847 			 /*control*/ 0);
1848 
1849 	io->io_hdr.nexus.targ_lun = lun;
1850 	io->io_hdr.nexus.initid = iid;
1851 
1852 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1853 		retval = 1;
1854 		goto bailout;
1855 	}
1856 
1857 	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1858 		retval = 1;
1859 		ctl_io_error_print(io, NULL, stderr);
1860 	} else if (path_str != NULL)
1861 		ctl_scsi_path_string(io, path_str, path_len);
1862 
1863 bailout:
1864 	ctl_scsi_free_io(io);
1865 
1866 	return (retval);
1867 }
1868 
1869 static int
1870 cctl_inquiry(int fd, int lun, int iid, int retries)
1871 {
1872 	struct scsi_inquiry_data *inq_data;
1873 	char scsi_path[40];
1874 	int retval;
1875 
1876 	inq_data = malloc(sizeof(*inq_data));
1877 	if (inq_data == NULL) {
1878 		warnx("%s: can't allocate inquiry data", __func__);
1879 		retval = 1;
1880 		goto bailout;
1881 	}
1882 
1883 	if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1884 				       sizeof(scsi_path), inq_data)) != 0)
1885 		goto bailout;
1886 
1887 	printf("%s", scsi_path);
1888 	scsi_print_inquiry(inq_data);
1889 
1890 bailout:
1891 	if (inq_data != NULL)
1892 		free(inq_data);
1893 
1894 	return (retval);
1895 }
1896 
1897 static int
1898 cctl_req_sense(int fd, int lun, int iid, int retries)
1899 {
1900 	union ctl_io *io;
1901 	struct scsi_sense_data *sense_data;
1902 	int retval;
1903 
1904 	retval = 0;
1905 
1906 	io = ctl_scsi_alloc_io(iid);
1907 	if (io == NULL) {
1908 		warnx("cctl_req_sense: can't allocate memory\n");
1909 		return (1);
1910 	}
1911 	sense_data = malloc(sizeof(*sense_data));
1912 	memset(sense_data, 0, sizeof(*sense_data));
1913 
1914 	ctl_scsi_request_sense(/*io*/ io,
1915 			       /*data_ptr*/ (uint8_t *)sense_data,
1916 			       /*data_len*/ sizeof(*sense_data),
1917 			       /*byte2*/ 0,
1918 			       /*tag_type*/ CTL_TAG_SIMPLE,
1919 			       /*control*/ 0);
1920 
1921 	io->io_hdr.nexus.targ_lun = lun;
1922 	io->io_hdr.nexus.initid = iid;
1923 
1924 	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1925 		retval = 1;
1926 		goto bailout;
1927 	}
1928 
1929 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1930 		bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
1931 		io->scsiio.sense_len = sizeof(*sense_data);
1932 		ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
1933 	} else
1934 		ctl_io_error_print(io, NULL, stderr);
1935 
1936 bailout:
1937 
1938 	ctl_scsi_free_io(io);
1939 	free(sense_data);
1940 
1941 	return (retval);
1942 }
1943 
1944 static int
1945 cctl_report_target_port_group(int fd, int lun, int iid)
1946 {
1947 	union ctl_io *io;
1948 	uint32_t datalen;
1949 	uint8_t *dataptr;
1950 	int retval;
1951 
1952 	dataptr = NULL;
1953 	retval = 0;
1954 
1955 	io = ctl_scsi_alloc_io(iid);
1956 	if (io == NULL) {
1957 		warn("%s: can't allocate memory", __func__);
1958 		return (1);
1959 	}
1960 
1961 	datalen = 64;
1962 	dataptr = (uint8_t *)malloc(datalen);
1963 	if (dataptr == NULL) {
1964 		warn("%s: can't allocate %d bytes", __func__, datalen);
1965 		retval = 1;
1966 		goto bailout;
1967 	}
1968 
1969 	memset(dataptr, 0, datalen);
1970 
1971 	ctl_scsi_maintenance_in(/*io*/ io,
1972 				/*data_ptr*/ dataptr,
1973 				/*data_len*/ datalen,
1974 				/*action*/ SA_RPRT_TRGT_GRP,
1975 				/*tag_type*/ CTL_TAG_SIMPLE,
1976 				/*control*/ 0);
1977 
1978 	io->io_hdr.nexus.targ_lun = lun;
1979 	io->io_hdr.nexus.initid = iid;
1980 
1981 	if (cctl_do_io(fd, 0, io, __func__) != 0) {
1982 		retval = 1;
1983 		goto bailout;
1984 	}
1985 
1986 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1987 		int returned_len, used_len;
1988 
1989 		returned_len = scsi_4btoul(&dataptr[0]) + 4;
1990 
1991 		for (used_len = 0; used_len < returned_len; used_len++) {
1992 			fprintf(stdout, "0x%02x ", dataptr[used_len]);
1993 			if (((used_len+1) % 8) == 0)
1994 				fprintf(stdout, "\n");
1995 		}
1996 		fprintf(stdout, "\n");
1997 	} else
1998 		ctl_io_error_print(io, NULL, stderr);
1999 
2000 bailout:
2001 	ctl_scsi_free_io(io);
2002 
2003 	if (dataptr != NULL)
2004 		free(dataptr);
2005 
2006 	return (retval);
2007 }
2008 
2009 static int
2010 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2011 {
2012 	union ctl_io *io;
2013 	uint32_t datalen;
2014 	uint8_t *dataptr;
2015 	int retval;
2016 
2017 	retval = 0;
2018 	dataptr = NULL;
2019 
2020 	io = ctl_scsi_alloc_io(iid);
2021 	if (io == NULL) {
2022 		warn("%s: can't allocate memory", __func__);
2023 		return (1);
2024 	}
2025 
2026 	datalen = 256;
2027 	dataptr = (uint8_t *)malloc(datalen);
2028 	if (dataptr == NULL) {
2029 		warn("%s: can't allocate %d bytes", __func__, datalen);
2030 		retval = 1;
2031 		goto bailout;
2032 	}
2033 
2034 	memset(dataptr, 0, datalen);
2035 
2036 	ctl_scsi_inquiry(/*io*/        io,
2037 			 /*data_ptr*/  dataptr,
2038 			 /*data_len*/  datalen,
2039 			 /*byte2*/     SI_EVPD,
2040 			 /*page_code*/ SVPD_DEVICE_ID,
2041 			 /*tag_type*/  CTL_TAG_SIMPLE,
2042 			 /*control*/   0);
2043 
2044 	io->io_hdr.nexus.targ_lun = lun;
2045 	io->io_hdr.nexus.initid = iid;
2046 
2047 	if (cctl_do_io(fd, 0, io, __func__) != 0) {
2048 		retval = 1;
2049 		goto bailout;
2050 	}
2051 
2052 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2053 		int returned_len, used_len;
2054 
2055 		returned_len = scsi_2btoul(&dataptr[2]) + 4;
2056 
2057 		for (used_len = 0; used_len < returned_len; used_len++) {
2058 			fprintf(stdout, "0x%02x ", dataptr[used_len]);
2059 			if (((used_len+1) % 8) == 0)
2060 				fprintf(stdout, "\n");
2061 		}
2062 		fprintf(stdout, "\n");
2063 	} else
2064 		ctl_io_error_print(io, NULL, stderr);
2065 
2066 bailout:
2067 	ctl_scsi_free_io(io);
2068 
2069 	if (dataptr != NULL)
2070 		free(dataptr);
2071 
2072 	return (retval);
2073 }
2074 
2075 static int
2076 cctl_persistent_reserve_in(int fd, int lun, int iid,
2077                            int argc, char **argv, char *combinedopt,
2078 			   int retry_count)
2079 {
2080 	union ctl_io *io;
2081 	uint32_t datalen;
2082 	uint8_t *dataptr;
2083 	int action = -1;
2084 	int retval;
2085 	int c;
2086 
2087 	retval = 0;
2088 	dataptr = NULL;
2089 
2090 	io = ctl_scsi_alloc_io(iid);
2091 	if (io == NULL) {
2092 		warn("%s: can't allocate memory", __func__);
2093 		return (1);
2094 	}
2095 
2096 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2097 		switch (c) {
2098 		case 'a':
2099 			action = strtol(optarg, NULL, 0);
2100 			break;
2101 		default:
2102 			break;
2103 		}
2104 	}
2105 
2106 	if (action < 0 || action > 2) {
2107 		warn("action must be specified and in the range: 0-2");
2108 		retval = 1;
2109 		goto bailout;
2110 	}
2111 
2112 
2113 	datalen = 256;
2114 	dataptr = (uint8_t *)malloc(datalen);
2115 	if (dataptr == NULL) {
2116 		warn("%s: can't allocate %d bytes", __func__, datalen);
2117 		retval = 1;
2118 		goto bailout;
2119 	}
2120 
2121 	memset(dataptr, 0, datalen);
2122 
2123 	ctl_scsi_persistent_res_in(io,
2124 				   /*data_ptr*/ dataptr,
2125 				   /*data_len*/ datalen,
2126 				   /*action*/   action,
2127 				   /*tag_type*/ CTL_TAG_SIMPLE,
2128 				   /*control*/  0);
2129 
2130 	io->io_hdr.nexus.targ_lun = lun;
2131 	io->io_hdr.nexus.initid = iid;
2132 
2133 	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2134 		retval = 1;
2135 		goto bailout;
2136 	}
2137 
2138 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2139 		int returned_len, used_len;
2140 
2141 		switch (action) {
2142 		case 0:
2143 			returned_len = scsi_4btoul(&dataptr[4]) + 8;
2144 			returned_len = min(returned_len, 256);
2145 			break;
2146 		case 1:
2147 			returned_len = scsi_4btoul(&dataptr[4]) + 8;
2148 			break;
2149 		case 2:
2150 			returned_len = 8;
2151 			break;
2152 		default:
2153 			warnx("%s: invalid action %d", __func__, action);
2154 			goto bailout;
2155 			break; /* NOTREACHED */
2156 		}
2157 
2158 		for (used_len = 0; used_len < returned_len; used_len++) {
2159 			fprintf(stdout, "0x%02x ", dataptr[used_len]);
2160 			if (((used_len+1) % 8) == 0)
2161 				fprintf(stdout, "\n");
2162 		}
2163 		fprintf(stdout, "\n");
2164 	} else
2165 		ctl_io_error_print(io, NULL, stderr);
2166 
2167 bailout:
2168 	ctl_scsi_free_io(io);
2169 
2170 	if (dataptr != NULL)
2171 		free(dataptr);
2172 
2173 	return (retval);
2174 }
2175 
2176 static int
2177 cctl_persistent_reserve_out(int fd, int lun, int iid,
2178 			    int argc, char **argv, char *combinedopt,
2179 			    int retry_count)
2180 {
2181 	union ctl_io *io;
2182 	uint32_t datalen;
2183 	uint64_t key = 0, sa_key = 0;
2184 	int action = -1, restype = -1;
2185 	uint8_t *dataptr;
2186 	int retval;
2187 	int c;
2188 
2189 	retval = 0;
2190 	dataptr = NULL;
2191 
2192 	io = ctl_scsi_alloc_io(iid);
2193 	if (io == NULL) {
2194 		warn("%s: can't allocate memory", __func__);
2195 		return (1);
2196 	}
2197 
2198 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2199 		switch (c) {
2200 		case 'a':
2201 			action = strtol(optarg, NULL, 0);
2202 			break;
2203 		case 'k':
2204 			key = strtoull(optarg, NULL, 0);
2205 			break;
2206 		case 'r':
2207 			restype = strtol(optarg, NULL, 0);
2208 			break;
2209 		case 's':
2210 			sa_key = strtoull(optarg, NULL, 0);
2211 			break;
2212 		default:
2213 			break;
2214 		}
2215 	}
2216 	if (action < 0 || action > 5) {
2217 		warn("action must be specified and in the range: 0-5");
2218 		retval = 1;
2219 		goto bailout;
2220 	}
2221 
2222 	if (restype < 0 || restype > 5) {
2223 		if (action != 0 && action != 5 && action != 3) {
2224 			warn("'restype' must specified and in the range: 0-5");
2225 			retval = 1;
2226 			goto bailout;
2227 		}
2228 	}
2229 
2230 	datalen = 24;
2231 	dataptr = (uint8_t *)malloc(datalen);
2232 	if (dataptr == NULL) {
2233 		warn("%s: can't allocate %d bytes", __func__, datalen);
2234 		retval = 1;
2235 		goto bailout;
2236 	}
2237 
2238 	memset(dataptr, 0, datalen);
2239 
2240 	ctl_scsi_persistent_res_out(io,
2241 				    /*data_ptr*/ dataptr,
2242 				    /*data_len*/ datalen,
2243 				    /*action*/   action,
2244 				    /*type*/     restype,
2245 				    /*key*/      key,
2246 				    /*sa key*/   sa_key,
2247 				    /*tag_type*/ CTL_TAG_SIMPLE,
2248 				    /*control*/  0);
2249 
2250 	io->io_hdr.nexus.targ_lun = lun;
2251 	io->io_hdr.nexus.initid = iid;
2252 
2253 	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2254 		retval = 1;
2255 		goto bailout;
2256 	}
2257 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2258 		char scsi_path[40];
2259 		ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2260 		fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2261 			"successfully\n", scsi_path);
2262 	} else
2263 		ctl_io_error_print(io, NULL, stderr);
2264 
2265 bailout:
2266 	ctl_scsi_free_io(io);
2267 
2268 	if (dataptr != NULL)
2269 		free(dataptr);
2270 
2271 	return (retval);
2272 }
2273 
2274 struct cctl_req_option {
2275 	char			     *name;
2276 	int			      namelen;
2277 	char			     *value;
2278 	int			      vallen;
2279 	STAILQ_ENTRY(cctl_req_option) links;
2280 };
2281 
2282 static int
2283 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2284 {
2285 	struct ctl_lun_req req;
2286 	int device_type = -1;
2287 	uint64_t lun_size = 0;
2288 	uint32_t blocksize = 0, req_lun_id = 0;
2289 	char *serial_num = NULL;
2290 	char *device_id = NULL;
2291 	int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2292 	char *backend_name = NULL;
2293 	STAILQ_HEAD(, cctl_req_option) option_list;
2294 	int num_options = 0;
2295 	int retval = 0, c;
2296 
2297 	STAILQ_INIT(&option_list);
2298 
2299 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2300 		switch (c) {
2301 		case 'b':
2302 			backend_name = strdup(optarg);
2303 			break;
2304 		case 'B':
2305 			blocksize = strtoul(optarg, NULL, 0);
2306 			blocksize_set = 1;
2307 			break;
2308 		case 'd':
2309 			device_id = strdup(optarg);
2310 			break;
2311 		case 'l':
2312 			req_lun_id = strtoul(optarg, NULL, 0);
2313 			lun_id_set = 1;
2314 			break;
2315 		case 'o': {
2316 			struct cctl_req_option *option;
2317 			char *tmpstr;
2318 			char *name, *value;
2319 
2320 			tmpstr = strdup(optarg);
2321 			name = strsep(&tmpstr, "=");
2322 			if (name == NULL) {
2323 				warnx("%s: option -o takes \"name=value\""
2324 				      "argument", __func__);
2325 				retval = 1;
2326 				goto bailout;
2327 			}
2328 			value = strsep(&tmpstr, "=");
2329 			if (value == NULL) {
2330 				warnx("%s: option -o takes \"name=value\""
2331 				      "argument", __func__);
2332 				retval = 1;
2333 				goto bailout;
2334 			}
2335 			option = malloc(sizeof(*option));
2336 			if (option == NULL) {
2337 				warn("%s: error allocating %zd bytes",
2338 				     __func__, sizeof(*option));
2339 				retval = 1;
2340 				goto bailout;
2341 			}
2342 			option->name = strdup(name);
2343 			option->namelen = strlen(name) + 1;
2344 			option->value = strdup(value);
2345 			option->vallen = strlen(value) + 1;
2346 			free(tmpstr);
2347 
2348 			STAILQ_INSERT_TAIL(&option_list, option, links);
2349 			num_options++;
2350 			break;
2351 		}
2352 		case 's':
2353 			if (strcasecmp(optarg, "auto") != 0) {
2354 				retval = expand_number(optarg, &lun_size);
2355 				if (retval != 0) {
2356 					warn("%s: invalid -s argument",
2357 					    __func__);
2358 					retval = 1;
2359 					goto bailout;
2360 				}
2361 			}
2362 			lun_size_set = 1;
2363 			break;
2364 		case 'S':
2365 			serial_num = strdup(optarg);
2366 			break;
2367 		case 't':
2368 			device_type = strtoul(optarg, NULL, 0);
2369 			break;
2370 		default:
2371 			break;
2372 		}
2373 	}
2374 
2375 	if (backend_name == NULL) {
2376 		warnx("%s: backend name (-b) must be specified", __func__);
2377 		retval = 1;
2378 		goto bailout;
2379 	}
2380 
2381 	bzero(&req, sizeof(req));
2382 
2383 	strlcpy(req.backend, backend_name, sizeof(req.backend));
2384 	req.reqtype = CTL_LUNREQ_CREATE;
2385 
2386 	if (blocksize_set != 0)
2387 		req.reqdata.create.blocksize_bytes = blocksize;
2388 
2389 	if (lun_size_set != 0)
2390 		req.reqdata.create.lun_size_bytes = lun_size;
2391 
2392 	if (lun_id_set != 0) {
2393 		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2394 		req.reqdata.create.req_lun_id = req_lun_id;
2395 	}
2396 
2397 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2398 
2399 	if (device_type != -1)
2400 		req.reqdata.create.device_type = device_type;
2401 	else
2402 		req.reqdata.create.device_type = T_DIRECT;
2403 
2404 	if (serial_num != NULL) {
2405 		strlcpy(req.reqdata.create.serial_num, serial_num,
2406 			sizeof(req.reqdata.create.serial_num));
2407 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2408 	}
2409 
2410 	if (device_id != NULL) {
2411 		strlcpy(req.reqdata.create.device_id, device_id,
2412 			sizeof(req.reqdata.create.device_id));
2413 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2414 	}
2415 
2416 	req.num_be_args = num_options;
2417 	if (num_options > 0) {
2418 		struct cctl_req_option *option, *next_option;
2419 		int i;
2420 
2421 		req.be_args = malloc(num_options * sizeof(*req.be_args));
2422 		if (req.be_args == NULL) {
2423 			warn("%s: error allocating %zd bytes", __func__,
2424 			     num_options * sizeof(*req.be_args));
2425 			retval = 1;
2426 			goto bailout;
2427 		}
2428 
2429 		for (i = 0, option = STAILQ_FIRST(&option_list);
2430 		     i < num_options; i++, option = next_option) {
2431 			next_option = STAILQ_NEXT(option, links);
2432 
2433 			req.be_args[i].namelen = option->namelen;
2434 			req.be_args[i].name = strdup(option->name);
2435 			req.be_args[i].vallen = option->vallen;
2436 			req.be_args[i].value = strdup(option->value);
2437 			/*
2438 			 * XXX KDM do we want a way to specify a writeable
2439 			 * flag of some sort?  Do we want a way to specify
2440 			 * binary data?
2441 			 */
2442 			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2443 
2444 			STAILQ_REMOVE(&option_list, option, cctl_req_option,
2445 				      links);
2446 			free(option->name);
2447 			free(option->value);
2448 			free(option);
2449 		}
2450 	}
2451 
2452 	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2453 		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2454 		retval = 1;
2455 		goto bailout;
2456 	}
2457 
2458 	switch (req.status) {
2459 	case CTL_LUN_ERROR:
2460 		warnx("LUN creation error: %s", req.error_str);
2461 		retval = 1;
2462 		goto bailout;
2463 	case CTL_LUN_WARNING:
2464 		warnx("LUN creation warning: %s", req.error_str);
2465 		break;
2466 	case CTL_LUN_OK:
2467 		break;
2468 	default:
2469 		warnx("unknown LUN creation status: %d", req.status);
2470 		retval = 1;
2471 		goto bailout;
2472 	}
2473 
2474 	fprintf(stdout, "LUN created successfully\n");
2475 	fprintf(stdout, "backend:       %s\n", req.backend);
2476 	fprintf(stdout, "device type:   %d\n",req.reqdata.create.device_type);
2477 	fprintf(stdout, "LUN size:      %ju bytes\n",
2478 		(uintmax_t)req.reqdata.create.lun_size_bytes);
2479 	fprintf(stdout, "blocksize      %u bytes\n",
2480 		req.reqdata.create.blocksize_bytes);
2481 	fprintf(stdout, "LUN ID:        %d\n", req.reqdata.create.req_lun_id);
2482 	fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2483 	fprintf(stdout, "Device ID;     %s\n", req.reqdata.create.device_id);
2484 
2485 bailout:
2486 	return (retval);
2487 }
2488 
2489 static int
2490 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2491 {
2492 	struct ctl_lun_req req;
2493 	uint32_t lun_id = 0;
2494 	int lun_id_set = 0;
2495 	char *backend_name = NULL;
2496 	STAILQ_HEAD(, cctl_req_option) option_list;
2497 	int num_options = 0;
2498 	int retval = 0, c;
2499 
2500 	STAILQ_INIT(&option_list);
2501 
2502 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2503 		switch (c) {
2504 		case 'b':
2505 			backend_name = strdup(optarg);
2506 			break;
2507 		case 'l':
2508 			lun_id = strtoul(optarg, NULL, 0);
2509 			lun_id_set = 1;
2510 			break;
2511 		case 'o': {
2512 			struct cctl_req_option *option;
2513 			char *tmpstr;
2514 			char *name, *value;
2515 
2516 			tmpstr = strdup(optarg);
2517 			name = strsep(&tmpstr, "=");
2518 			if (name == NULL) {
2519 				warnx("%s: option -o takes \"name=value\""
2520 				      "argument", __func__);
2521 				retval = 1;
2522 				goto bailout;
2523 			}
2524 			value = strsep(&tmpstr, "=");
2525 			if (value == NULL) {
2526 				warnx("%s: option -o takes \"name=value\""
2527 				      "argument", __func__);
2528 				retval = 1;
2529 				goto bailout;
2530 			}
2531 			option = malloc(sizeof(*option));
2532 			if (option == NULL) {
2533 				warn("%s: error allocating %zd bytes",
2534 				     __func__, sizeof(*option));
2535 				retval = 1;
2536 				goto bailout;
2537 			}
2538 			option->name = strdup(name);
2539 			option->namelen = strlen(name) + 1;
2540 			option->value = strdup(value);
2541 			option->vallen = strlen(value) + 1;
2542 			free(tmpstr);
2543 
2544 			STAILQ_INSERT_TAIL(&option_list, option, links);
2545 			num_options++;
2546 			break;
2547 		}
2548 		default:
2549 			break;
2550 		}
2551 	}
2552 
2553 	if (backend_name == NULL)
2554 		errx(1, "%s: backend name (-b) must be specified", __func__);
2555 
2556 	if (lun_id_set == 0)
2557 		errx(1, "%s: LUN id (-l) must be specified", __func__);
2558 
2559 	bzero(&req, sizeof(req));
2560 
2561 	strlcpy(req.backend, backend_name, sizeof(req.backend));
2562 	req.reqtype = CTL_LUNREQ_RM;
2563 
2564 	req.reqdata.rm.lun_id = lun_id;
2565 
2566 	req.num_be_args = num_options;
2567 	if (num_options > 0) {
2568 		struct cctl_req_option *option, *next_option;
2569 		int i;
2570 
2571 		req.be_args = malloc(num_options * sizeof(*req.be_args));
2572 		if (req.be_args == NULL) {
2573 			warn("%s: error allocating %zd bytes", __func__,
2574 			     num_options * sizeof(*req.be_args));
2575 			retval = 1;
2576 			goto bailout;
2577 		}
2578 
2579 		for (i = 0, option = STAILQ_FIRST(&option_list);
2580 		     i < num_options; i++, option = next_option) {
2581 			next_option = STAILQ_NEXT(option, links);
2582 
2583 			req.be_args[i].namelen = option->namelen;
2584 			req.be_args[i].name = strdup(option->name);
2585 			req.be_args[i].vallen = option->vallen;
2586 			req.be_args[i].value = strdup(option->value);
2587 			/*
2588 			 * XXX KDM do we want a way to specify a writeable
2589 			 * flag of some sort?  Do we want a way to specify
2590 			 * binary data?
2591 			 */
2592 			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2593 
2594 			STAILQ_REMOVE(&option_list, option, cctl_req_option,
2595 				      links);
2596 			free(option->name);
2597 			free(option->value);
2598 			free(option);
2599 		}
2600 	}
2601 
2602 	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2603 		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2604 		retval = 1;
2605 		goto bailout;
2606 	}
2607 
2608 	switch (req.status) {
2609 	case CTL_LUN_ERROR:
2610 		warnx("LUN removal error: %s", req.error_str);
2611 		retval = 1;
2612 		goto bailout;
2613 	case CTL_LUN_WARNING:
2614 		warnx("LUN removal warning: %s", req.error_str);
2615 		break;
2616 	case CTL_LUN_OK:
2617 		break;
2618 	default:
2619 		warnx("unknown LUN removal status: %d", req.status);
2620 		retval = 1;
2621 		goto bailout;
2622 	}
2623 
2624 	printf("LUN %d removed successfully\n", lun_id);
2625 
2626 bailout:
2627 	return (retval);
2628 }
2629 
2630 static int
2631 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2632 {
2633 	struct ctl_lun_req req;
2634 	uint64_t lun_size = 0;
2635 	uint32_t lun_id = 0;
2636 	int lun_id_set = 0, lun_size_set = 0;
2637 	char *backend_name = NULL;
2638 	STAILQ_HEAD(, cctl_req_option) option_list;
2639 	int num_options = 0;
2640 	int retval = 0, c;
2641 
2642 	STAILQ_INIT(&option_list);
2643 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2644 		switch (c) {
2645 		case 'b':
2646 			backend_name = strdup(optarg);
2647 			break;
2648 		case 'l':
2649 			lun_id = strtoul(optarg, NULL, 0);
2650 			lun_id_set = 1;
2651 			break;
2652 		case 'o': {
2653 			struct cctl_req_option *option;
2654 			char *tmpstr;
2655 			char *name, *value;
2656 
2657 			tmpstr = strdup(optarg);
2658 			name = strsep(&tmpstr, "=");
2659 			if (name == NULL) {
2660 				warnx("%s: option -o takes \"name=value\""
2661 				      "argument", __func__);
2662 				retval = 1;
2663 				goto bailout;
2664 			}
2665 			value = strsep(&tmpstr, "=");
2666 			if (value == NULL) {
2667 				warnx("%s: option -o takes \"name=value\""
2668 				      "argument", __func__);
2669 				retval = 1;
2670 				goto bailout;
2671 			}
2672 			option = malloc(sizeof(*option));
2673 			if (option == NULL) {
2674 				warn("%s: error allocating %zd bytes",
2675 				     __func__, sizeof(*option));
2676 				retval = 1;
2677 				goto bailout;
2678 			}
2679 			option->name = strdup(name);
2680 			option->namelen = strlen(name) + 1;
2681 			option->value = strdup(value);
2682 			option->vallen = strlen(value) + 1;
2683 			free(tmpstr);
2684 
2685 			STAILQ_INSERT_TAIL(&option_list, option, links);
2686 			num_options++;
2687 			break;
2688 		}
2689 		case 's':
2690 			if (strcasecmp(optarg, "auto") != 0) {
2691 				retval = expand_number(optarg, &lun_size);
2692 				if (retval != 0) {
2693 					warn("%s: invalid -s argument",
2694 					    __func__);
2695 					retval = 1;
2696 					goto bailout;
2697 				}
2698 			}
2699 			lun_size_set = 1;
2700 			break;
2701 		default:
2702 			break;
2703 		}
2704 	}
2705 
2706 	if (backend_name == NULL)
2707 		errx(1, "%s: backend name (-b) must be specified", __func__);
2708 
2709 	if (lun_id_set == 0)
2710 		errx(1, "%s: LUN id (-l) must be specified", __func__);
2711 
2712 	if (lun_size_set == 0 && num_options == 0)
2713 		errx(1, "%s: size (-s) or options (-o) must be specified",
2714 		    __func__);
2715 
2716 	bzero(&req, sizeof(req));
2717 
2718 	strlcpy(req.backend, backend_name, sizeof(req.backend));
2719 	req.reqtype = CTL_LUNREQ_MODIFY;
2720 
2721 	req.reqdata.modify.lun_id = lun_id;
2722 	req.reqdata.modify.lun_size_bytes = lun_size;
2723 
2724 	req.num_be_args = num_options;
2725 	if (num_options > 0) {
2726 		struct cctl_req_option *option, *next_option;
2727 		int i;
2728 
2729 		req.be_args = malloc(num_options * sizeof(*req.be_args));
2730 		if (req.be_args == NULL) {
2731 			warn("%s: error allocating %zd bytes", __func__,
2732 			     num_options * sizeof(*req.be_args));
2733 			retval = 1;
2734 			goto bailout;
2735 		}
2736 
2737 		for (i = 0, option = STAILQ_FIRST(&option_list);
2738 		     i < num_options; i++, option = next_option) {
2739 			next_option = STAILQ_NEXT(option, links);
2740 
2741 			req.be_args[i].namelen = option->namelen;
2742 			req.be_args[i].name = strdup(option->name);
2743 			req.be_args[i].vallen = option->vallen;
2744 			req.be_args[i].value = strdup(option->value);
2745 			/*
2746 			 * XXX KDM do we want a way to specify a writeable
2747 			 * flag of some sort?  Do we want a way to specify
2748 			 * binary data?
2749 			 */
2750 			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2751 
2752 			STAILQ_REMOVE(&option_list, option, cctl_req_option,
2753 				      links);
2754 			free(option->name);
2755 			free(option->value);
2756 			free(option);
2757 		}
2758 	}
2759 
2760 	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2761 		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2762 		retval = 1;
2763 		goto bailout;
2764 	}
2765 
2766 	switch (req.status) {
2767 	case CTL_LUN_ERROR:
2768 		warnx("LUN modification error: %s", req.error_str);
2769 		retval = 1;
2770 		goto bailout;
2771 	case CTL_LUN_WARNING:
2772 		warnx("LUN modification warning: %s", req.error_str);
2773 		break;
2774 	case CTL_LUN_OK:
2775 		break;
2776 	default:
2777 		warnx("unknown LUN modification status: %d", req.status);
2778 		retval = 1;
2779 		goto bailout;
2780 	}
2781 
2782 	printf("LUN %d modified successfully\n", lun_id);
2783 
2784 bailout:
2785 	return (retval);
2786 }
2787 
2788 struct cctl_islist_conn {
2789 	int connection_id;
2790 	char *initiator;
2791 	char *initiator_addr;
2792 	char *initiator_alias;
2793 	char *target;
2794 	char *target_alias;
2795 	char *header_digest;
2796 	char *data_digest;
2797 	char *max_recv_data_segment_length;
2798 	char *max_send_data_segment_length;
2799 	char *max_burst_length;
2800 	char *first_burst_length;
2801 	char *offload;
2802 	int immediate_data;
2803 	int iser;
2804 	STAILQ_ENTRY(cctl_islist_conn) links;
2805 };
2806 
2807 struct cctl_islist_data {
2808 	int num_conns;
2809 	STAILQ_HEAD(,cctl_islist_conn) conn_list;
2810 	struct cctl_islist_conn *cur_conn;
2811 	int level;
2812 	struct sbuf *cur_sb[32];
2813 };
2814 
2815 static void
2816 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2817 {
2818 	int i;
2819 	struct cctl_islist_data *islist;
2820 	struct cctl_islist_conn *cur_conn;
2821 
2822 	islist = (struct cctl_islist_data *)user_data;
2823 	cur_conn = islist->cur_conn;
2824 	islist->level++;
2825 	if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2826 	    sizeof(islist->cur_sb[0])))
2827 		errx(1, "%s: too many nesting levels, %zd max", __func__,
2828 		     sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2829 
2830 	islist->cur_sb[islist->level] = sbuf_new_auto();
2831 	if (islist->cur_sb[islist->level] == NULL)
2832 		err(1, "%s: Unable to allocate sbuf", __func__);
2833 
2834 	if (strcmp(name, "connection") == 0) {
2835 		if (cur_conn != NULL)
2836 			errx(1, "%s: improper connection element nesting",
2837 			    __func__);
2838 
2839 		cur_conn = calloc(1, sizeof(*cur_conn));
2840 		if (cur_conn == NULL)
2841 			err(1, "%s: cannot allocate %zd bytes", __func__,
2842 			    sizeof(*cur_conn));
2843 
2844 		islist->num_conns++;
2845 		islist->cur_conn = cur_conn;
2846 
2847 		STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2848 
2849 		for (i = 0; attr[i] != NULL; i += 2) {
2850 			if (strcmp(attr[i], "id") == 0) {
2851 				cur_conn->connection_id =
2852 				    strtoull(attr[i+1], NULL, 0);
2853 			} else {
2854 				errx(1,
2855 				    "%s: invalid connection attribute %s = %s",
2856 				     __func__, attr[i], attr[i+1]);
2857 			}
2858 		}
2859 	}
2860 }
2861 
2862 static void
2863 cctl_islist_end_element(void *user_data, const char *name)
2864 {
2865 	struct cctl_islist_data *islist;
2866 	struct cctl_islist_conn *cur_conn;
2867 	char *str;
2868 
2869 	islist = (struct cctl_islist_data *)user_data;
2870 	cur_conn = islist->cur_conn;
2871 
2872 	if ((cur_conn == NULL)
2873 	 && (strcmp(name, "ctlislist") != 0))
2874 		errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2875 
2876 	if (islist->cur_sb[islist->level] == NULL)
2877 		errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2878 		     islist->level, name);
2879 
2880 	sbuf_finish(islist->cur_sb[islist->level]);
2881 	str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2882 	if (str == NULL)
2883 		err(1, "%s can't allocate %zd bytes for string", __func__,
2884 		    sbuf_len(islist->cur_sb[islist->level]));
2885 
2886 	sbuf_delete(islist->cur_sb[islist->level]);
2887 	islist->cur_sb[islist->level] = NULL;
2888 	islist->level--;
2889 
2890 	if (strcmp(name, "initiator") == 0) {
2891 		cur_conn->initiator = str;
2892 		str = NULL;
2893 	} else if (strcmp(name, "initiator_addr") == 0) {
2894 		cur_conn->initiator_addr = str;
2895 		str = NULL;
2896 	} else if (strcmp(name, "initiator_alias") == 0) {
2897 		cur_conn->initiator_alias = str;
2898 		str = NULL;
2899 	} else if (strcmp(name, "target") == 0) {
2900 		cur_conn->target = str;
2901 		str = NULL;
2902 	} else if (strcmp(name, "target_alias") == 0) {
2903 		cur_conn->target_alias = str;
2904 		str = NULL;
2905 	} else if (strcmp(name, "target_portal_group_tag") == 0) {
2906 	} else if (strcmp(name, "header_digest") == 0) {
2907 		cur_conn->header_digest = str;
2908 		str = NULL;
2909 	} else if (strcmp(name, "data_digest") == 0) {
2910 		cur_conn->data_digest = str;
2911 		str = NULL;
2912 	} else if (strcmp(name, "max_recv_data_segment_length") == 0) {
2913 		cur_conn->max_recv_data_segment_length = str;
2914 		str = NULL;
2915 	} else if (strcmp(name, "max_send_data_segment_length") == 0) {
2916 		cur_conn->max_send_data_segment_length = str;
2917 		str = NULL;
2918 	} else if (strcmp(name, "max_burst_length") == 0) {
2919 		cur_conn->max_burst_length = str;
2920 		str = NULL;
2921 	} else if (strcmp(name, "first_burst_length") == 0) {
2922 		cur_conn->first_burst_length = str;
2923 		str = NULL;
2924 	} else if (strcmp(name, "offload") == 0) {
2925 		cur_conn->offload = str;
2926 		str = NULL;
2927 	} else if (strcmp(name, "immediate_data") == 0) {
2928 		cur_conn->immediate_data = atoi(str);
2929 	} else if (strcmp(name, "iser") == 0) {
2930 		cur_conn->iser = atoi(str);
2931 	} else if (strcmp(name, "connection") == 0) {
2932 		islist->cur_conn = NULL;
2933 	} else if (strcmp(name, "ctlislist") == 0) {
2934 		/* Nothing. */
2935 	} else {
2936 		/*
2937 		 * Unknown element; ignore it for forward compatibility.
2938 		 */
2939 	}
2940 
2941 	free(str);
2942 }
2943 
2944 static void
2945 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2946 {
2947 	struct cctl_islist_data *islist;
2948 
2949 	islist = (struct cctl_islist_data *)user_data;
2950 
2951 	sbuf_bcat(islist->cur_sb[islist->level], str, len);
2952 }
2953 
2954 static int
2955 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2956 {
2957 	struct ctl_iscsi req;
2958 	struct cctl_islist_data islist;
2959 	struct cctl_islist_conn *conn;
2960 	XML_Parser parser;
2961 	char *conn_str;
2962 	int conn_len;
2963 	int dump_xml = 0;
2964 	int c, retval, verbose = 0;
2965 
2966 	retval = 0;
2967 	conn_len = 4096;
2968 
2969 	bzero(&islist, sizeof(islist));
2970 	STAILQ_INIT(&islist.conn_list);
2971 
2972 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2973 		switch (c) {
2974 		case 'v':
2975 			verbose = 1;
2976 			break;
2977 		case 'x':
2978 			dump_xml = 1;
2979 			break;
2980 		default:
2981 			break;
2982 		}
2983 	}
2984 
2985 retry:
2986 	conn_str = malloc(conn_len);
2987 
2988 	bzero(&req, sizeof(req));
2989 	req.type = CTL_ISCSI_LIST;
2990 	req.data.list.alloc_len = conn_len;
2991 	req.data.list.conn_xml = conn_str;
2992 
2993 	if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2994 		warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2995 		retval = 1;
2996 		goto bailout;
2997 	}
2998 
2999 	if (req.status == CTL_ISCSI_ERROR) {
3000 		warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3001 		      __func__, req.error_str);
3002 	} else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3003 		conn_len = conn_len << 1;
3004 		goto retry;
3005 	}
3006 
3007 	if (dump_xml != 0) {
3008 		printf("%s", conn_str);
3009 		goto bailout;
3010 	}
3011 
3012 	parser = XML_ParserCreate(NULL);
3013 	if (parser == NULL) {
3014 		warn("%s: Unable to create XML parser", __func__);
3015 		retval = 1;
3016 		goto bailout;
3017 	}
3018 
3019 	XML_SetUserData(parser, &islist);
3020 	XML_SetElementHandler(parser, cctl_islist_start_element,
3021 	    cctl_islist_end_element);
3022 	XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3023 
3024 	retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3025 	if (retval != 1) {
3026 		warnx("%s: Unable to parse XML: Error %d", __func__,
3027 		    XML_GetErrorCode(parser));
3028 		XML_ParserFree(parser);
3029 		retval = 1;
3030 		goto bailout;
3031 	}
3032 	retval = 0;
3033 	XML_ParserFree(parser);
3034 
3035 	if (verbose != 0) {
3036 		STAILQ_FOREACH(conn, &islist.conn_list, links) {
3037 			printf("%-25s %d\n", "Session ID:", conn->connection_id);
3038 			printf("%-25s %s\n", "Initiator name:", conn->initiator);
3039 			printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr);
3040 			printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias);
3041 			printf("%-25s %s\n", "Target name:", conn->target);
3042 			printf("%-25s %s\n", "Target alias:", conn->target_alias);
3043 			printf("%-25s %s\n", "Header digest:", conn->header_digest);
3044 			printf("%-25s %s\n", "Data digest:", conn->data_digest);
3045 			printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length);
3046 			printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length);
3047 			printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length);
3048 			printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length);
3049 			printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No");
3050 			printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No");
3051 			printf("%-25s %s\n", "Offload driver:", conn->offload);
3052 			printf("\n");
3053 		}
3054 	} else {
3055 		printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3056 		    "Target name");
3057 		STAILQ_FOREACH(conn, &islist.conn_list, links) {
3058 			printf("%4u %-16s %-36s %-36s\n",
3059 			    conn->connection_id, conn->initiator_addr, conn->initiator,
3060 			    conn->target);
3061 		}
3062 	}
3063 bailout:
3064 	free(conn_str);
3065 
3066 	return (retval);
3067 }
3068 
3069 static int
3070 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3071 {
3072 	struct ctl_iscsi req;
3073 	int retval = 0, c;
3074 	int all = 0, connection_id = -1, nargs = 0;
3075 	char *initiator_name = NULL, *initiator_addr = NULL;
3076 
3077 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3078 		switch (c) {
3079 		case 'a':
3080 			all = 1;
3081 			nargs++;
3082 			break;
3083 		case 'c':
3084 			connection_id = strtoul(optarg, NULL, 0);
3085 			nargs++;
3086 			break;
3087 		case 'i':
3088 			initiator_name = strdup(optarg);
3089 			if (initiator_name == NULL)
3090 				err(1, "%s: strdup", __func__);
3091 			nargs++;
3092 			break;
3093 		case 'p':
3094 			initiator_addr = strdup(optarg);
3095 			if (initiator_addr == NULL)
3096 				err(1, "%s: strdup", __func__);
3097 			nargs++;
3098 			break;
3099 		default:
3100 			break;
3101 		}
3102 	}
3103 
3104 	if (nargs == 0)
3105 		errx(1, "%s: either -a, -c, -i, or -p must be specified",
3106 		    __func__);
3107 	if (nargs > 1)
3108 		errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3109 		    __func__);
3110 
3111 	bzero(&req, sizeof(req));
3112 	req.type = CTL_ISCSI_LOGOUT;
3113 	req.data.logout.connection_id = connection_id;
3114 	if (initiator_addr != NULL)
3115 		strlcpy(req.data.logout.initiator_addr,
3116 		    initiator_addr, sizeof(req.data.logout.initiator_addr));
3117 	if (initiator_name != NULL)
3118 		strlcpy(req.data.logout.initiator_name,
3119 		    initiator_name, sizeof(req.data.logout.initiator_name));
3120 	if (all != 0)
3121 		req.data.logout.all = 1;
3122 
3123 	if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3124 		warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3125 		retval = 1;
3126 		goto bailout;
3127 	}
3128 
3129 	if (req.status != CTL_ISCSI_OK) {
3130 		warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3131 		      __func__, req.error_str);
3132 		retval = 1;
3133 		goto bailout;
3134 	}
3135 
3136 	printf("iSCSI logout requests submitted\n");
3137 
3138 bailout:
3139 	return (retval);
3140 }
3141 
3142 static int
3143 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3144 {
3145 	struct ctl_iscsi req;
3146 	int retval = 0, c;
3147 	int all = 0, connection_id = -1, nargs = 0;
3148 	char *initiator_name = NULL, *initiator_addr = NULL;
3149 
3150 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3151 		switch (c) {
3152 		case 'a':
3153 			all = 1;
3154 			nargs++;
3155 			break;
3156 		case 'c':
3157 			connection_id = strtoul(optarg, NULL, 0);
3158 			nargs++;
3159 			break;
3160 		case 'i':
3161 			initiator_name = strdup(optarg);
3162 			if (initiator_name == NULL)
3163 				err(1, "%s: strdup", __func__);
3164 			nargs++;
3165 			break;
3166 		case 'p':
3167 			initiator_addr = strdup(optarg);
3168 			if (initiator_addr == NULL)
3169 				err(1, "%s: strdup", __func__);
3170 			nargs++;
3171 			break;
3172 		default:
3173 			break;
3174 		}
3175 	}
3176 
3177 	if (nargs == 0)
3178 		errx(1, "%s: either -a, -c, -i, or -p must be specified",
3179 		    __func__);
3180 	if (nargs > 1)
3181 		errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3182 		    __func__);
3183 
3184 	bzero(&req, sizeof(req));
3185 	req.type = CTL_ISCSI_TERMINATE;
3186 	req.data.terminate.connection_id = connection_id;
3187 	if (initiator_addr != NULL)
3188 		strlcpy(req.data.terminate.initiator_addr,
3189 		    initiator_addr, sizeof(req.data.terminate.initiator_addr));
3190 	if (initiator_name != NULL)
3191 		strlcpy(req.data.terminate.initiator_name,
3192 		    initiator_name, sizeof(req.data.terminate.initiator_name));
3193 	if (all != 0)
3194 		req.data.terminate.all = 1;
3195 
3196 	if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3197 		warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3198 		retval = 1;
3199 		goto bailout;
3200 	}
3201 
3202 	if (req.status != CTL_ISCSI_OK) {
3203 		warnx("%s: error returned from CTL iSCSI connection "
3204 		    "termination request:\n%s", __func__, req.error_str);
3205 		retval = 1;
3206 		goto bailout;
3207 	}
3208 
3209 	printf("iSCSI connections terminated\n");
3210 
3211 bailout:
3212 	return (retval);
3213 }
3214 
3215 /*
3216  * Name/value pair used for per-LUN attributes.
3217  */
3218 struct cctl_lun_nv {
3219 	char *name;
3220 	char *value;
3221 	STAILQ_ENTRY(cctl_lun_nv) links;
3222 };
3223 
3224 /*
3225  * Backend LUN information.
3226  */
3227 struct cctl_lun {
3228 	uint64_t lun_id;
3229 	char *backend_type;
3230 	uint64_t size_blocks;
3231 	uint32_t blocksize;
3232 	char *serial_number;
3233 	char *device_id;
3234 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
3235 	STAILQ_ENTRY(cctl_lun) links;
3236 };
3237 
3238 struct cctl_devlist_data {
3239 	int num_luns;
3240 	STAILQ_HEAD(,cctl_lun) lun_list;
3241 	struct cctl_lun *cur_lun;
3242 	int level;
3243 	struct sbuf *cur_sb[32];
3244 };
3245 
3246 static void
3247 cctl_start_element(void *user_data, const char *name, const char **attr)
3248 {
3249 	int i;
3250 	struct cctl_devlist_data *devlist;
3251 	struct cctl_lun *cur_lun;
3252 
3253 	devlist = (struct cctl_devlist_data *)user_data;
3254 	cur_lun = devlist->cur_lun;
3255 	devlist->level++;
3256 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3257 	    sizeof(devlist->cur_sb[0])))
3258 		errx(1, "%s: too many nesting levels, %zd max", __func__,
3259 		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3260 
3261 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
3262 	if (devlist->cur_sb[devlist->level] == NULL)
3263 		err(1, "%s: Unable to allocate sbuf", __func__);
3264 
3265 	if (strcmp(name, "lun") == 0) {
3266 		if (cur_lun != NULL)
3267 			errx(1, "%s: improper lun element nesting", __func__);
3268 
3269 		cur_lun = calloc(1, sizeof(*cur_lun));
3270 		if (cur_lun == NULL)
3271 			err(1, "%s: cannot allocate %zd bytes", __func__,
3272 			    sizeof(*cur_lun));
3273 
3274 		devlist->num_luns++;
3275 		devlist->cur_lun = cur_lun;
3276 
3277 		STAILQ_INIT(&cur_lun->attr_list);
3278 		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3279 
3280 		for (i = 0; attr[i] != NULL; i += 2) {
3281 			if (strcmp(attr[i], "id") == 0) {
3282 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3283 			} else {
3284 				errx(1, "%s: invalid LUN attribute %s = %s",
3285 				     __func__, attr[i], attr[i+1]);
3286 			}
3287 		}
3288 	}
3289 }
3290 
3291 static void
3292 cctl_end_element(void *user_data, const char *name)
3293 {
3294 	struct cctl_devlist_data *devlist;
3295 	struct cctl_lun *cur_lun;
3296 	char *str;
3297 
3298 	devlist = (struct cctl_devlist_data *)user_data;
3299 	cur_lun = devlist->cur_lun;
3300 
3301 	if ((cur_lun == NULL)
3302 	 && (strcmp(name, "ctllunlist") != 0))
3303 		errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3304 
3305 	if (devlist->cur_sb[devlist->level] == NULL)
3306 		errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3307 		     devlist->level, name);
3308 
3309 	if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3310 		err(1, "%s: sbuf_finish", __func__);
3311 	str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3312 	if (str == NULL)
3313 		err(1, "%s can't allocate %zd bytes for string", __func__,
3314 		    sbuf_len(devlist->cur_sb[devlist->level]));
3315 
3316 	if (strlen(str) == 0) {
3317 		free(str);
3318 		str = NULL;
3319 	}
3320 
3321 	sbuf_delete(devlist->cur_sb[devlist->level]);
3322 	devlist->cur_sb[devlist->level] = NULL;
3323 	devlist->level--;
3324 
3325 	if (strcmp(name, "backend_type") == 0) {
3326 		cur_lun->backend_type = str;
3327 		str = NULL;
3328 	} else if (strcmp(name, "size") == 0) {
3329 		cur_lun->size_blocks = strtoull(str, NULL, 0);
3330 	} else if (strcmp(name, "blocksize") == 0) {
3331 		cur_lun->blocksize = strtoul(str, NULL, 0);
3332 	} else if (strcmp(name, "serial_number") == 0) {
3333 		cur_lun->serial_number = str;
3334 		str = NULL;
3335 	} else if (strcmp(name, "device_id") == 0) {
3336 		cur_lun->device_id = str;
3337 		str = NULL;
3338 	} else if (strcmp(name, "lun") == 0) {
3339 		devlist->cur_lun = NULL;
3340 	} else if (strcmp(name, "ctllunlist") == 0) {
3341 		/* Nothing. */
3342 	} else {
3343 		struct cctl_lun_nv *nv;
3344 
3345 		nv = calloc(1, sizeof(*nv));
3346 		if (nv == NULL)
3347 			err(1, "%s: can't allocate %zd bytes for nv pair",
3348 			    __func__, sizeof(*nv));
3349 
3350 		nv->name = strdup(name);
3351 		if (nv->name == NULL)
3352 			err(1, "%s: can't allocated %zd bytes for string",
3353 			    __func__, strlen(name));
3354 
3355 		nv->value = str;
3356 		str = NULL;
3357 		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3358 	}
3359 
3360 	free(str);
3361 }
3362 
3363 static void
3364 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3365 {
3366 	struct cctl_devlist_data *devlist;
3367 
3368 	devlist = (struct cctl_devlist_data *)user_data;
3369 
3370 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3371 }
3372 
3373 static int
3374 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3375 {
3376 	struct ctl_lun_list list;
3377 	struct cctl_devlist_data devlist;
3378 	struct cctl_lun *lun;
3379 	XML_Parser parser;
3380 	char *lun_str;
3381 	int lun_len;
3382 	int dump_xml = 0;
3383 	int retval, c;
3384 	char *backend = NULL;
3385 	int verbose = 0;
3386 
3387 	retval = 0;
3388 	lun_len = 4096;
3389 
3390 	bzero(&devlist, sizeof(devlist));
3391 	STAILQ_INIT(&devlist.lun_list);
3392 
3393 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3394 		switch (c) {
3395 		case 'b':
3396 			backend = strdup(optarg);
3397 			break;
3398 		case 'v':
3399 			verbose++;
3400 			break;
3401 		case 'x':
3402 			dump_xml = 1;
3403 			break;
3404 		default:
3405 			break;
3406 		}
3407 	}
3408 
3409 retry:
3410 	lun_str = malloc(lun_len);
3411 
3412 	bzero(&list, sizeof(list));
3413 	list.alloc_len = lun_len;
3414 	list.status = CTL_LUN_LIST_NONE;
3415 	list.lun_xml = lun_str;
3416 
3417 	if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3418 		warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3419 		retval = 1;
3420 		goto bailout;
3421 	}
3422 
3423 	if (list.status == CTL_LUN_LIST_ERROR) {
3424 		warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3425 		      __func__, list.error_str);
3426 	} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3427 		lun_len = lun_len << 1;
3428 		goto retry;
3429 	}
3430 
3431 	if (dump_xml != 0) {
3432 		printf("%s", lun_str);
3433 		goto bailout;
3434 	}
3435 
3436 	parser = XML_ParserCreate(NULL);
3437 	if (parser == NULL) {
3438 		warn("%s: Unable to create XML parser", __func__);
3439 		retval = 1;
3440 		goto bailout;
3441 	}
3442 
3443 	XML_SetUserData(parser, &devlist);
3444 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3445 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
3446 
3447 	retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3448 	if (retval != 1) {
3449 		warnx("%s: Unable to parse XML: Error %d", __func__,
3450 		    XML_GetErrorCode(parser));
3451 		XML_ParserFree(parser);
3452 		retval = 1;
3453 		goto bailout;
3454 	}
3455 	retval = 0;
3456 	XML_ParserFree(parser);
3457 
3458 	printf("LUN Backend  %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3459 	       "Serial Number", "Device ID");
3460 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3461 		struct cctl_lun_nv *nv;
3462 
3463 		if ((backend != NULL)
3464 		 && (strcmp(lun->backend_type, backend) != 0))
3465 			continue;
3466 
3467 		printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3468 		       (uintmax_t)lun->lun_id,
3469 		       lun->backend_type, (uintmax_t)lun->size_blocks,
3470 		       lun->blocksize, lun->serial_number, lun->device_id);
3471 
3472 		if (verbose == 0)
3473 			continue;
3474 
3475 		STAILQ_FOREACH(nv, &lun->attr_list, links) {
3476 			printf("      %s=%s\n", nv->name, nv->value);
3477 		}
3478 	}
3479 bailout:
3480 	free(lun_str);
3481 
3482 	return (retval);
3483 }
3484 
3485 /*
3486  * Port information.
3487  */
3488 struct cctl_port {
3489 	uint64_t port_id;
3490 	char *online;
3491 	char *frontend_type;
3492 	char *name;
3493 	int pp, vp;
3494 	char *target, *port, *lun_map;
3495 	STAILQ_HEAD(,cctl_lun_nv) init_list;
3496 	STAILQ_HEAD(,cctl_lun_nv) lun_list;
3497 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
3498 	STAILQ_ENTRY(cctl_port) links;
3499 };
3500 
3501 struct cctl_portlist_data {
3502 	int num_ports;
3503 	STAILQ_HEAD(,cctl_port) port_list;
3504 	struct cctl_port *cur_port;
3505 	int level;
3506 	uint64_t cur_id;
3507 	struct sbuf *cur_sb[32];
3508 };
3509 
3510 static void
3511 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3512 {
3513 	int i;
3514 	struct cctl_portlist_data *portlist;
3515 	struct cctl_port *cur_port;
3516 
3517 	portlist = (struct cctl_portlist_data *)user_data;
3518 	cur_port = portlist->cur_port;
3519 	portlist->level++;
3520 	if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3521 	    sizeof(portlist->cur_sb[0])))
3522 		errx(1, "%s: too many nesting levels, %zd max", __func__,
3523 		     sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3524 
3525 	portlist->cur_sb[portlist->level] = sbuf_new_auto();
3526 	if (portlist->cur_sb[portlist->level] == NULL)
3527 		err(1, "%s: Unable to allocate sbuf", __func__);
3528 
3529 	portlist->cur_id = 0;
3530 	for (i = 0; attr[i] != NULL; i += 2) {
3531 		if (strcmp(attr[i], "id") == 0) {
3532 			portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3533 			break;
3534 		}
3535 	}
3536 
3537 	if (strcmp(name, "targ_port") == 0) {
3538 		if (cur_port != NULL)
3539 			errx(1, "%s: improper port element nesting", __func__);
3540 
3541 		cur_port = calloc(1, sizeof(*cur_port));
3542 		if (cur_port == NULL)
3543 			err(1, "%s: cannot allocate %zd bytes", __func__,
3544 			    sizeof(*cur_port));
3545 
3546 		portlist->num_ports++;
3547 		portlist->cur_port = cur_port;
3548 
3549 		STAILQ_INIT(&cur_port->init_list);
3550 		STAILQ_INIT(&cur_port->lun_list);
3551 		STAILQ_INIT(&cur_port->attr_list);
3552 		cur_port->port_id = portlist->cur_id;
3553 		STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3554 	}
3555 }
3556 
3557 static void
3558 cctl_end_pelement(void *user_data, const char *name)
3559 {
3560 	struct cctl_portlist_data *portlist;
3561 	struct cctl_port *cur_port;
3562 	char *str;
3563 
3564 	portlist = (struct cctl_portlist_data *)user_data;
3565 	cur_port = portlist->cur_port;
3566 
3567 	if ((cur_port == NULL)
3568 	 && (strcmp(name, "ctlportlist") != 0))
3569 		errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3570 
3571 	if (portlist->cur_sb[portlist->level] == NULL)
3572 		errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3573 		     portlist->level, name);
3574 
3575 	if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3576 		err(1, "%s: sbuf_finish", __func__);
3577 	str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3578 	if (str == NULL)
3579 		err(1, "%s can't allocate %zd bytes for string", __func__,
3580 		    sbuf_len(portlist->cur_sb[portlist->level]));
3581 
3582 	if (strlen(str) == 0) {
3583 		free(str);
3584 		str = NULL;
3585 	}
3586 
3587 	sbuf_delete(portlist->cur_sb[portlist->level]);
3588 	portlist->cur_sb[portlist->level] = NULL;
3589 	portlist->level--;
3590 
3591 	if (strcmp(name, "frontend_type") == 0) {
3592 		cur_port->frontend_type = str;
3593 		str = NULL;
3594 	} else if (strcmp(name, "port_name") == 0) {
3595 		cur_port->name = str;
3596 		str = NULL;
3597 	} else if (strcmp(name, "online") == 0) {
3598 		cur_port->online = str;
3599 		str = NULL;
3600 	} else if (strcmp(name, "physical_port") == 0) {
3601 		cur_port->pp = strtoull(str, NULL, 0);
3602 	} else if (strcmp(name, "virtual_port") == 0) {
3603 		cur_port->vp = strtoull(str, NULL, 0);
3604 	} else if (strcmp(name, "target") == 0) {
3605 		cur_port->target = str;
3606 		str = NULL;
3607 	} else if (strcmp(name, "port") == 0) {
3608 		cur_port->port = str;
3609 		str = NULL;
3610 	} else if (strcmp(name, "lun_map") == 0) {
3611 		cur_port->lun_map = str;
3612 		str = NULL;
3613 	} else if (strcmp(name, "targ_port") == 0) {
3614 		portlist->cur_port = NULL;
3615 	} else if (strcmp(name, "ctlportlist") == 0) {
3616 		/* Nothing. */
3617 	} else {
3618 		struct cctl_lun_nv *nv;
3619 
3620 		nv = calloc(1, sizeof(*nv));
3621 		if (nv == NULL)
3622 			err(1, "%s: can't allocate %zd bytes for nv pair",
3623 			    __func__, sizeof(*nv));
3624 
3625 		if (strcmp(name, "initiator") == 0 ||
3626 		    strcmp(name, "lun") == 0)
3627 			asprintf(&nv->name, "%ju", portlist->cur_id);
3628 		else
3629 			nv->name = strdup(name);
3630 		if (nv->name == NULL)
3631 			err(1, "%s: can't allocated %zd bytes for string",
3632 			    __func__, strlen(name));
3633 
3634 		nv->value = str;
3635 		str = NULL;
3636 		if (strcmp(name, "initiator") == 0)
3637 			STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3638 		else if (strcmp(name, "lun") == 0)
3639 			STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3640 		else
3641 			STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3642 	}
3643 
3644 	free(str);
3645 }
3646 
3647 static void
3648 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3649 {
3650 	struct cctl_portlist_data *portlist;
3651 
3652 	portlist = (struct cctl_portlist_data *)user_data;
3653 
3654 	sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3655 }
3656 
3657 static int
3658 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3659 {
3660 	struct ctl_lun_list list;
3661 	struct cctl_portlist_data portlist;
3662 	struct cctl_port *port;
3663 	XML_Parser parser;
3664 	char *port_str;
3665 	int port_len;
3666 	int dump_xml = 0;
3667 	int retval, c;
3668 	char *frontend = NULL;
3669 	uint64_t portarg = UINT64_MAX;
3670 	int verbose = 0, init = 0, lun = 0, quiet = 0;
3671 
3672 	retval = 0;
3673 	port_len = 4096;
3674 
3675 	bzero(&portlist, sizeof(portlist));
3676 	STAILQ_INIT(&portlist.port_list);
3677 
3678 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3679 		switch (c) {
3680 		case 'f':
3681 			frontend = strdup(optarg);
3682 			break;
3683 		case 'i':
3684 			init++;
3685 			break;
3686 		case 'l':
3687 			lun++;
3688 			break;
3689 		case 'p':
3690 			portarg = strtoll(optarg, NULL, 0);
3691 			break;
3692 		case 'q':
3693 			quiet++;
3694 			break;
3695 		case 'v':
3696 			verbose++;
3697 			break;
3698 		case 'x':
3699 			dump_xml = 1;
3700 			break;
3701 		default:
3702 			break;
3703 		}
3704 	}
3705 
3706 retry:
3707 	port_str = malloc(port_len);
3708 
3709 	bzero(&list, sizeof(list));
3710 	list.alloc_len = port_len;
3711 	list.status = CTL_LUN_LIST_NONE;
3712 	list.lun_xml = port_str;
3713 
3714 	if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3715 		warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3716 		retval = 1;
3717 		goto bailout;
3718 	}
3719 
3720 	if (list.status == CTL_LUN_LIST_ERROR) {
3721 		warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3722 		      __func__, list.error_str);
3723 	} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3724 		port_len = port_len << 1;
3725 		goto retry;
3726 	}
3727 
3728 	if (dump_xml != 0) {
3729 		printf("%s", port_str);
3730 		goto bailout;
3731 	}
3732 
3733 	parser = XML_ParserCreate(NULL);
3734 	if (parser == NULL) {
3735 		warn("%s: Unable to create XML parser", __func__);
3736 		retval = 1;
3737 		goto bailout;
3738 	}
3739 
3740 	XML_SetUserData(parser, &portlist);
3741 	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3742 	XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3743 
3744 	retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3745 	if (retval != 1) {
3746 		warnx("%s: Unable to parse XML: Error %d", __func__,
3747 		    XML_GetErrorCode(parser));
3748 		XML_ParserFree(parser);
3749 		retval = 1;
3750 		goto bailout;
3751 	}
3752 	retval = 0;
3753 	XML_ParserFree(parser);
3754 
3755 	if (quiet == 0)
3756 		printf("Port Online Frontend Name     pp vp\n");
3757 	STAILQ_FOREACH(port, &portlist.port_list, links) {
3758 		struct cctl_lun_nv *nv;
3759 
3760 		if ((frontend != NULL)
3761 		 && (strcmp(port->frontend_type, frontend) != 0))
3762 			continue;
3763 
3764 		if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3765 			continue;
3766 
3767 		printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3768 		    (uintmax_t)port->port_id, port->online,
3769 		    port->frontend_type, port->name, port->pp, port->vp,
3770 		    port->port ? port->port : "");
3771 
3772 		if (init || verbose) {
3773 			if (port->target)
3774 				printf("  Target: %s\n", port->target);
3775 			STAILQ_FOREACH(nv, &port->init_list, links) {
3776 				printf("  Initiator %s: %s\n",
3777 				    nv->name, nv->value);
3778 			}
3779 		}
3780 
3781 		if (lun || verbose) {
3782 			if (port->lun_map) {
3783 				STAILQ_FOREACH(nv, &port->lun_list, links)
3784 					printf("  LUN %s: %s\n",
3785 					    nv->name, nv->value);
3786 				if (STAILQ_EMPTY(&port->lun_list))
3787 					printf("  No LUNs mapped\n");
3788 			} else
3789 				printf("  All LUNs mapped\n");
3790 		}
3791 
3792 		if (verbose) {
3793 			STAILQ_FOREACH(nv, &port->attr_list, links) {
3794 				printf("      %s=%s\n", nv->name, nv->value);
3795 			}
3796 		}
3797 	}
3798 bailout:
3799 	free(port_str);
3800 
3801 	return (retval);
3802 }
3803 
3804 static int
3805 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3806 {
3807 	struct ctl_lun_map lm;
3808 	int retval = 0, c;
3809 
3810 	retval = 0;
3811 	lm.port = UINT32_MAX;
3812 	lm.plun = UINT32_MAX;
3813 	lm.lun = UINT32_MAX;
3814 
3815 	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3816 		switch (c) {
3817 		case 'p':
3818 			lm.port = strtoll(optarg, NULL, 0);
3819 			break;
3820 		case 'l':
3821 			lm.plun = strtoll(optarg, NULL, 0);
3822 			break;
3823 		case 'L':
3824 			lm.lun = strtoll(optarg, NULL, 0);
3825 			break;
3826 		default:
3827 			break;
3828 		}
3829 	}
3830 
3831 	if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3832 		warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3833 		retval = 1;
3834 	}
3835 
3836 	return (retval);
3837 }
3838 
3839 void
3840 usage(int error)
3841 {
3842 	fprintf(error ? stderr : stdout,
3843 "Usage:\n"
3844 "Primary commands:\n"
3845 "         ctladm tur         [dev_id][general options]\n"
3846 "         ctladm inquiry     [dev_id][general options]\n"
3847 "         ctladm devid       [dev_id][general options]\n"
3848 "         ctladm reqsense    [dev_id][general options]\n"
3849 "         ctladm reportluns  [dev_id][general options]\n"
3850 "         ctladm read        [dev_id][general options] <-l lba> <-d len>\n"
3851 "                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3852 "         ctladm write       [dev_id][general options] <-l lba> <-d len>\n"
3853 "                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3854 "         ctladm readcap     [dev_id][general options] [-c cdbsize]\n"
3855 "         ctladm modesense   [dev_id][general options] <-m page|-l> [-P pc]\n"
3856 "                            [-d] [-S subpage] [-c cdbsize]\n"
3857 "         ctladm prin        [dev_id][general options] <-a action>\n"
3858 "         ctladm prout       [dev_id][general options] <-a action>\n"
3859 "                            <-r restype] [-k key] [-s sa_key]\n"
3860 "         ctladm rtpg        [dev_id][general options]\n"
3861 "         ctladm start       [dev_id][general options] [-i] [-o]\n"
3862 "         ctladm stop        [dev_id][general options] [-i] [-o]\n"
3863 "         ctladm synccache   [dev_id][general options] [-l lba]\n"
3864 "                            [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3865 "         ctladm create      <-b backend> [-B blocksize] [-d device_id]\n"
3866 "                            [-l lun_id] [-o name=value] [-s size_bytes]\n"
3867 "                            [-S serial_num] [-t dev_type]\n"
3868 "         ctladm remove      <-b backend> <-l lun_id> [-o name=value]\n"
3869 "         ctladm modify      <-b backend> <-l lun_id> <-s size_bytes>\n"
3870 "         ctladm devlist     [-b backend] [-v] [-x]\n"
3871 "         ctladm lunlist\n"
3872 "         ctladm lunmap      -p targ_port [-l pLUN] [-L cLUN]\n"
3873 "         ctladm delay       [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3874 "                            [-t secs]\n"
3875 "         ctladm inject      [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3876 "                            [-s len fmt [args]] [-c] [-d delete_id]\n"
3877 "         ctladm port        <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3878 "                            [-p targ_port] [-t port_type]\n"
3879 "         ctladm portlist    [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3880 "         ctladm islist      [-v | -x]\n"
3881 "         ctladm islogout    <-a | -c connection-id | -i name | -p portal>\n"
3882 "         ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3883 "         ctladm dumpooa\n"
3884 "         ctladm dumpstructs\n"
3885 "         ctladm help\n"
3886 "General Options:\n"
3887 "-I intiator_id           : defaults to 7, used to change the initiator id\n"
3888 "-C retries               : specify the number of times to retry this command\n"
3889 "-D devicename            : specify the device to operate on\n"
3890 "                         : (default is %s)\n"
3891 "read/write options:\n"
3892 "-l lba                   : logical block address\n"
3893 "-d len                   : read/write length, in blocks\n"
3894 "-f file|-                : write/read data to/from file or stdout/stdin\n"
3895 "-b blocksize             : block size, in bytes\n"
3896 "-c cdbsize               : specify minimum cdb size: 6, 10, 12 or 16\n"
3897 "-N                       : do not copy data to/from userland\n"
3898 "readcapacity options:\n"
3899 "-c cdbsize               : specify minimum cdb size: 10 or 16\n"
3900 "modesense options:\n"
3901 "-m page                  : specify the mode page to view\n"
3902 "-l                       : request a list of supported pages\n"
3903 "-P pc                    : specify the page control value: 0-3 (current,\n"
3904 "                           changeable, default, saved, respectively)\n"
3905 "-d                       : disable block descriptors for mode sense\n"
3906 "-S subpage               : specify a subpage\n"
3907 "-c cdbsize               : specify minimum cdb size: 6 or 10\n"
3908 "persistent reserve in options:\n"
3909 "-a action                : specify the action value: 0-2 (read key, read\n"
3910 "                           reservation, read capabilities, respectively)\n"
3911 "persistent reserve out options:\n"
3912 "-a action                : specify the action value: 0-5 (register, reserve,\n"
3913 "                           release, clear, preempt, register and ignore)\n"
3914 "-k key                   : key value\n"
3915 "-s sa_key                : service action value\n"
3916 "-r restype               : specify the reservation type: 0-5(wr ex, ex ac,\n"
3917 "                           wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3918 "start/stop options:\n"
3919 "-i                       : set the immediate bit (CTL does not support this)\n"
3920 "-o                       : set the on/offline bit\n"
3921 "synccache options:\n"
3922 "-l lba                   : set the starting LBA\n"
3923 "-b blockcount            : set the length to sync in blocks\n"
3924 "-r                       : set the relative addressing bit\n"
3925 "-i                       : set the immediate bit\n"
3926 "-c cdbsize               : specify minimum cdb size: 10 or 16\n"
3927 "create options:\n"
3928 "-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
3929 "-B blocksize             : LUN blocksize in bytes (some backends)\n"
3930 "-d device_id             : SCSI VPD page 0x83 ID\n"
3931 "-l lun_id                : requested LUN number\n"
3932 "-o name=value            : backend-specific options, multiple allowed\n"
3933 "-s size_bytes            : LUN size in bytes (some backends)\n"
3934 "-S serial_num            : SCSI VPD page 0x80 serial number\n"
3935 "-t dev_type              : SCSI device type (0=disk, 3=processor)\n"
3936 "remove options:\n"
3937 "-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
3938 "-l lun_id                : LUN number to delete\n"
3939 "-o name=value            : backend-specific options, multiple allowed\n"
3940 "devlist options:\n"
3941 "-b backend               : list devices from specified backend only\n"
3942 "-v                       : be verbose, show backend attributes\n"
3943 "-x                       : dump raw XML\n"
3944 "delay options:\n"
3945 "-l datamove|done         : delay command at datamove or done phase\n"
3946 "-T oneshot               : delay one command, then resume normal completion\n"
3947 "-T cont                  : delay all commands\n"
3948 "-t secs                  : number of seconds to delay\n"
3949 "inject options:\n"
3950 "-i error_action          : action to perform\n"
3951 "-p pattern               : command pattern to look for\n"
3952 "-r lba,len               : LBA range for pattern\n"
3953 "-s len fmt [args]        : sense data for custom sense action\n"
3954 "-c                       : continuous operation\n"
3955 "-d delete_id             : error id to delete\n"
3956 "port options:\n"
3957 "-l                       : list frontend ports\n"
3958 "-o on|off                : turn frontend ports on or off\n"
3959 "-w wwnn                  : set WWNN for one frontend\n"
3960 "-W wwpn                  : set WWPN for one frontend\n"
3961 "-t port_type             : specify fc, scsi, ioctl, internal frontend type\n"
3962 "-p targ_port             : specify target port number\n"
3963 "-q                       : omit header in list output\n"
3964 "-x                       : output port list in XML format\n"
3965 "portlist options:\n"
3966 "-f fronetnd              : specify frontend type\n"
3967 "-i                       : report target and initiators addresses\n"
3968 "-l                       : report LUN mapping\n"
3969 "-p targ_port             : specify target port number\n"
3970 "-q                       : omit header in list output\n"
3971 "-v                       : verbose output (report all port options)\n"
3972 "-x                       : output port list in XML format\n"
3973 "lunmap options:\n"
3974 "-p targ_port             : specify target port number\n"
3975 "-L pLUN                  : specify port-visible LUN\n"
3976 "-L cLUN                  : specify CTL LUN\n",
3977 CTL_DEFAULT_DEV);
3978 }
3979 
3980 int
3981 main(int argc, char **argv)
3982 {
3983 	int c;
3984 	ctladm_cmdfunction command;
3985 	ctladm_cmdargs cmdargs;
3986 	ctladm_optret optreturn;
3987 	char *device;
3988 	const char *mainopt = "C:D:I:";
3989 	const char *subopt = NULL;
3990 	char combinedopt[256];
3991 	int lun;
3992 	int optstart = 2;
3993 	int retval, fd;
3994 	int retries;
3995 	int initid;
3996 	int saved_errno;
3997 
3998 	retval = 0;
3999 	cmdargs = CTLADM_ARG_NONE;
4000 	command = CTLADM_CMD_HELP;
4001 	device = NULL;
4002 	fd = -1;
4003 	retries = 0;
4004 	lun = 0;
4005 	initid = 7;
4006 
4007 	if (argc < 2) {
4008 		usage(1);
4009 		retval = 1;
4010 		goto bailout;
4011 	}
4012 
4013 	/*
4014 	 * Get the base option.
4015 	 */
4016 	optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4017 
4018 	if (optreturn == CC_OR_AMBIGUOUS) {
4019 		warnx("ambiguous option %s", argv[1]);
4020 		usage(0);
4021 		exit(1);
4022 	} else if (optreturn == CC_OR_NOT_FOUND) {
4023 		warnx("option %s not found", argv[1]);
4024 		usage(0);
4025 		exit(1);
4026 	}
4027 
4028 	if (cmdargs & CTLADM_ARG_NEED_TL) {
4029 		if ((argc < 3) || (!isdigit(argv[2][0]))) {
4030 			warnx("option %s requires a lun argument",
4031 			      argv[1]);
4032 			usage(0);
4033 			exit(1);
4034 		}
4035 		lun = strtol(argv[2], NULL, 0);
4036 
4037 		cmdargs |= CTLADM_ARG_TARG_LUN;
4038 		optstart++;
4039 	}
4040 
4041 	/*
4042 	 * Ahh, getopt(3) is a pain.
4043 	 *
4044 	 * This is a gross hack.  There really aren't many other good
4045 	 * options (excuse the pun) for parsing options in a situation like
4046 	 * this.  getopt is kinda braindead, so you end up having to run
4047 	 * through the options twice, and give each invocation of getopt
4048 	 * the option string for the other invocation.
4049 	 *
4050 	 * You would think that you could just have two groups of options.
4051 	 * The first group would get parsed by the first invocation of
4052 	 * getopt, and the second group would get parsed by the second
4053 	 * invocation of getopt.  It doesn't quite work out that way.  When
4054 	 * the first invocation of getopt finishes, it leaves optind pointing
4055 	 * to the argument _after_ the first argument in the second group.
4056 	 * So when the second invocation of getopt comes around, it doesn't
4057 	 * recognize the first argument it gets and then bails out.
4058 	 *
4059 	 * A nice alternative would be to have a flag for getopt that says
4060 	 * "just keep parsing arguments even when you encounter an unknown
4061 	 * argument", but there isn't one.  So there's no real clean way to
4062 	 * easily parse two sets of arguments without having one invocation
4063 	 * of getopt know about the other.
4064 	 *
4065 	 * Without this hack, the first invocation of getopt would work as
4066 	 * long as the generic arguments are first, but the second invocation
4067 	 * (in the subfunction) would fail in one of two ways.  In the case
4068 	 * where you don't set optreset, it would fail because optind may be
4069 	 * pointing to the argument after the one it should be pointing at.
4070 	 * In the case where you do set optreset, and reset optind, it would
4071 	 * fail because getopt would run into the first set of options, which
4072 	 * it doesn't understand.
4073 	 *
4074 	 * All of this would "sort of" work if you could somehow figure out
4075 	 * whether optind had been incremented one option too far.  The
4076 	 * mechanics of that, however, are more daunting than just giving
4077 	 * both invocations all of the expect options for either invocation.
4078 	 *
4079 	 * Needless to say, I wouldn't mind if someone invented a better
4080 	 * (non-GPL!) command line parsing interface than getopt.  I
4081 	 * wouldn't mind if someone added more knobs to getopt to make it
4082 	 * work better.  Who knows, I may talk myself into doing it someday,
4083 	 * if the standards weenies let me.  As it is, it just leads to
4084 	 * hackery like this and causes people to avoid it in some cases.
4085 	 *
4086 	 * KDM, September 8th, 1998
4087 	 */
4088 	if (subopt != NULL)
4089 		sprintf(combinedopt, "%s%s", mainopt, subopt);
4090 	else
4091 		sprintf(combinedopt, "%s", mainopt);
4092 
4093 	/*
4094 	 * Start getopt processing at argv[2/3], since we've already
4095 	 * accepted argv[1..2] as the command name, and as a possible
4096 	 * device name.
4097 	 */
4098 	optind = optstart;
4099 
4100 	/*
4101 	 * Now we run through the argument list looking for generic
4102 	 * options, and ignoring options that possibly belong to
4103 	 * subfunctions.
4104 	 */
4105 	while ((c = getopt(argc, argv, combinedopt))!= -1){
4106 		switch (c) {
4107 		case 'C':
4108 			cmdargs |= CTLADM_ARG_RETRIES;
4109 			retries = strtol(optarg, NULL, 0);
4110 			break;
4111 		case 'D':
4112 			device = strdup(optarg);
4113 			cmdargs |= CTLADM_ARG_DEVICE;
4114 			break;
4115 		case 'I':
4116 			cmdargs |= CTLADM_ARG_INITIATOR;
4117 			initid = strtol(optarg, NULL, 0);
4118 			break;
4119 		default:
4120 			break;
4121 		}
4122 	}
4123 
4124 	if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4125 		initid = 7;
4126 
4127 	optind = optstart;
4128 	optreset = 1;
4129 
4130 	/*
4131 	 * Default to opening the CTL device for now.
4132 	 */
4133 	if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4134 	 && (command != CTLADM_CMD_HELP)) {
4135 		device = strdup(CTL_DEFAULT_DEV);
4136 		cmdargs |= CTLADM_ARG_DEVICE;
4137 	}
4138 
4139 	if ((cmdargs & CTLADM_ARG_DEVICE)
4140 	 && (command != CTLADM_CMD_HELP)) {
4141 		fd = open(device, O_RDWR);
4142 		if (fd == -1 && errno == ENOENT) {
4143 			saved_errno = errno;
4144 			retval = kldload("ctl");
4145 			if (retval != -1)
4146 				fd = open(device, O_RDWR);
4147 			else
4148 				errno = saved_errno;
4149 		}
4150 		if (fd == -1) {
4151 			fprintf(stderr, "%s: error opening %s: %s\n",
4152 				argv[0], device, strerror(errno));
4153 			retval = 1;
4154 			goto bailout;
4155 		}
4156 #ifdef	WANT_ISCSI
4157 		else {
4158 			if (modfind("cfiscsi") == -1 &&
4159 			    kldload("cfiscsi") == -1)
4160 				warn("couldn't load cfiscsi");
4161 		}
4162 #endif
4163 	} else if ((command != CTLADM_CMD_HELP)
4164 		&& ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4165 		fprintf(stderr, "%s: you must specify a device with the "
4166 			"--device argument for this command\n", argv[0]);
4167 		command = CTLADM_CMD_HELP;
4168 		retval = 1;
4169 	}
4170 
4171 	switch (command) {
4172 	case CTLADM_CMD_TUR:
4173 		retval = cctl_tur(fd, lun, initid, retries);
4174 		break;
4175 	case CTLADM_CMD_INQUIRY:
4176 		retval = cctl_inquiry(fd, lun, initid, retries);
4177 		break;
4178 	case CTLADM_CMD_REQ_SENSE:
4179 		retval = cctl_req_sense(fd, lun, initid, retries);
4180 		break;
4181 	case CTLADM_CMD_REPORT_LUNS:
4182 		retval = cctl_report_luns(fd, lun, initid, retries);
4183 		break;
4184 	case CTLADM_CMD_CREATE:
4185 		retval = cctl_create_lun(fd, argc, argv, combinedopt);
4186 		break;
4187 	case CTLADM_CMD_RM:
4188 		retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4189 		break;
4190 	case CTLADM_CMD_DEVLIST:
4191 		retval = cctl_devlist(fd, argc, argv, combinedopt);
4192 		break;
4193 	case CTLADM_CMD_READ:
4194 	case CTLADM_CMD_WRITE:
4195 		retval = cctl_read_write(fd, lun, initid, retries,
4196 					 argc, argv, combinedopt, command);
4197 		break;
4198 	case CTLADM_CMD_PORT:
4199 		retval = cctl_port(fd, argc, argv, combinedopt);
4200 		break;
4201 	case CTLADM_CMD_PORTLIST:
4202 		retval = cctl_portlist(fd, argc, argv, combinedopt);
4203 		break;
4204 	case CTLADM_CMD_LUNMAP:
4205 		retval = cctl_lunmap(fd, argc, argv, combinedopt);
4206 		break;
4207 	case CTLADM_CMD_READCAPACITY:
4208 		retval = cctl_read_capacity(fd, lun, initid, retries,
4209 					    argc, argv, combinedopt);
4210 		break;
4211 	case CTLADM_CMD_MODESENSE:
4212 		retval = cctl_mode_sense(fd, lun, initid, retries,
4213 					 argc, argv, combinedopt);
4214 		break;
4215 	case CTLADM_CMD_START:
4216 	case CTLADM_CMD_STOP:
4217 		retval = cctl_start_stop(fd, lun, initid, retries,
4218 					 (command == CTLADM_CMD_START) ? 1 : 0,
4219 					 argc, argv, combinedopt);
4220 		break;
4221 	case CTLADM_CMD_SYNC_CACHE:
4222 		retval = cctl_sync_cache(fd, lun, initid, retries,
4223 					 argc, argv, combinedopt);
4224 		break;
4225 	case CTLADM_CMD_LUNLIST:
4226 		retval = cctl_lunlist(fd);
4227 		break;
4228 	case CTLADM_CMD_DELAY:
4229 		retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4230 		break;
4231 	case CTLADM_CMD_ERR_INJECT:
4232 		retval = cctl_error_inject(fd, lun, argc, argv,
4233 					   combinedopt);
4234 		break;
4235 	case CTLADM_CMD_DUMPOOA:
4236 		retval = cctl_dump_ooa(fd, argc, argv);
4237 		break;
4238 	case CTLADM_CMD_DUMPSTRUCTS:
4239 		retval = cctl_dump_structs(fd, cmdargs);
4240 		break;
4241 	case CTLADM_CMD_PRES_IN:
4242 		retval = cctl_persistent_reserve_in(fd, lun, initid,
4243 		                                    argc, argv, combinedopt,
4244 						    retries);
4245 		break;
4246 	case CTLADM_CMD_PRES_OUT:
4247 		retval = cctl_persistent_reserve_out(fd, lun, initid,
4248 						     argc, argv, combinedopt,
4249 						     retries);
4250 		break;
4251 	case CTLADM_CMD_INQ_VPD_DEVID:
4252 	        retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4253 		break;
4254 	case CTLADM_CMD_RTPG:
4255 	        retval = cctl_report_target_port_group(fd, lun, initid);
4256 		break;
4257 	case CTLADM_CMD_MODIFY:
4258 	        retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4259 		break;
4260 	case CTLADM_CMD_ISLIST:
4261 	        retval = cctl_islist(fd, argc, argv, combinedopt);
4262 		break;
4263 	case CTLADM_CMD_ISLOGOUT:
4264 	        retval = cctl_islogout(fd, argc, argv, combinedopt);
4265 		break;
4266 	case CTLADM_CMD_ISTERMINATE:
4267 	        retval = cctl_isterminate(fd, argc, argv, combinedopt);
4268 		break;
4269 	case CTLADM_CMD_HELP:
4270 	default:
4271 		usage(retval);
4272 		break;
4273 	}
4274 bailout:
4275 
4276 	if (fd != -1)
4277 		close(fd);
4278 
4279 	exit (retval);
4280 }
4281 
4282 /*
4283  * vim: ts=8
4284  */
4285