Skip to contentSkip to conversation

file-posix: Use OFD lock only if the filesystem supports the lock

PATCH New Masayoshi Mizuma 1 participant 1 message Nov 18, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 file-posix: Use OFD lock only if the filesystem supports the lock

From: Masayoshi Mizuma <[email protected]>

locking=auto doesn't work if the filesystem doesn't support OFD lock. In that situation, following error happens:

qemu-system-x86_64: -blockdev driver=qcow2,node-name=disk,file.driver=file,file.filename=/mnt/guest.qcow2,file.locking=auto: Failed to lock byte 100

qemu_probe_lock_ops() judges whether qemu can use OFD lock or not with doing fcntl(F_OFD_GETLK) to /dev/null. So the error happens if /dev/null supports OFD lock, but the filesystem doesn't support the lock.

Lock the actual file, not /dev/null, using F_OFD_SETLK and if that fails, then fallback to F_SETLK.

Fixes ca749954b0 ("osdep: Add runtime OFD lock detection")
---
 block/file-posix.c         |  56 ++++++++---------
 include/qemu/osdep.h       |   2 +-
 tests/test-image-locking.c |  13 +++-
 util/osdep.c               | 125 +++++++++++++++++++++----------------
 4 files changed, 111 insertions(+), 85 deletions(-)
block/file-posix.c +28 -28
index d5fd1dbcd2..ff8a2804c5 100644
@@ -584,34 +584,6 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     s->use_linux_io_uring = (aio == BLOCKDEV_AIO_OPTIONS_IO_URING);
 #endif
 
-    locking = qapi_enum_parse(&OnOffAuto_lookup,
-                              qemu_opt_get(opts, "locking"),
-                              ON_OFF_AUTO_AUTO, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
-        ret = -EINVAL;
-        goto fail;
-    }
-    switch (locking) {
-    case ON_OFF_AUTO_ON:
-        s->use_lock = true;
-        if (!qemu_has_ofd_lock()) {
-            warn_report("File lock requested but OFD locking syscall is "
-                        "unavailable, falling back to POSIX file locks");
-            error_printf("Due to the implementation, locks can be lost "
-                         "unexpectedly.\n");
-        }
-        break;
-    case ON_OFF_AUTO_OFF:
-        s->use_lock = false;
-        break;
-    case ON_OFF_AUTO_AUTO:
-        s->use_lock = qemu_has_ofd_lock();
-        break;
-    default:
-        abort();
-    }
-
     str = qemu_opt_get(opts, "pr-manager");
     if (str) {
         s->pr_mgr = pr_manager_lookup(str, &local_err);
@@ -641,6 +613,34 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     }
     s->fd = fd;
 
+    locking = qapi_enum_parse(&OnOffAuto_lookup,
+                              qemu_opt_get(opts, "locking"),
+                              ON_OFF_AUTO_AUTO, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        ret = -EINVAL;
+        goto fail;
+    }
+    switch (locking) {
+    case ON_OFF_AUTO_ON:
+        s->use_lock = true;
+        if (!qemu_has_ofd_lock(s->fd)) {
+            warn_report("File lock requested but OFD locking syscall is "
+                        "unavailable, falling back to POSIX file locks");
+            error_printf("Due to the implementation, locks can be lost "
+                         "unexpectedly.\n");
+        }
+        break;
+    case ON_OFF_AUTO_OFF:
+        s->use_lock = false;
+        break;
+    case ON_OFF_AUTO_AUTO:
+        s->use_lock = qemu_has_ofd_lock(s->fd);
+        break;
+    default:
+        abort();
+    }
+
     /* Check s->open_flags rather than bdrv_flags due to auto-read-only */
     if (s->open_flags & O_RDWR) {
         ret = check_hdev_writable(s->fd);
include/qemu/osdep.h +1 -1
index f9ec8c84e9..222138a81a 100644
@@ -512,7 +512,7 @@ int qemu_dup(int fd);
 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);
+bool qemu_has_ofd_lock(int fd);
 #endif
 
 #if defined(__HAIKU__) && defined(__i386__)
tests/test-image-locking.c +10 -3
index ba057bd66c..7a28d9d043 100644
@@ -144,14 +144,21 @@ static void test_set_perm_abort(void)
 
 int main(int argc, char **argv)
 {
+    int fd;
+
     bdrv_init();
     qemu_init_main_loop(&error_abort);
 
     g_test_init(&argc, &argv, NULL);
 
-    if (qemu_has_ofd_lock()) {
-        g_test_add_func("/image-locking/basic", test_image_locking_basic);
-        g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort);
+    fd = open("/dev/null", O_RDONLY);
+
+    if (fd != -1 && qemu_has_ofd_lock(fd)) {
+        if (qemu_has_ofd_lock(fd)) {
+            g_test_add_func("/image-locking/basic", test_image_locking_basic);
+            g_test_add_func("/image-locking/set-perm-abort",
+                            test_set_perm_abort);
+        }
     }
 
     return g_test_run();
util/osdep.c +72 -53

171 lines not on this page · raw message

Signature · 1 line
2.27.0