ae23c9
From ecf9ad9048427f1b63a250d9dd595264cf03e256 Mon Sep 17 00:00:00 2001
ae23c9
From: Max Reitz <mreitz@redhat.com>
ae23c9
Date: Mon, 9 Jul 2018 15:11:22 +0200
ae23c9
Subject: [PATCH 204/268] file-posix: Unlock FD after creation
ae23c9
ae23c9
RH-Author: Max Reitz <mreitz@redhat.com>
ae23c9
Message-id: <20180709151122.27541-3-mreitz@redhat.com>
ae23c9
Patchwork-id: 81269
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 2/2] file-posix: Unlock FD after creation
ae23c9
Bugzilla: 1599335
ae23c9
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ae23c9
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
ae23c9
Closing the FD does not necessarily mean that it is unlocked.  Fix this
ae23c9
by relinquishing all permission locks before qemu_close().
ae23c9
ae23c9
Reported-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
(cherry picked from commit 7c20c808a5cbf5d244735bc78fc3138c739c1946)
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/file-posix.c | 17 ++++++++++++++---
ae23c9
 1 file changed, 14 insertions(+), 3 deletions(-)
ae23c9
ae23c9
diff --git a/block/file-posix.c b/block/file-posix.c
ae23c9
index e876770..cbf7c11 100644
ae23c9
--- a/block/file-posix.c
ae23c9
+++ b/block/file-posix.c
ae23c9
@@ -2051,6 +2051,7 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
ae23c9
 static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
 {
ae23c9
     BlockdevCreateOptionsFile *file_opts;
ae23c9
+    Error *local_err = NULL;
ae23c9
     int fd;
ae23c9
     uint64_t perm, shared;
ae23c9
     int result = 0;
ae23c9
@@ -2096,13 +2097,13 @@ static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
     /* Step two: Check that nobody else has taken conflicting locks */
ae23c9
     result = raw_check_lock_bytes(fd, perm, shared, errp);
ae23c9
     if (result < 0) {
ae23c9
-        goto out_close;
ae23c9
+        goto out_unlock;
ae23c9
     }
ae23c9
 
ae23c9
     /* Clear the file by truncating it to 0 */
ae23c9
     result = raw_regular_truncate(fd, 0, PREALLOC_MODE_OFF, errp);
ae23c9
     if (result < 0) {
ae23c9
-        goto out_close;
ae23c9
+        goto out_unlock;
ae23c9
     }
ae23c9
 
ae23c9
     if (file_opts->nocow) {
ae23c9
@@ -2125,7 +2126,17 @@ static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
     result = raw_regular_truncate(fd, file_opts->size, file_opts->preallocation,
ae23c9
                                   errp);
ae23c9
     if (result < 0) {
ae23c9
-        goto out_close;
ae23c9
+        goto out_unlock;
ae23c9
+    }
ae23c9
+
ae23c9
+out_unlock:
ae23c9
+    raw_apply_lock_bytes(fd, 0, 0, true, &local_err);
ae23c9
+    if (local_err) {
ae23c9
+        /* The above call should not fail, and if it does, that does
ae23c9
+         * not mean the whole creation operation has failed.  So
ae23c9
+         * report it the user for their convenience, but do not report
ae23c9
+         * it to the caller. */
ae23c9
+        error_report_err(local_err);
ae23c9
     }
ae23c9
 
ae23c9
 out_close:
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9