From: Leo Chen <liangc8367@gmail.com>
To: linux-fsdevel@vger.kernel.org
Subject: Is concurrent file read/write with O_DIRECT flag atomic?
Date: Sun, 12 Nov 2017 09:59:49 -0500 [thread overview]
Message-ID: <CAOG584HKOS4JGug-Lb=sNB__hzWvrrt=p-_qy9+5JzNmBppKwQ@mail.gmail.com> (raw)
Hi,
I apologize if this topic is not proper for this mail list. I asked
the question on other channel, but haven't got answers yet (see
https://stackoverflow.com/questions/47245162/is-concurrent-file-read-write-with-o-direct-flag-atomic).
Basically, I have a non-sparse binary file. A writer process opens the
file using O_DIRECT flag, and it keeps calling pwrite() to update the
first 128KB data of the file. Meanwhile, multiple readers also keeps
calling pread() to read the first 128KB data. The readers open the
file using O_DIRECT flag.
Although I could not find any document saying that O_DIRECT guarantee
atomicity of concurrent read/write, I thought that the readers should
read back consistent data. Since the data to read/write from/to the
file is block aligned, I assume that kernel would just submit a single
scatter-gather command for one pwrite() or one pread(). Per my
knowledge of SCSI/SATA driver, HDD should (correct me if I'm wrong)
process each scatter-gather command atomically. Therefore, I thought
read/write operations in this scenario are atomic.
I wrote a program to verify my thought. Surprisingly, the readers did
occasionally read back mixed data. For example, in the first pwrite(),
the writer writes all 0x11, and in the 2nd pwrite(), it writes all
0x22, and in the 3rd write, it writes all 0x33... Occasionally, a
reader can read back data like "0x11, 0x11, .... 0x11, 0x22, 0x22....
0x22". The data appears to be from two consecutive pwrite() calls. I
checked the offset where the broken starts. The offset seems to be
sector-aligned (512-byte-aligned).
Why could such consistent issue happen? Did I miss anything in my
analysis and theory?
Thanks,
Leo
next reply other threads:[~2017-11-12 14:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-12 14:59 Leo Chen [this message]
2017-11-12 15:09 ` Is concurrent file read/write with O_DIRECT flag atomic? Liang Chen
2017-11-13 1:09 ` Dave Chinner
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='CAOG584HKOS4JGug-Lb=sNB__hzWvrrt=p-_qy9+5JzNmBppKwQ@mail.gmail.com' \
--to=liangc8367@gmail.com \
--cc=linux-fsdevel@vger.kernel.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).