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 <berra...@redhat.com> 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
signature.asc
Description: OpenPGP digital signature