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 /* 8932115b10SPawel Jakub Dawidek * Structure used to comunicate 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 /* 1546d0c801eSPawel Jakub Dawidek * Number of seconds to sleep between reconnect retries or keepalive packets. 155f7fe83f9SPawel Jakub Dawidek */ 1566d0c801eSPawel Jakub Dawidek #define RETRY_SLEEP 10 15732115b10SPawel Jakub Dawidek 15832115b10SPawel Jakub Dawidek #define ISCONNECTED(res, no) \ 15932115b10SPawel Jakub Dawidek ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL) 16032115b10SPawel Jakub Dawidek 16132115b10SPawel Jakub Dawidek #define QUEUE_INSERT1(hio, name, ncomp) do { \ 16232115b10SPawel Jakub Dawidek bool _wakeup; \ 16332115b10SPawel Jakub Dawidek \ 16432115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 16532115b10SPawel Jakub Dawidek _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \ 16632115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \ 16732115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \ 16832115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock[ncomp]); \ 16932115b10SPawel Jakub Dawidek if (_wakeup) \ 17032115b10SPawel Jakub Dawidek cv_signal(&hio_##name##_list_cond[(ncomp)]); \ 17132115b10SPawel Jakub Dawidek } while (0) 17232115b10SPawel Jakub Dawidek #define QUEUE_INSERT2(hio, name) do { \ 17332115b10SPawel Jakub Dawidek bool _wakeup; \ 17432115b10SPawel Jakub Dawidek \ 17532115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \ 17632115b10SPawel Jakub Dawidek _wakeup = TAILQ_EMPTY(&hio_##name##_list); \ 17732115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\ 17832115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \ 17932115b10SPawel Jakub Dawidek if (_wakeup) \ 18032115b10SPawel Jakub Dawidek cv_signal(&hio_##name##_list_cond); \ 18132115b10SPawel Jakub Dawidek } while (0) 182448efa94SPawel Jakub Dawidek #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \ 183448efa94SPawel Jakub Dawidek bool _last; \ 184448efa94SPawel Jakub Dawidek \ 18532115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \ 186448efa94SPawel Jakub Dawidek _last = false; \ 187448efa94SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \ 188448efa94SPawel Jakub Dawidek cv_timedwait(&hio_##name##_list_cond[(ncomp)], \ 189448efa94SPawel Jakub Dawidek &hio_##name##_list_lock[(ncomp)], (timeout)); \ 190448efa94SPawel Jakub Dawidek if ((timeout) != 0) \ 191448efa94SPawel Jakub Dawidek _last = true; \ 19232115b10SPawel Jakub Dawidek } \ 193448efa94SPawel Jakub Dawidek if (hio != NULL) { \ 19432115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \ 19532115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \ 196448efa94SPawel Jakub Dawidek } \ 19732115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \ 19832115b10SPawel Jakub Dawidek } while (0) 19932115b10SPawel Jakub Dawidek #define QUEUE_TAKE2(hio, name) do { \ 20032115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \ 20132115b10SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 20232115b10SPawel Jakub Dawidek cv_wait(&hio_##name##_list_cond, \ 20332115b10SPawel Jakub Dawidek &hio_##name##_list_lock); \ 20432115b10SPawel Jakub Dawidek } \ 20532115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \ 20632115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \ 20732115b10SPawel Jakub Dawidek } while (0) 20832115b10SPawel Jakub Dawidek 209328e0f4bSPawel Jakub Dawidek #define SYNCREQ(hio) do { \ 210328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_unit = -1; \ 211328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_seq = 1; \ 212328e0f4bSPawel Jakub Dawidek } while (0) 21332115b10SPawel Jakub Dawidek #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1) 21432115b10SPawel Jakub Dawidek #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0) 21532115b10SPawel Jakub Dawidek #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2) 21632115b10SPawel Jakub Dawidek 21732115b10SPawel Jakub Dawidek static struct hast_resource *gres; 21832115b10SPawel Jakub Dawidek 21932115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock; 22032115b10SPawel Jakub Dawidek static struct rangelocks *range_regular; 22132115b10SPawel Jakub Dawidek static bool range_regular_wait; 22232115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond; 22332115b10SPawel Jakub Dawidek static struct rangelocks *range_sync; 22432115b10SPawel Jakub Dawidek static bool range_sync_wait; 22532115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond; 22632115b10SPawel Jakub Dawidek 22732115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg); 22832115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg); 22932115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg); 23032115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg); 23132115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg); 23232115b10SPawel Jakub Dawidek static void *sync_thread(void *arg); 23332115b10SPawel Jakub Dawidek static void *guard_thread(void *arg); 23432115b10SPawel Jakub Dawidek 2356d0c801eSPawel Jakub Dawidek static void 23632115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res) 23732115b10SPawel Jakub Dawidek { 23832115b10SPawel Jakub Dawidek int rerrno; 23932115b10SPawel Jakub Dawidek 24032115b10SPawel Jakub Dawidek /* Remember errno. */ 24132115b10SPawel Jakub Dawidek rerrno = errno; 24232115b10SPawel Jakub Dawidek 24332115b10SPawel Jakub Dawidek /* Destroy ggate provider if we created one. */ 24432115b10SPawel Jakub Dawidek if (res->hr_ggateunit >= 0) { 24532115b10SPawel Jakub Dawidek struct g_gate_ctl_destroy ggiod; 24632115b10SPawel Jakub Dawidek 2474e47b646SPawel Jakub Dawidek bzero(&ggiod, sizeof(ggiod)); 24832115b10SPawel Jakub Dawidek ggiod.gctl_version = G_GATE_VERSION; 24932115b10SPawel Jakub Dawidek ggiod.gctl_unit = res->hr_ggateunit; 25032115b10SPawel Jakub Dawidek ggiod.gctl_force = 1; 25132115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) { 252783ee753SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 253783ee753SPawel Jakub Dawidek "Unable to destroy hast/%s device", 25432115b10SPawel Jakub Dawidek res->hr_provname); 25532115b10SPawel Jakub Dawidek } 25632115b10SPawel Jakub Dawidek res->hr_ggateunit = -1; 25732115b10SPawel Jakub Dawidek } 25832115b10SPawel Jakub Dawidek 25932115b10SPawel Jakub Dawidek /* Restore errno. */ 26032115b10SPawel Jakub Dawidek errno = rerrno; 26132115b10SPawel Jakub Dawidek } 26232115b10SPawel Jakub Dawidek 263e43e02f1SPawel Jakub Dawidek static __dead2 void 26432115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...) 26532115b10SPawel Jakub Dawidek { 26632115b10SPawel Jakub Dawidek va_list ap; 26732115b10SPawel Jakub Dawidek 2682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(exitcode != EX_OK); 26932115b10SPawel Jakub Dawidek va_start(ap, fmt); 27032115b10SPawel Jakub Dawidek pjdlogv_errno(LOG_ERR, fmt, ap); 27132115b10SPawel Jakub Dawidek va_end(ap); 27232115b10SPawel Jakub Dawidek cleanup(gres); 27332115b10SPawel Jakub Dawidek exit(exitcode); 27432115b10SPawel Jakub Dawidek } 27532115b10SPawel Jakub Dawidek 276e43e02f1SPawel Jakub Dawidek static __dead2 void 27732115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...) 27832115b10SPawel Jakub Dawidek { 27932115b10SPawel Jakub Dawidek va_list ap; 28032115b10SPawel Jakub Dawidek 28132115b10SPawel Jakub Dawidek va_start(ap, fmt); 28232115b10SPawel Jakub Dawidek pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap); 28332115b10SPawel Jakub Dawidek va_end(ap); 28432115b10SPawel Jakub Dawidek cleanup(gres); 28532115b10SPawel Jakub Dawidek exit(exitcode); 28632115b10SPawel Jakub Dawidek } 28732115b10SPawel Jakub Dawidek 28832115b10SPawel Jakub Dawidek static int 28932115b10SPawel Jakub Dawidek hast_activemap_flush(struct hast_resource *res) 29032115b10SPawel Jakub Dawidek { 29132115b10SPawel Jakub Dawidek const unsigned char *buf; 29232115b10SPawel Jakub Dawidek size_t size; 29332115b10SPawel Jakub Dawidek 29432115b10SPawel Jakub Dawidek buf = activemap_bitmap(res->hr_amp, &size); 2952ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(buf != NULL); 2962ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0); 29732115b10SPawel Jakub Dawidek if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) != 29832115b10SPawel Jakub Dawidek (ssize_t)size) { 29932115b10SPawel Jakub Dawidek KEEP_ERRNO(pjdlog_errno(LOG_ERR, 30032115b10SPawel Jakub Dawidek "Unable to flush activemap to disk")); 30132115b10SPawel Jakub Dawidek return (-1); 30232115b10SPawel Jakub Dawidek } 30332115b10SPawel Jakub Dawidek return (0); 30432115b10SPawel Jakub Dawidek } 30532115b10SPawel Jakub Dawidek 306f377917cSPawel Jakub Dawidek static bool 307f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res) 308f377917cSPawel Jakub Dawidek { 309f377917cSPawel Jakub Dawidek 310f377917cSPawel Jakub Dawidek return (strcmp(res->hr_remoteaddr, "none") != 0); 311f377917cSPawel Jakub Dawidek } 312f377917cSPawel Jakub Dawidek 31332115b10SPawel Jakub Dawidek static void 31432115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused) 31532115b10SPawel Jakub Dawidek { 31632115b10SPawel Jakub Dawidek struct hio *hio; 31732115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 31832115b10SPawel Jakub Dawidek 31932115b10SPawel Jakub Dawidek /* 32032115b10SPawel Jakub Dawidek * In the future it might be per-resource value. 32132115b10SPawel Jakub Dawidek */ 32232115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 32332115b10SPawel Jakub Dawidek 32432115b10SPawel Jakub Dawidek /* 32532115b10SPawel Jakub Dawidek * Allocate memory needed by lists. 32632115b10SPawel Jakub Dawidek */ 32732115b10SPawel Jakub Dawidek hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps); 32832115b10SPawel Jakub Dawidek if (hio_send_list == NULL) { 32932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 33032115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send lists.", 33132115b10SPawel Jakub Dawidek sizeof(hio_send_list[0]) * ncomps); 33232115b10SPawel Jakub Dawidek } 33332115b10SPawel Jakub Dawidek hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps); 33432115b10SPawel Jakub Dawidek if (hio_send_list_lock == NULL) { 33532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 33632115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list locks.", 33732115b10SPawel Jakub Dawidek sizeof(hio_send_list_lock[0]) * ncomps); 33832115b10SPawel Jakub Dawidek } 33932115b10SPawel Jakub Dawidek hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps); 34032115b10SPawel Jakub Dawidek if (hio_send_list_cond == NULL) { 34132115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 34232115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list condition variables.", 34332115b10SPawel Jakub Dawidek sizeof(hio_send_list_cond[0]) * ncomps); 34432115b10SPawel Jakub Dawidek } 34532115b10SPawel Jakub Dawidek hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps); 34632115b10SPawel Jakub Dawidek if (hio_recv_list == NULL) { 34732115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 34832115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv lists.", 34932115b10SPawel Jakub Dawidek sizeof(hio_recv_list[0]) * ncomps); 35032115b10SPawel Jakub Dawidek } 35132115b10SPawel Jakub Dawidek hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps); 35232115b10SPawel Jakub Dawidek if (hio_recv_list_lock == NULL) { 35332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 35432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list locks.", 35532115b10SPawel Jakub Dawidek sizeof(hio_recv_list_lock[0]) * ncomps); 35632115b10SPawel Jakub Dawidek } 35732115b10SPawel Jakub Dawidek hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps); 35832115b10SPawel Jakub Dawidek if (hio_recv_list_cond == NULL) { 35932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 36032115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list condition variables.", 36132115b10SPawel Jakub Dawidek sizeof(hio_recv_list_cond[0]) * ncomps); 36232115b10SPawel Jakub Dawidek } 36332115b10SPawel Jakub Dawidek hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps); 36432115b10SPawel Jakub Dawidek if (hio_remote_lock == NULL) { 36532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 36632115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for remote connections locks.", 36732115b10SPawel Jakub Dawidek sizeof(hio_remote_lock[0]) * ncomps); 36832115b10SPawel Jakub Dawidek } 36932115b10SPawel Jakub Dawidek 37032115b10SPawel Jakub Dawidek /* 37132115b10SPawel Jakub Dawidek * Initialize lists, their locks and theirs condition variables. 37232115b10SPawel Jakub Dawidek */ 37332115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_free_list); 37432115b10SPawel Jakub Dawidek mtx_init(&hio_free_list_lock); 37532115b10SPawel Jakub Dawidek cv_init(&hio_free_list_cond); 37632115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_NCOMPONENTS; ii++) { 37732115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_send_list[ii]); 37832115b10SPawel Jakub Dawidek mtx_init(&hio_send_list_lock[ii]); 37932115b10SPawel Jakub Dawidek cv_init(&hio_send_list_cond[ii]); 38032115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_recv_list[ii]); 38132115b10SPawel Jakub Dawidek mtx_init(&hio_recv_list_lock[ii]); 38232115b10SPawel Jakub Dawidek cv_init(&hio_recv_list_cond[ii]); 38332115b10SPawel Jakub Dawidek rw_init(&hio_remote_lock[ii]); 38432115b10SPawel Jakub Dawidek } 38532115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_done_list); 38632115b10SPawel Jakub Dawidek mtx_init(&hio_done_list_lock); 38732115b10SPawel Jakub Dawidek cv_init(&hio_done_list_cond); 38832115b10SPawel Jakub Dawidek mtx_init(&metadata_lock); 38932115b10SPawel Jakub Dawidek 39032115b10SPawel Jakub Dawidek /* 39132115b10SPawel Jakub Dawidek * Allocate requests pool and initialize requests. 39232115b10SPawel Jakub Dawidek */ 39332115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_HIO_MAX; ii++) { 39432115b10SPawel Jakub Dawidek hio = malloc(sizeof(*hio)); 39532115b10SPawel Jakub Dawidek if (hio == NULL) { 39632115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 39732115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for hio request.", 39832115b10SPawel Jakub Dawidek sizeof(*hio)); 39932115b10SPawel Jakub Dawidek } 40032115b10SPawel Jakub Dawidek hio->hio_countdown = 0; 40132115b10SPawel Jakub Dawidek hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps); 40232115b10SPawel Jakub Dawidek if (hio->hio_errors == NULL) { 40332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 40432115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio errors.", 40532115b10SPawel Jakub Dawidek sizeof(hio->hio_errors[0]) * ncomps); 40632115b10SPawel Jakub Dawidek } 40732115b10SPawel Jakub Dawidek hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps); 40832115b10SPawel Jakub Dawidek if (hio->hio_next == NULL) { 40932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 41032115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio_next field.", 41132115b10SPawel Jakub Dawidek sizeof(hio->hio_next[0]) * ncomps); 41232115b10SPawel Jakub Dawidek } 41332115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_version = G_GATE_VERSION; 41432115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_data = malloc(MAXPHYS); 41532115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_data == NULL) { 41632115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 41732115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for gctl_data.", 41832115b10SPawel Jakub Dawidek MAXPHYS); 41932115b10SPawel Jakub Dawidek } 42032115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_length = MAXPHYS; 42132115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_error = 0; 42232115b10SPawel Jakub Dawidek TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next); 42332115b10SPawel Jakub Dawidek } 42432115b10SPawel Jakub Dawidek } 42532115b10SPawel Jakub Dawidek 426ce837469SPawel Jakub Dawidek static bool 427ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res) 428ce837469SPawel Jakub Dawidek { 429ce837469SPawel Jakub Dawidek 430ce837469SPawel Jakub Dawidek mtx_lock(&metadata_lock); 431ce837469SPawel Jakub Dawidek if (res->hr_resuid != 0) { 432ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 433ce837469SPawel Jakub Dawidek return (false); 434ce837469SPawel Jakub Dawidek } else { 435ce837469SPawel Jakub Dawidek /* Initialize unique resource identifier. */ 436ce837469SPawel Jakub Dawidek arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid)); 437ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 438ce837469SPawel Jakub Dawidek if (metadata_write(res) < 0) 439ce837469SPawel Jakub Dawidek exit(EX_NOINPUT); 440ce837469SPawel Jakub Dawidek return (true); 441ce837469SPawel Jakub Dawidek } 442ce837469SPawel Jakub Dawidek } 443ce837469SPawel Jakub Dawidek 44432115b10SPawel Jakub Dawidek static void 44532115b10SPawel Jakub Dawidek init_local(struct hast_resource *res) 44632115b10SPawel Jakub Dawidek { 44732115b10SPawel Jakub Dawidek unsigned char *buf; 44832115b10SPawel Jakub Dawidek size_t mapsize; 44932115b10SPawel Jakub Dawidek 45032115b10SPawel Jakub Dawidek if (metadata_read(res, true) < 0) 45132115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 45232115b10SPawel Jakub Dawidek mtx_init(&res->hr_amp_lock); 45332115b10SPawel Jakub Dawidek if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize, 45432115b10SPawel Jakub Dawidek res->hr_local_sectorsize, res->hr_keepdirty) < 0) { 45532115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create activemap"); 45632115b10SPawel Jakub Dawidek } 45732115b10SPawel Jakub Dawidek mtx_init(&range_lock); 45832115b10SPawel Jakub Dawidek cv_init(&range_regular_cond); 45932115b10SPawel Jakub Dawidek if (rangelock_init(&range_regular) < 0) 46032115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create regular range lock"); 46132115b10SPawel Jakub Dawidek cv_init(&range_sync_cond); 46232115b10SPawel Jakub Dawidek if (rangelock_init(&range_sync) < 0) 46332115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create sync range lock"); 46432115b10SPawel Jakub Dawidek mapsize = activemap_ondisk_size(res->hr_amp); 46532115b10SPawel Jakub Dawidek buf = calloc(1, mapsize); 46632115b10SPawel Jakub Dawidek if (buf == NULL) { 46732115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL, 46832115b10SPawel Jakub Dawidek "Unable to allocate buffer for activemap."); 46932115b10SPawel Jakub Dawidek } 47032115b10SPawel Jakub Dawidek if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 47132115b10SPawel Jakub Dawidek (ssize_t)mapsize) { 47232115b10SPawel Jakub Dawidek primary_exit(EX_NOINPUT, "Unable to read activemap"); 47332115b10SPawel Jakub Dawidek } 47432115b10SPawel Jakub Dawidek activemap_copyin(res->hr_amp, buf, mapsize); 475b0dfbe5bSPawel Jakub Dawidek free(buf); 47632115b10SPawel Jakub Dawidek if (res->hr_resuid != 0) 47732115b10SPawel Jakub Dawidek return; 47832115b10SPawel Jakub Dawidek /* 479ce837469SPawel Jakub Dawidek * We're using provider for the first time. Initialize local and remote 480ce837469SPawel Jakub Dawidek * counters. We don't initialize resuid here, as we want to do it just 481ce837469SPawel Jakub Dawidek * in time. The reason for this is that we want to inform secondary 482ce837469SPawel Jakub Dawidek * that there were no writes yet, so there is no need to synchronize 483ce837469SPawel Jakub Dawidek * anything. 48432115b10SPawel Jakub Dawidek */ 48532115b10SPawel Jakub Dawidek res->hr_primary_localcnt = 1; 48632115b10SPawel Jakub Dawidek res->hr_primary_remotecnt = 0; 48732115b10SPawel Jakub Dawidek if (metadata_write(res) < 0) 48832115b10SPawel Jakub Dawidek exit(EX_NOINPUT); 48932115b10SPawel Jakub Dawidek } 49032115b10SPawel Jakub Dawidek 49132ecf620SPawel Jakub Dawidek static int 49232ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp) 49332ecf620SPawel Jakub Dawidek { 49432ecf620SPawel Jakub Dawidek struct proto_conn *conn; 49532ecf620SPawel Jakub Dawidek int16_t val; 49632ecf620SPawel Jakub Dawidek 49732ecf620SPawel Jakub Dawidek val = 1; 49832ecf620SPawel Jakub Dawidek if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) { 49932ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 50032ecf620SPawel Jakub Dawidek "Unable to send connection request to parent"); 50132ecf620SPawel Jakub Dawidek } 50232ecf620SPawel Jakub Dawidek if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) { 50332ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 50432ecf620SPawel Jakub Dawidek "Unable to receive reply to connection request from parent"); 50532ecf620SPawel Jakub Dawidek } 50632ecf620SPawel Jakub Dawidek if (val != 0) { 50732ecf620SPawel Jakub Dawidek errno = val; 50832ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 50932ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 51032ecf620SPawel Jakub Dawidek return (-1); 51132ecf620SPawel Jakub Dawidek } 51232ecf620SPawel Jakub Dawidek if (proto_connection_recv(res->hr_conn, true, &conn) < 0) { 51332ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, 51432ecf620SPawel Jakub Dawidek "Unable to receive connection from parent"); 51532ecf620SPawel Jakub Dawidek } 51632ecf620SPawel Jakub Dawidek if (proto_connect_wait(conn, HAST_TIMEOUT) < 0) { 51732ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 51832ecf620SPawel Jakub Dawidek res->hr_remoteaddr); 51932ecf620SPawel Jakub Dawidek proto_close(conn); 52032ecf620SPawel Jakub Dawidek return (-1); 52132ecf620SPawel Jakub Dawidek } 52232ecf620SPawel Jakub Dawidek /* Error in setting timeout is not critical, but why should it fail? */ 52332ecf620SPawel Jakub Dawidek if (proto_timeout(conn, res->hr_timeout) < 0) 52432ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 52532ecf620SPawel Jakub Dawidek 52632ecf620SPawel Jakub Dawidek *connp = conn; 52732ecf620SPawel Jakub Dawidek 52832ecf620SPawel Jakub Dawidek return (0); 52932ecf620SPawel Jakub Dawidek } 53032ecf620SPawel Jakub Dawidek 5310d9014f3SPawel Jakub Dawidek static bool 5320d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp, 5330d9014f3SPawel Jakub Dawidek struct proto_conn **outp) 53432115b10SPawel Jakub Dawidek { 5350d9014f3SPawel Jakub Dawidek struct proto_conn *in, *out; 53632115b10SPawel Jakub Dawidek struct nv *nvout, *nvin; 53732115b10SPawel Jakub Dawidek const unsigned char *token; 53832115b10SPawel Jakub Dawidek unsigned char *map; 53932115b10SPawel Jakub Dawidek const char *errmsg; 54032115b10SPawel Jakub Dawidek int32_t extentsize; 54132115b10SPawel Jakub Dawidek int64_t datasize; 54232115b10SPawel Jakub Dawidek uint32_t mapsize; 54332115b10SPawel Jakub Dawidek size_t size; 54432115b10SPawel Jakub Dawidek 5452ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL)); 5462ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(real_remote(res)); 5470d9014f3SPawel Jakub Dawidek 5480d9014f3SPawel Jakub Dawidek in = out = NULL; 5492be8fd75SPawel Jakub Dawidek errmsg = NULL; 5500d9014f3SPawel Jakub Dawidek 55132ecf620SPawel Jakub Dawidek if (primary_connect(res, &out) == -1) 55232ecf620SPawel Jakub Dawidek return (false); 55332ecf620SPawel Jakub Dawidek 55432115b10SPawel Jakub Dawidek /* 55532115b10SPawel Jakub Dawidek * First handshake step. 55632115b10SPawel Jakub Dawidek * Setup outgoing connection with remote node. 55732115b10SPawel Jakub Dawidek */ 55832115b10SPawel Jakub Dawidek nvout = nv_alloc(); 55932115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 56032115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 56132115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 56232115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 56332115b10SPawel Jakub Dawidek res->hr_remoteaddr); 56432115b10SPawel Jakub Dawidek nv_free(nvout); 56532115b10SPawel Jakub Dawidek goto close; 56632115b10SPawel Jakub Dawidek } 5670d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, out, nvout, NULL, 0) < 0) { 56832115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 56932115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 57032115b10SPawel Jakub Dawidek res->hr_remoteaddr); 57132115b10SPawel Jakub Dawidek nv_free(nvout); 57232115b10SPawel Jakub Dawidek goto close; 57332115b10SPawel Jakub Dawidek } 57432115b10SPawel Jakub Dawidek nv_free(nvout); 5750d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 57632115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 57732115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 57832115b10SPawel Jakub Dawidek res->hr_remoteaddr); 57932115b10SPawel Jakub Dawidek goto close; 58032115b10SPawel Jakub Dawidek } 58132115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 58232115b10SPawel Jakub Dawidek if (errmsg != NULL) { 58332115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 58432115b10SPawel Jakub Dawidek nv_free(nvin); 58532115b10SPawel Jakub Dawidek goto close; 58632115b10SPawel Jakub Dawidek } 58732115b10SPawel Jakub Dawidek token = nv_get_uint8_array(nvin, &size, "token"); 58832115b10SPawel Jakub Dawidek if (token == NULL) { 58932115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s has no 'token' field.", 59032115b10SPawel Jakub Dawidek res->hr_remoteaddr); 59132115b10SPawel Jakub Dawidek nv_free(nvin); 59232115b10SPawel Jakub Dawidek goto close; 59332115b10SPawel Jakub Dawidek } 59432115b10SPawel Jakub Dawidek if (size != sizeof(res->hr_token)) { 59532115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).", 59632115b10SPawel Jakub Dawidek res->hr_remoteaddr, size, sizeof(res->hr_token)); 59732115b10SPawel Jakub Dawidek nv_free(nvin); 59832115b10SPawel Jakub Dawidek goto close; 59932115b10SPawel Jakub Dawidek } 60032115b10SPawel Jakub Dawidek bcopy(token, res->hr_token, sizeof(res->hr_token)); 60132115b10SPawel Jakub Dawidek nv_free(nvin); 60232115b10SPawel Jakub Dawidek 60332115b10SPawel Jakub Dawidek /* 60432115b10SPawel Jakub Dawidek * Second handshake step. 60532115b10SPawel Jakub Dawidek * Setup incoming connection with remote node. 60632115b10SPawel Jakub Dawidek */ 60732ecf620SPawel Jakub Dawidek if (primary_connect(res, &in) == -1) 60832115b10SPawel Jakub Dawidek goto close; 60932ecf620SPawel Jakub Dawidek 61032115b10SPawel Jakub Dawidek nvout = nv_alloc(); 61132115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource"); 61232115b10SPawel Jakub Dawidek nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token), 61332115b10SPawel Jakub Dawidek "token"); 614ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) { 615ce837469SPawel Jakub Dawidek /* 616ce837469SPawel Jakub Dawidek * The resuid field was not yet initialized. 617ce837469SPawel Jakub Dawidek * Because we do synchronization inside init_resuid(), it is 618ce837469SPawel Jakub Dawidek * possible that someone already initialized it, the function 619ce837469SPawel Jakub Dawidek * will return false then, but if we successfully initialized 620ce837469SPawel Jakub Dawidek * it, we will get true. True means that there were no writes 621ce837469SPawel Jakub Dawidek * to this resource yet and we want to inform secondary that 622ce837469SPawel Jakub Dawidek * synchronization is not needed by sending "virgin" argument. 623ce837469SPawel Jakub Dawidek */ 624ce837469SPawel Jakub Dawidek if (init_resuid(res)) 625ce837469SPawel Jakub Dawidek nv_add_int8(nvout, 1, "virgin"); 626ce837469SPawel Jakub Dawidek } 62732115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_resuid, "resuid"); 62832115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt"); 62932115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt"); 63032115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) { 63132115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout), 63232115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s", 63332115b10SPawel Jakub Dawidek res->hr_remoteaddr); 63432115b10SPawel Jakub Dawidek nv_free(nvout); 63532115b10SPawel Jakub Dawidek goto close; 63632115b10SPawel Jakub Dawidek } 6370d9014f3SPawel Jakub Dawidek if (hast_proto_send(res, in, nvout, NULL, 0) < 0) { 63832115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 63932115b10SPawel Jakub Dawidek "Unable to send handshake header to %s", 64032115b10SPawel Jakub Dawidek res->hr_remoteaddr); 64132115b10SPawel Jakub Dawidek nv_free(nvout); 64232115b10SPawel Jakub Dawidek goto close; 64332115b10SPawel Jakub Dawidek } 64432115b10SPawel Jakub Dawidek nv_free(nvout); 6450d9014f3SPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) < 0) { 64632115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 64732115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s", 64832115b10SPawel Jakub Dawidek res->hr_remoteaddr); 64932115b10SPawel Jakub Dawidek goto close; 65032115b10SPawel Jakub Dawidek } 65132115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg"); 65232115b10SPawel Jakub Dawidek if (errmsg != NULL) { 65332115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg); 65432115b10SPawel Jakub Dawidek nv_free(nvin); 65532115b10SPawel Jakub Dawidek goto close; 65632115b10SPawel Jakub Dawidek } 65732115b10SPawel Jakub Dawidek datasize = nv_get_int64(nvin, "datasize"); 65832115b10SPawel Jakub Dawidek if (datasize != res->hr_datasize) { 65932115b10SPawel Jakub Dawidek pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).", 66032115b10SPawel Jakub Dawidek (intmax_t)res->hr_datasize, (intmax_t)datasize); 66132115b10SPawel Jakub Dawidek nv_free(nvin); 66232115b10SPawel Jakub Dawidek goto close; 66332115b10SPawel Jakub Dawidek } 66432115b10SPawel Jakub Dawidek extentsize = nv_get_int32(nvin, "extentsize"); 66532115b10SPawel Jakub Dawidek if (extentsize != res->hr_extentsize) { 66632115b10SPawel Jakub Dawidek pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).", 66732115b10SPawel Jakub Dawidek (ssize_t)res->hr_extentsize, (ssize_t)extentsize); 66832115b10SPawel Jakub Dawidek nv_free(nvin); 66932115b10SPawel Jakub Dawidek goto close; 67032115b10SPawel Jakub Dawidek } 67132115b10SPawel Jakub Dawidek res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt"); 67232115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 67332115b10SPawel Jakub Dawidek res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc"); 67432115b10SPawel Jakub Dawidek map = NULL; 67532115b10SPawel Jakub Dawidek mapsize = nv_get_uint32(nvin, "mapsize"); 67632115b10SPawel Jakub Dawidek if (mapsize > 0) { 67732115b10SPawel Jakub Dawidek map = malloc(mapsize); 67832115b10SPawel Jakub Dawidek if (map == NULL) { 67932115b10SPawel Jakub Dawidek pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).", 68032115b10SPawel Jakub Dawidek (uintmax_t)mapsize); 68132115b10SPawel Jakub Dawidek nv_free(nvin); 68232115b10SPawel Jakub Dawidek goto close; 68332115b10SPawel Jakub Dawidek } 68432115b10SPawel Jakub Dawidek /* 68532115b10SPawel Jakub Dawidek * Remote node have some dirty extents on its own, lets 68632115b10SPawel Jakub Dawidek * download its activemap. 68732115b10SPawel Jakub Dawidek */ 6880d9014f3SPawel Jakub Dawidek if (hast_proto_recv_data(res, out, nvin, map, 68932115b10SPawel Jakub Dawidek mapsize) < 0) { 69032115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 69132115b10SPawel Jakub Dawidek "Unable to receive remote activemap"); 69232115b10SPawel Jakub Dawidek nv_free(nvin); 69332115b10SPawel Jakub Dawidek free(map); 69432115b10SPawel Jakub Dawidek goto close; 69532115b10SPawel Jakub Dawidek } 69632115b10SPawel Jakub Dawidek /* 69732115b10SPawel Jakub Dawidek * Merge local and remote bitmaps. 69832115b10SPawel Jakub Dawidek */ 69932115b10SPawel Jakub Dawidek activemap_merge(res->hr_amp, map, mapsize); 70032115b10SPawel Jakub Dawidek free(map); 70132115b10SPawel Jakub Dawidek /* 70232115b10SPawel Jakub Dawidek * Now that we merged bitmaps from both nodes, flush it to the 70332115b10SPawel Jakub Dawidek * disk before we start to synchronize. 70432115b10SPawel Jakub Dawidek */ 70532115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 70632115b10SPawel Jakub Dawidek } 707584a9bc3SPawel Jakub Dawidek nv_free(nvin); 70832115b10SPawel Jakub Dawidek pjdlog_info("Connected to %s.", res->hr_remoteaddr); 7090d9014f3SPawel Jakub Dawidek if (inp != NULL && outp != NULL) { 7100d9014f3SPawel Jakub Dawidek *inp = in; 7110d9014f3SPawel Jakub Dawidek *outp = out; 7120d9014f3SPawel Jakub Dawidek } else { 7130d9014f3SPawel Jakub Dawidek res->hr_remotein = in; 7140d9014f3SPawel Jakub Dawidek res->hr_remoteout = out; 7150d9014f3SPawel Jakub Dawidek } 7165bdff860SPawel Jakub Dawidek event_send(res, EVENT_CONNECT); 7170d9014f3SPawel Jakub Dawidek return (true); 7180d9014f3SPawel Jakub Dawidek close: 7192be8fd75SPawel Jakub Dawidek if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0) 7205bdff860SPawel Jakub Dawidek event_send(res, EVENT_SPLITBRAIN); 7210d9014f3SPawel Jakub Dawidek proto_close(out); 7220d9014f3SPawel Jakub Dawidek if (in != NULL) 7230d9014f3SPawel Jakub Dawidek proto_close(in); 7240d9014f3SPawel Jakub Dawidek return (false); 7250d9014f3SPawel Jakub Dawidek } 7260d9014f3SPawel Jakub Dawidek 7270d9014f3SPawel Jakub Dawidek static void 7280d9014f3SPawel Jakub Dawidek sync_start(void) 7290d9014f3SPawel Jakub Dawidek { 7300d9014f3SPawel Jakub Dawidek 73132115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 73232115b10SPawel Jakub Dawidek sync_inprogress = true; 73332115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 73432115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 73532115b10SPawel Jakub Dawidek } 73632115b10SPawel Jakub Dawidek 73732115b10SPawel Jakub Dawidek static void 73855ce1e7cSPawel Jakub Dawidek sync_stop(void) 73955ce1e7cSPawel Jakub Dawidek { 74055ce1e7cSPawel Jakub Dawidek 74155ce1e7cSPawel Jakub Dawidek mtx_lock(&sync_lock); 74255ce1e7cSPawel Jakub Dawidek if (sync_inprogress) 74355ce1e7cSPawel Jakub Dawidek sync_inprogress = false; 74455ce1e7cSPawel Jakub Dawidek mtx_unlock(&sync_lock); 74555ce1e7cSPawel Jakub Dawidek } 74655ce1e7cSPawel Jakub Dawidek 74755ce1e7cSPawel Jakub Dawidek static void 74832115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res) 74932115b10SPawel Jakub Dawidek { 75032115b10SPawel Jakub Dawidek struct g_gate_ctl_create ggiocreate; 75132115b10SPawel Jakub Dawidek struct g_gate_ctl_cancel ggiocancel; 75232115b10SPawel Jakub Dawidek 75332115b10SPawel Jakub Dawidek /* 75432115b10SPawel Jakub Dawidek * We communicate with ggate via /dev/ggctl. Open it. 75532115b10SPawel Jakub Dawidek */ 75632115b10SPawel Jakub Dawidek res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR); 75732115b10SPawel Jakub Dawidek if (res->hr_ggatefd < 0) 75832115b10SPawel Jakub Dawidek primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME); 75932115b10SPawel Jakub Dawidek /* 76032115b10SPawel Jakub Dawidek * Create provider before trying to connect, as connection failure 76132115b10SPawel Jakub Dawidek * is not critical, but may take some time. 76232115b10SPawel Jakub Dawidek */ 7634e47b646SPawel Jakub Dawidek bzero(&ggiocreate, sizeof(ggiocreate)); 76432115b10SPawel Jakub Dawidek ggiocreate.gctl_version = G_GATE_VERSION; 76532115b10SPawel Jakub Dawidek ggiocreate.gctl_mediasize = res->hr_datasize; 76632115b10SPawel Jakub Dawidek ggiocreate.gctl_sectorsize = res->hr_local_sectorsize; 76732115b10SPawel Jakub Dawidek ggiocreate.gctl_flags = 0; 76820b77db9SPawel Jakub Dawidek ggiocreate.gctl_maxcount = G_GATE_MAX_QUEUE_SIZE; 76932115b10SPawel Jakub Dawidek ggiocreate.gctl_timeout = 0; 77032115b10SPawel Jakub Dawidek ggiocreate.gctl_unit = G_GATE_NAME_GIVEN; 77132115b10SPawel Jakub Dawidek snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s", 77232115b10SPawel Jakub Dawidek res->hr_provname); 77332115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) { 77432115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s created.", res->hr_provname); 77532115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocreate.gctl_unit; 77632115b10SPawel Jakub Dawidek return; 77732115b10SPawel Jakub Dawidek } 77832115b10SPawel Jakub Dawidek if (errno != EEXIST) { 77932115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to create hast/%s device", 78032115b10SPawel Jakub Dawidek res->hr_provname); 78132115b10SPawel Jakub Dawidek } 78232115b10SPawel Jakub Dawidek pjdlog_debug(1, 78332115b10SPawel Jakub Dawidek "Device hast/%s already exists, we will try to take it over.", 78432115b10SPawel Jakub Dawidek res->hr_provname); 78532115b10SPawel Jakub Dawidek /* 78632115b10SPawel Jakub Dawidek * If we received EEXIST, we assume that the process who created the 78732115b10SPawel Jakub Dawidek * provider died and didn't clean up. In that case we will start from 78832115b10SPawel Jakub Dawidek * where he left of. 78932115b10SPawel Jakub Dawidek */ 7904e47b646SPawel Jakub Dawidek bzero(&ggiocancel, sizeof(ggiocancel)); 79132115b10SPawel Jakub Dawidek ggiocancel.gctl_version = G_GATE_VERSION; 79232115b10SPawel Jakub Dawidek ggiocancel.gctl_unit = G_GATE_NAME_GIVEN; 79332115b10SPawel Jakub Dawidek snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s", 79432115b10SPawel Jakub Dawidek res->hr_provname); 79532115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) { 79632115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s recovered.", res->hr_provname); 79732115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocancel.gctl_unit; 79832115b10SPawel Jakub Dawidek return; 79932115b10SPawel Jakub Dawidek } 80032115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to take over hast/%s device", 80132115b10SPawel Jakub Dawidek res->hr_provname); 80232115b10SPawel Jakub Dawidek } 80332115b10SPawel Jakub Dawidek 80432115b10SPawel Jakub Dawidek void 80532115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res) 80632115b10SPawel Jakub Dawidek { 80732115b10SPawel Jakub Dawidek pthread_t td; 80832115b10SPawel Jakub Dawidek pid_t pid; 809*bc7a916aSMikolaj Golub int error, mode, debuglevel; 81032115b10SPawel Jakub Dawidek 81132115b10SPawel Jakub Dawidek /* 81232ecf620SPawel Jakub Dawidek * Create communication channel for sending control commands from 81332ecf620SPawel Jakub Dawidek * parent to child. 81432115b10SPawel Jakub Dawidek */ 81532115b10SPawel Jakub Dawidek if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 816d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 81732115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8186be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 81932115b10SPawel Jakub Dawidek "Unable to create control sockets between parent and child"); 82032115b10SPawel Jakub Dawidek } 8215bdff860SPawel Jakub Dawidek /* 82232ecf620SPawel Jakub Dawidek * Create communication channel for sending events from child to parent. 8235bdff860SPawel Jakub Dawidek */ 8245bdff860SPawel Jakub Dawidek if (proto_client("socketpair://", &res->hr_event) < 0) { 825d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 8265bdff860SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8275bdff860SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 8285bdff860SPawel Jakub Dawidek "Unable to create event sockets between child and parent"); 8295bdff860SPawel Jakub Dawidek } 83032ecf620SPawel Jakub Dawidek /* 83132ecf620SPawel Jakub Dawidek * Create communication channel for sending connection requests from 83232ecf620SPawel Jakub Dawidek * child to parent. 83332ecf620SPawel Jakub Dawidek */ 83432ecf620SPawel Jakub Dawidek if (proto_client("socketpair://", &res->hr_conn) < 0) { 83532ecf620SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 83632ecf620SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 83732ecf620SPawel Jakub Dawidek pjdlog_exit(EX_OSERR, 83832ecf620SPawel Jakub Dawidek "Unable to create connection sockets between child and parent"); 83932ecf620SPawel Jakub Dawidek } 84032115b10SPawel Jakub Dawidek 84132115b10SPawel Jakub Dawidek pid = fork(); 84232115b10SPawel Jakub Dawidek if (pid < 0) { 843d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */ 84432115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh)); 8456be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_TEMPFAIL, "Unable to fork"); 84632115b10SPawel Jakub Dawidek } 84732115b10SPawel Jakub Dawidek 84832115b10SPawel Jakub Dawidek if (pid > 0) { 84932115b10SPawel Jakub Dawidek /* This is parent. */ 8505bdff860SPawel Jakub Dawidek /* Declare that we are receiver. */ 8515bdff860SPawel Jakub Dawidek proto_recv(res->hr_event, NULL, 0); 85232ecf620SPawel Jakub Dawidek proto_recv(res->hr_conn, NULL, 0); 853da1783eaSPawel Jakub Dawidek /* Declare that we are sender. */ 854da1783eaSPawel Jakub Dawidek proto_send(res->hr_ctrl, NULL, 0); 85532115b10SPawel Jakub Dawidek res->hr_workerpid = pid; 85632115b10SPawel Jakub Dawidek return; 85732115b10SPawel Jakub Dawidek } 858ecc99c89SPawel Jakub Dawidek 8595b41e644SPawel Jakub Dawidek gres = res; 860da1783eaSPawel Jakub Dawidek mode = pjdlog_mode_get(); 861*bc7a916aSMikolaj Golub debuglevel = pjdlog_debug_get(); 86232115b10SPawel Jakub Dawidek 8635bdff860SPawel Jakub Dawidek /* Declare that we are sender. */ 8645bdff860SPawel Jakub Dawidek proto_send(res->hr_event, NULL, 0); 86532ecf620SPawel Jakub Dawidek proto_send(res->hr_conn, NULL, 0); 866da1783eaSPawel Jakub Dawidek /* Declare that we are receiver. */ 867da1783eaSPawel Jakub Dawidek proto_recv(res->hr_ctrl, NULL, 0); 868da1783eaSPawel Jakub Dawidek descriptors_cleanup(res); 869da1783eaSPawel Jakub Dawidek 870f463896eSPawel Jakub Dawidek descriptors_assert(res, mode); 871f463896eSPawel Jakub Dawidek 872da1783eaSPawel Jakub Dawidek pjdlog_init(mode); 873*bc7a916aSMikolaj Golub pjdlog_debug_set(debuglevel); 874da1783eaSPawel Jakub Dawidek pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 875da1783eaSPawel Jakub Dawidek setproctitle("%s (primary)", res->hr_name); 8765bdff860SPawel Jakub Dawidek 87732115b10SPawel Jakub Dawidek init_local(res); 87832115b10SPawel Jakub Dawidek init_ggate(res); 87932115b10SPawel Jakub Dawidek init_environment(res); 880115f4e5cSPawel Jakub Dawidek 8816d7967deSPawel Jakub Dawidek if (drop_privs() != 0) { 8826d7967deSPawel Jakub Dawidek cleanup(res); 8836d7967deSPawel Jakub Dawidek exit(EX_CONFIG); 8846d7967deSPawel Jakub Dawidek } 885f4c96f94SPawel Jakub Dawidek pjdlog_info("Privileges successfully dropped."); 8866d7967deSPawel Jakub Dawidek 8878b70e6aeSPawel Jakub Dawidek /* 8884a88128bSPawel Jakub Dawidek * Create the guard thread first, so we can handle signals from the 8894a88128bSPawel Jakub Dawidek * very begining. 8904a88128bSPawel Jakub Dawidek */ 8914a88128bSPawel Jakub Dawidek error = pthread_create(&td, NULL, guard_thread, res); 8922ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 8934a88128bSPawel Jakub Dawidek /* 8948b70e6aeSPawel Jakub Dawidek * Create the control thread before sending any event to the parent, 8958b70e6aeSPawel Jakub Dawidek * as we can deadlock when parent sends control request to worker, 8968b70e6aeSPawel Jakub Dawidek * but worker has no control thread started yet, so parent waits. 8978b70e6aeSPawel Jakub Dawidek * In the meantime worker sends an event to the parent, but parent 8988b70e6aeSPawel Jakub Dawidek * is unable to handle the event, because it waits for control 8998b70e6aeSPawel Jakub Dawidek * request response. 9008b70e6aeSPawel Jakub Dawidek */ 9018b70e6aeSPawel Jakub Dawidek error = pthread_create(&td, NULL, ctrl_thread, res); 9022ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 9038b70e6aeSPawel Jakub Dawidek if (real_remote(res) && init_remote(res, NULL, NULL)) 9048b70e6aeSPawel Jakub Dawidek sync_start(); 90532115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_recv_thread, res); 9062ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 90732115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, local_send_thread, res); 9082ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 90932115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_send_thread, res); 9102ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 91132115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_recv_thread, res); 9122ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 91332115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_send_thread, res); 9142ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0); 9154a88128bSPawel Jakub Dawidek (void)sync_thread(res); 91632115b10SPawel Jakub Dawidek } 91732115b10SPawel Jakub Dawidek 91832115b10SPawel Jakub Dawidek static void 91932115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...) 92032115b10SPawel Jakub Dawidek { 92132115b10SPawel Jakub Dawidek char msg[1024]; 92232115b10SPawel Jakub Dawidek va_list ap; 92332115b10SPawel Jakub Dawidek int len; 92432115b10SPawel Jakub Dawidek 92532115b10SPawel Jakub Dawidek va_start(ap, fmt); 92632115b10SPawel Jakub Dawidek len = vsnprintf(msg, sizeof(msg), fmt, ap); 92732115b10SPawel Jakub Dawidek va_end(ap); 92832115b10SPawel Jakub Dawidek if ((size_t)len < sizeof(msg)) { 92932115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 93032115b10SPawel Jakub Dawidek case BIO_READ: 93132115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 93232115b10SPawel Jakub Dawidek "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 93332115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 93432115b10SPawel Jakub Dawidek break; 93532115b10SPawel Jakub Dawidek case BIO_DELETE: 93632115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 93732115b10SPawel Jakub Dawidek "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 93832115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 93932115b10SPawel Jakub Dawidek break; 94032115b10SPawel Jakub Dawidek case BIO_FLUSH: 94132115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 94232115b10SPawel Jakub Dawidek break; 94332115b10SPawel Jakub Dawidek case BIO_WRITE: 94432115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 94532115b10SPawel Jakub Dawidek "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset, 94632115b10SPawel Jakub Dawidek (uintmax_t)ggio->gctl_length); 94732115b10SPawel Jakub Dawidek break; 94832115b10SPawel Jakub Dawidek default: 94932115b10SPawel Jakub Dawidek (void)snprintf(msg + len, sizeof(msg) - len, 95032115b10SPawel Jakub Dawidek "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd); 95132115b10SPawel Jakub Dawidek break; 95232115b10SPawel Jakub Dawidek } 95332115b10SPawel Jakub Dawidek } 95432115b10SPawel Jakub Dawidek pjdlog_common(loglevel, debuglevel, -1, "%s", msg); 95532115b10SPawel Jakub Dawidek } 95632115b10SPawel Jakub Dawidek 95732115b10SPawel Jakub Dawidek static void 95832115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp) 95932115b10SPawel Jakub Dawidek { 96032115b10SPawel Jakub Dawidek 96132115b10SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 96232115b10SPawel Jakub Dawidek /* 96332115b10SPawel Jakub Dawidek * A race is possible between dropping rlock and acquiring wlock - 96432115b10SPawel Jakub Dawidek * another thread can close connection in-between. 96532115b10SPawel Jakub Dawidek */ 96632115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 9672ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 9682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 96932115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 97032115b10SPawel Jakub Dawidek return; 97132115b10SPawel Jakub Dawidek } 97232115b10SPawel Jakub Dawidek 9732ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 9742ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 97532115b10SPawel Jakub Dawidek 9768f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing incoming connection to %s.", 97732115b10SPawel Jakub Dawidek res->hr_remoteaddr); 97832115b10SPawel Jakub Dawidek proto_close(res->hr_remotein); 97932115b10SPawel Jakub Dawidek res->hr_remotein = NULL; 9808f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing outgoing connection to %s.", 98132115b10SPawel Jakub Dawidek res->hr_remoteaddr); 98232115b10SPawel Jakub Dawidek proto_close(res->hr_remoteout); 98332115b10SPawel Jakub Dawidek res->hr_remoteout = NULL; 98432115b10SPawel Jakub Dawidek 98532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 98632115b10SPawel Jakub Dawidek 9878f8c798cSPawel Jakub Dawidek pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr); 9888f8c798cSPawel Jakub Dawidek 98932115b10SPawel Jakub Dawidek /* 99032115b10SPawel Jakub Dawidek * Stop synchronization if in-progress. 99132115b10SPawel Jakub Dawidek */ 99255ce1e7cSPawel Jakub Dawidek sync_stop(); 9935b41e644SPawel Jakub Dawidek 9945bdff860SPawel Jakub Dawidek event_send(res, EVENT_DISCONNECT); 995f7fe83f9SPawel Jakub Dawidek } 99632115b10SPawel Jakub Dawidek 99732115b10SPawel Jakub Dawidek /* 99832115b10SPawel Jakub Dawidek * Thread receives ggate I/O requests from the kernel and passes them to 99932115b10SPawel Jakub Dawidek * appropriate threads: 100032115b10SPawel Jakub Dawidek * WRITE - always goes to both local_send and remote_send threads 100132115b10SPawel Jakub Dawidek * READ (when the block is up-to-date on local component) - 100232115b10SPawel Jakub Dawidek * only local_send thread 100332115b10SPawel Jakub Dawidek * READ (when the block isn't up-to-date on local component) - 100432115b10SPawel Jakub Dawidek * only remote_send thread 100532115b10SPawel Jakub Dawidek * DELETE - always goes to both local_send and remote_send threads 100632115b10SPawel Jakub Dawidek * FLUSH - always goes to both local_send and remote_send threads 100732115b10SPawel Jakub Dawidek */ 100832115b10SPawel Jakub Dawidek static void * 100932115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg) 101032115b10SPawel Jakub Dawidek { 101132115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 101232115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 101332115b10SPawel Jakub Dawidek struct hio *hio; 101432115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 101532115b10SPawel Jakub Dawidek int error; 101632115b10SPawel Jakub Dawidek 101732115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 101832115b10SPawel Jakub Dawidek 101932115b10SPawel Jakub Dawidek for (;;) { 102032115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: Taking free request."); 102132115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 102232115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio); 102332115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 102432115b10SPawel Jakub Dawidek ggio->gctl_unit = res->hr_ggateunit; 102532115b10SPawel Jakub Dawidek ggio->gctl_length = MAXPHYS; 102632115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 102732115b10SPawel Jakub Dawidek pjdlog_debug(2, 102832115b10SPawel Jakub Dawidek "ggate_recv: (%p) Waiting for request from the kernel.", 102932115b10SPawel Jakub Dawidek hio); 103032115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) { 103132115b10SPawel Jakub Dawidek if (sigexit_received) 103232115b10SPawel Jakub Dawidek pthread_exit(NULL); 103332115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_START failed"); 103432115b10SPawel Jakub Dawidek } 103532115b10SPawel Jakub Dawidek error = ggio->gctl_error; 103632115b10SPawel Jakub Dawidek switch (error) { 103732115b10SPawel Jakub Dawidek case 0: 103832115b10SPawel Jakub Dawidek break; 103932115b10SPawel Jakub Dawidek case ECANCELED: 104032115b10SPawel Jakub Dawidek /* Exit gracefully. */ 104132115b10SPawel Jakub Dawidek if (!sigexit_received) { 104232115b10SPawel Jakub Dawidek pjdlog_debug(2, 104332115b10SPawel Jakub Dawidek "ggate_recv: (%p) Received cancel from the kernel.", 104432115b10SPawel Jakub Dawidek hio); 104532115b10SPawel Jakub Dawidek pjdlog_info("Received cancel from the kernel, exiting."); 104632115b10SPawel Jakub Dawidek } 104732115b10SPawel Jakub Dawidek pthread_exit(NULL); 104832115b10SPawel Jakub Dawidek case ENOMEM: 104932115b10SPawel Jakub Dawidek /* 105032115b10SPawel Jakub Dawidek * Buffer too small? Impossible, we allocate MAXPHYS 105132115b10SPawel Jakub Dawidek * bytes - request can't be bigger than that. 105232115b10SPawel Jakub Dawidek */ 105332115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 105432115b10SPawel Jakub Dawidek case ENXIO: 105532115b10SPawel Jakub Dawidek default: 105632115b10SPawel Jakub Dawidek primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.", 105732115b10SPawel Jakub Dawidek strerror(error)); 105832115b10SPawel Jakub Dawidek } 105932115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 106032115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 106132115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, 106232115b10SPawel Jakub Dawidek "ggate_recv: (%p) Request received from the kernel: ", 106332115b10SPawel Jakub Dawidek hio); 106432115b10SPawel Jakub Dawidek /* 106532115b10SPawel Jakub Dawidek * Inform all components about new write request. 106632115b10SPawel Jakub Dawidek * For read request prefer local component unless the given 106732115b10SPawel Jakub Dawidek * range is out-of-date, then use remote component. 106832115b10SPawel Jakub Dawidek */ 106932115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 107032115b10SPawel Jakub Dawidek case BIO_READ: 107132115b10SPawel Jakub Dawidek pjdlog_debug(2, 107232115b10SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queue.", 107332115b10SPawel Jakub Dawidek hio); 107432115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 107532115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 107632115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF || 107732115b10SPawel Jakub Dawidek res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 107832115b10SPawel Jakub Dawidek /* 107932115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 108032115b10SPawel Jakub Dawidek * so handle request locally. 108132115b10SPawel Jakub Dawidek */ 108232115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 108332115b10SPawel Jakub Dawidek ncomp = 0; 108432115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == 108532115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY) */ { 10862ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == 108732115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY); 108832115b10SPawel Jakub Dawidek /* 108932115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 109032115b10SPawel Jakub Dawidek * so send request to the remote node. 109132115b10SPawel Jakub Dawidek */ 109232115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 109332115b10SPawel Jakub Dawidek ncomp = 1; 109432115b10SPawel Jakub Dawidek } 109532115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 109632115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 109732115b10SPawel Jakub Dawidek break; 109832115b10SPawel Jakub Dawidek case BIO_WRITE: 1099ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) { 1100ce837469SPawel Jakub Dawidek /* This is first write, initialize resuid. */ 1101ce837469SPawel Jakub Dawidek (void)init_resuid(res); 1102ce837469SPawel Jakub Dawidek } 110332115b10SPawel Jakub Dawidek for (;;) { 110432115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 110532115b10SPawel Jakub Dawidek if (rangelock_islocked(range_sync, 110632115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 110732115b10SPawel Jakub Dawidek pjdlog_debug(2, 110832115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu locked.", 110932115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 111032115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 111132115b10SPawel Jakub Dawidek range_regular_wait = true; 111232115b10SPawel Jakub Dawidek cv_wait(&range_regular_cond, &range_lock); 111332115b10SPawel Jakub Dawidek range_regular_wait = false; 111432115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 111532115b10SPawel Jakub Dawidek continue; 111632115b10SPawel Jakub Dawidek } 111732115b10SPawel Jakub Dawidek if (rangelock_add(range_regular, 111832115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length) < 0) { 111932115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 112032115b10SPawel Jakub Dawidek pjdlog_debug(2, 112132115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu is already locked, waiting.", 112232115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset, 112332115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length); 112432115b10SPawel Jakub Dawidek sleep(1); 112532115b10SPawel Jakub Dawidek continue; 112632115b10SPawel Jakub Dawidek } 112732115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 112832115b10SPawel Jakub Dawidek break; 112932115b10SPawel Jakub Dawidek } 113032115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 113132115b10SPawel Jakub Dawidek if (activemap_write_start(res->hr_amp, 113232115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) { 113332115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 113432115b10SPawel Jakub Dawidek } 113532115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 113632115b10SPawel Jakub Dawidek /* FALLTHROUGH */ 113732115b10SPawel Jakub Dawidek case BIO_DELETE: 113832115b10SPawel Jakub Dawidek case BIO_FLUSH: 113932115b10SPawel Jakub Dawidek pjdlog_debug(2, 114032115b10SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queues.", 114132115b10SPawel Jakub Dawidek hio); 114232115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, ncomps); 114332115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 114432115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ii); 114532115b10SPawel Jakub Dawidek break; 114632115b10SPawel Jakub Dawidek } 114732115b10SPawel Jakub Dawidek } 114832115b10SPawel Jakub Dawidek /* NOTREACHED */ 114932115b10SPawel Jakub Dawidek return (NULL); 115032115b10SPawel Jakub Dawidek } 115132115b10SPawel Jakub Dawidek 115232115b10SPawel Jakub Dawidek /* 115332115b10SPawel Jakub Dawidek * Thread reads from or writes to local component. 115432115b10SPawel Jakub Dawidek * If local read fails, it redirects it to remote_send thread. 115532115b10SPawel Jakub Dawidek */ 115632115b10SPawel Jakub Dawidek static void * 115732115b10SPawel Jakub Dawidek local_send_thread(void *arg) 115832115b10SPawel Jakub Dawidek { 115932115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 116032115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 116132115b10SPawel Jakub Dawidek struct hio *hio; 116232115b10SPawel Jakub Dawidek unsigned int ncomp, rncomp; 116332115b10SPawel Jakub Dawidek ssize_t ret; 116432115b10SPawel Jakub Dawidek 116532115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 116632115b10SPawel Jakub Dawidek ncomp = 0; 116732115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 116832115b10SPawel Jakub Dawidek rncomp = 1; 116932115b10SPawel Jakub Dawidek 117032115b10SPawel Jakub Dawidek for (;;) { 117132115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: Taking request."); 1172448efa94SPawel Jakub Dawidek QUEUE_TAKE1(hio, send, ncomp, 0); 117332115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: (%p) Got request.", hio); 117432115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 117532115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 117632115b10SPawel Jakub Dawidek case BIO_READ: 117732115b10SPawel Jakub Dawidek ret = pread(res->hr_localfd, ggio->gctl_data, 117832115b10SPawel Jakub Dawidek ggio->gctl_length, 117932115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 118032115b10SPawel Jakub Dawidek if (ret == ggio->gctl_length) 118132115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 118232115b10SPawel Jakub Dawidek else { 118332115b10SPawel Jakub Dawidek /* 118432115b10SPawel Jakub Dawidek * If READ failed, try to read from remote node. 118532115b10SPawel Jakub Dawidek */ 1186cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 1187cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1188cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s), trying remote node. ", 1189cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1190cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 1191cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1192cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd), trying remote node. ", 1193fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1194cd7b7ee5SPawel Jakub Dawidek } 119532115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, rncomp); 119632115b10SPawel Jakub Dawidek continue; 119732115b10SPawel Jakub Dawidek } 119832115b10SPawel Jakub Dawidek break; 119932115b10SPawel Jakub Dawidek case BIO_WRITE: 120032115b10SPawel Jakub Dawidek ret = pwrite(res->hr_localfd, ggio->gctl_data, 120132115b10SPawel Jakub Dawidek ggio->gctl_length, 120232115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff); 1203cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 120432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1205cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1206cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1207cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1208cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) { 120932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = EIO; 1210cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1211cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd): ", 1212fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length); 1213cd7b7ee5SPawel Jakub Dawidek } else { 121432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1215cd7b7ee5SPawel Jakub Dawidek } 121632115b10SPawel Jakub Dawidek break; 121732115b10SPawel Jakub Dawidek case BIO_DELETE: 121832115b10SPawel Jakub Dawidek ret = g_delete(res->hr_localfd, 121932115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff, 122032115b10SPawel Jakub Dawidek ggio->gctl_length); 1221cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 122232115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1223cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1224cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1225cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1226cd7b7ee5SPawel Jakub Dawidek } else { 122732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1228cd7b7ee5SPawel Jakub Dawidek } 122932115b10SPawel Jakub Dawidek break; 123032115b10SPawel Jakub Dawidek case BIO_FLUSH: 123132115b10SPawel Jakub Dawidek ret = g_flush(res->hr_localfd); 1232cd7b7ee5SPawel Jakub Dawidek if (ret < 0) { 123332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 1234cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio, 1235cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ", 1236cd7b7ee5SPawel Jakub Dawidek strerror(errno)); 1237cd7b7ee5SPawel Jakub Dawidek } else { 123832115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 1239cd7b7ee5SPawel Jakub Dawidek } 124032115b10SPawel Jakub Dawidek break; 124132115b10SPawel Jakub Dawidek } 124232115b10SPawel Jakub Dawidek if (refcount_release(&hio->hio_countdown)) { 124332115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 124432115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 124532115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 124632115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 124732115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 124832115b10SPawel Jakub Dawidek } else { 124932115b10SPawel Jakub Dawidek pjdlog_debug(2, 125032115b10SPawel Jakub Dawidek "local_send: (%p) Moving request to the done queue.", 125132115b10SPawel Jakub Dawidek hio); 125232115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 125332115b10SPawel Jakub Dawidek } 125432115b10SPawel Jakub Dawidek } 125532115b10SPawel Jakub Dawidek } 125632115b10SPawel Jakub Dawidek /* NOTREACHED */ 125732115b10SPawel Jakub Dawidek return (NULL); 125832115b10SPawel Jakub Dawidek } 125932115b10SPawel Jakub Dawidek 1260448efa94SPawel Jakub Dawidek static void 1261448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp) 1262448efa94SPawel Jakub Dawidek { 1263448efa94SPawel Jakub Dawidek struct nv *nv; 1264448efa94SPawel Jakub Dawidek 126521e7bc5eSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 126621e7bc5eSPawel Jakub Dawidek 126721e7bc5eSPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 126821e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1269448efa94SPawel Jakub Dawidek return; 127021e7bc5eSPawel Jakub Dawidek } 1271448efa94SPawel Jakub Dawidek 12722ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 12732ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 1274448efa94SPawel Jakub Dawidek 1275448efa94SPawel Jakub Dawidek nv = nv_alloc(); 1276448efa94SPawel Jakub Dawidek nv_add_uint8(nv, HIO_KEEPALIVE, "cmd"); 1277448efa94SPawel Jakub Dawidek if (nv_error(nv) != 0) { 127821e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1279448efa94SPawel Jakub Dawidek nv_free(nv); 1280448efa94SPawel Jakub Dawidek pjdlog_debug(1, 1281448efa94SPawel Jakub Dawidek "keepalive_send: Unable to prepare header to send."); 1282448efa94SPawel Jakub Dawidek return; 1283448efa94SPawel Jakub Dawidek } 1284448efa94SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) { 128521e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1286448efa94SPawel Jakub Dawidek pjdlog_common(LOG_DEBUG, 1, errno, 1287448efa94SPawel Jakub Dawidek "keepalive_send: Unable to send request"); 1288448efa94SPawel Jakub Dawidek nv_free(nv); 1289448efa94SPawel Jakub Dawidek remote_close(res, ncomp); 1290448efa94SPawel Jakub Dawidek return; 1291448efa94SPawel Jakub Dawidek } 129221e7bc5eSPawel Jakub Dawidek 129321e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 1294448efa94SPawel Jakub Dawidek nv_free(nv); 1295448efa94SPawel Jakub Dawidek pjdlog_debug(2, "keepalive_send: Request sent."); 1296448efa94SPawel Jakub Dawidek } 1297448efa94SPawel Jakub Dawidek 129832115b10SPawel Jakub Dawidek /* 129932115b10SPawel Jakub Dawidek * Thread sends request to secondary node. 130032115b10SPawel Jakub Dawidek */ 130132115b10SPawel Jakub Dawidek static void * 130232115b10SPawel Jakub Dawidek remote_send_thread(void *arg) 130332115b10SPawel Jakub Dawidek { 130432115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 130532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1306448efa94SPawel Jakub Dawidek time_t lastcheck, now; 130732115b10SPawel Jakub Dawidek struct hio *hio; 130832115b10SPawel Jakub Dawidek struct nv *nv; 130932115b10SPawel Jakub Dawidek unsigned int ncomp; 131032115b10SPawel Jakub Dawidek bool wakeup; 131132115b10SPawel Jakub Dawidek uint64_t offset, length; 131232115b10SPawel Jakub Dawidek uint8_t cmd; 131332115b10SPawel Jakub Dawidek void *data; 131432115b10SPawel Jakub Dawidek 131532115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 131632115b10SPawel Jakub Dawidek ncomp = 1; 1317448efa94SPawel Jakub Dawidek lastcheck = time(NULL); 131832115b10SPawel Jakub Dawidek 131932115b10SPawel Jakub Dawidek for (;;) { 132032115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: Taking request."); 1321448efa94SPawel Jakub Dawidek QUEUE_TAKE1(hio, send, ncomp, RETRY_SLEEP); 1322448efa94SPawel Jakub Dawidek if (hio == NULL) { 1323448efa94SPawel Jakub Dawidek now = time(NULL); 1324448efa94SPawel Jakub Dawidek if (lastcheck + RETRY_SLEEP <= now) { 1325448efa94SPawel Jakub Dawidek keepalive_send(res, ncomp); 1326448efa94SPawel Jakub Dawidek lastcheck = now; 1327448efa94SPawel Jakub Dawidek } 1328448efa94SPawel Jakub Dawidek continue; 1329448efa94SPawel Jakub Dawidek } 133032115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: (%p) Got request.", hio); 133132115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 133232115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 133332115b10SPawel Jakub Dawidek case BIO_READ: 133432115b10SPawel Jakub Dawidek cmd = HIO_READ; 133532115b10SPawel Jakub Dawidek data = NULL; 133632115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 133732115b10SPawel Jakub Dawidek length = ggio->gctl_length; 133832115b10SPawel Jakub Dawidek break; 133932115b10SPawel Jakub Dawidek case BIO_WRITE: 134032115b10SPawel Jakub Dawidek cmd = HIO_WRITE; 134132115b10SPawel Jakub Dawidek data = ggio->gctl_data; 134232115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 134332115b10SPawel Jakub Dawidek length = ggio->gctl_length; 134432115b10SPawel Jakub Dawidek break; 134532115b10SPawel Jakub Dawidek case BIO_DELETE: 134632115b10SPawel Jakub Dawidek cmd = HIO_DELETE; 134732115b10SPawel Jakub Dawidek data = NULL; 134832115b10SPawel Jakub Dawidek offset = ggio->gctl_offset; 134932115b10SPawel Jakub Dawidek length = ggio->gctl_length; 135032115b10SPawel Jakub Dawidek break; 135132115b10SPawel Jakub Dawidek case BIO_FLUSH: 135232115b10SPawel Jakub Dawidek cmd = HIO_FLUSH; 135332115b10SPawel Jakub Dawidek data = NULL; 135432115b10SPawel Jakub Dawidek offset = 0; 135532115b10SPawel Jakub Dawidek length = 0; 135632115b10SPawel Jakub Dawidek break; 135732115b10SPawel Jakub Dawidek default: 13582ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(!"invalid condition"); 135932115b10SPawel Jakub Dawidek abort(); 136032115b10SPawel Jakub Dawidek } 136132115b10SPawel Jakub Dawidek nv = nv_alloc(); 136232115b10SPawel Jakub Dawidek nv_add_uint8(nv, cmd, "cmd"); 136332115b10SPawel Jakub Dawidek nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq"); 136432115b10SPawel Jakub Dawidek nv_add_uint64(nv, offset, "offset"); 136532115b10SPawel Jakub Dawidek nv_add_uint64(nv, length, "length"); 136632115b10SPawel Jakub Dawidek if (nv_error(nv) != 0) { 136732115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = nv_error(nv); 136832115b10SPawel Jakub Dawidek pjdlog_debug(2, 136932115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to prepare header to send.", 137032115b10SPawel Jakub Dawidek hio); 137132115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 137232115b10SPawel Jakub Dawidek "Unable to prepare header to send (%s): ", 137332115b10SPawel Jakub Dawidek strerror(nv_error(nv))); 137432115b10SPawel Jakub Dawidek /* Move failed request immediately to the done queue. */ 137532115b10SPawel Jakub Dawidek goto done_queue; 137632115b10SPawel Jakub Dawidek } 137732115b10SPawel Jakub Dawidek pjdlog_debug(2, 137832115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the recv queue.", 137932115b10SPawel Jakub Dawidek hio); 138032115b10SPawel Jakub Dawidek /* 138132115b10SPawel Jakub Dawidek * Protect connection from disappearing. 138232115b10SPawel Jakub Dawidek */ 138332115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 138432115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 138532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 138632115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = ENOTCONN; 138732115b10SPawel Jakub Dawidek goto done_queue; 138832115b10SPawel Jakub Dawidek } 138932115b10SPawel Jakub Dawidek /* 139032115b10SPawel Jakub Dawidek * Move the request to recv queue before sending it, because 139132115b10SPawel Jakub Dawidek * in different order we can get reply before we move request 139232115b10SPawel Jakub Dawidek * to recv queue. 139332115b10SPawel Jakub Dawidek */ 139432115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 139532115b10SPawel Jakub Dawidek wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]); 139632115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 139732115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 139832115b10SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, data, 139932115b10SPawel Jakub Dawidek data != NULL ? length : 0) < 0) { 140032115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 140132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 140232115b10SPawel Jakub Dawidek pjdlog_debug(2, 140332115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to send request.", hio); 140432115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio, 140532115b10SPawel Jakub Dawidek "Unable to send request (%s): ", 140632115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 1407ee087cdfSPawel Jakub Dawidek remote_close(res, ncomp); 140832115b10SPawel Jakub Dawidek /* 140932115b10SPawel Jakub Dawidek * Take request back from the receive queue and move 141032115b10SPawel Jakub Dawidek * it immediately to the done queue. 141132115b10SPawel Jakub Dawidek */ 141232115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 141332115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, hio_next[ncomp]); 141432115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 141532115b10SPawel Jakub Dawidek goto done_queue; 141632115b10SPawel Jakub Dawidek } 141732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 141832115b10SPawel Jakub Dawidek nv_free(nv); 141932115b10SPawel Jakub Dawidek if (wakeup) 142032115b10SPawel Jakub Dawidek cv_signal(&hio_recv_list_cond[ncomp]); 142132115b10SPawel Jakub Dawidek continue; 142232115b10SPawel Jakub Dawidek done_queue: 142332115b10SPawel Jakub Dawidek nv_free(nv); 142432115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 142532115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 142632115b10SPawel Jakub Dawidek continue; 142732115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 142832115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 142932115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 143032115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 143132115b10SPawel Jakub Dawidek continue; 143232115b10SPawel Jakub Dawidek } 143332115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 143432115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 143532115b10SPawel Jakub Dawidek if (activemap_need_sync(res->hr_amp, ggio->gctl_offset, 143632115b10SPawel Jakub Dawidek ggio->gctl_length)) { 143732115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 143832115b10SPawel Jakub Dawidek } 143932115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 144032115b10SPawel Jakub Dawidek } 144132115b10SPawel Jakub Dawidek if (!refcount_release(&hio->hio_countdown)) 144232115b10SPawel Jakub Dawidek continue; 144332115b10SPawel Jakub Dawidek pjdlog_debug(2, 144432115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the done queue.", 144532115b10SPawel Jakub Dawidek hio); 144632115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 144732115b10SPawel Jakub Dawidek } 144832115b10SPawel Jakub Dawidek /* NOTREACHED */ 144932115b10SPawel Jakub Dawidek return (NULL); 145032115b10SPawel Jakub Dawidek } 145132115b10SPawel Jakub Dawidek 145232115b10SPawel Jakub Dawidek /* 145332115b10SPawel Jakub Dawidek * Thread receives answer from secondary node and passes it to ggate_send 145432115b10SPawel Jakub Dawidek * thread. 145532115b10SPawel Jakub Dawidek */ 145632115b10SPawel Jakub Dawidek static void * 145732115b10SPawel Jakub Dawidek remote_recv_thread(void *arg) 145832115b10SPawel Jakub Dawidek { 145932115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 146032115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 146132115b10SPawel Jakub Dawidek struct hio *hio; 146232115b10SPawel Jakub Dawidek struct nv *nv; 146332115b10SPawel Jakub Dawidek unsigned int ncomp; 146432115b10SPawel Jakub Dawidek uint64_t seq; 146532115b10SPawel Jakub Dawidek int error; 146632115b10SPawel Jakub Dawidek 146732115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 146832115b10SPawel Jakub Dawidek ncomp = 1; 146932115b10SPawel Jakub Dawidek 147032115b10SPawel Jakub Dawidek for (;;) { 147132115b10SPawel Jakub Dawidek /* Wait until there is anything to receive. */ 147232115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 147332115b10SPawel Jakub Dawidek while (TAILQ_EMPTY(&hio_recv_list[ncomp])) { 147432115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_recv: No requests, waiting."); 147532115b10SPawel Jakub Dawidek cv_wait(&hio_recv_list_cond[ncomp], 147632115b10SPawel Jakub Dawidek &hio_recv_list_lock[ncomp]); 147732115b10SPawel Jakub Dawidek } 147832115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 147932115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 148032115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 148132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 148232115b10SPawel Jakub Dawidek /* 148332115b10SPawel Jakub Dawidek * Connection is dead, so move all pending requests to 148432115b10SPawel Jakub Dawidek * the done queue (one-by-one). 148532115b10SPawel Jakub Dawidek */ 148632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 148732115b10SPawel Jakub Dawidek hio = TAILQ_FIRST(&hio_recv_list[ncomp]); 14882ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(hio != NULL); 148932115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 149032115b10SPawel Jakub Dawidek hio_next[ncomp]); 149132115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 149232115b10SPawel Jakub Dawidek goto done_queue; 149332115b10SPawel Jakub Dawidek } 149432115b10SPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) { 149532115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 149632115b10SPawel Jakub Dawidek "Unable to receive reply header"); 149732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 149832115b10SPawel Jakub Dawidek remote_close(res, ncomp); 149932115b10SPawel Jakub Dawidek continue; 150032115b10SPawel Jakub Dawidek } 150132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 150232115b10SPawel Jakub Dawidek seq = nv_get_uint64(nv, "seq"); 150332115b10SPawel Jakub Dawidek if (seq == 0) { 150432115b10SPawel Jakub Dawidek pjdlog_error("Header contains no 'seq' field."); 150532115b10SPawel Jakub Dawidek nv_free(nv); 150632115b10SPawel Jakub Dawidek continue; 150732115b10SPawel Jakub Dawidek } 150832115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]); 150932115b10SPawel Jakub Dawidek TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) { 151032115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_seq == seq) { 151132115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio, 151232115b10SPawel Jakub Dawidek hio_next[ncomp]); 151332115b10SPawel Jakub Dawidek break; 151432115b10SPawel Jakub Dawidek } 151532115b10SPawel Jakub Dawidek } 151632115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]); 151732115b10SPawel Jakub Dawidek if (hio == NULL) { 151832115b10SPawel Jakub Dawidek pjdlog_error("Found no request matching received 'seq' field (%ju).", 151932115b10SPawel Jakub Dawidek (uintmax_t)seq); 152032115b10SPawel Jakub Dawidek nv_free(nv); 152132115b10SPawel Jakub Dawidek continue; 152232115b10SPawel Jakub Dawidek } 152332115b10SPawel Jakub Dawidek error = nv_get_int16(nv, "error"); 152432115b10SPawel Jakub Dawidek if (error != 0) { 152532115b10SPawel Jakub Dawidek /* Request failed on remote side. */ 152672089204SPawel Jakub Dawidek hio->hio_errors[ncomp] = error; 1527cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, &hio->hio_ggio, 1528cd7b7ee5SPawel Jakub Dawidek "Remote request failed (%s): ", strerror(error)); 152932115b10SPawel Jakub Dawidek nv_free(nv); 153032115b10SPawel Jakub Dawidek goto done_queue; 153132115b10SPawel Jakub Dawidek } 153232115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 153332115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) { 153432115b10SPawel Jakub Dawidek case BIO_READ: 153532115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 153632115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 153732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 153832115b10SPawel Jakub Dawidek nv_free(nv); 153932115b10SPawel Jakub Dawidek goto done_queue; 154032115b10SPawel Jakub Dawidek } 154132115b10SPawel Jakub Dawidek if (hast_proto_recv_data(res, res->hr_remotein, nv, 154232115b10SPawel Jakub Dawidek ggio->gctl_data, ggio->gctl_length) < 0) { 154332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno; 154432115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR, 154532115b10SPawel Jakub Dawidek "Unable to receive reply data"); 154632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 154732115b10SPawel Jakub Dawidek nv_free(nv); 154832115b10SPawel Jakub Dawidek remote_close(res, ncomp); 154932115b10SPawel Jakub Dawidek goto done_queue; 155032115b10SPawel Jakub Dawidek } 155132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 155232115b10SPawel Jakub Dawidek break; 155332115b10SPawel Jakub Dawidek case BIO_WRITE: 155432115b10SPawel Jakub Dawidek case BIO_DELETE: 155532115b10SPawel Jakub Dawidek case BIO_FLUSH: 155632115b10SPawel Jakub Dawidek break; 155732115b10SPawel Jakub Dawidek default: 15582ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(!"invalid condition"); 155932115b10SPawel Jakub Dawidek abort(); 156032115b10SPawel Jakub Dawidek } 156132115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0; 156232115b10SPawel Jakub Dawidek nv_free(nv); 156332115b10SPawel Jakub Dawidek done_queue: 156432115b10SPawel Jakub Dawidek if (refcount_release(&hio->hio_countdown)) { 156532115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) { 156632115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 156732115b10SPawel Jakub Dawidek SYNCREQDONE(hio); 156832115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 156932115b10SPawel Jakub Dawidek cv_signal(&sync_cond); 157032115b10SPawel Jakub Dawidek } else { 157132115b10SPawel Jakub Dawidek pjdlog_debug(2, 157232115b10SPawel Jakub Dawidek "remote_recv: (%p) Moving request to the done queue.", 157332115b10SPawel Jakub Dawidek hio); 157432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done); 157532115b10SPawel Jakub Dawidek } 157632115b10SPawel Jakub Dawidek } 157732115b10SPawel Jakub Dawidek } 157832115b10SPawel Jakub Dawidek /* NOTREACHED */ 157932115b10SPawel Jakub Dawidek return (NULL); 158032115b10SPawel Jakub Dawidek } 158132115b10SPawel Jakub Dawidek 158232115b10SPawel Jakub Dawidek /* 158332115b10SPawel Jakub Dawidek * Thread sends answer to the kernel. 158432115b10SPawel Jakub Dawidek */ 158532115b10SPawel Jakub Dawidek static void * 158632115b10SPawel Jakub Dawidek ggate_send_thread(void *arg) 158732115b10SPawel Jakub Dawidek { 158832115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 158932115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 159032115b10SPawel Jakub Dawidek struct hio *hio; 159132115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 159232115b10SPawel Jakub Dawidek 159332115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 159432115b10SPawel Jakub Dawidek 159532115b10SPawel Jakub Dawidek for (;;) { 159632115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: Taking request."); 159732115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, done); 159832115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: (%p) Got request.", hio); 159932115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 160032115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 160132115b10SPawel Jakub Dawidek if (hio->hio_errors[ii] == 0) { 160232115b10SPawel Jakub Dawidek /* 160332115b10SPawel Jakub Dawidek * One successful request is enough to declare 160432115b10SPawel Jakub Dawidek * success. 160532115b10SPawel Jakub Dawidek */ 160632115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 160732115b10SPawel Jakub Dawidek break; 160832115b10SPawel Jakub Dawidek } 160932115b10SPawel Jakub Dawidek } 161032115b10SPawel Jakub Dawidek if (ii == ncomps) { 161132115b10SPawel Jakub Dawidek /* 161232115b10SPawel Jakub Dawidek * None of the requests were successful. 161332115b10SPawel Jakub Dawidek * Use first error. 161432115b10SPawel Jakub Dawidek */ 161532115b10SPawel Jakub Dawidek ggio->gctl_error = hio->hio_errors[0]; 161632115b10SPawel Jakub Dawidek } 161732115b10SPawel Jakub Dawidek if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) { 161832115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 161932115b10SPawel Jakub Dawidek activemap_write_complete(res->hr_amp, 162032115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length); 162132115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 162232115b10SPawel Jakub Dawidek } 162332115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) { 162432115b10SPawel Jakub Dawidek /* 162532115b10SPawel Jakub Dawidek * Unlock range we locked. 162632115b10SPawel Jakub Dawidek */ 162732115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 162832115b10SPawel Jakub Dawidek rangelock_del(range_regular, ggio->gctl_offset, 162932115b10SPawel Jakub Dawidek ggio->gctl_length); 163032115b10SPawel Jakub Dawidek if (range_sync_wait) 163132115b10SPawel Jakub Dawidek cv_signal(&range_sync_cond); 163232115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 163332115b10SPawel Jakub Dawidek /* 163432115b10SPawel Jakub Dawidek * Bump local count if this is first write after 163532115b10SPawel Jakub Dawidek * connection failure with remote node. 163632115b10SPawel Jakub Dawidek */ 163732115b10SPawel Jakub Dawidek ncomp = 1; 163832115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 163932115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) { 164032115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 164132115b10SPawel Jakub Dawidek if (res->hr_primary_localcnt == 164232115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt) { 164332115b10SPawel Jakub Dawidek res->hr_primary_localcnt++; 164432115b10SPawel Jakub Dawidek pjdlog_debug(1, 164532115b10SPawel Jakub Dawidek "Increasing localcnt to %ju.", 164632115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt); 164732115b10SPawel Jakub Dawidek (void)metadata_write(res); 164832115b10SPawel Jakub Dawidek } 164932115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 165032115b10SPawel Jakub Dawidek } 165132115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 165232115b10SPawel Jakub Dawidek } 165332115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) 165432115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed"); 165532115b10SPawel Jakub Dawidek pjdlog_debug(2, 165632115b10SPawel Jakub Dawidek "ggate_send: (%p) Moving request to the free queue.", hio); 165732115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 165832115b10SPawel Jakub Dawidek } 165932115b10SPawel Jakub Dawidek /* NOTREACHED */ 166032115b10SPawel Jakub Dawidek return (NULL); 166132115b10SPawel Jakub Dawidek } 166232115b10SPawel Jakub Dawidek 166332115b10SPawel Jakub Dawidek /* 166432115b10SPawel Jakub Dawidek * Thread synchronize local and remote components. 166532115b10SPawel Jakub Dawidek */ 166632115b10SPawel Jakub Dawidek static void * 166732115b10SPawel Jakub Dawidek sync_thread(void *arg __unused) 166832115b10SPawel Jakub Dawidek { 166932115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 167032115b10SPawel Jakub Dawidek struct hio *hio; 167132115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio; 1672fa356f6cSPawel Jakub Dawidek struct timeval tstart, tend, tdiff; 167332115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps; 167432115b10SPawel Jakub Dawidek off_t offset, length, synced; 167532115b10SPawel Jakub Dawidek bool dorewind; 167632115b10SPawel Jakub Dawidek int syncext; 167732115b10SPawel Jakub Dawidek 167832115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 167932115b10SPawel Jakub Dawidek dorewind = true; 1680b9cf0cf5SPawel Jakub Dawidek synced = 0; 1681b9cf0cf5SPawel Jakub Dawidek offset = -1; 168232115b10SPawel Jakub Dawidek 168332115b10SPawel Jakub Dawidek for (;;) { 168432115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 1685b9cf0cf5SPawel Jakub Dawidek if (offset >= 0 && !sync_inprogress) { 1686fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1687fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1688fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization interrupted after %#.0T. " 1689fa356f6cSPawel Jakub Dawidek "%NB synchronized so far.", &tdiff, 169053d9b386SPawel Jakub Dawidek (intmax_t)synced); 16915bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCINTR); 169253d9b386SPawel Jakub Dawidek } 169332115b10SPawel Jakub Dawidek while (!sync_inprogress) { 169432115b10SPawel Jakub Dawidek dorewind = true; 169532115b10SPawel Jakub Dawidek synced = 0; 169632115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 169732115b10SPawel Jakub Dawidek } 169832115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 169932115b10SPawel Jakub Dawidek /* 170032115b10SPawel Jakub Dawidek * Obtain offset at which we should synchronize. 170132115b10SPawel Jakub Dawidek * Rewind synchronization if needed. 170232115b10SPawel Jakub Dawidek */ 170332115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock); 170432115b10SPawel Jakub Dawidek if (dorewind) 170532115b10SPawel Jakub Dawidek activemap_sync_rewind(res->hr_amp); 170632115b10SPawel Jakub Dawidek offset = activemap_sync_offset(res->hr_amp, &length, &syncext); 170732115b10SPawel Jakub Dawidek if (syncext != -1) { 170832115b10SPawel Jakub Dawidek /* 170932115b10SPawel Jakub Dawidek * We synchronized entire syncext extent, we can mark 171032115b10SPawel Jakub Dawidek * it as clean now. 171132115b10SPawel Jakub Dawidek */ 171232115b10SPawel Jakub Dawidek if (activemap_extent_complete(res->hr_amp, syncext)) 171332115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res); 171432115b10SPawel Jakub Dawidek } 171532115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock); 171632115b10SPawel Jakub Dawidek if (dorewind) { 171732115b10SPawel Jakub Dawidek dorewind = false; 171832115b10SPawel Jakub Dawidek if (offset < 0) 171932115b10SPawel Jakub Dawidek pjdlog_info("Nodes are in sync."); 172032115b10SPawel Jakub Dawidek else { 1721fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization started. %NB to go.", 1722fa356f6cSPawel Jakub Dawidek (intmax_t)(res->hr_extentsize * 172332115b10SPawel Jakub Dawidek activemap_ndirty(res->hr_amp))); 17245bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCSTART); 1725fa356f6cSPawel Jakub Dawidek gettimeofday(&tstart, NULL); 172632115b10SPawel Jakub Dawidek } 172732115b10SPawel Jakub Dawidek } 172832115b10SPawel Jakub Dawidek if (offset < 0) { 172955ce1e7cSPawel Jakub Dawidek sync_stop(); 173032115b10SPawel Jakub Dawidek pjdlog_debug(1, "Nothing to synchronize."); 173132115b10SPawel Jakub Dawidek /* 173232115b10SPawel Jakub Dawidek * Synchronization complete, make both localcnt and 173332115b10SPawel Jakub Dawidek * remotecnt equal. 173432115b10SPawel Jakub Dawidek */ 173532115b10SPawel Jakub Dawidek ncomp = 1; 173632115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 173732115b10SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 173832115b10SPawel Jakub Dawidek if (synced > 0) { 1739fa356f6cSPawel Jakub Dawidek int64_t bps; 1740fa356f6cSPawel Jakub Dawidek 1741fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL); 1742fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff); 1743fa356f6cSPawel Jakub Dawidek bps = (int64_t)((double)synced / 1744fa356f6cSPawel Jakub Dawidek ((double)tdiff.tv_sec + 1745fa356f6cSPawel Jakub Dawidek (double)tdiff.tv_usec / 1000000)); 174632115b10SPawel Jakub Dawidek pjdlog_info("Synchronization complete. " 1747fa356f6cSPawel Jakub Dawidek "%NB synchronized in %#.0lT (%NB/sec).", 1748fa356f6cSPawel Jakub Dawidek (intmax_t)synced, &tdiff, 1749fa356f6cSPawel Jakub Dawidek (intmax_t)bps); 17505bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCDONE); 175132115b10SPawel Jakub Dawidek } 175232115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 175332115b10SPawel Jakub Dawidek res->hr_syncsrc = HAST_SYNCSRC_UNDEF; 175432115b10SPawel Jakub Dawidek res->hr_primary_localcnt = 175532115b10SPawel Jakub Dawidek res->hr_secondary_localcnt; 175632115b10SPawel Jakub Dawidek res->hr_primary_remotecnt = 175732115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt; 175832115b10SPawel Jakub Dawidek pjdlog_debug(1, 175932115b10SPawel Jakub Dawidek "Setting localcnt to %ju and remotecnt to %ju.", 176032115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt, 176132115b10SPawel Jakub Dawidek (uintmax_t)res->hr_secondary_localcnt); 176232115b10SPawel Jakub Dawidek (void)metadata_write(res); 176332115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 176432115b10SPawel Jakub Dawidek } 176532115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 176632115b10SPawel Jakub Dawidek continue; 176732115b10SPawel Jakub Dawidek } 176832115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: Taking free request."); 176932115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free); 177032115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Got free request.", hio); 177132115b10SPawel Jakub Dawidek /* 177232115b10SPawel Jakub Dawidek * Lock the range we are going to synchronize. We don't want 177332115b10SPawel Jakub Dawidek * race where someone writes between our read and write. 177432115b10SPawel Jakub Dawidek */ 177532115b10SPawel Jakub Dawidek for (;;) { 177632115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 177732115b10SPawel Jakub Dawidek if (rangelock_islocked(range_regular, offset, length)) { 177832115b10SPawel Jakub Dawidek pjdlog_debug(2, 177932115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd locked.", 178032115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 178132115b10SPawel Jakub Dawidek range_sync_wait = true; 178232115b10SPawel Jakub Dawidek cv_wait(&range_sync_cond, &range_lock); 178332115b10SPawel Jakub Dawidek range_sync_wait = false; 178432115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 178532115b10SPawel Jakub Dawidek continue; 178632115b10SPawel Jakub Dawidek } 178732115b10SPawel Jakub Dawidek if (rangelock_add(range_sync, offset, length) < 0) { 178832115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 178932115b10SPawel Jakub Dawidek pjdlog_debug(2, 179032115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd is already locked, waiting.", 179132115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length); 179232115b10SPawel Jakub Dawidek sleep(1); 179332115b10SPawel Jakub Dawidek continue; 179432115b10SPawel Jakub Dawidek } 179532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 179632115b10SPawel Jakub Dawidek break; 179732115b10SPawel Jakub Dawidek } 179832115b10SPawel Jakub Dawidek /* 179932115b10SPawel Jakub Dawidek * First read the data from synchronization source. 180032115b10SPawel Jakub Dawidek */ 180132115b10SPawel Jakub Dawidek SYNCREQ(hio); 180232115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio; 180332115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_READ; 180432115b10SPawel Jakub Dawidek ggio->gctl_offset = offset; 180532115b10SPawel Jakub Dawidek ggio->gctl_length = length; 180632115b10SPawel Jakub Dawidek ggio->gctl_error = 0; 180732115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 180832115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 180932115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 181032115b10SPawel Jakub Dawidek hio); 181132115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 181232115b10SPawel Jakub Dawidek hio); 181332115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 181432115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 181532115b10SPawel Jakub Dawidek /* 181632115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 181732115b10SPawel Jakub Dawidek * so handle request locally. 181832115b10SPawel Jakub Dawidek */ 181932115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 182032115b10SPawel Jakub Dawidek ncomp = 0; 182132115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 18222ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 182332115b10SPawel Jakub Dawidek /* 182432115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 182532115b10SPawel Jakub Dawidek * so send request to the remote node. 182632115b10SPawel Jakub Dawidek */ 182732115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 182832115b10SPawel Jakub Dawidek ncomp = 1; 182932115b10SPawel Jakub Dawidek } 183032115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 183132115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 183232115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 183332115b10SPawel Jakub Dawidek 183432115b10SPawel Jakub Dawidek /* 183532115b10SPawel Jakub Dawidek * Let's wait for READ to finish. 183632115b10SPawel Jakub Dawidek */ 183732115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 183832115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 183932115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 184032115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 184132115b10SPawel Jakub Dawidek 184232115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 184332115b10SPawel Jakub Dawidek pjdlog_error("Unable to read synchronization data: %s.", 184432115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 184532115b10SPawel Jakub Dawidek goto free_queue; 184632115b10SPawel Jakub Dawidek } 184732115b10SPawel Jakub Dawidek 184832115b10SPawel Jakub Dawidek /* 184932115b10SPawel Jakub Dawidek * We read the data from synchronization source, now write it 185032115b10SPawel Jakub Dawidek * to synchronization target. 185132115b10SPawel Jakub Dawidek */ 185232115b10SPawel Jakub Dawidek SYNCREQ(hio); 185332115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_WRITE; 185432115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 185532115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL; 185632115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ", 185732115b10SPawel Jakub Dawidek hio); 185832115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.", 185932115b10SPawel Jakub Dawidek hio); 186032115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock); 186132115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) { 186232115b10SPawel Jakub Dawidek /* 186332115b10SPawel Jakub Dawidek * This range is up-to-date on local component, 186432115b10SPawel Jakub Dawidek * so we update remote component. 186532115b10SPawel Jakub Dawidek */ 186632115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */ 186732115b10SPawel Jakub Dawidek ncomp = 1; 186832115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ { 18692ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY); 187032115b10SPawel Jakub Dawidek /* 187132115b10SPawel Jakub Dawidek * This range is out-of-date on local component, 187232115b10SPawel Jakub Dawidek * so we update it. 187332115b10SPawel Jakub Dawidek */ 187432115b10SPawel Jakub Dawidek /* Local component is 0 for now. */ 187532115b10SPawel Jakub Dawidek ncomp = 0; 187632115b10SPawel Jakub Dawidek } 187732115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock); 187832115b10SPawel Jakub Dawidek 187932115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queues.", 188032115b10SPawel Jakub Dawidek hio); 188132115b10SPawel Jakub Dawidek refcount_init(&hio->hio_countdown, 1); 188232115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp); 188332115b10SPawel Jakub Dawidek 188432115b10SPawel Jakub Dawidek /* 188532115b10SPawel Jakub Dawidek * Let's wait for WRITE to finish. 188632115b10SPawel Jakub Dawidek */ 188732115b10SPawel Jakub Dawidek mtx_lock(&sync_lock); 188832115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio)) 188932115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock); 189032115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock); 189132115b10SPawel Jakub Dawidek 189232115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) { 189332115b10SPawel Jakub Dawidek pjdlog_error("Unable to write synchronization data: %s.", 189432115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp])); 189532115b10SPawel Jakub Dawidek goto free_queue; 189632115b10SPawel Jakub Dawidek } 1897e23d2d01SPawel Jakub Dawidek 1898e23d2d01SPawel Jakub Dawidek synced += length; 189932115b10SPawel Jakub Dawidek free_queue: 190032115b10SPawel Jakub Dawidek mtx_lock(&range_lock); 190132115b10SPawel Jakub Dawidek rangelock_del(range_sync, offset, length); 190232115b10SPawel Jakub Dawidek if (range_regular_wait) 190332115b10SPawel Jakub Dawidek cv_signal(&range_regular_cond); 190432115b10SPawel Jakub Dawidek mtx_unlock(&range_lock); 190532115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the free queue.", 190632115b10SPawel Jakub Dawidek hio); 190732115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free); 190832115b10SPawel Jakub Dawidek } 190932115b10SPawel Jakub Dawidek /* NOTREACHED */ 191032115b10SPawel Jakub Dawidek return (NULL); 191132115b10SPawel Jakub Dawidek } 191232115b10SPawel Jakub Dawidek 1913115f4e5cSPawel Jakub Dawidek void 1914115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv) 19150989854dSPawel Jakub Dawidek { 19160989854dSPawel Jakub Dawidek unsigned int ii, ncomps; 1917115f4e5cSPawel Jakub Dawidek int modified, vint; 1918115f4e5cSPawel Jakub Dawidek const char *vstr; 19190989854dSPawel Jakub Dawidek 19200989854dSPawel Jakub Dawidek pjdlog_info("Reloading configuration..."); 19210989854dSPawel Jakub Dawidek 19222ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 19232ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(gres == res); 1924115f4e5cSPawel Jakub Dawidek nv_assert(nv, "remoteaddr"); 1925115f4e5cSPawel Jakub Dawidek nv_assert(nv, "replication"); 19261fee97b0SPawel Jakub Dawidek nv_assert(nv, "checksum"); 19278cd3d45aSPawel Jakub Dawidek nv_assert(nv, "compression"); 1928115f4e5cSPawel Jakub Dawidek nv_assert(nv, "timeout"); 1929115f4e5cSPawel Jakub Dawidek nv_assert(nv, "exec"); 1930115f4e5cSPawel Jakub Dawidek 19310989854dSPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 19320989854dSPawel Jakub Dawidek 19331fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR 0x01 19341fee97b0SPawel Jakub Dawidek #define MODIFIED_REPLICATION 0x02 19351fee97b0SPawel Jakub Dawidek #define MODIFIED_CHECKSUM 0x04 19368cd3d45aSPawel Jakub Dawidek #define MODIFIED_COMPRESSION 0x08 19371fee97b0SPawel Jakub Dawidek #define MODIFIED_TIMEOUT 0x10 19381fee97b0SPawel Jakub Dawidek #define MODIFIED_EXEC 0x20 19390989854dSPawel Jakub Dawidek modified = 0; 1940115f4e5cSPawel Jakub Dawidek 1941115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 1942115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_remoteaddr, vstr) != 0) { 19430989854dSPawel Jakub Dawidek /* 19440989854dSPawel Jakub Dawidek * Don't copy res->hr_remoteaddr to gres just yet. 19450989854dSPawel Jakub Dawidek * We want remote_close() to log disconnect from the old 19460989854dSPawel Jakub Dawidek * addresses, not from the new ones. 19470989854dSPawel Jakub Dawidek */ 19480989854dSPawel Jakub Dawidek modified |= MODIFIED_REMOTEADDR; 19490989854dSPawel Jakub Dawidek } 1950115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "replication"); 1951115f4e5cSPawel Jakub Dawidek if (gres->hr_replication != vint) { 1952115f4e5cSPawel Jakub Dawidek gres->hr_replication = vint; 19530989854dSPawel Jakub Dawidek modified |= MODIFIED_REPLICATION; 19540989854dSPawel Jakub Dawidek } 19551fee97b0SPawel Jakub Dawidek vint = nv_get_int32(nv, "checksum"); 19561fee97b0SPawel Jakub Dawidek if (gres->hr_checksum != vint) { 19571fee97b0SPawel Jakub Dawidek gres->hr_checksum = vint; 19581fee97b0SPawel Jakub Dawidek modified |= MODIFIED_CHECKSUM; 19591fee97b0SPawel Jakub Dawidek } 19608cd3d45aSPawel Jakub Dawidek vint = nv_get_int32(nv, "compression"); 19618cd3d45aSPawel Jakub Dawidek if (gres->hr_compression != vint) { 19628cd3d45aSPawel Jakub Dawidek gres->hr_compression = vint; 19638cd3d45aSPawel Jakub Dawidek modified |= MODIFIED_COMPRESSION; 19648cd3d45aSPawel Jakub Dawidek } 1965115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "timeout"); 1966115f4e5cSPawel Jakub Dawidek if (gres->hr_timeout != vint) { 1967115f4e5cSPawel Jakub Dawidek gres->hr_timeout = vint; 19680989854dSPawel Jakub Dawidek modified |= MODIFIED_TIMEOUT; 19690989854dSPawel Jakub Dawidek } 1970115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "exec"); 1971115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_exec, vstr) != 0) { 1972115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec)); 19730becad39SPawel Jakub Dawidek modified |= MODIFIED_EXEC; 19740becad39SPawel Jakub Dawidek } 1975115f4e5cSPawel Jakub Dawidek 19760989854dSPawel Jakub Dawidek /* 19771fee97b0SPawel Jakub Dawidek * Change timeout for connected sockets. 19781fee97b0SPawel Jakub Dawidek * Don't bother if we need to reconnect. 19790989854dSPawel Jakub Dawidek */ 19801fee97b0SPawel Jakub Dawidek if ((modified & MODIFIED_TIMEOUT) != 0 && 19811fee97b0SPawel Jakub Dawidek (modified & (MODIFIED_REMOTEADDR | MODIFIED_REPLICATION)) == 0) { 19820989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 19830989854dSPawel Jakub Dawidek if (!ISREMOTE(ii)) 19840989854dSPawel Jakub Dawidek continue; 19850989854dSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ii]); 19860989854dSPawel Jakub Dawidek if (!ISCONNECTED(gres, ii)) { 19870989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 19880989854dSPawel Jakub Dawidek continue; 19890989854dSPawel Jakub Dawidek } 19900989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]); 19910989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remotein, 19920989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 19930989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 19940989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 19950989854dSPawel Jakub Dawidek } 19960989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remoteout, 19970989854dSPawel Jakub Dawidek gres->hr_timeout) < 0) { 19980989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, 19990989854dSPawel Jakub Dawidek "Unable to set connection timeout"); 20000989854dSPawel Jakub Dawidek } 20010989854dSPawel Jakub Dawidek } 20021fee97b0SPawel Jakub Dawidek } 20031fee97b0SPawel Jakub Dawidek if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_REPLICATION)) != 0) { 20040989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) { 20050989854dSPawel Jakub Dawidek if (!ISREMOTE(ii)) 20060989854dSPawel Jakub Dawidek continue; 20070989854dSPawel Jakub Dawidek remote_close(gres, ii); 20080989854dSPawel Jakub Dawidek } 20090989854dSPawel Jakub Dawidek if (modified & MODIFIED_REMOTEADDR) { 2010115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr"); 2011115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_remoteaddr, vstr, 20120989854dSPawel Jakub Dawidek sizeof(gres->hr_remoteaddr)); 20130989854dSPawel Jakub Dawidek } 20140989854dSPawel Jakub Dawidek } 20150989854dSPawel Jakub Dawidek #undef MODIFIED_REMOTEADDR 20160989854dSPawel Jakub Dawidek #undef MODIFIED_REPLICATION 20171fee97b0SPawel Jakub Dawidek #undef MODIFIED_CHECKSUM 20188cd3d45aSPawel Jakub Dawidek #undef MODIFIED_COMPRESSION 20190989854dSPawel Jakub Dawidek #undef MODIFIED_TIMEOUT 20200becad39SPawel Jakub Dawidek #undef MODIFIED_EXEC 20210989854dSPawel Jakub Dawidek 20220989854dSPawel Jakub Dawidek pjdlog_info("Configuration reloaded successfully."); 20230989854dSPawel Jakub Dawidek } 20240989854dSPawel Jakub Dawidek 2025f7fe83f9SPawel Jakub Dawidek static void 2026ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp) 2027ff6bb1f8SPawel Jakub Dawidek { 2028ff6bb1f8SPawel Jakub Dawidek struct proto_conn *in, *out; 2029ff6bb1f8SPawel Jakub Dawidek 2030ff6bb1f8SPawel Jakub Dawidek if (!ISREMOTE(ncomp)) 2031ff6bb1f8SPawel Jakub Dawidek return; 2032ff6bb1f8SPawel Jakub Dawidek 2033ff6bb1f8SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]); 2034ff6bb1f8SPawel Jakub Dawidek 2035ff6bb1f8SPawel Jakub Dawidek if (!real_remote(res)) { 2036ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2037ff6bb1f8SPawel Jakub Dawidek return; 2038ff6bb1f8SPawel Jakub Dawidek } 2039ff6bb1f8SPawel Jakub Dawidek 2040ff6bb1f8SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) { 20412ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL); 20422ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL); 2043ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2044ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Connection to %s is ok.", 2045ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2046ff6bb1f8SPawel Jakub Dawidek return; 2047ff6bb1f8SPawel Jakub Dawidek } 2048ff6bb1f8SPawel Jakub Dawidek 20492ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 20502ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 2051ff6bb1f8SPawel Jakub Dawidek /* 2052ff6bb1f8SPawel Jakub Dawidek * Upgrade the lock. It doesn't have to be atomic as no other thread 2053ff6bb1f8SPawel Jakub Dawidek * can change connection status from disconnected to connected. 2054ff6bb1f8SPawel Jakub Dawidek */ 2055ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2056ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnecting to %s.", 2057ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2058ff6bb1f8SPawel Jakub Dawidek in = out = NULL; 2059ff6bb1f8SPawel Jakub Dawidek if (init_remote(res, &in, &out)) { 2060ff6bb1f8SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]); 20612ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 20622ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 20632ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in != NULL && out != NULL); 2064ff6bb1f8SPawel Jakub Dawidek res->hr_remotein = in; 2065ff6bb1f8SPawel Jakub Dawidek res->hr_remoteout = out; 2066ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]); 2067ff6bb1f8SPawel Jakub Dawidek pjdlog_info("Successfully reconnected to %s.", 2068ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2069ff6bb1f8SPawel Jakub Dawidek sync_start(); 2070ff6bb1f8SPawel Jakub Dawidek } else { 2071ff6bb1f8SPawel Jakub Dawidek /* Both connections should be NULL. */ 20722ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL); 20732ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL); 20742ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in == NULL && out == NULL); 2075ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", 2076ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr); 2077ff6bb1f8SPawel Jakub Dawidek } 2078ff6bb1f8SPawel Jakub Dawidek } 2079ff6bb1f8SPawel Jakub Dawidek 208032115b10SPawel Jakub Dawidek /* 208132115b10SPawel Jakub Dawidek * Thread guards remote connections and reconnects when needed, handles 208232115b10SPawel Jakub Dawidek * signals, etc. 208332115b10SPawel Jakub Dawidek */ 208432115b10SPawel Jakub Dawidek static void * 208532115b10SPawel Jakub Dawidek guard_thread(void *arg) 208632115b10SPawel Jakub Dawidek { 208732115b10SPawel Jakub Dawidek struct hast_resource *res = arg; 208832115b10SPawel Jakub Dawidek unsigned int ii, ncomps; 20896d0c801eSPawel Jakub Dawidek struct timespec timeout; 2090ff6bb1f8SPawel Jakub Dawidek time_t lastcheck, now; 20916d0c801eSPawel Jakub Dawidek sigset_t mask; 20926d0c801eSPawel Jakub Dawidek int signo; 209332115b10SPawel Jakub Dawidek 209432115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS; 2095ff6bb1f8SPawel Jakub Dawidek lastcheck = time(NULL); 209632115b10SPawel Jakub Dawidek 20976d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigemptyset(&mask) == 0); 20986d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 20996d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 21006d0c801eSPawel Jakub Dawidek 2101d448536cSPawel Jakub Dawidek timeout.tv_sec = RETRY_SLEEP; 21026d0c801eSPawel Jakub Dawidek timeout.tv_nsec = 0; 21036d0c801eSPawel Jakub Dawidek signo = -1; 21046d0c801eSPawel Jakub Dawidek 210532115b10SPawel Jakub Dawidek for (;;) { 21066d0c801eSPawel Jakub Dawidek switch (signo) { 21076d0c801eSPawel Jakub Dawidek case SIGINT: 21086d0c801eSPawel Jakub Dawidek case SIGTERM: 21096d0c801eSPawel Jakub Dawidek sigexit_received = true; 211032115b10SPawel Jakub Dawidek primary_exitx(EX_OK, 211132115b10SPawel Jakub Dawidek "Termination signal received, exiting."); 21126d0c801eSPawel Jakub Dawidek break; 21136d0c801eSPawel Jakub Dawidek default: 2114ff6bb1f8SPawel Jakub Dawidek break; 2115f7fe83f9SPawel Jakub Dawidek } 21166d0c801eSPawel Jakub Dawidek 21176d0c801eSPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Checking connections."); 2118ff6bb1f8SPawel Jakub Dawidek now = time(NULL); 21196d0c801eSPawel Jakub Dawidek if (lastcheck + RETRY_SLEEP <= now) { 21206d0c801eSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) 2121ff6bb1f8SPawel Jakub Dawidek guard_one(res, ii); 2122ff6bb1f8SPawel Jakub Dawidek lastcheck = now; 212332115b10SPawel Jakub Dawidek } 21246d0c801eSPawel Jakub Dawidek signo = sigtimedwait(&mask, NULL, &timeout); 212532115b10SPawel Jakub Dawidek } 212632115b10SPawel Jakub Dawidek /* NOTREACHED */ 212732115b10SPawel Jakub Dawidek return (NULL); 212832115b10SPawel Jakub Dawidek } 2129