diff --git a/util/osdep.c b/util/osdep.c
index 66d01b9160..dee1f076da 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -225,6 +225,20 @@ static void qemu_probe_lock_ops(void)
}
}
+bool qemu_has_file_lock(int fd)
+{
+ int ret;
+ struct flock fl = {
+ .l_whence = SEEK_SET,
+ .l_start = 0,
+ .l_len = 0,
+ .l_type = F_WRLCK,
+ };
+
+ ret = fcntl(fd, F_GETLK, &fl);
I think we need
qemu_probe_lock_ops();
ret = fcntl(fd, fcntl_op_getlk, &fl);
pattern instead, like in qemu_lock_fd_test(). Otherwise, what we check may
differ with what we are going to use.
No, we explicitly do *not* want that. This function is *only*
about checking whether traditional fcntl locks work or not on
this specific file handle.
Hmm, than may be name the function qemu_has_posix_lock(), to stress that fact?
All other qemu*lock*(fd) API functions do rely on fcnt_op_getlk/fcntl_op_setlk
and work with lock type determined by qemu_probe_lock_ops().
Support for OFD locks is a separate check, and its result
applies system wide.
Still, I don't follow, why should we check posix lock, when we are
going to use ofd locks. What if OFD locks are supported by kernel,
but specific file-system supports posix lock, but not ofd? Than
we'll fail the same way as described in commit message and the
patch doesn't help. Or what I miss?
That's not a scenario that exists. OFD locks are implemented by the
kernel in the generic VFS layer, so apply to all filesystems. The
filesystems merely have to support traditiaonl fcntl locks, and then
they get OFD for free.
IOW, there are two separate questions the code needs answers to
1. Does this specific filesystem support locking
2. Does the operating system support OFD locking
The problem in the commit message is because the original code was asking
question 2 only and geting the correct answer that the OS supports OFD.
The image was stored on a filesystem, however, that does not support fnctl
locks at all and hence locking failed. This failure would happen regardless
of whether OFD or traditional fcntl locks were used.
The problem is solved by also asking the first question before enabling
use of locks.
OK, thanks for explanation. Sorry, I was not attentive to previous versions,
but I remember that something about this was discussed, so probably you explain
this thing not the first time;( Hmm, still, what's wrong with checking fs by
OFD lock trying? It will fail anyway? Or, it will NOT fail, because OFD knows
that there is no locks, and will not ask filesystem driver and we will fail
only later, when try to set the lock? If so, it worth a comment in
file-posix.c.. As it looks like a design flaw of OFD locks.