]>
Commit | Line | Data |
---|---|---|
c36c53f8 WB |
1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
2 | From: Wolfgang Bumiller <w.bumiller@proxmox.com> | |
83faa3fe TL |
3 | Date: Mon, 6 Apr 2020 12:16:50 +0200 |
4 | Subject: [PATCH] PVE: [Up+Config] file-posix: make locking optiono on create | |
c36c53f8 WB |
5 | |
6 | Otherwise creating images on nfs/cifs can be problematic. | |
7 | ||
8 | Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com> | |
b855dce7 | 9 | Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com> |
c36c53f8 | 10 | --- |
8dca018b | 11 | block/file-posix.c | 59 ++++++++++++++++++++++++++++++-------------- |
c36c53f8 | 12 | qapi/block-core.json | 3 ++- |
8dca018b | 13 | 2 files changed, 42 insertions(+), 20 deletions(-) |
c36c53f8 WB |
14 | |
15 | diff --git a/block/file-posix.c b/block/file-posix.c | |
d03e1b3c | 16 | index 9a16d86344..bd68df57ad 100644 |
c36c53f8 WB |
17 | --- a/block/file-posix.c |
18 | +++ b/block/file-posix.c | |
d03e1b3c | 19 | @@ -2487,6 +2487,7 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp) |
c36c53f8 WB |
20 | int fd; |
21 | uint64_t perm, shared; | |
22 | int result = 0; | |
23 | + bool locked = false; | |
24 | ||
25 | /* Validate options and set default values */ | |
26 | assert(options->driver == BLOCKDEV_DRIVER_FILE); | |
d03e1b3c | 27 | @@ -2527,19 +2528,22 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp) |
c36c53f8 WB |
28 | perm = BLK_PERM_WRITE | BLK_PERM_RESIZE; |
29 | shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; | |
30 | ||
31 | - /* Step one: Take locks */ | |
b855dce7 | 32 | - result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); |
c36c53f8 WB |
33 | - if (result < 0) { |
34 | - goto out_close; | |
35 | - } | |
36 | + if (file_opts->locking != ON_OFF_AUTO_OFF) { | |
37 | + /* Step one: Take locks */ | |
b855dce7 | 38 | + result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); |
c36c53f8 WB |
39 | + if (result < 0) { |
40 | + goto out_close; | |
41 | + } | |
42 | + locked = true; | |
43 | ||
44 | - /* Step two: Check that nobody else has taken conflicting locks */ | |
45 | - result = raw_check_lock_bytes(fd, perm, shared, errp); | |
46 | - if (result < 0) { | |
b855dce7 TL |
47 | - error_append_hint(errp, |
48 | - "Is another process using the image [%s]?\n", | |
49 | - file_opts->filename); | |
c36c53f8 WB |
50 | - goto out_unlock; |
51 | + /* Step two: Check that nobody else has taken conflicting locks */ | |
52 | + result = raw_check_lock_bytes(fd, perm, shared, errp); | |
53 | + if (result < 0) { | |
b855dce7 TL |
54 | + error_append_hint(errp, |
55 | + "Is another process using the image [%s]?\n", | |
56 | + file_opts->filename); | |
c36c53f8 WB |
57 | + goto out_unlock; |
58 | + } | |
59 | } | |
60 | ||
61 | /* Clear the file by truncating it to 0 */ | |
d03e1b3c | 62 | @@ -2593,13 +2597,15 @@ raw_co_create(BlockdevCreateOptions *options, Error **errp) |
c36c53f8 WB |
63 | } |
64 | ||
65 | out_unlock: | |
b855dce7 | 66 | - raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err); |
c36c53f8 WB |
67 | - if (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. */ | |
b855dce7 | 72 | - warn_report_err(local_err); |
c36c53f8 | 73 | + if (locked) { |
b855dce7 | 74 | + raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err); |
c36c53f8 WB |
75 | + if (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. */ | |
b855dce7 | 80 | + warn_report_err(local_err); |
c36c53f8 WB |
81 | + } |
82 | } | |
83 | ||
84 | out_close: | |
d03e1b3c | 85 | @@ -2624,6 +2630,7 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv, |
c36c53f8 WB |
86 | PreallocMode prealloc; |
87 | char *buf = NULL; | |
88 | Error *local_err = NULL; | |
89 | + OnOffAuto locking; | |
90 | ||
91 | /* Skip file: protocol prefix */ | |
92 | strstart(filename, "file:", &filename); | |
d03e1b3c | 93 | @@ -2646,6 +2653,18 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv, |
c36c53f8 WB |
94 | return -EINVAL; |
95 | } | |
96 | ||
97 | + locking = qapi_enum_parse(&OnOffAuto_lookup, | |
98 | + qemu_opt_get(opts, "locking"), | |
99 | + ON_OFF_AUTO_AUTO, &local_err); | |
100 | + if (local_err) { | |
101 | + error_propagate(errp, local_err); | |
102 | + return -EINVAL; | |
103 | + } | |
104 | + | |
105 | + if (locking == ON_OFF_AUTO_AUTO) { | |
106 | + locking = ON_OFF_AUTO_OFF; | |
107 | + } | |
108 | + | |
109 | options = (BlockdevCreateOptions) { | |
110 | .driver = BLOCKDEV_DRIVER_FILE, | |
111 | .u.file = { | |
d03e1b3c | 112 | @@ -2657,6 +2676,8 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv, |
c36c53f8 | 113 | .nocow = nocow, |
60ae3775 SR |
114 | .has_extent_size_hint = has_extent_size_hint, |
115 | .extent_size_hint = extent_size_hint, | |
c36c53f8 WB |
116 | + .has_locking = true, |
117 | + .locking = locking, | |
118 | }, | |
119 | }; | |
120 | return raw_co_create(&options, errp); | |
121 | diff --git a/qapi/block-core.json b/qapi/block-core.json | |
d03e1b3c | 122 | index 7daaf545be..9e902b96bb 100644 |
c36c53f8 WB |
123 | --- a/qapi/block-core.json |
124 | +++ b/qapi/block-core.json | |
d03e1b3c | 125 | @@ -4624,7 +4624,8 @@ |
60ae3775 SR |
126 | 'size': 'size', |
127 | '*preallocation': 'PreallocMode', | |
128 | '*nocow': 'bool', | |
129 | - '*extent-size-hint': 'size'} } | |
130 | + '*extent-size-hint': 'size', | |
c36c53f8 WB |
131 | + '*locking': 'OnOffAuto' } } |
132 | ||
133 | ## | |
134 | # @BlockdevCreateOptionsGluster: |