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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
|
/*
Copyright (c) 2008-2013 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.
*/
#include "glusterfs/dict.h"
#include "glusterfs/statedump.h"
#include "glusterfs/client_t.h"
#include "glusterfs/list.h"
#include "glusterfs/libglusterfs-messages.h"
static int
gf_client_chain_client_entries(cliententry_t *entries, uint32_t startidx,
uint32_t endcount)
{
uint32_t i = 0;
if (!entries) {
gf_msg_callingfn("client_t", GF_LOG_WARNING, EINVAL, LG_MSG_INVALID_ARG,
"!entries");
return -1;
}
/* Chain only till the second to last entry because we want to
* ensure that the last entry has GF_CLIENTTABLE_END.
*/
for (i = startidx; i < (endcount - 1); i++)
entries[i].next_free = i + 1;
/* i has already been incremented up to the last entry. */
entries[i].next_free = GF_CLIENTTABLE_END;
return 0;
}
static int
gf_client_clienttable_expand(clienttable_t *clienttable, uint32_t nr)
{
cliententry_t *oldclients = NULL;
uint32_t oldmax_clients = -1;
int ret = -1;
if (clienttable == NULL || nr <= clienttable->max_clients) {
gf_msg_callingfn("client_t", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
"invalid argument");
ret = EINVAL;
goto out;
}
oldclients = clienttable->cliententries;
oldmax_clients = clienttable->max_clients;
clienttable->cliententries = GF_CALLOC(nr, sizeof(cliententry_t),
gf_common_mt_cliententry_t);
if (!clienttable->cliententries) {
clienttable->cliententries = oldclients;
ret = 0;
goto out;
}
clienttable->max_clients = nr;
if (oldclients) {
uint32_t cpy = oldmax_clients * sizeof(cliententry_t);
memcpy(clienttable->cliententries, oldclients, cpy);
}
gf_client_chain_client_entries(clienttable->cliententries, oldmax_clients,
clienttable->max_clients);
/* Now that expansion is done, we must update the client list
* head pointer so that the client allocation functions can continue
* using the expanded table.
*/
clienttable->first_free = oldmax_clients;
GF_FREE(oldclients);
ret = 0;
out:
return ret;
}
clienttable_t *
gf_clienttable_alloc(void)
{
clienttable_t *clienttable = NULL;
int result = 0;
clienttable = GF_CALLOC(1, sizeof(clienttable_t),
gf_common_mt_clienttable_t);
if (!clienttable)
return NULL;
LOCK_INIT(&clienttable->lock);
result = gf_client_clienttable_expand(clienttable,
GF_CLIENTTABLE_INITIAL_SIZE);
if (result != 0) {
gf_msg("client_t", GF_LOG_ERROR, 0, LG_MSG_EXPAND_CLIENT_TABLE_FAILED,
"gf_client_clienttable_expand failed");
GF_FREE(clienttable);
return NULL;
}
return clienttable;
}
/*
* Increments ref.bind if the client is already present or creates a new
* client with ref.bind = 1,ref.count = 1 it signifies that
* as long as ref.bind is > 0 client should be alive.
*/
client_t *
gf_client_get(xlator_t *this, client_auth_data_t *cred, char *client_uid,
char *subdir_mount)
{
client_t *client = NULL;
cliententry_t *cliententry = NULL;
clienttable_t *clienttable = NULL;
unsigned int i = 0;
unsigned int client_uid_len = 0;
if (this == NULL || client_uid == NULL) {
gf_msg_callingfn("client_t", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
"invalid argument");
errno = EINVAL;
return NULL;
}
clienttable = this->ctx->clienttable;
LOCK(&clienttable->lock);
{
for (; i < clienttable->max_clients; i++) {
client = clienttable->cliententries[i].client;
if (client == NULL)
continue;
/*
* look for matching client_uid, _and_
* if auth was used, matching auth flavour and data
*/
if (strcmp(client_uid, client->client_uid) == 0 &&
(cred->flavour && (cred->flavour == client->auth.flavour &&
(size_t)cred->datalen == client->auth.len &&
memcmp(cred->authdata, client->auth.data,
client->auth.len) == 0))) {
GF_ATOMIC_INC(client->bind);
goto unlock;
}
}
client_uid_len = strlen(client_uid) + 1;
client = GF_CALLOC(1, sizeof(client_t) + client_uid_len,
gf_common_mt_client_t);
if (client == NULL) {
errno = ENOMEM;
goto unlock;
}
GF_ATOMIC_INIT(client->bind, 1);
GF_ATOMIC_INIT(client->count, 1);
client->this = this;
client->tbl_index = clienttable->first_free;
client->auth.flavour = cred->flavour;
if (cred->flavour) {
client->auth.data = GF_MALLOC(cred->datalen, gf_common_mt_client_t);
if (client->auth.data == NULL) {
GF_FREE(client);
client = NULL;
errno = ENOMEM;
goto unlock;
}
memcpy(client->auth.data, cred->authdata, cred->datalen);
client->auth.len = cred->datalen;
}
if (subdir_mount != NULL)
client->subdir_mount = gf_strdup(subdir_mount);
GF_ATOMIC_INIT(client->fd_cnt, 0);
LOCK_INIT(&client->scratch_ctx_lock);
memcpy(client->client_uid, client_uid, client_uid_len);
cliententry = &clienttable->cliententries[clienttable->first_free];
if (cliententry->next_free == GF_CLIENTTABLE_END) {
int result = gf_client_clienttable_expand(
clienttable,
clienttable->max_clients + GF_CLIENTTABLE_INITIAL_SIZE);
if (result != 0) {
GF_FREE(client);
client = NULL;
errno = result;
goto unlock;
}
cliententry = &clienttable->cliententries[client->tbl_index];
cliententry->next_free = clienttable->first_free;
}
cliententry->client = client;
clienttable->first_free = cliententry->next_free;
cliententry->next_free = GF_CLIENTENTRY_ALLOCATED;
}
unlock:
UNLOCK(&clienttable->lock);
if (client)
gf_msg_callingfn("client_t", GF_LOG_DEBUG, 0, LG_MSG_BIND_REF,
"%s: bind_ref: %" GF_PRI_ATOMIC
", ref: "
"%" GF_PRI_ATOMIC,
client->client_uid, GF_ATOMIC_GET(client->bind),
GF_ATOMIC_GET(client->count));
return client;
}
void
gf_client_put(client_t *client, gf_boolean_t *detached)
{
gf_boolean_t unref = _gf_false;
int bind_ref;
if (client == NULL)
goto out;
if (detached)
*detached = _gf_false;
bind_ref = GF_ATOMIC_DEC(client->bind);
if (bind_ref == 0)
unref = _gf_true;
gf_msg_callingfn("client_t", GF_LOG_DEBUG, 0, LG_MSG_BIND_REF,
"%s: "
"bind_ref: %" GF_PRI_ATOMIC ", ref: %" GF_PRI_ATOMIC
", "
"unref: %d",
client->client_uid, GF_ATOMIC_GET(client->bind),
GF_ATOMIC_GET(client->count), unref);
if (unref) {
if (detached)
*detached = _gf_true;
gf_client_unref(client);
}
out:
return;
}
client_t *
gf_client_ref(client_t *client)
{
if (!client) {
gf_msg_callingfn("client_t", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
"null client");
return NULL;
}
GF_ATOMIC_INC(client->count);
gf_msg_callingfn("client_t", GF_LOG_DEBUG, 0, LG_MSG_REF_COUNT,
"%s: "
"ref-count %" GF_PRI_ATOMIC,
client->client_uid, GF_ATOMIC_GET(client->count));
return client;
}
static void
gf_client_destroy_recursive(xlator_t *xl, client_t *client)
{
xlator_list_t *trav;
if (!xl->call_cleanup && xl->cbks->client_destroy) {
xl->cbks->client_destroy(xl, client);
}
for (trav = xl->children; trav; trav = trav->next) {
gf_client_destroy_recursive(trav->xlator, client);
}
}
static void
client_destroy(client_t *client)
{
clienttable_t *clienttable = NULL;
glusterfs_graph_t *gtrav = NULL;
if (client == NULL) {
gf_msg_callingfn("xlator", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
"invalid argument");
goto out;
}
clienttable = client->this->ctx->clienttable;
LOCK(&clienttable->lock);
{
clienttable->cliententries[client->tbl_index].client = NULL;
clienttable->cliententries[client->tbl_index]
.next_free = clienttable->first_free;
clienttable->first_free = client->tbl_index;
}
UNLOCK(&clienttable->lock);
list_for_each_entry(gtrav, &client->this->ctx->graphs, list)
{
gf_client_destroy_recursive(gtrav->top, client);
}
if (client->subdir_inode)
inode_unref(client->subdir_inode);
LOCK_DESTROY(&client->scratch_ctx_lock);
GF_FREE(client->auth.data);
GF_FREE(client->auth.username);
GF_FREE(client->auth.passwd);
GF_FREE(client->subdir_mount);
GF_FREE(client->client_name);
GF_FREE(client);
out:
return;
}
static int
gf_client_disconnect_recursive(xlator_t *xl, client_t *client)
{
int ret = 0;
xlator_list_t *trav;
if (!xl->call_cleanup && xl->cbks->client_disconnect) {
ret = xl->cbks->client_disconnect(xl, client);
}
for (trav = xl->children; trav; trav = trav->next) {
ret |= gf_client_disconnect_recursive(trav->xlator, client);
}
return ret;
}
int
gf_client_disconnect(client_t *client)
{
int ret = 0;
glusterfs_graph_t *gtrav = NULL;
list_for_each_entry(gtrav, &client->this->ctx->graphs, list)
{
ret |= gf_client_disconnect_recursive(gtrav->top, client);
}
return ret;
}
void
gf_client_unref(client_t *client)
{
uint64_t refcount;
if (!client) {
gf_msg_callingfn("client_t", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
"client is NULL");
return;
}
refcount = GF_ATOMIC_DEC(client->count);
gf_msg_callingfn("client_t", GF_LOG_DEBUG, 0, LG_MSG_REF_COUNT,
"%s: "
"ref-count %" GF_PRI_ATOMIC,
client->client_uid, refcount);
if (refcount == 0) {
gf_msg(THIS->name, GF_LOG_INFO, 0, LG_MSG_DISCONNECT_CLIENT,
"Shutting down connection %s", client->client_uid);
client_destroy(client);
}
}
/* value (assumed to be passed NULL) is returned if the key was found.
ret value >= 0 - the location it is found (value is non NULL)
or the 1st free slot (in case value is NULL)
or -1 in case no free slots found
*/
static int
__client_ctx_get_int(client_t *client, void *key, void **value)
{
int index = 0;
int set_idx = -1;
for (index = 0; index < GF_CLIENTCTX_INITIAL_SIZE; index++) {
if (!client->scratch_ctx[index].ctx_key) {
if (set_idx == -1)
set_idx = index;
/* don't break, check if key already exists
further on */
} else if (client->scratch_ctx[index].ctx_key == key) {
*value = client->scratch_ctx[index].ctx_value;
return index;
}
}
// if the value was not found, return -1 or the 1st free slot.
return set_idx;
}
/*will return success with old value if exist*/
void *
client_ctx_set(client_t *client, void *key, void *value)
{
int ret = 0;
void *ret_value = NULL;
if (!client || !key || !value)
return NULL;
LOCK(&client->scratch_ctx_lock);
{
ret = __client_ctx_get_int(client, key, &ret_value);
if (!ret_value && ret >= 0) {
// key was not found and ret is the location of the 1st free slot
client->scratch_ctx[ret].ctx_key = key;
client->scratch_ctx[ret].ctx_value = value;
ret_value = value;
}
}
UNLOCK(&client->scratch_ctx_lock);
return ret_value;
}
void *
client_ctx_get(client_t *client, void *key)
{
void *ret_value = NULL;
if (!client || !key)
return NULL;
LOCK(&client->scratch_ctx_lock);
{
(void)__client_ctx_get_int(client, key, &ret_value);
}
UNLOCK(&client->scratch_ctx_lock);
return ret_value;
}
void *
client_ctx_del(client_t *client, void *key)
{
int ret = 0;
void *ret_value = NULL;
if (!client || !key)
goto out;
LOCK(&client->scratch_ctx_lock);
{
ret = __client_ctx_get_int(client, key, &ret_value);
if (ret_value) {
client->scratch_ctx[ret].ctx_key = 0;
client->scratch_ctx[ret].ctx_value = 0;
}
}
UNLOCK(&client->scratch_ctx_lock);
out:
return ret_value;
}
void
client_ctx_dump(client_t *client, char *prefix)
{
#if 0 /* TBD, FIXME */
struct client_ctx *client_ctx = NULL;
xlator_t *xl = NULL;
int i = 0;
if ((client == NULL) || (client->ctx == NULL)) {
goto out;
}
LOCK (&client->ctx_lock);
if (client->ctx != NULL) {
client_ctx = GF_CALLOC (client->inode->table->xl->graph->ctx_count,
sizeof (*client_ctx),
gf_common_mt_client_ctx);
if (client_ctx == NULL) {
goto unlock;
}
for (i = 0; i < client->inode->table->xl->graph->ctx_count; i++) {
client_ctx[i] = client->ctx[i];
}
}
unlock:
UNLOCK (&client->ctx_lock);
if (client_ctx == NULL) {
goto out;
}
for (i = 0; i < client->inode->table->xl->graph->ctx_count; i++) {
if (client_ctx[i].xl_key) {
xl = (xlator_t *)(long)client_ctx[i].xl_key;
if (xl->dumpops && xl->dumpops->clientctx)
xl->dumpops->clientctx (xl, client);
}
}
out:
GF_FREE (client_ctx);
#endif
}
/*
* the following functions are here to preserve legacy behavior of the
* protocol/server xlator dump, but perhaps they should just be folded
* into the client dump instead?
*/
int
gf_client_dump_fdtables_to_dict(xlator_t *this, dict_t *dict)
{
clienttable_t *clienttable = NULL;
int count = 0;
int ret = -1;
#ifdef NOTYET
client_t *client = NULL;
char key[GF_DUMP_MAX_BUF_LEN] = {
0,
};
#endif
GF_VALIDATE_OR_GOTO(THIS->name, this, out);
GF_VALIDATE_OR_GOTO(this->name, dict, out);
clienttable = this->ctx->clienttable;
if (!clienttable)
return -1;
#ifdef NOTYET
ret = TRY_LOCK(&clienttable->lock);
{
if (ret) {
gf_msg("client_t", GF_LOG_WARNING, 0, LG_MSG_LOCK_FAILED,
"Unable to acquire lock");
return -1;
}
for (; count < clienttable->max_clients; count++) {
if (GF_CLIENTENTRY_ALLOCATED !=
clienttable->cliententries[count].next_free)
continue;
client = clienttable->cliententries[count].client;
if (client->bound_xl &&
!strcmp(client->bound_xl->name, this->name)) {
snprintf(key, sizeof(key), "conn%d", count++);
fdtable_dump_to_dict(client->server_ctx.fdtable, key, dict);
}
}
}
UNLOCK(&clienttable->lock);
#endif
ret = dict_set_int32(dict, "conncount", count);
out:
return ret;
}
int
gf_client_dump_fdtables(xlator_t *this)
{
client_t *client = NULL;
clienttable_t *clienttable = NULL;
int count = 1;
int ret = -1;
char key[GF_DUMP_MAX_BUF_LEN] = {
0,
};
GF_VALIDATE_OR_GOTO(THIS->name, this, out);
clienttable = this->ctx->clienttable;
if (!clienttable)
return -1;
ret = TRY_LOCK(&clienttable->lock);
{
if (ret) {
gf_msg("client_t", GF_LOG_WARNING, 0, LG_MSG_LOCK_FAILED,
"Unable to acquire lock");
return -1;
}
for (; count < clienttable->max_clients; count++) {
if (GF_CLIENTENTRY_ALLOCATED !=
clienttable->cliententries[count].next_free)
continue;
client = clienttable->cliententries[count].client;
if (client->client_uid) {
gf_proc_dump_build_key(key, "conn", "%d.id", count);
gf_proc_dump_write(key, "%s", client->client_uid);
}
if (client->subdir_mount) {
gf_proc_dump_build_key(key, "conn", "%d.subdir", count);
gf_proc_dump_write(key, "%s", client->subdir_mount);
}
gf_proc_dump_build_key(key, "conn", "%d.ref", count);
gf_proc_dump_write(key, "%" GF_PRI_ATOMIC,
GF_ATOMIC_GET(client->count));
if (client->bound_xl) {
gf_proc_dump_build_key(key, "conn", "%d.bound_xl", count);
gf_proc_dump_write(key, "%s", client->bound_xl->name);
}
#ifdef NOTYET
gf_proc_dump_build_key(key, "conn", "%d.id", count);
fdtable_dump(client->server_ctx.fdtable, key);
#endif
}
}
UNLOCK(&clienttable->lock);
ret = 0;
out:
return ret;
}
int
gf_client_dump_inodes_to_dict(xlator_t *this, dict_t *dict)
{
client_t *client = NULL;
clienttable_t *clienttable = NULL;
xlator_t *prev_bound_xl = NULL;
char key[32] = {
0,
};
int count = 0;
int ret = -1;
GF_VALIDATE_OR_GOTO(THIS->name, this, out);
GF_VALIDATE_OR_GOTO(this->name, dict, out);
clienttable = this->ctx->clienttable;
if (!clienttable)
return -1;
ret = LOCK(&clienttable->lock);
{
if (ret) {
gf_msg("client_t", GF_LOG_WARNING, 0, LG_MSG_LOCK_FAILED,
"Unable to acquire lock");
return -1;
}
for (; count < clienttable->max_clients; count++) {
if (GF_CLIENTENTRY_ALLOCATED !=
clienttable->cliententries[count].next_free)
continue;
client = clienttable->cliententries[count].client;
if (client->bound_xl &&
!strcmp(client->bound_xl->name, this->name)) {
if (client->bound_xl->itable) {
/* Presently every brick contains only
* one bound_xl for all connections.
* This will lead to duplicating of
* the inode lists, if listing is
* done for every connection. This
* simple check prevents duplication
* in the present case. If need arises
* the check can be improved.
*/
if (client->bound_xl == prev_bound_xl)
continue;
prev_bound_xl = client->bound_xl;
snprintf(key, sizeof(key), "conn%d", count);
inode_table_dump_to_dict(client->bound_xl->itable, key,
dict);
}
}
}
}
UNLOCK(&clienttable->lock);
ret = dict_set_int32(dict, "conncount", count);
out:
if (prev_bound_xl)
prev_bound_xl = NULL;
return ret;
}
int
gf_client_dump_inodes(xlator_t *this)
{
client_t *client = NULL;
clienttable_t *clienttable = NULL;
xlator_t *prev_bound_xl = NULL;
int count = 0;
int ret = -1;
char key[GF_DUMP_MAX_BUF_LEN] = {
0,
};
GF_VALIDATE_OR_GOTO(THIS->name, this, out);
clienttable = this->ctx->clienttable;
if (!clienttable)
goto out;
ret = TRY_LOCK(&clienttable->lock);
{
if (ret) {
gf_msg("client_t", GF_LOG_WARNING, 0, LG_MSG_LOCK_FAILED,
"Unable to acquire lock");
goto out;
}
for (; count < clienttable->max_clients; count++) {
if (GF_CLIENTENTRY_ALLOCATED !=
clienttable->cliententries[count].next_free)
continue;
client = clienttable->cliententries[count].client;
if (client->bound_xl && client->bound_xl->itable) {
/* Presently every brick contains only
* one bound_xl for all connections.
* This will lead to duplicating of
* the inode lists, if listing is
* done for every connection. This
* simple check prevents duplication
* in the present case. If need arises
* the check can be improved.
*/
if (client->bound_xl == prev_bound_xl)
continue;
prev_bound_xl = client->bound_xl;
gf_proc_dump_build_key(key, "conn", "%d.bound_xl.%s", count,
client->bound_xl->name);
inode_table_dump(client->bound_xl->itable, key);
}
}
}
UNLOCK(&clienttable->lock);
ret = 0;
out:
return ret;
}
|