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

UNMAP

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

NOTES

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

EXIT STATUS

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

EXAMPLES

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

AUTHORS

322       Written by Douglas Gilbert.
323

REPORTING BUGS

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

SEE ALSO

334       sg_format,sg_get_lba_status,sg_readcap,sg_vpd,sg_unmap(sg3_utils)
335
336
337
338sg3_utils-1.43                   November 2017                SG_WRITE_SAME(8)
Impressum