1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Fri, 26 Apr 2019 09:15:28 +0200
4 Subject: [PATCH] PVE: [Up+Config] file-posix: make locking optiono on create
6 Otherwise creating images on nfs/cifs can be problematic.
8 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
9 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
11 block/file-posix.c | 61 +++++++++++++++++++++++++++++---------------
12 qapi/block-core.json | 3 ++-
13 2 files changed, 43 insertions(+), 21 deletions(-)
15 diff --git a/block/file-posix.c b/block/file-posix.c
16 index 71aa45ce5d..ae1cd3980e 100644
17 --- a/block/file-posix.c
18 +++ b/block/file-posix.c
19 @@ -2230,6 +2230,7 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
21 uint64_t perm, shared;
23 + bool locked = false;
25 /* Validate options and set default values */
26 assert(options->driver == BLOCKDEV_DRIVER_FILE);
27 @@ -2263,19 +2264,22 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
28 perm = BLK_PERM_WRITE | BLK_PERM_RESIZE;
29 shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
31 - /* Step one: Take locks */
32 - result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp);
36 + if (file_opts->locking != ON_OFF_AUTO_OFF) {
37 + /* Step one: Take locks */
38 + result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp);
44 - /* Step two: Check that nobody else has taken conflicting locks */
45 - result = raw_check_lock_bytes(fd, perm, shared, errp);
47 - error_append_hint(errp,
48 - "Is another process using the image [%s]?\n",
49 - file_opts->filename);
51 + /* Step two: Check that nobody else has taken conflicting locks */
52 + result = raw_check_lock_bytes(fd, perm, shared, errp);
54 + error_append_hint(errp,
55 + "Is another process using the image [%s]?\n",
56 + file_opts->filename);
61 /* Clear the file by truncating it to 0 */
62 @@ -2308,13 +2312,15 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp)
66 - raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err);
68 - /* The above call should not fail, and if it does, that does
69 - * not mean the whole creation operation has failed. So
70 - * report it the user for their convenience, but do not report
71 - * it to the caller. */
72 - warn_report_err(local_err);
74 + raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err);
76 + /* The above call should not fail, and if it does, that does
77 + * not mean the whole creation operation has failed. So
78 + * report it the user for their convenience, but do not report
79 + * it to the caller. */
80 + warn_report_err(local_err);
85 @@ -2335,6 +2341,7 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
86 PreallocMode prealloc;
88 Error *local_err = NULL;
91 /* Skip file: protocol prefix */
92 strstart(filename, "file:", &filename);
93 @@ -2352,6 +2359,18 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
97 + locking = qapi_enum_parse(&OnOffAuto_lookup,
98 + qemu_opt_get(opts, "locking"),
99 + ON_OFF_AUTO_AUTO, &local_err);
101 + error_propagate(errp, local_err);
105 + if (locking == ON_OFF_AUTO_AUTO) {
106 + locking = ON_OFF_AUTO_OFF;
109 options = (BlockdevCreateOptions) {
110 .driver = BLOCKDEV_DRIVER_FILE,
112 @@ -2361,6 +2380,8 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
113 .preallocation = prealloc,
116 + .has_locking = true,
117 + .locking = locking,
120 return raw_co_create(&options, errp);
121 @@ -2838,7 +2859,7 @@ static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared,
124 /* Copy locks to the new fd */
125 - if (s->perm_change_fd) {
126 + if (s->use_lock && s->perm_change_fd) {
127 ret = raw_apply_lock_bytes(NULL, s->perm_change_fd, perm, ~shared,
130 diff --git a/qapi/block-core.json b/qapi/block-core.json
131 index 97cb7ec41c..f432d4f3ec 100644
132 --- a/qapi/block-core.json
133 +++ b/qapi/block-core.json
134 @@ -4284,7 +4284,8 @@
135 'data': { 'filename': 'str',
137 '*preallocation': 'PreallocMode',
138 - '*nocow': 'bool' } }
140 + '*locking': 'OnOffAuto' } }
143 # @BlockdevCreateOptionsGluster: