1 /*
2 * Copyright (c) 2014-2022 Douglas Gilbert.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the BSD_LICENSE file.
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <getopt.h>
19 #define __STDC_FORMAT_MACROS 1
20 #include <inttypes.h>
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "sg_lib.h"
27 #include "sg_lib_data.h"
28 #include "sg_pt.h"
29 #include "sg_cmds_basic.h"
30 #include "sg_unaligned.h"
31 #include "sg_pr2serr.h"
32
33 /* A utility program originally written for the Linux OS SCSI subsystem.
34 *
35 * This program issues one of the following SCSI commands:
36 * - CLOSE ZONE
37 * - FINISH ZONE
38 * - OPEN ZONE
39 * - REMOVE ELEMENT AND MODIFY ZONES
40 * - SEQUENTIALIZE ZONE
41 */
42
43 static const char * version_str = "1.18 20220609";
44
45 #define SG_ZONING_OUT_CMDLEN 16
46 #define CLOSE_ZONE_SA 0x1
47 #define FINISH_ZONE_SA 0x2
48 #define OPEN_ZONE_SA 0x3
49 #define SEQUENTIALIZE_ZONE_SA 0x10
50 #define REM_ELEM_MOD_ZONES_SA 0x1a /* uses SERVICE ACTION IN(16) */
51
52 #define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
53 #define DEF_PT_TIMEOUT 60 /* 60 seconds */
54
55
56 static struct option long_options[] = {
57 {"all", no_argument, 0, 'a'},
58 {"close", no_argument, 0, 'c'},
59 {"count", required_argument, 0, 'C'},
60 {"element", required_argument, 0, 'e'},
61 {"finish", no_argument, 0, 'f'},
62 {"help", no_argument, 0, 'h'},
63 {"open", no_argument, 0, 'o'},
64 {"quick", no_argument, 0, 'q'},
65 {"remove", no_argument, 0, 'r'},
66 {"reset-all", no_argument, 0, 'R'}, /* same as --all */
67 {"reset_all", no_argument, 0, 'R'},
68 {"sequentialize", no_argument, 0, 'S'},
69 {"verbose", no_argument, 0, 'v'},
70 {"version", no_argument, 0, 'V'},
71 {"zone", required_argument, 0, 'z'},
72 {0, 0, 0, 0},
73 };
74
75 /* Indexed by service action of opcode 0x94 (Zone out) unless noted */
76 static const char * sa_name_arr[] = {
77 "no SA=0", /* 0x0 */
78 "Close zone",
79 "Finish zone",
80 "Open zone",
81 "-", "-", "-", "-",
82 "-",
83 "-", "-", "-", "-",
84 "-",
85 "-",
86 "-",
87 "Sequentialize zone", /* 0x10 */
88 "-", "-", "-", "-",
89 "-", "-", "-", "-",
90 "-",
91 "Remove element and modify zones", /* service action in(16), 0x1a */
92 };
93
94
95 static void
usage()96 usage()
97 {
98 pr2serr("Usage: "
99 "sg_zone [--all] [--close] [--count=ZC] [--element=EID] "
100 "[--finish]\n"
101 " [--help] [--open] [--quick] [--remove] "
102 "[--sequentialize]\n"
103 " [--verbose] [--version] [--zone=ID] DEVICE\n");
104 pr2serr(" where:\n"
105 " --all|-a sets the ALL flag in the cdb\n"
106 " --close|-c issue CLOSE ZONE command\n"
107 " --count=ZC|-C ZC set zone count field (def: 0)\n"
108 " --element=EID|-e EID EID is the element identifier to "
109 "remove;\n"
110 " default is 0 which is an invalid "
111 "EID\n"
112 " --finish|-f issue FINISH ZONE command\n"
113 " --help|-h print out usage message\n"
114 " --open|-o issue OPEN ZONE command\n"
115 " --quick|-q bypass 15 second warn and wait "
116 "(for --remove)\n"
117 " --remove|-r issue REMOVE ELEMENT AND MODIFY ZONES "
118 "command\n"
119 " --sequentialize|-S issue SEQUENTIALIZE ZONE command\n"
120 " --verbose|-v increase verbosity\n"
121 " --version|-V print version string and exit\n"
122 " --zone=ID|-z ID ID is the starting LBA of the zone "
123 "(def: 0)\n\n"
124 "Performs a SCSI OPEN ZONE, CLOSE ZONE, FINISH ZONE, "
125 "REMOVE ELEMENT AND\nMODIFY ZONES or SEQUENTIALIZE ZONE "
126 "command. Either --close, --finish,\n--open, --remove or "
127 "--sequentialize option needs to be given.\n");
128 }
129
130 /* Invokes the zone out command indicated by 'sa' (ZBC). Return of 0
131 * -> success, various SG_LIB_CAT_* positive values or -1 -> other errors */
132 static int
sg_ll_zone_out(int sg_fd,int sa,uint64_t zid,uint16_t zc,bool all,bool noisy,int verbose)133 sg_ll_zone_out(int sg_fd, int sa, uint64_t zid, uint16_t zc, bool all,
134 bool noisy, int verbose)
135 {
136 int ret, res, sense_cat;
137 struct sg_pt_base * ptvp;
138 uint8_t zo_cdb[SG_ZONING_OUT_CMDLEN] =
139 {SG_ZONING_OUT, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
140 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
141 char b[64];
142
143 zo_cdb[1] = 0x1f & sa;
144 if (REM_ELEM_MOD_ZONES_SA == sa) { /* zid carries element identifier */
145 zo_cdb[0] = SG_SERVICE_ACTION_IN_16; /* N.B. changing opcode */
146 sg_put_unaligned_be32((uint32_t)zid, zo_cdb + 10); /* element id */
147 } else {
148 sg_put_unaligned_be64(zid, zo_cdb + 2);
149 sg_put_unaligned_be16(zc, zo_cdb + 12);
150 if (all)
151 zo_cdb[14] = 0x1;
152 }
153 sg_get_opcode_sa_name(zo_cdb[0], sa, -1, sizeof(b), b);
154 if (verbose) {
155 char d[128];
156
157 pr2serr(" %s cdb: %s\n", b,
158 sg_get_command_str(zo_cdb, SG_ZONING_OUT_CMDLEN,
159 false, sizeof(d), d));
160 }
161
162 ptvp = construct_scsi_pt_obj();
163 if (NULL == ptvp) {
164 pr2serr("%s: out of memory\n", b);
165 return -1;
166 }
167 set_scsi_pt_cdb(ptvp, zo_cdb, sizeof(zo_cdb));
168 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
169 res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
170 ret = sg_cmds_process_resp(ptvp, b, res, noisy,
171 verbose, &sense_cat);
172 if (-1 == ret) {
173 if (get_scsi_pt_transport_err(ptvp))
174 ret = SG_LIB_TRANSPORT_ERROR;
175 else
176 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
177 } else if (-2 == ret) {
178 switch (sense_cat) {
179 case SG_LIB_CAT_RECOVERED:
180 case SG_LIB_CAT_NO_SENSE:
181 ret = 0;
182 break;
183 default:
184 ret = sense_cat;
185 break;
186 }
187 } else
188 ret = 0;
189 destruct_scsi_pt_obj(ptvp);
190 return ret;
191 }
192
193
194 int
main(int argc,char * argv[])195 main(int argc, char * argv[])
196 {
197 bool all = false;
198 bool close = false;
199 bool finish = false;
200 bool open = false;
201 bool quick = false;
202 bool reamz = false;
203 bool element_id_given = false;
204 bool sequentialize = false;
205 bool verbose_given = false;
206 bool version_given = false;
207 int res, c, n;
208 int sg_fd = -1;
209 int verbose = 0;
210 int ret = 0;
211 int sa = 0;
212 uint16_t zc = 0;
213 uint64_t zid = 0;
214 int64_t ll;
215 const char * device_name = NULL;
216 const char * sa_name;
217
218 while (1) {
219 int option_index = 0;
220
221 c = getopt_long(argc, argv, "acC:e:fhoqrRSvVz:", long_options,
222 &option_index);
223 if (c == -1)
224 break;
225
226 switch (c) {
227 case 'a':
228 case 'R':
229 all = true;
230 break;
231 case 'c':
232 close = true;
233 sa = CLOSE_ZONE_SA;
234 break;
235 case 'C':
236 n = sg_get_num(optarg);
237 if ((n < 0) || (n > 0xffff)) {
238 pr2serr("--count= expects an argument between 0 and 0xffff "
239 "inclusive\n");
240 return SG_LIB_SYNTAX_ERROR;
241 }
242 zc = (uint16_t)n;
243 break;
244 case 'e':
245 ll = sg_get_llnum(optarg);
246 if ((ll < 0) || (ll > UINT32_MAX)) {
247 pr2serr("bad argument to '--element=EID'\n");
248 return SG_LIB_SYNTAX_ERROR;
249 }
250 if (0 == ll)
251 pr2serr("Warning: 0 is an invalid element identifier\n");
252 zid = (uint64_t)ll; /* putting element_id in zid */
253 element_id_given = true;
254 break;
255 case 'f':
256 finish = true;
257 sa = FINISH_ZONE_SA;
258 break;
259 case 'h':
260 case '?':
261 usage();
262 return 0;
263 case 'o':
264 open = true;
265 sa = OPEN_ZONE_SA;
266 break;
267 case 'q':
268 quick = true;
269 break;
270 case 'r':
271 reamz = true;
272 sa = REM_ELEM_MOD_ZONES_SA;
273 break;
274 case 'S':
275 sequentialize = true;
276 sa = SEQUENTIALIZE_ZONE_SA;
277 break;
278 case 'v':
279 verbose_given = true;
280 ++verbose;
281 break;
282 case 'V':
283 version_given = true;
284 break;
285 case 'z':
286 ll = sg_get_llnum(optarg);
287 if (-1 == ll) {
288 pr2serr("bad argument to '--zone=ID'\n");
289 return SG_LIB_SYNTAX_ERROR;
290 }
291 zid = (uint64_t)ll;
292 break;
293 default:
294 pr2serr("unrecognised option code 0x%x ??\n", c);
295 usage();
296 return SG_LIB_SYNTAX_ERROR;
297 }
298 }
299 if (optind < argc) {
300 if (NULL == device_name) {
301 device_name = argv[optind];
302 ++optind;
303 }
304 if (optind < argc) {
305 for (; optind < argc; ++optind)
306 pr2serr("Unexpected extra argument: %s\n",
307 argv[optind]);
308 usage();
309 return SG_LIB_SYNTAX_ERROR;
310 }
311 }
312
313 #ifdef DEBUG
314 pr2serr("In DEBUG mode, ");
315 if (verbose_given && version_given) {
316 pr2serr("but override: '-vV' given, zero verbose and continue\n");
317 verbose_given = false;
318 version_given = false;
319 verbose = 0;
320 } else if (! verbose_given) {
321 pr2serr("set '-vv'\n");
322 verbose = 2;
323 } else
324 pr2serr("keep verbose=%d\n", verbose);
325 #else
326 if (verbose_given && version_given)
327 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
328 #endif
329 if (version_given) {
330 pr2serr("version: %s\n", version_str);
331 return 0;
332 }
333
334 if (1 != ((int)close + (int)finish + (int)open + (int)sequentialize +
335 (int)reamz)) {
336 pr2serr("One, and only one, of these options needs to be given:\n"
337 " --close, --finish, --open, --remove or --sequentialize "
338 "\n\n");
339 usage();
340 return SG_LIB_CONTRADICT;
341 }
342 if (element_id_given && (! reamz)) {
343 pr2serr("The --element=EID option should only be used with the "
344 "--remove option\n\n");
345 usage();
346 return SG_LIB_CONTRADICT;
347 }
348 sa_name = sa_name_arr[sa];
349
350 if (NULL == device_name) {
351 pr2serr("missing device name!\n");
352 usage();
353 return SG_LIB_SYNTAX_ERROR;
354 }
355
356 sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose);
357 if (sg_fd < 0) {
358 int err = -sg_fd;
359 if (verbose)
360 pr2serr("open error: %s: %s\n", device_name,
361 safe_strerror(err));
362 ret = sg_convert_errno(err);
363 goto fini;
364 }
365 if (reamz && (! quick))
366 sg_warn_and_wait(sa_name_arr[REM_ELEM_MOD_ZONES_SA], device_name,
367 false);
368
369 res = sg_ll_zone_out(sg_fd, sa, zid, zc, all, true, verbose);
370 ret = res;
371 if (res) {
372 if (SG_LIB_CAT_INVALID_OP == res)
373 pr2serr("%s command not supported\n", sa_name);
374 else {
375 char b[80];
376
377 sg_get_category_sense_str(res, sizeof(b), b, verbose);
378 pr2serr("%s command: %s\n", sa_name, b);
379 }
380 }
381
382 fini:
383 if (sg_fd >= 0) {
384 res = sg_cmds_close_device(sg_fd);
385 if (res < 0) {
386 pr2serr("close error: %s\n", safe_strerror(-res));
387 if (0 == ret)
388 ret = sg_convert_errno(-res);
389 }
390 }
391 if (0 == verbose) {
392 if (! sg_if_can2stderr("sg_zone failed: ", ret))
393 pr2serr("Some error occurred, try again with '-v' or '-vv' for "
394 "more information\n");
395 }
396 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
397 }
398