2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009-2010 The FreeBSD Foundation
|
2010-08-27 14:01:28 +00:00
|
|
|
* Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
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/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgeom.h>
|
|
|
|
#include <pthread.h>
|
2010-09-22 19:08:11 +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 <pjdlog.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-29 21:41:53 +00:00
|
|
|
#include "hooks.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "metadata.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "subr.h"
|
|
|
|
#include "synch.h"
|
|
|
|
|
|
|
|
struct hio {
|
2011-03-22 10:39:34 +00:00
|
|
|
uint64_t hio_seq;
|
|
|
|
int hio_error;
|
2010-02-18 23:16:19 +00:00
|
|
|
void *hio_data;
|
|
|
|
uint8_t hio_cmd;
|
|
|
|
uint64_t hio_offset;
|
|
|
|
uint64_t hio_length;
|
2013-02-17 21:12:34 +00:00
|
|
|
bool hio_memsync;
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_ENTRY(hio) hio_next;
|
|
|
|
};
|
|
|
|
|
2010-08-30 00:31:30 +00:00
|
|
|
static struct hast_resource *gres;
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
/*
|
2013-09-19 20:20:59 +00:00
|
|
|
* Disk thread (the one that does I/O requests) takes requests from this list.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
static TAILQ_HEAD(, hio) hio_disk_list;
|
2013-10-26 08:38:21 +00:00
|
|
|
static size_t hio_disk_list_size;
|
2010-02-18 23:16:19 +00:00
|
|
|
static pthread_mutex_t hio_disk_list_lock;
|
|
|
|
static pthread_cond_t hio_disk_list_cond;
|
|
|
|
/*
|
2013-09-19 20:20:59 +00:00
|
|
|
* Thread that sends requests back to primary takes requests from this list.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum number of outstanding I/O requests.
|
|
|
|
*/
|
|
|
|
#define HAST_HIO_MAX 256
|
|
|
|
|
|
|
|
static void *recv_thread(void *arg);
|
|
|
|
static void *disk_thread(void *arg);
|
|
|
|
static void *send_thread(void *arg);
|
|
|
|
|
2010-08-27 14:01:28 +00:00
|
|
|
#define QUEUE_INSERT(name, hio) 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-08-27 14:01:28 +00:00
|
|
|
TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_##name##_list_size++; \
|
2010-08-27 14:01:28 +00:00
|
|
|
mtx_unlock(&hio_##name##_list_lock); \
|
|
|
|
} while (0)
|
|
|
|
#define QUEUE_TAKE(name, hio) 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-08-27 14:01:28 +00:00
|
|
|
TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next); \
|
|
|
|
mtx_unlock(&hio_##name##_list_lock); \
|
|
|
|
} while (0)
|
2011-10-27 20:36:35 +00:00
|
|
|
|
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_disk_list_size, "local_queue_size");
|
|
|
|
nv_add_uint64(nvout, (uint64_t)hio_send_list_size, "send_queue_size");
|
|
|
|
}
|
|
|
|
|
2011-10-27 20:01:23 +00:00
|
|
|
static void
|
|
|
|
hio_clear(struct hio *hio)
|
|
|
|
{
|
|
|
|
|
|
|
|
hio->hio_seq = 0;
|
|
|
|
hio->hio_error = 0;
|
|
|
|
hio->hio_cmd = HIO_UNDEF;
|
|
|
|
hio->hio_offset = 0;
|
|
|
|
hio->hio_length = 0;
|
2013-02-17 21:12:34 +00:00
|
|
|
hio->hio_memsync = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hio_copy(const struct hio *srchio, struct hio *dsthio)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't copy hio_error, hio_data and hio_next fields.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dsthio->hio_seq = srchio->hio_seq;
|
|
|
|
dsthio->hio_cmd = srchio->hio_cmd;
|
|
|
|
dsthio->hio_offset = srchio->hio_offset;
|
|
|
|
dsthio->hio_length = srchio->hio_length;
|
|
|
|
dsthio->hio_memsync = srchio->hio_memsync;
|
2011-10-27 20:01:23 +00:00
|
|
|
}
|
2010-08-27 14:01:28 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
init_environment(void)
|
|
|
|
{
|
|
|
|
struct hio *hio;
|
|
|
|
unsigned int ii;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize lists, their locks and theirs condition variables.
|
|
|
|
*/
|
|
|
|
TAILQ_INIT(&hio_free_list);
|
|
|
|
mtx_init(&hio_free_list_lock);
|
|
|
|
cv_init(&hio_free_list_cond);
|
|
|
|
TAILQ_INIT(&hio_disk_list);
|
|
|
|
mtx_init(&hio_disk_list_lock);
|
|
|
|
cv_init(&hio_disk_list_cond);
|
|
|
|
TAILQ_INIT(&hio_send_list);
|
|
|
|
mtx_init(&hio_send_list_lock);
|
|
|
|
cv_init(&hio_send_list_cond);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate requests pool and initialize requests.
|
|
|
|
*/
|
|
|
|
for (ii = 0; ii < HAST_HIO_MAX; ii++) {
|
|
|
|
hio = malloc(sizeof(*hio));
|
|
|
|
if (hio == NULL) {
|
2010-08-05 18:56:24 +00:00
|
|
|
pjdlog_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate memory (%zu bytes) for hio request.",
|
|
|
|
sizeof(*hio));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
hio->hio_data = malloc(MAXPHYS);
|
|
|
|
if (hio->hio_data == NULL) {
|
2010-08-05 18:56:24 +00:00
|
|
|
pjdlog_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate memory (%zu bytes) for gctl_data.",
|
|
|
|
(size_t)MAXPHYS);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2011-10-27 20:01:23 +00:00
|
|
|
hio_clear(hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
|
2013-10-26 08:38:21 +00:00
|
|
|
hio_free_list_size++;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_local(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_remote(struct hast_resource *res, struct nv *nvin)
|
|
|
|
{
|
|
|
|
uint64_t resuid;
|
|
|
|
struct nv *nvout;
|
|
|
|
unsigned char *map;
|
|
|
|
size_t mapsize;
|
|
|
|
|
2011-06-17 07:07:26 +00:00
|
|
|
#ifdef notyet
|
2011-04-02 09:25:13 +00:00
|
|
|
/* Setup direction. */
|
|
|
|
if (proto_send(res->hr_remoteout, NULL, 0) == -1)
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
|
2011-06-17 07:07:26 +00:00
|
|
|
#endif
|
2011-04-02 09:25:13 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
nvout = nv_alloc();
|
|
|
|
nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
|
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
|
|
|
|
resuid = nv_get_uint64(nvin, "resuid");
|
|
|
|
res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
|
|
|
|
res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
|
|
|
|
nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
|
|
|
|
nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
|
|
|
|
mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
|
|
|
|
METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
|
|
|
|
map = malloc(mapsize);
|
|
|
|
if (map == NULL) {
|
|
|
|
pjdlog_exitx(EX_TEMPFAIL,
|
|
|
|
"Unable to allocate memory (%zu bytes) for activemap.",
|
|
|
|
mapsize);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* When we work as primary and secondary is missing we will increase
|
|
|
|
* localcnt in our metadata. When secondary is connected and synced
|
|
|
|
* we make localcnt be equal to remotecnt, which means nodes are more
|
|
|
|
* or less in sync.
|
|
|
|
* Split-brain condition is when both nodes are not able to communicate
|
|
|
|
* and are both configured as primary nodes. In turn, they can both
|
|
|
|
* make incompatible changes to the data and we have to detect that.
|
|
|
|
* Under split-brain condition we will increase our localcnt on first
|
|
|
|
* write and remote node will increase its localcnt on first write.
|
|
|
|
* When we connect we can see that primary's localcnt is greater than
|
|
|
|
* our remotecnt (primary was modified while we weren't watching) and
|
|
|
|
* our localcnt is greater than primary's remotecnt (we were modified
|
|
|
|
* while primary wasn't watching).
|
|
|
|
* There are many possible combinations which are all gathered below.
|
|
|
|
* Don't pay too much attention to exact numbers, the more important
|
|
|
|
* is to compare them. We compare secondary's local with primary's
|
|
|
|
* remote and secondary's remote with primary's local.
|
|
|
|
* Note that every case where primary's localcnt is smaller than
|
|
|
|
* secondary's remotecnt and where secondary's localcnt is smaller than
|
|
|
|
* primary's remotecnt should be impossible in practise. We will perform
|
|
|
|
* full synchronization then. Those cases are marked with an asterisk.
|
|
|
|
* Regular synchronization means that only extents marked as dirty are
|
|
|
|
* synchronized (regular synchronization).
|
|
|
|
*
|
|
|
|
* SECONDARY METADATA PRIMARY METADATA
|
|
|
|
* local=3 remote=3 local=2 remote=2* ?! Full sync from secondary.
|
|
|
|
* local=3 remote=3 local=2 remote=3* ?! Full sync from primary.
|
|
|
|
* local=3 remote=3 local=2 remote=4* ?! Full sync from primary.
|
|
|
|
* local=3 remote=3 local=3 remote=2 Primary is out-of-date,
|
|
|
|
* regular sync from secondary.
|
|
|
|
* local=3 remote=3 local=3 remote=3 Regular sync just in case.
|
|
|
|
* local=3 remote=3 local=3 remote=4* ?! Full sync from primary.
|
|
|
|
* local=3 remote=3 local=4 remote=2 Split-brain condition.
|
|
|
|
* local=3 remote=3 local=4 remote=3 Secondary out-of-date,
|
|
|
|
* regular sync from primary.
|
|
|
|
* local=3 remote=3 local=4 remote=4* ?! Full sync from primary.
|
|
|
|
*/
|
|
|
|
if (res->hr_resuid == 0) {
|
|
|
|
/*
|
2010-10-24 17:28:25 +00:00
|
|
|
* Provider is used for the first time. If primary node done no
|
|
|
|
* writes yet as well (we will find "virgin" argument) then
|
|
|
|
* there is no need to synchronize anything. If primary node
|
|
|
|
* done any writes already we have to synchronize everything.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_resuid = resuid;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (metadata_write(res) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
exit(EX_NOINPUT);
|
2010-10-24 17:28:25 +00:00
|
|
|
if (nv_exists(nvin, "virgin")) {
|
|
|
|
free(map);
|
|
|
|
map = NULL;
|
|
|
|
mapsize = 0;
|
|
|
|
} else {
|
|
|
|
memset(map, 0xff, mapsize);
|
|
|
|
}
|
2011-04-19 19:26:27 +00:00
|
|
|
nv_add_int8(nvout, 1, "virgin");
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
|
2011-03-21 14:50:12 +00:00
|
|
|
} else if (res->hr_resuid != resuid) {
|
|
|
|
char errmsg[256];
|
|
|
|
|
2011-10-27 20:01:23 +00:00
|
|
|
free(map);
|
2011-03-21 14:50:12 +00:00
|
|
|
(void)snprintf(errmsg, sizeof(errmsg),
|
|
|
|
"Resource unique ID mismatch (primary=%ju, secondary=%ju).",
|
|
|
|
(uintmax_t)resuid, (uintmax_t)res->hr_resuid);
|
|
|
|
pjdlog_error("%s", errmsg);
|
|
|
|
nv_add_string(nvout, errmsg, "errmsg");
|
2012-01-13 23:25:35 +00:00
|
|
|
if (hast_proto_send(res, res->hr_remotein, nvout,
|
|
|
|
NULL, 0) == -1) {
|
|
|
|
pjdlog_exit(EX_TEMPFAIL,
|
|
|
|
"Unable to send response to %s",
|
2011-03-21 14:50:12 +00:00
|
|
|
res->hr_remoteaddr);
|
|
|
|
}
|
2011-03-21 14:51:16 +00:00
|
|
|
nv_free(nvout);
|
2011-03-21 14:50:12 +00:00
|
|
|
exit(EX_CONFIG);
|
2010-02-18 23:16:19 +00:00
|
|
|
} else if (
|
2011-10-27 16:22:17 +00:00
|
|
|
/* Is primary out-of-date? */
|
2010-02-18 23:16:19 +00:00
|
|
|
(res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
|
|
|
|
res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
|
2011-10-27 16:22:17 +00:00
|
|
|
/* Are the nodes more or less in sync? */
|
2010-02-18 23:16:19 +00:00
|
|
|
(res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
|
|
|
|
res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
|
2011-10-27 16:22:17 +00:00
|
|
|
/* Is secondary out-of-date? */
|
2010-02-18 23:16:19 +00:00
|
|
|
(res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
|
|
|
|
res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
|
|
|
|
/*
|
|
|
|
* Nodes are more or less in sync or one of the nodes is
|
|
|
|
* out-of-date.
|
|
|
|
* It doesn't matter at this point which one, we just have to
|
|
|
|
* send out local bitmap to the remote node.
|
|
|
|
*/
|
|
|
|
if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
|
|
|
|
(ssize_t)mapsize) {
|
|
|
|
pjdlog_exit(LOG_ERR, "Unable to read activemap");
|
|
|
|
}
|
|
|
|
if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
|
|
|
|
res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
|
|
|
|
/* Primary is out-of-date, sync from secondary. */
|
|
|
|
nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Secondary is out-of-date or counts match.
|
|
|
|
* Sync from primary.
|
|
|
|
*/
|
|
|
|
nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
|
|
|
|
}
|
|
|
|
} else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
|
|
|
|
res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
|
|
|
|
/*
|
|
|
|
* Not good, we have split-brain condition.
|
|
|
|
*/
|
2011-10-27 20:01:23 +00:00
|
|
|
free(map);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_error("Split-brain detected, exiting.");
|
|
|
|
nv_add_string(nvout, "Split-brain condition!", "errmsg");
|
2012-01-13 23:25:35 +00:00
|
|
|
if (hast_proto_send(res, res->hr_remotein, nvout,
|
|
|
|
NULL, 0) == -1) {
|
|
|
|
pjdlog_exit(EX_TEMPFAIL,
|
|
|
|
"Unable to send response to %s",
|
2011-10-27 20:01:23 +00:00
|
|
|
res->hr_remoteaddr);
|
|
|
|
}
|
|
|
|
nv_free(nvout);
|
|
|
|
/* Exit on split-brain. */
|
|
|
|
event_send(res, EVENT_SPLITBRAIN);
|
|
|
|
exit(EX_CONFIG);
|
2010-02-18 23:16:19 +00:00
|
|
|
} else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
|
|
|
|
res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
|
|
|
|
/*
|
|
|
|
* This should never happen in practise, but we will perform
|
|
|
|
* full synchronization.
|
|
|
|
*/
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_primary_localcnt < res->hr_secondary_remotecnt);
|
|
|
|
mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
|
|
|
|
METADATA_SIZE, res->hr_extentsize,
|
|
|
|
res->hr_local_sectorsize);
|
|
|
|
memset(map, 0xff, mapsize);
|
|
|
|
if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
|
|
|
|
/* In this one of five cases sync from secondary. */
|
|
|
|
nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
|
|
|
|
} else {
|
|
|
|
/* For the rest four cases sync from primary. */
|
|
|
|
nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
|
|
|
|
}
|
|
|
|
pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
|
|
|
|
(uintmax_t)res->hr_primary_localcnt,
|
|
|
|
(uintmax_t)res->hr_primary_remotecnt,
|
|
|
|
(uintmax_t)res->hr_secondary_localcnt,
|
|
|
|
(uintmax_t)res->hr_secondary_remotecnt);
|
|
|
|
}
|
2011-03-25 20:19:15 +00:00
|
|
|
nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) == -1) {
|
2010-10-24 15:44:23 +00:00
|
|
|
pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s",
|
2010-02-18 23:16:19 +00:00
|
|
|
res->hr_remoteaddr);
|
|
|
|
}
|
2010-10-24 15:42:16 +00:00
|
|
|
if (map != NULL)
|
|
|
|
free(map);
|
2010-06-14 21:41:22 +00:00
|
|
|
nv_free(nvout);
|
2011-06-17 07:07:26 +00:00
|
|
|
#ifdef notyet
|
2011-04-02 09:25:13 +00:00
|
|
|
/* Setup direction. */
|
|
|
|
if (proto_recv(res->hr_remotein, 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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hastd_secondary(struct hast_resource *res, struct nv *nvin)
|
|
|
|
{
|
2010-09-22 19:08:11 +00:00
|
|
|
sigset_t mask;
|
2010-02-18 23:16:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Create communication channel between parent and child.
|
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR,
|
|
|
|
"Unable to create control sockets between parent and child");
|
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
/*
|
|
|
|
* Create communication channel between child and parent.
|
|
|
|
*/
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
|
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");
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
pid = fork();
|
2012-01-06 23:44:26 +00:00
|
|
|
if (pid == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR, "Unable to fork");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid > 0) {
|
|
|
|
/* This is parent. */
|
|
|
|
proto_close(res->hr_remotein);
|
|
|
|
res->hr_remotein = NULL;
|
|
|
|
proto_close(res->hr_remoteout);
|
|
|
|
res->hr_remoteout = NULL;
|
2010-08-30 23:26:10 +00:00
|
|
|
/* Declare that we are receiver. */
|
|
|
|
proto_recv(res->hr_event, 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-08-30 00:31:30 +00:00
|
|
|
|
2011-01-28 21:52:37 +00:00
|
|
|
/* Declare that we are sender. */
|
|
|
|
proto_send(res->hr_event, NULL, 0);
|
|
|
|
/* Declare that we are receiver. */
|
|
|
|
proto_recv(res->hr_ctrl, NULL, 0);
|
|
|
|
descriptors_cleanup(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
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-02-18 23:16:19 +00:00
|
|
|
|
2010-09-22 19:08:11 +00:00
|
|
|
PJDLOG_VERIFY(sigemptyset(&mask) == 0);
|
|
|
|
PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
|
2010-08-05 18:58:00 +00:00
|
|
|
|
2010-04-29 15:36:32 +00:00
|
|
|
/* Error in setting timeout is not critical, but why should it fail? */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_timeout(res->hr_remoteout, res->hr_timeout) == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
init_local(res);
|
|
|
|
init_environment();
|
2010-09-22 19:03:11 +00:00
|
|
|
|
2011-05-14 17:02:03 +00:00
|
|
|
if (drop_privs(res) != 0)
|
2011-01-28 22:35:46 +00:00
|
|
|
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-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);
|
2010-09-22 19:03:11 +00:00
|
|
|
|
|
|
|
init_remote(res, nvin);
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(res, EVENT_CONNECT);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
error = pthread_create(&td, NULL, 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, disk_thread, res);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(error == 0);
|
2010-09-22 19:03:11 +00:00
|
|
|
(void)send_thread(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-13 23:25:35 +00:00
|
|
|
reqlog(int loglevel, int debuglevel, int error, struct hio *hio,
|
|
|
|
const char *fmt, ...)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
char msg[1024];
|
|
|
|
va_list ap;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if ((size_t)len < sizeof(msg)) {
|
|
|
|
switch (hio->hio_cmd) {
|
|
|
|
case HIO_READ:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len,
|
|
|
|
"READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
|
|
|
|
(uintmax_t)hio->hio_length);
|
|
|
|
break;
|
|
|
|
case HIO_DELETE:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len,
|
|
|
|
"DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
|
|
|
|
(uintmax_t)hio->hio_length);
|
|
|
|
break;
|
|
|
|
case HIO_FLUSH:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
|
|
|
|
break;
|
|
|
|
case HIO_WRITE:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len,
|
|
|
|
"WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
|
|
|
|
(uintmax_t)hio->hio_length);
|
|
|
|
break;
|
2010-08-27 14:26:37 +00:00
|
|
|
case HIO_KEEPALIVE:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
|
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
default:
|
|
|
|
(void)snprintf(msg + len, sizeof(msg) - len,
|
|
|
|
"UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pjdlog_common(loglevel, debuglevel, error, "%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-27 20:01:23 +00:00
|
|
|
requnpack(struct hast_resource *res, struct hio *hio, struct nv *nv)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
|
2011-10-27 20:01:23 +00:00
|
|
|
hio->hio_cmd = nv_get_uint8(nv, "cmd");
|
2010-02-18 23:16:19 +00:00
|
|
|
if (hio->hio_cmd == 0) {
|
|
|
|
pjdlog_error("Header contains no 'cmd' field.");
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
2011-10-27 20:01:23 +00:00
|
|
|
if (hio->hio_cmd != HIO_KEEPALIVE) {
|
|
|
|
hio->hio_seq = nv_get_uint64(nv, "seq");
|
|
|
|
if (hio->hio_seq == 0) {
|
|
|
|
pjdlog_error("Header contains no 'seq' field.");
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
switch (hio->hio_cmd) {
|
2011-05-21 20:21:20 +00:00
|
|
|
case HIO_FLUSH:
|
2010-08-27 14:26:37 +00:00
|
|
|
case HIO_KEEPALIVE:
|
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
case HIO_WRITE:
|
2013-02-17 21:12:34 +00:00
|
|
|
hio->hio_memsync = nv_exists(nv, "memsync");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case HIO_READ:
|
2010-02-18 23:16:19 +00:00
|
|
|
case HIO_DELETE:
|
2011-10-27 20:01:23 +00:00
|
|
|
hio->hio_offset = nv_get_uint64(nv, "offset");
|
|
|
|
if (nv_error(nv) != 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_error("Header is missing 'offset' field.");
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
2011-10-27 20:01:23 +00:00
|
|
|
hio->hio_length = nv_get_uint64(nv, "length");
|
|
|
|
if (nv_error(nv) != 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_error("Header is missing 'length' field.");
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (hio->hio_length == 0) {
|
|
|
|
pjdlog_error("Data length is zero.");
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
2013-03-14 23:03:48 +00:00
|
|
|
if (hio->hio_cmd != HIO_DELETE && hio->hio_length > MAXPHYS) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_error("Data length is too large (%ju > %ju).",
|
|
|
|
(uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
|
|
|
|
pjdlog_error("Offset %ju is not multiple of sector size.",
|
|
|
|
(uintmax_t)hio->hio_offset);
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
|
|
|
|
pjdlog_error("Length %ju is not multiple of sector size.",
|
|
|
|
(uintmax_t)hio->hio_length);
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (hio->hio_offset + hio->hio_length >
|
|
|
|
(uint64_t)res->hr_datasize) {
|
|
|
|
pjdlog_error("Data offset is too large (%ju > %ju).",
|
|
|
|
(uintmax_t)(hio->hio_offset + hio->hio_length),
|
|
|
|
(uintmax_t)res->hr_datasize);
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pjdlog_error("Header contains invalid 'cmd' (%hhu).",
|
|
|
|
hio->hio_cmd);
|
|
|
|
hio->hio_error = EINVAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
hio->hio_error = 0;
|
|
|
|
end:
|
|
|
|
return (hio->hio_error);
|
|
|
|
}
|
|
|
|
|
2010-09-20 13:23:43 +00:00
|
|
|
static __dead2 void
|
2010-08-30 00:31:30 +00:00
|
|
|
secondary_exit(int exitcode, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(exitcode != EX_OK);
|
2010-08-30 00:31:30 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
pjdlogv_errno(LOG_ERR, fmt, ap);
|
|
|
|
va_end(ap);
|
2010-08-30 23:26:10 +00:00
|
|
|
event_send(gres, EVENT_DISCONNECT);
|
2010-08-30 00:31:30 +00:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Thread receives requests from the primary node.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
recv_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
2013-02-17 21:12:34 +00:00
|
|
|
struct hio *hio, *mshio;
|
2011-10-27 20:01:23 +00:00
|
|
|
struct nv *nv;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "recv: Taking free request.");
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_TAKE(free, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2, "recv: (%p) Got request.", hio);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
|
2010-08-30 00:31:30 +00:00
|
|
|
secondary_exit(EX_TEMPFAIL,
|
2010-02-18 23:16:19 +00:00
|
|
|
"Unable to receive request header");
|
|
|
|
}
|
2011-10-27 20:01:23 +00:00
|
|
|
if (requnpack(res, hio, nv) != 0) {
|
|
|
|
nv_free(nv);
|
2010-08-27 14:01:28 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"recv: (%p) Moving request to the send queue.",
|
|
|
|
hio);
|
|
|
|
QUEUE_INSERT(send, hio);
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-23 21:15:19 +00:00
|
|
|
switch (hio->hio_cmd) {
|
|
|
|
case HIO_READ:
|
|
|
|
res->hr_stat_read++;
|
|
|
|
break;
|
|
|
|
case HIO_WRITE:
|
|
|
|
res->hr_stat_write++;
|
|
|
|
break;
|
|
|
|
case HIO_DELETE:
|
|
|
|
res->hr_stat_delete++;
|
|
|
|
break;
|
|
|
|
case HIO_FLUSH:
|
|
|
|
res->hr_stat_flush++;
|
|
|
|
break;
|
2011-10-27 20:01:23 +00:00
|
|
|
case HIO_KEEPALIVE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
|
|
|
|
hio->hio_cmd);
|
2011-05-23 21:15:19 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
reqlog(LOG_DEBUG, 2, -1, hio,
|
|
|
|
"recv: (%p) Got request header: ", hio);
|
2010-08-27 14:26:37 +00:00
|
|
|
if (hio->hio_cmd == HIO_KEEPALIVE) {
|
2011-10-27 20:01:23 +00:00
|
|
|
nv_free(nv);
|
2010-08-27 14:26:37 +00:00
|
|
|
pjdlog_debug(2,
|
|
|
|
"recv: (%p) Moving request to the free queue.",
|
|
|
|
hio);
|
2011-10-27 20:01:23 +00:00
|
|
|
hio_clear(hio);
|
2010-08-27 14:26:37 +00:00
|
|
|
QUEUE_INSERT(free, hio);
|
|
|
|
continue;
|
|
|
|
} else if (hio->hio_cmd == HIO_WRITE) {
|
2011-10-27 20:01:23 +00:00
|
|
|
if (hast_proto_recv_data(res, res->hr_remotein, nv,
|
2012-01-10 22:39:07 +00:00
|
|
|
hio->hio_data, MAXPHYS) == -1) {
|
2010-08-30 00:31:30 +00:00
|
|
|
secondary_exit(EX_TEMPFAIL,
|
2010-08-31 12:03:29 +00:00
|
|
|
"Unable to receive request data");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
if (hio->hio_memsync) {
|
|
|
|
/*
|
|
|
|
* For memsync requests we expect two replies.
|
|
|
|
* Clone the hio so we can handle both of them.
|
|
|
|
*/
|
|
|
|
pjdlog_debug(2, "recv: Taking free request.");
|
|
|
|
QUEUE_TAKE(free, mshio);
|
|
|
|
pjdlog_debug(2, "recv: (%p) Got request.",
|
|
|
|
mshio);
|
|
|
|
hio_copy(hio, mshio);
|
|
|
|
mshio->hio_error = 0;
|
|
|
|
/*
|
|
|
|
* We want to keep 'memsync' tag only on the
|
|
|
|
* request going onto send queue (mshio).
|
|
|
|
*/
|
|
|
|
hio->hio_memsync = false;
|
|
|
|
pjdlog_debug(2,
|
|
|
|
"recv: (%p) Moving memsync request to the send queue.",
|
|
|
|
mshio);
|
|
|
|
QUEUE_INSERT(send, mshio);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2011-10-27 20:01:23 +00:00
|
|
|
nv_free(nv);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
|
|
|
|
hio);
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_INSERT(disk, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread reads from or writes to local component and also handles DELETE and
|
|
|
|
* FLUSH requests.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
disk_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct hio *hio;
|
|
|
|
ssize_t ret;
|
2011-09-28 13:19:47 +00:00
|
|
|
bool clear_activemap, logerror;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
clear_activemap = true;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "disk: Taking request.");
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_TAKE(disk, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
while (clear_activemap) {
|
|
|
|
unsigned char *map;
|
|
|
|
size_t mapsize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When first request is received, it means that primary
|
|
|
|
* already received our activemap, merged it and stored
|
|
|
|
* locally. We can now safely clear our activemap.
|
|
|
|
*/
|
|
|
|
mapsize =
|
|
|
|
activemap_calc_ondisk_size(res->hr_local_mediasize -
|
|
|
|
METADATA_SIZE, res->hr_extentsize,
|
|
|
|
res->hr_local_sectorsize);
|
|
|
|
map = calloc(1, mapsize);
|
|
|
|
if (map == NULL) {
|
|
|
|
pjdlog_warning("Unable to allocate memory to clear local activemap.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pwrite(res->hr_localfd, map, mapsize,
|
|
|
|
METADATA_SIZE) != (ssize_t)mapsize) {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to store cleared activemap");
|
|
|
|
free(map);
|
2013-02-25 20:09:07 +00:00
|
|
|
res->hr_stat_activemap_write_error++;
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(map);
|
|
|
|
clear_activemap = false;
|
|
|
|
pjdlog_debug(1, "Local activemap cleared.");
|
2011-09-28 13:13:43 +00:00
|
|
|
break;
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
|
2011-09-28 13:19:47 +00:00
|
|
|
logerror = true;
|
2010-02-18 23:16:19 +00:00
|
|
|
/* Handle the actual request. */
|
|
|
|
switch (hio->hio_cmd) {
|
|
|
|
case HIO_READ:
|
|
|
|
ret = pread(res->hr_localfd, hio->hio_data,
|
|
|
|
hio->hio_length,
|
|
|
|
hio->hio_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_error = errno;
|
|
|
|
else if (ret != (int64_t)hio->hio_length)
|
|
|
|
hio->hio_error = EIO;
|
|
|
|
else
|
|
|
|
hio->hio_error = 0;
|
|
|
|
break;
|
|
|
|
case HIO_WRITE:
|
|
|
|
ret = pwrite(res->hr_localfd, hio->hio_data,
|
|
|
|
hio->hio_length,
|
|
|
|
hio->hio_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_error = errno;
|
|
|
|
else if (ret != (int64_t)hio->hio_length)
|
|
|
|
hio->hio_error = EIO;
|
|
|
|
else
|
|
|
|
hio->hio_error = 0;
|
|
|
|
break;
|
|
|
|
case HIO_DELETE:
|
|
|
|
ret = g_delete(res->hr_localfd,
|
|
|
|
hio->hio_offset + res->hr_localoff,
|
|
|
|
hio->hio_length);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ret == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_error = errno;
|
|
|
|
else
|
|
|
|
hio->hio_error = 0;
|
|
|
|
break;
|
|
|
|
case HIO_FLUSH:
|
2011-09-28 13:19:47 +00:00
|
|
|
if (!res->hr_localflush) {
|
|
|
|
ret = -1;
|
|
|
|
hio->hio_error = EOPNOTSUPP;
|
|
|
|
logerror = false;
|
|
|
|
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_error = errno;
|
2011-09-28 13:19:47 +00:00
|
|
|
} else {
|
2010-02-18 23:16:19 +00:00
|
|
|
hio->hio_error = 0;
|
2011-09-28 13:19:47 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
2011-10-27 20:01:23 +00:00
|
|
|
default:
|
|
|
|
PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
|
|
|
|
hio->hio_cmd);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2011-09-28 13:19:47 +00:00
|
|
|
if (logerror && hio->hio_error != 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
reqlog(LOG_ERR, 0, hio->hio_error, hio,
|
|
|
|
"Request failed: ");
|
|
|
|
}
|
|
|
|
pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
|
|
|
|
hio);
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_INSERT(send, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread sends requests back to primary node.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
send_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct hast_resource *res = arg;
|
|
|
|
struct nv *nvout;
|
|
|
|
struct hio *hio;
|
|
|
|
void *data;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pjdlog_debug(2, "send: Taking request.");
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_TAKE(send, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
|
|
|
|
nvout = nv_alloc();
|
|
|
|
/* Copy sequence number. */
|
2011-10-27 20:01:23 +00:00
|
|
|
nv_add_uint64(nvout, hio->hio_seq, "seq");
|
2013-02-17 21:12:34 +00:00
|
|
|
if (hio->hio_memsync) {
|
|
|
|
PJDLOG_ASSERT(hio->hio_cmd == HIO_WRITE);
|
|
|
|
nv_add_int8(nvout, 1, "received");
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
switch (hio->hio_cmd) {
|
|
|
|
case HIO_READ:
|
|
|
|
if (hio->hio_error == 0) {
|
|
|
|
data = hio->hio_data;
|
|
|
|
length = hio->hio_length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We send no data in case of an error.
|
|
|
|
*/
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case HIO_DELETE:
|
|
|
|
case HIO_FLUSH:
|
|
|
|
case HIO_WRITE:
|
|
|
|
data = NULL;
|
|
|
|
length = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-27 07:57:15 +00:00
|
|
|
PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
|
|
|
|
hio->hio_cmd);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2013-02-25 20:09:07 +00:00
|
|
|
if (hio->hio_error != 0) {
|
|
|
|
switch (hio->hio_cmd) {
|
|
|
|
case HIO_READ:
|
|
|
|
res->hr_stat_read_error++;
|
|
|
|
break;
|
|
|
|
case HIO_WRITE:
|
|
|
|
res->hr_stat_write_error++;
|
|
|
|
break;
|
|
|
|
case HIO_DELETE:
|
|
|
|
res->hr_stat_delete_error++;
|
|
|
|
break;
|
|
|
|
case HIO_FLUSH:
|
|
|
|
res->hr_stat_flush_error++;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_add_int16(nvout, hio->hio_error, "error");
|
2013-02-25 20:09:07 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
if (hast_proto_send(res, res->hr_remoteout, nvout, data,
|
2012-01-10 22:39:07 +00:00
|
|
|
length) == -1) {
|
2012-01-13 23:25:35 +00:00
|
|
|
secondary_exit(EX_TEMPFAIL, "Unable to send reply");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
nv_free(nvout);
|
2010-06-14 21:46:48 +00:00
|
|
|
pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
|
2010-02-18 23:16:19 +00:00
|
|
|
hio);
|
2011-10-27 20:01:23 +00:00
|
|
|
hio_clear(hio);
|
2010-08-27 14:01:28 +00:00
|
|
|
QUEUE_INSERT(free, hio);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (NULL);
|
|
|
|
}
|