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>
Hanna Czenczek committed
Mar 9, 2026 at 16:08 UTC
247fa896d510d6cc22d697e7c4979d8a3727de52
3 files changed
+20
-16
block/file-posix.c
+1
-16
@@ -1056,21 +1056,6 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
1056
return ret;
1057
}
1058
1059
-/* Sets a specific flag */
1060
-static int fcntl_setfl(int fd, int flag)
1061
-{
1062
- int flags;
1063
-
1064
- flags = fcntl(fd, F_GETFL);
1065
- if (flags == -1) {
1066
- return -errno;
1067
- }
1068
- if (fcntl(fd, F_SETFL, flags | flag) == -1) {
1069
- return -errno;
1070
- }
1071
- return 0;
1072
-}
1073
-
1059
static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
1060
int *open_flags, uint64_t perm, Error **errp)
1061
{
@@ -1109,7 +1094,7 @@ static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
1094
/* dup the original fd */
1095
fd = qemu_dup(s->fd);
1096
if (fd >= 0) {
1112
- ret = fcntl_setfl(fd, *open_flags);
1097
+ ret = qemu_fcntl_addfl(fd, *open_flags);
1098
if (ret) {
1099
qemu_close(fd);
1100
fd = -1;
include/qemu/osdep.h
+1
@@ -633,6 +633,7 @@ int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
633
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
634
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
635
bool qemu_has_ofd_lock(void);
636
+int qemu_fcntl_addfl(int fd, int flag);
637
#endif
638
639
bool qemu_has_direct_io(void);
util/osdep.c
+18
@@ -280,6 +280,24 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive)
280
return fl.l_type == F_UNLCK ? 0 : -EAGAIN;
281
}
282
}
283
+
284
+/**
285
+ * Set the given flag(s) (fcntl GETFL/SETFL) on the given FD, while retaining
286
+ * other flags.
287
+ */
288
+int qemu_fcntl_addfl(int fd, int flag)
289
+{
290
+ int flags;
291
+
292
+ flags = fcntl(fd, F_GETFL);
293
+ if (flags == -1) {
294
+ return -errno;
295
+ }
296
+ if (fcntl(fd, F_SETFL, flags | flag) == -1) {
297
+ return -errno;
298
+ }
299
+ return 0;
300
+}
301
#endif
302
303
bool qemu_has_direct_io(void)