2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 The FreeBSD Foundation
|
2011-03-06 22:56:14 +00:00
|
|
|
* Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
2010-02-18 23:16:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <geom/gate/g_gate.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgeom.h>
|
|
|
|
#include <pthread.h>
|
2010-08-30 00:06:05 +00:00
|
|
|
#include <signal.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <activemap.h>
|
|
|
|
#include <nv.h>
|
|
|
|
#include <rangelock.h>
|
|
|
|
|
|
|
|
#include "control.h"
|
2010-08-30 23:26:10 +00:00
|
|
|
#include "event.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "hast.h"
|
|
|
|
#include "hast_proto.h"
|
|
|
|
#include "hastd.h"
|
2010-08-27 15:16:52 +00:00
|
|
|
#include "hooks.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "metadata.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "pjdlog.h"
|
2013-02-17 21:12:34 +00:00
|
|
|
#include "refcnt.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "subr.h"
|
|
|
|
#include "synch.h"
|
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
/* The is only one remote component for now. */
|
|
|
|
#define ISREMOTE(no) ((no) == 1)
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
struct hio {
|
|
|
|
/*
|
|
|
|
* Number of components we are still waiting for.
|
|
|
|
* When this field goes to 0, we can send the request back to the
|
|
|
|
* kernel. Each component has to decrease this counter by one
|
|
|
|
* even on failure.
|
|
|
|
*/
|
2013-04-27 05:01:29 +00:00
|
|
|
refcnt_t hio_countdown;
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Each component has a place to store its own error.
|
|
|
|
* Once the request is handled by all components we can decide if the
|
|
|
|
* request overall is successful or not.
|
|
|
|
*/
|
|
|
|
int *hio_errors;
|
|
|
|
/*
|
2011-03-21 08:54:59 +00:00
|
|
|
* Structure used to communicate with GEOM Gate class.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
struct g_gate_ctl_io hio_ggio;
|
2011-10-27 20:32:57 +00:00
|
|
|
/*
|
|
|
|
* Request was already confirmed to GEOM Gate.
|
|
|
|
*/
|
|
|
|
bool hio_done;
|
2013-12-10 19:56:26 +00:00
|
|
|
/*
|
|
|
|
* Number of components we are still waiting before sending write
|
|
|
|
* completion ack to GEOM Gate. Used for memsync.
|
|
|
|
*/
|
|
|
|
refcnt_t hio_writecount;
|
|
|
|
/*
|
|
|
|
* Memsync request was acknowleged by remote.
|
|
|
|
*/
|
|
|
|
bool hio_memsyncacked;
|
2011-10-27 20:32:57 +00:00
|
|
|
/*
|
|
|
|
* Remember replication from the time the request was initiated,
|
|
|
|
* so we won't get confused when replication changes on reload.
|
|
|
|
*/
|
|
|
|
int hio_replication;
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_ENTRY(hio) *hio_next;
|
|
|
|
};
|
|
|
|
#define hio_free_next hio_next[0]
|
|
|
|
#define hio_done_next hio_next[0]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free list holds unused structures. When free list is empty, we have to wait
|
|
|
|
* until some in-progress requests are freed.
|
|
|
|
*/
|
|
|
|
static TAILQ_HEAD(, hio) hio_free_list;
|
2013-10-26 08:38:21 +00:00
|
|
|
static size_t hio_free_list_size;
|
2010-02-18 23:16:19 +00:00
|
|
|
static pthread_mutex_t hio_free_list_lock;
|
|
|
|
static pthread_cond_t hio_free_list_cond;
|
|
|
|
/*
|
|
|
|
* There is one send list for every component. One requests is placed on all
|
|
|
|
* send lists - each component gets the same request, but each component is
|
|
|
|
* responsible for managing his own send list.
|
|
|
|
*/
|
|
|
|
static TAILQ_HEAD(, hio) *hio_send_list;
|
2013-10-26 08:38:21 +00:00
|
|
|
static size_t *hio_send_list_size;
|
2010-02-18 23:16:19 +00:00
|
|
|
static pthread_mutex_t *hio_send_list_lock;
|
|
|
|
static pthread_cond_t *hio_send_list_cond;
|
2013-10-26 08:38:21 +00:00
|
|
|
#define hio_send_local_list_size hio_send_list_size[0]
|
|
|
|
#define hio_send_remote_list_size hio_send_list_size[1]
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* There is one recv list for every component, although local components don't
|
|
|
|
* use recv lists as local requests are done synchronously.
|
|
|
|
*/
|
|
|
|
static TAILQ_HEAD(, hio) *hio_recv_list;
|
2013-10-26 08:38:21 +00:00
|
|
|
static size_t *hio_recv_list_size;
|
2010-02-18 23:16:19 +00:00
|
|
|
static pthread_mutex_t *hio_recv_list_lock;
|
|
|
|
static pthread_cond_t *hio_recv_list_cond;
|
2013-10-26 08:38:21 +00:00
|
|
|
#define hio_recv_remote_list_size hio_recv_list_size[1]
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Request is placed on done list by the slowest component (the one that
|
|
|
|
* decreased hio_countdown from 1 to 0).
|
|
|
|
*/
|
|
|
|
static TAILQ_HEAD(, hio) hio_done_list;
|
2013-10-26 08:38:21 +00:00
|
|
|
static size_t hio_done_list_size;
|
2010-02-18 23:16:19 +00:00
|
|
|
static pthread_mutex_t hio_done_list_lock;
|
|
|
|
static pthread_cond_t hio_done_list_cond;
|
|
|
|
/*
|
|
|
|
* Structure below are for interaction with sync thread.
|
|
|
|
*/
|
|
|
|
static bool sync_inprogress;
|
|
|
|
static pthread_mutex_t sync_lock;
|
|
|
|
static pthread_cond_t sync_cond;
|
|
|
|
/*
|
|
|
|
* The lock below allows to synchornize access to remote connections.
|
|
|
|
*/
|
|
|
|
static pthread_rwlock_t *hio_remote_lock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock to synchronize metadata updates. Also synchronize access to
|
|
|
|
* hr_primary_localcnt and hr_primary_remotecnt fields.
|
|
|
|
*/
|
|
|
|
static pthread_mutex_t metadata_lock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum number of outstanding I/O requests.
|
|
|
|
*/
|
|
|
|
#define HAST_HIO_MAX 256
|
|
|
|
/*
|
|
|
|
* Number of components. At this point there are only two components: local
|
|
|
|
* and remote, but in the future it might be possible to use multiple local
|
|
|
|
* and remote components.
|
|
|
|
*/
|
|
|
|
#define HAST_NCOMPONENTS 2
|
|
|
|
|
|
|
|
#define ISCONNECTED(res, no) \
|
|
|
|
((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
|
|
|
|
|
|
|
|
#define QUEUE_INSERT1(hio, name, ncomp) do { \
|
|
|
|
mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
|
2013-12-10 20:06:41 +00:00
|
|
|
if (TAILQ_EMPTY(&hio_##name##_list[(ncomp)])) \
|
|
|
|
cv_broadcast(&hio_##name##_list_cond[(ncomp)]); \
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \
|
|
|
|
hio_next[(ncomp)]); \
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_##name##_list_size[(ncomp)]++; \
|
2013-12-10 20:06:41 +00:00
|
|
|
mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
|
2010-02-18 23:16:19 +00:00
|
|
|
} while (0)
|
|
|
|
#define QUEUE_INSERT2(hio, name) do { \
|
|
|
|
mtx_lock(&hio_##name##_list_lock); \
|
2013-12-10 20:06:41 +00:00
|
|
|
if (TAILQ_EMPTY(&hio_##name##_list)) \
|
|
|
|
cv_broadcast(&hio_##name##_list_cond); \
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_##name##_list_size++; \
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_unlock(&hio_##name##_list_lock); \
|
|
|
|
} while (0)
|
2010-11-02 22:13:08 +00:00
|
|
|
#define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \
|
|
|
|
bool _last; \
|
|
|
|
\
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
|
2010-11-02 22:13:08 +00:00
|
|
|
_last = false; \
|
|
|
|
while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
|
|
|
|
cv_timedwait(&hio_##name##_list_cond[(ncomp)], \
|
|
|
|
&hio_##name##_list_lock[(ncomp)], (timeout)); \
|
2011-03-22 10:39:34 +00:00
|
|
|
if ((timeout) != 0) \
|
2010-11-02 22:13:08 +00:00
|
|
|
_last = true; \
|
|
|
|
} \
|
|
|
|
if (hio != NULL) { \
|
2013-10-26 08:38:21 +00:00
|
|
|
PJDLOG_ASSERT(hio_##name##_list_size[(ncomp)] != 0); \
|
|
|
|
hio_##name##_list_size[(ncomp)]--; \
|
2010-11-02 22:13:08 +00:00
|
|
|
TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \
|
|
|
|
hio_next[(ncomp)]); \
|
2010-02-18 23:16:19 +00:00
|
|
|
} \
|
|
|
|
mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
|
|
|
|
} while (0)
|
|
|
|
#define QUEUE_TAKE2(hio, name) do { \
|
|
|
|
mtx_lock(&hio_##name##_list_lock); \
|
|
|
|
while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
|
|
|
|
cv_wait(&hio_##name##_list_cond, \
|
|
|
|
&hio_##name##_list_lock); \
|
|
|
|
} \
|
2013-10-26 08:38:21 +00:00
|
|
|
PJDLOG_ASSERT(hio_##name##_list_size != 0); \
|
|
|
|
hio_##name##_list_size--; \
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \
|
|
|
|
mtx_unlock(&hio_##name##_list_lock); \
|
|
|
|
} while (0)
|
|
|
|
|
2013-12-10 19:58:10 +00:00
|
|
|
#define ISFULLSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_FULLSYNC)
|
|
|
|
#define ISMEMSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_MEMSYNC)
|
|
|
|
#define ISASYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_ASYNC)
|
|
|
|
|
2010-06-14 21:44:20 +00:00
|
|
|
#define SYNCREQ(hio) do { \
|
|
|
|
(hio)->hio_ggio.gctl_unit = -1; \
|
|
|
|
(hio)->hio_ggio.gctl_seq = 1; \
|
|
|
|
} while (0)
|
2010-02-18 23:16:19 +00:00
|
|
|
#define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1)
|
|
|
|
#define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
|
|
|
|
#define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2)
|
2013-12-10 19:58:10 +00:00
|
|
|
|
|
|
|
#define ISMEMSYNCWRITE(hio) (ISMEMSYNC(hio) && \
|
|
|
|
(hio)->hio_ggio.gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio))
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
static struct hast_resource *gres;
|
|
|
|
|
|
|
|
static pthread_mutex_t range_lock;
|
|
|
|
static struct rangelocks *range_regular;
|
|
|
|
static bool range_regular_wait;
|
|
|
|
static pthread_cond_t range_regular_cond;
|
|
|
|
static struct rangelocks *range_sync;
|
|
|
|
static bool range_sync_wait;
|
|
|
|
static pthread_cond_t range_sync_cond;
|
2011-04-20 18:43:28 +00:00
|
|
|
static bool fullystarted;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
static void *ggate_recv_thread(void *arg);
|
|
|
|
static void *local_send_thread(void *arg);
|
|
|
|
static void *remote_send_thread(void *arg);
|
|
|
|
static void *remote_recv_thread(void *arg);
|
|
|
|
static void *ggate_send_thread(void *arg);
|
|
|
|
static void *sync_thread(void *arg);
|
|
|
|
static void *guard_thread(void *arg);
|
|
|
|
|
2013-10-26 08:38:21 +00:00
|
|
|
static void
|
|
|
|
output_status_aux(struct nv *nvout)
|
|
|
|
{
|
|
|
|
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_free_list_size,
|
|
|
|
"idle_queue_size");
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_send_local_list_size,
|
|
|
|
"local_queue_size");
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_send_remote_list_size,
|
|
|
|
"send_queue_size");
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_recv_remote_list_size,
|
|
|
|
"recv_queue_size");
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_done_list_size,
|
|
|
|
"done_queue_size");
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
cleanup(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
int rerrno;
|
|
|
|
|
|
|
|
/* Remember errno. */
|
|
|
|
rerrno = errno;
|
|
|
|
|
|
|
|
/* Destroy ggate provider if we created one. */
|
|
|
|
if (res->hr_ggateunit >= 0) {
|
|
|
|
struct g_gate_ctl_destroy ggiod;
|
|
|
|
|
2010-10-07 18:23:28 +00:00
|
|
|
bzero(&ggiod, sizeof(ggiod));
|
2010-02-18 23:16:19 +00:00
|
|
|
ggiod.gctl_version = G_GATE_VERSION;
|
|
|
|
ggiod.gctl_unit = res->hr_ggateunit;
|
|
|
|
ggiod.gctl_force = 1;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
|
2010-10-07 18:20:16 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to destroy hast/%s device",
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_provname);
|
|
|
|
}
|
|
|
|
res->hr_ggateunit = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno. */
|
|
|
|
errno = rerrno;
|
|
|
|
}
|
|
|
|
|
2010-09-20 13:23:43 +00:00
|
|
|
static __dead2 void
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exit(int exitcode, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(exitcode != EX_OK);
|
2010-02-18 23:16:19 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
pjdlogv_errno(LOG_ERR, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
cleanup(gres);
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2010-09-20 13:23:43 +00:00
|
|
|
static __dead2 void
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exitx(int exitcode, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
cleanup(gres);
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:19:08 +00:00
|
|
|
/* Expects res->hr_amp locked, returns unlocked. */
|
2010-02-18 23:16:19 +00:00
|
|
|
static int
|
|
|
|
hast_activemap_flush(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
const unsigned char *buf;
|
|
|
|
size_t size;
|
2013-09-19 20:19:08 +00:00
|
|
|
int ret;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2013-09-19 20:19:08 +00:00
|
|
|
mtx_lock(&res->hr_amp_diskmap_lock);
|
2010-02-18 23:16:19 +00:00
|
|
|
buf = activemap_bitmap(res->hr_amp, &size);
|
2013-09-19 20:19:08 +00:00
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(buf != NULL);
|
|
|
|
PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
|
2013-09-19 20:19:08 +00:00
|
|
|
ret = 0;
|
2010-02-18 23:16:19 +00:00
|
|
|
if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
|
|
|
|
(ssize_t)size) {
|
2011-09-27 08:26:09 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
|
2013-02-25 20:09:07 +00:00
|
|
|
res->hr_stat_activemap_write_error++;
|
2013-09-19 20:19:08 +00:00
|
|
|
ret = -1;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-09-19 20:19:08 +00:00
|
|
|
if (ret == 0 && res->hr_metaflush == 1 &&
|
|
|
|
g_flush(res->hr_localfd) == -1) {
|
2011-09-28 13:08:51 +00:00
|
|
|
if (errno == EOPNOTSUPP) {
|
|
|
|
pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
|
|
|
|
res->hr_localpath);
|
|
|
|
res->hr_metaflush = 0;
|
|
|
|
} else {
|
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to flush disk cache on activemap update");
|
2013-02-25 20:09:07 +00:00
|
|
|
res->hr_stat_activemap_flush_error++;
|
2013-09-19 20:19:08 +00:00
|
|
|
ret = -1;
|
2011-09-28 13:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-19 20:19:08 +00:00
|
|
|
mtx_unlock(&res->hr_amp_diskmap_lock);
|
|
|
|
return (ret);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 19:01:57 +00:00
|
|
|
static bool
|
|
|
|
real_remote(const struct hast_resource *res)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (strcmp(res->hr_remoteaddr, "none") != 0);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
init_environment(struct hast_resource *res __unused)
|
|
|
|
{
|
|
|
|
struct hio *hio;
|
|
|
|
unsigned int ii, ncomps;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the future it might be per-resource value.
|
|
|
|
*/
|
|
|
|
ncomps = HAST_NCOMPONENTS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate memory needed by lists.
|
|
|
|
*/
|
|
|
|
hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
|
|
|
|
if (hio_send_list == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for send lists.",
|
|
|
|
sizeof(hio_send_list[0]) * ncomps);
|
|
|
|
}
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_send_list_size = malloc(sizeof(hio_send_list_size[0]) * ncomps);
|
|
|
|
if (hio_send_list_size == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for send list counters.",
|
|
|
|
sizeof(hio_send_list_size[0]) * ncomps);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
|
|
|
|
if (hio_send_list_lock == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for send list locks.",
|
|
|
|
sizeof(hio_send_list_lock[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
|
|
|
|
if (hio_send_list_cond == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for send list condition variables.",
|
|
|
|
sizeof(hio_send_list_cond[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
|
|
|
|
if (hio_recv_list == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for recv lists.",
|
|
|
|
sizeof(hio_recv_list[0]) * ncomps);
|
|
|
|
}
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size = malloc(sizeof(hio_recv_list_size[0]) * ncomps);
|
|
|
|
if (hio_recv_list_size == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for recv list counters.",
|
|
|
|
sizeof(hio_recv_list_size[0]) * ncomps);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
|
|
|
|
if (hio_recv_list_lock == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for recv list locks.",
|
|
|
|
sizeof(hio_recv_list_lock[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
|
|
|
|
if (hio_recv_list_cond == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for recv list condition variables.",
|
|
|
|
sizeof(hio_recv_list_cond[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
|
|
|
|
if (hio_remote_lock == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for remote connections locks.",
|
|
|
|
sizeof(hio_remote_lock[0]) * ncomps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-26 08:38:21 +00:00
|
|
|
* Initialize lists, their counters, locks and condition variables.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
TAILQ_INIT(&hio_free_list);
|
|
|
|
mtx_init(&hio_free_list_lock);
|
|
|
|
cv_init(&hio_free_list_cond);
|
|
|
|
for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
|
|
|
|
TAILQ_INIT(&hio_send_list[ii]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_send_list_size[ii] = 0;
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_init(&hio_send_list_lock[ii]);
|
|
|
|
cv_init(&hio_send_list_cond[ii]);
|
|
|
|
TAILQ_INIT(&hio_recv_list[ii]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size[ii] = 0;
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_init(&hio_recv_list_lock[ii]);
|
|
|
|
cv_init(&hio_recv_list_cond[ii]);
|
|
|
|
rw_init(&hio_remote_lock[ii]);
|
|
|
|
}
|
|
|
|
TAILQ_INIT(&hio_done_list);
|
|
|
|
mtx_init(&hio_done_list_lock);
|
|
|
|
cv_init(&hio_done_list_cond);
|
|
|
|
mtx_init(&metadata_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate requests pool and initialize requests.
|
|
|
|
*/
|
|
|
|
for (ii = 0; ii < HAST_HIO_MAX; ii++) {
|
|
|
|
hio = malloc(sizeof(*hio));
|
|
|
|
if (hio == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for hio request.",
|
|
|
|
sizeof(*hio));
|
|
|
|
}
|
2013-04-27 05:01:29 +00:00
|
|
|
refcnt_init(&hio->hio_countdown, 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
|
|
|
|
if (hio->hio_errors == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable allocate %zu bytes of memory for hio errors.",
|
|
|
|
sizeof(hio->hio_errors[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
|
|
|
|
if (hio->hio_next == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable allocate %zu bytes of memory for hio_next field.",
|
|
|
|
sizeof(hio->hio_next[0]) * ncomps);
|
|
|
|
}
|
|
|
|
hio->hio_ggio.gctl_version = G_GATE_VERSION;
|
|
|
|
hio->hio_ggio.gctl_data = malloc(MAXPHYS);
|
|
|
|
if (hio->hio_ggio.gctl_data == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate %zu bytes of memory for gctl_data.",
|
|
|
|
MAXPHYS);
|
|
|
|
}
|
|
|
|
hio->hio_ggio.gctl_length = MAXPHYS;
|
|
|
|
hio->hio_ggio.gctl_error = 0;
|
|
|
|
TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_free_list_size++;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-24 17:28:25 +00:00
|
|
|
static bool
|
|
|
|
init_resuid(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
if (res->hr_resuid != 0) {
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
return (false);
|
|
|
|
} else {
|
|
|
|
/* Initialize unique resource identifier. */
|
|
|
|
arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
|
|
|
|
mtx_unlock(&metadata_lock);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (metadata_write(res) == -1)
|
2010-10-24 17:28:25 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
init_local(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t mapsize;
|
|
|
|
|
2012-01-10 22:39:07 +00:00
|
|
|
if (metadata_read(res, true) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
mtx_init(&res->hr_amp_lock);
|
|
|
|
if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
|
2012-01-10 22:39:07 +00:00
|
|
|
res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exit(EX_TEMPFAIL, "Unable to create activemap");
|
|
|
|
}
|
|
|
|
mtx_init(&range_lock);
|
|
|
|
cv_init(&range_regular_cond);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (rangelock_init(&range_regular) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
|
|
|
|
cv_init(&range_sync_cond);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (rangelock_init(&range_sync) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
|
|
|
|
mapsize = activemap_ondisk_size(res->hr_amp);
|
|
|
|
buf = calloc(1, mapsize);
|
|
|
|
if (buf == NULL) {
|
|
|
|
primary_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate buffer for activemap.");
|
|
|
|
}
|
|
|
|
if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
|
|
|
|
(ssize_t)mapsize) {
|
|
|
|
primary_exit(EX_NOINPUT, "Unable to read activemap");
|
|
|
|
}
|
|
|
|
activemap_copyin(res->hr_amp, buf, mapsize);
|
2010-06-14 21:37:25 +00:00
|
|
|
free(buf);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (res->hr_resuid != 0)
|
|
|
|
return;
|
|
|
|
/*
|
2010-10-24 17:28:25 +00:00
|
|
|
* We're using provider for the first time. Initialize local and remote
|
|
|
|
* counters. We don't initialize resuid here, as we want to do it just
|
|
|
|
* in time. The reason for this is that we want to inform secondary
|
|
|
|
* that there were no writes yet, so there is no need to synchronize
|
|
|
|
* anything.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2011-03-21 21:16:12 +00:00
|
|
|
res->hr_primary_localcnt = 0;
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_primary_remotecnt = 0;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (metadata_write(res) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
exit(EX_NOINPUT);
|
|
|
|
}
|
|
|
|
|
2011-02-03 11:39:49 +00:00
|
|
|
static int
|
|
|
|
primary_connect(struct hast_resource *res, struct proto_conn **connp)
|
|
|
|
{
|
|
|
|
struct proto_conn *conn;
|
|
|
|
int16_t val;
|
|
|
|
|
|
|
|
val = 1;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
primary_exit(EX_TEMPFAIL,
|
|
|
|
"Unable to send connection request to parent");
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
primary_exit(EX_TEMPFAIL,
|
|
|
|
"Unable to receive reply to connection request from parent");
|
|
|
|
}
|
|
|
|
if (val != 0) {
|
|
|
|
errno = val;
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
primary_exit(EX_TEMPFAIL,
|
|
|
|
"Unable to receive connection from parent");
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_connect_wait(conn, res->hr_timeout) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
proto_close(conn);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* Error in setting timeout is not critical, but why should it fail? */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_timeout(conn, res->hr_timeout) == -1)
|
2011-02-03 11:39:49 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
|
|
|
|
|
|
|
|
*connp = conn;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
|
2012-07-04 20:20:48 +00:00
|
|
|
/*
|
|
|
|
* Function instructs GEOM_GATE to handle reads directly from within the kernel.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
enable_direct_reads(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
struct g_gate_ctl_modify ggiomodify;
|
|
|
|
|
|
|
|
bzero(&ggiomodify, sizeof(ggiomodify));
|
|
|
|
ggiomodify.gctl_version = G_GATE_VERSION;
|
|
|
|
ggiomodify.gctl_unit = res->hr_ggateunit;
|
|
|
|
ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
|
|
|
|
strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
|
|
|
|
sizeof(ggiomodify.gctl_readprov));
|
|
|
|
ggiomodify.gctl_readoffset = res->hr_localoff;
|
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
|
|
|
|
pjdlog_debug(1, "Direct reads enabled.");
|
|
|
|
else
|
|
|
|
pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
|
|
|
|
}
|
2011-02-03 11:39:49 +00:00
|
|
|
|
2011-04-20 18:43:28 +00:00
|
|
|
static int
|
2010-03-27 16:35:07 +00:00
|
|
|
init_remote(struct hast_resource *res, struct proto_conn **inp,
|
|
|
|
struct proto_conn **outp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2010-03-27 16:35:07 +00:00
|
|
|
struct proto_conn *in, *out;
|
2010-02-18 23:16:19 +00:00
|
|
|
struct nv *nvout, *nvin;
|
|
|
|
const unsigned char *token;
|
|
|
|
unsigned char *map;
|
|
|
|
const char *errmsg;
|
|
|
|
int32_t extentsize;
|
|
|
|
int64_t datasize;
|
|
|
|
uint32_t mapsize;
|
2013-02-17 21:12:34 +00:00
|
|
|
uint8_t version;
|
2010-02-18 23:16:19 +00:00
|
|
|
size_t size;
|
2011-04-20 18:43:28 +00:00
|
|
|
int error;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
|
|
|
|
PJDLOG_ASSERT(real_remote(res));
|
2010-03-27 16:35:07 +00:00
|
|
|
|
|
|
|
in = out = NULL;
|
2010-08-30 00:12:10 +00:00
|
|
|
errmsg = NULL;
|
2010-03-27 16:35:07 +00:00
|
|
|
|
2011-02-03 11:39:49 +00:00
|
|
|
if (primary_connect(res, &out) == -1)
|
2011-04-20 18:43:28 +00:00
|
|
|
return (ECONNREFUSED);
|
|
|
|
|
|
|
|
error = ECONNABORTED;
|
2011-02-03 11:39:49 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* First handshake step.
|
|
|
|
* Setup outgoing connection with remote node.
|
|
|
|
*/
|
|
|
|
nvout = nv_alloc();
|
|
|
|
nv_add_string(nvout, res->hr_name, "resource");
|
2013-02-17 21:12:34 +00:00
|
|
|
nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
|
2010-02-18 23:16:19 +00:00
|
|
|
if (nv_error(nvout) != 0) {
|
|
|
|
pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
|
|
|
|
"Unable to allocate header for connection with %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
nv_free(nvout);
|
|
|
|
goto close;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to send handshake header to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
nv_free(nvout);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
nv_free(nvout);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(out, &nvin) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to receive handshake header from %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
errmsg = nv_get_string(nvin, "errmsg");
|
|
|
|
if (errmsg != NULL) {
|
|
|
|
pjdlog_warning("%s", errmsg);
|
2011-04-20 18:43:28 +00:00
|
|
|
if (nv_exists(nvin, "wait"))
|
|
|
|
error = EBUSY;
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
version = nv_get_uint8(nvin, "version");
|
|
|
|
if (version == 0) {
|
|
|
|
/*
|
|
|
|
* If no version is sent, it means this is protocol version 1.
|
|
|
|
*/
|
|
|
|
version = 1;
|
|
|
|
}
|
|
|
|
if (version > HAST_PROTO_VERSION) {
|
|
|
|
pjdlog_warning("Invalid version received (%hhu).", version);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
res->hr_version = version;
|
|
|
|
pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
|
2010-02-18 23:16:19 +00:00
|
|
|
token = nv_get_uint8_array(nvin, &size, "token");
|
|
|
|
if (token == NULL) {
|
|
|
|
pjdlog_warning("Handshake header from %s has no 'token' field.",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
if (size != sizeof(res->hr_token)) {
|
|
|
|
pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
|
|
|
|
res->hr_remoteaddr, size, sizeof(res->hr_token));
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
bcopy(token, res->hr_token, sizeof(res->hr_token));
|
|
|
|
nv_free(nvin);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second handshake step.
|
|
|
|
* Setup incoming connection with remote node.
|
|
|
|
*/
|
2011-02-03 11:39:49 +00:00
|
|
|
if (primary_connect(res, &in) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto close;
|
2011-02-03 11:39:49 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
nvout = nv_alloc();
|
|
|
|
nv_add_string(nvout, res->hr_name, "resource");
|
|
|
|
nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
|
|
|
|
"token");
|
2010-10-24 17:28:25 +00:00
|
|
|
if (res->hr_resuid == 0) {
|
|
|
|
/*
|
|
|
|
* The resuid field was not yet initialized.
|
|
|
|
* Because we do synchronization inside init_resuid(), it is
|
|
|
|
* possible that someone already initialized it, the function
|
|
|
|
* will return false then, but if we successfully initialized
|
|
|
|
* it, we will get true. True means that there were no writes
|
|
|
|
* to this resource yet and we want to inform secondary that
|
|
|
|
* synchronization is not needed by sending "virgin" argument.
|
|
|
|
*/
|
|
|
|
if (init_resuid(res))
|
|
|
|
nv_add_int8(nvout, 1, "virgin");
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_add_uint64(nvout, res->hr_resuid, "resuid");
|
|
|
|
nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
|
|
|
|
nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
|
|
|
|
if (nv_error(nvout) != 0) {
|
|
|
|
pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
|
|
|
|
"Unable to allocate header for connection with %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
nv_free(nvout);
|
|
|
|
goto close;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to send handshake header to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
nv_free(nvout);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
nv_free(nvout);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(out, &nvin) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to receive handshake header from %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
errmsg = nv_get_string(nvin, "errmsg");
|
|
|
|
if (errmsg != NULL) {
|
|
|
|
pjdlog_warning("%s", errmsg);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
datasize = nv_get_int64(nvin, "datasize");
|
|
|
|
if (datasize != res->hr_datasize) {
|
|
|
|
pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
|
|
|
|
(intmax_t)res->hr_datasize, (intmax_t)datasize);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
extentsize = nv_get_int32(nvin, "extentsize");
|
|
|
|
if (extentsize != res->hr_extentsize) {
|
|
|
|
pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
|
|
|
|
(ssize_t)res->hr_extentsize, (ssize_t)extentsize);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
|
|
|
|
res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
|
|
|
|
res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
|
2012-07-04 20:20:48 +00:00
|
|
|
if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
|
|
|
|
enable_direct_reads(res);
|
2011-04-19 19:26:27 +00:00
|
|
|
if (nv_exists(nvin, "virgin")) {
|
|
|
|
/*
|
|
|
|
* Secondary was reinitialized, bump localcnt if it is 0 as
|
|
|
|
* only we have the data.
|
|
|
|
*/
|
|
|
|
PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
|
|
|
|
PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
|
|
|
|
|
|
|
|
if (res->hr_primary_localcnt == 0) {
|
|
|
|
PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
|
|
|
|
|
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
res->hr_primary_localcnt++;
|
|
|
|
pjdlog_debug(1, "Increasing localcnt to %ju.",
|
|
|
|
(uintmax_t)res->hr_primary_localcnt);
|
|
|
|
(void)metadata_write(res);
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
map = NULL;
|
|
|
|
mapsize = nv_get_uint32(nvin, "mapsize");
|
|
|
|
if (mapsize > 0) {
|
|
|
|
map = malloc(mapsize);
|
|
|
|
if (map == NULL) {
|
|
|
|
pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
|
|
|
|
(uintmax_t)mapsize);
|
|
|
|
nv_free(nvin);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Remote node have some dirty extents on its own, lets
|
|
|
|
* download its activemap.
|
|
|
|
*/
|
2010-03-27 16:35:07 +00:00
|
|
|
if (hast_proto_recv_data(res, out, nvin, map,
|
2012-01-10 22:39:07 +00:00
|
|
|
mapsize) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to receive remote activemap");
|
|
|
|
nv_free(nvin);
|
|
|
|
free(map);
|
|
|
|
goto close;
|
|
|
|
}
|
2013-10-26 08:35:54 +00:00
|
|
|
mtx_lock(&res->hr_amp_lock);
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Merge local and remote bitmaps.
|
|
|
|
*/
|
|
|
|
activemap_merge(res->hr_amp, map, mapsize);
|
|
|
|
free(map);
|
|
|
|
/*
|
|
|
|
* Now that we merged bitmaps from both nodes, flush it to the
|
|
|
|
* disk before we start to synchronize.
|
|
|
|
*/
|
|
|
|
(void)hast_activemap_flush(res);
|
|
|
|
}
|
2010-10-24 15:41:23 +00:00
|
|
|
nv_free(nvin);
|
2011-06-17 07:07:26 +00:00
|
|
|
#ifdef notyet
|
2011-04-02 09:25:13 +00:00
|
|
|
/* Setup directions. */
|
|
|
|
if (proto_send(out, NULL, 0) == -1)
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
|
|
|
|
if (proto_recv(in, NULL, 0) == -1)
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
|
2011-06-17 07:07:26 +00:00
|
|
|
#endif
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_info("Connected to %s.", res->hr_remoteaddr);
|
2013-02-17 21:12:34 +00:00
|
|
|
if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
|
|
|
|
res->hr_version < 2) {
|
|
|
|
pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
|
|
|
|
res->hr_replication = HAST_REPLICATION_FULLSYNC;
|
|
|
|
} else if (res->hr_replication != res->hr_original_replication) {
|
|
|
|
/*
|
|
|
|
* This is in case hastd disconnected and was upgraded.
|
|
|
|
*/
|
|
|
|
res->hr_replication = res->hr_original_replication;
|
|
|
|
}
|
2010-03-27 16:35:07 +00:00
|
|
|
if (inp != NULL && outp != NULL) {
|
|
|
|
*inp = in;
|
|
|
|
*outp = out;
|
|
|
|
} else {
|
|
|
|
res->hr_remotein = in;
|
|
|
|
res->hr_remoteout = out;
|
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_CONNECT);
|
2011-04-20 18:43:28 +00:00
|
|
|
return (0);
|
2010-03-27 16:35:07 +00:00
|
|
|
close:
|
2010-08-30 00:12:10 +00:00
|
|
|
if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_SPLITBRAIN);
|
2010-03-27 16:35:07 +00:00
|
|
|
proto_close(out);
|
|
|
|
if (in != NULL)
|
|
|
|
proto_close(in);
|
2011-04-20 18:43:28 +00:00
|
|
|
return (error);
|
2010-03-27 16:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_start(void)
|
|
|
|
{
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
sync_inprogress = true;
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
cv_signal(&sync_cond);
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:06:00 +00:00
|
|
|
static void
|
|
|
|
sync_stop(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
if (sync_inprogress)
|
|
|
|
sync_inprogress = false;
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
init_ggate(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
struct g_gate_ctl_create ggiocreate;
|
|
|
|
struct g_gate_ctl_cancel ggiocancel;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We communicate with ggate via /dev/ggctl. Open it.
|
|
|
|
*/
|
|
|
|
res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (res->hr_ggatefd == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
|
|
|
|
/*
|
|
|
|
* Create provider before trying to connect, as connection failure
|
|
|
|
* is not critical, but may take some time.
|
|
|
|
*/
|
2010-10-07 18:23:28 +00:00
|
|
|
bzero(&ggiocreate, sizeof(ggiocreate));
|
2010-02-18 23:16:19 +00:00
|
|
|
ggiocreate.gctl_version = G_GATE_VERSION;
|
|
|
|
ggiocreate.gctl_mediasize = res->hr_datasize;
|
|
|
|
ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
|
|
|
|
ggiocreate.gctl_flags = 0;
|
2011-04-02 07:01:09 +00:00
|
|
|
ggiocreate.gctl_maxcount = 0;
|
2010-02-18 23:16:19 +00:00
|
|
|
ggiocreate.gctl_timeout = 0;
|
|
|
|
ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
|
|
|
|
snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
|
|
|
|
res->hr_provname);
|
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
|
|
|
|
pjdlog_info("Device hast/%s created.", res->hr_provname);
|
|
|
|
res->hr_ggateunit = ggiocreate.gctl_unit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
primary_exit(EX_OSERR, "Unable to create hast/%s device",
|
|
|
|
res->hr_provname);
|
|
|
|
}
|
|
|
|
pjdlog_debug(1,
|
|
|
|
"Device hast/%s already exists, we will try to take it over.",
|
|
|
|
res->hr_provname);
|
|
|
|
/*
|
|
|
|
* If we received EEXIST, we assume that the process who created the
|
|
|
|
* provider died and didn't clean up. In that case we will start from
|
|
|
|
* where he left of.
|
|
|
|
*/
|
2010-10-07 18:23:28 +00:00
|
|
|
bzero(&ggiocancel, sizeof(ggiocancel));
|
2010-02-18 23:16:19 +00:00
|
|
|
ggiocancel.gctl_version = G_GATE_VERSION;
|
|
|
|
ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
|
|
|
|
snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
|
|
|
|
res->hr_provname);
|
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
|
|
|
|
pjdlog_info("Device hast/%s recovered.", res->hr_provname);
|
|
|
|
res->hr_ggateunit = ggiocancel.gctl_unit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
primary_exit(EX_OSERR, "Unable to take over hast/%s device",
|
|
|
|
res->hr_provname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hastd_primary(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
pthread_t td;
|
|
|
|
pid_t pid;
|
2011-03-11 12:12:35 +00:00
|
|
|
int error, mode, debuglevel;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
/*
|
2011-02-03 11:39:49 +00:00
|
|
|
* Create communication channel for sending control commands from
|
|
|
|
* parent to child.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
|
2011-01-28 21:51:40 +00:00
|
|
|
/* TODO: There's no need for this to be fatal error. */
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
2010-08-30 22:28:04 +00:00
|
|
|
pjdlog_exit(EX_OSERR,
|
2010-02-18 23:16:19 +00:00
|
|
|
"Unable to create control sockets between parent and child");
|
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
/*
|
2011-02-03 11:39:49 +00:00
|
|
|
* Create communication channel for sending events from child to parent.
|
2010-08-30 23:26:10 +00:00
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
|
2011-01-28 21:51:40 +00:00
|
|
|
/* TODO: There's no need for this to be fatal error. */
|
2010-08-30 23:26:10 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR,
|
|
|
|
"Unable to create event sockets between child and parent");
|
|
|
|
}
|
2011-02-03 11:39:49 +00:00
|
|
|
/*
|
|
|
|
* Create communication channel for sending connection requests from
|
|
|
|
* child to parent.
|
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
/* TODO: There's no need for this to be fatal error. */
|
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR,
|
|
|
|
"Unable to create connection sockets between child and parent");
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
pid = fork();
|
2012-01-06 23:44:26 +00:00
|
|
|
if (pid == -1) {
|
2011-01-28 21:51:40 +00:00
|
|
|
/* TODO: There's no need for this to be fatal error. */
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
2010-08-30 22:28:04 +00:00
|
|
|
pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid > 0) {
|
|
|
|
/* This is parent. */
|
2010-08-30 23:26:10 +00:00
|
|
|
/* Declare that we are receiver. */
|
|
|
|
proto_recv(res->hr_event, NULL, 0);
|
2011-02-03 11:39:49 +00:00
|
|
|
proto_recv(res->hr_conn, NULL, 0);
|
2011-01-28 21:52:37 +00:00
|
|
|
/* Declare that we are sender. */
|
|
|
|
proto_send(res->hr_ctrl, NULL, 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_workerpid = pid;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-29 21:41:53 +00:00
|
|
|
|
2010-08-30 00:31:30 +00:00
|
|
|
gres = res;
|
2013-10-26 08:38:21 +00:00
|
|
|
res->output_status_aux = output_status_aux;
|
2011-01-28 21:52:37 +00:00
|
|
|
mode = pjdlog_mode_get();
|
2011-03-11 12:12:35 +00:00
|
|
|
debuglevel = pjdlog_debug_get();
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-30 23:26:10 +00:00
|
|
|
/* Declare that we are sender. */
|
|
|
|
proto_send(res->hr_event, NULL, 0);
|
2011-02-03 11:39:49 +00:00
|
|
|
proto_send(res->hr_conn, NULL, 0);
|
2011-01-28 21:52:37 +00:00
|
|
|
/* Declare that we are receiver. */
|
|
|
|
proto_recv(res->hr_ctrl, NULL, 0);
|
|
|
|
descriptors_cleanup(res);
|
|
|
|
|
2011-01-28 21:57:42 +00:00
|
|
|
descriptors_assert(res, mode);
|
|
|
|
|
2011-01-28 21:52:37 +00:00
|
|
|
pjdlog_init(mode);
|
2011-03-11 12:12:35 +00:00
|
|
|
pjdlog_debug_set(debuglevel);
|
2011-01-28 21:52:37 +00:00
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
|
2011-03-25 20:13:38 +00:00
|
|
|
setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
|
2010-08-30 23:26:10 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
init_local(res);
|
|
|
|
init_ggate(res);
|
|
|
|
init_environment(res);
|
2011-01-24 15:04:15 +00:00
|
|
|
|
2011-05-14 17:02:03 +00:00
|
|
|
if (drop_privs(res) != 0) {
|
2011-01-28 22:35:46 +00:00
|
|
|
cleanup(res);
|
|
|
|
exit(EX_CONFIG);
|
|
|
|
}
|
2011-02-03 10:37:44 +00:00
|
|
|
pjdlog_info("Privileges successfully dropped.");
|
2011-01-28 22:35:46 +00:00
|
|
|
|
2010-10-07 18:19:02 +00:00
|
|
|
/*
|
|
|
|
* Create the guard thread first, so we can handle signals from the
|
2012-01-07 16:09:33 +00:00
|
|
|
* very beginning.
|
2010-10-07 18:19:02 +00:00
|
|
|
*/
|
|
|
|
error = pthread_create(&td, NULL, guard_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-09-22 19:03:11 +00:00
|
|
|
/*
|
|
|
|
* Create the control thread before sending any event to the parent,
|
|
|
|
* as we can deadlock when parent sends control request to worker,
|
|
|
|
* but worker has no control thread started yet, so parent waits.
|
|
|
|
* In the meantime worker sends an event to the parent, but parent
|
|
|
|
* is unable to handle the event, because it waits for control
|
|
|
|
* request response.
|
|
|
|
*/
|
|
|
|
error = pthread_create(&td, NULL, ctrl_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2011-04-20 18:43:28 +00:00
|
|
|
if (real_remote(res)) {
|
|
|
|
error = init_remote(res, NULL, NULL);
|
|
|
|
if (error == 0) {
|
|
|
|
sync_start();
|
|
|
|
} else if (error == EBUSY) {
|
|
|
|
time_t start = time(NULL);
|
|
|
|
|
|
|
|
pjdlog_warning("Waiting for remote node to become %s for %ds.",
|
|
|
|
role2str(HAST_ROLE_SECONDARY),
|
|
|
|
res->hr_timeout);
|
|
|
|
for (;;) {
|
|
|
|
sleep(1);
|
|
|
|
error = init_remote(res, NULL, NULL);
|
|
|
|
if (error != EBUSY)
|
|
|
|
break;
|
|
|
|
if (time(NULL) > start + res->hr_timeout)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error == EBUSY) {
|
|
|
|
pjdlog_warning("Remote node is still %s, starting anyway.",
|
|
|
|
role2str(HAST_ROLE_PRIMARY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
error = pthread_create(&td, NULL, ggate_recv_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
error = pthread_create(&td, NULL, local_send_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
error = pthread_create(&td, NULL, remote_send_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
error = pthread_create(&td, NULL, remote_recv_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
error = pthread_create(&td, NULL, ggate_send_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2011-04-20 18:43:28 +00:00
|
|
|
fullystarted = true;
|
2010-10-07 18:19:02 +00:00
|
|
|
(void)sync_thread(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-17 21:12:34 +00:00
|
|
|
reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
|
|
|
|
const char *fmt, ...)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
char msg[1024];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2012-06-03 10:50:46 +00:00
|
|
|
(void)vsnprintf(msg, sizeof(msg), fmt, ap);
|
2010-02-18 23:16:19 +00:00
|
|
|
va_end(ap);
|
2012-06-03 10:50:46 +00:00
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
(void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
|
2013-02-17 21:12:34 +00:00
|
|
|
(uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
|
2012-06-03 10:50:46 +00:00
|
|
|
break;
|
|
|
|
case BIO_DELETE:
|
|
|
|
(void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
|
2013-02-17 21:12:34 +00:00
|
|
|
(uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
|
2012-06-03 10:50:46 +00:00
|
|
|
break;
|
|
|
|
case BIO_FLUSH:
|
|
|
|
(void)snprlcat(msg, sizeof(msg), "FLUSH.");
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
(void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
|
2013-02-17 21:12:34 +00:00
|
|
|
(uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
|
2012-06-03 10:50:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
|
|
|
|
(unsigned int)ggio->gctl_cmd);
|
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remote_close(struct hast_resource *res, int ncomp)
|
|
|
|
{
|
|
|
|
|
|
|
|
rw_wlock(&hio_remote_lock[ncomp]);
|
|
|
|
/*
|
2011-10-27 20:10:21 +00:00
|
|
|
* Check for a race between dropping rlock and acquiring wlock -
|
2010-02-18 23:16:19 +00:00
|
|
|
* another thread can close connection in-between.
|
|
|
|
*/
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein == NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout == NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein != NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-27 14:12:53 +00:00
|
|
|
pjdlog_debug(2, "Closing incoming connection to %s.",
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_remoteaddr);
|
|
|
|
proto_close(res->hr_remotein);
|
|
|
|
res->hr_remotein = NULL;
|
2010-08-27 14:12:53 +00:00
|
|
|
pjdlog_debug(2, "Closing outgoing connection to %s.",
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_remoteaddr);
|
|
|
|
proto_close(res->hr_remoteout);
|
|
|
|
res->hr_remoteout = NULL;
|
|
|
|
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
|
2010-08-27 14:12:53 +00:00
|
|
|
pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Stop synchronization if in-progress.
|
|
|
|
*/
|
2010-08-27 14:06:00 +00:00
|
|
|
sync_stop();
|
2010-08-30 00:31:30 +00:00
|
|
|
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_DISCONNECT);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 20:32:57 +00:00
|
|
|
/*
|
|
|
|
* Acknowledge write completion to the kernel, but don't update activemap yet.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_complete(struct hast_resource *res, struct hio *hio)
|
|
|
|
{
|
|
|
|
struct g_gate_ctl_io *ggio;
|
|
|
|
unsigned int ncomp;
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(!hio->hio_done);
|
|
|
|
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bump local count if this is first write after
|
|
|
|
* connection failure with remote node.
|
|
|
|
*/
|
|
|
|
ncomp = 1;
|
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
|
|
|
|
res->hr_primary_localcnt++;
|
|
|
|
pjdlog_debug(1, "Increasing localcnt to %ju.",
|
|
|
|
(uintmax_t)res->hr_primary_localcnt);
|
|
|
|
(void)metadata_write(res);
|
|
|
|
}
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
}
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
|
2011-10-27 20:32:57 +00:00
|
|
|
primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
|
|
|
|
hio->hio_done = true;
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Thread receives ggate I/O requests from the kernel and passes them to
|
|
|
|
* appropriate threads:
|
|
|
|
* WRITE - always goes to both local_send and remote_send threads
|
|
|
|
* READ (when the block is up-to-date on local component) -
|
|
|
|
* only local_send thread
|
|
|
|
* READ (when the block isn't up-to-date on local component) -
|
|
|
|
* only remote_send thread
|
|
|
|
* DELETE - always goes to both local_send and remote_send threads
|
|
|
|
* FLUSH - always goes to both local_send and remote_send threads
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
ggate_recv_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
|
|
|
struct hio *hio;
|
|
|
|
unsigned int ii, ncomp, ncomps;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "ggate_recv: Taking free request.");
|
|
|
|
QUEUE_TAKE2(hio, free);
|
|
|
|
pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
ggio->gctl_unit = res->hr_ggateunit;
|
|
|
|
ggio->gctl_length = MAXPHYS;
|
|
|
|
ggio->gctl_error = 0;
|
2011-10-27 20:32:57 +00:00
|
|
|
hio->hio_done = false;
|
|
|
|
hio->hio_replication = res->hr_replication;
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"ggate_recv: (%p) Waiting for request from the kernel.",
|
|
|
|
hio);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
if (sigexit_received)
|
|
|
|
pthread_exit(NULL);
|
|
|
|
primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
|
|
|
|
}
|
|
|
|
error = ggio->gctl_error;
|
|
|
|
switch (error) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case ECANCELED:
|
|
|
|
/* Exit gracefully. */
|
|
|
|
if (!sigexit_received) {
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"ggate_recv: (%p) Received cancel from the kernel.",
|
|
|
|
hio);
|
|
|
|
pjdlog_info("Received cancel from the kernel, exiting.");
|
|
|
|
}
|
|
|
|
pthread_exit(NULL);
|
|
|
|
case ENOMEM:
|
|
|
|
/*
|
|
|
|
* Buffer too small? Impossible, we allocate MAXPHYS
|
|
|
|
* bytes - request can't be bigger than that.
|
|
|
|
*/
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case ENXIO:
|
|
|
|
default:
|
|
|
|
primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
|
|
|
|
strerror(error));
|
|
|
|
}
|
2011-10-27 20:32:57 +00:00
|
|
|
|
|
|
|
ncomp = 0;
|
|
|
|
ncomps = HAST_NCOMPONENTS;
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
for (ii = 0; ii < ncomps; ii++)
|
|
|
|
hio->hio_errors[ii] = EINVAL;
|
|
|
|
reqlog(LOG_DEBUG, 2, ggio,
|
|
|
|
"ggate_recv: (%p) Request received from the kernel: ",
|
|
|
|
hio);
|
2011-10-27 20:32:57 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Inform all components about new write request.
|
|
|
|
* For read request prefer local component unless the given
|
|
|
|
* range is out-of-date, then use remote component.
|
|
|
|
*/
|
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
2011-05-23 21:15:19 +00:00
|
|
|
res->hr_stat_read++;
|
2011-10-27 20:32:57 +00:00
|
|
|
ncomps = 1;
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
|
|
|
|
res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
|
|
|
|
/*
|
|
|
|
* This range is up-to-date on local component,
|
|
|
|
* so handle request locally.
|
|
|
|
*/
|
|
|
|
/* Local component is 0 for now. */
|
|
|
|
ncomp = 0;
|
|
|
|
} else /* if (res->hr_syncsrc ==
|
|
|
|
HAST_SYNCSRC_SECONDARY) */ {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_syncsrc ==
|
2010-02-18 23:16:19 +00:00
|
|
|
HAST_SYNCSRC_SECONDARY);
|
|
|
|
/*
|
|
|
|
* This range is out-of-date on local component,
|
|
|
|
* so send request to the remote node.
|
|
|
|
*/
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
ncomp = 1;
|
|
|
|
}
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
2011-05-23 21:15:19 +00:00
|
|
|
res->hr_stat_write++;
|
2011-10-27 18:45:01 +00:00
|
|
|
if (res->hr_resuid == 0 &&
|
|
|
|
res->hr_primary_localcnt == 0) {
|
|
|
|
/* This is first write. */
|
2011-03-21 21:16:12 +00:00
|
|
|
res->hr_primary_localcnt = 1;
|
2010-10-24 17:28:25 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
for (;;) {
|
|
|
|
mtx_lock(&range_lock);
|
|
|
|
if (rangelock_islocked(range_sync,
|
|
|
|
ggio->gctl_offset, ggio->gctl_length)) {
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"regular: Range offset=%jd length=%zu locked.",
|
|
|
|
(intmax_t)ggio->gctl_offset,
|
|
|
|
(size_t)ggio->gctl_length);
|
|
|
|
range_regular_wait = true;
|
|
|
|
cv_wait(&range_regular_cond, &range_lock);
|
|
|
|
range_regular_wait = false;
|
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (rangelock_add(range_regular,
|
2012-01-10 22:39:07 +00:00
|
|
|
ggio->gctl_offset, ggio->gctl_length) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"regular: Range offset=%jd length=%zu is already locked, waiting.",
|
|
|
|
(intmax_t)ggio->gctl_offset,
|
|
|
|
(size_t)ggio->gctl_length);
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mtx_lock(&res->hr_amp_lock);
|
|
|
|
if (activemap_write_start(res->hr_amp,
|
|
|
|
ggio->gctl_offset, ggio->gctl_length)) {
|
2011-05-23 21:15:19 +00:00
|
|
|
res->hr_stat_activemap_update++;
|
2010-02-18 23:16:19 +00:00
|
|
|
(void)hast_activemap_flush(res);
|
2013-09-19 20:19:08 +00:00
|
|
|
} else {
|
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-12-10 19:58:10 +00:00
|
|
|
if (ISMEMSYNC(hio)) {
|
2013-12-10 19:56:26 +00:00
|
|
|
hio->hio_memsyncacked = false;
|
|
|
|
refcnt_init(&hio->hio_writecount, ncomps);
|
|
|
|
}
|
2011-10-27 20:32:57 +00:00
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
case BIO_DELETE:
|
2011-10-27 20:32:57 +00:00
|
|
|
res->hr_stat_delete++;
|
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
case BIO_FLUSH:
|
2011-10-27 20:32:57 +00:00
|
|
|
res->hr_stat_flush++;
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-10-27 20:32:57 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"ggate_recv: (%p) Moving request to the send queues.", hio);
|
2013-12-10 19:56:26 +00:00
|
|
|
refcnt_init(&hio->hio_countdown, ncomps);
|
2013-02-17 21:12:34 +00:00
|
|
|
for (ii = ncomp; ii < ncomps; ii++)
|
2011-10-27 20:32:57 +00:00
|
|
|
QUEUE_INSERT1(hio, send, ii);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread reads from or writes to local component.
|
|
|
|
* If local read fails, it redirects it to remote_send thread.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
local_send_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
|
|
|
struct hio *hio;
|
|
|
|
unsigned int ncomp, rncomp;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Local component is 0 for now. */
|
|
|
|
ncomp = 0;
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
rncomp = 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "local_send: Taking request.");
|
2010-11-02 22:13:08 +00:00
|
|
|
QUEUE_TAKE1(hio, send, ncomp, 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2, "local_send: (%p) Got request.", hio);
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
ret = pread(res->hr_localfd, ggio->gctl_data,
|
|
|
|
ggio->gctl_length,
|
|
|
|
ggio->gctl_offset + res->hr_localoff);
|
|
|
|
if (ret == ggio->gctl_length)
|
|
|
|
hio->hio_errors[ncomp] = 0;
|
2011-05-29 21:20:47 +00:00
|
|
|
else if (!ISSYNCREQ(hio)) {
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* If READ failed, try to read from remote node.
|
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ret == -1) {
|
2010-12-16 07:30:47 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%s), trying remote node. ",
|
|
|
|
strerror(errno));
|
|
|
|
} else if (ret != ggio->gctl_length) {
|
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%zd != %jd), trying remote node. ",
|
2010-12-16 19:48:03 +00:00
|
|
|
ret, (intmax_t)ggio->gctl_length);
|
2010-12-16 07:30:47 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
QUEUE_INSERT1(hio, send, rncomp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
ret = pwrite(res->hr_localfd, ggio->gctl_data,
|
|
|
|
ggio->gctl_length,
|
|
|
|
ggio->gctl_offset + res->hr_localoff);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ret == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = errno;
|
2010-12-16 07:30:47 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%s): ",
|
|
|
|
strerror(errno));
|
|
|
|
} else if (ret != ggio->gctl_length) {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = EIO;
|
2010-12-16 07:30:47 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%zd != %jd): ",
|
2010-12-16 19:48:03 +00:00
|
|
|
ret, (intmax_t)ggio->gctl_length);
|
2010-12-16 07:30:47 +00:00
|
|
|
} else {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = 0;
|
2013-12-10 19:58:10 +00:00
|
|
|
if (ISASYNC(hio)) {
|
2011-10-27 20:32:57 +00:00
|
|
|
ggio->gctl_error = 0;
|
|
|
|
write_complete(res, hio);
|
|
|
|
}
|
2010-12-16 07:30:47 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
case BIO_DELETE:
|
|
|
|
ret = g_delete(res->hr_localfd,
|
|
|
|
ggio->gctl_offset + res->hr_localoff,
|
|
|
|
ggio->gctl_length);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ret == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = errno;
|
2010-12-16 07:30:47 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%s): ",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = 0;
|
2010-12-16 07:30:47 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
case BIO_FLUSH:
|
2011-09-28 13:19:47 +00:00
|
|
|
if (!res->hr_localflush) {
|
|
|
|
ret = -1;
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
ret = g_flush(res->hr_localfd);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ret == -1) {
|
2011-09-28 13:19:47 +00:00
|
|
|
if (errno == EOPNOTSUPP)
|
|
|
|
res->hr_localflush = false;
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = errno;
|
2010-12-16 07:30:47 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
|
|
|
"Local request failed (%s): ",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = 0;
|
2010-12-16 07:30:47 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-10 19:56:26 +00:00
|
|
|
if (ISMEMSYNCWRITE(hio)) {
|
|
|
|
if (refcnt_release(&hio->hio_writecount) == 0) {
|
|
|
|
write_complete(res, hio);
|
2013-02-17 21:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-10 19:56:26 +00:00
|
|
|
if (refcnt_release(&hio->hio_countdown) > 0)
|
|
|
|
continue;
|
2011-10-27 20:13:39 +00:00
|
|
|
if (ISSYNCREQ(hio)) {
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
SYNCREQDONE(hio);
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
cv_signal(&sync_cond);
|
|
|
|
} else {
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"local_send: (%p) Moving request to the done queue.",
|
|
|
|
hio);
|
|
|
|
QUEUE_INSERT2(hio, done);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-11-02 22:13:08 +00:00
|
|
|
static void
|
|
|
|
keepalive_send(struct hast_resource *res, unsigned int ncomp)
|
|
|
|
{
|
|
|
|
struct nv *nv;
|
|
|
|
|
2011-02-03 11:33:32 +00:00
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2010-11-02 22:13:08 +00:00
|
|
|
return;
|
2011-02-03 11:33:32 +00:00
|
|
|
}
|
2011-03-22 10:39:34 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein != NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout != NULL);
|
2010-11-02 22:13:08 +00:00
|
|
|
|
|
|
|
nv = nv_alloc();
|
|
|
|
nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
|
|
|
|
if (nv_error(nv) != 0) {
|
2011-02-03 11:33:32 +00:00
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2010-11-02 22:13:08 +00:00
|
|
|
nv_free(nv);
|
|
|
|
pjdlog_debug(1,
|
|
|
|
"keepalive_send: Unable to prepare header to send.");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
|
2011-02-03 11:33:32 +00:00
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2010-11-02 22:13:08 +00:00
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"keepalive_send: Unable to send request");
|
|
|
|
nv_free(nv);
|
|
|
|
remote_close(res, ncomp);
|
|
|
|
return;
|
|
|
|
}
|
2011-02-03 11:33:32 +00:00
|
|
|
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2010-11-02 22:13:08 +00:00
|
|
|
nv_free(nv);
|
|
|
|
pjdlog_debug(2, "keepalive_send: Request sent.");
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Thread sends request to secondary node.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
remote_send_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
2010-11-02 22:13:08 +00:00
|
|
|
time_t lastcheck, now;
|
2010-02-18 23:16:19 +00:00
|
|
|
struct hio *hio;
|
|
|
|
struct nv *nv;
|
|
|
|
unsigned int ncomp;
|
|
|
|
bool wakeup;
|
|
|
|
uint64_t offset, length;
|
|
|
|
uint8_t cmd;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
ncomp = 1;
|
2011-03-22 10:39:34 +00:00
|
|
|
lastcheck = time(NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "remote_send: Taking request.");
|
2011-03-17 21:02:14 +00:00
|
|
|
QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
|
2010-11-02 22:13:08 +00:00
|
|
|
if (hio == NULL) {
|
|
|
|
now = time(NULL);
|
2011-03-17 21:02:14 +00:00
|
|
|
if (lastcheck + HAST_KEEPALIVE <= now) {
|
2010-11-02 22:13:08 +00:00
|
|
|
keepalive_send(res, ncomp);
|
|
|
|
lastcheck = now;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
cmd = HIO_READ;
|
|
|
|
data = NULL;
|
|
|
|
offset = ggio->gctl_offset;
|
|
|
|
length = ggio->gctl_length;
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
cmd = HIO_WRITE;
|
|
|
|
data = ggio->gctl_data;
|
|
|
|
offset = ggio->gctl_offset;
|
|
|
|
length = ggio->gctl_length;
|
|
|
|
break;
|
|
|
|
case BIO_DELETE:
|
|
|
|
cmd = HIO_DELETE;
|
|
|
|
data = NULL;
|
|
|
|
offset = ggio->gctl_offset;
|
|
|
|
length = ggio->gctl_length;
|
|
|
|
break;
|
|
|
|
case BIO_FLUSH:
|
|
|
|
cmd = HIO_FLUSH;
|
|
|
|
data = NULL;
|
|
|
|
offset = 0;
|
|
|
|
length = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-27 07:59:10 +00:00
|
|
|
PJDLOG_ABORT("invalid condition");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
nv = nv_alloc();
|
|
|
|
nv_add_uint8(nv, cmd, "cmd");
|
|
|
|
nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
|
|
|
|
nv_add_uint64(nv, offset, "offset");
|
|
|
|
nv_add_uint64(nv, length, "length");
|
2013-12-10 19:56:26 +00:00
|
|
|
if (ISMEMSYNCWRITE(hio))
|
2013-02-17 21:12:34 +00:00
|
|
|
nv_add_uint8(nv, 1, "memsync");
|
2010-02-18 23:16:19 +00:00
|
|
|
if (nv_error(nv) != 0) {
|
|
|
|
hio->hio_errors[ncomp] = nv_error(nv);
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"remote_send: (%p) Unable to prepare header to send.",
|
|
|
|
hio);
|
|
|
|
reqlog(LOG_ERR, 0, ggio,
|
|
|
|
"Unable to prepare header to send (%s): ",
|
|
|
|
strerror(nv_error(nv)));
|
|
|
|
/* Move failed request immediately to the done queue. */
|
|
|
|
goto done_queue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Protect connection from disappearing.
|
|
|
|
*/
|
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
hio->hio_errors[ncomp] = ENOTCONN;
|
|
|
|
goto done_queue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move the request to recv queue before sending it, because
|
|
|
|
* in different order we can get reply before we move request
|
|
|
|
* to recv queue.
|
|
|
|
*/
|
2011-10-27 18:49:16 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"remote_send: (%p) Moving request to the recv queue.",
|
|
|
|
hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_lock(&hio_recv_list_lock[ncomp]);
|
|
|
|
wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
|
|
|
|
TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size[ncomp]++;
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_unlock(&hio_recv_list_lock[ncomp]);
|
|
|
|
if (hast_proto_send(res, res->hr_remoteout, nv, data,
|
2012-01-10 22:39:07 +00:00
|
|
|
data != NULL ? length : 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = errno;
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"remote_send: (%p) Unable to send request.", hio);
|
|
|
|
reqlog(LOG_ERR, 0, ggio,
|
|
|
|
"Unable to send request (%s): ",
|
|
|
|
strerror(hio->hio_errors[ncomp]));
|
2010-08-29 22:17:53 +00:00
|
|
|
remote_close(res, ncomp);
|
2013-12-10 20:05:07 +00:00
|
|
|
} else {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
nv_free(nv);
|
|
|
|
if (wakeup)
|
|
|
|
cv_signal(&hio_recv_list_cond[ncomp]);
|
|
|
|
continue;
|
|
|
|
done_queue:
|
|
|
|
nv_free(nv);
|
|
|
|
if (ISSYNCREQ(hio)) {
|
2013-02-17 21:12:34 +00:00
|
|
|
if (refcnt_release(&hio->hio_countdown) > 0)
|
2010-02-18 23:16:19 +00:00
|
|
|
continue;
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
SYNCREQDONE(hio);
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
cv_signal(&sync_cond);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ggio->gctl_cmd == BIO_WRITE) {
|
|
|
|
mtx_lock(&res->hr_amp_lock);
|
|
|
|
if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
|
|
|
|
ggio->gctl_length)) {
|
|
|
|
(void)hast_activemap_flush(res);
|
2013-09-19 20:19:08 +00:00
|
|
|
} else {
|
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-12-10 19:56:26 +00:00
|
|
|
if (ISMEMSYNCWRITE(hio)) {
|
|
|
|
if (refcnt_release(&hio->hio_writecount) == 0) {
|
|
|
|
if (hio->hio_errors[0] == 0)
|
|
|
|
write_complete(res, hio);
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
if (refcnt_release(&hio->hio_countdown) > 0)
|
2010-02-18 23:16:19 +00:00
|
|
|
continue;
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"remote_send: (%p) Moving request to the done queue.",
|
|
|
|
hio);
|
|
|
|
QUEUE_INSERT2(hio, done);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread receives answer from secondary node and passes it to ggate_send
|
|
|
|
* thread.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
remote_recv_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
|
|
|
struct hio *hio;
|
|
|
|
struct nv *nv;
|
|
|
|
unsigned int ncomp;
|
|
|
|
uint64_t seq;
|
2013-02-17 21:12:34 +00:00
|
|
|
bool memsyncack;
|
2010-02-18 23:16:19 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
ncomp = 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* Wait until there is anything to receive. */
|
|
|
|
mtx_lock(&hio_recv_list_lock[ncomp]);
|
|
|
|
while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
|
|
|
|
pjdlog_debug(2, "remote_recv: No requests, waiting.");
|
|
|
|
cv_wait(&hio_recv_list_cond[ncomp],
|
|
|
|
&hio_recv_list_lock[ncomp]);
|
|
|
|
}
|
|
|
|
mtx_unlock(&hio_recv_list_lock[ncomp]);
|
2011-10-27 20:15:37 +00:00
|
|
|
|
2013-02-17 21:12:34 +00:00
|
|
|
memsyncack = false;
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
/*
|
|
|
|
* Connection is dead, so move all pending requests to
|
|
|
|
* the done queue (one-by-one).
|
|
|
|
*/
|
|
|
|
mtx_lock(&hio_recv_list_lock[ncomp]);
|
|
|
|
hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(hio != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
|
|
|
|
hio_next[ncomp]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size[ncomp]--;
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_unlock(&hio_recv_list_lock[ncomp]);
|
2013-12-10 19:56:26 +00:00
|
|
|
hio->hio_errors[ncomp] = ENOTCONN;
|
2010-02-18 23:16:19 +00:00
|
|
|
goto done_queue;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to receive reply header");
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
remote_close(res, ncomp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
seq = nv_get_uint64(nv, "seq");
|
|
|
|
if (seq == 0) {
|
|
|
|
pjdlog_error("Header contains no 'seq' field.");
|
|
|
|
nv_free(nv);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
memsyncack = nv_exists(nv, "received");
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_lock(&hio_recv_list_lock[ncomp]);
|
|
|
|
TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
|
|
|
|
if (hio->hio_ggio.gctl_seq == seq) {
|
|
|
|
TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
|
|
|
|
hio_next[ncomp]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size[ncomp]--;
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock(&hio_recv_list_lock[ncomp]);
|
|
|
|
if (hio == NULL) {
|
|
|
|
pjdlog_error("Found no request matching received 'seq' field (%ju).",
|
|
|
|
(uintmax_t)seq);
|
|
|
|
nv_free(nv);
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-27 18:49:16 +00:00
|
|
|
ggio = &hio->hio_ggio;
|
2010-02-18 23:16:19 +00:00
|
|
|
error = nv_get_int16(nv, "error");
|
|
|
|
if (error != 0) {
|
|
|
|
/* Request failed on remote side. */
|
2010-12-16 07:29:58 +00:00
|
|
|
hio->hio_errors[ncomp] = error;
|
2011-10-27 18:49:16 +00:00
|
|
|
reqlog(LOG_WARNING, 0, ggio,
|
2010-12-16 07:30:47 +00:00
|
|
|
"Remote request failed (%s): ", strerror(error));
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_free(nv);
|
|
|
|
goto done_queue;
|
|
|
|
}
|
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
if (!ISCONNECTED(res, ncomp)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
nv_free(nv);
|
|
|
|
goto done_queue;
|
|
|
|
}
|
|
|
|
if (hast_proto_recv_data(res, res->hr_remotein, nv,
|
2012-01-10 22:39:07 +00:00
|
|
|
ggio->gctl_data, ggio->gctl_length) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_errors[ncomp] = errno;
|
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to receive reply data");
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
nv_free(nv);
|
|
|
|
remote_close(res, ncomp);
|
|
|
|
goto done_queue;
|
|
|
|
}
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
case BIO_DELETE:
|
|
|
|
case BIO_FLUSH:
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-27 07:59:10 +00:00
|
|
|
PJDLOG_ABORT("invalid condition");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
hio->hio_errors[ncomp] = 0;
|
|
|
|
nv_free(nv);
|
|
|
|
done_queue:
|
2013-12-10 19:56:26 +00:00
|
|
|
if (ISMEMSYNCWRITE(hio)) {
|
|
|
|
if (!hio->hio_memsyncacked) {
|
|
|
|
PJDLOG_ASSERT(memsyncack ||
|
|
|
|
hio->hio_errors[ncomp] != 0);
|
|
|
|
/* Remote ack arrived. */
|
|
|
|
if (refcnt_release(&hio->hio_writecount) == 0) {
|
2013-02-17 21:12:34 +00:00
|
|
|
if (hio->hio_errors[0] == 0)
|
|
|
|
write_complete(res, hio);
|
2013-12-10 19:56:26 +00:00
|
|
|
}
|
|
|
|
hio->hio_memsyncacked = true;
|
|
|
|
if (hio->hio_errors[ncomp] == 0) {
|
2013-02-17 21:12:34 +00:00
|
|
|
pjdlog_debug(2,
|
2013-12-10 19:56:26 +00:00
|
|
|
"remote_recv: (%p) Moving request "
|
|
|
|
"back to the recv queue.", hio);
|
2013-02-17 21:12:34 +00:00
|
|
|
mtx_lock(&hio_recv_list_lock[ncomp]);
|
|
|
|
TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
|
|
|
|
hio, hio_next[ncomp]);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_recv_list_size[ncomp]++;
|
2013-02-17 21:12:34 +00:00
|
|
|
mtx_unlock(&hio_recv_list_lock[ncomp]);
|
2013-12-10 19:56:26 +00:00
|
|
|
continue;
|
2013-02-17 21:12:34 +00:00
|
|
|
}
|
2013-12-10 19:56:26 +00:00
|
|
|
} else {
|
|
|
|
PJDLOG_ASSERT(!memsyncack);
|
|
|
|
/* Remote final reply arrived. */
|
2013-02-17 21:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-10 19:56:26 +00:00
|
|
|
if (refcnt_release(&hio->hio_countdown) > 0)
|
|
|
|
continue;
|
2011-10-27 20:13:39 +00:00
|
|
|
if (ISSYNCREQ(hio)) {
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
SYNCREQDONE(hio);
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
cv_signal(&sync_cond);
|
|
|
|
} else {
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"remote_recv: (%p) Moving request to the done queue.",
|
|
|
|
hio);
|
|
|
|
QUEUE_INSERT2(hio, done);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread sends answer to the kernel.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
ggate_send_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
|
|
|
struct hio *hio;
|
2011-10-27 20:32:57 +00:00
|
|
|
unsigned int ii, ncomps;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
ncomps = HAST_NCOMPONENTS;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "ggate_send: Taking request.");
|
|
|
|
QUEUE_TAKE2(hio, done);
|
|
|
|
pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
for (ii = 0; ii < ncomps; ii++) {
|
|
|
|
if (hio->hio_errors[ii] == 0) {
|
|
|
|
/*
|
|
|
|
* One successful request is enough to declare
|
|
|
|
* success.
|
|
|
|
*/
|
|
|
|
ggio->gctl_error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ii == ncomps) {
|
|
|
|
/*
|
|
|
|
* None of the requests were successful.
|
2011-03-22 19:49:27 +00:00
|
|
|
* Use the error from local component except the
|
|
|
|
* case when we did only remote request.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2011-03-22 19:49:27 +00:00
|
|
|
if (ggio->gctl_cmd == BIO_READ &&
|
|
|
|
res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
|
|
|
|
ggio->gctl_error = hio->hio_errors[1];
|
|
|
|
else
|
|
|
|
ggio->gctl_error = hio->hio_errors[0];
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
|
|
|
|
mtx_lock(&res->hr_amp_lock);
|
2011-06-28 21:01:32 +00:00
|
|
|
if (activemap_write_complete(res->hr_amp,
|
2011-07-13 05:32:55 +00:00
|
|
|
ggio->gctl_offset, ggio->gctl_length)) {
|
2011-06-28 21:01:32 +00:00
|
|
|
res->hr_stat_activemap_update++;
|
|
|
|
(void)hast_activemap_flush(res);
|
2013-09-19 20:19:08 +00:00
|
|
|
} else {
|
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
2011-06-28 21:01:32 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
if (ggio->gctl_cmd == BIO_WRITE) {
|
|
|
|
/*
|
|
|
|
* Unlock range we locked.
|
|
|
|
*/
|
|
|
|
mtx_lock(&range_lock);
|
|
|
|
rangelock_del(range_regular, ggio->gctl_offset,
|
|
|
|
ggio->gctl_length);
|
|
|
|
if (range_sync_wait)
|
|
|
|
cv_signal(&range_sync_cond);
|
|
|
|
mtx_unlock(&range_lock);
|
2011-10-27 20:32:57 +00:00
|
|
|
if (!hio->hio_done)
|
|
|
|
write_complete(res, hio);
|
|
|
|
} else {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
|
2011-10-27 20:32:57 +00:00
|
|
|
primary_exit(EX_OSERR,
|
|
|
|
"G_GATE_CMD_DONE failed");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-25 20:09:07 +00:00
|
|
|
if (hio->hio_errors[0]) {
|
|
|
|
switch (ggio->gctl_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
res->hr_stat_read_error++;
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
res->hr_stat_write_error++;
|
|
|
|
break;
|
|
|
|
case BIO_DELETE:
|
|
|
|
res->hr_stat_delete_error++;
|
|
|
|
break;
|
|
|
|
case BIO_FLUSH:
|
|
|
|
res->hr_stat_flush_error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"ggate_send: (%p) Moving request to the free queue.", hio);
|
|
|
|
QUEUE_INSERT2(hio, free);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread synchronize local and remote components.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
sync_thread(void *arg __unused)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct hio *hio;
|
|
|
|
struct g_gate_ctl_io *ggio;
|
2011-03-07 10:41:12 +00:00
|
|
|
struct timeval tstart, tend, tdiff;
|
2010-02-18 23:16:19 +00:00
|
|
|
unsigned int ii, ncomp, ncomps;
|
|
|
|
off_t offset, length, synced;
|
2012-07-04 20:20:48 +00:00
|
|
|
bool dorewind, directreads;
|
2010-02-18 23:16:19 +00:00
|
|
|
int syncext;
|
|
|
|
|
|
|
|
ncomps = HAST_NCOMPONENTS;
|
|
|
|
dorewind = true;
|
2010-08-27 21:20:32 +00:00
|
|
|
synced = 0;
|
|
|
|
offset = -1;
|
2012-07-04 20:20:48 +00:00
|
|
|
directreads = false;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mtx_lock(&sync_lock);
|
2010-08-27 21:20:32 +00:00
|
|
|
if (offset >= 0 && !sync_inprogress) {
|
2011-03-07 10:41:12 +00:00
|
|
|
gettimeofday(&tend, NULL);
|
|
|
|
timersub(&tend, &tstart, &tdiff);
|
|
|
|
pjdlog_info("Synchronization interrupted after %#.0T. "
|
|
|
|
"%NB synchronized so far.", &tdiff,
|
2010-08-27 14:08:10 +00:00
|
|
|
(intmax_t)synced);
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_SYNCINTR);
|
2010-08-27 14:08:10 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
while (!sync_inprogress) {
|
|
|
|
dorewind = true;
|
|
|
|
synced = 0;
|
|
|
|
cv_wait(&sync_cond, &sync_lock);
|
|
|
|
}
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
/*
|
|
|
|
* Obtain offset at which we should synchronize.
|
|
|
|
* Rewind synchronization if needed.
|
|
|
|
*/
|
|
|
|
mtx_lock(&res->hr_amp_lock);
|
|
|
|
if (dorewind)
|
|
|
|
activemap_sync_rewind(res->hr_amp);
|
|
|
|
offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
|
|
|
|
if (syncext != -1) {
|
|
|
|
/*
|
|
|
|
* We synchronized entire syncext extent, we can mark
|
|
|
|
* it as clean now.
|
|
|
|
*/
|
|
|
|
if (activemap_extent_complete(res->hr_amp, syncext))
|
|
|
|
(void)hast_activemap_flush(res);
|
2013-09-19 20:19:08 +00:00
|
|
|
else
|
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
|
|
|
} else {
|
|
|
|
mtx_unlock(&res->hr_amp_lock);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
if (dorewind) {
|
|
|
|
dorewind = false;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (offset == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_info("Nodes are in sync.");
|
|
|
|
else {
|
2011-03-07 10:41:12 +00:00
|
|
|
pjdlog_info("Synchronization started. %NB to go.",
|
|
|
|
(intmax_t)(res->hr_extentsize *
|
2010-02-18 23:16:19 +00:00
|
|
|
activemap_ndirty(res->hr_amp)));
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_SYNCSTART);
|
2011-03-07 10:41:12 +00:00
|
|
|
gettimeofday(&tstart, NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (offset == -1) {
|
2010-08-27 14:06:00 +00:00
|
|
|
sync_stop();
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1, "Nothing to synchronize.");
|
|
|
|
/*
|
|
|
|
* Synchronization complete, make both localcnt and
|
|
|
|
* remotecnt equal.
|
|
|
|
*/
|
|
|
|
ncomp = 1;
|
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
if (ISCONNECTED(res, ncomp)) {
|
|
|
|
if (synced > 0) {
|
2011-03-07 10:41:12 +00:00
|
|
|
int64_t bps;
|
|
|
|
|
|
|
|
gettimeofday(&tend, NULL);
|
|
|
|
timersub(&tend, &tstart, &tdiff);
|
|
|
|
bps = (int64_t)((double)synced /
|
|
|
|
((double)tdiff.tv_sec +
|
|
|
|
(double)tdiff.tv_usec / 1000000));
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_info("Synchronization complete. "
|
2011-03-07 10:41:12 +00:00
|
|
|
"%NB synchronized in %#.0lT (%NB/sec).",
|
|
|
|
(intmax_t)synced, &tdiff,
|
|
|
|
(intmax_t)bps);
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_SYNCDONE);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
mtx_lock(&metadata_lock);
|
2012-07-04 20:20:48 +00:00
|
|
|
if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
|
|
|
|
directreads = true;
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
|
|
|
|
res->hr_primary_localcnt =
|
|
|
|
res->hr_secondary_remotecnt;
|
2011-03-22 20:27:26 +00:00
|
|
|
res->hr_primary_remotecnt =
|
|
|
|
res->hr_secondary_localcnt;
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1,
|
|
|
|
"Setting localcnt to %ju and remotecnt to %ju.",
|
|
|
|
(uintmax_t)res->hr_primary_localcnt,
|
2011-03-22 20:27:26 +00:00
|
|
|
(uintmax_t)res->hr_primary_remotecnt);
|
2010-02-18 23:16:19 +00:00
|
|
|
(void)metadata_write(res);
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
}
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
2012-07-04 20:20:48 +00:00
|
|
|
if (directreads) {
|
|
|
|
directreads = false;
|
|
|
|
enable_direct_reads(res);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pjdlog_debug(2, "sync: Taking free request.");
|
|
|
|
QUEUE_TAKE2(hio, free);
|
|
|
|
pjdlog_debug(2, "sync: (%p) Got free request.", hio);
|
|
|
|
/*
|
|
|
|
* Lock the range we are going to synchronize. We don't want
|
|
|
|
* race where someone writes between our read and write.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
mtx_lock(&range_lock);
|
|
|
|
if (rangelock_islocked(range_regular, offset, length)) {
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"sync: Range offset=%jd length=%jd locked.",
|
|
|
|
(intmax_t)offset, (intmax_t)length);
|
|
|
|
range_sync_wait = true;
|
|
|
|
cv_wait(&range_sync_cond, &range_lock);
|
|
|
|
range_sync_wait = false;
|
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (rangelock_add(range_sync, offset, length) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"sync: Range offset=%jd length=%jd is already locked, waiting.",
|
|
|
|
(intmax_t)offset, (intmax_t)length);
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* First read the data from synchronization source.
|
|
|
|
*/
|
|
|
|
SYNCREQ(hio);
|
|
|
|
ggio = &hio->hio_ggio;
|
|
|
|
ggio->gctl_cmd = BIO_READ;
|
|
|
|
ggio->gctl_offset = offset;
|
|
|
|
ggio->gctl_length = length;
|
|
|
|
ggio->gctl_error = 0;
|
2011-10-27 20:32:57 +00:00
|
|
|
hio->hio_done = false;
|
|
|
|
hio->hio_replication = res->hr_replication;
|
2010-02-18 23:16:19 +00:00
|
|
|
for (ii = 0; ii < ncomps; ii++)
|
|
|
|
hio->hio_errors[ii] = EINVAL;
|
|
|
|
reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
|
|
|
|
hio);
|
|
|
|
pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
|
|
|
|
hio);
|
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
|
|
|
|
/*
|
|
|
|
* This range is up-to-date on local component,
|
|
|
|
* so handle request locally.
|
|
|
|
*/
|
|
|
|
/* Local component is 0 for now. */
|
|
|
|
ncomp = 0;
|
|
|
|
} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* This range is out-of-date on local component,
|
|
|
|
* so send request to the remote node.
|
|
|
|
*/
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
ncomp = 1;
|
|
|
|
}
|
|
|
|
mtx_unlock(&metadata_lock);
|
2013-04-27 05:01:29 +00:00
|
|
|
refcnt_init(&hio->hio_countdown, 1);
|
2010-02-18 23:16:19 +00:00
|
|
|
QUEUE_INSERT1(hio, send, ncomp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let's wait for READ to finish.
|
|
|
|
*/
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
while (!ISSYNCREQDONE(hio))
|
|
|
|
cv_wait(&sync_cond, &sync_lock);
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
|
|
|
|
if (hio->hio_errors[ncomp] != 0) {
|
|
|
|
pjdlog_error("Unable to read synchronization data: %s.",
|
|
|
|
strerror(hio->hio_errors[ncomp]));
|
|
|
|
goto free_queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We read the data from synchronization source, now write it
|
|
|
|
* to synchronization target.
|
|
|
|
*/
|
|
|
|
SYNCREQ(hio);
|
|
|
|
ggio->gctl_cmd = BIO_WRITE;
|
|
|
|
for (ii = 0; ii < ncomps; ii++)
|
|
|
|
hio->hio_errors[ii] = EINVAL;
|
|
|
|
reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
|
|
|
|
hio);
|
|
|
|
pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
|
|
|
|
hio);
|
|
|
|
mtx_lock(&metadata_lock);
|
|
|
|
if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
|
|
|
|
/*
|
|
|
|
* This range is up-to-date on local component,
|
|
|
|
* so we update remote component.
|
|
|
|
*/
|
|
|
|
/* Remote component is 1 for now. */
|
|
|
|
ncomp = 1;
|
|
|
|
} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* This range is out-of-date on local component,
|
|
|
|
* so we update it.
|
|
|
|
*/
|
|
|
|
/* Local component is 0 for now. */
|
|
|
|
ncomp = 0;
|
|
|
|
}
|
|
|
|
mtx_unlock(&metadata_lock);
|
|
|
|
|
2011-10-27 20:15:37 +00:00
|
|
|
pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
|
2010-02-18 23:16:19 +00:00
|
|
|
hio);
|
2013-04-27 05:01:29 +00:00
|
|
|
refcnt_init(&hio->hio_countdown, 1);
|
2010-02-18 23:16:19 +00:00
|
|
|
QUEUE_INSERT1(hio, send, ncomp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let's wait for WRITE to finish.
|
|
|
|
*/
|
|
|
|
mtx_lock(&sync_lock);
|
|
|
|
while (!ISSYNCREQDONE(hio))
|
|
|
|
cv_wait(&sync_cond, &sync_lock);
|
|
|
|
mtx_unlock(&sync_lock);
|
|
|
|
|
|
|
|
if (hio->hio_errors[ncomp] != 0) {
|
|
|
|
pjdlog_error("Unable to write synchronization data: %s.",
|
|
|
|
strerror(hio->hio_errors[ncomp]));
|
|
|
|
goto free_queue;
|
|
|
|
}
|
2010-08-27 14:10:25 +00:00
|
|
|
|
|
|
|
synced += length;
|
2010-02-18 23:16:19 +00:00
|
|
|
free_queue:
|
|
|
|
mtx_lock(&range_lock);
|
|
|
|
rangelock_del(range_sync, offset, length);
|
|
|
|
if (range_regular_wait)
|
|
|
|
cv_signal(&range_regular_cond);
|
|
|
|
mtx_unlock(&range_lock);
|
|
|
|
pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
|
|
|
|
hio);
|
|
|
|
QUEUE_INSERT2(hio, free);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2011-01-24 15:04:15 +00:00
|
|
|
void
|
|
|
|
primary_config_reload(struct hast_resource *res, struct nv *nv)
|
2010-08-05 19:16:31 +00:00
|
|
|
{
|
|
|
|
unsigned int ii, ncomps;
|
2011-01-24 15:04:15 +00:00
|
|
|
int modified, vint;
|
|
|
|
const char *vstr;
|
2010-08-05 19:16:31 +00:00
|
|
|
|
|
|
|
pjdlog_info("Reloading configuration...");
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
|
|
|
|
PJDLOG_ASSERT(gres == res);
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_assert(nv, "remoteaddr");
|
2011-03-21 08:54:59 +00:00
|
|
|
nv_assert(nv, "sourceaddr");
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_assert(nv, "replication");
|
2011-03-06 22:56:14 +00:00
|
|
|
nv_assert(nv, "checksum");
|
2011-03-06 23:09:33 +00:00
|
|
|
nv_assert(nv, "compression");
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_assert(nv, "timeout");
|
|
|
|
nv_assert(nv, "exec");
|
2011-09-28 13:08:51 +00:00
|
|
|
nv_assert(nv, "metaflush");
|
2010-08-05 19:16:31 +00:00
|
|
|
|
2011-01-24 15:04:15 +00:00
|
|
|
ncomps = HAST_NCOMPONENTS;
|
2010-08-05 19:16:31 +00:00
|
|
|
|
2011-03-06 22:56:14 +00:00
|
|
|
#define MODIFIED_REMOTEADDR 0x01
|
2011-03-21 08:54:59 +00:00
|
|
|
#define MODIFIED_SOURCEADDR 0x02
|
|
|
|
#define MODIFIED_REPLICATION 0x04
|
|
|
|
#define MODIFIED_CHECKSUM 0x08
|
|
|
|
#define MODIFIED_COMPRESSION 0x10
|
|
|
|
#define MODIFIED_TIMEOUT 0x20
|
|
|
|
#define MODIFIED_EXEC 0x40
|
2011-09-28 13:08:51 +00:00
|
|
|
#define MODIFIED_METAFLUSH 0x80
|
2010-08-05 19:16:31 +00:00
|
|
|
modified = 0;
|
2011-01-24 15:04:15 +00:00
|
|
|
|
|
|
|
vstr = nv_get_string(nv, "remoteaddr");
|
|
|
|
if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
|
2010-08-05 19:16:31 +00:00
|
|
|
/*
|
|
|
|
* Don't copy res->hr_remoteaddr to gres just yet.
|
|
|
|
* We want remote_close() to log disconnect from the old
|
|
|
|
* addresses, not from the new ones.
|
|
|
|
*/
|
|
|
|
modified |= MODIFIED_REMOTEADDR;
|
|
|
|
}
|
2011-03-21 08:54:59 +00:00
|
|
|
vstr = nv_get_string(nv, "sourceaddr");
|
|
|
|
if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
|
|
|
|
strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
|
|
|
|
modified |= MODIFIED_SOURCEADDR;
|
|
|
|
}
|
2011-01-24 15:04:15 +00:00
|
|
|
vint = nv_get_int32(nv, "replication");
|
|
|
|
if (gres->hr_replication != vint) {
|
|
|
|
gres->hr_replication = vint;
|
2010-08-05 19:16:31 +00:00
|
|
|
modified |= MODIFIED_REPLICATION;
|
|
|
|
}
|
2011-03-06 22:56:14 +00:00
|
|
|
vint = nv_get_int32(nv, "checksum");
|
|
|
|
if (gres->hr_checksum != vint) {
|
|
|
|
gres->hr_checksum = vint;
|
|
|
|
modified |= MODIFIED_CHECKSUM;
|
|
|
|
}
|
2011-03-06 23:09:33 +00:00
|
|
|
vint = nv_get_int32(nv, "compression");
|
|
|
|
if (gres->hr_compression != vint) {
|
|
|
|
gres->hr_compression = vint;
|
|
|
|
modified |= MODIFIED_COMPRESSION;
|
|
|
|
}
|
2011-01-24 15:04:15 +00:00
|
|
|
vint = nv_get_int32(nv, "timeout");
|
|
|
|
if (gres->hr_timeout != vint) {
|
|
|
|
gres->hr_timeout = vint;
|
2010-08-05 19:16:31 +00:00
|
|
|
modified |= MODIFIED_TIMEOUT;
|
|
|
|
}
|
2011-01-24 15:04:15 +00:00
|
|
|
vstr = nv_get_string(nv, "exec");
|
|
|
|
if (strcmp(gres->hr_exec, vstr) != 0) {
|
|
|
|
strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
|
2010-08-27 15:16:52 +00:00
|
|
|
modified |= MODIFIED_EXEC;
|
|
|
|
}
|
2011-09-28 13:08:51 +00:00
|
|
|
vint = nv_get_int32(nv, "metaflush");
|
|
|
|
if (gres->hr_metaflush != vint) {
|
|
|
|
gres->hr_metaflush = vint;
|
|
|
|
modified |= MODIFIED_METAFLUSH;
|
|
|
|
}
|
2011-01-24 15:04:15 +00:00
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
/*
|
2011-03-06 22:56:14 +00:00
|
|
|
* Change timeout for connected sockets.
|
|
|
|
* Don't bother if we need to reconnect.
|
2010-08-05 19:16:31 +00:00
|
|
|
*/
|
2011-03-06 22:56:14 +00:00
|
|
|
if ((modified & MODIFIED_TIMEOUT) != 0 &&
|
2011-10-27 20:32:57 +00:00
|
|
|
(modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
|
2010-08-05 19:16:31 +00:00
|
|
|
for (ii = 0; ii < ncomps; ii++) {
|
|
|
|
if (!ISREMOTE(ii))
|
|
|
|
continue;
|
|
|
|
rw_rlock(&hio_remote_lock[ii]);
|
|
|
|
if (!ISCONNECTED(gres, ii)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ii]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rw_unlock(&hio_remote_lock[ii]);
|
|
|
|
if (proto_timeout(gres->hr_remotein,
|
2012-01-10 22:39:07 +00:00
|
|
|
gres->hr_timeout) == -1) {
|
2010-08-05 19:16:31 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to set connection timeout");
|
|
|
|
}
|
|
|
|
if (proto_timeout(gres->hr_remoteout,
|
2012-01-10 22:39:07 +00:00
|
|
|
gres->hr_timeout) == -1) {
|
2010-08-05 19:16:31 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to set connection timeout");
|
|
|
|
}
|
|
|
|
}
|
2011-03-06 22:56:14 +00:00
|
|
|
}
|
2011-10-27 20:32:57 +00:00
|
|
|
if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
|
2010-08-05 19:16:31 +00:00
|
|
|
for (ii = 0; ii < ncomps; ii++) {
|
|
|
|
if (!ISREMOTE(ii))
|
|
|
|
continue;
|
|
|
|
remote_close(gres, ii);
|
|
|
|
}
|
|
|
|
if (modified & MODIFIED_REMOTEADDR) {
|
2011-01-24 15:04:15 +00:00
|
|
|
vstr = nv_get_string(nv, "remoteaddr");
|
|
|
|
strlcpy(gres->hr_remoteaddr, vstr,
|
2010-08-05 19:16:31 +00:00
|
|
|
sizeof(gres->hr_remoteaddr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef MODIFIED_REMOTEADDR
|
2011-03-21 08:54:59 +00:00
|
|
|
#undef MODIFIED_SOURCEADDR
|
2010-08-05 19:16:31 +00:00
|
|
|
#undef MODIFIED_REPLICATION
|
2011-03-06 22:56:14 +00:00
|
|
|
#undef MODIFIED_CHECKSUM
|
2011-03-06 23:09:33 +00:00
|
|
|
#undef MODIFIED_COMPRESSION
|
2010-08-05 19:16:31 +00:00
|
|
|
#undef MODIFIED_TIMEOUT
|
2010-08-27 15:16:52 +00:00
|
|
|
#undef MODIFIED_EXEC
|
2011-09-28 13:08:51 +00:00
|
|
|
#undef MODIFIED_METAFLUSH
|
2010-08-05 19:16:31 +00:00
|
|
|
|
|
|
|
pjdlog_info("Configuration reloaded successfully.");
|
|
|
|
}
|
|
|
|
|
2010-08-29 22:55:21 +00:00
|
|
|
static void
|
|
|
|
guard_one(struct hast_resource *res, unsigned int ncomp)
|
|
|
|
{
|
|
|
|
struct proto_conn *in, *out;
|
|
|
|
|
|
|
|
if (!ISREMOTE(ncomp))
|
|
|
|
return;
|
|
|
|
|
|
|
|
rw_rlock(&hio_remote_lock[ncomp]);
|
|
|
|
|
|
|
|
if (!real_remote(res)) {
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ISCONNECTED(res, ncomp)) {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein != NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout != NULL);
|
2010-08-29 22:55:21 +00:00
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein == NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout == NULL);
|
2010-08-29 22:55:21 +00:00
|
|
|
/*
|
|
|
|
* Upgrade the lock. It doesn't have to be atomic as no other thread
|
|
|
|
* can change connection status from disconnected to connected.
|
|
|
|
*/
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
in = out = NULL;
|
2011-04-20 18:43:28 +00:00
|
|
|
if (init_remote(res, &in, &out) == 0) {
|
2010-08-29 22:55:21 +00:00
|
|
|
rw_wlock(&hio_remote_lock[ncomp]);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein == NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout == NULL);
|
|
|
|
PJDLOG_ASSERT(in != NULL && out != NULL);
|
2010-08-29 22:55:21 +00:00
|
|
|
res->hr_remotein = in;
|
|
|
|
res->hr_remoteout = out;
|
|
|
|
rw_unlock(&hio_remote_lock[ncomp]);
|
|
|
|
pjdlog_info("Successfully reconnected to %s.",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
sync_start();
|
|
|
|
} else {
|
|
|
|
/* Both connections should be NULL. */
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein == NULL);
|
|
|
|
PJDLOG_ASSERT(res->hr_remoteout == NULL);
|
|
|
|
PJDLOG_ASSERT(in == NULL && out == NULL);
|
2010-08-29 22:55:21 +00:00
|
|
|
pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Thread guards remote connections and reconnects when needed, handles
|
|
|
|
* signals, etc.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
guard_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
unsigned int ii, ncomps;
|
2010-08-30 00:06:05 +00:00
|
|
|
struct timespec timeout;
|
2010-08-29 22:55:21 +00:00
|
|
|
time_t lastcheck, now;
|
2010-08-30 00:06:05 +00:00
|
|
|
sigset_t mask;
|
|
|
|
int signo;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
ncomps = HAST_NCOMPONENTS;
|
2010-08-29 22:55:21 +00:00
|
|
|
lastcheck = time(NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-30 00:06:05 +00:00
|
|
|
PJDLOG_VERIFY(sigemptyset(&mask) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
|
|
|
|
|
2011-03-17 21:02:14 +00:00
|
|
|
timeout.tv_sec = HAST_KEEPALIVE;
|
2010-08-30 00:06:05 +00:00
|
|
|
timeout.tv_nsec = 0;
|
|
|
|
signo = -1;
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
for (;;) {
|
2010-08-30 00:06:05 +00:00
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
sigexit_received = true;
|
2010-02-18 23:16:19 +00:00
|
|
|
primary_exitx(EX_OK,
|
|
|
|
"Termination signal received, exiting.");
|
2010-08-30 00:06:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2010-08-27 14:26:37 +00:00
|
|
|
|
2011-04-20 18:43:28 +00:00
|
|
|
/*
|
|
|
|
* Don't check connections until we fully started,
|
|
|
|
* as we may still be looping, waiting for remote node
|
|
|
|
* to switch from primary to secondary.
|
|
|
|
*/
|
|
|
|
if (fullystarted) {
|
|
|
|
pjdlog_debug(2, "remote_guard: Checking connections.");
|
|
|
|
now = time(NULL);
|
|
|
|
if (lastcheck + HAST_KEEPALIVE <= now) {
|
|
|
|
for (ii = 0; ii < ncomps; ii++)
|
|
|
|
guard_one(res, ii);
|
|
|
|
lastcheck = now;
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2010-08-30 00:06:05 +00:00
|
|
|
signo = sigtimedwait(&mask, NULL, &timeout);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|