1SG_WRITE_SAME(8)                   SG3_UTILS                  SG_WRITE_SAME(8)
2
3
4

NAME

6       sg_write_same - send SCSI WRITE SAME command
7

SYNOPSIS

9       sg_write_same  [--10]  [--16]  [--32] [--anchor] [--grpnum=GN] [--help]
10       [--in=IF]  [--lba=LBA]  [--lbdata]  [--num=NUM]   [--ndob]   [--pbdata]
11       [--timeout=TO]   [--unmap]  [--verbose]  [--version]  [--wrprotect=WPR]
12       [--xferlen=LEN] DEVICE
13

DESCRIPTION

15       Send the SCSI WRITE SAME (10, 16 or 32 byte) command  to  DEVICE.  This
16       command  writes  the given block NUM times to consecutive blocks on the
17       DEVICE starting at logical block address LBA.
18
19       The length of the block to be written multiple times is  obtained  from
20       either  the  LEN argument, or the length of the given input file IF, or
21       by calling READ CAPACITY(16) on DEVICE. The contents of the block to be
22       written  are obtained from the input file IF or zeros are used. If READ
23       CAPACITY(16) is called (which implies IF was not given) and the PROT_EN
24       bit  is  set  then  an extra 8 bytes (i.e.  more than the logical block
25       size) of 0xff are sent. If READ CAPACITY(16)  fails  then  READ  CAPAC‐
26       ITY(10) is used to determine the block size.
27
28       If  neither  --10,  --16  nor --32 is given then WRITE SAME(10) is sent
29       unless one of the following conditions  is  met.   If  LBA  (plus  NUM)
30       exceeds 32 bits, NUM exceeds 65535, or the --unmap option is given then
31       WRITE SAME(16) is sent.  The --10, --16 and --32 options  are  mutually
32       exclusive.
33
34       SBC-3  revision 35d introduced a "no data-out buffer" (NDOB) bit which,
35       if set, bypasses the requirement to send a single block of data to  the
36       DEVICE together with the command. Only WRITE SAME (16 and 32 byte) sup‐
37       port the NDOB bit. If given, a user  block  of  zeros  is  assumed;  if
38       required, protection information of 0xffs is assumed.
39
40       In  SBC-3 revision 26 the UNMAP and ANCHOR bits were added to the WRITE
41       SAME (10) command. Since the UNMAP bit has been in WRITE SAME (16)  and
42       WRITE  SAME  (32)  since  SBC-3 revision 18, the lower of the two (i.e.
43       WRITE SAME (16)) is the default when the --unmap option is  given.   To
44       send WRITE SAME (10) use the --10 option.
45
46       Take  care:  The WRITE SAME(10, 16 and 32) commands may interpret a NUM
47       of zero as write to the end of DEVICE. This utility defaults NUM to 1 .
48       The  WRITE SAME commands have no IMMED bit so if NUM is large (or zero)
49       then an invocation of this utility could take a long time,  potentially
50       as  long as a FORMAT UNIT command. In such situations the command time‐
51       out value TO may need to be increased from its default value of 60 sec‐
52       onds.  In SBC-3 revision 26 the WSNZ (write same no zero) bit was added
53       to the Block Limits VPD page [0xB0]. If set  the  WRITE  SAME  commands
54       will not accept a NUM of zero. The same SBC-3 revision added the "Maxi‐
55       mum Write Same Length" field to the Block Limits VPD page.
56
57       The Logical Block Provisioning VPD page [0xB2] contains  the  LBWS  and
58       LBW10 bits. If LBWS is set then WRITE SAME (16) supports the UNMAP bit.
59       If LBWS10 is set then WRITE SAME (10) supports the UNMAP bit. If either
60       LBWS  or  LBWS10 is set and the WRITE SAME (32) is supported then WRITE
61       SAME (32) supports the UNMAP bit. This is as of SBC-3 revision 26.
62
63       As a precaution against an  accidental  'sg_write_same  /dev/sda'  (for
64       example)  overwriting LBA 0 on /dev/sda with zeros, at least one of the
65       --in=IF, --lba=LBA or --num=NUM options must be given.  Obviously  this
66       utility can destroy a lot of user data so check the options carefully.
67

OPTIONS

69       Arguments to long options are mandatory for short options as well.
70
71       -R, --10
72              send  a  SCSI  WRITE SAME (10) command to DEVICE. The ability to
73              set the --unmap (and --anchor) options to this command was added
74              in SBC-3 revision 26.
75
76       -S, --16
77              send a SCSI WRITE SAME (16) command to DEVICE.
78
79       -T, --32
80              send a SCSI WRITE SAME (32) command to DEVICE.
81
82       -a, --anchor
83              sets the ANCHOR bit in the cdb. Introduced in SBC-3 revision 22.
84              That draft requires the --unmap option to also be specified.
85
86       -g, --grpnum=GN
87              sets the 'Group number' field to GN.  Defaults  to  a  value  of
88              zero.  GN should be a value between 0 and 31.
89
90       -h, --help
91              output the usage message then exit.
92
93       -i, --in=IF
94              read data (binary) from file named IF and use it as the data out
95              buffer for the SCSI WRITE SAME command. The length of  the  data
96              out buffer is --xferlen=LEN or, if that is not given, the length
97              of the IF file. If IF is "-" then stdin is read. If this  option
98              is not given then 0x00 bytes are used as fill with the length of
99              the data out buffer obtained from --xferlen=LEN  or  by  calling
100              READ  CAPACITY(16  or 10).  If the response to READ CAPACITY(16)
101              has the PROT_EN bit set then data out buffer  size  is  modified
102              accordingly with the last 8 bytes set to 0xff.
103
104       -l, --lba=LBA
105              where  LBA  is the logical block address to start the WRITE SAME
106              command.  Defaults to lba 0 which is a dangerous block to  over‐
107              write  on a disk that is in use. Assumed to be in decimal unless
108              prefixed with '0x' or has a trailing 'h'.
109
110       -L, --lbdata
111              sets the LBDATA bit in the WRITE SAME cdb.  This  bit  was  made
112              obsolete in sbc3r32 in September 2012.
113
114       -N, --ndob
115              sets  the  NDOB bit in the WRITE SAME (16 and 32 byte) commands.
116              Default is to clear this bit. When this  option  is  given  then
117              --in=IF  is  not  allowed and --xferlen=LEN can only be given if
118              LEN is 0 .
119
120       -n, --num=NUM
121              where NUM is the number of blocks, starting at LBA, to write the
122              data  out  buffer  to. The default value for NUM is 1. The value
123              corresponds to the 'Number of logical blocks' field in the WRITE
124              SAME cdb.
125              Note  that  a  value of 0 in NUM may be interpreted as write the
126              data out buffer on every block starting at LBA to the end of the
127              DEVICE.   If  the WSNZ bit (introduced in sbc3r26, January 2011)
128              in the Block Limits VPD page is set then the value of 0 is  dis‐
129              allowed, yielding an Invalid request sense key.
130
131       -P, --pbdata
132              sets  the  PBDATA  bit  in the WRITE SAME cdb. This bit was made
133              obsolete in sbc3r32 in September 2012.
134
135       -t, --timeout=TO
136              where TO is the command timeout value in  seconds.  The  default
137              value is 60 seconds. If NUM is large (or zero) a WRITE SAME com‐
138              mand may require considerably more time than 60 seconds to  com‐
139              plete.
140
141       -U, --unmap
142              sets  the  UNMAP  bit  in the WRITE SAME(10, 16 and 32) cdb. See
143              UNMAP section below.
144
145       -v, --verbose
146              increase the degree of verbosity (debug messages).
147
148       -V, --version
149              output version string then exit.
150
151       -w, --wrprotect=WPR
152              sets the "Write protect" field in the WRITE SAME cdb to WPR. The
153              default  value  is  zero. WPR should be a value between 0 and 7.
154              When WPR is 1 or greater, and the disk's protection type is 1 or
155              greater,  then  8  extra  bytes  of  protection  information are
156              expected or generated (to place in the command's data  out  buf‐
157              fer).
158
159       -x, --xferlen=LEN
160              where  LEN is the data out buffer length. Defaults to the length
161              of the IF file or, if that is not given, then  the  READ  CAPAC‐
162              ITY(16  or 10) command is used to find the 'Logical block length
163              in bytes'. That figure may  be  increased  by  8  bytes  if  the
164              DEVICE's protection type is 1 or greater and the WRPROTECT field
165              (see --wrprotect=WPR) is 1 or greater. If both this  option  and
166              the  IF  option  are  given and LEN exceeds the length of the IF
167              file then LEN is the data out buffer length with zeros  used  as
168              pad bytes.
169

UNMAP

171       Logical  block  provisioning is a new term introduced in SBC-3 revision
172       25 for the ability to mark blocks as unused. For large storage  arrays,
173       it  is  a way to provision less physical storage than the READ CAPACITY
174       command reports is  available,  potentially  allocating  more  physical
175       storage  when  WRITE  commands  require  it. For flash memory (e.g. SSD
176       drives) it is a way of potentially saving  power  (and  perhaps  access
177       time) when it is known large sections (or almost all) of the flash mem‐
178       ory is not in use. SSDs need wear levelling algorithms to have  accept‐
179       able  endurance  and  typically  over provision to simplify those algo‐
180       rithms; hence they typically contain more physical flash  storage  than
181       their logical size would dictate.
182
183       Support  for  logical  block provisioning is indicated by the LBPME bit
184       being set in the READ CAPACITY(16) command response (see the sg_readcap
185       utility).   That  implies  at  least one of the UNMAP or WRITE SAME(16)
186       commands is implemented. If the UNMAP command is implemented  then  the
187       "Maximum  unmap  LBA  count" and "Maximum unmap block descriptor count"
188       fields in the Block Limits VPD page should both be greater  than  zero.
189       The  READ CAPACITY(16) command response also contains a LBPRZ bit which
190       if set means that if unmapped  blocks  are  read  then  zeros  will  be
191       returned  for  the  data (and if protection information is active, 0xff
192       bytes are returned for that). In SBC-3 revision 27 the same  LBPRZ  bit
193       was added to the Logical Block Provisioning VPD page.
194
195       In SBC-3 revision 25 the LBPU and ANC_SUP bits where added to the Logi‐
196       cal Block Provisioning VPD page. When LBPU is set it indicates that the
197       device  supports the UNMAP command (see the sg_unmap utility). When the
198       ANC_SUP bit is set it indicates the device supports anchored LBAs.
199
200       When the UNMAP bit is set in the cdb then the data out buffer  is  also
201       sent.   Additionally the data section of that data out buffer should be
202       full of 0x0 bytes while the data protection block, 8 bytes at  the  end
203       if  present,  should  be set to 0xff bytes. If these conditions are not
204       met and the LBPRZ bit is set then the UNMAP bit is ignored and the data
205       out  buffer  is  written to the DEVICE as if the UNMAP bit was zero. In
206       the absence of the --in=IF option, this utility will  attempt  build  a
207       data  out  buffer  that meets the requirements for the UNMAP bit in the
208       cdb to be acted on by the DEVICE.
209
210       Logical blocks may also be unmapped by the SCSI UNMAP and  FORMAT  UNIT
211       commands (see the sg_unmap and sg_format utilities).
212
213       The  unmap  capability  in  SCSI is closely related to the ATA DATA SET
214       MANAGEMENT command with the "Trim" bit set. That  ATA  trim  capability
215       does  not  interact  well  with SATA command queueing known as NCQ. T13
216       have introduced a new command called the SFQ DATA SET  MANAGEMENT  com‐
217       mand also with a the "Trim" bit to address that problem. The SCSI WRITE
218       SAME with the UNMAP bit set and the UNMAP  commands  do  not  have  any
219       problems with SCSI queueing.
220

NOTES

222       Various  numeric  arguments  (e.g. LBA) may include multiplicative suf‐
223       fixes or be given in hexadecimal. See the "NUMERIC  ARGUMENTS"  section
224       in the sg3_utils(8) man page.
225
226       In  Linux,  prior  to  lk 3.17, the sg driver did not support cdb sizes
227       greater than 16 bytes. Hence a device node like /dev/sg1 which is asso‐
228       ciated  with  the  sg  driver  would fail with this utility if the --32
229       option was given (or implied by other options).  The  bsg  driver  with
230       device  nodes like /dev/bsg/6:0:0:1 does support cdb sizes greater than
231       16 bytes since its introduction in lk 2.6.28 .
232

EXIT STATUS

234       The exit status of sg_write_same is 0 when it is successful.  Otherwise
235       see the sg3_utils(8) man page.
236

EXAMPLES

238       One  simple  usage  is  to  write blocks of zero from (and including) a
239       given LBA:
240
241         sg_write_same --lba=0x1234 --num=63 /dev/sdc
242
243       Since --xferlen=LEN has not been given, then this utility will call the
244       READ CAPACITY command on /dev/sdc to determine the number of bytes in a
245       logical block.  Let us assume that is 512 bytes. Since --in=IF  is  not
246       given  a  block  of zeros is assumed. So 63 blocks of zeros (each block
247       containing 512 bytes) will be written from (and including) LBA 0x1234 .
248       Note that only one block of zeros is passed to the SCSI WRITE SAME com‐
249       mand in the data out buffer (as required by SBC-3).
250
251       A similar example follows but in this case the  blocks  are  "unmapped"
252       ("trimmed" in ATA speak) rather than zeroed:
253
254         sg_write_same --unmap -L 0x1234 -n 63 /dev/sdc
255
256       Note  that  if  the  LBPRZ bit in the READ CAPACITY(16) response is set
257       (i.e.  LPPRZ is an acronym for logical block provisioning  read  zeros)
258       then these two examples do the same thing, at least seen from the point
259       of view of subsequent reads.
260
261       This utility can also be used to write protection information  (PI)  on
262       disks formatted with a protection type greater than zero. PI is 8 bytes
263       of extra data appended to the user data of a logical block:  the  first
264       two bytes are a CRC (the "guard"), the next two bytes are the "applica‐
265       tion tag" and the last four bytes are the "reference tag". With protec‐
266       tion  types  1  and  2  if the application tag is 0xffff then the guard
267       should not be checked (against the user data).
268
269       In this example we assume the logical block size (of the user data)  is
270       512 bytes and the disk has been formatted with protection type 1. Since
271       we are going to modify LBA 2468 then we take a copy of it first:
272
273         dd if=/dev/sdb skip=2468 bs=512 of=2468.bin count=1
274
275       The following command line sets the user data to zeros and the PI to  8
276       0xFF bytes on LBA 2468:
277
278         sg_write_same --lba=2468 /dev/sdb
279
280       Reading  back  that  block should be successful because the application
281       tag is 0xffff which suppresses the guard (CRC) check (which would  oth‐
282       erwise be wrong):
283
284         dd if=/dev/sdb skip=2468 bs=512 of=/dev/null count=1
285
286       Now  an  attempt is made to create a binary file with zeros in the user
287       data, 0x0000 in the application tag and 0xff bytes in the other two  PI
288       fields.  It  is awkward to create 0xff bytes in a file (in Unix) as the
289       "tr" command below shows:
290
291         dd if=/dev/zero bs=1 count=512 of=ud.bin
292         tr "\000" "\377" < /dev/zero | dd bs=1 of=ff_s.bin count=8
293         cat ud.bin ff_s.bin > lb.bin
294         dd if=/dev/zero bs=1 count=2 seek=514 conv=notrunc of=lb.bin
295
296       The resulting file can be viewed with 'hexdump -C  lb.bin'  and  should
297       contain 520 bytes. Now that file can be written to LBA 2468 as follows:
298
299         sg_write_same --lba=2468 wrprotect=3 --in=lb.bin /dev/sdb
300
301       Note  the  --wrprotect=3  rather than being set to 1, since we want the
302       WRITE SAME command to succeed even though the PI data now indicates the
303       user  data  is  corrupted.  When an attempt is made to read the LBA, an
304       error should occur:
305
306         dd if=/dev/sdb skip=2468 bs=512 of=/dev/null count=1
307
308       dd errors are not very expressive, if dmesg is checked there should  be
309       a  line  something  like  this: "[sdb]  Add. Sense: Logical block guard
310       check failed". The block can be corrected  by  doing  a  "sg_write_same
311       --lba=1234  /dev/sdb"  again or restoring the original contents of that
312       LBA:
313
314         dd if=2468.bin bs=512 seek=2468 of=/dev/sdb conv=notrunc count=1
315
316       Hopefully the dd command would never try to truncate  the  output  file
317       when it is a block device.
318

AUTHORS

320       Written by Douglas Gilbert.
321

REPORTING BUGS

323       Report bugs to <dgilbert at interlog dot com>.
324
326       Copyright © 2009-2015 Douglas Gilbert
327       This  software is distributed under a FreeBSD license. There is NO war‐
328       ranty; not even for MERCHANTABILITY or FITNESS FOR  A  PARTICULAR  PUR‐
329       POSE.
330

SEE ALSO

332       sg_format,sg_get_lba_status,sg_readcap,sg_vpd,sg_unmap(sg3_utils)
333
334
335
336sg3_utils-1.41                   February 2015                SG_WRITE_SAME(8)
Impressum