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) { 296be1143efSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk"); 29732115b10SPawel Jakub Dawidek return (-1); 29832115b10SPawel Jakub Dawidek } 299518dd4c0SPawel Jakub Dawidek if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) { 300518dd4c0SPawel Jakub Dawidek if (errno == EOPNOTSUPP) { 301518dd4c0SPawel Jakub Dawidek pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.", 302518dd4c0SPawel Jakub Dawidek res->hr_localpath); 303518dd4c0SPawel Jakub Dawidek res->hr_metaflush = 0; 304518dd4c0SPawel Jakub Dawidek } else { 305518dd4c0SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 306518dd4c0SPawel Jakub Dawidek "Unable to flush disk cache on activemap update"); 307518dd4c0SPawel Jakub Dawidek return (-1); 308518dd4c0SPawel Jakub Dawidek } 309518dd4c0SPawel Jakub Dawidek } 31032115b10SPawel Jakub Dawidek return (0); 31132115b10SPawel Jakub Dawidek } 31232115b10SPawel Jakub Dawidek 313f377917cSPawel Jakub Dawidek static bool 314f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res) 315f377917cSPawel Jakub Dawidek { 316f377917cSPawel Jakub Dawidek 317f377917cSPawel Jakub Dawidek return (strcmp(res->hr_remoteaddr, "none") != 0); 318f377917cSPawel Jakub Dawidek } 319f377917cSPawel Jakub Dawidek 32032115b10SPawel Jakub Dawidek static void 32132115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused) 32232115b10SPawel Jakub Dawidek { 32332115b10SPawel Jakub Dawidek struct hio *hio; 32432115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 32532115b10SPawel Jakub Dawidek 32632115b10SPawel Jakub Dawidek /* 32732115b10SPawel Jakub Dawidek * In the future it might be per-resource value. 32832115b10SPawel Jakub Dawidek */ 32932115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 33032115b10SPawel Jakub Dawidek 33132115b10SPawel Jakub Dawidek /* 33232115b10SPawel Jakub Dawidek * Allocate memory needed by lists. 33332115b10SPawel Jakub Dawidek */ 33432115b10SPawel Jakub Dawidek hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps); 33532115b10SPawel Jakub Dawidek if (hio_send_list == NULL) { 33632115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 33732115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send lists.", 33832115b10SPawel Jakub Dawidek sizeof(hio_send_list[0]) * ncomps); 33932115b10SPawel Jakub Dawidek } 34032115b10SPawel Jakub Dawidek hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps); 34132115b10SPawel Jakub Dawidek if (hio_send_list_lock == NULL) { 34232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 34332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list locks.", 34432115b10SPawel Jakub Dawidek sizeof(hio_send_list_lock[0]) * ncomps); 34532115b10SPawel Jakub Dawidek } 34632115b10SPawel Jakub Dawidek hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps); 34732115b10SPawel Jakub Dawidek if (hio_send_list_cond == NULL) { 34832115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 34932115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list condition variables.", 35032115b10SPawel Jakub Dawidek sizeof(hio_send_list_cond[0]) * ncomps); 35132115b10SPawel Jakub Dawidek } 35232115b10SPawel Jakub Dawidek hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps); 35332115b10SPawel Jakub Dawidek if (hio_recv_list == NULL) { 35432115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 35532115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv lists.", 35632115b10SPawel Jakub Dawidek sizeof(hio_recv_list[0]) * ncomps); 35732115b10SPawel Jakub Dawidek } 35832115b10SPawel Jakub Dawidek hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps); 35932115b10SPawel Jakub Dawidek if (hio_recv_list_lock == NULL) { 36032115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 36132115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list locks.", 36232115b10SPawel Jakub Dawidek sizeof(hio_recv_list_lock[0]) * ncomps); 36332115b10SPawel Jakub Dawidek } 36432115b10SPawel Jakub Dawidek hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps); 36532115b10SPawel Jakub Dawidek if (hio_recv_list_cond == NULL) { 36632115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 36732115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list condition variables.", 36832115b10SPawel Jakub Dawidek sizeof(hio_recv_list_cond[0]) * ncomps); 36932115b10SPawel Jakub Dawidek } 37032115b10SPawel Jakub Dawidek hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps); 37132115b10SPawel Jakub Dawidek if (hio_remote_lock == NULL) { 37232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 37332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for remote connections locks.", 37432115b10SPawel Jakub Dawidek sizeof(hio_remote_lock[0]) * ncomps); 37532115b10SPawel Jakub Dawidek } 37632115b10SPawel Jakub Dawidek 37732115b10SPawel Jakub Dawidek /* 37832115b10SPawel Jakub Dawidek * Initialize lists, their locks and theirs condition variables. 37932115b10SPawel Jakub Dawidek */ 38032115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_free_list); 38132115b10SPawel Jakub Dawidek mtx_init(&hio_free_list_lock); 38232115b10SPawel Jakub Dawidek cv_init(&hio_free_list_cond); 38332115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_NCOMPONENTS; ii++) { 38432115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_send_list[ii]); 38532115b10SPawel Jakub Dawidek mtx_init(&hio_send_list_lock[ii]); 38632115b10SPawel Jakub Dawidek cv_init(&hio_send_list_cond[ii]); 38732115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_recv_list[ii]); 38832115b10SPawel Jakub Dawidek mtx_init(&hio_recv_list_lock[ii]); 38932115b10SPawel Jakub Dawidek cv_init(&hio_recv_list_cond[ii]); 39032115b10SPawel Jakub Dawidek rw_init(&hio_remote_lock[ii]); 39132115b10SPawel Jakub Dawidek } 39232115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_done_list); 39332115b10SPawel Jakub Dawidek mtx_init(&hio_done_list_lock); 39432115b10SPawel Jakub Dawidek cv_init(&hio_done_list_cond); 39532115b10SPawel Jakub Dawidek mtx_init(&metadata_lock); 39632115b10SPawel Jakub Dawidek 39732115b10SPawel Jakub Dawidek /* 39832115b10SPawel Jakub Dawidek * Allocate requests pool and initialize requests. 39932115b10SPawel Jakub Dawidek */ 40032115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_HIO_MAX; ii++) { 40132115b10SPawel Jakub Dawidek hio = malloc(sizeof(*hio)); 40232115b10SPawel Jakub Dawidek if (hio == NULL) { 40332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 40432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for hio request.", 40532115b10SPawel Jakub Dawidek sizeof(*hio)); 40632115b10SPawel Jakub Dawidek } 40732115b10SPawel Jakub Dawidek hio->hio_countdown = 0; 40832115b10SPawel Jakub Dawidek hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps); 40932115b10SPawel Jakub Dawidek if (hio->hio_errors == NULL) { 41032115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 41132115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio errors.", 41232115b10SPawel Jakub Dawidek sizeof(hio->hio_errors[0]) * ncomps); 41332115b10SPawel Jakub Dawidek } 41432115b10SPawel Jakub Dawidek hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps); 41532115b10SPawel Jakub Dawidek if (hio->hio_next == NULL) { 41632115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 41732115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio_next field.", 41832115b10SPawel Jakub Dawidek sizeof(hio->hio_next[0]) * ncomps); 41932115b10SPawel Jakub Dawidek } 42032115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_version = G_GATE_VERSION; 42132115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_data = malloc(MAXPHYS); 42232115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_data == NULL) { 42332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 42432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for gctl_data.", 42532115b10SPawel Jakub Dawidek MAXPHYS); 42632115b10SPawel Jakub Dawidek } 42732115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_length = MAXPHYS; 42832115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_error = 0; 42932115b10SPawel Jakub Dawidek TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next); 43032115b10SPawel Jakub Dawidek } 43132115b10SPawel Jakub Dawidek } 43232115b10SPawel Jakub Dawidek 433ce837469SPawel Jakub Dawidek static bool 434ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res) 435ce837469SPawel Jakub Dawidek { 436ce837469SPawel Jakub Dawidek 437ce837469SPawel Jakub Dawidek mtx_lock(&metadata_lock); 438ce837469SPawel Jakub Dawidek if (res->hr_resuid != 0) { 439ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 440ce837469SPawel Jakub Dawidek return (false); 441ce837469SPawel Jakub Dawidek } else { 442ce837469SPawel Jakub Dawidek /* Initialize unique resource identifier. */ 443ce837469SPawel Jakub Dawidek arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid)); 444ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 445ce837469SPawel Jakub Dawidek if (metadata_write(res) < 0) 446ce837469SPawel Jakub Dawidek exit(EX_NOINPUT); 447ce837469SPawel Jakub Dawidek return (true); 448ce837469SPawel Jakub Dawidek } 449ce837469SPawel Jakub Dawidek } 450ce837469SPawel Jakub Dawidek 45132115b10SPawel Jakub Dawidek static void 45232115b10SPawel Jakub Dawidek init_local(struct hast_resource *res) 45332115b10SPawel Jakub Dawidek { 45432115b10SPawel Jakub Dawidek unsigned char *buf; 45532115b10SPawel Jakub Dawidek size_t mapsize; 45632115b10SPawel Jakub Dawidek 45732115b10SPawel Jakub Dawidek if (metadata_read(res, true) < 0) 45832115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 45932115b10SPawel Jakub Dawidek mtx_init(&res->hr_amp_lock); 46032115b10SPawel Jakub Dawidek if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 46132115b10SPawel Jakub Dawidek res->hr_local_sectorsize, res->hr_keepdirty) < 0) { 46232115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 46332115b10SPawel Jakub Dawidek } 46432115b10SPawel Jakub Dawidek mtx_init(&range_lock); 46532115b10SPawel Jakub Dawidek cv_init(&range_regular_cond); 46632115b10SPawel Jakub Dawidek if (rangelock_init(&range_regular) < 0) 46732115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 46832115b10SPawel Jakub Dawidek cv_init(&range_sync_cond); 46932115b10SPawel Jakub Dawidek if (rangelock_init(&range_sync) < 0) 47032115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create sync range lock"); 47132115b10SPawel Jakub Dawidek mapsize = activemap_ondisk_size(res->hr_amp); 47232115b10SPawel Jakub Dawidek buf = calloc(1, mapsize); 47332115b10SPawel Jakub Dawidek if (buf == NULL) { 47432115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 47532115b10SPawel Jakub Dawidek "Unable to allocate buffer for activemap."); 47632115b10SPawel Jakub Dawidek } 47732115b10SPawel Jakub Dawidek if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 47832115b10SPawel Jakub Dawidek (ssize_t)mapsize) { 47932115b10SPawel Jakub Dawidek primary_exit(EX_NOINPUT, "Unable to read activemap"); 48032115b10SPawel Jakub Dawidek } 48132115b10SPawel Jakub Dawidek activemap_copyin(res->hr_amp, buf, mapsize); 482b0dfbe5bSPawel Jakub Dawidek free(buf); 48332115b10SPawel Jakub Dawidek if (res->hr_resuid != 0) 48432115b10SPawel Jakub Dawidek return; 48532115b10SPawel Jakub Dawidek /* 486ce837469SPawel Jakub Dawidek * We're using provider for the first time. Initialize local and remote 487ce837469SPawel Jakub Dawidek * counters. We don't initialize resuid here, as we want to do it just 488ce837469SPawel Jakub Dawidek * in time. The reason for this is that we want to inform secondary 489ce837469SPawel Jakub Dawidek * that there were no writes yet, so there is no need to synchronize 490ce837469SPawel Jakub Dawidek * anything. 49132115b10SPawel Jakub Dawidek */ 4929446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 0; 49332115b10SPawel Jakub Dawidek res->hr_primary_remotecnt = 0; 49432115b10SPawel Jakub Dawidek if (metadata_write(res) < 0) 49532115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 49632115b10SPawel Jakub Dawidek } 49732115b10SPawel Jakub Dawidek 49832ecf620SPawel Jakub Dawidek static int 49932ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp) 50032ecf620SPawel Jakub Dawidek { 50132ecf620SPawel Jakub Dawidek struct proto_conn *conn; 50232ecf620SPawel Jakub Dawidek int16_t val; 50332ecf620SPawel Jakub Dawidek 50432ecf620SPawel Jakub Dawidek val = 1; 50532ecf620SPawel Jakub Dawidek if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) { 50632ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 50732ecf620SPawel Jakub Dawidek "Unable to send connection request to parent"); 50832ecf620SPawel Jakub Dawidek } 50932ecf620SPawel Jakub Dawidek if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) { 51032ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 51132ecf620SPawel Jakub Dawidek "Unable to receive reply to connection request from parent"); 51232ecf620SPawel Jakub Dawidek } 51332ecf620SPawel Jakub Dawidek if (val != 0) { 51432ecf620SPawel Jakub Dawidek errno = val; 51532ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 51632ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 51732ecf620SPawel Jakub Dawidek return (-1); 51832ecf620SPawel Jakub Dawidek } 51932ecf620SPawel Jakub Dawidek if (proto_connection_recv(res->hr_conn, true, &conn) < 0) { 52032ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 52132ecf620SPawel Jakub Dawidek "Unable to receive connection from parent"); 52232ecf620SPawel Jakub Dawidek } 5237d4df5cdSPawel Jakub Dawidek if (proto_connect_wait(conn, res->hr_timeout) < 0) { 52432ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 52532ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 52632ecf620SPawel Jakub Dawidek proto_close(conn); 52732ecf620SPawel Jakub Dawidek return (-1); 52832ecf620SPawel Jakub Dawidek } 52932ecf620SPawel Jakub Dawidek /* Error in setting timeout is not critical, but why should it fail? */ 53032ecf620SPawel Jakub Dawidek if (proto_timeout(conn, res->hr_timeout) < 0) 53132ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 53232ecf620SPawel Jakub Dawidek 53332ecf620SPawel Jakub Dawidek *connp = conn; 53432ecf620SPawel Jakub Dawidek 53532ecf620SPawel Jakub Dawidek return (0); 53632ecf620SPawel Jakub Dawidek } 53732ecf620SPawel Jakub Dawidek 538ac0401e3SPawel Jakub Dawidek static int 5390d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp, 5400d9014f3SPawel Jakub Dawidek struct proto_conn **outp) 54132115b10SPawel Jakub Dawidek { 5420d9014f3SPawel Jakub Dawidek struct proto_conn *in, *out; 54332115b10SPawel Jakub Dawidek struct nv *nvout, *nvin; 54432115b10SPawel Jakub Dawidek const unsigned char *token; 54532115b10SPawel Jakub Dawidek unsigned char *map; 54632115b10SPawel Jakub Dawidek const char *errmsg; 54732115b10SPawel Jakub Dawidek int32_t extentsize; 54832115b10SPawel Jakub Dawidek int64_t datasize; 54932115b10SPawel Jakub Dawidek uint32_t mapsize; 55032115b10SPawel Jakub Dawidek size_t size; 551ac0401e3SPawel Jakub Dawidek int error; 55232115b10SPawel Jakub Dawidek 5532ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 5542ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(real_remote(res)); 5550d9014f3SPawel Jakub Dawidek 5560d9014f3SPawel Jakub Dawidek in = out = NULL; 5572be8fd75SPawel Jakub Dawidek errmsg = NULL; 5580d9014f3SPawel Jakub Dawidek 55932ecf620SPawel Jakub Dawidek if (primary_connect(res, &out) == -1) 560ac0401e3SPawel Jakub Dawidek return (ECONNREFUSED); 561ac0401e3SPawel Jakub Dawidek 562ac0401e3SPawel Jakub Dawidek error = ECONNABORTED; 56332ecf620SPawel Jakub Dawidek 56432115b10SPawel Jakub Dawidek /* 56532115b10SPawel Jakub Dawidek * First handshake step. 56632115b10SPawel Jakub Dawidek * Setup outgoing connection with remote node. 56732115b10SPawel Jakub Dawidek */ 56832115b10SPawel Jakub Dawidek nvout = nv_alloc(); 56932115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 57032115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 57132115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 57232115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 57332115b10SPawel Jakub Dawidek res->hr_remoteaddr); 57432115b10SPawel Jakub Dawidek nv_free(nvout); 57532115b10SPawel Jakub Dawidek goto close; 57632115b10SPawel Jakub Dawidek } 5770d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, out, nvout, NULL, 0) < 0) { 57832115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 57932115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 58032115b10SPawel Jakub Dawidek res->hr_remoteaddr); 58132115b10SPawel Jakub Dawidek nv_free(nvout); 58232115b10SPawel Jakub Dawidek goto close; 58332115b10SPawel Jakub Dawidek } 58432115b10SPawel Jakub Dawidek nv_free(nvout); 5850d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 58632115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 58732115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 58832115b10SPawel Jakub Dawidek res->hr_remoteaddr); 58932115b10SPawel Jakub Dawidek goto close; 59032115b10SPawel Jakub Dawidek } 59132115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 59232115b10SPawel Jakub Dawidek if (errmsg != NULL) { 59332115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 594ac0401e3SPawel Jakub Dawidek if (nv_exists(nvin, "wait")) 595ac0401e3SPawel Jakub Dawidek error = EBUSY; 59632115b10SPawel Jakub Dawidek nv_free(nvin); 59732115b10SPawel Jakub Dawidek goto close; 59832115b10SPawel Jakub Dawidek } 59932115b10SPawel Jakub Dawidek token = nv_get_uint8_array(nvin, &size, "token"); 60032115b10SPawel Jakub Dawidek if (token == NULL) { 60132115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s has no 'token' field.", 60232115b10SPawel Jakub Dawidek res->hr_remoteaddr); 60332115b10SPawel Jakub Dawidek nv_free(nvin); 60432115b10SPawel Jakub Dawidek goto close; 60532115b10SPawel Jakub Dawidek } 60632115b10SPawel Jakub Dawidek if (size != sizeof(res->hr_token)) { 60732115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 60832115b10SPawel Jakub Dawidek res->hr_remoteaddr, size, sizeof(res->hr_token)); 60932115b10SPawel Jakub Dawidek nv_free(nvin); 61032115b10SPawel Jakub Dawidek goto close; 61132115b10SPawel Jakub Dawidek } 61232115b10SPawel Jakub Dawidek bcopy(token, res->hr_token, sizeof(res->hr_token)); 61332115b10SPawel Jakub Dawidek nv_free(nvin); 61432115b10SPawel Jakub Dawidek 61532115b10SPawel Jakub Dawidek /* 61632115b10SPawel Jakub Dawidek * Second handshake step. 61732115b10SPawel Jakub Dawidek * Setup incoming connection with remote node. 61832115b10SPawel Jakub Dawidek */ 61932ecf620SPawel Jakub Dawidek if (primary_connect(res, &in) == -1) 62032115b10SPawel Jakub Dawidek goto close; 62132ecf620SPawel Jakub Dawidek 62232115b10SPawel Jakub Dawidek nvout = nv_alloc(); 62332115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 62432115b10SPawel Jakub Dawidek nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 62532115b10SPawel Jakub Dawidek "token"); 626ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) { 627ce837469SPawel Jakub Dawidek /* 628ce837469SPawel Jakub Dawidek * The resuid field was not yet initialized. 629ce837469SPawel Jakub Dawidek * Because we do synchronization inside init_resuid(), it is 630ce837469SPawel Jakub Dawidek * possible that someone already initialized it, the function 631ce837469SPawel Jakub Dawidek * will return false then, but if we successfully initialized 632ce837469SPawel Jakub Dawidek * it, we will get true. True means that there were no writes 633ce837469SPawel Jakub Dawidek * to this resource yet and we want to inform secondary that 634ce837469SPawel Jakub Dawidek * synchronization is not needed by sending "virgin" argument. 635ce837469SPawel Jakub Dawidek */ 636ce837469SPawel Jakub Dawidek if (init_resuid(res)) 637ce837469SPawel Jakub Dawidek nv_add_int8(nvout, 1, "virgin"); 638ce837469SPawel Jakub Dawidek } 63932115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_resuid, "resuid"); 64032115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 64132115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 64232115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 64332115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 64432115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 64532115b10SPawel Jakub Dawidek res->hr_remoteaddr); 64632115b10SPawel Jakub Dawidek nv_free(nvout); 64732115b10SPawel Jakub Dawidek goto close; 64832115b10SPawel Jakub Dawidek } 6490d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, in, nvout, NULL, 0) < 0) { 65032115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 65132115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 65232115b10SPawel Jakub Dawidek res->hr_remoteaddr); 65332115b10SPawel Jakub Dawidek nv_free(nvout); 65432115b10SPawel Jakub Dawidek goto close; 65532115b10SPawel Jakub Dawidek } 65632115b10SPawel Jakub Dawidek nv_free(nvout); 6570d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 65832115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 65932115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 66032115b10SPawel Jakub Dawidek res->hr_remoteaddr); 66132115b10SPawel Jakub Dawidek goto close; 66232115b10SPawel Jakub Dawidek } 66332115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 66432115b10SPawel Jakub Dawidek if (errmsg != NULL) { 66532115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 66632115b10SPawel Jakub Dawidek nv_free(nvin); 66732115b10SPawel Jakub Dawidek goto close; 66832115b10SPawel Jakub Dawidek } 66932115b10SPawel Jakub Dawidek datasize = nv_get_int64(nvin, "datasize"); 67032115b10SPawel Jakub Dawidek if (datasize != res->hr_datasize) { 67132115b10SPawel Jakub Dawidek pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 67232115b10SPawel Jakub Dawidek (intmax_t)res->hr_datasize, (intmax_t)datasize); 67332115b10SPawel Jakub Dawidek nv_free(nvin); 67432115b10SPawel Jakub Dawidek goto close; 67532115b10SPawel Jakub Dawidek } 67632115b10SPawel Jakub Dawidek extentsize = nv_get_int32(nvin, "extentsize"); 67732115b10SPawel Jakub Dawidek if (extentsize != res->hr_extentsize) { 67832115b10SPawel Jakub Dawidek pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 67932115b10SPawel Jakub Dawidek (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 68032115b10SPawel Jakub Dawidek nv_free(nvin); 68132115b10SPawel Jakub Dawidek goto close; 68232115b10SPawel Jakub Dawidek } 68332115b10SPawel Jakub Dawidek res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 68432115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 68532115b10SPawel Jakub Dawidek res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 68606cbf549SPawel Jakub Dawidek if (nv_exists(nvin, "virgin")) { 68706cbf549SPawel Jakub Dawidek /* 68806cbf549SPawel Jakub Dawidek * Secondary was reinitialized, bump localcnt if it is 0 as 68906cbf549SPawel Jakub Dawidek * only we have the data. 69006cbf549SPawel Jakub Dawidek */ 69106cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY); 69206cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_localcnt == 0); 69306cbf549SPawel Jakub Dawidek 69406cbf549SPawel Jakub Dawidek if (res->hr_primary_localcnt == 0) { 69506cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0); 69606cbf549SPawel Jakub Dawidek 69706cbf549SPawel Jakub Dawidek mtx_lock(&metadata_lock); 69806cbf549SPawel Jakub Dawidek res->hr_primary_localcnt++; 69906cbf549SPawel Jakub Dawidek pjdlog_debug(1, "Increasing localcnt to %ju.", 70006cbf549SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt); 70106cbf549SPawel Jakub Dawidek (void)metadata_write(res); 70206cbf549SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 70306cbf549SPawel Jakub Dawidek } 70406cbf549SPawel Jakub Dawidek } 70532115b10SPawel Jakub Dawidek map = NULL; 70632115b10SPawel Jakub Dawidek mapsize = nv_get_uint32(nvin, "mapsize"); 70732115b10SPawel Jakub Dawidek if (mapsize > 0) { 70832115b10SPawel Jakub Dawidek map = malloc(mapsize); 70932115b10SPawel Jakub Dawidek if (map == NULL) { 71032115b10SPawel Jakub Dawidek pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 71132115b10SPawel Jakub Dawidek (uintmax_t)mapsize); 71232115b10SPawel Jakub Dawidek nv_free(nvin); 71332115b10SPawel Jakub Dawidek goto close; 71432115b10SPawel Jakub Dawidek } 71532115b10SPawel Jakub Dawidek /* 71632115b10SPawel Jakub Dawidek * Remote node have some dirty extents on its own, lets 71732115b10SPawel Jakub Dawidek * download its activemap. 71832115b10SPawel Jakub Dawidek */ 7190d9014f3SPawel Jakub Dawidek if (hast_proto_recv_data(res, out, nvin, map, 72032115b10SPawel Jakub Dawidek mapsize) < 0) { 72132115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 72232115b10SPawel Jakub Dawidek "Unable to receive remote activemap"); 72332115b10SPawel Jakub Dawidek nv_free(nvin); 72432115b10SPawel Jakub Dawidek free(map); 72532115b10SPawel Jakub Dawidek goto close; 72632115b10SPawel Jakub Dawidek } 72732115b10SPawel Jakub Dawidek /* 72832115b10SPawel Jakub Dawidek * Merge local and remote bitmaps. 72932115b10SPawel Jakub Dawidek */ 73032115b10SPawel Jakub Dawidek activemap_merge(res->hr_amp, map, mapsize); 73132115b10SPawel Jakub Dawidek free(map); 73232115b10SPawel Jakub Dawidek /* 73332115b10SPawel Jakub Dawidek * Now that we merged bitmaps from both nodes, flush it to the 73432115b10SPawel Jakub Dawidek * disk before we start to synchronize. 73532115b10SPawel Jakub Dawidek */ 73632115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 73732115b10SPawel Jakub Dawidek } 738584a9bc3SPawel Jakub Dawidek nv_free(nvin); 739ba2a8224SMikolaj Golub #ifdef notyet 74002dfe972SPawel Jakub Dawidek /* Setup directions. */ 74102dfe972SPawel Jakub Dawidek if (proto_send(out, NULL, 0) == -1) 74202dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 74302dfe972SPawel Jakub Dawidek if (proto_recv(in, NULL, 0) == -1) 74402dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction"); 745ba2a8224SMikolaj Golub #endif 74632115b10SPawel Jakub Dawidek pjdlog_info("Connected to %s.", res->hr_remoteaddr); 7470d9014f3SPawel Jakub Dawidek if (inp != NULL && outp != NULL) { 7480d9014f3SPawel Jakub Dawidek *inp = in; 7490d9014f3SPawel Jakub Dawidek *outp = out; 7500d9014f3SPawel Jakub Dawidek } else { 7510d9014f3SPawel Jakub Dawidek res->hr_remotein = in; 7520d9014f3SPawel Jakub Dawidek res->hr_remoteout = out; 7530d9014f3SPawel Jakub Dawidek } 7545bdff860SPawel Jakub Dawidek event_send(res, EVENT_CONNECT); 755ac0401e3SPawel Jakub Dawidek return (0); 7560d9014f3SPawel Jakub Dawidek close: 7572be8fd75SPawel Jakub Dawidek if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0) 7585bdff860SPawel Jakub Dawidek event_send(res, EVENT_SPLITBRAIN); 7590d9014f3SPawel Jakub Dawidek proto_close(out); 7600d9014f3SPawel Jakub Dawidek if (in != NULL) 7610d9014f3SPawel Jakub Dawidek proto_close(in); 762ac0401e3SPawel Jakub Dawidek return (error); 7630d9014f3SPawel Jakub Dawidek } 7640d9014f3SPawel Jakub Dawidek 7650d9014f3SPawel Jakub Dawidek static void 7660d9014f3SPawel Jakub Dawidek sync_start(void) 7670d9014f3SPawel Jakub Dawidek { 7680d9014f3SPawel Jakub Dawidek 76932115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 77032115b10SPawel Jakub Dawidek sync_inprogress = true; 77132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 77232115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 77332115b10SPawel Jakub Dawidek } 77432115b10SPawel Jakub Dawidek 77532115b10SPawel Jakub Dawidek static void 77655ce1e7cSPawel Jakub Dawidek sync_stop(void) 77755ce1e7cSPawel Jakub Dawidek { 77855ce1e7cSPawel Jakub Dawidek 77955ce1e7cSPawel Jakub Dawidek mtx_lock(&sync_lock); 78055ce1e7cSPawel Jakub Dawidek if (sync_inprogress) 78155ce1e7cSPawel Jakub Dawidek sync_inprogress = false; 78255ce1e7cSPawel Jakub Dawidek mtx_unlock(&sync_lock); 78355ce1e7cSPawel Jakub Dawidek } 78455ce1e7cSPawel Jakub Dawidek 78555ce1e7cSPawel Jakub Dawidek static void 78632115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res) 78732115b10SPawel Jakub Dawidek { 78832115b10SPawel Jakub Dawidek struct g_gate_ctl_create ggiocreate; 78932115b10SPawel Jakub Dawidek struct g_gate_ctl_cancel ggiocancel; 79032115b10SPawel Jakub Dawidek 79132115b10SPawel Jakub Dawidek /* 79232115b10SPawel Jakub Dawidek * We communicate with ggate via /dev/ggctl. Open it. 79332115b10SPawel Jakub Dawidek */ 79432115b10SPawel Jakub Dawidek res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 79532115b10SPawel Jakub Dawidek if (res->hr_ggatefd < 0) 79632115b10SPawel Jakub Dawidek primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 79732115b10SPawel Jakub Dawidek /* 79832115b10SPawel Jakub Dawidek * Create provider before trying to connect, as connection failure 79932115b10SPawel Jakub Dawidek * is not critical, but may take some time. 80032115b10SPawel Jakub Dawidek */ 8014e47b646SPawel Jakub Dawidek bzero(&ggiocreate, sizeof(ggiocreate)); 80232115b10SPawel Jakub Dawidek ggiocreate.gctl_version = G_GATE_VERSION; 80332115b10SPawel Jakub Dawidek ggiocreate.gctl_mediasize = res->hr_datasize; 80432115b10SPawel Jakub Dawidek ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 80532115b10SPawel Jakub Dawidek ggiocreate.gctl_flags = 0; 8062a49afacSPawel Jakub Dawidek ggiocreate.gctl_maxcount = 0; 80732115b10SPawel Jakub Dawidek ggiocreate.gctl_timeout = 0; 80832115b10SPawel Jakub Dawidek ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 80932115b10SPawel Jakub Dawidek snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 81032115b10SPawel Jakub Dawidek res->hr_provname); 81132115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 81232115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s created.", res->hr_provname); 81332115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocreate.gctl_unit; 81432115b10SPawel Jakub Dawidek return; 81532115b10SPawel Jakub Dawidek } 81632115b10SPawel Jakub Dawidek if (errno != EEXIST) { 81732115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to create hast/%s device", 81832115b10SPawel Jakub Dawidek res->hr_provname); 81932115b10SPawel Jakub Dawidek } 82032115b10SPawel Jakub Dawidek pjdlog_debug(1, 82132115b10SPawel Jakub Dawidek "Device hast/%s already exists, we will try to take it over.", 82232115b10SPawel Jakub Dawidek res->hr_provname); 82332115b10SPawel Jakub Dawidek /* 82432115b10SPawel Jakub Dawidek * If we received EEXIST, we assume that the process who created the 82532115b10SPawel Jakub Dawidek * provider died and didn't clean up. In that case we will start from 82632115b10SPawel Jakub Dawidek * where he left of. 82732115b10SPawel Jakub Dawidek */ 8284e47b646SPawel Jakub Dawidek bzero(&ggiocancel, sizeof(ggiocancel)); 82932115b10SPawel Jakub Dawidek ggiocancel.gctl_version = G_GATE_VERSION; 83032115b10SPawel Jakub Dawidek ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 83132115b10SPawel Jakub Dawidek snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 83232115b10SPawel Jakub Dawidek res->hr_provname); 83332115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 83432115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s recovered.", res->hr_provname); 83532115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocancel.gctl_unit; 83632115b10SPawel Jakub Dawidek return; 83732115b10SPawel Jakub Dawidek } 83832115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to take over hast/%s device", 83932115b10SPawel Jakub Dawidek res->hr_provname); 84032115b10SPawel Jakub Dawidek } 84132115b10SPawel Jakub Dawidek 84232115b10SPawel Jakub Dawidek void 84332115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res) 84432115b10SPawel Jakub Dawidek { 84532115b10SPawel Jakub Dawidek pthread_t td; 84632115b10SPawel Jakub Dawidek pid_t pid; 847bc7a916aSMikolaj Golub int error, mode, debuglevel; 84832115b10SPawel Jakub Dawidek 84932115b10SPawel Jakub Dawidek /* 85032ecf620SPawel Jakub Dawidek * Create communication channel for sending control commands from 85132ecf620SPawel Jakub Dawidek * parent to child. 85232115b10SPawel Jakub Dawidek */ 8530b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) { 854d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 85532115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8566be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 85732115b10SPawel Jakub Dawidek "Unable to create control sockets between parent and child"); 85832115b10SPawel Jakub Dawidek } 8595bdff860SPawel Jakub Dawidek /* 86032ecf620SPawel Jakub Dawidek * Create communication channel for sending events from child to parent. 8615bdff860SPawel Jakub Dawidek */ 8620b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) { 863d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 8645bdff860SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8655bdff860SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 8665bdff860SPawel Jakub Dawidek "Unable to create event sockets between child and parent"); 8675bdff860SPawel Jakub Dawidek } 86832ecf620SPawel Jakub Dawidek /* 86932ecf620SPawel Jakub Dawidek * Create communication channel for sending connection requests from 87032ecf620SPawel Jakub Dawidek * child to parent. 87132ecf620SPawel Jakub Dawidek */ 8720b626a28SPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) { 87332ecf620SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 87432ecf620SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 87532ecf620SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 87632ecf620SPawel Jakub Dawidek "Unable to create connection sockets between child and parent"); 87732ecf620SPawel Jakub Dawidek } 87832115b10SPawel Jakub Dawidek 87932115b10SPawel Jakub Dawidek pid = fork(); 88032115b10SPawel Jakub Dawidek if (pid < 0) { 881d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 88232115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8836be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_TEMPFAIL, "Unable to fork"); 88432115b10SPawel Jakub Dawidek } 88532115b10SPawel Jakub Dawidek 88632115b10SPawel Jakub Dawidek if (pid > 0) { 88732115b10SPawel Jakub Dawidek /* This is parent. */ 8885bdff860SPawel Jakub Dawidek /* Declare that we are receiver. */ 8895bdff860SPawel Jakub Dawidek proto_recv(res->hr_event, NULL, 0); 89032ecf620SPawel Jakub Dawidek proto_recv(res->hr_conn, NULL, 0); 891da1783eaSPawel Jakub Dawidek /* Declare that we are sender. */ 892da1783eaSPawel Jakub Dawidek proto_send(res->hr_ctrl, NULL, 0); 89332115b10SPawel Jakub Dawidek res->hr_workerpid = pid; 89432115b10SPawel Jakub Dawidek return; 89532115b10SPawel Jakub Dawidek } 896ecc99c89SPawel Jakub Dawidek 8975b41e644SPawel Jakub Dawidek gres = res; 898da1783eaSPawel Jakub Dawidek mode = pjdlog_mode_get(); 899bc7a916aSMikolaj Golub debuglevel = pjdlog_debug_get(); 90032115b10SPawel Jakub Dawidek 9015bdff860SPawel Jakub Dawidek /* Declare that we are sender. */ 9025bdff860SPawel Jakub Dawidek proto_send(res->hr_event, NULL, 0); 90332ecf620SPawel Jakub Dawidek proto_send(res->hr_conn, NULL, 0); 904da1783eaSPawel Jakub Dawidek /* Declare that we are receiver. */ 905da1783eaSPawel Jakub Dawidek proto_recv(res->hr_ctrl, NULL, 0); 906da1783eaSPawel Jakub Dawidek descriptors_cleanup(res); 907da1783eaSPawel Jakub Dawidek 908f463896eSPawel Jakub Dawidek descriptors_assert(res, mode); 909f463896eSPawel Jakub Dawidek 910da1783eaSPawel Jakub Dawidek pjdlog_init(mode); 911bc7a916aSMikolaj Golub pjdlog_debug_set(debuglevel); 912da1783eaSPawel Jakub Dawidek pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 913643080b7SPawel Jakub Dawidek setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role)); 9145bdff860SPawel Jakub Dawidek 91532115b10SPawel Jakub Dawidek init_local(res); 91632115b10SPawel Jakub Dawidek init_ggate(res); 91732115b10SPawel Jakub Dawidek init_environment(res); 918115f4e5cSPawel Jakub Dawidek 9190cddb12fSPawel Jakub Dawidek if (drop_privs(res) != 0) { 9206d7967deSPawel Jakub Dawidek cleanup(res); 9216d7967deSPawel Jakub Dawidek exit(EX_CONFIG); 9226d7967deSPawel Jakub Dawidek } 923f4c96f94SPawel Jakub Dawidek pjdlog_info("Privileges successfully dropped."); 9246d7967deSPawel Jakub Dawidek 9258b70e6aeSPawel Jakub Dawidek /* 9264a88128bSPawel Jakub Dawidek * Create the guard thread first, so we can handle signals from the 9274a88128bSPawel Jakub Dawidek * very begining. 9284a88128bSPawel Jakub Dawidek */ 9294a88128bSPawel Jakub Dawidek error = pthread_create(&td, NULL, guard_thread, res); 9302ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 9314a88128bSPawel Jakub Dawidek /* 9328b70e6aeSPawel Jakub Dawidek * Create the control thread before sending any event to the parent, 9338b70e6aeSPawel Jakub Dawidek * as we can deadlock when parent sends control request to worker, 9348b70e6aeSPawel Jakub Dawidek * but worker has no control thread started yet, so parent waits. 9358b70e6aeSPawel Jakub Dawidek * In the meantime worker sends an event to the parent, but parent 9368b70e6aeSPawel Jakub Dawidek * is unable to handle the event, because it waits for control 9378b70e6aeSPawel Jakub Dawidek * request response. 9388b70e6aeSPawel Jakub Dawidek */ 9398b70e6aeSPawel Jakub Dawidek error = pthread_create(&td, NULL, ctrl_thread, res); 9402ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 941ac0401e3SPawel Jakub Dawidek if (real_remote(res)) { 942ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL); 943ac0401e3SPawel Jakub Dawidek if (error == 0) { 9448b70e6aeSPawel Jakub Dawidek sync_start(); 945ac0401e3SPawel Jakub Dawidek } else if (error == EBUSY) { 946ac0401e3SPawel Jakub Dawidek time_t start = time(NULL); 947ac0401e3SPawel Jakub Dawidek 948ac0401e3SPawel Jakub Dawidek pjdlog_warning("Waiting for remote node to become %s for %ds.", 949ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_SECONDARY), 950ac0401e3SPawel Jakub Dawidek res->hr_timeout); 951ac0401e3SPawel Jakub Dawidek for (;;) { 952ac0401e3SPawel Jakub Dawidek sleep(1); 953ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL); 954ac0401e3SPawel Jakub Dawidek if (error != EBUSY) 955ac0401e3SPawel Jakub Dawidek break; 956ac0401e3SPawel Jakub Dawidek if (time(NULL) > start + res->hr_timeout) 957ac0401e3SPawel Jakub Dawidek break; 958ac0401e3SPawel Jakub Dawidek } 959ac0401e3SPawel Jakub Dawidek if (error == EBUSY) { 960ac0401e3SPawel Jakub Dawidek pjdlog_warning("Remote node is still %s, starting anyway.", 961ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_PRIMARY)); 962ac0401e3SPawel Jakub Dawidek } 963ac0401e3SPawel Jakub Dawidek } 964ac0401e3SPawel Jakub Dawidek } 96532115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_recv_thread, res); 9662ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 96732115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, local_send_thread, res); 9682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 96932115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_send_thread, res); 9702ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 97132115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_recv_thread, res); 9722ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 97332115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_send_thread, res); 9742ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 975ac0401e3SPawel Jakub Dawidek fullystarted = true; 9764a88128bSPawel Jakub Dawidek (void)sync_thread(res); 97732115b10SPawel Jakub Dawidek } 97832115b10SPawel Jakub Dawidek 97932115b10SPawel Jakub Dawidek static void 98032115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...) 98132115b10SPawel Jakub Dawidek { 98232115b10SPawel Jakub Dawidek char msg[1024]; 98332115b10SPawel Jakub Dawidek va_list ap; 98432115b10SPawel Jakub Dawidek int len; 98532115b10SPawel Jakub Dawidek 98632115b10SPawel Jakub Dawidek va_start(ap, fmt); 98732115b10SPawel Jakub Dawidek len = vsnprintf(msg, sizeof(msg), fmt, ap); 98832115b10SPawel Jakub Dawidek va_end(ap); 98932115b10SPawel Jakub Dawidek if ((size_t)len < sizeof(msg)) { 99032115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 99132115b10SPawel Jakub Dawidek case BIO_READ: 99232115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 99332115b10SPawel Jakub Dawidek "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 99432115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 99532115b10SPawel Jakub Dawidek break; 99632115b10SPawel Jakub Dawidek case BIO_DELETE: 99732115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 99832115b10SPawel Jakub Dawidek "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 99932115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 100032115b10SPawel Jakub Dawidek break; 100132115b10SPawel Jakub Dawidek case BIO_FLUSH: 100232115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 100332115b10SPawel Jakub Dawidek break; 100432115b10SPawel Jakub Dawidek case BIO_WRITE: 100532115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 100632115b10SPawel Jakub Dawidek "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 100732115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 100832115b10SPawel Jakub Dawidek break; 100932115b10SPawel Jakub Dawidek default: 101032115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 101132115b10SPawel Jakub Dawidek "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd); 101232115b10SPawel Jakub Dawidek break; 101332115b10SPawel Jakub Dawidek } 101432115b10SPawel Jakub Dawidek } 101532115b10SPawel Jakub Dawidek pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 101632115b10SPawel Jakub Dawidek } 101732115b10SPawel Jakub Dawidek 101832115b10SPawel Jakub Dawidek static void 101932115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp) 102032115b10SPawel Jakub Dawidek { 102132115b10SPawel Jakub Dawidek 102232115b10SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 102332115b10SPawel Jakub Dawidek /* 10245a58d22aSPawel Jakub Dawidek * Check for a race between dropping rlock and acquiring wlock - 102532115b10SPawel Jakub Dawidek * another thread can close connection in-between. 102632115b10SPawel Jakub Dawidek */ 102732115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 10282ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 10292ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 103032115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 103132115b10SPawel Jakub Dawidek return; 103232115b10SPawel Jakub Dawidek } 103332115b10SPawel Jakub Dawidek 10342ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 10352ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 103632115b10SPawel Jakub Dawidek 10378f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing incoming connection to %s.", 103832115b10SPawel Jakub Dawidek res->hr_remoteaddr); 103932115b10SPawel Jakub Dawidek proto_close(res->hr_remotein); 104032115b10SPawel Jakub Dawidek res->hr_remotein = NULL; 10418f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing outgoing connection to %s.", 104232115b10SPawel Jakub Dawidek res->hr_remoteaddr); 104332115b10SPawel Jakub Dawidek proto_close(res->hr_remoteout); 104432115b10SPawel Jakub Dawidek res->hr_remoteout = NULL; 104532115b10SPawel Jakub Dawidek 104632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 104732115b10SPawel Jakub Dawidek 10488f8c798cSPawel Jakub Dawidek pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 10498f8c798cSPawel Jakub Dawidek 105032115b10SPawel Jakub Dawidek /* 105132115b10SPawel Jakub Dawidek * Stop synchronization if in-progress. 105232115b10SPawel Jakub Dawidek */ 105355ce1e7cSPawel Jakub Dawidek sync_stop(); 10545b41e644SPawel Jakub Dawidek 10555bdff860SPawel Jakub Dawidek event_send(res, EVENT_DISCONNECT); 1056f7fe83f9SPawel Jakub Dawidek } 105732115b10SPawel Jakub Dawidek 105832115b10SPawel Jakub Dawidek /* 105932115b10SPawel Jakub Dawidek * Thread receives ggate I/O requests from the kernel and passes them to 106032115b10SPawel Jakub Dawidek * appropriate threads: 106132115b10SPawel Jakub Dawidek * WRITE - always goes to both local_send and remote_send threads 106232115b10SPawel Jakub Dawidek * READ (when the block is up-to-date on local component) - 106332115b10SPawel Jakub Dawidek * only local_send thread 106432115b10SPawel Jakub Dawidek * READ (when the block isn't up-to-date on local component) - 106532115b10SPawel Jakub Dawidek * only remote_send thread 106632115b10SPawel Jakub Dawidek * DELETE - always goes to both local_send and remote_send threads 106732115b10SPawel Jakub Dawidek * FLUSH - always goes to both local_send and remote_send threads 106832115b10SPawel Jakub Dawidek */ 106932115b10SPawel Jakub Dawidek static void * 107032115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg) 107132115b10SPawel Jakub Dawidek { 107232115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 107332115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 107432115b10SPawel Jakub Dawidek struct hio *hio; 107532115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 107632115b10SPawel Jakub Dawidek int error; 107732115b10SPawel Jakub Dawidek 107832115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 107932115b10SPawel Jakub Dawidek 108032115b10SPawel Jakub Dawidek for (;;) { 108132115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: Taking free request."); 108232115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 108332115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 108432115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 108532115b10SPawel Jakub Dawidek ggio->gctl_unit = res->hr_ggateunit; 108632115b10SPawel Jakub Dawidek ggio->gctl_length = MAXPHYS; 108732115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 108832115b10SPawel Jakub Dawidek pjdlog_debug(2, 108932115b10SPawel Jakub Dawidek "ggate_recv: (%p) Waiting for request from the kernel.", 109032115b10SPawel Jakub Dawidek hio); 109132115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) { 109232115b10SPawel Jakub Dawidek if (sigexit_received) 109332115b10SPawel Jakub Dawidek pthread_exit(NULL); 109432115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 109532115b10SPawel Jakub Dawidek } 109632115b10SPawel Jakub Dawidek error = ggio->gctl_error; 109732115b10SPawel Jakub Dawidek switch (error) { 109832115b10SPawel Jakub Dawidek case 0: 109932115b10SPawel Jakub Dawidek break; 110032115b10SPawel Jakub Dawidek case ECANCELED: 110132115b10SPawel Jakub Dawidek /* Exit gracefully. */ 110232115b10SPawel Jakub Dawidek if (!sigexit_received) { 110332115b10SPawel Jakub Dawidek pjdlog_debug(2, 110432115b10SPawel Jakub Dawidek "ggate_recv: (%p) Received cancel from the kernel.", 110532115b10SPawel Jakub Dawidek hio); 110632115b10SPawel Jakub Dawidek pjdlog_info("Received cancel from the kernel, exiting."); 110732115b10SPawel Jakub Dawidek } 110832115b10SPawel Jakub Dawidek pthread_exit(NULL); 110932115b10SPawel Jakub Dawidek case ENOMEM: 111032115b10SPawel Jakub Dawidek /* 111132115b10SPawel Jakub Dawidek * Buffer too small? Impossible, we allocate MAXPHYS 111232115b10SPawel Jakub Dawidek * bytes - request can't be bigger than that. 111332115b10SPawel Jakub Dawidek */ 111432115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 111532115b10SPawel Jakub Dawidek case ENXIO: 111632115b10SPawel Jakub Dawidek default: 111732115b10SPawel Jakub Dawidek primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 111832115b10SPawel Jakub Dawidek strerror(error)); 111932115b10SPawel Jakub Dawidek } 112032115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 112132115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 112232115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, 112332115b10SPawel Jakub Dawidek "ggate_recv: (%p) Request received from the kernel: ", 112432115b10SPawel Jakub Dawidek hio); 112532115b10SPawel Jakub Dawidek /* 112632115b10SPawel Jakub Dawidek * Inform all components about new write request. 112732115b10SPawel Jakub Dawidek * For read request prefer local component unless the given 112832115b10SPawel Jakub Dawidek * range is out-of-date, then use remote component. 112932115b10SPawel Jakub Dawidek */ 113032115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 113132115b10SPawel Jakub Dawidek case BIO_READ: 11323db86c39SPawel Jakub Dawidek res->hr_stat_read++; 113332115b10SPawel Jakub Dawidek pjdlog_debug(2, 113432115b10SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queue.", 113532115b10SPawel Jakub Dawidek hio); 113632115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 113732115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 113832115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 113932115b10SPawel Jakub Dawidek res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 114032115b10SPawel Jakub Dawidek /* 114132115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 114232115b10SPawel Jakub Dawidek * so handle request locally. 114332115b10SPawel Jakub Dawidek */ 114432115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 114532115b10SPawel Jakub Dawidek ncomp = 0; 114632115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == 114732115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY) */ { 11482ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == 114932115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY); 115032115b10SPawel Jakub Dawidek /* 115132115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 115232115b10SPawel Jakub Dawidek * so send request to the remote node. 115332115b10SPawel Jakub Dawidek */ 115432115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 115532115b10SPawel Jakub Dawidek ncomp = 1; 115632115b10SPawel Jakub Dawidek } 115732115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 115832115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 115932115b10SPawel Jakub Dawidek break; 116032115b10SPawel Jakub Dawidek case BIO_WRITE: 11613db86c39SPawel Jakub Dawidek res->hr_stat_write++; 11628a34134aSPawel Jakub Dawidek if (res->hr_resuid == 0 && 11638a34134aSPawel Jakub Dawidek res->hr_primary_localcnt == 0) { 11648a34134aSPawel Jakub Dawidek /* This is first write. */ 11659446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 1; 1166ce837469SPawel Jakub Dawidek } 116732115b10SPawel Jakub Dawidek for (;;) { 116832115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 116932115b10SPawel Jakub Dawidek if (rangelock_islocked(range_sync, 117032115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 117132115b10SPawel Jakub Dawidek pjdlog_debug(2, 117232115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu locked.", 117332115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 117432115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 117532115b10SPawel Jakub Dawidek range_regular_wait = true; 117632115b10SPawel Jakub Dawidek cv_wait(&range_regular_cond, &range_lock); 117732115b10SPawel Jakub Dawidek range_regular_wait = false; 117832115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 117932115b10SPawel Jakub Dawidek continue; 118032115b10SPawel Jakub Dawidek } 118132115b10SPawel Jakub Dawidek if (rangelock_add(range_regular, 118232115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length) < 0) { 118332115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 118432115b10SPawel Jakub Dawidek pjdlog_debug(2, 118532115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu is already locked, waiting.", 118632115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 118732115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 118832115b10SPawel Jakub Dawidek sleep(1); 118932115b10SPawel Jakub Dawidek continue; 119032115b10SPawel Jakub Dawidek } 119132115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 119232115b10SPawel Jakub Dawidek break; 119332115b10SPawel Jakub Dawidek } 119432115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 119532115b10SPawel Jakub Dawidek if (activemap_write_start(res->hr_amp, 119632115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 11973db86c39SPawel Jakub Dawidek res->hr_stat_activemap_update++; 119832115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 119932115b10SPawel Jakub Dawidek } 120032115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 120132115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 120232115b10SPawel Jakub Dawidek case BIO_DELETE: 120332115b10SPawel Jakub Dawidek case BIO_FLUSH: 12043db86c39SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 12053db86c39SPawel Jakub Dawidek case BIO_DELETE: 12063db86c39SPawel Jakub Dawidek res->hr_stat_delete++; 12073db86c39SPawel Jakub Dawidek break; 12083db86c39SPawel Jakub Dawidek case BIO_FLUSH: 12093db86c39SPawel Jakub Dawidek res->hr_stat_flush++; 12103db86c39SPawel Jakub Dawidek break; 12113db86c39SPawel Jakub Dawidek } 121232115b10SPawel Jakub Dawidek pjdlog_debug(2, 1213e3feec94SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queue.", 121432115b10SPawel Jakub Dawidek hio); 121532115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, ncomps); 121632115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 121732115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ii); 121832115b10SPawel Jakub Dawidek break; 121932115b10SPawel Jakub Dawidek } 122032115b10SPawel Jakub Dawidek } 122132115b10SPawel Jakub Dawidek /* NOTREACHED */ 122232115b10SPawel Jakub Dawidek return (NULL); 122332115b10SPawel Jakub Dawidek } 122432115b10SPawel Jakub Dawidek 122532115b10SPawel Jakub Dawidek /* 122632115b10SPawel Jakub Dawidek * Thread reads from or writes to local component. 122732115b10SPawel Jakub Dawidek * If local read fails, it redirects it to remote_send thread. 122832115b10SPawel Jakub Dawidek */ 122932115b10SPawel Jakub Dawidek static void * 123032115b10SPawel Jakub Dawidek local_send_thread(void *arg) 123132115b10SPawel Jakub Dawidek { 123232115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 123332115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 123432115b10SPawel Jakub Dawidek struct hio *hio; 123532115b10SPawel Jakub Dawidek unsigned int ncomp, rncomp; 123632115b10SPawel Jakub Dawidek ssize_t ret; 123732115b10SPawel Jakub Dawidek 123832115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 123932115b10SPawel Jakub Dawidek ncomp = 0; 124032115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 124132115b10SPawel Jakub Dawidek rncomp = 1; 124232115b10SPawel Jakub Dawidek 124332115b10SPawel Jakub Dawidek for (;;) { 124432115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: Taking request."); 1245448efa94SPawel Jakub Dawidek QUEUE_TAKE1(hio, send, ncomp, 0); 124632115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: (%p) Got request.", hio); 124732115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 124832115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 124932115b10SPawel Jakub Dawidek case BIO_READ: 125032115b10SPawel Jakub Dawidek ret = pread(res->hr_localfd, ggio->gctl_data, 125132115b10SPawel Jakub Dawidek ggio->gctl_length, 125232115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 125332115b10SPawel Jakub Dawidek if (ret == ggio->gctl_length) 125432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1255a01a750fSMikolaj Golub else if (!ISSYNCREQ(hio)) { 125632115b10SPawel Jakub Dawidek /* 125732115b10SPawel Jakub Dawidek * If READ failed, try to read from remote node. 125832115b10SPawel Jakub Dawidek */ 1259cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 1260cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1261cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s), trying remote node. ", 1262cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1263cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 1264cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1265cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd), trying remote node. ", 1266fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1267cd7b7ee5SPawel Jakub Dawidek } 126832115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, rncomp); 126932115b10SPawel Jakub Dawidek continue; 127032115b10SPawel Jakub Dawidek } 127132115b10SPawel Jakub Dawidek break; 127232115b10SPawel Jakub Dawidek case BIO_WRITE: 127332115b10SPawel Jakub Dawidek ret = pwrite(res->hr_localfd, ggio->gctl_data, 127432115b10SPawel Jakub Dawidek ggio->gctl_length, 127532115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 1276cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 127732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1278cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1279cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1280cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1281cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 128232115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = EIO; 1283cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1284cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd): ", 1285fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1286cd7b7ee5SPawel Jakub Dawidek } else { 128732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1288cd7b7ee5SPawel Jakub Dawidek } 128932115b10SPawel Jakub Dawidek break; 129032115b10SPawel Jakub Dawidek case BIO_DELETE: 129132115b10SPawel Jakub Dawidek ret = g_delete(res->hr_localfd, 129232115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff, 129332115b10SPawel Jakub Dawidek ggio->gctl_length); 1294cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 129532115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1296cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1297cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1298cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1299cd7b7ee5SPawel Jakub Dawidek } else { 130032115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1301cd7b7ee5SPawel Jakub Dawidek } 130232115b10SPawel Jakub Dawidek break; 130332115b10SPawel Jakub Dawidek case BIO_FLUSH: 130412daf727SPawel Jakub Dawidek if (!res->hr_localflush) { 130512daf727SPawel Jakub Dawidek ret = -1; 130612daf727SPawel Jakub Dawidek errno = EOPNOTSUPP; 130712daf727SPawel Jakub Dawidek break; 130812daf727SPawel Jakub Dawidek } 130932115b10SPawel Jakub Dawidek ret = g_flush(res->hr_localfd); 1310cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 131112daf727SPawel Jakub Dawidek if (errno == EOPNOTSUPP) 131212daf727SPawel Jakub Dawidek res->hr_localflush = false; 131332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1314cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1315cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1316cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1317cd7b7ee5SPawel Jakub Dawidek } else { 131832115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1319cd7b7ee5SPawel Jakub Dawidek } 132032115b10SPawel Jakub Dawidek break; 132132115b10SPawel Jakub Dawidek } 1322*43b8675bSPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 1323*43b8675bSPawel Jakub Dawidek continue; 132432115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 132532115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 132632115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 132732115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 132832115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 132932115b10SPawel Jakub Dawidek } else { 133032115b10SPawel Jakub Dawidek pjdlog_debug(2, 133132115b10SPawel Jakub Dawidek "local_send: (%p) Moving request to the done queue.", 133232115b10SPawel Jakub Dawidek hio); 133332115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 133432115b10SPawel Jakub Dawidek } 133532115b10SPawel Jakub Dawidek } 133632115b10SPawel Jakub Dawidek /* NOTREACHED */ 133732115b10SPawel Jakub Dawidek return (NULL); 133832115b10SPawel Jakub Dawidek } 133932115b10SPawel Jakub Dawidek 1340448efa94SPawel Jakub Dawidek static void 1341448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp) 1342448efa94SPawel Jakub Dawidek { 1343448efa94SPawel Jakub Dawidek struct nv *nv; 1344448efa94SPawel Jakub Dawidek 134521e7bc5eSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 134621e7bc5eSPawel Jakub Dawidek 134721e7bc5eSPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 134821e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1349448efa94SPawel Jakub Dawidek return; 135021e7bc5eSPawel Jakub Dawidek } 1351448efa94SPawel Jakub Dawidek 13522ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 13532ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 1354448efa94SPawel Jakub Dawidek 1355448efa94SPawel Jakub Dawidek nv = nv_alloc(); 1356448efa94SPawel Jakub Dawidek nv_add_uint8(nv, HIO_KEEPALIVE, "cmd"); 1357448efa94SPawel Jakub Dawidek if (nv_error(nv) != 0) { 135821e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1359448efa94SPawel Jakub Dawidek nv_free(nv); 1360448efa94SPawel Jakub Dawidek pjdlog_debug(1, 1361448efa94SPawel Jakub Dawidek "keepalive_send: Unable to prepare header to send."); 1362448efa94SPawel Jakub Dawidek return; 1363448efa94SPawel Jakub Dawidek } 1364448efa94SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) { 136521e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1366448efa94SPawel Jakub Dawidek pjdlog_common(LOG_DEBUG, 1, errno, 1367448efa94SPawel Jakub Dawidek "keepalive_send: Unable to send request"); 1368448efa94SPawel Jakub Dawidek nv_free(nv); 1369448efa94SPawel Jakub Dawidek remote_close(res, ncomp); 1370448efa94SPawel Jakub Dawidek return; 1371448efa94SPawel Jakub Dawidek } 137221e7bc5eSPawel Jakub Dawidek 137321e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1374448efa94SPawel Jakub Dawidek nv_free(nv); 1375448efa94SPawel Jakub Dawidek pjdlog_debug(2, "keepalive_send: Request sent."); 1376448efa94SPawel Jakub Dawidek } 1377448efa94SPawel Jakub Dawidek 137832115b10SPawel Jakub Dawidek /* 137932115b10SPawel Jakub Dawidek * Thread sends request to secondary node. 138032115b10SPawel Jakub Dawidek */ 138132115b10SPawel Jakub Dawidek static void * 138232115b10SPawel Jakub Dawidek remote_send_thread(void *arg) 138332115b10SPawel Jakub Dawidek { 138432115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 138532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1386448efa94SPawel Jakub Dawidek time_t lastcheck, now; 138732115b10SPawel Jakub Dawidek struct hio *hio; 138832115b10SPawel Jakub Dawidek struct nv *nv; 138932115b10SPawel Jakub Dawidek unsigned int ncomp; 139032115b10SPawel Jakub Dawidek bool wakeup; 139132115b10SPawel Jakub Dawidek uint64_t offset, length; 139232115b10SPawel Jakub Dawidek uint8_t cmd; 139332115b10SPawel Jakub Dawidek void *data; 139432115b10SPawel Jakub Dawidek 139532115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 139632115b10SPawel Jakub Dawidek ncomp = 1; 1397448efa94SPawel Jakub Dawidek lastcheck = time(NULL); 139832115b10SPawel Jakub Dawidek 139932115b10SPawel Jakub Dawidek for (;;) { 140032115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: Taking request."); 14018d7dcf14SMikolaj Golub QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE); 1402448efa94SPawel Jakub Dawidek if (hio == NULL) { 1403448efa94SPawel Jakub Dawidek now = time(NULL); 14048d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) { 1405448efa94SPawel Jakub Dawidek keepalive_send(res, ncomp); 1406448efa94SPawel Jakub Dawidek lastcheck = now; 1407448efa94SPawel Jakub Dawidek } 1408448efa94SPawel Jakub Dawidek continue; 1409448efa94SPawel Jakub Dawidek } 141032115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 141132115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 141232115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 141332115b10SPawel Jakub Dawidek case BIO_READ: 141432115b10SPawel Jakub Dawidek cmd = HIO_READ; 141532115b10SPawel Jakub Dawidek data = NULL; 141632115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 141732115b10SPawel Jakub Dawidek length = ggio->gctl_length; 141832115b10SPawel Jakub Dawidek break; 141932115b10SPawel Jakub Dawidek case BIO_WRITE: 142032115b10SPawel Jakub Dawidek cmd = HIO_WRITE; 142132115b10SPawel Jakub Dawidek data = ggio->gctl_data; 142232115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 142332115b10SPawel Jakub Dawidek length = ggio->gctl_length; 142432115b10SPawel Jakub Dawidek break; 142532115b10SPawel Jakub Dawidek case BIO_DELETE: 142632115b10SPawel Jakub Dawidek cmd = HIO_DELETE; 142732115b10SPawel Jakub Dawidek data = NULL; 142832115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 142932115b10SPawel Jakub Dawidek length = ggio->gctl_length; 143032115b10SPawel Jakub Dawidek break; 143132115b10SPawel Jakub Dawidek case BIO_FLUSH: 143232115b10SPawel Jakub Dawidek cmd = HIO_FLUSH; 143332115b10SPawel Jakub Dawidek data = NULL; 143432115b10SPawel Jakub Dawidek offset = 0; 143532115b10SPawel Jakub Dawidek length = 0; 143632115b10SPawel Jakub Dawidek break; 143732115b10SPawel Jakub Dawidek default: 143809c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition"); 143932115b10SPawel Jakub Dawidek } 144032115b10SPawel Jakub Dawidek nv = nv_alloc(); 144132115b10SPawel Jakub Dawidek nv_add_uint8(nv, cmd, "cmd"); 144232115b10SPawel Jakub Dawidek nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 144332115b10SPawel Jakub Dawidek nv_add_uint64(nv, offset, "offset"); 144432115b10SPawel Jakub Dawidek nv_add_uint64(nv, length, "length"); 144532115b10SPawel Jakub Dawidek if (nv_error(nv) != 0) { 144632115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = nv_error(nv); 144732115b10SPawel Jakub Dawidek pjdlog_debug(2, 144832115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to prepare header to send.", 144932115b10SPawel Jakub Dawidek hio); 145032115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 145132115b10SPawel Jakub Dawidek "Unable to prepare header to send (%s): ", 145232115b10SPawel Jakub Dawidek strerror(nv_error(nv))); 145332115b10SPawel Jakub Dawidek /* Move failed request immediately to the done queue. */ 145432115b10SPawel Jakub Dawidek goto done_queue; 145532115b10SPawel Jakub Dawidek } 145632115b10SPawel Jakub Dawidek /* 145732115b10SPawel Jakub Dawidek * Protect connection from disappearing. 145832115b10SPawel Jakub Dawidek */ 145932115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 146032115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 146132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 146232115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = ENOTCONN; 146332115b10SPawel Jakub Dawidek goto done_queue; 146432115b10SPawel Jakub Dawidek } 146532115b10SPawel Jakub Dawidek /* 146632115b10SPawel Jakub Dawidek * Move the request to recv queue before sending it, because 146732115b10SPawel Jakub Dawidek * in different order we can get reply before we move request 146832115b10SPawel Jakub Dawidek * to recv queue. 146932115b10SPawel Jakub Dawidek */ 14701212a85cSPawel Jakub Dawidek pjdlog_debug(2, 14711212a85cSPawel Jakub Dawidek "remote_send: (%p) Moving request to the recv queue.", 14721212a85cSPawel Jakub Dawidek hio); 147332115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 147432115b10SPawel Jakub Dawidek wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 147532115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 147632115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 147732115b10SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, data, 147832115b10SPawel Jakub Dawidek data != NULL ? length : 0) < 0) { 147932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 148032115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 148132115b10SPawel Jakub Dawidek pjdlog_debug(2, 148232115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to send request.", hio); 148332115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 148432115b10SPawel Jakub Dawidek "Unable to send request (%s): ", 148532115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 1486ee087cdfSPawel Jakub Dawidek remote_close(res, ncomp); 148732115b10SPawel Jakub Dawidek /* 148832115b10SPawel Jakub Dawidek * Take request back from the receive queue and move 148932115b10SPawel Jakub Dawidek * it immediately to the done queue. 149032115b10SPawel Jakub Dawidek */ 149132115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 14921212a85cSPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 14931212a85cSPawel Jakub Dawidek hio_next[ncomp]); 149432115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 149532115b10SPawel Jakub Dawidek goto done_queue; 149632115b10SPawel Jakub Dawidek } 149732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 149832115b10SPawel Jakub Dawidek nv_free(nv); 149932115b10SPawel Jakub Dawidek if (wakeup) 150032115b10SPawel Jakub Dawidek cv_signal(&hio_recv_list_cond[ncomp]); 150132115b10SPawel Jakub Dawidek continue; 150232115b10SPawel Jakub Dawidek done_queue: 150332115b10SPawel Jakub Dawidek nv_free(nv); 150432115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 150532115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 150632115b10SPawel Jakub Dawidek continue; 150732115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 150832115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 150932115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 151032115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 151132115b10SPawel Jakub Dawidek continue; 151232115b10SPawel Jakub Dawidek } 151332115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 151432115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 151532115b10SPawel Jakub Dawidek if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 151632115b10SPawel Jakub Dawidek ggio->gctl_length)) { 151732115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 151832115b10SPawel Jakub Dawidek } 151932115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 152032115b10SPawel Jakub Dawidek } 152132115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 152232115b10SPawel Jakub Dawidek continue; 152332115b10SPawel Jakub Dawidek pjdlog_debug(2, 152432115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the done queue.", 152532115b10SPawel Jakub Dawidek hio); 152632115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 152732115b10SPawel Jakub Dawidek } 152832115b10SPawel Jakub Dawidek /* NOTREACHED */ 152932115b10SPawel Jakub Dawidek return (NULL); 153032115b10SPawel Jakub Dawidek } 153132115b10SPawel Jakub Dawidek 153232115b10SPawel Jakub Dawidek /* 153332115b10SPawel Jakub Dawidek * Thread receives answer from secondary node and passes it to ggate_send 153432115b10SPawel Jakub Dawidek * thread. 153532115b10SPawel Jakub Dawidek */ 153632115b10SPawel Jakub Dawidek static void * 153732115b10SPawel Jakub Dawidek remote_recv_thread(void *arg) 153832115b10SPawel Jakub Dawidek { 153932115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 154032115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 154132115b10SPawel Jakub Dawidek struct hio *hio; 154232115b10SPawel Jakub Dawidek struct nv *nv; 154332115b10SPawel Jakub Dawidek unsigned int ncomp; 154432115b10SPawel Jakub Dawidek uint64_t seq; 154532115b10SPawel Jakub Dawidek int error; 154632115b10SPawel Jakub Dawidek 154732115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 154832115b10SPawel Jakub Dawidek ncomp = 1; 154932115b10SPawel Jakub Dawidek 155032115b10SPawel Jakub Dawidek for (;;) { 155132115b10SPawel Jakub Dawidek /* Wait until there is anything to receive. */ 155232115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 155332115b10SPawel Jakub Dawidek while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 155432115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_recv: No requests, waiting."); 155532115b10SPawel Jakub Dawidek cv_wait(&hio_recv_list_cond[ncomp], 155632115b10SPawel Jakub Dawidek &hio_recv_list_lock[ncomp]); 155732115b10SPawel Jakub Dawidek } 155832115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 155932115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 156032115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 156132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 156232115b10SPawel Jakub Dawidek /* 156332115b10SPawel Jakub Dawidek * Connection is dead, so move all pending requests to 156432115b10SPawel Jakub Dawidek * the done queue (one-by-one). 156532115b10SPawel Jakub Dawidek */ 156632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 156732115b10SPawel Jakub Dawidek hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 15682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(hio != NULL); 156932115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 157032115b10SPawel Jakub Dawidek hio_next[ncomp]); 157132115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 157232115b10SPawel Jakub Dawidek goto done_queue; 157332115b10SPawel Jakub Dawidek } 157432115b10SPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) { 157532115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 157632115b10SPawel Jakub Dawidek "Unable to receive reply header"); 157732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 157832115b10SPawel Jakub Dawidek remote_close(res, ncomp); 157932115b10SPawel Jakub Dawidek continue; 158032115b10SPawel Jakub Dawidek } 158132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 158232115b10SPawel Jakub Dawidek seq = nv_get_uint64(nv, "seq"); 158332115b10SPawel Jakub Dawidek if (seq == 0) { 158432115b10SPawel Jakub Dawidek pjdlog_error("Header contains no 'seq' field."); 158532115b10SPawel Jakub Dawidek nv_free(nv); 158632115b10SPawel Jakub Dawidek continue; 158732115b10SPawel Jakub Dawidek } 158832115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 158932115b10SPawel Jakub Dawidek TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 159032115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_seq == seq) { 159132115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 159232115b10SPawel Jakub Dawidek hio_next[ncomp]); 159332115b10SPawel Jakub Dawidek break; 159432115b10SPawel Jakub Dawidek } 159532115b10SPawel Jakub Dawidek } 159632115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 159732115b10SPawel Jakub Dawidek if (hio == NULL) { 159832115b10SPawel Jakub Dawidek pjdlog_error("Found no request matching received 'seq' field (%ju).", 159932115b10SPawel Jakub Dawidek (uintmax_t)seq); 160032115b10SPawel Jakub Dawidek nv_free(nv); 160132115b10SPawel Jakub Dawidek continue; 160232115b10SPawel Jakub Dawidek } 16031212a85cSPawel Jakub Dawidek ggio = &hio->hio_ggio; 160432115b10SPawel Jakub Dawidek error = nv_get_int16(nv, "error"); 160532115b10SPawel Jakub Dawidek if (error != 0) { 160632115b10SPawel Jakub Dawidek /* Request failed on remote side. */ 160772089204SPawel Jakub Dawidek hio->hio_errors[ncomp] = error; 16081212a85cSPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1609cd7b7ee5SPawel Jakub Dawidek "Remote request failed (%s): ", strerror(error)); 161032115b10SPawel Jakub Dawidek nv_free(nv); 161132115b10SPawel Jakub Dawidek goto done_queue; 161232115b10SPawel Jakub Dawidek } 161332115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 161432115b10SPawel Jakub Dawidek case BIO_READ: 161532115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 161632115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 161732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 161832115b10SPawel Jakub Dawidek nv_free(nv); 161932115b10SPawel Jakub Dawidek goto done_queue; 162032115b10SPawel Jakub Dawidek } 162132115b10SPawel Jakub Dawidek if (hast_proto_recv_data(res, res->hr_remotein, nv, 162232115b10SPawel Jakub Dawidek ggio->gctl_data, ggio->gctl_length) < 0) { 162332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 162432115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 162532115b10SPawel Jakub Dawidek "Unable to receive reply data"); 162632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 162732115b10SPawel Jakub Dawidek nv_free(nv); 162832115b10SPawel Jakub Dawidek remote_close(res, ncomp); 162932115b10SPawel Jakub Dawidek goto done_queue; 163032115b10SPawel Jakub Dawidek } 163132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 163232115b10SPawel Jakub Dawidek break; 163332115b10SPawel Jakub Dawidek case BIO_WRITE: 163432115b10SPawel Jakub Dawidek case BIO_DELETE: 163532115b10SPawel Jakub Dawidek case BIO_FLUSH: 163632115b10SPawel Jakub Dawidek break; 163732115b10SPawel Jakub Dawidek default: 163809c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition"); 163932115b10SPawel Jakub Dawidek } 164032115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 164132115b10SPawel Jakub Dawidek nv_free(nv); 164232115b10SPawel Jakub Dawidek done_queue: 1643*43b8675bSPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 1644*43b8675bSPawel Jakub Dawidek continue; 164532115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 164632115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 164732115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 164832115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 164932115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 165032115b10SPawel Jakub Dawidek } else { 165132115b10SPawel Jakub Dawidek pjdlog_debug(2, 165232115b10SPawel Jakub Dawidek "remote_recv: (%p) Moving request to the done queue.", 165332115b10SPawel Jakub Dawidek hio); 165432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 165532115b10SPawel Jakub Dawidek } 165632115b10SPawel Jakub Dawidek } 165732115b10SPawel Jakub Dawidek /* NOTREACHED */ 165832115b10SPawel Jakub Dawidek return (NULL); 165932115b10SPawel Jakub Dawidek } 166032115b10SPawel Jakub Dawidek 166132115b10SPawel Jakub Dawidek /* 166232115b10SPawel Jakub Dawidek * Thread sends answer to the kernel. 166332115b10SPawel Jakub Dawidek */ 166432115b10SPawel Jakub Dawidek static void * 166532115b10SPawel Jakub Dawidek ggate_send_thread(void *arg) 166632115b10SPawel Jakub Dawidek { 166732115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 166832115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 166932115b10SPawel Jakub Dawidek struct hio *hio; 167032115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 167132115b10SPawel Jakub Dawidek 167232115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 167332115b10SPawel Jakub Dawidek 167432115b10SPawel Jakub Dawidek for (;;) { 167532115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: Taking request."); 167632115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, done); 167732115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 167832115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 167932115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 168032115b10SPawel Jakub Dawidek if (hio->hio_errors[ii] == 0) { 168132115b10SPawel Jakub Dawidek /* 168232115b10SPawel Jakub Dawidek * One successful request is enough to declare 168332115b10SPawel Jakub Dawidek * success. 168432115b10SPawel Jakub Dawidek */ 168532115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 168632115b10SPawel Jakub Dawidek break; 168732115b10SPawel Jakub Dawidek } 168832115b10SPawel Jakub Dawidek } 168932115b10SPawel Jakub Dawidek if (ii == ncomps) { 169032115b10SPawel Jakub Dawidek /* 169132115b10SPawel Jakub Dawidek * None of the requests were successful. 1692b068d5aaSMikolaj Golub * Use the error from local component except the 1693b068d5aaSMikolaj Golub * case when we did only remote request. 169432115b10SPawel Jakub Dawidek */ 1695b068d5aaSMikolaj Golub if (ggio->gctl_cmd == BIO_READ && 1696b068d5aaSMikolaj Golub res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) 1697b068d5aaSMikolaj Golub ggio->gctl_error = hio->hio_errors[1]; 1698b068d5aaSMikolaj Golub else 169932115b10SPawel Jakub Dawidek ggio->gctl_error = hio->hio_errors[0]; 170032115b10SPawel Jakub Dawidek } 170132115b10SPawel Jakub Dawidek if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 170232115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 1703d9f039e0SMikolaj Golub if (activemap_write_complete(res->hr_amp, 1704d9f039e0SMikolaj Golub ggio->gctl_offset, ggio->gctl_length)) { 1705d9f039e0SMikolaj Golub res->hr_stat_activemap_update++; 1706d9f039e0SMikolaj Golub (void)hast_activemap_flush(res); 1707d9f039e0SMikolaj Golub } 170832115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 170932115b10SPawel Jakub Dawidek } 171032115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 171132115b10SPawel Jakub Dawidek /* 171232115b10SPawel Jakub Dawidek * Unlock range we locked. 171332115b10SPawel Jakub Dawidek */ 171432115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 171532115b10SPawel Jakub Dawidek rangelock_del(range_regular, ggio->gctl_offset, 171632115b10SPawel Jakub Dawidek ggio->gctl_length); 171732115b10SPawel Jakub Dawidek if (range_sync_wait) 171832115b10SPawel Jakub Dawidek cv_signal(&range_sync_cond); 171932115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 172032115b10SPawel Jakub Dawidek /* 172132115b10SPawel Jakub Dawidek * Bump local count if this is first write after 172232115b10SPawel Jakub Dawidek * connection failure with remote node. 172332115b10SPawel Jakub Dawidek */ 172432115b10SPawel Jakub Dawidek ncomp = 1; 172532115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 172632115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 172732115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 172832115b10SPawel Jakub Dawidek if (res->hr_primary_localcnt == 172932115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt) { 173032115b10SPawel Jakub Dawidek res->hr_primary_localcnt++; 173132115b10SPawel Jakub Dawidek pjdlog_debug(1, 173232115b10SPawel Jakub Dawidek "Increasing localcnt to %ju.", 173332115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt); 173432115b10SPawel Jakub Dawidek (void)metadata_write(res); 173532115b10SPawel Jakub Dawidek } 173632115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 173732115b10SPawel Jakub Dawidek } 173832115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 173932115b10SPawel Jakub Dawidek } 174032115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) 174132115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 174232115b10SPawel Jakub Dawidek pjdlog_debug(2, 174332115b10SPawel Jakub Dawidek "ggate_send: (%p) Moving request to the free queue.", hio); 174432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 174532115b10SPawel Jakub Dawidek } 174632115b10SPawel Jakub Dawidek /* NOTREACHED */ 174732115b10SPawel Jakub Dawidek return (NULL); 174832115b10SPawel Jakub Dawidek } 174932115b10SPawel Jakub Dawidek 175032115b10SPawel Jakub Dawidek /* 175132115b10SPawel Jakub Dawidek * Thread synchronize local and remote components. 175232115b10SPawel Jakub Dawidek */ 175332115b10SPawel Jakub Dawidek static void * 175432115b10SPawel Jakub Dawidek sync_thread(void *arg __unused) 175532115b10SPawel Jakub Dawidek { 175632115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 175732115b10SPawel Jakub Dawidek struct hio *hio; 175832115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1759fa356f6cSPawel Jakub Dawidek struct timeval tstart, tend, tdiff; 176032115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 176132115b10SPawel Jakub Dawidek off_t offset, length, synced; 176232115b10SPawel Jakub Dawidek bool dorewind; 176332115b10SPawel Jakub Dawidek int syncext; 176432115b10SPawel Jakub Dawidek 176532115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 176632115b10SPawel Jakub Dawidek dorewind = true; 1767b9cf0cf5SPawel Jakub Dawidek synced = 0; 1768b9cf0cf5SPawel Jakub Dawidek offset = -1; 176932115b10SPawel Jakub Dawidek 177032115b10SPawel Jakub Dawidek for (;;) { 177132115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 1772b9cf0cf5SPawel Jakub Dawidek if (offset >= 0 && !sync_inprogress) { 1773fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1774fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1775fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization interrupted after %#.0T. " 1776fa356f6cSPawel Jakub Dawidek "%NB synchronized so far.", &tdiff, 177753d9b386SPawel Jakub Dawidek (intmax_t)synced); 17785bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCINTR); 177953d9b386SPawel Jakub Dawidek } 178032115b10SPawel Jakub Dawidek while (!sync_inprogress) { 178132115b10SPawel Jakub Dawidek dorewind = true; 178232115b10SPawel Jakub Dawidek synced = 0; 178332115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 178432115b10SPawel Jakub Dawidek } 178532115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 178632115b10SPawel Jakub Dawidek /* 178732115b10SPawel Jakub Dawidek * Obtain offset at which we should synchronize. 178832115b10SPawel Jakub Dawidek * Rewind synchronization if needed. 178932115b10SPawel Jakub Dawidek */ 179032115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 179132115b10SPawel Jakub Dawidek if (dorewind) 179232115b10SPawel Jakub Dawidek activemap_sync_rewind(res->hr_amp); 179332115b10SPawel Jakub Dawidek offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 179432115b10SPawel Jakub Dawidek if (syncext != -1) { 179532115b10SPawel Jakub Dawidek /* 179632115b10SPawel Jakub Dawidek * We synchronized entire syncext extent, we can mark 179732115b10SPawel Jakub Dawidek * it as clean now. 179832115b10SPawel Jakub Dawidek */ 179932115b10SPawel Jakub Dawidek if (activemap_extent_complete(res->hr_amp, syncext)) 180032115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 180132115b10SPawel Jakub Dawidek } 180232115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 180332115b10SPawel Jakub Dawidek if (dorewind) { 180432115b10SPawel Jakub Dawidek dorewind = false; 180532115b10SPawel Jakub Dawidek if (offset < 0) 180632115b10SPawel Jakub Dawidek pjdlog_info("Nodes are in sync."); 180732115b10SPawel Jakub Dawidek else { 1808fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization started. %NB to go.", 1809fa356f6cSPawel Jakub Dawidek (intmax_t)(res->hr_extentsize * 181032115b10SPawel Jakub Dawidek activemap_ndirty(res->hr_amp))); 18115bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCSTART); 1812fa356f6cSPawel Jakub Dawidek gettimeofday(&tstart, NULL); 181332115b10SPawel Jakub Dawidek } 181432115b10SPawel Jakub Dawidek } 181532115b10SPawel Jakub Dawidek if (offset < 0) { 181655ce1e7cSPawel Jakub Dawidek sync_stop(); 181732115b10SPawel Jakub Dawidek pjdlog_debug(1, "Nothing to synchronize."); 181832115b10SPawel Jakub Dawidek /* 181932115b10SPawel Jakub Dawidek * Synchronization complete, make both localcnt and 182032115b10SPawel Jakub Dawidek * remotecnt equal. 182132115b10SPawel Jakub Dawidek */ 182232115b10SPawel Jakub Dawidek ncomp = 1; 182332115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 182432115b10SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 182532115b10SPawel Jakub Dawidek if (synced > 0) { 1826fa356f6cSPawel Jakub Dawidek int64_t bps; 1827fa356f6cSPawel Jakub Dawidek 1828fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1829fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1830fa356f6cSPawel Jakub Dawidek bps = (int64_t)((double)synced / 1831fa356f6cSPawel Jakub Dawidek ((double)tdiff.tv_sec + 1832fa356f6cSPawel Jakub Dawidek (double)tdiff.tv_usec / 1000000)); 183332115b10SPawel Jakub Dawidek pjdlog_info("Synchronization complete. " 1834fa356f6cSPawel Jakub Dawidek "%NB synchronized in %#.0lT (%NB/sec).", 1835fa356f6cSPawel Jakub Dawidek (intmax_t)synced, &tdiff, 1836fa356f6cSPawel Jakub Dawidek (intmax_t)bps); 18375bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCDONE); 183832115b10SPawel Jakub Dawidek } 183932115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 184032115b10SPawel Jakub Dawidek res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 184132115b10SPawel Jakub Dawidek res->hr_primary_localcnt = 184232115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt; 18439237aa3fSMikolaj Golub res->hr_primary_remotecnt = 18449237aa3fSMikolaj Golub res->hr_secondary_localcnt; 184532115b10SPawel Jakub Dawidek pjdlog_debug(1, 184632115b10SPawel Jakub Dawidek "Setting localcnt to %ju and remotecnt to %ju.", 184732115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt, 18489237aa3fSMikolaj Golub (uintmax_t)res->hr_primary_remotecnt); 184932115b10SPawel Jakub Dawidek (void)metadata_write(res); 185032115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 185132115b10SPawel Jakub Dawidek } 185232115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 185332115b10SPawel Jakub Dawidek continue; 185432115b10SPawel Jakub Dawidek } 185532115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: Taking free request."); 185632115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 185732115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Got free request.", hio); 185832115b10SPawel Jakub Dawidek /* 185932115b10SPawel Jakub Dawidek * Lock the range we are going to synchronize. We don't want 186032115b10SPawel Jakub Dawidek * race where someone writes between our read and write. 186132115b10SPawel Jakub Dawidek */ 186232115b10SPawel Jakub Dawidek for (;;) { 186332115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 186432115b10SPawel Jakub Dawidek if (rangelock_islocked(range_regular, offset, length)) { 186532115b10SPawel Jakub Dawidek pjdlog_debug(2, 186632115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd locked.", 186732115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 186832115b10SPawel Jakub Dawidek range_sync_wait = true; 186932115b10SPawel Jakub Dawidek cv_wait(&range_sync_cond, &range_lock); 187032115b10SPawel Jakub Dawidek range_sync_wait = false; 187132115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 187232115b10SPawel Jakub Dawidek continue; 187332115b10SPawel Jakub Dawidek } 187432115b10SPawel Jakub Dawidek if (rangelock_add(range_sync, offset, length) < 0) { 187532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 187632115b10SPawel Jakub Dawidek pjdlog_debug(2, 187732115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd is already locked, waiting.", 187832115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 187932115b10SPawel Jakub Dawidek sleep(1); 188032115b10SPawel Jakub Dawidek continue; 188132115b10SPawel Jakub Dawidek } 188232115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 188332115b10SPawel Jakub Dawidek break; 188432115b10SPawel Jakub Dawidek } 188532115b10SPawel Jakub Dawidek /* 188632115b10SPawel Jakub Dawidek * First read the data from synchronization source. 188732115b10SPawel Jakub Dawidek */ 188832115b10SPawel Jakub Dawidek SYNCREQ(hio); 188932115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 189032115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_READ; 189132115b10SPawel Jakub Dawidek ggio->gctl_offset = offset; 189232115b10SPawel Jakub Dawidek ggio->gctl_length = length; 189332115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 189432115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 189532115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 189632115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 189732115b10SPawel Jakub Dawidek hio); 189832115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 189932115b10SPawel Jakub Dawidek hio); 190032115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 190132115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 190232115b10SPawel Jakub Dawidek /* 190332115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 190432115b10SPawel Jakub Dawidek * so handle request locally. 190532115b10SPawel Jakub Dawidek */ 190632115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 190732115b10SPawel Jakub Dawidek ncomp = 0; 190832115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 19092ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 191032115b10SPawel Jakub Dawidek /* 191132115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 191232115b10SPawel Jakub Dawidek * so send request to the remote node. 191332115b10SPawel Jakub Dawidek */ 191432115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 191532115b10SPawel Jakub Dawidek ncomp = 1; 191632115b10SPawel Jakub Dawidek } 191732115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 191832115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 191932115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 192032115b10SPawel Jakub Dawidek 192132115b10SPawel Jakub Dawidek /* 192232115b10SPawel Jakub Dawidek * Let's wait for READ to finish. 192332115b10SPawel Jakub Dawidek */ 192432115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 192532115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 192632115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 192732115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 192832115b10SPawel Jakub Dawidek 192932115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 193032115b10SPawel Jakub Dawidek pjdlog_error("Unable to read synchronization data: %s.", 193132115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 193232115b10SPawel Jakub Dawidek goto free_queue; 193332115b10SPawel Jakub Dawidek } 193432115b10SPawel Jakub Dawidek 193532115b10SPawel Jakub Dawidek /* 193632115b10SPawel Jakub Dawidek * We read the data from synchronization source, now write it 193732115b10SPawel Jakub Dawidek * to synchronization target. 193832115b10SPawel Jakub Dawidek */ 193932115b10SPawel Jakub Dawidek SYNCREQ(hio); 194032115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_WRITE; 194132115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 194232115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 194332115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 194432115b10SPawel Jakub Dawidek hio); 194532115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 194632115b10SPawel Jakub Dawidek hio); 194732115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 194832115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 194932115b10SPawel Jakub Dawidek /* 195032115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 195132115b10SPawel Jakub Dawidek * so we update remote component. 195232115b10SPawel Jakub Dawidek */ 195332115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 195432115b10SPawel Jakub Dawidek ncomp = 1; 195532115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 19562ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 195732115b10SPawel Jakub Dawidek /* 195832115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 195932115b10SPawel Jakub Dawidek * so we update it. 196032115b10SPawel Jakub Dawidek */ 196132115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 196232115b10SPawel Jakub Dawidek ncomp = 0; 196332115b10SPawel Jakub Dawidek } 196432115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 196532115b10SPawel Jakub Dawidek 196632115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queues.", 196732115b10SPawel Jakub Dawidek hio); 196832115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 196932115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 197032115b10SPawel Jakub Dawidek 197132115b10SPawel Jakub Dawidek /* 197232115b10SPawel Jakub Dawidek * Let's wait for WRITE to finish. 197332115b10SPawel Jakub Dawidek */ 197432115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 197532115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 197632115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 197732115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 197832115b10SPawel Jakub Dawidek 197932115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 198032115b10SPawel Jakub Dawidek pjdlog_error("Unable to write synchronization data: %s.", 198132115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 198232115b10SPawel Jakub Dawidek goto free_queue; 198332115b10SPawel Jakub Dawidek } 1984e23d2d01SPawel Jakub Dawidek 1985e23d2d01SPawel Jakub Dawidek synced += length; 198632115b10SPawel Jakub Dawidek free_queue: 198732115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 198832115b10SPawel Jakub Dawidek rangelock_del(range_sync, offset, length); 198932115b10SPawel Jakub Dawidek if (range_regular_wait) 199032115b10SPawel Jakub Dawidek cv_signal(&range_regular_cond); 199132115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 199232115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 199332115b10SPawel Jakub Dawidek hio); 199432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 199532115b10SPawel Jakub Dawidek } 199632115b10SPawel Jakub Dawidek /* NOTREACHED */ 199732115b10SPawel Jakub Dawidek return (NULL); 199832115b10SPawel Jakub Dawidek } 199932115b10SPawel Jakub Dawidek 2000115f4e5cSPawel Jakub Dawidek void 2001115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv) 20020989854dSPawel Jakub Dawidek { 20030989854dSPawel Jakub Dawidek unsigned int ii, ncomps; 2004115f4e5cSPawel Jakub Dawidek int modified, vint; 2005115f4e5cSPawel Jakub Dawidek const char *vstr; 20060989854dSPawel Jakub Dawidek 20070989854dSPawel Jakub Dawidek pjdlog_info("Reloading configuration..."); 20080989854dSPawel Jakub Dawidek 20092ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 20102ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(gres == res); 2011115f4e5cSPawel Jakub Dawidek nv_assert(nv, "remoteaddr"); 20120b626a28SPawel Jakub Dawidek nv_assert(nv, "sourceaddr"); 2013115f4e5cSPawel Jakub Dawidek nv_assert(nv, "replication"); 20141fee97b0SPawel Jakub Dawidek nv_assert(nv, "checksum"); 20158cd3d45aSPawel Jakub Dawidek nv_assert(nv, "compression"); 2016115f4e5cSPawel Jakub Dawidek nv_assert(nv, "timeout"); 2017115f4e5cSPawel Jakub Dawidek nv_assert(nv, "exec"); 2018518dd4c0SPawel Jakub Dawidek nv_assert(nv, "metaflush"); 2019115f4e5cSPawel Jakub Dawidek 20200989854dSPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 20210989854dSPawel Jakub Dawidek 20221fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR 0x01 20230b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR 0x02 20240b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION 0x04 20250b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM 0x08 20260b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION 0x10 20270b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT 0x20 20280b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC 0x40 2029518dd4c0SPawel Jakub Dawidek #define MODIFIED_METAFLUSH 0x80 20300989854dSPawel Jakub Dawidek modified = 0; 2031115f4e5cSPawel Jakub Dawidek 2032115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 2033115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_remoteaddr, vstr) != 0) { 20340989854dSPawel Jakub Dawidek /* 20350989854dSPawel Jakub Dawidek * Don't copy res->hr_remoteaddr to gres just yet. 20360989854dSPawel Jakub Dawidek * We want remote_close() to log disconnect from the old 20370989854dSPawel Jakub Dawidek * addresses, not from the new ones. 20380989854dSPawel Jakub Dawidek */ 20390989854dSPawel Jakub Dawidek modified |= MODIFIED_REMOTEADDR; 20400989854dSPawel Jakub Dawidek } 20410b626a28SPawel Jakub Dawidek vstr = nv_get_string(nv, "sourceaddr"); 20420b626a28SPawel Jakub Dawidek if (strcmp(gres->hr_sourceaddr, vstr) != 0) { 20430b626a28SPawel Jakub Dawidek strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr)); 20440b626a28SPawel Jakub Dawidek modified |= MODIFIED_SOURCEADDR; 20450b626a28SPawel Jakub Dawidek } 2046115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "replication"); 2047115f4e5cSPawel Jakub Dawidek if (gres->hr_replication != vint) { 2048115f4e5cSPawel Jakub Dawidek gres->hr_replication = vint; 20490989854dSPawel Jakub Dawidek modified |= MODIFIED_REPLICATION; 20500989854dSPawel Jakub Dawidek } 20511fee97b0SPawel Jakub Dawidek vint = nv_get_int32(nv, "checksum"); 20521fee97b0SPawel Jakub Dawidek if (gres->hr_checksum != vint) { 20531fee97b0SPawel Jakub Dawidek gres->hr_checksum = vint; 20541fee97b0SPawel Jakub Dawidek modified |= MODIFIED_CHECKSUM; 20551fee97b0SPawel Jakub Dawidek } 20568cd3d45aSPawel Jakub Dawidek vint = nv_get_int32(nv, "compression"); 20578cd3d45aSPawel Jakub Dawidek if (gres->hr_compression != vint) { 20588cd3d45aSPawel Jakub Dawidek gres->hr_compression = vint; 20598cd3d45aSPawel Jakub Dawidek modified |= MODIFIED_COMPRESSION; 20608cd3d45aSPawel Jakub Dawidek } 2061115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "timeout"); 2062115f4e5cSPawel Jakub Dawidek if (gres->hr_timeout != vint) { 2063115f4e5cSPawel Jakub Dawidek gres->hr_timeout = vint; 20640989854dSPawel Jakub Dawidek modified |= MODIFIED_TIMEOUT; 20650989854dSPawel Jakub Dawidek } 2066115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "exec"); 2067115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_exec, vstr) != 0) { 2068115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec)); 20690becad39SPawel Jakub Dawidek modified |= MODIFIED_EXEC; 20700becad39SPawel Jakub Dawidek } 2071518dd4c0SPawel Jakub Dawidek vint = nv_get_int32(nv, "metaflush"); 2072518dd4c0SPawel Jakub Dawidek if (gres->hr_metaflush != vint) { 2073518dd4c0SPawel Jakub Dawidek gres->hr_metaflush = vint; 2074518dd4c0SPawel Jakub Dawidek modified |= MODIFIED_METAFLUSH; 2075518dd4c0SPawel Jakub Dawidek } 2076115f4e5cSPawel Jakub Dawidek 20770989854dSPawel Jakub Dawidek /* 20781fee97b0SPawel Jakub Dawidek * Change timeout for connected sockets. 20791fee97b0SPawel Jakub Dawidek * Don't bother if we need to reconnect. 20800989854dSPawel Jakub Dawidek */ 20811fee97b0SPawel Jakub Dawidek if ((modified & MODIFIED_TIMEOUT) != 0 && 20820b626a28SPawel Jakub Dawidek (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 rw_rlock(&hio_remote_lock[ii]); 20880989854dSPawel Jakub Dawidek if (!ISCONNECTED(gres, ii)) { 20890989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 20900989854dSPawel Jakub Dawidek continue; 20910989854dSPawel Jakub Dawidek } 20920989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 20930989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remotein, 20940989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 20950989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 20960989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 20970989854dSPawel Jakub Dawidek } 20980989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remoteout, 20990989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 21000989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 21010989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 21020989854dSPawel Jakub Dawidek } 21030989854dSPawel Jakub Dawidek } 21041fee97b0SPawel Jakub Dawidek } 21050b626a28SPawel Jakub Dawidek if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR | 21060b626a28SPawel Jakub Dawidek MODIFIED_REPLICATION)) != 0) { 21070989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 21080989854dSPawel Jakub Dawidek if (!ISREMOTE(ii)) 21090989854dSPawel Jakub Dawidek continue; 21100989854dSPawel Jakub Dawidek remote_close(gres, ii); 21110989854dSPawel Jakub Dawidek } 21120989854dSPawel Jakub Dawidek if (modified & MODIFIED_REMOTEADDR) { 2113115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 2114115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_remoteaddr, vstr, 21150989854dSPawel Jakub Dawidek sizeof(gres->hr_remoteaddr)); 21160989854dSPawel Jakub Dawidek } 21170989854dSPawel Jakub Dawidek } 21180989854dSPawel Jakub Dawidek #undef MODIFIED_REMOTEADDR 21190b626a28SPawel Jakub Dawidek #undef MODIFIED_SOURCEADDR 21200989854dSPawel Jakub Dawidek #undef MODIFIED_REPLICATION 21211fee97b0SPawel Jakub Dawidek #undef MODIFIED_CHECKSUM 21228cd3d45aSPawel Jakub Dawidek #undef MODIFIED_COMPRESSION 21230989854dSPawel Jakub Dawidek #undef MODIFIED_TIMEOUT 21240becad39SPawel Jakub Dawidek #undef MODIFIED_EXEC 2125518dd4c0SPawel Jakub Dawidek #undef MODIFIED_METAFLUSH 21260989854dSPawel Jakub Dawidek 21270989854dSPawel Jakub Dawidek pjdlog_info("Configuration reloaded successfully."); 21280989854dSPawel Jakub Dawidek } 21290989854dSPawel Jakub Dawidek 2130f7fe83f9SPawel Jakub Dawidek static void 2131ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp) 2132ff6bb1f8SPawel Jakub Dawidek { 2133ff6bb1f8SPawel Jakub Dawidek struct proto_conn *in, *out; 2134ff6bb1f8SPawel Jakub Dawidek 2135ff6bb1f8SPawel Jakub Dawidek if (!ISREMOTE(ncomp)) 2136ff6bb1f8SPawel Jakub Dawidek return; 2137ff6bb1f8SPawel Jakub Dawidek 2138ff6bb1f8SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 2139ff6bb1f8SPawel Jakub Dawidek 2140ff6bb1f8SPawel Jakub Dawidek if (!real_remote(res)) { 2141ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2142ff6bb1f8SPawel Jakub Dawidek return; 2143ff6bb1f8SPawel Jakub Dawidek } 2144ff6bb1f8SPawel Jakub Dawidek 2145ff6bb1f8SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 21462ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 21472ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 2148ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2149ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Connection to %s is ok.", 2150ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2151ff6bb1f8SPawel Jakub Dawidek return; 2152ff6bb1f8SPawel Jakub Dawidek } 2153ff6bb1f8SPawel Jakub Dawidek 21542ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21552ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 2156ff6bb1f8SPawel Jakub Dawidek /* 2157ff6bb1f8SPawel Jakub Dawidek * Upgrade the lock. It doesn't have to be atomic as no other thread 2158ff6bb1f8SPawel Jakub Dawidek * can change connection status from disconnected to connected. 2159ff6bb1f8SPawel Jakub Dawidek */ 2160ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2161ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnecting to %s.", 2162ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2163ff6bb1f8SPawel Jakub Dawidek in = out = NULL; 2164ac0401e3SPawel Jakub Dawidek if (init_remote(res, &in, &out) == 0) { 2165ff6bb1f8SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 21662ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21672ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 21682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in != NULL && out != NULL); 2169ff6bb1f8SPawel Jakub Dawidek res->hr_remotein = in; 2170ff6bb1f8SPawel Jakub Dawidek res->hr_remoteout = out; 2171ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2172ff6bb1f8SPawel Jakub Dawidek pjdlog_info("Successfully reconnected to %s.", 2173ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2174ff6bb1f8SPawel Jakub Dawidek sync_start(); 2175ff6bb1f8SPawel Jakub Dawidek } else { 2176ff6bb1f8SPawel Jakub Dawidek /* Both connections should be NULL. */ 21772ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 21782ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 21792ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in == NULL && out == NULL); 2180ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", 2181ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2182ff6bb1f8SPawel Jakub Dawidek } 2183ff6bb1f8SPawel Jakub Dawidek } 2184ff6bb1f8SPawel Jakub Dawidek 218532115b10SPawel Jakub Dawidek /* 218632115b10SPawel Jakub Dawidek * Thread guards remote connections and reconnects when needed, handles 218732115b10SPawel Jakub Dawidek * signals, etc. 218832115b10SPawel Jakub Dawidek */ 218932115b10SPawel Jakub Dawidek static void * 219032115b10SPawel Jakub Dawidek guard_thread(void *arg) 219132115b10SPawel Jakub Dawidek { 219232115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 219332115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 21946d0c801eSPawel Jakub Dawidek struct timespec timeout; 2195ff6bb1f8SPawel Jakub Dawidek time_t lastcheck, now; 21966d0c801eSPawel Jakub Dawidek sigset_t mask; 21976d0c801eSPawel Jakub Dawidek int signo; 219832115b10SPawel Jakub Dawidek 219932115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 2200ff6bb1f8SPawel Jakub Dawidek lastcheck = time(NULL); 220132115b10SPawel Jakub Dawidek 22026d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigemptyset(&mask) == 0); 22036d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 22046d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 22056d0c801eSPawel Jakub Dawidek 22068d7dcf14SMikolaj Golub timeout.tv_sec = HAST_KEEPALIVE; 22076d0c801eSPawel Jakub Dawidek timeout.tv_nsec = 0; 22086d0c801eSPawel Jakub Dawidek signo = -1; 22096d0c801eSPawel Jakub Dawidek 221032115b10SPawel Jakub Dawidek for (;;) { 22116d0c801eSPawel Jakub Dawidek switch (signo) { 22126d0c801eSPawel Jakub Dawidek case SIGINT: 22136d0c801eSPawel Jakub Dawidek case SIGTERM: 22146d0c801eSPawel Jakub Dawidek sigexit_received = true; 221532115b10SPawel Jakub Dawidek primary_exitx(EX_OK, 221632115b10SPawel Jakub Dawidek "Termination signal received, exiting."); 22176d0c801eSPawel Jakub Dawidek break; 22186d0c801eSPawel Jakub Dawidek default: 2219ff6bb1f8SPawel Jakub Dawidek break; 2220f7fe83f9SPawel Jakub Dawidek } 22216d0c801eSPawel Jakub Dawidek 2222ac0401e3SPawel Jakub Dawidek /* 2223ac0401e3SPawel Jakub Dawidek * Don't check connections until we fully started, 2224ac0401e3SPawel Jakub Dawidek * as we may still be looping, waiting for remote node 2225ac0401e3SPawel Jakub Dawidek * to switch from primary to secondary. 2226ac0401e3SPawel Jakub Dawidek */ 2227ac0401e3SPawel Jakub Dawidek if (fullystarted) { 22286d0c801eSPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Checking connections."); 2229ff6bb1f8SPawel Jakub Dawidek now = time(NULL); 22308d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) { 22316d0c801eSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 2232ff6bb1f8SPawel Jakub Dawidek guard_one(res, ii); 2233ff6bb1f8SPawel Jakub Dawidek lastcheck = now; 223432115b10SPawel Jakub Dawidek } 2235ac0401e3SPawel Jakub Dawidek } 22366d0c801eSPawel Jakub Dawidek signo = sigtimedwait(&mask, NULL, &timeout); 223732115b10SPawel Jakub Dawidek } 223832115b10SPawel Jakub Dawidek /* NOTREACHED */ 223932115b10SPawel Jakub Dawidek return (NULL); 224032115b10SPawel Jakub Dawidek } 2241