1204076Spjd/*- 2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3330449Seadler * 4204076Spjd * Copyright (c) 2009-2010 The FreeBSD Foundation 5211877Spjd * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 6204076Spjd * All rights reserved. 7204076Spjd * 8204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from 9204076Spjd * the FreeBSD Foundation. 10204076Spjd * 11204076Spjd * Redistribution and use in source and binary forms, with or without 12204076Spjd * modification, are permitted provided that the following conditions 13204076Spjd * are met: 14204076Spjd * 1. Redistributions of source code must retain the above copyright 15204076Spjd * notice, this list of conditions and the following disclaimer. 16204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 17204076Spjd * notice, this list of conditions and the following disclaimer in the 18204076Spjd * documentation and/or other materials provided with the distribution. 19204076Spjd * 20204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23204076Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30204076Spjd * SUCH DAMAGE. 31204076Spjd */ 32204076Spjd 33204076Spjd#include <sys/cdefs.h> 34204076Spjd__FBSDID("$FreeBSD: stable/11/sbin/hastd/secondary.c 330449 2018-03-05 07:26:05Z eadler $"); 35204076Spjd 36204076Spjd#include <sys/param.h> 37204076Spjd#include <sys/time.h> 38204076Spjd#include <sys/bio.h> 39204076Spjd#include <sys/disk.h> 40204076Spjd#include <sys/stat.h> 41204076Spjd 42204076Spjd#include <err.h> 43204076Spjd#include <errno.h> 44204076Spjd#include <fcntl.h> 45204076Spjd#include <libgeom.h> 46204076Spjd#include <pthread.h> 47213009Spjd#include <signal.h> 48204076Spjd#include <stdint.h> 49204076Spjd#include <stdio.h> 50204076Spjd#include <string.h> 51204076Spjd#include <sysexits.h> 52204076Spjd#include <unistd.h> 53204076Spjd 54204076Spjd#include <activemap.h> 55204076Spjd#include <nv.h> 56204076Spjd#include <pjdlog.h> 57204076Spjd 58204076Spjd#include "control.h" 59212038Spjd#include "event.h" 60204076Spjd#include "hast.h" 61204076Spjd#include "hast_proto.h" 62204076Spjd#include "hastd.h" 63211977Spjd#include "hooks.h" 64204076Spjd#include "metadata.h" 65204076Spjd#include "proto.h" 66204076Spjd#include "subr.h" 67204076Spjd#include "synch.h" 68204076Spjd 69204076Spjdstruct hio { 70219864Spjd uint64_t hio_seq; 71219864Spjd int hio_error; 72204076Spjd void *hio_data; 73204076Spjd uint8_t hio_cmd; 74204076Spjd uint64_t hio_offset; 75204076Spjd uint64_t hio_length; 76246922Spjd bool hio_memsync; 77204076Spjd TAILQ_ENTRY(hio) hio_next; 78204076Spjd}; 79204076Spjd 80211984Spjdstatic struct hast_resource *gres; 81211984Spjd 82204076Spjd/* 83204076Spjd * Free list holds unused structures. When free list is empty, we have to wait 84204076Spjd * until some in-progress requests are freed. 85204076Spjd */ 86204076Spjdstatic TAILQ_HEAD(, hio) hio_free_list; 87257155Strocinystatic size_t hio_free_list_size; 88204076Spjdstatic pthread_mutex_t hio_free_list_lock; 89204076Spjdstatic pthread_cond_t hio_free_list_cond; 90204076Spjd/* 91255717Strociny * Disk thread (the one that does I/O requests) takes requests from this list. 92204076Spjd */ 93204076Spjdstatic TAILQ_HEAD(, hio) hio_disk_list; 94257155Strocinystatic size_t hio_disk_list_size; 95204076Spjdstatic pthread_mutex_t hio_disk_list_lock; 96204076Spjdstatic pthread_cond_t hio_disk_list_cond; 97204076Spjd/* 98255717Strociny * Thread that sends requests back to primary takes requests from this list. 99204076Spjd */ 100204076Spjdstatic TAILQ_HEAD(, hio) hio_send_list; 101257155Strocinystatic size_t hio_send_list_size; 102204076Spjdstatic pthread_mutex_t hio_send_list_lock; 103204076Spjdstatic pthread_cond_t hio_send_list_cond; 104204076Spjd 105204076Spjd/* 106204076Spjd * Maximum number of outstanding I/O requests. 107204076Spjd */ 108204076Spjd#define HAST_HIO_MAX 256 109204076Spjd 110204076Spjdstatic void *recv_thread(void *arg); 111204076Spjdstatic void *disk_thread(void *arg); 112204076Spjdstatic void *send_thread(void *arg); 113204076Spjd 114211877Spjd#define QUEUE_INSERT(name, hio) do { \ 115211877Spjd mtx_lock(&hio_##name##_list_lock); \ 116259195Strociny if (TAILQ_EMPTY(&hio_##name##_list)) \ 117259195Strociny cv_broadcast(&hio_##name##_list_cond); \ 118211877Spjd TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \ 119257155Strociny hio_##name##_list_size++; \ 120211877Spjd mtx_unlock(&hio_##name##_list_lock); \ 121211877Spjd} while (0) 122211877Spjd#define QUEUE_TAKE(name, hio) do { \ 123211877Spjd mtx_lock(&hio_##name##_list_lock); \ 124211877Spjd while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 125211877Spjd cv_wait(&hio_##name##_list_cond, \ 126211877Spjd &hio_##name##_list_lock); \ 127211877Spjd } \ 128257155Strociny PJDLOG_ASSERT(hio_##name##_list_size != 0); \ 129257155Strociny hio_##name##_list_size--; \ 130211877Spjd TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next); \ 131211877Spjd mtx_unlock(&hio_##name##_list_lock); \ 132211877Spjd} while (0) 133226861Spjd 134226854Spjdstatic void 135257155Strocinyoutput_status_aux(struct nv *nvout) 136257155Strociny{ 137257155Strociny 138257155Strociny nv_add_uint64(nvout, (uint64_t)hio_free_list_size, "idle_queue_size"); 139257155Strociny nv_add_uint64(nvout, (uint64_t)hio_disk_list_size, "local_queue_size"); 140257155Strociny nv_add_uint64(nvout, (uint64_t)hio_send_list_size, "send_queue_size"); 141257155Strociny} 142257155Strociny 143257155Strocinystatic void 144226854Spjdhio_clear(struct hio *hio) 145226854Spjd{ 146211877Spjd 147226854Spjd hio->hio_seq = 0; 148226854Spjd hio->hio_error = 0; 149226854Spjd hio->hio_cmd = HIO_UNDEF; 150226854Spjd hio->hio_offset = 0; 151226854Spjd hio->hio_length = 0; 152246922Spjd hio->hio_memsync = false; 153226854Spjd} 154226854Spjd 155204076Spjdstatic void 156246922Spjdhio_copy(const struct hio *srchio, struct hio *dsthio) 157246922Spjd{ 158246922Spjd 159246922Spjd /* 160246922Spjd * We don't copy hio_error, hio_data and hio_next fields. 161246922Spjd */ 162246922Spjd 163246922Spjd dsthio->hio_seq = srchio->hio_seq; 164246922Spjd dsthio->hio_cmd = srchio->hio_cmd; 165246922Spjd dsthio->hio_offset = srchio->hio_offset; 166246922Spjd dsthio->hio_length = srchio->hio_length; 167246922Spjd dsthio->hio_memsync = srchio->hio_memsync; 168246922Spjd} 169246922Spjd 170246922Spjdstatic void 171204076Spjdinit_environment(void) 172204076Spjd{ 173204076Spjd struct hio *hio; 174204076Spjd unsigned int ii; 175204076Spjd 176204076Spjd /* 177204076Spjd * Initialize lists, their locks and theirs condition variables. 178204076Spjd */ 179204076Spjd TAILQ_INIT(&hio_free_list); 180204076Spjd mtx_init(&hio_free_list_lock); 181204076Spjd cv_init(&hio_free_list_cond); 182204076Spjd TAILQ_INIT(&hio_disk_list); 183204076Spjd mtx_init(&hio_disk_list_lock); 184204076Spjd cv_init(&hio_disk_list_cond); 185204076Spjd TAILQ_INIT(&hio_send_list); 186204076Spjd mtx_init(&hio_send_list_lock); 187204076Spjd cv_init(&hio_send_list_cond); 188204076Spjd 189204076Spjd /* 190204076Spjd * Allocate requests pool and initialize requests. 191204076Spjd */ 192204076Spjd for (ii = 0; ii < HAST_HIO_MAX; ii++) { 193204076Spjd hio = malloc(sizeof(*hio)); 194204076Spjd if (hio == NULL) { 195210879Spjd pjdlog_exitx(EX_TEMPFAIL, 196210879Spjd "Unable to allocate memory (%zu bytes) for hio request.", 197210879Spjd sizeof(*hio)); 198204076Spjd } 199204076Spjd hio->hio_data = malloc(MAXPHYS); 200204076Spjd if (hio->hio_data == NULL) { 201210879Spjd pjdlog_exitx(EX_TEMPFAIL, 202210879Spjd "Unable to allocate memory (%zu bytes) for gctl_data.", 203210879Spjd (size_t)MAXPHYS); 204204076Spjd } 205226854Spjd hio_clear(hio); 206204076Spjd TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next); 207257155Strociny hio_free_list_size++; 208204076Spjd } 209204076Spjd} 210204076Spjd 211204076Spjdstatic void 212204076Spjdinit_local(struct hast_resource *res) 213204076Spjd{ 214204076Spjd 215229945Spjd if (metadata_read(res, true) == -1) 216204076Spjd exit(EX_NOINPUT); 217204076Spjd} 218204076Spjd 219204076Spjdstatic void 220204076Spjdinit_remote(struct hast_resource *res, struct nv *nvin) 221204076Spjd{ 222204076Spjd uint64_t resuid; 223204076Spjd struct nv *nvout; 224204076Spjd unsigned char *map; 225204076Spjd size_t mapsize; 226204076Spjd 227223181Strociny#ifdef notyet 228220271Spjd /* Setup direction. */ 229220271Spjd if (proto_send(res->hr_remoteout, NULL, 0) == -1) 230220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 231223181Strociny#endif 232220271Spjd 233204076Spjd nvout = nv_alloc(); 234204076Spjd nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize"); 235204076Spjd nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize"); 236204076Spjd resuid = nv_get_uint64(nvin, "resuid"); 237204076Spjd res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt"); 238204076Spjd res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 239204076Spjd nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt"); 240204076Spjd nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt"); 241204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 242204076Spjd METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize); 243204076Spjd map = malloc(mapsize); 244204076Spjd if (map == NULL) { 245204076Spjd pjdlog_exitx(EX_TEMPFAIL, 246204076Spjd "Unable to allocate memory (%zu bytes) for activemap.", 247204076Spjd mapsize); 248204076Spjd } 249204076Spjd /* 250204076Spjd * When we work as primary and secondary is missing we will increase 251204076Spjd * localcnt in our metadata. When secondary is connected and synced 252204076Spjd * we make localcnt be equal to remotecnt, which means nodes are more 253204076Spjd * or less in sync. 254204076Spjd * Split-brain condition is when both nodes are not able to communicate 255204076Spjd * and are both configured as primary nodes. In turn, they can both 256204076Spjd * make incompatible changes to the data and we have to detect that. 257204076Spjd * Under split-brain condition we will increase our localcnt on first 258204076Spjd * write and remote node will increase its localcnt on first write. 259204076Spjd * When we connect we can see that primary's localcnt is greater than 260204076Spjd * our remotecnt (primary was modified while we weren't watching) and 261204076Spjd * our localcnt is greater than primary's remotecnt (we were modified 262204076Spjd * while primary wasn't watching). 263204076Spjd * There are many possible combinations which are all gathered below. 264204076Spjd * Don't pay too much attention to exact numbers, the more important 265204076Spjd * is to compare them. We compare secondary's local with primary's 266204076Spjd * remote and secondary's remote with primary's local. 267204076Spjd * Note that every case where primary's localcnt is smaller than 268204076Spjd * secondary's remotecnt and where secondary's localcnt is smaller than 269204076Spjd * primary's remotecnt should be impossible in practise. We will perform 270204076Spjd * full synchronization then. Those cases are marked with an asterisk. 271204076Spjd * Regular synchronization means that only extents marked as dirty are 272204076Spjd * synchronized (regular synchronization). 273204076Spjd * 274204076Spjd * SECONDARY METADATA PRIMARY METADATA 275204076Spjd * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary. 276204076Spjd * local=3 remote=3 local=2 remote=3* ?! Full sync from primary. 277204076Spjd * local=3 remote=3 local=2 remote=4* ?! Full sync from primary. 278204076Spjd * local=3 remote=3 local=3 remote=2 Primary is out-of-date, 279204076Spjd * regular sync from secondary. 280204076Spjd * local=3 remote=3 local=3 remote=3 Regular sync just in case. 281204076Spjd * local=3 remote=3 local=3 remote=4* ?! Full sync from primary. 282204076Spjd * local=3 remote=3 local=4 remote=2 Split-brain condition. 283204076Spjd * local=3 remote=3 local=4 remote=3 Secondary out-of-date, 284204076Spjd * regular sync from primary. 285204076Spjd * local=3 remote=3 local=4 remote=4* ?! Full sync from primary. 286204076Spjd */ 287204076Spjd if (res->hr_resuid == 0) { 288204076Spjd /* 289214284Spjd * Provider is used for the first time. If primary node done no 290214284Spjd * writes yet as well (we will find "virgin" argument) then 291214284Spjd * there is no need to synchronize anything. If primary node 292214284Spjd * done any writes already we have to synchronize everything. 293204076Spjd */ 294218138Spjd PJDLOG_ASSERT(res->hr_secondary_localcnt == 0); 295204076Spjd res->hr_resuid = resuid; 296229945Spjd if (metadata_write(res) == -1) 297204076Spjd exit(EX_NOINPUT); 298214284Spjd if (nv_exists(nvin, "virgin")) { 299214284Spjd free(map); 300214284Spjd map = NULL; 301214284Spjd mapsize = 0; 302214284Spjd } else { 303214284Spjd memset(map, 0xff, mapsize); 304214284Spjd } 305220865Spjd nv_add_int8(nvout, 1, "virgin"); 306204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 307219830Spjd } else if (res->hr_resuid != resuid) { 308219830Spjd char errmsg[256]; 309219830Spjd 310226854Spjd free(map); 311219830Spjd (void)snprintf(errmsg, sizeof(errmsg), 312219830Spjd "Resource unique ID mismatch (primary=%ju, secondary=%ju).", 313219830Spjd (uintmax_t)resuid, (uintmax_t)res->hr_resuid); 314219830Spjd pjdlog_error("%s", errmsg); 315219830Spjd nv_add_string(nvout, errmsg, "errmsg"); 316230092Spjd if (hast_proto_send(res, res->hr_remotein, nvout, 317230092Spjd NULL, 0) == -1) { 318230092Spjd pjdlog_exit(EX_TEMPFAIL, 319230092Spjd "Unable to send response to %s", 320219830Spjd res->hr_remoteaddr); 321219830Spjd } 322219831Spjd nv_free(nvout); 323219830Spjd exit(EX_CONFIG); 324204076Spjd } else if ( 325226842Spjd /* Is primary out-of-date? */ 326204076Spjd (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 327204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 328226842Spjd /* Are the nodes more or less in sync? */ 329204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 330204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 331226842Spjd /* Is secondary out-of-date? */ 332204076Spjd (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 333204076Spjd res->hr_secondary_remotecnt < res->hr_primary_localcnt)) { 334204076Spjd /* 335204076Spjd * Nodes are more or less in sync or one of the nodes is 336204076Spjd * out-of-date. 337204076Spjd * It doesn't matter at this point which one, we just have to 338204076Spjd * send out local bitmap to the remote node. 339204076Spjd */ 340204076Spjd if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) != 341204076Spjd (ssize_t)mapsize) { 342204076Spjd pjdlog_exit(LOG_ERR, "Unable to read activemap"); 343204076Spjd } 344204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 345204076Spjd res->hr_secondary_remotecnt == res->hr_primary_localcnt) { 346204076Spjd /* Primary is out-of-date, sync from secondary. */ 347204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 348204076Spjd } else { 349204076Spjd /* 350204076Spjd * Secondary is out-of-date or counts match. 351204076Spjd * Sync from primary. 352204076Spjd */ 353204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 354204076Spjd } 355204076Spjd } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 356204076Spjd res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 357204076Spjd /* 358204076Spjd * Not good, we have split-brain condition. 359204076Spjd */ 360226854Spjd free(map); 361204076Spjd pjdlog_error("Split-brain detected, exiting."); 362204076Spjd nv_add_string(nvout, "Split-brain condition!", "errmsg"); 363230092Spjd if (hast_proto_send(res, res->hr_remotein, nvout, 364230092Spjd NULL, 0) == -1) { 365230092Spjd pjdlog_exit(EX_TEMPFAIL, 366230092Spjd "Unable to send response to %s", 367226854Spjd res->hr_remoteaddr); 368226854Spjd } 369226854Spjd nv_free(nvout); 370226854Spjd /* Exit on split-brain. */ 371226854Spjd event_send(res, EVENT_SPLITBRAIN); 372226854Spjd exit(EX_CONFIG); 373204076Spjd } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt || 374204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ { 375204076Spjd /* 376204076Spjd * This should never happen in practise, but we will perform 377204076Spjd * full synchronization. 378204076Spjd */ 379218138Spjd PJDLOG_ASSERT(res->hr_secondary_localcnt < res->hr_primary_remotecnt || 380204076Spjd res->hr_primary_localcnt < res->hr_secondary_remotecnt); 381204076Spjd mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 382204076Spjd METADATA_SIZE, res->hr_extentsize, 383204076Spjd res->hr_local_sectorsize); 384204076Spjd memset(map, 0xff, mapsize); 385204076Spjd if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) { 386204076Spjd /* In this one of five cases sync from secondary. */ 387204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 388204076Spjd } else { 389204076Spjd /* For the rest four cases sync from primary. */ 390204076Spjd nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 391204076Spjd } 392204076Spjd pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).", 393204076Spjd (uintmax_t)res->hr_primary_localcnt, 394204076Spjd (uintmax_t)res->hr_primary_remotecnt, 395204076Spjd (uintmax_t)res->hr_secondary_localcnt, 396204076Spjd (uintmax_t)res->hr_secondary_remotecnt); 397204076Spjd } 398220007Spjd nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize"); 399229945Spjd if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) == -1) { 400214276Spjd pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s", 401204076Spjd res->hr_remoteaddr); 402204076Spjd } 403214275Spjd if (map != NULL) 404214275Spjd free(map); 405209182Spjd nv_free(nvout); 406223181Strociny#ifdef notyet 407220271Spjd /* Setup direction. */ 408220271Spjd if (proto_recv(res->hr_remotein, NULL, 0) == -1) 409220271Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 410223181Strociny#endif 411204076Spjd} 412204076Spjd 413204076Spjdvoid 414204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin) 415204076Spjd{ 416213009Spjd sigset_t mask; 417204076Spjd pthread_t td; 418204076Spjd pid_t pid; 419219482Strociny int error, mode, debuglevel; 420204076Spjd 421204076Spjd /* 422204076Spjd * Create communication channel between parent and child. 423204076Spjd */ 424229945Spjd if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) { 425204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 426204076Spjd pjdlog_exit(EX_OSERR, 427204076Spjd "Unable to create control sockets between parent and child"); 428204076Spjd } 429212038Spjd /* 430212038Spjd * Create communication channel between child and parent. 431212038Spjd */ 432229945Spjd if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) { 433212038Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 434212038Spjd pjdlog_exit(EX_OSERR, 435212038Spjd "Unable to create event sockets between child and parent"); 436212038Spjd } 437204076Spjd 438204076Spjd pid = fork(); 439229744Spjd if (pid == -1) { 440204076Spjd KEEP_ERRNO((void)pidfile_remove(pfh)); 441204076Spjd pjdlog_exit(EX_OSERR, "Unable to fork"); 442204076Spjd } 443204076Spjd 444204076Spjd if (pid > 0) { 445204076Spjd /* This is parent. */ 446204076Spjd proto_close(res->hr_remotein); 447204076Spjd res->hr_remotein = NULL; 448204076Spjd proto_close(res->hr_remoteout); 449204076Spjd res->hr_remoteout = NULL; 450212038Spjd /* Declare that we are receiver. */ 451212038Spjd proto_recv(res->hr_event, NULL, 0); 452218043Spjd /* Declare that we are sender. */ 453218043Spjd proto_send(res->hr_ctrl, NULL, 0); 454204076Spjd res->hr_workerpid = pid; 455204076Spjd return; 456204076Spjd } 457211977Spjd 458211984Spjd gres = res; 459257155Strociny res->output_status_aux = output_status_aux; 460218043Spjd mode = pjdlog_mode_get(); 461219482Strociny debuglevel = pjdlog_debug_get(); 462211984Spjd 463218043Spjd /* Declare that we are sender. */ 464218043Spjd proto_send(res->hr_event, NULL, 0); 465218043Spjd /* Declare that we are receiver. */ 466218043Spjd proto_recv(res->hr_ctrl, NULL, 0); 467218043Spjd descriptors_cleanup(res); 468204076Spjd 469218045Spjd descriptors_assert(res, mode); 470218045Spjd 471218043Spjd pjdlog_init(mode); 472219482Strociny pjdlog_debug_set(debuglevel); 473218043Spjd pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 474220005Spjd setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role)); 475204076Spjd 476213009Spjd PJDLOG_VERIFY(sigemptyset(&mask) == 0); 477213009Spjd PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); 478210880Spjd 479207371Spjd /* Error in setting timeout is not critical, but why should it fail? */ 480229945Spjd if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) == -1) 481207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 482229945Spjd if (proto_timeout(res->hr_remoteout, res->hr_timeout) == -1) 483207371Spjd pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 484207371Spjd 485204076Spjd init_local(res); 486213007Spjd init_environment(); 487213007Spjd 488221899Spjd if (drop_privs(res) != 0) 489218049Spjd exit(EX_CONFIG); 490218214Spjd pjdlog_info("Privileges successfully dropped."); 491218049Spjd 492213007Spjd /* 493213007Spjd * Create the control thread before sending any event to the parent, 494213007Spjd * as we can deadlock when parent sends control request to worker, 495213007Spjd * but worker has no control thread started yet, so parent waits. 496213007Spjd * In the meantime worker sends an event to the parent, but parent 497213007Spjd * is unable to handle the event, because it waits for control 498213007Spjd * request response. 499213007Spjd */ 500213007Spjd error = pthread_create(&td, NULL, ctrl_thread, res); 501218138Spjd PJDLOG_ASSERT(error == 0); 502213007Spjd 503204076Spjd init_remote(res, nvin); 504212038Spjd event_send(res, EVENT_CONNECT); 505204076Spjd 506204076Spjd error = pthread_create(&td, NULL, recv_thread, res); 507218138Spjd PJDLOG_ASSERT(error == 0); 508204076Spjd error = pthread_create(&td, NULL, disk_thread, res); 509218138Spjd PJDLOG_ASSERT(error == 0); 510213007Spjd (void)send_thread(res); 511204076Spjd} 512204076Spjd 513204076Spjdstatic void 514230092Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, 515230092Spjd const char *fmt, ...) 516204076Spjd{ 517204076Spjd char msg[1024]; 518204076Spjd va_list ap; 519204076Spjd int len; 520204076Spjd 521204076Spjd va_start(ap, fmt); 522204076Spjd len = vsnprintf(msg, sizeof(msg), fmt, ap); 523204076Spjd va_end(ap); 524204076Spjd if ((size_t)len < sizeof(msg)) { 525204076Spjd switch (hio->hio_cmd) { 526204076Spjd case HIO_READ: 527204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 528204076Spjd "READ(%ju, %ju).", (uintmax_t)hio->hio_offset, 529204076Spjd (uintmax_t)hio->hio_length); 530204076Spjd break; 531204076Spjd case HIO_DELETE: 532204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 533204076Spjd "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset, 534204076Spjd (uintmax_t)hio->hio_length); 535204076Spjd break; 536204076Spjd case HIO_FLUSH: 537204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 538204076Spjd break; 539204076Spjd case HIO_WRITE: 540204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 541204076Spjd "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset, 542204076Spjd (uintmax_t)hio->hio_length); 543204076Spjd break; 544211882Spjd case HIO_KEEPALIVE: 545211882Spjd (void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE."); 546211882Spjd break; 547204076Spjd default: 548204076Spjd (void)snprintf(msg + len, sizeof(msg) - len, 549204076Spjd "UNKNOWN(%u).", (unsigned int)hio->hio_cmd); 550204076Spjd break; 551204076Spjd } 552204076Spjd } 553204076Spjd pjdlog_common(loglevel, debuglevel, error, "%s", msg); 554204076Spjd} 555204076Spjd 556204076Spjdstatic int 557226854Spjdrequnpack(struct hast_resource *res, struct hio *hio, struct nv *nv) 558204076Spjd{ 559204076Spjd 560226854Spjd hio->hio_cmd = nv_get_uint8(nv, "cmd"); 561204076Spjd if (hio->hio_cmd == 0) { 562204076Spjd pjdlog_error("Header contains no 'cmd' field."); 563204076Spjd hio->hio_error = EINVAL; 564204076Spjd goto end; 565204076Spjd } 566226854Spjd if (hio->hio_cmd != HIO_KEEPALIVE) { 567226854Spjd hio->hio_seq = nv_get_uint64(nv, "seq"); 568226854Spjd if (hio->hio_seq == 0) { 569226854Spjd pjdlog_error("Header contains no 'seq' field."); 570226854Spjd hio->hio_error = EINVAL; 571226854Spjd goto end; 572226854Spjd } 573226854Spjd } 574204076Spjd switch (hio->hio_cmd) { 575222164Spjd case HIO_FLUSH: 576211882Spjd case HIO_KEEPALIVE: 577211882Spjd break; 578246922Spjd case HIO_WRITE: 579246922Spjd hio->hio_memsync = nv_exists(nv, "memsync"); 580246922Spjd /* FALLTHROUGH */ 581204076Spjd case HIO_READ: 582204076Spjd case HIO_DELETE: 583226854Spjd hio->hio_offset = nv_get_uint64(nv, "offset"); 584226854Spjd if (nv_error(nv) != 0) { 585204076Spjd pjdlog_error("Header is missing 'offset' field."); 586204076Spjd hio->hio_error = EINVAL; 587204076Spjd goto end; 588204076Spjd } 589226854Spjd hio->hio_length = nv_get_uint64(nv, "length"); 590226854Spjd if (nv_error(nv) != 0) { 591204076Spjd pjdlog_error("Header is missing 'length' field."); 592204076Spjd hio->hio_error = EINVAL; 593204076Spjd goto end; 594204076Spjd } 595204076Spjd if (hio->hio_length == 0) { 596204076Spjd pjdlog_error("Data length is zero."); 597204076Spjd hio->hio_error = EINVAL; 598204076Spjd goto end; 599204076Spjd } 600248294Spjd if (hio->hio_cmd != HIO_DELETE && hio->hio_length > MAXPHYS) { 601204076Spjd pjdlog_error("Data length is too large (%ju > %ju).", 602204076Spjd (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS); 603204076Spjd hio->hio_error = EINVAL; 604204076Spjd goto end; 605204076Spjd } 606204076Spjd if ((hio->hio_offset % res->hr_local_sectorsize) != 0) { 607204076Spjd pjdlog_error("Offset %ju is not multiple of sector size.", 608204076Spjd (uintmax_t)hio->hio_offset); 609204076Spjd hio->hio_error = EINVAL; 610204076Spjd goto end; 611204076Spjd } 612204076Spjd if ((hio->hio_length % res->hr_local_sectorsize) != 0) { 613204076Spjd pjdlog_error("Length %ju is not multiple of sector size.", 614204076Spjd (uintmax_t)hio->hio_length); 615204076Spjd hio->hio_error = EINVAL; 616204076Spjd goto end; 617204076Spjd } 618204076Spjd if (hio->hio_offset + hio->hio_length > 619204076Spjd (uint64_t)res->hr_datasize) { 620204076Spjd pjdlog_error("Data offset is too large (%ju > %ju).", 621204076Spjd (uintmax_t)(hio->hio_offset + hio->hio_length), 622204076Spjd (uintmax_t)res->hr_datasize); 623204076Spjd hio->hio_error = EINVAL; 624204076Spjd goto end; 625204076Spjd } 626204076Spjd break; 627204076Spjd default: 628204076Spjd pjdlog_error("Header contains invalid 'cmd' (%hhu).", 629204076Spjd hio->hio_cmd); 630204076Spjd hio->hio_error = EINVAL; 631204076Spjd goto end; 632204076Spjd } 633204076Spjd hio->hio_error = 0; 634204076Spjdend: 635204076Spjd return (hio->hio_error); 636204076Spjd} 637204076Spjd 638212899Spjdstatic __dead2 void 639211984Spjdsecondary_exit(int exitcode, const char *fmt, ...) 640211984Spjd{ 641211984Spjd va_list ap; 642211984Spjd 643218138Spjd PJDLOG_ASSERT(exitcode != EX_OK); 644211984Spjd va_start(ap, fmt); 645211984Spjd pjdlogv_errno(LOG_ERR, fmt, ap); 646211984Spjd va_end(ap); 647212038Spjd event_send(gres, EVENT_DISCONNECT); 648211984Spjd exit(exitcode); 649211984Spjd} 650211984Spjd 651204076Spjd/* 652204076Spjd * Thread receives requests from the primary node. 653204076Spjd */ 654204076Spjdstatic void * 655204076Spjdrecv_thread(void *arg) 656204076Spjd{ 657204076Spjd struct hast_resource *res = arg; 658246922Spjd struct hio *hio, *mshio; 659226854Spjd struct nv *nv; 660204076Spjd 661204076Spjd for (;;) { 662204076Spjd pjdlog_debug(2, "recv: Taking free request."); 663211877Spjd QUEUE_TAKE(free, hio); 664204076Spjd pjdlog_debug(2, "recv: (%p) Got request.", hio); 665229945Spjd if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) { 666211984Spjd secondary_exit(EX_TEMPFAIL, 667204076Spjd "Unable to receive request header"); 668204076Spjd } 669226854Spjd if (requnpack(res, hio, nv) != 0) { 670226854Spjd nv_free(nv); 671211877Spjd pjdlog_debug(2, 672211877Spjd "recv: (%p) Moving request to the send queue.", 673211877Spjd hio); 674211877Spjd QUEUE_INSERT(send, hio); 675211877Spjd continue; 676211877Spjd } 677222228Spjd switch (hio->hio_cmd) { 678222228Spjd case HIO_READ: 679222228Spjd res->hr_stat_read++; 680222228Spjd break; 681222228Spjd case HIO_WRITE: 682222228Spjd res->hr_stat_write++; 683222228Spjd break; 684222228Spjd case HIO_DELETE: 685222228Spjd res->hr_stat_delete++; 686222228Spjd break; 687222228Spjd case HIO_FLUSH: 688222228Spjd res->hr_stat_flush++; 689222228Spjd break; 690226854Spjd case HIO_KEEPALIVE: 691226854Spjd break; 692226854Spjd default: 693226854Spjd PJDLOG_ABORT("Unexpected command (cmd=%hhu).", 694226854Spjd hio->hio_cmd); 695222228Spjd } 696204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, 697204076Spjd "recv: (%p) Got request header: ", hio); 698211882Spjd if (hio->hio_cmd == HIO_KEEPALIVE) { 699226854Spjd nv_free(nv); 700211882Spjd pjdlog_debug(2, 701211882Spjd "recv: (%p) Moving request to the free queue.", 702211882Spjd hio); 703226854Spjd hio_clear(hio); 704211882Spjd QUEUE_INSERT(free, hio); 705211882Spjd continue; 706211882Spjd } else if (hio->hio_cmd == HIO_WRITE) { 707226854Spjd if (hast_proto_recv_data(res, res->hr_remotein, nv, 708229945Spjd hio->hio_data, MAXPHYS) == -1) { 709211984Spjd secondary_exit(EX_TEMPFAIL, 710212051Spjd "Unable to receive request data"); 711204076Spjd } 712246922Spjd if (hio->hio_memsync) { 713246922Spjd /* 714246922Spjd * For memsync requests we expect two replies. 715246922Spjd * Clone the hio so we can handle both of them. 716246922Spjd */ 717246922Spjd pjdlog_debug(2, "recv: Taking free request."); 718246922Spjd QUEUE_TAKE(free, mshio); 719246922Spjd pjdlog_debug(2, "recv: (%p) Got request.", 720246922Spjd mshio); 721246922Spjd hio_copy(hio, mshio); 722246922Spjd mshio->hio_error = 0; 723246922Spjd /* 724246922Spjd * We want to keep 'memsync' tag only on the 725246922Spjd * request going onto send queue (mshio). 726246922Spjd */ 727246922Spjd hio->hio_memsync = false; 728246922Spjd pjdlog_debug(2, 729246922Spjd "recv: (%p) Moving memsync request to the send queue.", 730246922Spjd mshio); 731246922Spjd QUEUE_INSERT(send, mshio); 732246922Spjd } 733204076Spjd } 734226854Spjd nv_free(nv); 735204076Spjd pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.", 736204076Spjd hio); 737211877Spjd QUEUE_INSERT(disk, hio); 738204076Spjd } 739204076Spjd /* NOTREACHED */ 740204076Spjd return (NULL); 741204076Spjd} 742204076Spjd 743204076Spjd/* 744204076Spjd * Thread reads from or writes to local component and also handles DELETE and 745204076Spjd * FLUSH requests. 746204076Spjd */ 747204076Spjdstatic void * 748204076Spjddisk_thread(void *arg) 749204076Spjd{ 750204076Spjd struct hast_resource *res = arg; 751204076Spjd struct hio *hio; 752204076Spjd ssize_t ret; 753225832Spjd bool clear_activemap, logerror; 754204076Spjd 755204076Spjd clear_activemap = true; 756204076Spjd 757204076Spjd for (;;) { 758204076Spjd pjdlog_debug(2, "disk: Taking request."); 759211877Spjd QUEUE_TAKE(disk, hio); 760204076Spjd while (clear_activemap) { 761204076Spjd unsigned char *map; 762204076Spjd size_t mapsize; 763204076Spjd 764204076Spjd /* 765204076Spjd * When first request is received, it means that primary 766204076Spjd * already received our activemap, merged it and stored 767204076Spjd * locally. We can now safely clear our activemap. 768204076Spjd */ 769204076Spjd mapsize = 770204076Spjd activemap_calc_ondisk_size(res->hr_local_mediasize - 771204076Spjd METADATA_SIZE, res->hr_extentsize, 772204076Spjd res->hr_local_sectorsize); 773204076Spjd map = calloc(1, mapsize); 774204076Spjd if (map == NULL) { 775204076Spjd pjdlog_warning("Unable to allocate memory to clear local activemap."); 776204076Spjd break; 777204076Spjd } 778204076Spjd if (pwrite(res->hr_localfd, map, mapsize, 779204076Spjd METADATA_SIZE) != (ssize_t)mapsize) { 780204076Spjd pjdlog_errno(LOG_WARNING, 781204076Spjd "Unable to store cleared activemap"); 782204076Spjd free(map); 783247281Strociny res->hr_stat_activemap_write_error++; 784204076Spjd break; 785204076Spjd } 786204076Spjd free(map); 787204076Spjd clear_activemap = false; 788204076Spjd pjdlog_debug(1, "Local activemap cleared."); 789225831Spjd break; 790204076Spjd } 791204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio); 792225832Spjd logerror = true; 793204076Spjd /* Handle the actual request. */ 794204076Spjd switch (hio->hio_cmd) { 795204076Spjd case HIO_READ: 796204076Spjd ret = pread(res->hr_localfd, hio->hio_data, 797204076Spjd hio->hio_length, 798204076Spjd hio->hio_offset + res->hr_localoff); 799229945Spjd if (ret == -1) 800204076Spjd hio->hio_error = errno; 801204076Spjd else if (ret != (int64_t)hio->hio_length) 802204076Spjd hio->hio_error = EIO; 803204076Spjd else 804204076Spjd hio->hio_error = 0; 805204076Spjd break; 806204076Spjd case HIO_WRITE: 807204076Spjd ret = pwrite(res->hr_localfd, hio->hio_data, 808204076Spjd hio->hio_length, 809204076Spjd hio->hio_offset + res->hr_localoff); 810229945Spjd if (ret == -1) 811204076Spjd hio->hio_error = errno; 812204076Spjd else if (ret != (int64_t)hio->hio_length) 813204076Spjd hio->hio_error = EIO; 814204076Spjd else 815204076Spjd hio->hio_error = 0; 816204076Spjd break; 817204076Spjd case HIO_DELETE: 818204076Spjd ret = g_delete(res->hr_localfd, 819204076Spjd hio->hio_offset + res->hr_localoff, 820204076Spjd hio->hio_length); 821229945Spjd if (ret == -1) 822204076Spjd hio->hio_error = errno; 823204076Spjd else 824204076Spjd hio->hio_error = 0; 825204076Spjd break; 826204076Spjd case HIO_FLUSH: 827225832Spjd if (!res->hr_localflush) { 828225832Spjd ret = -1; 829225832Spjd hio->hio_error = EOPNOTSUPP; 830225832Spjd logerror = false; 831225832Spjd break; 832225832Spjd } 833204076Spjd ret = g_flush(res->hr_localfd); 834229945Spjd if (ret == -1) { 835225832Spjd if (errno == EOPNOTSUPP) 836225832Spjd res->hr_localflush = false; 837204076Spjd hio->hio_error = errno; 838225832Spjd } else { 839204076Spjd hio->hio_error = 0; 840225832Spjd } 841204076Spjd break; 842226854Spjd default: 843226854Spjd PJDLOG_ABORT("Unexpected command (cmd=%hhu).", 844226854Spjd hio->hio_cmd); 845204076Spjd } 846225832Spjd if (logerror && hio->hio_error != 0) { 847204076Spjd reqlog(LOG_ERR, 0, hio->hio_error, hio, 848204076Spjd "Request failed: "); 849204076Spjd } 850204076Spjd pjdlog_debug(2, "disk: (%p) Moving request to the send queue.", 851204076Spjd hio); 852211877Spjd QUEUE_INSERT(send, hio); 853204076Spjd } 854204076Spjd /* NOTREACHED */ 855204076Spjd return (NULL); 856204076Spjd} 857204076Spjd 858204076Spjd/* 859204076Spjd * Thread sends requests back to primary node. 860204076Spjd */ 861204076Spjdstatic void * 862204076Spjdsend_thread(void *arg) 863204076Spjd{ 864204076Spjd struct hast_resource *res = arg; 865204076Spjd struct nv *nvout; 866204076Spjd struct hio *hio; 867204076Spjd void *data; 868204076Spjd size_t length; 869204076Spjd 870204076Spjd for (;;) { 871204076Spjd pjdlog_debug(2, "send: Taking request."); 872211877Spjd QUEUE_TAKE(send, hio); 873204076Spjd reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio); 874204076Spjd nvout = nv_alloc(); 875204076Spjd /* Copy sequence number. */ 876226854Spjd nv_add_uint64(nvout, hio->hio_seq, "seq"); 877246922Spjd if (hio->hio_memsync) { 878246922Spjd PJDLOG_ASSERT(hio->hio_cmd == HIO_WRITE); 879246922Spjd nv_add_int8(nvout, 1, "received"); 880246922Spjd } 881204076Spjd switch (hio->hio_cmd) { 882204076Spjd case HIO_READ: 883204076Spjd if (hio->hio_error == 0) { 884204076Spjd data = hio->hio_data; 885204076Spjd length = hio->hio_length; 886204076Spjd break; 887204076Spjd } 888204076Spjd /* 889204076Spjd * We send no data in case of an error. 890204076Spjd */ 891204076Spjd /* FALLTHROUGH */ 892204076Spjd case HIO_DELETE: 893204076Spjd case HIO_FLUSH: 894204076Spjd case HIO_WRITE: 895204076Spjd data = NULL; 896204076Spjd length = 0; 897204076Spjd break; 898204076Spjd default: 899225782Spjd PJDLOG_ABORT("Unexpected command (cmd=%hhu).", 900225782Spjd hio->hio_cmd); 901204076Spjd } 902247281Strociny if (hio->hio_error != 0) { 903247281Strociny switch (hio->hio_cmd) { 904247281Strociny case HIO_READ: 905247281Strociny res->hr_stat_read_error++; 906247281Strociny break; 907247281Strociny case HIO_WRITE: 908247281Strociny res->hr_stat_write_error++; 909247281Strociny break; 910247281Strociny case HIO_DELETE: 911247281Strociny res->hr_stat_delete_error++; 912247281Strociny break; 913247281Strociny case HIO_FLUSH: 914247281Strociny res->hr_stat_flush_error++; 915247281Strociny break; 916247281Strociny } 917204076Spjd nv_add_int16(nvout, hio->hio_error, "error"); 918247281Strociny } 919204076Spjd if (hast_proto_send(res, res->hr_remoteout, nvout, data, 920229945Spjd length) == -1) { 921230092Spjd secondary_exit(EX_TEMPFAIL, "Unable to send reply"); 922204076Spjd } 923204076Spjd nv_free(nvout); 924209185Spjd pjdlog_debug(2, "send: (%p) Moving request to the free queue.", 925204076Spjd hio); 926226854Spjd hio_clear(hio); 927211877Spjd QUEUE_INSERT(free, hio); 928204076Spjd } 929204076Spjd /* NOTREACHED */ 930204076Spjd return (NULL); 931204076Spjd} 932