primary.c revision 226859
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 226859 2011-10-27 20:32:57Z 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/refcount.h> 39204076Spjd#include <sys/stat.h> 40204076Spjd 41204076Spjd#include <geom/gate/g_gate.h> 42204076Spjd 43204076Spjd#include <err.h> 44204076Spjd#include <errno.h> 45204076Spjd#include <fcntl.h> 46204076Spjd#include <libgeom.h> 47204076Spjd#include <pthread.h> 48211982Spjd#include <signal.h> 49204076Spjd#include <stdint.h> 50204076Spjd#include <stdio.h> 51204076Spjd#include <string.h> 52204076Spjd#include <sysexits.h> 53204076Spjd#include <unistd.h> 54204076Spjd 55204076Spjd#include <activemap.h> 56204076Spjd#include <nv.h> 57204076Spjd#include <rangelock.h> 58204076Spjd 59204076Spjd#include "control.h" 60212038Spjd#include "event.h" 61204076Spjd#include "hast.h" 62204076Spjd#include "hast_proto.h" 63204076Spjd#include "hastd.h" 64211886Spjd#include "hooks.h" 65204076Spjd#include "metadata.h" 66204076Spjd#include "proto.h" 67204076Spjd#include "pjdlog.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; 257204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) { 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); 454214284Spjd if (metadata_write(res) < 0) 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 466204076Spjd if (metadata_read(res, true) < 0) 467204076Spjd exit(EX_NOINPUT); 468204076Spjd mtx_init(&res->hr_amp_lock); 469204076Spjd if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 470204076Spjd res->hr_local_sectorsize, res->hr_keepdirty) < 0) { 471204076Spjd primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 472204076Spjd } 473204076Spjd mtx_init(&range_lock); 474204076Spjd cv_init(&range_regular_cond); 475204076Spjd if (rangelock_init(&range_regular) < 0) 476204076Spjd primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 477204076Spjd cv_init(&range_sync_cond); 478204076Spjd if (rangelock_init(&range_sync) < 0) 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; 503204076Spjd if (metadata_write(res) < 0) 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; 514218218Spjd if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) { 515218218Spjd primary_exit(EX_TEMPFAIL, 516218218Spjd "Unable to send connection request to parent"); 517218218Spjd } 518218218Spjd if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) { 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 } 528218218Spjd if (proto_connection_recv(res->hr_conn, true, &conn) < 0) { 529218218Spjd primary_exit(EX_TEMPFAIL, 530218218Spjd "Unable to receive connection from parent"); 531218218Spjd } 532220006Spjd if (proto_connect_wait(conn, res->hr_timeout) < 0) { 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? */ 539218218Spjd if (proto_timeout(conn, res->hr_timeout) < 0) 540218218Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 541218218Spjd 542218218Spjd *connp = conn; 543218218Spjd 544218218Spjd return (0); 545218218Spjd} 546218218Spjd 547220898Spjdstatic int 548205738Spjdinit_remote(struct hast_resource *res, struct proto_conn **inp, 549205738Spjd struct proto_conn **outp) 550204076Spjd{ 551205738Spjd struct proto_conn *in, *out; 552204076Spjd struct nv *nvout, *nvin; 553204076Spjd const unsigned char *token; 554204076Spjd unsigned char *map; 555204076Spjd const char *errmsg; 556204076Spjd int32_t extentsize; 557204076Spjd int64_t datasize; 558204076Spjd uint32_t mapsize; 559204076Spjd size_t size; 560220898Spjd int error; 561204076Spjd 562218138Spjd PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 563218138Spjd PJDLOG_ASSERT(real_remote(res)); 564205738Spjd 565205738Spjd in = out = NULL; 566211983Spjd errmsg = NULL; 567205738Spjd 568218218Spjd if (primary_connect(res, &out) == -1) 569220898Spjd return (ECONNREFUSED); 570218218Spjd 571220898Spjd error = ECONNABORTED; 572220898Spjd 573204076Spjd /* 574204076Spjd * First handshake step. 575204076Spjd * Setup outgoing connection with remote node. 576204076Spjd */ 577204076Spjd nvout = nv_alloc(); 578204076Spjd nv_add_string(nvout, res->hr_name, "resource"); 579204076Spjd if (nv_error(nvout) != 0) { 580204076Spjd pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 581204076Spjd "Unable to allocate header for connection with %s", 582204076Spjd res->hr_remoteaddr); 583204076Spjd nv_free(nvout); 584204076Spjd goto close; 585204076Spjd } 586205738Spjd if (hast_proto_send(res, out, nvout, NULL, 0) < 0) { 587204076Spjd pjdlog_errno(LOG_WARNING, 588204076Spjd "Unable to send handshake header to %s", 589204076Spjd res->hr_remoteaddr); 590204076Spjd nv_free(nvout); 591204076Spjd goto close; 592204076Spjd } 593204076Spjd nv_free(nvout); 594205738Spjd if (hast_proto_recv_hdr(out, &nvin) < 0) { 595204076Spjd pjdlog_errno(LOG_WARNING, 596204076Spjd "Unable to receive handshake header from %s", 597204076Spjd res->hr_remoteaddr); 598204076Spjd goto close; 599204076Spjd } 600204076Spjd errmsg = nv_get_string(nvin, "errmsg"); 601204076Spjd if (errmsg != NULL) { 602204076Spjd pjdlog_warning("%s", errmsg); 603220898Spjd if (nv_exists(nvin, "wait")) 604220898Spjd error = EBUSY; 605204076Spjd nv_free(nvin); 606204076Spjd goto close; 607204076Spjd } 608204076Spjd token = nv_get_uint8_array(nvin, &size, "token"); 609204076Spjd if (token == NULL) { 610204076Spjd pjdlog_warning("Handshake header from %s has no 'token' field.", 611204076Spjd res->hr_remoteaddr); 612204076Spjd nv_free(nvin); 613204076Spjd goto close; 614204076Spjd } 615204076Spjd if (size != sizeof(res->hr_token)) { 616204076Spjd pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 617204076Spjd res->hr_remoteaddr, size, sizeof(res->hr_token)); 618204076Spjd nv_free(nvin); 619204076Spjd goto close; 620204076Spjd } 621204076Spjd bcopy(token, res->hr_token, sizeof(res->hr_token)); 622204076Spjd nv_free(nvin); 623204076Spjd 624204076Spjd /* 625204076Spjd * Second handshake step. 626204076Spjd * Setup incoming connection with remote node. 627204076Spjd */ 628218218Spjd if (primary_connect(res, &in) == -1) 629204076Spjd goto close; 630218218Spjd 631204076Spjd nvout = nv_alloc(); 632204076Spjd nv_add_string(nvout, res->hr_name, "resource"); 633204076Spjd nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 634204076Spjd "token"); 635214284Spjd if (res->hr_resuid == 0) { 636214284Spjd /* 637214284Spjd * The resuid field was not yet initialized. 638214284Spjd * Because we do synchronization inside init_resuid(), it is 639214284Spjd * possible that someone already initialized it, the function 640214284Spjd * will return false then, but if we successfully initialized 641214284Spjd * it, we will get true. True means that there were no writes 642214284Spjd * to this resource yet and we want to inform secondary that 643214284Spjd * synchronization is not needed by sending "virgin" argument. 644214284Spjd */ 645214284Spjd if (init_resuid(res)) 646214284Spjd nv_add_int8(nvout, 1, "virgin"); 647214284Spjd } 648204076Spjd nv_add_uint64(nvout, res->hr_resuid, "resuid"); 649204076Spjd nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 650204076Spjd nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 651204076Spjd if (nv_error(nvout) != 0) { 652204076Spjd pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 653204076Spjd "Unable to allocate header for connection with %s", 654204076Spjd res->hr_remoteaddr); 655204076Spjd nv_free(nvout); 656204076Spjd goto close; 657204076Spjd } 658205738Spjd if (hast_proto_send(res, in, nvout, NULL, 0) < 0) { 659204076Spjd pjdlog_errno(LOG_WARNING, 660204076Spjd "Unable to send handshake header to %s", 661204076Spjd res->hr_remoteaddr); 662204076Spjd nv_free(nvout); 663204076Spjd goto close; 664204076Spjd } 665204076Spjd nv_free(nvout); 666205738Spjd if (hast_proto_recv_hdr(out, &nvin) < 0) { 667204076Spjd pjdlog_errno(LOG_WARNING, 668204076Spjd "Unable to receive handshake header from %s", 669204076Spjd res->hr_remoteaddr); 670204076Spjd goto close; 671204076Spjd } 672204076Spjd errmsg = nv_get_string(nvin, "errmsg"); 673204076Spjd if (errmsg != NULL) { 674204076Spjd pjdlog_warning("%s", errmsg); 675204076Spjd nv_free(nvin); 676204076Spjd goto close; 677204076Spjd } 678204076Spjd datasize = nv_get_int64(nvin, "datasize"); 679204076Spjd if (datasize != res->hr_datasize) { 680204076Spjd pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 681204076Spjd (intmax_t)res->hr_datasize, (intmax_t)datasize); 682204076Spjd nv_free(nvin); 683204076Spjd goto close; 684204076Spjd } 685204076Spjd extentsize = nv_get_int32(nvin, "extentsize"); 686204076Spjd if (extentsize != res->hr_extentsize) { 687204076Spjd pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 688204076Spjd (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 689204076Spjd nv_free(nvin); 690204076Spjd goto close; 691204076Spjd } 692204076Spjd res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 693204076Spjd res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 694204076Spjd res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 695220865Spjd if (nv_exists(nvin, "virgin")) { 696220865Spjd /* 697220865Spjd * Secondary was reinitialized, bump localcnt if it is 0 as 698220865Spjd * only we have the data. 699220865Spjd */ 700220865Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY); 701220865Spjd PJDLOG_ASSERT(res->hr_secondary_localcnt == 0); 702220865Spjd 703220865Spjd if (res->hr_primary_localcnt == 0) { 704220865Spjd PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0); 705220865Spjd 706220865Spjd mtx_lock(&metadata_lock); 707220865Spjd res->hr_primary_localcnt++; 708220865Spjd pjdlog_debug(1, "Increasing localcnt to %ju.", 709220865Spjd (uintmax_t)res->hr_primary_localcnt); 710220865Spjd (void)metadata_write(res); 711220865Spjd mtx_unlock(&metadata_lock); 712220865Spjd } 713220865Spjd } 714204076Spjd map = NULL; 715204076Spjd mapsize = nv_get_uint32(nvin, "mapsize"); 716204076Spjd if (mapsize > 0) { 717204076Spjd map = malloc(mapsize); 718204076Spjd if (map == NULL) { 719204076Spjd pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 720204076Spjd (uintmax_t)mapsize); 721204076Spjd nv_free(nvin); 722204076Spjd goto close; 723204076Spjd } 724204076Spjd /* 725204076Spjd * Remote node have some dirty extents on its own, lets 726204076Spjd * download its activemap. 727204076Spjd */ 728205738Spjd if (hast_proto_recv_data(res, out, nvin, map, 729204076Spjd mapsize) < 0) { 730204076Spjd pjdlog_errno(LOG_ERR, 731204076Spjd "Unable to receive remote activemap"); 732204076Spjd nv_free(nvin); 733204076Spjd free(map); 734204076Spjd goto close; 735204076Spjd } 736204076Spjd /* 737204076Spjd * Merge local and remote bitmaps. 738204076Spjd */ 739204076Spjd activemap_merge(res->hr_amp, map, mapsize); 740204076Spjd free(map); 741204076Spjd /* 742204076Spjd * Now that we merged bitmaps from both nodes, flush it to the 743204076Spjd * disk before we start to synchronize. 744204076Spjd */ 745204076Spjd (void)hast_activemap_flush(res); 746204076Spjd } 747214274Spjd nv_free(nvin); 748223181Strociny#ifdef notyet 749220271Spjd /* Setup directions. */ 750220271Spjd if (proto_send(out, NULL, 0) == -1) 751220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 752220271Spjd if (proto_recv(in, NULL, 0) == -1) 753220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 754223181Strociny#endif 755204076Spjd pjdlog_info("Connected to %s.", res->hr_remoteaddr); 756205738Spjd if (inp != NULL && outp != NULL) { 757205738Spjd *inp = in; 758205738Spjd *outp = out; 759205738Spjd } else { 760205738Spjd res->hr_remotein = in; 761205738Spjd res->hr_remoteout = out; 762205738Spjd } 763212038Spjd event_send(res, EVENT_CONNECT); 764220898Spjd return (0); 765205738Spjdclose: 766211983Spjd if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0) 767212038Spjd event_send(res, EVENT_SPLITBRAIN); 768205738Spjd proto_close(out); 769205738Spjd if (in != NULL) 770205738Spjd proto_close(in); 771220898Spjd return (error); 772205738Spjd} 773205738Spjd 774205738Spjdstatic void 775205738Spjdsync_start(void) 776205738Spjd{ 777205738Spjd 778204076Spjd mtx_lock(&sync_lock); 779204076Spjd sync_inprogress = true; 780204076Spjd mtx_unlock(&sync_lock); 781204076Spjd cv_signal(&sync_cond); 782204076Spjd} 783204076Spjd 784204076Spjdstatic void 785211878Spjdsync_stop(void) 786211878Spjd{ 787211878Spjd 788211878Spjd mtx_lock(&sync_lock); 789211878Spjd if (sync_inprogress) 790211878Spjd sync_inprogress = false; 791211878Spjd mtx_unlock(&sync_lock); 792211878Spjd} 793211878Spjd 794211878Spjdstatic void 795204076Spjdinit_ggate(struct hast_resource *res) 796204076Spjd{ 797204076Spjd struct g_gate_ctl_create ggiocreate; 798204076Spjd struct g_gate_ctl_cancel ggiocancel; 799204076Spjd 800204076Spjd /* 801204076Spjd * We communicate with ggate via /dev/ggctl. Open it. 802204076Spjd */ 803204076Spjd res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 804204076Spjd if (res->hr_ggatefd < 0) 805204076Spjd primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 806204076Spjd /* 807204076Spjd * Create provider before trying to connect, as connection failure 808204076Spjd * is not critical, but may take some time. 809204076Spjd */ 810213533Spjd bzero(&ggiocreate, sizeof(ggiocreate)); 811204076Spjd ggiocreate.gctl_version = G_GATE_VERSION; 812204076Spjd ggiocreate.gctl_mediasize = res->hr_datasize; 813204076Spjd ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 814204076Spjd ggiocreate.gctl_flags = 0; 815220266Spjd ggiocreate.gctl_maxcount = 0; 816204076Spjd ggiocreate.gctl_timeout = 0; 817204076Spjd ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 818204076Spjd snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 819204076Spjd res->hr_provname); 820204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 821204076Spjd pjdlog_info("Device hast/%s created.", res->hr_provname); 822204076Spjd res->hr_ggateunit = ggiocreate.gctl_unit; 823204076Spjd return; 824204076Spjd } 825204076Spjd if (errno != EEXIST) { 826204076Spjd primary_exit(EX_OSERR, "Unable to create hast/%s device", 827204076Spjd res->hr_provname); 828204076Spjd } 829204076Spjd pjdlog_debug(1, 830204076Spjd "Device hast/%s already exists, we will try to take it over.", 831204076Spjd res->hr_provname); 832204076Spjd /* 833204076Spjd * If we received EEXIST, we assume that the process who created the 834204076Spjd * provider died and didn't clean up. In that case we will start from 835204076Spjd * where he left of. 836204076Spjd */ 837213533Spjd bzero(&ggiocancel, sizeof(ggiocancel)); 838204076Spjd ggiocancel.gctl_version = G_GATE_VERSION; 839204076Spjd ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 840204076Spjd snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 841204076Spjd res->hr_provname); 842204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 843204076Spjd pjdlog_info("Device hast/%s recovered.", res->hr_provname); 844204076Spjd res->hr_ggateunit = ggiocancel.gctl_unit; 845204076Spjd return; 846204076Spjd } 847204076Spjd primary_exit(EX_OSERR, "Unable to take over hast/%s device", 848204076Spjd res->hr_provname); 849204076Spjd} 850204076Spjd 851204076Spjdvoid 852204076Spjdhastd_primary(struct hast_resource *res) 853204076Spjd{ 854204076Spjd pthread_t td; 855204076Spjd pid_t pid; 856219482Strociny int error, mode, debuglevel; 857204076Spjd 858204076Spjd /* 859218218Spjd * Create communication channel for sending control commands from 860218218Spjd * parent to child. 861204076Spjd */ 862219818Spjd if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) { 863218042Spjd /* TODO: There's no need for this to be fatal error. */ 864204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 865212034Spjd pjdlog_exit(EX_OSERR, 866204076Spjd "Unable to create control sockets between parent and child"); 867204076Spjd } 868212038Spjd /* 869218218Spjd * Create communication channel for sending events from child to parent. 870212038Spjd */ 871219818Spjd if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) { 872218042Spjd /* TODO: There's no need for this to be fatal error. */ 873212038Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 874212038Spjd pjdlog_exit(EX_OSERR, 875212038Spjd "Unable to create event sockets between child and parent"); 876212038Spjd } 877218218Spjd /* 878218218Spjd * Create communication channel for sending connection requests from 879218218Spjd * child to parent. 880218218Spjd */ 881219818Spjd if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) { 882218218Spjd /* TODO: There's no need for this to be fatal error. */ 883218218Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 884218218Spjd pjdlog_exit(EX_OSERR, 885218218Spjd "Unable to create connection sockets between child and parent"); 886218218Spjd } 887204076Spjd 888204076Spjd pid = fork(); 889204076Spjd if (pid < 0) { 890218042Spjd /* TODO: There's no need for this to be fatal error. */ 891204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 892212034Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to fork"); 893204076Spjd } 894204076Spjd 895204076Spjd if (pid > 0) { 896204076Spjd /* This is parent. */ 897212038Spjd /* Declare that we are receiver. */ 898212038Spjd proto_recv(res->hr_event, NULL, 0); 899218218Spjd proto_recv(res->hr_conn, NULL, 0); 900218043Spjd /* Declare that we are sender. */ 901218043Spjd proto_send(res->hr_ctrl, NULL, 0); 902204076Spjd res->hr_workerpid = pid; 903204076Spjd return; 904204076Spjd } 905211977Spjd 906211984Spjd gres = res; 907218043Spjd mode = pjdlog_mode_get(); 908219482Strociny debuglevel = pjdlog_debug_get(); 909211984Spjd 910218043Spjd /* Declare that we are sender. */ 911218043Spjd proto_send(res->hr_event, NULL, 0); 912218218Spjd proto_send(res->hr_conn, NULL, 0); 913218043Spjd /* Declare that we are receiver. */ 914218043Spjd proto_recv(res->hr_ctrl, NULL, 0); 915218043Spjd descriptors_cleanup(res); 916204076Spjd 917218045Spjd descriptors_assert(res, mode); 918218045Spjd 919218043Spjd pjdlog_init(mode); 920219482Strociny pjdlog_debug_set(debuglevel); 921218043Spjd pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 922220005Spjd setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role)); 923204076Spjd 924204076Spjd init_local(res); 925213007Spjd init_ggate(res); 926213007Spjd init_environment(res); 927217784Spjd 928221899Spjd if (drop_privs(res) != 0) { 929218049Spjd cleanup(res); 930218049Spjd exit(EX_CONFIG); 931218049Spjd } 932218214Spjd pjdlog_info("Privileges successfully dropped."); 933218049Spjd 934213007Spjd /* 935213530Spjd * Create the guard thread first, so we can handle signals from the 936213530Spjd * very begining. 937213530Spjd */ 938213530Spjd error = pthread_create(&td, NULL, guard_thread, res); 939218138Spjd PJDLOG_ASSERT(error == 0); 940213530Spjd /* 941213007Spjd * Create the control thread before sending any event to the parent, 942213007Spjd * as we can deadlock when parent sends control request to worker, 943213007Spjd * but worker has no control thread started yet, so parent waits. 944213007Spjd * In the meantime worker sends an event to the parent, but parent 945213007Spjd * is unable to handle the event, because it waits for control 946213007Spjd * request response. 947213007Spjd */ 948213007Spjd error = pthread_create(&td, NULL, ctrl_thread, res); 949218138Spjd PJDLOG_ASSERT(error == 0); 950220898Spjd if (real_remote(res)) { 951220898Spjd error = init_remote(res, NULL, NULL); 952220898Spjd if (error == 0) { 953220898Spjd sync_start(); 954220898Spjd } else if (error == EBUSY) { 955220898Spjd time_t start = time(NULL); 956220898Spjd 957220898Spjd pjdlog_warning("Waiting for remote node to become %s for %ds.", 958220898Spjd role2str(HAST_ROLE_SECONDARY), 959220898Spjd res->hr_timeout); 960220898Spjd for (;;) { 961220898Spjd sleep(1); 962220898Spjd error = init_remote(res, NULL, NULL); 963220898Spjd if (error != EBUSY) 964220898Spjd break; 965220898Spjd if (time(NULL) > start + res->hr_timeout) 966220898Spjd break; 967220898Spjd } 968220898Spjd if (error == EBUSY) { 969220898Spjd pjdlog_warning("Remote node is still %s, starting anyway.", 970220898Spjd role2str(HAST_ROLE_PRIMARY)); 971220898Spjd } 972220898Spjd } 973220898Spjd } 974204076Spjd error = pthread_create(&td, NULL, ggate_recv_thread, res); 975218138Spjd PJDLOG_ASSERT(error == 0); 976204076Spjd error = pthread_create(&td, NULL, local_send_thread, res); 977218138Spjd PJDLOG_ASSERT(error == 0); 978204076Spjd error = pthread_create(&td, NULL, remote_send_thread, res); 979218138Spjd PJDLOG_ASSERT(error == 0); 980204076Spjd error = pthread_create(&td, NULL, remote_recv_thread, res); 981218138Spjd PJDLOG_ASSERT(error == 0); 982204076Spjd error = pthread_create(&td, NULL, ggate_send_thread, res); 983218138Spjd PJDLOG_ASSERT(error == 0); 984220898Spjd fullystarted = true; 985213530Spjd (void)sync_thread(res); 986204076Spjd} 987204076Spjd 988204076Spjdstatic void 989204076Spjdreqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...) 990204076Spjd{ 991204076Spjd char msg[1024]; 992204076Spjd va_list ap; 993204076Spjd int len; 994204076Spjd 995204076Spjd va_start(ap, fmt); 996204076Spjd len = vsnprintf(msg, sizeof(msg), fmt, ap); 997204076Spjd va_end(ap); 998204076Spjd if ((size_t)len < sizeof(msg)) { 999204076Spjd switch (ggio->gctl_cmd) { 1000204076Spjd case BIO_READ: 1001204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 1002204076Spjd "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 1003204076Spjd (uintmax_t)ggio->gctl_length); 1004204076Spjd break; 1005204076Spjd case BIO_DELETE: 1006204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 1007204076Spjd "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 1008204076Spjd (uintmax_t)ggio->gctl_length); 1009204076Spjd break; 1010204076Spjd case BIO_FLUSH: 1011204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 1012204076Spjd break; 1013204076Spjd case BIO_WRITE: 1014204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 1015204076Spjd "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 1016204076Spjd (uintmax_t)ggio->gctl_length); 1017204076Spjd break; 1018204076Spjd default: 1019204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 1020204076Spjd "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd); 1021204076Spjd break; 1022204076Spjd } 1023204076Spjd } 1024204076Spjd pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 1025204076Spjd} 1026204076Spjd 1027204076Spjdstatic void 1028204076Spjdremote_close(struct hast_resource *res, int ncomp) 1029204076Spjd{ 1030204076Spjd 1031204076Spjd rw_wlock(&hio_remote_lock[ncomp]); 1032204076Spjd /* 1033226855Spjd * Check for a race between dropping rlock and acquiring wlock - 1034204076Spjd * another thread can close connection in-between. 1035204076Spjd */ 1036204076Spjd if (!ISCONNECTED(res, ncomp)) { 1037218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 1038218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 1039204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1040204076Spjd return; 1041204076Spjd } 1042204076Spjd 1043218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 1044218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 1045204076Spjd 1046211881Spjd pjdlog_debug(2, "Closing incoming connection to %s.", 1047204076Spjd res->hr_remoteaddr); 1048204076Spjd proto_close(res->hr_remotein); 1049204076Spjd res->hr_remotein = NULL; 1050211881Spjd pjdlog_debug(2, "Closing outgoing connection to %s.", 1051204076Spjd res->hr_remoteaddr); 1052204076Spjd proto_close(res->hr_remoteout); 1053204076Spjd res->hr_remoteout = NULL; 1054204076Spjd 1055204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1056204076Spjd 1057211881Spjd pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 1058211881Spjd 1059204076Spjd /* 1060204076Spjd * Stop synchronization if in-progress. 1061204076Spjd */ 1062211878Spjd sync_stop(); 1063211984Spjd 1064212038Spjd event_send(res, EVENT_DISCONNECT); 1065204076Spjd} 1066204076Spjd 1067204076Spjd/* 1068226859Spjd * Acknowledge write completion to the kernel, but don't update activemap yet. 1069226859Spjd */ 1070226859Spjdstatic void 1071226859Spjdwrite_complete(struct hast_resource *res, struct hio *hio) 1072226859Spjd{ 1073226859Spjd struct g_gate_ctl_io *ggio; 1074226859Spjd unsigned int ncomp; 1075226859Spjd 1076226859Spjd PJDLOG_ASSERT(!hio->hio_done); 1077226859Spjd 1078226859Spjd ggio = &hio->hio_ggio; 1079226859Spjd PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE); 1080226859Spjd 1081226859Spjd /* 1082226859Spjd * Bump local count if this is first write after 1083226859Spjd * connection failure with remote node. 1084226859Spjd */ 1085226859Spjd ncomp = 1; 1086226859Spjd rw_rlock(&hio_remote_lock[ncomp]); 1087226859Spjd if (!ISCONNECTED(res, ncomp)) { 1088226859Spjd mtx_lock(&metadata_lock); 1089226859Spjd if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) { 1090226859Spjd res->hr_primary_localcnt++; 1091226859Spjd pjdlog_debug(1, "Increasing localcnt to %ju.", 1092226859Spjd (uintmax_t)res->hr_primary_localcnt); 1093226859Spjd (void)metadata_write(res); 1094226859Spjd } 1095226859Spjd mtx_unlock(&metadata_lock); 1096226859Spjd } 1097226859Spjd rw_unlock(&hio_remote_lock[ncomp]); 1098226859Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) 1099226859Spjd primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 1100226859Spjd hio->hio_done = true; 1101226859Spjd} 1102226859Spjd 1103226859Spjd/* 1104204076Spjd * Thread receives ggate I/O requests from the kernel and passes them to 1105204076Spjd * appropriate threads: 1106204076Spjd * WRITE - always goes to both local_send and remote_send threads 1107204076Spjd * READ (when the block is up-to-date on local component) - 1108204076Spjd * only local_send thread 1109204076Spjd * READ (when the block isn't up-to-date on local component) - 1110204076Spjd * only remote_send thread 1111204076Spjd * DELETE - always goes to both local_send and remote_send threads 1112204076Spjd * FLUSH - always goes to both local_send and remote_send threads 1113204076Spjd */ 1114204076Spjdstatic void * 1115204076Spjdggate_recv_thread(void *arg) 1116204076Spjd{ 1117204076Spjd struct hast_resource *res = arg; 1118204076Spjd struct g_gate_ctl_io *ggio; 1119204076Spjd struct hio *hio; 1120204076Spjd unsigned int ii, ncomp, ncomps; 1121204076Spjd int error; 1122204076Spjd 1123204076Spjd for (;;) { 1124204076Spjd pjdlog_debug(2, "ggate_recv: Taking free request."); 1125204076Spjd QUEUE_TAKE2(hio, free); 1126204076Spjd pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 1127204076Spjd ggio = &hio->hio_ggio; 1128204076Spjd ggio->gctl_unit = res->hr_ggateunit; 1129204076Spjd ggio->gctl_length = MAXPHYS; 1130204076Spjd ggio->gctl_error = 0; 1131226859Spjd hio->hio_done = false; 1132226859Spjd hio->hio_replication = res->hr_replication; 1133204076Spjd pjdlog_debug(2, 1134204076Spjd "ggate_recv: (%p) Waiting for request from the kernel.", 1135204076Spjd hio); 1136204076Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) { 1137204076Spjd if (sigexit_received) 1138204076Spjd pthread_exit(NULL); 1139204076Spjd primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 1140204076Spjd } 1141204076Spjd error = ggio->gctl_error; 1142204076Spjd switch (error) { 1143204076Spjd case 0: 1144204076Spjd break; 1145204076Spjd case ECANCELED: 1146204076Spjd /* Exit gracefully. */ 1147204076Spjd if (!sigexit_received) { 1148204076Spjd pjdlog_debug(2, 1149204076Spjd "ggate_recv: (%p) Received cancel from the kernel.", 1150204076Spjd hio); 1151204076Spjd pjdlog_info("Received cancel from the kernel, exiting."); 1152204076Spjd } 1153204076Spjd pthread_exit(NULL); 1154204076Spjd case ENOMEM: 1155204076Spjd /* 1156204076Spjd * Buffer too small? Impossible, we allocate MAXPHYS 1157204076Spjd * bytes - request can't be bigger than that. 1158204076Spjd */ 1159204076Spjd /* FALLTHROUGH */ 1160204076Spjd case ENXIO: 1161204076Spjd default: 1162204076Spjd primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 1163204076Spjd strerror(error)); 1164204076Spjd } 1165226859Spjd 1166226859Spjd ncomp = 0; 1167226859Spjd ncomps = HAST_NCOMPONENTS; 1168226859Spjd 1169204076Spjd for (ii = 0; ii < ncomps; ii++) 1170204076Spjd hio->hio_errors[ii] = EINVAL; 1171204076Spjd reqlog(LOG_DEBUG, 2, ggio, 1172204076Spjd "ggate_recv: (%p) Request received from the kernel: ", 1173204076Spjd hio); 1174226859Spjd 1175204076Spjd /* 1176204076Spjd * Inform all components about new write request. 1177204076Spjd * For read request prefer local component unless the given 1178204076Spjd * range is out-of-date, then use remote component. 1179204076Spjd */ 1180204076Spjd switch (ggio->gctl_cmd) { 1181204076Spjd case BIO_READ: 1182222228Spjd res->hr_stat_read++; 1183226859Spjd ncomps = 1; 1184204076Spjd mtx_lock(&metadata_lock); 1185204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 1186204076Spjd res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1187204076Spjd /* 1188204076Spjd * This range is up-to-date on local component, 1189204076Spjd * so handle request locally. 1190204076Spjd */ 1191204076Spjd /* Local component is 0 for now. */ 1192204076Spjd ncomp = 0; 1193204076Spjd } else /* if (res->hr_syncsrc == 1194204076Spjd HAST_SYNCSRC_SECONDARY) */ { 1195218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == 1196204076Spjd HAST_SYNCSRC_SECONDARY); 1197204076Spjd /* 1198204076Spjd * This range is out-of-date on local component, 1199204076Spjd * so send request to the remote node. 1200204076Spjd */ 1201204076Spjd /* Remote component is 1 for now. */ 1202204076Spjd ncomp = 1; 1203204076Spjd } 1204204076Spjd mtx_unlock(&metadata_lock); 1205204076Spjd break; 1206204076Spjd case BIO_WRITE: 1207222228Spjd res->hr_stat_write++; 1208226851Spjd if (res->hr_resuid == 0 && 1209226851Spjd res->hr_primary_localcnt == 0) { 1210226851Spjd /* This is first write. */ 1211219844Spjd res->hr_primary_localcnt = 1; 1212214284Spjd } 1213204076Spjd for (;;) { 1214204076Spjd mtx_lock(&range_lock); 1215204076Spjd if (rangelock_islocked(range_sync, 1216204076Spjd ggio->gctl_offset, ggio->gctl_length)) { 1217204076Spjd pjdlog_debug(2, 1218204076Spjd "regular: Range offset=%jd length=%zu locked.", 1219204076Spjd (intmax_t)ggio->gctl_offset, 1220204076Spjd (size_t)ggio->gctl_length); 1221204076Spjd range_regular_wait = true; 1222204076Spjd cv_wait(&range_regular_cond, &range_lock); 1223204076Spjd range_regular_wait = false; 1224204076Spjd mtx_unlock(&range_lock); 1225204076Spjd continue; 1226204076Spjd } 1227204076Spjd if (rangelock_add(range_regular, 1228204076Spjd ggio->gctl_offset, ggio->gctl_length) < 0) { 1229204076Spjd mtx_unlock(&range_lock); 1230204076Spjd pjdlog_debug(2, 1231204076Spjd "regular: Range offset=%jd length=%zu is already locked, waiting.", 1232204076Spjd (intmax_t)ggio->gctl_offset, 1233204076Spjd (size_t)ggio->gctl_length); 1234204076Spjd sleep(1); 1235204076Spjd continue; 1236204076Spjd } 1237204076Spjd mtx_unlock(&range_lock); 1238204076Spjd break; 1239204076Spjd } 1240204076Spjd mtx_lock(&res->hr_amp_lock); 1241204076Spjd if (activemap_write_start(res->hr_amp, 1242204076Spjd ggio->gctl_offset, ggio->gctl_length)) { 1243222228Spjd res->hr_stat_activemap_update++; 1244204076Spjd (void)hast_activemap_flush(res); 1245204076Spjd } 1246204076Spjd mtx_unlock(&res->hr_amp_lock); 1247226859Spjd break; 1248204076Spjd case BIO_DELETE: 1249226859Spjd res->hr_stat_delete++; 1250226859Spjd break; 1251204076Spjd case BIO_FLUSH: 1252226859Spjd res->hr_stat_flush++; 1253204076Spjd break; 1254204076Spjd } 1255226859Spjd pjdlog_debug(2, 1256226859Spjd "ggate_recv: (%p) Moving request to the send queues.", hio); 1257226859Spjd refcount_init(&hio->hio_countdown, ncomps); 1258226859Spjd for (ii = ncomp; ii < ncomps; ii++) 1259226859Spjd QUEUE_INSERT1(hio, send, ii); 1260204076Spjd } 1261204076Spjd /* NOTREACHED */ 1262204076Spjd return (NULL); 1263204076Spjd} 1264204076Spjd 1265204076Spjd/* 1266204076Spjd * Thread reads from or writes to local component. 1267204076Spjd * If local read fails, it redirects it to remote_send thread. 1268204076Spjd */ 1269204076Spjdstatic void * 1270204076Spjdlocal_send_thread(void *arg) 1271204076Spjd{ 1272204076Spjd struct hast_resource *res = arg; 1273204076Spjd struct g_gate_ctl_io *ggio; 1274204076Spjd struct hio *hio; 1275204076Spjd unsigned int ncomp, rncomp; 1276204076Spjd ssize_t ret; 1277204076Spjd 1278204076Spjd /* Local component is 0 for now. */ 1279204076Spjd ncomp = 0; 1280204076Spjd /* Remote component is 1 for now. */ 1281204076Spjd rncomp = 1; 1282204076Spjd 1283204076Spjd for (;;) { 1284204076Spjd pjdlog_debug(2, "local_send: Taking request."); 1285214692Spjd QUEUE_TAKE1(hio, send, ncomp, 0); 1286204076Spjd pjdlog_debug(2, "local_send: (%p) Got request.", hio); 1287204076Spjd ggio = &hio->hio_ggio; 1288204076Spjd switch (ggio->gctl_cmd) { 1289204076Spjd case BIO_READ: 1290204076Spjd ret = pread(res->hr_localfd, ggio->gctl_data, 1291204076Spjd ggio->gctl_length, 1292204076Spjd ggio->gctl_offset + res->hr_localoff); 1293204076Spjd if (ret == ggio->gctl_length) 1294204076Spjd hio->hio_errors[ncomp] = 0; 1295222467Strociny else if (!ISSYNCREQ(hio)) { 1296204076Spjd /* 1297204076Spjd * If READ failed, try to read from remote node. 1298204076Spjd */ 1299216479Spjd if (ret < 0) { 1300216479Spjd reqlog(LOG_WARNING, 0, ggio, 1301216479Spjd "Local request failed (%s), trying remote node. ", 1302216479Spjd strerror(errno)); 1303216479Spjd } else if (ret != ggio->gctl_length) { 1304216479Spjd reqlog(LOG_WARNING, 0, ggio, 1305216479Spjd "Local request failed (%zd != %jd), trying remote node. ", 1306216494Spjd ret, (intmax_t)ggio->gctl_length); 1307216479Spjd } 1308204076Spjd QUEUE_INSERT1(hio, send, rncomp); 1309204076Spjd continue; 1310204076Spjd } 1311204076Spjd break; 1312204076Spjd case BIO_WRITE: 1313204076Spjd ret = pwrite(res->hr_localfd, ggio->gctl_data, 1314204076Spjd ggio->gctl_length, 1315204076Spjd ggio->gctl_offset + res->hr_localoff); 1316216479Spjd if (ret < 0) { 1317204076Spjd hio->hio_errors[ncomp] = errno; 1318216479Spjd reqlog(LOG_WARNING, 0, ggio, 1319216479Spjd "Local request failed (%s): ", 1320216479Spjd strerror(errno)); 1321216479Spjd } else if (ret != ggio->gctl_length) { 1322204076Spjd hio->hio_errors[ncomp] = EIO; 1323216479Spjd reqlog(LOG_WARNING, 0, ggio, 1324216479Spjd "Local request failed (%zd != %jd): ", 1325216494Spjd ret, (intmax_t)ggio->gctl_length); 1326216479Spjd } else { 1327204076Spjd hio->hio_errors[ncomp] = 0; 1328226859Spjd if (hio->hio_replication == 1329226859Spjd HAST_REPLICATION_ASYNC) { 1330226859Spjd ggio->gctl_error = 0; 1331226859Spjd write_complete(res, hio); 1332226859Spjd } 1333216479Spjd } 1334204076Spjd break; 1335204076Spjd case BIO_DELETE: 1336204076Spjd ret = g_delete(res->hr_localfd, 1337204076Spjd ggio->gctl_offset + res->hr_localoff, 1338204076Spjd ggio->gctl_length); 1339216479Spjd if (ret < 0) { 1340204076Spjd hio->hio_errors[ncomp] = errno; 1341216479Spjd reqlog(LOG_WARNING, 0, ggio, 1342216479Spjd "Local request failed (%s): ", 1343216479Spjd strerror(errno)); 1344216479Spjd } else { 1345204076Spjd hio->hio_errors[ncomp] = 0; 1346216479Spjd } 1347204076Spjd break; 1348204076Spjd case BIO_FLUSH: 1349225832Spjd if (!res->hr_localflush) { 1350225832Spjd ret = -1; 1351225832Spjd errno = EOPNOTSUPP; 1352225832Spjd break; 1353225832Spjd } 1354204076Spjd ret = g_flush(res->hr_localfd); 1355216479Spjd if (ret < 0) { 1356225832Spjd if (errno == EOPNOTSUPP) 1357225832Spjd res->hr_localflush = false; 1358204076Spjd hio->hio_errors[ncomp] = errno; 1359216479Spjd reqlog(LOG_WARNING, 0, ggio, 1360216479Spjd "Local request failed (%s): ", 1361216479Spjd strerror(errno)); 1362216479Spjd } else { 1363204076Spjd hio->hio_errors[ncomp] = 0; 1364216479Spjd } 1365204076Spjd break; 1366204076Spjd } 1367226856Spjd if (!refcount_release(&hio->hio_countdown)) 1368226856Spjd continue; 1369226856Spjd if (ISSYNCREQ(hio)) { 1370226856Spjd mtx_lock(&sync_lock); 1371226856Spjd SYNCREQDONE(hio); 1372226856Spjd mtx_unlock(&sync_lock); 1373226856Spjd cv_signal(&sync_cond); 1374226856Spjd } else { 1375226856Spjd pjdlog_debug(2, 1376226856Spjd "local_send: (%p) Moving request to the done queue.", 1377226856Spjd hio); 1378226856Spjd QUEUE_INSERT2(hio, done); 1379204076Spjd } 1380204076Spjd } 1381204076Spjd /* NOTREACHED */ 1382204076Spjd return (NULL); 1383204076Spjd} 1384204076Spjd 1385214692Spjdstatic void 1386214692Spjdkeepalive_send(struct hast_resource *res, unsigned int ncomp) 1387214692Spjd{ 1388214692Spjd struct nv *nv; 1389214692Spjd 1390218217Spjd rw_rlock(&hio_remote_lock[ncomp]); 1391218217Spjd 1392218217Spjd if (!ISCONNECTED(res, ncomp)) { 1393218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1394214692Spjd return; 1395218217Spjd } 1396219864Spjd 1397218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 1398218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 1399214692Spjd 1400214692Spjd nv = nv_alloc(); 1401214692Spjd nv_add_uint8(nv, HIO_KEEPALIVE, "cmd"); 1402214692Spjd if (nv_error(nv) != 0) { 1403218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1404214692Spjd nv_free(nv); 1405214692Spjd pjdlog_debug(1, 1406214692Spjd "keepalive_send: Unable to prepare header to send."); 1407214692Spjd return; 1408214692Spjd } 1409214692Spjd if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) { 1410218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1411214692Spjd pjdlog_common(LOG_DEBUG, 1, errno, 1412214692Spjd "keepalive_send: Unable to send request"); 1413214692Spjd nv_free(nv); 1414214692Spjd remote_close(res, ncomp); 1415214692Spjd return; 1416214692Spjd } 1417218217Spjd 1418218217Spjd rw_unlock(&hio_remote_lock[ncomp]); 1419214692Spjd nv_free(nv); 1420214692Spjd pjdlog_debug(2, "keepalive_send: Request sent."); 1421214692Spjd} 1422214692Spjd 1423204076Spjd/* 1424204076Spjd * Thread sends request to secondary node. 1425204076Spjd */ 1426204076Spjdstatic void * 1427204076Spjdremote_send_thread(void *arg) 1428204076Spjd{ 1429204076Spjd struct hast_resource *res = arg; 1430204076Spjd struct g_gate_ctl_io *ggio; 1431214692Spjd time_t lastcheck, now; 1432204076Spjd struct hio *hio; 1433204076Spjd struct nv *nv; 1434204076Spjd unsigned int ncomp; 1435204076Spjd bool wakeup; 1436204076Spjd uint64_t offset, length; 1437204076Spjd uint8_t cmd; 1438204076Spjd void *data; 1439204076Spjd 1440204076Spjd /* Remote component is 1 for now. */ 1441204076Spjd ncomp = 1; 1442219864Spjd lastcheck = time(NULL); 1443204076Spjd 1444204076Spjd for (;;) { 1445204076Spjd pjdlog_debug(2, "remote_send: Taking request."); 1446219721Strociny QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE); 1447214692Spjd if (hio == NULL) { 1448214692Spjd now = time(NULL); 1449219721Strociny if (lastcheck + HAST_KEEPALIVE <= now) { 1450214692Spjd keepalive_send(res, ncomp); 1451214692Spjd lastcheck = now; 1452214692Spjd } 1453214692Spjd continue; 1454214692Spjd } 1455204076Spjd pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 1456204076Spjd ggio = &hio->hio_ggio; 1457204076Spjd switch (ggio->gctl_cmd) { 1458204076Spjd case BIO_READ: 1459204076Spjd cmd = HIO_READ; 1460204076Spjd data = NULL; 1461204076Spjd offset = ggio->gctl_offset; 1462204076Spjd length = ggio->gctl_length; 1463204076Spjd break; 1464204076Spjd case BIO_WRITE: 1465204076Spjd cmd = HIO_WRITE; 1466204076Spjd data = ggio->gctl_data; 1467204076Spjd offset = ggio->gctl_offset; 1468204076Spjd length = ggio->gctl_length; 1469204076Spjd break; 1470204076Spjd case BIO_DELETE: 1471204076Spjd cmd = HIO_DELETE; 1472204076Spjd data = NULL; 1473204076Spjd offset = ggio->gctl_offset; 1474204076Spjd length = ggio->gctl_length; 1475204076Spjd break; 1476204076Spjd case BIO_FLUSH: 1477204076Spjd cmd = HIO_FLUSH; 1478204076Spjd data = NULL; 1479204076Spjd offset = 0; 1480204076Spjd length = 0; 1481204076Spjd break; 1482204076Spjd default: 1483225783Spjd PJDLOG_ABORT("invalid condition"); 1484204076Spjd } 1485204076Spjd nv = nv_alloc(); 1486204076Spjd nv_add_uint8(nv, cmd, "cmd"); 1487204076Spjd nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 1488204076Spjd nv_add_uint64(nv, offset, "offset"); 1489204076Spjd nv_add_uint64(nv, length, "length"); 1490204076Spjd if (nv_error(nv) != 0) { 1491204076Spjd hio->hio_errors[ncomp] = nv_error(nv); 1492204076Spjd pjdlog_debug(2, 1493204076Spjd "remote_send: (%p) Unable to prepare header to send.", 1494204076Spjd hio); 1495204076Spjd reqlog(LOG_ERR, 0, ggio, 1496204076Spjd "Unable to prepare header to send (%s): ", 1497204076Spjd strerror(nv_error(nv))); 1498204076Spjd /* Move failed request immediately to the done queue. */ 1499204076Spjd goto done_queue; 1500204076Spjd } 1501204076Spjd /* 1502204076Spjd * Protect connection from disappearing. 1503204076Spjd */ 1504204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1505204076Spjd if (!ISCONNECTED(res, ncomp)) { 1506204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1507204076Spjd hio->hio_errors[ncomp] = ENOTCONN; 1508204076Spjd goto done_queue; 1509204076Spjd } 1510204076Spjd /* 1511204076Spjd * Move the request to recv queue before sending it, because 1512204076Spjd * in different order we can get reply before we move request 1513204076Spjd * to recv queue. 1514204076Spjd */ 1515226852Spjd pjdlog_debug(2, 1516226852Spjd "remote_send: (%p) Moving request to the recv queue.", 1517226852Spjd hio); 1518204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1519204076Spjd wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 1520204076Spjd TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 1521204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1522204076Spjd if (hast_proto_send(res, res->hr_remoteout, nv, data, 1523204076Spjd data != NULL ? length : 0) < 0) { 1524204076Spjd hio->hio_errors[ncomp] = errno; 1525204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1526204076Spjd pjdlog_debug(2, 1527204076Spjd "remote_send: (%p) Unable to send request.", hio); 1528204076Spjd reqlog(LOG_ERR, 0, ggio, 1529204076Spjd "Unable to send request (%s): ", 1530204076Spjd strerror(hio->hio_errors[ncomp])); 1531211979Spjd remote_close(res, ncomp); 1532204076Spjd /* 1533204076Spjd * Take request back from the receive queue and move 1534204076Spjd * it immediately to the done queue. 1535204076Spjd */ 1536204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1537226852Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1538226852Spjd hio_next[ncomp]); 1539204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1540204076Spjd goto done_queue; 1541204076Spjd } 1542204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1543204076Spjd nv_free(nv); 1544204076Spjd if (wakeup) 1545204076Spjd cv_signal(&hio_recv_list_cond[ncomp]); 1546204076Spjd continue; 1547204076Spjddone_queue: 1548204076Spjd nv_free(nv); 1549204076Spjd if (ISSYNCREQ(hio)) { 1550204076Spjd if (!refcount_release(&hio->hio_countdown)) 1551204076Spjd continue; 1552204076Spjd mtx_lock(&sync_lock); 1553204076Spjd SYNCREQDONE(hio); 1554204076Spjd mtx_unlock(&sync_lock); 1555204076Spjd cv_signal(&sync_cond); 1556204076Spjd continue; 1557204076Spjd } 1558204076Spjd if (ggio->gctl_cmd == BIO_WRITE) { 1559204076Spjd mtx_lock(&res->hr_amp_lock); 1560204076Spjd if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 1561204076Spjd ggio->gctl_length)) { 1562204076Spjd (void)hast_activemap_flush(res); 1563204076Spjd } 1564204076Spjd mtx_unlock(&res->hr_amp_lock); 1565204076Spjd } 1566204076Spjd if (!refcount_release(&hio->hio_countdown)) 1567204076Spjd continue; 1568204076Spjd pjdlog_debug(2, 1569204076Spjd "remote_send: (%p) Moving request to the done queue.", 1570204076Spjd hio); 1571204076Spjd QUEUE_INSERT2(hio, done); 1572204076Spjd } 1573204076Spjd /* NOTREACHED */ 1574204076Spjd return (NULL); 1575204076Spjd} 1576204076Spjd 1577204076Spjd/* 1578204076Spjd * Thread receives answer from secondary node and passes it to ggate_send 1579204076Spjd * thread. 1580204076Spjd */ 1581204076Spjdstatic void * 1582204076Spjdremote_recv_thread(void *arg) 1583204076Spjd{ 1584204076Spjd struct hast_resource *res = arg; 1585204076Spjd struct g_gate_ctl_io *ggio; 1586204076Spjd struct hio *hio; 1587204076Spjd struct nv *nv; 1588204076Spjd unsigned int ncomp; 1589204076Spjd uint64_t seq; 1590204076Spjd int error; 1591204076Spjd 1592204076Spjd /* Remote component is 1 for now. */ 1593204076Spjd ncomp = 1; 1594204076Spjd 1595204076Spjd for (;;) { 1596204076Spjd /* Wait until there is anything to receive. */ 1597204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1598204076Spjd while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 1599204076Spjd pjdlog_debug(2, "remote_recv: No requests, waiting."); 1600204076Spjd cv_wait(&hio_recv_list_cond[ncomp], 1601204076Spjd &hio_recv_list_lock[ncomp]); 1602204076Spjd } 1603204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1604226857Spjd 1605204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1606204076Spjd if (!ISCONNECTED(res, ncomp)) { 1607204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1608204076Spjd /* 1609204076Spjd * Connection is dead, so move all pending requests to 1610204076Spjd * the done queue (one-by-one). 1611204076Spjd */ 1612204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1613204076Spjd hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 1614218138Spjd PJDLOG_ASSERT(hio != NULL); 1615204076Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1616204076Spjd hio_next[ncomp]); 1617204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1618204076Spjd goto done_queue; 1619204076Spjd } 1620204076Spjd if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) { 1621204076Spjd pjdlog_errno(LOG_ERR, 1622204076Spjd "Unable to receive reply header"); 1623204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1624204076Spjd remote_close(res, ncomp); 1625204076Spjd continue; 1626204076Spjd } 1627204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1628204076Spjd seq = nv_get_uint64(nv, "seq"); 1629204076Spjd if (seq == 0) { 1630204076Spjd pjdlog_error("Header contains no 'seq' field."); 1631204076Spjd nv_free(nv); 1632204076Spjd continue; 1633204076Spjd } 1634204076Spjd mtx_lock(&hio_recv_list_lock[ncomp]); 1635204076Spjd TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 1636204076Spjd if (hio->hio_ggio.gctl_seq == seq) { 1637204076Spjd TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 1638204076Spjd hio_next[ncomp]); 1639204076Spjd break; 1640204076Spjd } 1641204076Spjd } 1642204076Spjd mtx_unlock(&hio_recv_list_lock[ncomp]); 1643204076Spjd if (hio == NULL) { 1644204076Spjd pjdlog_error("Found no request matching received 'seq' field (%ju).", 1645204076Spjd (uintmax_t)seq); 1646204076Spjd nv_free(nv); 1647204076Spjd continue; 1648204076Spjd } 1649226852Spjd ggio = &hio->hio_ggio; 1650204076Spjd error = nv_get_int16(nv, "error"); 1651204076Spjd if (error != 0) { 1652204076Spjd /* Request failed on remote side. */ 1653216478Spjd hio->hio_errors[ncomp] = error; 1654226852Spjd reqlog(LOG_WARNING, 0, ggio, 1655216479Spjd "Remote request failed (%s): ", strerror(error)); 1656204076Spjd nv_free(nv); 1657204076Spjd goto done_queue; 1658204076Spjd } 1659204076Spjd switch (ggio->gctl_cmd) { 1660204076Spjd case BIO_READ: 1661204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1662204076Spjd if (!ISCONNECTED(res, ncomp)) { 1663204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1664204076Spjd nv_free(nv); 1665204076Spjd goto done_queue; 1666204076Spjd } 1667204076Spjd if (hast_proto_recv_data(res, res->hr_remotein, nv, 1668204076Spjd ggio->gctl_data, ggio->gctl_length) < 0) { 1669204076Spjd hio->hio_errors[ncomp] = errno; 1670204076Spjd pjdlog_errno(LOG_ERR, 1671204076Spjd "Unable to receive reply data"); 1672204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1673204076Spjd nv_free(nv); 1674204076Spjd remote_close(res, ncomp); 1675204076Spjd goto done_queue; 1676204076Spjd } 1677204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1678204076Spjd break; 1679204076Spjd case BIO_WRITE: 1680204076Spjd case BIO_DELETE: 1681204076Spjd case BIO_FLUSH: 1682204076Spjd break; 1683204076Spjd default: 1684225783Spjd PJDLOG_ABORT("invalid condition"); 1685204076Spjd } 1686204076Spjd hio->hio_errors[ncomp] = 0; 1687204076Spjd nv_free(nv); 1688204076Spjddone_queue: 1689226856Spjd if (!refcount_release(&hio->hio_countdown)) 1690226856Spjd continue; 1691226856Spjd if (ISSYNCREQ(hio)) { 1692226856Spjd mtx_lock(&sync_lock); 1693226856Spjd SYNCREQDONE(hio); 1694226856Spjd mtx_unlock(&sync_lock); 1695226856Spjd cv_signal(&sync_cond); 1696226856Spjd } else { 1697226856Spjd pjdlog_debug(2, 1698226856Spjd "remote_recv: (%p) Moving request to the done queue.", 1699226856Spjd hio); 1700226856Spjd QUEUE_INSERT2(hio, done); 1701204076Spjd } 1702204076Spjd } 1703204076Spjd /* NOTREACHED */ 1704204076Spjd return (NULL); 1705204076Spjd} 1706204076Spjd 1707204076Spjd/* 1708204076Spjd * Thread sends answer to the kernel. 1709204076Spjd */ 1710204076Spjdstatic void * 1711204076Spjdggate_send_thread(void *arg) 1712204076Spjd{ 1713204076Spjd struct hast_resource *res = arg; 1714204076Spjd struct g_gate_ctl_io *ggio; 1715204076Spjd struct hio *hio; 1716226859Spjd unsigned int ii, ncomps; 1717204076Spjd 1718204076Spjd ncomps = HAST_NCOMPONENTS; 1719204076Spjd 1720204076Spjd for (;;) { 1721204076Spjd pjdlog_debug(2, "ggate_send: Taking request."); 1722204076Spjd QUEUE_TAKE2(hio, done); 1723204076Spjd pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 1724204076Spjd ggio = &hio->hio_ggio; 1725204076Spjd for (ii = 0; ii < ncomps; ii++) { 1726204076Spjd if (hio->hio_errors[ii] == 0) { 1727204076Spjd /* 1728204076Spjd * One successful request is enough to declare 1729204076Spjd * success. 1730204076Spjd */ 1731204076Spjd ggio->gctl_error = 0; 1732204076Spjd break; 1733204076Spjd } 1734204076Spjd } 1735204076Spjd if (ii == ncomps) { 1736204076Spjd /* 1737204076Spjd * None of the requests were successful. 1738219879Strociny * Use the error from local component except the 1739219879Strociny * case when we did only remote request. 1740204076Spjd */ 1741219879Strociny if (ggio->gctl_cmd == BIO_READ && 1742219879Strociny res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) 1743219879Strociny ggio->gctl_error = hio->hio_errors[1]; 1744219879Strociny else 1745219879Strociny ggio->gctl_error = hio->hio_errors[0]; 1746204076Spjd } 1747204076Spjd if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 1748204076Spjd mtx_lock(&res->hr_amp_lock); 1749223655Strociny if (activemap_write_complete(res->hr_amp, 1750223974Strociny ggio->gctl_offset, ggio->gctl_length)) { 1751223655Strociny res->hr_stat_activemap_update++; 1752223655Strociny (void)hast_activemap_flush(res); 1753223655Strociny } 1754204076Spjd mtx_unlock(&res->hr_amp_lock); 1755204076Spjd } 1756204076Spjd if (ggio->gctl_cmd == BIO_WRITE) { 1757204076Spjd /* 1758204076Spjd * Unlock range we locked. 1759204076Spjd */ 1760204076Spjd mtx_lock(&range_lock); 1761204076Spjd rangelock_del(range_regular, ggio->gctl_offset, 1762204076Spjd ggio->gctl_length); 1763204076Spjd if (range_sync_wait) 1764204076Spjd cv_signal(&range_sync_cond); 1765204076Spjd mtx_unlock(&range_lock); 1766226859Spjd if (!hio->hio_done) 1767226859Spjd write_complete(res, hio); 1768226859Spjd } else { 1769226859Spjd if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) { 1770226859Spjd primary_exit(EX_OSERR, 1771226859Spjd "G_GATE_CMD_DONE failed"); 1772204076Spjd } 1773204076Spjd } 1774204076Spjd pjdlog_debug(2, 1775204076Spjd "ggate_send: (%p) Moving request to the free queue.", hio); 1776204076Spjd QUEUE_INSERT2(hio, free); 1777204076Spjd } 1778204076Spjd /* NOTREACHED */ 1779204076Spjd return (NULL); 1780204076Spjd} 1781204076Spjd 1782204076Spjd/* 1783204076Spjd * Thread synchronize local and remote components. 1784204076Spjd */ 1785204076Spjdstatic void * 1786204076Spjdsync_thread(void *arg __unused) 1787204076Spjd{ 1788204076Spjd struct hast_resource *res = arg; 1789204076Spjd struct hio *hio; 1790204076Spjd struct g_gate_ctl_io *ggio; 1791219372Spjd struct timeval tstart, tend, tdiff; 1792204076Spjd unsigned int ii, ncomp, ncomps; 1793204076Spjd off_t offset, length, synced; 1794204076Spjd bool dorewind; 1795204076Spjd int syncext; 1796204076Spjd 1797204076Spjd ncomps = HAST_NCOMPONENTS; 1798204076Spjd dorewind = true; 1799211897Spjd synced = 0; 1800211897Spjd offset = -1; 1801204076Spjd 1802204076Spjd for (;;) { 1803204076Spjd mtx_lock(&sync_lock); 1804211897Spjd if (offset >= 0 && !sync_inprogress) { 1805219372Spjd gettimeofday(&tend, NULL); 1806219372Spjd timersub(&tend, &tstart, &tdiff); 1807219372Spjd pjdlog_info("Synchronization interrupted after %#.0T. " 1808219372Spjd "%NB synchronized so far.", &tdiff, 1809211879Spjd (intmax_t)synced); 1810212038Spjd event_send(res, EVENT_SYNCINTR); 1811211879Spjd } 1812204076Spjd while (!sync_inprogress) { 1813204076Spjd dorewind = true; 1814204076Spjd synced = 0; 1815204076Spjd cv_wait(&sync_cond, &sync_lock); 1816204076Spjd } 1817204076Spjd mtx_unlock(&sync_lock); 1818204076Spjd /* 1819204076Spjd * Obtain offset at which we should synchronize. 1820204076Spjd * Rewind synchronization if needed. 1821204076Spjd */ 1822204076Spjd mtx_lock(&res->hr_amp_lock); 1823204076Spjd if (dorewind) 1824204076Spjd activemap_sync_rewind(res->hr_amp); 1825204076Spjd offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 1826204076Spjd if (syncext != -1) { 1827204076Spjd /* 1828204076Spjd * We synchronized entire syncext extent, we can mark 1829204076Spjd * it as clean now. 1830204076Spjd */ 1831204076Spjd if (activemap_extent_complete(res->hr_amp, syncext)) 1832204076Spjd (void)hast_activemap_flush(res); 1833204076Spjd } 1834204076Spjd mtx_unlock(&res->hr_amp_lock); 1835204076Spjd if (dorewind) { 1836204076Spjd dorewind = false; 1837204076Spjd if (offset < 0) 1838204076Spjd pjdlog_info("Nodes are in sync."); 1839204076Spjd else { 1840219372Spjd pjdlog_info("Synchronization started. %NB to go.", 1841219372Spjd (intmax_t)(res->hr_extentsize * 1842204076Spjd activemap_ndirty(res->hr_amp))); 1843212038Spjd event_send(res, EVENT_SYNCSTART); 1844219372Spjd gettimeofday(&tstart, NULL); 1845204076Spjd } 1846204076Spjd } 1847204076Spjd if (offset < 0) { 1848211878Spjd sync_stop(); 1849204076Spjd pjdlog_debug(1, "Nothing to synchronize."); 1850204076Spjd /* 1851204076Spjd * Synchronization complete, make both localcnt and 1852204076Spjd * remotecnt equal. 1853204076Spjd */ 1854204076Spjd ncomp = 1; 1855204076Spjd rw_rlock(&hio_remote_lock[ncomp]); 1856204076Spjd if (ISCONNECTED(res, ncomp)) { 1857204076Spjd if (synced > 0) { 1858219372Spjd int64_t bps; 1859219372Spjd 1860219372Spjd gettimeofday(&tend, NULL); 1861219372Spjd timersub(&tend, &tstart, &tdiff); 1862219372Spjd bps = (int64_t)((double)synced / 1863219372Spjd ((double)tdiff.tv_sec + 1864219372Spjd (double)tdiff.tv_usec / 1000000)); 1865204076Spjd pjdlog_info("Synchronization complete. " 1866219372Spjd "%NB synchronized in %#.0lT (%NB/sec).", 1867219372Spjd (intmax_t)synced, &tdiff, 1868219372Spjd (intmax_t)bps); 1869212038Spjd event_send(res, EVENT_SYNCDONE); 1870204076Spjd } 1871204076Spjd mtx_lock(&metadata_lock); 1872204076Spjd res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 1873204076Spjd res->hr_primary_localcnt = 1874219882Strociny res->hr_secondary_remotecnt; 1875219882Strociny res->hr_primary_remotecnt = 1876204076Spjd res->hr_secondary_localcnt; 1877204076Spjd pjdlog_debug(1, 1878204076Spjd "Setting localcnt to %ju and remotecnt to %ju.", 1879204076Spjd (uintmax_t)res->hr_primary_localcnt, 1880219882Strociny (uintmax_t)res->hr_primary_remotecnt); 1881204076Spjd (void)metadata_write(res); 1882204076Spjd mtx_unlock(&metadata_lock); 1883204076Spjd } 1884204076Spjd rw_unlock(&hio_remote_lock[ncomp]); 1885204076Spjd continue; 1886204076Spjd } 1887204076Spjd pjdlog_debug(2, "sync: Taking free request."); 1888204076Spjd QUEUE_TAKE2(hio, free); 1889204076Spjd pjdlog_debug(2, "sync: (%p) Got free request.", hio); 1890204076Spjd /* 1891204076Spjd * Lock the range we are going to synchronize. We don't want 1892204076Spjd * race where someone writes between our read and write. 1893204076Spjd */ 1894204076Spjd for (;;) { 1895204076Spjd mtx_lock(&range_lock); 1896204076Spjd if (rangelock_islocked(range_regular, offset, length)) { 1897204076Spjd pjdlog_debug(2, 1898204076Spjd "sync: Range offset=%jd length=%jd locked.", 1899204076Spjd (intmax_t)offset, (intmax_t)length); 1900204076Spjd range_sync_wait = true; 1901204076Spjd cv_wait(&range_sync_cond, &range_lock); 1902204076Spjd range_sync_wait = false; 1903204076Spjd mtx_unlock(&range_lock); 1904204076Spjd continue; 1905204076Spjd } 1906204076Spjd if (rangelock_add(range_sync, offset, length) < 0) { 1907204076Spjd mtx_unlock(&range_lock); 1908204076Spjd pjdlog_debug(2, 1909204076Spjd "sync: Range offset=%jd length=%jd is already locked, waiting.", 1910204076Spjd (intmax_t)offset, (intmax_t)length); 1911204076Spjd sleep(1); 1912204076Spjd continue; 1913204076Spjd } 1914204076Spjd mtx_unlock(&range_lock); 1915204076Spjd break; 1916204076Spjd } 1917204076Spjd /* 1918204076Spjd * First read the data from synchronization source. 1919204076Spjd */ 1920204076Spjd SYNCREQ(hio); 1921204076Spjd ggio = &hio->hio_ggio; 1922204076Spjd ggio->gctl_cmd = BIO_READ; 1923204076Spjd ggio->gctl_offset = offset; 1924204076Spjd ggio->gctl_length = length; 1925204076Spjd ggio->gctl_error = 0; 1926226859Spjd hio->hio_done = false; 1927226859Spjd hio->hio_replication = res->hr_replication; 1928204076Spjd for (ii = 0; ii < ncomps; ii++) 1929204076Spjd hio->hio_errors[ii] = EINVAL; 1930204076Spjd reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 1931204076Spjd hio); 1932204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 1933204076Spjd hio); 1934204076Spjd mtx_lock(&metadata_lock); 1935204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1936204076Spjd /* 1937204076Spjd * This range is up-to-date on local component, 1938204076Spjd * so handle request locally. 1939204076Spjd */ 1940204076Spjd /* Local component is 0 for now. */ 1941204076Spjd ncomp = 0; 1942204076Spjd } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 1943218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 1944204076Spjd /* 1945204076Spjd * This range is out-of-date on local component, 1946204076Spjd * so send request to the remote node. 1947204076Spjd */ 1948204076Spjd /* Remote component is 1 for now. */ 1949204076Spjd ncomp = 1; 1950204076Spjd } 1951204076Spjd mtx_unlock(&metadata_lock); 1952204076Spjd refcount_init(&hio->hio_countdown, 1); 1953204076Spjd QUEUE_INSERT1(hio, send, ncomp); 1954204076Spjd 1955204076Spjd /* 1956204076Spjd * Let's wait for READ to finish. 1957204076Spjd */ 1958204076Spjd mtx_lock(&sync_lock); 1959204076Spjd while (!ISSYNCREQDONE(hio)) 1960204076Spjd cv_wait(&sync_cond, &sync_lock); 1961204076Spjd mtx_unlock(&sync_lock); 1962204076Spjd 1963204076Spjd if (hio->hio_errors[ncomp] != 0) { 1964204076Spjd pjdlog_error("Unable to read synchronization data: %s.", 1965204076Spjd strerror(hio->hio_errors[ncomp])); 1966204076Spjd goto free_queue; 1967204076Spjd } 1968204076Spjd 1969204076Spjd /* 1970204076Spjd * We read the data from synchronization source, now write it 1971204076Spjd * to synchronization target. 1972204076Spjd */ 1973204076Spjd SYNCREQ(hio); 1974204076Spjd ggio->gctl_cmd = BIO_WRITE; 1975204076Spjd for (ii = 0; ii < ncomps; ii++) 1976204076Spjd hio->hio_errors[ii] = EINVAL; 1977204076Spjd reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 1978204076Spjd hio); 1979204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 1980204076Spjd hio); 1981204076Spjd mtx_lock(&metadata_lock); 1982204076Spjd if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 1983204076Spjd /* 1984204076Spjd * This range is up-to-date on local component, 1985204076Spjd * so we update remote component. 1986204076Spjd */ 1987204076Spjd /* Remote component is 1 for now. */ 1988204076Spjd ncomp = 1; 1989204076Spjd } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 1990218138Spjd PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 1991204076Spjd /* 1992204076Spjd * This range is out-of-date on local component, 1993204076Spjd * so we update it. 1994204076Spjd */ 1995204076Spjd /* Local component is 0 for now. */ 1996204076Spjd ncomp = 0; 1997204076Spjd } 1998204076Spjd mtx_unlock(&metadata_lock); 1999204076Spjd 2000226857Spjd pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 2001204076Spjd hio); 2002204076Spjd refcount_init(&hio->hio_countdown, 1); 2003204076Spjd QUEUE_INSERT1(hio, send, ncomp); 2004204076Spjd 2005204076Spjd /* 2006204076Spjd * Let's wait for WRITE to finish. 2007204076Spjd */ 2008204076Spjd mtx_lock(&sync_lock); 2009204076Spjd while (!ISSYNCREQDONE(hio)) 2010204076Spjd cv_wait(&sync_cond, &sync_lock); 2011204076Spjd mtx_unlock(&sync_lock); 2012204076Spjd 2013204076Spjd if (hio->hio_errors[ncomp] != 0) { 2014204076Spjd pjdlog_error("Unable to write synchronization data: %s.", 2015204076Spjd strerror(hio->hio_errors[ncomp])); 2016204076Spjd goto free_queue; 2017204076Spjd } 2018211880Spjd 2019211880Spjd synced += length; 2020204076Spjdfree_queue: 2021204076Spjd mtx_lock(&range_lock); 2022204076Spjd rangelock_del(range_sync, offset, length); 2023204076Spjd if (range_regular_wait) 2024204076Spjd cv_signal(&range_regular_cond); 2025204076Spjd mtx_unlock(&range_lock); 2026204076Spjd pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 2027204076Spjd hio); 2028204076Spjd QUEUE_INSERT2(hio, free); 2029204076Spjd } 2030204076Spjd /* NOTREACHED */ 2031204076Spjd return (NULL); 2032204076Spjd} 2033204076Spjd 2034217784Spjdvoid 2035217784Spjdprimary_config_reload(struct hast_resource *res, struct nv *nv) 2036210886Spjd{ 2037210886Spjd unsigned int ii, ncomps; 2038217784Spjd int modified, vint; 2039217784Spjd const char *vstr; 2040210886Spjd 2041210886Spjd pjdlog_info("Reloading configuration..."); 2042210886Spjd 2043218138Spjd PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 2044218138Spjd PJDLOG_ASSERT(gres == res); 2045217784Spjd nv_assert(nv, "remoteaddr"); 2046219818Spjd nv_assert(nv, "sourceaddr"); 2047217784Spjd nv_assert(nv, "replication"); 2048219351Spjd nv_assert(nv, "checksum"); 2049219354Spjd nv_assert(nv, "compression"); 2050217784Spjd nv_assert(nv, "timeout"); 2051217784Spjd nv_assert(nv, "exec"); 2052225830Spjd nv_assert(nv, "metaflush"); 2053217784Spjd 2054210886Spjd ncomps = HAST_NCOMPONENTS; 2055210886Spjd 2056219351Spjd#define MODIFIED_REMOTEADDR 0x01 2057219818Spjd#define MODIFIED_SOURCEADDR 0x02 2058219818Spjd#define MODIFIED_REPLICATION 0x04 2059219818Spjd#define MODIFIED_CHECKSUM 0x08 2060219818Spjd#define MODIFIED_COMPRESSION 0x10 2061219818Spjd#define MODIFIED_TIMEOUT 0x20 2062219818Spjd#define MODIFIED_EXEC 0x40 2063225830Spjd#define MODIFIED_METAFLUSH 0x80 2064210886Spjd modified = 0; 2065217784Spjd 2066217784Spjd vstr = nv_get_string(nv, "remoteaddr"); 2067217784Spjd if (strcmp(gres->hr_remoteaddr, vstr) != 0) { 2068210886Spjd /* 2069210886Spjd * Don't copy res->hr_remoteaddr to gres just yet. 2070210886Spjd * We want remote_close() to log disconnect from the old 2071210886Spjd * addresses, not from the new ones. 2072210886Spjd */ 2073210886Spjd modified |= MODIFIED_REMOTEADDR; 2074210886Spjd } 2075219818Spjd vstr = nv_get_string(nv, "sourceaddr"); 2076219818Spjd if (strcmp(gres->hr_sourceaddr, vstr) != 0) { 2077219818Spjd strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr)); 2078219818Spjd modified |= MODIFIED_SOURCEADDR; 2079219818Spjd } 2080217784Spjd vint = nv_get_int32(nv, "replication"); 2081217784Spjd if (gres->hr_replication != vint) { 2082217784Spjd gres->hr_replication = vint; 2083210886Spjd modified |= MODIFIED_REPLICATION; 2084210886Spjd } 2085219351Spjd vint = nv_get_int32(nv, "checksum"); 2086219351Spjd if (gres->hr_checksum != vint) { 2087219351Spjd gres->hr_checksum = vint; 2088219351Spjd modified |= MODIFIED_CHECKSUM; 2089219351Spjd } 2090219354Spjd vint = nv_get_int32(nv, "compression"); 2091219354Spjd if (gres->hr_compression != vint) { 2092219354Spjd gres->hr_compression = vint; 2093219354Spjd modified |= MODIFIED_COMPRESSION; 2094219354Spjd } 2095217784Spjd vint = nv_get_int32(nv, "timeout"); 2096217784Spjd if (gres->hr_timeout != vint) { 2097217784Spjd gres->hr_timeout = vint; 2098210886Spjd modified |= MODIFIED_TIMEOUT; 2099210886Spjd } 2100217784Spjd vstr = nv_get_string(nv, "exec"); 2101217784Spjd if (strcmp(gres->hr_exec, vstr) != 0) { 2102217784Spjd strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec)); 2103211886Spjd modified |= MODIFIED_EXEC; 2104211886Spjd } 2105225830Spjd vint = nv_get_int32(nv, "metaflush"); 2106225830Spjd if (gres->hr_metaflush != vint) { 2107225830Spjd gres->hr_metaflush = vint; 2108225830Spjd modified |= MODIFIED_METAFLUSH; 2109225830Spjd } 2110217784Spjd 2111210886Spjd /* 2112219351Spjd * Change timeout for connected sockets. 2113219351Spjd * Don't bother if we need to reconnect. 2114210886Spjd */ 2115219351Spjd if ((modified & MODIFIED_TIMEOUT) != 0 && 2116226859Spjd (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) { 2117210886Spjd for (ii = 0; ii < ncomps; ii++) { 2118210886Spjd if (!ISREMOTE(ii)) 2119210886Spjd continue; 2120210886Spjd rw_rlock(&hio_remote_lock[ii]); 2121210886Spjd if (!ISCONNECTED(gres, ii)) { 2122210886Spjd rw_unlock(&hio_remote_lock[ii]); 2123210886Spjd continue; 2124210886Spjd } 2125210886Spjd rw_unlock(&hio_remote_lock[ii]); 2126210886Spjd if (proto_timeout(gres->hr_remotein, 2127210886Spjd gres->hr_timeout) < 0) { 2128210886Spjd pjdlog_errno(LOG_WARNING, 2129210886Spjd "Unable to set connection timeout"); 2130210886Spjd } 2131210886Spjd if (proto_timeout(gres->hr_remoteout, 2132210886Spjd gres->hr_timeout) < 0) { 2133210886Spjd pjdlog_errno(LOG_WARNING, 2134210886Spjd "Unable to set connection timeout"); 2135210886Spjd } 2136210886Spjd } 2137219351Spjd } 2138226859Spjd if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) { 2139210886Spjd for (ii = 0; ii < ncomps; ii++) { 2140210886Spjd if (!ISREMOTE(ii)) 2141210886Spjd continue; 2142210886Spjd remote_close(gres, ii); 2143210886Spjd } 2144210886Spjd if (modified & MODIFIED_REMOTEADDR) { 2145217784Spjd vstr = nv_get_string(nv, "remoteaddr"); 2146217784Spjd strlcpy(gres->hr_remoteaddr, vstr, 2147210886Spjd sizeof(gres->hr_remoteaddr)); 2148210886Spjd } 2149210886Spjd } 2150210886Spjd#undef MODIFIED_REMOTEADDR 2151219818Spjd#undef MODIFIED_SOURCEADDR 2152210886Spjd#undef MODIFIED_REPLICATION 2153219351Spjd#undef MODIFIED_CHECKSUM 2154219354Spjd#undef MODIFIED_COMPRESSION 2155210886Spjd#undef MODIFIED_TIMEOUT 2156211886Spjd#undef MODIFIED_EXEC 2157225830Spjd#undef MODIFIED_METAFLUSH 2158210886Spjd 2159210886Spjd pjdlog_info("Configuration reloaded successfully."); 2160210886Spjd} 2161210886Spjd 2162211882Spjdstatic void 2163211981Spjdguard_one(struct hast_resource *res, unsigned int ncomp) 2164211981Spjd{ 2165211981Spjd struct proto_conn *in, *out; 2166211981Spjd 2167211981Spjd if (!ISREMOTE(ncomp)) 2168211981Spjd return; 2169211981Spjd 2170211981Spjd rw_rlock(&hio_remote_lock[ncomp]); 2171211981Spjd 2172211981Spjd if (!real_remote(res)) { 2173211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2174211981Spjd return; 2175211981Spjd } 2176211981Spjd 2177211981Spjd if (ISCONNECTED(res, ncomp)) { 2178218138Spjd PJDLOG_ASSERT(res->hr_remotein != NULL); 2179218138Spjd PJDLOG_ASSERT(res->hr_remoteout != NULL); 2180211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2181211981Spjd pjdlog_debug(2, "remote_guard: Connection to %s is ok.", 2182211981Spjd res->hr_remoteaddr); 2183211981Spjd return; 2184211981Spjd } 2185211981Spjd 2186218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2187218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2188211981Spjd /* 2189211981Spjd * Upgrade the lock. It doesn't have to be atomic as no other thread 2190211981Spjd * can change connection status from disconnected to connected. 2191211981Spjd */ 2192211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2193211981Spjd pjdlog_debug(2, "remote_guard: Reconnecting to %s.", 2194211981Spjd res->hr_remoteaddr); 2195211981Spjd in = out = NULL; 2196220898Spjd if (init_remote(res, &in, &out) == 0) { 2197211981Spjd rw_wlock(&hio_remote_lock[ncomp]); 2198218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2199218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2200218138Spjd PJDLOG_ASSERT(in != NULL && out != NULL); 2201211981Spjd res->hr_remotein = in; 2202211981Spjd res->hr_remoteout = out; 2203211981Spjd rw_unlock(&hio_remote_lock[ncomp]); 2204211981Spjd pjdlog_info("Successfully reconnected to %s.", 2205211981Spjd res->hr_remoteaddr); 2206211981Spjd sync_start(); 2207211981Spjd } else { 2208211981Spjd /* Both connections should be NULL. */ 2209218138Spjd PJDLOG_ASSERT(res->hr_remotein == NULL); 2210218138Spjd PJDLOG_ASSERT(res->hr_remoteout == NULL); 2211218138Spjd PJDLOG_ASSERT(in == NULL && out == NULL); 2212211981Spjd pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", 2213211981Spjd res->hr_remoteaddr); 2214211981Spjd } 2215211981Spjd} 2216211981Spjd 2217204076Spjd/* 2218204076Spjd * Thread guards remote connections and reconnects when needed, handles 2219204076Spjd * signals, etc. 2220204076Spjd */ 2221204076Spjdstatic void * 2222204076Spjdguard_thread(void *arg) 2223204076Spjd{ 2224204076Spjd struct hast_resource *res = arg; 2225204076Spjd unsigned int ii, ncomps; 2226211982Spjd struct timespec timeout; 2227211981Spjd time_t lastcheck, now; 2228211982Spjd sigset_t mask; 2229211982Spjd int signo; 2230204076Spjd 2231204076Spjd ncomps = HAST_NCOMPONENTS; 2232211981Spjd lastcheck = time(NULL); 2233204076Spjd 2234211982Spjd PJDLOG_VERIFY(sigemptyset(&mask) == 0); 2235211982Spjd PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 2236211982Spjd PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 2237211982Spjd 2238219721Strociny timeout.tv_sec = HAST_KEEPALIVE; 2239211982Spjd timeout.tv_nsec = 0; 2240211982Spjd signo = -1; 2241211982Spjd 2242204076Spjd for (;;) { 2243211982Spjd switch (signo) { 2244211982Spjd case SIGINT: 2245211982Spjd case SIGTERM: 2246211982Spjd sigexit_received = true; 2247204076Spjd primary_exitx(EX_OK, 2248204076Spjd "Termination signal received, exiting."); 2249211982Spjd break; 2250211982Spjd default: 2251211982Spjd break; 2252204076Spjd } 2253211882Spjd 2254220898Spjd /* 2255220898Spjd * Don't check connections until we fully started, 2256220898Spjd * as we may still be looping, waiting for remote node 2257220898Spjd * to switch from primary to secondary. 2258220898Spjd */ 2259220898Spjd if (fullystarted) { 2260220898Spjd pjdlog_debug(2, "remote_guard: Checking connections."); 2261220898Spjd now = time(NULL); 2262220898Spjd if (lastcheck + HAST_KEEPALIVE <= now) { 2263220898Spjd for (ii = 0; ii < ncomps; ii++) 2264220898Spjd guard_one(res, ii); 2265220898Spjd lastcheck = now; 2266220898Spjd } 2267204076Spjd } 2268211982Spjd signo = sigtimedwait(&mask, NULL, &timeout); 2269204076Spjd } 2270204076Spjd /* NOTREACHED */ 2271204076Spjd return (NULL); 2272204076Spjd} 2273