secondary.c revision 209185
1204076Spjd/*- 2204076Spjd * Copyright (c) 2009-2010 The FreeBSD Foundation 3204076Spjd * All rights reserved. 4204076Spjd * 5204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from 6204076Spjd * the FreeBSD Foundation. 7204076Spjd * 8204076Spjd * Redistribution and use in source and binary forms, with or without 9204076Spjd * modification, are permitted provided that the following conditions 10204076Spjd * are met: 11204076Spjd * 1. Redistributions of source code must retain the above copyright 12204076Spjd * notice, this list of conditions and the following disclaimer. 13204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 14204076Spjd * notice, this list of conditions and the following disclaimer in the 15204076Spjd * documentation and/or other materials provided with the distribution. 16204076Spjd * 17204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20204076Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27204076Spjd * SUCH DAMAGE. 28204076Spjd */ 29204076Spjd 30204076Spjd#include <sys/cdefs.h> 31204076Spjd__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 209185 2010-06-14 21:46:48Z pjd $"); 32204076Spjd 33204076Spjd#include <sys/param.h> 34204076Spjd#include <sys/time.h> 35204076Spjd#include <sys/bio.h> 36204076Spjd#include <sys/disk.h> 37204076Spjd#include <sys/stat.h> 38204076Spjd 39204076Spjd#include <assert.h> 40204076Spjd#include <err.h> 41204076Spjd#include <errno.h> 42204076Spjd#include <fcntl.h> 43204076Spjd#include <libgeom.h> 44204076Spjd#include <pthread.h> 45204076Spjd#include <stdint.h> 46204076Spjd#include <stdio.h> 47204076Spjd#include <string.h> 48204076Spjd#include <sysexits.h> 49204076Spjd#include <unistd.h> 50204076Spjd 51204076Spjd#include <activemap.h> 52204076Spjd#include <nv.h> 53204076Spjd#include <pjdlog.h> 54204076Spjd 55204076Spjd#include "control.h" 56204076Spjd#include "hast.h" 57204076Spjd#include "hast_proto.h" 58204076Spjd#include "hastd.h" 59204076Spjd#include "metadata.h" 60204076Spjd#include "proto.h" 61204076Spjd#include "subr.h" 62204076Spjd#include "synch.h" 63204076Spjd 64204076Spjdstruct hio { 65204076Spjd uint64_t hio_seq; 66204076Spjd int hio_error; 67204076Spjd struct nv *hio_nv; 68204076Spjd void *hio_data; 69204076Spjd uint8_t hio_cmd; 70204076Spjd uint64_t hio_offset; 71204076Spjd uint64_t hio_length; 72204076Spjd TAILQ_ENTRY(hio) hio_next; 73204076Spjd}; 74204076Spjd 75204076Spjd/* 76204076Spjd * Free list holds unused structures. When free list is empty, we have to wait 77204076Spjd * until some in-progress requests are freed. 78204076Spjd */ 79204076Spjdstatic TAILQ_HEAD(, hio) hio_free_list; 80204076Spjdstatic pthread_mutex_t hio_free_list_lock; 81204076Spjdstatic pthread_cond_t hio_free_list_cond; 82204076Spjd/* 83204076Spjd * Disk thread (the one that do I/O requests) takes requests from this list. 84204076Spjd */ 85204076Spjdstatic TAILQ_HEAD(, hio) hio_disk_list; 86204076Spjdstatic pthread_mutex_t hio_disk_list_lock; 87204076Spjdstatic pthread_cond_t hio_disk_list_cond; 88204076Spjd/* 89204076Spjd * There is one recv list for every component, although local components don't 90204076Spjd * use recv lists as local requests are done synchronously. 91204076Spjd */ 92204076Spjdstatic TAILQ_HEAD(, hio) hio_send_list; 93204076Spjdstatic pthread_mutex_t hio_send_list_lock; 94204076Spjdstatic pthread_cond_t hio_send_list_cond; 95204076Spjd 96204076Spjd/* 97204076Spjd * Maximum number of outstanding I/O requests. 98204076Spjd */ 99204076Spjd#define HAST_HIO_MAX 256 100204076Spjd 101204076Spjdstatic void *recv_thread(void *arg); 102204076Spjdstatic void *disk_thread(void *arg); 103204076Spjdstatic void *send_thread(void *arg); 104204076Spjd 105204076Spjdstatic void 106204076Spjdinit_environment(void) 107204076Spjd{ 108204076Spjd struct hio *hio; 109204076Spjd unsigned int ii; 110204076Spjd 111204076Spjd /* 112204076Spjd * Initialize lists, their locks and theirs condition variables. 113204076Spjd */ 114204076Spjd TAILQ_INIT(&hio_free_list); 115204076Spjd mtx_init(&hio_free_list_lock); 116204076Spjd cv_init(&hio_free_list_cond); 117204076Spjd TAILQ_INIT(&hio_disk_list); 118204076Spjd mtx_init(&hio_disk_list_lock); 119204076Spjd cv_init(&hio_disk_list_cond); 120204076Spjd TAILQ_INIT(&hio_send_list); 121204076Spjd mtx_init(&hio_send_list_lock); 122204076Spjd cv_init(&hio_send_list_cond); 123204076Spjd 124204076Spjd /* 125204076Spjd * Allocate requests pool and initialize requests. 126204076Spjd */ 127204076Spjd for (ii = 0; ii < HAST_HIO_MAX; ii++) { 128204076Spjd hio = malloc(sizeof(*hio)); 129204076Spjd if (hio == NULL) { 130204076Spjd errx(EX_TEMPFAIL, "cannot allocate %zu bytes of memory " 131204076Spjd "for hio request", sizeof(*hio)); 132204076Spjd } 133204076Spjd hio->hio_error = 0; 134204076Spjd hio->hio_data = malloc(MAXPHYS); 135204076Spjd if (hio->hio_data == NULL) { 136204076Spjd errx(EX_TEMPFAIL, "cannot allocate %zu bytes of memory " 137204076Spjd "for gctl_data", (size_t)MAXPHYS); 138204076Spjd } 139204076Spjd TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next); 140204076Spjd } 141204076Spjd} 142204076Spjd 143204076Spjdstatic void 144204076Spjdinit_local(struct hast_resource *res) 145204076Spjd{ 146204076Spjd 147204076Spjd if (metadata_read(res, true) < 0) 148204076Spjd exit(EX_NOINPUT); 149204076Spjd} 150204076Spjd 151204076Spjdstatic void 152204076Spjdinit_remote(struct hast_resource *res, struct nv *nvin) 153204076Spjd{ 154204076Spjd uint64_t resuid; 155204076Spjd struct nv *nvout; 156204076Spjd unsigned char *map; 157204076Spjd size_t mapsize; 158204076Spjd 159204076Spjd map = NULL; 160204076Spjd mapsize = 0; 161204076Spjd nvout = nv_alloc(); 162204076Spjd nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize"); 163204076Spjd nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize"); 164204076Spjd resuid = nv_get_uint64(nvin, "resuid"); 165204076Spjd res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt"); 166204076Spjd res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 167204076Spjd nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt"); 168204076Spjd nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt"); 169204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 170204076Spjd METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize); 171204076Spjd map = malloc(mapsize); 172204076Spjd if (map == NULL) { 173204076Spjd pjdlog_exitx(EX_TEMPFAIL, 174204076Spjd "Unable to allocate memory (%zu bytes) for activemap.", 175204076Spjd mapsize); 176204076Spjd } 177204076Spjd nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize"); 178204076Spjd /* 179204076Spjd * When we work as primary and secondary is missing we will increase 180204076Spjd * localcnt in our metadata. When secondary is connected and synced 181204076Spjd * we make localcnt be equal to remotecnt, which means nodes are more 182204076Spjd * or less in sync. 183204076Spjd * Split-brain condition is when both nodes are not able to communicate 184204076Spjd * and are both configured as primary nodes. In turn, they can both 185204076Spjd * make incompatible changes to the data and we have to detect that. 186204076Spjd * Under split-brain condition we will increase our localcnt on first 187204076Spjd * write and remote node will increase its localcnt on first write. 188204076Spjd * When we connect we can see that primary's localcnt is greater than 189204076Spjd * our remotecnt (primary was modified while we weren't watching) and 190204076Spjd * our localcnt is greater than primary's remotecnt (we were modified 191204076Spjd * while primary wasn't watching). 192204076Spjd * There are many possible combinations which are all gathered below. 193204076Spjd * Don't pay too much attention to exact numbers, the more important 194204076Spjd * is to compare them. We compare secondary's local with primary's 195204076Spjd * remote and secondary's remote with primary's local. 196204076Spjd * Note that every case where primary's localcnt is smaller than 197204076Spjd * secondary's remotecnt and where secondary's localcnt is smaller than 198204076Spjd * primary's remotecnt should be impossible in practise. We will perform 199204076Spjd * full synchronization then. Those cases are marked with an asterisk. 200204076Spjd * Regular synchronization means that only extents marked as dirty are 201204076Spjd * synchronized (regular synchronization). 202204076Spjd * 203204076Spjd * SECONDARY METADATA PRIMARY METADATA 204204076Spjd * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary. 205204076Spjd * local=3 remote=3 local=2 remote=3* ?! Full sync from primary. 206204076Spjd * local=3 remote=3 local=2 remote=4* ?! Full sync from primary. 207204076Spjd * local=3 remote=3 local=3 remote=2 Primary is out-of-date, 208204076Spjd * regular sync from secondary. 209204076Spjd * local=3 remote=3 local=3 remote=3 Regular sync just in case. 210204076Spjd * local=3 remote=3 local=3 remote=4* ?! Full sync from primary. 211204076Spjd * local=3 remote=3 local=4 remote=2 Split-brain condition. 212204076Spjd * local=3 remote=3 local=4 remote=3 Secondary out-of-date, 213204076Spjd * regular sync from primary. 214204076Spjd * local=3 remote=3 local=4 remote=4* ?! Full sync from primary. 215204076Spjd */ 216204076Spjd if (res->hr_resuid == 0) { 217204076Spjd /* 218204076Spjd * Provider is used for the first time. Initialize everything. 219204076Spjd */ 220204076Spjd assert(res->hr_secondary_localcnt == 0); 221204076Spjd res->hr_resuid = resuid; 222204076Spjd if (metadata_write(res) < 0) 223204076Spjd exit(EX_NOINPUT); 224204076Spjd memset(map, 0xff, mapsize); 225204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 226204076Spjd } else if ( 227204076Spjd /* Is primary is out-of-date? */ 228204076Spjd (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 229204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 230204076Spjd /* Node are more or less in sync? */ 231204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 232204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 233204076Spjd /* Is secondary is out-of-date? */ 234204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 235204076Spjd res->hr_secondary_remotecnt < res->hr_primary_localcnt)) { 236204076Spjd /* 237204076Spjd * Nodes are more or less in sync or one of the nodes is 238204076Spjd * out-of-date. 239204076Spjd * It doesn't matter at this point which one, we just have to 240204076Spjd * send out local bitmap to the remote node. 241204076Spjd */ 242204076Spjd if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) != 243204076Spjd (ssize_t)mapsize) { 244204076Spjd pjdlog_exit(LOG_ERR, "Unable to read activemap"); 245204076Spjd } 246204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 247204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) { 248204076Spjd /* Primary is out-of-date, sync from secondary. */ 249204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 250204076Spjd } else { 251204076Spjd /* 252204076Spjd * Secondary is out-of-date or counts match. 253204076Spjd * Sync from primary. 254204076Spjd */ 255204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 256204076Spjd } 257204076Spjd } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 258204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 259204076Spjd /* 260204076Spjd * Not good, we have split-brain condition. 261204076Spjd */ 262204076Spjd pjdlog_error("Split-brain detected, exiting."); 263204076Spjd nv_add_string(nvout, "Split-brain condition!", "errmsg"); 264204076Spjd free(map); 265204076Spjd map = NULL; 266204076Spjd mapsize = 0; 267204076Spjd } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt || 268204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ { 269204076Spjd /* 270204076Spjd * This should never happen in practise, but we will perform 271204076Spjd * full synchronization. 272204076Spjd */ 273204076Spjd assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt || 274204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt); 275204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 276204076Spjd METADATA_SIZE, res->hr_extentsize, 277204076Spjd res->hr_local_sectorsize); 278204076Spjd memset(map, 0xff, mapsize); 279204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) { 280204076Spjd /* In this one of five cases sync from secondary. */ 281204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 282204076Spjd } else { 283204076Spjd /* For the rest four cases sync from primary. */ 284204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 285204076Spjd } 286204076Spjd pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).", 287204076Spjd (uintmax_t)res->hr_primary_localcnt, 288204076Spjd (uintmax_t)res->hr_primary_remotecnt, 289204076Spjd (uintmax_t)res->hr_secondary_localcnt, 290204076Spjd (uintmax_t)res->hr_secondary_remotecnt); 291204076Spjd } 292204076Spjd if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) { 293204076Spjd pjdlog_errno(LOG_WARNING, "Unable to send activemap to %s", 294204076Spjd res->hr_remoteaddr); 295204076Spjd nv_free(nvout); 296204076Spjd exit(EX_TEMPFAIL); 297204076Spjd } 298209182Spjd nv_free(nvout); 299204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 300204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 301204076Spjd /* Exit on split-brain. */ 302204076Spjd exit(EX_CONFIG); 303204076Spjd } 304204076Spjd} 305204076Spjd 306204076Spjdvoid 307204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin) 308204076Spjd{ 309204076Spjd pthread_t td; 310204076Spjd pid_t pid; 311204076Spjd int error; 312204076Spjd 313204076Spjd /* 314204076Spjd * Create communication channel between parent and child. 315204076Spjd */ 316204076Spjd if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 317204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 318204076Spjd pjdlog_exit(EX_OSERR, 319204076Spjd "Unable to create control sockets between parent and child"); 320204076Spjd } 321204076Spjd 322204076Spjd pid = fork(); 323204076Spjd if (pid < 0) { 324204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 325204076Spjd pjdlog_exit(EX_OSERR, "Unable to fork"); 326204076Spjd } 327204076Spjd 328204076Spjd if (pid > 0) { 329204076Spjd /* This is parent. */ 330204076Spjd proto_close(res->hr_remotein); 331204076Spjd res->hr_remotein = NULL; 332204076Spjd proto_close(res->hr_remoteout); 333204076Spjd res->hr_remoteout = NULL; 334204076Spjd res->hr_workerpid = pid; 335204076Spjd return; 336204076Spjd } 337204076Spjd (void)pidfile_close(pfh); 338204076Spjd 339204076Spjd setproctitle("%s (secondary)", res->hr_name); 340204076Spjd 341207371Spjd /* Error in setting timeout is not critical, but why should it fail? */ 342207371Spjd if (proto_timeout(res->hr_remotein, 0) < 0) 343207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 344207371Spjd if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0) 345207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 346207371Spjd 347204076Spjd init_local(res); 348204076Spjd init_remote(res, nvin); 349204076Spjd init_environment(); 350204076Spjd 351204076Spjd error = pthread_create(&td, NULL, recv_thread, res); 352204076Spjd assert(error == 0); 353204076Spjd error = pthread_create(&td, NULL, disk_thread, res); 354204076Spjd assert(error == 0); 355204076Spjd error = pthread_create(&td, NULL, send_thread, res); 356204076Spjd assert(error == 0); 357204076Spjd (void)ctrl_thread(res); 358204076Spjd} 359204076Spjd 360204076Spjdstatic void 361204076Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...) 362204076Spjd{ 363204076Spjd char msg[1024]; 364204076Spjd va_list ap; 365204076Spjd int len; 366204076Spjd 367204076Spjd va_start(ap, fmt); 368204076Spjd len = vsnprintf(msg, sizeof(msg), fmt, ap); 369204076Spjd va_end(ap); 370204076Spjd if ((size_t)len < sizeof(msg)) { 371204076Spjd switch (hio->hio_cmd) { 372204076Spjd case HIO_READ: 373204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 374204076Spjd "READ(%ju, %ju).", (uintmax_t)hio->hio_offset, 375204076Spjd (uintmax_t)hio->hio_length); 376204076Spjd break; 377204076Spjd case HIO_DELETE: 378204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 379204076Spjd "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset, 380204076Spjd (uintmax_t)hio->hio_length); 381204076Spjd break; 382204076Spjd case HIO_FLUSH: 383204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 384204076Spjd break; 385204076Spjd case HIO_WRITE: 386204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 387204076Spjd "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset, 388204076Spjd (uintmax_t)hio->hio_length); 389204076Spjd break; 390204076Spjd default: 391204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 392204076Spjd "UNKNOWN(%u).", (unsigned int)hio->hio_cmd); 393204076Spjd break; 394204076Spjd } 395204076Spjd } 396204076Spjd pjdlog_common(loglevel, debuglevel, error, "%s", msg); 397204076Spjd} 398204076Spjd 399204076Spjdstatic int 400204076Spjdrequnpack(struct hast_resource *res, struct hio *hio) 401204076Spjd{ 402204076Spjd 403204076Spjd hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd"); 404204076Spjd if (hio->hio_cmd == 0) { 405204076Spjd pjdlog_error("Header contains no 'cmd' field."); 406204076Spjd hio->hio_error = EINVAL; 407204076Spjd goto end; 408204076Spjd } 409204076Spjd switch (hio->hio_cmd) { 410204076Spjd case HIO_READ: 411204076Spjd case HIO_WRITE: 412204076Spjd case HIO_DELETE: 413204076Spjd hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset"); 414204076Spjd if (nv_error(hio->hio_nv) != 0) { 415204076Spjd pjdlog_error("Header is missing 'offset' field."); 416204076Spjd hio->hio_error = EINVAL; 417204076Spjd goto end; 418204076Spjd } 419204076Spjd hio->hio_length = nv_get_uint64(hio->hio_nv, "length"); 420204076Spjd if (nv_error(hio->hio_nv) != 0) { 421204076Spjd pjdlog_error("Header is missing 'length' field."); 422204076Spjd hio->hio_error = EINVAL; 423204076Spjd goto end; 424204076Spjd } 425204076Spjd if (hio->hio_length == 0) { 426204076Spjd pjdlog_error("Data length is zero."); 427204076Spjd hio->hio_error = EINVAL; 428204076Spjd goto end; 429204076Spjd } 430204076Spjd if (hio->hio_length > MAXPHYS) { 431204076Spjd pjdlog_error("Data length is too large (%ju > %ju).", 432204076Spjd (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS); 433204076Spjd hio->hio_error = EINVAL; 434204076Spjd goto end; 435204076Spjd } 436204076Spjd if ((hio->hio_offset % res->hr_local_sectorsize) != 0) { 437204076Spjd pjdlog_error("Offset %ju is not multiple of sector size.", 438204076Spjd (uintmax_t)hio->hio_offset); 439204076Spjd hio->hio_error = EINVAL; 440204076Spjd goto end; 441204076Spjd } 442204076Spjd if ((hio->hio_length % res->hr_local_sectorsize) != 0) { 443204076Spjd pjdlog_error("Length %ju is not multiple of sector size.", 444204076Spjd (uintmax_t)hio->hio_length); 445204076Spjd hio->hio_error = EINVAL; 446204076Spjd goto end; 447204076Spjd } 448204076Spjd if (hio->hio_offset + hio->hio_length > 449204076Spjd (uint64_t)res->hr_datasize) { 450204076Spjd pjdlog_error("Data offset is too large (%ju > %ju).", 451204076Spjd (uintmax_t)(hio->hio_offset + hio->hio_length), 452204076Spjd (uintmax_t)res->hr_datasize); 453204076Spjd hio->hio_error = EINVAL; 454204076Spjd goto end; 455204076Spjd } 456204076Spjd break; 457204076Spjd default: 458204076Spjd pjdlog_error("Header contains invalid 'cmd' (%hhu).", 459204076Spjd hio->hio_cmd); 460204076Spjd hio->hio_error = EINVAL; 461204076Spjd goto end; 462204076Spjd } 463204076Spjd hio->hio_error = 0; 464204076Spjdend: 465204076Spjd return (hio->hio_error); 466204076Spjd} 467204076Spjd 468204076Spjd/* 469204076Spjd * Thread receives requests from the primary node. 470204076Spjd */ 471204076Spjdstatic void * 472204076Spjdrecv_thread(void *arg) 473204076Spjd{ 474204076Spjd struct hast_resource *res = arg; 475204076Spjd struct hio *hio; 476204076Spjd bool wakeup; 477204076Spjd 478204076Spjd for (;;) { 479204076Spjd pjdlog_debug(2, "recv: Taking free request."); 480204076Spjd mtx_lock(&hio_free_list_lock); 481204076Spjd while ((hio = TAILQ_FIRST(&hio_free_list)) == NULL) { 482204076Spjd pjdlog_debug(2, "recv: No free requests, waiting."); 483204076Spjd cv_wait(&hio_free_list_cond, &hio_free_list_lock); 484204076Spjd } 485204076Spjd TAILQ_REMOVE(&hio_free_list, hio, hio_next); 486204076Spjd mtx_unlock(&hio_free_list_lock); 487204076Spjd pjdlog_debug(2, "recv: (%p) Got request.", hio); 488204076Spjd if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) { 489204076Spjd pjdlog_exit(EX_TEMPFAIL, 490204076Spjd "Unable to receive request header"); 491204076Spjd } 492204076Spjd if (requnpack(res, hio) != 0) 493204076Spjd goto send_queue; 494204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, 495204076Spjd "recv: (%p) Got request header: ", hio); 496204076Spjd if (hio->hio_cmd == HIO_WRITE) { 497204076Spjd if (hast_proto_recv_data(res, res->hr_remotein, 498204076Spjd hio->hio_nv, hio->hio_data, MAXPHYS) < 0) { 499204076Spjd pjdlog_exit(EX_TEMPFAIL, 500204076Spjd "Unable to receive reply data"); 501204076Spjd } 502204076Spjd } 503204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.", 504204076Spjd hio); 505204076Spjd mtx_lock(&hio_disk_list_lock); 506204076Spjd wakeup = TAILQ_EMPTY(&hio_disk_list); 507204076Spjd TAILQ_INSERT_TAIL(&hio_disk_list, hio, hio_next); 508204076Spjd mtx_unlock(&hio_disk_list_lock); 509204076Spjd if (wakeup) 510204076Spjd cv_signal(&hio_disk_list_cond); 511204076Spjd continue; 512204076Spjdsend_queue: 513204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the send queue.", 514204076Spjd hio); 515204076Spjd mtx_lock(&hio_send_list_lock); 516204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 517204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 518204076Spjd mtx_unlock(&hio_send_list_lock); 519204076Spjd if (wakeup) 520204076Spjd cv_signal(&hio_send_list_cond); 521204076Spjd } 522204076Spjd /* NOTREACHED */ 523204076Spjd return (NULL); 524204076Spjd} 525204076Spjd 526204076Spjd/* 527204076Spjd * Thread reads from or writes to local component and also handles DELETE and 528204076Spjd * FLUSH requests. 529204076Spjd */ 530204076Spjdstatic void * 531204076Spjddisk_thread(void *arg) 532204076Spjd{ 533204076Spjd struct hast_resource *res = arg; 534204076Spjd struct hio *hio; 535204076Spjd ssize_t ret; 536204076Spjd bool clear_activemap, wakeup; 537204076Spjd 538204076Spjd clear_activemap = true; 539204076Spjd 540204076Spjd for (;;) { 541204076Spjd pjdlog_debug(2, "disk: Taking request."); 542204076Spjd mtx_lock(&hio_disk_list_lock); 543204076Spjd while ((hio = TAILQ_FIRST(&hio_disk_list)) == NULL) { 544204076Spjd pjdlog_debug(2, "disk: No requests, waiting."); 545204076Spjd cv_wait(&hio_disk_list_cond, &hio_disk_list_lock); 546204076Spjd } 547204076Spjd TAILQ_REMOVE(&hio_disk_list, hio, hio_next); 548204076Spjd mtx_unlock(&hio_disk_list_lock); 549204076Spjd while (clear_activemap) { 550204076Spjd unsigned char *map; 551204076Spjd size_t mapsize; 552204076Spjd 553204076Spjd /* 554204076Spjd * When first request is received, it means that primary 555204076Spjd * already received our activemap, merged it and stored 556204076Spjd * locally. We can now safely clear our activemap. 557204076Spjd */ 558204076Spjd mapsize = 559204076Spjd activemap_calc_ondisk_size(res->hr_local_mediasize - 560204076Spjd METADATA_SIZE, res->hr_extentsize, 561204076Spjd res->hr_local_sectorsize); 562204076Spjd map = calloc(1, mapsize); 563204076Spjd if (map == NULL) { 564204076Spjd pjdlog_warning("Unable to allocate memory to clear local activemap."); 565204076Spjd break; 566204076Spjd } 567204076Spjd if (pwrite(res->hr_localfd, map, mapsize, 568204076Spjd METADATA_SIZE) != (ssize_t)mapsize) { 569204076Spjd pjdlog_errno(LOG_WARNING, 570204076Spjd "Unable to store cleared activemap"); 571204076Spjd free(map); 572204076Spjd break; 573204076Spjd } 574204076Spjd free(map); 575204076Spjd clear_activemap = false; 576204076Spjd pjdlog_debug(1, "Local activemap cleared."); 577204076Spjd } 578204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio); 579204076Spjd /* Handle the actual request. */ 580204076Spjd switch (hio->hio_cmd) { 581204076Spjd case HIO_READ: 582204076Spjd ret = pread(res->hr_localfd, hio->hio_data, 583204076Spjd hio->hio_length, 584204076Spjd hio->hio_offset + res->hr_localoff); 585204076Spjd if (ret < 0) 586204076Spjd hio->hio_error = errno; 587204076Spjd else if (ret != (int64_t)hio->hio_length) 588204076Spjd hio->hio_error = EIO; 589204076Spjd else 590204076Spjd hio->hio_error = 0; 591204076Spjd break; 592204076Spjd case HIO_WRITE: 593204076Spjd ret = pwrite(res->hr_localfd, hio->hio_data, 594204076Spjd hio->hio_length, 595204076Spjd hio->hio_offset + res->hr_localoff); 596204076Spjd if (ret < 0) 597204076Spjd hio->hio_error = errno; 598204076Spjd else if (ret != (int64_t)hio->hio_length) 599204076Spjd hio->hio_error = EIO; 600204076Spjd else 601204076Spjd hio->hio_error = 0; 602204076Spjd break; 603204076Spjd case HIO_DELETE: 604204076Spjd ret = g_delete(res->hr_localfd, 605204076Spjd hio->hio_offset + res->hr_localoff, 606204076Spjd hio->hio_length); 607204076Spjd if (ret < 0) 608204076Spjd hio->hio_error = errno; 609204076Spjd else 610204076Spjd hio->hio_error = 0; 611204076Spjd break; 612204076Spjd case HIO_FLUSH: 613204076Spjd ret = g_flush(res->hr_localfd); 614204076Spjd if (ret < 0) 615204076Spjd hio->hio_error = errno; 616204076Spjd else 617204076Spjd hio->hio_error = 0; 618204076Spjd break; 619204076Spjd } 620204076Spjd if (hio->hio_error != 0) { 621204076Spjd reqlog(LOG_ERR, 0, hio->hio_error, hio, 622204076Spjd "Request failed: "); 623204076Spjd } 624204076Spjd pjdlog_debug(2, "disk: (%p) Moving request to the send queue.", 625204076Spjd hio); 626204076Spjd mtx_lock(&hio_send_list_lock); 627204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 628204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 629204076Spjd mtx_unlock(&hio_send_list_lock); 630204076Spjd if (wakeup) 631204076Spjd cv_signal(&hio_send_list_cond); 632204076Spjd } 633204076Spjd /* NOTREACHED */ 634204076Spjd return (NULL); 635204076Spjd} 636204076Spjd 637204076Spjd/* 638204076Spjd * Thread sends requests back to primary node. 639204076Spjd */ 640204076Spjdstatic void * 641204076Spjdsend_thread(void *arg) 642204076Spjd{ 643204076Spjd struct hast_resource *res = arg; 644204076Spjd struct nv *nvout; 645204076Spjd struct hio *hio; 646204076Spjd void *data; 647204076Spjd size_t length; 648204076Spjd bool wakeup; 649204076Spjd 650204076Spjd for (;;) { 651204076Spjd pjdlog_debug(2, "send: Taking request."); 652204076Spjd mtx_lock(&hio_send_list_lock); 653204076Spjd while ((hio = TAILQ_FIRST(&hio_send_list)) == NULL) { 654204076Spjd pjdlog_debug(2, "send: No requests, waiting."); 655204076Spjd cv_wait(&hio_send_list_cond, &hio_send_list_lock); 656204076Spjd } 657204076Spjd TAILQ_REMOVE(&hio_send_list, hio, hio_next); 658204076Spjd mtx_unlock(&hio_send_list_lock); 659204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio); 660204076Spjd nvout = nv_alloc(); 661204076Spjd /* Copy sequence number. */ 662204076Spjd nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq"); 663204076Spjd switch (hio->hio_cmd) { 664204076Spjd case HIO_READ: 665204076Spjd if (hio->hio_error == 0) { 666204076Spjd data = hio->hio_data; 667204076Spjd length = hio->hio_length; 668204076Spjd break; 669204076Spjd } 670204076Spjd /* 671204076Spjd * We send no data in case of an error. 672204076Spjd */ 673204076Spjd /* FALLTHROUGH */ 674204076Spjd case HIO_DELETE: 675204076Spjd case HIO_FLUSH: 676204076Spjd case HIO_WRITE: 677204076Spjd data = NULL; 678204076Spjd length = 0; 679204076Spjd break; 680204076Spjd default: 681204076Spjd abort(); 682204076Spjd break; 683204076Spjd } 684204076Spjd if (hio->hio_error != 0) 685204076Spjd nv_add_int16(nvout, hio->hio_error, "error"); 686204076Spjd if (hast_proto_send(res, res->hr_remoteout, nvout, data, 687204076Spjd length) < 0) { 688204076Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to send reply."); 689204076Spjd } 690204076Spjd nv_free(nvout); 691209185Spjd pjdlog_debug(2, "send: (%p) Moving request to the free queue.", 692204076Spjd hio); 693204076Spjd nv_free(hio->hio_nv); 694204076Spjd hio->hio_error = 0; 695204076Spjd mtx_lock(&hio_free_list_lock); 696204076Spjd wakeup = TAILQ_EMPTY(&hio_free_list); 697204076Spjd TAILQ_INSERT_TAIL(&hio_free_list, hio, hio_next); 698204076Spjd mtx_unlock(&hio_free_list_lock); 699204076Spjd if (wakeup) 700204076Spjd cv_signal(&hio_free_list_cond); 701204076Spjd } 702204076Spjd /* NOTREACHED */ 703204076Spjd return (NULL); 704204076Spjd} 705