qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v2 2/2] osdep: warn if opening a file O_DIRECT o


From: Eric Blake
Subject: Re: [Qemu-devel] [PATCH v2 2/2] osdep: warn if opening a file O_DIRECT on tmpfs fails
Date: Wed, 21 Aug 2013 13:04:01 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130805 Thunderbird/17.0.8

On 08/21/2013 07:16 AM, Stefan Hajnoczi wrote:
> Print a warning when opening a file O_DIRECT on tmpfs fails.  This saves
> users a lot of time trying to figure out the EINVAL error.
> 
> Daniel P. Berrange <address@hidden> suggested opening the file
> without O_DIRECT as a portable way to check whether the file system
> supports O_DIRECT.  That gets messy when flags contains O_CREAT since
> we'd create a file but return an error - or a race condition if we try
> to unlink the file.  It's simpler to check the file system type.

An alternative to this is as follows:

if the user passes in O_CREAT|O_DIRECT (but not O_EXCL), we _first_ try
O_CREAT|O_DIRECT|O_EXCL.  If that succeeds, O_DIRECT works and we
created the file, nothing further to do.  If it fails with EINVAL,
O_DIRECT is unsupported.  If it fails with EEXIST, the file already
exists, and we retry open(O_DIRECT) (no O_CREAT, because the file
already exists), and get our answer that way.  (It would be possible to
audit whether the kernel favors EINVAL vs. EEXIST in the case where both
errors are possible [ie. the file exists and O_DIRECT is unsupported],
to slightly optimize this code; but I'm not sure it's worth it).  If the
user passes in O_DIRECT but not O_CREAT, then you jump straight to the
middle case (since they didn't want creation in the first place).

That way, you can detect O_DIRECT failure on more than just tmpfs, and
without needing an #if __linux__, and with no nasty races in unlinking a
just-created file.  I'm starting to think Dan's suggestion has merit
after all, if done correctly.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]