secondary.c revision 210880
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 210880 2010-08-05 18:58:00Z 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) { 130210879Spjd pjdlog_exitx(EX_TEMPFAIL, 131210879Spjd "Unable to allocate memory (%zu bytes) for hio request.", 132210879Spjd sizeof(*hio)); 133204076Spjd } 134204076Spjd hio->hio_error = 0; 135204076Spjd hio->hio_data = malloc(MAXPHYS); 136204076Spjd if (hio->hio_data == NULL) { 137210879Spjd pjdlog_exitx(EX_TEMPFAIL, 138210879Spjd "Unable to allocate memory (%zu bytes) for gctl_data.", 139210879Spjd (size_t)MAXPHYS); 140204076Spjd } 141204076Spjd TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next); 142204076Spjd } 143204076Spjd} 144204076Spjd 145204076Spjdstatic void 146204076Spjdinit_local(struct hast_resource *res) 147204076Spjd{ 148204076Spjd 149204076Spjd if (metadata_read(res, true) < 0) 150204076Spjd exit(EX_NOINPUT); 151204076Spjd} 152204076Spjd 153204076Spjdstatic void 154204076Spjdinit_remote(struct hast_resource *res, struct nv *nvin) 155204076Spjd{ 156204076Spjd uint64_t resuid; 157204076Spjd struct nv *nvout; 158204076Spjd unsigned char *map; 159204076Spjd size_t mapsize; 160204076Spjd 161204076Spjd map = NULL; 162204076Spjd mapsize = 0; 163204076Spjd nvout = nv_alloc(); 164204076Spjd nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize"); 165204076Spjd nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize"); 166204076Spjd resuid = nv_get_uint64(nvin, "resuid"); 167204076Spjd res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt"); 168204076Spjd res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 169204076Spjd nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt"); 170204076Spjd nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt"); 171204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 172204076Spjd METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize); 173204076Spjd map = malloc(mapsize); 174204076Spjd if (map == NULL) { 175204076Spjd pjdlog_exitx(EX_TEMPFAIL, 176204076Spjd "Unable to allocate memory (%zu bytes) for activemap.", 177204076Spjd mapsize); 178204076Spjd } 179204076Spjd nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize"); 180204076Spjd /* 181204076Spjd * When we work as primary and secondary is missing we will increase 182204076Spjd * localcnt in our metadata. When secondary is connected and synced 183204076Spjd * we make localcnt be equal to remotecnt, which means nodes are more 184204076Spjd * or less in sync. 185204076Spjd * Split-brain condition is when both nodes are not able to communicate 186204076Spjd * and are both configured as primary nodes. In turn, they can both 187204076Spjd * make incompatible changes to the data and we have to detect that. 188204076Spjd * Under split-brain condition we will increase our localcnt on first 189204076Spjd * write and remote node will increase its localcnt on first write. 190204076Spjd * When we connect we can see that primary's localcnt is greater than 191204076Spjd * our remotecnt (primary was modified while we weren't watching) and 192204076Spjd * our localcnt is greater than primary's remotecnt (we were modified 193204076Spjd * while primary wasn't watching). 194204076Spjd * There are many possible combinations which are all gathered below. 195204076Spjd * Don't pay too much attention to exact numbers, the more important 196204076Spjd * is to compare them. We compare secondary's local with primary's 197204076Spjd * remote and secondary's remote with primary's local. 198204076Spjd * Note that every case where primary's localcnt is smaller than 199204076Spjd * secondary's remotecnt and where secondary's localcnt is smaller than 200204076Spjd * primary's remotecnt should be impossible in practise. We will perform 201204076Spjd * full synchronization then. Those cases are marked with an asterisk. 202204076Spjd * Regular synchronization means that only extents marked as dirty are 203204076Spjd * synchronized (regular synchronization). 204204076Spjd * 205204076Spjd * SECONDARY METADATA PRIMARY METADATA 206204076Spjd * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary. 207204076Spjd * local=3 remote=3 local=2 remote=3* ?! Full sync from primary. 208204076Spjd * local=3 remote=3 local=2 remote=4* ?! Full sync from primary. 209204076Spjd * local=3 remote=3 local=3 remote=2 Primary is out-of-date, 210204076Spjd * regular sync from secondary. 211204076Spjd * local=3 remote=3 local=3 remote=3 Regular sync just in case. 212204076Spjd * local=3 remote=3 local=3 remote=4* ?! Full sync from primary. 213204076Spjd * local=3 remote=3 local=4 remote=2 Split-brain condition. 214204076Spjd * local=3 remote=3 local=4 remote=3 Secondary out-of-date, 215204076Spjd * regular sync from primary. 216204076Spjd * local=3 remote=3 local=4 remote=4* ?! Full sync from primary. 217204076Spjd */ 218204076Spjd if (res->hr_resuid == 0) { 219204076Spjd /* 220204076Spjd * Provider is used for the first time. Initialize everything. 221204076Spjd */ 222204076Spjd assert(res->hr_secondary_localcnt == 0); 223204076Spjd res->hr_resuid = resuid; 224204076Spjd if (metadata_write(res) < 0) 225204076Spjd exit(EX_NOINPUT); 226204076Spjd memset(map, 0xff, mapsize); 227204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 228204076Spjd } else if ( 229204076Spjd /* Is primary is out-of-date? */ 230204076Spjd (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 231204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 232204076Spjd /* Node are more or less in sync? */ 233204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 234204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 235204076Spjd /* Is secondary is out-of-date? */ 236204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 237204076Spjd res->hr_secondary_remotecnt < res->hr_primary_localcnt)) { 238204076Spjd /* 239204076Spjd * Nodes are more or less in sync or one of the nodes is 240204076Spjd * out-of-date. 241204076Spjd * It doesn't matter at this point which one, we just have to 242204076Spjd * send out local bitmap to the remote node. 243204076Spjd */ 244204076Spjd if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) != 245204076Spjd (ssize_t)mapsize) { 246204076Spjd pjdlog_exit(LOG_ERR, "Unable to read activemap"); 247204076Spjd } 248204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 249204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) { 250204076Spjd /* Primary is out-of-date, sync from secondary. */ 251204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 252204076Spjd } else { 253204076Spjd /* 254204076Spjd * Secondary is out-of-date or counts match. 255204076Spjd * Sync from primary. 256204076Spjd */ 257204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 258204076Spjd } 259204076Spjd } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 260204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 261204076Spjd /* 262204076Spjd * Not good, we have split-brain condition. 263204076Spjd */ 264204076Spjd pjdlog_error("Split-brain detected, exiting."); 265204076Spjd nv_add_string(nvout, "Split-brain condition!", "errmsg"); 266204076Spjd free(map); 267204076Spjd map = NULL; 268204076Spjd mapsize = 0; 269204076Spjd } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt || 270204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ { 271204076Spjd /* 272204076Spjd * This should never happen in practise, but we will perform 273204076Spjd * full synchronization. 274204076Spjd */ 275204076Spjd assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt || 276204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt); 277204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 278204076Spjd METADATA_SIZE, res->hr_extentsize, 279204076Spjd res->hr_local_sectorsize); 280204076Spjd memset(map, 0xff, mapsize); 281204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) { 282204076Spjd /* In this one of five cases sync from secondary. */ 283204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 284204076Spjd } else { 285204076Spjd /* For the rest four cases sync from primary. */ 286204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 287204076Spjd } 288204076Spjd pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).", 289204076Spjd (uintmax_t)res->hr_primary_localcnt, 290204076Spjd (uintmax_t)res->hr_primary_remotecnt, 291204076Spjd (uintmax_t)res->hr_secondary_localcnt, 292204076Spjd (uintmax_t)res->hr_secondary_remotecnt); 293204076Spjd } 294204076Spjd if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) { 295204076Spjd pjdlog_errno(LOG_WARNING, "Unable to send activemap to %s", 296204076Spjd res->hr_remoteaddr); 297204076Spjd nv_free(nvout); 298204076Spjd exit(EX_TEMPFAIL); 299204076Spjd } 300209182Spjd nv_free(nvout); 301204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 302204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 303204076Spjd /* Exit on split-brain. */ 304204076Spjd exit(EX_CONFIG); 305204076Spjd } 306204076Spjd} 307204076Spjd 308204076Spjdvoid 309204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin) 310204076Spjd{ 311204076Spjd pthread_t td; 312204076Spjd pid_t pid; 313204076Spjd int error; 314204076Spjd 315204076Spjd /* 316204076Spjd * Create communication channel between parent and child. 317204076Spjd */ 318204076Spjd if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 319204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 320204076Spjd pjdlog_exit(EX_OSERR, 321204076Spjd "Unable to create control sockets between parent and child"); 322204076Spjd } 323204076Spjd 324204076Spjd pid = fork(); 325204076Spjd if (pid < 0) { 326204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 327204076Spjd pjdlog_exit(EX_OSERR, "Unable to fork"); 328204076Spjd } 329204076Spjd 330204076Spjd if (pid > 0) { 331204076Spjd /* This is parent. */ 332204076Spjd proto_close(res->hr_remotein); 333204076Spjd res->hr_remotein = NULL; 334204076Spjd proto_close(res->hr_remoteout); 335204076Spjd res->hr_remoteout = NULL; 336204076Spjd res->hr_workerpid = pid; 337204076Spjd return; 338204076Spjd } 339204076Spjd (void)pidfile_close(pfh); 340204076Spjd 341204076Spjd setproctitle("%s (secondary)", res->hr_name); 342204076Spjd 343210880Spjd signal(SIGHUP, SIG_DFL); 344210880Spjd signal(SIGCHLD, SIG_DFL); 345210880Spjd 346207371Spjd /* Error in setting timeout is not critical, but why should it fail? */ 347207371Spjd if (proto_timeout(res->hr_remotein, 0) < 0) 348207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 349207371Spjd if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0) 350207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 351207371Spjd 352204076Spjd init_local(res); 353204076Spjd init_remote(res, nvin); 354204076Spjd init_environment(); 355204076Spjd 356204076Spjd error = pthread_create(&td, NULL, recv_thread, res); 357204076Spjd assert(error == 0); 358204076Spjd error = pthread_create(&td, NULL, disk_thread, res); 359204076Spjd assert(error == 0); 360204076Spjd error = pthread_create(&td, NULL, send_thread, res); 361204076Spjd assert(error == 0); 362204076Spjd (void)ctrl_thread(res); 363204076Spjd} 364204076Spjd 365204076Spjdstatic void 366204076Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...) 367204076Spjd{ 368204076Spjd char msg[1024]; 369204076Spjd va_list ap; 370204076Spjd int len; 371204076Spjd 372204076Spjd va_start(ap, fmt); 373204076Spjd len = vsnprintf(msg, sizeof(msg), fmt, ap); 374204076Spjd va_end(ap); 375204076Spjd if ((size_t)len < sizeof(msg)) { 376204076Spjd switch (hio->hio_cmd) { 377204076Spjd case HIO_READ: 378204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 379204076Spjd "READ(%ju, %ju).", (uintmax_t)hio->hio_offset, 380204076Spjd (uintmax_t)hio->hio_length); 381204076Spjd break; 382204076Spjd case HIO_DELETE: 383204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 384204076Spjd "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset, 385204076Spjd (uintmax_t)hio->hio_length); 386204076Spjd break; 387204076Spjd case HIO_FLUSH: 388204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 389204076Spjd break; 390204076Spjd case HIO_WRITE: 391204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 392204076Spjd "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset, 393204076Spjd (uintmax_t)hio->hio_length); 394204076Spjd break; 395204076Spjd default: 396204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 397204076Spjd "UNKNOWN(%u).", (unsigned int)hio->hio_cmd); 398204076Spjd break; 399204076Spjd } 400204076Spjd } 401204076Spjd pjdlog_common(loglevel, debuglevel, error, "%s", msg); 402204076Spjd} 403204076Spjd 404204076Spjdstatic int 405204076Spjdrequnpack(struct hast_resource *res, struct hio *hio) 406204076Spjd{ 407204076Spjd 408204076Spjd hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd"); 409204076Spjd if (hio->hio_cmd == 0) { 410204076Spjd pjdlog_error("Header contains no 'cmd' field."); 411204076Spjd hio->hio_error = EINVAL; 412204076Spjd goto end; 413204076Spjd } 414204076Spjd switch (hio->hio_cmd) { 415204076Spjd case HIO_READ: 416204076Spjd case HIO_WRITE: 417204076Spjd case HIO_DELETE: 418204076Spjd hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset"); 419204076Spjd if (nv_error(hio->hio_nv) != 0) { 420204076Spjd pjdlog_error("Header is missing 'offset' field."); 421204076Spjd hio->hio_error = EINVAL; 422204076Spjd goto end; 423204076Spjd } 424204076Spjd hio->hio_length = nv_get_uint64(hio->hio_nv, "length"); 425204076Spjd if (nv_error(hio->hio_nv) != 0) { 426204076Spjd pjdlog_error("Header is missing 'length' field."); 427204076Spjd hio->hio_error = EINVAL; 428204076Spjd goto end; 429204076Spjd } 430204076Spjd if (hio->hio_length == 0) { 431204076Spjd pjdlog_error("Data length is zero."); 432204076Spjd hio->hio_error = EINVAL; 433204076Spjd goto end; 434204076Spjd } 435204076Spjd if (hio->hio_length > MAXPHYS) { 436204076Spjd pjdlog_error("Data length is too large (%ju > %ju).", 437204076Spjd (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS); 438204076Spjd hio->hio_error = EINVAL; 439204076Spjd goto end; 440204076Spjd } 441204076Spjd if ((hio->hio_offset % res->hr_local_sectorsize) != 0) { 442204076Spjd pjdlog_error("Offset %ju is not multiple of sector size.", 443204076Spjd (uintmax_t)hio->hio_offset); 444204076Spjd hio->hio_error = EINVAL; 445204076Spjd goto end; 446204076Spjd } 447204076Spjd if ((hio->hio_length % res->hr_local_sectorsize) != 0) { 448204076Spjd pjdlog_error("Length %ju is not multiple of sector size.", 449204076Spjd (uintmax_t)hio->hio_length); 450204076Spjd hio->hio_error = EINVAL; 451204076Spjd goto end; 452204076Spjd } 453204076Spjd if (hio->hio_offset + hio->hio_length > 454204076Spjd (uint64_t)res->hr_datasize) { 455204076Spjd pjdlog_error("Data offset is too large (%ju > %ju).", 456204076Spjd (uintmax_t)(hio->hio_offset + hio->hio_length), 457204076Spjd (uintmax_t)res->hr_datasize); 458204076Spjd hio->hio_error = EINVAL; 459204076Spjd goto end; 460204076Spjd } 461204076Spjd break; 462204076Spjd default: 463204076Spjd pjdlog_error("Header contains invalid 'cmd' (%hhu).", 464204076Spjd hio->hio_cmd); 465204076Spjd hio->hio_error = EINVAL; 466204076Spjd goto end; 467204076Spjd } 468204076Spjd hio->hio_error = 0; 469204076Spjdend: 470204076Spjd return (hio->hio_error); 471204076Spjd} 472204076Spjd 473204076Spjd/* 474204076Spjd * Thread receives requests from the primary node. 475204076Spjd */ 476204076Spjdstatic void * 477204076Spjdrecv_thread(void *arg) 478204076Spjd{ 479204076Spjd struct hast_resource *res = arg; 480204076Spjd struct hio *hio; 481204076Spjd bool wakeup; 482204076Spjd 483204076Spjd for (;;) { 484204076Spjd pjdlog_debug(2, "recv: Taking free request."); 485204076Spjd mtx_lock(&hio_free_list_lock); 486204076Spjd while ((hio = TAILQ_FIRST(&hio_free_list)) == NULL) { 487204076Spjd pjdlog_debug(2, "recv: No free requests, waiting."); 488204076Spjd cv_wait(&hio_free_list_cond, &hio_free_list_lock); 489204076Spjd } 490204076Spjd TAILQ_REMOVE(&hio_free_list, hio, hio_next); 491204076Spjd mtx_unlock(&hio_free_list_lock); 492204076Spjd pjdlog_debug(2, "recv: (%p) Got request.", hio); 493204076Spjd if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) { 494204076Spjd pjdlog_exit(EX_TEMPFAIL, 495204076Spjd "Unable to receive request header"); 496204076Spjd } 497204076Spjd if (requnpack(res, hio) != 0) 498204076Spjd goto send_queue; 499204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, 500204076Spjd "recv: (%p) Got request header: ", hio); 501204076Spjd if (hio->hio_cmd == HIO_WRITE) { 502204076Spjd if (hast_proto_recv_data(res, res->hr_remotein, 503204076Spjd hio->hio_nv, hio->hio_data, MAXPHYS) < 0) { 504204076Spjd pjdlog_exit(EX_TEMPFAIL, 505204076Spjd "Unable to receive reply data"); 506204076Spjd } 507204076Spjd } 508204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.", 509204076Spjd hio); 510204076Spjd mtx_lock(&hio_disk_list_lock); 511204076Spjd wakeup = TAILQ_EMPTY(&hio_disk_list); 512204076Spjd TAILQ_INSERT_TAIL(&hio_disk_list, hio, hio_next); 513204076Spjd mtx_unlock(&hio_disk_list_lock); 514204076Spjd if (wakeup) 515204076Spjd cv_signal(&hio_disk_list_cond); 516204076Spjd continue; 517204076Spjdsend_queue: 518204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the send queue.", 519204076Spjd hio); 520204076Spjd mtx_lock(&hio_send_list_lock); 521204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 522204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 523204076Spjd mtx_unlock(&hio_send_list_lock); 524204076Spjd if (wakeup) 525204076Spjd cv_signal(&hio_send_list_cond); 526204076Spjd } 527204076Spjd /* NOTREACHED */ 528204076Spjd return (NULL); 529204076Spjd} 530204076Spjd 531204076Spjd/* 532204076Spjd * Thread reads from or writes to local component and also handles DELETE and 533204076Spjd * FLUSH requests. 534204076Spjd */ 535204076Spjdstatic void * 536204076Spjddisk_thread(void *arg) 537204076Spjd{ 538204076Spjd struct hast_resource *res = arg; 539204076Spjd struct hio *hio; 540204076Spjd ssize_t ret; 541204076Spjd bool clear_activemap, wakeup; 542204076Spjd 543204076Spjd clear_activemap = true; 544204076Spjd 545204076Spjd for (;;) { 546204076Spjd pjdlog_debug(2, "disk: Taking request."); 547204076Spjd mtx_lock(&hio_disk_list_lock); 548204076Spjd while ((hio = TAILQ_FIRST(&hio_disk_list)) == NULL) { 549204076Spjd pjdlog_debug(2, "disk: No requests, waiting."); 550204076Spjd cv_wait(&hio_disk_list_cond, &hio_disk_list_lock); 551204076Spjd } 552204076Spjd TAILQ_REMOVE(&hio_disk_list, hio, hio_next); 553204076Spjd mtx_unlock(&hio_disk_list_lock); 554204076Spjd while (clear_activemap) { 555204076Spjd unsigned char *map; 556204076Spjd size_t mapsize; 557204076Spjd 558204076Spjd /* 559204076Spjd * When first request is received, it means that primary 560204076Spjd * already received our activemap, merged it and stored 561204076Spjd * locally. We can now safely clear our activemap. 562204076Spjd */ 563204076Spjd mapsize = 564204076Spjd activemap_calc_ondisk_size(res->hr_local_mediasize - 565204076Spjd METADATA_SIZE, res->hr_extentsize, 566204076Spjd res->hr_local_sectorsize); 567204076Spjd map = calloc(1, mapsize); 568204076Spjd if (map == NULL) { 569204076Spjd pjdlog_warning("Unable to allocate memory to clear local activemap."); 570204076Spjd break; 571204076Spjd } 572204076Spjd if (pwrite(res->hr_localfd, map, mapsize, 573204076Spjd METADATA_SIZE) != (ssize_t)mapsize) { 574204076Spjd pjdlog_errno(LOG_WARNING, 575204076Spjd "Unable to store cleared activemap"); 576204076Spjd free(map); 577204076Spjd break; 578204076Spjd } 579204076Spjd free(map); 580204076Spjd clear_activemap = false; 581204076Spjd pjdlog_debug(1, "Local activemap cleared."); 582204076Spjd } 583204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio); 584204076Spjd /* Handle the actual request. */ 585204076Spjd switch (hio->hio_cmd) { 586204076Spjd case HIO_READ: 587204076Spjd ret = pread(res->hr_localfd, hio->hio_data, 588204076Spjd hio->hio_length, 589204076Spjd hio->hio_offset + res->hr_localoff); 590204076Spjd if (ret < 0) 591204076Spjd hio->hio_error = errno; 592204076Spjd else if (ret != (int64_t)hio->hio_length) 593204076Spjd hio->hio_error = EIO; 594204076Spjd else 595204076Spjd hio->hio_error = 0; 596204076Spjd break; 597204076Spjd case HIO_WRITE: 598204076Spjd ret = pwrite(res->hr_localfd, hio->hio_data, 599204076Spjd hio->hio_length, 600204076Spjd hio->hio_offset + res->hr_localoff); 601204076Spjd if (ret < 0) 602204076Spjd hio->hio_error = errno; 603204076Spjd else if (ret != (int64_t)hio->hio_length) 604204076Spjd hio->hio_error = EIO; 605204076Spjd else 606204076Spjd hio->hio_error = 0; 607204076Spjd break; 608204076Spjd case HIO_DELETE: 609204076Spjd ret = g_delete(res->hr_localfd, 610204076Spjd hio->hio_offset + res->hr_localoff, 611204076Spjd hio->hio_length); 612204076Spjd if (ret < 0) 613204076Spjd hio->hio_error = errno; 614204076Spjd else 615204076Spjd hio->hio_error = 0; 616204076Spjd break; 617204076Spjd case HIO_FLUSH: 618204076Spjd ret = g_flush(res->hr_localfd); 619204076Spjd if (ret < 0) 620204076Spjd hio->hio_error = errno; 621204076Spjd else 622204076Spjd hio->hio_error = 0; 623204076Spjd break; 624204076Spjd } 625204076Spjd if (hio->hio_error != 0) { 626204076Spjd reqlog(LOG_ERR, 0, hio->hio_error, hio, 627204076Spjd "Request failed: "); 628204076Spjd } 629204076Spjd pjdlog_debug(2, "disk: (%p) Moving request to the send queue.", 630204076Spjd hio); 631204076Spjd mtx_lock(&hio_send_list_lock); 632204076Spjd wakeup = TAILQ_EMPTY(&hio_send_list); 633204076Spjd TAILQ_INSERT_TAIL(&hio_send_list, hio, hio_next); 634204076Spjd mtx_unlock(&hio_send_list_lock); 635204076Spjd if (wakeup) 636204076Spjd cv_signal(&hio_send_list_cond); 637204076Spjd } 638204076Spjd /* NOTREACHED */ 639204076Spjd return (NULL); 640204076Spjd} 641204076Spjd 642204076Spjd/* 643204076Spjd * Thread sends requests back to primary node. 644204076Spjd */ 645204076Spjdstatic void * 646204076Spjdsend_thread(void *arg) 647204076Spjd{ 648204076Spjd struct hast_resource *res = arg; 649204076Spjd struct nv *nvout; 650204076Spjd struct hio *hio; 651204076Spjd void *data; 652204076Spjd size_t length; 653204076Spjd bool wakeup; 654204076Spjd 655204076Spjd for (;;) { 656204076Spjd pjdlog_debug(2, "send: Taking request."); 657204076Spjd mtx_lock(&hio_send_list_lock); 658204076Spjd while ((hio = TAILQ_FIRST(&hio_send_list)) == NULL) { 659204076Spjd pjdlog_debug(2, "send: No requests, waiting."); 660204076Spjd cv_wait(&hio_send_list_cond, &hio_send_list_lock); 661204076Spjd } 662204076Spjd TAILQ_REMOVE(&hio_send_list, hio, hio_next); 663204076Spjd mtx_unlock(&hio_send_list_lock); 664204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio); 665204076Spjd nvout = nv_alloc(); 666204076Spjd /* Copy sequence number. */ 667204076Spjd nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq"); 668204076Spjd switch (hio->hio_cmd) { 669204076Spjd case HIO_READ: 670204076Spjd if (hio->hio_error == 0) { 671204076Spjd data = hio->hio_data; 672204076Spjd length = hio->hio_length; 673204076Spjd break; 674204076Spjd } 675204076Spjd /* 676204076Spjd * We send no data in case of an error. 677204076Spjd */ 678204076Spjd /* FALLTHROUGH */ 679204076Spjd case HIO_DELETE: 680204076Spjd case HIO_FLUSH: 681204076Spjd case HIO_WRITE: 682204076Spjd data = NULL; 683204076Spjd length = 0; 684204076Spjd break; 685204076Spjd default: 686204076Spjd abort(); 687204076Spjd break; 688204076Spjd } 689204076Spjd if (hio->hio_error != 0) 690204076Spjd nv_add_int16(nvout, hio->hio_error, "error"); 691204076Spjd if (hast_proto_send(res, res->hr_remoteout, nvout, data, 692204076Spjd length) < 0) { 693204076Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to send reply."); 694204076Spjd } 695204076Spjd nv_free(nvout); 696209185Spjd pjdlog_debug(2, "send: (%p) Moving request to the free queue.", 697204076Spjd hio); 698204076Spjd nv_free(hio->hio_nv); 699204076Spjd hio->hio_error = 0; 700204076Spjd mtx_lock(&hio_free_list_lock); 701204076Spjd wakeup = TAILQ_EMPTY(&hio_free_list); 702204076Spjd TAILQ_INSERT_TAIL(&hio_free_list, hio, hio_next); 703204076Spjd mtx_unlock(&hio_free_list_lock); 704204076Spjd if (wakeup) 705204076Spjd cv_signal(&hio_free_list_cond); 706204076Spjd } 707204076Spjd /* NOTREACHED */ 708204076Spjd return (NULL); 709204076Spjd} 710