1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
From 7b7ec67680415c22773ebb2a5daacf298b6b1e06 Mon Sep 17 00:00:00 2001
From: Xavi Hernandez <xhernandez@redhat.com>
Date: Sat, 13 Feb 2021 18:37:32 +0100
Subject: [PATCH 537/538] cluster/afr: Fix race in lockinfo (f)getxattr
A shared dictionary was updated outside the lock after having updated
the number of remaining answers. This means that one thread may be
processing the last answer and unwinding the request before another
thread completes updating the dict.
Thread 1 Thread 2
LOCK()
call_cnt-- (=1)
UNLOCK()
LOCK()
call_cnt-- (=0)
UNLOCK()
update_dict(dict)
if (call_cnt == 0) {
STACK_UNWIND(dict);
}
update_dict(dict)
if (call_cnt == 0) {
STACK_UNWIND(dict);
}
The updates from thread 1 are lost.
This patch also reduces the work done inside the locked region and
reduces code duplication.
Upstream-patch:
> Upstream-patch-link: https://github.com/gluster/glusterfs/pull/2162
> Fixes: #2161
> Change-Id: Idc0d34ab19ea6031de0641f7b05c624d90fac8fa
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
BUG: 1911292
Change-Id: Idc0d34ab19ea6031de0641f7b05c624d90fac8fa
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/228924
Tested-by: RHGS Build Bot <nigelb@redhat.com>
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
---
xlators/cluster/afr/src/afr-inode-read.c | 254 ++++++++++++++-----------------
1 file changed, 112 insertions(+), 142 deletions(-)
diff --git a/xlators/cluster/afr/src/afr-inode-read.c b/xlators/cluster/afr/src/afr-inode-read.c
index cf305af..98e195a 100644
--- a/xlators/cluster/afr/src/afr-inode-read.c
+++ b/xlators/cluster/afr/src/afr-inode-read.c
@@ -15,6 +15,8 @@
#include <stdlib.h>
#include <signal.h>
+#include <urcu/uatomic.h>
+
#include <glusterfs/glusterfs.h>
#include "afr.h"
#include <glusterfs/dict.h>
@@ -868,188 +870,121 @@ afr_getxattr_quota_size_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
return 0;
}
-int32_t
-afr_getxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
- int32_t op_ret, int32_t op_errno, dict_t *dict,
- dict_t *xdata)
+static int32_t
+afr_update_local_dicts(call_frame_t *frame, dict_t *dict, dict_t *xdata)
{
- int call_cnt = 0, len = 0;
- char *lockinfo_buf = NULL;
- dict_t *lockinfo = NULL, *newdict = NULL;
- afr_local_t *local = NULL;
+ afr_local_t *local;
+ dict_t *local_dict;
+ dict_t *local_xdata;
+ int32_t ret;
- LOCK(&frame->lock);
- {
- local = frame->local;
+ local = frame->local;
+ local_dict = NULL;
+ local_xdata = NULL;
- call_cnt = --local->call_count;
+ ret = -ENOMEM;
- if ((op_ret < 0) || (!dict && !xdata)) {
- goto unlock;
- }
-
- if (xdata) {
- if (!local->xdata_rsp) {
- local->xdata_rsp = dict_new();
- if (!local->xdata_rsp) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- goto unlock;
- }
- }
+ if ((dict != NULL) && (local->dict == NULL)) {
+ local_dict = dict_new();
+ if (local_dict == NULL) {
+ goto done;
}
+ }
- if (!dict) {
- goto unlock;
+ if ((xdata != NULL) && (local->xdata_rsp == NULL)) {
+ local_xdata = dict_new();
+ if (local_xdata == NULL) {
+ goto done;
}
+ }
- op_ret = dict_get_ptr_and_len(dict, GF_XATTR_LOCKINFO_KEY,
- (void **)&lockinfo_buf, &len);
+ if ((local_dict != NULL) || (local_xdata != NULL)) {
+ /* TODO: Maybe it would be better to preallocate both dicts before
+ * sending the requests. This way we don't need to use a LOCK()
+ * here. */
+ LOCK(&frame->lock);
- if (!lockinfo_buf) {
- goto unlock;
+ if ((local_dict != NULL) && (local->dict == NULL)) {
+ local->dict = local_dict;
+ local_dict = NULL;
}
- if (!local->dict) {
- local->dict = dict_new();
- if (!local->dict) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- goto unlock;
- }
+ if ((local_xdata != NULL) && (local->xdata_rsp == NULL)) {
+ local->xdata_rsp = local_xdata;
+ local_xdata = NULL;
}
- }
-unlock:
- UNLOCK(&frame->lock);
- if (lockinfo_buf != NULL) {
- lockinfo = dict_new();
- if (lockinfo == NULL) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- } else {
- op_ret = dict_unserialize(lockinfo_buf, len, &lockinfo);
-
- if (lockinfo && local->dict) {
- dict_copy(lockinfo, local->dict);
- }
- }
- }
-
- if (xdata && local->xdata_rsp) {
- dict_copy(xdata, local->xdata_rsp);
+ UNLOCK(&frame->lock);
}
- if (!call_cnt) {
- newdict = dict_new();
- if (!newdict) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- goto unwind;
+ if (dict != NULL) {
+ if (dict_copy(dict, local->dict) < 0) {
+ goto done;
}
+ }
- op_ret = dict_allocate_and_serialize(
- local->dict, (char **)&lockinfo_buf, (unsigned int *)&len);
- if (op_ret != 0) {
- local->op_ret = -1;
- goto unwind;
+ if (xdata != NULL) {
+ if (dict_copy(xdata, local->xdata_rsp) < 0) {
+ goto done;
}
+ }
- op_ret = dict_set_dynptr(newdict, GF_XATTR_LOCKINFO_KEY,
- (void *)lockinfo_buf, len);
- if (op_ret < 0) {
- local->op_ret = -1;
- local->op_errno = -op_ret;
- goto unwind;
- }
+ ret = 0;
- unwind:
- AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, newdict,
- local->xdata_rsp);
+done:
+ if (local_dict != NULL) {
+ dict_unref(local_dict);
}
- dict_unref(lockinfo);
+ if (local_xdata != NULL) {
+ dict_unref(local_xdata);
+ }
- return 0;
+ return ret;
}
-int32_t
-afr_fgetxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
- int32_t op_ret, int32_t op_errno, dict_t *dict,
- dict_t *xdata)
+static void
+afr_getxattr_lockinfo_cbk_common(call_frame_t *frame, int32_t op_ret,
+ int32_t op_errno, dict_t *dict, dict_t *xdata,
+ bool is_fgetxattr)
{
- int call_cnt = 0, len = 0;
+ int len = 0;
char *lockinfo_buf = NULL;
dict_t *lockinfo = NULL, *newdict = NULL;
afr_local_t *local = NULL;
- LOCK(&frame->lock);
- {
- local = frame->local;
-
- call_cnt = --local->call_count;
-
- if ((op_ret < 0) || (!dict && !xdata)) {
- goto unlock;
- }
-
- if (xdata) {
- if (!local->xdata_rsp) {
- local->xdata_rsp = dict_new();
- if (!local->xdata_rsp) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- goto unlock;
- }
- }
- }
-
- if (!dict) {
- goto unlock;
- }
+ local = frame->local;
+ if ((op_ret >= 0) && (dict != NULL)) {
op_ret = dict_get_ptr_and_len(dict, GF_XATTR_LOCKINFO_KEY,
(void **)&lockinfo_buf, &len);
-
- if (!lockinfo_buf) {
- goto unlock;
- }
-
- if (!local->dict) {
- local->dict = dict_new();
- if (!local->dict) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- goto unlock;
+ if (lockinfo_buf != NULL) {
+ lockinfo = dict_new();
+ if (lockinfo == NULL) {
+ op_ret = -1;
+ } else {
+ op_ret = dict_unserialize(lockinfo_buf, len, &lockinfo);
}
}
}
-unlock:
- UNLOCK(&frame->lock);
- if (lockinfo_buf != NULL) {
- lockinfo = dict_new();
- if (lockinfo == NULL) {
- local->op_ret = -1;
- local->op_errno = ENOMEM;
- } else {
- op_ret = dict_unserialize(lockinfo_buf, len, &lockinfo);
-
- if (lockinfo && local->dict) {
- dict_copy(lockinfo, local->dict);
- }
+ if ((op_ret >= 0) && ((lockinfo != NULL) || (xdata != NULL))) {
+ op_ret = afr_update_local_dicts(frame, lockinfo, xdata);
+ if (lockinfo != NULL) {
+ dict_unref(lockinfo);
}
}
- if (xdata && local->xdata_rsp) {
- dict_copy(xdata, local->xdata_rsp);
+ if (op_ret < 0) {
+ local->op_ret = -1;
+ local->op_errno = ENOMEM;
}
- if (!call_cnt) {
+ if (uatomic_sub_return(&local->call_count, 1) == 0) {
newdict = dict_new();
if (!newdict) {
local->op_ret = -1;
- local->op_errno = ENOMEM;
+ local->op_errno = op_errno = ENOMEM;
goto unwind;
}
@@ -1057,23 +992,58 @@ unlock:
local->dict, (char **)&lockinfo_buf, (unsigned int *)&len);
if (op_ret != 0) {
local->op_ret = -1;
+ local->op_errno = op_errno = ENOMEM;
goto unwind;
}
op_ret = dict_set_dynptr(newdict, GF_XATTR_LOCKINFO_KEY,
(void *)lockinfo_buf, len);
if (op_ret < 0) {
- local->op_ret = -1;
- local->op_errno = -op_ret;
+ GF_FREE(lockinfo_buf);
+ local->op_ret = op_ret = -1;
+ local->op_errno = op_errno = -op_ret;
goto unwind;
}
unwind:
- AFR_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, newdict,
- local->xdata_rsp);
+ /* TODO: These unwinds use op_ret and op_errno instead of local->op_ret
+ * and local->op_errno. This doesn't seem right because any
+ * failure during processing of each answer could be silently
+ * ignored. This is kept this was the old behavior and because
+ * local->op_ret is initialized as -1 and local->op_errno is
+ * initialized as EUCLEAN, which makes these values useless. */
+ if (is_fgetxattr) {
+ AFR_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, newdict,
+ local->xdata_rsp);
+ } else {
+ AFR_STACK_UNWIND(getxattr, frame, op_ret, op_errno, newdict,
+ local->xdata_rsp);
+ }
+
+ if (newdict != NULL) {
+ dict_unref(newdict);
+ }
}
+}
+
+static int32_t
+afr_getxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
+ int32_t op_ret, int32_t op_errno, dict_t *dict,
+ dict_t *xdata)
+{
+ afr_getxattr_lockinfo_cbk_common(frame, op_ret, op_errno, dict, xdata,
+ false);
- dict_unref(lockinfo);
+ return 0;
+}
+
+static int32_t
+afr_fgetxattr_lockinfo_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
+ int32_t op_ret, int32_t op_errno, dict_t *dict,
+ dict_t *xdata)
+{
+ afr_getxattr_lockinfo_cbk_common(frame, op_ret, op_errno, dict, xdata,
+ true);
return 0;
}
--
1.8.3.1
|