linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Warren <swarren@wwwdotorg.org>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, Simon Glass <sjg@chromium.org>
Subject: dd, close(), sync, and the Linux disk cache
Date: Wed, 12 Feb 2020 17:02:43 -0700	[thread overview]
Message-ID: <b7ad1223-4224-da46-4c48-50427360f31c@wwwdotorg.org> (raw)

Jens et. al.,

If I dd to an SD card (e.g. via a USB SD card reader), is it required to 
run sync afterward in order to guarantee that all written data is 
written to the SD card? I'm running dd with a simple command-line like 
"dd if=file.img of=/dev/sdc".

In practice on my system, strace shows me that dd completes its write 
operations relatively quickly, since many are buffered in the kernel. 
However, dd's call to close() takes a very long time. I believe this 
delay is due to the kernel flushing all the buffers to disk, and so a 
sync is not required. I found code in the kernel that does appear to do 
this:

https://elixir.bootlin.com/linux/v5.5/source/fs/block_dev.c#L2145
dev_blk_fops.release = blkdev_close
blkdev_close calls blkdev_put
blkdev_put calls __blkdev_put
__blkdev_put calls sync_blockdev if the device open count is 0
sync_blockdev calls __sync_blockdev with the wait flag set
__sync_blockdev calls filemap_write_and_wait

However, Simon finds that when he dd's to an SD card, he needs to 
execute sync afterward to avoid a corrupted SD card. I'm trying to 
understand what the difference is, and whether sync is the correct 
solution for this issue, or just something that happens to take some 
time (e.g. to flush buffers to the sytem's main hard disk) during which 
some other issue is typically resolved.

For reference, I've seen the "no sync required" behaviour on my laptop 
with a built-in PCIe SD reader which shows up as /dev/mmcblkN and on my 
desktop using a USB SD reader which shows up as /dev/sdN. Simon is I 
believe using a USB SD card reader, which is actually a mux device that 
allows switching the SD card between hosts, with the host->host switch 
happening immediately after the dd (or dd+sync) completes.

             reply	other threads:[~2020-02-13  0:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-13  0:02 Stephen Warren [this message]
2020-02-19 16:32 ` dd, close(), sync, and the Linux disk cache Christoph Hellwig
2020-02-19 17:01   ` Stephen Warren
2020-02-19 17:13     ` Alberto Faria

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b7ad1223-4224-da46-4c48-50427360f31c@wwwdotorg.org \
    --to=swarren@wwwdotorg.org \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=sjg@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).