primary.c revision 246922
1204076Spjd/*- 2204076Spjd * Copyright (c) 2009 The FreeBSD Foundation 3219351Spjd * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 4204076Spjd * All rights reserved. 5204076Spjd * 6204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from 7204076Spjd * the FreeBSD Foundation. 8204076Spjd * 9204076Spjd * Redistribution and use in source and binary forms, with or without 10204076Spjd * modification, are permitted provided that the following conditions 11204076Spjd * are met: 12204076Spjd * 1. Redistributions of source code must retain the above copyright 13204076Spjd * notice, this list of conditions and the following disclaimer. 14204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 15204076Spjd * notice, this list of conditions and the following disclaimer in the 16204076Spjd * documentation and/or other materials provided with the distribution. 17204076Spjd * 18204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21204076Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28204076Spjd * SUCH DAMAGE. 29204076Spjd */ 30204076Spjd 31204076Spjd#include <sys/cdefs.h> 32204076Spjd__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 246922 2013-02-17 21:12:34Z pjd $"); 33204076Spjd 34204076Spjd#include <sys/types.h> 35204076Spjd#include <sys/time.h> 36204076Spjd#include <sys/bio.h> 37204076Spjd#include <sys/disk.h> 38204076Spjd#include <sys/stat.h> 39204076Spjd 40204076Spjd#include <geom/gate/g_gate.h> 41204076Spjd 42204076Spjd#include <err.h> 43204076Spjd#include <errno.h> 44204076Spjd#include <fcntl.h> 45204076Spjd#include <libgeom.h> 46204076Spjd#include <pthread.h> 47211982Spjd#include <signal.h> 48204076Spjd#include <stdint.h> 49204076Spjd#include <stdio.h> 50204076Spjd#include <string.h> 51204076Spjd#include <sysexits.h> 52204076Spjd#include <unistd.h> 53204076Spjd 54204076Spjd#include <activemap.h> 55204076Spjd#include <nv.h> 56204076Spjd#include <rangelock.h> 57204076Spjd 58204076Spjd#include "control.h" 59212038Spjd#include "event.h" 60204076Spjd#include "hast.h" 61204076Spjd#include "hast_proto.h" 62204076Spjd#include "hastd.h" 63211886Spjd#include "hooks.h" 64204076Spjd#include "metadata.h" 65204076Spjd#include "proto.h" 66204076Spjd#include "pjdlog.h" 67246922Spjd#include "refcnt.h" 68204076Spjd#include "subr.h" 69204076Spjd#include "synch.h" 70204076Spjd 71210886Spjd/* The is only one remote component for now. */ 72210886Spjd#define ISREMOTE(no) ((no) == 1) 73210886Spjd 74204076Spjdstruct hio { 75204076Spjd /* 76204076Spjd * Number of components we are still waiting for. 77204076Spjd * When this field goes to 0, we can send the request back to the 78204076Spjd * kernel. Each component has to decrease this counter by one 79204076Spjd * even on failure. 80204076Spjd */ 81204076Spjd unsigned int hio_countdown; 82204076Spjd /* 83204076Spjd * Each component has a place to store its own error. 84204076Spjd * Once the request is handled by all components we can decide if the 85204076Spjd * request overall is successful or not. 86204076Spjd */ 87204076Spjd int *hio_errors; 88204076Spjd /* 89219818Spjd * Structure used to communicate with GEOM Gate class. 90204076Spjd */ 91204076Spjd struct g_gate_ctl_io hio_ggio; 92226859Spjd /* 93226859Spjd * Request was already confirmed to GEOM Gate. 94226859Spjd */ 95226859Spjd bool hio_done; 96226859Spjd /* 97226859Spjd * Remember replication from the time the request was initiated, 98226859Spjd * so we won't get confused when replication changes on reload. 99226859Spjd */ 100226859Spjd int hio_replication; 101204076Spjd TAILQ_ENTRY(hio) *hio_next; 102204076Spjd}; 103204076Spjd#define hio_free_next hio_next[0] 104204076Spjd#define hio_done_next hio_next[0] 105204076Spjd 106204076Spjd/* 107204076Spjd * Free list holds unused structures. When free list is empty, we have to wait 108204076Spjd * until some in-progress requests are freed. 109204076Spjd */ 110204076Spjdstatic TAILQ_HEAD(, hio) hio_free_list; 111204076Spjdstatic pthread_mutex_t hio_free_list_lock; 112204076Spjdstatic pthread_cond_t hio_free_list_cond; 113204076Spjd/* 114204076Spjd * There is one send list for every component. One requests is placed on all 115204076Spjd * send lists - each component gets the same request, but each component is 116204076Spjd * responsible for managing his own send list. 117204076Spjd */ 118204076Spjdstatic TAILQ_HEAD(, hio) *hio_send_list; 119204076Spjdstatic pthread_mutex_t *hio_send_list_lock; 120204076Spjdstatic pthread_cond_t *hio_send_list_cond; 121204076Spjd/* 122204076Spjd * There is one recv list for every component, although local components don't 123204076Spjd * use recv lists as local requests are done synchronously. 124204076Spjd */ 125204076Spjdstatic TAILQ_HEAD(, hio) *hio_recv_list; 126204076Spjdstatic pthread_mutex_t *hio_recv_list_lock; 127204076Spjdstatic pthread_cond_t *hio_recv_list_cond; 128204076Spjd/* 129204076Spjd * Request is placed on done list by the slowest component (the one that 130204076Spjd * decreased hio_countdown from 1 to 0). 131204076Spjd */ 132204076Spjdstatic TAILQ_HEAD(, hio) hio_done_list; 133204076Spjdstatic pthread_mutex_t hio_done_list_lock; 134204076Spjdstatic pthread_cond_t hio_done_list_cond; 135204076Spjd/* 136204076Spjd * Structure below are for interaction with sync thread. 137204076Spjd */ 138204076Spjdstatic bool sync_inprogress; 139204076Spjdstatic pthread_mutex_t sync_lock; 140204076Spjdstatic pthread_cond_t sync_cond; 141204076Spjd/* 142204076Spjd * The lock below allows to synchornize access to remote connections. 143204076Spjd */ 144204076Spjdstatic pthread_rwlock_t *hio_remote_lock; 145204076Spjd 146204076Spjd/* 147204076Spjd * Lock to synchronize metadata updates. Also synchronize access to 148204076Spjd * hr_primary_localcnt and hr_primary_remotecnt fields. 149204076Spjd */ 150204076Spjdstatic pthread_mutex_t metadata_lock; 151204076Spjd 152204076Spjd/* 153204076Spjd * Maximum number of outstanding I/O requests. 154204076Spjd */ 155204076Spjd#define HAST_HIO_MAX 256 156204076Spjd/* 157204076Spjd * Number of components. At this point there are only two components: local 158204076Spjd * and remote, but in the future it might be possible to use multiple local 159204076Spjd * and remote components. 160204076Spjd */ 161204076Spjd#define HAST_NCOMPONENTS 2 162204076Spjd 163204076Spjd#define ISCONNECTED(res, no) \ 164204076Spjd ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL) 165204076Spjd 166204076Spjd#define QUEUE_INSERT1(hio, name, ncomp) do { \ 167204076Spjd bool _wakeup; \ 168204076Spjd \ 169204076Spjd mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 170204076Spjd _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \ 171204076Spjd TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \ 172204076Spjd hio_next[(ncomp)]); \ 173204076Spjd mtx_unlock(&hio_##name##_list_lock[ncomp]); \ 174204076Spjd if (_wakeup) \ 175204076Spjd cv_signal(&hio_##name##_list_cond[(ncomp)]); \ 176204076Spjd} while (0) 177204076Spjd#define QUEUE_INSERT2(hio, name) do { \ 178204076Spjd bool _wakeup; \ 179204076Spjd \ 180204076Spjd mtx_lock(&hio_##name##_list_lock); \ 181204076Spjd _wakeup = TAILQ_EMPTY(&hio_##name##_list); \ 182204076Spjd TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\ 183204076Spjd mtx_unlock(&hio_##name##_list_lock); \ 184204076Spjd if (_wakeup) \ 185204076Spjd cv_signal(&hio_##name##_list_cond); \ 186204076Spjd} while (0) 187214692Spjd#define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \ 188214692Spjd bool _last; \ 189214692Spjd \ 190204076Spjd mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 191214692Spjd _last = false; \ 192214692Spjd while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \ 193214692Spjd cv_timedwait(&hio_##name##_list_cond[(ncomp)], \ 194214692Spjd &hio_##name##_list_lock[(ncomp)], (timeout)); \ 195219864Spjd if ((timeout) != 0) \ 196214692Spjd _last = true; \ 197204076Spjd } \ 198214692Spjd if (hio != NULL) { \ 199214692Spjd TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \ 200214692Spjd hio_next[(ncomp)]); \ 201214692Spjd } \ 202204076Spjd mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \ 203204076Spjd} while (0) 204204076Spjd#define QUEUE_TAKE2(hio, name) do { \ 205204076Spjd mtx_lock(&hio_##name##_list_lock); \ 206204076Spjd while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 207204076Spjd cv_wait(&hio_##name##_list_cond, \ 208204076Spjd &hio_##name##_list_lock); \ 209204076Spjd } \ 210204076Spjd TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \ 211204076Spjd mtx_unlock(&hio_##name##_list_lock); \ 212204076Spjd} while (0) 213204076Spjd 214209183Spjd#define SYNCREQ(hio) do { \ 215209183Spjd (hio)->hio_ggio.gctl_unit = -1; \ 216209183Spjd (hio)->hio_ggio.gctl_seq = 1; \ 217209183Spjd} while (0) 218204076Spjd#define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1) 219204076Spjd#define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0) 220204076Spjd#define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2) 221204076Spjd 222204076Spjdstatic struct hast_resource *gres; 223204076Spjd 224204076Spjdstatic pthread_mutex_t range_lock; 225204076Spjdstatic struct rangelocks *range_regular; 226204076Spjdstatic bool range_regular_wait; 227204076Spjdstatic pthread_cond_t range_regular_cond; 228204076Spjdstatic struct rangelocks *range_sync; 229204076Spjdstatic bool range_sync_wait; 230204076Spjdstatic pthread_cond_t range_sync_cond; 231220898Spjdstatic bool fullystarted; 232204076Spjd 233204076Spjdstatic void *ggate_recv_thread(void *arg); 234204076Spjdstatic void *local_send_thread(void *arg); 235204076Spjdstatic void *remote_send_thread(void *arg); 236204076Spjdstatic void *remote_recv_thread(void *arg); 237204076Spjdstatic void *ggate_send_thread(void *arg); 238204076Spjdstatic void *sync_thread(void *arg); 239204076Spjdstatic void *guard_thread(void *arg); 240204076Spjd 241211982Spjdstatic void 242204076Spjdcleanup(struct hast_resource *res) 243204076Spjd{ 244204076Spjd int rerrno; 245204076Spjd 246204076Spjd /* Remember errno. */ 247204076Spjd rerrno = errno; 248204076Spjd 249204076Spjd /* Destroy ggate provider if we created one. */ 250204076Spjd if (res->hr_ggateunit >= 0) { 251204076Spjd struct g_gate_ctl_destroy ggiod; 252204076Spjd 253213533Spjd bzero(&ggiod, sizeof(ggiod)); 254204076Spjd ggiod.gctl_version = G_GATE_VERSION; 255204076Spjd ggiod.gctl_unit = res->hr_ggateunit; 256204076Spjd ggiod.gctl_force = 1; 257229945Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) { 258213531Spjd pjdlog_errno(LOG_WARNING, 259213531Spjd "Unable to destroy hast/%s device", 260204076Spjd res->hr_provname); 261204076Spjd } 262204076Spjd res->hr_ggateunit = -1; 263204076Spjd } 264204076Spjd 265204076Spjd /* Restore errno. */ 266204076Spjd errno = rerrno; 267204076Spjd} 268204076Spjd 269212899Spjdstatic __dead2 void 270204076Spjdprimary_exit(int exitcode, const char *fmt, ...) 271204076Spjd{ 272204076Spjd va_list ap; 273204076Spjd 274218138Spjd PJDLOG_ASSERT(exitcode != EX_OK); 275204076Spjd va_start(ap, fmt); 276204076Spjd pjdlogv_errno(LOG_ERR, fmt, ap); 277204076Spjd va_end(ap); 278204076Spjd cleanup(gres); 279204076Spjd exit(exitcode); 280204076Spjd} 281204076Spjd 282212899Spjdstatic __dead2 void 283204076Spjdprimary_exitx(int exitcode, const char *fmt, ...) 284204076Spjd{ 285204076Spjd va_list ap; 286204076Spjd 287204076Spjd va_start(ap, fmt); 288204076Spjd pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap); 289204076Spjd va_end(ap); 290204076Spjd cleanup(gres); 291204076Spjd exit(exitcode); 292204076Spjd} 293204076Spjd 294204076Spjdstatic int 295204076Spjdhast_activemap_flush(struct hast_resource *res) 296204076Spjd{ 297204076Spjd const unsigned char *buf; 298204076Spjd size_t size; 299204076Spjd 300204076Spjd buf = activemap_bitmap(res->hr_amp, &size); 301218138Spjd PJDLOG_ASSERT(buf != NULL); 302218138Spjd PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0); 303204076Spjd if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) != 304204076Spjd (ssize_t)size) { 305225786Spjd pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk"); 306204076Spjd return (-1); 307204076Spjd } 308225830Spjd if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) { 309225830Spjd if (errno == EOPNOTSUPP) { 310225830Spjd pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.", 311225830Spjd res->hr_localpath); 312225830Spjd res->hr_metaflush = 0; 313225830Spjd } else { 314225830Spjd pjdlog_errno(LOG_ERR, 315225830Spjd "Unable to flush disk cache on activemap update"); 316225830Spjd return (-1); 317225830Spjd } 318225830Spjd } 319204076Spjd return (0); 320204076Spjd} 321204076Spjd 322210881Spjdstatic bool 323210881Spjdreal_remote(const struct hast_resource *res) 324210881Spjd{ 325210881Spjd 326210881Spjd return (strcmp(res->hr_remoteaddr, "none") != 0); 327210881Spjd} 328210881Spjd 329204076Spjdstatic void 330204076Spjdinit_environment(struct hast_resource *res __unused) 331204076Spjd{ 332204076Spjd struct hio *hio; 333204076Spjd unsigned int ii, ncomps; 334204076Spjd 335204076Spjd /* 336204076Spjd * In the future it might be per-resource value. 337204076Spjd */ 338204076Spjd ncomps = HAST_NCOMPONENTS; 339204076Spjd 340204076Spjd /* 341204076Spjd * Allocate memory needed by lists. 342204076Spjd */ 343204076Spjd hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps); 344204076Spjd if (hio_send_list == NULL) { 345204076Spjd primary_exitx(EX_TEMPFAIL, 346204076Spjd "Unable to allocate %zu bytes of memory for send lists.", 347204076Spjd sizeof(hio_send_list[0]) * ncomps); 348204076Spjd } 349204076Spjd hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps); 350204076Spjd if (hio_send_list_lock == NULL) { 351204076Spjd primary_exitx(EX_TEMPFAIL, 352204076Spjd "Unable to allocate %zu bytes of memory for send list locks.", 353204076Spjd sizeof(hio_send_list_lock[0]) * ncomps); 354204076Spjd } 355204076Spjd hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps); 356204076Spjd if (hio_send_list_cond == NULL) { 357204076Spjd primary_exitx(EX_TEMPFAIL, 358204076Spjd "Unable to allocate %zu bytes of memory for send list condition variables.", 359204076Spjd sizeof(hio_send_list_cond[0]) * ncomps); 360204076Spjd } 361204076Spjd hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps); 362204076Spjd if (hio_recv_list == NULL) { 363204076Spjd primary_exitx(EX_TEMPFAIL, 364204076Spjd "Unable to allocate %zu bytes of memory for recv lists.", 365204076Spjd sizeof(hio_recv_list[0]) * ncomps); 366204076Spjd } 367204076Spjd hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps); 368204076Spjd if (hio_recv_list_lock == NULL) { 369204076Spjd primary_exitx(EX_TEMPFAIL, 370204076Spjd "Unable to allocate %zu bytes of memory for recv list locks.", 371204076Spjd sizeof(hio_recv_list_lock[0]) * ncomps); 372204076Spjd } 373204076Spjd hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps); 374204076Spjd if (hio_recv_list_cond == NULL) { 375204076Spjd primary_exitx(EX_TEMPFAIL, 376204076Spjd "Unable to allocate %zu bytes of memory for recv list condition variables.", 377204076Spjd sizeof(hio_recv_list_cond[0]) * ncomps); 378204076Spjd } 379204076Spjd hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps); 380204076Spjd if (hio_remote_lock == NULL) { 381204076Spjd primary_exitx(EX_TEMPFAIL, 382204076Spjd "Unable to allocate %zu bytes of memory for remote connections locks.", 383204076Spjd sizeof(hio_remote_lock[0]) * ncomps); 384204076Spjd } 385204076Spjd 386204076Spjd /* 387204076Spjd * Initialize lists, their locks and theirs condition variables. 388204076Spjd */ 389204076Spjd TAILQ_INIT(&hio_free_list); 390204076Spjd mtx_init(&hio_free_list_lock); 391204076Spjd cv_init(&hio_free_list_cond); 392204076Spjd for (ii = 0; ii < HAST_NCOMPONENTS; ii++) { 393204076Spjd TAILQ_INIT(&hio_send_list[ii]); 394204076Spjd mtx_init(&hio_send_list_lock[ii]); 395204076Spjd cv_init(&hio_send_list_cond[ii]); 396204076Spjd TAILQ_INIT(&hio_recv_list[ii]); 397204076Spjd mtx_init(&hio_recv_list_lock[ii]); 398204076Spjd cv_init(&hio_recv_list_cond[ii]); 399204076Spjd rw_init(&hio_remote_lock[ii]); 400204076Spjd } 401204076Spjd TAILQ_INIT(&hio_done_list); 402204076Spjd mtx_init(&hio_done_list_lock); 403204076Spjd cv_init(&hio_done_list_cond); 404204076Spjd mtx_init(&metadata_lock); 405204076Spjd 406204076Spjd /* 407204076Spjd * Allocate requests pool and initialize requests. 408204076Spjd */ 409204076Spjd for (ii = 0; ii < HAST_HIO_MAX; ii++) { 410204076Spjd hio = malloc(sizeof(*hio)); 411204076Spjd if (hio == NULL) { 412204076Spjd primary_exitx(EX_TEMPFAIL, 413204076Spjd "Unable to allocate %zu bytes of memory for hio request.", 414204076Spjd sizeof(*hio)); 415204076Spjd } 416204076Spjd hio->hio_countdown = 0; 417204076Spjd hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps); 418204076Spjd if (hio->hio_errors == NULL) { 419204076Spjd primary_exitx(EX_TEMPFAIL, 420204076Spjd "Unable allocate %zu bytes of memory for hio errors.", 421204076Spjd sizeof(hio->hio_errors[0]) * ncomps); 422204076Spjd } 423204076Spjd hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps); 424204076Spjd if (hio->hio_next == NULL) { 425204076Spjd primary_exitx(EX_TEMPFAIL, 426204076Spjd "Unable allocate %zu bytes of memory for hio_next field.", 427204076Spjd sizeof(hio->hio_next[0]) * ncomps); 428204076Spjd } 429204076Spjd hio->hio_ggio.gctl_version = G_GATE_VERSION; 430204076Spjd hio->hio_ggio.gctl_data = malloc(MAXPHYS); 431204076Spjd if (hio->hio_ggio.gctl_data == NULL) { 432204076Spjd primary_exitx(EX_TEMPFAIL, 433204076Spjd "Unable to allocate %zu bytes of memory for gctl_data.", 434204076Spjd MAXPHYS); 435204076Spjd } 436204076Spjd hio->hio_ggio.gctl_length = MAXPHYS; 437204076Spjd hio->hio_ggio.gctl_error = 0; 438204076Spjd TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next); 439204076Spjd } 440204076Spjd} 441204076Spjd 442214284Spjdstatic bool 443214284Spjdinit_resuid(struct hast_resource *res) 444214284Spjd{ 445214284Spjd 446214284Spjd mtx_lock(&metadata_lock); 447214284Spjd if (res->hr_resuid != 0) { 448214284Spjd mtx_unlock(&metadata_lock); 449214284Spjd return (false); 450214284Spjd } else { 451214284Spjd /* Initialize unique resource identifier. */ 452214284Spjd arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid)); 453214284Spjd mtx_unlock(&metadata_lock); 454229945Spjd if (metadata_write(res) == -1) 455214284Spjd exit(EX_NOINPUT); 456214284Spjd return (true); 457214284Spjd } 458214284Spjd} 459214284Spjd 460204076Spjdstatic void 461204076Spjdinit_local(struct hast_resource *res) 462204076Spjd{ 463204076Spjd unsigned char *buf; 464204076Spjd size_t mapsize; 465204076Spjd 466229945Spjd if (metadata_read(res, true) == -1) 467204076Spjd exit(EX_NOINPUT); 468204076Spjd mtx_init(&res->hr_amp_lock); 469204076Spjd if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 470229945Spjd res->hr_local_sectorsize, res->hr_keepdirty) == -1) { 471204076Spjd primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 472204076Spjd } 473204076Spjd mtx_init(&range_lock); 474204076Spjd cv_init(&range_regular_cond); 475229945Spjd if (rangelock_init(&range_regular) == -1) 476204076Spjd primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 477204076Spjd cv_init(&range_sync_cond); 478229945Spjd if (rangelock_init(&range_sync) == -1) 479204076Spjd primary_exit(EX_TEMPFAIL, "Unable to create sync range lock"); 480204076Spjd mapsize = activemap_ondisk_size(res->hr_amp); 481204076Spjd buf = calloc(1, mapsize); 482204076Spjd if (buf == NULL) { 483204076Spjd primary_exitx(EX_TEMPFAIL, 484204076Spjd "Unable to allocate buffer for activemap."); 485204076Spjd } 486204076Spjd if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 487204076Spjd (ssize_t)mapsize) { 488204076Spjd primary_exit(EX_NOINPUT, "Unable to read activemap"); 489204076Spjd } 490204076Spjd activemap_copyin(res->hr_amp, buf, mapsize); 491209181Spjd free(buf); 492204076Spjd if (res->hr_resuid != 0) 493204076Spjd return; 494204076Spjd /* 495214284Spjd * We're using provider for the first time. Initialize local and remote 496214284Spjd * counters. We don't initialize resuid here, as we want to do it just 497214284Spjd * in time. The reason for this is that we want to inform secondary 498214284Spjd * that there were no writes yet, so there is no need to synchronize 499214284Spjd * anything. 500204076Spjd */ 501219844Spjd res->hr_primary_localcnt = 0; 502204076Spjd res->hr_primary_remotecnt = 0; 503229945Spjd if (metadata_write(res) == -1) 504204076Spjd exit(EX_NOINPUT); 505204076Spjd} 506204076Spjd 507218218Spjdstatic int 508218218Spjdprimary_connect(struct hast_resource *res, struct proto_conn **connp) 509218218Spjd{ 510218218Spjd struct proto_conn *conn; 511218218Spjd int16_t val; 512218218Spjd 513218218Spjd val = 1; 514229945Spjd if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) { 515218218Spjd primary_exit(EX_TEMPFAIL, 516218218Spjd "Unable to send connection request to parent"); 517218218Spjd } 518229945Spjd if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) { 519218218Spjd primary_exit(EX_TEMPFAIL, 520218218Spjd "Unable to receive reply to connection request from parent"); 521218218Spjd } 522218218Spjd if (val != 0) { 523218218Spjd errno = val; 524218218Spjd pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 525218218Spjd res->hr_remoteaddr); 526218218Spjd return (-1); 527218218Spjd } 528229945Spjd if (proto_connection_recv(res->hr_conn, true, &conn) == -1) { 529218218Spjd primary_exit(EX_TEMPFAIL, 530218218Spjd "Unable to receive connection from parent"); 531218218Spjd } 532229945Spjd if (proto_connect_wait(conn, res->hr_timeout) == -1) { 533218218Spjd pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 534218218Spjd res->hr_remoteaddr); 535218218Spjd proto_close(conn); 536218218Spjd return (-1); 537218218Spjd } 538218218Spjd /* Error in setting timeout is not critical, but why should it fail? */ 539229945Spjd if (proto_timeout(conn, res->hr_timeout) == -1) 540218218Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 541218218Spjd 542218218Spjd *connp = conn; 543218218Spjd 544218218Spjd return (0); 545218218Spjd} 546246922Spjd 547238120Spjd/* 548238120Spjd * Function instructs GEOM_GATE to handle reads directly from within the kernel. 549238120Spjd */ 550238120Spjdstatic void 551238120Spjdenable_direct_reads(struct hast_resource *res) 552238120Spjd{ 553238120Spjd struct g_gate_ctl_modify ggiomodify; 554218218Spjd 555238120Spjd bzero(&ggiomodify, sizeof(ggiomodify)); 556238120Spjd ggiomodify.gctl_version = G_GATE_VERSION; 557238120Spjd ggiomodify.gctl_unit = res->hr_ggateunit; 558238120Spjd ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET; 559238120Spjd strlcpy(ggiomodify.gctl_readprov, res->hr_localpath, 560238120Spjd sizeof(ggiomodify.gctl_readprov)); 561238120Spjd ggiomodify.gctl_readoffset = res->hr_localoff; 562238120Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0) 563238120Spjd pjdlog_debug(1, "Direct reads enabled."); 564238120Spjd else 565238120Spjd pjdlog_errno(LOG_WARNING, "Failed to enable direct reads"); 566238120Spjd} 567238120Spjd 568220898Spjdstatic int 569205738Spjdinit_remote(struct hast_resource *res, struct proto_conn **inp, 570205738Spjd struct proto_conn **outp) 571204076Spjd{ 572205738Spjd struct proto_conn *in, *out; 573204076Spjd struct nv *nvout, *nvin; 574204076Spjd const unsigned char *token; 575204076Spjd unsigned char *map; 576204076Spjd const char *errmsg; 577204076Spjd int32_t extentsize; 578204076Spjd int64_t datasize; 579204076Spjd uint32_t mapsize; 580246922Spjd uint8_t version; 581204076Spjd size_t size; 582220898Spjd int error; 583204076Spjd 584218138Spjd PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 585218138Spjd PJDLOG_ASSERT(real_remote(res)); 586205738Spjd 587205738Spjd in = out = NULL; 588211983Spjd errmsg = NULL; 589205738Spjd 590218218Spjd if (primary_connect(res, &out) == -1) 591220898Spjd return (ECONNREFUSED); 592218218Spjd 593220898Spjd error = ECONNABORTED; 594220898Spjd 595204076Spjd /* 596204076Spjd * First handshake step. 597204076Spjd * Setup outgoing connection with remote node. 598204076Spjd */ 599204076Spjd nvout = nv_alloc(); 600204076Spjd nv_add_string(nvout, res->hr_name, "resource"); 601246922Spjd nv_add_uint8(nvout, HAST_PROTO_VERSION, "version"); 602204076Spjd if (nv_error(nvout) != 0) { 603204076Spjd pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 604204076Spjd "Unable to allocate header for connection with %s", 605204076Spjd res->hr_remoteaddr); 606204076Spjd nv_free(nvout); 607204076Spjd goto close; 608204076Spjd } 609229945Spjd if (hast_proto_send(res, out, nvout, NULL, 0) == -1) { 610204076Spjd pjdlog_errno(LOG_WARNING, 611204076Spjd "Unable to send handshake header to %s", 612204076Spjd res->hr_remoteaddr); 613204076Spjd nv_free(nvout); 614204076Spjd goto close; 615204076Spjd } 616204076Spjd nv_free(nvout); 617229945Spjd if (hast_proto_recv_hdr(out, &nvin) == -1) { 618204076Spjd pjdlog_errno(LOG_WARNING, 619204076Spjd "Unable to receive handshake header from %s", 620204076Spjd res->hr_remoteaddr); 621204076Spjd goto close; 622204076Spjd } 623204076Spjd errmsg = nv_get_string(nvin, "errmsg"); 624204076Spjd if (errmsg != NULL) { 625204076Spjd pjdlog_warning("%s", errmsg); 626220898Spjd if (nv_exists(nvin, "wait")) 627220898Spjd error = EBUSY; 628204076Spjd nv_free(nvin); 629204076Spjd goto close; 630204076Spjd } 631246922Spjd version = nv_get_uint8(nvin, "version"); 632246922Spjd if (version == 0) { 633246922Spjd /* 634246922Spjd * If no version is sent, it means this is protocol version 1. 635246922Spjd */ 636246922Spjd version = 1; 637246922Spjd } 638246922Spjd if (version > HAST_PROTO_VERSION) { 639246922Spjd pjdlog_warning("Invalid version received (%hhu).", version); 640246922Spjd nv_free(nvin); 641246922Spjd goto close; 642246922Spjd } 643246922Spjd res->hr_version = version; 644246922Spjd pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version); 645204076Spjd token = nv_get_uint8_array(nvin, &size, "token"); 646204076Spjd if (token == NULL) { 647204076Spjd pjdlog_warning("Handshake header from %s has no 'token' field.", 648204076Spjd res->hr_remoteaddr); 649204076Spjd nv_free(nvin); 650204076Spjd goto close; 651204076Spjd } 652204076Spjd if (size != sizeof(res->hr_token)) { 653204076Spjd pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 654204076Spjd res->hr_remoteaddr, size, sizeof(res->hr_token)); 655204076Spjd nv_free(nvin); 656204076Spjd goto close; 657204076Spjd } 658204076Spjd bcopy(token, res->hr_token, sizeof(res->hr_token)); 659204076Spjd nv_free(nvin); 660204076Spjd 661204076Spjd /* 662204076Spjd * Second handshake step. 663204076Spjd * Setup incoming connection with remote node. 664204076Spjd */ 665218218Spjd if (primary_connect(res, &in) == -1) 666204076Spjd goto close; 667218218Spjd 668204076Spjd nvout = nv_alloc(); 669204076Spjd nv_add_string(nvout, res->hr_name, "resource"); 670204076Spjd nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 671204076Spjd "token"); 672214284Spjd if (res->hr_resuid == 0) { 673214284Spjd /* 674214284Spjd * The resuid field was not yet initialized. 675214284Spjd * Because we do synchronization inside init_resuid(), it is 676214284Spjd * possible that someone already initialized it, the function 677214284Spjd * will return false then, but if we successfully initialized 678214284Spjd * it, we will get true. True means that there were no writes 679214284Spjd * to this resource yet and we want to inform secondary that 680214284Spjd * synchronization is not needed by sending "virgin" argument. 681214284Spjd */ 682214284Spjd if (init_resuid(res)) 683214284Spjd nv_add_int8(nvout, 1, "virgin"); 684214284Spjd } 685204076Spjd nv_add_uint64(nvout, res->hr_resuid, "resuid"); 686204076Spjd nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 687204076Spjd nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 688204076Spjd if (nv_error(nvout) != 0) { 689204076Spjd pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 690204076Spjd "Unable to allocate header for connection with %s", 691204076Spjd res->hr_remoteaddr); 692204076Spjd nv_free(nvout); 693204076Spjd goto close; 694204076Spjd } 695229945Spjd if (hast_proto_send(res, in, nvout, NULL, 0) == -1) { 696204076Spjd pjdlog_errno(LOG_WARNING, 697204076Spjd "Unable to send handshake header to %s", 698204076Spjd res->hr_remoteaddr); 699204076Spjd nv_free(nvout); 700204076Spjd goto close; 701204076Spjd } 702204076Spjd nv_free(nvout); 703229945Spjd if (hast_proto_recv_hdr(out, &nvin) == -1) { 704204076Spjd pjdlog_errno(LOG_WARNING, 705204076Spjd "Unable to receive handshake header from %s", 706204076Spjd res->hr_remoteaddr); 707204076Spjd goto close; 708204076Spjd } 709204076Spjd errmsg = nv_get_string(nvin, "errmsg"); 710204076Spjd if (errmsg != NULL) { 711204076Spjd pjdlog_warning("%s", errmsg); 712204076Spjd nv_free(nvin); 713204076Spjd goto close; 714204076Spjd } 715204076Spjd datasize = nv_get_int64(nvin, "datasize"); 716204076Spjd if (datasize != res->hr_datasize) { 717204076Spjd pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 718204076Spjd (intmax_t)res->hr_datasize, (intmax_t)datasize); 719204076Spjd nv_free(nvin); 720204076Spjd goto close; 721204076Spjd } 722204076Spjd extentsize = nv_get_int32(nvin, "extentsize"); 723204076Spjd if (extentsize != res->hr_extentsize) { 724204076Spjd pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 725204076Spjd (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 726204076Spjd nv_free(nvin); 727204076Spjd goto close; 728204076Spjd } 729204076Spjd res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 730204076Spjd res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 731204076Spjd res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 732238120Spjd if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) 733238120Spjd enable_direct_reads(res); 734220865Spjd if (nv_exists(nvin, "virgin")) { 735220865Spjd /* 736220865Spjd * Secondary was reinitialized, bump localcnt if it is 0 as 737220865Spjd * only we have the data. 738220865Spjd */ 739220865Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY); 740220865Spjd PJDLOG_ASSERT(res->hr_secondary_localcnt == 0); 741220865Spjd 742220865Spjd if (res->hr_primary_localcnt == 0) { 743220865Spjd PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0); 744220865Spjd 745220865Spjd mtx_lock(&metadata_lock); 746220865Spjd res->hr_primary_localcnt++; 747220865Spjd pjdlog_debug(1, "Increasing localcnt to %ju.", 748220865Spjd (uintmax_t)res->hr_primary_localcnt); 749220865Spjd (void)metadata_write(res); 750220865Spjd mtx_unlock(&metadata_lock); 751220865Spjd } 752220865Spjd } 753204076Spjd map = NULL; 754204076Spjd mapsize = nv_get_uint32(nvin, "mapsize"); 755204076Spjd if (mapsize > 0) { 756204076Spjd map = malloc(mapsize); 757204076Spjd if (map == NULL) { 758204076Spjd pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 759204076Spjd (uintmax_t)mapsize); 760204076Spjd nv_free(nvin); 761204076Spjd goto close; 762204076Spjd } 763204076Spjd /* 764204076Spjd * Remote node have some dirty extents on its own, lets 765204076Spjd * download its activemap. 766204076Spjd */ 767205738Spjd if (hast_proto_recv_data(res, out, nvin, map, 768229945Spjd mapsize) == -1) { 769204076Spjd pjdlog_errno(LOG_ERR, 770204076Spjd "Unable to receive remote activemap"); 771204076Spjd nv_free(nvin); 772204076Spjd free(map); 773204076Spjd goto close; 774204076Spjd } 775204076Spjd /* 776204076Spjd * Merge local and remote bitmaps. 777204076Spjd */ 778204076Spjd activemap_merge(res->hr_amp, map, mapsize); 779204076Spjd free(map); 780204076Spjd /* 781204076Spjd * Now that we merged bitmaps from both nodes, flush it to the 782204076Spjd * disk before we start to synchronize. 783204076Spjd */ 784204076Spjd (void)hast_activemap_flush(res); 785204076Spjd } 786214274Spjd nv_free(nvin); 787223181Strociny#ifdef notyet 788220271Spjd /* Setup directions. */ 789220271Spjd if (proto_send(out, NULL, 0) == -1) 790220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 791220271Spjd if (proto_recv(in, NULL, 0) == -1) 792220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 793223181Strociny#endif 794204076Spjd pjdlog_info("Connected to %s.", res->hr_remoteaddr); 795246922Spjd if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC && 796246922Spjd res->hr_version < 2) { 797246922Spjd pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode."); 798246922Spjd res->hr_replication = HAST_REPLICATION_FULLSYNC; 799246922Spjd } else if (res->hr_replication != res->hr_original_replication) { 800246922Spjd /* 801246922Spjd * This is in case hastd disconnected and was upgraded. 802246922Spjd */ 803246922Spjd res->hr_replication = res->hr_original_replication; 804246922Spjd } 805205738Spjd if (inp != NULL && outp != NULL) { 806205738Spjd *inp = in; 807205738Spjd *outp = out; 808205738Spjd } else { 809205738Spjd res->hr_remotein = in; 810205738Spjd res->hr_remoteout = out; 811205738Spjd } 812212038Spjd event_send(res, EVENT_CONNECT); 813220898Spjd return (0); 814205738Spjdclose: 815211983Spjd if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0) 816212038Spjd event_send(res, EVENT_SPLITBRAIN); 817205738Spjd proto_close(out); 818205738Spjd if (in != NULL) 819205738Spjd proto_close(in); 820220898Spjd return (error); 821205738Spjd} 822205738Spjd 823205738Spjdstatic void 824205738Spjdsync_start(void) 825205738Spjd{ 826205738Spjd 827204076Spjd mtx_lock(&sync_lock); 828204076Spjd sync_inprogress = true; 829204076Spjd mtx_unlock(&sync_lock); 830204076Spjd cv_signal(&sync_cond); 831204076Spjd} 832204076Spjd 833204076Spjdstatic void 834211878Spjdsync_stop(void) 835211878Spjd{ 836211878Spjd 837211878Spjd mtx_lock(&sync_lock); 838211878Spjd if (sync_inprogress) 839211878Spjd sync_inprogress = false; 840211878Spjd mtx_unlock(&sync_lock); 841211878Spjd} 842211878Spjd 843211878Spjdstatic void 844204076Spjdinit_ggate(struct hast_resource *res) 845204076Spjd{ 846204076Spjd struct g_gate_ctl_create ggiocreate; 847204076Spjd struct g_gate_ctl_cancel ggiocancel; 848204076Spjd 849204076Spjd /* 850204076Spjd * We communicate with ggate via /dev/ggctl. Open it. 851204076Spjd */ 852204076Spjd res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 853229945Spjd if (res->hr_ggatefd == -1) 854204076Spjd primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 855204076Spjd /* 856204076Spjd * Create provider before trying to connect, as connection failure 857204076Spjd * is not critical, but may take some time. 858204076Spjd */ 859213533Spjd bzero(&ggiocreate, sizeof(ggiocreate)); 860204076Spjd ggiocreate.gctl_version = G_GATE_VERSION; 861204076Spjd ggiocreate.gctl_mediasize = res->hr_datasize; 862204076Spjd ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 863204076Spjd ggiocreate.gctl_flags = 0; 864220266Spjd ggiocreate.gctl_maxcount = 0; 865204076Spjd ggiocreate.gctl_timeout = 0; 866204076Spjd ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 867204076Spjd snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 868204076Spjd res->hr_provname); 869204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 870204076Spjd pjdlog_info("Device hast/%s created.", res->hr_provname); 871204076Spjd res->hr_ggateunit = ggiocreate.gctl_unit; 872204076Spjd return; 873204076Spjd } 874204076Spjd if (errno != EEXIST) { 875204076Spjd primary_exit(EX_OSERR, "Unable to create hast/%s device", 876204076Spjd res->hr_provname); 877204076Spjd } 878204076Spjd pjdlog_debug(1, 879204076Spjd "Device hast/%s already exists, we will try to take it over.", 880204076Spjd res->hr_provname); 881204076Spjd /* 882204076Spjd * If we received EEXIST, we assume that the process who created the 883204076Spjd * provider died and didn't clean up. In that case we will start from 884204076Spjd * where he left of. 885204076Spjd */ 886213533Spjd bzero(&ggiocancel, sizeof(ggiocancel)); 887204076Spjd ggiocancel.gctl_version = G_GATE_VERSION; 888204076Spjd ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 889204076Spjd snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 890204076Spjd res->hr_provname); 891204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 892204076Spjd pjdlog_info("Device hast/%s recovered.", res->hr_provname); 893204076Spjd res->hr_ggateunit = ggiocancel.gctl_unit; 894204076Spjd return; 895204076Spjd } 896204076Spjd primary_exit(EX_OSERR, "Unable to take over hast/%s device", 897204076Spjd res->hr_provname); 898204076Spjd} 899204076Spjd 900204076Spjdvoid 901204076Spjdhastd_primary(struct hast_resource *res) 902204076Spjd{ 903204076Spjd pthread_t td; 904204076Spjd pid_t pid; 905219482Strociny int error, mode, debuglevel; 906204076Spjd 907204076Spjd /* 908218218Spjd * Create communication channel for sending control commands from 909218218Spjd * parent to child. 910204076Spjd */ 911229945Spjd if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) { 912218042Spjd /* TODO: There's no need for this to be fatal error. */ 913204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 914212034Spjd pjdlog_exit(EX_OSERR, 915204076Spjd "Unable to create control sockets between parent and child"); 916204076Spjd } 917212038Spjd /* 918218218Spjd * Create communication channel for sending events from child to parent. 919212038Spjd */ 920229945Spjd if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) { 921218042Spjd /* TODO: There's no need for this to be fatal error. */ 922212038Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 923212038Spjd pjdlog_exit(EX_OSERR, 924212038Spjd "Unable to create event sockets between child and parent"); 925212038Spjd } 926218218Spjd /* 927218218Spjd * Create communication channel for sending connection requests from 928218218Spjd * child to parent. 929218218Spjd */ 930229945Spjd if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) { 931218218Spjd /* TODO: There's no need for this to be fatal error. */ 932218218Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 933218218Spjd pjdlog_exit(EX_OSERR, 934218218Spjd "Unable to create connection sockets between child and parent"); 935218218Spjd } 936204076Spjd 937204076Spjd pid = fork(); 938229744Spjd if (pid == -1) { 939218042Spjd /* TODO: There's no need for this to be fatal error. */ 940204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 941212034Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to fork"); 942204076Spjd } 943204076Spjd 944204076Spjd if (pid > 0) { 945204076Spjd /* This is parent. */ 946212038Spjd /* Declare that we are receiver. */ 947212038Spjd proto_recv(res->hr_event, NULL, 0); 948218218Spjd proto_recv(res->hr_conn, NULL, 0); 949218043Spjd /* Declare that we are sender. */ 950218043Spjd proto_send(res->hr_ctrl, NULL, 0); 951204076Spjd res->hr_workerpid = pid; 952204076Spjd return; 953204076Spjd } 954211977Spjd 955211984Spjd gres = res; 956218043Spjd mode = pjdlog_mode_get(); 957219482Strociny debuglevel = pjdlog_debug_get(); 958211984Spjd 959218043Spjd /* Declare that we are sender. */ 960218043Spjd proto_send(res->hr_event, NULL, 0); 961218218Spjd proto_send(res->hr_conn, NULL, 0); 962218043Spjd /* Declare that we are receiver. */ 963218043Spjd proto_recv(res->hr_ctrl, NULL, 0); 964218043Spjd descriptors_cleanup(res); 965204076Spjd 966218045Spjd descriptors_assert(res, mode); 967218045Spjd 968218043Spjd pjdlog_init(mode); 969219482Strociny pjdlog_debug_set(debuglevel); 970218043Spjd pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 971220005Spjd setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role)); 972204076Spjd 973204076Spjd init_local(res); 974213007Spjd init_ggate(res); 975213007Spjd init_environment(res); 976217784Spjd 977221899Spjd if (drop_privs(res) != 0) { 978218049Spjd cleanup(res); 979218049Spjd exit(EX_CONFIG); 980218049Spjd } 981218214Spjd pjdlog_info("Privileges successfully dropped."); 982218049Spjd 983213007Spjd /* 984213530Spjd * Create the guard thread first, so we can handle signals from the 985229778Suqs * very beginning. 986213530Spjd */ 987213530Spjd error = pthread_create(&td, NULL, guard_thread, res); 988218138Spjd PJDLOG_ASSERT(error == 0); 989213530Spjd /* 990213007Spjd * Create the control thread before sending any event to the parent, 991213007Spjd * as we can deadlock when parent sends control request to worker, 992213007Spjd * but worker has no control thread started yet, so parent waits. 993213007Spjd * In the meantime worker sends an event to the parent, but parent 994213007Spjd * is unable to handle the event, because it waits for control 995213007Spjd * request response. 996213007Spjd */ 997213007Spjd error = pthread_create(&td, NULL, ctrl_thread, res); 998218138Spjd PJDLOG_ASSERT(error == 0); 999220898Spjd if (real_remote(res)) { 1000220898Spjd error = init_remote(res, NULL, NULL); 1001220898Spjd if (error == 0) { 1002220898Spjd sync_start(); 1003220898Spjd } else if (error == EBUSY) { 1004220898Spjd time_t start = time(NULL); 1005220898Spjd 1006220898Spjd pjdlog_warning("Waiting for remote node to become %s for %ds.", 1007220898Spjd role2str(HAST_ROLE_SECONDARY), 1008220898Spjd res->hr_timeout); 1009220898Spjd for (;;) { 1010220898Spjd sleep(1); 1011220898Spjd error = init_remote(res, NULL, NULL); 1012220898Spjd if (error != EBUSY) 1013220898Spjd break; 1014220898Spjd if (time(NULL) > start + res->hr_timeout) 1015220898Spjd break; 1016220898Spjd } 1017220898Spjd if (error == EBUSY) { 1018220898Spjd pjdlog_warning("Remote node is still %s, starting anyway.", 1019220898Spjd role2str(HAST_ROLE_PRIMARY)); 1020220898Spjd } 1021220898Spjd } 1022220898Spjd } 1023204076Spjd error = pthread_create(&td, NULL, ggate_recv_thread, res); 1024218138Spjd PJDLOG_ASSERT(error == 0); 1025204076Spjd error = pthread_create(&td, NULL, local_send_thread, res); 1026218138Spjd PJDLOG_ASSERT(error == 0); 1027204076Spjd error = pthread_create(&td, NULL, remote_send_thread, res); 1028218138Spjd PJDLOG_ASSERT(error == 0); 1029204076Spjd error = pthread_create(&td, NULL, remote_recv_thread, res); 1030218138Spjd PJDLOG_ASSERT(error == 0); 1031204076Spjd error = pthread_create(&td, NULL, ggate_send_thread, res); 1032218138Spjd PJDLOG_ASSERT(error == 0); 1033220898Spjd fullystarted = true; 1034213530Spjd (void)sync_thread(res); 1035204076Spjd} 1036204076Spjd 1037204076Spjdstatic void 1038246922Spjdreqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, 1039246922Spjd const char *fmt, ...) 1040204076Spjd{ 1041204076Spjd char msg[1024]; 1042204076Spjd va_list ap; 1043204076Spjd 1044204076Spjd va_start(ap, fmt); 1045236507Spjd (void)vsnprintf(msg, sizeof(msg), fmt, ap); 1046204076Spjd va_end(ap); 1047236507Spjd switch (ggio->gctl_cmd) { 1048236507Spjd case BIO_READ: 1049236507Spjd (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).", 1050246922Spjd (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length); 1051236507Spjd break; 1052236507Spjd case BIO_DELETE: 1053236507Spjd (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).", 1054246922Spjd (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length); 1055236507Spjd break; 1056236507Spjd case BIO_FLUSH: 1057236507Spjd (void)snprlcat(msg, sizeof(msg), "FLUSH."); 1058236507Spjd break; 1059236507Spjd case BIO_WRITE: 1060236507Spjd (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).", 1061246922Spjd (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length); 1062236507Spjd break; 1063236507Spjd default: 1064236507Spjd (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).", 1065236507Spjd (unsigned int)ggio->gctl_cmd); 1066236507Spjd break; 1067204076Spjd } 1068204076Spjd pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 1069204076Spjd} 1070204076Spjd 1071204076Spjdstatic void 1072204076Spjdremote_close(struct hast_resource *res, int ncomp) 1073204076Spjd{ 1074204076Spjd 1075204076Spjd rw_wlock(&hio_remote_lock[ncomp]); 1076204076Spjd /* 1077226855Spjd * Check for a race between dropping rlock and acquiring wlock - 1078204076Spjd * another thread can close connection in-between. 1079204076Spjd */ 1080204076Spjd if (!ISCONNECTED(res, ncomp)) { 1081218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 1082218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 1083204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1084204076Spjd return; 1085204076Spjd } 1086204076Spjd 1087218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 1088218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 1089204076Spjd 1090211881Spjd pjdlog_debug(2, "Closing incoming connection to %s.", 1091204076Spjd res->hr_remoteaddr); 1092204076Spjd proto_close(res->hr_remotein); 1093204076Spjd res->hr_remotein = NULL; 1094211881Spjd pjdlog_debug(2, "Closing outgoing connection to %s.", 1095204076Spjd res->hr_remoteaddr); 1096204076Spjd proto_close(res->hr_remoteout); 1097204076Spjd res->hr_remoteout = NULL; 1098204076Spjd 1099204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1100204076Spjd 1101211881Spjd pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 1102211881Spjd 1103204076Spjd /* 1104204076Spjd * Stop synchronization if in-progress. 1105204076Spjd */ 1106211878Spjd sync_stop(); 1107211984Spjd 1108212038Spjd event_send(res, EVENT_DISCONNECT); 1109204076Spjd} 1110204076Spjd 1111204076Spjd/* 1112226859Spjd * Acknowledge write completion to the kernel, but don't update activemap yet. 1113226859Spjd */ 1114226859Spjdstatic void 1115226859Spjdwrite_complete(struct hast_resource *res, struct hio *hio) 1116226859Spjd{ 1117226859Spjd struct g_gate_ctl_io *ggio; 1118226859Spjd unsigned int ncomp; 1119226859Spjd 1120226859Spjd PJDLOG_ASSERT(!hio->hio_done); 1121226859Spjd 1122226859Spjd ggio = &hio->hio_ggio; 1123226859Spjd PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE); 1124226859Spjd 1125226859Spjd /* 1126226859Spjd * Bump local count if this is first write after 1127226859Spjd * connection failure with remote node. 1128226859Spjd */ 1129226859Spjd ncomp = 1; 1130226859Spjd rw_rlock(&hio_remote_lock[ncomp]); 1131226859Spjd if (!ISCONNECTED(res, ncomp)) { 1132226859Spjd mtx_lock(&metadata_lock); 1133226859Spjd if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) { 1134226859Spjd res->hr_primary_localcnt++; 1135226859Spjd pjdlog_debug(1, "Increasing localcnt to %ju.", 1136226859Spjd (uintmax_t)res->hr_primary_localcnt); 1137226859Spjd (void)metadata_write(res); 1138226859Spjd } 1139226859Spjd mtx_unlock(&metadata_lock); 1140226859Spjd } 1141226859Spjd rw_unlock(&hio_remote_lock[ncomp]); 1142229945Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) 1143226859Spjd primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 1144226859Spjd hio->hio_done = true; 1145226859Spjd} 1146226859Spjd 1147226859Spjd/* 1148204076Spjd * Thread receives ggate I/O requests from the kernel and passes them to 1149204076Spjd * appropriate threads: 1150204076Spjd * WRITE - always goes to both local_send and remote_send threads 1151204076Spjd * READ (when the block is up-to-date on local component) - 1152204076Spjd * only local_send thread 1153204076Spjd * READ (when the block isn't up-to-date on local component) - 1154204076Spjd * only remote_send thread 1155204076Spjd * DELETE - always goes to both local_send and remote_send threads 1156204076Spjd * FLUSH - always goes to both local_send and remote_send threads 1157204076Spjd */ 1158204076Spjdstatic void * 1159204076Spjdggate_recv_thread(void *arg) 1160204076Spjd{ 1161204076Spjd struct hast_resource *res = arg; 1162204076Spjd struct g_gate_ctl_io *ggio; 1163204076Spjd struct hio *hio; 1164204076Spjd unsigned int ii, ncomp, ncomps; 1165204076Spjd int error; 1166204076Spjd 1167204076Spjd for (;;) { 1168204076Spjd pjdlog_debug(2, "ggate_recv: Taking free request."); 1169204076Spjd QUEUE_TAKE2(hio, free); 1170204076Spjd pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 1171204076Spjd ggio = &hio->hio_ggio; 1172204076Spjd ggio->gctl_unit = res->hr_ggateunit; 1173204076Spjd ggio->gctl_length = MAXPHYS; 1174204076Spjd ggio->gctl_error = 0; 1175226859Spjd hio->hio_done = false; 1176226859Spjd hio->hio_replication = res->hr_replication; 1177204076Spjd pjdlog_debug(2, 1178204076Spjd "ggate_recv: (%p) Waiting for request from the kernel.", 1179204076Spjd hio); 1180229945Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) { 1181204076Spjd if (sigexit_received) 1182204076Spjd pthread_exit(NULL); 1183204076Spjd primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 1184204076Spjd } 1185204076Spjd error = ggio->gctl_error; 1186204076Spjd switch (error) { 1187204076Spjd case 0: 1188204076Spjd break; 1189204076Spjd case ECANCELED: 1190204076Spjd /* Exit gracefully. */ 1191204076Spjd if (!sigexit_received) { 1192204076Spjd pjdlog_debug(2, 1193204076Spjd "ggate_recv: (%p) Received cancel from the kernel.", 1194204076Spjd hio); 1195204076Spjd pjdlog_info("Received cancel from the kernel, exiting."); 1196204076Spjd } 1197204076Spjd pthread_exit(NULL); 1198204076Spjd case ENOMEM: 1199204076Spjd /* 1200204076Spjd * Buffer too small? Impossible, we allocate MAXPHYS 1201204076Spjd * bytes - request can't be bigger than that. 1202204076Spjd */ 1203204076Spjd /* FALLTHROUGH */ 1204204076Spjd case ENXIO: 1205204076Spjd default: 1206204076Spjd primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 1207204076Spjd strerror(error)); 1208204076Spjd } 1209226859Spjd 1210226859Spjd ncomp = 0; 1211226859Spjd ncomps = HAST_NCOMPONENTS; 1212226859Spjd 1213204076Spjd for (ii = 0; ii < ncomps; ii++) 1214204076Spjd hio->hio_errors[ii] = EINVAL; 1215204076Spjd reqlog(LOG_DEBUG, 2, ggio, 1216204076Spjd "ggate_recv: (%p) Request received from the kernel: ", 1217204076Spjd hio); 1218226859Spjd 1219204076Spjd /* 1220204076Spjd * Inform all components about new write request. 1221204076Spjd * For read request prefer local component unless the given 1222204076Spjd * range is out-of-date, then use remote component. 1223204076Spjd */ 1224204076Spjd switch (ggio->gctl_cmd) { 1225204076Spjd case BIO_READ: 1226222228Spjd res->hr_stat_read++; 1227226859Spjd ncomps = 1; 1228204076Spjd mtx_lock(&metadata_lock); 1229204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 1230204076Spjd res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1231204076Spjd /* 1232204076Spjd * This range is up-to-date on local component, 1233204076Spjd * so handle request locally. 1234204076Spjd */ 1235204076Spjd /* Local component is 0 for now. */ 1236204076Spjd ncomp = 0; 1237204076Spjd } else /* if (res->hr_syncsrc == 1238204076Spjd HAST_SYNCSRC_SECONDARY) */ { 1239218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == 1240204076Spjd HAST_SYNCSRC_SECONDARY); 1241204076Spjd /* 1242204076Spjd * This range is out-of-date on local component, 1243204076Spjd * so send request to the remote node. 1244204076Spjd */ 1245204076Spjd /* Remote component is 1 for now. */ 1246204076Spjd ncomp = 1; 1247204076Spjd } 1248204076Spjd mtx_unlock(&metadata_lock); 1249204076Spjd break; 1250204076Spjd case BIO_WRITE: 1251222228Spjd res->hr_stat_write++; 1252226851Spjd if (res->hr_resuid == 0 && 1253226851Spjd res->hr_primary_localcnt == 0) { 1254226851Spjd /* This is first write. */ 1255219844Spjd res->hr_primary_localcnt = 1; 1256214284Spjd } 1257204076Spjd for (;;) { 1258204076Spjd mtx_lock(&range_lock); 1259204076Spjd if (rangelock_islocked(range_sync, 1260204076Spjd ggio->gctl_offset, ggio->gctl_length)) { 1261204076Spjd pjdlog_debug(2, 1262204076Spjd "regular: Range offset=%jd length=%zu locked.", 1263204076Spjd (intmax_t)ggio->gctl_offset, 1264204076Spjd (size_t)ggio->gctl_length); 1265204076Spjd range_regular_wait = true; 1266204076Spjd cv_wait(&range_regular_cond, &range_lock); 1267204076Spjd range_regular_wait = false; 1268204076Spjd mtx_unlock(&range_lock); 1269204076Spjd continue; 1270204076Spjd } 1271204076Spjd if (rangelock_add(range_regular, 1272229945Spjd ggio->gctl_offset, ggio->gctl_length) == -1) { 1273204076Spjd mtx_unlock(&range_lock); 1274204076Spjd pjdlog_debug(2, 1275204076Spjd "regular: Range offset=%jd length=%zu is already locked, waiting.", 1276204076Spjd (intmax_t)ggio->gctl_offset, 1277204076Spjd (size_t)ggio->gctl_length); 1278204076Spjd sleep(1); 1279204076Spjd continue; 1280204076Spjd } 1281204076Spjd mtx_unlock(&range_lock); 1282204076Spjd break; 1283204076Spjd } 1284204076Spjd mtx_lock(&res->hr_amp_lock); 1285204076Spjd if (activemap_write_start(res->hr_amp, 1286204076Spjd ggio->gctl_offset, ggio->gctl_length)) { 1287222228Spjd res->hr_stat_activemap_update++; 1288204076Spjd (void)hast_activemap_flush(res); 1289204076Spjd } 1290204076Spjd mtx_unlock(&res->hr_amp_lock); 1291226859Spjd break; 1292204076Spjd case BIO_DELETE: 1293226859Spjd res->hr_stat_delete++; 1294226859Spjd break; 1295204076Spjd case BIO_FLUSH: 1296226859Spjd res->hr_stat_flush++; 1297204076Spjd break; 1298204076Spjd } 1299226859Spjd pjdlog_debug(2, 1300226859Spjd "ggate_recv: (%p) Moving request to the send queues.", hio); 1301246922Spjd hio->hio_countdown = ncomps; 1302246922Spjd if (hio->hio_replication == HAST_REPLICATION_MEMSYNC && 1303246922Spjd ggio->gctl_cmd == BIO_WRITE) { 1304246922Spjd /* Each remote request needs two responses in memsync. */ 1305246922Spjd hio->hio_countdown++; 1306246922Spjd } 1307246922Spjd for (ii = ncomp; ii < ncomps; ii++) 1308226859Spjd QUEUE_INSERT1(hio, send, ii); 1309204076Spjd } 1310204076Spjd /* NOTREACHED */ 1311204076Spjd return (NULL); 1312204076Spjd} 1313204076Spjd 1314204076Spjd/* 1315204076Spjd * Thread reads from or writes to local component. 1316204076Spjd * If local read fails, it redirects it to remote_send thread. 1317204076Spjd */ 1318204076Spjdstatic void * 1319204076Spjdlocal_send_thread(void *arg) 1320204076Spjd{ 1321204076Spjd struct hast_resource *res = arg; 1322204076Spjd struct g_gate_ctl_io *ggio; 1323204076Spjd struct hio *hio; 1324204076Spjd unsigned int ncomp, rncomp; 1325204076Spjd ssize_t ret; 1326204076Spjd 1327204076Spjd /* Local component is 0 for now. */ 1328204076Spjd ncomp = 0; 1329204076Spjd /* Remote component is 1 for now. */ 1330204076Spjd rncomp = 1; 1331204076Spjd 1332204076Spjd for (;;) { 1333204076Spjd pjdlog_debug(2, "local_send: Taking request."); 1334214692Spjd QUEUE_TAKE1(hio, send, ncomp, 0); 1335204076Spjd pjdlog_debug(2, "local_send: (%p) Got request.", hio); 1336204076Spjd ggio = &hio->hio_ggio; 1337204076Spjd switch (ggio->gctl_cmd) { 1338204076Spjd case BIO_READ: 1339204076Spjd ret = pread(res->hr_localfd, ggio->gctl_data, 1340204076Spjd ggio->gctl_length, 1341204076Spjd ggio->gctl_offset + res->hr_localoff); 1342204076Spjd if (ret == ggio->gctl_length) 1343204076Spjd hio->hio_errors[ncomp] = 0; 1344222467Strociny else if (!ISSYNCREQ(hio)) { 1345204076Spjd /* 1346204076Spjd * If READ failed, try to read from remote node. 1347204076Spjd */ 1348229945Spjd if (ret == -1) { 1349216479Spjd reqlog(LOG_WARNING, 0, ggio, 1350216479Spjd "Local request failed (%s), trying remote node. ", 1351216479Spjd strerror(errno)); 1352216479Spjd } else if (ret != ggio->gctl_length) { 1353216479Spjd reqlog(LOG_WARNING, 0, ggio, 1354216479Spjd "Local request failed (%zd != %jd), trying remote node. ", 1355216494Spjd ret, (intmax_t)ggio->gctl_length); 1356216479Spjd } 1357204076Spjd QUEUE_INSERT1(hio, send, rncomp); 1358204076Spjd continue; 1359204076Spjd } 1360204076Spjd break; 1361204076Spjd case BIO_WRITE: 1362204076Spjd ret = pwrite(res->hr_localfd, ggio->gctl_data, 1363204076Spjd ggio->gctl_length, 1364204076Spjd ggio->gctl_offset + res->hr_localoff); 1365229945Spjd if (ret == -1) { 1366204076Spjd hio->hio_errors[ncomp] = errno; 1367216479Spjd reqlog(LOG_WARNING, 0, ggio, 1368216479Spjd "Local request failed (%s): ", 1369216479Spjd strerror(errno)); 1370216479Spjd } else if (ret != ggio->gctl_length) { 1371204076Spjd hio->hio_errors[ncomp] = EIO; 1372216479Spjd reqlog(LOG_WARNING, 0, ggio, 1373216479Spjd "Local request failed (%zd != %jd): ", 1374216494Spjd ret, (intmax_t)ggio->gctl_length); 1375216479Spjd } else { 1376204076Spjd hio->hio_errors[ncomp] = 0; 1377226859Spjd if (hio->hio_replication == 1378246922Spjd HAST_REPLICATION_ASYNC) { 1379226859Spjd ggio->gctl_error = 0; 1380226859Spjd write_complete(res, hio); 1381226859Spjd } 1382216479Spjd } 1383204076Spjd break; 1384204076Spjd case BIO_DELETE: 1385204076Spjd ret = g_delete(res->hr_localfd, 1386204076Spjd ggio->gctl_offset + res->hr_localoff, 1387204076Spjd ggio->gctl_length); 1388229945Spjd if (ret == -1) { 1389204076Spjd hio->hio_errors[ncomp] = errno; 1390216479Spjd reqlog(LOG_WARNING, 0, ggio, 1391216479Spjd "Local request failed (%s): ", 1392216479Spjd strerror(errno)); 1393216479Spjd } else { 1394204076Spjd hio->hio_errors[ncomp] = 0; 1395216479Spjd } 1396204076Spjd break; 1397204076Spjd case BIO_FLUSH: 1398225832Spjd if (!res->hr_localflush) { 1399225832Spjd ret = -1; 1400225832Spjd errno = EOPNOTSUPP; 1401225832Spjd break; 1402225832Spjd } 1403204076Spjd ret = g_flush(res->hr_localfd); 1404229945Spjd if (ret == -1) { 1405225832Spjd if (errno == EOPNOTSUPP) 1406225832Spjd res->hr_localflush = false; 1407204076Spjd hio->hio_errors[ncomp] = errno; 1408216479Spjd reqlog(LOG_WARNING, 0, ggio, 1409216479Spjd "Local request failed (%s): ", 1410216479Spjd strerror(errno)); 1411216479Spjd } else { 1412204076Spjd hio->hio_errors[ncomp] = 0; 1413216479Spjd } 1414204076Spjd break; 1415204076Spjd } 1416246922Spjd 1417246922Spjd if (hio->hio_replication != HAST_REPLICATION_MEMSYNC || 1418246922Spjd ggio->gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) { 1419246922Spjd if (refcnt_release(&hio->hio_countdown) > 0) 1420246922Spjd continue; 1421246922Spjd } else { 1422246922Spjd /* 1423246922Spjd * Depending on hio_countdown value, requests finished 1424246922Spjd * in the following order: 1425246922Spjd * 0: remote memsync, remote final, local write 1426246922Spjd * 1: remote memsync, local write, (remote final) 1427246922Spjd * 2: local write, (remote memsync), (remote final) 1428246922Spjd */ 1429246922Spjd switch (refcnt_release(&hio->hio_countdown)) { 1430246922Spjd case 0: 1431246922Spjd /* 1432246922Spjd * Local write finished as last. 1433246922Spjd */ 1434246922Spjd break; 1435246922Spjd case 1: 1436246922Spjd /* 1437246922Spjd * Local write finished after remote memsync 1438246922Spjd * reply arrvied. We can complete the write now. 1439246922Spjd */ 1440246922Spjd if (hio->hio_errors[0] == 0) 1441246922Spjd write_complete(res, hio); 1442246922Spjd continue; 1443246922Spjd case 2: 1444246922Spjd /* 1445246922Spjd * Local write finished as first. 1446246922Spjd */ 1447246922Spjd continue; 1448246922Spjd default: 1449246922Spjd PJDLOG_ABORT("Invalid hio_countdown."); 1450246922Spjd } 1451246922Spjd } 1452226856Spjd if (ISSYNCREQ(hio)) { 1453226856Spjd mtx_lock(&sync_lock); 1454226856Spjd SYNCREQDONE(hio); 1455226856Spjd mtx_unlock(&sync_lock); 1456226856Spjd cv_signal(&sync_cond); 1457226856Spjd } else { 1458226856Spjd pjdlog_debug(2, 1459226856Spjd "local_send: (%p) Moving request to the done queue.", 1460226856Spjd hio); 1461226856Spjd QUEUE_INSERT2(hio, done); 1462204076Spjd } 1463204076Spjd } 1464204076Spjd /* NOTREACHED */ 1465204076Spjd return (NULL); 1466204076Spjd} 1467204076Spjd 1468214692Spjdstatic void 1469214692Spjdkeepalive_send(struct hast_resource *res, unsigned int ncomp) 1470214692Spjd{ 1471214692Spjd struct nv *nv; 1472214692Spjd 1473218217Spjd rw_rlock(&hio_remote_lock[ncomp]); 1474218217Spjd 1475218217Spjd if (!ISCONNECTED(res, ncomp)) { 1476218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1477214692Spjd return; 1478218217Spjd } 1479219864Spjd 1480218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 1481218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 1482214692Spjd 1483214692Spjd nv = nv_alloc(); 1484214692Spjd nv_add_uint8(nv, HIO_KEEPALIVE, "cmd"); 1485214692Spjd if (nv_error(nv) != 0) { 1486218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1487214692Spjd nv_free(nv); 1488214692Spjd pjdlog_debug(1, 1489214692Spjd "keepalive_send: Unable to prepare header to send."); 1490214692Spjd return; 1491214692Spjd } 1492229945Spjd if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) { 1493218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1494214692Spjd pjdlog_common(LOG_DEBUG, 1, errno, 1495214692Spjd "keepalive_send: Unable to send request"); 1496214692Spjd nv_free(nv); 1497214692Spjd remote_close(res, ncomp); 1498214692Spjd return; 1499214692Spjd } 1500218217Spjd 1501218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1502214692Spjd nv_free(nv); 1503214692Spjd pjdlog_debug(2, "keepalive_send: Request sent."); 1504214692Spjd} 1505214692Spjd 1506204076Spjd/* 1507204076Spjd * Thread sends request to secondary node. 1508204076Spjd */ 1509204076Spjdstatic void * 1510204076Spjdremote_send_thread(void *arg) 1511204076Spjd{ 1512204076Spjd struct hast_resource *res = arg; 1513204076Spjd struct g_gate_ctl_io *ggio; 1514214692Spjd time_t lastcheck, now; 1515204076Spjd struct hio *hio; 1516204076Spjd struct nv *nv; 1517204076Spjd unsigned int ncomp; 1518204076Spjd bool wakeup; 1519204076Spjd uint64_t offset, length; 1520204076Spjd uint8_t cmd; 1521204076Spjd void *data; 1522204076Spjd 1523204076Spjd /* Remote component is 1 for now. */ 1524204076Spjd ncomp = 1; 1525219864Spjd lastcheck = time(NULL); 1526204076Spjd 1527204076Spjd for (;;) { 1528204076Spjd pjdlog_debug(2, "remote_send: Taking request."); 1529219721Strociny QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE); 1530214692Spjd if (hio == NULL) { 1531214692Spjd now = time(NULL); 1532219721Strociny if (lastcheck + HAST_KEEPALIVE <= now) { 1533214692Spjd keepalive_send(res, ncomp); 1534214692Spjd lastcheck = now; 1535214692Spjd } 1536214692Spjd continue; 1537214692Spjd } 1538204076Spjd pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 1539204076Spjd ggio = &hio->hio_ggio; 1540204076Spjd switch (ggio->gctl_cmd) { 1541204076Spjd case BIO_READ: 1542204076Spjd cmd = HIO_READ; 1543204076Spjd data = NULL; 1544204076Spjd offset = ggio->gctl_offset; 1545204076Spjd length = ggio->gctl_length; 1546204076Spjd break; 1547204076Spjd case BIO_WRITE: 1548204076Spjd cmd = HIO_WRITE; 1549204076Spjd data = ggio->gctl_data; 1550204076Spjd offset = ggio->gctl_offset; 1551204076Spjd length = ggio->gctl_length; 1552204076Spjd break; 1553204076Spjd case BIO_DELETE: 1554204076Spjd cmd = HIO_DELETE; 1555204076Spjd data = NULL; 1556204076Spjd offset = ggio->gctl_offset; 1557204076Spjd length = ggio->gctl_length; 1558204076Spjd break; 1559204076Spjd case BIO_FLUSH: 1560204076Spjd cmd = HIO_FLUSH; 1561204076Spjd data = NULL; 1562204076Spjd offset = 0; 1563204076Spjd length = 0; 1564204076Spjd break; 1565204076Spjd default: 1566225783Spjd PJDLOG_ABORT("invalid condition"); 1567204076Spjd } 1568204076Spjd nv = nv_alloc(); 1569204076Spjd nv_add_uint8(nv, cmd, "cmd"); 1570204076Spjd nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 1571204076Spjd nv_add_uint64(nv, offset, "offset"); 1572204076Spjd nv_add_uint64(nv, length, "length"); 1573246922Spjd if (hio->hio_replication == HAST_REPLICATION_MEMSYNC && 1574246922Spjd ggio->gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio)) { 1575246922Spjd nv_add_uint8(nv, 1, "memsync"); 1576246922Spjd } 1577204076Spjd if (nv_error(nv) != 0) { 1578204076Spjd hio->hio_errors[ncomp] = nv_error(nv); 1579204076Spjd pjdlog_debug(2, 1580204076Spjd "remote_send: (%p) Unable to prepare header to send.", 1581204076Spjd hio); 1582204076Spjd reqlog(LOG_ERR, 0, ggio, 1583204076Spjd "Unable to prepare header to send (%s): ", 1584204076Spjd strerror(nv_error(nv))); 1585204076Spjd /* Move failed request immediately to the done queue. */ 1586204076Spjd goto done_queue; 1587204076Spjd } 1588204076Spjd /* 1589204076Spjd * Protect connection from disappearing. 1590204076Spjd */ 1591204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1592204076Spjd if (!ISCONNECTED(res, ncomp)) { 1593204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1594204076Spjd hio->hio_errors[ncomp] = ENOTCONN; 1595204076Spjd goto done_queue; 1596204076Spjd } 1597204076Spjd /* 1598204076Spjd * Move the request to recv queue before sending it, because 1599204076Spjd * in different order we can get reply before we move request 1600204076Spjd * to recv queue. 1601204076Spjd */ 1602226852Spjd pjdlog_debug(2, 1603226852Spjd "remote_send: (%p) Moving request to the recv queue.", 1604226852Spjd hio); 1605204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1606204076Spjd wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 1607204076Spjd TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 1608204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1609204076Spjd if (hast_proto_send(res, res->hr_remoteout, nv, data, 1610229945Spjd data != NULL ? length : 0) == -1) { 1611204076Spjd hio->hio_errors[ncomp] = errno; 1612204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1613204076Spjd pjdlog_debug(2, 1614204076Spjd "remote_send: (%p) Unable to send request.", hio); 1615204076Spjd reqlog(LOG_ERR, 0, ggio, 1616204076Spjd "Unable to send request (%s): ", 1617204076Spjd strerror(hio->hio_errors[ncomp])); 1618211979Spjd remote_close(res, ncomp); 1619204076Spjd /* 1620204076Spjd * Take request back from the receive queue and move 1621204076Spjd * it immediately to the done queue. 1622204076Spjd */ 1623204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1624226852Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1625226852Spjd hio_next[ncomp]); 1626204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1627204076Spjd goto done_queue; 1628204076Spjd } 1629204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1630204076Spjd nv_free(nv); 1631204076Spjd if (wakeup) 1632204076Spjd cv_signal(&hio_recv_list_cond[ncomp]); 1633204076Spjd continue; 1634204076Spjddone_queue: 1635204076Spjd nv_free(nv); 1636204076Spjd if (ISSYNCREQ(hio)) { 1637246922Spjd if (refcnt_release(&hio->hio_countdown) > 0) 1638204076Spjd continue; 1639204076Spjd mtx_lock(&sync_lock); 1640204076Spjd SYNCREQDONE(hio); 1641204076Spjd mtx_unlock(&sync_lock); 1642204076Spjd cv_signal(&sync_cond); 1643204076Spjd continue; 1644204076Spjd } 1645204076Spjd if (ggio->gctl_cmd == BIO_WRITE) { 1646204076Spjd mtx_lock(&res->hr_amp_lock); 1647204076Spjd if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 1648204076Spjd ggio->gctl_length)) { 1649204076Spjd (void)hast_activemap_flush(res); 1650204076Spjd } 1651204076Spjd mtx_unlock(&res->hr_amp_lock); 1652246922Spjd if (hio->hio_replication == HAST_REPLICATION_MEMSYNC) 1653246922Spjd (void)refcnt_release(&hio->hio_countdown); 1654204076Spjd } 1655246922Spjd if (refcnt_release(&hio->hio_countdown) > 0) 1656204076Spjd continue; 1657204076Spjd pjdlog_debug(2, 1658204076Spjd "remote_send: (%p) Moving request to the done queue.", 1659204076Spjd hio); 1660204076Spjd QUEUE_INSERT2(hio, done); 1661204076Spjd } 1662204076Spjd /* NOTREACHED */ 1663204076Spjd return (NULL); 1664204076Spjd} 1665204076Spjd 1666204076Spjd/* 1667204076Spjd * Thread receives answer from secondary node and passes it to ggate_send 1668204076Spjd * thread. 1669204076Spjd */ 1670204076Spjdstatic void * 1671204076Spjdremote_recv_thread(void *arg) 1672204076Spjd{ 1673204076Spjd struct hast_resource *res = arg; 1674204076Spjd struct g_gate_ctl_io *ggio; 1675204076Spjd struct hio *hio; 1676204076Spjd struct nv *nv; 1677204076Spjd unsigned int ncomp; 1678204076Spjd uint64_t seq; 1679246922Spjd bool memsyncack; 1680204076Spjd int error; 1681204076Spjd 1682204076Spjd /* Remote component is 1 for now. */ 1683204076Spjd ncomp = 1; 1684204076Spjd 1685204076Spjd for (;;) { 1686204076Spjd /* Wait until there is anything to receive. */ 1687204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1688204076Spjd while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 1689204076Spjd pjdlog_debug(2, "remote_recv: No requests, waiting."); 1690204076Spjd cv_wait(&hio_recv_list_cond[ncomp], 1691204076Spjd &hio_recv_list_lock[ncomp]); 1692204076Spjd } 1693204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1694226857Spjd 1695246922Spjd memsyncack = false; 1696246922Spjd 1697204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1698204076Spjd if (!ISCONNECTED(res, ncomp)) { 1699204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1700204076Spjd /* 1701204076Spjd * Connection is dead, so move all pending requests to 1702204076Spjd * the done queue (one-by-one). 1703204076Spjd */ 1704204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1705204076Spjd hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 1706218138Spjd PJDLOG_ASSERT(hio != NULL); 1707204076Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1708204076Spjd hio_next[ncomp]); 1709204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1710204076Spjd goto done_queue; 1711204076Spjd } 1712229945Spjd if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) { 1713204076Spjd pjdlog_errno(LOG_ERR, 1714204076Spjd "Unable to receive reply header"); 1715204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1716204076Spjd remote_close(res, ncomp); 1717204076Spjd continue; 1718204076Spjd } 1719204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1720204076Spjd seq = nv_get_uint64(nv, "seq"); 1721204076Spjd if (seq == 0) { 1722204076Spjd pjdlog_error("Header contains no 'seq' field."); 1723204076Spjd nv_free(nv); 1724204076Spjd continue; 1725204076Spjd } 1726246922Spjd memsyncack = nv_exists(nv, "received"); 1727204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1728204076Spjd TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 1729204076Spjd if (hio->hio_ggio.gctl_seq == seq) { 1730204076Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1731204076Spjd hio_next[ncomp]); 1732204076Spjd break; 1733204076Spjd } 1734204076Spjd } 1735204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1736204076Spjd if (hio == NULL) { 1737204076Spjd pjdlog_error("Found no request matching received 'seq' field (%ju).", 1738204076Spjd (uintmax_t)seq); 1739204076Spjd nv_free(nv); 1740204076Spjd continue; 1741204076Spjd } 1742226852Spjd ggio = &hio->hio_ggio; 1743204076Spjd error = nv_get_int16(nv, "error"); 1744204076Spjd if (error != 0) { 1745204076Spjd /* Request failed on remote side. */ 1746216478Spjd hio->hio_errors[ncomp] = error; 1747226852Spjd reqlog(LOG_WARNING, 0, ggio, 1748216479Spjd "Remote request failed (%s): ", strerror(error)); 1749204076Spjd nv_free(nv); 1750204076Spjd goto done_queue; 1751204076Spjd } 1752204076Spjd switch (ggio->gctl_cmd) { 1753204076Spjd case BIO_READ: 1754204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1755204076Spjd if (!ISCONNECTED(res, ncomp)) { 1756204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1757204076Spjd nv_free(nv); 1758204076Spjd goto done_queue; 1759204076Spjd } 1760204076Spjd if (hast_proto_recv_data(res, res->hr_remotein, nv, 1761229945Spjd ggio->gctl_data, ggio->gctl_length) == -1) { 1762204076Spjd hio->hio_errors[ncomp] = errno; 1763204076Spjd pjdlog_errno(LOG_ERR, 1764204076Spjd "Unable to receive reply data"); 1765204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1766204076Spjd nv_free(nv); 1767204076Spjd remote_close(res, ncomp); 1768204076Spjd goto done_queue; 1769204076Spjd } 1770204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1771204076Spjd break; 1772204076Spjd case BIO_WRITE: 1773204076Spjd case BIO_DELETE: 1774204076Spjd case BIO_FLUSH: 1775204076Spjd break; 1776204076Spjd default: 1777225783Spjd PJDLOG_ABORT("invalid condition"); 1778204076Spjd } 1779204076Spjd hio->hio_errors[ncomp] = 0; 1780204076Spjd nv_free(nv); 1781204076Spjddone_queue: 1782246922Spjd if (hio->hio_replication != HAST_REPLICATION_MEMSYNC || 1783246922Spjd hio->hio_ggio.gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) { 1784246922Spjd if (refcnt_release(&hio->hio_countdown) > 0) 1785246922Spjd continue; 1786246922Spjd } else { 1787246922Spjd /* 1788246922Spjd * Depending on hio_countdown value, requests finished 1789246922Spjd * in the following order: 1790246922Spjd * 1791246922Spjd * 0: local write, remote memsync, remote final 1792246922Spjd * or 1793246922Spjd * 0: remote memsync, local write, remote final 1794246922Spjd * 1795246922Spjd * 1: local write, remote memsync, (remote final) 1796246922Spjd * or 1797246922Spjd * 1: remote memsync, remote final, (local write) 1798246922Spjd * 1799246922Spjd * 2: remote memsync, (local write), (remote final) 1800246922Spjd * or 1801246922Spjd * 2: remote memsync, (remote final), (local write) 1802246922Spjd */ 1803246922Spjd switch (refcnt_release(&hio->hio_countdown)) { 1804246922Spjd case 0: 1805246922Spjd /* 1806246922Spjd * Remote final reply arrived. 1807246922Spjd */ 1808246922Spjd PJDLOG_ASSERT(!memsyncack); 1809246922Spjd break; 1810246922Spjd case 1: 1811246922Spjd if (memsyncack) { 1812246922Spjd /* 1813246922Spjd * Local request already finished, so we 1814246922Spjd * can complete the write. 1815246922Spjd */ 1816246922Spjd if (hio->hio_errors[0] == 0) 1817246922Spjd write_complete(res, hio); 1818246922Spjd /* 1819246922Spjd * We still need to wait for final 1820246922Spjd * remote reply. 1821246922Spjd */ 1822246922Spjd pjdlog_debug(2, 1823246922Spjd "remote_recv: (%p) Moving request back to the recv queue.", 1824246922Spjd hio); 1825246922Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1826246922Spjd TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], 1827246922Spjd hio, hio_next[ncomp]); 1828246922Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1829246922Spjd } else { 1830246922Spjd /* 1831246922Spjd * Remote final reply arrived before 1832246922Spjd * local write finished. 1833246922Spjd * Nothing to do in such case. 1834246922Spjd */ 1835246922Spjd } 1836246922Spjd continue; 1837246922Spjd case 2: 1838246922Spjd /* 1839246922Spjd * We received remote memsync reply even before 1840246922Spjd * local write finished. 1841246922Spjd */ 1842246922Spjd PJDLOG_ASSERT(memsyncack); 1843246922Spjd 1844246922Spjd pjdlog_debug(2, 1845246922Spjd "remote_recv: (%p) Moving request back to the recv queue.", 1846246922Spjd hio); 1847246922Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1848246922Spjd TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, 1849246922Spjd hio_next[ncomp]); 1850246922Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1851246922Spjd continue; 1852246922Spjd default: 1853246922Spjd PJDLOG_ABORT("Invalid hio_countdown."); 1854246922Spjd } 1855246922Spjd } 1856226856Spjd if (ISSYNCREQ(hio)) { 1857226856Spjd mtx_lock(&sync_lock); 1858226856Spjd SYNCREQDONE(hio); 1859226856Spjd mtx_unlock(&sync_lock); 1860226856Spjd cv_signal(&sync_cond); 1861226856Spjd } else { 1862226856Spjd pjdlog_debug(2, 1863226856Spjd "remote_recv: (%p) Moving request to the done queue.", 1864226856Spjd hio); 1865226856Spjd QUEUE_INSERT2(hio, done); 1866204076Spjd } 1867204076Spjd } 1868204076Spjd /* NOTREACHED */ 1869204076Spjd return (NULL); 1870204076Spjd} 1871204076Spjd 1872204076Spjd/* 1873204076Spjd * Thread sends answer to the kernel. 1874204076Spjd */ 1875204076Spjdstatic void * 1876204076Spjdggate_send_thread(void *arg) 1877204076Spjd{ 1878204076Spjd struct hast_resource *res = arg; 1879204076Spjd struct g_gate_ctl_io *ggio; 1880204076Spjd struct hio *hio; 1881226859Spjd unsigned int ii, ncomps; 1882204076Spjd 1883204076Spjd ncomps = HAST_NCOMPONENTS; 1884204076Spjd 1885204076Spjd for (;;) { 1886204076Spjd pjdlog_debug(2, "ggate_send: Taking request."); 1887204076Spjd QUEUE_TAKE2(hio, done); 1888204076Spjd pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 1889204076Spjd ggio = &hio->hio_ggio; 1890204076Spjd for (ii = 0; ii < ncomps; ii++) { 1891204076Spjd if (hio->hio_errors[ii] == 0) { 1892204076Spjd /* 1893204076Spjd * One successful request is enough to declare 1894204076Spjd * success. 1895204076Spjd */ 1896204076Spjd ggio->gctl_error = 0; 1897204076Spjd break; 1898204076Spjd } 1899204076Spjd } 1900204076Spjd if (ii == ncomps) { 1901204076Spjd /* 1902204076Spjd * None of the requests were successful. 1903219879Strociny * Use the error from local component except the 1904219879Strociny * case when we did only remote request. 1905204076Spjd */ 1906219879Strociny if (ggio->gctl_cmd == BIO_READ && 1907219879Strociny res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) 1908219879Strociny ggio->gctl_error = hio->hio_errors[1]; 1909219879Strociny else 1910219879Strociny ggio->gctl_error = hio->hio_errors[0]; 1911204076Spjd } 1912204076Spjd if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 1913204076Spjd mtx_lock(&res->hr_amp_lock); 1914223655Strociny if (activemap_write_complete(res->hr_amp, 1915223974Strociny ggio->gctl_offset, ggio->gctl_length)) { 1916223655Strociny res->hr_stat_activemap_update++; 1917223655Strociny (void)hast_activemap_flush(res); 1918223655Strociny } 1919204076Spjd mtx_unlock(&res->hr_amp_lock); 1920204076Spjd } 1921204076Spjd if (ggio->gctl_cmd == BIO_WRITE) { 1922204076Spjd /* 1923204076Spjd * Unlock range we locked. 1924204076Spjd */ 1925204076Spjd mtx_lock(&range_lock); 1926204076Spjd rangelock_del(range_regular, ggio->gctl_offset, 1927204076Spjd ggio->gctl_length); 1928204076Spjd if (range_sync_wait) 1929204076Spjd cv_signal(&range_sync_cond); 1930204076Spjd mtx_unlock(&range_lock); 1931226859Spjd if (!hio->hio_done) 1932226859Spjd write_complete(res, hio); 1933226859Spjd } else { 1934229945Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) { 1935226859Spjd primary_exit(EX_OSERR, 1936226859Spjd "G_GATE_CMD_DONE failed"); 1937204076Spjd } 1938204076Spjd } 1939204076Spjd pjdlog_debug(2, 1940204076Spjd "ggate_send: (%p) Moving request to the free queue.", hio); 1941204076Spjd QUEUE_INSERT2(hio, free); 1942204076Spjd } 1943204076Spjd /* NOTREACHED */ 1944204076Spjd return (NULL); 1945204076Spjd} 1946204076Spjd 1947204076Spjd/* 1948204076Spjd * Thread synchronize local and remote components. 1949204076Spjd */ 1950204076Spjdstatic void * 1951204076Spjdsync_thread(void *arg __unused) 1952204076Spjd{ 1953204076Spjd struct hast_resource *res = arg; 1954204076Spjd struct hio *hio; 1955204076Spjd struct g_gate_ctl_io *ggio; 1956219372Spjd struct timeval tstart, tend, tdiff; 1957204076Spjd unsigned int ii, ncomp, ncomps; 1958204076Spjd off_t offset, length, synced; 1959238120Spjd bool dorewind, directreads; 1960204076Spjd int syncext; 1961204076Spjd 1962204076Spjd ncomps = HAST_NCOMPONENTS; 1963204076Spjd dorewind = true; 1964211897Spjd synced = 0; 1965211897Spjd offset = -1; 1966238120Spjd directreads = false; 1967204076Spjd 1968204076Spjd for (;;) { 1969204076Spjd mtx_lock(&sync_lock); 1970211897Spjd if (offset >= 0 && !sync_inprogress) { 1971219372Spjd gettimeofday(&tend, NULL); 1972219372Spjd timersub(&tend, &tstart, &tdiff); 1973219372Spjd pjdlog_info("Synchronization interrupted after %#.0T. " 1974219372Spjd "%NB synchronized so far.", &tdiff, 1975211879Spjd (intmax_t)synced); 1976212038Spjd event_send(res, EVENT_SYNCINTR); 1977211879Spjd } 1978204076Spjd while (!sync_inprogress) { 1979204076Spjd dorewind = true; 1980204076Spjd synced = 0; 1981204076Spjd cv_wait(&sync_cond, &sync_lock); 1982204076Spjd } 1983204076Spjd mtx_unlock(&sync_lock); 1984204076Spjd /* 1985204076Spjd * Obtain offset at which we should synchronize. 1986204076Spjd * Rewind synchronization if needed. 1987204076Spjd */ 1988204076Spjd mtx_lock(&res->hr_amp_lock); 1989204076Spjd if (dorewind) 1990204076Spjd activemap_sync_rewind(res->hr_amp); 1991204076Spjd offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 1992204076Spjd if (syncext != -1) { 1993204076Spjd /* 1994204076Spjd * We synchronized entire syncext extent, we can mark 1995204076Spjd * it as clean now. 1996204076Spjd */ 1997204076Spjd if (activemap_extent_complete(res->hr_amp, syncext)) 1998204076Spjd (void)hast_activemap_flush(res); 1999204076Spjd } 2000204076Spjd mtx_unlock(&res->hr_amp_lock); 2001204076Spjd if (dorewind) { 2002204076Spjd dorewind = false; 2003229945Spjd if (offset == -1) 2004204076Spjd pjdlog_info("Nodes are in sync."); 2005204076Spjd else { 2006219372Spjd pjdlog_info("Synchronization started. %NB to go.", 2007219372Spjd (intmax_t)(res->hr_extentsize * 2008204076Spjd activemap_ndirty(res->hr_amp))); 2009212038Spjd event_send(res, EVENT_SYNCSTART); 2010219372Spjd gettimeofday(&tstart, NULL); 2011204076Spjd } 2012204076Spjd } 2013229945Spjd if (offset == -1) { 2014211878Spjd sync_stop(); 2015204076Spjd pjdlog_debug(1, "Nothing to synchronize."); 2016204076Spjd /* 2017204076Spjd * Synchronization complete, make both localcnt and 2018204076Spjd * remotecnt equal. 2019204076Spjd */ 2020204076Spjd ncomp = 1; 2021204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 2022204076Spjd if (ISCONNECTED(res, ncomp)) { 2023204076Spjd if (synced > 0) { 2024219372Spjd int64_t bps; 2025219372Spjd 2026219372Spjd gettimeofday(&tend, NULL); 2027219372Spjd timersub(&tend, &tstart, &tdiff); 2028219372Spjd bps = (int64_t)((double)synced / 2029219372Spjd ((double)tdiff.tv_sec + 2030219372Spjd (double)tdiff.tv_usec / 1000000)); 2031204076Spjd pjdlog_info("Synchronization complete. " 2032219372Spjd "%NB synchronized in %#.0lT (%NB/sec).", 2033219372Spjd (intmax_t)synced, &tdiff, 2034219372Spjd (intmax_t)bps); 2035212038Spjd event_send(res, EVENT_SYNCDONE); 2036204076Spjd } 2037204076Spjd mtx_lock(&metadata_lock); 2038238120Spjd if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) 2039238120Spjd directreads = true; 2040204076Spjd res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 2041204076Spjd res->hr_primary_localcnt = 2042219882Strociny res->hr_secondary_remotecnt; 2043219882Strociny res->hr_primary_remotecnt = 2044204076Spjd res->hr_secondary_localcnt; 2045204076Spjd pjdlog_debug(1, 2046204076Spjd "Setting localcnt to %ju and remotecnt to %ju.", 2047204076Spjd (uintmax_t)res->hr_primary_localcnt, 2048219882Strociny (uintmax_t)res->hr_primary_remotecnt); 2049204076Spjd (void)metadata_write(res); 2050204076Spjd mtx_unlock(&metadata_lock); 2051204076Spjd } 2052204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 2053238120Spjd if (directreads) { 2054238120Spjd directreads = false; 2055238120Spjd enable_direct_reads(res); 2056238120Spjd } 2057204076Spjd continue; 2058204076Spjd } 2059204076Spjd pjdlog_debug(2, "sync: Taking free request."); 2060204076Spjd QUEUE_TAKE2(hio, free); 2061204076Spjd pjdlog_debug(2, "sync: (%p) Got free request.", hio); 2062204076Spjd /* 2063204076Spjd * Lock the range we are going to synchronize. We don't want 2064204076Spjd * race where someone writes between our read and write. 2065204076Spjd */ 2066204076Spjd for (;;) { 2067204076Spjd mtx_lock(&range_lock); 2068204076Spjd if (rangelock_islocked(range_regular, offset, length)) { 2069204076Spjd pjdlog_debug(2, 2070204076Spjd "sync: Range offset=%jd length=%jd locked.", 2071204076Spjd (intmax_t)offset, (intmax_t)length); 2072204076Spjd range_sync_wait = true; 2073204076Spjd cv_wait(&range_sync_cond, &range_lock); 2074204076Spjd range_sync_wait = false; 2075204076Spjd mtx_unlock(&range_lock); 2076204076Spjd continue; 2077204076Spjd } 2078229945Spjd if (rangelock_add(range_sync, offset, length) == -1) { 2079204076Spjd mtx_unlock(&range_lock); 2080204076Spjd pjdlog_debug(2, 2081204076Spjd "sync: Range offset=%jd length=%jd is already locked, waiting.", 2082204076Spjd (intmax_t)offset, (intmax_t)length); 2083204076Spjd sleep(1); 2084204076Spjd continue; 2085204076Spjd } 2086204076Spjd mtx_unlock(&range_lock); 2087204076Spjd break; 2088204076Spjd } 2089204076Spjd /* 2090204076Spjd * First read the data from synchronization source. 2091204076Spjd */ 2092204076Spjd SYNCREQ(hio); 2093204076Spjd ggio = &hio->hio_ggio; 2094204076Spjd ggio->gctl_cmd = BIO_READ; 2095204076Spjd ggio->gctl_offset = offset; 2096204076Spjd ggio->gctl_length = length; 2097204076Spjd ggio->gctl_error = 0; 2098226859Spjd hio->hio_done = false; 2099226859Spjd hio->hio_replication = res->hr_replication; 2100204076Spjd for (ii = 0; ii < ncomps; ii++) 2101204076Spjd hio->hio_errors[ii] = EINVAL; 2102204076Spjd reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 2103204076Spjd hio); 2104204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 2105204076Spjd hio); 2106204076Spjd mtx_lock(&metadata_lock); 2107204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 2108204076Spjd /* 2109204076Spjd * This range is up-to-date on local component, 2110204076Spjd * so handle request locally. 2111204076Spjd */ 2112204076Spjd /* Local component is 0 for now. */ 2113204076Spjd ncomp = 0; 2114204076Spjd } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 2115218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 2116204076Spjd /* 2117204076Spjd * This range is out-of-date on local component, 2118204076Spjd * so send request to the remote node. 2119204076Spjd */ 2120204076Spjd /* Remote component is 1 for now. */ 2121204076Spjd ncomp = 1; 2122204076Spjd } 2123204076Spjd mtx_unlock(&metadata_lock); 2124246922Spjd hio->hio_countdown = 1; 2125204076Spjd QUEUE_INSERT1(hio, send, ncomp); 2126204076Spjd 2127204076Spjd /* 2128204076Spjd * Let's wait for READ to finish. 2129204076Spjd */ 2130204076Spjd mtx_lock(&sync_lock); 2131204076Spjd while (!ISSYNCREQDONE(hio)) 2132204076Spjd cv_wait(&sync_cond, &sync_lock); 2133204076Spjd mtx_unlock(&sync_lock); 2134204076Spjd 2135204076Spjd if (hio->hio_errors[ncomp] != 0) { 2136204076Spjd pjdlog_error("Unable to read synchronization data: %s.", 2137204076Spjd strerror(hio->hio_errors[ncomp])); 2138204076Spjd goto free_queue; 2139204076Spjd } 2140204076Spjd 2141204076Spjd /* 2142204076Spjd * We read the data from synchronization source, now write it 2143204076Spjd * to synchronization target. 2144204076Spjd */ 2145204076Spjd SYNCREQ(hio); 2146204076Spjd ggio->gctl_cmd = BIO_WRITE; 2147204076Spjd for (ii = 0; ii < ncomps; ii++) 2148204076Spjd hio->hio_errors[ii] = EINVAL; 2149204076Spjd reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 2150204076Spjd hio); 2151204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 2152204076Spjd hio); 2153204076Spjd mtx_lock(&metadata_lock); 2154204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 2155204076Spjd /* 2156204076Spjd * This range is up-to-date on local component, 2157204076Spjd * so we update remote component. 2158204076Spjd */ 2159204076Spjd /* Remote component is 1 for now. */ 2160204076Spjd ncomp = 1; 2161204076Spjd } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 2162218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 2163204076Spjd /* 2164204076Spjd * This range is out-of-date on local component, 2165204076Spjd * so we update it. 2166204076Spjd */ 2167204076Spjd /* Local component is 0 for now. */ 2168204076Spjd ncomp = 0; 2169204076Spjd } 2170204076Spjd mtx_unlock(&metadata_lock); 2171204076Spjd 2172226857Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 2173204076Spjd hio); 2174246922Spjd hio->hio_countdown = 1; 2175204076Spjd QUEUE_INSERT1(hio, send, ncomp); 2176204076Spjd 2177204076Spjd /* 2178204076Spjd * Let's wait for WRITE to finish. 2179204076Spjd */ 2180204076Spjd mtx_lock(&sync_lock); 2181204076Spjd while (!ISSYNCREQDONE(hio)) 2182204076Spjd cv_wait(&sync_cond, &sync_lock); 2183204076Spjd mtx_unlock(&sync_lock); 2184204076Spjd 2185204076Spjd if (hio->hio_errors[ncomp] != 0) { 2186204076Spjd pjdlog_error("Unable to write synchronization data: %s.", 2187204076Spjd strerror(hio->hio_errors[ncomp])); 2188204076Spjd goto free_queue; 2189204076Spjd } 2190211880Spjd 2191211880Spjd synced += length; 2192204076Spjdfree_queue: 2193204076Spjd mtx_lock(&range_lock); 2194204076Spjd rangelock_del(range_sync, offset, length); 2195204076Spjd if (range_regular_wait) 2196204076Spjd cv_signal(&range_regular_cond); 2197204076Spjd mtx_unlock(&range_lock); 2198204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 2199204076Spjd hio); 2200204076Spjd QUEUE_INSERT2(hio, free); 2201204076Spjd } 2202204076Spjd /* NOTREACHED */ 2203204076Spjd return (NULL); 2204204076Spjd} 2205204076Spjd 2206217784Spjdvoid 2207217784Spjdprimary_config_reload(struct hast_resource *res, struct nv *nv) 2208210886Spjd{ 2209210886Spjd unsigned int ii, ncomps; 2210217784Spjd int modified, vint; 2211217784Spjd const char *vstr; 2212210886Spjd 2213210886Spjd pjdlog_info("Reloading configuration..."); 2214210886Spjd 2215218138Spjd PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 2216218138Spjd PJDLOG_ASSERT(gres == res); 2217217784Spjd nv_assert(nv, "remoteaddr"); 2218219818Spjd nv_assert(nv, "sourceaddr"); 2219217784Spjd nv_assert(nv, "replication"); 2220219351Spjd nv_assert(nv, "checksum"); 2221219354Spjd nv_assert(nv, "compression"); 2222217784Spjd nv_assert(nv, "timeout"); 2223217784Spjd nv_assert(nv, "exec"); 2224225830Spjd nv_assert(nv, "metaflush"); 2225217784Spjd 2226210886Spjd ncomps = HAST_NCOMPONENTS; 2227210886Spjd 2228219351Spjd#define MODIFIED_REMOTEADDR 0x01 2229219818Spjd#define MODIFIED_SOURCEADDR 0x02 2230219818Spjd#define MODIFIED_REPLICATION 0x04 2231219818Spjd#define MODIFIED_CHECKSUM 0x08 2232219818Spjd#define MODIFIED_COMPRESSION 0x10 2233219818Spjd#define MODIFIED_TIMEOUT 0x20 2234219818Spjd#define MODIFIED_EXEC 0x40 2235225830Spjd#define MODIFIED_METAFLUSH 0x80 2236210886Spjd modified = 0; 2237217784Spjd 2238217784Spjd vstr = nv_get_string(nv, "remoteaddr"); 2239217784Spjd if (strcmp(gres->hr_remoteaddr, vstr) != 0) { 2240210886Spjd /* 2241210886Spjd * Don't copy res->hr_remoteaddr to gres just yet. 2242210886Spjd * We want remote_close() to log disconnect from the old 2243210886Spjd * addresses, not from the new ones. 2244210886Spjd */ 2245210886Spjd modified |= MODIFIED_REMOTEADDR; 2246210886Spjd } 2247219818Spjd vstr = nv_get_string(nv, "sourceaddr"); 2248219818Spjd if (strcmp(gres->hr_sourceaddr, vstr) != 0) { 2249219818Spjd strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr)); 2250219818Spjd modified |= MODIFIED_SOURCEADDR; 2251219818Spjd } 2252217784Spjd vint = nv_get_int32(nv, "replication"); 2253217784Spjd if (gres->hr_replication != vint) { 2254217784Spjd gres->hr_replication = vint; 2255210886Spjd modified |= MODIFIED_REPLICATION; 2256210886Spjd } 2257219351Spjd vint = nv_get_int32(nv, "checksum"); 2258219351Spjd if (gres->hr_checksum != vint) { 2259219351Spjd gres->hr_checksum = vint; 2260219351Spjd modified |= MODIFIED_CHECKSUM; 2261219351Spjd } 2262219354Spjd vint = nv_get_int32(nv, "compression"); 2263219354Spjd if (gres->hr_compression != vint) { 2264219354Spjd gres->hr_compression = vint; 2265219354Spjd modified |= MODIFIED_COMPRESSION; 2266219354Spjd } 2267217784Spjd vint = nv_get_int32(nv, "timeout"); 2268217784Spjd if (gres->hr_timeout != vint) { 2269217784Spjd gres->hr_timeout = vint; 2270210886Spjd modified |= MODIFIED_TIMEOUT; 2271210886Spjd } 2272217784Spjd vstr = nv_get_string(nv, "exec"); 2273217784Spjd if (strcmp(gres->hr_exec, vstr) != 0) { 2274217784Spjd strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec)); 2275211886Spjd modified |= MODIFIED_EXEC; 2276211886Spjd } 2277225830Spjd vint = nv_get_int32(nv, "metaflush"); 2278225830Spjd if (gres->hr_metaflush != vint) { 2279225830Spjd gres->hr_metaflush = vint; 2280225830Spjd modified |= MODIFIED_METAFLUSH; 2281225830Spjd } 2282217784Spjd 2283210886Spjd /* 2284219351Spjd * Change timeout for connected sockets. 2285219351Spjd * Don't bother if we need to reconnect. 2286210886Spjd */ 2287219351Spjd if ((modified & MODIFIED_TIMEOUT) != 0 && 2288226859Spjd (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) { 2289210886Spjd for (ii = 0; ii < ncomps; ii++) { 2290210886Spjd if (!ISREMOTE(ii)) 2291210886Spjd continue; 2292210886Spjd rw_rlock(&hio_remote_lock[ii]); 2293210886Spjd if (!ISCONNECTED(gres, ii)) { 2294210886Spjd rw_unlock(&hio_remote_lock[ii]); 2295210886Spjd continue; 2296210886Spjd } 2297210886Spjd rw_unlock(&hio_remote_lock[ii]); 2298210886Spjd if (proto_timeout(gres->hr_remotein, 2299229945Spjd gres->hr_timeout) == -1) { 2300210886Spjd pjdlog_errno(LOG_WARNING, 2301210886Spjd "Unable to set connection timeout"); 2302210886Spjd } 2303210886Spjd if (proto_timeout(gres->hr_remoteout, 2304229945Spjd gres->hr_timeout) == -1) { 2305210886Spjd pjdlog_errno(LOG_WARNING, 2306210886Spjd "Unable to set connection timeout"); 2307210886Spjd } 2308210886Spjd } 2309219351Spjd } 2310226859Spjd if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) { 2311210886Spjd for (ii = 0; ii < ncomps; ii++) { 2312210886Spjd if (!ISREMOTE(ii)) 2313210886Spjd continue; 2314210886Spjd remote_close(gres, ii); 2315210886Spjd } 2316210886Spjd if (modified & MODIFIED_REMOTEADDR) { 2317217784Spjd vstr = nv_get_string(nv, "remoteaddr"); 2318217784Spjd strlcpy(gres->hr_remoteaddr, vstr, 2319210886Spjd sizeof(gres->hr_remoteaddr)); 2320210886Spjd } 2321210886Spjd } 2322210886Spjd#undef MODIFIED_REMOTEADDR 2323219818Spjd#undef MODIFIED_SOURCEADDR 2324210886Spjd#undef MODIFIED_REPLICATION 2325219351Spjd#undef MODIFIED_CHECKSUM 2326219354Spjd#undef MODIFIED_COMPRESSION 2327210886Spjd#undef MODIFIED_TIMEOUT 2328211886Spjd#undef MODIFIED_EXEC 2329225830Spjd#undef MODIFIED_METAFLUSH 2330210886Spjd 2331210886Spjd pjdlog_info("Configuration reloaded successfully."); 2332210886Spjd} 2333210886Spjd 2334211882Spjdstatic void 2335211981Spjdguard_one(struct hast_resource *res, unsigned int ncomp) 2336211981Spjd{ 2337211981Spjd struct proto_conn *in, *out; 2338211981Spjd 2339211981Spjd if (!ISREMOTE(ncomp)) 2340211981Spjd return; 2341211981Spjd 2342211981Spjd rw_rlock(&hio_remote_lock[ncomp]); 2343211981Spjd 2344211981Spjd if (!real_remote(res)) { 2345211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2346211981Spjd return; 2347211981Spjd } 2348211981Spjd 2349211981Spjd if (ISCONNECTED(res, ncomp)) { 2350218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 2351218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 2352211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2353211981Spjd pjdlog_debug(2, "remote_guard: Connection to %s is ok.", 2354211981Spjd res->hr_remoteaddr); 2355211981Spjd return; 2356211981Spjd } 2357211981Spjd 2358218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2359218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2360211981Spjd /* 2361211981Spjd * Upgrade the lock. It doesn't have to be atomic as no other thread 2362211981Spjd * can change connection status from disconnected to connected. 2363211981Spjd */ 2364211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2365211981Spjd pjdlog_debug(2, "remote_guard: Reconnecting to %s.", 2366211981Spjd res->hr_remoteaddr); 2367211981Spjd in = out = NULL; 2368220898Spjd if (init_remote(res, &in, &out) == 0) { 2369211981Spjd rw_wlock(&hio_remote_lock[ncomp]); 2370218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2371218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2372218138Spjd PJDLOG_ASSERT(in != NULL && out != NULL); 2373211981Spjd res->hr_remotein = in; 2374211981Spjd res->hr_remoteout = out; 2375211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2376211981Spjd pjdlog_info("Successfully reconnected to %s.", 2377211981Spjd res->hr_remoteaddr); 2378211981Spjd sync_start(); 2379211981Spjd } else { 2380211981Spjd /* Both connections should be NULL. */ 2381218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2382218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2383218138Spjd PJDLOG_ASSERT(in == NULL && out == NULL); 2384211981Spjd pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", 2385211981Spjd res->hr_remoteaddr); 2386211981Spjd } 2387211981Spjd} 2388211981Spjd 2389204076Spjd/* 2390204076Spjd * Thread guards remote connections and reconnects when needed, handles 2391204076Spjd * signals, etc. 2392204076Spjd */ 2393204076Spjdstatic void * 2394204076Spjdguard_thread(void *arg) 2395204076Spjd{ 2396204076Spjd struct hast_resource *res = arg; 2397204076Spjd unsigned int ii, ncomps; 2398211982Spjd struct timespec timeout; 2399211981Spjd time_t lastcheck, now; 2400211982Spjd sigset_t mask; 2401211982Spjd int signo; 2402204076Spjd 2403204076Spjd ncomps = HAST_NCOMPONENTS; 2404211981Spjd lastcheck = time(NULL); 2405204076Spjd 2406211982Spjd PJDLOG_VERIFY(sigemptyset(&mask) == 0); 2407211982Spjd PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 2408211982Spjd PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 2409211982Spjd 2410219721Strociny timeout.tv_sec = HAST_KEEPALIVE; 2411211982Spjd timeout.tv_nsec = 0; 2412211982Spjd signo = -1; 2413211982Spjd 2414204076Spjd for (;;) { 2415211982Spjd switch (signo) { 2416211982Spjd case SIGINT: 2417211982Spjd case SIGTERM: 2418211982Spjd sigexit_received = true; 2419204076Spjd primary_exitx(EX_OK, 2420204076Spjd "Termination signal received, exiting."); 2421211982Spjd break; 2422211982Spjd default: 2423211982Spjd break; 2424204076Spjd } 2425211882Spjd 2426220898Spjd /* 2427220898Spjd * Don't check connections until we fully started, 2428220898Spjd * as we may still be looping, waiting for remote node 2429220898Spjd * to switch from primary to secondary. 2430220898Spjd */ 2431220898Spjd if (fullystarted) { 2432220898Spjd pjdlog_debug(2, "remote_guard: Checking connections."); 2433220898Spjd now = time(NULL); 2434220898Spjd if (lastcheck + HAST_KEEPALIVE <= now) { 2435220898Spjd for (ii = 0; ii < ncomps; ii++) 2436220898Spjd guard_one(res, ii); 2437220898Spjd lastcheck = now; 2438220898Spjd } 2439204076Spjd } 2440211982Spjd signo = sigtimedwait(&mask, NULL, &timeout); 2441204076Spjd } 2442204076Spjd /* NOTREACHED */ 2443204076Spjd return (NULL); 2444204076Spjd} 2445