block: Move qemu_fcntl_addfl() into osdep.c

Move file-posix's helper to add a flag (or a set of flags) to an FD's
existing set of flags into osdep.c for other places to use.

Suggested-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
Message-ID: <20260309150856.26800-16-hreitz@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Hanna Czenczek
2026-03-09 16:08:46 +01:00
committed by Kevin Wolf
parent 4adf36d940
commit 247fa896d5
3 changed files with 20 additions and 16 deletions

View File

@@ -1056,21 +1056,6 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
return ret;
}
/* Sets a specific flag */
static int fcntl_setfl(int fd, int flag)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (flags == -1) {
return -errno;
}
if (fcntl(fd, F_SETFL, flags | flag) == -1) {
return -errno;
}
return 0;
}
static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
int *open_flags, uint64_t perm, Error **errp)
{
@@ -1109,7 +1094,7 @@ static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
/* dup the original fd */
fd = qemu_dup(s->fd);
if (fd >= 0) {
ret = fcntl_setfl(fd, *open_flags);
ret = qemu_fcntl_addfl(fd, *open_flags);
if (ret) {
qemu_close(fd);
fd = -1;

View File

@@ -633,6 +633,7 @@ int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
bool qemu_has_ofd_lock(void);
int qemu_fcntl_addfl(int fd, int flag);
#endif
bool qemu_has_direct_io(void);

View File

@@ -280,6 +280,24 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive)
return fl.l_type == F_UNLCK ? 0 : -EAGAIN;
}
}
/**
* Set the given flag(s) (fcntl GETFL/SETFL) on the given FD, while retaining
* other flags.
*/
int qemu_fcntl_addfl(int fd, int flag)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (flags == -1) {
return -errno;
}
if (fcntl(fd, F_SETFL, flags | flag) == -1) {
return -errno;
}
return 0;
}
#endif
bool qemu_has_direct_io(void)