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