b38b0f
From 47a21881507f1bba1cf2fbabb0f8efce57ee7fb9 Mon Sep 17 00:00:00 2001
b38b0f
From: Max Reitz <mreitz@redhat.com>
b38b0f
Date: Wed, 3 Apr 2019 17:13:11 +0100
b38b0f
Subject: [PATCH 04/11] file-posix: Drop s->lock_fd
b38b0f
b38b0f
RH-Author: Max Reitz <mreitz@redhat.com>
b38b0f
Message-id: <20190403171315.20841-5-mreitz@redhat.com>
b38b0f
Patchwork-id: 85403
b38b0f
O-Subject: [RHEL-8.1 qemu-kvm PATCH 4/8] file-posix: Drop s->lock_fd
b38b0f
Bugzilla: 1694148
b38b0f
RH-Acked-by: John Snow <jsnow@redhat.com>
b38b0f
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
b38b0f
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
b38b0f
b38b0f
From: Fam Zheng <famz@redhat.com>
b38b0f
b38b0f
The lock_fd field is not strictly necessary because transferring locked
b38b0f
bytes from old fd to the new one shouldn't fail anyway. This spares the
b38b0f
user one fd per image.
b38b0f
b38b0f
Signed-off-by: Fam Zheng <famz@redhat.com>
b38b0f
Reviewed-by: Max Reitz <mreitz@redhat.com>
b38b0f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
b38b0f
(cherry picked from commit f2e3af29b70624659a50903bd075e1663b64c9da)
b38b0f
Signed-off-by: Max Reitz <mreitz@redhat.com>
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 block/file-posix.c | 37 +++++++++++++------------------------
b38b0f
 1 file changed, 13 insertions(+), 24 deletions(-)
b38b0f
b38b0f
diff --git a/block/file-posix.c b/block/file-posix.c
b38b0f
index 2a05193..97e7ff2 100644
b38b0f
--- a/block/file-posix.c
b38b0f
+++ b/block/file-posix.c
b38b0f
@@ -142,7 +142,6 @@ do { \
b38b0f
 
b38b0f
 typedef struct BDRVRawState {
b38b0f
     int fd;
b38b0f
-    int lock_fd;
b38b0f
     bool use_lock;
b38b0f
     int type;
b38b0f
     int open_flags;
b38b0f
@@ -153,7 +152,7 @@ typedef struct BDRVRawState {
b38b0f
     uint64_t shared_perm;
b38b0f
 
b38b0f
     /* The perms bits whose corresponding bytes are already locked in
b38b0f
-     * s->lock_fd. */
b38b0f
+     * s->fd. */
b38b0f
     uint64_t locked_perm;
b38b0f
     uint64_t locked_shared_perm;
b38b0f
 
b38b0f
@@ -545,18 +544,6 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
b38b0f
     }
b38b0f
     s->fd = fd;
b38b0f
 
b38b0f
-    s->lock_fd = -1;
b38b0f
-    if (s->use_lock) {
b38b0f
-        fd = qemu_open(filename, s->open_flags);
b38b0f
-        if (fd < 0) {
b38b0f
-            ret = -errno;
b38b0f
-            error_setg_errno(errp, errno, "Could not open '%s' for locking",
b38b0f
-                             filename);
b38b0f
-            qemu_close(s->fd);
b38b0f
-            goto fail;
b38b0f
-        }
b38b0f
-        s->lock_fd = fd;
b38b0f
-    }
b38b0f
     s->perm = 0;
b38b0f
     s->shared_perm = BLK_PERM_ALL;
b38b0f
 
b38b0f
@@ -811,15 +798,13 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
b38b0f
         return 0;
b38b0f
     }
b38b0f
 
b38b0f
-    assert(s->lock_fd > 0);
b38b0f
-
b38b0f
     switch (op) {
b38b0f
     case RAW_PL_PREPARE:
b38b0f
-        ret = raw_apply_lock_bytes(s, s->lock_fd, s->perm | new_perm,
b38b0f
+        ret = raw_apply_lock_bytes(s, s->fd, s->perm | new_perm,
b38b0f
                                    ~s->shared_perm | ~new_shared,
b38b0f
                                    false, errp);
b38b0f
         if (!ret) {
b38b0f
-            ret = raw_check_lock_bytes(s->lock_fd, new_perm, new_shared, errp);
b38b0f
+            ret = raw_check_lock_bytes(s->fd, new_perm, new_shared, errp);
b38b0f
             if (!ret) {
b38b0f
                 return 0;
b38b0f
             }
b38b0f
@@ -830,7 +815,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
b38b0f
         op = RAW_PL_ABORT;
b38b0f
         /* fall through to unlock bytes. */
b38b0f
     case RAW_PL_ABORT:
b38b0f
-        raw_apply_lock_bytes(s, s->lock_fd, s->perm, ~s->shared_perm,
b38b0f
+        raw_apply_lock_bytes(s, s->fd, s->perm, ~s->shared_perm,
b38b0f
                              true, &local_err);
b38b0f
         if (local_err) {
b38b0f
             /* Theoretically the above call only unlocks bytes and it cannot
b38b0f
@@ -840,7 +825,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
b38b0f
         }
b38b0f
         break;
b38b0f
     case RAW_PL_COMMIT:
b38b0f
-        raw_apply_lock_bytes(s, s->lock_fd, new_perm, ~new_shared,
b38b0f
+        raw_apply_lock_bytes(s, s->fd, new_perm, ~new_shared,
b38b0f
                              true, &local_err);
b38b0f
         if (local_err) {
b38b0f
             /* Theoretically the above call only unlocks bytes and it cannot
b38b0f
@@ -938,9 +923,17 @@ static void raw_reopen_commit(BDRVReopenState *state)
b38b0f
 {
b38b0f
     BDRVRawReopenState *rs = state->opaque;
b38b0f
     BDRVRawState *s = state->bs->opaque;
b38b0f
+    Error *local_err = NULL;
b38b0f
 
b38b0f
     s->open_flags = rs->open_flags;
b38b0f
 
b38b0f
+    /* Copy locks to the new fd before closing the old one. */
b38b0f
+    raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm,
b38b0f
+                         ~s->locked_shared_perm, false, &local_err);
b38b0f
+    if (local_err) {
b38b0f
+        /* shouldn't fail in a sane host, but report it just in case. */
b38b0f
+        error_report_err(local_err);
b38b0f
+    }
b38b0f
     qemu_close(s->fd);
b38b0f
     s->fd = rs->fd;
b38b0f
 
b38b0f
@@ -1903,10 +1896,6 @@ static void raw_close(BlockDriverState *bs)
b38b0f
         qemu_close(s->fd);
b38b0f
         s->fd = -1;
b38b0f
     }
b38b0f
-    if (s->lock_fd >= 0) {
b38b0f
-        qemu_close(s->lock_fd);
b38b0f
-        s->lock_fd = -1;
b38b0f
-    }
b38b0f
 }
b38b0f
 
b38b0f
 /**
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f