xref: /aosp_15_r20/external/sg3_utils/examples/sg_compare_and_write.txt (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1#		sg_compare_and_write.txt
2# This file provides a usage example of sg_compare_and_write.
3# sg_compare_and_write accepts a buffer containing 2 logical instances:
4# - the verify instance: used to match the current content of the LBA range
5# - the write instance: used to write to the LBA if the verify succeeds
6#
7# In case of failure to verify the data, the command will return with check
8# condition with the sense code set to MISCOMPARE DURING VERIFY OPERATION.
9#
10# The following example shows initialization, successful and unsuccessful
11# compare and write using sg3_utils. I am using caw_buf_zero2one and
12# caw_buf_one2zero as shown below.
13
14$ hexdump /tmp/caw_buf_zero2one
150000000 0000 0000 0000 0000 0000 0000 0000 0000
16*
170000200 1111 1111 1111 1111 1111 1111 1111 1111
18*
190000400
20
21$ hexdump /tmp/caw_buf_one2zero
220000000 1111 1111 1111 1111 1111 1111 1111 1111
23*
240000200 0000 0000 0000 0000 0000 0000 0000 0000
25*
260000400
27
28$ sg_map -i -x
29/dev/sg0  0 0 0 0  0  /dev/sda  ATA       ST3320613AS       CC2H
30/dev/sg1  3 0 0 0  5  /dev/scd0  HL-DT-ST  DVD-RAM GH22NS30  1.01
31/dev/sg2  5 0 0 0  0  /dev/sdb  KMNRIO    K2                0000
32/dev/sg3  5 0 0 1  0  /dev/sdc  KMNRIO    K2                0000
33
34# First I zero out the volume to make sure that the first compare and write
35# will succeed
36$ sg_write_same --16 -i /dev/zero -n 0x200000  -x 512 /dev/sdc
37
38$ dd if=/dev/sdc bs=512 count=1 skip=100 2>/dev/null | hexdump
390000000 0000 0000 0000 0000 0000 0000 0000 0000
40*
410000200
42
43$ ./sg_compare_and_write --in=/tmp/caw_buf_zero2one --lba=100 --xferlen=1024 /dev/sdc
44
45# contents of LBA 100 are a block of ones
46$ dd if=/dev/sdc bs=512 count=1 skip=100 2>/dev/null | hexdump
470000000 1111 1111 1111 1111 1111 1111 1111 1111
48*
490000200
50
51# We repeat the same compare and write command (zero2one input buffer).
52# compare and write fails since the verify failed (compared the zero block to
53# the actual 1 block in LBA 100
54$ ./sg_compare_and_write --in=/tmp/caw_buf_zero2one --lba=100 --xferlen=1024 /dev/sdc
55COMPARE AND WRITE:  Fixed format, current;  Sense key: Miscompare
56 Additional sense: Miscompare during verify operation
57sg_compare_and_write: SCSI COMPARE AND WRITE failed
58
59# Now we use the second buffer (one2zero)
60$ ./sg_compare_and_write --in=/tmp/caw_buf_one2zero --lba=100 --xferlen=1024 /dev/sdc
61
62# operation succeeded, contents of LBA 100 are back to zero
63$ dd if=/dev/sdc bs=512 count=1 skip=100 2>/dev/null | hexdump
640000000 0000 0000 0000 0000 0000 0000 0000 0000
65*
660000200
67
68