Re: zero filling a storage device (was: dd and mbr)

From: Daniel Feenberg <feenberg_at_nber.org>
Date: Fri, 14 Jan 2022 12:52:32 UTC

On Thu, 13 Jan 2022, Kevin P. Neal wrote:

> On Fri, Jan 14, 2022 at 01:30:02PM +1100, Greg 'groggy' Lehey wrote:
>> On Thursday, 13 January 2022 at 17:26:12 -0800, David Christensen wrote:
>>> On 1/11/22 2:02 AM, Roderick wrote:
>>>
>>>
>>> On 1/13/22 12:32 AM, Roderick wrote:
>>
>> Yes, that's fine.  If the block size is larger than the maximum that
>> the disk can handle, the system divides it into smaller transfers.
>>
>>> Suppose I have a storage device with M blocks of size B bytes each.
>>> Some of the blocks contain non-zero bytes and other blocks contain only
>>> zero bytes.
>>>
>>> Suppose I have a program with a memory buffer of N blocks filled with
>>> zero bytes, where N < M.
>>>
>>> If the program writes the buffer to the storage device starting at
>>> block-aligned location P, where P + N * B <= M * B, will the storage
>>> device write zero bytes to the storage device blocks that already
>>> contain only zero bytes?
>>
>> Yes.  The storage device doesn't know the current contents of the
>> disk.  To do so it would first have to read them, which takes far too
>> long.
>>
>>> Is the answer different for a HDD, an SSD, a USB flash drive, an SD
>>> card, a compact flash card, etc.?
>>
>> No.  It's also not different for other operating systems.
>
> Are we certain that an SSD won't at least track that there is nothing
> written to a logical block and therefore it must be all zeros? I'm not
> 100% that an SSD will always keep a logical block assigned to a physical
> block. And I'm not 100% certain that an SSD won't notice that all zeros
> are being written to a block and just optimize out the write.
>
> -- 
> Kevin P. Neal                                http://www.pobox.com/~kpn/
>
> "What is mathematics? The age-old answer is, of course, that mathematics
> is what mathematicians do." - Donald Knuth
>

Doesn't the filesystem code handle sparse files on its own? This man page:

    https://www.freebsd.org/cgi/man.cgi?du

strongly implies that it does. A block of all zero bytes shouldn't occupy 
hardly any disk space at all. See, for instance:

    https://en.wikipedia.org/wiki/Sparse_file

Of course, if the purpose of the write is to zero out blocks for security, 
then writing a sparse file won't do that.

Daniel Feenberg