xref: /freebsd/sbin/hastd/primary.c (revision ba2a822490c1ac6e94ec4296eeac09a1a3be2d59)
132115b10SPawel Jakub Dawidek /*-
232115b10SPawel Jakub Dawidek  * Copyright (c) 2009 The FreeBSD Foundation
31fee97b0SPawel Jakub Dawidek  * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
432115b10SPawel Jakub Dawidek  * All rights reserved.
532115b10SPawel Jakub Dawidek  *
632115b10SPawel Jakub Dawidek  * This software was developed by Pawel Jakub Dawidek under sponsorship from
732115b10SPawel Jakub Dawidek  * the FreeBSD Foundation.
832115b10SPawel Jakub Dawidek  *
932115b10SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
1032115b10SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
1132115b10SPawel Jakub Dawidek  * are met:
1232115b10SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
1332115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
1432115b10SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
1532115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
1632115b10SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
1732115b10SPawel Jakub Dawidek  *
1832115b10SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1932115b10SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2032115b10SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2132115b10SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2232115b10SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2332115b10SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2432115b10SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2532115b10SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2632115b10SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2732115b10SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2832115b10SPawel Jakub Dawidek  * SUCH DAMAGE.
2932115b10SPawel Jakub Dawidek  */
3032115b10SPawel Jakub Dawidek 
3132115b10SPawel Jakub Dawidek #include <sys/cdefs.h>
3232115b10SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
3332115b10SPawel Jakub Dawidek 
3432115b10SPawel Jakub Dawidek #include <sys/types.h>
3532115b10SPawel Jakub Dawidek #include <sys/time.h>
3632115b10SPawel Jakub Dawidek #include <sys/bio.h>
3732115b10SPawel Jakub Dawidek #include <sys/disk.h>
3832115b10SPawel Jakub Dawidek #include <sys/refcount.h>
3932115b10SPawel Jakub Dawidek #include <sys/stat.h>
4032115b10SPawel Jakub Dawidek 
4132115b10SPawel Jakub Dawidek #include <geom/gate/g_gate.h>
4232115b10SPawel Jakub Dawidek 
4332115b10SPawel Jakub Dawidek #include <err.h>
4432115b10SPawel Jakub Dawidek #include <errno.h>
4532115b10SPawel Jakub Dawidek #include <fcntl.h>
4632115b10SPawel Jakub Dawidek #include <libgeom.h>
4732115b10SPawel Jakub Dawidek #include <pthread.h>
486d0c801eSPawel Jakub Dawidek #include <signal.h>
4932115b10SPawel Jakub Dawidek #include <stdint.h>
5032115b10SPawel Jakub Dawidek #include <stdio.h>
5132115b10SPawel Jakub Dawidek #include <string.h>
5232115b10SPawel Jakub Dawidek #include <sysexits.h>
5332115b10SPawel Jakub Dawidek #include <unistd.h>
5432115b10SPawel Jakub Dawidek 
5532115b10SPawel Jakub Dawidek #include <activemap.h>
5632115b10SPawel Jakub Dawidek #include <nv.h>
5732115b10SPawel Jakub Dawidek #include <rangelock.h>
5832115b10SPawel Jakub Dawidek 
5932115b10SPawel Jakub Dawidek #include "control.h"
605bdff860SPawel Jakub Dawidek #include "event.h"
6132115b10SPawel Jakub Dawidek #include "hast.h"
6232115b10SPawel Jakub Dawidek #include "hast_proto.h"
6332115b10SPawel Jakub Dawidek #include "hastd.h"
640becad39SPawel Jakub Dawidek #include "hooks.h"
6532115b10SPawel Jakub Dawidek #include "metadata.h"
6632115b10SPawel Jakub Dawidek #include "proto.h"
6732115b10SPawel Jakub Dawidek #include "pjdlog.h"
6832115b10SPawel Jakub Dawidek #include "subr.h"
6932115b10SPawel Jakub Dawidek #include "synch.h"
7032115b10SPawel Jakub Dawidek 
710989854dSPawel Jakub Dawidek /* The is only one remote component for now. */
720989854dSPawel Jakub Dawidek #define	ISREMOTE(no)	((no) == 1)
730989854dSPawel Jakub Dawidek 
7432115b10SPawel Jakub Dawidek struct hio {
7532115b10SPawel Jakub Dawidek 	/*
7632115b10SPawel Jakub Dawidek 	 * Number of components we are still waiting for.
7732115b10SPawel Jakub Dawidek 	 * When this field goes to 0, we can send the request back to the
7832115b10SPawel Jakub Dawidek 	 * kernel. Each component has to decrease this counter by one
7932115b10SPawel Jakub Dawidek 	 * even on failure.
8032115b10SPawel Jakub Dawidek 	 */
8132115b10SPawel Jakub Dawidek 	unsigned int		 hio_countdown;
8232115b10SPawel Jakub Dawidek 	/*
8332115b10SPawel Jakub Dawidek 	 * Each component has a place to store its own error.
8432115b10SPawel Jakub Dawidek 	 * Once the request is handled by all components we can decide if the
8532115b10SPawel Jakub Dawidek 	 * request overall is successful or not.
8632115b10SPawel Jakub Dawidek 	 */
8732115b10SPawel Jakub Dawidek 	int			*hio_errors;
8832115b10SPawel Jakub Dawidek 	/*
890b626a28SPawel Jakub Dawidek 	 * Structure used to communicate with GEOM Gate class.
9032115b10SPawel Jakub Dawidek 	 */
9132115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io	 hio_ggio;
9232115b10SPawel Jakub Dawidek 	TAILQ_ENTRY(hio)	*hio_next;
9332115b10SPawel Jakub Dawidek };
9432115b10SPawel Jakub Dawidek #define	hio_free_next	hio_next[0]
9532115b10SPawel Jakub Dawidek #define	hio_done_next	hio_next[0]
9632115b10SPawel Jakub Dawidek 
9732115b10SPawel Jakub Dawidek /*
9832115b10SPawel Jakub Dawidek  * Free list holds unused structures. When free list is empty, we have to wait
9932115b10SPawel Jakub Dawidek  * until some in-progress requests are freed.
10032115b10SPawel Jakub Dawidek  */
10132115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_free_list;
10232115b10SPawel Jakub Dawidek static pthread_mutex_t hio_free_list_lock;
10332115b10SPawel Jakub Dawidek static pthread_cond_t hio_free_list_cond;
10432115b10SPawel Jakub Dawidek /*
10532115b10SPawel Jakub Dawidek  * There is one send list for every component. One requests is placed on all
10632115b10SPawel Jakub Dawidek  * send lists - each component gets the same request, but each component is
10732115b10SPawel Jakub Dawidek  * responsible for managing his own send list.
10832115b10SPawel Jakub Dawidek  */
10932115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_send_list;
11032115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_send_list_lock;
11132115b10SPawel Jakub Dawidek static pthread_cond_t *hio_send_list_cond;
11232115b10SPawel Jakub Dawidek /*
11332115b10SPawel Jakub Dawidek  * There is one recv list for every component, although local components don't
11432115b10SPawel Jakub Dawidek  * use recv lists as local requests are done synchronously.
11532115b10SPawel Jakub Dawidek  */
11632115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_recv_list;
11732115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_recv_list_lock;
11832115b10SPawel Jakub Dawidek static pthread_cond_t *hio_recv_list_cond;
11932115b10SPawel Jakub Dawidek /*
12032115b10SPawel Jakub Dawidek  * Request is placed on done list by the slowest component (the one that
12132115b10SPawel Jakub Dawidek  * decreased hio_countdown from 1 to 0).
12232115b10SPawel Jakub Dawidek  */
12332115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_done_list;
12432115b10SPawel Jakub Dawidek static pthread_mutex_t hio_done_list_lock;
12532115b10SPawel Jakub Dawidek static pthread_cond_t hio_done_list_cond;
12632115b10SPawel Jakub Dawidek /*
12732115b10SPawel Jakub Dawidek  * Structure below are for interaction with sync thread.
12832115b10SPawel Jakub Dawidek  */
12932115b10SPawel Jakub Dawidek static bool sync_inprogress;
13032115b10SPawel Jakub Dawidek static pthread_mutex_t sync_lock;
13132115b10SPawel Jakub Dawidek static pthread_cond_t sync_cond;
13232115b10SPawel Jakub Dawidek /*
13332115b10SPawel Jakub Dawidek  * The lock below allows to synchornize access to remote connections.
13432115b10SPawel Jakub Dawidek  */
13532115b10SPawel Jakub Dawidek static pthread_rwlock_t *hio_remote_lock;
13632115b10SPawel Jakub Dawidek 
13732115b10SPawel Jakub Dawidek /*
13832115b10SPawel Jakub Dawidek  * Lock to synchronize metadata updates. Also synchronize access to
13932115b10SPawel Jakub Dawidek  * hr_primary_localcnt and hr_primary_remotecnt fields.
14032115b10SPawel Jakub Dawidek  */
14132115b10SPawel Jakub Dawidek static pthread_mutex_t metadata_lock;
14232115b10SPawel Jakub Dawidek 
14332115b10SPawel Jakub Dawidek /*
14432115b10SPawel Jakub Dawidek  * Maximum number of outstanding I/O requests.
14532115b10SPawel Jakub Dawidek  */
14632115b10SPawel Jakub Dawidek #define	HAST_HIO_MAX	256
14732115b10SPawel Jakub Dawidek /*
14832115b10SPawel Jakub Dawidek  * Number of components. At this point there are only two components: local
14932115b10SPawel Jakub Dawidek  * and remote, but in the future it might be possible to use multiple local
15032115b10SPawel Jakub Dawidek  * and remote components.
15132115b10SPawel Jakub Dawidek  */
15232115b10SPawel Jakub Dawidek #define	HAST_NCOMPONENTS	2
15332115b10SPawel Jakub Dawidek 
15432115b10SPawel Jakub Dawidek #define	ISCONNECTED(res, no)	\
15532115b10SPawel Jakub Dawidek 	((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
15632115b10SPawel Jakub Dawidek 
15732115b10SPawel Jakub Dawidek #define	QUEUE_INSERT1(hio, name, ncomp)	do {				\
15832115b10SPawel Jakub Dawidek 	bool _wakeup;							\
15932115b10SPawel Jakub Dawidek 									\
16032115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
16132115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]);		\
16232115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio),		\
16332115b10SPawel Jakub Dawidek 	    hio_next[(ncomp)]);						\
16432115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[ncomp]);			\
16532115b10SPawel Jakub Dawidek 	if (_wakeup)							\
16632115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond[(ncomp)]);		\
16732115b10SPawel Jakub Dawidek } while (0)
16832115b10SPawel Jakub Dawidek #define	QUEUE_INSERT2(hio, name)	do {				\
16932115b10SPawel Jakub Dawidek 	bool _wakeup;							\
17032115b10SPawel Jakub Dawidek 									\
17132115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
17232115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
17332115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
17432115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
17532115b10SPawel Jakub Dawidek 	if (_wakeup)							\
17632115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond);			\
17732115b10SPawel Jakub Dawidek } while (0)
178448efa94SPawel Jakub Dawidek #define	QUEUE_TAKE1(hio, name, ncomp, timeout)	do {			\
179448efa94SPawel Jakub Dawidek 	bool _last;							\
180448efa94SPawel Jakub Dawidek 									\
18132115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
182448efa94SPawel Jakub Dawidek 	_last = false;							\
183448efa94SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
184448efa94SPawel Jakub Dawidek 		cv_timedwait(&hio_##name##_list_cond[(ncomp)],		\
185448efa94SPawel Jakub Dawidek 		    &hio_##name##_list_lock[(ncomp)], (timeout));	\
186448efa94SPawel Jakub Dawidek 		if ((timeout) != 0)					\
187448efa94SPawel Jakub Dawidek 			_last = true;					\
18832115b10SPawel Jakub Dawidek 	}								\
189448efa94SPawel Jakub Dawidek 	if (hio != NULL) {						\
19032115b10SPawel Jakub Dawidek 		TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio),	\
19132115b10SPawel Jakub Dawidek 		    hio_next[(ncomp)]);					\
192448efa94SPawel Jakub Dawidek 	}								\
19332115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[(ncomp)]);			\
19432115b10SPawel Jakub Dawidek } while (0)
19532115b10SPawel Jakub Dawidek #define	QUEUE_TAKE2(hio, name)	do {					\
19632115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
19732115b10SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
19832115b10SPawel Jakub Dawidek 		cv_wait(&hio_##name##_list_cond,			\
19932115b10SPawel Jakub Dawidek 		    &hio_##name##_list_lock);				\
20032115b10SPawel Jakub Dawidek 	}								\
20132115b10SPawel Jakub Dawidek 	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next);	\
20232115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
20332115b10SPawel Jakub Dawidek } while (0)
20432115b10SPawel Jakub Dawidek 
205328e0f4bSPawel Jakub Dawidek #define	SYNCREQ(hio)		do {					\
206328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_unit = -1;					\
207328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_seq = 1;					\
208328e0f4bSPawel Jakub Dawidek } while (0)
20932115b10SPawel Jakub Dawidek #define	ISSYNCREQ(hio)		((hio)->hio_ggio.gctl_unit == -1)
21032115b10SPawel Jakub Dawidek #define	SYNCREQDONE(hio)	do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
21132115b10SPawel Jakub Dawidek #define	ISSYNCREQDONE(hio)	((hio)->hio_ggio.gctl_unit == -2)
21232115b10SPawel Jakub Dawidek 
21332115b10SPawel Jakub Dawidek static struct hast_resource *gres;
21432115b10SPawel Jakub Dawidek 
21532115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock;
21632115b10SPawel Jakub Dawidek static struct rangelocks *range_regular;
21732115b10SPawel Jakub Dawidek static bool range_regular_wait;
21832115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond;
21932115b10SPawel Jakub Dawidek static struct rangelocks *range_sync;
22032115b10SPawel Jakub Dawidek static bool range_sync_wait;
22132115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond;
222ac0401e3SPawel Jakub Dawidek static bool fullystarted;
22332115b10SPawel Jakub Dawidek 
22432115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg);
22532115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg);
22632115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg);
22732115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg);
22832115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg);
22932115b10SPawel Jakub Dawidek static void *sync_thread(void *arg);
23032115b10SPawel Jakub Dawidek static void *guard_thread(void *arg);
23132115b10SPawel Jakub Dawidek 
2326d0c801eSPawel Jakub Dawidek static void
23332115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res)
23432115b10SPawel Jakub Dawidek {
23532115b10SPawel Jakub Dawidek 	int rerrno;
23632115b10SPawel Jakub Dawidek 
23732115b10SPawel Jakub Dawidek 	/* Remember errno. */
23832115b10SPawel Jakub Dawidek 	rerrno = errno;
23932115b10SPawel Jakub Dawidek 
24032115b10SPawel Jakub Dawidek 	/* Destroy ggate provider if we created one. */
24132115b10SPawel Jakub Dawidek 	if (res->hr_ggateunit >= 0) {
24232115b10SPawel Jakub Dawidek 		struct g_gate_ctl_destroy ggiod;
24332115b10SPawel Jakub Dawidek 
2444e47b646SPawel Jakub Dawidek 		bzero(&ggiod, sizeof(ggiod));
24532115b10SPawel Jakub Dawidek 		ggiod.gctl_version = G_GATE_VERSION;
24632115b10SPawel Jakub Dawidek 		ggiod.gctl_unit = res->hr_ggateunit;
24732115b10SPawel Jakub Dawidek 		ggiod.gctl_force = 1;
24832115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) {
249783ee753SPawel Jakub Dawidek 			pjdlog_errno(LOG_WARNING,
250783ee753SPawel Jakub Dawidek 			    "Unable to destroy hast/%s device",
25132115b10SPawel Jakub Dawidek 			    res->hr_provname);
25232115b10SPawel Jakub Dawidek 		}
25332115b10SPawel Jakub Dawidek 		res->hr_ggateunit = -1;
25432115b10SPawel Jakub Dawidek 	}
25532115b10SPawel Jakub Dawidek 
25632115b10SPawel Jakub Dawidek 	/* Restore errno. */
25732115b10SPawel Jakub Dawidek 	errno = rerrno;
25832115b10SPawel Jakub Dawidek }
25932115b10SPawel Jakub Dawidek 
260e43e02f1SPawel Jakub Dawidek static __dead2 void
26132115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...)
26232115b10SPawel Jakub Dawidek {
26332115b10SPawel Jakub Dawidek 	va_list ap;
26432115b10SPawel Jakub Dawidek 
2652ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(exitcode != EX_OK);
26632115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
26732115b10SPawel Jakub Dawidek 	pjdlogv_errno(LOG_ERR, fmt, ap);
26832115b10SPawel Jakub Dawidek 	va_end(ap);
26932115b10SPawel Jakub Dawidek 	cleanup(gres);
27032115b10SPawel Jakub Dawidek 	exit(exitcode);
27132115b10SPawel Jakub Dawidek }
27232115b10SPawel Jakub Dawidek 
273e43e02f1SPawel Jakub Dawidek static __dead2 void
27432115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...)
27532115b10SPawel Jakub Dawidek {
27632115b10SPawel Jakub Dawidek 	va_list ap;
27732115b10SPawel Jakub Dawidek 
27832115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
27932115b10SPawel Jakub Dawidek 	pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
28032115b10SPawel Jakub Dawidek 	va_end(ap);
28132115b10SPawel Jakub Dawidek 	cleanup(gres);
28232115b10SPawel Jakub Dawidek 	exit(exitcode);
28332115b10SPawel Jakub Dawidek }
28432115b10SPawel Jakub Dawidek 
28532115b10SPawel Jakub Dawidek static int
28632115b10SPawel Jakub Dawidek hast_activemap_flush(struct hast_resource *res)
28732115b10SPawel Jakub Dawidek {
28832115b10SPawel Jakub Dawidek 	const unsigned char *buf;
28932115b10SPawel Jakub Dawidek 	size_t size;
29032115b10SPawel Jakub Dawidek 
29132115b10SPawel Jakub Dawidek 	buf = activemap_bitmap(res->hr_amp, &size);
2922ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(buf != NULL);
2932ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
29432115b10SPawel Jakub Dawidek 	if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
29532115b10SPawel Jakub Dawidek 	    (ssize_t)size) {
29632115b10SPawel Jakub Dawidek 		KEEP_ERRNO(pjdlog_errno(LOG_ERR,
29732115b10SPawel Jakub Dawidek 		    "Unable to flush activemap to disk"));
29832115b10SPawel Jakub Dawidek 		return (-1);
29932115b10SPawel Jakub Dawidek 	}
30032115b10SPawel Jakub Dawidek 	return (0);
30132115b10SPawel Jakub Dawidek }
30232115b10SPawel Jakub Dawidek 
303f377917cSPawel Jakub Dawidek static bool
304f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res)
305f377917cSPawel Jakub Dawidek {
306f377917cSPawel Jakub Dawidek 
307f377917cSPawel Jakub Dawidek 	return (strcmp(res->hr_remoteaddr, "none") != 0);
308f377917cSPawel Jakub Dawidek }
309f377917cSPawel Jakub Dawidek 
31032115b10SPawel Jakub Dawidek static void
31132115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused)
31232115b10SPawel Jakub Dawidek {
31332115b10SPawel Jakub Dawidek 	struct hio *hio;
31432115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
31532115b10SPawel Jakub Dawidek 
31632115b10SPawel Jakub Dawidek 	/*
31732115b10SPawel Jakub Dawidek 	 * In the future it might be per-resource value.
31832115b10SPawel Jakub Dawidek 	 */
31932115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
32032115b10SPawel Jakub Dawidek 
32132115b10SPawel Jakub Dawidek 	/*
32232115b10SPawel Jakub Dawidek 	 * Allocate memory needed by lists.
32332115b10SPawel Jakub Dawidek 	 */
32432115b10SPawel Jakub Dawidek 	hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
32532115b10SPawel Jakub Dawidek 	if (hio_send_list == NULL) {
32632115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
32732115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send lists.",
32832115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list[0]) * ncomps);
32932115b10SPawel Jakub Dawidek 	}
33032115b10SPawel Jakub Dawidek 	hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
33132115b10SPawel Jakub Dawidek 	if (hio_send_list_lock == NULL) {
33232115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
33332115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list locks.",
33432115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_lock[0]) * ncomps);
33532115b10SPawel Jakub Dawidek 	}
33632115b10SPawel Jakub Dawidek 	hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
33732115b10SPawel Jakub Dawidek 	if (hio_send_list_cond == NULL) {
33832115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
33932115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list condition variables.",
34032115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_cond[0]) * ncomps);
34132115b10SPawel Jakub Dawidek 	}
34232115b10SPawel Jakub Dawidek 	hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
34332115b10SPawel Jakub Dawidek 	if (hio_recv_list == NULL) {
34432115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
34532115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv lists.",
34632115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list[0]) * ncomps);
34732115b10SPawel Jakub Dawidek 	}
34832115b10SPawel Jakub Dawidek 	hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
34932115b10SPawel Jakub Dawidek 	if (hio_recv_list_lock == NULL) {
35032115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35132115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list locks.",
35232115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_lock[0]) * ncomps);
35332115b10SPawel Jakub Dawidek 	}
35432115b10SPawel Jakub Dawidek 	hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
35532115b10SPawel Jakub Dawidek 	if (hio_recv_list_cond == NULL) {
35632115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35732115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list condition variables.",
35832115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_cond[0]) * ncomps);
35932115b10SPawel Jakub Dawidek 	}
36032115b10SPawel Jakub Dawidek 	hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
36132115b10SPawel Jakub Dawidek 	if (hio_remote_lock == NULL) {
36232115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
36332115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for remote connections locks.",
36432115b10SPawel Jakub Dawidek 		    sizeof(hio_remote_lock[0]) * ncomps);
36532115b10SPawel Jakub Dawidek 	}
36632115b10SPawel Jakub Dawidek 
36732115b10SPawel Jakub Dawidek 	/*
36832115b10SPawel Jakub Dawidek 	 * Initialize lists, their locks and theirs condition variables.
36932115b10SPawel Jakub Dawidek 	 */
37032115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_free_list);
37132115b10SPawel Jakub Dawidek 	mtx_init(&hio_free_list_lock);
37232115b10SPawel Jakub Dawidek 	cv_init(&hio_free_list_cond);
37332115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
37432115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_send_list[ii]);
37532115b10SPawel Jakub Dawidek 		mtx_init(&hio_send_list_lock[ii]);
37632115b10SPawel Jakub Dawidek 		cv_init(&hio_send_list_cond[ii]);
37732115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_recv_list[ii]);
37832115b10SPawel Jakub Dawidek 		mtx_init(&hio_recv_list_lock[ii]);
37932115b10SPawel Jakub Dawidek 		cv_init(&hio_recv_list_cond[ii]);
38032115b10SPawel Jakub Dawidek 		rw_init(&hio_remote_lock[ii]);
38132115b10SPawel Jakub Dawidek 	}
38232115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_done_list);
38332115b10SPawel Jakub Dawidek 	mtx_init(&hio_done_list_lock);
38432115b10SPawel Jakub Dawidek 	cv_init(&hio_done_list_cond);
38532115b10SPawel Jakub Dawidek 	mtx_init(&metadata_lock);
38632115b10SPawel Jakub Dawidek 
38732115b10SPawel Jakub Dawidek 	/*
38832115b10SPawel Jakub Dawidek 	 * Allocate requests pool and initialize requests.
38932115b10SPawel Jakub Dawidek 	 */
39032115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
39132115b10SPawel Jakub Dawidek 		hio = malloc(sizeof(*hio));
39232115b10SPawel Jakub Dawidek 		if (hio == NULL) {
39332115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
39432115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for hio request.",
39532115b10SPawel Jakub Dawidek 			    sizeof(*hio));
39632115b10SPawel Jakub Dawidek 		}
39732115b10SPawel Jakub Dawidek 		hio->hio_countdown = 0;
39832115b10SPawel Jakub Dawidek 		hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
39932115b10SPawel Jakub Dawidek 		if (hio->hio_errors == NULL) {
40032115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
40132115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio errors.",
40232115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_errors[0]) * ncomps);
40332115b10SPawel Jakub Dawidek 		}
40432115b10SPawel Jakub Dawidek 		hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
40532115b10SPawel Jakub Dawidek 		if (hio->hio_next == NULL) {
40632115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
40732115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio_next field.",
40832115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_next[0]) * ncomps);
40932115b10SPawel Jakub Dawidek 		}
41032115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_version = G_GATE_VERSION;
41132115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_data = malloc(MAXPHYS);
41232115b10SPawel Jakub Dawidek 		if (hio->hio_ggio.gctl_data == NULL) {
41332115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
41432115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for gctl_data.",
41532115b10SPawel Jakub Dawidek 			    MAXPHYS);
41632115b10SPawel Jakub Dawidek 		}
41732115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_length = MAXPHYS;
41832115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_error = 0;
41932115b10SPawel Jakub Dawidek 		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
42032115b10SPawel Jakub Dawidek 	}
42132115b10SPawel Jakub Dawidek }
42232115b10SPawel Jakub Dawidek 
423ce837469SPawel Jakub Dawidek static bool
424ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res)
425ce837469SPawel Jakub Dawidek {
426ce837469SPawel Jakub Dawidek 
427ce837469SPawel Jakub Dawidek 	mtx_lock(&metadata_lock);
428ce837469SPawel Jakub Dawidek 	if (res->hr_resuid != 0) {
429ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
430ce837469SPawel Jakub Dawidek 		return (false);
431ce837469SPawel Jakub Dawidek 	} else {
432ce837469SPawel Jakub Dawidek 		/* Initialize unique resource identifier. */
433ce837469SPawel Jakub Dawidek 		arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
434ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
435ce837469SPawel Jakub Dawidek 		if (metadata_write(res) < 0)
436ce837469SPawel Jakub Dawidek 			exit(EX_NOINPUT);
437ce837469SPawel Jakub Dawidek 		return (true);
438ce837469SPawel Jakub Dawidek 	}
439ce837469SPawel Jakub Dawidek }
440ce837469SPawel Jakub Dawidek 
44132115b10SPawel Jakub Dawidek static void
44232115b10SPawel Jakub Dawidek init_local(struct hast_resource *res)
44332115b10SPawel Jakub Dawidek {
44432115b10SPawel Jakub Dawidek 	unsigned char *buf;
44532115b10SPawel Jakub Dawidek 	size_t mapsize;
44632115b10SPawel Jakub Dawidek 
44732115b10SPawel Jakub Dawidek 	if (metadata_read(res, true) < 0)
44832115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
44932115b10SPawel Jakub Dawidek 	mtx_init(&res->hr_amp_lock);
45032115b10SPawel Jakub Dawidek 	if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
45132115b10SPawel Jakub Dawidek 	    res->hr_local_sectorsize, res->hr_keepdirty) < 0) {
45232115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create activemap");
45332115b10SPawel Jakub Dawidek 	}
45432115b10SPawel Jakub Dawidek 	mtx_init(&range_lock);
45532115b10SPawel Jakub Dawidek 	cv_init(&range_regular_cond);
45632115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_regular) < 0)
45732115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
45832115b10SPawel Jakub Dawidek 	cv_init(&range_sync_cond);
45932115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_sync) < 0)
46032115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
46132115b10SPawel Jakub Dawidek 	mapsize = activemap_ondisk_size(res->hr_amp);
46232115b10SPawel Jakub Dawidek 	buf = calloc(1, mapsize);
46332115b10SPawel Jakub Dawidek 	if (buf == NULL) {
46432115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
46532115b10SPawel Jakub Dawidek 		    "Unable to allocate buffer for activemap.");
46632115b10SPawel Jakub Dawidek 	}
46732115b10SPawel Jakub Dawidek 	if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
46832115b10SPawel Jakub Dawidek 	    (ssize_t)mapsize) {
46932115b10SPawel Jakub Dawidek 		primary_exit(EX_NOINPUT, "Unable to read activemap");
47032115b10SPawel Jakub Dawidek 	}
47132115b10SPawel Jakub Dawidek 	activemap_copyin(res->hr_amp, buf, mapsize);
472b0dfbe5bSPawel Jakub Dawidek 	free(buf);
47332115b10SPawel Jakub Dawidek 	if (res->hr_resuid != 0)
47432115b10SPawel Jakub Dawidek 		return;
47532115b10SPawel Jakub Dawidek 	/*
476ce837469SPawel Jakub Dawidek 	 * We're using provider for the first time. Initialize local and remote
477ce837469SPawel Jakub Dawidek 	 * counters. We don't initialize resuid here, as we want to do it just
478ce837469SPawel Jakub Dawidek 	 * in time. The reason for this is that we want to inform secondary
479ce837469SPawel Jakub Dawidek 	 * that there were no writes yet, so there is no need to synchronize
480ce837469SPawel Jakub Dawidek 	 * anything.
48132115b10SPawel Jakub Dawidek 	 */
4829446b453SPawel Jakub Dawidek 	res->hr_primary_localcnt = 0;
48332115b10SPawel Jakub Dawidek 	res->hr_primary_remotecnt = 0;
48432115b10SPawel Jakub Dawidek 	if (metadata_write(res) < 0)
48532115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
48632115b10SPawel Jakub Dawidek }
48732115b10SPawel Jakub Dawidek 
48832ecf620SPawel Jakub Dawidek static int
48932ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp)
49032ecf620SPawel Jakub Dawidek {
49132ecf620SPawel Jakub Dawidek 	struct proto_conn *conn;
49232ecf620SPawel Jakub Dawidek 	int16_t val;
49332ecf620SPawel Jakub Dawidek 
49432ecf620SPawel Jakub Dawidek 	val = 1;
49532ecf620SPawel Jakub Dawidek 	if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
49632ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
49732ecf620SPawel Jakub Dawidek 		    "Unable to send connection request to parent");
49832ecf620SPawel Jakub Dawidek 	}
49932ecf620SPawel Jakub Dawidek 	if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
50032ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
50132ecf620SPawel Jakub Dawidek 		    "Unable to receive reply to connection request from parent");
50232ecf620SPawel Jakub Dawidek 	}
50332ecf620SPawel Jakub Dawidek 	if (val != 0) {
50432ecf620SPawel Jakub Dawidek 		errno = val;
50532ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
50632ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
50732ecf620SPawel Jakub Dawidek 		return (-1);
50832ecf620SPawel Jakub Dawidek 	}
50932ecf620SPawel Jakub Dawidek 	if (proto_connection_recv(res->hr_conn, true, &conn) < 0) {
51032ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
51132ecf620SPawel Jakub Dawidek 		    "Unable to receive connection from parent");
51232ecf620SPawel Jakub Dawidek 	}
5137d4df5cdSPawel Jakub Dawidek 	if (proto_connect_wait(conn, res->hr_timeout) < 0) {
51432ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
51532ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
51632ecf620SPawel Jakub Dawidek 		proto_close(conn);
51732ecf620SPawel Jakub Dawidek 		return (-1);
51832ecf620SPawel Jakub Dawidek 	}
51932ecf620SPawel Jakub Dawidek 	/* Error in setting timeout is not critical, but why should it fail? */
52032ecf620SPawel Jakub Dawidek 	if (proto_timeout(conn, res->hr_timeout) < 0)
52132ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
52232ecf620SPawel Jakub Dawidek 
52332ecf620SPawel Jakub Dawidek 	*connp = conn;
52432ecf620SPawel Jakub Dawidek 
52532ecf620SPawel Jakub Dawidek 	return (0);
52632ecf620SPawel Jakub Dawidek }
52732ecf620SPawel Jakub Dawidek 
528ac0401e3SPawel Jakub Dawidek static int
5290d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp,
5300d9014f3SPawel Jakub Dawidek     struct proto_conn **outp)
53132115b10SPawel Jakub Dawidek {
5320d9014f3SPawel Jakub Dawidek 	struct proto_conn *in, *out;
53332115b10SPawel Jakub Dawidek 	struct nv *nvout, *nvin;
53432115b10SPawel Jakub Dawidek 	const unsigned char *token;
53532115b10SPawel Jakub Dawidek 	unsigned char *map;
53632115b10SPawel Jakub Dawidek 	const char *errmsg;
53732115b10SPawel Jakub Dawidek 	int32_t extentsize;
53832115b10SPawel Jakub Dawidek 	int64_t datasize;
53932115b10SPawel Jakub Dawidek 	uint32_t mapsize;
54032115b10SPawel Jakub Dawidek 	size_t size;
541ac0401e3SPawel Jakub Dawidek 	int error;
54232115b10SPawel Jakub Dawidek 
5432ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
5442ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(real_remote(res));
5450d9014f3SPawel Jakub Dawidek 
5460d9014f3SPawel Jakub Dawidek 	in = out = NULL;
5472be8fd75SPawel Jakub Dawidek 	errmsg = NULL;
5480d9014f3SPawel Jakub Dawidek 
54932ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &out) == -1)
550ac0401e3SPawel Jakub Dawidek 		return (ECONNREFUSED);
551ac0401e3SPawel Jakub Dawidek 
552ac0401e3SPawel Jakub Dawidek 	error = ECONNABORTED;
55332ecf620SPawel Jakub Dawidek 
55432115b10SPawel Jakub Dawidek 	/*
55532115b10SPawel Jakub Dawidek 	 * First handshake step.
55632115b10SPawel Jakub Dawidek 	 * Setup outgoing connection with remote node.
55732115b10SPawel Jakub Dawidek 	 */
55832115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
55932115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
56032115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
56132115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
56232115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
56332115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
56432115b10SPawel Jakub Dawidek 		nv_free(nvout);
56532115b10SPawel Jakub Dawidek 		goto close;
56632115b10SPawel Jakub Dawidek 	}
5670d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, out, nvout, NULL, 0) < 0) {
56832115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
56932115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
57032115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
57132115b10SPawel Jakub Dawidek 		nv_free(nvout);
57232115b10SPawel Jakub Dawidek 		goto close;
57332115b10SPawel Jakub Dawidek 	}
57432115b10SPawel Jakub Dawidek 	nv_free(nvout);
5750d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
57632115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
57732115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
57832115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
57932115b10SPawel Jakub Dawidek 		goto close;
58032115b10SPawel Jakub Dawidek 	}
58132115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
58232115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
58332115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
584ac0401e3SPawel Jakub Dawidek 		if (nv_exists(nvin, "wait"))
585ac0401e3SPawel Jakub Dawidek 			error = EBUSY;
58632115b10SPawel Jakub Dawidek 		nv_free(nvin);
58732115b10SPawel Jakub Dawidek 		goto close;
58832115b10SPawel Jakub Dawidek 	}
58932115b10SPawel Jakub Dawidek 	token = nv_get_uint8_array(nvin, &size, "token");
59032115b10SPawel Jakub Dawidek 	if (token == NULL) {
59132115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s has no 'token' field.",
59232115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
59332115b10SPawel Jakub Dawidek 		nv_free(nvin);
59432115b10SPawel Jakub Dawidek 		goto close;
59532115b10SPawel Jakub Dawidek 	}
59632115b10SPawel Jakub Dawidek 	if (size != sizeof(res->hr_token)) {
59732115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
59832115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr, size, sizeof(res->hr_token));
59932115b10SPawel Jakub Dawidek 		nv_free(nvin);
60032115b10SPawel Jakub Dawidek 		goto close;
60132115b10SPawel Jakub Dawidek 	}
60232115b10SPawel Jakub Dawidek 	bcopy(token, res->hr_token, sizeof(res->hr_token));
60332115b10SPawel Jakub Dawidek 	nv_free(nvin);
60432115b10SPawel Jakub Dawidek 
60532115b10SPawel Jakub Dawidek 	/*
60632115b10SPawel Jakub Dawidek 	 * Second handshake step.
60732115b10SPawel Jakub Dawidek 	 * Setup incoming connection with remote node.
60832115b10SPawel Jakub Dawidek 	 */
60932ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &in) == -1)
61032115b10SPawel Jakub Dawidek 		goto close;
61132ecf620SPawel Jakub Dawidek 
61232115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
61332115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
61432115b10SPawel Jakub Dawidek 	nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
61532115b10SPawel Jakub Dawidek 	    "token");
616ce837469SPawel Jakub Dawidek 	if (res->hr_resuid == 0) {
617ce837469SPawel Jakub Dawidek 		/*
618ce837469SPawel Jakub Dawidek 		 * The resuid field was not yet initialized.
619ce837469SPawel Jakub Dawidek 		 * Because we do synchronization inside init_resuid(), it is
620ce837469SPawel Jakub Dawidek 		 * possible that someone already initialized it, the function
621ce837469SPawel Jakub Dawidek 		 * will return false then, but if we successfully initialized
622ce837469SPawel Jakub Dawidek 		 * it, we will get true. True means that there were no writes
623ce837469SPawel Jakub Dawidek 		 * to this resource yet and we want to inform secondary that
624ce837469SPawel Jakub Dawidek 		 * synchronization is not needed by sending "virgin" argument.
625ce837469SPawel Jakub Dawidek 		 */
626ce837469SPawel Jakub Dawidek 		if (init_resuid(res))
627ce837469SPawel Jakub Dawidek 			nv_add_int8(nvout, 1, "virgin");
628ce837469SPawel Jakub Dawidek 	}
62932115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_resuid, "resuid");
63032115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
63132115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
63232115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
63332115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
63432115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
63532115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
63632115b10SPawel Jakub Dawidek 		nv_free(nvout);
63732115b10SPawel Jakub Dawidek 		goto close;
63832115b10SPawel Jakub Dawidek 	}
6390d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, in, nvout, NULL, 0) < 0) {
64032115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
64132115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
64232115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
64332115b10SPawel Jakub Dawidek 		nv_free(nvout);
64432115b10SPawel Jakub Dawidek 		goto close;
64532115b10SPawel Jakub Dawidek 	}
64632115b10SPawel Jakub Dawidek 	nv_free(nvout);
6470d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
64832115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
64932115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
65032115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
65132115b10SPawel Jakub Dawidek 		goto close;
65232115b10SPawel Jakub Dawidek 	}
65332115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
65432115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
65532115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
65632115b10SPawel Jakub Dawidek 		nv_free(nvin);
65732115b10SPawel Jakub Dawidek 		goto close;
65832115b10SPawel Jakub Dawidek 	}
65932115b10SPawel Jakub Dawidek 	datasize = nv_get_int64(nvin, "datasize");
66032115b10SPawel Jakub Dawidek 	if (datasize != res->hr_datasize) {
66132115b10SPawel Jakub Dawidek 		pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
66232115b10SPawel Jakub Dawidek 		    (intmax_t)res->hr_datasize, (intmax_t)datasize);
66332115b10SPawel Jakub Dawidek 		nv_free(nvin);
66432115b10SPawel Jakub Dawidek 		goto close;
66532115b10SPawel Jakub Dawidek 	}
66632115b10SPawel Jakub Dawidek 	extentsize = nv_get_int32(nvin, "extentsize");
66732115b10SPawel Jakub Dawidek 	if (extentsize != res->hr_extentsize) {
66832115b10SPawel Jakub Dawidek 		pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
66932115b10SPawel Jakub Dawidek 		    (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
67032115b10SPawel Jakub Dawidek 		nv_free(nvin);
67132115b10SPawel Jakub Dawidek 		goto close;
67232115b10SPawel Jakub Dawidek 	}
67332115b10SPawel Jakub Dawidek 	res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
67432115b10SPawel Jakub Dawidek 	res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
67532115b10SPawel Jakub Dawidek 	res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
67606cbf549SPawel Jakub Dawidek 	if (nv_exists(nvin, "virgin")) {
67706cbf549SPawel Jakub Dawidek 		/*
67806cbf549SPawel Jakub Dawidek 		 * Secondary was reinitialized, bump localcnt if it is 0 as
67906cbf549SPawel Jakub Dawidek 		 * only we have the data.
68006cbf549SPawel Jakub Dawidek 		 */
68106cbf549SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
68206cbf549SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
68306cbf549SPawel Jakub Dawidek 
68406cbf549SPawel Jakub Dawidek 		if (res->hr_primary_localcnt == 0) {
68506cbf549SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
68606cbf549SPawel Jakub Dawidek 
68706cbf549SPawel Jakub Dawidek 			mtx_lock(&metadata_lock);
68806cbf549SPawel Jakub Dawidek 			res->hr_primary_localcnt++;
68906cbf549SPawel Jakub Dawidek 			pjdlog_debug(1, "Increasing localcnt to %ju.",
69006cbf549SPawel Jakub Dawidek 			    (uintmax_t)res->hr_primary_localcnt);
69106cbf549SPawel Jakub Dawidek 			(void)metadata_write(res);
69206cbf549SPawel Jakub Dawidek 			mtx_unlock(&metadata_lock);
69306cbf549SPawel Jakub Dawidek 		}
69406cbf549SPawel Jakub Dawidek 	}
69532115b10SPawel Jakub Dawidek 	map = NULL;
69632115b10SPawel Jakub Dawidek 	mapsize = nv_get_uint32(nvin, "mapsize");
69732115b10SPawel Jakub Dawidek 	if (mapsize > 0) {
69832115b10SPawel Jakub Dawidek 		map = malloc(mapsize);
69932115b10SPawel Jakub Dawidek 		if (map == NULL) {
70032115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
70132115b10SPawel Jakub Dawidek 			    (uintmax_t)mapsize);
70232115b10SPawel Jakub Dawidek 			nv_free(nvin);
70332115b10SPawel Jakub Dawidek 			goto close;
70432115b10SPawel Jakub Dawidek 		}
70532115b10SPawel Jakub Dawidek 		/*
70632115b10SPawel Jakub Dawidek 		 * Remote node have some dirty extents on its own, lets
70732115b10SPawel Jakub Dawidek 		 * download its activemap.
70832115b10SPawel Jakub Dawidek 		 */
7090d9014f3SPawel Jakub Dawidek 		if (hast_proto_recv_data(res, out, nvin, map,
71032115b10SPawel Jakub Dawidek 		    mapsize) < 0) {
71132115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
71232115b10SPawel Jakub Dawidek 			    "Unable to receive remote activemap");
71332115b10SPawel Jakub Dawidek 			nv_free(nvin);
71432115b10SPawel Jakub Dawidek 			free(map);
71532115b10SPawel Jakub Dawidek 			goto close;
71632115b10SPawel Jakub Dawidek 		}
71732115b10SPawel Jakub Dawidek 		/*
71832115b10SPawel Jakub Dawidek 		 * Merge local and remote bitmaps.
71932115b10SPawel Jakub Dawidek 		 */
72032115b10SPawel Jakub Dawidek 		activemap_merge(res->hr_amp, map, mapsize);
72132115b10SPawel Jakub Dawidek 		free(map);
72232115b10SPawel Jakub Dawidek 		/*
72332115b10SPawel Jakub Dawidek 		 * Now that we merged bitmaps from both nodes, flush it to the
72432115b10SPawel Jakub Dawidek 		 * disk before we start to synchronize.
72532115b10SPawel Jakub Dawidek 		 */
72632115b10SPawel Jakub Dawidek 		(void)hast_activemap_flush(res);
72732115b10SPawel Jakub Dawidek 	}
728584a9bc3SPawel Jakub Dawidek 	nv_free(nvin);
729*ba2a8224SMikolaj Golub #ifdef notyet
73002dfe972SPawel Jakub Dawidek 	/* Setup directions. */
73102dfe972SPawel Jakub Dawidek 	if (proto_send(out, NULL, 0) == -1)
73202dfe972SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
73302dfe972SPawel Jakub Dawidek 	if (proto_recv(in, NULL, 0) == -1)
73402dfe972SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
735*ba2a8224SMikolaj Golub #endif
73632115b10SPawel Jakub Dawidek 	pjdlog_info("Connected to %s.", res->hr_remoteaddr);
7370d9014f3SPawel Jakub Dawidek 	if (inp != NULL && outp != NULL) {
7380d9014f3SPawel Jakub Dawidek 		*inp = in;
7390d9014f3SPawel Jakub Dawidek 		*outp = out;
7400d9014f3SPawel Jakub Dawidek 	} else {
7410d9014f3SPawel Jakub Dawidek 		res->hr_remotein = in;
7420d9014f3SPawel Jakub Dawidek 		res->hr_remoteout = out;
7430d9014f3SPawel Jakub Dawidek 	}
7445bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_CONNECT);
745ac0401e3SPawel Jakub Dawidek 	return (0);
7460d9014f3SPawel Jakub Dawidek close:
7472be8fd75SPawel Jakub Dawidek 	if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
7485bdff860SPawel Jakub Dawidek 		event_send(res, EVENT_SPLITBRAIN);
7490d9014f3SPawel Jakub Dawidek 	proto_close(out);
7500d9014f3SPawel Jakub Dawidek 	if (in != NULL)
7510d9014f3SPawel Jakub Dawidek 		proto_close(in);
752ac0401e3SPawel Jakub Dawidek 	return (error);
7530d9014f3SPawel Jakub Dawidek }
7540d9014f3SPawel Jakub Dawidek 
7550d9014f3SPawel Jakub Dawidek static void
7560d9014f3SPawel Jakub Dawidek sync_start(void)
7570d9014f3SPawel Jakub Dawidek {
7580d9014f3SPawel Jakub Dawidek 
75932115b10SPawel Jakub Dawidek 	mtx_lock(&sync_lock);
76032115b10SPawel Jakub Dawidek 	sync_inprogress = true;
76132115b10SPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
76232115b10SPawel Jakub Dawidek 	cv_signal(&sync_cond);
76332115b10SPawel Jakub Dawidek }
76432115b10SPawel Jakub Dawidek 
76532115b10SPawel Jakub Dawidek static void
76655ce1e7cSPawel Jakub Dawidek sync_stop(void)
76755ce1e7cSPawel Jakub Dawidek {
76855ce1e7cSPawel Jakub Dawidek 
76955ce1e7cSPawel Jakub Dawidek 	mtx_lock(&sync_lock);
77055ce1e7cSPawel Jakub Dawidek 	if (sync_inprogress)
77155ce1e7cSPawel Jakub Dawidek 		sync_inprogress = false;
77255ce1e7cSPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
77355ce1e7cSPawel Jakub Dawidek }
77455ce1e7cSPawel Jakub Dawidek 
77555ce1e7cSPawel Jakub Dawidek static void
77632115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res)
77732115b10SPawel Jakub Dawidek {
77832115b10SPawel Jakub Dawidek 	struct g_gate_ctl_create ggiocreate;
77932115b10SPawel Jakub Dawidek 	struct g_gate_ctl_cancel ggiocancel;
78032115b10SPawel Jakub Dawidek 
78132115b10SPawel Jakub Dawidek 	/*
78232115b10SPawel Jakub Dawidek 	 * We communicate with ggate via /dev/ggctl. Open it.
78332115b10SPawel Jakub Dawidek 	 */
78432115b10SPawel Jakub Dawidek 	res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
78532115b10SPawel Jakub Dawidek 	if (res->hr_ggatefd < 0)
78632115b10SPawel Jakub Dawidek 		primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
78732115b10SPawel Jakub Dawidek 	/*
78832115b10SPawel Jakub Dawidek 	 * Create provider before trying to connect, as connection failure
78932115b10SPawel Jakub Dawidek 	 * is not critical, but may take some time.
79032115b10SPawel Jakub Dawidek 	 */
7914e47b646SPawel Jakub Dawidek 	bzero(&ggiocreate, sizeof(ggiocreate));
79232115b10SPawel Jakub Dawidek 	ggiocreate.gctl_version = G_GATE_VERSION;
79332115b10SPawel Jakub Dawidek 	ggiocreate.gctl_mediasize = res->hr_datasize;
79432115b10SPawel Jakub Dawidek 	ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
79532115b10SPawel Jakub Dawidek 	ggiocreate.gctl_flags = 0;
7962a49afacSPawel Jakub Dawidek 	ggiocreate.gctl_maxcount = 0;
79732115b10SPawel Jakub Dawidek 	ggiocreate.gctl_timeout = 0;
79832115b10SPawel Jakub Dawidek 	ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
79932115b10SPawel Jakub Dawidek 	snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
80032115b10SPawel Jakub Dawidek 	    res->hr_provname);
80132115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
80232115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s created.", res->hr_provname);
80332115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocreate.gctl_unit;
80432115b10SPawel Jakub Dawidek 		return;
80532115b10SPawel Jakub Dawidek 	}
80632115b10SPawel Jakub Dawidek 	if (errno != EEXIST) {
80732115b10SPawel Jakub Dawidek 		primary_exit(EX_OSERR, "Unable to create hast/%s device",
80832115b10SPawel Jakub Dawidek 		    res->hr_provname);
80932115b10SPawel Jakub Dawidek 	}
81032115b10SPawel Jakub Dawidek 	pjdlog_debug(1,
81132115b10SPawel Jakub Dawidek 	    "Device hast/%s already exists, we will try to take it over.",
81232115b10SPawel Jakub Dawidek 	    res->hr_provname);
81332115b10SPawel Jakub Dawidek 	/*
81432115b10SPawel Jakub Dawidek 	 * If we received EEXIST, we assume that the process who created the
81532115b10SPawel Jakub Dawidek 	 * provider died and didn't clean up. In that case we will start from
81632115b10SPawel Jakub Dawidek 	 * where he left of.
81732115b10SPawel Jakub Dawidek 	 */
8184e47b646SPawel Jakub Dawidek 	bzero(&ggiocancel, sizeof(ggiocancel));
81932115b10SPawel Jakub Dawidek 	ggiocancel.gctl_version = G_GATE_VERSION;
82032115b10SPawel Jakub Dawidek 	ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
82132115b10SPawel Jakub Dawidek 	snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
82232115b10SPawel Jakub Dawidek 	    res->hr_provname);
82332115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
82432115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s recovered.", res->hr_provname);
82532115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocancel.gctl_unit;
82632115b10SPawel Jakub Dawidek 		return;
82732115b10SPawel Jakub Dawidek 	}
82832115b10SPawel Jakub Dawidek 	primary_exit(EX_OSERR, "Unable to take over hast/%s device",
82932115b10SPawel Jakub Dawidek 	    res->hr_provname);
83032115b10SPawel Jakub Dawidek }
83132115b10SPawel Jakub Dawidek 
83232115b10SPawel Jakub Dawidek void
83332115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res)
83432115b10SPawel Jakub Dawidek {
83532115b10SPawel Jakub Dawidek 	pthread_t td;
83632115b10SPawel Jakub Dawidek 	pid_t pid;
837bc7a916aSMikolaj Golub 	int error, mode, debuglevel;
83832115b10SPawel Jakub Dawidek 
83932115b10SPawel Jakub Dawidek 	/*
84032ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending control commands from
84132ecf620SPawel Jakub Dawidek 	 * parent to child.
84232115b10SPawel Jakub Dawidek 	 */
8430b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
844d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
84532115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8466be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
84732115b10SPawel Jakub Dawidek 		    "Unable to create control sockets between parent and child");
84832115b10SPawel Jakub Dawidek 	}
8495bdff860SPawel Jakub Dawidek 	/*
85032ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending events from child to parent.
8515bdff860SPawel Jakub Dawidek 	 */
8520b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
853d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
8545bdff860SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8555bdff860SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
8565bdff860SPawel Jakub Dawidek 		    "Unable to create event sockets between child and parent");
8575bdff860SPawel Jakub Dawidek 	}
85832ecf620SPawel Jakub Dawidek 	/*
85932ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending connection requests from
86032ecf620SPawel Jakub Dawidek 	 * child to parent.
86132ecf620SPawel Jakub Dawidek 	 */
8620b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) {
86332ecf620SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
86432ecf620SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
86532ecf620SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
86632ecf620SPawel Jakub Dawidek 		    "Unable to create connection sockets between child and parent");
86732ecf620SPawel Jakub Dawidek 	}
86832115b10SPawel Jakub Dawidek 
86932115b10SPawel Jakub Dawidek 	pid = fork();
87032115b10SPawel Jakub Dawidek 	if (pid < 0) {
871d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
87232115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8736be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
87432115b10SPawel Jakub Dawidek 	}
87532115b10SPawel Jakub Dawidek 
87632115b10SPawel Jakub Dawidek 	if (pid > 0) {
87732115b10SPawel Jakub Dawidek 		/* This is parent. */
8785bdff860SPawel Jakub Dawidek 		/* Declare that we are receiver. */
8795bdff860SPawel Jakub Dawidek 		proto_recv(res->hr_event, NULL, 0);
88032ecf620SPawel Jakub Dawidek 		proto_recv(res->hr_conn, NULL, 0);
881da1783eaSPawel Jakub Dawidek 		/* Declare that we are sender. */
882da1783eaSPawel Jakub Dawidek 		proto_send(res->hr_ctrl, NULL, 0);
88332115b10SPawel Jakub Dawidek 		res->hr_workerpid = pid;
88432115b10SPawel Jakub Dawidek 		return;
88532115b10SPawel Jakub Dawidek 	}
886ecc99c89SPawel Jakub Dawidek 
8875b41e644SPawel Jakub Dawidek 	gres = res;
888da1783eaSPawel Jakub Dawidek 	mode = pjdlog_mode_get();
889bc7a916aSMikolaj Golub 	debuglevel = pjdlog_debug_get();
89032115b10SPawel Jakub Dawidek 
8915bdff860SPawel Jakub Dawidek 	/* Declare that we are sender. */
8925bdff860SPawel Jakub Dawidek 	proto_send(res->hr_event, NULL, 0);
89332ecf620SPawel Jakub Dawidek 	proto_send(res->hr_conn, NULL, 0);
894da1783eaSPawel Jakub Dawidek 	/* Declare that we are receiver. */
895da1783eaSPawel Jakub Dawidek 	proto_recv(res->hr_ctrl, NULL, 0);
896da1783eaSPawel Jakub Dawidek 	descriptors_cleanup(res);
897da1783eaSPawel Jakub Dawidek 
898f463896eSPawel Jakub Dawidek 	descriptors_assert(res, mode);
899f463896eSPawel Jakub Dawidek 
900da1783eaSPawel Jakub Dawidek 	pjdlog_init(mode);
901bc7a916aSMikolaj Golub 	pjdlog_debug_set(debuglevel);
902da1783eaSPawel Jakub Dawidek 	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
903643080b7SPawel Jakub Dawidek 	setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
9045bdff860SPawel Jakub Dawidek 
90532115b10SPawel Jakub Dawidek 	init_local(res);
90632115b10SPawel Jakub Dawidek 	init_ggate(res);
90732115b10SPawel Jakub Dawidek 	init_environment(res);
908115f4e5cSPawel Jakub Dawidek 
9090cddb12fSPawel Jakub Dawidek 	if (drop_privs(res) != 0) {
9106d7967deSPawel Jakub Dawidek 		cleanup(res);
9116d7967deSPawel Jakub Dawidek 		exit(EX_CONFIG);
9126d7967deSPawel Jakub Dawidek 	}
913f4c96f94SPawel Jakub Dawidek 	pjdlog_info("Privileges successfully dropped.");
9146d7967deSPawel Jakub Dawidek 
9158b70e6aeSPawel Jakub Dawidek 	/*
9164a88128bSPawel Jakub Dawidek 	 * Create the guard thread first, so we can handle signals from the
9174a88128bSPawel Jakub Dawidek 	 * very begining.
9184a88128bSPawel Jakub Dawidek 	 */
9194a88128bSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, guard_thread, res);
9202ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
9214a88128bSPawel Jakub Dawidek 	/*
9228b70e6aeSPawel Jakub Dawidek 	 * Create the control thread before sending any event to the parent,
9238b70e6aeSPawel Jakub Dawidek 	 * as we can deadlock when parent sends control request to worker,
9248b70e6aeSPawel Jakub Dawidek 	 * but worker has no control thread started yet, so parent waits.
9258b70e6aeSPawel Jakub Dawidek 	 * In the meantime worker sends an event to the parent, but parent
9268b70e6aeSPawel Jakub Dawidek 	 * is unable to handle the event, because it waits for control
9278b70e6aeSPawel Jakub Dawidek 	 * request response.
9288b70e6aeSPawel Jakub Dawidek 	 */
9298b70e6aeSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ctrl_thread, res);
9302ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
931ac0401e3SPawel Jakub Dawidek 	if (real_remote(res)) {
932ac0401e3SPawel Jakub Dawidek 		error = init_remote(res, NULL, NULL);
933ac0401e3SPawel Jakub Dawidek 		if (error == 0) {
9348b70e6aeSPawel Jakub Dawidek 			sync_start();
935ac0401e3SPawel Jakub Dawidek 		} else if (error == EBUSY) {
936ac0401e3SPawel Jakub Dawidek 			time_t start = time(NULL);
937ac0401e3SPawel Jakub Dawidek 
938ac0401e3SPawel Jakub Dawidek 			pjdlog_warning("Waiting for remote node to become %s for %ds.",
939ac0401e3SPawel Jakub Dawidek 			    role2str(HAST_ROLE_SECONDARY),
940ac0401e3SPawel Jakub Dawidek 			    res->hr_timeout);
941ac0401e3SPawel Jakub Dawidek 			for (;;) {
942ac0401e3SPawel Jakub Dawidek 				sleep(1);
943ac0401e3SPawel Jakub Dawidek 				error = init_remote(res, NULL, NULL);
944ac0401e3SPawel Jakub Dawidek 				if (error != EBUSY)
945ac0401e3SPawel Jakub Dawidek 					break;
946ac0401e3SPawel Jakub Dawidek 				if (time(NULL) > start + res->hr_timeout)
947ac0401e3SPawel Jakub Dawidek 					break;
948ac0401e3SPawel Jakub Dawidek 			}
949ac0401e3SPawel Jakub Dawidek 			if (error == EBUSY) {
950ac0401e3SPawel Jakub Dawidek 				pjdlog_warning("Remote node is still %s, starting anyway.",
951ac0401e3SPawel Jakub Dawidek 				    role2str(HAST_ROLE_PRIMARY));
952ac0401e3SPawel Jakub Dawidek 			}
953ac0401e3SPawel Jakub Dawidek 		}
954ac0401e3SPawel Jakub Dawidek 	}
95532115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_recv_thread, res);
9562ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
95732115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, local_send_thread, res);
9582ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
95932115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_send_thread, res);
9602ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
96132115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_recv_thread, res);
9622ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
96332115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_send_thread, res);
9642ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
965ac0401e3SPawel Jakub Dawidek 	fullystarted = true;
9664a88128bSPawel Jakub Dawidek 	(void)sync_thread(res);
96732115b10SPawel Jakub Dawidek }
96832115b10SPawel Jakub Dawidek 
96932115b10SPawel Jakub Dawidek static void
97032115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...)
97132115b10SPawel Jakub Dawidek {
97232115b10SPawel Jakub Dawidek 	char msg[1024];
97332115b10SPawel Jakub Dawidek 	va_list ap;
97432115b10SPawel Jakub Dawidek 	int len;
97532115b10SPawel Jakub Dawidek 
97632115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
97732115b10SPawel Jakub Dawidek 	len = vsnprintf(msg, sizeof(msg), fmt, ap);
97832115b10SPawel Jakub Dawidek 	va_end(ap);
97932115b10SPawel Jakub Dawidek 	if ((size_t)len < sizeof(msg)) {
98032115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
98132115b10SPawel Jakub Dawidek 		case BIO_READ:
98232115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
98332115b10SPawel Jakub Dawidek 			    "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
98432115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
98532115b10SPawel Jakub Dawidek 			break;
98632115b10SPawel Jakub Dawidek 		case BIO_DELETE:
98732115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
98832115b10SPawel Jakub Dawidek 			    "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
98932115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
99032115b10SPawel Jakub Dawidek 			break;
99132115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
99232115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
99332115b10SPawel Jakub Dawidek 			break;
99432115b10SPawel Jakub Dawidek 		case BIO_WRITE:
99532115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
99632115b10SPawel Jakub Dawidek 			    "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
99732115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
99832115b10SPawel Jakub Dawidek 			break;
99932115b10SPawel Jakub Dawidek 		default:
100032115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
100132115b10SPawel Jakub Dawidek 			    "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd);
100232115b10SPawel Jakub Dawidek 			break;
100332115b10SPawel Jakub Dawidek 		}
100432115b10SPawel Jakub Dawidek 	}
100532115b10SPawel Jakub Dawidek 	pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
100632115b10SPawel Jakub Dawidek }
100732115b10SPawel Jakub Dawidek 
100832115b10SPawel Jakub Dawidek static void
100932115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp)
101032115b10SPawel Jakub Dawidek {
101132115b10SPawel Jakub Dawidek 
101232115b10SPawel Jakub Dawidek 	rw_wlock(&hio_remote_lock[ncomp]);
101332115b10SPawel Jakub Dawidek 	/*
101432115b10SPawel Jakub Dawidek 	 * A race is possible between dropping rlock and acquiring wlock -
101532115b10SPawel Jakub Dawidek 	 * another thread can close connection in-between.
101632115b10SPawel Jakub Dawidek 	 */
101732115b10SPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
10182ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
10192ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
102032115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
102132115b10SPawel Jakub Dawidek 		return;
102232115b10SPawel Jakub Dawidek 	}
102332115b10SPawel Jakub Dawidek 
10242ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
10252ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
102632115b10SPawel Jakub Dawidek 
10278f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing incoming connection to %s.",
102832115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
102932115b10SPawel Jakub Dawidek 	proto_close(res->hr_remotein);
103032115b10SPawel Jakub Dawidek 	res->hr_remotein = NULL;
10318f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing outgoing connection to %s.",
103232115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
103332115b10SPawel Jakub Dawidek 	proto_close(res->hr_remoteout);
103432115b10SPawel Jakub Dawidek 	res->hr_remoteout = NULL;
103532115b10SPawel Jakub Dawidek 
103632115b10SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
103732115b10SPawel Jakub Dawidek 
10388f8c798cSPawel Jakub Dawidek 	pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
10398f8c798cSPawel Jakub Dawidek 
104032115b10SPawel Jakub Dawidek 	/*
104132115b10SPawel Jakub Dawidek 	 * Stop synchronization if in-progress.
104232115b10SPawel Jakub Dawidek 	 */
104355ce1e7cSPawel Jakub Dawidek 	sync_stop();
10445b41e644SPawel Jakub Dawidek 
10455bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_DISCONNECT);
1046f7fe83f9SPawel Jakub Dawidek }
104732115b10SPawel Jakub Dawidek 
104832115b10SPawel Jakub Dawidek /*
104932115b10SPawel Jakub Dawidek  * Thread receives ggate I/O requests from the kernel and passes them to
105032115b10SPawel Jakub Dawidek  * appropriate threads:
105132115b10SPawel Jakub Dawidek  * WRITE - always goes to both local_send and remote_send threads
105232115b10SPawel Jakub Dawidek  * READ (when the block is up-to-date on local component) -
105332115b10SPawel Jakub Dawidek  *	only local_send thread
105432115b10SPawel Jakub Dawidek  * READ (when the block isn't up-to-date on local component) -
105532115b10SPawel Jakub Dawidek  *	only remote_send thread
105632115b10SPawel Jakub Dawidek  * DELETE - always goes to both local_send and remote_send threads
105732115b10SPawel Jakub Dawidek  * FLUSH - always goes to both local_send and remote_send threads
105832115b10SPawel Jakub Dawidek  */
105932115b10SPawel Jakub Dawidek static void *
106032115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg)
106132115b10SPawel Jakub Dawidek {
106232115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
106332115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
106432115b10SPawel Jakub Dawidek 	struct hio *hio;
106532115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
106632115b10SPawel Jakub Dawidek 	int error;
106732115b10SPawel Jakub Dawidek 
106832115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
106932115b10SPawel Jakub Dawidek 
107032115b10SPawel Jakub Dawidek 	for (;;) {
107132115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: Taking free request.");
107232115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
107332115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
107432115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
107532115b10SPawel Jakub Dawidek 		ggio->gctl_unit = res->hr_ggateunit;
107632115b10SPawel Jakub Dawidek 		ggio->gctl_length = MAXPHYS;
107732115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
107832115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
107932115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Waiting for request from the kernel.",
108032115b10SPawel Jakub Dawidek 		    hio);
108132115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) {
108232115b10SPawel Jakub Dawidek 			if (sigexit_received)
108332115b10SPawel Jakub Dawidek 				pthread_exit(NULL);
108432115b10SPawel Jakub Dawidek 			primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
108532115b10SPawel Jakub Dawidek 		}
108632115b10SPawel Jakub Dawidek 		error = ggio->gctl_error;
108732115b10SPawel Jakub Dawidek 		switch (error) {
108832115b10SPawel Jakub Dawidek 		case 0:
108932115b10SPawel Jakub Dawidek 			break;
109032115b10SPawel Jakub Dawidek 		case ECANCELED:
109132115b10SPawel Jakub Dawidek 			/* Exit gracefully. */
109232115b10SPawel Jakub Dawidek 			if (!sigexit_received) {
109332115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
109432115b10SPawel Jakub Dawidek 				    "ggate_recv: (%p) Received cancel from the kernel.",
109532115b10SPawel Jakub Dawidek 				    hio);
109632115b10SPawel Jakub Dawidek 				pjdlog_info("Received cancel from the kernel, exiting.");
109732115b10SPawel Jakub Dawidek 			}
109832115b10SPawel Jakub Dawidek 			pthread_exit(NULL);
109932115b10SPawel Jakub Dawidek 		case ENOMEM:
110032115b10SPawel Jakub Dawidek 			/*
110132115b10SPawel Jakub Dawidek 			 * Buffer too small? Impossible, we allocate MAXPHYS
110232115b10SPawel Jakub Dawidek 			 * bytes - request can't be bigger than that.
110332115b10SPawel Jakub Dawidek 			 */
110432115b10SPawel Jakub Dawidek 			/* FALLTHROUGH */
110532115b10SPawel Jakub Dawidek 		case ENXIO:
110632115b10SPawel Jakub Dawidek 		default:
110732115b10SPawel Jakub Dawidek 			primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
110832115b10SPawel Jakub Dawidek 			    strerror(error));
110932115b10SPawel Jakub Dawidek 		}
111032115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
111132115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
111232115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio,
111332115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Request received from the kernel: ",
111432115b10SPawel Jakub Dawidek 		    hio);
111532115b10SPawel Jakub Dawidek 		/*
111632115b10SPawel Jakub Dawidek 		 * Inform all components about new write request.
111732115b10SPawel Jakub Dawidek 		 * For read request prefer local component unless the given
111832115b10SPawel Jakub Dawidek 		 * range is out-of-date, then use remote component.
111932115b10SPawel Jakub Dawidek 		 */
112032115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
112132115b10SPawel Jakub Dawidek 		case BIO_READ:
11223db86c39SPawel Jakub Dawidek 			res->hr_stat_read++;
112332115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
112432115b10SPawel Jakub Dawidek 			    "ggate_recv: (%p) Moving request to the send queue.",
112532115b10SPawel Jakub Dawidek 			    hio);
112632115b10SPawel Jakub Dawidek 			refcount_init(&hio->hio_countdown, 1);
112732115b10SPawel Jakub Dawidek 			mtx_lock(&metadata_lock);
112832115b10SPawel Jakub Dawidek 			if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
112932115b10SPawel Jakub Dawidek 			    res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
113032115b10SPawel Jakub Dawidek 				/*
113132115b10SPawel Jakub Dawidek 				 * This range is up-to-date on local component,
113232115b10SPawel Jakub Dawidek 				 * so handle request locally.
113332115b10SPawel Jakub Dawidek 				 */
113432115b10SPawel Jakub Dawidek 				 /* Local component is 0 for now. */
113532115b10SPawel Jakub Dawidek 				ncomp = 0;
113632115b10SPawel Jakub Dawidek 			} else /* if (res->hr_syncsrc ==
113732115b10SPawel Jakub Dawidek 			    HAST_SYNCSRC_SECONDARY) */ {
11382ec483c5SPawel Jakub Dawidek 				PJDLOG_ASSERT(res->hr_syncsrc ==
113932115b10SPawel Jakub Dawidek 				    HAST_SYNCSRC_SECONDARY);
114032115b10SPawel Jakub Dawidek 				/*
114132115b10SPawel Jakub Dawidek 				 * This range is out-of-date on local component,
114232115b10SPawel Jakub Dawidek 				 * so send request to the remote node.
114332115b10SPawel Jakub Dawidek 				 */
114432115b10SPawel Jakub Dawidek 				 /* Remote component is 1 for now. */
114532115b10SPawel Jakub Dawidek 				ncomp = 1;
114632115b10SPawel Jakub Dawidek 			}
114732115b10SPawel Jakub Dawidek 			mtx_unlock(&metadata_lock);
114832115b10SPawel Jakub Dawidek 			QUEUE_INSERT1(hio, send, ncomp);
114932115b10SPawel Jakub Dawidek 			break;
115032115b10SPawel Jakub Dawidek 		case BIO_WRITE:
11513db86c39SPawel Jakub Dawidek 			res->hr_stat_write++;
1152ce837469SPawel Jakub Dawidek 			if (res->hr_resuid == 0) {
11539446b453SPawel Jakub Dawidek 				/*
11549446b453SPawel Jakub Dawidek 				 * This is first write, initialize localcnt and
11559446b453SPawel Jakub Dawidek 				 * resuid.
11569446b453SPawel Jakub Dawidek 				 */
11579446b453SPawel Jakub Dawidek 				res->hr_primary_localcnt = 1;
1158ce837469SPawel Jakub Dawidek 				(void)init_resuid(res);
1159ce837469SPawel Jakub Dawidek 			}
116032115b10SPawel Jakub Dawidek 			for (;;) {
116132115b10SPawel Jakub Dawidek 				mtx_lock(&range_lock);
116232115b10SPawel Jakub Dawidek 				if (rangelock_islocked(range_sync,
116332115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length)) {
116432115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
116532115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu locked.",
116632115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
116732115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
116832115b10SPawel Jakub Dawidek 					range_regular_wait = true;
116932115b10SPawel Jakub Dawidek 					cv_wait(&range_regular_cond, &range_lock);
117032115b10SPawel Jakub Dawidek 					range_regular_wait = false;
117132115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
117232115b10SPawel Jakub Dawidek 					continue;
117332115b10SPawel Jakub Dawidek 				}
117432115b10SPawel Jakub Dawidek 				if (rangelock_add(range_regular,
117532115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length) < 0) {
117632115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
117732115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
117832115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu is already locked, waiting.",
117932115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
118032115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
118132115b10SPawel Jakub Dawidek 					sleep(1);
118232115b10SPawel Jakub Dawidek 					continue;
118332115b10SPawel Jakub Dawidek 				}
118432115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
118532115b10SPawel Jakub Dawidek 				break;
118632115b10SPawel Jakub Dawidek 			}
118732115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
118832115b10SPawel Jakub Dawidek 			if (activemap_write_start(res->hr_amp,
118932115b10SPawel Jakub Dawidek 			    ggio->gctl_offset, ggio->gctl_length)) {
11903db86c39SPawel Jakub Dawidek 				res->hr_stat_activemap_update++;
119132115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
119232115b10SPawel Jakub Dawidek 			}
119332115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
119432115b10SPawel Jakub Dawidek 			/* FALLTHROUGH */
119532115b10SPawel Jakub Dawidek 		case BIO_DELETE:
119632115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
11973db86c39SPawel Jakub Dawidek 			switch (ggio->gctl_cmd) {
11983db86c39SPawel Jakub Dawidek 			case BIO_DELETE:
11993db86c39SPawel Jakub Dawidek 				res->hr_stat_delete++;
12003db86c39SPawel Jakub Dawidek 				break;
12013db86c39SPawel Jakub Dawidek 			case BIO_FLUSH:
12023db86c39SPawel Jakub Dawidek 				res->hr_stat_flush++;
12033db86c39SPawel Jakub Dawidek 				break;
12043db86c39SPawel Jakub Dawidek 			}
120532115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
120632115b10SPawel Jakub Dawidek 			    "ggate_recv: (%p) Moving request to the send queues.",
120732115b10SPawel Jakub Dawidek 			    hio);
120832115b10SPawel Jakub Dawidek 			refcount_init(&hio->hio_countdown, ncomps);
120932115b10SPawel Jakub Dawidek 			for (ii = 0; ii < ncomps; ii++)
121032115b10SPawel Jakub Dawidek 				QUEUE_INSERT1(hio, send, ii);
121132115b10SPawel Jakub Dawidek 			break;
121232115b10SPawel Jakub Dawidek 		}
121332115b10SPawel Jakub Dawidek 	}
121432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
121532115b10SPawel Jakub Dawidek 	return (NULL);
121632115b10SPawel Jakub Dawidek }
121732115b10SPawel Jakub Dawidek 
121832115b10SPawel Jakub Dawidek /*
121932115b10SPawel Jakub Dawidek  * Thread reads from or writes to local component.
122032115b10SPawel Jakub Dawidek  * If local read fails, it redirects it to remote_send thread.
122132115b10SPawel Jakub Dawidek  */
122232115b10SPawel Jakub Dawidek static void *
122332115b10SPawel Jakub Dawidek local_send_thread(void *arg)
122432115b10SPawel Jakub Dawidek {
122532115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
122632115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
122732115b10SPawel Jakub Dawidek 	struct hio *hio;
122832115b10SPawel Jakub Dawidek 	unsigned int ncomp, rncomp;
122932115b10SPawel Jakub Dawidek 	ssize_t ret;
123032115b10SPawel Jakub Dawidek 
123132115b10SPawel Jakub Dawidek 	/* Local component is 0 for now. */
123232115b10SPawel Jakub Dawidek 	ncomp = 0;
123332115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
123432115b10SPawel Jakub Dawidek 	rncomp = 1;
123532115b10SPawel Jakub Dawidek 
123632115b10SPawel Jakub Dawidek 	for (;;) {
123732115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: Taking request.");
1238448efa94SPawel Jakub Dawidek 		QUEUE_TAKE1(hio, send, ncomp, 0);
123932115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: (%p) Got request.", hio);
124032115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
124132115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
124232115b10SPawel Jakub Dawidek 		case BIO_READ:
124332115b10SPawel Jakub Dawidek 			ret = pread(res->hr_localfd, ggio->gctl_data,
124432115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
124532115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
124632115b10SPawel Jakub Dawidek 			if (ret == ggio->gctl_length)
124732115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1248a01a750fSMikolaj Golub 			else if (!ISSYNCREQ(hio)) {
124932115b10SPawel Jakub Dawidek 				/*
125032115b10SPawel Jakub Dawidek 				 * If READ failed, try to read from remote node.
125132115b10SPawel Jakub Dawidek 				 */
1252cd7b7ee5SPawel Jakub Dawidek 				if (ret < 0) {
1253cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1254cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%s), trying remote node. ",
1255cd7b7ee5SPawel Jakub Dawidek 					    strerror(errno));
1256cd7b7ee5SPawel Jakub Dawidek 				} else if (ret != ggio->gctl_length) {
1257cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1258cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%zd != %jd), trying remote node. ",
1259fba1bf5aSPawel Jakub Dawidek 					    ret, (intmax_t)ggio->gctl_length);
1260cd7b7ee5SPawel Jakub Dawidek 				}
126132115b10SPawel Jakub Dawidek 				QUEUE_INSERT1(hio, send, rncomp);
126232115b10SPawel Jakub Dawidek 				continue;
126332115b10SPawel Jakub Dawidek 			}
126432115b10SPawel Jakub Dawidek 			break;
126532115b10SPawel Jakub Dawidek 		case BIO_WRITE:
126632115b10SPawel Jakub Dawidek 			ret = pwrite(res->hr_localfd, ggio->gctl_data,
126732115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
126832115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
1269cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
127032115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1271cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1272cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1273cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1274cd7b7ee5SPawel Jakub Dawidek 			} else if (ret != ggio->gctl_length) {
127532115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = EIO;
1276cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1277cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%zd != %jd): ",
1278fba1bf5aSPawel Jakub Dawidek 				    ret, (intmax_t)ggio->gctl_length);
1279cd7b7ee5SPawel Jakub Dawidek 			} else {
128032115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1281cd7b7ee5SPawel Jakub Dawidek 			}
128232115b10SPawel Jakub Dawidek 			break;
128332115b10SPawel Jakub Dawidek 		case BIO_DELETE:
128432115b10SPawel Jakub Dawidek 			ret = g_delete(res->hr_localfd,
128532115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff,
128632115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
1287cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
128832115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1289cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1290cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1291cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1292cd7b7ee5SPawel Jakub Dawidek 			} else {
129332115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1294cd7b7ee5SPawel Jakub Dawidek 			}
129532115b10SPawel Jakub Dawidek 			break;
129632115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
129732115b10SPawel Jakub Dawidek 			ret = g_flush(res->hr_localfd);
1298cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
129932115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1300cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1301cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1302cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1303cd7b7ee5SPawel Jakub Dawidek 			} else {
130432115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1305cd7b7ee5SPawel Jakub Dawidek 			}
130632115b10SPawel Jakub Dawidek 			break;
130732115b10SPawel Jakub Dawidek 		}
130832115b10SPawel Jakub Dawidek 		if (refcount_release(&hio->hio_countdown)) {
130932115b10SPawel Jakub Dawidek 			if (ISSYNCREQ(hio)) {
131032115b10SPawel Jakub Dawidek 				mtx_lock(&sync_lock);
131132115b10SPawel Jakub Dawidek 				SYNCREQDONE(hio);
131232115b10SPawel Jakub Dawidek 				mtx_unlock(&sync_lock);
131332115b10SPawel Jakub Dawidek 				cv_signal(&sync_cond);
131432115b10SPawel Jakub Dawidek 			} else {
131532115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
131632115b10SPawel Jakub Dawidek 				    "local_send: (%p) Moving request to the done queue.",
131732115b10SPawel Jakub Dawidek 				    hio);
131832115b10SPawel Jakub Dawidek 				QUEUE_INSERT2(hio, done);
131932115b10SPawel Jakub Dawidek 			}
132032115b10SPawel Jakub Dawidek 		}
132132115b10SPawel Jakub Dawidek 	}
132232115b10SPawel Jakub Dawidek 	/* NOTREACHED */
132332115b10SPawel Jakub Dawidek 	return (NULL);
132432115b10SPawel Jakub Dawidek }
132532115b10SPawel Jakub Dawidek 
1326448efa94SPawel Jakub Dawidek static void
1327448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp)
1328448efa94SPawel Jakub Dawidek {
1329448efa94SPawel Jakub Dawidek 	struct nv *nv;
1330448efa94SPawel Jakub Dawidek 
133121e7bc5eSPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
133221e7bc5eSPawel Jakub Dawidek 
133321e7bc5eSPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
133421e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1335448efa94SPawel Jakub Dawidek 		return;
133621e7bc5eSPawel Jakub Dawidek 	}
1337448efa94SPawel Jakub Dawidek 
13382ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
13392ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
1340448efa94SPawel Jakub Dawidek 
1341448efa94SPawel Jakub Dawidek 	nv = nv_alloc();
1342448efa94SPawel Jakub Dawidek 	nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1343448efa94SPawel Jakub Dawidek 	if (nv_error(nv) != 0) {
134421e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1345448efa94SPawel Jakub Dawidek 		nv_free(nv);
1346448efa94SPawel Jakub Dawidek 		pjdlog_debug(1,
1347448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to prepare header to send.");
1348448efa94SPawel Jakub Dawidek 		return;
1349448efa94SPawel Jakub Dawidek 	}
1350448efa94SPawel Jakub Dawidek 	if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) {
135121e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1352448efa94SPawel Jakub Dawidek 		pjdlog_common(LOG_DEBUG, 1, errno,
1353448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to send request");
1354448efa94SPawel Jakub Dawidek 		nv_free(nv);
1355448efa94SPawel Jakub Dawidek 		remote_close(res, ncomp);
1356448efa94SPawel Jakub Dawidek 		return;
1357448efa94SPawel Jakub Dawidek 	}
135821e7bc5eSPawel Jakub Dawidek 
135921e7bc5eSPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
1360448efa94SPawel Jakub Dawidek 	nv_free(nv);
1361448efa94SPawel Jakub Dawidek 	pjdlog_debug(2, "keepalive_send: Request sent.");
1362448efa94SPawel Jakub Dawidek }
1363448efa94SPawel Jakub Dawidek 
136432115b10SPawel Jakub Dawidek /*
136532115b10SPawel Jakub Dawidek  * Thread sends request to secondary node.
136632115b10SPawel Jakub Dawidek  */
136732115b10SPawel Jakub Dawidek static void *
136832115b10SPawel Jakub Dawidek remote_send_thread(void *arg)
136932115b10SPawel Jakub Dawidek {
137032115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
137132115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1372448efa94SPawel Jakub Dawidek 	time_t lastcheck, now;
137332115b10SPawel Jakub Dawidek 	struct hio *hio;
137432115b10SPawel Jakub Dawidek 	struct nv *nv;
137532115b10SPawel Jakub Dawidek 	unsigned int ncomp;
137632115b10SPawel Jakub Dawidek 	bool wakeup;
137732115b10SPawel Jakub Dawidek 	uint64_t offset, length;
137832115b10SPawel Jakub Dawidek 	uint8_t cmd;
137932115b10SPawel Jakub Dawidek 	void *data;
138032115b10SPawel Jakub Dawidek 
138132115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
138232115b10SPawel Jakub Dawidek 	ncomp = 1;
1383448efa94SPawel Jakub Dawidek 	lastcheck = time(NULL);
138432115b10SPawel Jakub Dawidek 
138532115b10SPawel Jakub Dawidek 	for (;;) {
138632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: Taking request.");
13878d7dcf14SMikolaj Golub 		QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1388448efa94SPawel Jakub Dawidek 		if (hio == NULL) {
1389448efa94SPawel Jakub Dawidek 			now = time(NULL);
13908d7dcf14SMikolaj Golub 			if (lastcheck + HAST_KEEPALIVE <= now) {
1391448efa94SPawel Jakub Dawidek 				keepalive_send(res, ncomp);
1392448efa94SPawel Jakub Dawidek 				lastcheck = now;
1393448efa94SPawel Jakub Dawidek 			}
1394448efa94SPawel Jakub Dawidek 			continue;
1395448efa94SPawel Jakub Dawidek 		}
139632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
139732115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
139832115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
139932115b10SPawel Jakub Dawidek 		case BIO_READ:
140032115b10SPawel Jakub Dawidek 			cmd = HIO_READ;
140132115b10SPawel Jakub Dawidek 			data = NULL;
140232115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
140332115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
140432115b10SPawel Jakub Dawidek 			break;
140532115b10SPawel Jakub Dawidek 		case BIO_WRITE:
140632115b10SPawel Jakub Dawidek 			cmd = HIO_WRITE;
140732115b10SPawel Jakub Dawidek 			data = ggio->gctl_data;
140832115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
140932115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
141032115b10SPawel Jakub Dawidek 			break;
141132115b10SPawel Jakub Dawidek 		case BIO_DELETE:
141232115b10SPawel Jakub Dawidek 			cmd = HIO_DELETE;
141332115b10SPawel Jakub Dawidek 			data = NULL;
141432115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
141532115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
141632115b10SPawel Jakub Dawidek 			break;
141732115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
141832115b10SPawel Jakub Dawidek 			cmd = HIO_FLUSH;
141932115b10SPawel Jakub Dawidek 			data = NULL;
142032115b10SPawel Jakub Dawidek 			offset = 0;
142132115b10SPawel Jakub Dawidek 			length = 0;
142232115b10SPawel Jakub Dawidek 			break;
142332115b10SPawel Jakub Dawidek 		default:
14242ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(!"invalid condition");
142532115b10SPawel Jakub Dawidek 			abort();
142632115b10SPawel Jakub Dawidek 		}
142732115b10SPawel Jakub Dawidek 		nv = nv_alloc();
142832115b10SPawel Jakub Dawidek 		nv_add_uint8(nv, cmd, "cmd");
142932115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
143032115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, offset, "offset");
143132115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, length, "length");
143232115b10SPawel Jakub Dawidek 		if (nv_error(nv) != 0) {
143332115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = nv_error(nv);
143432115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
143532115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to prepare header to send.",
143632115b10SPawel Jakub Dawidek 			    hio);
143732115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
143832115b10SPawel Jakub Dawidek 			    "Unable to prepare header to send (%s): ",
143932115b10SPawel Jakub Dawidek 			    strerror(nv_error(nv)));
144032115b10SPawel Jakub Dawidek 			/* Move failed request immediately to the done queue. */
144132115b10SPawel Jakub Dawidek 			goto done_queue;
144232115b10SPawel Jakub Dawidek 		}
144332115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
144432115b10SPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the recv queue.",
144532115b10SPawel Jakub Dawidek 		    hio);
144632115b10SPawel Jakub Dawidek 		/*
144732115b10SPawel Jakub Dawidek 		 * Protect connection from disappearing.
144832115b10SPawel Jakub Dawidek 		 */
144932115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
145032115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
145132115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
145232115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = ENOTCONN;
145332115b10SPawel Jakub Dawidek 			goto done_queue;
145432115b10SPawel Jakub Dawidek 		}
145532115b10SPawel Jakub Dawidek 		/*
145632115b10SPawel Jakub Dawidek 		 * Move the request to recv queue before sending it, because
145732115b10SPawel Jakub Dawidek 		 * in different order we can get reply before we move request
145832115b10SPawel Jakub Dawidek 		 * to recv queue.
145932115b10SPawel Jakub Dawidek 		 */
146032115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
146132115b10SPawel Jakub Dawidek 		wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
146232115b10SPawel Jakub Dawidek 		TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
146332115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
146432115b10SPawel Jakub Dawidek 		if (hast_proto_send(res, res->hr_remoteout, nv, data,
146532115b10SPawel Jakub Dawidek 		    data != NULL ? length : 0) < 0) {
146632115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = errno;
146732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
146832115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
146932115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to send request.", hio);
147032115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
147132115b10SPawel Jakub Dawidek 			    "Unable to send request (%s): ",
147232115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
1473ee087cdfSPawel Jakub Dawidek 			remote_close(res, ncomp);
147432115b10SPawel Jakub Dawidek 			/*
147532115b10SPawel Jakub Dawidek 			 * Take request back from the receive queue and move
147632115b10SPawel Jakub Dawidek 			 * it immediately to the done queue.
147732115b10SPawel Jakub Dawidek 			 */
147832115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
147932115b10SPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
148032115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
148132115b10SPawel Jakub Dawidek 			goto done_queue;
148232115b10SPawel Jakub Dawidek 		}
148332115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
148432115b10SPawel Jakub Dawidek 		nv_free(nv);
148532115b10SPawel Jakub Dawidek 		if (wakeup)
148632115b10SPawel Jakub Dawidek 			cv_signal(&hio_recv_list_cond[ncomp]);
148732115b10SPawel Jakub Dawidek 		continue;
148832115b10SPawel Jakub Dawidek done_queue:
148932115b10SPawel Jakub Dawidek 		nv_free(nv);
149032115b10SPawel Jakub Dawidek 		if (ISSYNCREQ(hio)) {
149132115b10SPawel Jakub Dawidek 			if (!refcount_release(&hio->hio_countdown))
149232115b10SPawel Jakub Dawidek 				continue;
149332115b10SPawel Jakub Dawidek 			mtx_lock(&sync_lock);
149432115b10SPawel Jakub Dawidek 			SYNCREQDONE(hio);
149532115b10SPawel Jakub Dawidek 			mtx_unlock(&sync_lock);
149632115b10SPawel Jakub Dawidek 			cv_signal(&sync_cond);
149732115b10SPawel Jakub Dawidek 			continue;
149832115b10SPawel Jakub Dawidek 		}
149932115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
150032115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
150132115b10SPawel Jakub Dawidek 			if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
150232115b10SPawel Jakub Dawidek 			    ggio->gctl_length)) {
150332115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
150432115b10SPawel Jakub Dawidek 			}
150532115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
150632115b10SPawel Jakub Dawidek 		}
150732115b10SPawel Jakub Dawidek 		if (!refcount_release(&hio->hio_countdown))
150832115b10SPawel Jakub Dawidek 			continue;
150932115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
151032115b10SPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the done queue.",
151132115b10SPawel Jakub Dawidek 		    hio);
151232115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, done);
151332115b10SPawel Jakub Dawidek 	}
151432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
151532115b10SPawel Jakub Dawidek 	return (NULL);
151632115b10SPawel Jakub Dawidek }
151732115b10SPawel Jakub Dawidek 
151832115b10SPawel Jakub Dawidek /*
151932115b10SPawel Jakub Dawidek  * Thread receives answer from secondary node and passes it to ggate_send
152032115b10SPawel Jakub Dawidek  * thread.
152132115b10SPawel Jakub Dawidek  */
152232115b10SPawel Jakub Dawidek static void *
152332115b10SPawel Jakub Dawidek remote_recv_thread(void *arg)
152432115b10SPawel Jakub Dawidek {
152532115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
152632115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
152732115b10SPawel Jakub Dawidek 	struct hio *hio;
152832115b10SPawel Jakub Dawidek 	struct nv *nv;
152932115b10SPawel Jakub Dawidek 	unsigned int ncomp;
153032115b10SPawel Jakub Dawidek 	uint64_t seq;
153132115b10SPawel Jakub Dawidek 	int error;
153232115b10SPawel Jakub Dawidek 
153332115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
153432115b10SPawel Jakub Dawidek 	ncomp = 1;
153532115b10SPawel Jakub Dawidek 
153632115b10SPawel Jakub Dawidek 	for (;;) {
153732115b10SPawel Jakub Dawidek 		/* Wait until there is anything to receive. */
153832115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
153932115b10SPawel Jakub Dawidek 		while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
154032115b10SPawel Jakub Dawidek 			pjdlog_debug(2, "remote_recv: No requests, waiting.");
154132115b10SPawel Jakub Dawidek 			cv_wait(&hio_recv_list_cond[ncomp],
154232115b10SPawel Jakub Dawidek 			    &hio_recv_list_lock[ncomp]);
154332115b10SPawel Jakub Dawidek 		}
154432115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
154532115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
154632115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
154732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
154832115b10SPawel Jakub Dawidek 			/*
154932115b10SPawel Jakub Dawidek 			 * Connection is dead, so move all pending requests to
155032115b10SPawel Jakub Dawidek 			 * the done queue (one-by-one).
155132115b10SPawel Jakub Dawidek 			 */
155232115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
155332115b10SPawel Jakub Dawidek 			hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
15542ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(hio != NULL);
155532115b10SPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
155632115b10SPawel Jakub Dawidek 			    hio_next[ncomp]);
155732115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
155832115b10SPawel Jakub Dawidek 			goto done_queue;
155932115b10SPawel Jakub Dawidek 		}
156032115b10SPawel Jakub Dawidek 		if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) {
156132115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
156232115b10SPawel Jakub Dawidek 			    "Unable to receive reply header");
156332115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
156432115b10SPawel Jakub Dawidek 			remote_close(res, ncomp);
156532115b10SPawel Jakub Dawidek 			continue;
156632115b10SPawel Jakub Dawidek 		}
156732115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
156832115b10SPawel Jakub Dawidek 		seq = nv_get_uint64(nv, "seq");
156932115b10SPawel Jakub Dawidek 		if (seq == 0) {
157032115b10SPawel Jakub Dawidek 			pjdlog_error("Header contains no 'seq' field.");
157132115b10SPawel Jakub Dawidek 			nv_free(nv);
157232115b10SPawel Jakub Dawidek 			continue;
157332115b10SPawel Jakub Dawidek 		}
157432115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
157532115b10SPawel Jakub Dawidek 		TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
157632115b10SPawel Jakub Dawidek 			if (hio->hio_ggio.gctl_seq == seq) {
157732115b10SPawel Jakub Dawidek 				TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
157832115b10SPawel Jakub Dawidek 				    hio_next[ncomp]);
157932115b10SPawel Jakub Dawidek 				break;
158032115b10SPawel Jakub Dawidek 			}
158132115b10SPawel Jakub Dawidek 		}
158232115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
158332115b10SPawel Jakub Dawidek 		if (hio == NULL) {
158432115b10SPawel Jakub Dawidek 			pjdlog_error("Found no request matching received 'seq' field (%ju).",
158532115b10SPawel Jakub Dawidek 			    (uintmax_t)seq);
158632115b10SPawel Jakub Dawidek 			nv_free(nv);
158732115b10SPawel Jakub Dawidek 			continue;
158832115b10SPawel Jakub Dawidek 		}
158932115b10SPawel Jakub Dawidek 		error = nv_get_int16(nv, "error");
159032115b10SPawel Jakub Dawidek 		if (error != 0) {
159132115b10SPawel Jakub Dawidek 			/* Request failed on remote side. */
159272089204SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = error;
1593cd7b7ee5SPawel Jakub Dawidek 			reqlog(LOG_WARNING, 0, &hio->hio_ggio,
1594cd7b7ee5SPawel Jakub Dawidek 			    "Remote request failed (%s): ", strerror(error));
159532115b10SPawel Jakub Dawidek 			nv_free(nv);
159632115b10SPawel Jakub Dawidek 			goto done_queue;
159732115b10SPawel Jakub Dawidek 		}
159832115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
159932115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
160032115b10SPawel Jakub Dawidek 		case BIO_READ:
160132115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
160232115b10SPawel Jakub Dawidek 			if (!ISCONNECTED(res, ncomp)) {
160332115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
160432115b10SPawel Jakub Dawidek 				nv_free(nv);
160532115b10SPawel Jakub Dawidek 				goto done_queue;
160632115b10SPawel Jakub Dawidek 			}
160732115b10SPawel Jakub Dawidek 			if (hast_proto_recv_data(res, res->hr_remotein, nv,
160832115b10SPawel Jakub Dawidek 			    ggio->gctl_data, ggio->gctl_length) < 0) {
160932115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
161032115b10SPawel Jakub Dawidek 				pjdlog_errno(LOG_ERR,
161132115b10SPawel Jakub Dawidek 				    "Unable to receive reply data");
161232115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
161332115b10SPawel Jakub Dawidek 				nv_free(nv);
161432115b10SPawel Jakub Dawidek 				remote_close(res, ncomp);
161532115b10SPawel Jakub Dawidek 				goto done_queue;
161632115b10SPawel Jakub Dawidek 			}
161732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
161832115b10SPawel Jakub Dawidek 			break;
161932115b10SPawel Jakub Dawidek 		case BIO_WRITE:
162032115b10SPawel Jakub Dawidek 		case BIO_DELETE:
162132115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
162232115b10SPawel Jakub Dawidek 			break;
162332115b10SPawel Jakub Dawidek 		default:
16242ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(!"invalid condition");
162532115b10SPawel Jakub Dawidek 			abort();
162632115b10SPawel Jakub Dawidek 		}
162732115b10SPawel Jakub Dawidek 		hio->hio_errors[ncomp] = 0;
162832115b10SPawel Jakub Dawidek 		nv_free(nv);
162932115b10SPawel Jakub Dawidek done_queue:
163032115b10SPawel Jakub Dawidek 		if (refcount_release(&hio->hio_countdown)) {
163132115b10SPawel Jakub Dawidek 			if (ISSYNCREQ(hio)) {
163232115b10SPawel Jakub Dawidek 				mtx_lock(&sync_lock);
163332115b10SPawel Jakub Dawidek 				SYNCREQDONE(hio);
163432115b10SPawel Jakub Dawidek 				mtx_unlock(&sync_lock);
163532115b10SPawel Jakub Dawidek 				cv_signal(&sync_cond);
163632115b10SPawel Jakub Dawidek 			} else {
163732115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
163832115b10SPawel Jakub Dawidek 				    "remote_recv: (%p) Moving request to the done queue.",
163932115b10SPawel Jakub Dawidek 				    hio);
164032115b10SPawel Jakub Dawidek 				QUEUE_INSERT2(hio, done);
164132115b10SPawel Jakub Dawidek 			}
164232115b10SPawel Jakub Dawidek 		}
164332115b10SPawel Jakub Dawidek 	}
164432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
164532115b10SPawel Jakub Dawidek 	return (NULL);
164632115b10SPawel Jakub Dawidek }
164732115b10SPawel Jakub Dawidek 
164832115b10SPawel Jakub Dawidek /*
164932115b10SPawel Jakub Dawidek  * Thread sends answer to the kernel.
165032115b10SPawel Jakub Dawidek  */
165132115b10SPawel Jakub Dawidek static void *
165232115b10SPawel Jakub Dawidek ggate_send_thread(void *arg)
165332115b10SPawel Jakub Dawidek {
165432115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
165532115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
165632115b10SPawel Jakub Dawidek 	struct hio *hio;
165732115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
165832115b10SPawel Jakub Dawidek 
165932115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
166032115b10SPawel Jakub Dawidek 
166132115b10SPawel Jakub Dawidek 	for (;;) {
166232115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: Taking request.");
166332115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, done);
166432115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
166532115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
166632115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
166732115b10SPawel Jakub Dawidek 			if (hio->hio_errors[ii] == 0) {
166832115b10SPawel Jakub Dawidek 				/*
166932115b10SPawel Jakub Dawidek 				 * One successful request is enough to declare
167032115b10SPawel Jakub Dawidek 				 * success.
167132115b10SPawel Jakub Dawidek 				 */
167232115b10SPawel Jakub Dawidek 				ggio->gctl_error = 0;
167332115b10SPawel Jakub Dawidek 				break;
167432115b10SPawel Jakub Dawidek 			}
167532115b10SPawel Jakub Dawidek 		}
167632115b10SPawel Jakub Dawidek 		if (ii == ncomps) {
167732115b10SPawel Jakub Dawidek 			/*
167832115b10SPawel Jakub Dawidek 			 * None of the requests were successful.
1679b068d5aaSMikolaj Golub 			 * Use the error from local component except the
1680b068d5aaSMikolaj Golub 			 * case when we did only remote request.
168132115b10SPawel Jakub Dawidek 			 */
1682b068d5aaSMikolaj Golub 			if (ggio->gctl_cmd == BIO_READ &&
1683b068d5aaSMikolaj Golub 			    res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1684b068d5aaSMikolaj Golub 				ggio->gctl_error = hio->hio_errors[1];
1685b068d5aaSMikolaj Golub 			else
168632115b10SPawel Jakub Dawidek 				ggio->gctl_error = hio->hio_errors[0];
168732115b10SPawel Jakub Dawidek 		}
168832115b10SPawel Jakub Dawidek 		if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
168932115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
169032115b10SPawel Jakub Dawidek 			activemap_write_complete(res->hr_amp,
169132115b10SPawel Jakub Dawidek 			    ggio->gctl_offset, ggio->gctl_length);
169232115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
169332115b10SPawel Jakub Dawidek 		}
169432115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
169532115b10SPawel Jakub Dawidek 			/*
169632115b10SPawel Jakub Dawidek 			 * Unlock range we locked.
169732115b10SPawel Jakub Dawidek 			 */
169832115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
169932115b10SPawel Jakub Dawidek 			rangelock_del(range_regular, ggio->gctl_offset,
170032115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
170132115b10SPawel Jakub Dawidek 			if (range_sync_wait)
170232115b10SPawel Jakub Dawidek 				cv_signal(&range_sync_cond);
170332115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
170432115b10SPawel Jakub Dawidek 			/*
170532115b10SPawel Jakub Dawidek 			 * Bump local count if this is first write after
170632115b10SPawel Jakub Dawidek 			 * connection failure with remote node.
170732115b10SPawel Jakub Dawidek 			 */
170832115b10SPawel Jakub Dawidek 			ncomp = 1;
170932115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
171032115b10SPawel Jakub Dawidek 			if (!ISCONNECTED(res, ncomp)) {
171132115b10SPawel Jakub Dawidek 				mtx_lock(&metadata_lock);
171232115b10SPawel Jakub Dawidek 				if (res->hr_primary_localcnt ==
171332115b10SPawel Jakub Dawidek 				    res->hr_secondary_remotecnt) {
171432115b10SPawel Jakub Dawidek 					res->hr_primary_localcnt++;
171532115b10SPawel Jakub Dawidek 					pjdlog_debug(1,
171632115b10SPawel Jakub Dawidek 					    "Increasing localcnt to %ju.",
171732115b10SPawel Jakub Dawidek 					    (uintmax_t)res->hr_primary_localcnt);
171832115b10SPawel Jakub Dawidek 					(void)metadata_write(res);
171932115b10SPawel Jakub Dawidek 				}
172032115b10SPawel Jakub Dawidek 				mtx_unlock(&metadata_lock);
172132115b10SPawel Jakub Dawidek 			}
172232115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
172332115b10SPawel Jakub Dawidek 		}
172432115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0)
172532115b10SPawel Jakub Dawidek 			primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
172632115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
172732115b10SPawel Jakub Dawidek 		    "ggate_send: (%p) Moving request to the free queue.", hio);
172832115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
172932115b10SPawel Jakub Dawidek 	}
173032115b10SPawel Jakub Dawidek 	/* NOTREACHED */
173132115b10SPawel Jakub Dawidek 	return (NULL);
173232115b10SPawel Jakub Dawidek }
173332115b10SPawel Jakub Dawidek 
173432115b10SPawel Jakub Dawidek /*
173532115b10SPawel Jakub Dawidek  * Thread synchronize local and remote components.
173632115b10SPawel Jakub Dawidek  */
173732115b10SPawel Jakub Dawidek static void *
173832115b10SPawel Jakub Dawidek sync_thread(void *arg __unused)
173932115b10SPawel Jakub Dawidek {
174032115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
174132115b10SPawel Jakub Dawidek 	struct hio *hio;
174232115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1743fa356f6cSPawel Jakub Dawidek 	struct timeval tstart, tend, tdiff;
174432115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
174532115b10SPawel Jakub Dawidek 	off_t offset, length, synced;
174632115b10SPawel Jakub Dawidek 	bool dorewind;
174732115b10SPawel Jakub Dawidek 	int syncext;
174832115b10SPawel Jakub Dawidek 
174932115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
175032115b10SPawel Jakub Dawidek 	dorewind = true;
1751b9cf0cf5SPawel Jakub Dawidek 	synced = 0;
1752b9cf0cf5SPawel Jakub Dawidek 	offset = -1;
175332115b10SPawel Jakub Dawidek 
175432115b10SPawel Jakub Dawidek 	for (;;) {
175532115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
1756b9cf0cf5SPawel Jakub Dawidek 		if (offset >= 0 && !sync_inprogress) {
1757fa356f6cSPawel Jakub Dawidek 			gettimeofday(&tend, NULL);
1758fa356f6cSPawel Jakub Dawidek 			timersub(&tend, &tstart, &tdiff);
1759fa356f6cSPawel Jakub Dawidek 			pjdlog_info("Synchronization interrupted after %#.0T. "
1760fa356f6cSPawel Jakub Dawidek 			    "%NB synchronized so far.", &tdiff,
176153d9b386SPawel Jakub Dawidek 			    (intmax_t)synced);
17625bdff860SPawel Jakub Dawidek 			event_send(res, EVENT_SYNCINTR);
176353d9b386SPawel Jakub Dawidek 		}
176432115b10SPawel Jakub Dawidek 		while (!sync_inprogress) {
176532115b10SPawel Jakub Dawidek 			dorewind = true;
176632115b10SPawel Jakub Dawidek 			synced = 0;
176732115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
176832115b10SPawel Jakub Dawidek 		}
176932115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
177032115b10SPawel Jakub Dawidek 		/*
177132115b10SPawel Jakub Dawidek 		 * Obtain offset at which we should synchronize.
177232115b10SPawel Jakub Dawidek 		 * Rewind synchronization if needed.
177332115b10SPawel Jakub Dawidek 		 */
177432115b10SPawel Jakub Dawidek 		mtx_lock(&res->hr_amp_lock);
177532115b10SPawel Jakub Dawidek 		if (dorewind)
177632115b10SPawel Jakub Dawidek 			activemap_sync_rewind(res->hr_amp);
177732115b10SPawel Jakub Dawidek 		offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
177832115b10SPawel Jakub Dawidek 		if (syncext != -1) {
177932115b10SPawel Jakub Dawidek 			/*
178032115b10SPawel Jakub Dawidek 			 * We synchronized entire syncext extent, we can mark
178132115b10SPawel Jakub Dawidek 			 * it as clean now.
178232115b10SPawel Jakub Dawidek 			 */
178332115b10SPawel Jakub Dawidek 			if (activemap_extent_complete(res->hr_amp, syncext))
178432115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
178532115b10SPawel Jakub Dawidek 		}
178632115b10SPawel Jakub Dawidek 		mtx_unlock(&res->hr_amp_lock);
178732115b10SPawel Jakub Dawidek 		if (dorewind) {
178832115b10SPawel Jakub Dawidek 			dorewind = false;
178932115b10SPawel Jakub Dawidek 			if (offset < 0)
179032115b10SPawel Jakub Dawidek 				pjdlog_info("Nodes are in sync.");
179132115b10SPawel Jakub Dawidek 			else {
1792fa356f6cSPawel Jakub Dawidek 				pjdlog_info("Synchronization started. %NB to go.",
1793fa356f6cSPawel Jakub Dawidek 				    (intmax_t)(res->hr_extentsize *
179432115b10SPawel Jakub Dawidek 				    activemap_ndirty(res->hr_amp)));
17955bdff860SPawel Jakub Dawidek 				event_send(res, EVENT_SYNCSTART);
1796fa356f6cSPawel Jakub Dawidek 				gettimeofday(&tstart, NULL);
179732115b10SPawel Jakub Dawidek 			}
179832115b10SPawel Jakub Dawidek 		}
179932115b10SPawel Jakub Dawidek 		if (offset < 0) {
180055ce1e7cSPawel Jakub Dawidek 			sync_stop();
180132115b10SPawel Jakub Dawidek 			pjdlog_debug(1, "Nothing to synchronize.");
180232115b10SPawel Jakub Dawidek 			/*
180332115b10SPawel Jakub Dawidek 			 * Synchronization complete, make both localcnt and
180432115b10SPawel Jakub Dawidek 			 * remotecnt equal.
180532115b10SPawel Jakub Dawidek 			 */
180632115b10SPawel Jakub Dawidek 			ncomp = 1;
180732115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
180832115b10SPawel Jakub Dawidek 			if (ISCONNECTED(res, ncomp)) {
180932115b10SPawel Jakub Dawidek 				if (synced > 0) {
1810fa356f6cSPawel Jakub Dawidek 					int64_t bps;
1811fa356f6cSPawel Jakub Dawidek 
1812fa356f6cSPawel Jakub Dawidek 					gettimeofday(&tend, NULL);
1813fa356f6cSPawel Jakub Dawidek 					timersub(&tend, &tstart, &tdiff);
1814fa356f6cSPawel Jakub Dawidek 					bps = (int64_t)((double)synced /
1815fa356f6cSPawel Jakub Dawidek 					    ((double)tdiff.tv_sec +
1816fa356f6cSPawel Jakub Dawidek 					    (double)tdiff.tv_usec / 1000000));
181732115b10SPawel Jakub Dawidek 					pjdlog_info("Synchronization complete. "
1818fa356f6cSPawel Jakub Dawidek 					    "%NB synchronized in %#.0lT (%NB/sec).",
1819fa356f6cSPawel Jakub Dawidek 					    (intmax_t)synced, &tdiff,
1820fa356f6cSPawel Jakub Dawidek 					    (intmax_t)bps);
18215bdff860SPawel Jakub Dawidek 					event_send(res, EVENT_SYNCDONE);
182232115b10SPawel Jakub Dawidek 				}
182332115b10SPawel Jakub Dawidek 				mtx_lock(&metadata_lock);
182432115b10SPawel Jakub Dawidek 				res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
182532115b10SPawel Jakub Dawidek 				res->hr_primary_localcnt =
182632115b10SPawel Jakub Dawidek 				    res->hr_secondary_remotecnt;
18279237aa3fSMikolaj Golub 				res->hr_primary_remotecnt =
18289237aa3fSMikolaj Golub 				    res->hr_secondary_localcnt;
182932115b10SPawel Jakub Dawidek 				pjdlog_debug(1,
183032115b10SPawel Jakub Dawidek 				    "Setting localcnt to %ju and remotecnt to %ju.",
183132115b10SPawel Jakub Dawidek 				    (uintmax_t)res->hr_primary_localcnt,
18329237aa3fSMikolaj Golub 				    (uintmax_t)res->hr_primary_remotecnt);
183332115b10SPawel Jakub Dawidek 				(void)metadata_write(res);
183432115b10SPawel Jakub Dawidek 				mtx_unlock(&metadata_lock);
183532115b10SPawel Jakub Dawidek 			}
183632115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
183732115b10SPawel Jakub Dawidek 			continue;
183832115b10SPawel Jakub Dawidek 		}
183932115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: Taking free request.");
184032115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
184132115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Got free request.", hio);
184232115b10SPawel Jakub Dawidek 		/*
184332115b10SPawel Jakub Dawidek 		 * Lock the range we are going to synchronize. We don't want
184432115b10SPawel Jakub Dawidek 		 * race where someone writes between our read and write.
184532115b10SPawel Jakub Dawidek 		 */
184632115b10SPawel Jakub Dawidek 		for (;;) {
184732115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
184832115b10SPawel Jakub Dawidek 			if (rangelock_islocked(range_regular, offset, length)) {
184932115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
185032115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd locked.",
185132115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
185232115b10SPawel Jakub Dawidek 				range_sync_wait = true;
185332115b10SPawel Jakub Dawidek 				cv_wait(&range_sync_cond, &range_lock);
185432115b10SPawel Jakub Dawidek 				range_sync_wait = false;
185532115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
185632115b10SPawel Jakub Dawidek 				continue;
185732115b10SPawel Jakub Dawidek 			}
185832115b10SPawel Jakub Dawidek 			if (rangelock_add(range_sync, offset, length) < 0) {
185932115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
186032115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
186132115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd is already locked, waiting.",
186232115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
186332115b10SPawel Jakub Dawidek 				sleep(1);
186432115b10SPawel Jakub Dawidek 				continue;
186532115b10SPawel Jakub Dawidek 			}
186632115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
186732115b10SPawel Jakub Dawidek 			break;
186832115b10SPawel Jakub Dawidek 		}
186932115b10SPawel Jakub Dawidek 		/*
187032115b10SPawel Jakub Dawidek 		 * First read the data from synchronization source.
187132115b10SPawel Jakub Dawidek 		 */
187232115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
187332115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
187432115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_READ;
187532115b10SPawel Jakub Dawidek 		ggio->gctl_offset = offset;
187632115b10SPawel Jakub Dawidek 		ggio->gctl_length = length;
187732115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
187832115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
187932115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
188032115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
188132115b10SPawel Jakub Dawidek 		    hio);
188232115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
188332115b10SPawel Jakub Dawidek 		    hio);
188432115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
188532115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
188632115b10SPawel Jakub Dawidek 			/*
188732115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
188832115b10SPawel Jakub Dawidek 			 * so handle request locally.
188932115b10SPawel Jakub Dawidek 			 */
189032115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
189132115b10SPawel Jakub Dawidek 			ncomp = 0;
189232115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
18932ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
189432115b10SPawel Jakub Dawidek 			/*
189532115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
189632115b10SPawel Jakub Dawidek 			 * so send request to the remote node.
189732115b10SPawel Jakub Dawidek 			 */
189832115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
189932115b10SPawel Jakub Dawidek 			ncomp = 1;
190032115b10SPawel Jakub Dawidek 		}
190132115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
190232115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, 1);
190332115b10SPawel Jakub Dawidek 		QUEUE_INSERT1(hio, send, ncomp);
190432115b10SPawel Jakub Dawidek 
190532115b10SPawel Jakub Dawidek 		/*
190632115b10SPawel Jakub Dawidek 		 * Let's wait for READ to finish.
190732115b10SPawel Jakub Dawidek 		 */
190832115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
190932115b10SPawel Jakub Dawidek 		while (!ISSYNCREQDONE(hio))
191032115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
191132115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
191232115b10SPawel Jakub Dawidek 
191332115b10SPawel Jakub Dawidek 		if (hio->hio_errors[ncomp] != 0) {
191432115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to read synchronization data: %s.",
191532115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
191632115b10SPawel Jakub Dawidek 			goto free_queue;
191732115b10SPawel Jakub Dawidek 		}
191832115b10SPawel Jakub Dawidek 
191932115b10SPawel Jakub Dawidek 		/*
192032115b10SPawel Jakub Dawidek 		 * We read the data from synchronization source, now write it
192132115b10SPawel Jakub Dawidek 		 * to synchronization target.
192232115b10SPawel Jakub Dawidek 		 */
192332115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
192432115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_WRITE;
192532115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
192632115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
192732115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
192832115b10SPawel Jakub Dawidek 		    hio);
192932115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
193032115b10SPawel Jakub Dawidek 		    hio);
193132115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
193232115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
193332115b10SPawel Jakub Dawidek 			/*
193432115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
193532115b10SPawel Jakub Dawidek 			 * so we update remote component.
193632115b10SPawel Jakub Dawidek 			 */
193732115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
193832115b10SPawel Jakub Dawidek 			ncomp = 1;
193932115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
19402ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
194132115b10SPawel Jakub Dawidek 			/*
194232115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
194332115b10SPawel Jakub Dawidek 			 * so we update it.
194432115b10SPawel Jakub Dawidek 			 */
194532115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
194632115b10SPawel Jakub Dawidek 			ncomp = 0;
194732115b10SPawel Jakub Dawidek 		}
194832115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
194932115b10SPawel Jakub Dawidek 
195032115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queues.",
195132115b10SPawel Jakub Dawidek 		    hio);
195232115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, 1);
195332115b10SPawel Jakub Dawidek 		QUEUE_INSERT1(hio, send, ncomp);
195432115b10SPawel Jakub Dawidek 
195532115b10SPawel Jakub Dawidek 		/*
195632115b10SPawel Jakub Dawidek 		 * Let's wait for WRITE to finish.
195732115b10SPawel Jakub Dawidek 		 */
195832115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
195932115b10SPawel Jakub Dawidek 		while (!ISSYNCREQDONE(hio))
196032115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
196132115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
196232115b10SPawel Jakub Dawidek 
196332115b10SPawel Jakub Dawidek 		if (hio->hio_errors[ncomp] != 0) {
196432115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to write synchronization data: %s.",
196532115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
196632115b10SPawel Jakub Dawidek 			goto free_queue;
196732115b10SPawel Jakub Dawidek 		}
1968e23d2d01SPawel Jakub Dawidek 
1969e23d2d01SPawel Jakub Dawidek 		synced += length;
197032115b10SPawel Jakub Dawidek free_queue:
197132115b10SPawel Jakub Dawidek 		mtx_lock(&range_lock);
197232115b10SPawel Jakub Dawidek 		rangelock_del(range_sync, offset, length);
197332115b10SPawel Jakub Dawidek 		if (range_regular_wait)
197432115b10SPawel Jakub Dawidek 			cv_signal(&range_regular_cond);
197532115b10SPawel Jakub Dawidek 		mtx_unlock(&range_lock);
197632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
197732115b10SPawel Jakub Dawidek 		    hio);
197832115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
197932115b10SPawel Jakub Dawidek 	}
198032115b10SPawel Jakub Dawidek 	/* NOTREACHED */
198132115b10SPawel Jakub Dawidek 	return (NULL);
198232115b10SPawel Jakub Dawidek }
198332115b10SPawel Jakub Dawidek 
1984115f4e5cSPawel Jakub Dawidek void
1985115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv)
19860989854dSPawel Jakub Dawidek {
19870989854dSPawel Jakub Dawidek 	unsigned int ii, ncomps;
1988115f4e5cSPawel Jakub Dawidek 	int modified, vint;
1989115f4e5cSPawel Jakub Dawidek 	const char *vstr;
19900989854dSPawel Jakub Dawidek 
19910989854dSPawel Jakub Dawidek 	pjdlog_info("Reloading configuration...");
19920989854dSPawel Jakub Dawidek 
19932ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
19942ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(gres == res);
1995115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "remoteaddr");
19960b626a28SPawel Jakub Dawidek 	nv_assert(nv, "sourceaddr");
1997115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "replication");
19981fee97b0SPawel Jakub Dawidek 	nv_assert(nv, "checksum");
19998cd3d45aSPawel Jakub Dawidek 	nv_assert(nv, "compression");
2000115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "timeout");
2001115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "exec");
2002115f4e5cSPawel Jakub Dawidek 
20030989854dSPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
20040989854dSPawel Jakub Dawidek 
20051fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR	0x01
20060b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR	0x02
20070b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION	0x04
20080b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM	0x08
20090b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION	0x10
20100b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT	0x20
20110b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC		0x40
20120989854dSPawel Jakub Dawidek 	modified = 0;
2013115f4e5cSPawel Jakub Dawidek 
2014115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "remoteaddr");
2015115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
20160989854dSPawel Jakub Dawidek 		/*
20170989854dSPawel Jakub Dawidek 		 * Don't copy res->hr_remoteaddr to gres just yet.
20180989854dSPawel Jakub Dawidek 		 * We want remote_close() to log disconnect from the old
20190989854dSPawel Jakub Dawidek 		 * addresses, not from the new ones.
20200989854dSPawel Jakub Dawidek 		 */
20210989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REMOTEADDR;
20220989854dSPawel Jakub Dawidek 	}
20230b626a28SPawel Jakub Dawidek 	vstr = nv_get_string(nv, "sourceaddr");
20240b626a28SPawel Jakub Dawidek 	if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
20250b626a28SPawel Jakub Dawidek 		strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
20260b626a28SPawel Jakub Dawidek 		modified |= MODIFIED_SOURCEADDR;
20270b626a28SPawel Jakub Dawidek 	}
2028115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "replication");
2029115f4e5cSPawel Jakub Dawidek 	if (gres->hr_replication != vint) {
2030115f4e5cSPawel Jakub Dawidek 		gres->hr_replication = vint;
20310989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REPLICATION;
20320989854dSPawel Jakub Dawidek 	}
20331fee97b0SPawel Jakub Dawidek 	vint = nv_get_int32(nv, "checksum");
20341fee97b0SPawel Jakub Dawidek 	if (gres->hr_checksum != vint) {
20351fee97b0SPawel Jakub Dawidek 		gres->hr_checksum = vint;
20361fee97b0SPawel Jakub Dawidek 		modified |= MODIFIED_CHECKSUM;
20371fee97b0SPawel Jakub Dawidek 	}
20388cd3d45aSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "compression");
20398cd3d45aSPawel Jakub Dawidek 	if (gres->hr_compression != vint) {
20408cd3d45aSPawel Jakub Dawidek 		gres->hr_compression = vint;
20418cd3d45aSPawel Jakub Dawidek 		modified |= MODIFIED_COMPRESSION;
20428cd3d45aSPawel Jakub Dawidek 	}
2043115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "timeout");
2044115f4e5cSPawel Jakub Dawidek 	if (gres->hr_timeout != vint) {
2045115f4e5cSPawel Jakub Dawidek 		gres->hr_timeout = vint;
20460989854dSPawel Jakub Dawidek 		modified |= MODIFIED_TIMEOUT;
20470989854dSPawel Jakub Dawidek 	}
2048115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "exec");
2049115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_exec, vstr) != 0) {
2050115f4e5cSPawel Jakub Dawidek 		strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
20510becad39SPawel Jakub Dawidek 		modified |= MODIFIED_EXEC;
20520becad39SPawel Jakub Dawidek 	}
2053115f4e5cSPawel Jakub Dawidek 
20540989854dSPawel Jakub Dawidek 	/*
20551fee97b0SPawel Jakub Dawidek 	 * Change timeout for connected sockets.
20561fee97b0SPawel Jakub Dawidek 	 * Don't bother if we need to reconnect.
20570989854dSPawel Jakub Dawidek 	 */
20581fee97b0SPawel Jakub Dawidek 	if ((modified & MODIFIED_TIMEOUT) != 0 &&
20590b626a28SPawel Jakub Dawidek 	    (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR |
20600b626a28SPawel Jakub Dawidek 	    MODIFIED_REPLICATION)) == 0) {
20610989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
20620989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
20630989854dSPawel Jakub Dawidek 				continue;
20640989854dSPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ii]);
20650989854dSPawel Jakub Dawidek 			if (!ISCONNECTED(gres, ii)) {
20660989854dSPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ii]);
20670989854dSPawel Jakub Dawidek 				continue;
20680989854dSPawel Jakub Dawidek 			}
20690989854dSPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ii]);
20700989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remotein,
20710989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
20720989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
20730989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
20740989854dSPawel Jakub Dawidek 			}
20750989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remoteout,
20760989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
20770989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
20780989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
20790989854dSPawel Jakub Dawidek 			}
20800989854dSPawel Jakub Dawidek 		}
20811fee97b0SPawel Jakub Dawidek 	}
20820b626a28SPawel Jakub Dawidek 	if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR |
20830b626a28SPawel Jakub Dawidek 	    MODIFIED_REPLICATION)) != 0) {
20840989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
20850989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
20860989854dSPawel Jakub Dawidek 				continue;
20870989854dSPawel Jakub Dawidek 			remote_close(gres, ii);
20880989854dSPawel Jakub Dawidek 		}
20890989854dSPawel Jakub Dawidek 		if (modified & MODIFIED_REMOTEADDR) {
2090115f4e5cSPawel Jakub Dawidek 			vstr = nv_get_string(nv, "remoteaddr");
2091115f4e5cSPawel Jakub Dawidek 			strlcpy(gres->hr_remoteaddr, vstr,
20920989854dSPawel Jakub Dawidek 			    sizeof(gres->hr_remoteaddr));
20930989854dSPawel Jakub Dawidek 		}
20940989854dSPawel Jakub Dawidek 	}
20950989854dSPawel Jakub Dawidek #undef	MODIFIED_REMOTEADDR
20960b626a28SPawel Jakub Dawidek #undef	MODIFIED_SOURCEADDR
20970989854dSPawel Jakub Dawidek #undef	MODIFIED_REPLICATION
20981fee97b0SPawel Jakub Dawidek #undef	MODIFIED_CHECKSUM
20998cd3d45aSPawel Jakub Dawidek #undef	MODIFIED_COMPRESSION
21000989854dSPawel Jakub Dawidek #undef	MODIFIED_TIMEOUT
21010becad39SPawel Jakub Dawidek #undef	MODIFIED_EXEC
21020989854dSPawel Jakub Dawidek 
21030989854dSPawel Jakub Dawidek 	pjdlog_info("Configuration reloaded successfully.");
21040989854dSPawel Jakub Dawidek }
21050989854dSPawel Jakub Dawidek 
2106f7fe83f9SPawel Jakub Dawidek static void
2107ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp)
2108ff6bb1f8SPawel Jakub Dawidek {
2109ff6bb1f8SPawel Jakub Dawidek 	struct proto_conn *in, *out;
2110ff6bb1f8SPawel Jakub Dawidek 
2111ff6bb1f8SPawel Jakub Dawidek 	if (!ISREMOTE(ncomp))
2112ff6bb1f8SPawel Jakub Dawidek 		return;
2113ff6bb1f8SPawel Jakub Dawidek 
2114ff6bb1f8SPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
2115ff6bb1f8SPawel Jakub Dawidek 
2116ff6bb1f8SPawel Jakub Dawidek 	if (!real_remote(res)) {
2117ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2118ff6bb1f8SPawel Jakub Dawidek 		return;
2119ff6bb1f8SPawel Jakub Dawidek 	}
2120ff6bb1f8SPawel Jakub Dawidek 
2121ff6bb1f8SPawel Jakub Dawidek 	if (ISCONNECTED(res, ncomp)) {
21222ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein != NULL);
21232ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout != NULL);
2124ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2125ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2126ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2127ff6bb1f8SPawel Jakub Dawidek 		return;
2128ff6bb1f8SPawel Jakub Dawidek 	}
2129ff6bb1f8SPawel Jakub Dawidek 
21302ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein == NULL);
21312ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout == NULL);
2132ff6bb1f8SPawel Jakub Dawidek 	/*
2133ff6bb1f8SPawel Jakub Dawidek 	 * Upgrade the lock. It doesn't have to be atomic as no other thread
2134ff6bb1f8SPawel Jakub Dawidek 	 * can change connection status from disconnected to connected.
2135ff6bb1f8SPawel Jakub Dawidek 	 */
2136ff6bb1f8SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
2137ff6bb1f8SPawel Jakub Dawidek 	pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2138ff6bb1f8SPawel Jakub Dawidek 	    res->hr_remoteaddr);
2139ff6bb1f8SPawel Jakub Dawidek 	in = out = NULL;
2140ac0401e3SPawel Jakub Dawidek 	if (init_remote(res, &in, &out) == 0) {
2141ff6bb1f8SPawel Jakub Dawidek 		rw_wlock(&hio_remote_lock[ncomp]);
21422ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
21432ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
21442ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in != NULL && out != NULL);
2145ff6bb1f8SPawel Jakub Dawidek 		res->hr_remotein = in;
2146ff6bb1f8SPawel Jakub Dawidek 		res->hr_remoteout = out;
2147ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2148ff6bb1f8SPawel Jakub Dawidek 		pjdlog_info("Successfully reconnected to %s.",
2149ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2150ff6bb1f8SPawel Jakub Dawidek 		sync_start();
2151ff6bb1f8SPawel Jakub Dawidek 	} else {
2152ff6bb1f8SPawel Jakub Dawidek 		/* Both connections should be NULL. */
21532ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
21542ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
21552ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in == NULL && out == NULL);
2156ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2157ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2158ff6bb1f8SPawel Jakub Dawidek 	}
2159ff6bb1f8SPawel Jakub Dawidek }
2160ff6bb1f8SPawel Jakub Dawidek 
216132115b10SPawel Jakub Dawidek /*
216232115b10SPawel Jakub Dawidek  * Thread guards remote connections and reconnects when needed, handles
216332115b10SPawel Jakub Dawidek  * signals, etc.
216432115b10SPawel Jakub Dawidek  */
216532115b10SPawel Jakub Dawidek static void *
216632115b10SPawel Jakub Dawidek guard_thread(void *arg)
216732115b10SPawel Jakub Dawidek {
216832115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
216932115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
21706d0c801eSPawel Jakub Dawidek 	struct timespec timeout;
2171ff6bb1f8SPawel Jakub Dawidek 	time_t lastcheck, now;
21726d0c801eSPawel Jakub Dawidek 	sigset_t mask;
21736d0c801eSPawel Jakub Dawidek 	int signo;
217432115b10SPawel Jakub Dawidek 
217532115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
2176ff6bb1f8SPawel Jakub Dawidek 	lastcheck = time(NULL);
217732115b10SPawel Jakub Dawidek 
21786d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
21796d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
21806d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
21816d0c801eSPawel Jakub Dawidek 
21828d7dcf14SMikolaj Golub 	timeout.tv_sec = HAST_KEEPALIVE;
21836d0c801eSPawel Jakub Dawidek 	timeout.tv_nsec = 0;
21846d0c801eSPawel Jakub Dawidek 	signo = -1;
21856d0c801eSPawel Jakub Dawidek 
218632115b10SPawel Jakub Dawidek 	for (;;) {
21876d0c801eSPawel Jakub Dawidek 		switch (signo) {
21886d0c801eSPawel Jakub Dawidek 		case SIGINT:
21896d0c801eSPawel Jakub Dawidek 		case SIGTERM:
21906d0c801eSPawel Jakub Dawidek 			sigexit_received = true;
219132115b10SPawel Jakub Dawidek 			primary_exitx(EX_OK,
219232115b10SPawel Jakub Dawidek 			    "Termination signal received, exiting.");
21936d0c801eSPawel Jakub Dawidek 			break;
21946d0c801eSPawel Jakub Dawidek 		default:
2195ff6bb1f8SPawel Jakub Dawidek 			break;
2196f7fe83f9SPawel Jakub Dawidek 		}
21976d0c801eSPawel Jakub Dawidek 
2198ac0401e3SPawel Jakub Dawidek 		/*
2199ac0401e3SPawel Jakub Dawidek 		 * Don't check connections until we fully started,
2200ac0401e3SPawel Jakub Dawidek 		 * as we may still be looping, waiting for remote node
2201ac0401e3SPawel Jakub Dawidek 		 * to switch from primary to secondary.
2202ac0401e3SPawel Jakub Dawidek 		 */
2203ac0401e3SPawel Jakub Dawidek 		if (fullystarted) {
22046d0c801eSPawel Jakub Dawidek 			pjdlog_debug(2, "remote_guard: Checking connections.");
2205ff6bb1f8SPawel Jakub Dawidek 			now = time(NULL);
22068d7dcf14SMikolaj Golub 			if (lastcheck + HAST_KEEPALIVE <= now) {
22076d0c801eSPawel Jakub Dawidek 				for (ii = 0; ii < ncomps; ii++)
2208ff6bb1f8SPawel Jakub Dawidek 					guard_one(res, ii);
2209ff6bb1f8SPawel Jakub Dawidek 				lastcheck = now;
221032115b10SPawel Jakub Dawidek 			}
2211ac0401e3SPawel Jakub Dawidek 		}
22126d0c801eSPawel Jakub Dawidek 		signo = sigtimedwait(&mask, NULL, &timeout);
221332115b10SPawel Jakub Dawidek 	}
221432115b10SPawel Jakub Dawidek 	/* NOTREACHED */
221532115b10SPawel Jakub Dawidek 	return (NULL);
221632115b10SPawel Jakub Dawidek }
2217