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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*
Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
%token VOLUME_BEGIN VOLUME_END OPTION NEWLINE SUBVOLUME ID WHITESPACE COMMENT TYPE STRING_TOK
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#define RELAX_POISONING
#include "xlator.h"
#include "graph-utils.h"
#include "logging.h"
static int new_volume (char *name);
static int volume_type (char *type);
static int volume_option (char *key, char *value);
static int volume_sub (char *sub);
static int volume_end (void);
static void sub_error (void);
static void type_error (void);
static void option_error (void);
#define YYSTYPE char *
#define GF_CMD_BUFFER_LEN (8 * GF_UNIT_KB)
int yyerror (const char *);
int yylex ();
%}
%%
VOLUMES: VOLUME | VOLUMES VOLUME;
VOLUME: VOLUME_HEADER VOLUME_DATA VOLUME_FOOTER;
VOLUME_HEADER: VOLUME_BEGIN WORD {if (new_volume ($2) == -1) { YYABORT; }};
VOLUME_FOOTER: VOLUME_END {if (volume_end () == -1) { YYABORT; }};
VOLUME_DATA: TYPE_LINE OPTIONS_LINE SUBVOLUME_LINE OPTIONS_LINE |
TYPE_LINE SUBVOLUME_LINE OPTIONS_LINE |
TYPE_LINE OPTIONS_LINE SUBVOLUME_LINE |
TYPE_LINE SUBVOLUME_LINE |
TYPE_LINE OPTIONS_LINE |
OPTIONS_LINE SUBVOLUME_LINE OPTIONS_LINE | /* error case */
OPTIONS_LINE; /* error case */
TYPE_LINE: TYPE WORD {if (volume_type ($2) == -1) { YYABORT; }} | TYPE { type_error(); YYABORT; };
SUBVOLUME_LINE: SUBVOLUME WORDS | SUBVOLUME { sub_error (); YYABORT; };
OPTIONS_LINE: OPTION_LINE | OPTIONS_LINE OPTION_LINE;
OPTION_LINE: OPTION WORD WORD {if (volume_option ($2, $3) == -1) { YYABORT; }} |
OPTION WORD { option_error (); YYABORT; } |
OPTION { option_error (); YYABORT; };
WORDS: WORD {if (volume_sub ($1) == -1) {YYABORT; }} | WORDS WORD { if (volume_sub ($2) == -1) { YYABORT; }};
WORD: ID | STRING_TOK ;
%%
xlator_t *curr;
glusterfs_graph_t *construct;
static void
type_error (void)
{
extern int yylineno;
gf_log ("parser", GF_LOG_ERROR,
"Volume %s, before line %d: Please specify volume type",
curr->name, yylineno);
return;
}
static void
sub_error (void)
{
extern int yylineno;
gf_log ("parser", GF_LOG_ERROR,
"Volume %s, before line %d: Please specify subvolumes",
curr->name, yylineno);
return;
}
static void
option_error (void)
{
extern int yylineno;
gf_log ("parser", GF_LOG_ERROR,
"Volume %s, before line %d: Please specify "
"option <key> <value>",
curr->name, yylineno);
return;
}
static int
new_volume (char *name)
{
extern int yylineno;
xlator_t *trav = NULL;
int ret = 0;
if (!name) {
gf_log ("parser", GF_LOG_DEBUG,
"Invalid argument name: '%s'", name);
ret = -1;
goto out;
}
if (curr) {
gf_log ("parser", GF_LOG_ERROR,
"new volume (%s) defintion in line %d unexpected",
name, yylineno);
ret = -1;
goto out;
}
curr = (void *) GF_CALLOC (1, sizeof (*curr),
gf_common_mt_xlator_t);
if (!curr) {
gf_log ("parser", GF_LOG_ERROR, "Out of memory");
ret = -1;
goto out;
}
trav = construct->first;
while (trav) {
if (!strcmp (name, trav->name)) {
gf_log ("parser", GF_LOG_ERROR,
"Line %d: volume '%s' defined again",
yylineno, name);
ret = -1;
goto out;
}
trav = trav->next;
}
curr->name = gf_strdup (name);
if (!curr->name) {
GF_FREE (curr);
ret = -1;
goto out;
}
curr->options = get_new_dict ();
if (!curr->options) {
GF_FREE (curr->name);
GF_FREE (curr);
ret = -1;
goto out;
}
curr->next = construct->first;
if (curr->next)
curr->next->prev = curr;
curr->graph = construct;
construct->first = curr;
construct->xl_count++;
gf_log ("parser", GF_LOG_TRACE, "New node for '%s'", name);
out:
GF_FREE (name);
return ret;
}
static int
volume_type (char *type)
{
extern int yylineno;
int32_t ret = 0;
if (!type) {
gf_log ("parser", GF_LOG_DEBUG, "Invalid argument type");
ret = -1;
goto out;
}
ret = xlator_set_type (curr, type);
if (ret) {
gf_log ("parser", GF_LOG_ERROR,
"Volume '%s', line %d: type '%s' is not valid or "
"not found on this machine",
curr->name, yylineno, type);
ret = -1;
goto out;
}
gf_log ("parser", GF_LOG_TRACE, "Type:%s:%s", curr->name, type);
out:
GF_FREE (type);
return 0;
}
static int
volume_option (char *key, char *value)
{
extern int yylineno;
int ret = 0;
char *set_value = NULL;
if (!key || !value){
gf_log ("parser", GF_LOG_ERROR, "Invalid argument");
ret = -1;
goto out;
}
set_value = gf_strdup (value);
ret = dict_set_dynstr (curr->options, key, set_value);
if (ret == 1) {
gf_log ("parser", GF_LOG_ERROR,
"Volume '%s', line %d: duplicate entry "
"('option %s') present",
curr->name, yylineno, key);
ret = -1;
goto out;
}
gf_log ("parser", GF_LOG_TRACE, "Option:%s:%s:%s",
curr->name, key, value);
out:
GF_FREE (key);
GF_FREE (value);
return 0;
}
static int
volume_sub (char *sub)
{
extern int yylineno;
xlator_t *trav = NULL;
int ret = 0;
if (!sub) {
gf_log ("parser", GF_LOG_ERROR, "Invalid subvolumes argument");
ret = -1;
goto out;
}
trav = construct->first;
while (trav) {
if (!strcmp (sub, trav->name))
break;
trav = trav->next;
}
if (!trav) {
gf_log ("parser", GF_LOG_ERROR,
"Volume '%s', line %d: subvolume '%s' is not defined "
"prior to usage",
curr->name, yylineno, sub);
ret = -1;
goto out;
}
if (trav == curr) {
gf_log ("parser", GF_LOG_ERROR,
"Volume '%s', line %d: has '%s' itself as subvolume",
curr->name, yylineno, sub);
ret = -1;
goto out;
}
ret = glusterfs_xlator_link (curr, trav);
if (ret) {
gf_log ("parser", GF_LOG_ERROR, "Out of memory");
ret = -1;
goto out;
}
gf_log ("parser", GF_LOG_TRACE, "child:%s->%s", curr->name, sub);
out:
GF_FREE (sub);
return 0;
}
static int
volume_end (void)
{
if (!curr->fops) {
gf_log ("parser", GF_LOG_ERROR,
"\"type\" not specified for volume %s", curr->name);
return -1;
}
gf_log ("parser", GF_LOG_TRACE, "end:%s", curr->name);
curr = NULL;
return 0;
}
int
yywrap ()
{
return 1;
}
int
yyerror (const char *str)
{
extern char *yytext;
extern int yylineno;
if (curr && curr->name && yytext) {
if (!strcmp (yytext, "volume")) {
gf_log ("parser", GF_LOG_ERROR,
"'end-volume' not defined for volume '%s'",
curr->name);
} else if (!strcmp (yytext, "type")) {
gf_log ("parser", GF_LOG_ERROR,
"line %d: duplicate 'type' defined for "
"volume '%s'",
yylineno, curr->name);
} else if (!strcmp (yytext, "subvolumes")) {
gf_log ("parser", GF_LOG_ERROR,
"line %d: duplicate 'subvolumes' defined for "
"volume '%s'",
yylineno, curr->name);
} else if (curr) {
gf_log ("parser", GF_LOG_ERROR,
"syntax error: line %d (volume '%s'): \"%s\""
"\nallowed tokens are 'volume', 'type', "
"'subvolumes', 'option', 'end-volume'()",
yylineno, curr->name,
yytext);
} else {
gf_log ("parser", GF_LOG_ERROR,
"syntax error: line %d (just after volume "
"'%s'): \"%s\"\n(%s)",
yylineno, curr->name,
yytext,
"allowed tokens are 'volume', 'type', "
"'subvolumes', 'option', 'end-volume'");
}
} else {
gf_log ("parser", GF_LOG_ERROR,
"syntax error in line %d: \"%s\" \n"
"(allowed tokens are 'volume', 'type', "
"'subvolumes', 'option', 'end-volume')\n",
yylineno, yytext);
}
return -1;
}
static int
execute_cmd (char *cmd, char **result, size_t size)
{
FILE *fpp = NULL;
int i = 0;
int status = 0;
int character = 0;
char *buf = *result;
fpp = popen (cmd, "r");
if (!fpp) {
gf_log ("parser", GF_LOG_ERROR, "%s: failed to popen", cmd);
return -1;
}
while ((character = fgetc (fpp)) != EOF) {
if (i == size) {
size *= 2;
buf = *result = GF_REALLOC (*result, size);
}
buf[i++] = character;
}
if (i > 0) {
i--;
buf[i] = '\0';
}
status = pclose (fpp);
if (status == -1 || !WIFEXITED (status) ||
((WEXITSTATUS (status)) != 0)) {
i = -1;
buf[0] = '\0';
}
return i;
}
static int
preprocess (FILE *srcfp, FILE *dstfp)
{
int ret = 0;
int i = 0;
char *cmd = NULL;
char *result = NULL;
size_t cmd_buf_size = GF_CMD_BUFFER_LEN;
char escaped = 0;
char in_backtick = 0;
int line = 1;
int column = 0;
int character = 0;
fseek (srcfp, 0L, SEEK_SET);
fseek (dstfp, 0L, SEEK_SET);
cmd = GF_CALLOC (cmd_buf_size, 1,
gf_common_mt_char);
if (cmd == NULL) {
gf_log ("parser", GF_LOG_ERROR, "Out of memory");
return -1;
}
result = GF_CALLOC (cmd_buf_size * 2, 1,
gf_common_mt_char);
if (result == NULL) {
GF_FREE (cmd);
gf_log ("parser", GF_LOG_ERROR, "Out of memory");
return -1;
}
while ((character = fgetc (srcfp)) != EOF) {
if ((character == '`') && !escaped) {
if (in_backtick) {
cmd[i] = '\0';
result[0] = '\0';
ret = execute_cmd (cmd, &result,
2 * cmd_buf_size);
if (ret < 0) {
ret = -1;
goto out;
}
fwrite (result, ret, 1, dstfp);
} else {
i = 0;
cmd[i] = '\0';
}
in_backtick = !in_backtick;
} else {
if (in_backtick) {
if (i == cmd_buf_size) {
cmd_buf_size *= 2;
cmd = GF_REALLOC (cmd, cmd_buf_size);
if (cmd == NULL) {
return -1;
}
result = GF_REALLOC (result,
2 * cmd_buf_size);
if (result == NULL) {
GF_FREE (cmd);
return -1;
}
}
cmd[i++] = character;
} else {
fputc (character, dstfp);
}
}
if (character == '\\') {
escaped = !escaped;
} else {
escaped = 0;
}
if (character == '\n') {
line++;
column = 0;
} else {
column++;
}
}
if (in_backtick) {
gf_log ("parser", GF_LOG_ERROR,
"Unterminated backtick in volume specfication file at line (%d), column (%d).",
line, column);
ret = -1;
}
out:
fseek (srcfp, 0L, SEEK_SET);
fseek (dstfp, 0L, SEEK_SET);
GF_FREE (cmd);
GF_FREE (result);
return ret;
}
extern FILE *yyin;
glusterfs_graph_t *
glusterfs_graph_new ()
{
glusterfs_graph_t *graph = NULL;
graph = GF_CALLOC (1, sizeof (*graph),
gf_common_mt_glusterfs_graph_t);
if (!graph)
return NULL;
INIT_LIST_HEAD (&graph->list);
gettimeofday (&graph->dob, NULL);
return graph;
}
glusterfs_graph_t *
glusterfs_graph_construct (FILE *fp)
{
int ret = 0;
glusterfs_graph_t *graph = NULL;
FILE *tmp_file = NULL;
graph = glusterfs_graph_new ();
if (!graph)
return NULL;
tmp_file = tmpfile ();
if (tmp_file == NULL) {
gf_log ("parser", GF_LOG_ERROR,
"cannot create temporary file");
glusterfs_graph_destroy (graph);
return NULL;
}
ret = preprocess (fp, tmp_file);
if (ret < 0) {
gf_log ("parser", GF_LOG_ERROR,
"parsing of backticks failed");
glusterfs_graph_destroy (graph);
fclose (tmp_file);
return NULL;
}
yyin = tmp_file;
construct = graph;
ret = yyparse ();
construct = NULL;
fclose (tmp_file);
if (ret == 1) {
gf_log ("parser", GF_LOG_DEBUG,
"parsing of volfile failed, please review it "
"once more");
glusterfs_graph_destroy (graph);
return NULL;
}
return graph;
}
|