public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: Christoph Hellwig <hch@infradead.org>
Cc: Vyacheslav Kovalevsky <slava.kovalevskiy.2014@gmail.com>,
	tytso@mit.edu, adilger.kernel@dilger.ca,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: Writing more than 4096 bytes with O_SYNC flag does not persist all previously written data if system crashes
Date: Tue, 24 Feb 2026 14:23:39 -0800	[thread overview]
Message-ID: <20260224222339.GA13823@frogsfrogsfrogs> (raw)
In-Reply-To: <aZ2599dwNuqPQgzB@infradead.org>

On Tue, Feb 24, 2026 at 06:47:19AM -0800, Christoph Hellwig wrote:
> A lot of folks have already explained the O_SYNC semantics correctly,
> but I have another major question about your test case.
> 
> On Wed, Feb 18, 2026 at 04:29:30PM +0300, Vyacheslav Kovalevsky wrote:
> > Detailed description
> > ====================
> > 
> > Hello, there seems to be an issue with ext4 crash behavior:
> > 
> > 1. Create and sync a new file.
> > 2. Open the file and write some data (must be more than 4096 bytes).
> > 3. Close the file.
> > 4. Open the file with O_SYNC flag and write some data.
> > 
> > After system crash the file will have the wrong size and some previously
> > written data will be lost.
> 
> The wrong size here seems incorrect.  Even if the old data written
> through the non-O_SYNC fd wasn't written out I absolutely can't see how
> the file would have an incorrect size here.  Can you please share your
> test case?

He did, way at the beginning: open a file, write 5000 bytes, close it,
open again with O_SYNC, write 300 bytes, close it, force-reboot, and
watch the file come back up with only 4096 bytes written.

I /think/ that's because generic_write_sync only flushes the range that
was dirtied by the write() call, so only the first 4k gets written back
to disk.  xfs and ext4 exhibit this behavior; vfat and btrfs persist all
50000 bytes.

--D

#!/bin/bash -x

# Let's see if a small O_SYNC write flushes the rest of the file?

dev="${1:-/dev/sda}"
mnt="${2:-/mnt}"
fstyp="${3:-xfs}"

devsz=$(blockdev --getsz $dev)
test -z "$devsz" && exit 1

umount $dev $mnt

dmsetup remove crap
dmsetup create crap --table "0 $devsz linear $dev 0"
dmdev=/dev/mapper/crap
test -b "$dmdev" || exit 1

rmmod $fstyp

wipefs -a $dmdev
mkfs.$fstyp $dmdev
mount $dmdev $mnt

xfs_io -f -c 'pwrite -S 0x58 0 50000' $mnt/a
xfs_io -s -c 'pwrite -S 0x42 10 300' $mnt/a

dmsetup suspend crap --noflush
dmsetup load crap --table "0 $devsz error"
dmsetup resume crap
dmsetup table
umount $mnt

dmsetup suspend crap
dmsetup load crap --table "0 $devsz linear $dev 0"
dmsetup resume crap

mount $dmdev $mnt
od -tx1 -Ad -c $mnt/a
stat $mnt/a
umount $mnt
dmsetup remove crap

  reply	other threads:[~2026-02-24 22:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-18 13:29 Writing more than 4096 bytes with O_SYNC flag does not persist all previously written data if system crashes Vyacheslav Kovalevsky
2026-02-18 21:55 ` Andreas Dilger
2026-02-19 13:32   ` Theodore Tso
2026-02-23 12:46     ` Alejandro Colomar
2026-02-23 19:32       ` Theodore Tso
2026-02-24  1:21         ` Andreas Dilger
2026-03-03 13:19         ` Alejandro Colomar
2026-02-24 14:47 ` Christoph Hellwig
2026-02-24 22:23   ` Darrick J. Wong [this message]
2026-02-25 14:20     ` Christoph Hellwig

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=20260224222339.GA13823@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=adilger.kernel@dilger.ca \
    --cc=hch@infradead.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=slava.kovalevskiy.2014@gmail.com \
    --cc=tytso@mit.edu \
    /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