primary.c revision 211881
1/*- 2 * Copyright (c) 2009 The FreeBSD Foundation 3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4 * All rights reserved. 5 * 6 * This software was developed by Pawel Jakub Dawidek under sponsorship from 7 * the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 211881 2010-08-27 14:12:53Z pjd $"); 33 34#include <sys/types.h> 35#include <sys/time.h> 36#include <sys/bio.h> 37#include <sys/disk.h> 38#include <sys/refcount.h> 39#include <sys/stat.h> 40 41#include <geom/gate/g_gate.h> 42 43#include <assert.h> 44#include <err.h> 45#include <errno.h> 46#include <fcntl.h> 47#include <libgeom.h> 48#include <pthread.h> 49#include <stdint.h> 50#include <stdio.h> 51#include <string.h> 52#include <sysexits.h> 53#include <unistd.h> 54 55#include <activemap.h> 56#include <nv.h> 57#include <rangelock.h> 58 59#include "control.h" 60#include "hast.h" 61#include "hast_proto.h" 62#include "hastd.h" 63#include "metadata.h" 64#include "proto.h" 65#include "pjdlog.h" 66#include "subr.h" 67#include "synch.h" 68 69/* The is only one remote component for now. */ 70#define ISREMOTE(no) ((no) == 1) 71 72struct hio { 73 /* 74 * Number of components we are still waiting for. 75 * When this field goes to 0, we can send the request back to the 76 * kernel. Each component has to decrease this counter by one 77 * even on failure. 78 */ 79 unsigned int hio_countdown; 80 /* 81 * Each component has a place to store its own error. 82 * Once the request is handled by all components we can decide if the 83 * request overall is successful or not. 84 */ 85 int *hio_errors; 86 /* 87 * Structure used to comunicate with GEOM Gate class. 88 */ 89 struct g_gate_ctl_io hio_ggio; 90 TAILQ_ENTRY(hio) *hio_next; 91}; 92#define hio_free_next hio_next[0] 93#define hio_done_next hio_next[0] 94 95/* 96 * Free list holds unused structures. When free list is empty, we have to wait 97 * until some in-progress requests are freed. 98 */ 99static TAILQ_HEAD(, hio) hio_free_list; 100static pthread_mutex_t hio_free_list_lock; 101static pthread_cond_t hio_free_list_cond; 102/* 103 * There is one send list for every component. One requests is placed on all 104 * send lists - each component gets the same request, but each component is 105 * responsible for managing his own send list. 106 */ 107static TAILQ_HEAD(, hio) *hio_send_list; 108static pthread_mutex_t *hio_send_list_lock; 109static pthread_cond_t *hio_send_list_cond; 110/* 111 * There is one recv list for every component, although local components don't 112 * use recv lists as local requests are done synchronously. 113 */ 114static TAILQ_HEAD(, hio) *hio_recv_list; 115static pthread_mutex_t *hio_recv_list_lock; 116static pthread_cond_t *hio_recv_list_cond; 117/* 118 * Request is placed on done list by the slowest component (the one that 119 * decreased hio_countdown from 1 to 0). 120 */ 121static TAILQ_HEAD(, hio) hio_done_list; 122static pthread_mutex_t hio_done_list_lock; 123static pthread_cond_t hio_done_list_cond; 124/* 125 * Structure below are for interaction with sync thread. 126 */ 127static bool sync_inprogress; 128static pthread_mutex_t sync_lock; 129static pthread_cond_t sync_cond; 130/* 131 * The lock below allows to synchornize access to remote connections. 132 */ 133static pthread_rwlock_t *hio_remote_lock; 134static pthread_mutex_t hio_guard_lock; 135static pthread_cond_t hio_guard_cond; 136 137/* 138 * Lock to synchronize metadata updates. Also synchronize access to 139 * hr_primary_localcnt and hr_primary_remotecnt fields. 140 */ 141static pthread_mutex_t metadata_lock; 142 143/* 144 * Maximum number of outstanding I/O requests. 145 */ 146#define HAST_HIO_MAX 256 147/* 148 * Number of components. At this point there are only two components: local 149 * and remote, but in the future it might be possible to use multiple local 150 * and remote components. 151 */ 152#define HAST_NCOMPONENTS 2 153/* 154 * Number of seconds to sleep before next reconnect try. 155 */ 156#define RECONNECT_SLEEP 5 157 158#define ISCONNECTED(res, no) \ 159 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL) 160 161#define QUEUE_INSERT1(hio, name, ncomp) do { \ 162 bool _wakeup; \ 163 \ 164 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 165 _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \ 166 TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \ 167 hio_next[(ncomp)]); \ 168 mtx_unlock(&hio_##name##_list_lock[ncomp]); \ 169 if (_wakeup) \ 170 cv_signal(&hio_##name##_list_cond[(ncomp)]); \ 171} while (0) 172#define QUEUE_INSERT2(hio, name) do { \ 173 bool _wakeup; \ 174 \ 175 mtx_lock(&hio_##name##_list_lock); \ 176 _wakeup = TAILQ_EMPTY(&hio_##name##_list); \ 177 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\ 178 mtx_unlock(&hio_##name##_list_lock); \ 179 if (_wakeup) \ 180 cv_signal(&hio_##name##_list_cond); \ 181} while (0) 182#define QUEUE_TAKE1(hio, name, ncomp) do { \ 183 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 184 while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL) { \ 185 cv_wait(&hio_##name##_list_cond[(ncomp)], \ 186 &hio_##name##_list_lock[(ncomp)]); \ 187 } \ 188 TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \ 189 hio_next[(ncomp)]); \ 190 mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \ 191} while (0) 192#define QUEUE_TAKE2(hio, name) do { \ 193 mtx_lock(&hio_##name##_list_lock); \ 194 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 195 cv_wait(&hio_##name##_list_cond, \ 196 &hio_##name##_list_lock); \ 197 } \ 198 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \ 199 mtx_unlock(&hio_##name##_list_lock); \ 200} while (0) 201 202#define SYNCREQ(hio) do { \ 203 (hio)->hio_ggio.gctl_unit = -1; \ 204 (hio)->hio_ggio.gctl_seq = 1; \ 205} while (0) 206#define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1) 207#define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0) 208#define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2) 209 210static struct hast_resource *gres; 211 212static pthread_mutex_t range_lock; 213static struct rangelocks *range_regular; 214static bool range_regular_wait; 215static pthread_cond_t range_regular_cond; 216static struct rangelocks *range_sync; 217static bool range_sync_wait; 218static pthread_cond_t range_sync_cond; 219 220static void *ggate_recv_thread(void *arg); 221static void *local_send_thread(void *arg); 222static void *remote_send_thread(void *arg); 223static void *remote_recv_thread(void *arg); 224static void *ggate_send_thread(void *arg); 225static void *sync_thread(void *arg); 226static void *guard_thread(void *arg); 227 228static void sighandler(int sig); 229 230static void 231cleanup(struct hast_resource *res) 232{ 233 int rerrno; 234 235 /* Remember errno. */ 236 rerrno = errno; 237 238 /* 239 * Close descriptor to /dev/hast/<name> 240 * to work-around race in the kernel. 241 */ 242 close(res->hr_localfd); 243 244 /* Destroy ggate provider if we created one. */ 245 if (res->hr_ggateunit >= 0) { 246 struct g_gate_ctl_destroy ggiod; 247 248 ggiod.gctl_version = G_GATE_VERSION; 249 ggiod.gctl_unit = res->hr_ggateunit; 250 ggiod.gctl_force = 1; 251 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) { 252 pjdlog_warning("Unable to destroy hast/%s device", 253 res->hr_provname); 254 } 255 res->hr_ggateunit = -1; 256 } 257 258 /* Restore errno. */ 259 errno = rerrno; 260} 261 262static void 263primary_exit(int exitcode, const char *fmt, ...) 264{ 265 va_list ap; 266 267 assert(exitcode != EX_OK); 268 va_start(ap, fmt); 269 pjdlogv_errno(LOG_ERR, fmt, ap); 270 va_end(ap); 271 cleanup(gres); 272 exit(exitcode); 273} 274 275static void 276primary_exitx(int exitcode, const char *fmt, ...) 277{ 278 va_list ap; 279 280 va_start(ap, fmt); 281 pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap); 282 va_end(ap); 283 cleanup(gres); 284 exit(exitcode); 285} 286 287static int 288hast_activemap_flush(struct hast_resource *res) 289{ 290 const unsigned char *buf; 291 size_t size; 292 293 buf = activemap_bitmap(res->hr_amp, &size); 294 assert(buf != NULL); 295 assert((size % res->hr_local_sectorsize) == 0); 296 if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) != 297 (ssize_t)size) { 298 KEEP_ERRNO(pjdlog_errno(LOG_ERR, 299 "Unable to flush activemap to disk")); 300 return (-1); 301 } 302 return (0); 303} 304 305static bool 306real_remote(const struct hast_resource *res) 307{ 308 309 return (strcmp(res->hr_remoteaddr, "none") != 0); 310} 311 312static void 313init_environment(struct hast_resource *res __unused) 314{ 315 struct hio *hio; 316 unsigned int ii, ncomps; 317 318 /* 319 * In the future it might be per-resource value. 320 */ 321 ncomps = HAST_NCOMPONENTS; 322 323 /* 324 * Allocate memory needed by lists. 325 */ 326 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps); 327 if (hio_send_list == NULL) { 328 primary_exitx(EX_TEMPFAIL, 329 "Unable to allocate %zu bytes of memory for send lists.", 330 sizeof(hio_send_list[0]) * ncomps); 331 } 332 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps); 333 if (hio_send_list_lock == NULL) { 334 primary_exitx(EX_TEMPFAIL, 335 "Unable to allocate %zu bytes of memory for send list locks.", 336 sizeof(hio_send_list_lock[0]) * ncomps); 337 } 338 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps); 339 if (hio_send_list_cond == NULL) { 340 primary_exitx(EX_TEMPFAIL, 341 "Unable to allocate %zu bytes of memory for send list condition variables.", 342 sizeof(hio_send_list_cond[0]) * ncomps); 343 } 344 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps); 345 if (hio_recv_list == NULL) { 346 primary_exitx(EX_TEMPFAIL, 347 "Unable to allocate %zu bytes of memory for recv lists.", 348 sizeof(hio_recv_list[0]) * ncomps); 349 } 350 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps); 351 if (hio_recv_list_lock == NULL) { 352 primary_exitx(EX_TEMPFAIL, 353 "Unable to allocate %zu bytes of memory for recv list locks.", 354 sizeof(hio_recv_list_lock[0]) * ncomps); 355 } 356 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps); 357 if (hio_recv_list_cond == NULL) { 358 primary_exitx(EX_TEMPFAIL, 359 "Unable to allocate %zu bytes of memory for recv list condition variables.", 360 sizeof(hio_recv_list_cond[0]) * ncomps); 361 } 362 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps); 363 if (hio_remote_lock == NULL) { 364 primary_exitx(EX_TEMPFAIL, 365 "Unable to allocate %zu bytes of memory for remote connections locks.", 366 sizeof(hio_remote_lock[0]) * ncomps); 367 } 368 369 /* 370 * Initialize lists, their locks and theirs condition variables. 371 */ 372 TAILQ_INIT(&hio_free_list); 373 mtx_init(&hio_free_list_lock); 374 cv_init(&hio_free_list_cond); 375 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) { 376 TAILQ_INIT(&hio_send_list[ii]); 377 mtx_init(&hio_send_list_lock[ii]); 378 cv_init(&hio_send_list_cond[ii]); 379 TAILQ_INIT(&hio_recv_list[ii]); 380 mtx_init(&hio_recv_list_lock[ii]); 381 cv_init(&hio_recv_list_cond[ii]); 382 rw_init(&hio_remote_lock[ii]); 383 } 384 TAILQ_INIT(&hio_done_list); 385 mtx_init(&hio_done_list_lock); 386 cv_init(&hio_done_list_cond); 387 mtx_init(&hio_guard_lock); 388 cv_init(&hio_guard_cond); 389 mtx_init(&metadata_lock); 390 391 /* 392 * Allocate requests pool and initialize requests. 393 */ 394 for (ii = 0; ii < HAST_HIO_MAX; ii++) { 395 hio = malloc(sizeof(*hio)); 396 if (hio == NULL) { 397 primary_exitx(EX_TEMPFAIL, 398 "Unable to allocate %zu bytes of memory for hio request.", 399 sizeof(*hio)); 400 } 401 hio->hio_countdown = 0; 402 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps); 403 if (hio->hio_errors == NULL) { 404 primary_exitx(EX_TEMPFAIL, 405 "Unable allocate %zu bytes of memory for hio errors.", 406 sizeof(hio->hio_errors[0]) * ncomps); 407 } 408 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps); 409 if (hio->hio_next == NULL) { 410 primary_exitx(EX_TEMPFAIL, 411 "Unable allocate %zu bytes of memory for hio_next field.", 412 sizeof(hio->hio_next[0]) * ncomps); 413 } 414 hio->hio_ggio.gctl_version = G_GATE_VERSION; 415 hio->hio_ggio.gctl_data = malloc(MAXPHYS); 416 if (hio->hio_ggio.gctl_data == NULL) { 417 primary_exitx(EX_TEMPFAIL, 418 "Unable to allocate %zu bytes of memory for gctl_data.", 419 MAXPHYS); 420 } 421 hio->hio_ggio.gctl_length = MAXPHYS; 422 hio->hio_ggio.gctl_error = 0; 423 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next); 424 } 425 426 /* 427 * Turn on signals handling. 428 */ 429 signal(SIGINT, sighandler); 430 signal(SIGTERM, sighandler); 431 signal(SIGHUP, sighandler); 432} 433 434static void 435init_local(struct hast_resource *res) 436{ 437 unsigned char *buf; 438 size_t mapsize; 439 440 if (metadata_read(res, true) < 0) 441 exit(EX_NOINPUT); 442 mtx_init(&res->hr_amp_lock); 443 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 444 res->hr_local_sectorsize, res->hr_keepdirty) < 0) { 445 primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 446 } 447 mtx_init(&range_lock); 448 cv_init(&range_regular_cond); 449 if (rangelock_init(&range_regular) < 0) 450 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 451 cv_init(&range_sync_cond); 452 if (rangelock_init(&range_sync) < 0) 453 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock"); 454 mapsize = activemap_ondisk_size(res->hr_amp); 455 buf = calloc(1, mapsize); 456 if (buf == NULL) { 457 primary_exitx(EX_TEMPFAIL, 458 "Unable to allocate buffer for activemap."); 459 } 460 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 461 (ssize_t)mapsize) { 462 primary_exit(EX_NOINPUT, "Unable to read activemap"); 463 } 464 activemap_copyin(res->hr_amp, buf, mapsize); 465 free(buf); 466 if (res->hr_resuid != 0) 467 return; 468 /* 469 * We're using provider for the first time, so we have to generate 470 * resource unique identifier and initialize local and remote counts. 471 */ 472 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid)); 473 res->hr_primary_localcnt = 1; 474 res->hr_primary_remotecnt = 0; 475 if (metadata_write(res) < 0) 476 exit(EX_NOINPUT); 477} 478 479static bool 480init_remote(struct hast_resource *res, struct proto_conn **inp, 481 struct proto_conn **outp) 482{ 483 struct proto_conn *in, *out; 484 struct nv *nvout, *nvin; 485 const unsigned char *token; 486 unsigned char *map; 487 const char *errmsg; 488 int32_t extentsize; 489 int64_t datasize; 490 uint32_t mapsize; 491 size_t size; 492 493 assert((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 494 assert(real_remote(res)); 495 496 in = out = NULL; 497 498 /* Prepare outgoing connection with remote node. */ 499 if (proto_client(res->hr_remoteaddr, &out) < 0) { 500 primary_exit(EX_TEMPFAIL, "Unable to create connection to %s", 501 res->hr_remoteaddr); 502 } 503 /* Try to connect, but accept failure. */ 504 if (proto_connect(out) < 0) { 505 pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 506 res->hr_remoteaddr); 507 goto close; 508 } 509 /* Error in setting timeout is not critical, but why should it fail? */ 510 if (proto_timeout(out, res->hr_timeout) < 0) 511 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 512 /* 513 * First handshake step. 514 * Setup outgoing connection with remote node. 515 */ 516 nvout = nv_alloc(); 517 nv_add_string(nvout, res->hr_name, "resource"); 518 if (nv_error(nvout) != 0) { 519 pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 520 "Unable to allocate header for connection with %s", 521 res->hr_remoteaddr); 522 nv_free(nvout); 523 goto close; 524 } 525 if (hast_proto_send(res, out, nvout, NULL, 0) < 0) { 526 pjdlog_errno(LOG_WARNING, 527 "Unable to send handshake header to %s", 528 res->hr_remoteaddr); 529 nv_free(nvout); 530 goto close; 531 } 532 nv_free(nvout); 533 if (hast_proto_recv_hdr(out, &nvin) < 0) { 534 pjdlog_errno(LOG_WARNING, 535 "Unable to receive handshake header from %s", 536 res->hr_remoteaddr); 537 goto close; 538 } 539 errmsg = nv_get_string(nvin, "errmsg"); 540 if (errmsg != NULL) { 541 pjdlog_warning("%s", errmsg); 542 nv_free(nvin); 543 goto close; 544 } 545 token = nv_get_uint8_array(nvin, &size, "token"); 546 if (token == NULL) { 547 pjdlog_warning("Handshake header from %s has no 'token' field.", 548 res->hr_remoteaddr); 549 nv_free(nvin); 550 goto close; 551 } 552 if (size != sizeof(res->hr_token)) { 553 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 554 res->hr_remoteaddr, size, sizeof(res->hr_token)); 555 nv_free(nvin); 556 goto close; 557 } 558 bcopy(token, res->hr_token, sizeof(res->hr_token)); 559 nv_free(nvin); 560 561 /* 562 * Second handshake step. 563 * Setup incoming connection with remote node. 564 */ 565 if (proto_client(res->hr_remoteaddr, &in) < 0) { 566 pjdlog_errno(LOG_WARNING, "Unable to create connection to %s", 567 res->hr_remoteaddr); 568 } 569 /* Try to connect, but accept failure. */ 570 if (proto_connect(in) < 0) { 571 pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 572 res->hr_remoteaddr); 573 goto close; 574 } 575 /* Error in setting timeout is not critical, but why should it fail? */ 576 if (proto_timeout(in, res->hr_timeout) < 0) 577 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 578 nvout = nv_alloc(); 579 nv_add_string(nvout, res->hr_name, "resource"); 580 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 581 "token"); 582 nv_add_uint64(nvout, res->hr_resuid, "resuid"); 583 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 584 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 585 if (nv_error(nvout) != 0) { 586 pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 587 "Unable to allocate header for connection with %s", 588 res->hr_remoteaddr); 589 nv_free(nvout); 590 goto close; 591 } 592 if (hast_proto_send(res, in, nvout, NULL, 0) < 0) { 593 pjdlog_errno(LOG_WARNING, 594 "Unable to send handshake header to %s", 595 res->hr_remoteaddr); 596 nv_free(nvout); 597 goto close; 598 } 599 nv_free(nvout); 600 if (hast_proto_recv_hdr(out, &nvin) < 0) { 601 pjdlog_errno(LOG_WARNING, 602 "Unable to receive handshake header from %s", 603 res->hr_remoteaddr); 604 goto close; 605 } 606 errmsg = nv_get_string(nvin, "errmsg"); 607 if (errmsg != NULL) { 608 pjdlog_warning("%s", errmsg); 609 nv_free(nvin); 610 goto close; 611 } 612 datasize = nv_get_int64(nvin, "datasize"); 613 if (datasize != res->hr_datasize) { 614 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 615 (intmax_t)res->hr_datasize, (intmax_t)datasize); 616 nv_free(nvin); 617 goto close; 618 } 619 extentsize = nv_get_int32(nvin, "extentsize"); 620 if (extentsize != res->hr_extentsize) { 621 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 622 (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 623 nv_free(nvin); 624 goto close; 625 } 626 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 627 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 628 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 629 map = NULL; 630 mapsize = nv_get_uint32(nvin, "mapsize"); 631 if (mapsize > 0) { 632 map = malloc(mapsize); 633 if (map == NULL) { 634 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 635 (uintmax_t)mapsize); 636 nv_free(nvin); 637 goto close; 638 } 639 /* 640 * Remote node have some dirty extents on its own, lets 641 * download its activemap. 642 */ 643 if (hast_proto_recv_data(res, out, nvin, map, 644 mapsize) < 0) { 645 pjdlog_errno(LOG_ERR, 646 "Unable to receive remote activemap"); 647 nv_free(nvin); 648 free(map); 649 goto close; 650 } 651 /* 652 * Merge local and remote bitmaps. 653 */ 654 activemap_merge(res->hr_amp, map, mapsize); 655 free(map); 656 /* 657 * Now that we merged bitmaps from both nodes, flush it to the 658 * disk before we start to synchronize. 659 */ 660 (void)hast_activemap_flush(res); 661 } 662 pjdlog_info("Connected to %s.", res->hr_remoteaddr); 663 if (inp != NULL && outp != NULL) { 664 *inp = in; 665 *outp = out; 666 } else { 667 res->hr_remotein = in; 668 res->hr_remoteout = out; 669 } 670 return (true); 671close: 672 proto_close(out); 673 if (in != NULL) 674 proto_close(in); 675 return (false); 676} 677 678static void 679sync_start(void) 680{ 681 682 mtx_lock(&sync_lock); 683 sync_inprogress = true; 684 mtx_unlock(&sync_lock); 685 cv_signal(&sync_cond); 686} 687 688static void 689sync_stop(void) 690{ 691 692 mtx_lock(&sync_lock); 693 if (sync_inprogress) 694 sync_inprogress = false; 695 mtx_unlock(&sync_lock); 696} 697 698static void 699init_ggate(struct hast_resource *res) 700{ 701 struct g_gate_ctl_create ggiocreate; 702 struct g_gate_ctl_cancel ggiocancel; 703 704 /* 705 * We communicate with ggate via /dev/ggctl. Open it. 706 */ 707 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 708 if (res->hr_ggatefd < 0) 709 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 710 /* 711 * Create provider before trying to connect, as connection failure 712 * is not critical, but may take some time. 713 */ 714 ggiocreate.gctl_version = G_GATE_VERSION; 715 ggiocreate.gctl_mediasize = res->hr_datasize; 716 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 717 ggiocreate.gctl_flags = 0; 718 ggiocreate.gctl_maxcount = G_GATE_MAX_QUEUE_SIZE; 719 ggiocreate.gctl_timeout = 0; 720 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 721 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 722 res->hr_provname); 723 bzero(ggiocreate.gctl_info, sizeof(ggiocreate.gctl_info)); 724 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 725 pjdlog_info("Device hast/%s created.", res->hr_provname); 726 res->hr_ggateunit = ggiocreate.gctl_unit; 727 return; 728 } 729 if (errno != EEXIST) { 730 primary_exit(EX_OSERR, "Unable to create hast/%s device", 731 res->hr_provname); 732 } 733 pjdlog_debug(1, 734 "Device hast/%s already exists, we will try to take it over.", 735 res->hr_provname); 736 /* 737 * If we received EEXIST, we assume that the process who created the 738 * provider died and didn't clean up. In that case we will start from 739 * where he left of. 740 */ 741 ggiocancel.gctl_version = G_GATE_VERSION; 742 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 743 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 744 res->hr_provname); 745 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 746 pjdlog_info("Device hast/%s recovered.", res->hr_provname); 747 res->hr_ggateunit = ggiocancel.gctl_unit; 748 return; 749 } 750 primary_exit(EX_OSERR, "Unable to take over hast/%s device", 751 res->hr_provname); 752} 753 754void 755hastd_primary(struct hast_resource *res) 756{ 757 pthread_t td; 758 pid_t pid; 759 int error; 760 761 gres = res; 762 763 /* 764 * Create communication channel between parent and child. 765 */ 766 if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 767 KEEP_ERRNO((void)pidfile_remove(pfh)); 768 primary_exit(EX_OSERR, 769 "Unable to create control sockets between parent and child"); 770 } 771 772 pid = fork(); 773 if (pid < 0) { 774 KEEP_ERRNO((void)pidfile_remove(pfh)); 775 primary_exit(EX_TEMPFAIL, "Unable to fork"); 776 } 777 778 if (pid > 0) { 779 /* This is parent. */ 780 res->hr_workerpid = pid; 781 return; 782 } 783 (void)pidfile_close(pfh); 784 785 setproctitle("%s (primary)", res->hr_name); 786 787 signal(SIGHUP, SIG_DFL); 788 signal(SIGCHLD, SIG_DFL); 789 790 init_local(res); 791 if (real_remote(res) && init_remote(res, NULL, NULL)) 792 sync_start(); 793 init_ggate(res); 794 init_environment(res); 795 error = pthread_create(&td, NULL, ggate_recv_thread, res); 796 assert(error == 0); 797 error = pthread_create(&td, NULL, local_send_thread, res); 798 assert(error == 0); 799 error = pthread_create(&td, NULL, remote_send_thread, res); 800 assert(error == 0); 801 error = pthread_create(&td, NULL, remote_recv_thread, res); 802 assert(error == 0); 803 error = pthread_create(&td, NULL, ggate_send_thread, res); 804 assert(error == 0); 805 error = pthread_create(&td, NULL, sync_thread, res); 806 assert(error == 0); 807 error = pthread_create(&td, NULL, ctrl_thread, res); 808 assert(error == 0); 809 (void)guard_thread(res); 810} 811 812static void 813reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...) 814{ 815 char msg[1024]; 816 va_list ap; 817 int len; 818 819 va_start(ap, fmt); 820 len = vsnprintf(msg, sizeof(msg), fmt, ap); 821 va_end(ap); 822 if ((size_t)len < sizeof(msg)) { 823 switch (ggio->gctl_cmd) { 824 case BIO_READ: 825 (void)snprintf(msg + len, sizeof(msg) - len, 826 "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 827 (uintmax_t)ggio->gctl_length); 828 break; 829 case BIO_DELETE: 830 (void)snprintf(msg + len, sizeof(msg) - len, 831 "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 832 (uintmax_t)ggio->gctl_length); 833 break; 834 case BIO_FLUSH: 835 (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 836 break; 837 case BIO_WRITE: 838 (void)snprintf(msg + len, sizeof(msg) - len, 839 "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 840 (uintmax_t)ggio->gctl_length); 841 break; 842 default: 843 (void)snprintf(msg + len, sizeof(msg) - len, 844 "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd); 845 break; 846 } 847 } 848 pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 849} 850 851static void 852remote_close(struct hast_resource *res, int ncomp) 853{ 854 855 rw_wlock(&hio_remote_lock[ncomp]); 856 /* 857 * A race is possible between dropping rlock and acquiring wlock - 858 * another thread can close connection in-between. 859 */ 860 if (!ISCONNECTED(res, ncomp)) { 861 assert(res->hr_remotein == NULL); 862 assert(res->hr_remoteout == NULL); 863 rw_unlock(&hio_remote_lock[ncomp]); 864 return; 865 } 866 867 assert(res->hr_remotein != NULL); 868 assert(res->hr_remoteout != NULL); 869 870 pjdlog_debug(2, "Closing incoming connection to %s.", 871 res->hr_remoteaddr); 872 proto_close(res->hr_remotein); 873 res->hr_remotein = NULL; 874 pjdlog_debug(2, "Closing outgoing connection to %s.", 875 res->hr_remoteaddr); 876 proto_close(res->hr_remoteout); 877 res->hr_remoteout = NULL; 878 879 rw_unlock(&hio_remote_lock[ncomp]); 880 881 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 882 883 /* 884 * Stop synchronization if in-progress. 885 */ 886 sync_stop(); 887 888 /* 889 * Wake up guard thread, so it can immediately start reconnect. 890 */ 891 mtx_lock(&hio_guard_lock); 892 cv_signal(&hio_guard_cond); 893 mtx_unlock(&hio_guard_lock); 894} 895 896/* 897 * Thread receives ggate I/O requests from the kernel and passes them to 898 * appropriate threads: 899 * WRITE - always goes to both local_send and remote_send threads 900 * READ (when the block is up-to-date on local component) - 901 * only local_send thread 902 * READ (when the block isn't up-to-date on local component) - 903 * only remote_send thread 904 * DELETE - always goes to both local_send and remote_send threads 905 * FLUSH - always goes to both local_send and remote_send threads 906 */ 907static void * 908ggate_recv_thread(void *arg) 909{ 910 struct hast_resource *res = arg; 911 struct g_gate_ctl_io *ggio; 912 struct hio *hio; 913 unsigned int ii, ncomp, ncomps; 914 int error; 915 916 ncomps = HAST_NCOMPONENTS; 917 918 for (;;) { 919 pjdlog_debug(2, "ggate_recv: Taking free request."); 920 QUEUE_TAKE2(hio, free); 921 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 922 ggio = &hio->hio_ggio; 923 ggio->gctl_unit = res->hr_ggateunit; 924 ggio->gctl_length = MAXPHYS; 925 ggio->gctl_error = 0; 926 pjdlog_debug(2, 927 "ggate_recv: (%p) Waiting for request from the kernel.", 928 hio); 929 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) { 930 if (sigexit_received) 931 pthread_exit(NULL); 932 primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 933 } 934 error = ggio->gctl_error; 935 switch (error) { 936 case 0: 937 break; 938 case ECANCELED: 939 /* Exit gracefully. */ 940 if (!sigexit_received) { 941 pjdlog_debug(2, 942 "ggate_recv: (%p) Received cancel from the kernel.", 943 hio); 944 pjdlog_info("Received cancel from the kernel, exiting."); 945 } 946 pthread_exit(NULL); 947 case ENOMEM: 948 /* 949 * Buffer too small? Impossible, we allocate MAXPHYS 950 * bytes - request can't be bigger than that. 951 */ 952 /* FALLTHROUGH */ 953 case ENXIO: 954 default: 955 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 956 strerror(error)); 957 } 958 for (ii = 0; ii < ncomps; ii++) 959 hio->hio_errors[ii] = EINVAL; 960 reqlog(LOG_DEBUG, 2, ggio, 961 "ggate_recv: (%p) Request received from the kernel: ", 962 hio); 963 /* 964 * Inform all components about new write request. 965 * For read request prefer local component unless the given 966 * range is out-of-date, then use remote component. 967 */ 968 switch (ggio->gctl_cmd) { 969 case BIO_READ: 970 pjdlog_debug(2, 971 "ggate_recv: (%p) Moving request to the send queue.", 972 hio); 973 refcount_init(&hio->hio_countdown, 1); 974 mtx_lock(&metadata_lock); 975 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 976 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 977 /* 978 * This range is up-to-date on local component, 979 * so handle request locally. 980 */ 981 /* Local component is 0 for now. */ 982 ncomp = 0; 983 } else /* if (res->hr_syncsrc == 984 HAST_SYNCSRC_SECONDARY) */ { 985 assert(res->hr_syncsrc == 986 HAST_SYNCSRC_SECONDARY); 987 /* 988 * This range is out-of-date on local component, 989 * so send request to the remote node. 990 */ 991 /* Remote component is 1 for now. */ 992 ncomp = 1; 993 } 994 mtx_unlock(&metadata_lock); 995 QUEUE_INSERT1(hio, send, ncomp); 996 break; 997 case BIO_WRITE: 998 for (;;) { 999 mtx_lock(&range_lock); 1000 if (rangelock_islocked(range_sync, 1001 ggio->gctl_offset, ggio->gctl_length)) { 1002 pjdlog_debug(2, 1003 "regular: Range offset=%jd length=%zu locked.", 1004 (intmax_t)ggio->gctl_offset, 1005 (size_t)ggio->gctl_length); 1006 range_regular_wait = true; 1007 cv_wait(&range_regular_cond, &range_lock); 1008 range_regular_wait = false; 1009 mtx_unlock(&range_lock); 1010 continue; 1011 } 1012 if (rangelock_add(range_regular, 1013 ggio->gctl_offset, ggio->gctl_length) < 0) { 1014 mtx_unlock(&range_lock); 1015 pjdlog_debug(2, 1016 "regular: Range offset=%jd length=%zu is already locked, waiting.", 1017 (intmax_t)ggio->gctl_offset, 1018 (size_t)ggio->gctl_length); 1019 sleep(1); 1020 continue; 1021 } 1022 mtx_unlock(&range_lock); 1023 break; 1024 } 1025 mtx_lock(&res->hr_amp_lock); 1026 if (activemap_write_start(res->hr_amp, 1027 ggio->gctl_offset, ggio->gctl_length)) { 1028 (void)hast_activemap_flush(res); 1029 } 1030 mtx_unlock(&res->hr_amp_lock); 1031 /* FALLTHROUGH */ 1032 case BIO_DELETE: 1033 case BIO_FLUSH: 1034 pjdlog_debug(2, 1035 "ggate_recv: (%p) Moving request to the send queues.", 1036 hio); 1037 refcount_init(&hio->hio_countdown, ncomps); 1038 for (ii = 0; ii < ncomps; ii++) 1039 QUEUE_INSERT1(hio, send, ii); 1040 break; 1041 } 1042 } 1043 /* NOTREACHED */ 1044 return (NULL); 1045} 1046 1047/* 1048 * Thread reads from or writes to local component. 1049 * If local read fails, it redirects it to remote_send thread. 1050 */ 1051static void * 1052local_send_thread(void *arg) 1053{ 1054 struct hast_resource *res = arg; 1055 struct g_gate_ctl_io *ggio; 1056 struct hio *hio; 1057 unsigned int ncomp, rncomp; 1058 ssize_t ret; 1059 1060 /* Local component is 0 for now. */ 1061 ncomp = 0; 1062 /* Remote component is 1 for now. */ 1063 rncomp = 1; 1064 1065 for (;;) { 1066 pjdlog_debug(2, "local_send: Taking request."); 1067 QUEUE_TAKE1(hio, send, ncomp); 1068 pjdlog_debug(2, "local_send: (%p) Got request.", hio); 1069 ggio = &hio->hio_ggio; 1070 switch (ggio->gctl_cmd) { 1071 case BIO_READ: 1072 ret = pread(res->hr_localfd, ggio->gctl_data, 1073 ggio->gctl_length, 1074 ggio->gctl_offset + res->hr_localoff); 1075 if (ret == ggio->gctl_length) 1076 hio->hio_errors[ncomp] = 0; 1077 else { 1078 /* 1079 * If READ failed, try to read from remote node. 1080 */ 1081 QUEUE_INSERT1(hio, send, rncomp); 1082 continue; 1083 } 1084 break; 1085 case BIO_WRITE: 1086 ret = pwrite(res->hr_localfd, ggio->gctl_data, 1087 ggio->gctl_length, 1088 ggio->gctl_offset + res->hr_localoff); 1089 if (ret < 0) 1090 hio->hio_errors[ncomp] = errno; 1091 else if (ret != ggio->gctl_length) 1092 hio->hio_errors[ncomp] = EIO; 1093 else 1094 hio->hio_errors[ncomp] = 0; 1095 break; 1096 case BIO_DELETE: 1097 ret = g_delete(res->hr_localfd, 1098 ggio->gctl_offset + res->hr_localoff, 1099 ggio->gctl_length); 1100 if (ret < 0) 1101 hio->hio_errors[ncomp] = errno; 1102 else 1103 hio->hio_errors[ncomp] = 0; 1104 break; 1105 case BIO_FLUSH: 1106 ret = g_flush(res->hr_localfd); 1107 if (ret < 0) 1108 hio->hio_errors[ncomp] = errno; 1109 else 1110 hio->hio_errors[ncomp] = 0; 1111 break; 1112 } 1113 if (refcount_release(&hio->hio_countdown)) { 1114 if (ISSYNCREQ(hio)) { 1115 mtx_lock(&sync_lock); 1116 SYNCREQDONE(hio); 1117 mtx_unlock(&sync_lock); 1118 cv_signal(&sync_cond); 1119 } else { 1120 pjdlog_debug(2, 1121 "local_send: (%p) Moving request to the done queue.", 1122 hio); 1123 QUEUE_INSERT2(hio, done); 1124 } 1125 } 1126 } 1127 /* NOTREACHED */ 1128 return (NULL); 1129} 1130 1131/* 1132 * Thread sends request to secondary node. 1133 */ 1134static void * 1135remote_send_thread(void *arg) 1136{ 1137 struct hast_resource *res = arg; 1138 struct g_gate_ctl_io *ggio; 1139 struct hio *hio; 1140 struct nv *nv; 1141 unsigned int ncomp; 1142 bool wakeup; 1143 uint64_t offset, length; 1144 uint8_t cmd; 1145 void *data; 1146 1147 /* Remote component is 1 for now. */ 1148 ncomp = 1; 1149 1150 for (;;) { 1151 pjdlog_debug(2, "remote_send: Taking request."); 1152 QUEUE_TAKE1(hio, send, ncomp); 1153 pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 1154 ggio = &hio->hio_ggio; 1155 switch (ggio->gctl_cmd) { 1156 case BIO_READ: 1157 cmd = HIO_READ; 1158 data = NULL; 1159 offset = ggio->gctl_offset; 1160 length = ggio->gctl_length; 1161 break; 1162 case BIO_WRITE: 1163 cmd = HIO_WRITE; 1164 data = ggio->gctl_data; 1165 offset = ggio->gctl_offset; 1166 length = ggio->gctl_length; 1167 break; 1168 case BIO_DELETE: 1169 cmd = HIO_DELETE; 1170 data = NULL; 1171 offset = ggio->gctl_offset; 1172 length = ggio->gctl_length; 1173 break; 1174 case BIO_FLUSH: 1175 cmd = HIO_FLUSH; 1176 data = NULL; 1177 offset = 0; 1178 length = 0; 1179 break; 1180 default: 1181 assert(!"invalid condition"); 1182 abort(); 1183 } 1184 nv = nv_alloc(); 1185 nv_add_uint8(nv, cmd, "cmd"); 1186 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 1187 nv_add_uint64(nv, offset, "offset"); 1188 nv_add_uint64(nv, length, "length"); 1189 if (nv_error(nv) != 0) { 1190 hio->hio_errors[ncomp] = nv_error(nv); 1191 pjdlog_debug(2, 1192 "remote_send: (%p) Unable to prepare header to send.", 1193 hio); 1194 reqlog(LOG_ERR, 0, ggio, 1195 "Unable to prepare header to send (%s): ", 1196 strerror(nv_error(nv))); 1197 /* Move failed request immediately to the done queue. */ 1198 goto done_queue; 1199 } 1200 pjdlog_debug(2, 1201 "remote_send: (%p) Moving request to the recv queue.", 1202 hio); 1203 /* 1204 * Protect connection from disappearing. 1205 */ 1206 rw_rlock(&hio_remote_lock[ncomp]); 1207 if (!ISCONNECTED(res, ncomp)) { 1208 rw_unlock(&hio_remote_lock[ncomp]); 1209 hio->hio_errors[ncomp] = ENOTCONN; 1210 goto done_queue; 1211 } 1212 /* 1213 * Move the request to recv queue before sending it, because 1214 * in different order we can get reply before we move request 1215 * to recv queue. 1216 */ 1217 mtx_lock(&hio_recv_list_lock[ncomp]); 1218 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 1219 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 1220 mtx_unlock(&hio_recv_list_lock[ncomp]); 1221 if (hast_proto_send(res, res->hr_remoteout, nv, data, 1222 data != NULL ? length : 0) < 0) { 1223 hio->hio_errors[ncomp] = errno; 1224 rw_unlock(&hio_remote_lock[ncomp]); 1225 remote_close(res, ncomp); 1226 pjdlog_debug(2, 1227 "remote_send: (%p) Unable to send request.", hio); 1228 reqlog(LOG_ERR, 0, ggio, 1229 "Unable to send request (%s): ", 1230 strerror(hio->hio_errors[ncomp])); 1231 /* 1232 * Take request back from the receive queue and move 1233 * it immediately to the done queue. 1234 */ 1235 mtx_lock(&hio_recv_list_lock[ncomp]); 1236 TAILQ_REMOVE(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 1237 mtx_unlock(&hio_recv_list_lock[ncomp]); 1238 goto done_queue; 1239 } 1240 rw_unlock(&hio_remote_lock[ncomp]); 1241 nv_free(nv); 1242 if (wakeup) 1243 cv_signal(&hio_recv_list_cond[ncomp]); 1244 continue; 1245done_queue: 1246 nv_free(nv); 1247 if (ISSYNCREQ(hio)) { 1248 if (!refcount_release(&hio->hio_countdown)) 1249 continue; 1250 mtx_lock(&sync_lock); 1251 SYNCREQDONE(hio); 1252 mtx_unlock(&sync_lock); 1253 cv_signal(&sync_cond); 1254 continue; 1255 } 1256 if (ggio->gctl_cmd == BIO_WRITE) { 1257 mtx_lock(&res->hr_amp_lock); 1258 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 1259 ggio->gctl_length)) { 1260 (void)hast_activemap_flush(res); 1261 } 1262 mtx_unlock(&res->hr_amp_lock); 1263 } 1264 if (!refcount_release(&hio->hio_countdown)) 1265 continue; 1266 pjdlog_debug(2, 1267 "remote_send: (%p) Moving request to the done queue.", 1268 hio); 1269 QUEUE_INSERT2(hio, done); 1270 } 1271 /* NOTREACHED */ 1272 return (NULL); 1273} 1274 1275/* 1276 * Thread receives answer from secondary node and passes it to ggate_send 1277 * thread. 1278 */ 1279static void * 1280remote_recv_thread(void *arg) 1281{ 1282 struct hast_resource *res = arg; 1283 struct g_gate_ctl_io *ggio; 1284 struct hio *hio; 1285 struct nv *nv; 1286 unsigned int ncomp; 1287 uint64_t seq; 1288 int error; 1289 1290 /* Remote component is 1 for now. */ 1291 ncomp = 1; 1292 1293 for (;;) { 1294 /* Wait until there is anything to receive. */ 1295 mtx_lock(&hio_recv_list_lock[ncomp]); 1296 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 1297 pjdlog_debug(2, "remote_recv: No requests, waiting."); 1298 cv_wait(&hio_recv_list_cond[ncomp], 1299 &hio_recv_list_lock[ncomp]); 1300 } 1301 mtx_unlock(&hio_recv_list_lock[ncomp]); 1302 rw_rlock(&hio_remote_lock[ncomp]); 1303 if (!ISCONNECTED(res, ncomp)) { 1304 rw_unlock(&hio_remote_lock[ncomp]); 1305 /* 1306 * Connection is dead, so move all pending requests to 1307 * the done queue (one-by-one). 1308 */ 1309 mtx_lock(&hio_recv_list_lock[ncomp]); 1310 hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 1311 assert(hio != NULL); 1312 TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1313 hio_next[ncomp]); 1314 mtx_unlock(&hio_recv_list_lock[ncomp]); 1315 goto done_queue; 1316 } 1317 if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) { 1318 pjdlog_errno(LOG_ERR, 1319 "Unable to receive reply header"); 1320 rw_unlock(&hio_remote_lock[ncomp]); 1321 remote_close(res, ncomp); 1322 continue; 1323 } 1324 rw_unlock(&hio_remote_lock[ncomp]); 1325 seq = nv_get_uint64(nv, "seq"); 1326 if (seq == 0) { 1327 pjdlog_error("Header contains no 'seq' field."); 1328 nv_free(nv); 1329 continue; 1330 } 1331 mtx_lock(&hio_recv_list_lock[ncomp]); 1332 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 1333 if (hio->hio_ggio.gctl_seq == seq) { 1334 TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1335 hio_next[ncomp]); 1336 break; 1337 } 1338 } 1339 mtx_unlock(&hio_recv_list_lock[ncomp]); 1340 if (hio == NULL) { 1341 pjdlog_error("Found no request matching received 'seq' field (%ju).", 1342 (uintmax_t)seq); 1343 nv_free(nv); 1344 continue; 1345 } 1346 error = nv_get_int16(nv, "error"); 1347 if (error != 0) { 1348 /* Request failed on remote side. */ 1349 hio->hio_errors[ncomp] = 0; 1350 nv_free(nv); 1351 goto done_queue; 1352 } 1353 ggio = &hio->hio_ggio; 1354 switch (ggio->gctl_cmd) { 1355 case BIO_READ: 1356 rw_rlock(&hio_remote_lock[ncomp]); 1357 if (!ISCONNECTED(res, ncomp)) { 1358 rw_unlock(&hio_remote_lock[ncomp]); 1359 nv_free(nv); 1360 goto done_queue; 1361 } 1362 if (hast_proto_recv_data(res, res->hr_remotein, nv, 1363 ggio->gctl_data, ggio->gctl_length) < 0) { 1364 hio->hio_errors[ncomp] = errno; 1365 pjdlog_errno(LOG_ERR, 1366 "Unable to receive reply data"); 1367 rw_unlock(&hio_remote_lock[ncomp]); 1368 nv_free(nv); 1369 remote_close(res, ncomp); 1370 goto done_queue; 1371 } 1372 rw_unlock(&hio_remote_lock[ncomp]); 1373 break; 1374 case BIO_WRITE: 1375 case BIO_DELETE: 1376 case BIO_FLUSH: 1377 break; 1378 default: 1379 assert(!"invalid condition"); 1380 abort(); 1381 } 1382 hio->hio_errors[ncomp] = 0; 1383 nv_free(nv); 1384done_queue: 1385 if (refcount_release(&hio->hio_countdown)) { 1386 if (ISSYNCREQ(hio)) { 1387 mtx_lock(&sync_lock); 1388 SYNCREQDONE(hio); 1389 mtx_unlock(&sync_lock); 1390 cv_signal(&sync_cond); 1391 } else { 1392 pjdlog_debug(2, 1393 "remote_recv: (%p) Moving request to the done queue.", 1394 hio); 1395 QUEUE_INSERT2(hio, done); 1396 } 1397 } 1398 } 1399 /* NOTREACHED */ 1400 return (NULL); 1401} 1402 1403/* 1404 * Thread sends answer to the kernel. 1405 */ 1406static void * 1407ggate_send_thread(void *arg) 1408{ 1409 struct hast_resource *res = arg; 1410 struct g_gate_ctl_io *ggio; 1411 struct hio *hio; 1412 unsigned int ii, ncomp, ncomps; 1413 1414 ncomps = HAST_NCOMPONENTS; 1415 1416 for (;;) { 1417 pjdlog_debug(2, "ggate_send: Taking request."); 1418 QUEUE_TAKE2(hio, done); 1419 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 1420 ggio = &hio->hio_ggio; 1421 for (ii = 0; ii < ncomps; ii++) { 1422 if (hio->hio_errors[ii] == 0) { 1423 /* 1424 * One successful request is enough to declare 1425 * success. 1426 */ 1427 ggio->gctl_error = 0; 1428 break; 1429 } 1430 } 1431 if (ii == ncomps) { 1432 /* 1433 * None of the requests were successful. 1434 * Use first error. 1435 */ 1436 ggio->gctl_error = hio->hio_errors[0]; 1437 } 1438 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 1439 mtx_lock(&res->hr_amp_lock); 1440 activemap_write_complete(res->hr_amp, 1441 ggio->gctl_offset, ggio->gctl_length); 1442 mtx_unlock(&res->hr_amp_lock); 1443 } 1444 if (ggio->gctl_cmd == BIO_WRITE) { 1445 /* 1446 * Unlock range we locked. 1447 */ 1448 mtx_lock(&range_lock); 1449 rangelock_del(range_regular, ggio->gctl_offset, 1450 ggio->gctl_length); 1451 if (range_sync_wait) 1452 cv_signal(&range_sync_cond); 1453 mtx_unlock(&range_lock); 1454 /* 1455 * Bump local count if this is first write after 1456 * connection failure with remote node. 1457 */ 1458 ncomp = 1; 1459 rw_rlock(&hio_remote_lock[ncomp]); 1460 if (!ISCONNECTED(res, ncomp)) { 1461 mtx_lock(&metadata_lock); 1462 if (res->hr_primary_localcnt == 1463 res->hr_secondary_remotecnt) { 1464 res->hr_primary_localcnt++; 1465 pjdlog_debug(1, 1466 "Increasing localcnt to %ju.", 1467 (uintmax_t)res->hr_primary_localcnt); 1468 (void)metadata_write(res); 1469 } 1470 mtx_unlock(&metadata_lock); 1471 } 1472 rw_unlock(&hio_remote_lock[ncomp]); 1473 } 1474 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) 1475 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 1476 pjdlog_debug(2, 1477 "ggate_send: (%p) Moving request to the free queue.", hio); 1478 QUEUE_INSERT2(hio, free); 1479 } 1480 /* NOTREACHED */ 1481 return (NULL); 1482} 1483 1484/* 1485 * Thread synchronize local and remote components. 1486 */ 1487static void * 1488sync_thread(void *arg __unused) 1489{ 1490 struct hast_resource *res = arg; 1491 struct hio *hio; 1492 struct g_gate_ctl_io *ggio; 1493 unsigned int ii, ncomp, ncomps; 1494 off_t offset, length, synced; 1495 bool dorewind; 1496 int syncext; 1497 1498 ncomps = HAST_NCOMPONENTS; 1499 dorewind = true; 1500 synced = -1; 1501 1502 for (;;) { 1503 mtx_lock(&sync_lock); 1504 if (synced == -1) 1505 synced = 0; 1506 else if (!sync_inprogress) { 1507 pjdlog_info("Synchronization interrupted. " 1508 "%jd bytes synchronized so far.", 1509 (intmax_t)synced); 1510 } 1511 while (!sync_inprogress) { 1512 dorewind = true; 1513 synced = 0; 1514 cv_wait(&sync_cond, &sync_lock); 1515 } 1516 mtx_unlock(&sync_lock); 1517 /* 1518 * Obtain offset at which we should synchronize. 1519 * Rewind synchronization if needed. 1520 */ 1521 mtx_lock(&res->hr_amp_lock); 1522 if (dorewind) 1523 activemap_sync_rewind(res->hr_amp); 1524 offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 1525 if (syncext != -1) { 1526 /* 1527 * We synchronized entire syncext extent, we can mark 1528 * it as clean now. 1529 */ 1530 if (activemap_extent_complete(res->hr_amp, syncext)) 1531 (void)hast_activemap_flush(res); 1532 } 1533 mtx_unlock(&res->hr_amp_lock); 1534 if (dorewind) { 1535 dorewind = false; 1536 if (offset < 0) 1537 pjdlog_info("Nodes are in sync."); 1538 else { 1539 pjdlog_info("Synchronization started. %ju bytes to go.", 1540 (uintmax_t)(res->hr_extentsize * 1541 activemap_ndirty(res->hr_amp))); 1542 } 1543 } 1544 if (offset < 0) { 1545 sync_stop(); 1546 pjdlog_debug(1, "Nothing to synchronize."); 1547 /* 1548 * Synchronization complete, make both localcnt and 1549 * remotecnt equal. 1550 */ 1551 ncomp = 1; 1552 rw_rlock(&hio_remote_lock[ncomp]); 1553 if (ISCONNECTED(res, ncomp)) { 1554 if (synced > 0) { 1555 pjdlog_info("Synchronization complete. " 1556 "%jd bytes synchronized.", 1557 (intmax_t)synced); 1558 } 1559 mtx_lock(&metadata_lock); 1560 res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 1561 res->hr_primary_localcnt = 1562 res->hr_secondary_localcnt; 1563 res->hr_primary_remotecnt = 1564 res->hr_secondary_remotecnt; 1565 pjdlog_debug(1, 1566 "Setting localcnt to %ju and remotecnt to %ju.", 1567 (uintmax_t)res->hr_primary_localcnt, 1568 (uintmax_t)res->hr_secondary_localcnt); 1569 (void)metadata_write(res); 1570 mtx_unlock(&metadata_lock); 1571 } 1572 rw_unlock(&hio_remote_lock[ncomp]); 1573 continue; 1574 } 1575 pjdlog_debug(2, "sync: Taking free request."); 1576 QUEUE_TAKE2(hio, free); 1577 pjdlog_debug(2, "sync: (%p) Got free request.", hio); 1578 /* 1579 * Lock the range we are going to synchronize. We don't want 1580 * race where someone writes between our read and write. 1581 */ 1582 for (;;) { 1583 mtx_lock(&range_lock); 1584 if (rangelock_islocked(range_regular, offset, length)) { 1585 pjdlog_debug(2, 1586 "sync: Range offset=%jd length=%jd locked.", 1587 (intmax_t)offset, (intmax_t)length); 1588 range_sync_wait = true; 1589 cv_wait(&range_sync_cond, &range_lock); 1590 range_sync_wait = false; 1591 mtx_unlock(&range_lock); 1592 continue; 1593 } 1594 if (rangelock_add(range_sync, offset, length) < 0) { 1595 mtx_unlock(&range_lock); 1596 pjdlog_debug(2, 1597 "sync: Range offset=%jd length=%jd is already locked, waiting.", 1598 (intmax_t)offset, (intmax_t)length); 1599 sleep(1); 1600 continue; 1601 } 1602 mtx_unlock(&range_lock); 1603 break; 1604 } 1605 /* 1606 * First read the data from synchronization source. 1607 */ 1608 SYNCREQ(hio); 1609 ggio = &hio->hio_ggio; 1610 ggio->gctl_cmd = BIO_READ; 1611 ggio->gctl_offset = offset; 1612 ggio->gctl_length = length; 1613 ggio->gctl_error = 0; 1614 for (ii = 0; ii < ncomps; ii++) 1615 hio->hio_errors[ii] = EINVAL; 1616 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 1617 hio); 1618 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 1619 hio); 1620 mtx_lock(&metadata_lock); 1621 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1622 /* 1623 * This range is up-to-date on local component, 1624 * so handle request locally. 1625 */ 1626 /* Local component is 0 for now. */ 1627 ncomp = 0; 1628 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 1629 assert(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 1630 /* 1631 * This range is out-of-date on local component, 1632 * so send request to the remote node. 1633 */ 1634 /* Remote component is 1 for now. */ 1635 ncomp = 1; 1636 } 1637 mtx_unlock(&metadata_lock); 1638 refcount_init(&hio->hio_countdown, 1); 1639 QUEUE_INSERT1(hio, send, ncomp); 1640 1641 /* 1642 * Let's wait for READ to finish. 1643 */ 1644 mtx_lock(&sync_lock); 1645 while (!ISSYNCREQDONE(hio)) 1646 cv_wait(&sync_cond, &sync_lock); 1647 mtx_unlock(&sync_lock); 1648 1649 if (hio->hio_errors[ncomp] != 0) { 1650 pjdlog_error("Unable to read synchronization data: %s.", 1651 strerror(hio->hio_errors[ncomp])); 1652 goto free_queue; 1653 } 1654 1655 /* 1656 * We read the data from synchronization source, now write it 1657 * to synchronization target. 1658 */ 1659 SYNCREQ(hio); 1660 ggio->gctl_cmd = BIO_WRITE; 1661 for (ii = 0; ii < ncomps; ii++) 1662 hio->hio_errors[ii] = EINVAL; 1663 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 1664 hio); 1665 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 1666 hio); 1667 mtx_lock(&metadata_lock); 1668 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1669 /* 1670 * This range is up-to-date on local component, 1671 * so we update remote component. 1672 */ 1673 /* Remote component is 1 for now. */ 1674 ncomp = 1; 1675 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 1676 assert(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 1677 /* 1678 * This range is out-of-date on local component, 1679 * so we update it. 1680 */ 1681 /* Local component is 0 for now. */ 1682 ncomp = 0; 1683 } 1684 mtx_unlock(&metadata_lock); 1685 1686 pjdlog_debug(2, "sync: (%p) Moving request to the send queues.", 1687 hio); 1688 refcount_init(&hio->hio_countdown, 1); 1689 QUEUE_INSERT1(hio, send, ncomp); 1690 1691 /* 1692 * Let's wait for WRITE to finish. 1693 */ 1694 mtx_lock(&sync_lock); 1695 while (!ISSYNCREQDONE(hio)) 1696 cv_wait(&sync_cond, &sync_lock); 1697 mtx_unlock(&sync_lock); 1698 1699 if (hio->hio_errors[ncomp] != 0) { 1700 pjdlog_error("Unable to write synchronization data: %s.", 1701 strerror(hio->hio_errors[ncomp])); 1702 goto free_queue; 1703 } 1704 1705 synced += length; 1706free_queue: 1707 mtx_lock(&range_lock); 1708 rangelock_del(range_sync, offset, length); 1709 if (range_regular_wait) 1710 cv_signal(&range_regular_cond); 1711 mtx_unlock(&range_lock); 1712 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 1713 hio); 1714 QUEUE_INSERT2(hio, free); 1715 } 1716 /* NOTREACHED */ 1717 return (NULL); 1718} 1719 1720static void 1721sighandler(int sig) 1722{ 1723 bool unlock; 1724 1725 switch (sig) { 1726 case SIGINT: 1727 case SIGTERM: 1728 sigexit_received = true; 1729 break; 1730 case SIGHUP: 1731 sighup_received = true; 1732 break; 1733 default: 1734 assert(!"invalid condition"); 1735 } 1736 /* 1737 * XXX: Racy, but if we cannot obtain hio_guard_lock here, we don't 1738 * want to risk deadlock. 1739 */ 1740 unlock = mtx_trylock(&hio_guard_lock); 1741 cv_signal(&hio_guard_cond); 1742 if (unlock) 1743 mtx_unlock(&hio_guard_lock); 1744} 1745 1746static void 1747config_reload(void) 1748{ 1749 struct hastd_config *newcfg; 1750 struct hast_resource *res; 1751 unsigned int ii, ncomps; 1752 int modified; 1753 1754 pjdlog_info("Reloading configuration..."); 1755 1756 ncomps = HAST_NCOMPONENTS; 1757 1758 newcfg = yy_config_parse(cfgpath, false); 1759 if (newcfg == NULL) 1760 goto failed; 1761 1762 TAILQ_FOREACH(res, &newcfg->hc_resources, hr_next) { 1763 if (strcmp(res->hr_name, gres->hr_name) == 0) 1764 break; 1765 } 1766 /* 1767 * If resource was removed from the configuration file, resource 1768 * name, provider name or path to local component was modified we 1769 * shouldn't be here. This means that someone modified configuration 1770 * file and send SIGHUP to us instead of main hastd process. 1771 * Log advice and ignore the signal. 1772 */ 1773 if (res == NULL || strcmp(gres->hr_name, res->hr_name) != 0 || 1774 strcmp(gres->hr_provname, res->hr_provname) != 0 || 1775 strcmp(gres->hr_localpath, res->hr_localpath) != 0) { 1776 pjdlog_warning("To reload configuration send SIGHUP to the main hastd process (pid %u).", 1777 (unsigned int)getppid()); 1778 goto failed; 1779 } 1780 1781#define MODIFIED_REMOTEADDR 0x1 1782#define MODIFIED_REPLICATION 0x2 1783#define MODIFIED_TIMEOUT 0x4 1784 modified = 0; 1785 if (strcmp(gres->hr_remoteaddr, res->hr_remoteaddr) != 0) { 1786 /* 1787 * Don't copy res->hr_remoteaddr to gres just yet. 1788 * We want remote_close() to log disconnect from the old 1789 * addresses, not from the new ones. 1790 */ 1791 modified |= MODIFIED_REMOTEADDR; 1792 } 1793 if (gres->hr_replication != res->hr_replication) { 1794 gres->hr_replication = res->hr_replication; 1795 modified |= MODIFIED_REPLICATION; 1796 } 1797 if (gres->hr_timeout != res->hr_timeout) { 1798 gres->hr_timeout = res->hr_timeout; 1799 modified |= MODIFIED_TIMEOUT; 1800 } 1801 /* 1802 * If only timeout was modified we only need to change it without 1803 * reconnecting. 1804 */ 1805 if (modified == MODIFIED_TIMEOUT) { 1806 for (ii = 0; ii < ncomps; ii++) { 1807 if (!ISREMOTE(ii)) 1808 continue; 1809 rw_rlock(&hio_remote_lock[ii]); 1810 if (!ISCONNECTED(gres, ii)) { 1811 rw_unlock(&hio_remote_lock[ii]); 1812 continue; 1813 } 1814 rw_unlock(&hio_remote_lock[ii]); 1815 if (proto_timeout(gres->hr_remotein, 1816 gres->hr_timeout) < 0) { 1817 pjdlog_errno(LOG_WARNING, 1818 "Unable to set connection timeout"); 1819 } 1820 if (proto_timeout(gres->hr_remoteout, 1821 gres->hr_timeout) < 0) { 1822 pjdlog_errno(LOG_WARNING, 1823 "Unable to set connection timeout"); 1824 } 1825 } 1826 } else { 1827 for (ii = 0; ii < ncomps; ii++) { 1828 if (!ISREMOTE(ii)) 1829 continue; 1830 remote_close(gres, ii); 1831 } 1832 if (modified & MODIFIED_REMOTEADDR) { 1833 strlcpy(gres->hr_remoteaddr, res->hr_remoteaddr, 1834 sizeof(gres->hr_remoteaddr)); 1835 } 1836 } 1837#undef MODIFIED_REMOTEADDR 1838#undef MODIFIED_REPLICATION 1839#undef MODIFIED_TIMEOUT 1840 1841 pjdlog_info("Configuration reloaded successfully."); 1842 return; 1843failed: 1844 if (newcfg != NULL) { 1845 if (newcfg->hc_controlconn != NULL) 1846 proto_close(newcfg->hc_controlconn); 1847 if (newcfg->hc_listenconn != NULL) 1848 proto_close(newcfg->hc_listenconn); 1849 yy_config_free(newcfg); 1850 } 1851 pjdlog_warning("Configuration not reloaded."); 1852} 1853 1854/* 1855 * Thread guards remote connections and reconnects when needed, handles 1856 * signals, etc. 1857 */ 1858static void * 1859guard_thread(void *arg) 1860{ 1861 struct hast_resource *res = arg; 1862 struct proto_conn *in, *out; 1863 unsigned int ii, ncomps; 1864 int timeout; 1865 1866 ncomps = HAST_NCOMPONENTS; 1867 1868 for (;;) { 1869 if (sigexit_received) { 1870 primary_exitx(EX_OK, 1871 "Termination signal received, exiting."); 1872 } 1873 if (sighup_received) { 1874 sighup_received = false; 1875 config_reload(); 1876 } 1877 /* 1878 * If all the connection will be fine, we will sleep until 1879 * someone wakes us up. 1880 * If any of the connections will be broken and we won't be 1881 * able to connect, we will sleep only for RECONNECT_SLEEP 1882 * seconds so we can retry soon. 1883 */ 1884 timeout = 0; 1885 pjdlog_debug(2, "remote_guard: Checking connections."); 1886 mtx_lock(&hio_guard_lock); 1887 for (ii = 0; ii < ncomps; ii++) { 1888 if (!ISREMOTE(ii)) 1889 continue; 1890 rw_rlock(&hio_remote_lock[ii]); 1891 if (ISCONNECTED(res, ii)) { 1892 assert(res->hr_remotein != NULL); 1893 assert(res->hr_remoteout != NULL); 1894 rw_unlock(&hio_remote_lock[ii]); 1895 pjdlog_debug(2, 1896 "remote_guard: Connection to %s is ok.", 1897 res->hr_remoteaddr); 1898 } else if (real_remote(res)) { 1899 assert(res->hr_remotein == NULL); 1900 assert(res->hr_remoteout == NULL); 1901 /* 1902 * Upgrade the lock. It doesn't have to be 1903 * atomic as no other thread can change 1904 * connection status from disconnected to 1905 * connected. 1906 */ 1907 rw_unlock(&hio_remote_lock[ii]); 1908 pjdlog_debug(2, 1909 "remote_guard: Reconnecting to %s.", 1910 res->hr_remoteaddr); 1911 in = out = NULL; 1912 if (init_remote(res, &in, &out)) { 1913 rw_wlock(&hio_remote_lock[ii]); 1914 assert(res->hr_remotein == NULL); 1915 assert(res->hr_remoteout == NULL); 1916 assert(in != NULL && out != NULL); 1917 res->hr_remotein = in; 1918 res->hr_remoteout = out; 1919 rw_unlock(&hio_remote_lock[ii]); 1920 pjdlog_info("Successfully reconnected to %s.", 1921 res->hr_remoteaddr); 1922 sync_start(); 1923 } else { 1924 /* Both connections should be NULL. */ 1925 assert(res->hr_remotein == NULL); 1926 assert(res->hr_remoteout == NULL); 1927 assert(in == NULL && out == NULL); 1928 pjdlog_debug(2, 1929 "remote_guard: Reconnect to %s failed.", 1930 res->hr_remoteaddr); 1931 timeout = RECONNECT_SLEEP; 1932 } 1933 } else { 1934 rw_unlock(&hio_remote_lock[ii]); 1935 } 1936 } 1937 (void)cv_timedwait(&hio_guard_cond, &hio_guard_lock, timeout); 1938 mtx_unlock(&hio_guard_lock); 1939 } 1940 /* NOTREACHED */ 1941 return (NULL); 1942} 1943