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