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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
From 23ab7175e64ab4d75fbcb6874008843cc78b65b8 Mon Sep 17 00:00:00 2001
From: Ashish Pandey <aspandey@redhat.com>
Date: Fri, 16 Apr 2021 18:48:56 +0530
Subject: [PATCH 541/542] glusterd-volgen: Add functionality to accept any
custom xlator
Add new function which allow users to insert any custom xlators.
It makes to provide a way to add any processing into file operations.
Users can deploy the plugin(xlator shared object) and integrate it to glusterfsd.
If users want to enable a custom xlator, do the follows:
1. put xlator object(.so file) into "XLATOR_DIR/user/"
2. set the option user.xlator.<xlator> to the existing xlator-name to specify of the position in graph
3. restart gluster volume
Options for custom xlator are able to set in "user.xlator.<xlator>.<optkey>".
Backport of :
>https://github.com/gluster/glusterfs/commit/ea86b664f3b1f54901ce1b7d7fba7d80456f2089
>Fixes: https://github.com/gluster/glusterfs/issues/1943
>Change-Id: Ife3ae1514ea474f5dae2897223012f9d04b64674
>Signed-off-by:Ryo Furuhashi <ryo.furuhashi.nh@hitachi.com>
>Co-authored-by: Yaniv Kaul <ykaul@redhat.com>
>Co-authored-by: Xavi Hernandez <xhernandez@users.noreply.github.com>
Change-Id: Ic8f28bfcfde67213eb1092b0ebf4822c874d37bb
BUG: 1927235
Signed-off-by: Ashish Pandey <aspandey@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/236830
Tested-by: RHGS Build Bot <nigelb@redhat.com>
Reviewed-by: Ravishankar Narayanankutty <ravishankar@redhat.com>
Reviewed-by: Xavi Hernandez Juan <xhernandez@redhat.com>
---
cli/src/cli-rpc-ops.c | 148 ++++++++++++++++++++------
cli/src/cli.h | 2 -
tests/basic/user-xlator.t | 65 ++++++++++++
tests/env.rc.in | 3 +
xlators/mgmt/glusterd/src/glusterd-volgen.c | 155 ++++++++++++++++++++++++++++
5 files changed, 342 insertions(+), 31 deletions(-)
create mode 100755 tests/basic/user-xlator.t
diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c
index 4e91265..51b5447 100644
--- a/cli/src/cli-rpc-ops.c
+++ b/cli/src/cli-rpc-ops.c
@@ -2269,49 +2269,131 @@ out:
return ret;
}
-char *
-is_server_debug_xlator(void *myframe)
+/*
+ * returns
+ * 1 : is server debug xlator
+ * 0 : is not server debug xlator
+ * <0 : error
+ */
+static int
+is_server_debug_xlator(char *key, char *value)
+{
+ if (!key || !value)
+ return -1;
+
+ if (strcmp("debug.trace", key) == 0 ||
+ strcmp("debug.error-gen", key) == 0) {
+ if (strcmp("client", value) == 0)
+ return 0;
+ else
+ return 1;
+ }
+
+ return 0;
+}
+
+/*
+ * returns
+ * 1 : is user xlator
+ * 0 : is not user xlator
+ * <0 : error
+ */
+static int
+is_server_user_xlator(char *key, char *value)
+{
+ int ret = 0;
+
+ if (!key || !value)
+ return -1;
+
+ ret = fnmatch("user.xlator.*", key, 0);
+ if (ret < 0) {
+ ret = -1;
+ goto out;
+ } else if (ret == FNM_NOMATCH) {
+ ret = 0;
+ goto out;
+ }
+
+ ret = fnmatch("user.xlator.*.*", key, 0);
+ if (ret < 0) {
+ ret = -1;
+ goto out;
+ } else if (ret != FNM_NOMATCH) { // this is user xlator's option key
+ ret = 0;
+ goto out;
+ }
+
+ ret = 1;
+
+out:
+ return ret;
+}
+
+static int
+added_server_xlator(void *myframe, char **added_xlator)
{
call_frame_t *frame = NULL;
cli_local_t *local = NULL;
char **words = NULL;
char *key = NULL;
char *value = NULL;
- char *debug_xlator = NULL;
+ int ret = 0;
frame = myframe;
local = frame->local;
words = (char **)local->words;
while (*words != NULL) {
- if (strstr(*words, "trace") == NULL &&
- strstr(*words, "error-gen") == NULL) {
- words++;
- continue;
- }
-
key = *words;
words++;
value = *words;
- if (value == NULL)
+
+ if (!value) {
break;
- if (strstr(value, "client")) {
- words++;
- continue;
- } else {
- if (!(strstr(value, "posix") || strstr(value, "acl") ||
- strstr(value, "locks") || strstr(value, "io-threads") ||
- strstr(value, "marker") || strstr(value, "index"))) {
- words++;
- continue;
- } else {
- debug_xlator = gf_strdup(key);
- break;
+ }
+
+ ret = is_server_debug_xlator(key, value);
+ if (ret < 0) {
+ gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR,
+ "failed to check that debug xlator was added");
+ ret = -1;
+ goto out;
+ }
+
+ if (ret) {
+ *added_xlator = gf_strdup(key);
+ if (!*added_xlator) {
+ gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR,
+ "Out of memory");
+ ret = -1;
+ goto out;
+ }
+ break;
+ }
+
+ ret = is_server_user_xlator(key, value);
+ if (ret < 0) {
+ gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR,
+ "failed to check that user xlator was added");
+ ret = -1;
+ goto out;
+ }
+
+ if (ret) {
+ *added_xlator = gf_strdup(key);
+ if (!*added_xlator) {
+ gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR,
+ "Out of memory");
+ ret = -1;
+ goto out;
}
+ break;
}
}
- return debug_xlator;
+out:
+ return ret;
}
int
@@ -2327,7 +2409,7 @@ gf_cli_set_volume_cbk(struct rpc_req *req, struct iovec *iov, int count,
char msg[1024] = {
0,
};
- char *debug_xlator = NULL;
+ char *added_xlator = NULL;
char tmp_str[512] = {
0,
};
@@ -2365,18 +2447,26 @@ gf_cli_set_volume_cbk(struct rpc_req *req, struct iovec *iov, int count,
* The process has to be restarted. So this is a check from the
* volume set option such that if debug xlators such as trace/errorgen
* are provided in the set command, warn the user.
+ * volume set option such that if user custom xlators or debug
+ * xlators such as trace/errorgen are provided in the set command,
+ * warn the user.
*/
- debug_xlator = is_server_debug_xlator(myframe);
+ ret = added_server_xlator(myframe, &added_xlator);
+ if (ret < 0) {
+ gf_log("cli", GF_LOG_ERROR,
+ "failed to check that server graph has been changed");
+ goto out;
+ }
if (dict_get_str(dict, "help-str", &help_str) && !msg[0])
snprintf(msg, sizeof(msg), "Set volume %s",
(rsp.op_ret) ? "unsuccessful" : "successful");
- if (rsp.op_ret == 0 && debug_xlator) {
+ if (rsp.op_ret == 0 && added_xlator) {
snprintf(tmp_str, sizeof(tmp_str),
"\n%s translator has been "
"added to the server volume file. Please restart the"
" volume for enabling the translator",
- debug_xlator);
+ added_xlator);
}
if ((global_state->mode & GLUSTER_MODE_XML) && (help_str == NULL)) {
@@ -2394,7 +2484,7 @@ gf_cli_set_volume_cbk(struct rpc_req *req, struct iovec *iov, int count,
cli_err("volume set: failed");
} else {
if (help_str == NULL) {
- if (debug_xlator == NULL)
+ if (added_xlator == NULL)
cli_out("volume set: success");
else
cli_out("volume set: success%s", tmp_str);
@@ -2408,7 +2498,7 @@ gf_cli_set_volume_cbk(struct rpc_req *req, struct iovec *iov, int count,
out:
if (dict)
dict_unref(dict);
- GF_FREE(debug_xlator);
+ GF_FREE(added_xlator);
cli_cmd_broadcast_response(ret);
gf_free_xdr_cli_rsp(rsp);
return ret;
diff --git a/cli/src/cli.h b/cli/src/cli.h
index 7b4f446..b5b69ea 100644
--- a/cli/src/cli.h
+++ b/cli/src/cli.h
@@ -502,8 +502,6 @@ cli_xml_output_snapshot(int cmd_type, dict_t *dict, int op_ret, int op_errno,
int
cli_xml_snapshot_status_single_snap(cli_local_t *local, dict_t *dict,
char *key);
-char *
-is_server_debug_xlator(void *myframe);
int32_t
cli_cmd_snapshot_parse(const char **words, int wordcount, dict_t **options,
diff --git a/tests/basic/user-xlator.t b/tests/basic/user-xlator.t
new file mode 100755
index 0000000..a711f9f
--- /dev/null
+++ b/tests/basic/user-xlator.t
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+. $(dirname $0)/../include.rc
+. $(dirname $0)/../volume.rc
+
+#### patchy.dev.d-backends-patchy1.vol
+brick=${B0//\//-}
+SERVER_VOLFILE="/var/lib/glusterd/vols/${V0}/${V0}.${H0}.${brick:1}-${V0}1.vol"
+
+cleanup;
+
+TEST mkdir -p $B0/single-brick
+TEST mkdir -p ${GLUSTER_XLATOR_DIR}/user
+
+## deploy dummy user xlator
+TEST cp ${GLUSTER_XLATOR_DIR}/playground/template.so ${GLUSTER_XLATOR_DIR}/user/hoge.so
+
+TEST glusterd
+TEST $CLI volume create $V0 replica 3 $H0:$B0/${V0}{1,2,3,4,5,6};
+TEST $CLI volume set $V0 user.xlator.hoge posix
+TEST grep -q 'user/hoge' ${SERVER_VOLFILE}
+
+TEST $CLI volume set $V0 user.xlator.hoge.opt1 10
+TEST grep -q '"option opt1 10"' ${SERVER_VOLFILE}
+TEST $CLI volume set $V0 user.xlator.hoge.opt2 hogehoge
+TEST grep -q '"option opt2 hogehoge"' ${SERVER_VOLFILE}
+TEST $CLI volume set $V0 user.xlator.hoge.opt3 true
+TEST grep -q '"option opt3 true"' ${SERVER_VOLFILE}
+
+TEST $CLI volume start $V0
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}1
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}2
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}3
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}4
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}5
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}6
+
+TEST $CLI volume set $V0 user.xlator.hoge trash
+TEST grep -q 'user/hoge' ${SERVER_VOLFILE}
+
+TEST $CLI volume stop $V0
+TEST $CLI volume start $V0
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}1
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}2
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}3
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}4
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}5
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}6
+
+TEST ! $CLI volume set $V0 user.xlator.hoge unknown
+TEST grep -q 'user/hoge' ${SERVER_VOLFILE} # When the CLI fails, the volfile is not modified.
+
+TEST $CLI volume stop $V0
+TEST $CLI volume start $V0
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}1
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}2
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}3
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}4
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}5
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/${V0}6
+
+#### teardown
+
+TEST rm -f ${GLUSTER_XLATOR_DIR}/user/hoge.so
+cleanup;
diff --git a/tests/env.rc.in b/tests/env.rc.in
index c7472a7..1f0ca88 100644
--- a/tests/env.rc.in
+++ b/tests/env.rc.in
@@ -40,3 +40,6 @@ export GLUSTER_LIBEXECDIR
RUN_NFS_TESTS=@BUILD_GNFS@
export RUN_NFS_TESTS
+
+GLUSTER_XLATOR_DIR=@libdir@/glusterfs/@PACKAGE_VERSION@/xlator
+export GLUSTER_XLATOR_DIR
\ No newline at end of file
diff --git a/xlators/mgmt/glusterd/src/glusterd-volgen.c b/xlators/mgmt/glusterd/src/glusterd-volgen.c
index 1920284..a242b5c 100644
--- a/xlators/mgmt/glusterd/src/glusterd-volgen.c
+++ b/xlators/mgmt/glusterd/src/glusterd-volgen.c
@@ -45,6 +45,11 @@ struct gd_validate_reconf_opts {
extern struct volopt_map_entry glusterd_volopt_map[];
+struct check_and_add_user_xlator_t {
+ volgen_graph_t *graph;
+ char *volname;
+};
+
#define RPC_SET_OPT(XL, CLI_OPT, XLATOR_OPT, ERROR_CMD) \
do { \
char *_value = NULL; \
@@ -2822,6 +2827,145 @@ out:
return ret;
}
+static gf_boolean_t
+check_user_xlator_position(dict_t *dict, char *key, data_t *value,
+ void *prev_xlname)
+{
+ if (strncmp(key, "user.xlator.", SLEN("user.xlator.")) != 0) {
+ return false;
+ }
+
+ if (fnmatch("user.xlator.*.*", key, 0) == 0) {
+ return false;
+ }
+
+ char *value_str = data_to_str(value);
+ if (!value_str) {
+ return false;
+ }
+
+ if (strcmp(value_str, prev_xlname) == 0) {
+ gf_log("glusterd", GF_LOG_INFO,
+ "found insert position of user-xlator(%s)", key);
+ return true;
+ }
+
+ return false;
+}
+
+static int
+set_user_xlator_option(dict_t *set_dict, char *key, data_t *value, void *data)
+{
+ xlator_t *xl = data;
+ char *optname = strrchr(key, '.') + 1;
+
+ gf_log("glusterd", GF_LOG_DEBUG, "set user xlator option %s = %s", key,
+ value->data);
+
+ return xlator_set_option(xl, optname, strlen(optname), data_to_str(value));
+}
+
+static int
+insert_user_xlator_to_graph(dict_t *set_dict, char *key, data_t *value,
+ void *action_data)
+{
+ int ret = -1;
+
+ struct check_and_add_user_xlator_t *data = action_data;
+
+ char *xlator_name = strrchr(key, '.') + 1; // user.xlator.<xlator_name>
+ char *xlator_option_matcher = NULL;
+ char *type = NULL;
+ xlator_t *xl = NULL;
+
+ // convert optkey to xlator type
+ if (gf_asprintf(&type, "user/%s", xlator_name) < 0) {
+ gf_log("glusterd", GF_LOG_ERROR, "failed to generate user-xlator type");
+ goto out;
+ }
+
+ gf_log("glusterd", GF_LOG_INFO, "add user xlator=%s to graph", type);
+
+ xl = volgen_graph_add(data->graph, type, data->volname);
+ if (!xl) {
+ goto out;
+ }
+
+ ret = gf_asprintf(&xlator_option_matcher, "user.xlator.%s.*", xlator_name);
+ if (ret < 0) {
+ gf_log("glusterd", GF_LOG_ERROR,
+ "failed to generate user-xlator option matcher");
+ goto out;
+ }
+
+ dict_foreach_fnmatch(set_dict, xlator_option_matcher,
+ set_user_xlator_option, xl);
+
+out:
+ if (type)
+ GF_FREE(type);
+ if (xlator_option_matcher)
+ GF_FREE(xlator_option_matcher);
+
+ return ret;
+}
+
+static int
+validate_user_xlator_position(dict_t *this, char *key, data_t *value,
+ void *unused)
+{
+ int ret = -1;
+ int i = 0;
+
+ if (!value)
+ goto out;
+
+ if (fnmatch("user.xlator.*.*", key, 0) == 0) {
+ ret = 0;
+ goto out;
+ }
+
+ char *value_str = data_to_str(value);
+ if (!value_str)
+ goto out;
+
+ int num_xlators = sizeof(server_graph_table) /
+ sizeof(server_graph_table[0]);
+ for (i = 0; i < num_xlators; i++) {
+ if (server_graph_table[i].dbg_key &&
+ strcmp(value_str, server_graph_table[i].dbg_key) == 0) {
+ ret = 0;
+ goto out;
+ }
+ }
+
+out:
+ if (ret == -1)
+ gf_log("glusterd", GF_LOG_ERROR, "invalid user xlator position %s = %s",
+ key, value->data);
+
+ return ret;
+}
+
+static int
+check_and_add_user_xl(volgen_graph_t *graph, dict_t *set_dict, char *volname,
+ char *prev_xlname)
+{
+ if (!prev_xlname)
+ goto out;
+
+ struct check_and_add_user_xlator_t data = {.graph = graph,
+ .volname = volname};
+
+ if (dict_foreach_match(set_dict, check_user_xlator_position, prev_xlname,
+ insert_user_xlator_to_graph, &data) < 0) {
+ return -1;
+ }
+
+out:
+ return 0;
+}
+
static int
server_graph_builder(volgen_graph_t *graph, glusterd_volinfo_t *volinfo,
dict_t *set_dict, void *param)
@@ -2831,6 +2975,12 @@ server_graph_builder(volgen_graph_t *graph, glusterd_volinfo_t *volinfo,
char *loglevel = NULL;
int i = 0;
+ if (dict_foreach_fnmatch(set_dict, "user.xlator.*",
+ validate_user_xlator_position, NULL) < 0) {
+ ret = -EINVAL;
+ goto out;
+ }
+
i = sizeof(server_graph_table) / sizeof(server_graph_table[0]) - 1;
while (i >= 0) {
@@ -2848,6 +2998,11 @@ server_graph_builder(volgen_graph_t *graph, glusterd_volinfo_t *volinfo,
if (ret)
goto out;
+ ret = check_and_add_user_xl(graph, set_dict, volinfo->volname,
+ server_graph_table[i].dbg_key);
+ if (ret)
+ goto out;
+
i--;
}
--
1.8.3.1
|