Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Jan 2022 07:52:32 -0500 (EST)
From:      Daniel Feenberg <feenberg@nber.org>
To:        "Kevin P. Neal" <kpn@neutralgood.org>
Cc:        "Greg 'groggy' Lehey" <grog@freebsd.org>, David Christensen <dpchrist@holgerdanske.com>, freebsd-questions@freebsd.org
Subject:   Re:  zero filling a storage device (was: dd and mbr)
Message-ID:  <a8c8e1b9-832d-6855-654-60cd1e5b38b@nber.org>
In-Reply-To: <YeDryNdYe1S20wd2@neutralgood.org>
References:  <77680665-7ddb-23c5-e866-05d112339b60@holgerdanske.com> <20220114023002.GP61872@eureka.lemis.com> <YeDryNdYe1S20wd2@neutralgood.org>

next in thread | previous in thread | raw e-mail | index | archive | help


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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?a8c8e1b9-832d-6855-654-60cd1e5b38b>