Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux: use O_EXCL when opening a file for write #1822

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion engines/nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,14 @@ int fio_nvme_reset_wp(struct thread_data *td, struct fio_file *f,
/* If the file is not yet opened, open it for this function. */
fd = f->fd;
if (fd < 0) {
fd = open(f->file_name, O_RDWR | O_LARGEFILE);
int flags = O_RDWR | O_LARGEFILE;

#ifdef FIO_USE_O_EXCL
if (!td->o.allow_mounted_write)
flags |= O_EXCL;
#endif

fd = open(f->file_name, flags);
if (fd < 0)
return -errno;
}
Expand Down
8 changes: 8 additions & 0 deletions engines/xnvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ static struct xnvme_opts xnvme_opts_from_fioe(struct thread_data *td)

opts.direct = td->o.odirect;

opts.rdonly = opts.wronly = opts.rdwr = 0;
if (td_rw(td))
opts.rdwr = 1;
else if (td_write(td))
opts.wronly = 1;
else
opts.rdonly = 1;

return opts;
}

Expand Down
12 changes: 9 additions & 3 deletions filesetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ int generic_open_file(struct thread_data *td, struct fio_file *f)
int is_std = 0;
int flags = 0;
int from_hash = 0;
int write_flags = O_RDWR;

dprint(FD_FILE, "fd open %s\n", f->file_name);

Expand All @@ -744,10 +745,15 @@ int generic_open_file(struct thread_data *td, struct fio_file *f)
if (f->filetype != FIO_TYPE_FILE)
flags |= FIO_O_NOATIME;

#ifdef FIO_USE_O_EXCL
if (!td->o.allow_mounted_write && (flags & O_CREAT) == 0)
write_flags |= O_EXCL;
#endif

open_again:
if (td_write(td)) {
if (!read_only)
flags |= O_RDWR;
flags |= write_flags;

if (td->o.verify_only) {
flags &= ~O_RDWR;
Expand All @@ -763,7 +769,7 @@ int generic_open_file(struct thread_data *td, struct fio_file *f)
from_hash = file_lookup_open(f, flags);
} else if (td_read(td)) {
if (td_ioengine_flagged(td, FIO_RO_NEEDS_RW_OPEN) && !read_only)
flags |= O_RDWR;
flags |= write_flags;
else
flags |= O_RDONLY;

Expand All @@ -774,7 +780,7 @@ int generic_open_file(struct thread_data *td, struct fio_file *f)
} else if (td_trim(td)) {
assert(!td_rw(td)); /* should have matched above */
if (!read_only)
flags |= O_RDWR;
flags |= write_flags;
from_hash = file_lookup_open(f, flags);
}

Expand Down
2 changes: 2 additions & 0 deletions os/os-linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,6 @@ static inline bool os_cpu_has(cpu_features feature)
return have_feature;
}

#define FIO_USE_O_EXCL

#endif
9 changes: 8 additions & 1 deletion oslib/linux-blkzoned.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,14 @@ int blkzoned_reset_wp(struct thread_data *td, struct fio_file *f,
/* If the file is not yet opened, open it for this function. */
fd = f->fd;
if (fd < 0) {
fd = open(f->file_name, O_RDWR | O_LARGEFILE);
int flags = O_RDWR | O_LARGEFILE;

#ifdef FIO_USE_O_EXCL
if (!td->o.allow_mounted_write)
flags |= O_EXCL;
#endif

fd = open(f->file_name, flags);
if (fd < 0)
return -errno;
}
Expand Down
Loading