/* REF ARRAY Implementation of the dynamic array with reference count. Copyright (C) Dmitri Pal 2009 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #define _GNU_SOURCE #include /* for errors */ #include #include #include #include #include "ref_array.h" #include "config.h" #include "trace.h" /* The structure used in referenced array */ struct ref_array { void *storage; /* The storage buffer */ size_t elsize; /* Size of one element in the buffer */ uint32_t size; /* Size of the storage in items */ uint32_t grow_by; /* What increment use to reallocate memory */ uint32_t len; /* Number of the elements in the array */ uint32_t refcount; /* Reference count */ ref_array_fn cb; /* Cleanup callback */ void *cb_data; /* Caller's callback data */ }; /****************************************************/ /* INTERNAL FUNCTIONS */ /****************************************************/ static int ref_array_grow(struct ref_array *ra) { int error = EOK; void *newbuf = NULL; TRACE_FLOW_STRING("ref_array_grow", "Entry"); TRACE_INFO_NUMBER("Current length: ", ra->len); TRACE_INFO_NUMBER("Current size: ", ra->size); /* Grow buffer if needed */ newbuf = realloc(ra->storage, (ra->size + ra->grow_by) * ra->elsize); if (newbuf == NULL) { TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM); return ENOMEM; } ra->storage = newbuf; ra->size += ra->grow_by; TRACE_INFO_NUMBER("Final size: ", ra->size); TRACE_FLOW_NUMBER("elapi_grow_data. Exit. Returning", error); return error; } /****************************************************/ /* PUBLIC FUNCTIONS */ /****************************************************/ /* Create referenced array */ int ref_array_create(struct ref_array **ra, size_t elemsz, uint32_t grow_by, ref_array_fn cb, void *data) { struct ref_array *new_ra = NULL; TRACE_FLOW_STRING("ref_array_create", "Entry"); if (!ra) { TRACE_ERROR_NUMBER("Uninitialized argument.", EINVAL); return EINVAL; } if ((!elemsz) || (!grow_by)) { TRACE_ERROR_NUMBER("Invalid argument.", EINVAL); return EINVAL; } new_ra = (struct ref_array *)malloc(sizeof(struct ref_array)); if (!new_ra) { TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM); return ENOMEM; } new_ra->storage = NULL; new_ra->elsize = elemsz; new_ra->size = 0; new_ra->grow_by = grow_by; new_ra->len = 0; new_ra->refcount = 1; new_ra->cb = cb; new_ra->cb_data = data; *ra = new_ra; TRACE_FLOW_STRING("ref_array_create", "Exit"); return EOK; } /* Get new reference to an array */ struct ref_array *ref_array_getref(struct ref_array *ra) { TRACE_FLOW_STRING("ref_array_getref", "Entry"); /* Check if array is not NULL */ if (ra) { TRACE_INFO_NUMBER("Increasing reference count. Current: ", ra->refcount); /* Increase reference count */ ra->refcount++; TRACE_INFO_NUMBER("Increased reference count. New: ", ra->refcount); } else { TRACE_ERROR_STRING("Uninitialized array.", "Returning NULL"); } TRACE_FLOW_STRING("ref_array_getref", "Exit"); return ra; } /* Delete the array */ void ref_array_destroy(struct ref_array *ra) { int idx; TRACE_FLOW_STRING("ref_array_destroy", "Entry"); /* Check if array is not NULL */ if (!ra) { TRACE_ERROR_STRING("Uninitialized array.", "Coding error???"); return; } TRACE_INFO_NUMBER("Current reference count: ", ra->refcount); if (ra->refcount) { /* Decrease reference count */ ra->refcount--; if (ra->refcount == 0) { TRACE_INFO_NUMBER("It is time to delete array. Count:", ra->refcount); if (ra->cb) { for (idx = 0; idx < ra->len; idx++) { ra->cb((unsigned char *)(ra->storage) + idx * ra->elsize, REF_ARRAY_DESTROY, ra->cb_data); } } free(ra->storage); free(ra); } } else { /* Should never be here... * This can happen if the caller by mistake would try to * destroy the object from within the callback. Brrr.... */ TRACE_ERROR_STRING("Reference count is 0.", "Coding error???"); } TRACE_FLOW_STRING("ref_array_destroy", "Exit"); } /* Add new element to the array */ int ref_array_append(struct ref_array *ra, void *element) { int error = EOK; TRACE_FLOW_STRING("ref_array_append", "Entry"); if ((!ra) || (!element)) { TRACE_ERROR_NUMBER("Uninitialized argument.", EINVAL); return EINVAL; } /* Do we have enough room for a new element? */ if (ra->size == ra->len) { error = ref_array_grow(ra); if (error) { TRACE_ERROR_NUMBER("Failed to grow array.", error); return EINVAL; } } /* Copy element */ memcpy((unsigned char *)(ra->storage) + ra->len * ra->elsize, element, ra->elsize); ra->len++; TRACE_FLOW_STRING("ref_array_append", "Exit"); return error; } /* Get element */ void *ref_array_get(struct ref_array *ra, uint32_t idx, void *acptr) { TRACE_FLOW_STRING("ref_array_get", "Entry"); if (!ra) { TRACE_ERROR_STRING("Uninitialized argument.", ""); return NULL; } if (idx >= ra->len) { TRACE_ERROR_NUMBER("Invalid idx.", idx); return NULL; } TRACE_INFO_NUMBER("Index: ", idx); if (acptr) { TRACE_INFO_STRING("Copying data.", ""); memcpy(acptr, (unsigned char *)(ra->storage) + idx * ra->elsize, ra->elsize); } TRACE_FLOW_STRING("ref_array_get returning internal storage", "Exit"); return (unsigned char *)(ra->storage) + idx * ra->elsize; } /* Get length */ int ref_array_getlen(struct ref_array *ra, uint32_t *len) { TRACE_FLOW_STRING("ref_array_getlen", "Entry"); if ((!ra) || (!len)) { TRACE_ERROR_STRING("Uninitialized argument.", ""); return EINVAL; } *len = ra->len; TRACE_FLOW_STRING("ref_array_getlen", "Exit"); return EOK; } /* Alternative function to get length */ uint32_t ref_array_len(struct ref_array *ra) { TRACE_FLOW_STRING("ref_array_len", "Entry"); if (!ra) { TRACE_ERROR_STRING("Uninitialized argument.", ""); errno = EINVAL; return 0; } TRACE_FLOW_STRING("ref_array_len", "Exit"); return ra->len; } /* Debug function */ void ref_array_debug(struct ref_array *ra) { int i,j; printf("\nARRAY DUMP START\n"); printf("Length = %u\n", ra->len); printf("Size = %u\n", ra->size); printf("Element = %u\n", (unsigned int)(ra->elsize)); printf("Grow by = %u\n", ra->grow_by); printf("Count = %u\n", ra->refcount); printf("ARRAY:\n"); for (i = 0; i < ra->len; i++) { for (j = 0; j < ra->elsize; j++) { printf("%x", *((unsigned char *)(ra->storage) + i * ra->elsize + j)); } printf("\n%s\n", *((char **)((unsigned char *)(ra->storage) + i * ra->elsize))); } printf("\nARRAY DUMP END\n\n"); } >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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
/*
   Copyright (c) 2006-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>
#include <pthread.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 graphyyerror (const char *);
int graphyylex ();
%}


%%
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 graphyylineno;

        gf_log ("parser", GF_LOG_ERROR,
                "Volume %s, before line %d: Please specify volume type",
                curr->name, graphyylineno);
        return;
}


static void
sub_error (void)
{
        extern int graphyylineno;

        gf_log ("parser", GF_LOG_ERROR,
                "Volume %s, before line %d: Please specify subvolumes",
                curr->name, graphyylineno);
        return;
}


static void
option_error (void)
{
        extern int graphyylineno;

        gf_log ("parser", GF_LOG_ERROR,
                "Volume %s, before line %d: Please specify "
                "option <key> <value>",
                curr->name, graphyylineno);
        return;
}


static int
new_volume (char *name)
{
        extern int   graphyylineno;
        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, graphyylineno);
                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",
                                graphyylineno, 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   graphyylineno;
        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, graphyylineno, 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  graphyylineno;
        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, graphyylineno, 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       graphyylineno;
        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, graphyylineno, 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, graphyylineno, 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
graphyywrap ()
{
        return 1;
}


int
graphyyerror (const char *str)
{
        extern char  *graphyytext;
        extern int    graphyylineno;

        if (curr && curr->name && graphyytext) {
                if (!strcmp (graphyytext, "volume")) {
                        gf_log ("parser", GF_LOG_ERROR,
                                "'end-volume' not defined for volume '%s'",
				curr->name);
                } else if (!strcmp (graphyytext, "type")) {
                        gf_log ("parser", GF_LOG_ERROR,
                                "line %d: duplicate 'type' defined for "
				"volume '%s'",
                                graphyylineno, curr->name);
                } else if (!strcmp (graphyytext, "subvolumes")) {
                        gf_log ("parser", GF_LOG_ERROR,
                                "line %d: duplicate 'subvolumes' defined for "
				"volume '%s'",
                                graphyylineno, 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'()",
                                graphyylineno, curr->name,
				graphyytext);
                } else {
                        gf_log ("parser", GF_LOG_ERROR,
                                "syntax error: line %d (just after volume "
				"'%s'): \"%s\"\n(%s)",
                                graphyylineno, curr->name,
				graphyytext,
                                "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",
                        graphyylineno, graphyytext);
        }

        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;