public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* PROBLEM: Failed open() with O_DIRECT creates file
@ 2016-09-07 15:10 Hallvard Breien Furuseth
  2016-09-07 15:15 ` Hallvard Breien Furuseth
  0 siblings, 1 reply; 2+ messages in thread
From: Hallvard Breien Furuseth @ 2016-09-07 15:10 UTC (permalink / raw)
  To: linux-kernel

If the filesystem does not support O_DIRECT, then
open(...O_CREAT|O_DIRECT..) fails but creates the file anyway.

Eric Sandeen@RedHat thought a fix would need a lot of vfs restructuring.
(I reported this in 2013 to RedHat (bug#1008073), but just realized he
was talking about "upstream" so maybe the report didn't get further.)

Linux 3.10.0-327.28.3.el7.x86_64, RHEL 7.2 (Maipo).
The bug existed at least since 2.6.18-348.6.1.el5.

To reproduce, run this as:

  ./a.out /dev/test-direct.dat

(Originally I wrote to /dev/shm/, but tmpfs now accepts O_DIRECT.)

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    char *fname = argc > 1 ? argv[1] : "test-direct.dat";
    int fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_DIRECT, 0666);
    int e = fd < 0 ? errno : 0;

    if (fd >= 0)
        puts("open() succeeded");
    else
        perror("open() error");
    if ((access(fname, F_OK) == 0) != (fd >= 0 || e == EEXIST))
        puts(fd < 0 ? "Created file anyway!" : "File disappeared!");

    if (e != EEXIST)
        unlink(fname);
    return 0;
}

-- 
Hallvard

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-09-07 15:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-07 15:10 PROBLEM: Failed open() with O_DIRECT creates file Hallvard Breien Furuseth
2016-09-07 15:15 ` Hallvard Breien Furuseth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox