• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* A utility program for copying files. Specialised for "files" that
2  * represent devices that understand the SCSI command set.
3  *
4  * Copyright (C) 1999 - 2022 D. Gilbert and P. Allworth
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  *
12  * This program is a specialisation of the Unix "dd" command in which
13  * either the input or the output file is a scsi generic device, raw
14  * device, a block device or a normal file. The logical block size ('bs')
15  * is assumed to be 512 if not given. This program complains if 'ibs' or
16  * 'obs' are given with a value that differs from 'bs' (or the default 512).
17  * If 'if' is not given or 'if=-' then stdin is assumed. If 'of' is
18  * not given or 'of=-' then stdout assumed.
19  *
20  * A non-standard argument "bpt" (blocks per transfer) is added to control
21  * the maximum number of blocks in each transfer. The default value is 128.
22  * For example if "bs=512" and "bpt=32" then a maximum of 32 blocks (16 KiB
23  * in this case) is transferred to or from the sg device in a single SCSI
24  * command. The actual size of the SCSI READ or WRITE command block can be
25  * selected with the "cdbsz" argument.
26  *
27  * This version is designed for the Linux kernel 2, 3, 4 and 5 series.
28  */
29 
30 #define _XOPEN_SOURCE 600
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE 1
33 #endif
34 
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <stdbool.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <time.h>
46 #include <limits.h>
47 #define __STDC_FORMAT_MACROS 1
48 #include <inttypes.h>
49 #include <sys/ioctl.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include <sys/file.h>
53 #include <sys/sysmacros.h>
54 #ifndef major
55 #include <sys/types.h>
56 #endif
57 #include <linux/major.h>        /* for MEM_MAJOR, SCSI_GENERIC_MAJOR, etc */
58 #include <linux/fs.h>           /* for BLKSSZGET and friends */
59 
60 #ifdef HAVE_CONFIG_H
61 #include "config.h"
62 #endif
63 #ifdef HAVE_GETRANDOM
64 #include <sys/random.h>         /* for getrandom() system call */
65 #endif
66 #include "sg_lib.h"
67 #include "sg_cmds_basic.h"
68 #include "sg_cmds_extra.h"
69 #include "sg_io_linux.h"
70 #include "sg_unaligned.h"
71 #include "sg_pr2serr.h"
72 
73 static const char * version_str = "6.35 20220826";
74 
75 
76 #define ME "sg_dd: "
77 
78 
79 #define STR_SZ 1024
80 #define INOUTF_SZ 512
81 #define EBUFF_SZ 768
82 
83 #define DEF_BLOCK_SIZE 512
84 #define DEF_BLOCKS_PER_TRANSFER 128
85 #define DEF_BLOCKS_PER_2048TRANSFER 32
86 #define DEF_SCSI_CDBSZ 10
87 #define MAX_SCSI_CDBSZ 16
88 #define MAX_BPT_VALUE (1 << 24)         /* used for maximum bs as well */
89 #define MAX_COUNT_SKIP_SEEK (1LL << 48) /* coverity wants upper bound */
90 
91 #define DEF_MODE_CDB_SZ 10
92 #define DEF_MODE_RESP_LEN 252
93 #define RW_ERR_RECOVERY_MP 1
94 #define CACHING_MP 8
95 #define CONTROL_MP 0xa
96 
97 #define SENSE_BUFF_LEN 64       /* Arbitrary, could be larger */
98 #define READ_CAP_REPLY_LEN 8
99 #define RCAP16_REPLY_LEN 32
100 #define READ_LONG_OPCODE 0x3E
101 #define READ_LONG_CMD_LEN 10
102 #define READ_LONG_DEF_BLK_INC 8
103 #define VERIFY10 0x2f
104 #define VERIFY12 0xaf
105 #define VERIFY16 0x8f
106 
107 #define DEF_TIMEOUT 60000       /* 60,000 millisecs == 60 seconds */
108 
109 #ifndef RAW_MAJOR
110 #define RAW_MAJOR 255   /*unlikely value */
111 #endif
112 
113 #define SG_LIB_FLOCK_ERR 90
114 
115 #define FT_OTHER 1              /* filetype is probably normal */
116 #define FT_SG 2                 /* filetype is sg char device or supports
117                                    SG_IO ioctl */
118 #define FT_RAW 4                /* filetype is raw char device */
119 #define FT_DEV_NULL 8           /* either "/dev/null" or "." as filename */
120 #define FT_ST 16                /* filetype is st char device (tape) */
121 #define FT_BLOCK 32             /* filetype is block device */
122 #define FT_FIFO 64              /* filetype is a fifo (name pipe) */
123 #define FT_NVME 128             /* NVMe char device (e.g. /dev/nvme2) */
124 #define FT_RANDOM_0_FF 256      /* iflag=00, iflag=ff and iflag=random
125                                    overriding if=IFILE */
126 #define FT_ERROR 512            /* couldn't "stat" file */
127 
128 #define DEV_NULL_MINOR_NUM 3
129 
130 #define SG_DD_BYPASS 999        /* failed but coe set */
131 
132 /* If platform does not support O_DIRECT then define it harmlessly */
133 #ifndef O_DIRECT
134 #define O_DIRECT 0
135 #endif
136 
137 #define MIN_RESERVED_SIZE 8192
138 
139 #define MAX_UNIT_ATTENTIONS 10
140 #define MAX_ABORTED_CMDS 256
141 
142 #define PROGRESS_TRIGGER_MS 120000      /* milliseconds: 2 minutes */
143 #define PROGRESS2_TRIGGER_MS 60000      /* milliseconds: 1 minute */
144 #define PROGRESS3_TRIGGER_MS 30000      /* milliseconds: 30 seconds */
145 
146 static int sum_of_resids = 0;
147 
148 static int64_t dd_count = -1;
149 static int64_t req_count = 0;
150 static int64_t in_full = 0;
151 static int in_partial = 0;
152 static int64_t out_full = 0;
153 static int out_partial = 0;
154 static int64_t out_sparse_num = 0;
155 static int recovered_errs = 0;
156 static int unrecovered_errs = 0;
157 static int miscompare_errs = 0;
158 static int read_longs = 0;
159 static int num_retries = 0;
160 static int progress = 0;
161 static int dry_run = 0;
162 
163 static bool do_time = false;
164 static bool start_tm_valid = false;
165 static bool do_verify = false;          /* when false: do copy */
166 static int verbose = 0;
167 static int blk_sz = 0;
168 static int max_uas = MAX_UNIT_ATTENTIONS;
169 static int max_aborted = MAX_ABORTED_CMDS;
170 static int coe_limit = 0;
171 static int coe_count = 0;
172 static int cmd_timeout = DEF_TIMEOUT;   /* in milliseconds */
173 static uint32_t glob_pack_id = 0;       /* pre-increment */
174 static struct timeval start_tm;
175 
176 static uint8_t * zeros_buff = NULL;
177 static uint8_t * free_zeros_buff = NULL;
178 static int read_long_blk_inc = READ_LONG_DEF_BLK_INC;
179 
180 static long seed;
181 #ifdef HAVE_SRAND48_R   /* gcc extension. N.B. non-reentrant version slower */
182 static struct drand48_data drand;/* opaque, used by srand48_r and mrand48_r */
183 #endif
184 
185 static const char * sg_allow_dio = "/sys/module/sg/parameters/allow_dio";
186 
187 struct flags_t {
188     bool append;
189     bool dio;
190     bool direct;
191     bool dpo;
192     bool dsync;
193     bool excl;
194     bool flock;
195     bool ff;
196     bool fua;
197     bool nocreat;
198     bool random;
199     bool sgio;
200     bool sparse;
201     bool zero;
202     int cdbsz;
203     int cdl;
204     int coe;
205     int nocache;
206     int pdt;
207     int retries;
208 };
209 
210 static struct flags_t iflag;
211 static struct flags_t oflag;
212 
213 static void calc_duration_throughput(bool contin);
214 
215 
216 static void
install_handler(int sig_num,void (* sig_handler)(int sig))217 install_handler(int sig_num, void (*sig_handler)(int sig))
218 {
219     struct sigaction sigact;
220 
221     sigaction(sig_num, NULL, &sigact);
222     if (sigact.sa_handler != SIG_IGN) {
223         sigact.sa_handler = sig_handler;
224         sigemptyset(&sigact.sa_mask);
225         sigact.sa_flags = 0;
226         sigaction(sig_num, &sigact, NULL);
227     }
228 }
229 
230 
231 static void
print_stats(const char * str)232 print_stats(const char * str)
233 {
234     if (0 != dd_count)
235         pr2serr("  remaining block count=%" PRId64 "\n", dd_count);
236     pr2serr("%s%" PRId64 "+%d records in\n", str, in_full - in_partial,
237             in_partial);
238     pr2serr("%s%" PRId64 "+%d records %s\n", str, out_full - out_partial,
239             out_partial, (do_verify ? "verified" : "out"));
240     if (oflag.sparse)
241         pr2serr("%s%" PRId64 " bypassed records out\n", str, out_sparse_num);
242     if (recovered_errs > 0)
243         pr2serr("%s%d recovered errors\n", str, recovered_errs);
244     if (num_retries > 0)
245         pr2serr("%s%d retries attempted\n", str, num_retries);
246     if (unrecovered_errs > 0) {
247         pr2serr("%s%d unrecovered error(s)\n", str, unrecovered_errs);
248         if (iflag.coe || oflag.coe)
249             pr2serr("%s%d read_longs fetched part of unrecovered read "
250                     "errors\n", str, read_longs);
251     }
252     if (miscompare_errs > 0)
253         pr2serr("%s%d miscompare error(s)\n", str, miscompare_errs);
254 }
255 
256 
257 static void
interrupt_handler(int sig)258 interrupt_handler(int sig)
259 {
260     struct sigaction sigact;
261 
262     sigact.sa_handler = SIG_DFL;
263     sigemptyset(&sigact.sa_mask);
264     sigact.sa_flags = 0;
265     sigaction(sig, &sigact, NULL);
266     pr2serr("Interrupted by signal,");
267     if (do_time)
268         calc_duration_throughput(false);
269     print_stats("");
270     kill(getpid (), sig);
271 }
272 
273 
274 static void
siginfo_handler(int sig)275 siginfo_handler(int sig)
276 {
277     if (sig) { ; }      /* unused, dummy to suppress warning */
278     pr2serr("Progress report, continuing ...\n");
279     if (do_time)
280         calc_duration_throughput(true);
281     print_stats("  ");
282 }
283 
284 static bool bsg_major_checked = false;
285 static int bsg_major = 0;
286 
287 static void
find_bsg_major(void)288 find_bsg_major(void)
289 {
290     int n;
291     char *cp;
292     FILE *fp;
293     const char *proc_devices = "/proc/devices";
294     char a[128];
295     char b[128];
296 
297     if (NULL == (fp = fopen(proc_devices, "r"))) {
298         if (verbose)
299             pr2serr("fopen %s failed: %s\n", proc_devices, strerror(errno));
300         return;
301     }
302     while ((cp = fgets(b, sizeof(b), fp))) {
303         if ((1 == sscanf(b, "%126s", a)) &&
304             (0 == memcmp(a, "Character", 9)))
305             break;
306     }
307     while (cp && (cp = fgets(b, sizeof(b), fp))) {
308         if (2 == sscanf(b, "%d %126s", &n, a)) {
309             if (0 == strcmp("bsg", a)) {
310                 bsg_major = n;
311                 break;
312             }
313         } else
314             break;
315     }
316     if (verbose > 5) {
317         if (cp)
318             pr2serr("found bsg_major=%d\n", bsg_major);
319         else
320             pr2serr("found no bsg char device in %s\n", proc_devices);
321     }
322     fclose(fp);
323 }
324 
325 static bool nvme_major_checked = false;
326 static int nvme_major = 0;
327 
328 static void
find_nvme_major(void)329 find_nvme_major(void)
330 {
331     int n;
332     char *cp;
333     FILE *fp;
334     const char *proc_devices = "/proc/devices";
335     char a[128];
336     char b[128];
337 
338     if (NULL == (fp = fopen(proc_devices, "r"))) {
339         if (verbose)
340             pr2serr("fopen %s failed: %s\n", proc_devices, strerror(errno));
341         return;
342     }
343     while ((cp = fgets(b, sizeof(b), fp))) {
344         if ((1 == sscanf(b, "%126s", a)) &&
345             (0 == memcmp(a, "Character", 9)))
346             break;
347     }
348     while (cp && (cp = fgets(b, sizeof(b), fp))) {
349         if (2 == sscanf(b, "%d %126s", &n, a)) {
350             if (0 == strcmp("nvme", a)) {
351                 nvme_major = n;
352                 break;
353             }
354         } else
355             break;
356     }
357     if (verbose > 5) {
358         if (cp)
359             pr2serr("found nvme_major=%d\n", bsg_major);
360         else
361             pr2serr("found no nvme char device in %s\n", proc_devices);
362     }
363     fclose(fp);
364 }
365 
366 
367 static int
dd_filetype(const char * filename)368 dd_filetype(const char * filename)
369 {
370     size_t len = strlen(filename);
371     struct stat st;
372 
373     if ((1 == len) && ('.' == filename[0]))
374         return FT_DEV_NULL;
375     if (stat(filename, &st) < 0)
376         return FT_ERROR;
377     if (S_ISCHR(st.st_mode)) {
378         /* major() and minor() defined in sys/sysmacros.h */
379         if ((MEM_MAJOR == major(st.st_rdev)) &&
380             (DEV_NULL_MINOR_NUM == minor(st.st_rdev)))
381             return FT_DEV_NULL;
382         if (RAW_MAJOR == major(st.st_rdev))
383             return FT_RAW;
384         if (SCSI_GENERIC_MAJOR == major(st.st_rdev))
385             return FT_SG;
386         if (SCSI_TAPE_MAJOR == major(st.st_rdev))
387             return FT_ST;
388         if (! bsg_major_checked) {
389             bsg_major_checked = true;
390             find_bsg_major();
391         }
392         if (bsg_major == (int)major(st.st_rdev))
393             return FT_SG;
394         if (! nvme_major_checked) {
395             nvme_major_checked = true;
396             find_nvme_major();
397         }
398         if (nvme_major == (int)major(st.st_rdev))
399             return FT_NVME;     /* treat as sg device */
400     } else if (S_ISBLK(st.st_mode))
401         return FT_BLOCK;
402     else if (S_ISFIFO(st.st_mode))
403         return FT_FIFO;
404     return FT_OTHER;
405 }
406 
407 
408 static char *
dd_filetype_str(int ft,char * buff)409 dd_filetype_str(int ft, char * buff)
410 {
411     int off = 0;
412 
413     if (FT_DEV_NULL & ft)
414         off += sg_scnpr(buff + off, 32, "null device ");
415     if (FT_SG & ft)
416         off += sg_scnpr(buff + off, 32, "SCSI generic (sg) device ");
417     if (FT_BLOCK & ft)
418         off += sg_scnpr(buff + off, 32, "block device ");
419     if (FT_FIFO & ft)
420         off += sg_scnpr(buff + off, 32, "fifo (named pipe) ");
421     if (FT_ST & ft)
422         off += sg_scnpr(buff + off, 32, "SCSI tape device ");
423     if (FT_RAW & ft)
424         off += sg_scnpr(buff + off, 32, "raw device ");
425     if (FT_NVME & ft)
426         off += sg_scnpr(buff + off, 32, "NVMe char device ");
427     if (FT_OTHER & ft)
428         off += sg_scnpr(buff + off, 32, "other (perhaps ordinary file) ");
429     if (FT_ERROR & ft)
430         sg_scnpr(buff + off, 32, "unable to 'stat' file ");
431     return buff;
432 }
433 
434 
435 static void
usage()436 usage()
437 {
438     pr2serr("Usage: sg_dd  [bs=BS] [conv=CONV] [count=COUNT] [ibs=BS] "
439             "[if=IFILE]\n"
440             "              [iflag=FLAGS] [obs=BS] [of=OFILE] [oflag=FLAGS] "
441             "[seek=SEEK]\n"
442             "              [skip=SKIP] [--dry-run] [--help] [--verbose] "
443             "[--version]\n\n"
444             "              [blk_sgio=0|1] [bpt=BPT] [cdbsz=6|10|12|16] "
445             "[cdl=CDL]\n"
446             "              [coe=0|1|2|3] [coe_limit=CL] [dio=0|1] "
447             "[odir=0|1]\n"
448             "              [of2=OFILE2] [retries=RETR] [sync=0|1] "
449             "[time=0|1[,TO]]\n"
450             "              [verbose=VERB] [--progress] [--verify]\n"
451             "  where:\n"
452             "    blk_sgio    0->block device use normal I/O(def), 1->use "
453             "SG_IO\n"
454             "    bpt         is blocks_per_transfer (default is 128 or 32 "
455             "when BS>=2048)\n"
456             "    bs          logical block size (default is 512)\n");
457     pr2serr("    cdbsz       size of SCSI READ or WRITE cdb (default is "
458             "10)\n"
459             "    cdl         command duration limits value 0 to 7 (def: "
460             "0 (no cdl))\n"
461             "    coe         0->exit on error (def), 1->continue on sg "
462             "error (zero\n"
463             "                fill), 2->also try read_long on unrecovered "
464             "reads,\n"
465             "                3->and set the CORRCT bit on the read long\n"
466             "    coe_limit   limit consecutive 'bad' blocks on reads to CL "
467             "times\n"
468             "                when COE>1 (default: 0 which is no limit)\n"
469             "    conv        comma separated list from: [nocreat,noerror,"
470             "notrunc,\n"
471             "                null,sparse,sync]\n"
472             "    count       number of blocks to copy (def: device size)\n"
473             "    dio         for direct IO, 1->attempt, 0->indirect IO "
474             "(def)\n"
475             "    ibs         input logical block size (if given must be same "
476             "as 'bs=')\n"
477             "    if          file or device to read from (def: stdin)\n"
478             "    iflag       comma separated list from: [00,coe,dio,direct,"
479             "dpo,dsync,\n"
480             "                excl,ff,flock,fua,nocache,null,random,sgio]\n"
481             "    obs         output logical block size (if given must be "
482             "same as 'bs=')\n"
483             "    odir        1->use O_DIRECT when opening block dev, "
484             "0->don't(def)\n"
485             "    of          file or device to write to (def: stdout), "
486             "OFILE of '.'\n");
487     pr2serr("                treated as /dev/null\n"
488             "    of2         additional output file (def: /dev/null), "
489             "OFILE2 should be\n"
490             "                normal file or pipe\n"
491             "    oflag       comma separated list from: [append,coe,dio,"
492             "direct,dpo,\n"
493             "                dsync,excl,flock,fua,nocache,nocreat,null,sgio,"
494             "sparse]\n"
495             "    retries     retry sgio errors RETR times (def: 0)\n"
496             "    seek        block position to start writing to OFILE\n"
497             "    skip        block position to start reading from IFILE\n"
498             "    sync        0->no sync(def), 1->SYNCHRONIZE CACHE on "
499             "OFILE after copy\n"
500             "    time        0->no timing(def), 1->time plus calculate "
501             "throughput;\n"
502             "                TO is command timeout in seconds (def: 60)\n"
503             "    verbose     0->quiet(def), 1->some noise, 2->more noise, "
504             "etc\n"
505             "    --dry-run    do preparation but bypass copy (or read)\n"
506             "    --help|-h    print out this usage message then exit\n"
507             "    --progress|-p    print progress report every 2 minutes\n"
508             "    --verbose|-v   same as 'verbose=1', can be used multiple "
509             "times\n"
510             "    --verify|-x    do verify/compare rather than copy "
511             "(OFILE must\n"
512             "                   be a sg device)\n"
513             "    --version|-V    print version information then exit\n\n"
514             "Copy from IFILE to OFILE, similar to dd command; specialized "
515             "for SCSI\ndevices. If the --verify option is given then IFILE "
516             "is read and that data\nis used to compare with OFILE using "
517             "the VERIFY(n) SCSI command (with\nBYTCHK=1).\n");
518 }
519 
520 
521 /* Return of 0 -> success, see sg_ll_read_capacity*() otherwise */
522 static int
scsi_read_capacity(int sg_fd,int64_t * num_sect,int * sect_sz)523 scsi_read_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
524 {
525     int res, verb;
526     unsigned int ui;
527     uint8_t rcBuff[RCAP16_REPLY_LEN];
528 
529     verb = (verbose ? verbose - 1: 0);
530     res = sg_ll_readcap_10(sg_fd, false, 0, rcBuff, READ_CAP_REPLY_LEN, true,
531                            verb);
532     if (0 != res)
533         return res;
534 
535     if ((0xff == rcBuff[0]) && (0xff == rcBuff[1]) && (0xff == rcBuff[2]) &&
536         (0xff == rcBuff[3])) {
537         int64_t ls;
538 
539         res = sg_ll_readcap_16(sg_fd, false, 0, rcBuff, RCAP16_REPLY_LEN,
540                                true, verb);
541         if (0 != res)
542             return res;
543         ls = (int64_t)sg_get_unaligned_be64(rcBuff);
544         *num_sect = ls + 1;
545         *sect_sz = (int)sg_get_unaligned_be32(rcBuff + 8);
546     } else {
547         ui = sg_get_unaligned_be32(rcBuff);
548         /* take care not to sign extend values > 0x7fffffff */
549         *num_sect = (int64_t)ui + 1;
550         *sect_sz = (int)sg_get_unaligned_be32(rcBuff + 4);
551     }
552     if (verbose)
553         pr2serr("      number of blocks=%" PRId64 " [0x%" PRIx64 "], "
554                 "logical block size=%d\n", *num_sect, *num_sect, *sect_sz);
555     return 0;
556 }
557 
558 
559 /* Return of 0 -> success, -1 -> failure. BLKGETSIZE64, BLKGETSIZE and */
560 /* BLKSSZGET macros problematic (from <linux/fs.h> or <sys/mount.h>). */
561 static int
read_blkdev_capacity(int sg_fd,int64_t * num_sect,int * sect_sz)562 read_blkdev_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
563 {
564 #ifdef BLKSSZGET
565     if ((ioctl(sg_fd, BLKSSZGET, sect_sz) < 0) && (*sect_sz > 0)) {
566         perror("BLKSSZGET ioctl error");
567         return -1;
568     } else {
569  #ifdef BLKGETSIZE64
570         uint64_t ull;
571 
572         if (ioctl(sg_fd, BLKGETSIZE64, &ull) < 0) {
573 
574             perror("BLKGETSIZE64 ioctl error");
575             return -1;
576         }
577         *num_sect = ((int64_t)ull / (int64_t)*sect_sz);
578         if (verbose)
579             pr2serr("      [bgs64] number of blocks=%" PRId64 " [0x%" PRIx64
580                     "], logical block size=%d\n", *num_sect, *num_sect,
581                     *sect_sz);
582  #else
583         unsigned long ul;
584 
585         if (ioctl(sg_fd, BLKGETSIZE, &ul) < 0) {
586             perror("BLKGETSIZE ioctl error");
587             return -1;
588         }
589         *num_sect = (int64_t)ul;
590         if (verbose)
591             pr2serr("      [bgs] number of blocks=%" PRId64 " [0x%" PRIx64
592                     "],  logical block size=%d\n", *num_sect, *num_sect,
593                     *sect_sz);
594  #endif
595     }
596     return 0;
597 #else
598     if (verbose)
599         pr2serr("      BLKSSZGET+BLKGETSIZE ioctl not available\n");
600     *num_sect = 0;
601     *sect_sz = 0;
602     return -1;
603 #endif
604 }
605 
606 
607 static int
sg_build_scsi_cdb(uint8_t * cdbp,int cdb_sz,unsigned int blocks,int64_t start_block,bool is_verify,bool write_true,bool fua,bool dpo,int cdl)608 sg_build_scsi_cdb(uint8_t * cdbp, int cdb_sz, unsigned int blocks,
609                   int64_t start_block, bool is_verify, bool write_true,
610                   bool fua, bool dpo, int cdl)
611 {
612     int sz_ind;
613     int rd_opcode[] = {0x8, 0x28, 0xa8, 0x88};
614     int ve_opcode[] = {0xff /* no VERIFY(6) */, VERIFY10, VERIFY12, VERIFY16};
615     int wr_opcode[] = {0xa, 0x2a, 0xaa, 0x8a};
616 
617     memset(cdbp, 0, cdb_sz);
618     if (is_verify)
619         cdbp[1] = 0x2;  /* (BYTCHK=1) << 1 */
620     else {
621         if (dpo)
622             cdbp[1] |= 0x10;
623         if (fua)
624             cdbp[1] |= 0x8;
625     }
626     switch (cdb_sz) {
627     case 6:
628         sz_ind = 0;
629         if (is_verify && write_true) {
630             pr2serr(ME "there is no VERIFY(6), choose a larger cdbsz\n");
631             return 1;
632         }
633         cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
634                                                rd_opcode[sz_ind]);
635         sg_put_unaligned_be24(0x1fffff & start_block, cdbp + 1);
636         cdbp[4] = (256 == blocks) ? 0 : (uint8_t)blocks;
637         if (blocks > 256) {
638             pr2serr(ME "for 6 byte commands, maximum number of blocks is "
639                     "256\n");
640             return 1;
641         }
642         if ((start_block + blocks - 1) & (~0x1fffff)) {
643             pr2serr(ME "for 6 byte commands, can't address blocks beyond "
644                     "%d\n", 0x1fffff);
645             return 1;
646         }
647         if (dpo || fua) {
648             pr2serr(ME "for 6 byte commands, neither dpo nor fua bits "
649                     "supported\n");
650             return 1;
651         }
652         break;
653     case 10:
654         sz_ind = 1;
655         if (is_verify && write_true)
656             cdbp[0] = ve_opcode[sz_ind];
657         else
658             cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
659                                              rd_opcode[sz_ind]);
660         sg_put_unaligned_be32(start_block, cdbp + 2);
661         sg_put_unaligned_be16(blocks, cdbp + 7);
662         if (blocks & (~0xffff)) {
663             pr2serr(ME "for 10 byte commands, maximum number of blocks is "
664                     "%d\n", 0xffff);
665             return 1;
666         }
667         break;
668     case 12:
669         sz_ind = 2;
670         if (is_verify && write_true)
671             cdbp[0] = ve_opcode[sz_ind];
672         else
673             cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
674                                              rd_opcode[sz_ind]);
675         sg_put_unaligned_be32(start_block, cdbp + 2);
676         sg_put_unaligned_be32(blocks, cdbp + 6);
677         break;
678     case 16:
679         sz_ind = 3;
680         if (is_verify && write_true)
681             cdbp[0] = ve_opcode[sz_ind];
682         else
683             cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
684                                              rd_opcode[sz_ind]);
685         if ((! is_verify) && (cdl > 0)) {
686             if (cdl & 0x4)
687                 cdbp[1] |= 0x1;
688             if (cdl & 0x3)
689                 cdbp[14] |= ((cdl & 0x3) << 6);
690         }
691         sg_put_unaligned_be64(start_block, cdbp + 2);
692         sg_put_unaligned_be32(blocks, cdbp + 10);
693         break;
694     default:
695         pr2serr(ME "expected cdb size of 6, 10, 12, or 16 but got %d\n",
696                 cdb_sz);
697         return 1;
698     }
699     return 0;
700 }
701 
702 
703 /* Does SCSI READ on IFILE. Returns 0 -> successful,
704  * SG_LIB_SYNTAX_ERROR -> unable to build cdb,
705  * SG_LIB_CAT_UNIT_ATTENTION -> try again,
706  * SG_LIB_CAT_MEDIUM_HARD_WITH_INFO -> 'io_addrp' written to,
707  * SG_LIB_CAT_MEDIUM_HARD -> no info field,
708  * SG_LIB_CAT_NOT_READY, SG_LIB_CAT_ABORTED_COMMAND,
709  * -2 -> ENOMEM, -1 other errors */
710 static int
sg_read_low(int sg_fd,uint8_t * buff,int blocks,int64_t from_block,int bs,const struct flags_t * ifp,bool * diop,uint64_t * io_addrp)711 sg_read_low(int sg_fd, uint8_t * buff, int blocks, int64_t from_block,
712             int bs, const struct flags_t * ifp, bool * diop,
713             uint64_t * io_addrp)
714 {
715     bool info_valid;
716     bool print_cdb_after = false;
717     int res, slen;
718     const uint8_t * sbp;
719     uint8_t rdCmd[MAX_SCSI_CDBSZ];
720     uint8_t senseBuff[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
721     struct sg_io_hdr io_hdr;
722 
723     if (sg_build_scsi_cdb(rdCmd, ifp->cdbsz, blocks, from_block, do_verify,
724                           false, ifp->fua, ifp->dpo, ifp->cdl)) {
725         pr2serr(ME "bad rd cdb build, from_block=%" PRId64 ", blocks=%d\n",
726                 from_block, blocks);
727         return SG_LIB_SYNTAX_ERROR;
728     }
729 
730     memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
731     io_hdr.interface_id = 'S';
732     io_hdr.cmd_len = ifp->cdbsz;
733     io_hdr.cmdp = rdCmd;
734     io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
735     io_hdr.dxfer_len = bs * blocks;
736     io_hdr.dxferp = buff;
737     io_hdr.mx_sb_len = SENSE_BUFF_LEN;
738     io_hdr.sbp = senseBuff;
739     io_hdr.timeout = cmd_timeout;
740     io_hdr.pack_id = (int)++glob_pack_id;
741     if (diop && *diop)
742         io_hdr.flags |= SG_FLAG_DIRECT_IO;
743 
744     if (verbose > 2)
745         sg_print_command_len(rdCmd, ifp->cdbsz);
746 
747     while (((res = ioctl(sg_fd, SG_IO, &io_hdr)) < 0) &&
748            ((EINTR == errno) || (EAGAIN == errno) || (EBUSY == errno)))
749         ;
750     if (res < 0) {
751         if (ENOMEM == errno)
752             return -2;
753         perror("reading (SG_IO) on sg device, error");
754         return -1;
755     }
756     if (verbose > 2)
757         pr2serr("      duration=%u ms\n", io_hdr.duration);
758     res = sg_err_category3(&io_hdr);
759     sbp = io_hdr.sbp;
760     slen = io_hdr.sb_len_wr;
761     switch (res) {
762     case SG_LIB_CAT_CLEAN:
763     case SG_LIB_CAT_CONDITION_MET:
764         break;
765     case SG_LIB_CAT_RECOVERED:
766         ++recovered_errs;
767         info_valid = sg_get_sense_info_fld(sbp, slen, io_addrp);
768         if (info_valid) {
769             pr2serr("    lba of last recovered error in this READ=0x%" PRIx64
770                     "\n", *io_addrp);
771             if (verbose > 1)
772                 sg_chk_n_print3("reading", &io_hdr, true);
773         } else {
774             pr2serr("Recovered error: [no info] reading from block=0x%" PRIx64
775                     ", num=%d\n", from_block, blocks);
776             sg_chk_n_print3("reading", &io_hdr, verbose > 1);
777         }
778         break;
779     case SG_LIB_CAT_ABORTED_COMMAND:
780     case SG_LIB_CAT_UNIT_ATTENTION:
781         sg_chk_n_print3("reading", &io_hdr, verbose > 1);
782         return res;
783     case SG_LIB_CAT_MEDIUM_HARD:
784         if (verbose > 1)
785             sg_chk_n_print3("reading", &io_hdr, verbose > 1);
786         ++unrecovered_errs;
787         info_valid = sg_get_sense_info_fld(sbp, slen, io_addrp);
788         /* MMC devices don't necessarily set VALID bit */
789         if (info_valid || ((5 == ifp->pdt) && (*io_addrp > 0)))
790             return SG_LIB_CAT_MEDIUM_HARD_WITH_INFO;
791         else {
792             pr2serr("Medium, hardware or blank check error but no lba of "
793                     "failure in sense\n");
794             return res;
795         }
796         break;
797     case SG_LIB_CAT_NOT_READY:
798         ++unrecovered_errs;
799         if (verbose > 0)
800             sg_chk_n_print3("reading", &io_hdr, verbose > 1);
801         return res;
802     case SG_LIB_CAT_ILLEGAL_REQ:
803         if (5 == ifp->pdt) {    /* MMC READs can go down this path */
804             bool ili;
805             struct sg_scsi_sense_hdr ssh;
806 
807             if (verbose > 1)
808                 sg_chk_n_print3("reading", &io_hdr, verbose > 1);
809             if (sg_scsi_normalize_sense(sbp, slen, &ssh) &&
810                 (0x64 == ssh.asc) && (0x0 == ssh.ascq)) {
811                 if (sg_get_sense_filemark_eom_ili(sbp, slen, NULL, NULL,
812                                                   &ili) && ili) {
813                     sg_get_sense_info_fld(sbp, slen, io_addrp);
814                     if (*io_addrp > 0) {
815                         ++unrecovered_errs;
816                         return SG_LIB_CAT_MEDIUM_HARD_WITH_INFO;
817                     } else
818                         pr2serr("MMC READ gave 'illegal mode for this track' "
819                                 "and ILI but no LBA of failure\n");
820                 }
821                 ++unrecovered_errs;
822                 return SG_LIB_CAT_MEDIUM_HARD;
823             }
824         }
825         if (verbose > 0)
826             print_cdb_after = true;
827 #if defined(__GNUC__)
828 #if (__GNUC__ >= 7)
829         __attribute__((fallthrough));
830         /* FALL THROUGH */
831 #endif
832 #endif
833     default:
834         ++unrecovered_errs;
835         if (verbose > 0)
836             sg_chk_n_print3("reading", &io_hdr, verbose > 1);
837         if (print_cdb_after)
838             sg_print_command_len(rdCmd, ifp->cdbsz);
839         return res;
840     }
841     if (diop && *diop &&
842         ((io_hdr.info & SG_INFO_DIRECT_IO_MASK) != SG_INFO_DIRECT_IO))
843         *diop = false;      /* flag that dio not done (completely) */
844     sum_of_resids += io_hdr.resid;
845     return 0;
846 }
847 
848 
849 /* Does repeats associated with a SCSI READ on IFILE. Returns 0 -> successful,
850  * SG_LIB_SYNTAX_ERROR  -> unable to build cdb, SG_LIB_CAT_UNIT_ATTENTION ->
851  * try again, SG_LIB_CAT_NOT_READY, SG_LIB_CAT_MEDIUM_HARD,
852  * SG_LIB_CAT_ABORTED_COMMAND, -2 -> ENOMEM, -1 other errors */
853 static int
sg_read(int sg_fd,uint8_t * buff,int blocks,int64_t from_block,int bs,struct flags_t * ifp,bool * diop,int * blks_readp)854 sg_read(int sg_fd, uint8_t * buff, int blocks, int64_t from_block,
855         int bs, struct flags_t * ifp, bool * diop, int * blks_readp)
856 {
857     bool may_coe = false;
858     bool repeat;
859     int res, blks, xferred;
860     int ret = 0;
861     int retries_tmp;
862     uint64_t io_addr;
863     int64_t lba;
864     uint8_t * bp;
865 
866     retries_tmp = ifp->retries;
867     for (xferred = 0, blks = blocks, lba = from_block, bp = buff;
868          blks > 0; blks = blocks - xferred) {
869         io_addr = 0;
870         repeat = false;
871         may_coe = false;
872         res = sg_read_low(sg_fd, bp, blks, lba, bs, ifp, diop, &io_addr);
873         switch (res) {
874         case 0:
875             if (blks_readp)
876                 *blks_readp = xferred + blks;
877             if (coe_limit > 0)
878                 coe_count = 0;  /* good read clears coe_count */
879             return 0;
880         case -2:        /* ENOMEM */
881             return res;
882         case SG_LIB_CAT_NOT_READY:
883             pr2serr("Device (r) not ready\n");
884             return res;
885         case SG_LIB_CAT_ABORTED_COMMAND:
886             if (--max_aborted > 0) {
887                 pr2serr("Aborted command, continuing (r)\n");
888                 repeat = true;
889             } else {
890                 pr2serr("Aborted command, too many (r)\n");
891                 return res;
892             }
893             break;
894         case SG_LIB_CAT_UNIT_ATTENTION:
895             if (--max_uas > 0) {
896                 pr2serr("Unit attention, continuing (r)\n");
897                 repeat = true;
898             } else {
899                 pr2serr("Unit attention, too many (r)\n");
900                 return res;
901             }
902             break;
903         case SG_LIB_CAT_MEDIUM_HARD_WITH_INFO:
904             if (retries_tmp > 0) {
905                 pr2serr(">>> retrying a sgio read, lba=0x%" PRIx64 "\n",
906                         (uint64_t)lba);
907                 --retries_tmp;
908                 ++num_retries;
909                 if (unrecovered_errs > 0)
910                     --unrecovered_errs;
911                 repeat = true;
912             }
913             ret = SG_LIB_CAT_MEDIUM_HARD;
914             break; /* unrecovered read error at lba=io_addr */
915         case SG_LIB_SYNTAX_ERROR:
916             ifp->coe = 0;
917             ret = res;
918             goto err_out;
919         case -1:
920             ret = res;
921             goto err_out;
922         case SG_LIB_CAT_MEDIUM_HARD:
923             may_coe = true;
924 #if defined(__GNUC__)
925 #if (__GNUC__ >= 7)
926             __attribute__((fallthrough));
927             /* FALL THROUGH */
928 #endif
929 #endif
930         default:
931             if (retries_tmp > 0) {
932                 pr2serr(">>> retrying a sgio read, lba=0x%" PRIx64 "\n",
933                         (uint64_t)lba);
934                 --retries_tmp;
935                 ++num_retries;
936                 if (unrecovered_errs > 0)
937                     --unrecovered_errs;
938                 repeat = true;
939                 break;
940             }
941             ret = res;
942             goto err_out;
943         }
944         if (repeat)
945             continue;
946         if ((io_addr < (uint64_t)lba) ||
947             (io_addr >= (uint64_t)(lba + blks))) {
948                 pr2serr("  Unrecovered error lba 0x%" PRIx64 " not in "
949                         "correct range:\n\t[0x%" PRIx64 ",0x%" PRIx64 "]\n",
950                         io_addr, (uint64_t)lba,
951                         (uint64_t)(lba + blks - 1));
952             may_coe = true;
953             goto err_out;
954         }
955         blks = (int)(io_addr - (uint64_t)lba);
956         if (blks > 0) {
957             if (verbose)
958                 pr2serr("  partial read of %d blocks prior to medium error\n",
959                         blks);
960             res = sg_read_low(sg_fd, bp, blks, lba, bs, ifp, diop, &io_addr);
961             switch (res) {
962             case 0:
963                 break;
964             case -1:
965                 ifp->coe = 0;
966                 ret = res;
967                 goto err_out;
968             case -2:
969                 pr2serr("ENOMEM again, unexpected (r)\n");
970                 return -1;
971             case SG_LIB_CAT_NOT_READY:
972                 pr2serr("device (r) not ready\n");
973                 return res;
974             case SG_LIB_CAT_UNIT_ATTENTION:
975                 pr2serr("Unit attention, unexpected (r)\n");
976                 return res;
977             case SG_LIB_CAT_ABORTED_COMMAND:
978                 pr2serr("Aborted command, unexpected (r)\n");
979                 return res;
980             case SG_LIB_CAT_MEDIUM_HARD_WITH_INFO:
981             case SG_LIB_CAT_MEDIUM_HARD:
982                 ret = SG_LIB_CAT_MEDIUM_HARD;
983                 goto err_out;
984             case SG_LIB_SYNTAX_ERROR:
985             default:
986                 pr2serr(">> unexpected result=%d from sg_read_low() 2\n",
987                         res);
988                 ret = res;
989                 goto err_out;
990             }
991         }
992         xferred += blks;
993         if (0 == ifp->coe) {
994             /* give up at block before problem unless 'coe' */
995             if (blks_readp)
996                 *blks_readp = xferred;
997             return ret;
998         }
999         if (bs < 32) {
1000             pr2serr(">> bs=%d too small for read_long\n", bs);
1001             return -1;  /* nah, block size can't be that small */
1002         }
1003         bp += (blks * bs);
1004         lba += blks;
1005         if ((0 != ifp->pdt) || (ifp->coe < 2)) {
1006             pr2serr(">> unrecovered read error at blk=%" PRId64 ", pdt=%d, "
1007                     "use zeros\n", lba, ifp->pdt);
1008             memset(bp, 0, bs);
1009         } else if (io_addr < UINT_MAX) {
1010             bool corrct, ok;
1011             int offset, nl, r;
1012             uint8_t * buffp;
1013             uint8_t * free_buffp;
1014 
1015             buffp = sg_memalign(bs * 2, 0, &free_buffp, false);
1016             if (NULL == buffp) {
1017                 pr2serr(">> heap problems\n");
1018                 return -1;
1019             }
1020             corrct = (ifp->coe > 2);
1021             res = sg_ll_read_long10(sg_fd, /* pblock */false, corrct, lba,
1022                                     buffp, bs + read_long_blk_inc, &offset,
1023                                     true, verbose);
1024             ok = false;
1025             switch (res) {
1026             case 0:
1027                 ok = true;
1028                 ++read_longs;
1029                 break;
1030             case SG_LIB_CAT_ILLEGAL_REQ_WITH_INFO:
1031                 nl = bs + read_long_blk_inc - offset;
1032                 if ((nl < 32) || (nl > (bs * 2))) {
1033                     pr2serr(">> read_long(10) len=%d unexpected\n", nl);
1034                     break;
1035                 }
1036                 /* remember for next read_long attempt, if required */
1037                 read_long_blk_inc = nl - bs;
1038 
1039                 if (verbose)
1040                     pr2serr("read_long(10): adjusted len=%d\n", nl);
1041                 r = sg_ll_read_long10(sg_fd, false, corrct, lba, buffp, nl,
1042                                       &offset, true, verbose);
1043                 if (0 == r) {
1044                     ok = true;
1045                     ++read_longs;
1046                     break;
1047                 } else
1048                     pr2serr(">> unexpected result=%d on second "
1049                             "read_long(10)\n", r);
1050                 break;
1051             case SG_LIB_CAT_INVALID_OP:
1052                 pr2serr(">> read_long(10); not supported\n");
1053                 break;
1054             case SG_LIB_CAT_ILLEGAL_REQ:
1055                 pr2serr(">> read_long(10): bad cdb field\n");
1056                 break;
1057             case SG_LIB_CAT_NOT_READY:
1058                 pr2serr(">> read_long(10): device not ready\n");
1059                 break;
1060             case SG_LIB_CAT_UNIT_ATTENTION:
1061                 pr2serr(">> read_long(10): unit attention\n");
1062                 break;
1063             case SG_LIB_CAT_ABORTED_COMMAND:
1064                 pr2serr(">> read_long(10): aborted command\n");
1065                 break;
1066             default:
1067                 pr2serr(">> read_long(10): problem (%d)\n", res);
1068                 break;
1069             }
1070             if (ok)
1071                 memcpy(bp, buffp, bs);
1072             else
1073                 memset(bp, 0, bs);
1074             free(free_buffp);
1075         } else {
1076             pr2serr(">> read_long(10) cannot handle blk=%" PRId64 ", use "
1077                     "zeros\n", lba);
1078             memset(bp, 0, bs);
1079         }
1080         ++xferred;
1081         bp += bs;
1082         ++lba;
1083         if ((coe_limit > 0) && (++coe_count > coe_limit)) {
1084             if (blks_readp)
1085                 *blks_readp = xferred + blks;
1086             pr2serr(">> coe_limit on consecutive reads exceeded\n");
1087             return SG_LIB_CAT_MEDIUM_HARD;
1088         }
1089     }
1090     if (blks_readp)
1091         *blks_readp = xferred;
1092     return 0;
1093 
1094 err_out:
1095     if (ifp->coe) {
1096         memset(bp, 0, bs * blks);
1097         pr2serr(">> unable to read at blk=%" PRId64 " for %d bytes, use "
1098                 "zeros\n", lba, bs * blks);
1099         if (blks > 1)
1100             pr2serr(">>   try reducing bpt to limit number of zeros written "
1101                     "near bad block(s)\n");
1102         /* fudge success */
1103         if (blks_readp)
1104             *blks_readp = xferred + blks;
1105         if ((coe_limit > 0) && (++coe_count > coe_limit)) {
1106             pr2serr(">> coe_limit on consecutive reads exceeded\n");
1107             return ret;
1108         }
1109         return may_coe ? 0 : ret;
1110     } else
1111         return ret;
1112 }
1113 
1114 
1115 /* Does a SCSI WRITE or VERIFY (if do_verify set) on OFILE. Returns:
1116  * 0 -> successful, SG_LIB_SYNTAX_ERROR -> unable to build cdb,
1117  * SG_LIB_CAT_NOT_READY, SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_MEDIUM_HARD,
1118  * SG_LIB_CAT_ABORTED_COMMAND, -2 -> recoverable (ENOMEM),
1119  * -1 -> unrecoverable error + others. SG_DD_BYPASS -> failed but coe set. */
1120 static int
sg_write(int sg_fd,uint8_t * buff,int blocks,int64_t to_block,int bs,const struct flags_t * ofp,bool * diop)1121 sg_write(int sg_fd, uint8_t * buff, int blocks, int64_t to_block,
1122          int bs, const struct flags_t * ofp, bool * diop)
1123 {
1124     bool info_valid;
1125     int res;
1126     uint64_t io_addr = 0;
1127     const char * op_str = do_verify ? "verifying" : "writing";
1128     uint8_t wrCmd[MAX_SCSI_CDBSZ];
1129     uint8_t senseBuff[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
1130     struct sg_io_hdr io_hdr;
1131 
1132     if (sg_build_scsi_cdb(wrCmd, ofp->cdbsz, blocks, to_block, do_verify,
1133                           true, ofp->fua, ofp->dpo, ofp->cdl)) {
1134         pr2serr(ME "bad wr cdb build, to_block=%" PRId64 ", blocks=%d\n",
1135                 to_block, blocks);
1136         return SG_LIB_SYNTAX_ERROR;
1137     }
1138 
1139     memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
1140     io_hdr.interface_id = 'S';
1141     io_hdr.cmd_len = ofp->cdbsz;
1142     io_hdr.cmdp = wrCmd;
1143     io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
1144     io_hdr.dxfer_len = bs * blocks;
1145     io_hdr.dxferp = buff;
1146     io_hdr.mx_sb_len = SENSE_BUFF_LEN;
1147     io_hdr.sbp = senseBuff;
1148     io_hdr.timeout = cmd_timeout;
1149     io_hdr.pack_id = (int)++glob_pack_id;
1150     if (diop && *diop)
1151         io_hdr.flags |= SG_FLAG_DIRECT_IO;
1152 
1153     if (verbose > 2)
1154         sg_print_command_len(wrCmd, ofp->cdbsz);
1155 
1156     while (((res = ioctl(sg_fd, SG_IO, &io_hdr)) < 0) &&
1157            ((EINTR == errno) || (EAGAIN == errno) || (EBUSY == errno)))
1158         ;
1159     if (res < 0) {
1160         if (ENOMEM == errno)
1161             return -2;
1162         if (do_verify)
1163             perror("verifying (SG_IO) on sg device, error");
1164         else
1165             perror("writing (SG_IO) on sg device, error");
1166         return -1;
1167     }
1168 
1169     if (verbose > 2)
1170         pr2serr("      duration=%u ms\n", io_hdr.duration);
1171     res = sg_err_category3(&io_hdr);
1172     switch (res) {
1173     case SG_LIB_CAT_CLEAN:
1174     case SG_LIB_CAT_CONDITION_MET:
1175         break;
1176     case SG_LIB_CAT_RECOVERED:
1177         ++recovered_errs;
1178         info_valid = sg_get_sense_info_fld(io_hdr.sbp, io_hdr.sb_len_wr,
1179                                            &io_addr);
1180         if (info_valid) {
1181             pr2serr("    lba of last recovered error in this WRITE=0x%" PRIx64
1182                     "\n", io_addr);
1183             if (verbose > 1)
1184                 sg_chk_n_print3(op_str, &io_hdr, true);
1185         } else {
1186             pr2serr("Recovered error: [no info] %s to block=0x%" PRIx64
1187                     ", num=%d\n", op_str, to_block, blocks);
1188             sg_chk_n_print3(op_str, &io_hdr, verbose > 1);
1189         }
1190         break;
1191     case SG_LIB_CAT_ABORTED_COMMAND:
1192     case SG_LIB_CAT_UNIT_ATTENTION:
1193         sg_chk_n_print3(op_str, &io_hdr, verbose > 1);
1194         return res;
1195     case SG_LIB_CAT_MISCOMPARE: /* must be VERIFY cpommand */
1196         ++miscompare_errs;
1197         if (ofp->coe) {
1198             if (verbose > 1)
1199                 pr2serr(">> bypass due to miscompare: out blk=%" PRId64
1200                         " for %d blocks\n", to_block, blocks);
1201             return SG_DD_BYPASS; /* fudge success */
1202         } else {
1203             pr2serr("VERIFY reports miscompare\n");
1204             return res;
1205         }
1206     case SG_LIB_CAT_NOT_READY:
1207         ++unrecovered_errs;
1208         pr2serr("device not ready (w)\n");
1209         return res;
1210     case SG_LIB_CAT_MEDIUM_HARD:
1211     default:
1212         sg_chk_n_print3(op_str, &io_hdr, verbose > 1);
1213         if ((SG_LIB_CAT_ILLEGAL_REQ == res) && verbose)
1214             sg_print_command_len(wrCmd, ofp->cdbsz);
1215         ++unrecovered_errs;
1216         if (ofp->coe) {
1217             if (verbose > 1)
1218                 pr2serr(">> ignored errors for out blk=%" PRId64 " for %d "
1219                         "bytes\n", to_block, bs * blocks);
1220             return SG_DD_BYPASS; /* fudge success */
1221         } else
1222             return res;
1223     }
1224     if (diop && *diop &&
1225         ((io_hdr.info & SG_INFO_DIRECT_IO_MASK) != SG_INFO_DIRECT_IO))
1226         *diop = false;      /* flag that dio not done (completely) */
1227     return 0;
1228 }
1229 
1230 
1231 static void
calc_duration_throughput(bool contin)1232 calc_duration_throughput(bool contin)
1233 {
1234     struct timeval end_tm, res_tm;
1235     double a, b;
1236     int64_t blks;
1237 
1238     if (start_tm_valid && (start_tm.tv_sec || start_tm.tv_usec)) {
1239         blks = (in_full > out_full) ? in_full : out_full;
1240         gettimeofday(&end_tm, NULL);
1241         res_tm.tv_sec = end_tm.tv_sec - start_tm.tv_sec;
1242         res_tm.tv_usec = end_tm.tv_usec - start_tm.tv_usec;
1243         if (res_tm.tv_usec < 0) {
1244             --res_tm.tv_sec;
1245             res_tm.tv_usec += 1000000;
1246         }
1247         a = res_tm.tv_sec;
1248         a += (0.000001 * res_tm.tv_usec);
1249         b = (double)blk_sz * blks;
1250         pr2serr("time to %s data%s: %d.%06d secs",
1251                 (do_verify ? "verify" : "copy"), (contin ? " so far" : ""),
1252                 (int)res_tm.tv_sec, (int)res_tm.tv_usec);
1253         if ((a > 0.00001) && (b > 511))
1254             pr2serr(" at %.2f MB/sec\n", b / (a * 1000000.0));
1255         else
1256             pr2serr("\n");
1257     }
1258 }
1259 
1260 /* Process arguments given to 'iflag=" or 'oflag=" options. Returns 0
1261  * on success, 1 on error. */
1262 static int
process_flags(const char * arg,struct flags_t * fp)1263 process_flags(const char * arg, struct flags_t * fp)
1264 {
1265     char buff[256];
1266     char * cp;
1267     char * np;
1268 
1269     strncpy(buff, arg, sizeof(buff));
1270     buff[sizeof(buff) - 1] = '\0';
1271     if ('\0' == buff[0]) {
1272         pr2serr("no flag found\n");
1273         return 1;
1274     }
1275     cp = buff;
1276     do {
1277         np = strchr(cp, ',');
1278         if (np)
1279             *np++ = '\0';
1280         if (0 == strcmp(cp, "00"))
1281             fp->zero = true;
1282         else if (0 == strcmp(cp, "append"))
1283             fp->append = true;
1284         else if (0 == strcmp(cp, "coe"))
1285             ++fp->coe;
1286         else if (0 == strcmp(cp, "dio"))
1287             fp->dio = true;
1288         else if (0 == strcmp(cp, "direct"))
1289             fp->direct = true;
1290         else if (0 == strcmp(cp, "dpo"))
1291             fp->dpo = true;
1292         else if (0 == strcmp(cp, "dsync"))
1293             fp->dsync = true;
1294         else if (0 == strcmp(cp, "excl"))
1295             fp->excl = true;
1296         else if (0 == strcmp(cp, "flock"))
1297             fp->flock = true;
1298         else if (0 == strcmp(cp, "ff"))
1299             fp->ff = true;
1300         else if (0 == strcmp(cp, "fua"))
1301             fp->fua = true;
1302         else if (0 == strcmp(cp, "nocache"))
1303             ++fp->nocache;
1304         else if (0 == strcmp(cp, "nocreat"))
1305             fp->nocreat = true;
1306         else if (0 == strcmp(cp, "null"))
1307             ;
1308         else if (0 == strcmp(cp, "random"))
1309             fp->random = true;
1310         else if (0 == strcmp(cp, "sgio"))
1311             fp->sgio = true;
1312         else if (0 == strcmp(cp, "sparse"))
1313             fp->sparse = true;
1314         else {
1315             pr2serr("unrecognised flag: %s\n", cp);
1316             return 1;
1317         }
1318         cp = np;
1319     } while (cp);
1320     return 0;
1321 }
1322 
1323 /* Process arguments given to 'conv=" option. Returns 0 on success,
1324  * 1 on error. */
1325 static int
process_conv(const char * arg,struct flags_t * ifp,struct flags_t * ofp)1326 process_conv(const char * arg, struct flags_t * ifp, struct flags_t * ofp)
1327 {
1328     char buff[256];
1329     char * cp;
1330     char * np;
1331 
1332     strncpy(buff, arg, sizeof(buff));
1333     buff[sizeof(buff) - 1] = '\0';
1334     if ('\0' == buff[0]) {
1335         pr2serr("no conversions found\n");
1336         return 1;
1337     }
1338     cp = buff;
1339     do {
1340         np = strchr(cp, ',');
1341         if (np)
1342             *np++ = '\0';
1343         if (0 == strcmp(cp, "nocreat"))
1344             ofp->nocreat = true;
1345         else if (0 == strcmp(cp, "noerror"))
1346             ++ifp->coe;         /* will still fail on write error */
1347         else if (0 == strcmp(cp, "notrunc"))
1348             ;         /* this is the default action of sg_dd so ignore */
1349         else if (0 == strcmp(cp, "null"))
1350             ;
1351         else if (0 == strcmp(cp, "sparse"))
1352             ofp->sparse = true;
1353         else if (0 == strcmp(cp, "sync"))
1354             ;   /* dd(susv4): pad errored block(s) with zeros but sg_dd does
1355                  * that by default. Typical dd use: 'conv=noerror,sync' */
1356         else {
1357             pr2serr("unrecognised flag: %s\n", cp);
1358             return 1;
1359         }
1360         cp = np;
1361     } while (cp);
1362     return 0;
1363 }
1364 
1365 /* Returns open input file descriptor (>= 0) or a negative value
1366  * (-SG_LIB_FILE_ERROR or -SG_LIB_CAT_OTHER) if error.
1367  */
1368 static int
open_if(const char * inf,int64_t skip,int bpt,struct flags_t * ifp,int * in_typep,int vb)1369 open_if(const char * inf, int64_t skip, int bpt, struct flags_t * ifp,
1370         int * in_typep, int vb)
1371 {
1372     int infd = -1;
1373     int flags, fl, t, res;
1374     char ebuff[EBUFF_SZ];
1375     struct sg_simple_inquiry_resp sir;
1376 
1377     *in_typep = dd_filetype(inf);
1378     if (vb)
1379         pr2serr(" >> Input file type: %s\n",
1380                 dd_filetype_str(*in_typep, ebuff));
1381     if (FT_ERROR & *in_typep) {
1382         pr2serr(ME "unable access %s\n", inf);
1383         goto file_err;
1384     } else if ((FT_BLOCK & *in_typep) && ifp->sgio)
1385         *in_typep |= FT_SG;
1386 
1387     if (FT_ST & *in_typep) {
1388         pr2serr(ME "unable to use scsi tape device %s\n", inf);
1389         goto file_err;
1390     } else if (FT_SG & *in_typep) {
1391         flags = O_NONBLOCK;
1392         if (ifp->direct)
1393             flags |= O_DIRECT;
1394         if (ifp->excl)
1395             flags |= O_EXCL;
1396         if (ifp->dsync)
1397             flags |= O_SYNC;
1398         fl = O_RDWR;
1399         if ((infd = open(inf, fl | flags)) < 0) {
1400             fl = O_RDONLY;
1401             if ((infd = open(inf, fl | flags)) < 0) {
1402                 snprintf(ebuff, EBUFF_SZ,
1403                          ME "could not open %s for sg reading", inf);
1404                 perror(ebuff);
1405                 goto file_err;
1406             }
1407         }
1408         if (vb)
1409             pr2serr("        open input(sg_io), flags=0x%x\n", fl | flags);
1410         if (sg_simple_inquiry(infd, &sir, false, (vb ? (vb - 1) : 0))) {
1411             pr2serr("INQUIRY failed on %s\n", inf);
1412             goto other_err;
1413         }
1414         ifp->pdt = sir.peripheral_type;
1415         if (vb)
1416             pr2serr("    %s: %.8s  %.16s  %.4s  [pdt=%d]\n", inf, sir.vendor,
1417                     sir.product, sir.revision, ifp->pdt);
1418         if (! (FT_BLOCK & *in_typep)) {
1419             t = blk_sz * bpt;
1420             res = ioctl(infd, SG_SET_RESERVED_SIZE, &t);
1421             if (res < 0)
1422                 perror(ME "SG_SET_RESERVED_SIZE error");
1423             res = ioctl(infd, SG_GET_VERSION_NUM, &t);
1424             if ((res < 0) || (t < 30000)) {
1425                 if (FT_BLOCK & *in_typep)
1426                     pr2serr(ME "SG_IO unsupported on this block device\n");
1427                 else
1428                     pr2serr(ME "sg driver prior to 3.x.y\n");
1429                 goto file_err;
1430             }
1431         }
1432     } else if (FT_NVME & *in_typep) {
1433         pr2serr("Don't support NVMe char devices as IFILE\n");
1434         goto file_err;
1435     } else {
1436         flags = O_RDONLY;
1437         if (ifp->direct)
1438             flags |= O_DIRECT;
1439         if (ifp->excl)
1440             flags |= O_EXCL;
1441         if (ifp->dsync)
1442             flags |= O_SYNC;
1443         infd = open(inf, flags);
1444         if (infd < 0) {
1445             snprintf(ebuff, EBUFF_SZ,
1446                      ME "could not open %s for reading", inf);
1447             perror(ebuff);
1448             goto file_err;
1449         } else {
1450             if (vb)
1451                 pr2serr("        open input, flags=0x%x\n", flags);
1452             if (skip > 0) {
1453                 off64_t offset = skip;
1454 
1455                 offset *= blk_sz;       /* could exceed 32 bits here! */
1456                 if (lseek64(infd, offset, SEEK_SET) < 0) {
1457                     snprintf(ebuff, EBUFF_SZ, ME "couldn't skip to "
1458                              "required position on %s", inf);
1459                     perror(ebuff);
1460                     goto file_err;
1461                 }
1462                 if (vb)
1463                     pr2serr("  >> skip: lseek64 SEEK_SET, byte offset=0x%"
1464                             PRIx64 "\n", (uint64_t)offset);
1465             }
1466 #ifdef HAVE_POSIX_FADVISE
1467             if (ifp->nocache) {
1468                 int rt;
1469 
1470                 rt = posix_fadvise(infd, 0, 0, POSIX_FADV_SEQUENTIAL);
1471                 if (rt)
1472                     pr2serr("open_if: posix_fadvise(SEQUENTIAL), err=%d\n",
1473                             rt);
1474             }
1475 #endif
1476         }
1477     }
1478     if (ifp->flock && (infd >= 0)) {
1479         res = flock(infd, LOCK_EX | LOCK_NB);
1480         if (res < 0) {
1481             close(infd);
1482             snprintf(ebuff, EBUFF_SZ, ME "flock(LOCK_EX | LOCK_NB) on %s "
1483                      "failed", inf);
1484             perror(ebuff);
1485             return -SG_LIB_FLOCK_ERR;
1486         }
1487     }
1488     return infd;
1489 
1490 file_err:
1491     if (infd >= 0)
1492         close(infd);
1493     return -SG_LIB_FILE_ERROR;
1494 other_err:
1495     if (infd >= 0)
1496         close(infd);
1497     return -SG_LIB_CAT_OTHER;
1498 }
1499 
1500 /* Returns open output file descriptor (>= 0), -1 for don't
1501  * bother opening (e.g. /dev/null), or a more negative value
1502  * (-SG_LIB_FILE_ERROR or -SG_LIB_CAT_OTHER) if error.
1503  */
1504 static int
open_of(const char * outf,int64_t seek,int bpt,struct flags_t * ofp,int * out_typep,int vb)1505 open_of(const char * outf, int64_t seek, int bpt, struct flags_t * ofp,
1506         int * out_typep, int vb)
1507 {
1508     bool not_found;
1509     int outfd = -1;
1510     int flags, t, res;
1511     char ebuff[EBUFF_SZ];
1512     struct sg_simple_inquiry_resp sir;
1513 
1514     *out_typep = dd_filetype(outf);
1515     if (vb)
1516         pr2serr(" >> Output file type: %s\n",
1517                 dd_filetype_str(*out_typep, ebuff));
1518     not_found = (FT_ERROR == *out_typep);  /* assume error was not found */
1519 
1520     if ((FT_BLOCK & *out_typep) && ofp->sgio)
1521         *out_typep |= FT_SG;
1522 
1523     if (FT_ST & *out_typep) {
1524         pr2serr(ME "unable to use scsi tape device %s\n", outf);
1525         goto file_err;
1526     } else if (FT_SG & *out_typep) {
1527         flags = O_RDWR | O_NONBLOCK;
1528         if (ofp->direct)
1529             flags |= O_DIRECT;
1530         if (ofp->excl)
1531             flags |= O_EXCL;
1532         if (ofp->dsync)
1533             flags |= O_SYNC;
1534         if ((outfd = open(outf, flags)) < 0) {
1535             snprintf(ebuff, EBUFF_SZ,
1536                      ME "could not open %s for sg writing", outf);
1537             perror(ebuff);
1538             goto file_err;
1539         }
1540         if (vb)
1541             pr2serr("        open output(sg_io), flags=0x%x\n", flags);
1542         if (sg_simple_inquiry(outfd, &sir, false, (vb ? (vb - 1) : 0))) {
1543             pr2serr("INQUIRY failed on %s\n", outf);
1544             goto other_err;
1545         }
1546         ofp->pdt = sir.peripheral_type;
1547         if (vb)
1548             pr2serr("    %s: %.8s  %.16s  %.4s  [pdt=%d]\n", outf, sir.vendor,
1549                     sir.product, sir.revision, ofp->pdt);
1550         if (! (FT_BLOCK & *out_typep)) {
1551             t = blk_sz * bpt;
1552             res = ioctl(outfd, SG_SET_RESERVED_SIZE, &t);
1553             if (res < 0)
1554                 perror(ME "SG_SET_RESERVED_SIZE error");
1555             res = ioctl(outfd, SG_GET_VERSION_NUM, &t);
1556             if ((res < 0) || (t < 30000)) {
1557                 pr2serr(ME "sg driver prior to 3.x.y\n");
1558                 goto file_err;
1559             }
1560         }
1561     } else if (FT_NVME & *out_typep) {
1562         pr2serr("Don't support NVMe char devices as OFILE\n");
1563         goto file_err;
1564     } else if (FT_DEV_NULL & *out_typep)
1565         outfd = -1; /* don't bother opening */
1566     else if (FT_RAW & *out_typep) {
1567         flags = O_WRONLY;
1568         if (ofp->direct)
1569             flags |= O_DIRECT;
1570         if (ofp->excl)
1571             flags |= O_EXCL;
1572         if (ofp->dsync)
1573             flags |= O_SYNC;
1574         if ((outfd = open(outf, flags)) < 0) {
1575             snprintf(ebuff, EBUFF_SZ,
1576                     ME "could not open %s for raw writing", outf);
1577             perror(ebuff);
1578             goto file_err;
1579         }
1580     } else {    /* FT_OTHER or FT_ERROR (not found so create) */
1581         flags = O_WRONLY;
1582         if (! ofp->nocreat)
1583             flags |= O_CREAT;
1584         if (ofp->direct)
1585             flags |= O_DIRECT;
1586         if (ofp->excl)
1587             flags |= O_EXCL;
1588         if (ofp->dsync)
1589             flags |= O_SYNC;
1590         if (ofp->append)
1591             flags |= O_APPEND;
1592         if ((outfd = open(outf, flags, 0666)) < 0) {
1593             snprintf(ebuff, EBUFF_SZ,
1594                     ME "could not open %s for writing", outf);
1595             perror(ebuff);
1596             goto file_err;
1597         }
1598         if (vb)
1599             pr2serr("        %s output, flags=0x%x\n",
1600                     (not_found ? "create" : "open"), flags);
1601         if (seek > 0) {
1602             off64_t offset = seek;
1603 
1604             offset *= blk_sz;       /* could exceed 32 bits here! */
1605             if (lseek64(outfd, offset, SEEK_SET) < 0) {
1606                 snprintf(ebuff, EBUFF_SZ,
1607                     ME "couldn't seek to required position on %s", outf);
1608                 perror(ebuff);
1609                 goto file_err;
1610             }
1611             if (vb)
1612                 pr2serr("   >> seek: lseek64 SEEK_SET, byte offset=0x%" PRIx64
1613                         "\n", (uint64_t)offset);
1614         }
1615     }
1616     if (ofp->flock && (outfd >= 0)) {
1617         res = flock(outfd, LOCK_EX | LOCK_NB);
1618         if (res < 0) {
1619             snprintf(ebuff, EBUFF_SZ, ME "flock(LOCK_EX | LOCK_NB) on %s "
1620                      "failed", outf);
1621             perror(ebuff);
1622             close(outfd);
1623             return -SG_LIB_FLOCK_ERR;
1624         }
1625     }
1626     return outfd;
1627 
1628 file_err:
1629     if (outfd >= 0)
1630         close(outfd);
1631     return -SG_LIB_FILE_ERROR;
1632 other_err:
1633     if (outfd >= 0)
1634         close(outfd);
1635     return -SG_LIB_CAT_OTHER;
1636 }
1637 
1638 /* Returns the number of times 'ch' is found in string 's' given the
1639  * string's length. */
1640 static int
num_chs_in_str(const char * s,int slen,int ch)1641 num_chs_in_str(const char * s, int slen, int ch)
1642 {
1643     int res = 0;
1644 
1645     while (--slen >= 0) {
1646         if (ch == s[slen])
1647             ++res;
1648     }
1649     return res;
1650 }
1651 
1652 /* Returns true when it time to output a progress report; else false. */
1653 static bool
check_progress(void)1654 check_progress(void)
1655 {
1656 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1657     static bool have_prev, measure;
1658     static struct timespec prev_true_tm;
1659     static int count, threshold;
1660     bool res = false;
1661     uint32_t elapsed_ms, ms;
1662     struct timespec now_tm, res_tm;
1663 
1664     if (progress) {
1665         if (! have_prev) {
1666             have_prev = true;
1667             measure = true;
1668             clock_gettime(CLOCK_MONOTONIC, &prev_true_tm);
1669             return false;       /* starting reference */
1670         }
1671         if (! measure) {
1672             if (++count >= threshold)
1673                 count = 0;
1674             else
1675                 return false;
1676         }
1677         clock_gettime(CLOCK_MONOTONIC, &now_tm);
1678         res_tm.tv_sec = now_tm.tv_sec - prev_true_tm.tv_sec;
1679         res_tm.tv_nsec = now_tm.tv_nsec - prev_true_tm.tv_nsec;
1680         if (res_tm.tv_nsec < 0) {
1681             --res_tm.tv_sec;
1682             res_tm.tv_nsec += 1000000000;
1683         }
1684         elapsed_ms = (1000 * res_tm.tv_sec) + (res_tm.tv_nsec / 1000000);
1685         if (measure) {
1686             ++threshold;
1687             if (elapsed_ms > 80)        /* 80 milliseconds */
1688                 measure = false;
1689         }
1690         if (elapsed_ms >= PROGRESS3_TRIGGER_MS) {
1691             if (elapsed_ms >= PROGRESS2_TRIGGER_MS) {
1692                 if (elapsed_ms >= PROGRESS_TRIGGER_MS) {
1693                     ms = PROGRESS_TRIGGER_MS;
1694                     res = true;
1695                 } else if (progress > 1) {
1696                     ms = PROGRESS2_TRIGGER_MS;
1697                     res = true;
1698                 }
1699             } else if (progress > 2) {
1700                 ms = PROGRESS3_TRIGGER_MS;
1701                 res = true;
1702             }
1703         }
1704         if (res) {
1705             prev_true_tm.tv_sec += (ms / 1000);
1706             prev_true_tm.tv_nsec += (ms % 1000) * 1000000;
1707             if (prev_true_tm.tv_nsec >= 1000000000) {
1708                 ++prev_true_tm.tv_sec;
1709                 prev_true_tm.tv_nsec -= 1000000000;
1710             }
1711         }
1712     }
1713     return res;
1714 
1715 #elif defined(HAVE_GETTIMEOFDAY)
1716     static bool have_prev, measure;
1717     static struct timeval prev_true_tm;
1718     static int count, threshold;
1719     bool res = false;
1720     uint32_t elapsed_ms, ms;
1721     struct timeval now_tm, res_tm;
1722 
1723     if (progress) {
1724         if (! have_prev) {
1725             have_prev = true;
1726             gettimeofday(&prev_true_tm, NULL);
1727             return false;       /* starting reference */
1728         }
1729         if (! measure) {
1730             if (++count >= threshold)
1731                 count = 0;
1732             else
1733                 return false;
1734         }
1735         gettimeofday(&now_tm, NULL);
1736         res_tm.tv_sec = now_tm.tv_sec - prev_true_tm.tv_sec;
1737         res_tm.tv_usec = now_tm.tv_usec - prev_true_tm.tv_usec;
1738         if (res_tm.tv_usec < 0) {
1739             --res_tm.tv_sec;
1740             res_tm.tv_usec += 1000000;
1741         }
1742         elapsed_ms = (1000 * res_tm.tv_sec) + (res_tm.tv_usec / 1000);
1743         if (measure) {
1744             ++threshold;
1745             if (elapsed_ms > 80)        /* 80 milliseconds */
1746                 measure = false;
1747         }
1748         if (elapsed_ms >= PROGRESS3_TRIGGER_MS) {
1749             if (elapsed_ms >= PROGRESS2_TRIGGER_MS) {
1750                 if (elapsed_ms >= PROGRESS_TRIGGER_MS) {
1751                     ms = PROGRESS_TRIGGER_MS;
1752                     res = true;
1753                 } else if (progress > 1) {
1754                     ms = PROGRESS2_TRIGGER_MS;
1755                     res = true;
1756                 }
1757             } else if (progress > 2) {
1758                 ms = PROGRESS3_TRIGGER_MS;
1759                 res = true;
1760             }
1761         }
1762         if (res) {
1763             prev_true_tm.tv_sec += (ms / 1000);
1764             prev_true_tm.tv_usec += (ms % 1000) * 1000;
1765             if (prev_true_tm.tv_usec >= 1000000) {
1766                 ++prev_true_tm.tv_sec;
1767                 prev_true_tm.tv_usec -= 1000000;
1768             }
1769         }
1770     }
1771     return res;
1772 
1773 #else   /* no clock reading functions available */
1774     return false;
1775 #endif
1776 }
1777 
1778 
1779 int
main(int argc,char * argv[])1780 main(int argc, char * argv[])
1781 {
1782     bool bpt_given = false;
1783     bool cdbsz_given = false;
1784     bool cdl_given = false;
1785     bool dio_tmp, first;
1786     bool do_sync = false;
1787     bool penult_sparse_skip = false;
1788     bool sparse_skip = false;
1789     bool verbose_given = false;
1790     bool version_given = false;
1791     int res, k, n, t, buf_sz, blocks_per, infd, outfd, out2fd, keylen;
1792     int retries_tmp, blks_read, bytes_read, bytes_of2, bytes_of;
1793     int in_sect_sz, out_sect_sz;
1794     int blocks = 0;
1795     int bpt = DEF_BLOCKS_PER_TRANSFER;
1796     int dio_incomplete_count = 0;
1797     int ibs = 0;
1798     int in_type = FT_OTHER;
1799     int obs = 0;
1800     int out_type = FT_OTHER;
1801     int out2_type = FT_OTHER;
1802     int penult_blocks = 0;
1803     int ret = 0;
1804     int64_t skip = 0;
1805     int64_t seek = 0;
1806     int64_t in_num_sect = -1;
1807     int64_t out_num_sect = -1;
1808     char * key;
1809     char * buf;
1810     const char * ccp = NULL;
1811     const char * cc2p;
1812     uint8_t * wrkBuff = NULL;
1813     uint8_t * wrkPos;
1814     char inf[INOUTF_SZ];
1815     char outf[INOUTF_SZ];
1816     char out2f[INOUTF_SZ];
1817     char str[STR_SZ];
1818     char ebuff[EBUFF_SZ];
1819 
1820     inf[0] = '\0';
1821     outf[0] = '\0';
1822     out2f[0] = '\0';
1823     iflag.cdbsz = DEF_SCSI_CDBSZ;
1824     oflag.cdbsz = DEF_SCSI_CDBSZ;
1825 
1826     for (k = 1; k < argc; k++) {
1827         if (argv[k]) {
1828             strncpy(str, argv[k], STR_SZ);
1829             str[STR_SZ - 1] = '\0';
1830         } else
1831             continue;
1832         for (key = str, buf = key; *buf && *buf != '=';)
1833             buf++;
1834         if (*buf)
1835             *buf++ = '\0';
1836         keylen = strlen(key);
1837         if (0 == strncmp(key, "app", 3)) {
1838             iflag.append = !! sg_get_num(buf);
1839             oflag.append = iflag.append;
1840         } else if (0 == strcmp(key, "blk_sgio")) {
1841             iflag.sgio = !! sg_get_num(buf);
1842             oflag.sgio = iflag.sgio;
1843         } else if (0 == strcmp(key, "bpt")) {
1844             bpt = sg_get_num(buf);
1845             if (-1 == bpt) {
1846                 pr2serr(ME "bad argument to 'bpt='\n");
1847                 return SG_LIB_SYNTAX_ERROR;
1848             }
1849             bpt_given = true;
1850         } else if (0 == strcmp(key, "bs")) {
1851             blk_sz = sg_get_num(buf);
1852             if ((blk_sz < 0) || (blk_sz > MAX_BPT_VALUE)) {
1853                 pr2serr(ME "bad argument to 'bs='\n");
1854                 return SG_LIB_SYNTAX_ERROR;
1855             }
1856         } else if (0 == strcmp(key, "cdbsz")) {
1857             iflag.cdbsz = sg_get_num(buf);
1858             if ((iflag.cdbsz < 6) || (iflag.cdbsz > 32)) {
1859                 pr2serr(ME "'cdbsz' expects 6, 10, 12, 16 or 32\n");
1860                 return SG_LIB_SYNTAX_ERROR;
1861             }
1862             oflag.cdbsz = iflag.cdbsz;
1863             cdbsz_given = true;
1864         } else if (0 == strcmp(key, "cdl")) {
1865             const char * cp = strchr(buf, ',');
1866 
1867             iflag.cdl = sg_get_num(buf);
1868             if ((iflag.cdl < 0) || (iflag.cdl > 7)) {
1869                 pr2serr(ME "bad argument to 'cdl=', expect 0 to 7\n");
1870                 return SG_LIB_SYNTAX_ERROR;
1871             }
1872             if (cp) {
1873                 oflag.cdl = sg_get_num(cp + 1);
1874                 if ((oflag.cdl < 0) || (oflag.cdl > 7)) {
1875                     pr2serr(ME "bad argument to 'cdl=ICDL,OCDL', expect OCDL "
1876                             "to be 0 to 7\n");
1877                     return SG_LIB_SYNTAX_ERROR;
1878                 }
1879             } else
1880                 oflag.cdl = iflag.cdl;
1881             cdl_given = true;
1882         } else if (0 == strcmp(key, "coe")) {
1883             iflag.coe = sg_get_num(buf);
1884             oflag.coe = iflag.coe;
1885         } else if (0 == strcmp(key, "coe_limit")) {
1886             coe_limit = sg_get_num(buf);
1887             if (-1 == coe_limit) {
1888                 pr2serr(ME "bad argument to 'coe_limit='\n");
1889                 return SG_LIB_SYNTAX_ERROR;
1890             }
1891         } else if (0 == strcmp(key, "conv")) {
1892             if (process_conv(buf, &iflag, &oflag)) {
1893                 pr2serr(ME "bad argument to 'conv='\n");
1894                 return SG_LIB_SYNTAX_ERROR;
1895             }
1896         } else if (0 == strcmp(key, "count")) {
1897             if (0 != strcmp("-1", buf)) {
1898                 dd_count = sg_get_llnum(buf);
1899                 if ((dd_count < 0) || (dd_count > MAX_COUNT_SKIP_SEEK)) {
1900                     pr2serr(ME "bad argument to 'count='\n");
1901                     return SG_LIB_SYNTAX_ERROR;
1902                 }
1903             }   /* treat 'count=-1' as calculate count (same as not given) */
1904         } else if (0 == strcmp(key, "dio")) {
1905             oflag.dio = !! sg_get_num(buf);
1906             iflag.dio = oflag.dio;
1907         } else if (0 == strcmp(key, "fua")) {
1908             t = sg_get_num(buf);
1909             oflag.fua = !! (t & 1);
1910             iflag.fua = !! (t & 2);
1911         } else if (0 == strcmp(key, "ibs")) {
1912             ibs = sg_get_num(buf);
1913             if ((ibs < 0) || (ibs > MAX_BPT_VALUE)) {
1914                 pr2serr(ME "bad argument to 'ibs='\n");
1915                 return SG_LIB_SYNTAX_ERROR;
1916             }
1917         } else if (strcmp(key, "if") == 0) {
1918             if ('\0' != inf[0]) {
1919                 pr2serr("Second IFILE argument??\n");
1920                 return SG_LIB_SYNTAX_ERROR;
1921             } else {
1922                 memcpy(inf, buf, INOUTF_SZ - 1);
1923                 inf[INOUTF_SZ - 1] = '\0';
1924             }
1925         } else if (0 == strcmp(key, "iflag")) {
1926             if (process_flags(buf, &iflag)) {
1927                 pr2serr(ME "bad argument to 'iflag='\n");
1928                 return SG_LIB_SYNTAX_ERROR;
1929             }
1930         } else if (0 == strcmp(key, "obs")) {
1931             obs = sg_get_num(buf);
1932             if ((obs < 0) || (obs > MAX_BPT_VALUE)) {
1933                 pr2serr(ME "bad argument to 'obs='\n");
1934                 return SG_LIB_SYNTAX_ERROR;
1935             }
1936         } else if (0 == strcmp(key, "odir")) {
1937             iflag.direct = !! sg_get_num(buf);
1938             oflag.direct = iflag.direct;
1939         } else if (strcmp(key, "of") == 0) {
1940             if ('\0' != outf[0]) {
1941                 pr2serr("Second OFILE argument??\n");
1942                 return SG_LIB_CONTRADICT;
1943             } else {
1944                 memcpy(outf, buf, INOUTF_SZ - 1);
1945                 outf[INOUTF_SZ - 1] = '\0';
1946             }
1947         } else if (strcmp(key, "of2") == 0) {
1948             if ('\0' != out2f[0]) {
1949                 pr2serr("Second OFILE2 argument??\n");
1950                 return SG_LIB_CONTRADICT;
1951             } else {
1952                 memcpy(out2f, buf, INOUTF_SZ - 1);
1953                 out2f[INOUTF_SZ - 1] = '\0';
1954             }
1955         } else if (0 == strcmp(key, "oflag")) {
1956             if (process_flags(buf, &oflag)) {
1957                 pr2serr(ME "bad argument to 'oflag='\n");
1958                 return SG_LIB_SYNTAX_ERROR;
1959             }
1960         } else if (0 == strcmp(key, "retries")) {
1961             iflag.retries = sg_get_num(buf);
1962             oflag.retries = iflag.retries;
1963             if (-1 == iflag.retries) {
1964                 pr2serr(ME "bad argument to 'retries='\n");
1965                 return SG_LIB_SYNTAX_ERROR;
1966             }
1967         } else if (0 == strcmp(key, "seek")) {
1968             seek = sg_get_llnum(buf);
1969             if ((seek < 0) || (seek > MAX_COUNT_SKIP_SEEK)) {
1970                 pr2serr(ME "bad argument to 'seek='\n");
1971                 return SG_LIB_SYNTAX_ERROR;
1972             }
1973         } else if (0 == strcmp(key, "skip")) {
1974             skip = sg_get_llnum(buf);
1975             if ((skip < 0) || (skip > MAX_COUNT_SKIP_SEEK)) {
1976                 pr2serr(ME "bad argument to 'skip='\n");
1977                 return SG_LIB_SYNTAX_ERROR;
1978             }
1979         } else if (0 == strcmp(key, "sync"))
1980             do_sync = !! sg_get_num(buf);
1981         else if (0 == strcmp(key, "time")) {
1982             const char * cp = strchr(buf, ',');
1983 
1984             do_time = !! sg_get_num(buf);
1985             if (cp) {
1986                 n = sg_get_num(cp + 1);
1987                 if (n < 0) {
1988                     pr2serr(ME "bad argument to 'time=0|1,TO'\n");
1989                     return SG_LIB_SYNTAX_ERROR;
1990                 }
1991                 cmd_timeout = n ? (n * 1000) : DEF_TIMEOUT;
1992             }
1993         } else if (0 == strncmp(key, "verb", 4))
1994             verbose = sg_get_num(buf);
1995         else if ((keylen > 1) && ('-' == key[0]) && ('-' != key[1])) {
1996             res = 0;
1997             n = num_chs_in_str(key + 1, keylen - 1, 'd');
1998             dry_run += n;
1999             res += n;
2000             n = num_chs_in_str(key + 1, keylen - 1, 'h');
2001             if (n > 0) {
2002                 usage();
2003                 return 0;
2004             }
2005             n = num_chs_in_str(key + 1, keylen - 1, 'p');
2006             progress += n;
2007             res += n;
2008             n = num_chs_in_str(key + 1, keylen - 1, 'v');
2009             if (n > 0)
2010                 verbose_given = true;
2011             verbose += n;
2012             res += n;
2013             n = num_chs_in_str(key + 1, keylen - 1, 'V');
2014             if (n > 0)
2015                 version_given = true;
2016             res += n;
2017             n = num_chs_in_str(key + 1, keylen - 1, 'x');
2018             if (n > 0)
2019                 do_verify = true;
2020             res += n;
2021             if (res < (keylen - 1)) {
2022                 pr2serr("Unrecognised short option in '%s', try '--help'\n",
2023                         key);
2024                 return SG_LIB_SYNTAX_ERROR;
2025             }
2026         } else if ((0 == strncmp(key, "--dry-run", 9)) ||
2027                  (0 == strncmp(key, "--dry_run", 9)))
2028             ++dry_run;
2029         else if ((0 == strncmp(key, "--help", 6)) ||
2030                  (0 == strcmp(key, "-?"))) {
2031             usage();
2032             return 0;
2033         } else if (0 == strncmp(key, "--progress", 10))
2034             ++progress;
2035         else if (0 == strncmp(key, "--verb", 6)) {
2036             verbose_given = true;
2037             ++verbose;
2038         } else if (0 == strncmp(key, "--veri", 6))
2039             do_verify = true;
2040         else if (0 == strncmp(key, "--vers", 6))
2041             version_given = true;
2042         else {
2043             pr2serr("Unrecognized option '%s'\n", key);
2044             pr2serr("For more information use '--help'\n");
2045             return SG_LIB_SYNTAX_ERROR;
2046         }
2047     }
2048 #ifdef DEBUG
2049     pr2serr("In DEBUG mode, ");
2050     if (verbose_given && version_given) {
2051         pr2serr("but override: '-vV' given, zero verbose and continue\n");
2052         verbose_given = false;
2053         version_given = false;
2054         verbose = 0;
2055     } else if (! verbose_given) {
2056         pr2serr("set '-vv'\n");
2057         verbose = 2;
2058     } else
2059         pr2serr("keep verbose=%d\n", verbose);
2060 #else
2061     if (verbose_given && version_given)
2062         pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
2063 #endif
2064     if (version_given) {
2065         pr2serr(ME "version: %s\n", version_str);
2066         return 0;
2067     }
2068     if (progress > 0 && !do_time)
2069         do_time = true;
2070     if (argc < 2) {
2071         pr2serr("Won't default both IFILE to stdin _and_ OFILE to stdout\n");
2072         pr2serr("For more information use '--help'\n");
2073         return SG_LIB_CONTRADICT;
2074     }
2075     if (blk_sz <= 0) {
2076         blk_sz = DEF_BLOCK_SIZE;
2077         pr2serr("Assume default 'bs' ((logical) block size) of %d bytes\n",
2078                 blk_sz);
2079     }
2080     if ((ibs && (ibs != blk_sz)) || (obs && (obs != blk_sz))) {
2081         pr2serr("If 'ibs' or 'obs' given must be same as 'bs'\n");
2082         pr2serr("For more information use '--help'\n");
2083         return SG_LIB_CONTRADICT;
2084     }
2085     if ((skip < 0) || (seek < 0)) {
2086         pr2serr("skip and seek cannot be negative\n");
2087         return SG_LIB_CONTRADICT;
2088     }
2089     if (oflag.append && (seek > 0)) {
2090         pr2serr("Can't use both append and seek switches\n");
2091         return SG_LIB_CONTRADICT;
2092     }
2093     if ((bpt < 1) || (bpt > MAX_BPT_VALUE)) {
2094         pr2serr("bpt must be > 0 and <= %d\n", MAX_BPT_VALUE);
2095         return SG_LIB_SYNTAX_ERROR;
2096     }
2097     if (iflag.sparse)
2098         pr2serr("sparse flag ignored for iflag\n");
2099 
2100     /* defaulting transfer size to 128*2048 for CD/DVDs is too large
2101        for the block layer in lk 2.6 and results in an EIO on the
2102        SG_IO ioctl. So reduce it in that case. */
2103     if ((blk_sz >= 2048) && (! bpt_given))
2104         bpt = DEF_BLOCKS_PER_2048TRANSFER;
2105 #ifdef DEBUG
2106     pr2serr(ME "if=%s skip=%" PRId64 " of=%s seek=%" PRId64 " count=%" PRId64
2107             "\n", inf, skip, outf, seek, dd_count);
2108 #endif
2109     install_handler(SIGINT, interrupt_handler);
2110     install_handler(SIGQUIT, interrupt_handler);
2111     install_handler(SIGPIPE, interrupt_handler);
2112     install_handler(SIGUSR1, siginfo_handler);
2113 
2114     infd = STDIN_FILENO;
2115     outfd = STDOUT_FILENO;
2116     iflag.pdt = -1;
2117     oflag.pdt = -1;
2118     if (iflag.zero && iflag.ff) {
2119         ccp = "<addr_as_data>";
2120         cc2p = "addr_as_data";
2121     } else if (iflag.ff) {
2122         ccp = "<0xff bytes>";
2123         cc2p = "ff";
2124     } else if (iflag.random) {
2125         ccp = "<random>";
2126         cc2p = "random";
2127 #ifdef HAVE_GETRANDOM
2128         {
2129             ssize_t ssz = getrandom(&seed, sizeof(seed), GRND_NONBLOCK);
2130 
2131             if (ssz < (ssize_t)sizeof(seed)) {
2132                 pr2serr("getrandom() failed, ret=%d\n", (int)ssz);
2133                 seed = (long)time(NULL);
2134             }
2135         }
2136 #else
2137         seed = (long)time(NULL);    /* use seconds since epoch as proxy */
2138 #endif
2139         if (verbose > 1)
2140             pr2serr("seed=%ld\n", seed);
2141 #ifdef HAVE_SRAND48_R
2142         srand48_r(seed, &drand);
2143 #else
2144         srand48(seed);
2145 #endif
2146     } else if (iflag.zero) {
2147        ccp = "<zero bytes>";
2148        cc2p = "00";
2149     }
2150     if (ccp) {
2151         if (inf[0]) {
2152             pr2serr("iflag=%s and if=%s contradict\n", cc2p, inf);
2153             return SG_LIB_CONTRADICT;
2154         }
2155         in_type = FT_RANDOM_0_FF;
2156         strcpy(inf, ccp);
2157         infd = -1;
2158     } else if (inf[0] && ('-' != inf[0])) {
2159         infd = open_if(inf, skip, bpt, &iflag, &in_type, verbose);
2160         if (infd < 0)
2161             return -infd;
2162     }
2163 
2164     if (outf[0] && ('-' != outf[0])) {
2165         outfd = open_of(outf, seek, bpt, &oflag, &out_type, verbose);
2166         if (outfd < -1)
2167             return -outfd;
2168     }
2169     if (do_verify) {
2170         if (! (FT_SG & out_type)) {
2171             pr2serr("--verify only supported when OFILE is a sg device or "
2172                     "oflag=sgio\n");
2173             ret = SG_LIB_CONTRADICT;
2174             goto bypass_copy;
2175         }
2176         if (oflag.sparse) {
2177             pr2serr("--verify cannot be used with oflag=sparse\n");
2178             ret = SG_LIB_CONTRADICT;
2179             goto bypass_copy;
2180         }
2181     }
2182     if (cdl_given && (! cdbsz_given)) {
2183         bool changed = false;
2184 
2185         if ((iflag.cdbsz < 16) && (iflag.cdl > 0)) {
2186             iflag.cdbsz = 16;
2187             changed = true;
2188         }
2189         if ((oflag.cdbsz < 16) && (! do_verify) && (oflag.cdl > 0)) {
2190             oflag.cdbsz = 16;
2191             changed = true;
2192         }
2193         if (changed)
2194             pr2serr(">> increasing cdbsz to 16 due to cdl > 0\n");
2195     }
2196     if (out2f[0]) {
2197         out2_type = dd_filetype(out2f);
2198         if ((out2fd = open(out2f, O_WRONLY | O_CREAT, 0666)) < 0) {
2199             res = errno;
2200             snprintf(ebuff, EBUFF_SZ,
2201                      ME "could not open %s for writing", out2f);
2202             perror(ebuff);
2203             return res;
2204         }
2205     } else
2206         out2fd = -1;
2207 
2208     if ((STDIN_FILENO == infd) && (STDOUT_FILENO == outfd)) {
2209         pr2serr("Can't have both 'if' as stdin _and_ 'of' as stdout\n");
2210         pr2serr("For more information use '--help'\n");
2211         return SG_LIB_CONTRADICT;
2212     }
2213     if (oflag.sparse) {
2214         if (STDOUT_FILENO == outfd) {
2215             pr2serr("oflag=sparse needs seekable output file\n");
2216             return SG_LIB_CONTRADICT;
2217         }
2218     }
2219 
2220     if ((dd_count < 0) || ((verbose > 0) && (0 == dd_count))) {
2221         in_num_sect = -1;
2222         in_sect_sz = -1;
2223         if (FT_SG & in_type) {
2224             res = scsi_read_capacity(infd, &in_num_sect, &in_sect_sz);
2225             if (SG_LIB_CAT_UNIT_ATTENTION == res) {
2226                 pr2serr("Unit attention (readcap in), continuing\n");
2227                 res = scsi_read_capacity(infd, &in_num_sect, &in_sect_sz);
2228             } else if (SG_LIB_CAT_ABORTED_COMMAND == res) {
2229                 pr2serr("Aborted command (readcap in), continuing\n");
2230                 res = scsi_read_capacity(infd, &in_num_sect, &in_sect_sz);
2231             }
2232             if (0 != res) {
2233                 if (res == SG_LIB_CAT_INVALID_OP)
2234                     pr2serr("read capacity not supported on %s\n", inf);
2235                 else if (res == SG_LIB_CAT_NOT_READY)
2236                     pr2serr("read capacity failed on %s - not ready\n", inf);
2237                 else
2238                     pr2serr("Unable to read capacity on %s\n", inf);
2239                 in_num_sect = -1;
2240             } else if (in_sect_sz != blk_sz)
2241                 pr2serr(">> warning: logical block size on %s confusion: "
2242                         "bs=%d, device claims=%d\n", inf, blk_sz, in_sect_sz);
2243         } else if (FT_BLOCK & in_type) {
2244             if (0 != read_blkdev_capacity(infd, &in_num_sect, &in_sect_sz)) {
2245                 pr2serr("Unable to read block capacity on %s\n", inf);
2246                 in_num_sect = -1;
2247             }
2248             if (blk_sz != in_sect_sz) {
2249                 pr2serr("logical block size on %s confusion: bs=%d, device "
2250                         "claims=%d\n", inf, blk_sz, in_sect_sz);
2251                 in_num_sect = -1;
2252             }
2253         }
2254         if (in_num_sect > skip)
2255             in_num_sect -= skip;
2256 
2257         out_num_sect = -1;
2258         out_sect_sz = -1;
2259         if (FT_SG & out_type) {
2260             res = scsi_read_capacity(outfd, &out_num_sect, &out_sect_sz);
2261             if (SG_LIB_CAT_UNIT_ATTENTION == res) {
2262                 pr2serr("Unit attention (readcap out), continuing\n");
2263                 res = scsi_read_capacity(outfd, &out_num_sect, &out_sect_sz);
2264             } else if (SG_LIB_CAT_ABORTED_COMMAND == res) {
2265                 pr2serr("Aborted command (readcap out), continuing\n");
2266                 res = scsi_read_capacity(outfd, &out_num_sect, &out_sect_sz);
2267             }
2268             if (0 != res) {
2269                 if (res == SG_LIB_CAT_INVALID_OP)
2270                     pr2serr("read capacity not supported on %s\n", outf);
2271                 else
2272                     pr2serr("Unable to read capacity on %s\n", outf);
2273                 out_num_sect = -1;
2274             } else if (blk_sz != out_sect_sz)
2275                 pr2serr(">> warning: logical block size on %s confusion: "
2276                         "bs=%d, device claims=%d\n", outf, blk_sz,
2277                         out_sect_sz);
2278         } else if (FT_BLOCK & out_type) {
2279             if (0 != read_blkdev_capacity(outfd, &out_num_sect,
2280                                           &out_sect_sz)) {
2281                 pr2serr("Unable to read block capacity on %s\n", outf);
2282                 out_num_sect = -1;
2283             } else if (blk_sz != out_sect_sz) {
2284                 pr2serr("logical block size on %s confusion: bs=%d, device "
2285                         "claims=%d\n", outf, blk_sz, out_sect_sz);
2286                 out_num_sect = -1;
2287             }
2288         }
2289         if (out_num_sect > seek)
2290             out_num_sect -= seek;
2291 #ifdef DEBUG
2292         pr2serr("Start of loop, count=%" PRId64 ", in_num_sect=%" PRId64
2293                 ", out_num_sect=%" PRId64 "\n", dd_count, in_num_sect,
2294                 out_num_sect);
2295 #endif
2296         if (dd_count < 0) {
2297             if (in_num_sect > 0) {
2298                 if (out_num_sect > 0)
2299                     dd_count = (in_num_sect > out_num_sect) ? out_num_sect :
2300                                                            in_num_sect;
2301                 else
2302                     dd_count = in_num_sect;
2303             } else
2304                 dd_count = out_num_sect;
2305         }
2306     }
2307 
2308     if (dd_count < 0) {
2309         pr2serr("Couldn't calculate count, please give one\n");
2310         return SG_LIB_CAT_OTHER;
2311     }
2312     if (! cdbsz_given) {
2313         if ((FT_SG & in_type) && (MAX_SCSI_CDBSZ != iflag.cdbsz) &&
2314             (((dd_count + skip) > UINT_MAX) || (bpt > USHRT_MAX))) {
2315             pr2serr("Note: SCSI command size increased to 16 bytes (for "
2316                     "'if')\n");
2317             iflag.cdbsz = MAX_SCSI_CDBSZ;
2318         }
2319         if ((FT_SG & out_type) && (MAX_SCSI_CDBSZ != oflag.cdbsz) &&
2320             (((dd_count + seek) > UINT_MAX) || (bpt > USHRT_MAX))) {
2321             pr2serr("Note: SCSI command size increased to 16 bytes (for "
2322                     "'of')\n");
2323             oflag.cdbsz = MAX_SCSI_CDBSZ;
2324         }
2325     }
2326 
2327     if (iflag.dio || iflag.direct || oflag.direct || (FT_RAW & in_type) ||
2328         (FT_RAW & out_type)) {  /* want heap buffer aligned to page_size */
2329 
2330         wrkPos = sg_memalign(blk_sz * bpt, 0, &wrkBuff, false);
2331         if (NULL == wrkPos) {
2332             pr2serr("sg_memalign: error, out of memory?\n");
2333             return sg_convert_errno(ENOMEM);
2334         }
2335     } else {
2336         wrkPos = sg_memalign(blk_sz * bpt, 0, &wrkBuff, false);
2337         if (0 == wrkPos) {
2338             pr2serr("Not enough user memory\n");
2339             return sg_convert_errno(ENOMEM);
2340         }
2341     }
2342 
2343     blocks_per = bpt;
2344 #ifdef DEBUG
2345     pr2serr("Start of loop, count=%" PRId64 ", blocks_per=%d\n", dd_count,
2346             blocks_per);
2347 #endif
2348     if (do_time) {
2349         start_tm.tv_sec = 0;
2350         start_tm.tv_usec = 0;
2351         gettimeofday(&start_tm, NULL);
2352         start_tm_valid = true;
2353     }
2354     req_count = dd_count;
2355 
2356     if (dry_run > 0) {
2357         pr2serr("Since --dry-run option given, bypassing copy\n");
2358         goto bypass_copy;
2359     }
2360 
2361     /* <<< main loop that does the copy >>> */
2362     while (dd_count > 0) {
2363         bytes_read = 0;
2364         bytes_of = 0;
2365         bytes_of2 = 0;
2366         penult_sparse_skip = sparse_skip;
2367         penult_blocks = penult_sparse_skip ? blocks : 0;
2368         sparse_skip = false;
2369         blocks = (dd_count > blocks_per) ? blocks_per : dd_count;
2370         if (FT_SG & in_type) {
2371             dio_tmp = iflag.dio;
2372             res = sg_read(infd, wrkPos, blocks, skip, blk_sz, &iflag,
2373                           &dio_tmp, &blks_read);
2374             if (-2 == res) {     /* ENOMEM, find what's available+try that */
2375                 if (ioctl(infd, SG_GET_RESERVED_SIZE, &buf_sz) < 0) {
2376                     perror("RESERVED_SIZE ioctls failed");
2377                     ret = res;
2378                     break;
2379                 }
2380                 if (buf_sz < MIN_RESERVED_SIZE)
2381                     buf_sz = MIN_RESERVED_SIZE;
2382                 blocks_per = (buf_sz + blk_sz - 1) / blk_sz;
2383                 if (blocks_per < blocks) {
2384                     blocks = blocks_per;
2385                     pr2serr("Reducing read to %d blocks per loop\n",
2386                             blocks_per);
2387                     res = sg_read(infd, wrkPos, blocks, skip, blk_sz,
2388                                   &iflag, &dio_tmp, &blks_read);
2389                 }
2390             }
2391             if (res) {
2392                 pr2serr("sg_read failed,%s at or after lba=%" PRId64 " [0x%"
2393                         PRIx64 "]\n", ((-2 == res) ?
2394                                  " try reducing bpt," : ""), skip, skip);
2395                 ret = res;
2396                 break;
2397             } else {
2398                 if (blks_read < blocks) {
2399                     dd_count = 0;   /* force exit after write */
2400                     blocks = blks_read;
2401                 }
2402                 in_full += blocks;
2403                 if (iflag.dio && (! dio_tmp))
2404                     dio_incomplete_count++;
2405             }
2406         } else if (FT_RANDOM_0_FF == in_type) {
2407             int j;
2408 
2409             res = blocks * blk_sz;
2410             if (iflag.zero && iflag.ff && (blk_sz >= 4)) {
2411                 uint32_t pos = (uint32_t)skip;
2412                 uint32_t off;
2413 
2414                 for (k = 0, off = 0; k < blocks; ++k, off += blk_sz, ++pos) {
2415                     for (j = 0; j < (blk_sz - 3); j += 4)
2416                         sg_put_unaligned_be32(pos, wrkPos + off + j);
2417                 }
2418             } else if (iflag.zero)
2419                 memset(wrkPos, 0, res);
2420             else if (iflag.ff)
2421                 memset(wrkPos, 0xff, res);
2422             else {
2423                 int kk, jj;
2424                 const int jbump = sizeof(uint32_t);
2425                 long rn;
2426                 uint8_t * bp;
2427 
2428                 bp = wrkPos;
2429                 for (kk = 0; kk < blocks; ++kk, bp += blk_sz) {
2430                     for (jj = 0; jj < blk_sz; jj += jbump) {
2431                        /* mrand48 takes uniformly from [-2^31, 2^31) */
2432 #ifdef HAVE_SRAND48_R
2433                         mrand48_r(&drand, &rn);
2434 #else
2435                         rn = mrand48();
2436 #endif
2437                         *((uint32_t *)(bp + jj)) = (uint32_t)rn;
2438                     }
2439                 }
2440             }
2441             bytes_read = res;
2442             in_full += blocks;
2443         } else {
2444             while (((res = read(infd, wrkPos, blocks * blk_sz)) < 0) &&
2445                    ((EINTR == errno) || (EAGAIN == errno) ||
2446                     (EBUSY == errno)))
2447                 ;
2448             if (verbose > 2)
2449                 pr2serr("read(unix): count=%d, res=%d\n", blocks * blk_sz,
2450                         res);
2451             if (res < 0) {
2452                 snprintf(ebuff, EBUFF_SZ, ME "reading, skip=%" PRId64 " ",
2453                          skip);
2454                 perror(ebuff);
2455                 ret = -1;
2456                 break;
2457             } else if (res < blocks * blk_sz) {
2458                 dd_count = 0;
2459                 blocks = res / blk_sz;
2460                 if ((res % blk_sz) > 0) {
2461                     blocks++;
2462                     in_partial++;
2463                 }
2464             }
2465             bytes_read = res;
2466             in_full += blocks;
2467         }
2468 
2469         if (0 == blocks)
2470             break;      /* nothing read so leave loop */
2471 
2472         if (out2f[0]) {
2473             while (((res = write(out2fd, wrkPos, blocks * blk_sz)) < 0) &&
2474                    ((EINTR == errno) || (EAGAIN == errno) ||
2475                     (EBUSY == errno)))
2476                 ;
2477             if (verbose > 2)
2478                 pr2serr("write to of2: count=%d, res=%d\n", blocks * blk_sz,
2479                         res);
2480             if (res < 0) {
2481                 snprintf(ebuff, EBUFF_SZ, ME "writing to of2, seek=%" PRId64
2482                          " ", seek);
2483                 perror(ebuff);
2484                 ret = -1;
2485                 break;
2486             }
2487             bytes_of2 = res;
2488         }
2489 
2490         if (oflag.sparse && (dd_count > blocks) &&
2491             (! (FT_DEV_NULL & out_type))) {
2492             if (NULL == zeros_buff) {
2493                 zeros_buff = sg_memalign(blocks * blk_sz, 0, &free_zeros_buff,
2494                                          false);
2495                 if (NULL == zeros_buff) {
2496                     pr2serr("zeros_buff sg_memalign failed\n");
2497                     ret = -1;
2498                     break;
2499                 }
2500             }
2501             if (0 == memcmp(wrkPos, zeros_buff, blocks * blk_sz))
2502                 sparse_skip = true;
2503         }
2504         if (sparse_skip) {
2505             if (FT_SG & out_type) {
2506                 out_sparse_num += blocks;
2507                 if (verbose > 2)
2508                     pr2serr("sparse bypassing sg_write: seek blk=%" PRId64
2509                             ", offset blks=%d\n", seek, blocks);
2510             } else if (FT_DEV_NULL & out_type)
2511                 ;
2512             else {
2513                 off64_t offset = (off64_t)blocks * blk_sz;
2514                 off64_t off_res;
2515 
2516                 if (verbose > 2)
2517                     pr2serr("sparse bypassing write: seek=%" PRId64 ", rel "
2518                             "offset=%" PRId64 "\n", (seek * blk_sz),
2519                             (int64_t)offset);
2520                 off_res = lseek64(outfd, offset, SEEK_CUR);
2521                 if (off_res < 0) {
2522                     pr2serr("sparse tried to bypass write: seek=%" PRId64
2523                             ", rel offset=%" PRId64 " but ...\n",
2524                             (seek * blk_sz), (int64_t)offset);
2525                     perror("lseek64 on output");
2526                     ret = SG_LIB_FILE_ERROR;
2527                     break;
2528                 } else if (verbose > 4)
2529                     pr2serr("oflag=sparse lseek64 result=%" PRId64 "\n",
2530                             (int64_t)off_res);
2531                 out_sparse_num += blocks;
2532             }
2533         } else if (FT_SG & out_type) {
2534             dio_tmp = oflag.dio;
2535             retries_tmp = oflag.retries;
2536             first = true;
2537             while (1) {
2538                 ret = sg_write(outfd, wrkPos, blocks, seek, blk_sz, &oflag,
2539                                &dio_tmp);
2540                 if ((0 == ret) || (SG_DD_BYPASS == ret))
2541                     break;
2542                 if ((SG_LIB_CAT_NOT_READY == ret) ||
2543                     (SG_LIB_SYNTAX_ERROR == ret))
2544                     break;
2545                 else if ((-2 == ret) && first) {
2546                     /* ENOMEM: find what's available and try that */
2547                     if (ioctl(outfd, SG_GET_RESERVED_SIZE, &buf_sz) < 0) {
2548                         perror("RESERVED_SIZE ioctls failed");
2549                         break;
2550                     }
2551                     if (buf_sz < MIN_RESERVED_SIZE)
2552                         buf_sz = MIN_RESERVED_SIZE;
2553                     blocks_per = (buf_sz + blk_sz - 1) / blk_sz;
2554                     if (blocks_per < blocks) {
2555                         blocks = blocks_per;
2556                         pr2serr("Reducing %s to %d blocks per loop\n",
2557                                 (do_verify ? "verify" : "write"), blocks);
2558                     } else
2559                         break;
2560                 } else if ((SG_LIB_CAT_UNIT_ATTENTION == ret) && first) {
2561                     if (--max_uas > 0)
2562                         pr2serr("Unit attention, continuing (w)\n");
2563                     else {
2564                         pr2serr("Unit attention, too many (w)\n");
2565                         break;
2566                     }
2567                 } else if ((SG_LIB_CAT_ABORTED_COMMAND == ret) && first) {
2568                     if (--max_aborted > 0)
2569                         pr2serr("Aborted command, continuing (w)\n");
2570                     else {
2571                         pr2serr("Aborted command, too many (w)\n");
2572                         break;
2573                     }
2574                 } else if (ret < 0)
2575                     break;
2576                 else if (retries_tmp > 0) {
2577                     pr2serr(">>> retrying a sgio %s, lba=0x%" PRIx64 "\n",
2578                             (do_verify ? "verify" : "write"), (uint64_t)seek);
2579                     --retries_tmp;
2580                     ++num_retries;
2581                     if (unrecovered_errs > 0)
2582                         --unrecovered_errs;
2583                 } else
2584                     break;
2585                 first = false;
2586             }
2587             if (SG_DD_BYPASS == ret)
2588                 ret = 0;        /* not bumping out_full */
2589             else if (0 != ret) {
2590                 pr2serr("sg_write failed,%s seek=%" PRId64 "\n",
2591                         ((-2 == ret) ? " try reducing bpt," : ""), seek);
2592                 break;
2593             } else {
2594                 out_full += blocks;
2595                 if (oflag.dio && (! dio_tmp))
2596                     dio_incomplete_count++;
2597             }
2598         } else if (FT_DEV_NULL & out_type)
2599             out_full += blocks; /* act as if written out without error */
2600         else {
2601             while (((res = write(outfd, wrkPos, blocks * blk_sz)) < 0) &&
2602                    ((EINTR == errno) || (EAGAIN == errno) ||
2603                     (EBUSY == errno)))
2604                 ;
2605             if (verbose > 2)
2606                 pr2serr("write(unix): count=%d, res=%d\n", blocks * blk_sz,
2607                         res);
2608             if (res < 0) {
2609                 snprintf(ebuff, EBUFF_SZ, ME "writing, seek=%" PRId64 " ",
2610                          seek);
2611                 perror(ebuff);
2612                 ret = -1;
2613                 break;
2614             } else if (res < blocks * blk_sz) {
2615                 pr2serr("output file probably full, seek=%" PRId64 " ", seek);
2616                 blocks = res / blk_sz;
2617                 out_full += blocks;
2618                 if ((res % blk_sz) > 0)
2619                     out_partial++;
2620                 ret = -1;
2621                 break;
2622             } else {
2623                 out_full += blocks;
2624                 bytes_of = res;
2625             }
2626         }
2627 #ifdef HAVE_POSIX_FADVISE
2628         {
2629             int rt, in_valid, out2_valid, out_valid;
2630 
2631             in_valid = ((FT_OTHER == in_type) || (FT_BLOCK == in_type));
2632             out2_valid = ((FT_OTHER == out2_type) || (FT_BLOCK == out2_type));
2633             out_valid = ((FT_OTHER == out_type) || (FT_BLOCK == out_type));
2634             if (iflag.nocache && (bytes_read > 0) && in_valid) {
2635                 rt = posix_fadvise(infd, 0, (skip * blk_sz) + bytes_read,
2636                                    POSIX_FADV_DONTNEED);
2637                 // rt = posix_fadvise(infd, (skip * blk_sz), bytes_read,
2638                                    // POSIX_FADV_DONTNEED);
2639                 // rt = posix_fadvise(infd, 0, 0, POSIX_FADV_DONTNEED);
2640                 if (rt)         /* returns error as result */
2641                     pr2serr("posix_fadvise on read, skip=%" PRId64
2642                             " ,err=%d\n", skip, rt);
2643             }
2644             if ((oflag.nocache & 2) && (bytes_of2 > 0) && out2_valid) {
2645                 rt = posix_fadvise(out2fd, 0, 0, POSIX_FADV_DONTNEED);
2646                 if (rt)
2647                     pr2serr("posix_fadvise on of2, seek=%" PRId64
2648                             " ,err=%d\n", seek, rt);
2649             }
2650             if ((oflag.nocache & 1) && (bytes_of > 0) && out_valid) {
2651                 rt = posix_fadvise(outfd, 0, 0, POSIX_FADV_DONTNEED);
2652                 if (rt)
2653                     pr2serr("posix_fadvise on output, seek=%" PRId64
2654                             " ,err=%d\n", seek, rt);
2655             }
2656         }
2657 #endif
2658         if (dd_count > 0)
2659             dd_count -= blocks;
2660         skip += blocks;
2661         seek += blocks;
2662         if (progress > 0) {
2663             if (check_progress()) {
2664                 calc_duration_throughput(true);
2665                 print_stats("");
2666             }
2667         }
2668     } /* end of main loop that does the copy ... */
2669 
2670     if (ret && penult_sparse_skip && (penult_blocks > 0)) {
2671         /* if error and skipped last output due to sparse ... */
2672         if ((FT_SG & out_type) || (FT_DEV_NULL & out_type))
2673             ;
2674         else {
2675             /* ... try writing to extend ofile to length prior to error */
2676             while (((res = write(outfd, zeros_buff, penult_blocks * blk_sz))
2677                     < 0) && ((EINTR == errno) || (EAGAIN == errno) ||
2678                              (EBUSY == errno)))
2679                 ;
2680             if (verbose > 2)
2681                 pr2serr("write(unix, sparse after error): count=%d, res=%d\n",
2682                         penult_blocks * blk_sz, res);
2683             if (res < 0) {
2684                 snprintf(ebuff, EBUFF_SZ, ME "writing(sparse after error), "
2685                         "seek=%" PRId64 " ", seek);
2686                 perror(ebuff);
2687             }
2688         }
2689     }
2690 
2691     if (do_sync) {
2692         if (FT_SG & out_type) {
2693             pr2serr(">> Synchronizing cache on %s\n", outf);
2694             res = sg_ll_sync_cache_10(outfd, false, false, 0, 0, 0, true, 0);
2695             if (SG_LIB_CAT_UNIT_ATTENTION == res) {
2696                 pr2serr("Unit attention (out, sync cache), continuing\n");
2697                 res = sg_ll_sync_cache_10(outfd, false, false, 0, 0, 0,
2698                                           false, 0);
2699             }
2700             if (0 != res)
2701                 pr2serr("Unable to synchronize cache\n");
2702         }
2703     }
2704 
2705 bypass_copy:
2706     if (do_time)
2707         calc_duration_throughput(false);
2708     if (progress > 0)
2709         pr2serr("\nCompleted:\n");
2710 
2711     if (wrkBuff)
2712         free(wrkBuff);
2713     if (free_zeros_buff)
2714         free(free_zeros_buff);
2715     if ((STDIN_FILENO != infd) && (infd >= 0))
2716         close(infd);
2717     if (! ((STDOUT_FILENO == outfd) || (FT_DEV_NULL & out_type))) {
2718         if (outfd >= 0)
2719             close(outfd);
2720     }
2721     if (dry_run > 0)
2722         goto bypass2;
2723 
2724     if (0 != dd_count) {
2725         pr2serr("Some error occurred,");
2726         if (0 == ret)
2727             ret = SG_LIB_CAT_OTHER;
2728     }
2729     print_stats("");
2730     if (dio_incomplete_count) {
2731         int fd;
2732         char c;
2733 
2734         pr2serr(">> Direct IO requested but incomplete %d times\n",
2735                 dio_incomplete_count);
2736         if ((fd = open(sg_allow_dio, O_RDONLY)) >= 0) {
2737             if (1 == read(fd, &c, 1)) {
2738                 if ('0' == c)
2739                     pr2serr(">>> %s set to '0' but should be set to '1' for "
2740                             "direct IO\n", sg_allow_dio);
2741             }
2742             close(fd);
2743         }
2744     }
2745     if (sum_of_resids)
2746         pr2serr(">> Non-zero sum of residual counts=%d\n", sum_of_resids);
2747 
2748 bypass2:
2749     return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
2750 }
2751