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