aboutsummaryrefslogtreecommitdiff
path: root/testing/sg_tst_excl.cpp
blob: 0354de60ad771a0c18c73430f18d6327bbd02618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/*
 * Copyright (c) 2013-2022 Douglas Gilbert.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <iostream>
#include <vector>
#include <system_error>
#include <thread>
#include <mutex>
#include <chrono>

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef HAVE_LINUX_SG_V4_HDR

/* Kernel uapi header contain __user decorations on user space pointers
 * to indicate they are unsafe in the kernel space. However glibc takes
 * all those __user decorations out from headers in /usr/include/linux .
 * So to stop compile errors when directly importing include/uapi/scsi/sg.h
 * undef __user before doing that include. */
#define __user

/* Want to block the original sg.h header from also being included. That
 * causes lots of multiple definition errors. This will only work if this
 * header is included _before_ the original sg.h header.  */
#define _SCSI_GENERIC_H         /* original kernel header guard */
#define _SCSI_SG_H              /* glibc header guard */

#include "uapi_sg.h"    /* local copy of include/uapi/scsi/sg.h */

#else
#define __user
#endif  /* end of: ifndef HAVE_LINUX_SG_V4_HDR */

#include "sg_lib.h"
#include "sg_io_linux.h"
#include "sg_unaligned.h"

static const char * version_str = "1.14 20220425";
static const char * util_name = "sg_tst_excl";

/* This is a test program for checking O_EXCL on open() works. It uses
 * multiple threads and can be run as multiple processes and attempts
 * to "break" O_EXCL. The strategy is to open a device O_EXCL|O_NONBLOCK
 * and do a double increment on a LB then close it. Prior to the first
 * increment, the value is checked for even or odd. Assuming the count
 * starts as an even (typically 0) then it should remain even. Odd instances
 * are counted and reported at the end of the program, after all threads
 * have completed.
 *
 * This is C++ code with some things from C++11 (e.g. threads) and was
 * only just able to compile (when some things were reverted) with gcc/g++
 * version 4.7.3 found in Ubuntu 13.04 . C++11 "feature complete" support
 * was not available until g++ version 4.8.1 and that is only currently
 * found in Fedora 19 .
 *
 * The build uses various object files from the <sg3_utils>/lib directory
 * which is assumed to be a sibling of this examples directory. Those
 * object files in the lib directory can be built with:
 *   cd <sg3_utils> ; ./configure ; cd lib; make
 * Then:
 *   cd ../testing
 *   make sg_tst_excl
 *
 * Currently this utility is Linux only and assumes the SG_IO v3 interface
 * which is supported by sg and block devices (but not bsg devices which
 * require the SG_IO v4 interface). This restriction is relaxed in the
 * sg_tst_excl2 variant of this utility.
 *
 * BEWARE: this utility modifies a logical block (default LBA 1000) on the
 * given device.
 *
 */

using namespace std;
using namespace std::chrono;

#define DEF_NUM_PER_THREAD 200
#define DEF_NUM_THREADS 4
#define DEF_WAIT_MS 0          /* 0: yield; -1: don't wait; -2: sleep(0) */


#define DEF_LBA 1000U

#define EBUFF_SZ 256

static mutex odd_count_mutex;
static mutex console_mutex;
static unsigned int odd_count;
static unsigned int ebusy_count;
static unsigned int eagain_count;
static int sg_ifc_ver = 3;


static void
usage(void)
{
    printf("Usage: %s [-b] [-f] [-h] [-i <sg_ver>] [-l <lba>] "
           "[-n <n_per_thr>]\n"
           "                   [-t <num_thrs>] [-V] [-w <wait_ms>] "
           "[-x] [-xx]\n"
           "                   <sg_disk_device>\n", util_name);
    printf("  where\n");
    printf("    -b                block on open (def: O_NONBLOCK)\n");
    printf("    -f                force: any SCSI disk (def: only "
           "scsi_debug)\n");
    printf("                      WARNING: <lba> written to\n");
    printf("    -h                print this usage message then exit\n");
    printf("    -i <sg_ver>       sg driver interface version (default: "
           "3)\n");
    printf("    -l <lba>          logical block to increment (def: %u)\n",
           DEF_LBA);
    printf("    -n <n_per_thr>    number of loops per thread "
           "(def: %d)\n", DEF_NUM_PER_THREAD);
    printf("    -t <num_thrs>     number of threads (def: %d)\n",
           DEF_NUM_THREADS);
    printf("    -V                print version number then exit\n");
    printf("    -w <wait_ms>      >0: sleep_for(<wait_ms>); =0: "
           "yield(); -1: no\n"
           "                      wait; -2: sleep(0)  (def: %d)\n",
           DEF_WAIT_MS);
    printf("    -x                don't use O_EXCL on first thread "
           "(def: use\n"
           "                      O_EXCL on all threads)\n"
           "    -xx               don't use O_EXCL on any thread\n\n");
    printf("Test O_EXCL open flag with Linux sg driver. Each open/close "
           "cycle with the\nO_EXCL flag does a double increment on "
           "lba (using its first 4 bytes).\nEach increment uses a READ_16, "
           "READ_16, increment, WRITE_16 cycle. The two\nREAD_16s are "
           "launched asynchronously. Note that '-xx' will run test\n"
           "without any O_EXCL flags.\n");
}


#define READ16_REPLY_LEN 512
#define READ16_CMD_LEN 16
#define WRITE16_REPLY_LEN 512
#define WRITE16_CMD_LEN 16

/* Opens dev_name and spins if busy (i.e. gets EBUSY), sleeping for
 * wait_ms milliseconds if wait_ms is positive.
 * Reads lba (twice) and treats the first 4 bytes as an int (SCSI endian),
 * increments it and writes it back. Repeats so that happens twice. Then
 * closes dev_name. If an error occurs returns -1 else returns 0 if
 * first int read from lba is even otherwise returns 1. */
static int
do_rd_inc_wr_twice_v3(const char * dev_name, unsigned int lba, int block,
                      int excl, int wait_ms, int id, unsigned int & ebusy,
                      unsigned int & eagains)
{
    bool odd = false;
    int k, sg_fd;
    struct sg_io_hdr pt, pt2;
    unsigned char r16CmdBlk [READ16_CMD_LEN] =
                {0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
    unsigned char w16CmdBlk [WRITE16_CMD_LEN] =
                {0x8a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
    unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
    unsigned char lb[READ16_REPLY_LEN];
    int open_flags = O_RDWR;

    sg_put_unaligned_be64(lba, r16CmdBlk + 2);
    sg_put_unaligned_be64(lba, w16CmdBlk + 2);
    if (! block)
        open_flags |= O_NONBLOCK;
    if (excl)
        open_flags |= O_EXCL;

    while (((sg_fd = open(dev_name, open_flags)) < 0) &&
           (EBUSY == errno)) {
        ++ebusy;
        if (wait_ms > 0)
            this_thread::sleep_for(milliseconds{wait_ms});
        else if (0 == wait_ms)
            this_thread::yield();
        else if (-2 == wait_ms)
            sleep(0);                   // process yield ??
    }
    if (sg_fd < 0) {
        char ebuff[EBUFF_SZ];

        snprintf(ebuff, EBUFF_SZ, "%s: error opening file: %s", __func__,
                 dev_name);
        perror(ebuff);
        return -1;
    }

    for (k = 0; k < 2; ++k) {
        bool ok = false;
        int res;
        unsigned int u = 0;

        /* Prepare READ_16 command */
        memset(&pt, 0, sizeof(pt));
        pt.interface_id = 'S';
        pt.cmd_len = sizeof(r16CmdBlk);
        pt.mx_sb_len = sizeof(sense_buffer);
        pt.dxfer_direction = SG_DXFER_FROM_DEV;
        pt.dxfer_len = READ16_REPLY_LEN;
        pt.dxferp = lb;
        pt.cmdp = r16CmdBlk;
        pt.sbp = sense_buffer;
        pt.timeout = 20000;     /* 20000 millisecs == 20 seconds */
        pt.pack_id = id;

        // queue up two READ_16s to same LBA
        if (write(sg_fd, &pt, sizeof(pt)) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" write(sg, READ_16)");
            }
            close(sg_fd);
            return -1;
        }
        pt2 = pt;
        if (write(sg_fd, &pt2, sizeof(pt2)) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" write(sg, READ_16) 2");
            }
            close(sg_fd);
            return -1;
        }

        while (((res = read(sg_fd, &pt, sizeof(pt))) < 0) &&
               (EAGAIN == errno)) {
            ++eagains;
            if (wait_ms > 0)
                this_thread::sleep_for(milliseconds{wait_ms});
            else if (0 == wait_ms)
                this_thread::yield();
            else if (-2 == wait_ms)
                sleep(0);                   // process yield ??
        }
        if (res < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" read(sg, READ_16)");
            }
            close(sg_fd);
            return -1;
        }
        /* now for the error processing */
        switch (sg_err_category3(&pt)) {
        case SG_LIB_CAT_CLEAN:
            ok = true;
            break;
        case SG_LIB_CAT_RECOVERED:
            {
                lock_guard<mutex> lg(console_mutex);

                fprintf(stderr, "Recovered error on READ_16, continuing\n");
            }
            ok = true;
            break;
        default: /* won't bother decoding other categories */
            {
                lock_guard<mutex> lg(console_mutex);

                sg_chk_n_print3("READ_16 command error", &pt, 1);
            }
            break;
        }
        if (ok) {
            while (((res = read(sg_fd, &pt2, sizeof(pt2))) < 0) &&
                   (EAGAIN == errno)) {
                ++eagains;
                if (wait_ms > 0)
                    this_thread::sleep_for(milliseconds{wait_ms});
                else if (0 == wait_ms)
                    this_thread::yield();
                else if (-2 == wait_ms)
                    sleep(0);                   // process yield ??
            }
            if (res < 0) {
                {
                    lock_guard<mutex> lg(console_mutex);

                    perror(" read(sg, READ_16) 2");
                }
                close(sg_fd);
                return -1;
            }
            pt = pt2;
            /* now for the error processing */
            ok = false;
            switch (sg_err_category3(&pt)) {
            case SG_LIB_CAT_CLEAN:
                ok = true;
                break;
            case SG_LIB_CAT_RECOVERED:
                {
                    lock_guard<mutex> lg(console_mutex);

                    fprintf(stderr, "%s: Recovered error on READ_16, "
                            "continuing 2\n", __func__);
                }
                ok = true;
                break;
            default: /* won't bother decoding other categories */
                {
                    lock_guard<mutex> lg(console_mutex);

                    sg_chk_n_print3("READ_16 command error 2", &pt, 1);
                }
                break;
            }
        }
        if (! ok) {
            close(sg_fd);
            return -1;
        }

        u = sg_get_unaligned_be32(lb);
        // Assuming u starts test as even (probably 0), expect it to stay even
        if (0 == k)
            odd = (1 == (u % 2));
        ++u;
        sg_put_unaligned_be32(u, lb);

        if (wait_ms > 0)       /* allow daylight for bad things ... */
            this_thread::sleep_for(milliseconds{wait_ms});
        else if (0 == wait_ms)
            this_thread::yield();
        else if (-2 == wait_ms)
            sleep(0);                   // process yield ??

        /* Prepare WRITE_16 command */
        memset(&pt, 0, sizeof(pt));
        pt.interface_id = 'S';
        pt.cmd_len = sizeof(w16CmdBlk);
        pt.mx_sb_len = sizeof(sense_buffer);
        pt.dxfer_direction = SG_DXFER_TO_DEV;
        pt.dxfer_len = WRITE16_REPLY_LEN;
        pt.dxferp = lb;
        pt.cmdp = w16CmdBlk;
        pt.sbp = sense_buffer;
        pt.timeout = 20000;     /* 20000 millisecs == 20 seconds */
        pt.pack_id = id;

        if (ioctl(sg_fd, SG_IO, &pt) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" WRITE_16 SG_IO ioctl error");
            }
            close(sg_fd);
            return -1;
        }
        /* now for the error processing */
        ok = false;
        switch (sg_err_category3(&pt)) {
        case SG_LIB_CAT_CLEAN:
            ok = true;
            break;
        case SG_LIB_CAT_RECOVERED:
            {
                lock_guard<mutex> lg(console_mutex);

                fprintf(stderr, "%s: Recovered error on WRITE_16, "
                        "continuing\n", __func__);
            }
            ok = true;
            break;
        default: /* won't bother decoding other categories */
            {
                lock_guard<mutex> lg(console_mutex);

                sg_chk_n_print3("WRITE_16 command error", &pt, 1);
            }
            break;
        }
        if (! ok) {
            close(sg_fd);
            return -1;
        }
    }
    close(sg_fd);
    return (int)odd;
}

/* Opens dev_name and spins if busy (i.e. gets EBUSY), sleeping for
 * wait_ms milliseconds if wait_ms is positive.
 * Reads lba (twice) and treats the first 4 bytes as an int (SCSI endian),
 * increments it and writes it back. Repeats so that happens twice. Then
 * closes dev_name. If an error occurs returns -1 else returns 0 if
 * first int read from lba is even otherwise returns 1. */
static int
do_rd_inc_wr_twice_v4(const char * dev_name, unsigned int lba, int block,
                      int excl, int wait_ms, int id, unsigned int & ebusy,
                      unsigned int & eagains)
{
    bool odd = false;
    int k, sg_fd;
    struct sg_io_v4 pt, pt2;
    unsigned char r16CmdBlk [READ16_CMD_LEN] =
                {0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
    unsigned char w16CmdBlk [WRITE16_CMD_LEN] =
                {0x8a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
    unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
    unsigned char lb[READ16_REPLY_LEN];
    int open_flags = O_RDWR;

    sg_put_unaligned_be64(lba, r16CmdBlk + 2);
    sg_put_unaligned_be64(lba, w16CmdBlk + 2);
    if (! block)
        open_flags |= O_NONBLOCK;
    if (excl)
        open_flags |= O_EXCL;

    while (((sg_fd = open(dev_name, open_flags)) < 0) &&
           (EBUSY == errno)) {
        ++ebusy;
        if (wait_ms > 0)
            this_thread::sleep_for(milliseconds{wait_ms});
        else if (0 == wait_ms)
            this_thread::yield();
        else if (-2 == wait_ms)
            sleep(0);                   // process yield ??
    }
    if (sg_fd < 0) {
        char ebuff[EBUFF_SZ];

        snprintf(ebuff, EBUFF_SZ, "%s: error opening file: %s", __func__,
                 dev_name);
        perror(ebuff);
        return -1;
    }

    for (k = 0; k < 2; ++k) {
        bool ok = false;
        int res;
        unsigned int u = 0;

        /* Prepare READ_16 command */
        memset(&pt, 0, sizeof(pt));
        pt.guard = 'Q';
        pt.request_len = sizeof(r16CmdBlk);
        pt.max_response_len = sizeof(sense_buffer);
        // pt.dxfer_direction = SG_DXFER_FROM_DEV;
        pt.din_xfer_len = READ16_REPLY_LEN;
        pt.din_xferp = (uint64_t)(sg_uintptr_t)lb;
        pt.request = (uint64_t)(sg_uintptr_t)r16CmdBlk;
        pt.response = (uint64_t)(sg_uintptr_t)sense_buffer;
        pt.timeout = 20000;     /* 20000 millisecs == 20 seconds */
        pt.request_extra = id;  /* pack_id field */

        // queue up two READ_16s to same LBA
        if (ioctl(sg_fd, SG_IOSUBMIT, &pt) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" write(sg, READ_16)");
            }
            close(sg_fd);
            return -1;
        }
        pt2 = pt;
        if (ioctl(sg_fd, SG_IOSUBMIT, &pt2) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" write(sg, READ_16) 2");
            }
            close(sg_fd);
            return -1;
        }

        while (((res = ioctl(sg_fd, SG_IORECEIVE, &pt)) < 0) &&
               (EAGAIN == errno)) {
            ++eagains;
            if (wait_ms > 0)
                this_thread::sleep_for(milliseconds{wait_ms});
            else if (0 == wait_ms)
                this_thread::yield();
            else if (-2 == wait_ms)
                sleep(0);                   // process yield ??
        }
        if (res < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" read(sg, READ_16)");
            }
            close(sg_fd);
            return -1;
        }
        /* now for the error processing */
        switch (sg_err_category_new(pt.device_status, pt.transport_status,
                pt.driver_status, sense_buffer, pt.response_len)) {
        case SG_LIB_CAT_CLEAN:
            ok = true;
            break;
        case SG_LIB_CAT_RECOVERED:
            {
                lock_guard<mutex> lg(console_mutex);

                fprintf(stderr, "Recovered error on READ_16, continuing\n");
            }
            ok = true;
            break;
        default: /* won't bother decoding other categories */
            {
                lock_guard<mutex> lg(console_mutex);

                sg_linux_sense_print("READ_16 command error",
                                     pt.device_status, pt.transport_status,
                                     pt.driver_status, sense_buffer,
                                     pt.response_len, true);
                // sg_chk_n_print3("READ_16 command error", &pt, 1);
            }
            break;
        }
        if (ok) {
            while (((res = ioctl(sg_fd, SG_IORECEIVE, &pt2)) < 0) &&
                   (EAGAIN == errno)) {
                ++eagains;
                if (wait_ms > 0)
                    this_thread::sleep_for(milliseconds{wait_ms});
                else if (0 == wait_ms)
                    this_thread::yield();
                else if (-2 == wait_ms)
                    sleep(0);                   // process yield ??
            }
            if (res < 0) {
                {
                    lock_guard<mutex> lg(console_mutex);

                    perror(" read(sg, READ_16) 2");
                }
                close(sg_fd);
                return -1;
            }
            pt = pt2;
            /* now for the error processing */
            ok = false;
            switch (sg_err_category_new(pt.device_status, pt.transport_status,
                    pt.driver_status, sense_buffer, pt.response_len)) {
            case SG_LIB_CAT_CLEAN:
                ok = true;
                break;
            case SG_LIB_CAT_RECOVERED:
                {
                    lock_guard<mutex> lg(console_mutex);

                    fprintf(stderr, "%s: Recovered error on READ_16, "
                            "continuing 2\n", __func__);
                }
                ok = true;
                break;
            default: /* won't bother decoding other categories */
                {
                    lock_guard<mutex> lg(console_mutex);

                    sg_linux_sense_print("READ_16 command error 2",
                                         pt.device_status,
                                         pt.transport_status,
                                         pt.driver_status, sense_buffer,
                                         pt.response_len, true);
                    // sg_chk_n_print3("READ_16 command error 2", &pt, 1);
                }
                break;
            }
        }
        if (! ok) {
            close(sg_fd);
            return -1;
        }

        u = sg_get_unaligned_be32(lb);
        // Assuming u starts test as even (probably 0), expect it to stay even
        if (0 == k)
            odd = (1 == (u % 2));
        ++u;
        sg_put_unaligned_be32(u, lb);

        if (wait_ms > 0)       /* allow daylight for bad things ... */
            this_thread::sleep_for(milliseconds{wait_ms});
        else if (0 == wait_ms)
            this_thread::yield();
        else if (-2 == wait_ms)
            sleep(0);                   // process yield ??

        /* Prepare WRITE_16 command */
        memset(&pt, 0, sizeof(pt));
        pt.guard = 'Q';
        pt.request_len = sizeof(w16CmdBlk);
        pt.max_response_len = sizeof(sense_buffer);
        // pt.dxfer_direction = SG_DXFER_TO_DEV;
        pt.dout_xfer_len = WRITE16_REPLY_LEN;
        pt.dout_xferp = (uint64_t)(sg_uintptr_t)lb;
        pt.request = (uint64_t)(sg_uintptr_t)w16CmdBlk;
        pt.response = (uint64_t)(sg_uintptr_t)sense_buffer;
        pt.timeout = 20000;     /* 20000 millisecs == 20 seconds */
        pt.request_extra = id;  /* pack_id field */

        if (ioctl(sg_fd, SG_IO, &pt) < 0) {
            {
                lock_guard<mutex> lg(console_mutex);

                perror(" WRITE_16 SG_IO ioctl error");
            }
            close(sg_fd);
            return -1;
        }
        /* now for the error processing */
        ok = false;
        switch (sg_err_category_new(pt.device_status, pt.transport_status,
                pt.driver_status, sense_buffer, pt.response_len)) {
        case SG_LIB_CAT_CLEAN:
            ok = true;
            break;
        case SG_LIB_CAT_RECOVERED:
            {
                lock_guard<mutex> lg(console_mutex);

                fprintf(stderr, "%s: Recovered error on WRITE_16, "
                        "continuing\n", __func__);
            }
            ok = true;
            break;
        default: /* won't bother decoding other categories */
            {
                lock_guard<mutex> lg(console_mutex);

                sg_linux_sense_print("WRITE_16 command error",
                                     pt.device_status, pt.transport_status,
                                     pt.driver_status, sense_buffer,
                                     pt.response_len, true);
            }
            break;
        }
        if (! ok) {
            close(sg_fd);
            return -1;
        }
    }
    close(sg_fd);
    return odd;
}



#define INQ_REPLY_LEN 96
#define INQ_CMD_LEN 6

/* Send INQUIRY and fetches response. If okay puts PRODUCT ID field
 * in b (up to m_blen bytes). Does not use O_EXCL flag. Returns 0 on success,
 * else -1 . */
static int
do_inquiry_prod_id(const char * dev_name, int block, int wait_ms,
                   unsigned int & ebusys, char * b, int b_mlen)
{
    int sg_fd, ok, ret;
    struct sg_io_hdr pt;
    unsigned char inqCmdBlk [INQ_CMD_LEN] =
                                {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
    unsigned char inqBuff[INQ_REPLY_LEN];
    unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
    int open_flags = O_RDWR;    /* O_EXCL | O_RDONLY fails with EPERM */

    if (! block)
        open_flags |= O_NONBLOCK;
    while (((sg_fd = open(dev_name, open_flags)) < 0) &&
           (EBUSY == errno)) {
        ++ebusys;
        if (wait_ms > 0)
            this_thread::sleep_for(milliseconds{wait_ms});
        else if (0 == wait_ms)
            this_thread::yield();
        else if (-2 == wait_ms)
            sleep(0);                   // process yield ??
    }
    if (sg_fd < 0) {
        char ebuff[EBUFF_SZ];

        snprintf(ebuff, EBUFF_SZ,
                 "do_inquiry_prod_id: error opening file: %s", dev_name);
        perror(ebuff);
        return -1;
    }
    /* Prepare INQUIRY command */
    memset(&pt, 0, sizeof(pt));
    pt.interface_id = 'S';
    pt.cmd_len = sizeof(inqCmdBlk);
    /* pt.iovec_count = 0; */  /* memset takes care of this */
    pt.mx_sb_len = sizeof(sense_buffer);
    pt.dxfer_direction = SG_DXFER_FROM_DEV;
    pt.dxfer_len = INQ_REPLY_LEN;
    pt.dxferp = inqBuff;
    pt.cmdp = inqCmdBlk;
    pt.sbp = sense_buffer;
    pt.timeout = 20000;     /* 20000 millisecs == 20 seconds */
    /* pt.flags = 0; */     /* take defaults: indirect IO, etc */
    /* pt.pack_id = 0; */
    /* pt.usr_ptr = NULL; */

    if (ioctl(sg_fd, SG_IO, &pt) < 0) {
        perror("do_inquiry_prod_id: Inquiry SG_IO ioctl error");
        close(sg_fd);
        return -1;
    }

    /* now for the error processing */
    ok = 0;
    switch (sg_err_category3(&pt)) {
    case SG_LIB_CAT_CLEAN:
        ok = 1;
        break;
    case SG_LIB_CAT_RECOVERED:
        fprintf(stderr, "Recovered error on INQUIRY, continuing\n");
        ok = 1;
        break;
    default: /* won't bother decoding other categories */
        sg_chk_n_print3("INQUIRY command error", &pt, 1);
        break;
    }
    if (ok) {
        /* Good, so fetch Product ID from response, copy to 'b' */
        if (b_mlen > 0) {
            if (b_mlen > 16) {
                memcpy(b, inqBuff + 16, 16);
                b[16] = '\0';
            } else {
                memcpy(b, inqBuff + 16, b_mlen - 1);
                b[b_mlen - 1] = '\0';
            }
        }
        ret = 0;
    } else
        ret = -1;
    close(sg_fd);
    return ret;
}

static void
work_thread(const char * dev_name, unsigned int lba, int id, int block,
            int excl, int num, int wait_ms)
{
    unsigned int thr_odd_count = 0;
    unsigned int thr_ebusy_count = 0;
    unsigned int thr_eagain_count = 0;
    int k, res;

    {
        lock_guard<mutex> lg(console_mutex);

        cerr << "Enter work_thread id=" << id << " excl=" << excl << " block="
             << block << endl;
    }
    for (k = 0; k < num; ++k) {
        if (sg_ifc_ver == 3)
            res = do_rd_inc_wr_twice_v3(dev_name, lba, block, excl, wait_ms,
                                        k, thr_ebusy_count, thr_eagain_count);
        else if (sg_ifc_ver == 4)
            res = do_rd_inc_wr_twice_v4(dev_name, lba, block, excl, wait_ms,
                                        k, thr_ebusy_count, thr_eagain_count);
        else {
            lock_guard<mutex> lg(console_mutex);

            cerr << "sg_ifc_ver=" << sg_ifc_ver << " not supported" << endl;
            res = -1;
        }
        if (res < 0)
            break;
        if (res)
            ++thr_odd_count;
    }
    {
        lock_guard<mutex> lg(console_mutex);

        if (k < num)
            cerr << "thread id=" << id << " FAILed at iteration: " << k <<
                    '\n';
        else
            cerr << "thread id=" << id << " normal exit" << '\n';
    }
    {
        lock_guard<mutex> lg(odd_count_mutex);

        odd_count += thr_odd_count;
        ebusy_count += thr_ebusy_count;
        eagain_count += thr_eagain_count;
    }
}


int
main(int argc, char * argv[])
{
    int k;
    int block = 0;
    int force = 0;
    unsigned int lba = DEF_LBA;
    int num_per_thread = DEF_NUM_PER_THREAD;
    int num_threads = DEF_NUM_THREADS;
    int wait_ms = DEF_WAIT_MS;
    int no_o_excl = 0;
    char * dev_name = NULL;

    for (k = 1; k < argc; ++k) {
        if (0 == memcmp("-b", argv[k], 2))
            ++block;
        else if (0 == memcmp("-f", argv[k], 2))
            ++force;
        else if (0 == memcmp("-h", argv[k], 2)) {
            usage();
            return 0;
        } else if (0 == memcmp("-i", argv[k], 2)) {
            ++k;
            if ((k < argc) && isdigit(*argv[k]))
                sg_ifc_ver = atoi(argv[k]);
            else
                break;
        } else if (0 == memcmp("-l", argv[k], 2)) {
            ++k;
            if ((k < argc) && isdigit(*argv[k]))
                lba = (unsigned int)atoi(argv[k]);
            else
                break;
        } else if (0 == memcmp("-n", argv[k], 2)) {
            ++k;
            if ((k < argc) && isdigit(*argv[k]))
                num_per_thread = atoi(argv[k]);
            else
                break;
        } else if (0 == memcmp("-t", argv[k], 2)) {
            ++k;
            if ((k < argc) && isdigit(*argv[k]))
                num_threads = atoi(argv[k]);
            else
                break;
        } else if (0 == memcmp("-V", argv[k], 2)) {
            printf("%s version: %s\n", util_name, version_str);
            return 0;
        } else if (0 == memcmp("-w", argv[k], 2)) {
            ++k;
            if ((k < argc) && (isdigit(*argv[k]) || ('-' == *argv[k]))) {
                if ('-' == *argv[k])
                    wait_ms = - atoi(argv[k] + 1);
                else
                    wait_ms = atoi(argv[k]);
            } else
                break;
        } else if (0 == memcmp("-xxx", argv[k], 4))
            no_o_excl += 3;
        else if (0 == memcmp("-xx", argv[k], 3))
            no_o_excl += 2;
        else if (0 == memcmp("-x", argv[k], 2))
            ++no_o_excl;
        else if (*argv[k] == '-') {
            printf("Unrecognized switch: %s\n", argv[k]);
            dev_name = NULL;
            break;
        }
        else if (! dev_name)
            dev_name = argv[k];
        else {
            printf("too many arguments\n");
            dev_name = 0;
            break;
        }
    }
    if (0 == dev_name) {
        usage();
        return 1;
    }
    try {
        struct stat a_stat;

        if (stat(dev_name, &a_stat) < 0) {
            perror("stat() on dev_name failed");
            return 1;
        }
        if (! S_ISCHR(a_stat.st_mode)) {
            fprintf(stderr, "%s should be a sg device which is a char "
                    "device. %s\n", dev_name, dev_name);
            fprintf(stderr, "is not a char device and damage could be done "
                    "if it is a BLOCK\ndevice, exiting ...\n");
            return 1;
        }
        if (! force) {
            char b[64];
            int res = do_inquiry_prod_id(dev_name, block, wait_ms,
                                         ebusy_count, b, sizeof(b));

            if (res) {
                fprintf(stderr, "INQUIRY failed on %s\n", dev_name);
                return 1;
            }
            // For safety, since <lba> written to, only permit scsi_debug
            // devices. Bypass this with '-f' option.
            if (0 != memcmp("scsi_debug", b, 10)) {
                fprintf(stderr, "Since this utility writes to LBA %u, only "
                        "devices with scsi_debug\nproduct ID accepted.\n",
                        lba);
                return 2;
            }
        }

        vector<thread *> vt;

        for (k = 0; k < num_threads; ++k) {
            int excl = 1;

            if (no_o_excl > 1)
                excl = 0;
            else if ((0 == k) && (1 == no_o_excl))
                excl = 0;

            thread * tp = new thread {work_thread, dev_name, lba, k, block,
                                      excl, num_per_thread, wait_ms};
            vt.push_back(tp);
        }

        // g++ 4.7.3 didn't like range-for loop here
        for (k = 0; k < (int)vt.size(); ++k)
            vt[k]->join();

        for (k = 0; k < (int)vt.size(); ++k)
            delete vt[k];

        if (no_o_excl)
            cout << "Odd count: " << odd_count << endl;
        else
            cout << "Expecting odd count of 0, got " << odd_count << endl;
        cout << "Number of EBUSYs: " << ebusy_count << endl;
        cout << "Number of EAGAINs: " << eagain_count << endl;

    }
    catch(system_error& e)  {
        cerr << "got a system_error exception: " << e.what() << '\n';
        auto ec = e.code();
        cerr << "category: " << ec.category().name() << '\n';
        cerr << "value: " << ec.value() << '\n';
        cerr << "message: " << ec.message() << '\n';
        cerr << "\nNote: if g++ may need '-pthread' or similar in "
                "compile/link line" << '\n';
    }
    catch(...) {
        cerr << "got another exception: " << '\n';
    }
    return 0;
}