secondary.c revision 204076
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 204076 2010-02-18 23:16:19Z 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 } 298204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 299204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 300204076Spjd /* Exit on split-brain. */ 301204076Spjd exit(EX_CONFIG); 302204076Spjd } 303204076Spjd} 304204076Spjd 305204076Spjdvoid 306204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin) 307204076Spjd{ 308204076Spjd pthread_t td; 309204076Spjd pid_t pid; 310204076Spjd int error; 311204076Spjd 312204076Spjd /* 313204076Spjd * Create communication channel between parent and child. 314204076Spjd */ 315204076Spjd if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 316204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 317204076Spjd pjdlog_exit(EX_OSERR, 318204076Spjd "Unable to create control sockets between parent and child"); 319204076Spjd } 320204076Spjd 321204076Spjd pid = fork(); 322204076Spjd if (pid < 0) { 323204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 324204076Spjd pjdlog_exit(EX_OSERR, "Unable to fork"); 325204076Spjd } 326204076Spjd 327204076Spjd if (pid > 0) { 328204076Spjd /* This is parent. */ 329204076Spjd proto_close(res->hr_remotein); 330204076Spjd res->hr_remotein = NULL; 331204076Spjd proto_close(res->hr_remoteout); 332204076Spjd res->hr_remoteout = NULL; 333204076Spjd res->hr_workerpid = pid; 334204076Spjd return; 335204076Spjd } 336204076Spjd (void)pidfile_close(pfh); 337204076Spjd 338204076Spjd setproctitle("%s (secondary)", res->hr_name); 339204076Spjd 340204076Spjd init_local(res); 341204076Spjd init_remote(res, nvin); 342204076Spjd init_environment(); 343204076Spjd 344204076Spjd error = pthread_create(&td, NULL, recv_thread, res); 345204076Spjd assert(error == 0); 346204076Spjd error = pthread_create(&td, NULL, disk_thread, res); 347204076Spjd assert(error == 0); 348204076Spjd error = pthread_create(&td, NULL, send_thread, res); 349204076Spjd assert(error == 0); 350204076Spjd (void)ctrl_thread(res); 351204076Spjd} 352204076Spjd 353204076Spjdstatic void 354204076Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...) 355204076Spjd{ 356204076Spjd char msg[1024]; 357204076Spjd va_list ap; 358204076Spjd int len; 359204076Spjd 360204076Spjd va_start(ap, fmt); 361204076Spjd len = vsnprintf(msg, sizeof(msg), fmt, ap); 362204076Spjd va_end(ap); 363204076Spjd if ((size_t)len < sizeof(msg)) { 364204076Spjd switch (hio->hio_cmd) { 365204076Spjd case HIO_READ: 366204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 367204076Spjd "READ(%ju, %ju).", (uintmax_t)hio->hio_offset, 368204076Spjd (uintmax_t)hio->hio_length); 369204076Spjd break; 370204076Spjd case HIO_DELETE: 371204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 372204076Spjd "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset, 373204076Spjd (uintmax_t)hio->hio_length); 374204076Spjd break; 375204076Spjd case HIO_FLUSH: 376204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 377204076Spjd break; 378204076Spjd case HIO_WRITE: 379204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 380204076Spjd "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset, 381204076Spjd (uintmax_t)hio->hio_length); 382204076Spjd break; 383204076Spjd default: 384204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 385204076Spjd "UNKNOWN(%u).", (unsigned int)hio->hio_cmd); 386204076Spjd break; 387204076Spjd } 388204076Spjd } 389204076Spjd pjdlog_common(loglevel, debuglevel, error, "%s", msg); 390204076Spjd} 391204076Spjd 392204076Spjdstatic int 393204076Spjdrequnpack(struct hast_resource *res, struct hio *hio) 394204076Spjd{ 395204076Spjd 396204076Spjd hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd"); 397204076Spjd if (hio->hio_cmd == 0) { 398204076Spjd pjdlog_error("Header contains no 'cmd' field."); 399204076Spjd hio->hio_error = EINVAL; 400204076Spjd goto end; 401204076Spjd } 402204076Spjd switch (hio->hio_cmd) { 403204076Spjd case HIO_READ: 404204076Spjd case HIO_WRITE: 405204076Spjd case HIO_DELETE: 406204076Spjd hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset"); 407204076Spjd if (nv_error(hio->hio_nv) != 0) { 408204076Spjd pjdlog_error("Header is missing 'offset' field."); 409204076Spjd hio->hio_error = EINVAL; 410204076Spjd goto end; 411204076Spjd } 412204076Spjd hio->hio_length = nv_get_uint64(hio->hio_nv, "length"); 413204076Spjd if (nv_error(hio->hio_nv) != 0) { 414204076Spjd pjdlog_error("Header is missing 'length' field."); 415204076Spjd hio->hio_error = EINVAL; 416204076Spjd goto end; 417204076Spjd } 418204076Spjd if (hio->hio_length == 0) { 419204076Spjd pjdlog_error("Data length is zero."); 420204076Spjd hio->hio_error = EINVAL; 421204076Spjd goto end; 422204076Spjd } 423204076Spjd if (hio->hio_length > MAXPHYS) { 424204076Spjd pjdlog_error("Data length is too large (%ju > %ju).", 425204076Spjd (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS); 426204076Spjd hio->hio_error = EINVAL; 427204076Spjd goto end; 428204076Spjd } 429204076Spjd if ((hio->hio_offset % res->hr_local_sectorsize) != 0) { 430204076Spjd pjdlog_error("Offset %ju is not multiple of sector size.", 431204076Spjd (uintmax_t)hio->hio_offset); 432204076Spjd hio->hio_error = EINVAL; 433204076Spjd goto end; 434204076Spjd } 435204076Spjd if ((hio->hio_length % res->hr_local_sectorsize) != 0) { 436204076Spjd pjdlog_error("Length %ju is not multiple of sector size.", 437204076Spjd (uintmax_t)hio->hio_length); 438204076Spjd hio->hio_error = EINVAL; 439204076Spjd goto end; 440204076Spjd } 441204076Spjd if (hio->hio_offset + hio->hio_length > 442204076Spjd (uint64_t)res->hr_datasize) { 443204076Spjd pjdlog_error("Data offset is too large (%ju > %ju).", 444204076Spjd (uintmax_t)(hio->hio_offset + hio->hio_length), 445204076Spjd (uintmax_t)res->hr_datasize); 446204076Spjd hio->hio_error = EINVAL; 447204076Spjd goto end; 448204076Spjd } 449204076Spjd break; 450204076Spjd default: 451204076Spjd pjdlog_error("Header contains invalid 'cmd' (%hhu).", 452204076Spjd hio->hio_cmd); 453204076Spjd hio->hio_error = EINVAL; 454204076Spjd goto end; 455204076Spjd } 456204076Spjd hio->hio_error = 0; 457204076Spjdend: 458204076Spjd return (hio->hio_error); 459204076Spjd} 460204076Spjd 461204076Spjd/* 462204076Spjd * Thread receives requests from the primary node. 463204076Spjd */ 464204076Spjdstatic void * 465204076Spjdrecv_thread(void *arg) 466204076Spjd{ 467204076Spjd struct hast_resource *res = arg; 468204076Spjd struct hio *hio; 469204076Spjd bool wakeup; 470204076Spjd 471204076Spjd for (;;) { 472204076Spjd pjdlog_debug(2, "recv: Taking free request."); 473204076Spjd mtx_lock(&hio_free_list_lock); 474204076Spjd while ((hio = TAILQ_FIRST(&hio_free_list)) == NULL) { 475204076Spjd pjdlog_debug(2, "recv: No free requests, waiting."); 476204076Spjd cv_wait(&hio_free_list_cond, &hio_free_list_lock); 477204076Spjd } 478204076Spjd TAILQ_REMOVE(&hio_free_list, hio, hio_next); 479204076Spjd mtx_unlock(&hio_free_list_lock); 480204076Spjd pjdlog_debug(2, "recv: (%p) Got request.", hio); 481204076Spjd if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) { 482204076Spjd pjdlog_exit(EX_TEMPFAIL, 483204076Spjd "Unable to receive request header"); 484204076Spjd } 485204076Spjd if (requnpack(res, hio) != 0) 486204076Spjd goto send_queue; 487204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, 488204076Spjd "recv: (%p) Got request header: ", hio); 489204076Spjd if (hio->hio_cmd == HIO_WRITE) { 490204076Spjd if (hast_proto_recv_data(res, res->hr_remotein, 491204076Spjd hio->hio_nv, hio->hio_data, MAXPHYS) < 0) { 492204076Spjd pjdlog_exit(EX_TEMPFAIL, 493204076Spjd "Unable to receive reply data"); 494204076Spjd } 495204076Spjd } 496204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.", 497204076Spjd hio); 498204076Spjd mtx_lock(&hio_disk_list_lock); 499204076Spjd wakeup = TAILQ_EMPTY(&hio_disk_list); 500204076Spjd TAILQ_INSERT_TAIL(&hio_disk_list, hio, hio_next); 501204076Spjd mtx_unlock(&hio_disk_list_lock); 502204076Spjd if (wakeup) 503204076Spjd cv_signal(&hio_disk_list_cond); 504204076Spjd continue; 505204076Spjdsend_queue: 506204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the send queue.", 507204076Spjd hio); 508204076Spjd mtx_lock(&hio_send_list_lock); 509204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 510204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 511204076Spjd mtx_unlock(&hio_send_list_lock); 512204076Spjd if (wakeup) 513204076Spjd cv_signal(&hio_send_list_cond); 514204076Spjd } 515204076Spjd /* NOTREACHED */ 516204076Spjd return (NULL); 517204076Spjd} 518204076Spjd 519204076Spjd/* 520204076Spjd * Thread reads from or writes to local component and also handles DELETE and 521204076Spjd * FLUSH requests. 522204076Spjd */ 523204076Spjdstatic void * 524204076Spjddisk_thread(void *arg) 525204076Spjd{ 526204076Spjd struct hast_resource *res = arg; 527204076Spjd struct hio *hio; 528204076Spjd ssize_t ret; 529204076Spjd bool clear_activemap, wakeup; 530204076Spjd 531204076Spjd clear_activemap = true; 532204076Spjd 533204076Spjd for (;;) { 534204076Spjd pjdlog_debug(2, "disk: Taking request."); 535204076Spjd mtx_lock(&hio_disk_list_lock); 536204076Spjd while ((hio = TAILQ_FIRST(&hio_disk_list)) == NULL) { 537204076Spjd pjdlog_debug(2, "disk: No requests, waiting."); 538204076Spjd cv_wait(&hio_disk_list_cond, &hio_disk_list_lock); 539204076Spjd } 540204076Spjd TAILQ_REMOVE(&hio_disk_list, hio, hio_next); 541204076Spjd mtx_unlock(&hio_disk_list_lock); 542204076Spjd while (clear_activemap) { 543204076Spjd unsigned char *map; 544204076Spjd size_t mapsize; 545204076Spjd 546204076Spjd /* 547204076Spjd * When first request is received, it means that primary 548204076Spjd * already received our activemap, merged it and stored 549204076Spjd * locally. We can now safely clear our activemap. 550204076Spjd */ 551204076Spjd mapsize = 552204076Spjd activemap_calc_ondisk_size(res->hr_local_mediasize - 553204076Spjd METADATA_SIZE, res->hr_extentsize, 554204076Spjd res->hr_local_sectorsize); 555204076Spjd map = calloc(1, mapsize); 556204076Spjd if (map == NULL) { 557204076Spjd pjdlog_warning("Unable to allocate memory to clear local activemap."); 558204076Spjd break; 559204076Spjd } 560204076Spjd if (pwrite(res->hr_localfd, map, mapsize, 561204076Spjd METADATA_SIZE) != (ssize_t)mapsize) { 562204076Spjd pjdlog_errno(LOG_WARNING, 563204076Spjd "Unable to store cleared activemap"); 564204076Spjd free(map); 565204076Spjd break; 566204076Spjd } 567204076Spjd free(map); 568204076Spjd clear_activemap = false; 569204076Spjd pjdlog_debug(1, "Local activemap cleared."); 570204076Spjd } 571204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio); 572204076Spjd /* Handle the actual request. */ 573204076Spjd switch (hio->hio_cmd) { 574204076Spjd case HIO_READ: 575204076Spjd ret = pread(res->hr_localfd, hio->hio_data, 576204076Spjd hio->hio_length, 577204076Spjd hio->hio_offset + res->hr_localoff); 578204076Spjd if (ret < 0) 579204076Spjd hio->hio_error = errno; 580204076Spjd else if (ret != (int64_t)hio->hio_length) 581204076Spjd hio->hio_error = EIO; 582204076Spjd else 583204076Spjd hio->hio_error = 0; 584204076Spjd break; 585204076Spjd case HIO_WRITE: 586204076Spjd ret = pwrite(res->hr_localfd, hio->hio_data, 587204076Spjd hio->hio_length, 588204076Spjd hio->hio_offset + res->hr_localoff); 589204076Spjd if (ret < 0) 590204076Spjd hio->hio_error = errno; 591204076Spjd else if (ret != (int64_t)hio->hio_length) 592204076Spjd hio->hio_error = EIO; 593204076Spjd else 594204076Spjd hio->hio_error = 0; 595204076Spjd break; 596204076Spjd case HIO_DELETE: 597204076Spjd ret = g_delete(res->hr_localfd, 598204076Spjd hio->hio_offset + res->hr_localoff, 599204076Spjd hio->hio_length); 600204076Spjd if (ret < 0) 601204076Spjd hio->hio_error = errno; 602204076Spjd else 603204076Spjd hio->hio_error = 0; 604204076Spjd break; 605204076Spjd case HIO_FLUSH: 606204076Spjd ret = g_flush(res->hr_localfd); 607204076Spjd if (ret < 0) 608204076Spjd hio->hio_error = errno; 609204076Spjd else 610204076Spjd hio->hio_error = 0; 611204076Spjd break; 612204076Spjd } 613204076Spjd if (hio->hio_error != 0) { 614204076Spjd reqlog(LOG_ERR, 0, hio->hio_error, hio, 615204076Spjd "Request failed: "); 616204076Spjd } 617204076Spjd pjdlog_debug(2, "disk: (%p) Moving request to the send queue.", 618204076Spjd hio); 619204076Spjd mtx_lock(&hio_send_list_lock); 620204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 621204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 622204076Spjd mtx_unlock(&hio_send_list_lock); 623204076Spjd if (wakeup) 624204076Spjd cv_signal(&hio_send_list_cond); 625204076Spjd } 626204076Spjd /* NOTREACHED */ 627204076Spjd return (NULL); 628204076Spjd} 629204076Spjd 630204076Spjd/* 631204076Spjd * Thread sends requests back to primary node. 632204076Spjd */ 633204076Spjdstatic void * 634204076Spjdsend_thread(void *arg) 635204076Spjd{ 636204076Spjd struct hast_resource *res = arg; 637204076Spjd struct nv *nvout; 638204076Spjd struct hio *hio; 639204076Spjd void *data; 640204076Spjd size_t length; 641204076Spjd bool wakeup; 642204076Spjd 643204076Spjd for (;;) { 644204076Spjd pjdlog_debug(2, "send: Taking request."); 645204076Spjd mtx_lock(&hio_send_list_lock); 646204076Spjd while ((hio = TAILQ_FIRST(&hio_send_list)) == NULL) { 647204076Spjd pjdlog_debug(2, "send: No requests, waiting."); 648204076Spjd cv_wait(&hio_send_list_cond, &hio_send_list_lock); 649204076Spjd } 650204076Spjd TAILQ_REMOVE(&hio_send_list, hio, hio_next); 651204076Spjd mtx_unlock(&hio_send_list_lock); 652204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio); 653204076Spjd nvout = nv_alloc(); 654204076Spjd /* Copy sequence number. */ 655204076Spjd nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq"); 656204076Spjd switch (hio->hio_cmd) { 657204076Spjd case HIO_READ: 658204076Spjd if (hio->hio_error == 0) { 659204076Spjd data = hio->hio_data; 660204076Spjd length = hio->hio_length; 661204076Spjd break; 662204076Spjd } 663204076Spjd /* 664204076Spjd * We send no data in case of an error. 665204076Spjd */ 666204076Spjd /* FALLTHROUGH */ 667204076Spjd case HIO_DELETE: 668204076Spjd case HIO_FLUSH: 669204076Spjd case HIO_WRITE: 670204076Spjd data = NULL; 671204076Spjd length = 0; 672204076Spjd break; 673204076Spjd default: 674204076Spjd abort(); 675204076Spjd break; 676204076Spjd } 677204076Spjd if (hio->hio_error != 0) 678204076Spjd nv_add_int16(nvout, hio->hio_error, "error"); 679204076Spjd if (hast_proto_send(res, res->hr_remoteout, nvout, data, 680204076Spjd length) < 0) { 681204076Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to send reply."); 682204076Spjd } 683204076Spjd nv_free(nvout); 684204076Spjd pjdlog_debug(2, "disk: (%p) Moving request to the free queue.", 685204076Spjd hio); 686204076Spjd nv_free(hio->hio_nv); 687204076Spjd hio->hio_error = 0; 688204076Spjd mtx_lock(&hio_free_list_lock); 689204076Spjd wakeup = TAILQ_EMPTY(&hio_free_list); 690204076Spjd TAILQ_INSERT_TAIL(&hio_free_list, hio, hio_next); 691204076Spjd mtx_unlock(&hio_free_list_lock); 692204076Spjd if (wakeup) 693204076Spjd cv_signal(&hio_free_list_cond); 694204076Spjd } 695204076Spjd /* NOTREACHED */ 696204076Spjd return (NULL); 697204076Spjd} 698