1
/* A utility program for copying files. Similar to 'dd' but using
2
* the 'Extended Copy' command.
4
* Copyright (c) 2011-2013 Hannes Reinecke, SUSE Labs
6
* Largely taken from 'sg_dd', which has the
8
* Copyright (C) 1999 - 2010 D. Gilbert and P. Allworth
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2, or (at your option)
14
This program is a specialisation of the Unix "dd" command in which
15
either the input or the output file is a scsi generic device, raw
16
device, a block device or a normal file. The block size ('bs') is
17
assumed to be 512 if not given. This program complains if 'ibs' or
18
'obs' are given with a value that differs from 'bs' (or the default 512).
19
If 'if' is not given or 'if=-' then stdin is assumed. If 'of' is
20
not given or 'of=-' then stdout assumed.
22
A non-standard argument "bpt" (blocks per transfer) is added to control
23
the maximum number of blocks in each transfer. The default value is 128.
24
For example if "bs=512" and "bpt=32" then a maximum of 32 blocks (16 KiB
25
in this case) is transferred to or from the sg device in a single SCSI
28
This version is designed for the linux kernel 2.4, 2.6 and 3 series.
31
#define _XOPEN_SOURCE 600
33
#define _GNU_SOURCE /* resolves u_char typedef in scsi/scsi.h [lk 2.4] */
45
#define __STDC_FORMAT_MACROS 1
47
#include <sys/ioctl.h>
48
#include <sys/types.h>
50
#include <sys/sysmacros.h>
53
#include <linux/major.h>
54
#include <linux/fs.h> /* <sys/mount.h> */
60
#include "sg_cmds_basic.h"
61
#include "sg_cmds_extra.h"
62
#include "sg_io_linux.h"
64
static const char * version_str = "0.34 20130507";
66
#define ME "sg_xcopy: "
74
#define DEF_BLOCK_SIZE 512
75
#define DEF_BLOCKS_PER_TRANSFER 128
76
#define DEF_BLOCKS_PER_2048TRANSFER 32
78
#define DEF_MODE_RESP_LEN 252
79
#define RW_ERR_RECOVERY_MP 1
81
#define CONTROL_MP 0xa
83
#define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
84
#define READ_CAP_REPLY_LEN 8
85
#define RCAP16_REPLY_LEN 32
87
#define DEF_TIMEOUT 60000 /* 60,000 millisecs == 60 seconds */
90
#define RAW_MAJOR 255 /*unlikey value */
93
#define SG_LIB_FLOCK_ERR 90
95
#define FT_OTHER 1 /* filetype is probably normal */
96
#define FT_SG 2 /* filetype is sg char device or supports
98
#define FT_RAW 4 /* filetype is raw char device */
99
#define FT_DEV_NULL 8 /* either "/dev/null" or "." as filename */
100
#define FT_ST 16 /* filetype is st char device (tape) */
101
#define FT_BLOCK 32 /* filetype is block device */
102
#define FT_FIFO 64 /* filetype is a fifo (name pipe) */
103
#define FT_ERROR 128 /* couldn't "stat" file */
107
#define TD_FC_WWPN_AND_PORT 4
116
#define TD_IP_COPY_SERVICE 2048
119
#define DEV_NULL_MINOR_NUM 3
121
#define MIN_RESERVED_SIZE 8192
123
#define MAX_UNIT_ATTENTIONS 10
124
#define MAX_ABORTED_CMDS 256
126
static int64_t dd_count = -1;
127
static int64_t in_full = 0;
128
static int in_partial = 0;
129
static int64_t out_full = 0;
130
static int out_partial = 0;
132
static int recovered_errs = 0;
133
static int unrecovered_errs = 0;
134
static int num_retries = 0;
137
static int do_time = 0;
138
static int verbose = 0;
139
static int start_tm_valid = 0;
140
static struct timeval start_tm;
141
static int blk_sz = 0;
142
static int priority = 1;
143
static int list_id_usage = -1;
145
static char xcopy_flag_cat = 0;
146
static char xcopy_flag_dc = 0;
149
char fname[INOUTF_SZ];
153
unsigned long min_bytes;
154
unsigned long max_bytes;
160
int pad; /* Data descriptor PAD bit (residual data treatment) */
161
int pdt; /* Peripheral device type */
167
static struct xcopy_fp_t ifp;
168
static struct xcopy_fp_t ofp;
170
static void calc_duration_throughput(int contin);
174
install_handler(int sig_num, void (*sig_handler) (int sig))
176
struct sigaction sigact;
177
sigaction (sig_num, NULL, &sigact);
178
if (sigact.sa_handler != SIG_IGN)
180
sigact.sa_handler = sig_handler;
181
sigemptyset (&sigact.sa_mask);
183
sigaction (sig_num, &sigact, NULL);
189
print_stats(const char * str)
192
fprintf(stderr, " remaining block count=%"PRId64"\n", dd_count);
193
fprintf(stderr, "%s%"PRId64"+%d records in\n", str, in_full - in_partial,
195
fprintf(stderr, "%s%"PRId64"+%d records out\n", str,
196
out_full - out_partial, out_partial);
198
if (recovered_errs > 0)
199
fprintf(stderr, "%s%d recovered errors\n", str, recovered_errs);
201
fprintf(stderr, "%s%d retries attempted\n", str, num_retries);
202
else if (unrecovered_errs)
203
fprintf(stderr, "%s%d unrecovered error(s)\n", str,
210
interrupt_handler(int sig)
212
struct sigaction sigact;
214
sigact.sa_handler = SIG_DFL;
215
sigemptyset(&sigact.sa_mask);
217
sigaction(sig, &sigact, NULL);
218
fprintf(stderr, "Interrupted by signal,");
220
calc_duration_throughput(0);
222
kill(getpid (), sig);
227
siginfo_handler(int sig)
229
sig = sig; /* dummy to stop -W warning messages */
230
fprintf(stderr, "Progress report, continuing ...\n");
232
calc_duration_throughput(1);
236
static int bsg_major_checked = 0;
237
static int bsg_major = 0;
242
const char * proc_devices = "/proc/devices";
249
if (NULL == (fp = fopen(proc_devices, "r"))) {
251
fprintf(stderr, "fopen %s failed: %s\n", proc_devices,
255
while ((cp = fgets(b, sizeof(b), fp))) {
256
if ((1 == sscanf(b, "%s", a)) &&
257
(0 == memcmp(a, "Character", 9)))
260
while (cp && (cp = fgets(b, sizeof(b), fp))) {
261
if (2 == sscanf(b, "%d %s", &n, a)) {
262
if (0 == strcmp("bsg", a)) {
271
fprintf(stderr, "found bsg_major=%d\n", bsg_major);
273
fprintf(stderr, "found no bsg char device in %s\n", proc_devices);
279
open_sg(struct xcopy_fp_t * fp, int verbose)
281
int devmajor, devminor, offset;
282
struct sg_simple_inquiry_resp sir;
283
char ebuff[EBUFF_SZ];
286
devmajor = major(fp->devno);
287
devminor = minor(fp->devno);
289
if (fp->sg_type & FT_SG) {
290
snprintf(ebuff, EBUFF_SZ, "%s", fp->fname);
291
} else if (fp->sg_type & FT_BLOCK || fp->sg_type & FT_OTHER) {
294
snprintf(ebuff, EBUFF_SZ, "/sys/dev/block/%d:%d/partition",
296
if ((fd = open(ebuff, O_RDONLY)) >= 0) {
297
len = read(fd, ebuff, EBUFF_SZ);
299
perror("read partition");
301
offset = strtoul(ebuff, NULL, 10);
306
snprintf(ebuff, EBUFF_SZ, "/dev/block/%d:%d", devmajor, devminor);
308
snprintf(ebuff, EBUFF_SZ, "/dev/char/%d:%d", devmajor, devminor);
310
fp->sg_fd = sg_cmds_open_device(ebuff, 0, verbose);
312
snprintf(ebuff, EBUFF_SZ,
313
ME "could not open %s device %d:%d for sg",
314
fp->sg_type & FT_BLOCK ? "block" : "char",
319
if (sg_simple_inquiry(fp->sg_fd, &sir, 0, verbose)) {
320
fprintf(stderr, "INQUIRY failed on %s\n", ebuff);
321
sg_cmds_close_device(fp->sg_fd);
326
fp->pdt = sir.peripheral_type;
328
fprintf(stderr, " %s: %.8s %.16s %.4s [pdt=%d]\n",
329
fp->fname, sir.vendor, sir.product, sir.revision, fp->pdt);
335
dd_filetype(struct xcopy_fp_t * fp)
338
size_t len = strlen(fp->fname);
340
if ((1 == len) && ('.' == fp->fname[0]))
342
if (stat(fp->fname, &st) < 0)
344
if (S_ISCHR(st.st_mode)) {
345
fp->devno = st.st_rdev;
346
/* major() and minor() defined in sys/sysmacros.h */
347
if ((MEM_MAJOR == major(st.st_rdev)) &&
348
(DEV_NULL_MINOR_NUM == minor(st.st_rdev)))
350
if (RAW_MAJOR == major(st.st_rdev))
352
if (SCSI_GENERIC_MAJOR == major(st.st_rdev))
354
if (SCSI_TAPE_MAJOR == major(st.st_rdev))
356
if (! bsg_major_checked) {
357
bsg_major_checked = 1;
360
if (bsg_major == (int)major(st.st_rdev))
362
} else if (S_ISBLK(st.st_mode)) {
363
fp->devno = st.st_rdev;
365
} else if (S_ISFIFO(st.st_mode)) {
366
fp->devno = st.st_dev;
369
fp->devno = st.st_dev;
370
return FT_OTHER | FT_BLOCK;
375
dd_filetype_str(int ft, char * buff)
379
if (FT_DEV_NULL & ft)
380
off += snprintf(buff + off, 32, "null device ");
382
off += snprintf(buff + off, 32, "SCSI generic (sg) device ");
384
off += snprintf(buff + off, 32, "block device ");
386
off += snprintf(buff + off, 32, "fifo (named pipe) ");
388
off += snprintf(buff + off, 32, "SCSI tape device ");
390
off += snprintf(buff + off, 32, "raw device ");
392
off += snprintf(buff + off, 32, "other (perhaps ordinary file) ");
394
off += snprintf(buff + off, 32, "unable to 'stat' file ");
399
seg_desc_from_dd_type(int in_ft, int in_off, int out_ft, int out_off)
416
if (in_off || out_off)
459
fprintf(stderr, "Usage: "
460
"sg_xcopy [bs=BS] [count=COUNT] [ibs=BS] [if=IFILE]"
462
" [obs=BS] [of=OFILE] [oflag=FLAGS] "
463
"[seek=SEEK] [skip=SKIP]\n"
464
" [--help] [--version]\n\n"
465
" [bpt=BPT] [cat=0|1] [dc=0|1] "
466
"[id_usage=hold|discard|disable]\n"
467
" [list_id=ID] [prio=PRIO] [time=0|1] "
469
" [--on_dst|--on_src] [--verbose]\n"
471
" bpt is blocks_per_transfer (default is 128 or 32 "
473
" bs block size (default is 512)\n");
475
" cat segment descriptor CAT bit (default: 0)\n"
476
" count number of blocks to copy (def: device size)\n"
477
" dc segment descriptor DC bit (default: 0)\n"
478
" ibs input block size (if given must be same as "
480
" id_usage sets list id usage field to hold (0), "
483
" if file or device to read from (def: stdin)\n"
484
" iflag comma separated list from: [cat,dc,excl,"
486
" list_id sets list identifier field to ID (default: 1)\n"
487
" obs output block size (if given must be same as "
489
" of file or device to write to (def: stdout), "
492
" treated as /dev/null\n"
493
" oflag comma separated list from: [append,cat,pad,dc,"
496
" prio set priority field to PRIO (def: 1)\n"
497
" seek block position to start writing to OFILE\n"
498
" skip block position to start reading from IFILE\n"
499
" time 0->no timing(def), 1->time plus calculate "
501
" verbose 0->quiet(def), 1->some noise, 2->more noise, "
503
" --help print out this usage message then exit\n"
504
" --on_dst send XCOPY command to the output file/device\n"
505
" --on_src send XCOPY command to the input file/device.\n"
506
" Default if this and --on_dst options not "
508
" --verbose same action as verbose=1\n"
509
" --version print version information then exit\n\n"
510
"Copy from IFILE to OFILE, similar to dd command; "
511
"but using the SCSI\nEXTENDED COPY (XCOPY) command.\n");
515
scsi_encode_seg_desc(unsigned char *seg_desc, int seg_desc_type,
516
int64_t num_blk, uint64_t src_lba, uint64_t dst_lba)
518
int seg_desc_len = 0;
520
seg_desc[0] = seg_desc_type;
521
seg_desc[1] = xcopy_flag_cat | (xcopy_flag_dc << 1);
522
if (seg_desc_type == 0x02) {
525
seg_desc[5] = 0; /* Source target index */
526
seg_desc[7] = 1; /* Destination target index */
527
seg_desc[10] = (num_blk >> 8) & 0xff;
528
seg_desc[11] = num_blk & 0xff;
529
seg_desc[12] = (src_lba >> 56) & 0xff;
530
seg_desc[13] = (src_lba >> 48) & 0xff;
531
seg_desc[14] = (src_lba >> 40) & 0xff;
532
seg_desc[15] = (src_lba >> 32) & 0xff;
533
seg_desc[16] = (src_lba >> 24) & 0xff;
534
seg_desc[17] = (src_lba >> 16) & 0xff;
535
seg_desc[18] = (src_lba >> 8) & 0xff;
536
seg_desc[19] = src_lba & 0xff;
537
seg_desc[20] = (dst_lba >> 56) & 0xff;
538
seg_desc[21] = (dst_lba >> 48) & 0xff;
539
seg_desc[22] = (dst_lba >> 40) & 0xff;
540
seg_desc[23] = (dst_lba >> 32) & 0xff;
541
seg_desc[24] = (dst_lba >> 24) & 0xff;
542
seg_desc[25] = (dst_lba >> 16) & 0xff;
543
seg_desc[26] = (dst_lba >> 8) & 0xff;
544
seg_desc[27] = dst_lba & 0xff;
546
seg_desc[2] = (seg_desc_len >> 8) & 0xFF;
547
seg_desc[3] = seg_desc_len & 0xFF;
549
return seg_desc_len + 4;
553
scsi_extended_copy(int sg_fd, unsigned char list_id,
554
unsigned char *src_desc, int src_desc_len,
555
unsigned char *dst_desc, int dst_desc_len,
556
int seg_desc_type, int64_t num_blk,
557
uint64_t src_lba, uint64_t dst_lba)
559
unsigned char xcopyBuff[256];
560
int desc_offset = 16;
564
verb = (verbose ? verbose - 1: 0);
566
memset(xcopyBuff, 0, 256);
567
xcopyBuff[0] = list_id;
568
xcopyBuff[1] = (list_id_usage << 3) | priority;
570
xcopyBuff[3] = src_desc_len + dst_desc_len; /* Two target descriptors */
571
memcpy(xcopyBuff + desc_offset, src_desc, src_desc_len);
572
desc_offset += src_desc_len;
573
memcpy(xcopyBuff + desc_offset, dst_desc, dst_desc_len);
574
desc_offset += dst_desc_len;
575
seg_desc_len = scsi_encode_seg_desc(xcopyBuff + desc_offset,
576
seg_desc_type, num_blk,
578
xcopyBuff[11] = seg_desc_len; /* One segment descriptor */
579
desc_offset += seg_desc_len;
581
fprintf(stderr, "\nParameter list in hex (length %d):\n", desc_offset);
582
dStrHex((const char *)xcopyBuff, desc_offset, 1);
584
return sg_ll_extended_copy(sg_fd, xcopyBuff, desc_offset, 0, verb);
587
/* Return of 0 -> success, see sg_ll_read_capacity*() otherwise */
589
scsi_read_capacity(struct xcopy_fp_t *xfp)
593
unsigned char rcBuff[RCAP16_REPLY_LEN];
596
verb = (verbose ? verbose - 1: 0);
597
res = sg_ll_readcap_10(xfp->sg_fd, 0, 0, rcBuff,
598
READ_CAP_REPLY_LEN, 0, verb);
602
if ((0xff == rcBuff[0]) && (0xff == rcBuff[1]) && (0xff == rcBuff[2]) &&
603
(0xff == rcBuff[3])) {
606
res = sg_ll_readcap_16(xfp->sg_fd, 0, 0, rcBuff,
607
RCAP16_REPLY_LEN, 0, verb);
610
for (k = 0, ls = 0; k < 8; ++k) {
614
xfp->num_sect = ls + 1;
615
xfp->sect_sz = (rcBuff[8] << 24) | (rcBuff[9] << 16) |
616
(rcBuff[10] << 8) | rcBuff[11];
618
ui = ((rcBuff[0] << 24) | (rcBuff[1] << 16) | (rcBuff[2] << 8) |
620
/* take care not to sign extend values > 0x7fffffff */
621
xfp->num_sect = (int64_t)ui + 1;
622
xfp->sect_sz = (rcBuff[4] << 24) | (rcBuff[5] << 16) |
623
(rcBuff[6] << 8) | rcBuff[7];
626
fprintf(stderr, " %s: number of blocks=%"PRId64" [0x%"PRIx64"], "
627
"block size=%d\n", xfp->fname, xfp->num_sect, xfp->num_sect,
633
scsi_operating_parameter(struct xcopy_fp_t *xfp, int is_target)
636
unsigned char rcBuff[256];
637
unsigned int rcBuffLen = 256, len, n, td_list = 0;
638
unsigned long num, max_target_num, max_segment_num, max_segment_len;
639
unsigned long max_desc_len, max_inline_data, held_data_limit;
642
verb = (verbose ? verbose - 1: 0);
643
res = sg_ll_receive_copy_results(xfp->sg_fd, 0x03, 0, rcBuff, rcBuffLen,
648
len = (rcBuff[0] << 24) | (rcBuff[1] << 16) | (rcBuff[2] << 8) |
650
if (len > rcBuffLen) {
651
fprintf(stderr, " <<report too long for internal buffer,"
652
" output truncated\n");
655
fprintf(stderr, "\nOutput response in hex:\n");
656
dStrHex((const char *)rcBuff, len, 1);
658
max_target_num = rcBuff[8] << 8 | rcBuff[9];
659
max_segment_num = rcBuff[10] << 8 | rcBuff[11];
660
max_desc_len = rcBuff[12] << 24 | rcBuff[13] << 16 | rcBuff[14] << 8 |
662
max_segment_len = rcBuff[16] << 24 | rcBuff[17] << 16 |
663
rcBuff[18] << 8 | rcBuff[19];
664
xfp->max_bytes = max_segment_len ? max_segment_len : ULONG_MAX;
665
max_inline_data = rcBuff[20] << 24 | rcBuff[21] << 16 | rcBuff[22] << 8 |
668
printf(" >> Receive copy results (report operating parameters):\n");
669
printf(" Maximum target descriptor count: %lu\n", max_target_num);
670
printf(" Maximum segment descriptor count: %lu\n", max_segment_num);
671
printf(" Maximum descriptor list length: %lu\n", max_desc_len);
672
printf(" Maximum segment length: %lu\n", max_segment_len);
673
printf(" Maximum inline data length: %lu\n", max_inline_data);
675
held_data_limit = rcBuff[24] << 24 | rcBuff[25] << 16 |
676
rcBuff[26] << 8 | rcBuff[27];
677
if (list_id_usage < 0) {
678
if (!held_data_limit)
684
printf(" Held data limit: %lu (usage: %d)\n",
685
held_data_limit, list_id_usage);
686
num = rcBuff[28] << 24 | rcBuff[29] << 16 | rcBuff[30] << 8 |
688
printf(" Maximum stream device transfer size: %lu\n", num);
689
printf(" Maximum concurrent copies: %u\n", rcBuff[36]);
690
printf(" Data segment granularity: %u bytes\n", 1 << rcBuff[37]);
691
printf(" Inline data granularity: %u bytes\n", 1 << rcBuff[38]);
692
printf(" Held data granularity: %u bytes\n", 1 << rcBuff[39]);
694
printf(" Implemented descriptor list:\n");
696
xfp->min_bytes = 1 << rcBuff[37];
698
for (n = 0; n < rcBuff[43]; n++) {
699
switch(rcBuff[44 + n]) {
700
case 0x00: /* copy block to stream device */
701
if (!is_target && (xfp->sg_type & FT_BLOCK))
703
if (is_target && (xfp->sg_type & FT_ST))
706
printf(" Copy Block to Stream device\n");
708
case 0x01: /* copy stream to block device */
709
if (!is_target && (xfp->sg_type & FT_ST))
711
if (is_target && (xfp->sg_type & FT_BLOCK))
714
printf(" Copy Stream to Block device\n");
716
case 0x02: /* copy block to block device */
717
if (!is_target && (xfp->sg_type & FT_BLOCK))
719
if (is_target && (xfp->sg_type & FT_BLOCK))
722
printf(" Copy Block to Block device\n");
724
case 0x03: /* copy stream to stream device */
725
if (!is_target && (xfp->sg_type & FT_ST))
727
if (is_target && (xfp->sg_type & FT_ST))
730
printf(" Copy Stream to Stream device\n");
732
case 0x04: /* copy inline data to stream device */
733
if (!is_target && (xfp->sg_type & FT_OTHER))
735
if (is_target && (xfp->sg_type & FT_ST))
738
printf(" Copy inline data to Stream device\n");
740
case 0x05: /* copy embedded data to stream device */
741
if (!is_target && (xfp->sg_type & FT_OTHER))
743
if (is_target && (xfp->sg_type & FT_ST))
746
printf(" Copy embedded data to Stream device\n");
748
case 0x06: /* Read from stream device and discard */
749
if (!is_target && (xfp->sg_type & FT_ST))
751
if (is_target && (xfp->sg_type & FT_DEV_NULL))
754
printf(" Read from stream device and discard\n");
756
case 0x07: /* Verify block or stream device operation */
757
if (!is_target && (xfp->sg_type & (FT_ST | FT_BLOCK)))
759
if (is_target && (xfp->sg_type & (FT_ST | FT_BLOCK)))
762
printf(" Verify block or stream device operation\n");
764
case 0x08: /* copy block device with offset to stream device */
765
if (!is_target && (xfp->sg_type & FT_BLOCK))
767
if (is_target && (xfp->sg_type & FT_ST))
770
printf(" Copy block device with offset to stream "
773
case 0x09: /* copy stream device to block device with offset */
774
if (!is_target && (xfp->sg_type & FT_ST))
776
if (is_target && (xfp->sg_type & FT_BLOCK))
779
printf(" Copy stream device to block device with "
782
case 0x0a: /* copy block device with offset to block device with
784
if (!is_target && (xfp->sg_type & FT_BLOCK))
786
if (is_target && (xfp->sg_type & FT_BLOCK))
789
printf(" Copy block device with offset to block "
790
"device with offset\n");
792
case 0x0b: /* copy block device to stream device and hold data */
793
if (!is_target && (xfp->sg_type & FT_BLOCK))
795
if (is_target && (xfp->sg_type & FT_ST))
798
printf(" Copy block device to stream device and hold "
801
case 0x0c: /* copy stream device to block device and hold data */
802
if (!is_target && (xfp->sg_type & FT_ST))
804
if (is_target && (xfp->sg_type & FT_BLOCK))
807
printf(" Copy stream device to block device and hold "
810
case 0x0d: /* copy block device to block device and hold data */
811
if (!is_target && (xfp->sg_type & FT_BLOCK))
813
if (is_target && (xfp->sg_type & FT_BLOCK))
816
printf(" Copy block device to block device and hold "
819
case 0x0e: /* copy stream device to stream device and hold data */
820
if (!is_target && (xfp->sg_type & FT_ST))
822
if (is_target && (xfp->sg_type & FT_ST))
825
printf(" Copy block device to block device and hold "
828
case 0x0f: /* read from stream device and hold data */
829
if (!is_target && (xfp->sg_type & FT_ST))
831
if (is_target && (xfp->sg_type & FT_DEV_NULL))
834
printf(" Read from stream device and hold data\n");
836
case 0xe0: /* FC N_Port_Name */
838
printf(" FC N_Port_Name target descriptor\n");
839
td_list |= TD_FC_WWPN;
841
case 0xe1: /* FC Port_ID */
843
printf(" FC Port_ID target descriptor\n");
844
td_list |= TD_FC_PORT;
846
case 0xe2: /* FC N_Port_ID with N_Port_Name checking */
848
printf(" FC N_Port_ID with N_Port_Name target "
850
td_list |= TD_FC_WWPN_AND_PORT;
852
case 0xe3: /* Parallel Interface T_L */
854
printf(" SPI T_L target descriptor\n");
857
case 0xe4: /* identification descriptor */
859
printf(" Identification target descriptor\n");
862
case 0xe5: /* IPv4 */
864
printf(" IPv4 target descriptor\n");
867
case 0xe6: /* Alias */
869
printf(" Alias target descriptor\n");
872
case 0xe7: /* RDMA */
874
printf(" RDMA target descriptor\n");
877
case 0xe8: /* FireWire */
879
printf(" IEEE 1394 target descriptor\n");
884
printf(" SAS target descriptor\n");
887
case 0xea: /* IPv6 */
889
printf(" IPv6 target descriptor\n");
892
case 0xeb: /* IP Copy Service */
894
printf(" IP Copy Service target descriptor\n");
895
td_list |= TD_IP_COPY_SERVICE;
899
printf(" ROD target descriptor\n");
903
fprintf(stderr, ">> Unhandled target descriptor 0x%02x\n",
909
fprintf(stderr, ">> no matching target descriptor supported\n");
916
decode_designation_descriptor(const unsigned char * ucp, int i_len)
918
int m, p_id, piv, c_set, assoc, desig_type, d_id, naa;
920
const unsigned char * ip;
925
p_id = ((ucp[0] >> 4) & 0xf);
926
c_set = (ucp[0] & 0xf);
927
piv = ((ucp[1] & 0x80) ? 1 : 0);
928
assoc = ((ucp[1] >> 4) & 0x3);
929
desig_type = (ucp[1] & 0xf);
930
printf(" designator type: %d, code set: %d\n", desig_type, c_set);
931
if (piv && ((1 == assoc) || (2 == assoc)))
932
printf(" transport: %s\n",
933
sg_get_trans_proto_str(p_id, sizeof(b), b));
935
switch (desig_type) {
936
case 0: /* vendor specific */
938
if ((1 == c_set) || (2 == c_set)) { /* ASCII or UTF-8 */
939
for (k = 0; (k < i_len) && isprint(ip[k]); ++k)
945
printf(" vendor specific: %.*s\n", i_len, ip);
947
dStrHex((const char *)ip, i_len, 0);
949
case 1: /* T10 vendor identification */
950
printf(" vendor id: %.8s\n", ip);
952
printf(" vendor specific: %.*s\n", i_len - 8, ip + 8);
954
case 2: /* EUI-64 based */
955
if ((8 != i_len) && (12 != i_len) && (16 != i_len)) {
956
fprintf(stderr, " << expect 8, 12 and 16 byte "
957
"EUI, got %d>>\n", i_len);
958
dStrHex((const char *)ip, i_len, 0);
962
for (m = 0; m < i_len; ++m)
963
printf("%02x", (unsigned int)ip[m]);
968
fprintf(stderr, " << unexpected code set %d for "
970
dStrHex((const char *)ip, i_len, 0);
973
naa = (ip[0] >> 4) & 0xff;
974
if (! ((2 == naa) || (5 == naa) || (6 == naa))) {
975
fprintf(stderr, " << unexpected NAA [0x%x]>>\n", naa);
976
dStrHex((const char *)ip, i_len, 0);
979
if ((5 == naa) && (0x10 == i_len)) {
981
fprintf(stderr, " << unexpected NAA 5 len 16, assuming "
987
fprintf(stderr, " << unexpected NAA 2 identifier "
988
"length: 0x%x>>\n", i_len);
989
dStrHex((const char *)ip, i_len, 0);
992
d_id = (((ip[0] & 0xf) << 8) | ip[1]);
993
/* c_id = ((ip[2] << 16) | (ip[3] << 8) | ip[4]); */
994
/* vsi = ((ip[5] << 16) | (ip[6] << 8) | ip[7]); */
996
for (m = 0; m < 8; ++m)
997
printf("%02x", (unsigned int)ip[m]);
999
} else if (5 == naa) {
1001
fprintf(stderr, " << unexpected NAA 5 identifier "
1002
"length: 0x%x>>\n", i_len);
1003
dStrHex((const char *)ip, i_len, 0);
1006
/* c_id = (((ip[0] & 0xf) << 20) | (ip[1] << 12) | */
1007
/* (ip[2] << 4) | ((ip[3] & 0xf0) >> 4)); */
1009
for (m = 1; m < 5; ++m) {
1014
for (m = 0; m < 8; ++m)
1015
printf("%02x", (unsigned int)ip[m]);
1017
} else if (6 == naa) {
1019
fprintf(stderr, " << unexpected NAA 6 identifier "
1020
"length: 0x%x>>\n", i_len);
1021
dStrHex((const char *)ip, i_len, 0);
1024
/* c_id = (((ip[0] & 0xf) << 20) | (ip[1] << 12) | */
1025
/* (ip[2] << 4) | ((ip[3] & 0xf0) >> 4)); */
1027
for (m = 1; m < 5; ++m) {
1032
for (m = 0; m < 16; ++m)
1033
printf("%02x", (unsigned int)ip[m]);
1037
case 4: /* Relative target port */
1038
if ((1 != c_set) || (1 != assoc) || (4 != i_len)) {
1039
fprintf(stderr, " << expected binary code_set, target "
1040
"port association, length 4>>\n");
1041
dStrHex((const char *)ip, i_len, 0);
1044
d_id = ((ip[2] << 8) | ip[3]);
1045
printf(" Relative target port: 0x%x\n", d_id);
1047
case 5: /* (primary) Target port group */
1048
if ((1 != c_set) || (1 != assoc) || (4 != i_len)) {
1049
fprintf(stderr, " << expected binary code_set, target "
1050
"port association, length 4>>\n");
1051
dStrHex((const char *)ip, i_len, 0);
1054
d_id = ((ip[2] << 8) | ip[3]);
1055
printf(" Target port group: 0x%x\n", d_id);
1057
case 6: /* Logical unit group */
1058
if ((1 != c_set) || (0 != assoc) || (4 != i_len)) {
1059
fprintf(stderr, " << expected binary code_set, logical "
1060
"unit association, length 4>>\n");
1061
dStrHex((const char *)ip, i_len, 0);
1064
d_id = ((ip[2] << 8) | ip[3]);
1065
printf(" Logical unit group: 0x%x\n", d_id);
1067
case 7: /* MD5 logical unit identifier */
1068
if ((1 != c_set) || (0 != assoc)) {
1069
printf(" << expected binary code_set, logical "
1070
"unit association>>\n");
1071
dStrHex((const char *)ip, i_len, 0);
1074
printf(" MD5 logical unit identifier:\n");
1075
dStrHex((const char *)ip, i_len, 0);
1077
case 8: /* SCSI name string */
1079
fprintf(stderr, " << expected UTF-8 code_set>>\n");
1080
dStrHex((const char *)ip, i_len, 0);
1083
printf(" SCSI name string:\n");
1084
/* does %s print out UTF-8 ok??
1085
* Seems to depend on the locale. Looks ok here with my
1086
* locale setting: en_AU.UTF-8
1088
printf(" %s\n", (const char *)ip);
1090
default: /* reserved */
1091
dStrHex((const char *)ip, i_len, 0);
1097
desc_from_vpd_id(int sg_fd, unsigned char *desc, int desc_len,
1098
unsigned int block_size, int pad)
1101
unsigned char rcBuff[256], *ucp, *best = NULL;
1102
unsigned int len = 254;
1103
int off = -1, u, i_len, best_len = 0, assoc, desig, f_desig = 0;
1105
memset(rcBuff, 0xff, len);
1106
res = sg_ll_inquiry(sg_fd, 0, 1, 0x83, rcBuff, 4, 1, verbose);
1108
fprintf(stderr, "VPD inquiry failed with %d\n", res);
1110
} else if (rcBuff[1] != 0x83) {
1111
fprintf(stderr, "invalid VPD response\n");
1112
return SG_LIB_CAT_MALFORMED;
1114
len = ((rcBuff[2] << 8) + rcBuff[3]) + 4;
1115
res = sg_ll_inquiry(sg_fd, 0, 1, 0x83, rcBuff, len, 1, verbose);
1117
fprintf(stderr, "VPD inquiry failed with %d\n", res);
1119
} else if (rcBuff[1] != 0x83) {
1120
fprintf(stderr, "invalid VPD response\n");
1121
return SG_LIB_CAT_MALFORMED;
1124
fprintf(stderr, "Output response in hex:\n");
1125
dStrHex((const char *)rcBuff, len, 1);
1128
while ((u = sg_vpd_dev_id_iter(rcBuff + 4, len - 4, &off, 0, -1, -1)) ==
1130
ucp = rcBuff + 4 + off;
1132
if (((unsigned int)off + i_len + 4) > len) {
1133
fprintf(stderr, " VPD page error: designator length %d longer "
1134
"than\n remaining response length=%d\n", i_len,
1136
return SG_LIB_CAT_MALFORMED;
1138
assoc = ((ucp[1] >> 4) & 0x3);
1139
desig = (ucp[1] & 0xf);
1141
fprintf(stderr, " Desc %d: assoc %u desig %u len %d\n", off,
1142
assoc, desig, i_len);
1143
/* Descriptor must be less than 16 bytes */
1152
if (!best || f_desig < 2) {
1157
} else if (desig == 1) {
1158
if (!best || f_desig == 0) {
1163
} else if (desig == 0) {
1173
decode_designation_descriptor(best, best_len);
1174
if (best_len + 4 < desc_len) {
1175
memset(desc, 0, 32);
1177
memcpy(desc + 4, best, best_len + 4);
1179
desc[28] = pad << 2;
1180
desc[29] = (block_size >> 16) & 0xff;
1181
desc[30] = (block_size >> 8) & 0xff;
1182
desc[31] = block_size & 0xff;
1184
fprintf(stderr, "Descriptor in hex (bs %d):\n", block_size);
1185
dStrHex((const char *)desc, 32, 1);
1189
return best_len + 8;
1195
calc_duration_throughput(int contin)
1197
struct timeval end_tm, res_tm;
1201
if (start_tm_valid && (start_tm.tv_sec || start_tm.tv_usec)) {
1202
blks = (in_full > out_full) ? in_full : out_full;
1203
gettimeofday(&end_tm, NULL);
1204
res_tm.tv_sec = end_tm.tv_sec - start_tm.tv_sec;
1205
res_tm.tv_usec = end_tm.tv_usec - start_tm.tv_usec;
1206
if (res_tm.tv_usec < 0) {
1208
res_tm.tv_usec += 1000000;
1211
a += (0.000001 * res_tm.tv_usec);
1212
b = (double)blk_sz * blks;
1213
fprintf(stderr, "time to transfer data%s: %d.%06d secs",
1214
(contin ? " so far" : ""), (int)res_tm.tv_sec,
1215
(int)res_tm.tv_usec);
1216
if ((a > 0.00001) && (b > 511))
1217
fprintf(stderr, " at %.2f MB/sec\n", b / (a * 1000000.0));
1219
fprintf(stderr, "\n");
1223
/* Process arguments given to 'iflag=" or 'oflag=" options. Returns 0
1224
* on success, 1 on error. */
1226
process_flags(const char * arg, struct xcopy_fp_t * fp)
1232
strncpy(buff, arg, sizeof(buff));
1233
buff[sizeof(buff) - 1] = '\0';
1234
if ('\0' == buff[0]) {
1235
fprintf(stderr, "no flag found\n");
1240
np = strchr(cp, ',');
1243
if (0 == strcmp(cp, "append"))
1245
else if (0 == strcmp(cp, "pad"))
1247
else if (0 == strcmp(cp, "excl"))
1249
else if (0 == strcmp(cp, "null"))
1251
else if (0 == strcmp(cp, "flock"))
1254
fprintf(stderr, "unrecognised flag: %s\n", cp);
1262
/* Returns open input file descriptor (>= 0) or a negative value
1263
* (-SG_LIB_FILE_ERROR or -SG_LIB_CAT_OTHER) if error.
1266
open_if(struct xcopy_fp_t * ifp, int verbose)
1268
int infd = -1, flags, fl, res;
1269
char ebuff[EBUFF_SZ];
1271
ifp->sg_type = dd_filetype(ifp);
1274
fprintf(stderr, " >> Input file type: %s, devno %d:%d\n",
1275
dd_filetype_str(ifp->sg_type, ebuff),
1276
major(ifp->devno), minor(ifp->devno));
1277
if (FT_ERROR & ifp->sg_type) {
1278
fprintf(stderr, ME "unable access %s\n", ifp->fname);
1285
if ((infd = open(ifp->fname, fl | flags)) < 0) {
1287
if ((infd = open(ifp->fname, fl | flags)) < 0) {
1288
snprintf(ebuff, EBUFF_SZ,
1289
ME "could not open %s for sg reading", ifp->fname);
1295
fprintf(stderr, " open input(sg_io), flags=0x%x\n",
1299
res = flock(infd, LOCK_EX | LOCK_NB);
1302
snprintf(ebuff, EBUFF_SZ, ME "flock(LOCK_EX | LOCK_NB) on %s "
1303
"failed", ifp->fname);
1305
return -SG_LIB_FLOCK_ERR;
1313
return -SG_LIB_FILE_ERROR;
1316
/* Returns open output file descriptor (>= 0), -1 for don't
1317
* bother opening (e.g. /dev/null), or a more negative value
1318
* (-SG_LIB_FILE_ERROR or -SG_LIB_CAT_OTHER) if error.
1321
open_of(struct xcopy_fp_t * ofp, int verbose)
1323
int outfd, flags, res;
1324
char ebuff[EBUFF_SZ];
1326
ofp->sg_type = dd_filetype(ofp);
1328
fprintf(stderr, " >> Output file type: %s, devno %d:%d\n",
1329
dd_filetype_str(ofp->sg_type, ebuff),
1330
major(ofp->devno), minor(ofp->devno));
1332
if (!(FT_DEV_NULL & ofp->sg_type)) {
1333
flags = O_RDWR | O_NONBLOCK;
1336
if ((outfd = open(ofp->fname, flags)) < 0) {
1337
snprintf(ebuff, EBUFF_SZ,
1338
ME "could not open %s for sg writing", ofp->fname);
1343
fprintf(stderr, " open output(sg_io), flags=0x%x\n",
1346
outfd = -1; /* don't bother opening */
1349
res = flock(outfd, LOCK_EX | LOCK_NB);
1352
snprintf(ebuff, EBUFF_SZ, ME "flock(LOCK_EX | LOCK_NB) on %s "
1353
"failed", ofp->fname);
1355
return -SG_LIB_FLOCK_ERR;
1361
return -SG_LIB_FILE_ERROR;
1366
main(int argc, char * argv[])
1372
int bpt = DEF_BLOCKS_PER_TRANSFER;
1380
int infd, outfd, xcopy_fd;
1382
unsigned char list_id = 1;
1383
int list_id_given = 0;
1384
unsigned char src_desc[256];
1385
unsigned char dst_desc[256];
1392
ifp.fname[0] = '\0';
1393
ofp.fname[0] = '\0';
1399
"Won't default both IFILE to stdin _and_ OFILE to stdout\n");
1400
fprintf(stderr, "For more information use '--help'\n");
1401
return SG_LIB_SYNTAX_ERROR;
1404
for (k = 1; k < argc; k++) {
1406
strncpy(str, argv[k], STR_SZ);
1407
str[STR_SZ - 1] = '\0';
1410
for (key = str, buf = key; *buf && *buf != '=';)
1414
if (0 == strncmp(key, "app", 3)) {
1415
ifp.append = sg_get_num(buf);
1416
ofp.append = ifp.append;
1417
} else if (0 == strcmp(key, "bpt")) {
1418
bpt = sg_get_num(buf);
1420
fprintf(stderr, ME "bad argument to 'bpt='\n");
1421
return SG_LIB_SYNTAX_ERROR;
1424
} else if (0 == strcmp(key, "bs")) {
1425
blk_sz = sg_get_num(buf);
1427
fprintf(stderr, ME "bad argument to 'bs='\n");
1428
return SG_LIB_SYNTAX_ERROR;
1430
} else if (0 == strcmp(key, "list_id")) {
1431
ret = sg_get_num(buf);
1432
if (-1 == ret || ret > 0xff) {
1433
fprintf(stderr, ME "bad argument to 'list_id='\n");
1434
return SG_LIB_SYNTAX_ERROR;
1436
list_id = (ret & 0xff);
1438
} else if (0 == strcmp(key, "id_usage")) {
1439
if (!strncmp(buf, "hold", 4))
1441
else if (!strncmp(buf, "discard", 7))
1443
else if (!strncmp(buf, "disable", 7))
1446
fprintf(stderr, ME "bad argument to 'list_id_usage='\n");
1447
return SG_LIB_SYNTAX_ERROR;
1449
} else if (0 == strcmp(key, "conv"))
1450
fprintf(stderr, ME ">>> ignoring all 'conv=' arguments\n");
1451
else if (0 == strcmp(key, "count")) {
1452
if (0 != strcmp("-1", buf)) {
1453
dd_count = sg_get_llnum(buf);
1454
if (-1LL == dd_count) {
1455
fprintf(stderr, ME "bad argument to 'count='\n");
1456
return SG_LIB_SYNTAX_ERROR;
1458
} /* treat 'count=-1' as calculate count (same as not given) */
1459
} else if (0 == strcmp(key, "prio")) {
1460
priority = sg_get_num(buf);
1461
} else if (0 == strcmp(key, "cat")) {
1462
xcopy_flag_cat = sg_get_num(buf);
1463
if (xcopy_flag_cat < 0 || xcopy_flag_cat > 1) {
1464
fprintf(stderr, ME "bad argument to 'cat='\n");
1465
return SG_LIB_SYNTAX_ERROR;
1467
} else if (0 == strcmp(key, "dc")) {
1468
xcopy_flag_dc = sg_get_num(buf);
1469
if (xcopy_flag_dc < 0 || xcopy_flag_dc > 1) {
1470
fprintf(stderr, ME "bad argument to 'dc='\n");
1471
return SG_LIB_SYNTAX_ERROR;
1473
} else if (0 == strcmp(key, "ibs")) {
1474
ibs = sg_get_num(buf);
1475
} else if (strcmp(key, "if") == 0) {
1476
if ('\0' != ifp.fname[0]) {
1477
fprintf(stderr, "Second IFILE argument??\n");
1478
return SG_LIB_SYNTAX_ERROR;
1480
strncpy(ifp.fname, buf, INOUTF_SZ);
1481
} else if (0 == strcmp(key, "iflag")) {
1482
if (process_flags(buf, &ifp)) {
1483
fprintf(stderr, ME "bad argument to 'iflag='\n");
1484
return SG_LIB_SYNTAX_ERROR;
1486
} else if (0 == strcmp(key, "obs")) {
1487
obs = sg_get_num(buf);
1488
} else if (strcmp(key, "of") == 0) {
1489
if ('\0' != ofp.fname[0]) {
1490
fprintf(stderr, "Second OFILE argument??\n");
1491
return SG_LIB_SYNTAX_ERROR;
1493
strncpy(ofp.fname, buf, INOUTF_SZ);
1494
} else if (0 == strcmp(key, "oflag")) {
1495
if (process_flags(buf, &ofp)) {
1496
fprintf(stderr, ME "bad argument to 'oflag='\n");
1497
return SG_LIB_SYNTAX_ERROR;
1500
} else if (0 == strcmp(key, "retries")) {
1501
ifp.retries = sg_get_num(buf);
1502
ofp.retries = ifp.retries;
1503
if (-1 == ifp.retries) {
1504
fprintf(stderr, ME "bad argument to 'retries='\n");
1505
return SG_LIB_SYNTAX_ERROR;
1508
} else if (0 == strcmp(key, "seek")) {
1509
seek = sg_get_llnum(buf);
1511
fprintf(stderr, ME "bad argument to 'seek='\n");
1512
return SG_LIB_SYNTAX_ERROR;
1514
} else if (0 == strcmp(key, "skip")) {
1515
skip = sg_get_llnum(buf);
1517
fprintf(stderr, ME "bad argument to 'skip='\n");
1518
return SG_LIB_SYNTAX_ERROR;
1520
} else if (0 == strcmp(key, "time"))
1521
do_time = sg_get_num(buf);
1522
else if (0 == strncmp(key, "verb", 4))
1523
verbose = sg_get_num(buf);
1524
else if (0 == strncmp(key, "--on_src", 8))
1526
else if (0 == strncmp(key, "--on_dst", 8))
1528
else if ((0 == strncmp(key, "--help", 7)) ||
1529
(0 == strncmp(key, "-h", 2)) ||
1530
(0 == strcmp(key, "-?"))) {
1533
} else if ((0 == strncmp(key, "--vers", 6)) ||
1534
(0 == strcmp(key, "-V"))) {
1535
fprintf(stderr, ME "%s\n", version_str);
1537
} else if (0 == strncmp(key, "--verb", 6))
1539
else if (0 == strcmp(key, "-vvvvv"))
1541
else if (0 == strcmp(key, "-vvvv"))
1543
else if (0 == strcmp(key, "-vvv"))
1545
else if (0 == strcmp(key, "-vv"))
1547
else if (0 == strcmp(key, "-v"))
1550
fprintf(stderr, "Unrecognized option '%s'\n", key);
1551
fprintf(stderr, "For more information use '--help'\n");
1552
return SG_LIB_SYNTAX_ERROR;
1555
if (on_src && on_dst) {
1556
fprintf(stderr, "Syntax error - either specify --on_src OR "
1558
fprintf(stderr, "For more information use '--help'\n");
1559
return SG_LIB_SYNTAX_ERROR;
1561
if (!on_src && !on_dst)
1563
if ((ibs && blk_sz && (ibs != blk_sz)) ||
1564
(obs && blk_sz && (obs != blk_sz))) {
1565
fprintf(stderr, "If 'ibs' or 'obs' given must be same as 'bs'\n");
1566
fprintf(stderr, "For more information use '--help'\n");
1567
return SG_LIB_SYNTAX_ERROR;
1574
if ((skip < 0) || (seek < 0)) {
1575
fprintf(stderr, "skip and seek cannot be negative\n");
1576
return SG_LIB_SYNTAX_ERROR;
1578
if ((ofp.append > 0) && (seek > 0)) {
1579
fprintf(stderr, "Can't use both append and seek switches\n");
1580
return SG_LIB_SYNTAX_ERROR;
1583
fprintf(stderr, "bpt must be greater than 0\n");
1584
return SG_LIB_SYNTAX_ERROR;
1586
if (list_id_usage == 3) { /* list_id usage disabled */
1590
fprintf(stderr, "list_id disabled by id_usage flag\n");
1591
return SG_LIB_SYNTAX_ERROR;
1596
fprintf(stderr, ME "%s if=%s skip=%" PRId64 " of=%s seek=%" PRId64
1597
" count=%" PRId64 "\n", (on_src)?"on-source":"on-destination",
1598
ifp.fname, skip, ofp.fname, seek, dd_count);
1600
install_handler(SIGINT, interrupt_handler);
1601
install_handler(SIGQUIT, interrupt_handler);
1602
install_handler(SIGPIPE, interrupt_handler);
1603
install_handler(SIGUSR1, siginfo_handler);
1605
infd = STDIN_FILENO;
1606
outfd = STDOUT_FILENO;
1609
if (ifp.fname[0] && ('-' != ifp.fname[0])) {
1610
infd = open_if(&ifp, verbose);
1615
if (ofp.fname[0] && ('-' != ofp.fname[0])) {
1616
outfd = open_of(&ofp, verbose);
1621
if (open_sg(&ifp, verbose) < 0)
1622
return SG_LIB_CAT_INVALID_OP;
1624
if (open_sg(&ofp, verbose) < 0)
1625
return SG_LIB_CAT_INVALID_OP;
1627
if ((STDIN_FILENO == infd) && (STDOUT_FILENO == outfd)) {
1629
"Can't have both 'if' as stdin _and_ 'of' as stdout\n");
1630
fprintf(stderr, "For more information use '--help'\n");
1631
return SG_LIB_SYNTAX_ERROR;
1634
res = scsi_read_capacity(&ifp);
1635
if (SG_LIB_CAT_UNIT_ATTENTION == res) {
1636
fprintf(stderr, "Unit attention (readcap in), continuing\n");
1637
res = scsi_read_capacity(&ifp);
1638
} else if (SG_LIB_CAT_ABORTED_COMMAND == res) {
1639
fprintf(stderr, "Aborted command (readcap in), continuing\n");
1640
res = scsi_read_capacity(&ifp);
1643
if (res == SG_LIB_CAT_INVALID_OP)
1644
fprintf(stderr, "read capacity not supported on %s\n",
1646
else if (res == SG_LIB_CAT_NOT_READY)
1647
fprintf(stderr, "read capacity failed on %s - not "
1648
"ready\n", ifp.fname);
1650
fprintf(stderr, "Unable to read capacity on %s\n", ifp.fname);
1652
} else if (ibs && ifp.sect_sz != ibs) {
1653
fprintf(stderr, ">> warning: block size on %s confusion: "
1654
"ibs=%d, device claims=%d\n", ifp.fname, ibs, ifp.sect_sz);
1656
if (skip && ifp.num_sect < skip) {
1657
fprintf(stderr, "argument to 'skip=' exceeds device size "
1658
"(max %"PRId64")\n", ifp.num_sect);
1659
return SG_LIB_SYNTAX_ERROR;
1662
res = scsi_read_capacity(&ofp);
1663
if (SG_LIB_CAT_UNIT_ATTENTION == res) {
1664
fprintf(stderr, "Unit attention (readcap out), continuing\n");
1665
res = scsi_read_capacity(&ofp);
1666
} else if (SG_LIB_CAT_ABORTED_COMMAND == res) {
1668
"Aborted command (readcap out), continuing\n");
1669
res = scsi_read_capacity(&ofp);
1672
if (res == SG_LIB_CAT_INVALID_OP)
1673
fprintf(stderr, "read capacity not supported on %s\n",
1676
fprintf(stderr, "Unable to read capacity on %s\n", ofp.fname);
1678
} else if (obs && obs != ofp.sect_sz) {
1679
fprintf(stderr, ">> warning: block size on %s confusion: "
1680
"obs=%d, device claims=%d\n", ofp.fname, obs, ofp.sect_sz);
1682
if (seek && ofp.num_sect < seek) {
1683
fprintf(stderr, "argument to 'seek=' exceeds device size "
1684
"(max %"PRId64")\n", ofp.num_sect);
1685
return SG_LIB_SYNTAX_ERROR;
1687
if ((dd_count < 0) || ((verbose > 0) && (0 == dd_count))) {
1688
if (xcopy_flag_dc == 0) {
1689
dd_count = ifp.num_sect - skip;
1690
if (dd_count * ifp.sect_sz > (ofp.num_sect - seek) * ofp.sect_sz)
1691
dd_count = (ofp.num_sect - seek) * ofp.sect_sz / ifp.sect_sz;
1693
dd_count = ofp.num_sect - seek;
1694
if (dd_count * ofp.sect_sz > (ifp.num_sect - skip) * ifp.sect_sz)
1695
dd_count = (ifp.num_sect - skip) * ifp.sect_sz / ofp.sect_sz;
1701
dd_bytes = dd_count * ofp.sect_sz;
1703
dd_bytes = dd_count * ifp.sect_sz;
1705
if (dd_bytes > ifp.num_sect * ifp.sect_sz) {
1706
fprintf(stderr, "access beyond end of source device "
1707
"(max %"PRId64")\n", ifp.num_sect);
1708
return SG_LIB_SYNTAX_ERROR;
1710
if (dd_bytes > ofp.num_sect * ofp.sect_sz) {
1711
fprintf(stderr, "access beyond end of target device "
1712
"(max %"PRId64")\n", ofp.num_sect);
1713
return SG_LIB_SYNTAX_ERROR;
1717
res = scsi_operating_parameter(&ifp, 0);
1719
if (SG_LIB_CAT_UNIT_ATTENTION == -res) {
1720
fprintf(stderr, "Unit attention (oper parm), continuing\n");
1721
res = scsi_operating_parameter(&ifp, 0);
1723
if (-res == SG_LIB_CAT_INVALID_OP) {
1724
fprintf(stderr, "receive copy results not supported on %s\n",
1729
} else if (-res == SG_LIB_CAT_NOT_READY)
1730
fprintf(stderr, "receive copy results failed on %s - not "
1731
"ready\n", ifp.fname);
1733
fprintf(stderr, "Unable to receive copy results on %s\n",
1738
} else if (res == 0)
1739
return SG_LIB_CAT_INVALID_OP;
1743
printf(" >> using VPD identification for source %s\n", ifp.fname);
1744
src_desc_len = desc_from_vpd_id(ifp.sg_fd, src_desc, 256,
1745
ifp.sect_sz, ifp.pad);
1746
if (src_desc_len > 256) {
1747
fprintf(stderr, "source descriptor too large (%d bytes)\n", res);
1748
return SG_LIB_CAT_MALFORMED;
1751
return SG_LIB_CAT_INVALID_OP;
1754
res = scsi_operating_parameter(&ofp, 1);
1756
if (SG_LIB_CAT_UNIT_ATTENTION == -res) {
1757
fprintf(stderr, "Unit attention (oper parm), continuing\n");
1758
res = scsi_operating_parameter(&ofp, 1);
1760
if (-res == SG_LIB_CAT_INVALID_OP) {
1761
fprintf(stderr, "receive copy results not supported on %s\n",
1766
} else if (-res == SG_LIB_CAT_NOT_READY)
1767
fprintf(stderr, "receive copy results failed on %s - not "
1768
"ready\n", ofp.fname);
1770
fprintf(stderr, "Unable to receive copy results on %s\n",
1775
} else if (res == 0)
1776
return SG_LIB_CAT_INVALID_OP;
1780
printf(" >> using VPD identification for destination %s\n",
1782
dst_desc_len = desc_from_vpd_id(ofp.sg_fd, dst_desc, 256,
1783
ofp.sect_sz, ofp.pad);
1784
if (dst_desc_len > 256) {
1785
fprintf(stderr, "destination descriptor too large (%d bytes)\n",
1787
return SG_LIB_CAT_MALFORMED;
1790
return SG_LIB_CAT_INVALID_OP;
1794
fprintf(stderr, "Couldn't calculate count, please give one\n");
1795
return SG_LIB_CAT_OTHER;
1798
if ((unsigned long)dd_count < ifp.min_bytes / ifp.sect_sz) {
1799
fprintf(stderr, "not enough data to read (min %ld bytes)\n",
1801
return SG_LIB_CAT_OTHER;
1803
if ((unsigned long)dd_count < ofp.min_bytes / ofp.sect_sz) {
1804
fprintf(stderr, "not enough data to write (min %ld bytes)\n",
1806
return SG_LIB_CAT_OTHER;
1810
if (xcopy_flag_dc) {
1811
if ((unsigned long)bpt * ofp.sect_sz > ofp.max_bytes) {
1812
fprintf(stderr, "bpt too large (max %ld blocks)\n",
1813
ofp.max_bytes / ofp.sect_sz);
1814
return SG_LIB_SYNTAX_ERROR;
1817
if ((unsigned long)bpt * ifp.sect_sz > ifp.max_bytes) {
1818
fprintf(stderr, "bpt too large (max %ld blocks)\n",
1819
ifp.max_bytes / ifp.sect_sz);
1820
return SG_LIB_SYNTAX_ERROR;
1825
bpt = ofp.max_bytes / ofp.sect_sz;
1827
bpt = ifp.max_bytes / ifp.sect_sz;
1830
seg_desc_type = seg_desc_from_dd_type(ifp.sg_type, 0, ofp.sg_type, 0);
1833
start_tm.tv_sec = 0;
1834
start_tm.tv_usec = 0;
1835
gettimeofday(&start_tm, NULL);
1841
"Start of loop, count=%"PRId64", bpt=%d, "
1842
"lba_in=%"PRId64", lba_out=%"PRId64"\n",
1843
dd_count, bpt, skip, seek);
1845
xcopy_fd = (on_src) ? infd : outfd;
1846
while (dd_count > 0) {
1851
res = scsi_extended_copy(xcopy_fd, list_id, src_desc, src_desc_len,
1852
dst_desc, dst_desc_len, seg_desc_type,
1853
blocks, skip, seek);
1863
calc_duration_throughput(0);
1865
fprintf(stderr, "sg_xcopy: failed with error %d (%"PRId64
1866
" blocks left)\n", res, dd_count);
1868
fprintf(stderr, "sg_xcopy: %"PRId64" blocks, %d command%s\n",
1869
in_full, num_xcopy, ((num_xcopy > 1) ? "s" : ""));