xref: /aosp_15_r20/external/sg3_utils/testing/sg_tst_excl2.cpp (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1*44704f69SBart Van Assche /*
2*44704f69SBart Van Assche  * Copyright (c) 2013-2022 Douglas Gilbert.
3*44704f69SBart Van Assche  * All rights reserved.
4*44704f69SBart Van Assche  *
5*44704f69SBart Van Assche  * Redistribution and use in source and binary forms, with or without
6*44704f69SBart Van Assche  * modification, are permitted provided that the following conditions
7*44704f69SBart Van Assche  * are met:
8*44704f69SBart Van Assche  * 1. Redistributions of source code must retain the above copyright
9*44704f69SBart Van Assche  *    notice, this list of conditions and the following disclaimer.
10*44704f69SBart Van Assche  * 2. Redistributions in binary form must reproduce the above copyright
11*44704f69SBart Van Assche  *    notice, this list of conditions and the following disclaimer in the
12*44704f69SBart Van Assche  *    documentation and/or other materials provided with the distribution.
13*44704f69SBart Van Assche  *
14*44704f69SBart Van Assche  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*44704f69SBart Van Assche  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*44704f69SBart Van Assche  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*44704f69SBart Van Assche  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*44704f69SBart Van Assche  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*44704f69SBart Van Assche  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*44704f69SBart Van Assche  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*44704f69SBart Van Assche  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*44704f69SBart Van Assche  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*44704f69SBart Van Assche  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*44704f69SBart Van Assche  * SUCH DAMAGE.
25*44704f69SBart Van Assche  *
26*44704f69SBart Van Assche  * SPDX-License-Identifier: BSD-2-Clause
27*44704f69SBart Van Assche  */
28*44704f69SBart Van Assche 
29*44704f69SBart Van Assche #include <iostream>
30*44704f69SBart Van Assche #include <vector>
31*44704f69SBart Van Assche #include <system_error>
32*44704f69SBart Van Assche #include <thread>
33*44704f69SBart Van Assche #include <mutex>
34*44704f69SBart Van Assche #include <chrono>
35*44704f69SBart Van Assche 
36*44704f69SBart Van Assche #include <unistd.h>
37*44704f69SBart Van Assche #include <fcntl.h>
38*44704f69SBart Van Assche #include <stdio.h>
39*44704f69SBart Van Assche #include <stdlib.h>
40*44704f69SBart Van Assche #include <string.h>
41*44704f69SBart Van Assche #include <errno.h>
42*44704f69SBart Van Assche #include <ctype.h>
43*44704f69SBart Van Assche #include <sys/ioctl.h>
44*44704f69SBart Van Assche #include <sys/types.h>
45*44704f69SBart Van Assche #include <sys/stat.h>
46*44704f69SBart Van Assche #include "sg_lib.h"
47*44704f69SBart Van Assche #include "sg_pt.h"
48*44704f69SBart Van Assche #include "sg_unaligned.h"
49*44704f69SBart Van Assche 
50*44704f69SBart Van Assche static const char * version_str = "1.11 20220425";
51*44704f69SBart Van Assche static const char * util_name = "sg_tst_excl2";
52*44704f69SBart Van Assche 
53*44704f69SBart Van Assche /* This is a test program for checking O_EXCL on open() works. It uses
54*44704f69SBart Van Assche  * multiple threads and can be run as multiple processes and attempts
55*44704f69SBart Van Assche  * to "break" O_EXCL. The strategy is to open a device O_EXCL|O_NONBLOCK
56*44704f69SBart Van Assche  * and do a double increment on a LB then close it. Prior to the first
57*44704f69SBart Van Assche  * increment, the value is checked for even or odd. Assuming the count
58*44704f69SBart Van Assche  * starts as an even (typically 0) then it should remain even. Odd instances
59*44704f69SBart Van Assche  * are counted and reported at the end of the program, after all threads
60*44704f69SBart Van Assche  * have completed.
61*44704f69SBart Van Assche  *
62*44704f69SBart Van Assche  * This is C++ code with some things from C++11 (e.g. threads) and was
63*44704f69SBart Van Assche  * only just able to compile (when some things were reverted) with gcc/g++
64*44704f69SBart Van Assche  * version 4.7.3 found in Ubuntu 13.04 . C++11 "feature complete" support
65*44704f69SBart Van Assche  * was not available until g++ version 4.8.1 and that is only currently
66*44704f69SBart Van Assche  * found in Fedora 19 .
67*44704f69SBart Van Assche  *
68*44704f69SBart Van Assche  * The build uses various object files from the <sg3_utils>/lib directory
69*44704f69SBart Van Assche  * which is assumed to be a sibling of this examples directory. Those
70*44704f69SBart Van Assche  * object files in the lib directory can be built with:
71*44704f69SBart Van Assche  *   cd <sg3_utils> ; ./configure ; cd lib; make
72*44704f69SBart Van Assche  * Then:
73*44704f69SBart Van Assche  *   cd ../testing
74*44704f69SBart Van Assche  *   make sg_tst_excl2
75*44704f69SBart Van Assche  *
76*44704f69SBart Van Assche  * BEWARE: this utility modifies a logical block (default LBA 1000) on the
77*44704f69SBart Van Assche  * given device.
78*44704f69SBart Van Assche  *
79*44704f69SBart Van Assche  */
80*44704f69SBart Van Assche 
81*44704f69SBart Van Assche using namespace std;
82*44704f69SBart Van Assche using namespace std::chrono;
83*44704f69SBart Van Assche 
84*44704f69SBart Van Assche #define DEF_NUM_PER_THREAD 200
85*44704f69SBart Van Assche #define DEF_NUM_THREADS 4
86*44704f69SBart Van Assche #define DEF_WAIT_MS 0          /* 0: yield; -1: don't wait; -2: sleep(0) */
87*44704f69SBart Van Assche 
88*44704f69SBart Van Assche #define DEF_LBA 1000
89*44704f69SBart Van Assche 
90*44704f69SBart Van Assche #define EBUFF_SZ 256
91*44704f69SBart Van Assche 
92*44704f69SBart Van Assche 
93*44704f69SBart Van Assche static mutex odd_count_mutex;
94*44704f69SBart Van Assche static mutex console_mutex;
95*44704f69SBart Van Assche static unsigned int odd_count;
96*44704f69SBart Van Assche static unsigned int ebusy_count;
97*44704f69SBart Van Assche 
98*44704f69SBart Van Assche 
99*44704f69SBart Van Assche static void
usage(void)100*44704f69SBart Van Assche usage(void)
101*44704f69SBart Van Assche {
102*44704f69SBart Van Assche     printf("Usage: %s [-b] [-f] [-h] [-l <lba>] [-n <n_per_thr>] "
103*44704f69SBart Van Assche            "[-t <num_thrs>]\n"
104*44704f69SBart Van Assche            "                    [-V] [-w <wait_ms>] [-x] "
105*44704f69SBart Van Assche            "<disk_device>\n", util_name);
106*44704f69SBart Van Assche     printf("  where\n");
107*44704f69SBart Van Assche     printf("    -b                block on open (def: O_NONBLOCK)\n");
108*44704f69SBart Van Assche     printf("    -f                force: any SCSI disk (def: only "
109*44704f69SBart Van Assche            "scsi_debug)\n");
110*44704f69SBart Van Assche     printf("                      WARNING: <lba> written to\n");
111*44704f69SBart Van Assche     printf("    -h                print this usage message then exit\n");
112*44704f69SBart Van Assche     printf("    -l <lba>          logical block to increment (def: %u)\n",
113*44704f69SBart Van Assche            DEF_LBA);
114*44704f69SBart Van Assche     printf("    -n <n_per_thr>    number of loops per thread "
115*44704f69SBart Van Assche            "(def: %d)\n", DEF_NUM_PER_THREAD);
116*44704f69SBart Van Assche     printf("    -t <num_thrs>     number of threads (def: %d)\n",
117*44704f69SBart Van Assche            DEF_NUM_THREADS);
118*44704f69SBart Van Assche     printf("    -V                print version number then exit\n");
119*44704f69SBart Van Assche     printf("    -w <wait_ms>      >0: sleep_for(<wait_ms>); =0: "
120*44704f69SBart Van Assche            "yield(); -1: no\n"
121*44704f69SBart Van Assche            "                      wait; -2: sleep(0)  (def: %d)\n",
122*44704f69SBart Van Assche            DEF_WAIT_MS);
123*44704f69SBart Van Assche     printf("    -x                don't use O_EXCL on first thread "
124*44704f69SBart Van Assche            "(def: use\n"
125*44704f69SBart Van Assche            "                      O_EXCL on all threads)\n\n");
126*44704f69SBart Van Assche     printf("Test O_EXCL open flag with pass-through drivers. Each "
127*44704f69SBart Van Assche            "open/close cycle with\nthe O_EXCL flag does a double increment "
128*44704f69SBart Van Assche            "on lba (using its first 4 bytes).\n");
129*44704f69SBart Van Assche }
130*44704f69SBart Van Assche 
131*44704f69SBart Van Assche /* Assumed a lock (mutex) held when pt_err() is called */
132*44704f69SBart Van Assche static int
pt_err(int res)133*44704f69SBart Van Assche pt_err(int res)
134*44704f69SBart Van Assche {
135*44704f69SBart Van Assche     if (res < 0)
136*44704f69SBart Van Assche         fprintf(stderr, "  pass through os error: %s\n", safe_strerror(-res));
137*44704f69SBart Van Assche     else if (SCSI_PT_DO_BAD_PARAMS == res)
138*44704f69SBart Van Assche         fprintf(stderr, "  bad pass through setup\n");
139*44704f69SBart Van Assche     else if (SCSI_PT_DO_TIMEOUT == res)
140*44704f69SBart Van Assche         fprintf(stderr, "  pass through timeout\n");
141*44704f69SBart Van Assche     else
142*44704f69SBart Van Assche         fprintf(stderr, "  do_scsi_pt error=%d\n", res);
143*44704f69SBart Van Assche     return -1;
144*44704f69SBart Van Assche }
145*44704f69SBart Van Assche 
146*44704f69SBart Van Assche /* Assumed a lock (mutex) held when pt_cat_no_good() is called */
147*44704f69SBart Van Assche static int
pt_cat_no_good(int cat,struct sg_pt_base * ptp,const unsigned char * sbp)148*44704f69SBart Van Assche pt_cat_no_good(int cat, struct sg_pt_base * ptp, const unsigned char * sbp)
149*44704f69SBart Van Assche {
150*44704f69SBart Van Assche     int slen;
151*44704f69SBart Van Assche     char b[256];
152*44704f69SBart Van Assche     const int bl = (int)sizeof(b);
153*44704f69SBart Van Assche 
154*44704f69SBart Van Assche     switch (cat) {
155*44704f69SBart Van Assche     case SCSI_PT_RESULT_STATUS: /* other than GOOD and CHECK CONDITION */
156*44704f69SBart Van Assche         sg_get_scsi_status_str(get_scsi_pt_status_response(ptp), bl, b);
157*44704f69SBart Van Assche         fprintf(stderr, "  scsi status: %s\n", b);
158*44704f69SBart Van Assche         break;
159*44704f69SBart Van Assche     case SCSI_PT_RESULT_SENSE:
160*44704f69SBart Van Assche         slen = get_scsi_pt_sense_len(ptp);
161*44704f69SBart Van Assche         sg_get_sense_str("", sbp, slen, 1, bl, b);
162*44704f69SBart Van Assche         fprintf(stderr, "%s", b);
163*44704f69SBart Van Assche         break;
164*44704f69SBart Van Assche     case SCSI_PT_RESULT_TRANSPORT_ERR:
165*44704f69SBart Van Assche         get_scsi_pt_transport_err_str(ptp, bl, b);
166*44704f69SBart Van Assche         fprintf(stderr, "  transport: %s", b);
167*44704f69SBart Van Assche         break;
168*44704f69SBart Van Assche     case SCSI_PT_RESULT_OS_ERR:
169*44704f69SBart Van Assche         get_scsi_pt_os_err_str(ptp, bl, b);
170*44704f69SBart Van Assche         fprintf(stderr, "  os: %s", b);
171*44704f69SBart Van Assche         break;
172*44704f69SBart Van Assche     default:
173*44704f69SBart Van Assche         fprintf(stderr, "  unknown pt result category (%d)\n", cat);
174*44704f69SBart Van Assche         break;
175*44704f69SBart Van Assche     }
176*44704f69SBart Van Assche     return -1;
177*44704f69SBart Van Assche }
178*44704f69SBart Van Assche 
179*44704f69SBart Van Assche #define READ16_REPLY_LEN 512
180*44704f69SBart Van Assche #define READ16_CMD_LEN 16
181*44704f69SBart Van Assche #define WRITE16_REPLY_LEN 512
182*44704f69SBart Van Assche #define WRITE16_CMD_LEN 16
183*44704f69SBart Van Assche 
184*44704f69SBart Van Assche /* Opens dev_name and spins if busy (i.e. gets EBUSY), sleeping for
185*44704f69SBart Van Assche  * wait_ms milliseconds if wait_ms is positive. Reads lba and treats the
186*44704f69SBart Van Assche  * first 4 bytes as an int (SCSI endian), increments it and writes it back.
187*44704f69SBart Van Assche  * Repeats so that happens twice. Then closes dev_name. If an error occurs
188*44704f69SBart Van Assche  * returns -1 else returns 0 if first int read is even otherwise returns 1. */
189*44704f69SBart Van Assche static int
do_rd_inc_wr_twice(const char * dev_name,unsigned int lba,int block,int excl,int wait_ms,unsigned int & ebusys)190*44704f69SBart Van Assche do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block,
191*44704f69SBart Van Assche                    int excl, int wait_ms, unsigned int & ebusys)
192*44704f69SBart Van Assche {
193*44704f69SBart Van Assche     int k, sg_fd, res, cat;
194*44704f69SBart Van Assche     int odd = 0;
195*44704f69SBart Van Assche     unsigned int u = 0;
196*44704f69SBart Van Assche     struct sg_pt_base * ptp = NULL;
197*44704f69SBart Van Assche     unsigned char r16CmdBlk [READ16_CMD_LEN] =
198*44704f69SBart Van Assche                 {0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
199*44704f69SBart Van Assche     unsigned char w16CmdBlk [WRITE16_CMD_LEN] =
200*44704f69SBart Van Assche                 {0x8a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
201*44704f69SBart Van Assche     unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
202*44704f69SBart Van Assche     unsigned char lb[READ16_REPLY_LEN];
203*44704f69SBart Van Assche     char ebuff[EBUFF_SZ];
204*44704f69SBart Van Assche     int open_flags = O_RDWR;
205*44704f69SBart Van Assche 
206*44704f69SBart Van Assche     sg_put_unaligned_be64(lba, r16CmdBlk + 2);
207*44704f69SBart Van Assche     sg_put_unaligned_be64(lba, w16CmdBlk + 2);
208*44704f69SBart Van Assche     if (! block)
209*44704f69SBart Van Assche         open_flags |= O_NONBLOCK;
210*44704f69SBart Van Assche     if (excl)
211*44704f69SBart Van Assche         open_flags |= O_EXCL;
212*44704f69SBart Van Assche 
213*44704f69SBart Van Assche     while (((sg_fd = scsi_pt_open_flags(dev_name, open_flags, 0)) < 0) &&
214*44704f69SBart Van Assche            (-EBUSY == sg_fd)) {
215*44704f69SBart Van Assche         ++ebusys;
216*44704f69SBart Van Assche         if (wait_ms > 0)
217*44704f69SBart Van Assche             this_thread::sleep_for(milliseconds{wait_ms});
218*44704f69SBart Van Assche         else if (0 == wait_ms)
219*44704f69SBart Van Assche             this_thread::yield();       // thread yield
220*44704f69SBart Van Assche         else if (-2 == wait_ms)
221*44704f69SBart Van Assche             sleep(0);                   // process yield ??
222*44704f69SBart Van Assche     }
223*44704f69SBart Van Assche     if (sg_fd < 0) {
224*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ,
225*44704f69SBart Van Assche                  "do_rd_inc_wr_twice: error opening file: %s", dev_name);
226*44704f69SBart Van Assche         {
227*44704f69SBart Van Assche             lock_guard<mutex> lg(console_mutex);
228*44704f69SBart Van Assche 
229*44704f69SBart Van Assche             perror(ebuff);
230*44704f69SBart Van Assche         }
231*44704f69SBart Van Assche         return -1;
232*44704f69SBart Van Assche     }
233*44704f69SBart Van Assche 
234*44704f69SBart Van Assche     ptp = construct_scsi_pt_obj();
235*44704f69SBart Van Assche     for (k = 0; k < 2; ++k) {
236*44704f69SBart Van Assche         /* Prepare READ_16 command */
237*44704f69SBart Van Assche         clear_scsi_pt_obj(ptp);
238*44704f69SBart Van Assche         set_scsi_pt_cdb(ptp, r16CmdBlk, sizeof(r16CmdBlk));
239*44704f69SBart Van Assche         set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
240*44704f69SBart Van Assche         set_scsi_pt_data_in(ptp, lb, READ16_REPLY_LEN);
241*44704f69SBart Van Assche         res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
242*44704f69SBart Van Assche         if (res) {
243*44704f69SBart Van Assche             {
244*44704f69SBart Van Assche                 lock_guard<mutex> lg(console_mutex);
245*44704f69SBart Van Assche 
246*44704f69SBart Van Assche                 fprintf(stderr, "READ_16 do_scsi_pt() submission error\n");
247*44704f69SBart Van Assche                 res = pt_err(res);
248*44704f69SBart Van Assche             }
249*44704f69SBart Van Assche             goto err;
250*44704f69SBart Van Assche         }
251*44704f69SBart Van Assche         cat = get_scsi_pt_result_category(ptp);
252*44704f69SBart Van Assche         if (SCSI_PT_RESULT_GOOD != cat) {
253*44704f69SBart Van Assche             {
254*44704f69SBart Van Assche                 lock_guard<mutex> lg(console_mutex);
255*44704f69SBart Van Assche 
256*44704f69SBart Van Assche                 fprintf(stderr, "READ_16 do_scsi_pt() category problem\n");
257*44704f69SBart Van Assche                 res = pt_cat_no_good(cat, ptp, sense_buffer);
258*44704f69SBart Van Assche             }
259*44704f69SBart Van Assche             goto err;
260*44704f69SBart Van Assche         }
261*44704f69SBart Van Assche 
262*44704f69SBart Van Assche         u = sg_get_unaligned_be32(lb);
263*44704f69SBart Van Assche         // Assuming u starts test as even (probably 0), expect it to stay even
264*44704f69SBart Van Assche         if (0 == k)
265*44704f69SBart Van Assche             odd = (1 == (u % 2));
266*44704f69SBart Van Assche         ++u;
267*44704f69SBart Van Assche         sg_put_unaligned_be32(u, lb);
268*44704f69SBart Van Assche 
269*44704f69SBart Van Assche         if (wait_ms > 0)       /* allow daylight for bad things ... */
270*44704f69SBart Van Assche             this_thread::sleep_for(milliseconds{wait_ms});
271*44704f69SBart Van Assche         else if (0 == wait_ms)
272*44704f69SBart Van Assche             this_thread::yield();       // thread yield
273*44704f69SBart Van Assche         else if (-2 == wait_ms)
274*44704f69SBart Van Assche             sleep(0);                   // process yield ??
275*44704f69SBart Van Assche 
276*44704f69SBart Van Assche         /* Prepare WRITE_16 command */
277*44704f69SBart Van Assche         clear_scsi_pt_obj(ptp);
278*44704f69SBart Van Assche         set_scsi_pt_cdb(ptp, w16CmdBlk, sizeof(w16CmdBlk));
279*44704f69SBart Van Assche         set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
280*44704f69SBart Van Assche         set_scsi_pt_data_out(ptp, lb, WRITE16_REPLY_LEN);
281*44704f69SBart Van Assche         res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
282*44704f69SBart Van Assche         if (res) {
283*44704f69SBart Van Assche             {
284*44704f69SBart Van Assche                 lock_guard<mutex> lg(console_mutex);
285*44704f69SBart Van Assche 
286*44704f69SBart Van Assche                 fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n");
287*44704f69SBart Van Assche                 res = pt_err(res);
288*44704f69SBart Van Assche             }
289*44704f69SBart Van Assche             goto err;
290*44704f69SBart Van Assche         }
291*44704f69SBart Van Assche         cat = get_scsi_pt_result_category(ptp);
292*44704f69SBart Van Assche         if (SCSI_PT_RESULT_GOOD != cat) {
293*44704f69SBart Van Assche             {
294*44704f69SBart Van Assche                 lock_guard<mutex> lg(console_mutex);
295*44704f69SBart Van Assche 
296*44704f69SBart Van Assche                 fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n");
297*44704f69SBart Van Assche                 res = pt_cat_no_good(cat, ptp, sense_buffer);
298*44704f69SBart Van Assche             }
299*44704f69SBart Van Assche             goto err;
300*44704f69SBart Van Assche         }
301*44704f69SBart Van Assche     }
302*44704f69SBart Van Assche err:
303*44704f69SBart Van Assche     if (ptp)
304*44704f69SBart Van Assche         destruct_scsi_pt_obj(ptp);
305*44704f69SBart Van Assche     scsi_pt_close_device(sg_fd);
306*44704f69SBart Van Assche     return odd;
307*44704f69SBart Van Assche }
308*44704f69SBart Van Assche 
309*44704f69SBart Van Assche 
310*44704f69SBart Van Assche 
311*44704f69SBart Van Assche #define INQ_REPLY_LEN 96
312*44704f69SBart Van Assche #define INQ_CMD_LEN 6
313*44704f69SBart Van Assche 
314*44704f69SBart Van Assche /* Send INQUIRY and fetches response. If okay puts PRODUCT ID field
315*44704f69SBart Van Assche  * in b (up to m_blen bytes). Does not use O_EXCL flag. Returns 0 on success,
316*44704f69SBart Van Assche  * else -1 . */
317*44704f69SBart Van Assche static int
do_inquiry_prod_id(const char * dev_name,int block,int wait_ms,unsigned int & ebusys,char * b,int b_mlen)318*44704f69SBart Van Assche do_inquiry_prod_id(const char * dev_name, int block, int wait_ms,
319*44704f69SBart Van Assche                    unsigned int & ebusys, char * b, int b_mlen)
320*44704f69SBart Van Assche {
321*44704f69SBart Van Assche     int sg_fd, res, cat;
322*44704f69SBart Van Assche     struct sg_pt_base * ptp = NULL;
323*44704f69SBart Van Assche     unsigned char inqCmdBlk [INQ_CMD_LEN] =
324*44704f69SBart Van Assche                                 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
325*44704f69SBart Van Assche     unsigned char inqBuff[INQ_REPLY_LEN];
326*44704f69SBart Van Assche     unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
327*44704f69SBart Van Assche     char ebuff[EBUFF_SZ];
328*44704f69SBart Van Assche     int open_flags = O_RDWR;    /* since O_EXCL | O_RDONLY gives EPERM */
329*44704f69SBart Van Assche 
330*44704f69SBart Van Assche     if (! block)
331*44704f69SBart Van Assche         open_flags |= O_NONBLOCK;
332*44704f69SBart Van Assche     while (((sg_fd = scsi_pt_open_flags(dev_name, open_flags, 0)) < 0) &&
333*44704f69SBart Van Assche            (-EBUSY == sg_fd)) {
334*44704f69SBart Van Assche         ++ebusys;
335*44704f69SBart Van Assche         if (wait_ms > 0)
336*44704f69SBart Van Assche             this_thread::sleep_for(milliseconds{wait_ms});
337*44704f69SBart Van Assche         else if (0 == wait_ms)
338*44704f69SBart Van Assche             this_thread::yield();       // thread yield
339*44704f69SBart Van Assche         else if (-2 == wait_ms)
340*44704f69SBart Van Assche             sleep(0);                   // process yield ??
341*44704f69SBart Van Assche     }
342*44704f69SBart Van Assche     if (sg_fd < 0) {
343*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ,
344*44704f69SBart Van Assche                  "do_inquiry_prod_id: error opening file: %s", dev_name);
345*44704f69SBart Van Assche         perror(ebuff);
346*44704f69SBart Van Assche         return -1;
347*44704f69SBart Van Assche     }
348*44704f69SBart Van Assche     /* Prepare INQUIRY command */
349*44704f69SBart Van Assche     ptp = construct_scsi_pt_obj();
350*44704f69SBart Van Assche     clear_scsi_pt_obj(ptp);
351*44704f69SBart Van Assche     set_scsi_pt_cdb(ptp, inqCmdBlk, sizeof(inqCmdBlk));
352*44704f69SBart Van Assche     set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
353*44704f69SBart Van Assche     set_scsi_pt_data_in(ptp, inqBuff, INQ_REPLY_LEN);
354*44704f69SBart Van Assche     res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
355*44704f69SBart Van Assche     if (res) {
356*44704f69SBart Van Assche         {
357*44704f69SBart Van Assche             lock_guard<mutex> lg(console_mutex);
358*44704f69SBart Van Assche 
359*44704f69SBart Van Assche             fprintf(stderr, "INQUIRY do_scsi_pt() submission error\n");
360*44704f69SBart Van Assche             res = pt_err(res);
361*44704f69SBart Van Assche         }
362*44704f69SBart Van Assche         goto err;
363*44704f69SBart Van Assche     }
364*44704f69SBart Van Assche     cat = get_scsi_pt_result_category(ptp);
365*44704f69SBart Van Assche     if (SCSI_PT_RESULT_GOOD != cat) {
366*44704f69SBart Van Assche         {
367*44704f69SBart Van Assche             lock_guard<mutex> lg(console_mutex);
368*44704f69SBart Van Assche 
369*44704f69SBart Van Assche             fprintf(stderr, "INQUIRY do_scsi_pt() category problem\n");
370*44704f69SBart Van Assche             res = pt_cat_no_good(cat, ptp, sense_buffer);
371*44704f69SBart Van Assche         }
372*44704f69SBart Van Assche         goto err;
373*44704f69SBart Van Assche     }
374*44704f69SBart Van Assche 
375*44704f69SBart Van Assche     /* Good, so fetch Product ID from response, copy to 'b' */
376*44704f69SBart Van Assche     if (b_mlen > 0) {
377*44704f69SBart Van Assche         if (b_mlen > 16) {
378*44704f69SBart Van Assche             memcpy(b, inqBuff + 16, 16);
379*44704f69SBart Van Assche             b[16] = '\0';
380*44704f69SBart Van Assche         } else {
381*44704f69SBart Van Assche             memcpy(b, inqBuff + 16, b_mlen - 1);
382*44704f69SBart Van Assche             b[b_mlen - 1] = '\0';
383*44704f69SBart Van Assche         }
384*44704f69SBart Van Assche     }
385*44704f69SBart Van Assche err:
386*44704f69SBart Van Assche     if (ptp)
387*44704f69SBart Van Assche         destruct_scsi_pt_obj(ptp);
388*44704f69SBart Van Assche     close(sg_fd);
389*44704f69SBart Van Assche     return 0;
390*44704f69SBart Van Assche }
391*44704f69SBart Van Assche 
392*44704f69SBart Van Assche static void
work_thread(const char * dev_name,unsigned int lba,int id,int block,int excl,int num,int wait_ms)393*44704f69SBart Van Assche work_thread(const char * dev_name, unsigned int lba, int id, int block,
394*44704f69SBart Van Assche             int excl, int num, int wait_ms)
395*44704f69SBart Van Assche {
396*44704f69SBart Van Assche     unsigned int thr_odd_count = 0;
397*44704f69SBart Van Assche     unsigned int thr_ebusy_count = 0;
398*44704f69SBart Van Assche     int k, res;
399*44704f69SBart Van Assche 
400*44704f69SBart Van Assche     {
401*44704f69SBart Van Assche         lock_guard<mutex> lg(console_mutex);
402*44704f69SBart Van Assche 
403*44704f69SBart Van Assche         cerr << "Enter work_thread id=" << id << " excl=" << excl << " block="
404*44704f69SBart Van Assche              << block << endl;
405*44704f69SBart Van Assche     }
406*44704f69SBart Van Assche     for (k = 0; k < num; ++k) {
407*44704f69SBart Van Assche         res = do_rd_inc_wr_twice(dev_name, lba, block, excl, wait_ms,
408*44704f69SBart Van Assche                                  thr_ebusy_count);
409*44704f69SBart Van Assche         if (res < 0)
410*44704f69SBart Van Assche             break;
411*44704f69SBart Van Assche         if (res)
412*44704f69SBart Van Assche             ++thr_odd_count;
413*44704f69SBart Van Assche     }
414*44704f69SBart Van Assche     {
415*44704f69SBart Van Assche         lock_guard<mutex> lg(console_mutex);
416*44704f69SBart Van Assche 
417*44704f69SBart Van Assche         if (k < num)
418*44704f69SBart Van Assche             cerr << "thread id=" << id << " FAILed at iteration: " << k <<
419*44704f69SBart Van Assche                     '\n';
420*44704f69SBart Van Assche         else
421*44704f69SBart Van Assche             cerr << "thread id=" << id << " normal exit" << '\n';
422*44704f69SBart Van Assche     }
423*44704f69SBart Van Assche 
424*44704f69SBart Van Assche     {
425*44704f69SBart Van Assche         lock_guard<mutex> lg(odd_count_mutex);
426*44704f69SBart Van Assche 
427*44704f69SBart Van Assche         odd_count += thr_odd_count;
428*44704f69SBart Van Assche         ebusy_count += thr_ebusy_count;
429*44704f69SBart Van Assche     }
430*44704f69SBart Van Assche }
431*44704f69SBart Van Assche 
432*44704f69SBart Van Assche 
433*44704f69SBart Van Assche int
main(int argc,char * argv[])434*44704f69SBart Van Assche main(int argc, char * argv[])
435*44704f69SBart Van Assche {
436*44704f69SBart Van Assche     int k, res;
437*44704f69SBart Van Assche     int block = 0;
438*44704f69SBart Van Assche     int force = 0;
439*44704f69SBart Van Assche     unsigned int lba = DEF_LBA;
440*44704f69SBart Van Assche     int num_per_thread = DEF_NUM_PER_THREAD;
441*44704f69SBart Van Assche     int num_threads = DEF_NUM_THREADS;
442*44704f69SBart Van Assche     int wait_ms = DEF_WAIT_MS;
443*44704f69SBart Van Assche     int exclude_o_excl = 0;
444*44704f69SBart Van Assche     char * dev_name = NULL;
445*44704f69SBart Van Assche     char b[64];
446*44704f69SBart Van Assche 
447*44704f69SBart Van Assche     for (k = 1; k < argc; ++k) {
448*44704f69SBart Van Assche         if (0 == memcmp("-b", argv[k], 2))
449*44704f69SBart Van Assche             ++block;
450*44704f69SBart Van Assche         else if (0 == memcmp("-f", argv[k], 2))
451*44704f69SBart Van Assche             ++force;
452*44704f69SBart Van Assche         else if (0 == memcmp("-h", argv[k], 2)) {
453*44704f69SBart Van Assche             usage();
454*44704f69SBart Van Assche             return 0;
455*44704f69SBart Van Assche         } else if (0 == memcmp("-l", argv[k], 2)) {
456*44704f69SBart Van Assche             ++k;
457*44704f69SBart Van Assche             if ((k < argc) && isdigit(*argv[k]))
458*44704f69SBart Van Assche                 lba = (unsigned int)atoi(argv[k]);
459*44704f69SBart Van Assche             else
460*44704f69SBart Van Assche                 break;
461*44704f69SBart Van Assche         } else if (0 == memcmp("-n", argv[k], 2)) {
462*44704f69SBart Van Assche             ++k;
463*44704f69SBart Van Assche             if ((k < argc) && isdigit(*argv[k]))
464*44704f69SBart Van Assche                 num_per_thread = atoi(argv[k]);
465*44704f69SBart Van Assche             else
466*44704f69SBart Van Assche                 break;
467*44704f69SBart Van Assche         } else if (0 == memcmp("-t", argv[k], 2)) {
468*44704f69SBart Van Assche             ++k;
469*44704f69SBart Van Assche             if ((k < argc) && isdigit(*argv[k]))
470*44704f69SBart Van Assche                 num_threads = atoi(argv[k]);
471*44704f69SBart Van Assche             else
472*44704f69SBart Van Assche                 break;
473*44704f69SBart Van Assche         } else if (0 == memcmp("-V", argv[k], 2)) {
474*44704f69SBart Van Assche             printf("%s version: %s\n", util_name, version_str);
475*44704f69SBart Van Assche             return 0;
476*44704f69SBart Van Assche         } else if (0 == memcmp("-w", argv[k], 2)) {
477*44704f69SBart Van Assche             ++k;
478*44704f69SBart Van Assche             if ((k < argc) && (isdigit(*argv[k]) || ('-' == *argv[k]))) {
479*44704f69SBart Van Assche                 if ('-' == *argv[k])
480*44704f69SBart Van Assche                     wait_ms = - atoi(argv[k] + 1);
481*44704f69SBart Van Assche                 else
482*44704f69SBart Van Assche                     wait_ms = atoi(argv[k]);
483*44704f69SBart Van Assche             } else
484*44704f69SBart Van Assche                 break;
485*44704f69SBart Van Assche         } else if (0 == memcmp("-x", argv[k], 2))
486*44704f69SBart Van Assche             ++exclude_o_excl;
487*44704f69SBart Van Assche         else if (*argv[k] == '-') {
488*44704f69SBart Van Assche             printf("Unrecognized switch: %s\n", argv[k]);
489*44704f69SBart Van Assche             dev_name = NULL;
490*44704f69SBart Van Assche             break;
491*44704f69SBart Van Assche         }
492*44704f69SBart Van Assche         else if (! dev_name)
493*44704f69SBart Van Assche             dev_name = argv[k];
494*44704f69SBart Van Assche         else {
495*44704f69SBart Van Assche             printf("too many arguments\n");
496*44704f69SBart Van Assche             dev_name = 0;
497*44704f69SBart Van Assche             break;
498*44704f69SBart Van Assche         }
499*44704f69SBart Van Assche     }
500*44704f69SBart Van Assche     if (0 == dev_name) {
501*44704f69SBart Van Assche         usage();
502*44704f69SBart Van Assche         return 1;
503*44704f69SBart Van Assche     }
504*44704f69SBart Van Assche     try {
505*44704f69SBart Van Assche 
506*44704f69SBart Van Assche         if (! force) {
507*44704f69SBart Van Assche             res = do_inquiry_prod_id(dev_name, block, wait_ms, ebusy_count,
508*44704f69SBart Van Assche                                      b, sizeof(b));
509*44704f69SBart Van Assche             if (res) {
510*44704f69SBart Van Assche                 fprintf(stderr, "INQUIRY failed on %s\n", dev_name);
511*44704f69SBart Van Assche                 return 1;
512*44704f69SBart Van Assche             }
513*44704f69SBart Van Assche             // For safety, since <lba> written to, only permit scsi_debug
514*44704f69SBart Van Assche             // devices. Bypass this with '-f' option.
515*44704f69SBart Van Assche             if (0 != memcmp("scsi_debug", b, 10)) {
516*44704f69SBart Van Assche                 fprintf(stderr, "Since this utility writes to LBA %d, only "
517*44704f69SBart Van Assche                         "devices with scsi_debug\nproduct ID accepted.\n",
518*44704f69SBart Van Assche                         lba);
519*44704f69SBart Van Assche                 return 2;
520*44704f69SBart Van Assche             }
521*44704f69SBart Van Assche         }
522*44704f69SBart Van Assche 
523*44704f69SBart Van Assche         vector<thread *> vt;
524*44704f69SBart Van Assche 
525*44704f69SBart Van Assche         for (k = 0; k < num_threads; ++k) {
526*44704f69SBart Van Assche             int excl = ((0 == k) && exclude_o_excl) ? 0 : 1;
527*44704f69SBart Van Assche 
528*44704f69SBart Van Assche             thread * tp = new thread {work_thread, dev_name, lba, k, block,
529*44704f69SBart Van Assche                                       excl, num_per_thread, wait_ms};
530*44704f69SBart Van Assche             vt.push_back(tp);
531*44704f69SBart Van Assche         }
532*44704f69SBart Van Assche 
533*44704f69SBart Van Assche         for (k = 0; k < (int)vt.size(); ++k)
534*44704f69SBart Van Assche             vt[k]->join();
535*44704f69SBart Van Assche 
536*44704f69SBart Van Assche         for (k = 0; k < (int)vt.size(); ++k)
537*44704f69SBart Van Assche             delete vt[k];
538*44704f69SBart Van Assche 
539*44704f69SBart Van Assche         cout << "Expecting odd count of 0, got " << odd_count << endl;
540*44704f69SBart Van Assche         cout << "Number of EBUSYs: " << ebusy_count << endl;
541*44704f69SBart Van Assche 
542*44704f69SBart Van Assche     }
543*44704f69SBart Van Assche     catch(system_error& e)  {
544*44704f69SBart Van Assche         cerr << "got a system_error exception: " << e.what() << '\n';
545*44704f69SBart Van Assche         auto ec = e.code();
546*44704f69SBart Van Assche         cerr << "category: " << ec.category().name() << '\n';
547*44704f69SBart Van Assche         cerr << "value: " << ec.value() << '\n';
548*44704f69SBart Van Assche         cerr << "message: " << ec.message() << '\n';
549*44704f69SBart Van Assche         cerr << "\nNote: if g++ may need '-pthread' or similar in "
550*44704f69SBart Van Assche                 "compile/link line" << '\n';
551*44704f69SBart Van Assche     }
552*44704f69SBart Van Assche     catch(...) {
553*44704f69SBart Van Assche         cerr << "got another exception: " << '\n';
554*44704f69SBart Van Assche     }
555*44704f69SBart Van Assche     return 0;
556*44704f69SBart Van Assche }
557