132115b10SPawel Jakub Dawidek /*- 232115b10SPawel Jakub Dawidek * Copyright (c) 2009 The FreeBSD Foundation 31fee97b0SPawel Jakub Dawidek * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 432115b10SPawel Jakub Dawidek * All rights reserved. 532115b10SPawel Jakub Dawidek * 632115b10SPawel Jakub Dawidek * This software was developed by Pawel Jakub Dawidek under sponsorship from 732115b10SPawel Jakub Dawidek * the FreeBSD Foundation. 832115b10SPawel Jakub Dawidek * 932115b10SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 1032115b10SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 1132115b10SPawel Jakub Dawidek * are met: 1232115b10SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 1332115b10SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 1432115b10SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 1532115b10SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 1632115b10SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 1732115b10SPawel Jakub Dawidek * 1832115b10SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 1932115b10SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2032115b10SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2132115b10SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 2232115b10SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2332115b10SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2432115b10SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2532115b10SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2632115b10SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2732115b10SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2832115b10SPawel Jakub Dawidek * SUCH DAMAGE. 2932115b10SPawel Jakub Dawidek */ 3032115b10SPawel Jakub Dawidek 3132115b10SPawel Jakub Dawidek #include <sys/cdefs.h> 3232115b10SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 3332115b10SPawel Jakub Dawidek 3432115b10SPawel Jakub Dawidek #include <sys/types.h> 3532115b10SPawel Jakub Dawidek #include <sys/time.h> 3632115b10SPawel Jakub Dawidek #include <sys/bio.h> 3732115b10SPawel Jakub Dawidek #include <sys/disk.h> 3832115b10SPawel Jakub Dawidek #include <sys/refcount.h> 3932115b10SPawel Jakub Dawidek #include <sys/stat.h> 4032115b10SPawel Jakub Dawidek 4132115b10SPawel Jakub Dawidek #include <geom/gate/g_gate.h> 4232115b10SPawel Jakub Dawidek 4332115b10SPawel Jakub Dawidek #include <err.h> 4432115b10SPawel Jakub Dawidek #include <errno.h> 4532115b10SPawel Jakub Dawidek #include <fcntl.h> 4632115b10SPawel Jakub Dawidek #include <libgeom.h> 4732115b10SPawel Jakub Dawidek #include <pthread.h> 486d0c801eSPawel Jakub Dawidek #include <signal.h> 4932115b10SPawel Jakub Dawidek #include <stdint.h> 5032115b10SPawel Jakub Dawidek #include <stdio.h> 5132115b10SPawel Jakub Dawidek #include <string.h> 5232115b10SPawel Jakub Dawidek #include <sysexits.h> 5332115b10SPawel Jakub Dawidek #include <unistd.h> 5432115b10SPawel Jakub Dawidek 5532115b10SPawel Jakub Dawidek #include <activemap.h> 5632115b10SPawel Jakub Dawidek #include <nv.h> 5732115b10SPawel Jakub Dawidek #include <rangelock.h> 5832115b10SPawel Jakub Dawidek 5932115b10SPawel Jakub Dawidek #include "control.h" 605bdff860SPawel Jakub Dawidek #include "event.h" 6132115b10SPawel Jakub Dawidek #include "hast.h" 6232115b10SPawel Jakub Dawidek #include "hast_proto.h" 6332115b10SPawel Jakub Dawidek #include "hastd.h" 640becad39SPawel Jakub Dawidek #include "hooks.h" 6532115b10SPawel Jakub Dawidek #include "metadata.h" 6632115b10SPawel Jakub Dawidek #include "proto.h" 6732115b10SPawel Jakub Dawidek #include "pjdlog.h" 6832115b10SPawel Jakub Dawidek #include "subr.h" 6932115b10SPawel Jakub Dawidek #include "synch.h" 7032115b10SPawel Jakub Dawidek 710989854dSPawel Jakub Dawidek /* The is only one remote component for now. */ 720989854dSPawel Jakub Dawidek #define ISREMOTE(no) ((no) == 1) 730989854dSPawel Jakub Dawidek 7432115b10SPawel Jakub Dawidek struct hio { 7532115b10SPawel Jakub Dawidek /* 7632115b10SPawel Jakub Dawidek * Number of components we are still waiting for. 7732115b10SPawel Jakub Dawidek * When this field goes to 0, we can send the request back to the 7832115b10SPawel Jakub Dawidek * kernel. Each component has to decrease this counter by one 7932115b10SPawel Jakub Dawidek * even on failure. 8032115b10SPawel Jakub Dawidek */ 8132115b10SPawel Jakub Dawidek unsigned int hio_countdown; 8232115b10SPawel Jakub Dawidek /* 8332115b10SPawel Jakub Dawidek * Each component has a place to store its own error. 8432115b10SPawel Jakub Dawidek * Once the request is handled by all components we can decide if the 8532115b10SPawel Jakub Dawidek * request overall is successful or not. 8632115b10SPawel Jakub Dawidek */ 8732115b10SPawel Jakub Dawidek int *hio_errors; 8832115b10SPawel Jakub Dawidek /* 890b626a28SPawel Jakub Dawidek * Structure used to communicate with GEOM Gate class. 9032115b10SPawel Jakub Dawidek */ 9132115b10SPawel Jakub Dawidek struct g_gate_ctl_io hio_ggio; 9232115b10SPawel Jakub Dawidek TAILQ_ENTRY(hio) *hio_next; 9332115b10SPawel Jakub Dawidek }; 9432115b10SPawel Jakub Dawidek #define hio_free_next hio_next[0] 9532115b10SPawel Jakub Dawidek #define hio_done_next hio_next[0] 9632115b10SPawel Jakub Dawidek 9732115b10SPawel Jakub Dawidek /* 9832115b10SPawel Jakub Dawidek * Free list holds unused structures. When free list is empty, we have to wait 9932115b10SPawel Jakub Dawidek * until some in-progress requests are freed. 10032115b10SPawel Jakub Dawidek */ 10132115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_free_list; 10232115b10SPawel Jakub Dawidek static pthread_mutex_t hio_free_list_lock; 10332115b10SPawel Jakub Dawidek static pthread_cond_t hio_free_list_cond; 10432115b10SPawel Jakub Dawidek /* 10532115b10SPawel Jakub Dawidek * There is one send list for every component. One requests is placed on all 10632115b10SPawel Jakub Dawidek * send lists - each component gets the same request, but each component is 10732115b10SPawel Jakub Dawidek * responsible for managing his own send list. 10832115b10SPawel Jakub Dawidek */ 10932115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_send_list; 11032115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_send_list_lock; 11132115b10SPawel Jakub Dawidek static pthread_cond_t *hio_send_list_cond; 11232115b10SPawel Jakub Dawidek /* 11332115b10SPawel Jakub Dawidek * There is one recv list for every component, although local components don't 11432115b10SPawel Jakub Dawidek * use recv lists as local requests are done synchronously. 11532115b10SPawel Jakub Dawidek */ 11632115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_recv_list; 11732115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_recv_list_lock; 11832115b10SPawel Jakub Dawidek static pthread_cond_t *hio_recv_list_cond; 11932115b10SPawel Jakub Dawidek /* 12032115b10SPawel Jakub Dawidek * Request is placed on done list by the slowest component (the one that 12132115b10SPawel Jakub Dawidek * decreased hio_countdown from 1 to 0). 12232115b10SPawel Jakub Dawidek */ 12332115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_done_list; 12432115b10SPawel Jakub Dawidek static pthread_mutex_t hio_done_list_lock; 12532115b10SPawel Jakub Dawidek static pthread_cond_t hio_done_list_cond; 12632115b10SPawel Jakub Dawidek /* 12732115b10SPawel Jakub Dawidek * Structure below are for interaction with sync thread. 12832115b10SPawel Jakub Dawidek */ 12932115b10SPawel Jakub Dawidek static bool sync_inprogress; 13032115b10SPawel Jakub Dawidek static pthread_mutex_t sync_lock; 13132115b10SPawel Jakub Dawidek static pthread_cond_t sync_cond; 13232115b10SPawel Jakub Dawidek /* 13332115b10SPawel Jakub Dawidek * The lock below allows to synchornize access to remote connections. 13432115b10SPawel Jakub Dawidek */ 13532115b10SPawel Jakub Dawidek static pthread_rwlock_t *hio_remote_lock; 13632115b10SPawel Jakub Dawidek 13732115b10SPawel Jakub Dawidek /* 13832115b10SPawel Jakub Dawidek * Lock to synchronize metadata updates. Also synchronize access to 13932115b10SPawel Jakub Dawidek * hr_primary_localcnt and hr_primary_remotecnt fields. 14032115b10SPawel Jakub Dawidek */ 14132115b10SPawel Jakub Dawidek static pthread_mutex_t metadata_lock; 14232115b10SPawel Jakub Dawidek 14332115b10SPawel Jakub Dawidek /* 14432115b10SPawel Jakub Dawidek * Maximum number of outstanding I/O requests. 14532115b10SPawel Jakub Dawidek */ 14632115b10SPawel Jakub Dawidek #define HAST_HIO_MAX 256 14732115b10SPawel Jakub Dawidek /* 14832115b10SPawel Jakub Dawidek * Number of components. At this point there are only two components: local 14932115b10SPawel Jakub Dawidek * and remote, but in the future it might be possible to use multiple local 15032115b10SPawel Jakub Dawidek * and remote components. 15132115b10SPawel Jakub Dawidek */ 15232115b10SPawel Jakub Dawidek #define HAST_NCOMPONENTS 2 15332115b10SPawel Jakub Dawidek 15432115b10SPawel Jakub Dawidek #define ISCONNECTED(res, no) \ 15532115b10SPawel Jakub Dawidek ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL) 15632115b10SPawel Jakub Dawidek 15732115b10SPawel Jakub Dawidek #define QUEUE_INSERT1(hio, name, ncomp) do { \ 15832115b10SPawel Jakub Dawidek bool _wakeup; \ 15932115b10SPawel Jakub Dawidek \ 16032115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 16132115b10SPawel Jakub Dawidek _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \ 16232115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \ 16332115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \ 16432115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock[ncomp]); \ 16532115b10SPawel Jakub Dawidek if (_wakeup) \ 16632115b10SPawel Jakub Dawidek cv_signal(&hio_##name##_list_cond[(ncomp)]); \ 16732115b10SPawel Jakub Dawidek } while (0) 16832115b10SPawel Jakub Dawidek #define QUEUE_INSERT2(hio, name) do { \ 16932115b10SPawel Jakub Dawidek bool _wakeup; \ 17032115b10SPawel Jakub Dawidek \ 17132115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \ 17232115b10SPawel Jakub Dawidek _wakeup = TAILQ_EMPTY(&hio_##name##_list); \ 17332115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\ 17432115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \ 17532115b10SPawel Jakub Dawidek if (_wakeup) \ 17632115b10SPawel Jakub Dawidek cv_signal(&hio_##name##_list_cond); \ 17732115b10SPawel Jakub Dawidek } while (0) 178448efa94SPawel Jakub Dawidek #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \ 179448efa94SPawel Jakub Dawidek bool _last; \ 180448efa94SPawel Jakub Dawidek \ 18132115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 182448efa94SPawel Jakub Dawidek _last = false; \ 183448efa94SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \ 184448efa94SPawel Jakub Dawidek cv_timedwait(&hio_##name##_list_cond[(ncomp)], \ 185448efa94SPawel Jakub Dawidek &hio_##name##_list_lock[(ncomp)], (timeout)); \ 186448efa94SPawel Jakub Dawidek if ((timeout) != 0) \ 187448efa94SPawel Jakub Dawidek _last = true; \ 18832115b10SPawel Jakub Dawidek } \ 189448efa94SPawel Jakub Dawidek if (hio != NULL) { \ 19032115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \ 19132115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \ 192448efa94SPawel Jakub Dawidek } \ 19332115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \ 19432115b10SPawel Jakub Dawidek } while (0) 19532115b10SPawel Jakub Dawidek #define QUEUE_TAKE2(hio, name) do { \ 19632115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \ 19732115b10SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 19832115b10SPawel Jakub Dawidek cv_wait(&hio_##name##_list_cond, \ 19932115b10SPawel Jakub Dawidek &hio_##name##_list_lock); \ 20032115b10SPawel Jakub Dawidek } \ 20132115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \ 20232115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \ 20332115b10SPawel Jakub Dawidek } while (0) 20432115b10SPawel Jakub Dawidek 205328e0f4bSPawel Jakub Dawidek #define SYNCREQ(hio) do { \ 206328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_unit = -1; \ 207328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_seq = 1; \ 208328e0f4bSPawel Jakub Dawidek } while (0) 20932115b10SPawel Jakub Dawidek #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1) 21032115b10SPawel Jakub Dawidek #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0) 21132115b10SPawel Jakub Dawidek #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2) 21232115b10SPawel Jakub Dawidek 21332115b10SPawel Jakub Dawidek static struct hast_resource *gres; 21432115b10SPawel Jakub Dawidek 21532115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock; 21632115b10SPawel Jakub Dawidek static struct rangelocks *range_regular; 21732115b10SPawel Jakub Dawidek static bool range_regular_wait; 21832115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond; 21932115b10SPawel Jakub Dawidek static struct rangelocks *range_sync; 22032115b10SPawel Jakub Dawidek static bool range_sync_wait; 22132115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond; 222ac0401e3SPawel Jakub Dawidek static bool fullystarted; 22332115b10SPawel Jakub Dawidek 22432115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg); 22532115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg); 22632115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg); 22732115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg); 22832115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg); 22932115b10SPawel Jakub Dawidek static void *sync_thread(void *arg); 23032115b10SPawel Jakub Dawidek static void *guard_thread(void *arg); 23132115b10SPawel Jakub Dawidek 2326d0c801eSPawel Jakub Dawidek static void 23332115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res) 23432115b10SPawel Jakub Dawidek { 23532115b10SPawel Jakub Dawidek int rerrno; 23632115b10SPawel Jakub Dawidek 23732115b10SPawel Jakub Dawidek /* Remember errno. */ 23832115b10SPawel Jakub Dawidek rerrno = errno; 23932115b10SPawel Jakub Dawidek 24032115b10SPawel Jakub Dawidek /* Destroy ggate provider if we created one. */ 24132115b10SPawel Jakub Dawidek if (res->hr_ggateunit >= 0) { 24232115b10SPawel Jakub Dawidek struct g_gate_ctl_destroy ggiod; 24332115b10SPawel Jakub Dawidek 2444e47b646SPawel Jakub Dawidek bzero(&ggiod, sizeof(ggiod)); 24532115b10SPawel Jakub Dawidek ggiod.gctl_version = G_GATE_VERSION; 24632115b10SPawel Jakub Dawidek ggiod.gctl_unit = res->hr_ggateunit; 24732115b10SPawel Jakub Dawidek ggiod.gctl_force = 1; 24832115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) { 249783ee753SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 250783ee753SPawel Jakub Dawidek "Unable to destroy hast/%s device", 25132115b10SPawel Jakub Dawidek res->hr_provname); 25232115b10SPawel Jakub Dawidek } 25332115b10SPawel Jakub Dawidek res->hr_ggateunit = -1; 25432115b10SPawel Jakub Dawidek } 25532115b10SPawel Jakub Dawidek 25632115b10SPawel Jakub Dawidek /* Restore errno. */ 25732115b10SPawel Jakub Dawidek errno = rerrno; 25832115b10SPawel Jakub Dawidek } 25932115b10SPawel Jakub Dawidek 260e43e02f1SPawel Jakub Dawidek static __dead2 void 26132115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...) 26232115b10SPawel Jakub Dawidek { 26332115b10SPawel Jakub Dawidek va_list ap; 26432115b10SPawel Jakub Dawidek 2652ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(exitcode != EX_OK); 26632115b10SPawel Jakub Dawidek va_start(ap, fmt); 26732115b10SPawel Jakub Dawidek pjdlogv_errno(LOG_ERR, fmt, ap); 26832115b10SPawel Jakub Dawidek va_end(ap); 26932115b10SPawel Jakub Dawidek cleanup(gres); 27032115b10SPawel Jakub Dawidek exit(exitcode); 27132115b10SPawel Jakub Dawidek } 27232115b10SPawel Jakub Dawidek 273e43e02f1SPawel Jakub Dawidek static __dead2 void 27432115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...) 27532115b10SPawel Jakub Dawidek { 27632115b10SPawel Jakub Dawidek va_list ap; 27732115b10SPawel Jakub Dawidek 27832115b10SPawel Jakub Dawidek va_start(ap, fmt); 27932115b10SPawel Jakub Dawidek pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap); 28032115b10SPawel Jakub Dawidek va_end(ap); 28132115b10SPawel Jakub Dawidek cleanup(gres); 28232115b10SPawel Jakub Dawidek exit(exitcode); 28332115b10SPawel Jakub Dawidek } 28432115b10SPawel Jakub Dawidek 28532115b10SPawel Jakub Dawidek static int 28632115b10SPawel Jakub Dawidek hast_activemap_flush(struct hast_resource *res) 28732115b10SPawel Jakub Dawidek { 28832115b10SPawel Jakub Dawidek const unsigned char *buf; 28932115b10SPawel Jakub Dawidek size_t size; 29032115b10SPawel Jakub Dawidek 29132115b10SPawel Jakub Dawidek buf = activemap_bitmap(res->hr_amp, &size); 2922ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(buf != NULL); 2932ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0); 29432115b10SPawel Jakub Dawidek if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) != 29532115b10SPawel Jakub Dawidek (ssize_t)size) { 296*be1143efSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk"); 29732115b10SPawel Jakub Dawidek return (-1); 29832115b10SPawel Jakub Dawidek } 29932115b10SPawel Jakub Dawidek return (0); 30032115b10SPawel Jakub Dawidek } 30132115b10SPawel Jakub Dawidek 302f377917cSPawel Jakub Dawidek static bool 303f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res) 304f377917cSPawel Jakub Dawidek { 305f377917cSPawel Jakub Dawidek 306f377917cSPawel Jakub Dawidek return (strcmp(res->hr_remoteaddr, "none") != 0); 307f377917cSPawel Jakub Dawidek } 308f377917cSPawel Jakub Dawidek 30932115b10SPawel Jakub Dawidek static void 31032115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused) 31132115b10SPawel Jakub Dawidek { 31232115b10SPawel Jakub Dawidek struct hio *hio; 31332115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 31432115b10SPawel Jakub Dawidek 31532115b10SPawel Jakub Dawidek /* 31632115b10SPawel Jakub Dawidek * In the future it might be per-resource value. 31732115b10SPawel Jakub Dawidek */ 31832115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 31932115b10SPawel Jakub Dawidek 32032115b10SPawel Jakub Dawidek /* 32132115b10SPawel Jakub Dawidek * Allocate memory needed by lists. 32232115b10SPawel Jakub Dawidek */ 32332115b10SPawel Jakub Dawidek hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps); 32432115b10SPawel Jakub Dawidek if (hio_send_list == NULL) { 32532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 32632115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send lists.", 32732115b10SPawel Jakub Dawidek sizeof(hio_send_list[0]) * ncomps); 32832115b10SPawel Jakub Dawidek } 32932115b10SPawel Jakub Dawidek hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps); 33032115b10SPawel Jakub Dawidek if (hio_send_list_lock == NULL) { 33132115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 33232115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list locks.", 33332115b10SPawel Jakub Dawidek sizeof(hio_send_list_lock[0]) * ncomps); 33432115b10SPawel Jakub Dawidek } 33532115b10SPawel Jakub Dawidek hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps); 33632115b10SPawel Jakub Dawidek if (hio_send_list_cond == NULL) { 33732115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 33832115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list condition variables.", 33932115b10SPawel Jakub Dawidek sizeof(hio_send_list_cond[0]) * ncomps); 34032115b10SPawel Jakub Dawidek } 34132115b10SPawel Jakub Dawidek hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps); 34232115b10SPawel Jakub Dawidek if (hio_recv_list == NULL) { 34332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 34432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv lists.", 34532115b10SPawel Jakub Dawidek sizeof(hio_recv_list[0]) * ncomps); 34632115b10SPawel Jakub Dawidek } 34732115b10SPawel Jakub Dawidek hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps); 34832115b10SPawel Jakub Dawidek if (hio_recv_list_lock == NULL) { 34932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 35032115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list locks.", 35132115b10SPawel Jakub Dawidek sizeof(hio_recv_list_lock[0]) * ncomps); 35232115b10SPawel Jakub Dawidek } 35332115b10SPawel Jakub Dawidek hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps); 35432115b10SPawel Jakub Dawidek if (hio_recv_list_cond == NULL) { 35532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 35632115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list condition variables.", 35732115b10SPawel Jakub Dawidek sizeof(hio_recv_list_cond[0]) * ncomps); 35832115b10SPawel Jakub Dawidek } 35932115b10SPawel Jakub Dawidek hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps); 36032115b10SPawel Jakub Dawidek if (hio_remote_lock == NULL) { 36132115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 36232115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for remote connections locks.", 36332115b10SPawel Jakub Dawidek sizeof(hio_remote_lock[0]) * ncomps); 36432115b10SPawel Jakub Dawidek } 36532115b10SPawel Jakub Dawidek 36632115b10SPawel Jakub Dawidek /* 36732115b10SPawel Jakub Dawidek * Initialize lists, their locks and theirs condition variables. 36832115b10SPawel Jakub Dawidek */ 36932115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_free_list); 37032115b10SPawel Jakub Dawidek mtx_init(&hio_free_list_lock); 37132115b10SPawel Jakub Dawidek cv_init(&hio_free_list_cond); 37232115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_NCOMPONENTS; ii++) { 37332115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_send_list[ii]); 37432115b10SPawel Jakub Dawidek mtx_init(&hio_send_list_lock[ii]); 37532115b10SPawel Jakub Dawidek cv_init(&hio_send_list_cond[ii]); 37632115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_recv_list[ii]); 37732115b10SPawel Jakub Dawidek mtx_init(&hio_recv_list_lock[ii]); 37832115b10SPawel Jakub Dawidek cv_init(&hio_recv_list_cond[ii]); 37932115b10SPawel Jakub Dawidek rw_init(&hio_remote_lock[ii]); 38032115b10SPawel Jakub Dawidek } 38132115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_done_list); 38232115b10SPawel Jakub Dawidek mtx_init(&hio_done_list_lock); 38332115b10SPawel Jakub Dawidek cv_init(&hio_done_list_cond); 38432115b10SPawel Jakub Dawidek mtx_init(&metadata_lock); 38532115b10SPawel Jakub Dawidek 38632115b10SPawel Jakub Dawidek /* 38732115b10SPawel Jakub Dawidek * Allocate requests pool and initialize requests. 38832115b10SPawel Jakub Dawidek */ 38932115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_HIO_MAX; ii++) { 39032115b10SPawel Jakub Dawidek hio = malloc(sizeof(*hio)); 39132115b10SPawel Jakub Dawidek if (hio == NULL) { 39232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 39332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for hio request.", 39432115b10SPawel Jakub Dawidek sizeof(*hio)); 39532115b10SPawel Jakub Dawidek } 39632115b10SPawel Jakub Dawidek hio->hio_countdown = 0; 39732115b10SPawel Jakub Dawidek hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps); 39832115b10SPawel Jakub Dawidek if (hio->hio_errors == NULL) { 39932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 40032115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio errors.", 40132115b10SPawel Jakub Dawidek sizeof(hio->hio_errors[0]) * ncomps); 40232115b10SPawel Jakub Dawidek } 40332115b10SPawel Jakub Dawidek hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps); 40432115b10SPawel Jakub Dawidek if (hio->hio_next == NULL) { 40532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 40632115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio_next field.", 40732115b10SPawel Jakub Dawidek sizeof(hio->hio_next[0]) * ncomps); 40832115b10SPawel Jakub Dawidek } 40932115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_version = G_GATE_VERSION; 41032115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_data = malloc(MAXPHYS); 41132115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_data == NULL) { 41232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 41332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for gctl_data.", 41432115b10SPawel Jakub Dawidek MAXPHYS); 41532115b10SPawel Jakub Dawidek } 41632115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_length = MAXPHYS; 41732115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_error = 0; 41832115b10SPawel Jakub Dawidek TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next); 41932115b10SPawel Jakub Dawidek } 42032115b10SPawel Jakub Dawidek } 42132115b10SPawel Jakub Dawidek 422ce837469SPawel Jakub Dawidek static bool 423ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res) 424ce837469SPawel Jakub Dawidek { 425ce837469SPawel Jakub Dawidek 426ce837469SPawel Jakub Dawidek mtx_lock(&metadata_lock); 427ce837469SPawel Jakub Dawidek if (res->hr_resuid != 0) { 428ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 429ce837469SPawel Jakub Dawidek return (false); 430ce837469SPawel Jakub Dawidek } else { 431ce837469SPawel Jakub Dawidek /* Initialize unique resource identifier. */ 432ce837469SPawel Jakub Dawidek arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid)); 433ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 434ce837469SPawel Jakub Dawidek if (metadata_write(res) < 0) 435ce837469SPawel Jakub Dawidek exit(EX_NOINPUT); 436ce837469SPawel Jakub Dawidek return (true); 437ce837469SPawel Jakub Dawidek } 438ce837469SPawel Jakub Dawidek } 439ce837469SPawel Jakub Dawidek 44032115b10SPawel Jakub Dawidek static void 44132115b10SPawel Jakub Dawidek init_local(struct hast_resource *res) 44232115b10SPawel Jakub Dawidek { 44332115b10SPawel Jakub Dawidek unsigned char *buf; 44432115b10SPawel Jakub Dawidek size_t mapsize; 44532115b10SPawel Jakub Dawidek 44632115b10SPawel Jakub Dawidek if (metadata_read(res, true) < 0) 44732115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 44832115b10SPawel Jakub Dawidek mtx_init(&res->hr_amp_lock); 44932115b10SPawel Jakub Dawidek if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 45032115b10SPawel Jakub Dawidek res->hr_local_sectorsize, res->hr_keepdirty) < 0) { 45132115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 45232115b10SPawel Jakub Dawidek } 45332115b10SPawel Jakub Dawidek mtx_init(&range_lock); 45432115b10SPawel Jakub Dawidek cv_init(&range_regular_cond); 45532115b10SPawel Jakub Dawidek if (rangelock_init(&range_regular) < 0) 45632115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 45732115b10SPawel Jakub Dawidek cv_init(&range_sync_cond); 45832115b10SPawel Jakub Dawidek if (rangelock_init(&range_sync) < 0) 45932115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create sync range lock"); 46032115b10SPawel Jakub Dawidek mapsize = activemap_ondisk_size(res->hr_amp); 46132115b10SPawel Jakub Dawidek buf = calloc(1, mapsize); 46232115b10SPawel Jakub Dawidek if (buf == NULL) { 46332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 46432115b10SPawel Jakub Dawidek "Unable to allocate buffer for activemap."); 46532115b10SPawel Jakub Dawidek } 46632115b10SPawel Jakub Dawidek if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 46732115b10SPawel Jakub Dawidek (ssize_t)mapsize) { 46832115b10SPawel Jakub Dawidek primary_exit(EX_NOINPUT, "Unable to read activemap"); 46932115b10SPawel Jakub Dawidek } 47032115b10SPawel Jakub Dawidek activemap_copyin(res->hr_amp, buf, mapsize); 471b0dfbe5bSPawel Jakub Dawidek free(buf); 47232115b10SPawel Jakub Dawidek if (res->hr_resuid != 0) 47332115b10SPawel Jakub Dawidek return; 47432115b10SPawel Jakub Dawidek /* 475ce837469SPawel Jakub Dawidek * We're using provider for the first time. Initialize local and remote 476ce837469SPawel Jakub Dawidek * counters. We don't initialize resuid here, as we want to do it just 477ce837469SPawel Jakub Dawidek * in time. The reason for this is that we want to inform secondary 478ce837469SPawel Jakub Dawidek * that there were no writes yet, so there is no need to synchronize 479ce837469SPawel Jakub Dawidek * anything. 48032115b10SPawel Jakub Dawidek */ 4819446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 0; 48232115b10SPawel Jakub Dawidek res->hr_primary_remotecnt = 0; 48332115b10SPawel Jakub Dawidek if (metadata_write(res) < 0) 48432115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 48532115b10SPawel Jakub Dawidek } 48632115b10SPawel Jakub Dawidek 48732ecf620SPawel Jakub Dawidek static int 48832ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp) 48932ecf620SPawel Jakub Dawidek { 49032ecf620SPawel Jakub Dawidek struct proto_conn *conn; 49132ecf620SPawel Jakub Dawidek int16_t val; 49232ecf620SPawel Jakub Dawidek 49332ecf620SPawel Jakub Dawidek val = 1; 49432ecf620SPawel Jakub Dawidek if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) { 49532ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 49632ecf620SPawel Jakub Dawidek "Unable to send connection request to parent"); 49732ecf620SPawel Jakub Dawidek } 49832ecf620SPawel Jakub Dawidek if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) { 49932ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 50032ecf620SPawel Jakub Dawidek "Unable to receive reply to connection request from parent"); 50132ecf620SPawel Jakub Dawidek } 50232ecf620SPawel Jakub Dawidek if (val != 0) { 50332ecf620SPawel Jakub Dawidek errno = val; 50432ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 50532ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 50632ecf620SPawel Jakub Dawidek return (-1); 50732ecf620SPawel Jakub Dawidek } 50832ecf620SPawel Jakub Dawidek if (proto_connection_recv(res->hr_conn, true, &conn) < 0) { 50932ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 51032ecf620SPawel Jakub Dawidek "Unable to receive connection from parent"); 51132ecf620SPawel Jakub Dawidek } 5127d4df5cdSPawel Jakub Dawidek if (proto_connect_wait(conn, res->hr_timeout) < 0) { 51332ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 51432ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 51532ecf620SPawel Jakub Dawidek proto_close(conn); 51632ecf620SPawel Jakub Dawidek return (-1); 51732ecf620SPawel Jakub Dawidek } 51832ecf620SPawel Jakub Dawidek /* Error in setting timeout is not critical, but why should it fail? */ 51932ecf620SPawel Jakub Dawidek if (proto_timeout(conn, res->hr_timeout) < 0) 52032ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 52132ecf620SPawel Jakub Dawidek 52232ecf620SPawel Jakub Dawidek *connp = conn; 52332ecf620SPawel Jakub Dawidek 52432ecf620SPawel Jakub Dawidek return (0); 52532ecf620SPawel Jakub Dawidek } 52632ecf620SPawel Jakub Dawidek 527ac0401e3SPawel Jakub Dawidek static int 5280d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp, 5290d9014f3SPawel Jakub Dawidek struct proto_conn **outp) 53032115b10SPawel Jakub Dawidek { 5310d9014f3SPawel Jakub Dawidek struct proto_conn *in, *out; 53232115b10SPawel Jakub Dawidek struct nv *nvout, *nvin; 53332115b10SPawel Jakub Dawidek const unsigned char *token; 53432115b10SPawel Jakub Dawidek unsigned char *map; 53532115b10SPawel Jakub Dawidek const char *errmsg; 53632115b10SPawel Jakub Dawidek int32_t extentsize; 53732115b10SPawel Jakub Dawidek int64_t datasize; 53832115b10SPawel Jakub Dawidek uint32_t mapsize; 53932115b10SPawel Jakub Dawidek size_t size; 540ac0401e3SPawel Jakub Dawidek int error; 54132115b10SPawel Jakub Dawidek 5422ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 5432ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(real_remote(res)); 5440d9014f3SPawel Jakub Dawidek 5450d9014f3SPawel Jakub Dawidek in = out = NULL; 5462be8fd75SPawel Jakub Dawidek errmsg = NULL; 5470d9014f3SPawel Jakub Dawidek 54832ecf620SPawel Jakub Dawidek if (primary_connect(res, &out) == -1) 549ac0401e3SPawel Jakub Dawidek return (ECONNREFUSED); 550ac0401e3SPawel Jakub Dawidek 551ac0401e3SPawel Jakub Dawidek error = ECONNABORTED; 55232ecf620SPawel Jakub Dawidek 55332115b10SPawel Jakub Dawidek /* 55432115b10SPawel Jakub Dawidek * First handshake step. 55532115b10SPawel Jakub Dawidek * Setup outgoing connection with remote node. 55632115b10SPawel Jakub Dawidek */ 55732115b10SPawel Jakub Dawidek nvout = nv_alloc(); 55832115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 55932115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 56032115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 56132115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 56232115b10SPawel Jakub Dawidek res->hr_remoteaddr); 56332115b10SPawel Jakub Dawidek nv_free(nvout); 56432115b10SPawel Jakub Dawidek goto close; 56532115b10SPawel Jakub Dawidek } 5660d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, out, nvout, NULL, 0) < 0) { 56732115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 56832115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 56932115b10SPawel Jakub Dawidek res->hr_remoteaddr); 57032115b10SPawel Jakub Dawidek nv_free(nvout); 57132115b10SPawel Jakub Dawidek goto close; 57232115b10SPawel Jakub Dawidek } 57332115b10SPawel Jakub Dawidek nv_free(nvout); 5740d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 57532115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 57632115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 57732115b10SPawel Jakub Dawidek res->hr_remoteaddr); 57832115b10SPawel Jakub Dawidek goto close; 57932115b10SPawel Jakub Dawidek } 58032115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 58132115b10SPawel Jakub Dawidek if (errmsg != NULL) { 58232115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 583ac0401e3SPawel Jakub Dawidek if (nv_exists(nvin, "wait")) 584ac0401e3SPawel Jakub Dawidek error = EBUSY; 58532115b10SPawel Jakub Dawidek nv_free(nvin); 58632115b10SPawel Jakub Dawidek goto close; 58732115b10SPawel Jakub Dawidek } 58832115b10SPawel Jakub Dawidek token = nv_get_uint8_array(nvin, &size, "token"); 58932115b10SPawel Jakub Dawidek if (token == NULL) { 59032115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s has no 'token' field.", 59132115b10SPawel Jakub Dawidek res->hr_remoteaddr); 59232115b10SPawel Jakub Dawidek nv_free(nvin); 59332115b10SPawel Jakub Dawidek goto close; 59432115b10SPawel Jakub Dawidek } 59532115b10SPawel Jakub Dawidek if (size != sizeof(res->hr_token)) { 59632115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 59732115b10SPawel Jakub Dawidek res->hr_remoteaddr, size, sizeof(res->hr_token)); 59832115b10SPawel Jakub Dawidek nv_free(nvin); 59932115b10SPawel Jakub Dawidek goto close; 60032115b10SPawel Jakub Dawidek } 60132115b10SPawel Jakub Dawidek bcopy(token, res->hr_token, sizeof(res->hr_token)); 60232115b10SPawel Jakub Dawidek nv_free(nvin); 60332115b10SPawel Jakub Dawidek 60432115b10SPawel Jakub Dawidek /* 60532115b10SPawel Jakub Dawidek * Second handshake step. 60632115b10SPawel Jakub Dawidek * Setup incoming connection with remote node. 60732115b10SPawel Jakub Dawidek */ 60832ecf620SPawel Jakub Dawidek if (primary_connect(res, &in) == -1) 60932115b10SPawel Jakub Dawidek goto close; 61032ecf620SPawel Jakub Dawidek 61132115b10SPawel Jakub Dawidek nvout = nv_alloc(); 61232115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 61332115b10SPawel Jakub Dawidek nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 61432115b10SPawel Jakub Dawidek "token"); 615ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) { 616ce837469SPawel Jakub Dawidek /* 617ce837469SPawel Jakub Dawidek * The resuid field was not yet initialized. 618ce837469SPawel Jakub Dawidek * Because we do synchronization inside init_resuid(), it is 619ce837469SPawel Jakub Dawidek * possible that someone already initialized it, the function 620ce837469SPawel Jakub Dawidek * will return false then, but if we successfully initialized 621ce837469SPawel Jakub Dawidek * it, we will get true. True means that there were no writes 622ce837469SPawel Jakub Dawidek * to this resource yet and we want to inform secondary that 623ce837469SPawel Jakub Dawidek * synchronization is not needed by sending "virgin" argument. 624ce837469SPawel Jakub Dawidek */ 625ce837469SPawel Jakub Dawidek if (init_resuid(res)) 626ce837469SPawel Jakub Dawidek nv_add_int8(nvout, 1, "virgin"); 627ce837469SPawel Jakub Dawidek } 62832115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_resuid, "resuid"); 62932115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 63032115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 63132115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 63232115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 63332115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 63432115b10SPawel Jakub Dawidek res->hr_remoteaddr); 63532115b10SPawel Jakub Dawidek nv_free(nvout); 63632115b10SPawel Jakub Dawidek goto close; 63732115b10SPawel Jakub Dawidek } 6380d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, in, nvout, NULL, 0) < 0) { 63932115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 64032115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 64132115b10SPawel Jakub Dawidek res->hr_remoteaddr); 64232115b10SPawel Jakub Dawidek nv_free(nvout); 64332115b10SPawel Jakub Dawidek goto close; 64432115b10SPawel Jakub Dawidek } 64532115b10SPawel Jakub Dawidek nv_free(nvout); 6460d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 64732115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 64832115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 64932115b10SPawel Jakub Dawidek res->hr_remoteaddr); 65032115b10SPawel Jakub Dawidek goto close; 65132115b10SPawel Jakub Dawidek } 65232115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 65332115b10SPawel Jakub Dawidek if (errmsg != NULL) { 65432115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 65532115b10SPawel Jakub Dawidek nv_free(nvin); 65632115b10SPawel Jakub Dawidek goto close; 65732115b10SPawel Jakub Dawidek } 65832115b10SPawel Jakub Dawidek datasize = nv_get_int64(nvin, "datasize"); 65932115b10SPawel Jakub Dawidek if (datasize != res->hr_datasize) { 66032115b10SPawel Jakub Dawidek pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 66132115b10SPawel Jakub Dawidek (intmax_t)res->hr_datasize, (intmax_t)datasize); 66232115b10SPawel Jakub Dawidek nv_free(nvin); 66332115b10SPawel Jakub Dawidek goto close; 66432115b10SPawel Jakub Dawidek } 66532115b10SPawel Jakub Dawidek extentsize = nv_get_int32(nvin, "extentsize"); 66632115b10SPawel Jakub Dawidek if (extentsize != res->hr_extentsize) { 66732115b10SPawel Jakub Dawidek pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 66832115b10SPawel Jakub Dawidek (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 66932115b10SPawel Jakub Dawidek nv_free(nvin); 67032115b10SPawel Jakub Dawidek goto close; 67132115b10SPawel Jakub Dawidek } 67232115b10SPawel Jakub Dawidek res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 67332115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 67432115b10SPawel Jakub Dawidek res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 67506cbf549SPawel Jakub Dawidek if (nv_exists(nvin, "virgin")) { 67606cbf549SPawel Jakub Dawidek /* 67706cbf549SPawel Jakub Dawidek * Secondary was reinitialized, bump localcnt if it is 0 as 67806cbf549SPawel Jakub Dawidek * only we have the data. 67906cbf549SPawel Jakub Dawidek */ 68006cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY); 68106cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_localcnt == 0); 68206cbf549SPawel Jakub Dawidek 68306cbf549SPawel Jakub Dawidek if (res->hr_primary_localcnt == 0) { 68406cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0); 68506cbf549SPawel Jakub Dawidek 68606cbf549SPawel Jakub Dawidek mtx_lock(&metadata_lock); 68706cbf549SPawel Jakub Dawidek res->hr_primary_localcnt++; 68806cbf549SPawel Jakub Dawidek pjdlog_debug(1, "Increasing localcnt to %ju.", 68906cbf549SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt); 69006cbf549SPawel Jakub Dawidek (void)metadata_write(res); 69106cbf549SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 69206cbf549SPawel Jakub Dawidek } 69306cbf549SPawel Jakub Dawidek } 69432115b10SPawel Jakub Dawidek map = NULL; 69532115b10SPawel Jakub Dawidek mapsize = nv_get_uint32(nvin, "mapsize"); 69632115b10SPawel Jakub Dawidek if (mapsize > 0) { 69732115b10SPawel Jakub Dawidek map = malloc(mapsize); 69832115b10SPawel Jakub Dawidek if (map == NULL) { 69932115b10SPawel Jakub Dawidek pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 70032115b10SPawel Jakub Dawidek (uintmax_t)mapsize); 70132115b10SPawel Jakub Dawidek nv_free(nvin); 70232115b10SPawel Jakub Dawidek goto close; 70332115b10SPawel Jakub Dawidek } 70432115b10SPawel Jakub Dawidek /* 70532115b10SPawel Jakub Dawidek * Remote node have some dirty extents on its own, lets 70632115b10SPawel Jakub Dawidek * download its activemap. 70732115b10SPawel Jakub Dawidek */ 7080d9014f3SPawel Jakub Dawidek if (hast_proto_recv_data(res, out, nvin, map, 70932115b10SPawel Jakub Dawidek mapsize) < 0) { 71032115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 71132115b10SPawel Jakub Dawidek "Unable to receive remote activemap"); 71232115b10SPawel Jakub Dawidek nv_free(nvin); 71332115b10SPawel Jakub Dawidek free(map); 71432115b10SPawel Jakub Dawidek goto close; 71532115b10SPawel Jakub Dawidek } 71632115b10SPawel Jakub Dawidek /* 71732115b10SPawel Jakub Dawidek * Merge local and remote bitmaps. 71832115b10SPawel Jakub Dawidek */ 71932115b10SPawel Jakub Dawidek activemap_merge(res->hr_amp, map, mapsize); 72032115b10SPawel Jakub Dawidek free(map); 72132115b10SPawel Jakub Dawidek /* 72232115b10SPawel Jakub Dawidek * Now that we merged bitmaps from both nodes, flush it to the 72332115b10SPawel Jakub Dawidek * disk before we start to synchronize. 72432115b10SPawel Jakub Dawidek */ 72532115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 72632115b10SPawel Jakub Dawidek } 727584a9bc3SPawel Jakub Dawidek nv_free(nvin); 728ba2a8224SMikolaj Golub #ifdef notyet 72902dfe972SPawel Jakub Dawidek /* Setup directions. */ 73002dfe972SPawel Jakub Dawidek if (proto_send(out, NULL, 0) == -1) 73102dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 73202dfe972SPawel Jakub Dawidek if (proto_recv(in, NULL, 0) == -1) 73302dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 734ba2a8224SMikolaj Golub #endif 73532115b10SPawel Jakub Dawidek pjdlog_info("Connected to %s.", res->hr_remoteaddr); 7360d9014f3SPawel Jakub Dawidek if (inp != NULL && outp != NULL) { 7370d9014f3SPawel Jakub Dawidek *inp = in; 7380d9014f3SPawel Jakub Dawidek *outp = out; 7390d9014f3SPawel Jakub Dawidek } else { 7400d9014f3SPawel Jakub Dawidek res->hr_remotein = in; 7410d9014f3SPawel Jakub Dawidek res->hr_remoteout = out; 7420d9014f3SPawel Jakub Dawidek } 7435bdff860SPawel Jakub Dawidek event_send(res, EVENT_CONNECT); 744ac0401e3SPawel Jakub Dawidek return (0); 7450d9014f3SPawel Jakub Dawidek close: 7462be8fd75SPawel Jakub Dawidek if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0) 7475bdff860SPawel Jakub Dawidek event_send(res, EVENT_SPLITBRAIN); 7480d9014f3SPawel Jakub Dawidek proto_close(out); 7490d9014f3SPawel Jakub Dawidek if (in != NULL) 7500d9014f3SPawel Jakub Dawidek proto_close(in); 751ac0401e3SPawel Jakub Dawidek return (error); 7520d9014f3SPawel Jakub Dawidek } 7530d9014f3SPawel Jakub Dawidek 7540d9014f3SPawel Jakub Dawidek static void 7550d9014f3SPawel Jakub Dawidek sync_start(void) 7560d9014f3SPawel Jakub Dawidek { 7570d9014f3SPawel Jakub Dawidek 75832115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 75932115b10SPawel Jakub Dawidek sync_inprogress = true; 76032115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 76132115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 76232115b10SPawel Jakub Dawidek } 76332115b10SPawel Jakub Dawidek 76432115b10SPawel Jakub Dawidek static void 76555ce1e7cSPawel Jakub Dawidek sync_stop(void) 76655ce1e7cSPawel Jakub Dawidek { 76755ce1e7cSPawel Jakub Dawidek 76855ce1e7cSPawel Jakub Dawidek mtx_lock(&sync_lock); 76955ce1e7cSPawel Jakub Dawidek if (sync_inprogress) 77055ce1e7cSPawel Jakub Dawidek sync_inprogress = false; 77155ce1e7cSPawel Jakub Dawidek mtx_unlock(&sync_lock); 77255ce1e7cSPawel Jakub Dawidek } 77355ce1e7cSPawel Jakub Dawidek 77455ce1e7cSPawel Jakub Dawidek static void 77532115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res) 77632115b10SPawel Jakub Dawidek { 77732115b10SPawel Jakub Dawidek struct g_gate_ctl_create ggiocreate; 77832115b10SPawel Jakub Dawidek struct g_gate_ctl_cancel ggiocancel; 77932115b10SPawel Jakub Dawidek 78032115b10SPawel Jakub Dawidek /* 78132115b10SPawel Jakub Dawidek * We communicate with ggate via /dev/ggctl. Open it. 78232115b10SPawel Jakub Dawidek */ 78332115b10SPawel Jakub Dawidek res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 78432115b10SPawel Jakub Dawidek if (res->hr_ggatefd < 0) 78532115b10SPawel Jakub Dawidek primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 78632115b10SPawel Jakub Dawidek /* 78732115b10SPawel Jakub Dawidek * Create provider before trying to connect, as connection failure 78832115b10SPawel Jakub Dawidek * is not critical, but may take some time. 78932115b10SPawel Jakub Dawidek */ 7904e47b646SPawel Jakub Dawidek bzero(&ggiocreate, sizeof(ggiocreate)); 79132115b10SPawel Jakub Dawidek ggiocreate.gctl_version = G_GATE_VERSION; 79232115b10SPawel Jakub Dawidek ggiocreate.gctl_mediasize = res->hr_datasize; 79332115b10SPawel Jakub Dawidek ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 79432115b10SPawel Jakub Dawidek ggiocreate.gctl_flags = 0; 7952a49afacSPawel Jakub Dawidek ggiocreate.gctl_maxcount = 0; 79632115b10SPawel Jakub Dawidek ggiocreate.gctl_timeout = 0; 79732115b10SPawel Jakub Dawidek ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 79832115b10SPawel Jakub Dawidek snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 79932115b10SPawel Jakub Dawidek res->hr_provname); 80032115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 80132115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s created.", res->hr_provname); 80232115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocreate.gctl_unit; 80332115b10SPawel Jakub Dawidek return; 80432115b10SPawel Jakub Dawidek } 80532115b10SPawel Jakub Dawidek if (errno != EEXIST) { 80632115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to create hast/%s device", 80732115b10SPawel Jakub Dawidek res->hr_provname); 80832115b10SPawel Jakub Dawidek } 80932115b10SPawel Jakub Dawidek pjdlog_debug(1, 81032115b10SPawel Jakub Dawidek "Device hast/%s already exists, we will try to take it over.", 81132115b10SPawel Jakub Dawidek res->hr_provname); 81232115b10SPawel Jakub Dawidek /* 81332115b10SPawel Jakub Dawidek * If we received EEXIST, we assume that the process who created the 81432115b10SPawel Jakub Dawidek * provider died and didn't clean up. In that case we will start from 81532115b10SPawel Jakub Dawidek * where he left of. 81632115b10SPawel Jakub Dawidek */ 8174e47b646SPawel Jakub Dawidek bzero(&ggiocancel, sizeof(ggiocancel)); 81832115b10SPawel Jakub Dawidek ggiocancel.gctl_version = G_GATE_VERSION; 81932115b10SPawel Jakub Dawidek ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 82032115b10SPawel Jakub Dawidek snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 82132115b10SPawel Jakub Dawidek res->hr_provname); 82232115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 82332115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s recovered.", res->hr_provname); 82432115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocancel.gctl_unit; 82532115b10SPawel Jakub Dawidek return; 82632115b10SPawel Jakub Dawidek } 82732115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to take over hast/%s device", 82832115b10SPawel Jakub Dawidek res->hr_provname); 82932115b10SPawel Jakub Dawidek } 83032115b10SPawel Jakub Dawidek 83132115b10SPawel Jakub Dawidek void 83232115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res) 83332115b10SPawel Jakub Dawidek { 83432115b10SPawel Jakub Dawidek pthread_t td; 83532115b10SPawel Jakub Dawidek pid_t pid; 836bc7a916aSMikolaj Golub int error, mode, debuglevel; 83732115b10SPawel Jakub Dawidek 83832115b10SPawel Jakub Dawidek /* 83932ecf620SPawel Jakub Dawidek * Create communication channel for sending control commands from 84032ecf620SPawel Jakub Dawidek * parent to child. 84132115b10SPawel Jakub Dawidek */ 8420b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) { 843d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 84432115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8456be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 84632115b10SPawel Jakub Dawidek "Unable to create control sockets between parent and child"); 84732115b10SPawel Jakub Dawidek } 8485bdff860SPawel Jakub Dawidek /* 84932ecf620SPawel Jakub Dawidek * Create communication channel for sending events from child to parent. 8505bdff860SPawel Jakub Dawidek */ 8510b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) { 852d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 8535bdff860SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8545bdff860SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 8555bdff860SPawel Jakub Dawidek "Unable to create event sockets between child and parent"); 8565bdff860SPawel Jakub Dawidek } 85732ecf620SPawel Jakub Dawidek /* 85832ecf620SPawel Jakub Dawidek * Create communication channel for sending connection requests from 85932ecf620SPawel Jakub Dawidek * child to parent. 86032ecf620SPawel Jakub Dawidek */ 8610b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) { 86232ecf620SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 86332ecf620SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 86432ecf620SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 86532ecf620SPawel Jakub Dawidek "Unable to create connection sockets between child and parent"); 86632ecf620SPawel Jakub Dawidek } 86732115b10SPawel Jakub Dawidek 86832115b10SPawel Jakub Dawidek pid = fork(); 86932115b10SPawel Jakub Dawidek if (pid < 0) { 870d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 87132115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8726be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_TEMPFAIL, "Unable to fork"); 87332115b10SPawel Jakub Dawidek } 87432115b10SPawel Jakub Dawidek 87532115b10SPawel Jakub Dawidek if (pid > 0) { 87632115b10SPawel Jakub Dawidek /* This is parent. */ 8775bdff860SPawel Jakub Dawidek /* Declare that we are receiver. */ 8785bdff860SPawel Jakub Dawidek proto_recv(res->hr_event, NULL, 0); 87932ecf620SPawel Jakub Dawidek proto_recv(res->hr_conn, NULL, 0); 880da1783eaSPawel Jakub Dawidek /* Declare that we are sender. */ 881da1783eaSPawel Jakub Dawidek proto_send(res->hr_ctrl, NULL, 0); 88232115b10SPawel Jakub Dawidek res->hr_workerpid = pid; 88332115b10SPawel Jakub Dawidek return; 88432115b10SPawel Jakub Dawidek } 885ecc99c89SPawel Jakub Dawidek 8865b41e644SPawel Jakub Dawidek gres = res; 887da1783eaSPawel Jakub Dawidek mode = pjdlog_mode_get(); 888bc7a916aSMikolaj Golub debuglevel = pjdlog_debug_get(); 88932115b10SPawel Jakub Dawidek 8905bdff860SPawel Jakub Dawidek /* Declare that we are sender. */ 8915bdff860SPawel Jakub Dawidek proto_send(res->hr_event, NULL, 0); 89232ecf620SPawel Jakub Dawidek proto_send(res->hr_conn, NULL, 0); 893da1783eaSPawel Jakub Dawidek /* Declare that we are receiver. */ 894da1783eaSPawel Jakub Dawidek proto_recv(res->hr_ctrl, NULL, 0); 895da1783eaSPawel Jakub Dawidek descriptors_cleanup(res); 896da1783eaSPawel Jakub Dawidek 897f463896eSPawel Jakub Dawidek descriptors_assert(res, mode); 898f463896eSPawel Jakub Dawidek 899da1783eaSPawel Jakub Dawidek pjdlog_init(mode); 900bc7a916aSMikolaj Golub pjdlog_debug_set(debuglevel); 901da1783eaSPawel Jakub Dawidek pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 902643080b7SPawel Jakub Dawidek setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role)); 9035bdff860SPawel Jakub Dawidek 90432115b10SPawel Jakub Dawidek init_local(res); 90532115b10SPawel Jakub Dawidek init_ggate(res); 90632115b10SPawel Jakub Dawidek init_environment(res); 907115f4e5cSPawel Jakub Dawidek 9080cddb12fSPawel Jakub Dawidek if (drop_privs(res) != 0) { 9096d7967deSPawel Jakub Dawidek cleanup(res); 9106d7967deSPawel Jakub Dawidek exit(EX_CONFIG); 9116d7967deSPawel Jakub Dawidek } 912f4c96f94SPawel Jakub Dawidek pjdlog_info("Privileges successfully dropped."); 9136d7967deSPawel Jakub Dawidek 9148b70e6aeSPawel Jakub Dawidek /* 9154a88128bSPawel Jakub Dawidek * Create the guard thread first, so we can handle signals from the 9164a88128bSPawel Jakub Dawidek * very begining. 9174a88128bSPawel Jakub Dawidek */ 9184a88128bSPawel Jakub Dawidek error = pthread_create(&td, NULL, guard_thread, res); 9192ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 9204a88128bSPawel Jakub Dawidek /* 9218b70e6aeSPawel Jakub Dawidek * Create the control thread before sending any event to the parent, 9228b70e6aeSPawel Jakub Dawidek * as we can deadlock when parent sends control request to worker, 9238b70e6aeSPawel Jakub Dawidek * but worker has no control thread started yet, so parent waits. 9248b70e6aeSPawel Jakub Dawidek * In the meantime worker sends an event to the parent, but parent 9258b70e6aeSPawel Jakub Dawidek * is unable to handle the event, because it waits for control 9268b70e6aeSPawel Jakub Dawidek * request response. 9278b70e6aeSPawel Jakub Dawidek */ 9288b70e6aeSPawel Jakub Dawidek error = pthread_create(&td, NULL, ctrl_thread, res); 9292ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 930ac0401e3SPawel Jakub Dawidek if (real_remote(res)) { 931ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL); 932ac0401e3SPawel Jakub Dawidek if (error == 0) { 9338b70e6aeSPawel Jakub Dawidek sync_start(); 934ac0401e3SPawel Jakub Dawidek } else if (error == EBUSY) { 935ac0401e3SPawel Jakub Dawidek time_t start = time(NULL); 936ac0401e3SPawel Jakub Dawidek 937ac0401e3SPawel Jakub Dawidek pjdlog_warning("Waiting for remote node to become %s for %ds.", 938ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_SECONDARY), 939ac0401e3SPawel Jakub Dawidek res->hr_timeout); 940ac0401e3SPawel Jakub Dawidek for (;;) { 941ac0401e3SPawel Jakub Dawidek sleep(1); 942ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL); 943ac0401e3SPawel Jakub Dawidek if (error != EBUSY) 944ac0401e3SPawel Jakub Dawidek break; 945ac0401e3SPawel Jakub Dawidek if (time(NULL) > start + res->hr_timeout) 946ac0401e3SPawel Jakub Dawidek break; 947ac0401e3SPawel Jakub Dawidek } 948ac0401e3SPawel Jakub Dawidek if (error == EBUSY) { 949ac0401e3SPawel Jakub Dawidek pjdlog_warning("Remote node is still %s, starting anyway.", 950ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_PRIMARY)); 951ac0401e3SPawel Jakub Dawidek } 952ac0401e3SPawel Jakub Dawidek } 953ac0401e3SPawel Jakub Dawidek } 95432115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_recv_thread, res); 9552ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 95632115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, local_send_thread, res); 9572ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 95832115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_send_thread, res); 9592ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 96032115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_recv_thread, res); 9612ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 96232115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_send_thread, res); 9632ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 964ac0401e3SPawel Jakub Dawidek fullystarted = true; 9654a88128bSPawel Jakub Dawidek (void)sync_thread(res); 96632115b10SPawel Jakub Dawidek } 96732115b10SPawel Jakub Dawidek 96832115b10SPawel Jakub Dawidek static void 96932115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...) 97032115b10SPawel Jakub Dawidek { 97132115b10SPawel Jakub Dawidek char msg[1024]; 97232115b10SPawel Jakub Dawidek va_list ap; 97332115b10SPawel Jakub Dawidek int len; 97432115b10SPawel Jakub Dawidek 97532115b10SPawel Jakub Dawidek va_start(ap, fmt); 97632115b10SPawel Jakub Dawidek len = vsnprintf(msg, sizeof(msg), fmt, ap); 97732115b10SPawel Jakub Dawidek va_end(ap); 97832115b10SPawel Jakub Dawidek if ((size_t)len < sizeof(msg)) { 97932115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 98032115b10SPawel Jakub Dawidek case BIO_READ: 98132115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 98232115b10SPawel Jakub Dawidek "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 98332115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 98432115b10SPawel Jakub Dawidek break; 98532115b10SPawel Jakub Dawidek case BIO_DELETE: 98632115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 98732115b10SPawel Jakub Dawidek "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 98832115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 98932115b10SPawel Jakub Dawidek break; 99032115b10SPawel Jakub Dawidek case BIO_FLUSH: 99132115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 99232115b10SPawel Jakub Dawidek break; 99332115b10SPawel Jakub Dawidek case BIO_WRITE: 99432115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 99532115b10SPawel Jakub Dawidek "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 99632115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 99732115b10SPawel Jakub Dawidek break; 99832115b10SPawel Jakub Dawidek default: 99932115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 100032115b10SPawel Jakub Dawidek "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd); 100132115b10SPawel Jakub Dawidek break; 100232115b10SPawel Jakub Dawidek } 100332115b10SPawel Jakub Dawidek } 100432115b10SPawel Jakub Dawidek pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 100532115b10SPawel Jakub Dawidek } 100632115b10SPawel Jakub Dawidek 100732115b10SPawel Jakub Dawidek static void 100832115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp) 100932115b10SPawel Jakub Dawidek { 101032115b10SPawel Jakub Dawidek 101132115b10SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 101232115b10SPawel Jakub Dawidek /* 101332115b10SPawel Jakub Dawidek * A race is possible between dropping rlock and acquiring wlock - 101432115b10SPawel Jakub Dawidek * another thread can close connection in-between. 101532115b10SPawel Jakub Dawidek */ 101632115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 10172ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 10182ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 101932115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 102032115b10SPawel Jakub Dawidek return; 102132115b10SPawel Jakub Dawidek } 102232115b10SPawel Jakub Dawidek 10232ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 10242ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 102532115b10SPawel Jakub Dawidek 10268f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing incoming connection to %s.", 102732115b10SPawel Jakub Dawidek res->hr_remoteaddr); 102832115b10SPawel Jakub Dawidek proto_close(res->hr_remotein); 102932115b10SPawel Jakub Dawidek res->hr_remotein = NULL; 10308f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing outgoing connection to %s.", 103132115b10SPawel Jakub Dawidek res->hr_remoteaddr); 103232115b10SPawel Jakub Dawidek proto_close(res->hr_remoteout); 103332115b10SPawel Jakub Dawidek res->hr_remoteout = NULL; 103432115b10SPawel Jakub Dawidek 103532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 103632115b10SPawel Jakub Dawidek 10378f8c798cSPawel Jakub Dawidek pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 10388f8c798cSPawel Jakub Dawidek 103932115b10SPawel Jakub Dawidek /* 104032115b10SPawel Jakub Dawidek * Stop synchronization if in-progress. 104132115b10SPawel Jakub Dawidek */ 104255ce1e7cSPawel Jakub Dawidek sync_stop(); 10435b41e644SPawel Jakub Dawidek 10445bdff860SPawel Jakub Dawidek event_send(res, EVENT_DISCONNECT); 1045f7fe83f9SPawel Jakub Dawidek } 104632115b10SPawel Jakub Dawidek 104732115b10SPawel Jakub Dawidek /* 104832115b10SPawel Jakub Dawidek * Thread receives ggate I/O requests from the kernel and passes them to 104932115b10SPawel Jakub Dawidek * appropriate threads: 105032115b10SPawel Jakub Dawidek * WRITE - always goes to both local_send and remote_send threads 105132115b10SPawel Jakub Dawidek * READ (when the block is up-to-date on local component) - 105232115b10SPawel Jakub Dawidek * only local_send thread 105332115b10SPawel Jakub Dawidek * READ (when the block isn't up-to-date on local component) - 105432115b10SPawel Jakub Dawidek * only remote_send thread 105532115b10SPawel Jakub Dawidek * DELETE - always goes to both local_send and remote_send threads 105632115b10SPawel Jakub Dawidek * FLUSH - always goes to both local_send and remote_send threads 105732115b10SPawel Jakub Dawidek */ 105832115b10SPawel Jakub Dawidek static void * 105932115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg) 106032115b10SPawel Jakub Dawidek { 106132115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 106232115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 106332115b10SPawel Jakub Dawidek struct hio *hio; 106432115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 106532115b10SPawel Jakub Dawidek int error; 106632115b10SPawel Jakub Dawidek 106732115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 106832115b10SPawel Jakub Dawidek 106932115b10SPawel Jakub Dawidek for (;;) { 107032115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: Taking free request."); 107132115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 107232115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 107332115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 107432115b10SPawel Jakub Dawidek ggio->gctl_unit = res->hr_ggateunit; 107532115b10SPawel Jakub Dawidek ggio->gctl_length = MAXPHYS; 107632115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 107732115b10SPawel Jakub Dawidek pjdlog_debug(2, 107832115b10SPawel Jakub Dawidek "ggate_recv: (%p) Waiting for request from the kernel.", 107932115b10SPawel Jakub Dawidek hio); 108032115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) { 108132115b10SPawel Jakub Dawidek if (sigexit_received) 108232115b10SPawel Jakub Dawidek pthread_exit(NULL); 108332115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 108432115b10SPawel Jakub Dawidek } 108532115b10SPawel Jakub Dawidek error = ggio->gctl_error; 108632115b10SPawel Jakub Dawidek switch (error) { 108732115b10SPawel Jakub Dawidek case 0: 108832115b10SPawel Jakub Dawidek break; 108932115b10SPawel Jakub Dawidek case ECANCELED: 109032115b10SPawel Jakub Dawidek /* Exit gracefully. */ 109132115b10SPawel Jakub Dawidek if (!sigexit_received) { 109232115b10SPawel Jakub Dawidek pjdlog_debug(2, 109332115b10SPawel Jakub Dawidek "ggate_recv: (%p) Received cancel from the kernel.", 109432115b10SPawel Jakub Dawidek hio); 109532115b10SPawel Jakub Dawidek pjdlog_info("Received cancel from the kernel, exiting."); 109632115b10SPawel Jakub Dawidek } 109732115b10SPawel Jakub Dawidek pthread_exit(NULL); 109832115b10SPawel Jakub Dawidek case ENOMEM: 109932115b10SPawel Jakub Dawidek /* 110032115b10SPawel Jakub Dawidek * Buffer too small? Impossible, we allocate MAXPHYS 110132115b10SPawel Jakub Dawidek * bytes - request can't be bigger than that. 110232115b10SPawel Jakub Dawidek */ 110332115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 110432115b10SPawel Jakub Dawidek case ENXIO: 110532115b10SPawel Jakub Dawidek default: 110632115b10SPawel Jakub Dawidek primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 110732115b10SPawel Jakub Dawidek strerror(error)); 110832115b10SPawel Jakub Dawidek } 110932115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 111032115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 111132115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, 111232115b10SPawel Jakub Dawidek "ggate_recv: (%p) Request received from the kernel: ", 111332115b10SPawel Jakub Dawidek hio); 111432115b10SPawel Jakub Dawidek /* 111532115b10SPawel Jakub Dawidek * Inform all components about new write request. 111632115b10SPawel Jakub Dawidek * For read request prefer local component unless the given 111732115b10SPawel Jakub Dawidek * range is out-of-date, then use remote component. 111832115b10SPawel Jakub Dawidek */ 111932115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 112032115b10SPawel Jakub Dawidek case BIO_READ: 11213db86c39SPawel Jakub Dawidek res->hr_stat_read++; 112232115b10SPawel Jakub Dawidek pjdlog_debug(2, 112332115b10SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queue.", 112432115b10SPawel Jakub Dawidek hio); 112532115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 112632115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 112732115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 112832115b10SPawel Jakub Dawidek res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 112932115b10SPawel Jakub Dawidek /* 113032115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 113132115b10SPawel Jakub Dawidek * so handle request locally. 113232115b10SPawel Jakub Dawidek */ 113332115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 113432115b10SPawel Jakub Dawidek ncomp = 0; 113532115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == 113632115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY) */ { 11372ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == 113832115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY); 113932115b10SPawel Jakub Dawidek /* 114032115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 114132115b10SPawel Jakub Dawidek * so send request to the remote node. 114232115b10SPawel Jakub Dawidek */ 114332115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 114432115b10SPawel Jakub Dawidek ncomp = 1; 114532115b10SPawel Jakub Dawidek } 114632115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 114732115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 114832115b10SPawel Jakub Dawidek break; 114932115b10SPawel Jakub Dawidek case BIO_WRITE: 11503db86c39SPawel Jakub Dawidek res->hr_stat_write++; 1151ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) { 11529446b453SPawel Jakub Dawidek /* 11539446b453SPawel Jakub Dawidek * This is first write, initialize localcnt and 11549446b453SPawel Jakub Dawidek * resuid. 11559446b453SPawel Jakub Dawidek */ 11569446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 1; 1157ce837469SPawel Jakub Dawidek (void)init_resuid(res); 1158ce837469SPawel Jakub Dawidek } 115932115b10SPawel Jakub Dawidek for (;;) { 116032115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 116132115b10SPawel Jakub Dawidek if (rangelock_islocked(range_sync, 116232115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 116332115b10SPawel Jakub Dawidek pjdlog_debug(2, 116432115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu locked.", 116532115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 116632115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 116732115b10SPawel Jakub Dawidek range_regular_wait = true; 116832115b10SPawel Jakub Dawidek cv_wait(&range_regular_cond, &range_lock); 116932115b10SPawel Jakub Dawidek range_regular_wait = false; 117032115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 117132115b10SPawel Jakub Dawidek continue; 117232115b10SPawel Jakub Dawidek } 117332115b10SPawel Jakub Dawidek if (rangelock_add(range_regular, 117432115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length) < 0) { 117532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 117632115b10SPawel Jakub Dawidek pjdlog_debug(2, 117732115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu is already locked, waiting.", 117832115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 117932115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 118032115b10SPawel Jakub Dawidek sleep(1); 118132115b10SPawel Jakub Dawidek continue; 118232115b10SPawel Jakub Dawidek } 118332115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 118432115b10SPawel Jakub Dawidek break; 118532115b10SPawel Jakub Dawidek } 118632115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 118732115b10SPawel Jakub Dawidek if (activemap_write_start(res->hr_amp, 118832115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 11893db86c39SPawel Jakub Dawidek res->hr_stat_activemap_update++; 119032115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 119132115b10SPawel Jakub Dawidek } 119232115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 119332115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 119432115b10SPawel Jakub Dawidek case BIO_DELETE: 119532115b10SPawel Jakub Dawidek case BIO_FLUSH: 11963db86c39SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 11973db86c39SPawel Jakub Dawidek case BIO_DELETE: 11983db86c39SPawel Jakub Dawidek res->hr_stat_delete++; 11993db86c39SPawel Jakub Dawidek break; 12003db86c39SPawel Jakub Dawidek case BIO_FLUSH: 12013db86c39SPawel Jakub Dawidek res->hr_stat_flush++; 12023db86c39SPawel Jakub Dawidek break; 12033db86c39SPawel Jakub Dawidek } 120432115b10SPawel Jakub Dawidek pjdlog_debug(2, 120532115b10SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queues.", 120632115b10SPawel Jakub Dawidek hio); 120732115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, ncomps); 120832115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 120932115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ii); 121032115b10SPawel Jakub Dawidek break; 121132115b10SPawel Jakub Dawidek } 121232115b10SPawel Jakub Dawidek } 121332115b10SPawel Jakub Dawidek /* NOTREACHED */ 121432115b10SPawel Jakub Dawidek return (NULL); 121532115b10SPawel Jakub Dawidek } 121632115b10SPawel Jakub Dawidek 121732115b10SPawel Jakub Dawidek /* 121832115b10SPawel Jakub Dawidek * Thread reads from or writes to local component. 121932115b10SPawel Jakub Dawidek * If local read fails, it redirects it to remote_send thread. 122032115b10SPawel Jakub Dawidek */ 122132115b10SPawel Jakub Dawidek static void * 122232115b10SPawel Jakub Dawidek local_send_thread(void *arg) 122332115b10SPawel Jakub Dawidek { 122432115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 122532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 122632115b10SPawel Jakub Dawidek struct hio *hio; 122732115b10SPawel Jakub Dawidek unsigned int ncomp, rncomp; 122832115b10SPawel Jakub Dawidek ssize_t ret; 122932115b10SPawel Jakub Dawidek 123032115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 123132115b10SPawel Jakub Dawidek ncomp = 0; 123232115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 123332115b10SPawel Jakub Dawidek rncomp = 1; 123432115b10SPawel Jakub Dawidek 123532115b10SPawel Jakub Dawidek for (;;) { 123632115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: Taking request."); 1237448efa94SPawel Jakub Dawidek QUEUE_TAKE1(hio, send, ncomp, 0); 123832115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: (%p) Got request.", hio); 123932115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 124032115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 124132115b10SPawel Jakub Dawidek case BIO_READ: 124232115b10SPawel Jakub Dawidek ret = pread(res->hr_localfd, ggio->gctl_data, 124332115b10SPawel Jakub Dawidek ggio->gctl_length, 124432115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 124532115b10SPawel Jakub Dawidek if (ret == ggio->gctl_length) 124632115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1247a01a750fSMikolaj Golub else if (!ISSYNCREQ(hio)) { 124832115b10SPawel Jakub Dawidek /* 124932115b10SPawel Jakub Dawidek * If READ failed, try to read from remote node. 125032115b10SPawel Jakub Dawidek */ 1251cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 1252cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1253cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s), trying remote node. ", 1254cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1255cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 1256cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1257cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd), trying remote node. ", 1258fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1259cd7b7ee5SPawel Jakub Dawidek } 126032115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, rncomp); 126132115b10SPawel Jakub Dawidek continue; 126232115b10SPawel Jakub Dawidek } 126332115b10SPawel Jakub Dawidek break; 126432115b10SPawel Jakub Dawidek case BIO_WRITE: 126532115b10SPawel Jakub Dawidek ret = pwrite(res->hr_localfd, ggio->gctl_data, 126632115b10SPawel Jakub Dawidek ggio->gctl_length, 126732115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 1268cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 126932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1270cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1271cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1272cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1273cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 127432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = EIO; 1275cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1276cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd): ", 1277fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1278cd7b7ee5SPawel Jakub Dawidek } else { 127932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1280cd7b7ee5SPawel Jakub Dawidek } 128132115b10SPawel Jakub Dawidek break; 128232115b10SPawel Jakub Dawidek case BIO_DELETE: 128332115b10SPawel Jakub Dawidek ret = g_delete(res->hr_localfd, 128432115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff, 128532115b10SPawel Jakub Dawidek ggio->gctl_length); 1286cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 128732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1288cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1289cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1290cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1291cd7b7ee5SPawel Jakub Dawidek } else { 129232115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1293cd7b7ee5SPawel Jakub Dawidek } 129432115b10SPawel Jakub Dawidek break; 129532115b10SPawel Jakub Dawidek case BIO_FLUSH: 129632115b10SPawel Jakub Dawidek ret = g_flush(res->hr_localfd); 1297cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 129832115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1299cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1300cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1301cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1302cd7b7ee5SPawel Jakub Dawidek } else { 130332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1304cd7b7ee5SPawel Jakub Dawidek } 130532115b10SPawel Jakub Dawidek break; 130632115b10SPawel Jakub Dawidek } 130732115b10SPawel Jakub Dawidek if (refcount_release(&hio->hio_countdown)) { 130832115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 130932115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 131032115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 131132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 131232115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 131332115b10SPawel Jakub Dawidek } else { 131432115b10SPawel Jakub Dawidek pjdlog_debug(2, 131532115b10SPawel Jakub Dawidek "local_send: (%p) Moving request to the done queue.", 131632115b10SPawel Jakub Dawidek hio); 131732115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 131832115b10SPawel Jakub Dawidek } 131932115b10SPawel Jakub Dawidek } 132032115b10SPawel Jakub Dawidek } 132132115b10SPawel Jakub Dawidek /* NOTREACHED */ 132232115b10SPawel Jakub Dawidek return (NULL); 132332115b10SPawel Jakub Dawidek } 132432115b10SPawel Jakub Dawidek 1325448efa94SPawel Jakub Dawidek static void 1326448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp) 1327448efa94SPawel Jakub Dawidek { 1328448efa94SPawel Jakub Dawidek struct nv *nv; 1329448efa94SPawel Jakub Dawidek 133021e7bc5eSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 133121e7bc5eSPawel Jakub Dawidek 133221e7bc5eSPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 133321e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1334448efa94SPawel Jakub Dawidek return; 133521e7bc5eSPawel Jakub Dawidek } 1336448efa94SPawel Jakub Dawidek 13372ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 13382ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 1339448efa94SPawel Jakub Dawidek 1340448efa94SPawel Jakub Dawidek nv = nv_alloc(); 1341448efa94SPawel Jakub Dawidek nv_add_uint8(nv, HIO_KEEPALIVE, "cmd"); 1342448efa94SPawel Jakub Dawidek if (nv_error(nv) != 0) { 134321e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1344448efa94SPawel Jakub Dawidek nv_free(nv); 1345448efa94SPawel Jakub Dawidek pjdlog_debug(1, 1346448efa94SPawel Jakub Dawidek "keepalive_send: Unable to prepare header to send."); 1347448efa94SPawel Jakub Dawidek return; 1348448efa94SPawel Jakub Dawidek } 1349448efa94SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) { 135021e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1351448efa94SPawel Jakub Dawidek pjdlog_common(LOG_DEBUG, 1, errno, 1352448efa94SPawel Jakub Dawidek "keepalive_send: Unable to send request"); 1353448efa94SPawel Jakub Dawidek nv_free(nv); 1354448efa94SPawel Jakub Dawidek remote_close(res, ncomp); 1355448efa94SPawel Jakub Dawidek return; 1356448efa94SPawel Jakub Dawidek } 135721e7bc5eSPawel Jakub Dawidek 135821e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1359448efa94SPawel Jakub Dawidek nv_free(nv); 1360448efa94SPawel Jakub Dawidek pjdlog_debug(2, "keepalive_send: Request sent."); 1361448efa94SPawel Jakub Dawidek } 1362448efa94SPawel Jakub Dawidek 136332115b10SPawel Jakub Dawidek /* 136432115b10SPawel Jakub Dawidek * Thread sends request to secondary node. 136532115b10SPawel Jakub Dawidek */ 136632115b10SPawel Jakub Dawidek static void * 136732115b10SPawel Jakub Dawidek remote_send_thread(void *arg) 136832115b10SPawel Jakub Dawidek { 136932115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 137032115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1371448efa94SPawel Jakub Dawidek time_t lastcheck, now; 137232115b10SPawel Jakub Dawidek struct hio *hio; 137332115b10SPawel Jakub Dawidek struct nv *nv; 137432115b10SPawel Jakub Dawidek unsigned int ncomp; 137532115b10SPawel Jakub Dawidek bool wakeup; 137632115b10SPawel Jakub Dawidek uint64_t offset, length; 137732115b10SPawel Jakub Dawidek uint8_t cmd; 137832115b10SPawel Jakub Dawidek void *data; 137932115b10SPawel Jakub Dawidek 138032115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 138132115b10SPawel Jakub Dawidek ncomp = 1; 1382448efa94SPawel Jakub Dawidek lastcheck = time(NULL); 138332115b10SPawel Jakub Dawidek 138432115b10SPawel Jakub Dawidek for (;;) { 138532115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: Taking request."); 13868d7dcf14SMikolaj Golub QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE); 1387448efa94SPawel Jakub Dawidek if (hio == NULL) { 1388448efa94SPawel Jakub Dawidek now = time(NULL); 13898d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) { 1390448efa94SPawel Jakub Dawidek keepalive_send(res, ncomp); 1391448efa94SPawel Jakub Dawidek lastcheck = now; 1392448efa94SPawel Jakub Dawidek } 1393448efa94SPawel Jakub Dawidek continue; 1394448efa94SPawel Jakub Dawidek } 139532115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 139632115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 139732115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 139832115b10SPawel Jakub Dawidek case BIO_READ: 139932115b10SPawel Jakub Dawidek cmd = HIO_READ; 140032115b10SPawel Jakub Dawidek data = NULL; 140132115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 140232115b10SPawel Jakub Dawidek length = ggio->gctl_length; 140332115b10SPawel Jakub Dawidek break; 140432115b10SPawel Jakub Dawidek case BIO_WRITE: 140532115b10SPawel Jakub Dawidek cmd = HIO_WRITE; 140632115b10SPawel Jakub Dawidek data = ggio->gctl_data; 140732115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 140832115b10SPawel Jakub Dawidek length = ggio->gctl_length; 140932115b10SPawel Jakub Dawidek break; 141032115b10SPawel Jakub Dawidek case BIO_DELETE: 141132115b10SPawel Jakub Dawidek cmd = HIO_DELETE; 141232115b10SPawel Jakub Dawidek data = NULL; 141332115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 141432115b10SPawel Jakub Dawidek length = ggio->gctl_length; 141532115b10SPawel Jakub Dawidek break; 141632115b10SPawel Jakub Dawidek case BIO_FLUSH: 141732115b10SPawel Jakub Dawidek cmd = HIO_FLUSH; 141832115b10SPawel Jakub Dawidek data = NULL; 141932115b10SPawel Jakub Dawidek offset = 0; 142032115b10SPawel Jakub Dawidek length = 0; 142132115b10SPawel Jakub Dawidek break; 142232115b10SPawel Jakub Dawidek default: 142309c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition"); 142432115b10SPawel Jakub Dawidek } 142532115b10SPawel Jakub Dawidek nv = nv_alloc(); 142632115b10SPawel Jakub Dawidek nv_add_uint8(nv, cmd, "cmd"); 142732115b10SPawel Jakub Dawidek nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 142832115b10SPawel Jakub Dawidek nv_add_uint64(nv, offset, "offset"); 142932115b10SPawel Jakub Dawidek nv_add_uint64(nv, length, "length"); 143032115b10SPawel Jakub Dawidek if (nv_error(nv) != 0) { 143132115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = nv_error(nv); 143232115b10SPawel Jakub Dawidek pjdlog_debug(2, 143332115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to prepare header to send.", 143432115b10SPawel Jakub Dawidek hio); 143532115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 143632115b10SPawel Jakub Dawidek "Unable to prepare header to send (%s): ", 143732115b10SPawel Jakub Dawidek strerror(nv_error(nv))); 143832115b10SPawel Jakub Dawidek /* Move failed request immediately to the done queue. */ 143932115b10SPawel Jakub Dawidek goto done_queue; 144032115b10SPawel Jakub Dawidek } 144132115b10SPawel Jakub Dawidek pjdlog_debug(2, 144232115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the recv queue.", 144332115b10SPawel Jakub Dawidek hio); 144432115b10SPawel Jakub Dawidek /* 144532115b10SPawel Jakub Dawidek * Protect connection from disappearing. 144632115b10SPawel Jakub Dawidek */ 144732115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 144832115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 144932115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 145032115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = ENOTCONN; 145132115b10SPawel Jakub Dawidek goto done_queue; 145232115b10SPawel Jakub Dawidek } 145332115b10SPawel Jakub Dawidek /* 145432115b10SPawel Jakub Dawidek * Move the request to recv queue before sending it, because 145532115b10SPawel Jakub Dawidek * in different order we can get reply before we move request 145632115b10SPawel Jakub Dawidek * to recv queue. 145732115b10SPawel Jakub Dawidek */ 145832115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 145932115b10SPawel Jakub Dawidek wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 146032115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 146132115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 146232115b10SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, data, 146332115b10SPawel Jakub Dawidek data != NULL ? length : 0) < 0) { 146432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 146532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 146632115b10SPawel Jakub Dawidek pjdlog_debug(2, 146732115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to send request.", hio); 146832115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 146932115b10SPawel Jakub Dawidek "Unable to send request (%s): ", 147032115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 1471ee087cdfSPawel Jakub Dawidek remote_close(res, ncomp); 147232115b10SPawel Jakub Dawidek /* 147332115b10SPawel Jakub Dawidek * Take request back from the receive queue and move 147432115b10SPawel Jakub Dawidek * it immediately to the done queue. 147532115b10SPawel Jakub Dawidek */ 147632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 147732115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 147832115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 147932115b10SPawel Jakub Dawidek goto done_queue; 148032115b10SPawel Jakub Dawidek } 148132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 148232115b10SPawel Jakub Dawidek nv_free(nv); 148332115b10SPawel Jakub Dawidek if (wakeup) 148432115b10SPawel Jakub Dawidek cv_signal(&hio_recv_list_cond[ncomp]); 148532115b10SPawel Jakub Dawidek continue; 148632115b10SPawel Jakub Dawidek done_queue: 148732115b10SPawel Jakub Dawidek nv_free(nv); 148832115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 148932115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 149032115b10SPawel Jakub Dawidek continue; 149132115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 149232115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 149332115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 149432115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 149532115b10SPawel Jakub Dawidek continue; 149632115b10SPawel Jakub Dawidek } 149732115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 149832115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 149932115b10SPawel Jakub Dawidek if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 150032115b10SPawel Jakub Dawidek ggio->gctl_length)) { 150132115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 150232115b10SPawel Jakub Dawidek } 150332115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 150432115b10SPawel Jakub Dawidek } 150532115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 150632115b10SPawel Jakub Dawidek continue; 150732115b10SPawel Jakub Dawidek pjdlog_debug(2, 150832115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the done queue.", 150932115b10SPawel Jakub Dawidek hio); 151032115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 151132115b10SPawel Jakub Dawidek } 151232115b10SPawel Jakub Dawidek /* NOTREACHED */ 151332115b10SPawel Jakub Dawidek return (NULL); 151432115b10SPawel Jakub Dawidek } 151532115b10SPawel Jakub Dawidek 151632115b10SPawel Jakub Dawidek /* 151732115b10SPawel Jakub Dawidek * Thread receives answer from secondary node and passes it to ggate_send 151832115b10SPawel Jakub Dawidek * thread. 151932115b10SPawel Jakub Dawidek */ 152032115b10SPawel Jakub Dawidek static void * 152132115b10SPawel Jakub Dawidek remote_recv_thread(void *arg) 152232115b10SPawel Jakub Dawidek { 152332115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 152432115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 152532115b10SPawel Jakub Dawidek struct hio *hio; 152632115b10SPawel Jakub Dawidek struct nv *nv; 152732115b10SPawel Jakub Dawidek unsigned int ncomp; 152832115b10SPawel Jakub Dawidek uint64_t seq; 152932115b10SPawel Jakub Dawidek int error; 153032115b10SPawel Jakub Dawidek 153132115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 153232115b10SPawel Jakub Dawidek ncomp = 1; 153332115b10SPawel Jakub Dawidek 153432115b10SPawel Jakub Dawidek for (;;) { 153532115b10SPawel Jakub Dawidek /* Wait until there is anything to receive. */ 153632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 153732115b10SPawel Jakub Dawidek while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 153832115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_recv: No requests, waiting."); 153932115b10SPawel Jakub Dawidek cv_wait(&hio_recv_list_cond[ncomp], 154032115b10SPawel Jakub Dawidek &hio_recv_list_lock[ncomp]); 154132115b10SPawel Jakub Dawidek } 154232115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 154332115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 154432115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 154532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 154632115b10SPawel Jakub Dawidek /* 154732115b10SPawel Jakub Dawidek * Connection is dead, so move all pending requests to 154832115b10SPawel Jakub Dawidek * the done queue (one-by-one). 154932115b10SPawel Jakub Dawidek */ 155032115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 155132115b10SPawel Jakub Dawidek hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 15522ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(hio != NULL); 155332115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 155432115b10SPawel Jakub Dawidek hio_next[ncomp]); 155532115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 155632115b10SPawel Jakub Dawidek goto done_queue; 155732115b10SPawel Jakub Dawidek } 155832115b10SPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) { 155932115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 156032115b10SPawel Jakub Dawidek "Unable to receive reply header"); 156132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 156232115b10SPawel Jakub Dawidek remote_close(res, ncomp); 156332115b10SPawel Jakub Dawidek continue; 156432115b10SPawel Jakub Dawidek } 156532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 156632115b10SPawel Jakub Dawidek seq = nv_get_uint64(nv, "seq"); 156732115b10SPawel Jakub Dawidek if (seq == 0) { 156832115b10SPawel Jakub Dawidek pjdlog_error("Header contains no 'seq' field."); 156932115b10SPawel Jakub Dawidek nv_free(nv); 157032115b10SPawel Jakub Dawidek continue; 157132115b10SPawel Jakub Dawidek } 157232115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 157332115b10SPawel Jakub Dawidek TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 157432115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_seq == seq) { 157532115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 157632115b10SPawel Jakub Dawidek hio_next[ncomp]); 157732115b10SPawel Jakub Dawidek break; 157832115b10SPawel Jakub Dawidek } 157932115b10SPawel Jakub Dawidek } 158032115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 158132115b10SPawel Jakub Dawidek if (hio == NULL) { 158232115b10SPawel Jakub Dawidek pjdlog_error("Found no request matching received 'seq' field (%ju).", 158332115b10SPawel Jakub Dawidek (uintmax_t)seq); 158432115b10SPawel Jakub Dawidek nv_free(nv); 158532115b10SPawel Jakub Dawidek continue; 158632115b10SPawel Jakub Dawidek } 158732115b10SPawel Jakub Dawidek error = nv_get_int16(nv, "error"); 158832115b10SPawel Jakub Dawidek if (error != 0) { 158932115b10SPawel Jakub Dawidek /* Request failed on remote side. */ 159072089204SPawel Jakub Dawidek hio->hio_errors[ncomp] = error; 1591cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, &hio->hio_ggio, 1592cd7b7ee5SPawel Jakub Dawidek "Remote request failed (%s): ", strerror(error)); 159332115b10SPawel Jakub Dawidek nv_free(nv); 159432115b10SPawel Jakub Dawidek goto done_queue; 159532115b10SPawel Jakub Dawidek } 159632115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 159732115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 159832115b10SPawel Jakub Dawidek case BIO_READ: 159932115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 160032115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 160132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 160232115b10SPawel Jakub Dawidek nv_free(nv); 160332115b10SPawel Jakub Dawidek goto done_queue; 160432115b10SPawel Jakub Dawidek } 160532115b10SPawel Jakub Dawidek if (hast_proto_recv_data(res, res->hr_remotein, nv, 160632115b10SPawel Jakub Dawidek ggio->gctl_data, ggio->gctl_length) < 0) { 160732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 160832115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 160932115b10SPawel Jakub Dawidek "Unable to receive reply data"); 161032115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 161132115b10SPawel Jakub Dawidek nv_free(nv); 161232115b10SPawel Jakub Dawidek remote_close(res, ncomp); 161332115b10SPawel Jakub Dawidek goto done_queue; 161432115b10SPawel Jakub Dawidek } 161532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 161632115b10SPawel Jakub Dawidek break; 161732115b10SPawel Jakub Dawidek case BIO_WRITE: 161832115b10SPawel Jakub Dawidek case BIO_DELETE: 161932115b10SPawel Jakub Dawidek case BIO_FLUSH: 162032115b10SPawel Jakub Dawidek break; 162132115b10SPawel Jakub Dawidek default: 162209c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition"); 162332115b10SPawel Jakub Dawidek } 162432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 162532115b10SPawel Jakub Dawidek nv_free(nv); 162632115b10SPawel Jakub Dawidek done_queue: 162732115b10SPawel Jakub Dawidek if (refcount_release(&hio->hio_countdown)) { 162832115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 162932115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 163032115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 163132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 163232115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 163332115b10SPawel Jakub Dawidek } else { 163432115b10SPawel Jakub Dawidek pjdlog_debug(2, 163532115b10SPawel Jakub Dawidek "remote_recv: (%p) Moving request to the done queue.", 163632115b10SPawel Jakub Dawidek hio); 163732115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 163832115b10SPawel Jakub Dawidek } 163932115b10SPawel Jakub Dawidek } 164032115b10SPawel Jakub Dawidek } 164132115b10SPawel Jakub Dawidek /* NOTREACHED */ 164232115b10SPawel Jakub Dawidek return (NULL); 164332115b10SPawel Jakub Dawidek } 164432115b10SPawel Jakub Dawidek 164532115b10SPawel Jakub Dawidek /* 164632115b10SPawel Jakub Dawidek * Thread sends answer to the kernel. 164732115b10SPawel Jakub Dawidek */ 164832115b10SPawel Jakub Dawidek static void * 164932115b10SPawel Jakub Dawidek ggate_send_thread(void *arg) 165032115b10SPawel Jakub Dawidek { 165132115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 165232115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 165332115b10SPawel Jakub Dawidek struct hio *hio; 165432115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 165532115b10SPawel Jakub Dawidek 165632115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 165732115b10SPawel Jakub Dawidek 165832115b10SPawel Jakub Dawidek for (;;) { 165932115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: Taking request."); 166032115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, done); 166132115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 166232115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 166332115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 166432115b10SPawel Jakub Dawidek if (hio->hio_errors[ii] == 0) { 166532115b10SPawel Jakub Dawidek /* 166632115b10SPawel Jakub Dawidek * One successful request is enough to declare 166732115b10SPawel Jakub Dawidek * success. 166832115b10SPawel Jakub Dawidek */ 166932115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 167032115b10SPawel Jakub Dawidek break; 167132115b10SPawel Jakub Dawidek } 167232115b10SPawel Jakub Dawidek } 167332115b10SPawel Jakub Dawidek if (ii == ncomps) { 167432115b10SPawel Jakub Dawidek /* 167532115b10SPawel Jakub Dawidek * None of the requests were successful. 1676b068d5aaSMikolaj Golub * Use the error from local component except the 1677b068d5aaSMikolaj Golub * case when we did only remote request. 167832115b10SPawel Jakub Dawidek */ 1679b068d5aaSMikolaj Golub if (ggio->gctl_cmd == BIO_READ && 1680b068d5aaSMikolaj Golub res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) 1681b068d5aaSMikolaj Golub ggio->gctl_error = hio->hio_errors[1]; 1682b068d5aaSMikolaj Golub else 168332115b10SPawel Jakub Dawidek ggio->gctl_error = hio->hio_errors[0]; 168432115b10SPawel Jakub Dawidek } 168532115b10SPawel Jakub Dawidek if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 168632115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 1687d9f039e0SMikolaj Golub if (activemap_write_complete(res->hr_amp, 1688d9f039e0SMikolaj Golub ggio->gctl_offset, ggio->gctl_length)) { 1689d9f039e0SMikolaj Golub res->hr_stat_activemap_update++; 1690d9f039e0SMikolaj Golub (void)hast_activemap_flush(res); 1691d9f039e0SMikolaj Golub } 169232115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 169332115b10SPawel Jakub Dawidek } 169432115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 169532115b10SPawel Jakub Dawidek /* 169632115b10SPawel Jakub Dawidek * Unlock range we locked. 169732115b10SPawel Jakub Dawidek */ 169832115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 169932115b10SPawel Jakub Dawidek rangelock_del(range_regular, ggio->gctl_offset, 170032115b10SPawel Jakub Dawidek ggio->gctl_length); 170132115b10SPawel Jakub Dawidek if (range_sync_wait) 170232115b10SPawel Jakub Dawidek cv_signal(&range_sync_cond); 170332115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 170432115b10SPawel Jakub Dawidek /* 170532115b10SPawel Jakub Dawidek * Bump local count if this is first write after 170632115b10SPawel Jakub Dawidek * connection failure with remote node. 170732115b10SPawel Jakub Dawidek */ 170832115b10SPawel Jakub Dawidek ncomp = 1; 170932115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 171032115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 171132115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 171232115b10SPawel Jakub Dawidek if (res->hr_primary_localcnt == 171332115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt) { 171432115b10SPawel Jakub Dawidek res->hr_primary_localcnt++; 171532115b10SPawel Jakub Dawidek pjdlog_debug(1, 171632115b10SPawel Jakub Dawidek "Increasing localcnt to %ju.", 171732115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt); 171832115b10SPawel Jakub Dawidek (void)metadata_write(res); 171932115b10SPawel Jakub Dawidek } 172032115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 172132115b10SPawel Jakub Dawidek } 172232115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 172332115b10SPawel Jakub Dawidek } 172432115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) 172532115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 172632115b10SPawel Jakub Dawidek pjdlog_debug(2, 172732115b10SPawel Jakub Dawidek "ggate_send: (%p) Moving request to the free queue.", hio); 172832115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 172932115b10SPawel Jakub Dawidek } 173032115b10SPawel Jakub Dawidek /* NOTREACHED */ 173132115b10SPawel Jakub Dawidek return (NULL); 173232115b10SPawel Jakub Dawidek } 173332115b10SPawel Jakub Dawidek 173432115b10SPawel Jakub Dawidek /* 173532115b10SPawel Jakub Dawidek * Thread synchronize local and remote components. 173632115b10SPawel Jakub Dawidek */ 173732115b10SPawel Jakub Dawidek static void * 173832115b10SPawel Jakub Dawidek sync_thread(void *arg __unused) 173932115b10SPawel Jakub Dawidek { 174032115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 174132115b10SPawel Jakub Dawidek struct hio *hio; 174232115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1743fa356f6cSPawel Jakub Dawidek struct timeval tstart, tend, tdiff; 174432115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 174532115b10SPawel Jakub Dawidek off_t offset, length, synced; 174632115b10SPawel Jakub Dawidek bool dorewind; 174732115b10SPawel Jakub Dawidek int syncext; 174832115b10SPawel Jakub Dawidek 174932115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 175032115b10SPawel Jakub Dawidek dorewind = true; 1751b9cf0cf5SPawel Jakub Dawidek synced = 0; 1752b9cf0cf5SPawel Jakub Dawidek offset = -1; 175332115b10SPawel Jakub Dawidek 175432115b10SPawel Jakub Dawidek for (;;) { 175532115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 1756b9cf0cf5SPawel Jakub Dawidek if (offset >= 0 && !sync_inprogress) { 1757fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1758fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1759fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization interrupted after %#.0T. " 1760fa356f6cSPawel Jakub Dawidek "%NB synchronized so far.", &tdiff, 176153d9b386SPawel Jakub Dawidek (intmax_t)synced); 17625bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCINTR); 176353d9b386SPawel Jakub Dawidek } 176432115b10SPawel Jakub Dawidek while (!sync_inprogress) { 176532115b10SPawel Jakub Dawidek dorewind = true; 176632115b10SPawel Jakub Dawidek synced = 0; 176732115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 176832115b10SPawel Jakub Dawidek } 176932115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 177032115b10SPawel Jakub Dawidek /* 177132115b10SPawel Jakub Dawidek * Obtain offset at which we should synchronize. 177232115b10SPawel Jakub Dawidek * Rewind synchronization if needed. 177332115b10SPawel Jakub Dawidek */ 177432115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 177532115b10SPawel Jakub Dawidek if (dorewind) 177632115b10SPawel Jakub Dawidek activemap_sync_rewind(res->hr_amp); 177732115b10SPawel Jakub Dawidek offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 177832115b10SPawel Jakub Dawidek if (syncext != -1) { 177932115b10SPawel Jakub Dawidek /* 178032115b10SPawel Jakub Dawidek * We synchronized entire syncext extent, we can mark 178132115b10SPawel Jakub Dawidek * it as clean now. 178232115b10SPawel Jakub Dawidek */ 178332115b10SPawel Jakub Dawidek if (activemap_extent_complete(res->hr_amp, syncext)) 178432115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 178532115b10SPawel Jakub Dawidek } 178632115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 178732115b10SPawel Jakub Dawidek if (dorewind) { 178832115b10SPawel Jakub Dawidek dorewind = false; 178932115b10SPawel Jakub Dawidek if (offset < 0) 179032115b10SPawel Jakub Dawidek pjdlog_info("Nodes are in sync."); 179132115b10SPawel Jakub Dawidek else { 1792fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization started. %NB to go.", 1793fa356f6cSPawel Jakub Dawidek (intmax_t)(res->hr_extentsize * 179432115b10SPawel Jakub Dawidek activemap_ndirty(res->hr_amp))); 17955bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCSTART); 1796fa356f6cSPawel Jakub Dawidek gettimeofday(&tstart, NULL); 179732115b10SPawel Jakub Dawidek } 179832115b10SPawel Jakub Dawidek } 179932115b10SPawel Jakub Dawidek if (offset < 0) { 180055ce1e7cSPawel Jakub Dawidek sync_stop(); 180132115b10SPawel Jakub Dawidek pjdlog_debug(1, "Nothing to synchronize."); 180232115b10SPawel Jakub Dawidek /* 180332115b10SPawel Jakub Dawidek * Synchronization complete, make both localcnt and 180432115b10SPawel Jakub Dawidek * remotecnt equal. 180532115b10SPawel Jakub Dawidek */ 180632115b10SPawel Jakub Dawidek ncomp = 1; 180732115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 180832115b10SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 180932115b10SPawel Jakub Dawidek if (synced > 0) { 1810fa356f6cSPawel Jakub Dawidek int64_t bps; 1811fa356f6cSPawel Jakub Dawidek 1812fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1813fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1814fa356f6cSPawel Jakub Dawidek bps = (int64_t)((double)synced / 1815fa356f6cSPawel Jakub Dawidek ((double)tdiff.tv_sec + 1816fa356f6cSPawel Jakub Dawidek (double)tdiff.tv_usec / 1000000)); 181732115b10SPawel Jakub Dawidek pjdlog_info("Synchronization complete. " 1818fa356f6cSPawel Jakub Dawidek "%NB synchronized in %#.0lT (%NB/sec).", 1819fa356f6cSPawel Jakub Dawidek (intmax_t)synced, &tdiff, 1820fa356f6cSPawel Jakub Dawidek (intmax_t)bps); 18215bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCDONE); 182232115b10SPawel Jakub Dawidek } 182332115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 182432115b10SPawel Jakub Dawidek res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 182532115b10SPawel Jakub Dawidek res->hr_primary_localcnt = 182632115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt; 18279237aa3fSMikolaj Golub res->hr_primary_remotecnt = 18289237aa3fSMikolaj Golub res->hr_secondary_localcnt; 182932115b10SPawel Jakub Dawidek pjdlog_debug(1, 183032115b10SPawel Jakub Dawidek "Setting localcnt to %ju and remotecnt to %ju.", 183132115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt, 18329237aa3fSMikolaj Golub (uintmax_t)res->hr_primary_remotecnt); 183332115b10SPawel Jakub Dawidek (void)metadata_write(res); 183432115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 183532115b10SPawel Jakub Dawidek } 183632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 183732115b10SPawel Jakub Dawidek continue; 183832115b10SPawel Jakub Dawidek } 183932115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: Taking free request."); 184032115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 184132115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Got free request.", hio); 184232115b10SPawel Jakub Dawidek /* 184332115b10SPawel Jakub Dawidek * Lock the range we are going to synchronize. We don't want 184432115b10SPawel Jakub Dawidek * race where someone writes between our read and write. 184532115b10SPawel Jakub Dawidek */ 184632115b10SPawel Jakub Dawidek for (;;) { 184732115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 184832115b10SPawel Jakub Dawidek if (rangelock_islocked(range_regular, offset, length)) { 184932115b10SPawel Jakub Dawidek pjdlog_debug(2, 185032115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd locked.", 185132115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 185232115b10SPawel Jakub Dawidek range_sync_wait = true; 185332115b10SPawel Jakub Dawidek cv_wait(&range_sync_cond, &range_lock); 185432115b10SPawel Jakub Dawidek range_sync_wait = false; 185532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 185632115b10SPawel Jakub Dawidek continue; 185732115b10SPawel Jakub Dawidek } 185832115b10SPawel Jakub Dawidek if (rangelock_add(range_sync, offset, length) < 0) { 185932115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 186032115b10SPawel Jakub Dawidek pjdlog_debug(2, 186132115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd is already locked, waiting.", 186232115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 186332115b10SPawel Jakub Dawidek sleep(1); 186432115b10SPawel Jakub Dawidek continue; 186532115b10SPawel Jakub Dawidek } 186632115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 186732115b10SPawel Jakub Dawidek break; 186832115b10SPawel Jakub Dawidek } 186932115b10SPawel Jakub Dawidek /* 187032115b10SPawel Jakub Dawidek * First read the data from synchronization source. 187132115b10SPawel Jakub Dawidek */ 187232115b10SPawel Jakub Dawidek SYNCREQ(hio); 187332115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 187432115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_READ; 187532115b10SPawel Jakub Dawidek ggio->gctl_offset = offset; 187632115b10SPawel Jakub Dawidek ggio->gctl_length = length; 187732115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 187832115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 187932115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 188032115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 188132115b10SPawel Jakub Dawidek hio); 188232115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 188332115b10SPawel Jakub Dawidek hio); 188432115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 188532115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 188632115b10SPawel Jakub Dawidek /* 188732115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 188832115b10SPawel Jakub Dawidek * so handle request locally. 188932115b10SPawel Jakub Dawidek */ 189032115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 189132115b10SPawel Jakub Dawidek ncomp = 0; 189232115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 18932ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 189432115b10SPawel Jakub Dawidek /* 189532115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 189632115b10SPawel Jakub Dawidek * so send request to the remote node. 189732115b10SPawel Jakub Dawidek */ 189832115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 189932115b10SPawel Jakub Dawidek ncomp = 1; 190032115b10SPawel Jakub Dawidek } 190132115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 190232115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 190332115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 190432115b10SPawel Jakub Dawidek 190532115b10SPawel Jakub Dawidek /* 190632115b10SPawel Jakub Dawidek * Let's wait for READ to finish. 190732115b10SPawel Jakub Dawidek */ 190832115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 190932115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 191032115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 191132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 191232115b10SPawel Jakub Dawidek 191332115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 191432115b10SPawel Jakub Dawidek pjdlog_error("Unable to read synchronization data: %s.", 191532115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 191632115b10SPawel Jakub Dawidek goto free_queue; 191732115b10SPawel Jakub Dawidek } 191832115b10SPawel Jakub Dawidek 191932115b10SPawel Jakub Dawidek /* 192032115b10SPawel Jakub Dawidek * We read the data from synchronization source, now write it 192132115b10SPawel Jakub Dawidek * to synchronization target. 192232115b10SPawel Jakub Dawidek */ 192332115b10SPawel Jakub Dawidek SYNCREQ(hio); 192432115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_WRITE; 192532115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 192632115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 192732115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 192832115b10SPawel Jakub Dawidek hio); 192932115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 193032115b10SPawel Jakub Dawidek hio); 193132115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 193232115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 193332115b10SPawel Jakub Dawidek /* 193432115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 193532115b10SPawel Jakub Dawidek * so we update remote component. 193632115b10SPawel Jakub Dawidek */ 193732115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 193832115b10SPawel Jakub Dawidek ncomp = 1; 193932115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 19402ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 194132115b10SPawel Jakub Dawidek /* 194232115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 194332115b10SPawel Jakub Dawidek * so we update it. 194432115b10SPawel Jakub Dawidek */ 194532115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 194632115b10SPawel Jakub Dawidek ncomp = 0; 194732115b10SPawel Jakub Dawidek } 194832115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 194932115b10SPawel Jakub Dawidek 195032115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queues.", 195132115b10SPawel Jakub Dawidek hio); 195232115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 195332115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 195432115b10SPawel Jakub Dawidek 195532115b10SPawel Jakub Dawidek /* 195632115b10SPawel Jakub Dawidek * Let's wait for WRITE to finish. 195732115b10SPawel Jakub Dawidek */ 195832115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 195932115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 196032115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 196132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 196232115b10SPawel Jakub Dawidek 196332115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 196432115b10SPawel Jakub Dawidek pjdlog_error("Unable to write synchronization data: %s.", 196532115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 196632115b10SPawel Jakub Dawidek goto free_queue; 196732115b10SPawel Jakub Dawidek } 1968e23d2d01SPawel Jakub Dawidek 1969e23d2d01SPawel Jakub Dawidek synced += length; 197032115b10SPawel Jakub Dawidek free_queue: 197132115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 197232115b10SPawel Jakub Dawidek rangelock_del(range_sync, offset, length); 197332115b10SPawel Jakub Dawidek if (range_regular_wait) 197432115b10SPawel Jakub Dawidek cv_signal(&range_regular_cond); 197532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 197632115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 197732115b10SPawel Jakub Dawidek hio); 197832115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 197932115b10SPawel Jakub Dawidek } 198032115b10SPawel Jakub Dawidek /* NOTREACHED */ 198132115b10SPawel Jakub Dawidek return (NULL); 198232115b10SPawel Jakub Dawidek } 198332115b10SPawel Jakub Dawidek 1984115f4e5cSPawel Jakub Dawidek void 1985115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv) 19860989854dSPawel Jakub Dawidek { 19870989854dSPawel Jakub Dawidek unsigned int ii, ncomps; 1988115f4e5cSPawel Jakub Dawidek int modified, vint; 1989115f4e5cSPawel Jakub Dawidek const char *vstr; 19900989854dSPawel Jakub Dawidek 19910989854dSPawel Jakub Dawidek pjdlog_info("Reloading configuration..."); 19920989854dSPawel Jakub Dawidek 19932ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 19942ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(gres == res); 1995115f4e5cSPawel Jakub Dawidek nv_assert(nv, "remoteaddr"); 19960b626a28SPawel Jakub Dawidek nv_assert(nv, "sourceaddr"); 1997115f4e5cSPawel Jakub Dawidek nv_assert(nv, "replication"); 19981fee97b0SPawel Jakub Dawidek nv_assert(nv, "checksum"); 19998cd3d45aSPawel Jakub Dawidek nv_assert(nv, "compression"); 2000115f4e5cSPawel Jakub Dawidek nv_assert(nv, "timeout"); 2001115f4e5cSPawel Jakub Dawidek nv_assert(nv, "exec"); 2002115f4e5cSPawel Jakub Dawidek 20030989854dSPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 20040989854dSPawel Jakub Dawidek 20051fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR 0x01 20060b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR 0x02 20070b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION 0x04 20080b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM 0x08 20090b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION 0x10 20100b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT 0x20 20110b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC 0x40 20120989854dSPawel Jakub Dawidek modified = 0; 2013115f4e5cSPawel Jakub Dawidek 2014115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 2015115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_remoteaddr, vstr) != 0) { 20160989854dSPawel Jakub Dawidek /* 20170989854dSPawel Jakub Dawidek * Don't copy res->hr_remoteaddr to gres just yet. 20180989854dSPawel Jakub Dawidek * We want remote_close() to log disconnect from the old 20190989854dSPawel Jakub Dawidek * addresses, not from the new ones. 20200989854dSPawel Jakub Dawidek */ 20210989854dSPawel Jakub Dawidek modified |= MODIFIED_REMOTEADDR; 20220989854dSPawel Jakub Dawidek } 20230b626a28SPawel Jakub Dawidek vstr = nv_get_string(nv, "sourceaddr"); 20240b626a28SPawel Jakub Dawidek if (strcmp(gres->hr_sourceaddr, vstr) != 0) { 20250b626a28SPawel Jakub Dawidek strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr)); 20260b626a28SPawel Jakub Dawidek modified |= MODIFIED_SOURCEADDR; 20270b626a28SPawel Jakub Dawidek } 2028115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "replication"); 2029115f4e5cSPawel Jakub Dawidek if (gres->hr_replication != vint) { 2030115f4e5cSPawel Jakub Dawidek gres->hr_replication = vint; 20310989854dSPawel Jakub Dawidek modified |= MODIFIED_REPLICATION; 20320989854dSPawel Jakub Dawidek } 20331fee97b0SPawel Jakub Dawidek vint = nv_get_int32(nv, "checksum"); 20341fee97b0SPawel Jakub Dawidek if (gres->hr_checksum != vint) { 20351fee97b0SPawel Jakub Dawidek gres->hr_checksum = vint; 20361fee97b0SPawel Jakub Dawidek modified |= MODIFIED_CHECKSUM; 20371fee97b0SPawel Jakub Dawidek } 20388cd3d45aSPawel Jakub Dawidek vint = nv_get_int32(nv, "compression"); 20398cd3d45aSPawel Jakub Dawidek if (gres->hr_compression != vint) { 20408cd3d45aSPawel Jakub Dawidek gres->hr_compression = vint; 20418cd3d45aSPawel Jakub Dawidek modified |= MODIFIED_COMPRESSION; 20428cd3d45aSPawel Jakub Dawidek } 2043115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "timeout"); 2044115f4e5cSPawel Jakub Dawidek if (gres->hr_timeout != vint) { 2045115f4e5cSPawel Jakub Dawidek gres->hr_timeout = vint; 20460989854dSPawel Jakub Dawidek modified |= MODIFIED_TIMEOUT; 20470989854dSPawel Jakub Dawidek } 2048115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "exec"); 2049115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_exec, vstr) != 0) { 2050115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec)); 20510becad39SPawel Jakub Dawidek modified |= MODIFIED_EXEC; 20520becad39SPawel Jakub Dawidek } 2053115f4e5cSPawel Jakub Dawidek 20540989854dSPawel Jakub Dawidek /* 20551fee97b0SPawel Jakub Dawidek * Change timeout for connected sockets. 20561fee97b0SPawel Jakub Dawidek * Don't bother if we need to reconnect. 20570989854dSPawel Jakub Dawidek */ 20581fee97b0SPawel Jakub Dawidek if ((modified & MODIFIED_TIMEOUT) != 0 && 20590b626a28SPawel Jakub Dawidek (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR | 20600b626a28SPawel Jakub Dawidek MODIFIED_REPLICATION)) == 0) { 20610989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 20620989854dSPawel Jakub Dawidek if (!ISREMOTE(ii)) 20630989854dSPawel Jakub Dawidek continue; 20640989854dSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ii]); 20650989854dSPawel Jakub Dawidek if (!ISCONNECTED(gres, ii)) { 20660989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 20670989854dSPawel Jakub Dawidek continue; 20680989854dSPawel Jakub Dawidek } 20690989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 20700989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remotein, 20710989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 20720989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 20730989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 20740989854dSPawel Jakub Dawidek } 20750989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remoteout, 20760989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 20770989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 20780989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 20790989854dSPawel Jakub Dawidek } 20800989854dSPawel Jakub Dawidek } 20811fee97b0SPawel Jakub Dawidek } 20820b626a28SPawel Jakub Dawidek if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR | 20830b626a28SPawel Jakub Dawidek MODIFIED_REPLICATION)) != 0) { 20840989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 20850989854dSPawel Jakub Dawidek if (!ISREMOTE(ii)) 20860989854dSPawel Jakub Dawidek continue; 20870989854dSPawel Jakub Dawidek remote_close(gres, ii); 20880989854dSPawel Jakub Dawidek } 20890989854dSPawel Jakub Dawidek if (modified & MODIFIED_REMOTEADDR) { 2090115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 2091115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_remoteaddr, vstr, 20920989854dSPawel Jakub Dawidek sizeof(gres->hr_remoteaddr)); 20930989854dSPawel Jakub Dawidek } 20940989854dSPawel Jakub Dawidek } 20950989854dSPawel Jakub Dawidek #undef MODIFIED_REMOTEADDR 20960b626a28SPawel Jakub Dawidek #undef MODIFIED_SOURCEADDR 20970989854dSPawel Jakub Dawidek #undef MODIFIED_REPLICATION 20981fee97b0SPawel Jakub Dawidek #undef MODIFIED_CHECKSUM 20998cd3d45aSPawel Jakub Dawidek #undef MODIFIED_COMPRESSION 21000989854dSPawel Jakub Dawidek #undef MODIFIED_TIMEOUT 21010becad39SPawel Jakub Dawidek #undef MODIFIED_EXEC 21020989854dSPawel Jakub Dawidek 21030989854dSPawel Jakub Dawidek pjdlog_info("Configuration reloaded successfully."); 21040989854dSPawel Jakub Dawidek } 21050989854dSPawel Jakub Dawidek 2106f7fe83f9SPawel Jakub Dawidek static void 2107ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp) 2108ff6bb1f8SPawel Jakub Dawidek { 2109ff6bb1f8SPawel Jakub Dawidek struct proto_conn *in, *out; 2110ff6bb1f8SPawel Jakub Dawidek 2111ff6bb1f8SPawel Jakub Dawidek if (!ISREMOTE(ncomp)) 2112ff6bb1f8SPawel Jakub Dawidek return; 2113ff6bb1f8SPawel Jakub Dawidek 2114ff6bb1f8SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 2115ff6bb1f8SPawel Jakub Dawidek 2116ff6bb1f8SPawel Jakub Dawidek if (!real_remote(res)) { 2117ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2118ff6bb1f8SPawel Jakub Dawidek return; 2119ff6bb1f8SPawel Jakub Dawidek } 2120ff6bb1f8SPawel Jakub Dawidek 2121ff6bb1f8SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 21222ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 21232ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 2124ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2125ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Connection to %s is ok.", 2126ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2127ff6bb1f8SPawel Jakub Dawidek return; 2128ff6bb1f8SPawel Jakub Dawidek } 2129ff6bb1f8SPawel Jakub Dawidek 21302ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21312ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 2132ff6bb1f8SPawel Jakub Dawidek /* 2133ff6bb1f8SPawel Jakub Dawidek * Upgrade the lock. It doesn't have to be atomic as no other thread 2134ff6bb1f8SPawel Jakub Dawidek * can change connection status from disconnected to connected. 2135ff6bb1f8SPawel Jakub Dawidek */ 2136ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2137ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnecting to %s.", 2138ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2139ff6bb1f8SPawel Jakub Dawidek in = out = NULL; 2140ac0401e3SPawel Jakub Dawidek if (init_remote(res, &in, &out) == 0) { 2141ff6bb1f8SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 21422ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21432ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 21442ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in != NULL && out != NULL); 2145ff6bb1f8SPawel Jakub Dawidek res->hr_remotein = in; 2146ff6bb1f8SPawel Jakub Dawidek res->hr_remoteout = out; 2147ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2148ff6bb1f8SPawel Jakub Dawidek pjdlog_info("Successfully reconnected to %s.", 2149ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2150ff6bb1f8SPawel Jakub Dawidek sync_start(); 2151ff6bb1f8SPawel Jakub Dawidek } else { 2152ff6bb1f8SPawel Jakub Dawidek /* Both connections should be NULL. */ 21532ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21542ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 21552ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in == NULL && out == NULL); 2156ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", 2157ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2158ff6bb1f8SPawel Jakub Dawidek } 2159ff6bb1f8SPawel Jakub Dawidek } 2160ff6bb1f8SPawel Jakub Dawidek 216132115b10SPawel Jakub Dawidek /* 216232115b10SPawel Jakub Dawidek * Thread guards remote connections and reconnects when needed, handles 216332115b10SPawel Jakub Dawidek * signals, etc. 216432115b10SPawel Jakub Dawidek */ 216532115b10SPawel Jakub Dawidek static void * 216632115b10SPawel Jakub Dawidek guard_thread(void *arg) 216732115b10SPawel Jakub Dawidek { 216832115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 216932115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 21706d0c801eSPawel Jakub Dawidek struct timespec timeout; 2171ff6bb1f8SPawel Jakub Dawidek time_t lastcheck, now; 21726d0c801eSPawel Jakub Dawidek sigset_t mask; 21736d0c801eSPawel Jakub Dawidek int signo; 217432115b10SPawel Jakub Dawidek 217532115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 2176ff6bb1f8SPawel Jakub Dawidek lastcheck = time(NULL); 217732115b10SPawel Jakub Dawidek 21786d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigemptyset(&mask) == 0); 21796d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 21806d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 21816d0c801eSPawel Jakub Dawidek 21828d7dcf14SMikolaj Golub timeout.tv_sec = HAST_KEEPALIVE; 21836d0c801eSPawel Jakub Dawidek timeout.tv_nsec = 0; 21846d0c801eSPawel Jakub Dawidek signo = -1; 21856d0c801eSPawel Jakub Dawidek 218632115b10SPawel Jakub Dawidek for (;;) { 21876d0c801eSPawel Jakub Dawidek switch (signo) { 21886d0c801eSPawel Jakub Dawidek case SIGINT: 21896d0c801eSPawel Jakub Dawidek case SIGTERM: 21906d0c801eSPawel Jakub Dawidek sigexit_received = true; 219132115b10SPawel Jakub Dawidek primary_exitx(EX_OK, 219232115b10SPawel Jakub Dawidek "Termination signal received, exiting."); 21936d0c801eSPawel Jakub Dawidek break; 21946d0c801eSPawel Jakub Dawidek default: 2195ff6bb1f8SPawel Jakub Dawidek break; 2196f7fe83f9SPawel Jakub Dawidek } 21976d0c801eSPawel Jakub Dawidek 2198ac0401e3SPawel Jakub Dawidek /* 2199ac0401e3SPawel Jakub Dawidek * Don't check connections until we fully started, 2200ac0401e3SPawel Jakub Dawidek * as we may still be looping, waiting for remote node 2201ac0401e3SPawel Jakub Dawidek * to switch from primary to secondary. 2202ac0401e3SPawel Jakub Dawidek */ 2203ac0401e3SPawel Jakub Dawidek if (fullystarted) { 22046d0c801eSPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Checking connections."); 2205ff6bb1f8SPawel Jakub Dawidek now = time(NULL); 22068d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) { 22076d0c801eSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 2208ff6bb1f8SPawel Jakub Dawidek guard_one(res, ii); 2209ff6bb1f8SPawel Jakub Dawidek lastcheck = now; 221032115b10SPawel Jakub Dawidek } 2211ac0401e3SPawel Jakub Dawidek } 22126d0c801eSPawel Jakub Dawidek signo = sigtimedwait(&mask, NULL, &timeout); 221332115b10SPawel Jakub Dawidek } 221432115b10SPawel Jakub Dawidek /* NOTREACHED */ 221532115b10SPawel Jakub Dawidek return (NULL); 221632115b10SPawel Jakub Dawidek } 2217