xref: /freebsd/sbin/hastd/primary.c (revision 643080b75f9a7b3cd876c78cf9925b032eb586e1)
132115b10SPawel Jakub Dawidek /*-
232115b10SPawel Jakub Dawidek  * Copyright (c) 2009 The FreeBSD Foundation
31fee97b0SPawel Jakub Dawidek  * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
432115b10SPawel Jakub Dawidek  * All rights reserved.
532115b10SPawel Jakub Dawidek  *
632115b10SPawel Jakub Dawidek  * This software was developed by Pawel Jakub Dawidek under sponsorship from
732115b10SPawel Jakub Dawidek  * the FreeBSD Foundation.
832115b10SPawel Jakub Dawidek  *
932115b10SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
1032115b10SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
1132115b10SPawel Jakub Dawidek  * are met:
1232115b10SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
1332115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
1432115b10SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
1532115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
1632115b10SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
1732115b10SPawel Jakub Dawidek  *
1832115b10SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1932115b10SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2032115b10SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2132115b10SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2232115b10SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2332115b10SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2432115b10SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2532115b10SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2632115b10SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2732115b10SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2832115b10SPawel Jakub Dawidek  * SUCH DAMAGE.
2932115b10SPawel Jakub Dawidek  */
3032115b10SPawel Jakub Dawidek 
3132115b10SPawel Jakub Dawidek #include <sys/cdefs.h>
3232115b10SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
3332115b10SPawel Jakub Dawidek 
3432115b10SPawel Jakub Dawidek #include <sys/types.h>
3532115b10SPawel Jakub Dawidek #include <sys/time.h>
3632115b10SPawel Jakub Dawidek #include <sys/bio.h>
3732115b10SPawel Jakub Dawidek #include <sys/disk.h>
3832115b10SPawel Jakub Dawidek #include <sys/refcount.h>
3932115b10SPawel Jakub Dawidek #include <sys/stat.h>
4032115b10SPawel Jakub Dawidek 
4132115b10SPawel Jakub Dawidek #include <geom/gate/g_gate.h>
4232115b10SPawel Jakub Dawidek 
4332115b10SPawel Jakub Dawidek #include <err.h>
4432115b10SPawel Jakub Dawidek #include <errno.h>
4532115b10SPawel Jakub Dawidek #include <fcntl.h>
4632115b10SPawel Jakub Dawidek #include <libgeom.h>
4732115b10SPawel Jakub Dawidek #include <pthread.h>
486d0c801eSPawel Jakub Dawidek #include <signal.h>
4932115b10SPawel Jakub Dawidek #include <stdint.h>
5032115b10SPawel Jakub Dawidek #include <stdio.h>
5132115b10SPawel Jakub Dawidek #include <string.h>
5232115b10SPawel Jakub Dawidek #include <sysexits.h>
5332115b10SPawel Jakub Dawidek #include <unistd.h>
5432115b10SPawel Jakub Dawidek 
5532115b10SPawel Jakub Dawidek #include <activemap.h>
5632115b10SPawel Jakub Dawidek #include <nv.h>
5732115b10SPawel Jakub Dawidek #include <rangelock.h>
5832115b10SPawel Jakub Dawidek 
5932115b10SPawel Jakub Dawidek #include "control.h"
605bdff860SPawel Jakub Dawidek #include "event.h"
6132115b10SPawel Jakub Dawidek #include "hast.h"
6232115b10SPawel Jakub Dawidek #include "hast_proto.h"
6332115b10SPawel Jakub Dawidek #include "hastd.h"
640becad39SPawel Jakub Dawidek #include "hooks.h"
6532115b10SPawel Jakub Dawidek #include "metadata.h"
6632115b10SPawel Jakub Dawidek #include "proto.h"
6732115b10SPawel Jakub Dawidek #include "pjdlog.h"
6832115b10SPawel Jakub Dawidek #include "subr.h"
6932115b10SPawel Jakub Dawidek #include "synch.h"
7032115b10SPawel Jakub Dawidek 
710989854dSPawel Jakub Dawidek /* The is only one remote component for now. */
720989854dSPawel Jakub Dawidek #define	ISREMOTE(no)	((no) == 1)
730989854dSPawel Jakub Dawidek 
7432115b10SPawel Jakub Dawidek struct hio {
7532115b10SPawel Jakub Dawidek 	/*
7632115b10SPawel Jakub Dawidek 	 * Number of components we are still waiting for.
7732115b10SPawel Jakub Dawidek 	 * When this field goes to 0, we can send the request back to the
7832115b10SPawel Jakub Dawidek 	 * kernel. Each component has to decrease this counter by one
7932115b10SPawel Jakub Dawidek 	 * even on failure.
8032115b10SPawel Jakub Dawidek 	 */
8132115b10SPawel Jakub Dawidek 	unsigned int		 hio_countdown;
8232115b10SPawel Jakub Dawidek 	/*
8332115b10SPawel Jakub Dawidek 	 * Each component has a place to store its own error.
8432115b10SPawel Jakub Dawidek 	 * Once the request is handled by all components we can decide if the
8532115b10SPawel Jakub Dawidek 	 * request overall is successful or not.
8632115b10SPawel Jakub Dawidek 	 */
8732115b10SPawel Jakub Dawidek 	int			*hio_errors;
8832115b10SPawel Jakub Dawidek 	/*
890b626a28SPawel Jakub Dawidek 	 * Structure used to communicate with GEOM Gate class.
9032115b10SPawel Jakub Dawidek 	 */
9132115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io	 hio_ggio;
9232115b10SPawel Jakub Dawidek 	TAILQ_ENTRY(hio)	*hio_next;
9332115b10SPawel Jakub Dawidek };
9432115b10SPawel Jakub Dawidek #define	hio_free_next	hio_next[0]
9532115b10SPawel Jakub Dawidek #define	hio_done_next	hio_next[0]
9632115b10SPawel Jakub Dawidek 
9732115b10SPawel Jakub Dawidek /*
9832115b10SPawel Jakub Dawidek  * Free list holds unused structures. When free list is empty, we have to wait
9932115b10SPawel Jakub Dawidek  * until some in-progress requests are freed.
10032115b10SPawel Jakub Dawidek  */
10132115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_free_list;
10232115b10SPawel Jakub Dawidek static pthread_mutex_t hio_free_list_lock;
10332115b10SPawel Jakub Dawidek static pthread_cond_t hio_free_list_cond;
10432115b10SPawel Jakub Dawidek /*
10532115b10SPawel Jakub Dawidek  * There is one send list for every component. One requests is placed on all
10632115b10SPawel Jakub Dawidek  * send lists - each component gets the same request, but each component is
10732115b10SPawel Jakub Dawidek  * responsible for managing his own send list.
10832115b10SPawel Jakub Dawidek  */
10932115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_send_list;
11032115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_send_list_lock;
11132115b10SPawel Jakub Dawidek static pthread_cond_t *hio_send_list_cond;
11232115b10SPawel Jakub Dawidek /*
11332115b10SPawel Jakub Dawidek  * There is one recv list for every component, although local components don't
11432115b10SPawel Jakub Dawidek  * use recv lists as local requests are done synchronously.
11532115b10SPawel Jakub Dawidek  */
11632115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_recv_list;
11732115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_recv_list_lock;
11832115b10SPawel Jakub Dawidek static pthread_cond_t *hio_recv_list_cond;
11932115b10SPawel Jakub Dawidek /*
12032115b10SPawel Jakub Dawidek  * Request is placed on done list by the slowest component (the one that
12132115b10SPawel Jakub Dawidek  * decreased hio_countdown from 1 to 0).
12232115b10SPawel Jakub Dawidek  */
12332115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_done_list;
12432115b10SPawel Jakub Dawidek static pthread_mutex_t hio_done_list_lock;
12532115b10SPawel Jakub Dawidek static pthread_cond_t hio_done_list_cond;
12632115b10SPawel Jakub Dawidek /*
12732115b10SPawel Jakub Dawidek  * Structure below are for interaction with sync thread.
12832115b10SPawel Jakub Dawidek  */
12932115b10SPawel Jakub Dawidek static bool sync_inprogress;
13032115b10SPawel Jakub Dawidek static pthread_mutex_t sync_lock;
13132115b10SPawel Jakub Dawidek static pthread_cond_t sync_cond;
13232115b10SPawel Jakub Dawidek /*
13332115b10SPawel Jakub Dawidek  * The lock below allows to synchornize access to remote connections.
13432115b10SPawel Jakub Dawidek  */
13532115b10SPawel Jakub Dawidek static pthread_rwlock_t *hio_remote_lock;
13632115b10SPawel Jakub Dawidek 
13732115b10SPawel Jakub Dawidek /*
13832115b10SPawel Jakub Dawidek  * Lock to synchronize metadata updates. Also synchronize access to
13932115b10SPawel Jakub Dawidek  * hr_primary_localcnt and hr_primary_remotecnt fields.
14032115b10SPawel Jakub Dawidek  */
14132115b10SPawel Jakub Dawidek static pthread_mutex_t metadata_lock;
14232115b10SPawel Jakub Dawidek 
14332115b10SPawel Jakub Dawidek /*
14432115b10SPawel Jakub Dawidek  * Maximum number of outstanding I/O requests.
14532115b10SPawel Jakub Dawidek  */
14632115b10SPawel Jakub Dawidek #define	HAST_HIO_MAX	256
14732115b10SPawel Jakub Dawidek /*
14832115b10SPawel Jakub Dawidek  * Number of components. At this point there are only two components: local
14932115b10SPawel Jakub Dawidek  * and remote, but in the future it might be possible to use multiple local
15032115b10SPawel Jakub Dawidek  * and remote components.
15132115b10SPawel Jakub Dawidek  */
15232115b10SPawel Jakub Dawidek #define	HAST_NCOMPONENTS	2
15332115b10SPawel Jakub Dawidek 
15432115b10SPawel Jakub Dawidek #define	ISCONNECTED(res, no)	\
15532115b10SPawel Jakub Dawidek 	((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
15632115b10SPawel Jakub Dawidek 
15732115b10SPawel Jakub Dawidek #define	QUEUE_INSERT1(hio, name, ncomp)	do {				\
15832115b10SPawel Jakub Dawidek 	bool _wakeup;							\
15932115b10SPawel Jakub Dawidek 									\
16032115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
16132115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]);		\
16232115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio),		\
16332115b10SPawel Jakub Dawidek 	    hio_next[(ncomp)]);						\
16432115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[ncomp]);			\
16532115b10SPawel Jakub Dawidek 	if (_wakeup)							\
16632115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond[(ncomp)]);		\
16732115b10SPawel Jakub Dawidek } while (0)
16832115b10SPawel Jakub Dawidek #define	QUEUE_INSERT2(hio, name)	do {				\
16932115b10SPawel Jakub Dawidek 	bool _wakeup;							\
17032115b10SPawel Jakub Dawidek 									\
17132115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
17232115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
17332115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
17432115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
17532115b10SPawel Jakub Dawidek 	if (_wakeup)							\
17632115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond);			\
17732115b10SPawel Jakub Dawidek } while (0)
178448efa94SPawel Jakub Dawidek #define	QUEUE_TAKE1(hio, name, ncomp, timeout)	do {			\
179448efa94SPawel Jakub Dawidek 	bool _last;							\
180448efa94SPawel Jakub Dawidek 									\
18132115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
182448efa94SPawel Jakub Dawidek 	_last = false;							\
183448efa94SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
184448efa94SPawel Jakub Dawidek 		cv_timedwait(&hio_##name##_list_cond[(ncomp)],		\
185448efa94SPawel Jakub Dawidek 		    &hio_##name##_list_lock[(ncomp)], (timeout));	\
186448efa94SPawel Jakub Dawidek 		if ((timeout) != 0)					\
187448efa94SPawel Jakub Dawidek 			_last = true;					\
18832115b10SPawel Jakub Dawidek 	}								\
189448efa94SPawel Jakub Dawidek 	if (hio != NULL) {						\
19032115b10SPawel Jakub Dawidek 		TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio),	\
19132115b10SPawel Jakub Dawidek 		    hio_next[(ncomp)]);					\
192448efa94SPawel Jakub Dawidek 	}								\
19332115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[(ncomp)]);			\
19432115b10SPawel Jakub Dawidek } while (0)
19532115b10SPawel Jakub Dawidek #define	QUEUE_TAKE2(hio, name)	do {					\
19632115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
19732115b10SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
19832115b10SPawel Jakub Dawidek 		cv_wait(&hio_##name##_list_cond,			\
19932115b10SPawel Jakub Dawidek 		    &hio_##name##_list_lock);				\
20032115b10SPawel Jakub Dawidek 	}								\
20132115b10SPawel Jakub Dawidek 	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next);	\
20232115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
20332115b10SPawel Jakub Dawidek } while (0)
20432115b10SPawel Jakub Dawidek 
205328e0f4bSPawel Jakub Dawidek #define	SYNCREQ(hio)		do {					\
206328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_unit = -1;					\
207328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_seq = 1;					\
208328e0f4bSPawel Jakub Dawidek } while (0)
20932115b10SPawel Jakub Dawidek #define	ISSYNCREQ(hio)		((hio)->hio_ggio.gctl_unit == -1)
21032115b10SPawel Jakub Dawidek #define	SYNCREQDONE(hio)	do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
21132115b10SPawel Jakub Dawidek #define	ISSYNCREQDONE(hio)	((hio)->hio_ggio.gctl_unit == -2)
21232115b10SPawel Jakub Dawidek 
21332115b10SPawel Jakub Dawidek static struct hast_resource *gres;
21432115b10SPawel Jakub Dawidek 
21532115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock;
21632115b10SPawel Jakub Dawidek static struct rangelocks *range_regular;
21732115b10SPawel Jakub Dawidek static bool range_regular_wait;
21832115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond;
21932115b10SPawel Jakub Dawidek static struct rangelocks *range_sync;
22032115b10SPawel Jakub Dawidek static bool range_sync_wait;
22132115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond;
22232115b10SPawel Jakub Dawidek 
22332115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg);
22432115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg);
22532115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg);
22632115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg);
22732115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg);
22832115b10SPawel Jakub Dawidek static void *sync_thread(void *arg);
22932115b10SPawel Jakub Dawidek static void *guard_thread(void *arg);
23032115b10SPawel Jakub Dawidek 
2316d0c801eSPawel Jakub Dawidek static void
23232115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res)
23332115b10SPawel Jakub Dawidek {
23432115b10SPawel Jakub Dawidek 	int rerrno;
23532115b10SPawel Jakub Dawidek 
23632115b10SPawel Jakub Dawidek 	/* Remember errno. */
23732115b10SPawel Jakub Dawidek 	rerrno = errno;
23832115b10SPawel Jakub Dawidek 
23932115b10SPawel Jakub Dawidek 	/* Destroy ggate provider if we created one. */
24032115b10SPawel Jakub Dawidek 	if (res->hr_ggateunit >= 0) {
24132115b10SPawel Jakub Dawidek 		struct g_gate_ctl_destroy ggiod;
24232115b10SPawel Jakub Dawidek 
2434e47b646SPawel Jakub Dawidek 		bzero(&ggiod, sizeof(ggiod));
24432115b10SPawel Jakub Dawidek 		ggiod.gctl_version = G_GATE_VERSION;
24532115b10SPawel Jakub Dawidek 		ggiod.gctl_unit = res->hr_ggateunit;
24632115b10SPawel Jakub Dawidek 		ggiod.gctl_force = 1;
24732115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) {
248783ee753SPawel Jakub Dawidek 			pjdlog_errno(LOG_WARNING,
249783ee753SPawel Jakub Dawidek 			    "Unable to destroy hast/%s device",
25032115b10SPawel Jakub Dawidek 			    res->hr_provname);
25132115b10SPawel Jakub Dawidek 		}
25232115b10SPawel Jakub Dawidek 		res->hr_ggateunit = -1;
25332115b10SPawel Jakub Dawidek 	}
25432115b10SPawel Jakub Dawidek 
25532115b10SPawel Jakub Dawidek 	/* Restore errno. */
25632115b10SPawel Jakub Dawidek 	errno = rerrno;
25732115b10SPawel Jakub Dawidek }
25832115b10SPawel Jakub Dawidek 
259e43e02f1SPawel Jakub Dawidek static __dead2 void
26032115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...)
26132115b10SPawel Jakub Dawidek {
26232115b10SPawel Jakub Dawidek 	va_list ap;
26332115b10SPawel Jakub Dawidek 
2642ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(exitcode != EX_OK);
26532115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
26632115b10SPawel Jakub Dawidek 	pjdlogv_errno(LOG_ERR, fmt, ap);
26732115b10SPawel Jakub Dawidek 	va_end(ap);
26832115b10SPawel Jakub Dawidek 	cleanup(gres);
26932115b10SPawel Jakub Dawidek 	exit(exitcode);
27032115b10SPawel Jakub Dawidek }
27132115b10SPawel Jakub Dawidek 
272e43e02f1SPawel Jakub Dawidek static __dead2 void
27332115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...)
27432115b10SPawel Jakub Dawidek {
27532115b10SPawel Jakub Dawidek 	va_list ap;
27632115b10SPawel Jakub Dawidek 
27732115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
27832115b10SPawel Jakub Dawidek 	pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
27932115b10SPawel Jakub Dawidek 	va_end(ap);
28032115b10SPawel Jakub Dawidek 	cleanup(gres);
28132115b10SPawel Jakub Dawidek 	exit(exitcode);
28232115b10SPawel Jakub Dawidek }
28332115b10SPawel Jakub Dawidek 
28432115b10SPawel Jakub Dawidek static int
28532115b10SPawel Jakub Dawidek hast_activemap_flush(struct hast_resource *res)
28632115b10SPawel Jakub Dawidek {
28732115b10SPawel Jakub Dawidek 	const unsigned char *buf;
28832115b10SPawel Jakub Dawidek 	size_t size;
28932115b10SPawel Jakub Dawidek 
29032115b10SPawel Jakub Dawidek 	buf = activemap_bitmap(res->hr_amp, &size);
2912ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(buf != NULL);
2922ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
29332115b10SPawel Jakub Dawidek 	if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
29432115b10SPawel Jakub Dawidek 	    (ssize_t)size) {
29532115b10SPawel Jakub Dawidek 		KEEP_ERRNO(pjdlog_errno(LOG_ERR,
29632115b10SPawel Jakub Dawidek 		    "Unable to flush activemap to disk"));
29732115b10SPawel Jakub Dawidek 		return (-1);
29832115b10SPawel Jakub Dawidek 	}
29932115b10SPawel Jakub Dawidek 	return (0);
30032115b10SPawel Jakub Dawidek }
30132115b10SPawel Jakub Dawidek 
302f377917cSPawel Jakub Dawidek static bool
303f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res)
304f377917cSPawel Jakub Dawidek {
305f377917cSPawel Jakub Dawidek 
306f377917cSPawel Jakub Dawidek 	return (strcmp(res->hr_remoteaddr, "none") != 0);
307f377917cSPawel Jakub Dawidek }
308f377917cSPawel Jakub Dawidek 
30932115b10SPawel Jakub Dawidek static void
31032115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused)
31132115b10SPawel Jakub Dawidek {
31232115b10SPawel Jakub Dawidek 	struct hio *hio;
31332115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
31432115b10SPawel Jakub Dawidek 
31532115b10SPawel Jakub Dawidek 	/*
31632115b10SPawel Jakub Dawidek 	 * In the future it might be per-resource value.
31732115b10SPawel Jakub Dawidek 	 */
31832115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
31932115b10SPawel Jakub Dawidek 
32032115b10SPawel Jakub Dawidek 	/*
32132115b10SPawel Jakub Dawidek 	 * Allocate memory needed by lists.
32232115b10SPawel Jakub Dawidek 	 */
32332115b10SPawel Jakub Dawidek 	hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
32432115b10SPawel Jakub Dawidek 	if (hio_send_list == NULL) {
32532115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
32632115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send lists.",
32732115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list[0]) * ncomps);
32832115b10SPawel Jakub Dawidek 	}
32932115b10SPawel Jakub Dawidek 	hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
33032115b10SPawel Jakub Dawidek 	if (hio_send_list_lock == NULL) {
33132115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
33232115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list locks.",
33332115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_lock[0]) * ncomps);
33432115b10SPawel Jakub Dawidek 	}
33532115b10SPawel Jakub Dawidek 	hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
33632115b10SPawel Jakub Dawidek 	if (hio_send_list_cond == NULL) {
33732115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
33832115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list condition variables.",
33932115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_cond[0]) * ncomps);
34032115b10SPawel Jakub Dawidek 	}
34132115b10SPawel Jakub Dawidek 	hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
34232115b10SPawel Jakub Dawidek 	if (hio_recv_list == NULL) {
34332115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
34432115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv lists.",
34532115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list[0]) * ncomps);
34632115b10SPawel Jakub Dawidek 	}
34732115b10SPawel Jakub Dawidek 	hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
34832115b10SPawel Jakub Dawidek 	if (hio_recv_list_lock == NULL) {
34932115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35032115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list locks.",
35132115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_lock[0]) * ncomps);
35232115b10SPawel Jakub Dawidek 	}
35332115b10SPawel Jakub Dawidek 	hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
35432115b10SPawel Jakub Dawidek 	if (hio_recv_list_cond == NULL) {
35532115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35632115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list condition variables.",
35732115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_cond[0]) * ncomps);
35832115b10SPawel Jakub Dawidek 	}
35932115b10SPawel Jakub Dawidek 	hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
36032115b10SPawel Jakub Dawidek 	if (hio_remote_lock == NULL) {
36132115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
36232115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for remote connections locks.",
36332115b10SPawel Jakub Dawidek 		    sizeof(hio_remote_lock[0]) * ncomps);
36432115b10SPawel Jakub Dawidek 	}
36532115b10SPawel Jakub Dawidek 
36632115b10SPawel Jakub Dawidek 	/*
36732115b10SPawel Jakub Dawidek 	 * Initialize lists, their locks and theirs condition variables.
36832115b10SPawel Jakub Dawidek 	 */
36932115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_free_list);
37032115b10SPawel Jakub Dawidek 	mtx_init(&hio_free_list_lock);
37132115b10SPawel Jakub Dawidek 	cv_init(&hio_free_list_cond);
37232115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
37332115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_send_list[ii]);
37432115b10SPawel Jakub Dawidek 		mtx_init(&hio_send_list_lock[ii]);
37532115b10SPawel Jakub Dawidek 		cv_init(&hio_send_list_cond[ii]);
37632115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_recv_list[ii]);
37732115b10SPawel Jakub Dawidek 		mtx_init(&hio_recv_list_lock[ii]);
37832115b10SPawel Jakub Dawidek 		cv_init(&hio_recv_list_cond[ii]);
37932115b10SPawel Jakub Dawidek 		rw_init(&hio_remote_lock[ii]);
38032115b10SPawel Jakub Dawidek 	}
38132115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_done_list);
38232115b10SPawel Jakub Dawidek 	mtx_init(&hio_done_list_lock);
38332115b10SPawel Jakub Dawidek 	cv_init(&hio_done_list_cond);
38432115b10SPawel Jakub Dawidek 	mtx_init(&metadata_lock);
38532115b10SPawel Jakub Dawidek 
38632115b10SPawel Jakub Dawidek 	/*
38732115b10SPawel Jakub Dawidek 	 * Allocate requests pool and initialize requests.
38832115b10SPawel Jakub Dawidek 	 */
38932115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
39032115b10SPawel Jakub Dawidek 		hio = malloc(sizeof(*hio));
39132115b10SPawel Jakub Dawidek 		if (hio == NULL) {
39232115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
39332115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for hio request.",
39432115b10SPawel Jakub Dawidek 			    sizeof(*hio));
39532115b10SPawel Jakub Dawidek 		}
39632115b10SPawel Jakub Dawidek 		hio->hio_countdown = 0;
39732115b10SPawel Jakub Dawidek 		hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
39832115b10SPawel Jakub Dawidek 		if (hio->hio_errors == NULL) {
39932115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
40032115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio errors.",
40132115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_errors[0]) * ncomps);
40232115b10SPawel Jakub Dawidek 		}
40332115b10SPawel Jakub Dawidek 		hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
40432115b10SPawel Jakub Dawidek 		if (hio->hio_next == NULL) {
40532115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
40632115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio_next field.",
40732115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_next[0]) * ncomps);
40832115b10SPawel Jakub Dawidek 		}
40932115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_version = G_GATE_VERSION;
41032115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_data = malloc(MAXPHYS);
41132115b10SPawel Jakub Dawidek 		if (hio->hio_ggio.gctl_data == NULL) {
41232115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
41332115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for gctl_data.",
41432115b10SPawel Jakub Dawidek 			    MAXPHYS);
41532115b10SPawel Jakub Dawidek 		}
41632115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_length = MAXPHYS;
41732115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_error = 0;
41832115b10SPawel Jakub Dawidek 		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
41932115b10SPawel Jakub Dawidek 	}
42032115b10SPawel Jakub Dawidek }
42132115b10SPawel Jakub Dawidek 
422ce837469SPawel Jakub Dawidek static bool
423ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res)
424ce837469SPawel Jakub Dawidek {
425ce837469SPawel Jakub Dawidek 
426ce837469SPawel Jakub Dawidek 	mtx_lock(&metadata_lock);
427ce837469SPawel Jakub Dawidek 	if (res->hr_resuid != 0) {
428ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
429ce837469SPawel Jakub Dawidek 		return (false);
430ce837469SPawel Jakub Dawidek 	} else {
431ce837469SPawel Jakub Dawidek 		/* Initialize unique resource identifier. */
432ce837469SPawel Jakub Dawidek 		arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
433ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
434ce837469SPawel Jakub Dawidek 		if (metadata_write(res) < 0)
435ce837469SPawel Jakub Dawidek 			exit(EX_NOINPUT);
436ce837469SPawel Jakub Dawidek 		return (true);
437ce837469SPawel Jakub Dawidek 	}
438ce837469SPawel Jakub Dawidek }
439ce837469SPawel Jakub Dawidek 
44032115b10SPawel Jakub Dawidek static void
44132115b10SPawel Jakub Dawidek init_local(struct hast_resource *res)
44232115b10SPawel Jakub Dawidek {
44332115b10SPawel Jakub Dawidek 	unsigned char *buf;
44432115b10SPawel Jakub Dawidek 	size_t mapsize;
44532115b10SPawel Jakub Dawidek 
44632115b10SPawel Jakub Dawidek 	if (metadata_read(res, true) < 0)
44732115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
44832115b10SPawel Jakub Dawidek 	mtx_init(&res->hr_amp_lock);
44932115b10SPawel Jakub Dawidek 	if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
45032115b10SPawel Jakub Dawidek 	    res->hr_local_sectorsize, res->hr_keepdirty) < 0) {
45132115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create activemap");
45232115b10SPawel Jakub Dawidek 	}
45332115b10SPawel Jakub Dawidek 	mtx_init(&range_lock);
45432115b10SPawel Jakub Dawidek 	cv_init(&range_regular_cond);
45532115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_regular) < 0)
45632115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
45732115b10SPawel Jakub Dawidek 	cv_init(&range_sync_cond);
45832115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_sync) < 0)
45932115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
46032115b10SPawel Jakub Dawidek 	mapsize = activemap_ondisk_size(res->hr_amp);
46132115b10SPawel Jakub Dawidek 	buf = calloc(1, mapsize);
46232115b10SPawel Jakub Dawidek 	if (buf == NULL) {
46332115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
46432115b10SPawel Jakub Dawidek 		    "Unable to allocate buffer for activemap.");
46532115b10SPawel Jakub Dawidek 	}
46632115b10SPawel Jakub Dawidek 	if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
46732115b10SPawel Jakub Dawidek 	    (ssize_t)mapsize) {
46832115b10SPawel Jakub Dawidek 		primary_exit(EX_NOINPUT, "Unable to read activemap");
46932115b10SPawel Jakub Dawidek 	}
47032115b10SPawel Jakub Dawidek 	activemap_copyin(res->hr_amp, buf, mapsize);
471b0dfbe5bSPawel Jakub Dawidek 	free(buf);
47232115b10SPawel Jakub Dawidek 	if (res->hr_resuid != 0)
47332115b10SPawel Jakub Dawidek 		return;
47432115b10SPawel Jakub Dawidek 	/*
475ce837469SPawel Jakub Dawidek 	 * We're using provider for the first time. Initialize local and remote
476ce837469SPawel Jakub Dawidek 	 * counters. We don't initialize resuid here, as we want to do it just
477ce837469SPawel Jakub Dawidek 	 * in time. The reason for this is that we want to inform secondary
478ce837469SPawel Jakub Dawidek 	 * that there were no writes yet, so there is no need to synchronize
479ce837469SPawel Jakub Dawidek 	 * anything.
48032115b10SPawel Jakub Dawidek 	 */
4819446b453SPawel Jakub Dawidek 	res->hr_primary_localcnt = 0;
48232115b10SPawel Jakub Dawidek 	res->hr_primary_remotecnt = 0;
48332115b10SPawel Jakub Dawidek 	if (metadata_write(res) < 0)
48432115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
48532115b10SPawel Jakub Dawidek }
48632115b10SPawel Jakub Dawidek 
48732ecf620SPawel Jakub Dawidek static int
48832ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp)
48932ecf620SPawel Jakub Dawidek {
49032ecf620SPawel Jakub Dawidek 	struct proto_conn *conn;
49132ecf620SPawel Jakub Dawidek 	int16_t val;
49232ecf620SPawel Jakub Dawidek 
49332ecf620SPawel Jakub Dawidek 	val = 1;
49432ecf620SPawel Jakub Dawidek 	if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
49532ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
49632ecf620SPawel Jakub Dawidek 		    "Unable to send connection request to parent");
49732ecf620SPawel Jakub Dawidek 	}
49832ecf620SPawel Jakub Dawidek 	if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
49932ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
50032ecf620SPawel Jakub Dawidek 		    "Unable to receive reply to connection request from parent");
50132ecf620SPawel Jakub Dawidek 	}
50232ecf620SPawel Jakub Dawidek 	if (val != 0) {
50332ecf620SPawel Jakub Dawidek 		errno = val;
50432ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
50532ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
50632ecf620SPawel Jakub Dawidek 		return (-1);
50732ecf620SPawel Jakub Dawidek 	}
50832ecf620SPawel Jakub Dawidek 	if (proto_connection_recv(res->hr_conn, true, &conn) < 0) {
50932ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
51032ecf620SPawel Jakub Dawidek 		    "Unable to receive connection from parent");
51132ecf620SPawel Jakub Dawidek 	}
51232ecf620SPawel Jakub Dawidek 	if (proto_connect_wait(conn, HAST_TIMEOUT) < 0) {
51332ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
51432ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
51532ecf620SPawel Jakub Dawidek 		proto_close(conn);
51632ecf620SPawel Jakub Dawidek 		return (-1);
51732ecf620SPawel Jakub Dawidek 	}
51832ecf620SPawel Jakub Dawidek 	/* Error in setting timeout is not critical, but why should it fail? */
51932ecf620SPawel Jakub Dawidek 	if (proto_timeout(conn, res->hr_timeout) < 0)
52032ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
52132ecf620SPawel Jakub Dawidek 
52232ecf620SPawel Jakub Dawidek 	*connp = conn;
52332ecf620SPawel Jakub Dawidek 
52432ecf620SPawel Jakub Dawidek 	return (0);
52532ecf620SPawel Jakub Dawidek }
52632ecf620SPawel Jakub Dawidek 
5270d9014f3SPawel Jakub Dawidek static bool
5280d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp,
5290d9014f3SPawel Jakub Dawidek     struct proto_conn **outp)
53032115b10SPawel Jakub Dawidek {
5310d9014f3SPawel Jakub Dawidek 	struct proto_conn *in, *out;
53232115b10SPawel Jakub Dawidek 	struct nv *nvout, *nvin;
53332115b10SPawel Jakub Dawidek 	const unsigned char *token;
53432115b10SPawel Jakub Dawidek 	unsigned char *map;
53532115b10SPawel Jakub Dawidek 	const char *errmsg;
53632115b10SPawel Jakub Dawidek 	int32_t extentsize;
53732115b10SPawel Jakub Dawidek 	int64_t datasize;
53832115b10SPawel Jakub Dawidek 	uint32_t mapsize;
53932115b10SPawel Jakub Dawidek 	size_t size;
54032115b10SPawel Jakub Dawidek 
5412ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
5422ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(real_remote(res));
5430d9014f3SPawel Jakub Dawidek 
5440d9014f3SPawel Jakub Dawidek 	in = out = NULL;
5452be8fd75SPawel Jakub Dawidek 	errmsg = NULL;
5460d9014f3SPawel Jakub Dawidek 
54732ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &out) == -1)
54832ecf620SPawel Jakub Dawidek 		return (false);
54932ecf620SPawel Jakub Dawidek 
55032115b10SPawel Jakub Dawidek 	/*
55132115b10SPawel Jakub Dawidek 	 * First handshake step.
55232115b10SPawel Jakub Dawidek 	 * Setup outgoing connection with remote node.
55332115b10SPawel Jakub Dawidek 	 */
55432115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
55532115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
55632115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
55732115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
55832115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
55932115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
56032115b10SPawel Jakub Dawidek 		nv_free(nvout);
56132115b10SPawel Jakub Dawidek 		goto close;
56232115b10SPawel Jakub Dawidek 	}
5630d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, out, nvout, NULL, 0) < 0) {
56432115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
56532115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
56632115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
56732115b10SPawel Jakub Dawidek 		nv_free(nvout);
56832115b10SPawel Jakub Dawidek 		goto close;
56932115b10SPawel Jakub Dawidek 	}
57032115b10SPawel Jakub Dawidek 	nv_free(nvout);
5710d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
57232115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
57332115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
57432115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
57532115b10SPawel Jakub Dawidek 		goto close;
57632115b10SPawel Jakub Dawidek 	}
57732115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
57832115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
57932115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
58032115b10SPawel Jakub Dawidek 		nv_free(nvin);
58132115b10SPawel Jakub Dawidek 		goto close;
58232115b10SPawel Jakub Dawidek 	}
58332115b10SPawel Jakub Dawidek 	token = nv_get_uint8_array(nvin, &size, "token");
58432115b10SPawel Jakub Dawidek 	if (token == NULL) {
58532115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s has no 'token' field.",
58632115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
58732115b10SPawel Jakub Dawidek 		nv_free(nvin);
58832115b10SPawel Jakub Dawidek 		goto close;
58932115b10SPawel Jakub Dawidek 	}
59032115b10SPawel Jakub Dawidek 	if (size != sizeof(res->hr_token)) {
59132115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
59232115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr, size, sizeof(res->hr_token));
59332115b10SPawel Jakub Dawidek 		nv_free(nvin);
59432115b10SPawel Jakub Dawidek 		goto close;
59532115b10SPawel Jakub Dawidek 	}
59632115b10SPawel Jakub Dawidek 	bcopy(token, res->hr_token, sizeof(res->hr_token));
59732115b10SPawel Jakub Dawidek 	nv_free(nvin);
59832115b10SPawel Jakub Dawidek 
59932115b10SPawel Jakub Dawidek 	/*
60032115b10SPawel Jakub Dawidek 	 * Second handshake step.
60132115b10SPawel Jakub Dawidek 	 * Setup incoming connection with remote node.
60232115b10SPawel Jakub Dawidek 	 */
60332ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &in) == -1)
60432115b10SPawel Jakub Dawidek 		goto close;
60532ecf620SPawel Jakub Dawidek 
60632115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
60732115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
60832115b10SPawel Jakub Dawidek 	nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
60932115b10SPawel Jakub Dawidek 	    "token");
610ce837469SPawel Jakub Dawidek 	if (res->hr_resuid == 0) {
611ce837469SPawel Jakub Dawidek 		/*
612ce837469SPawel Jakub Dawidek 		 * The resuid field was not yet initialized.
613ce837469SPawel Jakub Dawidek 		 * Because we do synchronization inside init_resuid(), it is
614ce837469SPawel Jakub Dawidek 		 * possible that someone already initialized it, the function
615ce837469SPawel Jakub Dawidek 		 * will return false then, but if we successfully initialized
616ce837469SPawel Jakub Dawidek 		 * it, we will get true. True means that there were no writes
617ce837469SPawel Jakub Dawidek 		 * to this resource yet and we want to inform secondary that
618ce837469SPawel Jakub Dawidek 		 * synchronization is not needed by sending "virgin" argument.
619ce837469SPawel Jakub Dawidek 		 */
620ce837469SPawel Jakub Dawidek 		if (init_resuid(res))
621ce837469SPawel Jakub Dawidek 			nv_add_int8(nvout, 1, "virgin");
622ce837469SPawel Jakub Dawidek 	}
62332115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_resuid, "resuid");
62432115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
62532115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
62632115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
62732115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
62832115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
62932115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
63032115b10SPawel Jakub Dawidek 		nv_free(nvout);
63132115b10SPawel Jakub Dawidek 		goto close;
63232115b10SPawel Jakub Dawidek 	}
6330d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, in, nvout, NULL, 0) < 0) {
63432115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
63532115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
63632115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
63732115b10SPawel Jakub Dawidek 		nv_free(nvout);
63832115b10SPawel Jakub Dawidek 		goto close;
63932115b10SPawel Jakub Dawidek 	}
64032115b10SPawel Jakub Dawidek 	nv_free(nvout);
6410d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
64232115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
64332115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
64432115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
64532115b10SPawel Jakub Dawidek 		goto close;
64632115b10SPawel Jakub Dawidek 	}
64732115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
64832115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
64932115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
65032115b10SPawel Jakub Dawidek 		nv_free(nvin);
65132115b10SPawel Jakub Dawidek 		goto close;
65232115b10SPawel Jakub Dawidek 	}
65332115b10SPawel Jakub Dawidek 	datasize = nv_get_int64(nvin, "datasize");
65432115b10SPawel Jakub Dawidek 	if (datasize != res->hr_datasize) {
65532115b10SPawel Jakub Dawidek 		pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
65632115b10SPawel Jakub Dawidek 		    (intmax_t)res->hr_datasize, (intmax_t)datasize);
65732115b10SPawel Jakub Dawidek 		nv_free(nvin);
65832115b10SPawel Jakub Dawidek 		goto close;
65932115b10SPawel Jakub Dawidek 	}
66032115b10SPawel Jakub Dawidek 	extentsize = nv_get_int32(nvin, "extentsize");
66132115b10SPawel Jakub Dawidek 	if (extentsize != res->hr_extentsize) {
66232115b10SPawel Jakub Dawidek 		pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
66332115b10SPawel Jakub Dawidek 		    (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
66432115b10SPawel Jakub Dawidek 		nv_free(nvin);
66532115b10SPawel Jakub Dawidek 		goto close;
66632115b10SPawel Jakub Dawidek 	}
66732115b10SPawel Jakub Dawidek 	res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
66832115b10SPawel Jakub Dawidek 	res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
66932115b10SPawel Jakub Dawidek 	res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
67032115b10SPawel Jakub Dawidek 	map = NULL;
67132115b10SPawel Jakub Dawidek 	mapsize = nv_get_uint32(nvin, "mapsize");
67232115b10SPawel Jakub Dawidek 	if (mapsize > 0) {
67332115b10SPawel Jakub Dawidek 		map = malloc(mapsize);
67432115b10SPawel Jakub Dawidek 		if (map == NULL) {
67532115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
67632115b10SPawel Jakub Dawidek 			    (uintmax_t)mapsize);
67732115b10SPawel Jakub Dawidek 			nv_free(nvin);
67832115b10SPawel Jakub Dawidek 			goto close;
67932115b10SPawel Jakub Dawidek 		}
68032115b10SPawel Jakub Dawidek 		/*
68132115b10SPawel Jakub Dawidek 		 * Remote node have some dirty extents on its own, lets
68232115b10SPawel Jakub Dawidek 		 * download its activemap.
68332115b10SPawel Jakub Dawidek 		 */
6840d9014f3SPawel Jakub Dawidek 		if (hast_proto_recv_data(res, out, nvin, map,
68532115b10SPawel Jakub Dawidek 		    mapsize) < 0) {
68632115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
68732115b10SPawel Jakub Dawidek 			    "Unable to receive remote activemap");
68832115b10SPawel Jakub Dawidek 			nv_free(nvin);
68932115b10SPawel Jakub Dawidek 			free(map);
69032115b10SPawel Jakub Dawidek 			goto close;
69132115b10SPawel Jakub Dawidek 		}
69232115b10SPawel Jakub Dawidek 		/*
69332115b10SPawel Jakub Dawidek 		 * Merge local and remote bitmaps.
69432115b10SPawel Jakub Dawidek 		 */
69532115b10SPawel Jakub Dawidek 		activemap_merge(res->hr_amp, map, mapsize);
69632115b10SPawel Jakub Dawidek 		free(map);
69732115b10SPawel Jakub Dawidek 		/*
69832115b10SPawel Jakub Dawidek 		 * Now that we merged bitmaps from both nodes, flush it to the
69932115b10SPawel Jakub Dawidek 		 * disk before we start to synchronize.
70032115b10SPawel Jakub Dawidek 		 */
70132115b10SPawel Jakub Dawidek 		(void)hast_activemap_flush(res);
70232115b10SPawel Jakub Dawidek 	}
703584a9bc3SPawel Jakub Dawidek 	nv_free(nvin);
70432115b10SPawel Jakub Dawidek 	pjdlog_info("Connected to %s.", res->hr_remoteaddr);
7050d9014f3SPawel Jakub Dawidek 	if (inp != NULL && outp != NULL) {
7060d9014f3SPawel Jakub Dawidek 		*inp = in;
7070d9014f3SPawel Jakub Dawidek 		*outp = out;
7080d9014f3SPawel Jakub Dawidek 	} else {
7090d9014f3SPawel Jakub Dawidek 		res->hr_remotein = in;
7100d9014f3SPawel Jakub Dawidek 		res->hr_remoteout = out;
7110d9014f3SPawel Jakub Dawidek 	}
7125bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_CONNECT);
7130d9014f3SPawel Jakub Dawidek 	return (true);
7140d9014f3SPawel Jakub Dawidek close:
7152be8fd75SPawel Jakub Dawidek 	if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
7165bdff860SPawel Jakub Dawidek 		event_send(res, EVENT_SPLITBRAIN);
7170d9014f3SPawel Jakub Dawidek 	proto_close(out);
7180d9014f3SPawel Jakub Dawidek 	if (in != NULL)
7190d9014f3SPawel Jakub Dawidek 		proto_close(in);
7200d9014f3SPawel Jakub Dawidek 	return (false);
7210d9014f3SPawel Jakub Dawidek }
7220d9014f3SPawel Jakub Dawidek 
7230d9014f3SPawel Jakub Dawidek static void
7240d9014f3SPawel Jakub Dawidek sync_start(void)
7250d9014f3SPawel Jakub Dawidek {
7260d9014f3SPawel Jakub Dawidek 
72732115b10SPawel Jakub Dawidek 	mtx_lock(&sync_lock);
72832115b10SPawel Jakub Dawidek 	sync_inprogress = true;
72932115b10SPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
73032115b10SPawel Jakub Dawidek 	cv_signal(&sync_cond);
73132115b10SPawel Jakub Dawidek }
73232115b10SPawel Jakub Dawidek 
73332115b10SPawel Jakub Dawidek static void
73455ce1e7cSPawel Jakub Dawidek sync_stop(void)
73555ce1e7cSPawel Jakub Dawidek {
73655ce1e7cSPawel Jakub Dawidek 
73755ce1e7cSPawel Jakub Dawidek 	mtx_lock(&sync_lock);
73855ce1e7cSPawel Jakub Dawidek 	if (sync_inprogress)
73955ce1e7cSPawel Jakub Dawidek 		sync_inprogress = false;
74055ce1e7cSPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
74155ce1e7cSPawel Jakub Dawidek }
74255ce1e7cSPawel Jakub Dawidek 
74355ce1e7cSPawel Jakub Dawidek static void
74432115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res)
74532115b10SPawel Jakub Dawidek {
74632115b10SPawel Jakub Dawidek 	struct g_gate_ctl_create ggiocreate;
74732115b10SPawel Jakub Dawidek 	struct g_gate_ctl_cancel ggiocancel;
74832115b10SPawel Jakub Dawidek 
74932115b10SPawel Jakub Dawidek 	/*
75032115b10SPawel Jakub Dawidek 	 * We communicate with ggate via /dev/ggctl. Open it.
75132115b10SPawel Jakub Dawidek 	 */
75232115b10SPawel Jakub Dawidek 	res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
75332115b10SPawel Jakub Dawidek 	if (res->hr_ggatefd < 0)
75432115b10SPawel Jakub Dawidek 		primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
75532115b10SPawel Jakub Dawidek 	/*
75632115b10SPawel Jakub Dawidek 	 * Create provider before trying to connect, as connection failure
75732115b10SPawel Jakub Dawidek 	 * is not critical, but may take some time.
75832115b10SPawel Jakub Dawidek 	 */
7594e47b646SPawel Jakub Dawidek 	bzero(&ggiocreate, sizeof(ggiocreate));
76032115b10SPawel Jakub Dawidek 	ggiocreate.gctl_version = G_GATE_VERSION;
76132115b10SPawel Jakub Dawidek 	ggiocreate.gctl_mediasize = res->hr_datasize;
76232115b10SPawel Jakub Dawidek 	ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
76332115b10SPawel Jakub Dawidek 	ggiocreate.gctl_flags = 0;
76420b77db9SPawel Jakub Dawidek 	ggiocreate.gctl_maxcount = G_GATE_MAX_QUEUE_SIZE;
76532115b10SPawel Jakub Dawidek 	ggiocreate.gctl_timeout = 0;
76632115b10SPawel Jakub Dawidek 	ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
76732115b10SPawel Jakub Dawidek 	snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
76832115b10SPawel Jakub Dawidek 	    res->hr_provname);
76932115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
77032115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s created.", res->hr_provname);
77132115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocreate.gctl_unit;
77232115b10SPawel Jakub Dawidek 		return;
77332115b10SPawel Jakub Dawidek 	}
77432115b10SPawel Jakub Dawidek 	if (errno != EEXIST) {
77532115b10SPawel Jakub Dawidek 		primary_exit(EX_OSERR, "Unable to create hast/%s device",
77632115b10SPawel Jakub Dawidek 		    res->hr_provname);
77732115b10SPawel Jakub Dawidek 	}
77832115b10SPawel Jakub Dawidek 	pjdlog_debug(1,
77932115b10SPawel Jakub Dawidek 	    "Device hast/%s already exists, we will try to take it over.",
78032115b10SPawel Jakub Dawidek 	    res->hr_provname);
78132115b10SPawel Jakub Dawidek 	/*
78232115b10SPawel Jakub Dawidek 	 * If we received EEXIST, we assume that the process who created the
78332115b10SPawel Jakub Dawidek 	 * provider died and didn't clean up. In that case we will start from
78432115b10SPawel Jakub Dawidek 	 * where he left of.
78532115b10SPawel Jakub Dawidek 	 */
7864e47b646SPawel Jakub Dawidek 	bzero(&ggiocancel, sizeof(ggiocancel));
78732115b10SPawel Jakub Dawidek 	ggiocancel.gctl_version = G_GATE_VERSION;
78832115b10SPawel Jakub Dawidek 	ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
78932115b10SPawel Jakub Dawidek 	snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
79032115b10SPawel Jakub Dawidek 	    res->hr_provname);
79132115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
79232115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s recovered.", res->hr_provname);
79332115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocancel.gctl_unit;
79432115b10SPawel Jakub Dawidek 		return;
79532115b10SPawel Jakub Dawidek 	}
79632115b10SPawel Jakub Dawidek 	primary_exit(EX_OSERR, "Unable to take over hast/%s device",
79732115b10SPawel Jakub Dawidek 	    res->hr_provname);
79832115b10SPawel Jakub Dawidek }
79932115b10SPawel Jakub Dawidek 
80032115b10SPawel Jakub Dawidek void
80132115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res)
80232115b10SPawel Jakub Dawidek {
80332115b10SPawel Jakub Dawidek 	pthread_t td;
80432115b10SPawel Jakub Dawidek 	pid_t pid;
805bc7a916aSMikolaj Golub 	int error, mode, debuglevel;
80632115b10SPawel Jakub Dawidek 
80732115b10SPawel Jakub Dawidek 	/*
80832ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending control commands from
80932ecf620SPawel Jakub Dawidek 	 * parent to child.
81032115b10SPawel Jakub Dawidek 	 */
8110b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
812d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
81332115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8146be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
81532115b10SPawel Jakub Dawidek 		    "Unable to create control sockets between parent and child");
81632115b10SPawel Jakub Dawidek 	}
8175bdff860SPawel Jakub Dawidek 	/*
81832ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending events from child to parent.
8195bdff860SPawel Jakub Dawidek 	 */
8200b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
821d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
8225bdff860SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8235bdff860SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
8245bdff860SPawel Jakub Dawidek 		    "Unable to create event sockets between child and parent");
8255bdff860SPawel Jakub Dawidek 	}
82632ecf620SPawel Jakub Dawidek 	/*
82732ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending connection requests from
82832ecf620SPawel Jakub Dawidek 	 * child to parent.
82932ecf620SPawel Jakub Dawidek 	 */
8300b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) {
83132ecf620SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
83232ecf620SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
83332ecf620SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
83432ecf620SPawel Jakub Dawidek 		    "Unable to create connection sockets between child and parent");
83532ecf620SPawel Jakub Dawidek 	}
83632115b10SPawel Jakub Dawidek 
83732115b10SPawel Jakub Dawidek 	pid = fork();
83832115b10SPawel Jakub Dawidek 	if (pid < 0) {
839d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
84032115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8416be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
84232115b10SPawel Jakub Dawidek 	}
84332115b10SPawel Jakub Dawidek 
84432115b10SPawel Jakub Dawidek 	if (pid > 0) {
84532115b10SPawel Jakub Dawidek 		/* This is parent. */
8465bdff860SPawel Jakub Dawidek 		/* Declare that we are receiver. */
8475bdff860SPawel Jakub Dawidek 		proto_recv(res->hr_event, NULL, 0);
84832ecf620SPawel Jakub Dawidek 		proto_recv(res->hr_conn, NULL, 0);
849da1783eaSPawel Jakub Dawidek 		/* Declare that we are sender. */
850da1783eaSPawel Jakub Dawidek 		proto_send(res->hr_ctrl, NULL, 0);
85132115b10SPawel Jakub Dawidek 		res->hr_workerpid = pid;
85232115b10SPawel Jakub Dawidek 		return;
85332115b10SPawel Jakub Dawidek 	}
854ecc99c89SPawel Jakub Dawidek 
8555b41e644SPawel Jakub Dawidek 	gres = res;
856da1783eaSPawel Jakub Dawidek 	mode = pjdlog_mode_get();
857bc7a916aSMikolaj Golub 	debuglevel = pjdlog_debug_get();
85832115b10SPawel Jakub Dawidek 
8595bdff860SPawel Jakub Dawidek 	/* Declare that we are sender. */
8605bdff860SPawel Jakub Dawidek 	proto_send(res->hr_event, NULL, 0);
86132ecf620SPawel Jakub Dawidek 	proto_send(res->hr_conn, NULL, 0);
862da1783eaSPawel Jakub Dawidek 	/* Declare that we are receiver. */
863da1783eaSPawel Jakub Dawidek 	proto_recv(res->hr_ctrl, NULL, 0);
864da1783eaSPawel Jakub Dawidek 	descriptors_cleanup(res);
865da1783eaSPawel Jakub Dawidek 
866f463896eSPawel Jakub Dawidek 	descriptors_assert(res, mode);
867f463896eSPawel Jakub Dawidek 
868da1783eaSPawel Jakub Dawidek 	pjdlog_init(mode);
869bc7a916aSMikolaj Golub 	pjdlog_debug_set(debuglevel);
870da1783eaSPawel Jakub Dawidek 	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
871*643080b7SPawel Jakub Dawidek 	setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
8725bdff860SPawel Jakub Dawidek 
87332115b10SPawel Jakub Dawidek 	init_local(res);
87432115b10SPawel Jakub Dawidek 	init_ggate(res);
87532115b10SPawel Jakub Dawidek 	init_environment(res);
876115f4e5cSPawel Jakub Dawidek 
8774d8dc3b8SPawel Jakub Dawidek 	if (drop_privs(true) != 0) {
8786d7967deSPawel Jakub Dawidek 		cleanup(res);
8796d7967deSPawel Jakub Dawidek 		exit(EX_CONFIG);
8806d7967deSPawel Jakub Dawidek 	}
881f4c96f94SPawel Jakub Dawidek 	pjdlog_info("Privileges successfully dropped.");
8826d7967deSPawel Jakub Dawidek 
8838b70e6aeSPawel Jakub Dawidek 	/*
8844a88128bSPawel Jakub Dawidek 	 * Create the guard thread first, so we can handle signals from the
8854a88128bSPawel Jakub Dawidek 	 * very begining.
8864a88128bSPawel Jakub Dawidek 	 */
8874a88128bSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, guard_thread, res);
8882ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
8894a88128bSPawel Jakub Dawidek 	/*
8908b70e6aeSPawel Jakub Dawidek 	 * Create the control thread before sending any event to the parent,
8918b70e6aeSPawel Jakub Dawidek 	 * as we can deadlock when parent sends control request to worker,
8928b70e6aeSPawel Jakub Dawidek 	 * but worker has no control thread started yet, so parent waits.
8938b70e6aeSPawel Jakub Dawidek 	 * In the meantime worker sends an event to the parent, but parent
8948b70e6aeSPawel Jakub Dawidek 	 * is unable to handle the event, because it waits for control
8958b70e6aeSPawel Jakub Dawidek 	 * request response.
8968b70e6aeSPawel Jakub Dawidek 	 */
8978b70e6aeSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ctrl_thread, res);
8982ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
8998b70e6aeSPawel Jakub Dawidek 	if (real_remote(res) && init_remote(res, NULL, NULL))
9008b70e6aeSPawel Jakub Dawidek 		sync_start();
90132115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_recv_thread, res);
9022ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
90332115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, local_send_thread, res);
9042ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
90532115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_send_thread, res);
9062ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
90732115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_recv_thread, res);
9082ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
90932115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_send_thread, res);
9102ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
9114a88128bSPawel Jakub Dawidek 	(void)sync_thread(res);
91232115b10SPawel Jakub Dawidek }
91332115b10SPawel Jakub Dawidek 
91432115b10SPawel Jakub Dawidek static void
91532115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...)
91632115b10SPawel Jakub Dawidek {
91732115b10SPawel Jakub Dawidek 	char msg[1024];
91832115b10SPawel Jakub Dawidek 	va_list ap;
91932115b10SPawel Jakub Dawidek 	int len;
92032115b10SPawel Jakub Dawidek 
92132115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
92232115b10SPawel Jakub Dawidek 	len = vsnprintf(msg, sizeof(msg), fmt, ap);
92332115b10SPawel Jakub Dawidek 	va_end(ap);
92432115b10SPawel Jakub Dawidek 	if ((size_t)len < sizeof(msg)) {
92532115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
92632115b10SPawel Jakub Dawidek 		case BIO_READ:
92732115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
92832115b10SPawel Jakub Dawidek 			    "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
92932115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
93032115b10SPawel Jakub Dawidek 			break;
93132115b10SPawel Jakub Dawidek 		case BIO_DELETE:
93232115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
93332115b10SPawel Jakub Dawidek 			    "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
93432115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
93532115b10SPawel Jakub Dawidek 			break;
93632115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
93732115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
93832115b10SPawel Jakub Dawidek 			break;
93932115b10SPawel Jakub Dawidek 		case BIO_WRITE:
94032115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
94132115b10SPawel Jakub Dawidek 			    "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
94232115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
94332115b10SPawel Jakub Dawidek 			break;
94432115b10SPawel Jakub Dawidek 		default:
94532115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
94632115b10SPawel Jakub Dawidek 			    "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd);
94732115b10SPawel Jakub Dawidek 			break;
94832115b10SPawel Jakub Dawidek 		}
94932115b10SPawel Jakub Dawidek 	}
95032115b10SPawel Jakub Dawidek 	pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
95132115b10SPawel Jakub Dawidek }
95232115b10SPawel Jakub Dawidek 
95332115b10SPawel Jakub Dawidek static void
95432115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp)
95532115b10SPawel Jakub Dawidek {
95632115b10SPawel Jakub Dawidek 
95732115b10SPawel Jakub Dawidek 	rw_wlock(&hio_remote_lock[ncomp]);
95832115b10SPawel Jakub Dawidek 	/*
95932115b10SPawel Jakub Dawidek 	 * A race is possible between dropping rlock and acquiring wlock -
96032115b10SPawel Jakub Dawidek 	 * another thread can close connection in-between.
96132115b10SPawel Jakub Dawidek 	 */
96232115b10SPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
9632ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
9642ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
96532115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
96632115b10SPawel Jakub Dawidek 		return;
96732115b10SPawel Jakub Dawidek 	}
96832115b10SPawel Jakub Dawidek 
9692ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
9702ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
97132115b10SPawel Jakub Dawidek 
9728f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing incoming connection to %s.",
97332115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
97432115b10SPawel Jakub Dawidek 	proto_close(res->hr_remotein);
97532115b10SPawel Jakub Dawidek 	res->hr_remotein = NULL;
9768f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing outgoing connection to %s.",
97732115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
97832115b10SPawel Jakub Dawidek 	proto_close(res->hr_remoteout);
97932115b10SPawel Jakub Dawidek 	res->hr_remoteout = NULL;
98032115b10SPawel Jakub Dawidek 
98132115b10SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
98232115b10SPawel Jakub Dawidek 
9838f8c798cSPawel Jakub Dawidek 	pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
9848f8c798cSPawel Jakub Dawidek 
98532115b10SPawel Jakub Dawidek 	/*
98632115b10SPawel Jakub Dawidek 	 * Stop synchronization if in-progress.
98732115b10SPawel Jakub Dawidek 	 */
98855ce1e7cSPawel Jakub Dawidek 	sync_stop();
9895b41e644SPawel Jakub Dawidek 
9905bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_DISCONNECT);
991f7fe83f9SPawel Jakub Dawidek }
99232115b10SPawel Jakub Dawidek 
99332115b10SPawel Jakub Dawidek /*
99432115b10SPawel Jakub Dawidek  * Thread receives ggate I/O requests from the kernel and passes them to
99532115b10SPawel Jakub Dawidek  * appropriate threads:
99632115b10SPawel Jakub Dawidek  * WRITE - always goes to both local_send and remote_send threads
99732115b10SPawel Jakub Dawidek  * READ (when the block is up-to-date on local component) -
99832115b10SPawel Jakub Dawidek  *	only local_send thread
99932115b10SPawel Jakub Dawidek  * READ (when the block isn't up-to-date on local component) -
100032115b10SPawel Jakub Dawidek  *	only remote_send thread
100132115b10SPawel Jakub Dawidek  * DELETE - always goes to both local_send and remote_send threads
100232115b10SPawel Jakub Dawidek  * FLUSH - always goes to both local_send and remote_send threads
100332115b10SPawel Jakub Dawidek  */
100432115b10SPawel Jakub Dawidek static void *
100532115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg)
100632115b10SPawel Jakub Dawidek {
100732115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
100832115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
100932115b10SPawel Jakub Dawidek 	struct hio *hio;
101032115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
101132115b10SPawel Jakub Dawidek 	int error;
101232115b10SPawel Jakub Dawidek 
101332115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
101432115b10SPawel Jakub Dawidek 
101532115b10SPawel Jakub Dawidek 	for (;;) {
101632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: Taking free request.");
101732115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
101832115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
101932115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
102032115b10SPawel Jakub Dawidek 		ggio->gctl_unit = res->hr_ggateunit;
102132115b10SPawel Jakub Dawidek 		ggio->gctl_length = MAXPHYS;
102232115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
102332115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
102432115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Waiting for request from the kernel.",
102532115b10SPawel Jakub Dawidek 		    hio);
102632115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) {
102732115b10SPawel Jakub Dawidek 			if (sigexit_received)
102832115b10SPawel Jakub Dawidek 				pthread_exit(NULL);
102932115b10SPawel Jakub Dawidek 			primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
103032115b10SPawel Jakub Dawidek 		}
103132115b10SPawel Jakub Dawidek 		error = ggio->gctl_error;
103232115b10SPawel Jakub Dawidek 		switch (error) {
103332115b10SPawel Jakub Dawidek 		case 0:
103432115b10SPawel Jakub Dawidek 			break;
103532115b10SPawel Jakub Dawidek 		case ECANCELED:
103632115b10SPawel Jakub Dawidek 			/* Exit gracefully. */
103732115b10SPawel Jakub Dawidek 			if (!sigexit_received) {
103832115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
103932115b10SPawel Jakub Dawidek 				    "ggate_recv: (%p) Received cancel from the kernel.",
104032115b10SPawel Jakub Dawidek 				    hio);
104132115b10SPawel Jakub Dawidek 				pjdlog_info("Received cancel from the kernel, exiting.");
104232115b10SPawel Jakub Dawidek 			}
104332115b10SPawel Jakub Dawidek 			pthread_exit(NULL);
104432115b10SPawel Jakub Dawidek 		case ENOMEM:
104532115b10SPawel Jakub Dawidek 			/*
104632115b10SPawel Jakub Dawidek 			 * Buffer too small? Impossible, we allocate MAXPHYS
104732115b10SPawel Jakub Dawidek 			 * bytes - request can't be bigger than that.
104832115b10SPawel Jakub Dawidek 			 */
104932115b10SPawel Jakub Dawidek 			/* FALLTHROUGH */
105032115b10SPawel Jakub Dawidek 		case ENXIO:
105132115b10SPawel Jakub Dawidek 		default:
105232115b10SPawel Jakub Dawidek 			primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
105332115b10SPawel Jakub Dawidek 			    strerror(error));
105432115b10SPawel Jakub Dawidek 		}
105532115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
105632115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
105732115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio,
105832115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Request received from the kernel: ",
105932115b10SPawel Jakub Dawidek 		    hio);
106032115b10SPawel Jakub Dawidek 		/*
106132115b10SPawel Jakub Dawidek 		 * Inform all components about new write request.
106232115b10SPawel Jakub Dawidek 		 * For read request prefer local component unless the given
106332115b10SPawel Jakub Dawidek 		 * range is out-of-date, then use remote component.
106432115b10SPawel Jakub Dawidek 		 */
106532115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
106632115b10SPawel Jakub Dawidek 		case BIO_READ:
106732115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
106832115b10SPawel Jakub Dawidek 			    "ggate_recv: (%p) Moving request to the send queue.",
106932115b10SPawel Jakub Dawidek 			    hio);
107032115b10SPawel Jakub Dawidek 			refcount_init(&hio->hio_countdown, 1);
107132115b10SPawel Jakub Dawidek 			mtx_lock(&metadata_lock);
107232115b10SPawel Jakub Dawidek 			if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
107332115b10SPawel Jakub Dawidek 			    res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
107432115b10SPawel Jakub Dawidek 				/*
107532115b10SPawel Jakub Dawidek 				 * This range is up-to-date on local component,
107632115b10SPawel Jakub Dawidek 				 * so handle request locally.
107732115b10SPawel Jakub Dawidek 				 */
107832115b10SPawel Jakub Dawidek 				 /* Local component is 0 for now. */
107932115b10SPawel Jakub Dawidek 				ncomp = 0;
108032115b10SPawel Jakub Dawidek 			} else /* if (res->hr_syncsrc ==
108132115b10SPawel Jakub Dawidek 			    HAST_SYNCSRC_SECONDARY) */ {
10822ec483c5SPawel Jakub Dawidek 				PJDLOG_ASSERT(res->hr_syncsrc ==
108332115b10SPawel Jakub Dawidek 				    HAST_SYNCSRC_SECONDARY);
108432115b10SPawel Jakub Dawidek 				/*
108532115b10SPawel Jakub Dawidek 				 * This range is out-of-date on local component,
108632115b10SPawel Jakub Dawidek 				 * so send request to the remote node.
108732115b10SPawel Jakub Dawidek 				 */
108832115b10SPawel Jakub Dawidek 				 /* Remote component is 1 for now. */
108932115b10SPawel Jakub Dawidek 				ncomp = 1;
109032115b10SPawel Jakub Dawidek 			}
109132115b10SPawel Jakub Dawidek 			mtx_unlock(&metadata_lock);
109232115b10SPawel Jakub Dawidek 			QUEUE_INSERT1(hio, send, ncomp);
109332115b10SPawel Jakub Dawidek 			break;
109432115b10SPawel Jakub Dawidek 		case BIO_WRITE:
1095ce837469SPawel Jakub Dawidek 			if (res->hr_resuid == 0) {
10969446b453SPawel Jakub Dawidek 				/*
10979446b453SPawel Jakub Dawidek 				 * This is first write, initialize localcnt and
10989446b453SPawel Jakub Dawidek 				 * resuid.
10999446b453SPawel Jakub Dawidek 				 */
11009446b453SPawel Jakub Dawidek 				res->hr_primary_localcnt = 1;
1101ce837469SPawel Jakub Dawidek 				(void)init_resuid(res);
1102ce837469SPawel Jakub Dawidek 			}
110332115b10SPawel Jakub Dawidek 			for (;;) {
110432115b10SPawel Jakub Dawidek 				mtx_lock(&range_lock);
110532115b10SPawel Jakub Dawidek 				if (rangelock_islocked(range_sync,
110632115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length)) {
110732115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
110832115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu locked.",
110932115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
111032115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
111132115b10SPawel Jakub Dawidek 					range_regular_wait = true;
111232115b10SPawel Jakub Dawidek 					cv_wait(&range_regular_cond, &range_lock);
111332115b10SPawel Jakub Dawidek 					range_regular_wait = false;
111432115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
111532115b10SPawel Jakub Dawidek 					continue;
111632115b10SPawel Jakub Dawidek 				}
111732115b10SPawel Jakub Dawidek 				if (rangelock_add(range_regular,
111832115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length) < 0) {
111932115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
112032115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
112132115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu is already locked, waiting.",
112232115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
112332115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
112432115b10SPawel Jakub Dawidek 					sleep(1);
112532115b10SPawel Jakub Dawidek 					continue;
112632115b10SPawel Jakub Dawidek 				}
112732115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
112832115b10SPawel Jakub Dawidek 				break;
112932115b10SPawel Jakub Dawidek 			}
113032115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
113132115b10SPawel Jakub Dawidek 			if (activemap_write_start(res->hr_amp,
113232115b10SPawel Jakub Dawidek 			    ggio->gctl_offset, ggio->gctl_length)) {
113332115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
113432115b10SPawel Jakub Dawidek 			}
113532115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
113632115b10SPawel Jakub Dawidek 			/* FALLTHROUGH */
113732115b10SPawel Jakub Dawidek 		case BIO_DELETE:
113832115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
113932115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
114032115b10SPawel Jakub Dawidek 			    "ggate_recv: (%p) Moving request to the send queues.",
114132115b10SPawel Jakub Dawidek 			    hio);
114232115b10SPawel Jakub Dawidek 			refcount_init(&hio->hio_countdown, ncomps);
114332115b10SPawel Jakub Dawidek 			for (ii = 0; ii < ncomps; ii++)
114432115b10SPawel Jakub Dawidek 				QUEUE_INSERT1(hio, send, ii);
114532115b10SPawel Jakub Dawidek 			break;
114632115b10SPawel Jakub Dawidek 		}
114732115b10SPawel Jakub Dawidek 	}
114832115b10SPawel Jakub Dawidek 	/* NOTREACHED */
114932115b10SPawel Jakub Dawidek 	return (NULL);
115032115b10SPawel Jakub Dawidek }
115132115b10SPawel Jakub Dawidek 
115232115b10SPawel Jakub Dawidek /*
115332115b10SPawel Jakub Dawidek  * Thread reads from or writes to local component.
115432115b10SPawel Jakub Dawidek  * If local read fails, it redirects it to remote_send thread.
115532115b10SPawel Jakub Dawidek  */
115632115b10SPawel Jakub Dawidek static void *
115732115b10SPawel Jakub Dawidek local_send_thread(void *arg)
115832115b10SPawel Jakub Dawidek {
115932115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
116032115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
116132115b10SPawel Jakub Dawidek 	struct hio *hio;
116232115b10SPawel Jakub Dawidek 	unsigned int ncomp, rncomp;
116332115b10SPawel Jakub Dawidek 	ssize_t ret;
116432115b10SPawel Jakub Dawidek 
116532115b10SPawel Jakub Dawidek 	/* Local component is 0 for now. */
116632115b10SPawel Jakub Dawidek 	ncomp = 0;
116732115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
116832115b10SPawel Jakub Dawidek 	rncomp = 1;
116932115b10SPawel Jakub Dawidek 
117032115b10SPawel Jakub Dawidek 	for (;;) {
117132115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: Taking request.");
1172448efa94SPawel Jakub Dawidek 		QUEUE_TAKE1(hio, send, ncomp, 0);
117332115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: (%p) Got request.", hio);
117432115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
117532115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
117632115b10SPawel Jakub Dawidek 		case BIO_READ:
117732115b10SPawel Jakub Dawidek 			ret = pread(res->hr_localfd, ggio->gctl_data,
117832115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
117932115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
118032115b10SPawel Jakub Dawidek 			if (ret == ggio->gctl_length)
118132115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
118232115b10SPawel Jakub Dawidek 			else {
118332115b10SPawel Jakub Dawidek 				/*
118432115b10SPawel Jakub Dawidek 				 * If READ failed, try to read from remote node.
118532115b10SPawel Jakub Dawidek 				 */
1186cd7b7ee5SPawel Jakub Dawidek 				if (ret < 0) {
1187cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1188cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%s), trying remote node. ",
1189cd7b7ee5SPawel Jakub Dawidek 					    strerror(errno));
1190cd7b7ee5SPawel Jakub Dawidek 				} else if (ret != ggio->gctl_length) {
1191cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1192cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%zd != %jd), trying remote node. ",
1193fba1bf5aSPawel Jakub Dawidek 					    ret, (intmax_t)ggio->gctl_length);
1194cd7b7ee5SPawel Jakub Dawidek 				}
119532115b10SPawel Jakub Dawidek 				QUEUE_INSERT1(hio, send, rncomp);
119632115b10SPawel Jakub Dawidek 				continue;
119732115b10SPawel Jakub Dawidek 			}
119832115b10SPawel Jakub Dawidek 			break;
119932115b10SPawel Jakub Dawidek 		case BIO_WRITE:
120032115b10SPawel Jakub Dawidek 			ret = pwrite(res->hr_localfd, ggio->gctl_data,
120132115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
120232115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
1203cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
120432115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1205cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1206cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1207cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1208cd7b7ee5SPawel Jakub Dawidek 			} else if (ret != ggio->gctl_length) {
120932115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = EIO;
1210cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1211cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%zd != %jd): ",
1212fba1bf5aSPawel Jakub Dawidek 				    ret, (intmax_t)ggio->gctl_length);
1213cd7b7ee5SPawel Jakub Dawidek 			} else {
121432115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1215cd7b7ee5SPawel Jakub Dawidek 			}
121632115b10SPawel Jakub Dawidek 			break;
121732115b10SPawel Jakub Dawidek 		case BIO_DELETE:
121832115b10SPawel Jakub Dawidek 			ret = g_delete(res->hr_localfd,
121932115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff,
122032115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
1221cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
122232115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1223cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1224cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1225cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1226cd7b7ee5SPawel Jakub Dawidek 			} else {
122732115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1228cd7b7ee5SPawel Jakub Dawidek 			}
122932115b10SPawel Jakub Dawidek 			break;
123032115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
123132115b10SPawel Jakub Dawidek 			ret = g_flush(res->hr_localfd);
1232cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
123332115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1234cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1235cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1236cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1237cd7b7ee5SPawel Jakub Dawidek 			} else {
123832115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1239cd7b7ee5SPawel Jakub Dawidek 			}
124032115b10SPawel Jakub Dawidek 			break;
124132115b10SPawel Jakub Dawidek 		}
124232115b10SPawel Jakub Dawidek 		if (refcount_release(&hio->hio_countdown)) {
124332115b10SPawel Jakub Dawidek 			if (ISSYNCREQ(hio)) {
124432115b10SPawel Jakub Dawidek 				mtx_lock(&sync_lock);
124532115b10SPawel Jakub Dawidek 				SYNCREQDONE(hio);
124632115b10SPawel Jakub Dawidek 				mtx_unlock(&sync_lock);
124732115b10SPawel Jakub Dawidek 				cv_signal(&sync_cond);
124832115b10SPawel Jakub Dawidek 			} else {
124932115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
125032115b10SPawel Jakub Dawidek 				    "local_send: (%p) Moving request to the done queue.",
125132115b10SPawel Jakub Dawidek 				    hio);
125232115b10SPawel Jakub Dawidek 				QUEUE_INSERT2(hio, done);
125332115b10SPawel Jakub Dawidek 			}
125432115b10SPawel Jakub Dawidek 		}
125532115b10SPawel Jakub Dawidek 	}
125632115b10SPawel Jakub Dawidek 	/* NOTREACHED */
125732115b10SPawel Jakub Dawidek 	return (NULL);
125832115b10SPawel Jakub Dawidek }
125932115b10SPawel Jakub Dawidek 
1260448efa94SPawel Jakub Dawidek static void
1261448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp)
1262448efa94SPawel Jakub Dawidek {
1263448efa94SPawel Jakub Dawidek 	struct nv *nv;
1264448efa94SPawel Jakub Dawidek 
126521e7bc5eSPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
126621e7bc5eSPawel Jakub Dawidek 
126721e7bc5eSPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
126821e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1269448efa94SPawel Jakub Dawidek 		return;
127021e7bc5eSPawel Jakub Dawidek 	}
1271448efa94SPawel Jakub Dawidek 
12722ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
12732ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
1274448efa94SPawel Jakub Dawidek 
1275448efa94SPawel Jakub Dawidek 	nv = nv_alloc();
1276448efa94SPawel Jakub Dawidek 	nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1277448efa94SPawel Jakub Dawidek 	if (nv_error(nv) != 0) {
127821e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1279448efa94SPawel Jakub Dawidek 		nv_free(nv);
1280448efa94SPawel Jakub Dawidek 		pjdlog_debug(1,
1281448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to prepare header to send.");
1282448efa94SPawel Jakub Dawidek 		return;
1283448efa94SPawel Jakub Dawidek 	}
1284448efa94SPawel Jakub Dawidek 	if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) {
128521e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1286448efa94SPawel Jakub Dawidek 		pjdlog_common(LOG_DEBUG, 1, errno,
1287448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to send request");
1288448efa94SPawel Jakub Dawidek 		nv_free(nv);
1289448efa94SPawel Jakub Dawidek 		remote_close(res, ncomp);
1290448efa94SPawel Jakub Dawidek 		return;
1291448efa94SPawel Jakub Dawidek 	}
129221e7bc5eSPawel Jakub Dawidek 
129321e7bc5eSPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
1294448efa94SPawel Jakub Dawidek 	nv_free(nv);
1295448efa94SPawel Jakub Dawidek 	pjdlog_debug(2, "keepalive_send: Request sent.");
1296448efa94SPawel Jakub Dawidek }
1297448efa94SPawel Jakub Dawidek 
129832115b10SPawel Jakub Dawidek /*
129932115b10SPawel Jakub Dawidek  * Thread sends request to secondary node.
130032115b10SPawel Jakub Dawidek  */
130132115b10SPawel Jakub Dawidek static void *
130232115b10SPawel Jakub Dawidek remote_send_thread(void *arg)
130332115b10SPawel Jakub Dawidek {
130432115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
130532115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1306448efa94SPawel Jakub Dawidek 	time_t lastcheck, now;
130732115b10SPawel Jakub Dawidek 	struct hio *hio;
130832115b10SPawel Jakub Dawidek 	struct nv *nv;
130932115b10SPawel Jakub Dawidek 	unsigned int ncomp;
131032115b10SPawel Jakub Dawidek 	bool wakeup;
131132115b10SPawel Jakub Dawidek 	uint64_t offset, length;
131232115b10SPawel Jakub Dawidek 	uint8_t cmd;
131332115b10SPawel Jakub Dawidek 	void *data;
131432115b10SPawel Jakub Dawidek 
131532115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
131632115b10SPawel Jakub Dawidek 	ncomp = 1;
1317448efa94SPawel Jakub Dawidek 	lastcheck = time(NULL);
131832115b10SPawel Jakub Dawidek 
131932115b10SPawel Jakub Dawidek 	for (;;) {
132032115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: Taking request.");
13218d7dcf14SMikolaj Golub 		QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1322448efa94SPawel Jakub Dawidek 		if (hio == NULL) {
1323448efa94SPawel Jakub Dawidek 			now = time(NULL);
13248d7dcf14SMikolaj Golub 			if (lastcheck + HAST_KEEPALIVE <= now) {
1325448efa94SPawel Jakub Dawidek 				keepalive_send(res, ncomp);
1326448efa94SPawel Jakub Dawidek 				lastcheck = now;
1327448efa94SPawel Jakub Dawidek 			}
1328448efa94SPawel Jakub Dawidek 			continue;
1329448efa94SPawel Jakub Dawidek 		}
133032115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
133132115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
133232115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
133332115b10SPawel Jakub Dawidek 		case BIO_READ:
133432115b10SPawel Jakub Dawidek 			cmd = HIO_READ;
133532115b10SPawel Jakub Dawidek 			data = NULL;
133632115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
133732115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
133832115b10SPawel Jakub Dawidek 			break;
133932115b10SPawel Jakub Dawidek 		case BIO_WRITE:
134032115b10SPawel Jakub Dawidek 			cmd = HIO_WRITE;
134132115b10SPawel Jakub Dawidek 			data = ggio->gctl_data;
134232115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
134332115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
134432115b10SPawel Jakub Dawidek 			break;
134532115b10SPawel Jakub Dawidek 		case BIO_DELETE:
134632115b10SPawel Jakub Dawidek 			cmd = HIO_DELETE;
134732115b10SPawel Jakub Dawidek 			data = NULL;
134832115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
134932115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
135032115b10SPawel Jakub Dawidek 			break;
135132115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
135232115b10SPawel Jakub Dawidek 			cmd = HIO_FLUSH;
135332115b10SPawel Jakub Dawidek 			data = NULL;
135432115b10SPawel Jakub Dawidek 			offset = 0;
135532115b10SPawel Jakub Dawidek 			length = 0;
135632115b10SPawel Jakub Dawidek 			break;
135732115b10SPawel Jakub Dawidek 		default:
13582ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(!"invalid condition");
135932115b10SPawel Jakub Dawidek 			abort();
136032115b10SPawel Jakub Dawidek 		}
136132115b10SPawel Jakub Dawidek 		nv = nv_alloc();
136232115b10SPawel Jakub Dawidek 		nv_add_uint8(nv, cmd, "cmd");
136332115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
136432115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, offset, "offset");
136532115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, length, "length");
136632115b10SPawel Jakub Dawidek 		if (nv_error(nv) != 0) {
136732115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = nv_error(nv);
136832115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
136932115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to prepare header to send.",
137032115b10SPawel Jakub Dawidek 			    hio);
137132115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
137232115b10SPawel Jakub Dawidek 			    "Unable to prepare header to send (%s): ",
137332115b10SPawel Jakub Dawidek 			    strerror(nv_error(nv)));
137432115b10SPawel Jakub Dawidek 			/* Move failed request immediately to the done queue. */
137532115b10SPawel Jakub Dawidek 			goto done_queue;
137632115b10SPawel Jakub Dawidek 		}
137732115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
137832115b10SPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the recv queue.",
137932115b10SPawel Jakub Dawidek 		    hio);
138032115b10SPawel Jakub Dawidek 		/*
138132115b10SPawel Jakub Dawidek 		 * Protect connection from disappearing.
138232115b10SPawel Jakub Dawidek 		 */
138332115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
138432115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
138532115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
138632115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = ENOTCONN;
138732115b10SPawel Jakub Dawidek 			goto done_queue;
138832115b10SPawel Jakub Dawidek 		}
138932115b10SPawel Jakub Dawidek 		/*
139032115b10SPawel Jakub Dawidek 		 * Move the request to recv queue before sending it, because
139132115b10SPawel Jakub Dawidek 		 * in different order we can get reply before we move request
139232115b10SPawel Jakub Dawidek 		 * to recv queue.
139332115b10SPawel Jakub Dawidek 		 */
139432115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
139532115b10SPawel Jakub Dawidek 		wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
139632115b10SPawel Jakub Dawidek 		TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
139732115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
139832115b10SPawel Jakub Dawidek 		if (hast_proto_send(res, res->hr_remoteout, nv, data,
139932115b10SPawel Jakub Dawidek 		    data != NULL ? length : 0) < 0) {
140032115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = errno;
140132115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
140232115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
140332115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to send request.", hio);
140432115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
140532115b10SPawel Jakub Dawidek 			    "Unable to send request (%s): ",
140632115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
1407ee087cdfSPawel Jakub Dawidek 			remote_close(res, ncomp);
140832115b10SPawel Jakub Dawidek 			/*
140932115b10SPawel Jakub Dawidek 			 * Take request back from the receive queue and move
141032115b10SPawel Jakub Dawidek 			 * it immediately to the done queue.
141132115b10SPawel Jakub Dawidek 			 */
141232115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
141332115b10SPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
141432115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
141532115b10SPawel Jakub Dawidek 			goto done_queue;
141632115b10SPawel Jakub Dawidek 		}
141732115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
141832115b10SPawel Jakub Dawidek 		nv_free(nv);
141932115b10SPawel Jakub Dawidek 		if (wakeup)
142032115b10SPawel Jakub Dawidek 			cv_signal(&hio_recv_list_cond[ncomp]);
142132115b10SPawel Jakub Dawidek 		continue;
142232115b10SPawel Jakub Dawidek done_queue:
142332115b10SPawel Jakub Dawidek 		nv_free(nv);
142432115b10SPawel Jakub Dawidek 		if (ISSYNCREQ(hio)) {
142532115b10SPawel Jakub Dawidek 			if (!refcount_release(&hio->hio_countdown))
142632115b10SPawel Jakub Dawidek 				continue;
142732115b10SPawel Jakub Dawidek 			mtx_lock(&sync_lock);
142832115b10SPawel Jakub Dawidek 			SYNCREQDONE(hio);
142932115b10SPawel Jakub Dawidek 			mtx_unlock(&sync_lock);
143032115b10SPawel Jakub Dawidek 			cv_signal(&sync_cond);
143132115b10SPawel Jakub Dawidek 			continue;
143232115b10SPawel Jakub Dawidek 		}
143332115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
143432115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
143532115b10SPawel Jakub Dawidek 			if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
143632115b10SPawel Jakub Dawidek 			    ggio->gctl_length)) {
143732115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
143832115b10SPawel Jakub Dawidek 			}
143932115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
144032115b10SPawel Jakub Dawidek 		}
144132115b10SPawel Jakub Dawidek 		if (!refcount_release(&hio->hio_countdown))
144232115b10SPawel Jakub Dawidek 			continue;
144332115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
144432115b10SPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the done queue.",
144532115b10SPawel Jakub Dawidek 		    hio);
144632115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, done);
144732115b10SPawel Jakub Dawidek 	}
144832115b10SPawel Jakub Dawidek 	/* NOTREACHED */
144932115b10SPawel Jakub Dawidek 	return (NULL);
145032115b10SPawel Jakub Dawidek }
145132115b10SPawel Jakub Dawidek 
145232115b10SPawel Jakub Dawidek /*
145332115b10SPawel Jakub Dawidek  * Thread receives answer from secondary node and passes it to ggate_send
145432115b10SPawel Jakub Dawidek  * thread.
145532115b10SPawel Jakub Dawidek  */
145632115b10SPawel Jakub Dawidek static void *
145732115b10SPawel Jakub Dawidek remote_recv_thread(void *arg)
145832115b10SPawel Jakub Dawidek {
145932115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
146032115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
146132115b10SPawel Jakub Dawidek 	struct hio *hio;
146232115b10SPawel Jakub Dawidek 	struct nv *nv;
146332115b10SPawel Jakub Dawidek 	unsigned int ncomp;
146432115b10SPawel Jakub Dawidek 	uint64_t seq;
146532115b10SPawel Jakub Dawidek 	int error;
146632115b10SPawel Jakub Dawidek 
146732115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
146832115b10SPawel Jakub Dawidek 	ncomp = 1;
146932115b10SPawel Jakub Dawidek 
147032115b10SPawel Jakub Dawidek 	for (;;) {
147132115b10SPawel Jakub Dawidek 		/* Wait until there is anything to receive. */
147232115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
147332115b10SPawel Jakub Dawidek 		while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
147432115b10SPawel Jakub Dawidek 			pjdlog_debug(2, "remote_recv: No requests, waiting.");
147532115b10SPawel Jakub Dawidek 			cv_wait(&hio_recv_list_cond[ncomp],
147632115b10SPawel Jakub Dawidek 			    &hio_recv_list_lock[ncomp]);
147732115b10SPawel Jakub Dawidek 		}
147832115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
147932115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
148032115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
148132115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
148232115b10SPawel Jakub Dawidek 			/*
148332115b10SPawel Jakub Dawidek 			 * Connection is dead, so move all pending requests to
148432115b10SPawel Jakub Dawidek 			 * the done queue (one-by-one).
148532115b10SPawel Jakub Dawidek 			 */
148632115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
148732115b10SPawel Jakub Dawidek 			hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
14882ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(hio != NULL);
148932115b10SPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
149032115b10SPawel Jakub Dawidek 			    hio_next[ncomp]);
149132115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
149232115b10SPawel Jakub Dawidek 			goto done_queue;
149332115b10SPawel Jakub Dawidek 		}
149432115b10SPawel Jakub Dawidek 		if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) {
149532115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
149632115b10SPawel Jakub Dawidek 			    "Unable to receive reply header");
149732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
149832115b10SPawel Jakub Dawidek 			remote_close(res, ncomp);
149932115b10SPawel Jakub Dawidek 			continue;
150032115b10SPawel Jakub Dawidek 		}
150132115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
150232115b10SPawel Jakub Dawidek 		seq = nv_get_uint64(nv, "seq");
150332115b10SPawel Jakub Dawidek 		if (seq == 0) {
150432115b10SPawel Jakub Dawidek 			pjdlog_error("Header contains no 'seq' field.");
150532115b10SPawel Jakub Dawidek 			nv_free(nv);
150632115b10SPawel Jakub Dawidek 			continue;
150732115b10SPawel Jakub Dawidek 		}
150832115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
150932115b10SPawel Jakub Dawidek 		TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
151032115b10SPawel Jakub Dawidek 			if (hio->hio_ggio.gctl_seq == seq) {
151132115b10SPawel Jakub Dawidek 				TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
151232115b10SPawel Jakub Dawidek 				    hio_next[ncomp]);
151332115b10SPawel Jakub Dawidek 				break;
151432115b10SPawel Jakub Dawidek 			}
151532115b10SPawel Jakub Dawidek 		}
151632115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
151732115b10SPawel Jakub Dawidek 		if (hio == NULL) {
151832115b10SPawel Jakub Dawidek 			pjdlog_error("Found no request matching received 'seq' field (%ju).",
151932115b10SPawel Jakub Dawidek 			    (uintmax_t)seq);
152032115b10SPawel Jakub Dawidek 			nv_free(nv);
152132115b10SPawel Jakub Dawidek 			continue;
152232115b10SPawel Jakub Dawidek 		}
152332115b10SPawel Jakub Dawidek 		error = nv_get_int16(nv, "error");
152432115b10SPawel Jakub Dawidek 		if (error != 0) {
152532115b10SPawel Jakub Dawidek 			/* Request failed on remote side. */
152672089204SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = error;
1527cd7b7ee5SPawel Jakub Dawidek 			reqlog(LOG_WARNING, 0, &hio->hio_ggio,
1528cd7b7ee5SPawel Jakub Dawidek 			    "Remote request failed (%s): ", strerror(error));
152932115b10SPawel Jakub Dawidek 			nv_free(nv);
153032115b10SPawel Jakub Dawidek 			goto done_queue;
153132115b10SPawel Jakub Dawidek 		}
153232115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
153332115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
153432115b10SPawel Jakub Dawidek 		case BIO_READ:
153532115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
153632115b10SPawel Jakub Dawidek 			if (!ISCONNECTED(res, ncomp)) {
153732115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
153832115b10SPawel Jakub Dawidek 				nv_free(nv);
153932115b10SPawel Jakub Dawidek 				goto done_queue;
154032115b10SPawel Jakub Dawidek 			}
154132115b10SPawel Jakub Dawidek 			if (hast_proto_recv_data(res, res->hr_remotein, nv,
154232115b10SPawel Jakub Dawidek 			    ggio->gctl_data, ggio->gctl_length) < 0) {
154332115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
154432115b10SPawel Jakub Dawidek 				pjdlog_errno(LOG_ERR,
154532115b10SPawel Jakub Dawidek 				    "Unable to receive reply data");
154632115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
154732115b10SPawel Jakub Dawidek 				nv_free(nv);
154832115b10SPawel Jakub Dawidek 				remote_close(res, ncomp);
154932115b10SPawel Jakub Dawidek 				goto done_queue;
155032115b10SPawel Jakub Dawidek 			}
155132115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
155232115b10SPawel Jakub Dawidek 			break;
155332115b10SPawel Jakub Dawidek 		case BIO_WRITE:
155432115b10SPawel Jakub Dawidek 		case BIO_DELETE:
155532115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
155632115b10SPawel Jakub Dawidek 			break;
155732115b10SPawel Jakub Dawidek 		default:
15582ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(!"invalid condition");
155932115b10SPawel Jakub Dawidek 			abort();
156032115b10SPawel Jakub Dawidek 		}
156132115b10SPawel Jakub Dawidek 		hio->hio_errors[ncomp] = 0;
156232115b10SPawel Jakub Dawidek 		nv_free(nv);
156332115b10SPawel Jakub Dawidek done_queue:
156432115b10SPawel Jakub Dawidek 		if (refcount_release(&hio->hio_countdown)) {
156532115b10SPawel Jakub Dawidek 			if (ISSYNCREQ(hio)) {
156632115b10SPawel Jakub Dawidek 				mtx_lock(&sync_lock);
156732115b10SPawel Jakub Dawidek 				SYNCREQDONE(hio);
156832115b10SPawel Jakub Dawidek 				mtx_unlock(&sync_lock);
156932115b10SPawel Jakub Dawidek 				cv_signal(&sync_cond);
157032115b10SPawel Jakub Dawidek 			} else {
157132115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
157232115b10SPawel Jakub Dawidek 				    "remote_recv: (%p) Moving request to the done queue.",
157332115b10SPawel Jakub Dawidek 				    hio);
157432115b10SPawel Jakub Dawidek 				QUEUE_INSERT2(hio, done);
157532115b10SPawel Jakub Dawidek 			}
157632115b10SPawel Jakub Dawidek 		}
157732115b10SPawel Jakub Dawidek 	}
157832115b10SPawel Jakub Dawidek 	/* NOTREACHED */
157932115b10SPawel Jakub Dawidek 	return (NULL);
158032115b10SPawel Jakub Dawidek }
158132115b10SPawel Jakub Dawidek 
158232115b10SPawel Jakub Dawidek /*
158332115b10SPawel Jakub Dawidek  * Thread sends answer to the kernel.
158432115b10SPawel Jakub Dawidek  */
158532115b10SPawel Jakub Dawidek static void *
158632115b10SPawel Jakub Dawidek ggate_send_thread(void *arg)
158732115b10SPawel Jakub Dawidek {
158832115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
158932115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
159032115b10SPawel Jakub Dawidek 	struct hio *hio;
159132115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
159232115b10SPawel Jakub Dawidek 
159332115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
159432115b10SPawel Jakub Dawidek 
159532115b10SPawel Jakub Dawidek 	for (;;) {
159632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: Taking request.");
159732115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, done);
159832115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
159932115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
160032115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
160132115b10SPawel Jakub Dawidek 			if (hio->hio_errors[ii] == 0) {
160232115b10SPawel Jakub Dawidek 				/*
160332115b10SPawel Jakub Dawidek 				 * One successful request is enough to declare
160432115b10SPawel Jakub Dawidek 				 * success.
160532115b10SPawel Jakub Dawidek 				 */
160632115b10SPawel Jakub Dawidek 				ggio->gctl_error = 0;
160732115b10SPawel Jakub Dawidek 				break;
160832115b10SPawel Jakub Dawidek 			}
160932115b10SPawel Jakub Dawidek 		}
161032115b10SPawel Jakub Dawidek 		if (ii == ncomps) {
161132115b10SPawel Jakub Dawidek 			/*
161232115b10SPawel Jakub Dawidek 			 * None of the requests were successful.
1613b068d5aaSMikolaj Golub 			 * Use the error from local component except the
1614b068d5aaSMikolaj Golub 			 * case when we did only remote request.
161532115b10SPawel Jakub Dawidek 			 */
1616b068d5aaSMikolaj Golub 			if (ggio->gctl_cmd == BIO_READ &&
1617b068d5aaSMikolaj Golub 			    res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1618b068d5aaSMikolaj Golub 				ggio->gctl_error = hio->hio_errors[1];
1619b068d5aaSMikolaj Golub 			else
162032115b10SPawel Jakub Dawidek 				ggio->gctl_error = hio->hio_errors[0];
162132115b10SPawel Jakub Dawidek 		}
162232115b10SPawel Jakub Dawidek 		if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
162332115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
162432115b10SPawel Jakub Dawidek 			activemap_write_complete(res->hr_amp,
162532115b10SPawel Jakub Dawidek 			    ggio->gctl_offset, ggio->gctl_length);
162632115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
162732115b10SPawel Jakub Dawidek 		}
162832115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
162932115b10SPawel Jakub Dawidek 			/*
163032115b10SPawel Jakub Dawidek 			 * Unlock range we locked.
163132115b10SPawel Jakub Dawidek 			 */
163232115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
163332115b10SPawel Jakub Dawidek 			rangelock_del(range_regular, ggio->gctl_offset,
163432115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
163532115b10SPawel Jakub Dawidek 			if (range_sync_wait)
163632115b10SPawel Jakub Dawidek 				cv_signal(&range_sync_cond);
163732115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
163832115b10SPawel Jakub Dawidek 			/*
163932115b10SPawel Jakub Dawidek 			 * Bump local count if this is first write after
164032115b10SPawel Jakub Dawidek 			 * connection failure with remote node.
164132115b10SPawel Jakub Dawidek 			 */
164232115b10SPawel Jakub Dawidek 			ncomp = 1;
164332115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
164432115b10SPawel Jakub Dawidek 			if (!ISCONNECTED(res, ncomp)) {
164532115b10SPawel Jakub Dawidek 				mtx_lock(&metadata_lock);
164632115b10SPawel Jakub Dawidek 				if (res->hr_primary_localcnt ==
164732115b10SPawel Jakub Dawidek 				    res->hr_secondary_remotecnt) {
164832115b10SPawel Jakub Dawidek 					res->hr_primary_localcnt++;
164932115b10SPawel Jakub Dawidek 					pjdlog_debug(1,
165032115b10SPawel Jakub Dawidek 					    "Increasing localcnt to %ju.",
165132115b10SPawel Jakub Dawidek 					    (uintmax_t)res->hr_primary_localcnt);
165232115b10SPawel Jakub Dawidek 					(void)metadata_write(res);
165332115b10SPawel Jakub Dawidek 				}
165432115b10SPawel Jakub Dawidek 				mtx_unlock(&metadata_lock);
165532115b10SPawel Jakub Dawidek 			}
165632115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
165732115b10SPawel Jakub Dawidek 		}
165832115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0)
165932115b10SPawel Jakub Dawidek 			primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
166032115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
166132115b10SPawel Jakub Dawidek 		    "ggate_send: (%p) Moving request to the free queue.", hio);
166232115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
166332115b10SPawel Jakub Dawidek 	}
166432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
166532115b10SPawel Jakub Dawidek 	return (NULL);
166632115b10SPawel Jakub Dawidek }
166732115b10SPawel Jakub Dawidek 
166832115b10SPawel Jakub Dawidek /*
166932115b10SPawel Jakub Dawidek  * Thread synchronize local and remote components.
167032115b10SPawel Jakub Dawidek  */
167132115b10SPawel Jakub Dawidek static void *
167232115b10SPawel Jakub Dawidek sync_thread(void *arg __unused)
167332115b10SPawel Jakub Dawidek {
167432115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
167532115b10SPawel Jakub Dawidek 	struct hio *hio;
167632115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1677fa356f6cSPawel Jakub Dawidek 	struct timeval tstart, tend, tdiff;
167832115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
167932115b10SPawel Jakub Dawidek 	off_t offset, length, synced;
168032115b10SPawel Jakub Dawidek 	bool dorewind;
168132115b10SPawel Jakub Dawidek 	int syncext;
168232115b10SPawel Jakub Dawidek 
168332115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
168432115b10SPawel Jakub Dawidek 	dorewind = true;
1685b9cf0cf5SPawel Jakub Dawidek 	synced = 0;
1686b9cf0cf5SPawel Jakub Dawidek 	offset = -1;
168732115b10SPawel Jakub Dawidek 
168832115b10SPawel Jakub Dawidek 	for (;;) {
168932115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
1690b9cf0cf5SPawel Jakub Dawidek 		if (offset >= 0 && !sync_inprogress) {
1691fa356f6cSPawel Jakub Dawidek 			gettimeofday(&tend, NULL);
1692fa356f6cSPawel Jakub Dawidek 			timersub(&tend, &tstart, &tdiff);
1693fa356f6cSPawel Jakub Dawidek 			pjdlog_info("Synchronization interrupted after %#.0T. "
1694fa356f6cSPawel Jakub Dawidek 			    "%NB synchronized so far.", &tdiff,
169553d9b386SPawel Jakub Dawidek 			    (intmax_t)synced);
16965bdff860SPawel Jakub Dawidek 			event_send(res, EVENT_SYNCINTR);
169753d9b386SPawel Jakub Dawidek 		}
169832115b10SPawel Jakub Dawidek 		while (!sync_inprogress) {
169932115b10SPawel Jakub Dawidek 			dorewind = true;
170032115b10SPawel Jakub Dawidek 			synced = 0;
170132115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
170232115b10SPawel Jakub Dawidek 		}
170332115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
170432115b10SPawel Jakub Dawidek 		/*
170532115b10SPawel Jakub Dawidek 		 * Obtain offset at which we should synchronize.
170632115b10SPawel Jakub Dawidek 		 * Rewind synchronization if needed.
170732115b10SPawel Jakub Dawidek 		 */
170832115b10SPawel Jakub Dawidek 		mtx_lock(&res->hr_amp_lock);
170932115b10SPawel Jakub Dawidek 		if (dorewind)
171032115b10SPawel Jakub Dawidek 			activemap_sync_rewind(res->hr_amp);
171132115b10SPawel Jakub Dawidek 		offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
171232115b10SPawel Jakub Dawidek 		if (syncext != -1) {
171332115b10SPawel Jakub Dawidek 			/*
171432115b10SPawel Jakub Dawidek 			 * We synchronized entire syncext extent, we can mark
171532115b10SPawel Jakub Dawidek 			 * it as clean now.
171632115b10SPawel Jakub Dawidek 			 */
171732115b10SPawel Jakub Dawidek 			if (activemap_extent_complete(res->hr_amp, syncext))
171832115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
171932115b10SPawel Jakub Dawidek 		}
172032115b10SPawel Jakub Dawidek 		mtx_unlock(&res->hr_amp_lock);
172132115b10SPawel Jakub Dawidek 		if (dorewind) {
172232115b10SPawel Jakub Dawidek 			dorewind = false;
172332115b10SPawel Jakub Dawidek 			if (offset < 0)
172432115b10SPawel Jakub Dawidek 				pjdlog_info("Nodes are in sync.");
172532115b10SPawel Jakub Dawidek 			else {
1726fa356f6cSPawel Jakub Dawidek 				pjdlog_info("Synchronization started. %NB to go.",
1727fa356f6cSPawel Jakub Dawidek 				    (intmax_t)(res->hr_extentsize *
172832115b10SPawel Jakub Dawidek 				    activemap_ndirty(res->hr_amp)));
17295bdff860SPawel Jakub Dawidek 				event_send(res, EVENT_SYNCSTART);
1730fa356f6cSPawel Jakub Dawidek 				gettimeofday(&tstart, NULL);
173132115b10SPawel Jakub Dawidek 			}
173232115b10SPawel Jakub Dawidek 		}
173332115b10SPawel Jakub Dawidek 		if (offset < 0) {
173455ce1e7cSPawel Jakub Dawidek 			sync_stop();
173532115b10SPawel Jakub Dawidek 			pjdlog_debug(1, "Nothing to synchronize.");
173632115b10SPawel Jakub Dawidek 			/*
173732115b10SPawel Jakub Dawidek 			 * Synchronization complete, make both localcnt and
173832115b10SPawel Jakub Dawidek 			 * remotecnt equal.
173932115b10SPawel Jakub Dawidek 			 */
174032115b10SPawel Jakub Dawidek 			ncomp = 1;
174132115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
174232115b10SPawel Jakub Dawidek 			if (ISCONNECTED(res, ncomp)) {
174332115b10SPawel Jakub Dawidek 				if (synced > 0) {
1744fa356f6cSPawel Jakub Dawidek 					int64_t bps;
1745fa356f6cSPawel Jakub Dawidek 
1746fa356f6cSPawel Jakub Dawidek 					gettimeofday(&tend, NULL);
1747fa356f6cSPawel Jakub Dawidek 					timersub(&tend, &tstart, &tdiff);
1748fa356f6cSPawel Jakub Dawidek 					bps = (int64_t)((double)synced /
1749fa356f6cSPawel Jakub Dawidek 					    ((double)tdiff.tv_sec +
1750fa356f6cSPawel Jakub Dawidek 					    (double)tdiff.tv_usec / 1000000));
175132115b10SPawel Jakub Dawidek 					pjdlog_info("Synchronization complete. "
1752fa356f6cSPawel Jakub Dawidek 					    "%NB synchronized in %#.0lT (%NB/sec).",
1753fa356f6cSPawel Jakub Dawidek 					    (intmax_t)synced, &tdiff,
1754fa356f6cSPawel Jakub Dawidek 					    (intmax_t)bps);
17555bdff860SPawel Jakub Dawidek 					event_send(res, EVENT_SYNCDONE);
175632115b10SPawel Jakub Dawidek 				}
175732115b10SPawel Jakub Dawidek 				mtx_lock(&metadata_lock);
175832115b10SPawel Jakub Dawidek 				res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
175932115b10SPawel Jakub Dawidek 				res->hr_primary_localcnt =
176032115b10SPawel Jakub Dawidek 				    res->hr_secondary_remotecnt;
17619237aa3fSMikolaj Golub 				res->hr_primary_remotecnt =
17629237aa3fSMikolaj Golub 				    res->hr_secondary_localcnt;
176332115b10SPawel Jakub Dawidek 				pjdlog_debug(1,
176432115b10SPawel Jakub Dawidek 				    "Setting localcnt to %ju and remotecnt to %ju.",
176532115b10SPawel Jakub Dawidek 				    (uintmax_t)res->hr_primary_localcnt,
17669237aa3fSMikolaj Golub 				    (uintmax_t)res->hr_primary_remotecnt);
176732115b10SPawel Jakub Dawidek 				(void)metadata_write(res);
176832115b10SPawel Jakub Dawidek 				mtx_unlock(&metadata_lock);
176932115b10SPawel Jakub Dawidek 			}
177032115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
177132115b10SPawel Jakub Dawidek 			continue;
177232115b10SPawel Jakub Dawidek 		}
177332115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: Taking free request.");
177432115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
177532115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Got free request.", hio);
177632115b10SPawel Jakub Dawidek 		/*
177732115b10SPawel Jakub Dawidek 		 * Lock the range we are going to synchronize. We don't want
177832115b10SPawel Jakub Dawidek 		 * race where someone writes between our read and write.
177932115b10SPawel Jakub Dawidek 		 */
178032115b10SPawel Jakub Dawidek 		for (;;) {
178132115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
178232115b10SPawel Jakub Dawidek 			if (rangelock_islocked(range_regular, offset, length)) {
178332115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
178432115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd locked.",
178532115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
178632115b10SPawel Jakub Dawidek 				range_sync_wait = true;
178732115b10SPawel Jakub Dawidek 				cv_wait(&range_sync_cond, &range_lock);
178832115b10SPawel Jakub Dawidek 				range_sync_wait = false;
178932115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
179032115b10SPawel Jakub Dawidek 				continue;
179132115b10SPawel Jakub Dawidek 			}
179232115b10SPawel Jakub Dawidek 			if (rangelock_add(range_sync, offset, length) < 0) {
179332115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
179432115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
179532115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd is already locked, waiting.",
179632115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
179732115b10SPawel Jakub Dawidek 				sleep(1);
179832115b10SPawel Jakub Dawidek 				continue;
179932115b10SPawel Jakub Dawidek 			}
180032115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
180132115b10SPawel Jakub Dawidek 			break;
180232115b10SPawel Jakub Dawidek 		}
180332115b10SPawel Jakub Dawidek 		/*
180432115b10SPawel Jakub Dawidek 		 * First read the data from synchronization source.
180532115b10SPawel Jakub Dawidek 		 */
180632115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
180732115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
180832115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_READ;
180932115b10SPawel Jakub Dawidek 		ggio->gctl_offset = offset;
181032115b10SPawel Jakub Dawidek 		ggio->gctl_length = length;
181132115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
181232115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
181332115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
181432115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
181532115b10SPawel Jakub Dawidek 		    hio);
181632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
181732115b10SPawel Jakub Dawidek 		    hio);
181832115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
181932115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
182032115b10SPawel Jakub Dawidek 			/*
182132115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
182232115b10SPawel Jakub Dawidek 			 * so handle request locally.
182332115b10SPawel Jakub Dawidek 			 */
182432115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
182532115b10SPawel Jakub Dawidek 			ncomp = 0;
182632115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
18272ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
182832115b10SPawel Jakub Dawidek 			/*
182932115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
183032115b10SPawel Jakub Dawidek 			 * so send request to the remote node.
183132115b10SPawel Jakub Dawidek 			 */
183232115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
183332115b10SPawel Jakub Dawidek 			ncomp = 1;
183432115b10SPawel Jakub Dawidek 		}
183532115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
183632115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, 1);
183732115b10SPawel Jakub Dawidek 		QUEUE_INSERT1(hio, send, ncomp);
183832115b10SPawel Jakub Dawidek 
183932115b10SPawel Jakub Dawidek 		/*
184032115b10SPawel Jakub Dawidek 		 * Let's wait for READ to finish.
184132115b10SPawel Jakub Dawidek 		 */
184232115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
184332115b10SPawel Jakub Dawidek 		while (!ISSYNCREQDONE(hio))
184432115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
184532115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
184632115b10SPawel Jakub Dawidek 
184732115b10SPawel Jakub Dawidek 		if (hio->hio_errors[ncomp] != 0) {
184832115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to read synchronization data: %s.",
184932115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
185032115b10SPawel Jakub Dawidek 			goto free_queue;
185132115b10SPawel Jakub Dawidek 		}
185232115b10SPawel Jakub Dawidek 
185332115b10SPawel Jakub Dawidek 		/*
185432115b10SPawel Jakub Dawidek 		 * We read the data from synchronization source, now write it
185532115b10SPawel Jakub Dawidek 		 * to synchronization target.
185632115b10SPawel Jakub Dawidek 		 */
185732115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
185832115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_WRITE;
185932115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
186032115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
186132115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
186232115b10SPawel Jakub Dawidek 		    hio);
186332115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
186432115b10SPawel Jakub Dawidek 		    hio);
186532115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
186632115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
186732115b10SPawel Jakub Dawidek 			/*
186832115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
186932115b10SPawel Jakub Dawidek 			 * so we update remote component.
187032115b10SPawel Jakub Dawidek 			 */
187132115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
187232115b10SPawel Jakub Dawidek 			ncomp = 1;
187332115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
18742ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
187532115b10SPawel Jakub Dawidek 			/*
187632115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
187732115b10SPawel Jakub Dawidek 			 * so we update it.
187832115b10SPawel Jakub Dawidek 			 */
187932115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
188032115b10SPawel Jakub Dawidek 			ncomp = 0;
188132115b10SPawel Jakub Dawidek 		}
188232115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
188332115b10SPawel Jakub Dawidek 
188432115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queues.",
188532115b10SPawel Jakub Dawidek 		    hio);
188632115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, 1);
188732115b10SPawel Jakub Dawidek 		QUEUE_INSERT1(hio, send, ncomp);
188832115b10SPawel Jakub Dawidek 
188932115b10SPawel Jakub Dawidek 		/*
189032115b10SPawel Jakub Dawidek 		 * Let's wait for WRITE to finish.
189132115b10SPawel Jakub Dawidek 		 */
189232115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
189332115b10SPawel Jakub Dawidek 		while (!ISSYNCREQDONE(hio))
189432115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
189532115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
189632115b10SPawel Jakub Dawidek 
189732115b10SPawel Jakub Dawidek 		if (hio->hio_errors[ncomp] != 0) {
189832115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to write synchronization data: %s.",
189932115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
190032115b10SPawel Jakub Dawidek 			goto free_queue;
190132115b10SPawel Jakub Dawidek 		}
1902e23d2d01SPawel Jakub Dawidek 
1903e23d2d01SPawel Jakub Dawidek 		synced += length;
190432115b10SPawel Jakub Dawidek free_queue:
190532115b10SPawel Jakub Dawidek 		mtx_lock(&range_lock);
190632115b10SPawel Jakub Dawidek 		rangelock_del(range_sync, offset, length);
190732115b10SPawel Jakub Dawidek 		if (range_regular_wait)
190832115b10SPawel Jakub Dawidek 			cv_signal(&range_regular_cond);
190932115b10SPawel Jakub Dawidek 		mtx_unlock(&range_lock);
191032115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
191132115b10SPawel Jakub Dawidek 		    hio);
191232115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
191332115b10SPawel Jakub Dawidek 	}
191432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
191532115b10SPawel Jakub Dawidek 	return (NULL);
191632115b10SPawel Jakub Dawidek }
191732115b10SPawel Jakub Dawidek 
1918115f4e5cSPawel Jakub Dawidek void
1919115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv)
19200989854dSPawel Jakub Dawidek {
19210989854dSPawel Jakub Dawidek 	unsigned int ii, ncomps;
1922115f4e5cSPawel Jakub Dawidek 	int modified, vint;
1923115f4e5cSPawel Jakub Dawidek 	const char *vstr;
19240989854dSPawel Jakub Dawidek 
19250989854dSPawel Jakub Dawidek 	pjdlog_info("Reloading configuration...");
19260989854dSPawel Jakub Dawidek 
19272ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
19282ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(gres == res);
1929115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "remoteaddr");
19300b626a28SPawel Jakub Dawidek 	nv_assert(nv, "sourceaddr");
1931115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "replication");
19321fee97b0SPawel Jakub Dawidek 	nv_assert(nv, "checksum");
19338cd3d45aSPawel Jakub Dawidek 	nv_assert(nv, "compression");
1934115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "timeout");
1935115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "exec");
1936115f4e5cSPawel Jakub Dawidek 
19370989854dSPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
19380989854dSPawel Jakub Dawidek 
19391fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR	0x01
19400b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR	0x02
19410b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION	0x04
19420b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM	0x08
19430b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION	0x10
19440b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT	0x20
19450b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC		0x40
19460989854dSPawel Jakub Dawidek 	modified = 0;
1947115f4e5cSPawel Jakub Dawidek 
1948115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "remoteaddr");
1949115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
19500989854dSPawel Jakub Dawidek 		/*
19510989854dSPawel Jakub Dawidek 		 * Don't copy res->hr_remoteaddr to gres just yet.
19520989854dSPawel Jakub Dawidek 		 * We want remote_close() to log disconnect from the old
19530989854dSPawel Jakub Dawidek 		 * addresses, not from the new ones.
19540989854dSPawel Jakub Dawidek 		 */
19550989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REMOTEADDR;
19560989854dSPawel Jakub Dawidek 	}
19570b626a28SPawel Jakub Dawidek 	vstr = nv_get_string(nv, "sourceaddr");
19580b626a28SPawel Jakub Dawidek 	if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
19590b626a28SPawel Jakub Dawidek 		strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
19600b626a28SPawel Jakub Dawidek 		modified |= MODIFIED_SOURCEADDR;
19610b626a28SPawel Jakub Dawidek 	}
1962115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "replication");
1963115f4e5cSPawel Jakub Dawidek 	if (gres->hr_replication != vint) {
1964115f4e5cSPawel Jakub Dawidek 		gres->hr_replication = vint;
19650989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REPLICATION;
19660989854dSPawel Jakub Dawidek 	}
19671fee97b0SPawel Jakub Dawidek 	vint = nv_get_int32(nv, "checksum");
19681fee97b0SPawel Jakub Dawidek 	if (gres->hr_checksum != vint) {
19691fee97b0SPawel Jakub Dawidek 		gres->hr_checksum = vint;
19701fee97b0SPawel Jakub Dawidek 		modified |= MODIFIED_CHECKSUM;
19711fee97b0SPawel Jakub Dawidek 	}
19728cd3d45aSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "compression");
19738cd3d45aSPawel Jakub Dawidek 	if (gres->hr_compression != vint) {
19748cd3d45aSPawel Jakub Dawidek 		gres->hr_compression = vint;
19758cd3d45aSPawel Jakub Dawidek 		modified |= MODIFIED_COMPRESSION;
19768cd3d45aSPawel Jakub Dawidek 	}
1977115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "timeout");
1978115f4e5cSPawel Jakub Dawidek 	if (gres->hr_timeout != vint) {
1979115f4e5cSPawel Jakub Dawidek 		gres->hr_timeout = vint;
19800989854dSPawel Jakub Dawidek 		modified |= MODIFIED_TIMEOUT;
19810989854dSPawel Jakub Dawidek 	}
1982115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "exec");
1983115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_exec, vstr) != 0) {
1984115f4e5cSPawel Jakub Dawidek 		strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
19850becad39SPawel Jakub Dawidek 		modified |= MODIFIED_EXEC;
19860becad39SPawel Jakub Dawidek 	}
1987115f4e5cSPawel Jakub Dawidek 
19880989854dSPawel Jakub Dawidek 	/*
19891fee97b0SPawel Jakub Dawidek 	 * Change timeout for connected sockets.
19901fee97b0SPawel Jakub Dawidek 	 * Don't bother if we need to reconnect.
19910989854dSPawel Jakub Dawidek 	 */
19921fee97b0SPawel Jakub Dawidek 	if ((modified & MODIFIED_TIMEOUT) != 0 &&
19930b626a28SPawel Jakub Dawidek 	    (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR |
19940b626a28SPawel Jakub Dawidek 	    MODIFIED_REPLICATION)) == 0) {
19950989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
19960989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
19970989854dSPawel Jakub Dawidek 				continue;
19980989854dSPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ii]);
19990989854dSPawel Jakub Dawidek 			if (!ISCONNECTED(gres, ii)) {
20000989854dSPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ii]);
20010989854dSPawel Jakub Dawidek 				continue;
20020989854dSPawel Jakub Dawidek 			}
20030989854dSPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ii]);
20040989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remotein,
20050989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
20060989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
20070989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
20080989854dSPawel Jakub Dawidek 			}
20090989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remoteout,
20100989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
20110989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
20120989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
20130989854dSPawel Jakub Dawidek 			}
20140989854dSPawel Jakub Dawidek 		}
20151fee97b0SPawel Jakub Dawidek 	}
20160b626a28SPawel Jakub Dawidek 	if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR |
20170b626a28SPawel Jakub Dawidek 	    MODIFIED_REPLICATION)) != 0) {
20180989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
20190989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
20200989854dSPawel Jakub Dawidek 				continue;
20210989854dSPawel Jakub Dawidek 			remote_close(gres, ii);
20220989854dSPawel Jakub Dawidek 		}
20230989854dSPawel Jakub Dawidek 		if (modified & MODIFIED_REMOTEADDR) {
2024115f4e5cSPawel Jakub Dawidek 			vstr = nv_get_string(nv, "remoteaddr");
2025115f4e5cSPawel Jakub Dawidek 			strlcpy(gres->hr_remoteaddr, vstr,
20260989854dSPawel Jakub Dawidek 			    sizeof(gres->hr_remoteaddr));
20270989854dSPawel Jakub Dawidek 		}
20280989854dSPawel Jakub Dawidek 	}
20290989854dSPawel Jakub Dawidek #undef	MODIFIED_REMOTEADDR
20300b626a28SPawel Jakub Dawidek #undef	MODIFIED_SOURCEADDR
20310989854dSPawel Jakub Dawidek #undef	MODIFIED_REPLICATION
20321fee97b0SPawel Jakub Dawidek #undef	MODIFIED_CHECKSUM
20338cd3d45aSPawel Jakub Dawidek #undef	MODIFIED_COMPRESSION
20340989854dSPawel Jakub Dawidek #undef	MODIFIED_TIMEOUT
20350becad39SPawel Jakub Dawidek #undef	MODIFIED_EXEC
20360989854dSPawel Jakub Dawidek 
20370989854dSPawel Jakub Dawidek 	pjdlog_info("Configuration reloaded successfully.");
20380989854dSPawel Jakub Dawidek }
20390989854dSPawel Jakub Dawidek 
2040f7fe83f9SPawel Jakub Dawidek static void
2041ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp)
2042ff6bb1f8SPawel Jakub Dawidek {
2043ff6bb1f8SPawel Jakub Dawidek 	struct proto_conn *in, *out;
2044ff6bb1f8SPawel Jakub Dawidek 
2045ff6bb1f8SPawel Jakub Dawidek 	if (!ISREMOTE(ncomp))
2046ff6bb1f8SPawel Jakub Dawidek 		return;
2047ff6bb1f8SPawel Jakub Dawidek 
2048ff6bb1f8SPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
2049ff6bb1f8SPawel Jakub Dawidek 
2050ff6bb1f8SPawel Jakub Dawidek 	if (!real_remote(res)) {
2051ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2052ff6bb1f8SPawel Jakub Dawidek 		return;
2053ff6bb1f8SPawel Jakub Dawidek 	}
2054ff6bb1f8SPawel Jakub Dawidek 
2055ff6bb1f8SPawel Jakub Dawidek 	if (ISCONNECTED(res, ncomp)) {
20562ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein != NULL);
20572ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout != NULL);
2058ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2059ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2060ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2061ff6bb1f8SPawel Jakub Dawidek 		return;
2062ff6bb1f8SPawel Jakub Dawidek 	}
2063ff6bb1f8SPawel Jakub Dawidek 
20642ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein == NULL);
20652ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout == NULL);
2066ff6bb1f8SPawel Jakub Dawidek 	/*
2067ff6bb1f8SPawel Jakub Dawidek 	 * Upgrade the lock. It doesn't have to be atomic as no other thread
2068ff6bb1f8SPawel Jakub Dawidek 	 * can change connection status from disconnected to connected.
2069ff6bb1f8SPawel Jakub Dawidek 	 */
2070ff6bb1f8SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
2071ff6bb1f8SPawel Jakub Dawidek 	pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2072ff6bb1f8SPawel Jakub Dawidek 	    res->hr_remoteaddr);
2073ff6bb1f8SPawel Jakub Dawidek 	in = out = NULL;
2074ff6bb1f8SPawel Jakub Dawidek 	if (init_remote(res, &in, &out)) {
2075ff6bb1f8SPawel Jakub Dawidek 		rw_wlock(&hio_remote_lock[ncomp]);
20762ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
20772ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
20782ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in != NULL && out != NULL);
2079ff6bb1f8SPawel Jakub Dawidek 		res->hr_remotein = in;
2080ff6bb1f8SPawel Jakub Dawidek 		res->hr_remoteout = out;
2081ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2082ff6bb1f8SPawel Jakub Dawidek 		pjdlog_info("Successfully reconnected to %s.",
2083ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2084ff6bb1f8SPawel Jakub Dawidek 		sync_start();
2085ff6bb1f8SPawel Jakub Dawidek 	} else {
2086ff6bb1f8SPawel Jakub Dawidek 		/* Both connections should be NULL. */
20872ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
20882ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
20892ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in == NULL && out == NULL);
2090ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2091ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2092ff6bb1f8SPawel Jakub Dawidek 	}
2093ff6bb1f8SPawel Jakub Dawidek }
2094ff6bb1f8SPawel Jakub Dawidek 
209532115b10SPawel Jakub Dawidek /*
209632115b10SPawel Jakub Dawidek  * Thread guards remote connections and reconnects when needed, handles
209732115b10SPawel Jakub Dawidek  * signals, etc.
209832115b10SPawel Jakub Dawidek  */
209932115b10SPawel Jakub Dawidek static void *
210032115b10SPawel Jakub Dawidek guard_thread(void *arg)
210132115b10SPawel Jakub Dawidek {
210232115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
210332115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
21046d0c801eSPawel Jakub Dawidek 	struct timespec timeout;
2105ff6bb1f8SPawel Jakub Dawidek 	time_t lastcheck, now;
21066d0c801eSPawel Jakub Dawidek 	sigset_t mask;
21076d0c801eSPawel Jakub Dawidek 	int signo;
210832115b10SPawel Jakub Dawidek 
210932115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
2110ff6bb1f8SPawel Jakub Dawidek 	lastcheck = time(NULL);
211132115b10SPawel Jakub Dawidek 
21126d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
21136d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
21146d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
21156d0c801eSPawel Jakub Dawidek 
21168d7dcf14SMikolaj Golub 	timeout.tv_sec = HAST_KEEPALIVE;
21176d0c801eSPawel Jakub Dawidek 	timeout.tv_nsec = 0;
21186d0c801eSPawel Jakub Dawidek 	signo = -1;
21196d0c801eSPawel Jakub Dawidek 
212032115b10SPawel Jakub Dawidek 	for (;;) {
21216d0c801eSPawel Jakub Dawidek 		switch (signo) {
21226d0c801eSPawel Jakub Dawidek 		case SIGINT:
21236d0c801eSPawel Jakub Dawidek 		case SIGTERM:
21246d0c801eSPawel Jakub Dawidek 			sigexit_received = true;
212532115b10SPawel Jakub Dawidek 			primary_exitx(EX_OK,
212632115b10SPawel Jakub Dawidek 			    "Termination signal received, exiting.");
21276d0c801eSPawel Jakub Dawidek 			break;
21286d0c801eSPawel Jakub Dawidek 		default:
2129ff6bb1f8SPawel Jakub Dawidek 			break;
2130f7fe83f9SPawel Jakub Dawidek 		}
21316d0c801eSPawel Jakub Dawidek 
21326d0c801eSPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Checking connections.");
2133ff6bb1f8SPawel Jakub Dawidek 		now = time(NULL);
21348d7dcf14SMikolaj Golub 		if (lastcheck + HAST_KEEPALIVE <= now) {
21356d0c801eSPawel Jakub Dawidek 			for (ii = 0; ii < ncomps; ii++)
2136ff6bb1f8SPawel Jakub Dawidek 				guard_one(res, ii);
2137ff6bb1f8SPawel Jakub Dawidek 			lastcheck = now;
213832115b10SPawel Jakub Dawidek 		}
21396d0c801eSPawel Jakub Dawidek 		signo = sigtimedwait(&mask, NULL, &timeout);
214032115b10SPawel Jakub Dawidek 	}
214132115b10SPawel Jakub Dawidek 	/* NOTREACHED */
214232115b10SPawel Jakub Dawidek 	return (NULL);
214332115b10SPawel Jakub Dawidek }
2144