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