xref: /freebsd/sbin/hastd/primary.c (revision dfb1aece41a2e232261809b866c19153cf1007dd)
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;
9207ebc362SPawel Jakub Dawidek 	/*
9307ebc362SPawel Jakub Dawidek 	 * Request was already confirmed to GEOM Gate.
9407ebc362SPawel Jakub Dawidek 	 */
9507ebc362SPawel Jakub Dawidek 	bool			 hio_done;
9607ebc362SPawel Jakub Dawidek 	/*
9707ebc362SPawel Jakub Dawidek 	 * Remember replication from the time the request was initiated,
9807ebc362SPawel Jakub Dawidek 	 * so we won't get confused when replication changes on reload.
9907ebc362SPawel Jakub Dawidek 	 */
10007ebc362SPawel Jakub Dawidek 	int			 hio_replication;
10132115b10SPawel Jakub Dawidek 	TAILQ_ENTRY(hio)	*hio_next;
10232115b10SPawel Jakub Dawidek };
10332115b10SPawel Jakub Dawidek #define	hio_free_next	hio_next[0]
10432115b10SPawel Jakub Dawidek #define	hio_done_next	hio_next[0]
10532115b10SPawel Jakub Dawidek 
10632115b10SPawel Jakub Dawidek /*
10732115b10SPawel Jakub Dawidek  * Free list holds unused structures. When free list is empty, we have to wait
10832115b10SPawel Jakub Dawidek  * until some in-progress requests are freed.
10932115b10SPawel Jakub Dawidek  */
11032115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_free_list;
11132115b10SPawel Jakub Dawidek static pthread_mutex_t hio_free_list_lock;
11232115b10SPawel Jakub Dawidek static pthread_cond_t hio_free_list_cond;
11332115b10SPawel Jakub Dawidek /*
11432115b10SPawel Jakub Dawidek  * There is one send list for every component. One requests is placed on all
11532115b10SPawel Jakub Dawidek  * send lists - each component gets the same request, but each component is
11632115b10SPawel Jakub Dawidek  * responsible for managing his own send list.
11732115b10SPawel Jakub Dawidek  */
11832115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_send_list;
11932115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_send_list_lock;
12032115b10SPawel Jakub Dawidek static pthread_cond_t *hio_send_list_cond;
12132115b10SPawel Jakub Dawidek /*
12232115b10SPawel Jakub Dawidek  * There is one recv list for every component, although local components don't
12332115b10SPawel Jakub Dawidek  * use recv lists as local requests are done synchronously.
12432115b10SPawel Jakub Dawidek  */
12532115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_recv_list;
12632115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_recv_list_lock;
12732115b10SPawel Jakub Dawidek static pthread_cond_t *hio_recv_list_cond;
12832115b10SPawel Jakub Dawidek /*
12932115b10SPawel Jakub Dawidek  * Request is placed on done list by the slowest component (the one that
13032115b10SPawel Jakub Dawidek  * decreased hio_countdown from 1 to 0).
13132115b10SPawel Jakub Dawidek  */
13232115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_done_list;
13332115b10SPawel Jakub Dawidek static pthread_mutex_t hio_done_list_lock;
13432115b10SPawel Jakub Dawidek static pthread_cond_t hio_done_list_cond;
13532115b10SPawel Jakub Dawidek /*
13632115b10SPawel Jakub Dawidek  * Structure below are for interaction with sync thread.
13732115b10SPawel Jakub Dawidek  */
13832115b10SPawel Jakub Dawidek static bool sync_inprogress;
13932115b10SPawel Jakub Dawidek static pthread_mutex_t sync_lock;
14032115b10SPawel Jakub Dawidek static pthread_cond_t sync_cond;
14132115b10SPawel Jakub Dawidek /*
14232115b10SPawel Jakub Dawidek  * The lock below allows to synchornize access to remote connections.
14332115b10SPawel Jakub Dawidek  */
14432115b10SPawel Jakub Dawidek static pthread_rwlock_t *hio_remote_lock;
14532115b10SPawel Jakub Dawidek 
14632115b10SPawel Jakub Dawidek /*
14732115b10SPawel Jakub Dawidek  * Lock to synchronize metadata updates. Also synchronize access to
14832115b10SPawel Jakub Dawidek  * hr_primary_localcnt and hr_primary_remotecnt fields.
14932115b10SPawel Jakub Dawidek  */
15032115b10SPawel Jakub Dawidek static pthread_mutex_t metadata_lock;
15132115b10SPawel Jakub Dawidek 
15232115b10SPawel Jakub Dawidek /*
15332115b10SPawel Jakub Dawidek  * Maximum number of outstanding I/O requests.
15432115b10SPawel Jakub Dawidek  */
15532115b10SPawel Jakub Dawidek #define	HAST_HIO_MAX	256
15632115b10SPawel Jakub Dawidek /*
15732115b10SPawel Jakub Dawidek  * Number of components. At this point there are only two components: local
15832115b10SPawel Jakub Dawidek  * and remote, but in the future it might be possible to use multiple local
15932115b10SPawel Jakub Dawidek  * and remote components.
16032115b10SPawel Jakub Dawidek  */
16132115b10SPawel Jakub Dawidek #define	HAST_NCOMPONENTS	2
16232115b10SPawel Jakub Dawidek 
16332115b10SPawel Jakub Dawidek #define	ISCONNECTED(res, no)	\
16432115b10SPawel Jakub Dawidek 	((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
16532115b10SPawel Jakub Dawidek 
16632115b10SPawel Jakub Dawidek #define	QUEUE_INSERT1(hio, name, ncomp)	do {				\
16732115b10SPawel Jakub Dawidek 	bool _wakeup;							\
16832115b10SPawel Jakub Dawidek 									\
16932115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
17032115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]);		\
17132115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio),		\
17232115b10SPawel Jakub Dawidek 	    hio_next[(ncomp)]);						\
17332115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[ncomp]);			\
17432115b10SPawel Jakub Dawidek 	if (_wakeup)							\
17532115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond[(ncomp)]);		\
17632115b10SPawel Jakub Dawidek } while (0)
17732115b10SPawel Jakub Dawidek #define	QUEUE_INSERT2(hio, name)	do {				\
17832115b10SPawel Jakub Dawidek 	bool _wakeup;							\
17932115b10SPawel Jakub Dawidek 									\
18032115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
18132115b10SPawel Jakub Dawidek 	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
18232115b10SPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
18332115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
18432115b10SPawel Jakub Dawidek 	if (_wakeup)							\
18532115b10SPawel Jakub Dawidek 		cv_signal(&hio_##name##_list_cond);			\
18632115b10SPawel Jakub Dawidek } while (0)
187448efa94SPawel Jakub Dawidek #define	QUEUE_TAKE1(hio, name, ncomp, timeout)	do {			\
188448efa94SPawel Jakub Dawidek 	bool _last;							\
189448efa94SPawel Jakub Dawidek 									\
19032115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock[(ncomp)]);			\
191448efa94SPawel Jakub Dawidek 	_last = false;							\
192448efa94SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
193448efa94SPawel Jakub Dawidek 		cv_timedwait(&hio_##name##_list_cond[(ncomp)],		\
194448efa94SPawel Jakub Dawidek 		    &hio_##name##_list_lock[(ncomp)], (timeout));	\
195448efa94SPawel Jakub Dawidek 		if ((timeout) != 0)					\
196448efa94SPawel Jakub Dawidek 			_last = true;					\
19732115b10SPawel Jakub Dawidek 	}								\
198448efa94SPawel Jakub Dawidek 	if (hio != NULL) {						\
19932115b10SPawel Jakub Dawidek 		TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio),	\
20032115b10SPawel Jakub Dawidek 		    hio_next[(ncomp)]);					\
201448efa94SPawel Jakub Dawidek 	}								\
20232115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock[(ncomp)]);			\
20332115b10SPawel Jakub Dawidek } while (0)
20432115b10SPawel Jakub Dawidek #define	QUEUE_TAKE2(hio, name)	do {					\
20532115b10SPawel Jakub Dawidek 	mtx_lock(&hio_##name##_list_lock);				\
20632115b10SPawel Jakub Dawidek 	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
20732115b10SPawel Jakub Dawidek 		cv_wait(&hio_##name##_list_cond,			\
20832115b10SPawel Jakub Dawidek 		    &hio_##name##_list_lock);				\
20932115b10SPawel Jakub Dawidek 	}								\
21032115b10SPawel Jakub Dawidek 	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next);	\
21132115b10SPawel Jakub Dawidek 	mtx_unlock(&hio_##name##_list_lock);				\
21232115b10SPawel Jakub Dawidek } while (0)
21332115b10SPawel Jakub Dawidek 
214328e0f4bSPawel Jakub Dawidek #define	SYNCREQ(hio)		do {					\
215328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_unit = -1;					\
216328e0f4bSPawel Jakub Dawidek 	(hio)->hio_ggio.gctl_seq = 1;					\
217328e0f4bSPawel Jakub Dawidek } while (0)
21832115b10SPawel Jakub Dawidek #define	ISSYNCREQ(hio)		((hio)->hio_ggio.gctl_unit == -1)
21932115b10SPawel Jakub Dawidek #define	SYNCREQDONE(hio)	do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
22032115b10SPawel Jakub Dawidek #define	ISSYNCREQDONE(hio)	((hio)->hio_ggio.gctl_unit == -2)
22132115b10SPawel Jakub Dawidek 
22232115b10SPawel Jakub Dawidek static struct hast_resource *gres;
22332115b10SPawel Jakub Dawidek 
22432115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock;
22532115b10SPawel Jakub Dawidek static struct rangelocks *range_regular;
22632115b10SPawel Jakub Dawidek static bool range_regular_wait;
22732115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond;
22832115b10SPawel Jakub Dawidek static struct rangelocks *range_sync;
22932115b10SPawel Jakub Dawidek static bool range_sync_wait;
23032115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond;
231ac0401e3SPawel Jakub Dawidek static bool fullystarted;
23232115b10SPawel Jakub Dawidek 
23332115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg);
23432115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg);
23532115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg);
23632115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg);
23732115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg);
23832115b10SPawel Jakub Dawidek static void *sync_thread(void *arg);
23932115b10SPawel Jakub Dawidek static void *guard_thread(void *arg);
24032115b10SPawel Jakub Dawidek 
2416d0c801eSPawel Jakub Dawidek static void
24232115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res)
24332115b10SPawel Jakub Dawidek {
24432115b10SPawel Jakub Dawidek 	int rerrno;
24532115b10SPawel Jakub Dawidek 
24632115b10SPawel Jakub Dawidek 	/* Remember errno. */
24732115b10SPawel Jakub Dawidek 	rerrno = errno;
24832115b10SPawel Jakub Dawidek 
24932115b10SPawel Jakub Dawidek 	/* Destroy ggate provider if we created one. */
25032115b10SPawel Jakub Dawidek 	if (res->hr_ggateunit >= 0) {
25132115b10SPawel Jakub Dawidek 		struct g_gate_ctl_destroy ggiod;
25232115b10SPawel Jakub Dawidek 
2534e47b646SPawel Jakub Dawidek 		bzero(&ggiod, sizeof(ggiod));
25432115b10SPawel Jakub Dawidek 		ggiod.gctl_version = G_GATE_VERSION;
25532115b10SPawel Jakub Dawidek 		ggiod.gctl_unit = res->hr_ggateunit;
25632115b10SPawel Jakub Dawidek 		ggiod.gctl_force = 1;
25732115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) {
258783ee753SPawel Jakub Dawidek 			pjdlog_errno(LOG_WARNING,
259783ee753SPawel Jakub Dawidek 			    "Unable to destroy hast/%s device",
26032115b10SPawel Jakub Dawidek 			    res->hr_provname);
26132115b10SPawel Jakub Dawidek 		}
26232115b10SPawel Jakub Dawidek 		res->hr_ggateunit = -1;
26332115b10SPawel Jakub Dawidek 	}
26432115b10SPawel Jakub Dawidek 
26532115b10SPawel Jakub Dawidek 	/* Restore errno. */
26632115b10SPawel Jakub Dawidek 	errno = rerrno;
26732115b10SPawel Jakub Dawidek }
26832115b10SPawel Jakub Dawidek 
269e43e02f1SPawel Jakub Dawidek static __dead2 void
27032115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...)
27132115b10SPawel Jakub Dawidek {
27232115b10SPawel Jakub Dawidek 	va_list ap;
27332115b10SPawel Jakub Dawidek 
2742ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(exitcode != EX_OK);
27532115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
27632115b10SPawel Jakub Dawidek 	pjdlogv_errno(LOG_ERR, fmt, ap);
27732115b10SPawel Jakub Dawidek 	va_end(ap);
27832115b10SPawel Jakub Dawidek 	cleanup(gres);
27932115b10SPawel Jakub Dawidek 	exit(exitcode);
28032115b10SPawel Jakub Dawidek }
28132115b10SPawel Jakub Dawidek 
282e43e02f1SPawel Jakub Dawidek static __dead2 void
28332115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...)
28432115b10SPawel Jakub Dawidek {
28532115b10SPawel Jakub Dawidek 	va_list ap;
28632115b10SPawel Jakub Dawidek 
28732115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
28832115b10SPawel Jakub Dawidek 	pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
28932115b10SPawel Jakub Dawidek 	va_end(ap);
29032115b10SPawel Jakub Dawidek 	cleanup(gres);
29132115b10SPawel Jakub Dawidek 	exit(exitcode);
29232115b10SPawel Jakub Dawidek }
29332115b10SPawel Jakub Dawidek 
29432115b10SPawel Jakub Dawidek static int
29532115b10SPawel Jakub Dawidek hast_activemap_flush(struct hast_resource *res)
29632115b10SPawel Jakub Dawidek {
29732115b10SPawel Jakub Dawidek 	const unsigned char *buf;
29832115b10SPawel Jakub Dawidek 	size_t size;
29932115b10SPawel Jakub Dawidek 
30032115b10SPawel Jakub Dawidek 	buf = activemap_bitmap(res->hr_amp, &size);
3012ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(buf != NULL);
3022ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
30332115b10SPawel Jakub Dawidek 	if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
30432115b10SPawel Jakub Dawidek 	    (ssize_t)size) {
305be1143efSPawel Jakub Dawidek 		pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
30632115b10SPawel Jakub Dawidek 		return (-1);
30732115b10SPawel Jakub Dawidek 	}
308518dd4c0SPawel Jakub Dawidek 	if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) {
309518dd4c0SPawel Jakub Dawidek 		if (errno == EOPNOTSUPP) {
310518dd4c0SPawel Jakub Dawidek 			pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
311518dd4c0SPawel Jakub Dawidek 			    res->hr_localpath);
312518dd4c0SPawel Jakub Dawidek 			res->hr_metaflush = 0;
313518dd4c0SPawel Jakub Dawidek 		} else {
314518dd4c0SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
315518dd4c0SPawel Jakub Dawidek 			    "Unable to flush disk cache on activemap update");
316518dd4c0SPawel Jakub Dawidek 			return (-1);
317518dd4c0SPawel Jakub Dawidek 		}
318518dd4c0SPawel Jakub Dawidek 	}
31932115b10SPawel Jakub Dawidek 	return (0);
32032115b10SPawel Jakub Dawidek }
32132115b10SPawel Jakub Dawidek 
322f377917cSPawel Jakub Dawidek static bool
323f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res)
324f377917cSPawel Jakub Dawidek {
325f377917cSPawel Jakub Dawidek 
326f377917cSPawel Jakub Dawidek 	return (strcmp(res->hr_remoteaddr, "none") != 0);
327f377917cSPawel Jakub Dawidek }
328f377917cSPawel Jakub Dawidek 
32932115b10SPawel Jakub Dawidek static void
33032115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused)
33132115b10SPawel Jakub Dawidek {
33232115b10SPawel Jakub Dawidek 	struct hio *hio;
33332115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
33432115b10SPawel Jakub Dawidek 
33532115b10SPawel Jakub Dawidek 	/*
33632115b10SPawel Jakub Dawidek 	 * In the future it might be per-resource value.
33732115b10SPawel Jakub Dawidek 	 */
33832115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
33932115b10SPawel Jakub Dawidek 
34032115b10SPawel Jakub Dawidek 	/*
34132115b10SPawel Jakub Dawidek 	 * Allocate memory needed by lists.
34232115b10SPawel Jakub Dawidek 	 */
34332115b10SPawel Jakub Dawidek 	hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
34432115b10SPawel Jakub Dawidek 	if (hio_send_list == NULL) {
34532115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
34632115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send lists.",
34732115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list[0]) * ncomps);
34832115b10SPawel Jakub Dawidek 	}
34932115b10SPawel Jakub Dawidek 	hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
35032115b10SPawel Jakub Dawidek 	if (hio_send_list_lock == NULL) {
35132115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35232115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list locks.",
35332115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_lock[0]) * ncomps);
35432115b10SPawel Jakub Dawidek 	}
35532115b10SPawel Jakub Dawidek 	hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
35632115b10SPawel Jakub Dawidek 	if (hio_send_list_cond == NULL) {
35732115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
35832115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for send list condition variables.",
35932115b10SPawel Jakub Dawidek 		    sizeof(hio_send_list_cond[0]) * ncomps);
36032115b10SPawel Jakub Dawidek 	}
36132115b10SPawel Jakub Dawidek 	hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
36232115b10SPawel Jakub Dawidek 	if (hio_recv_list == NULL) {
36332115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
36432115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv lists.",
36532115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list[0]) * ncomps);
36632115b10SPawel Jakub Dawidek 	}
36732115b10SPawel Jakub Dawidek 	hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
36832115b10SPawel Jakub Dawidek 	if (hio_recv_list_lock == NULL) {
36932115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
37032115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list locks.",
37132115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_lock[0]) * ncomps);
37232115b10SPawel Jakub Dawidek 	}
37332115b10SPawel Jakub Dawidek 	hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
37432115b10SPawel Jakub Dawidek 	if (hio_recv_list_cond == NULL) {
37532115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
37632115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for recv list condition variables.",
37732115b10SPawel Jakub Dawidek 		    sizeof(hio_recv_list_cond[0]) * ncomps);
37832115b10SPawel Jakub Dawidek 	}
37932115b10SPawel Jakub Dawidek 	hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
38032115b10SPawel Jakub Dawidek 	if (hio_remote_lock == NULL) {
38132115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
38232115b10SPawel Jakub Dawidek 		    "Unable to allocate %zu bytes of memory for remote connections locks.",
38332115b10SPawel Jakub Dawidek 		    sizeof(hio_remote_lock[0]) * ncomps);
38432115b10SPawel Jakub Dawidek 	}
38532115b10SPawel Jakub Dawidek 
38632115b10SPawel Jakub Dawidek 	/*
38732115b10SPawel Jakub Dawidek 	 * Initialize lists, their locks and theirs condition variables.
38832115b10SPawel Jakub Dawidek 	 */
38932115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_free_list);
39032115b10SPawel Jakub Dawidek 	mtx_init(&hio_free_list_lock);
39132115b10SPawel Jakub Dawidek 	cv_init(&hio_free_list_cond);
39232115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
39332115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_send_list[ii]);
39432115b10SPawel Jakub Dawidek 		mtx_init(&hio_send_list_lock[ii]);
39532115b10SPawel Jakub Dawidek 		cv_init(&hio_send_list_cond[ii]);
39632115b10SPawel Jakub Dawidek 		TAILQ_INIT(&hio_recv_list[ii]);
39732115b10SPawel Jakub Dawidek 		mtx_init(&hio_recv_list_lock[ii]);
39832115b10SPawel Jakub Dawidek 		cv_init(&hio_recv_list_cond[ii]);
39932115b10SPawel Jakub Dawidek 		rw_init(&hio_remote_lock[ii]);
40032115b10SPawel Jakub Dawidek 	}
40132115b10SPawel Jakub Dawidek 	TAILQ_INIT(&hio_done_list);
40232115b10SPawel Jakub Dawidek 	mtx_init(&hio_done_list_lock);
40332115b10SPawel Jakub Dawidek 	cv_init(&hio_done_list_cond);
40432115b10SPawel Jakub Dawidek 	mtx_init(&metadata_lock);
40532115b10SPawel Jakub Dawidek 
40632115b10SPawel Jakub Dawidek 	/*
40732115b10SPawel Jakub Dawidek 	 * Allocate requests pool and initialize requests.
40832115b10SPawel Jakub Dawidek 	 */
40932115b10SPawel Jakub Dawidek 	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
41032115b10SPawel Jakub Dawidek 		hio = malloc(sizeof(*hio));
41132115b10SPawel Jakub Dawidek 		if (hio == NULL) {
41232115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
41332115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for hio request.",
41432115b10SPawel Jakub Dawidek 			    sizeof(*hio));
41532115b10SPawel Jakub Dawidek 		}
41632115b10SPawel Jakub Dawidek 		hio->hio_countdown = 0;
41732115b10SPawel Jakub Dawidek 		hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
41832115b10SPawel Jakub Dawidek 		if (hio->hio_errors == NULL) {
41932115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
42032115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio errors.",
42132115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_errors[0]) * ncomps);
42232115b10SPawel Jakub Dawidek 		}
42332115b10SPawel Jakub Dawidek 		hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
42432115b10SPawel Jakub Dawidek 		if (hio->hio_next == NULL) {
42532115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
42632115b10SPawel Jakub Dawidek 			    "Unable allocate %zu bytes of memory for hio_next field.",
42732115b10SPawel Jakub Dawidek 			    sizeof(hio->hio_next[0]) * ncomps);
42832115b10SPawel Jakub Dawidek 		}
42932115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_version = G_GATE_VERSION;
43032115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_data = malloc(MAXPHYS);
43132115b10SPawel Jakub Dawidek 		if (hio->hio_ggio.gctl_data == NULL) {
43232115b10SPawel Jakub Dawidek 			primary_exitx(EX_TEMPFAIL,
43332115b10SPawel Jakub Dawidek 			    "Unable to allocate %zu bytes of memory for gctl_data.",
43432115b10SPawel Jakub Dawidek 			    MAXPHYS);
43532115b10SPawel Jakub Dawidek 		}
43632115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_length = MAXPHYS;
43732115b10SPawel Jakub Dawidek 		hio->hio_ggio.gctl_error = 0;
43832115b10SPawel Jakub Dawidek 		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
43932115b10SPawel Jakub Dawidek 	}
44032115b10SPawel Jakub Dawidek }
44132115b10SPawel Jakub Dawidek 
442ce837469SPawel Jakub Dawidek static bool
443ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res)
444ce837469SPawel Jakub Dawidek {
445ce837469SPawel Jakub Dawidek 
446ce837469SPawel Jakub Dawidek 	mtx_lock(&metadata_lock);
447ce837469SPawel Jakub Dawidek 	if (res->hr_resuid != 0) {
448ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
449ce837469SPawel Jakub Dawidek 		return (false);
450ce837469SPawel Jakub Dawidek 	} else {
451ce837469SPawel Jakub Dawidek 		/* Initialize unique resource identifier. */
452ce837469SPawel Jakub Dawidek 		arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
453ce837469SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
454ce837469SPawel Jakub Dawidek 		if (metadata_write(res) < 0)
455ce837469SPawel Jakub Dawidek 			exit(EX_NOINPUT);
456ce837469SPawel Jakub Dawidek 		return (true);
457ce837469SPawel Jakub Dawidek 	}
458ce837469SPawel Jakub Dawidek }
459ce837469SPawel Jakub Dawidek 
46032115b10SPawel Jakub Dawidek static void
46132115b10SPawel Jakub Dawidek init_local(struct hast_resource *res)
46232115b10SPawel Jakub Dawidek {
46332115b10SPawel Jakub Dawidek 	unsigned char *buf;
46432115b10SPawel Jakub Dawidek 	size_t mapsize;
46532115b10SPawel Jakub Dawidek 
46632115b10SPawel Jakub Dawidek 	if (metadata_read(res, true) < 0)
46732115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
46832115b10SPawel Jakub Dawidek 	mtx_init(&res->hr_amp_lock);
46932115b10SPawel Jakub Dawidek 	if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
47032115b10SPawel Jakub Dawidek 	    res->hr_local_sectorsize, res->hr_keepdirty) < 0) {
47132115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create activemap");
47232115b10SPawel Jakub Dawidek 	}
47332115b10SPawel Jakub Dawidek 	mtx_init(&range_lock);
47432115b10SPawel Jakub Dawidek 	cv_init(&range_regular_cond);
47532115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_regular) < 0)
47632115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
47732115b10SPawel Jakub Dawidek 	cv_init(&range_sync_cond);
47832115b10SPawel Jakub Dawidek 	if (rangelock_init(&range_sync) < 0)
47932115b10SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
48032115b10SPawel Jakub Dawidek 	mapsize = activemap_ondisk_size(res->hr_amp);
48132115b10SPawel Jakub Dawidek 	buf = calloc(1, mapsize);
48232115b10SPawel Jakub Dawidek 	if (buf == NULL) {
48332115b10SPawel Jakub Dawidek 		primary_exitx(EX_TEMPFAIL,
48432115b10SPawel Jakub Dawidek 		    "Unable to allocate buffer for activemap.");
48532115b10SPawel Jakub Dawidek 	}
48632115b10SPawel Jakub Dawidek 	if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
48732115b10SPawel Jakub Dawidek 	    (ssize_t)mapsize) {
48832115b10SPawel Jakub Dawidek 		primary_exit(EX_NOINPUT, "Unable to read activemap");
48932115b10SPawel Jakub Dawidek 	}
49032115b10SPawel Jakub Dawidek 	activemap_copyin(res->hr_amp, buf, mapsize);
491b0dfbe5bSPawel Jakub Dawidek 	free(buf);
49232115b10SPawel Jakub Dawidek 	if (res->hr_resuid != 0)
49332115b10SPawel Jakub Dawidek 		return;
49432115b10SPawel Jakub Dawidek 	/*
495ce837469SPawel Jakub Dawidek 	 * We're using provider for the first time. Initialize local and remote
496ce837469SPawel Jakub Dawidek 	 * counters. We don't initialize resuid here, as we want to do it just
497ce837469SPawel Jakub Dawidek 	 * in time. The reason for this is that we want to inform secondary
498ce837469SPawel Jakub Dawidek 	 * that there were no writes yet, so there is no need to synchronize
499ce837469SPawel Jakub Dawidek 	 * anything.
50032115b10SPawel Jakub Dawidek 	 */
5019446b453SPawel Jakub Dawidek 	res->hr_primary_localcnt = 0;
50232115b10SPawel Jakub Dawidek 	res->hr_primary_remotecnt = 0;
50332115b10SPawel Jakub Dawidek 	if (metadata_write(res) < 0)
50432115b10SPawel Jakub Dawidek 		exit(EX_NOINPUT);
50532115b10SPawel Jakub Dawidek }
50632115b10SPawel Jakub Dawidek 
50732ecf620SPawel Jakub Dawidek static int
50832ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp)
50932ecf620SPawel Jakub Dawidek {
51032ecf620SPawel Jakub Dawidek 	struct proto_conn *conn;
51132ecf620SPawel Jakub Dawidek 	int16_t val;
51232ecf620SPawel Jakub Dawidek 
51332ecf620SPawel Jakub Dawidek 	val = 1;
51432ecf620SPawel Jakub Dawidek 	if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
51532ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
51632ecf620SPawel Jakub Dawidek 		    "Unable to send connection request to parent");
51732ecf620SPawel Jakub Dawidek 	}
51832ecf620SPawel Jakub Dawidek 	if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
51932ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
52032ecf620SPawel Jakub Dawidek 		    "Unable to receive reply to connection request from parent");
52132ecf620SPawel Jakub Dawidek 	}
52232ecf620SPawel Jakub Dawidek 	if (val != 0) {
52332ecf620SPawel Jakub Dawidek 		errno = val;
52432ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
52532ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
52632ecf620SPawel Jakub Dawidek 		return (-1);
52732ecf620SPawel Jakub Dawidek 	}
52832ecf620SPawel Jakub Dawidek 	if (proto_connection_recv(res->hr_conn, true, &conn) < 0) {
52932ecf620SPawel Jakub Dawidek 		primary_exit(EX_TEMPFAIL,
53032ecf620SPawel Jakub Dawidek 		    "Unable to receive connection from parent");
53132ecf620SPawel Jakub Dawidek 	}
5327d4df5cdSPawel Jakub Dawidek 	if (proto_connect_wait(conn, res->hr_timeout) < 0) {
53332ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
53432ecf620SPawel Jakub Dawidek 		    res->hr_remoteaddr);
53532ecf620SPawel Jakub Dawidek 		proto_close(conn);
53632ecf620SPawel Jakub Dawidek 		return (-1);
53732ecf620SPawel Jakub Dawidek 	}
53832ecf620SPawel Jakub Dawidek 	/* Error in setting timeout is not critical, but why should it fail? */
53932ecf620SPawel Jakub Dawidek 	if (proto_timeout(conn, res->hr_timeout) < 0)
54032ecf620SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
54132ecf620SPawel Jakub Dawidek 
54232ecf620SPawel Jakub Dawidek 	*connp = conn;
54332ecf620SPawel Jakub Dawidek 
54432ecf620SPawel Jakub Dawidek 	return (0);
54532ecf620SPawel Jakub Dawidek }
54632ecf620SPawel Jakub Dawidek 
547ac0401e3SPawel Jakub Dawidek static int
5480d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp,
5490d9014f3SPawel Jakub Dawidek     struct proto_conn **outp)
55032115b10SPawel Jakub Dawidek {
5510d9014f3SPawel Jakub Dawidek 	struct proto_conn *in, *out;
55232115b10SPawel Jakub Dawidek 	struct nv *nvout, *nvin;
55332115b10SPawel Jakub Dawidek 	const unsigned char *token;
55432115b10SPawel Jakub Dawidek 	unsigned char *map;
55532115b10SPawel Jakub Dawidek 	const char *errmsg;
55632115b10SPawel Jakub Dawidek 	int32_t extentsize;
55732115b10SPawel Jakub Dawidek 	int64_t datasize;
55832115b10SPawel Jakub Dawidek 	uint32_t mapsize;
55932115b10SPawel Jakub Dawidek 	size_t size;
560ac0401e3SPawel Jakub Dawidek 	int error;
56132115b10SPawel Jakub Dawidek 
5622ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
5632ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(real_remote(res));
5640d9014f3SPawel Jakub Dawidek 
5650d9014f3SPawel Jakub Dawidek 	in = out = NULL;
5662be8fd75SPawel Jakub Dawidek 	errmsg = NULL;
5670d9014f3SPawel Jakub Dawidek 
56832ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &out) == -1)
569ac0401e3SPawel Jakub Dawidek 		return (ECONNREFUSED);
570ac0401e3SPawel Jakub Dawidek 
571ac0401e3SPawel Jakub Dawidek 	error = ECONNABORTED;
57232ecf620SPawel Jakub Dawidek 
57332115b10SPawel Jakub Dawidek 	/*
57432115b10SPawel Jakub Dawidek 	 * First handshake step.
57532115b10SPawel Jakub Dawidek 	 * Setup outgoing connection with remote node.
57632115b10SPawel Jakub Dawidek 	 */
57732115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
57832115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
57932115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
58032115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
58132115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
58232115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
58332115b10SPawel Jakub Dawidek 		nv_free(nvout);
58432115b10SPawel Jakub Dawidek 		goto close;
58532115b10SPawel Jakub Dawidek 	}
5860d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, out, nvout, NULL, 0) < 0) {
58732115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
58832115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
58932115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
59032115b10SPawel Jakub Dawidek 		nv_free(nvout);
59132115b10SPawel Jakub Dawidek 		goto close;
59232115b10SPawel Jakub Dawidek 	}
59332115b10SPawel Jakub Dawidek 	nv_free(nvout);
5940d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
59532115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
59632115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
59732115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
59832115b10SPawel Jakub Dawidek 		goto close;
59932115b10SPawel Jakub Dawidek 	}
60032115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
60132115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
60232115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
603ac0401e3SPawel Jakub Dawidek 		if (nv_exists(nvin, "wait"))
604ac0401e3SPawel Jakub Dawidek 			error = EBUSY;
60532115b10SPawel Jakub Dawidek 		nv_free(nvin);
60632115b10SPawel Jakub Dawidek 		goto close;
60732115b10SPawel Jakub Dawidek 	}
60832115b10SPawel Jakub Dawidek 	token = nv_get_uint8_array(nvin, &size, "token");
60932115b10SPawel Jakub Dawidek 	if (token == NULL) {
61032115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s has no 'token' field.",
61132115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
61232115b10SPawel Jakub Dawidek 		nv_free(nvin);
61332115b10SPawel Jakub Dawidek 		goto close;
61432115b10SPawel Jakub Dawidek 	}
61532115b10SPawel Jakub Dawidek 	if (size != sizeof(res->hr_token)) {
61632115b10SPawel Jakub Dawidek 		pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
61732115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr, size, sizeof(res->hr_token));
61832115b10SPawel Jakub Dawidek 		nv_free(nvin);
61932115b10SPawel Jakub Dawidek 		goto close;
62032115b10SPawel Jakub Dawidek 	}
62132115b10SPawel Jakub Dawidek 	bcopy(token, res->hr_token, sizeof(res->hr_token));
62232115b10SPawel Jakub Dawidek 	nv_free(nvin);
62332115b10SPawel Jakub Dawidek 
62432115b10SPawel Jakub Dawidek 	/*
62532115b10SPawel Jakub Dawidek 	 * Second handshake step.
62632115b10SPawel Jakub Dawidek 	 * Setup incoming connection with remote node.
62732115b10SPawel Jakub Dawidek 	 */
62832ecf620SPawel Jakub Dawidek 	if (primary_connect(res, &in) == -1)
62932115b10SPawel Jakub Dawidek 		goto close;
63032ecf620SPawel Jakub Dawidek 
63132115b10SPawel Jakub Dawidek 	nvout = nv_alloc();
63232115b10SPawel Jakub Dawidek 	nv_add_string(nvout, res->hr_name, "resource");
63332115b10SPawel Jakub Dawidek 	nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
63432115b10SPawel Jakub Dawidek 	    "token");
635ce837469SPawel Jakub Dawidek 	if (res->hr_resuid == 0) {
636ce837469SPawel Jakub Dawidek 		/*
637ce837469SPawel Jakub Dawidek 		 * The resuid field was not yet initialized.
638ce837469SPawel Jakub Dawidek 		 * Because we do synchronization inside init_resuid(), it is
639ce837469SPawel Jakub Dawidek 		 * possible that someone already initialized it, the function
640ce837469SPawel Jakub Dawidek 		 * will return false then, but if we successfully initialized
641ce837469SPawel Jakub Dawidek 		 * it, we will get true. True means that there were no writes
642ce837469SPawel Jakub Dawidek 		 * to this resource yet and we want to inform secondary that
643ce837469SPawel Jakub Dawidek 		 * synchronization is not needed by sending "virgin" argument.
644ce837469SPawel Jakub Dawidek 		 */
645ce837469SPawel Jakub Dawidek 		if (init_resuid(res))
646ce837469SPawel Jakub Dawidek 			nv_add_int8(nvout, 1, "virgin");
647ce837469SPawel Jakub Dawidek 	}
64832115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_resuid, "resuid");
64932115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
65032115b10SPawel Jakub Dawidek 	nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
65132115b10SPawel Jakub Dawidek 	if (nv_error(nvout) != 0) {
65232115b10SPawel Jakub Dawidek 		pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
65332115b10SPawel Jakub Dawidek 		    "Unable to allocate header for connection with %s",
65432115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
65532115b10SPawel Jakub Dawidek 		nv_free(nvout);
65632115b10SPawel Jakub Dawidek 		goto close;
65732115b10SPawel Jakub Dawidek 	}
6580d9014f3SPawel Jakub Dawidek 	if (hast_proto_send(res, in, nvout, NULL, 0) < 0) {
65932115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
66032115b10SPawel Jakub Dawidek 		    "Unable to send handshake header to %s",
66132115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
66232115b10SPawel Jakub Dawidek 		nv_free(nvout);
66332115b10SPawel Jakub Dawidek 		goto close;
66432115b10SPawel Jakub Dawidek 	}
66532115b10SPawel Jakub Dawidek 	nv_free(nvout);
6660d9014f3SPawel Jakub Dawidek 	if (hast_proto_recv_hdr(out, &nvin) < 0) {
66732115b10SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING,
66832115b10SPawel Jakub Dawidek 		    "Unable to receive handshake header from %s",
66932115b10SPawel Jakub Dawidek 		    res->hr_remoteaddr);
67032115b10SPawel Jakub Dawidek 		goto close;
67132115b10SPawel Jakub Dawidek 	}
67232115b10SPawel Jakub Dawidek 	errmsg = nv_get_string(nvin, "errmsg");
67332115b10SPawel Jakub Dawidek 	if (errmsg != NULL) {
67432115b10SPawel Jakub Dawidek 		pjdlog_warning("%s", errmsg);
67532115b10SPawel Jakub Dawidek 		nv_free(nvin);
67632115b10SPawel Jakub Dawidek 		goto close;
67732115b10SPawel Jakub Dawidek 	}
67832115b10SPawel Jakub Dawidek 	datasize = nv_get_int64(nvin, "datasize");
67932115b10SPawel Jakub Dawidek 	if (datasize != res->hr_datasize) {
68032115b10SPawel Jakub Dawidek 		pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
68132115b10SPawel Jakub Dawidek 		    (intmax_t)res->hr_datasize, (intmax_t)datasize);
68232115b10SPawel Jakub Dawidek 		nv_free(nvin);
68332115b10SPawel Jakub Dawidek 		goto close;
68432115b10SPawel Jakub Dawidek 	}
68532115b10SPawel Jakub Dawidek 	extentsize = nv_get_int32(nvin, "extentsize");
68632115b10SPawel Jakub Dawidek 	if (extentsize != res->hr_extentsize) {
68732115b10SPawel Jakub Dawidek 		pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
68832115b10SPawel Jakub Dawidek 		    (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
68932115b10SPawel Jakub Dawidek 		nv_free(nvin);
69032115b10SPawel Jakub Dawidek 		goto close;
69132115b10SPawel Jakub Dawidek 	}
69232115b10SPawel Jakub Dawidek 	res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
69332115b10SPawel Jakub Dawidek 	res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
69432115b10SPawel Jakub Dawidek 	res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
69506cbf549SPawel Jakub Dawidek 	if (nv_exists(nvin, "virgin")) {
69606cbf549SPawel Jakub Dawidek 		/*
69706cbf549SPawel Jakub Dawidek 		 * Secondary was reinitialized, bump localcnt if it is 0 as
69806cbf549SPawel Jakub Dawidek 		 * only we have the data.
69906cbf549SPawel Jakub Dawidek 		 */
70006cbf549SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
70106cbf549SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
70206cbf549SPawel Jakub Dawidek 
70306cbf549SPawel Jakub Dawidek 		if (res->hr_primary_localcnt == 0) {
70406cbf549SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
70506cbf549SPawel Jakub Dawidek 
70606cbf549SPawel Jakub Dawidek 			mtx_lock(&metadata_lock);
70706cbf549SPawel Jakub Dawidek 			res->hr_primary_localcnt++;
70806cbf549SPawel Jakub Dawidek 			pjdlog_debug(1, "Increasing localcnt to %ju.",
70906cbf549SPawel Jakub Dawidek 			    (uintmax_t)res->hr_primary_localcnt);
71006cbf549SPawel Jakub Dawidek 			(void)metadata_write(res);
71106cbf549SPawel Jakub Dawidek 			mtx_unlock(&metadata_lock);
71206cbf549SPawel Jakub Dawidek 		}
71306cbf549SPawel Jakub Dawidek 	}
71432115b10SPawel Jakub Dawidek 	map = NULL;
71532115b10SPawel Jakub Dawidek 	mapsize = nv_get_uint32(nvin, "mapsize");
71632115b10SPawel Jakub Dawidek 	if (mapsize > 0) {
71732115b10SPawel Jakub Dawidek 		map = malloc(mapsize);
71832115b10SPawel Jakub Dawidek 		if (map == NULL) {
71932115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
72032115b10SPawel Jakub Dawidek 			    (uintmax_t)mapsize);
72132115b10SPawel Jakub Dawidek 			nv_free(nvin);
72232115b10SPawel Jakub Dawidek 			goto close;
72332115b10SPawel Jakub Dawidek 		}
72432115b10SPawel Jakub Dawidek 		/*
72532115b10SPawel Jakub Dawidek 		 * Remote node have some dirty extents on its own, lets
72632115b10SPawel Jakub Dawidek 		 * download its activemap.
72732115b10SPawel Jakub Dawidek 		 */
7280d9014f3SPawel Jakub Dawidek 		if (hast_proto_recv_data(res, out, nvin, map,
72932115b10SPawel Jakub Dawidek 		    mapsize) < 0) {
73032115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
73132115b10SPawel Jakub Dawidek 			    "Unable to receive remote activemap");
73232115b10SPawel Jakub Dawidek 			nv_free(nvin);
73332115b10SPawel Jakub Dawidek 			free(map);
73432115b10SPawel Jakub Dawidek 			goto close;
73532115b10SPawel Jakub Dawidek 		}
73632115b10SPawel Jakub Dawidek 		/*
73732115b10SPawel Jakub Dawidek 		 * Merge local and remote bitmaps.
73832115b10SPawel Jakub Dawidek 		 */
73932115b10SPawel Jakub Dawidek 		activemap_merge(res->hr_amp, map, mapsize);
74032115b10SPawel Jakub Dawidek 		free(map);
74132115b10SPawel Jakub Dawidek 		/*
74232115b10SPawel Jakub Dawidek 		 * Now that we merged bitmaps from both nodes, flush it to the
74332115b10SPawel Jakub Dawidek 		 * disk before we start to synchronize.
74432115b10SPawel Jakub Dawidek 		 */
74532115b10SPawel Jakub Dawidek 		(void)hast_activemap_flush(res);
74632115b10SPawel Jakub Dawidek 	}
747584a9bc3SPawel Jakub Dawidek 	nv_free(nvin);
748ba2a8224SMikolaj Golub #ifdef notyet
74902dfe972SPawel Jakub Dawidek 	/* Setup directions. */
75002dfe972SPawel Jakub Dawidek 	if (proto_send(out, NULL, 0) == -1)
75102dfe972SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
75202dfe972SPawel Jakub Dawidek 	if (proto_recv(in, NULL, 0) == -1)
75302dfe972SPawel Jakub Dawidek 		pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
754ba2a8224SMikolaj Golub #endif
75532115b10SPawel Jakub Dawidek 	pjdlog_info("Connected to %s.", res->hr_remoteaddr);
7560d9014f3SPawel Jakub Dawidek 	if (inp != NULL && outp != NULL) {
7570d9014f3SPawel Jakub Dawidek 		*inp = in;
7580d9014f3SPawel Jakub Dawidek 		*outp = out;
7590d9014f3SPawel Jakub Dawidek 	} else {
7600d9014f3SPawel Jakub Dawidek 		res->hr_remotein = in;
7610d9014f3SPawel Jakub Dawidek 		res->hr_remoteout = out;
7620d9014f3SPawel Jakub Dawidek 	}
7635bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_CONNECT);
764ac0401e3SPawel Jakub Dawidek 	return (0);
7650d9014f3SPawel Jakub Dawidek close:
7662be8fd75SPawel Jakub Dawidek 	if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
7675bdff860SPawel Jakub Dawidek 		event_send(res, EVENT_SPLITBRAIN);
7680d9014f3SPawel Jakub Dawidek 	proto_close(out);
7690d9014f3SPawel Jakub Dawidek 	if (in != NULL)
7700d9014f3SPawel Jakub Dawidek 		proto_close(in);
771ac0401e3SPawel Jakub Dawidek 	return (error);
7720d9014f3SPawel Jakub Dawidek }
7730d9014f3SPawel Jakub Dawidek 
7740d9014f3SPawel Jakub Dawidek static void
7750d9014f3SPawel Jakub Dawidek sync_start(void)
7760d9014f3SPawel Jakub Dawidek {
7770d9014f3SPawel Jakub Dawidek 
77832115b10SPawel Jakub Dawidek 	mtx_lock(&sync_lock);
77932115b10SPawel Jakub Dawidek 	sync_inprogress = true;
78032115b10SPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
78132115b10SPawel Jakub Dawidek 	cv_signal(&sync_cond);
78232115b10SPawel Jakub Dawidek }
78332115b10SPawel Jakub Dawidek 
78432115b10SPawel Jakub Dawidek static void
78555ce1e7cSPawel Jakub Dawidek sync_stop(void)
78655ce1e7cSPawel Jakub Dawidek {
78755ce1e7cSPawel Jakub Dawidek 
78855ce1e7cSPawel Jakub Dawidek 	mtx_lock(&sync_lock);
78955ce1e7cSPawel Jakub Dawidek 	if (sync_inprogress)
79055ce1e7cSPawel Jakub Dawidek 		sync_inprogress = false;
79155ce1e7cSPawel Jakub Dawidek 	mtx_unlock(&sync_lock);
79255ce1e7cSPawel Jakub Dawidek }
79355ce1e7cSPawel Jakub Dawidek 
79455ce1e7cSPawel Jakub Dawidek static void
79532115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res)
79632115b10SPawel Jakub Dawidek {
79732115b10SPawel Jakub Dawidek 	struct g_gate_ctl_create ggiocreate;
79832115b10SPawel Jakub Dawidek 	struct g_gate_ctl_cancel ggiocancel;
79932115b10SPawel Jakub Dawidek 
80032115b10SPawel Jakub Dawidek 	/*
80132115b10SPawel Jakub Dawidek 	 * We communicate with ggate via /dev/ggctl. Open it.
80232115b10SPawel Jakub Dawidek 	 */
80332115b10SPawel Jakub Dawidek 	res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
80432115b10SPawel Jakub Dawidek 	if (res->hr_ggatefd < 0)
80532115b10SPawel Jakub Dawidek 		primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
80632115b10SPawel Jakub Dawidek 	/*
80732115b10SPawel Jakub Dawidek 	 * Create provider before trying to connect, as connection failure
80832115b10SPawel Jakub Dawidek 	 * is not critical, but may take some time.
80932115b10SPawel Jakub Dawidek 	 */
8104e47b646SPawel Jakub Dawidek 	bzero(&ggiocreate, sizeof(ggiocreate));
81132115b10SPawel Jakub Dawidek 	ggiocreate.gctl_version = G_GATE_VERSION;
81232115b10SPawel Jakub Dawidek 	ggiocreate.gctl_mediasize = res->hr_datasize;
81332115b10SPawel Jakub Dawidek 	ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
81432115b10SPawel Jakub Dawidek 	ggiocreate.gctl_flags = 0;
8152a49afacSPawel Jakub Dawidek 	ggiocreate.gctl_maxcount = 0;
81632115b10SPawel Jakub Dawidek 	ggiocreate.gctl_timeout = 0;
81732115b10SPawel Jakub Dawidek 	ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
81832115b10SPawel Jakub Dawidek 	snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
81932115b10SPawel Jakub Dawidek 	    res->hr_provname);
82032115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
82132115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s created.", res->hr_provname);
82232115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocreate.gctl_unit;
82332115b10SPawel Jakub Dawidek 		return;
82432115b10SPawel Jakub Dawidek 	}
82532115b10SPawel Jakub Dawidek 	if (errno != EEXIST) {
82632115b10SPawel Jakub Dawidek 		primary_exit(EX_OSERR, "Unable to create hast/%s device",
82732115b10SPawel Jakub Dawidek 		    res->hr_provname);
82832115b10SPawel Jakub Dawidek 	}
82932115b10SPawel Jakub Dawidek 	pjdlog_debug(1,
83032115b10SPawel Jakub Dawidek 	    "Device hast/%s already exists, we will try to take it over.",
83132115b10SPawel Jakub Dawidek 	    res->hr_provname);
83232115b10SPawel Jakub Dawidek 	/*
83332115b10SPawel Jakub Dawidek 	 * If we received EEXIST, we assume that the process who created the
83432115b10SPawel Jakub Dawidek 	 * provider died and didn't clean up. In that case we will start from
83532115b10SPawel Jakub Dawidek 	 * where he left of.
83632115b10SPawel Jakub Dawidek 	 */
8374e47b646SPawel Jakub Dawidek 	bzero(&ggiocancel, sizeof(ggiocancel));
83832115b10SPawel Jakub Dawidek 	ggiocancel.gctl_version = G_GATE_VERSION;
83932115b10SPawel Jakub Dawidek 	ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
84032115b10SPawel Jakub Dawidek 	snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
84132115b10SPawel Jakub Dawidek 	    res->hr_provname);
84232115b10SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
84332115b10SPawel Jakub Dawidek 		pjdlog_info("Device hast/%s recovered.", res->hr_provname);
84432115b10SPawel Jakub Dawidek 		res->hr_ggateunit = ggiocancel.gctl_unit;
84532115b10SPawel Jakub Dawidek 		return;
84632115b10SPawel Jakub Dawidek 	}
84732115b10SPawel Jakub Dawidek 	primary_exit(EX_OSERR, "Unable to take over hast/%s device",
84832115b10SPawel Jakub Dawidek 	    res->hr_provname);
84932115b10SPawel Jakub Dawidek }
85032115b10SPawel Jakub Dawidek 
85132115b10SPawel Jakub Dawidek void
85232115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res)
85332115b10SPawel Jakub Dawidek {
85432115b10SPawel Jakub Dawidek 	pthread_t td;
85532115b10SPawel Jakub Dawidek 	pid_t pid;
856bc7a916aSMikolaj Golub 	int error, mode, debuglevel;
85732115b10SPawel Jakub Dawidek 
85832115b10SPawel Jakub Dawidek 	/*
85932ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending control commands from
86032ecf620SPawel Jakub Dawidek 	 * parent to child.
86132115b10SPawel Jakub Dawidek 	 */
8620b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
863d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
86432115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8656be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
86632115b10SPawel Jakub Dawidek 		    "Unable to create control sockets between parent and child");
86732115b10SPawel Jakub Dawidek 	}
8685bdff860SPawel Jakub Dawidek 	/*
86932ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending events from child to parent.
8705bdff860SPawel Jakub Dawidek 	 */
8710b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
872d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
8735bdff860SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8745bdff860SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
8755bdff860SPawel Jakub Dawidek 		    "Unable to create event sockets between child and parent");
8765bdff860SPawel Jakub Dawidek 	}
87732ecf620SPawel Jakub Dawidek 	/*
87832ecf620SPawel Jakub Dawidek 	 * Create communication channel for sending connection requests from
87932ecf620SPawel Jakub Dawidek 	 * child to parent.
88032ecf620SPawel Jakub Dawidek 	 */
8810b626a28SPawel Jakub Dawidek 	if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) {
88232ecf620SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
88332ecf620SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
88432ecf620SPawel Jakub Dawidek 		pjdlog_exit(EX_OSERR,
88532ecf620SPawel Jakub Dawidek 		    "Unable to create connection sockets between child and parent");
88632ecf620SPawel Jakub Dawidek 	}
88732115b10SPawel Jakub Dawidek 
88832115b10SPawel Jakub Dawidek 	pid = fork();
889*dfb1aeceSPawel Jakub Dawidek 	if (pid == -1) {
890d64c0992SPawel Jakub Dawidek 		/* TODO: There's no need for this to be fatal error. */
89132115b10SPawel Jakub Dawidek 		KEEP_ERRNO((void)pidfile_remove(pfh));
8926be3a25cSPawel Jakub Dawidek 		pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
89332115b10SPawel Jakub Dawidek 	}
89432115b10SPawel Jakub Dawidek 
89532115b10SPawel Jakub Dawidek 	if (pid > 0) {
89632115b10SPawel Jakub Dawidek 		/* This is parent. */
8975bdff860SPawel Jakub Dawidek 		/* Declare that we are receiver. */
8985bdff860SPawel Jakub Dawidek 		proto_recv(res->hr_event, NULL, 0);
89932ecf620SPawel Jakub Dawidek 		proto_recv(res->hr_conn, NULL, 0);
900da1783eaSPawel Jakub Dawidek 		/* Declare that we are sender. */
901da1783eaSPawel Jakub Dawidek 		proto_send(res->hr_ctrl, NULL, 0);
90232115b10SPawel Jakub Dawidek 		res->hr_workerpid = pid;
90332115b10SPawel Jakub Dawidek 		return;
90432115b10SPawel Jakub Dawidek 	}
905ecc99c89SPawel Jakub Dawidek 
9065b41e644SPawel Jakub Dawidek 	gres = res;
907da1783eaSPawel Jakub Dawidek 	mode = pjdlog_mode_get();
908bc7a916aSMikolaj Golub 	debuglevel = pjdlog_debug_get();
90932115b10SPawel Jakub Dawidek 
9105bdff860SPawel Jakub Dawidek 	/* Declare that we are sender. */
9115bdff860SPawel Jakub Dawidek 	proto_send(res->hr_event, NULL, 0);
91232ecf620SPawel Jakub Dawidek 	proto_send(res->hr_conn, NULL, 0);
913da1783eaSPawel Jakub Dawidek 	/* Declare that we are receiver. */
914da1783eaSPawel Jakub Dawidek 	proto_recv(res->hr_ctrl, NULL, 0);
915da1783eaSPawel Jakub Dawidek 	descriptors_cleanup(res);
916da1783eaSPawel Jakub Dawidek 
917f463896eSPawel Jakub Dawidek 	descriptors_assert(res, mode);
918f463896eSPawel Jakub Dawidek 
919da1783eaSPawel Jakub Dawidek 	pjdlog_init(mode);
920bc7a916aSMikolaj Golub 	pjdlog_debug_set(debuglevel);
921da1783eaSPawel Jakub Dawidek 	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
922643080b7SPawel Jakub Dawidek 	setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
9235bdff860SPawel Jakub Dawidek 
92432115b10SPawel Jakub Dawidek 	init_local(res);
92532115b10SPawel Jakub Dawidek 	init_ggate(res);
92632115b10SPawel Jakub Dawidek 	init_environment(res);
927115f4e5cSPawel Jakub Dawidek 
9280cddb12fSPawel Jakub Dawidek 	if (drop_privs(res) != 0) {
9296d7967deSPawel Jakub Dawidek 		cleanup(res);
9306d7967deSPawel Jakub Dawidek 		exit(EX_CONFIG);
9316d7967deSPawel Jakub Dawidek 	}
932f4c96f94SPawel Jakub Dawidek 	pjdlog_info("Privileges successfully dropped.");
9336d7967deSPawel Jakub Dawidek 
9348b70e6aeSPawel Jakub Dawidek 	/*
9354a88128bSPawel Jakub Dawidek 	 * Create the guard thread first, so we can handle signals from the
9364a88128bSPawel Jakub Dawidek 	 * very begining.
9374a88128bSPawel Jakub Dawidek 	 */
9384a88128bSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, guard_thread, res);
9392ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
9404a88128bSPawel Jakub Dawidek 	/*
9418b70e6aeSPawel Jakub Dawidek 	 * Create the control thread before sending any event to the parent,
9428b70e6aeSPawel Jakub Dawidek 	 * as we can deadlock when parent sends control request to worker,
9438b70e6aeSPawel Jakub Dawidek 	 * but worker has no control thread started yet, so parent waits.
9448b70e6aeSPawel Jakub Dawidek 	 * In the meantime worker sends an event to the parent, but parent
9458b70e6aeSPawel Jakub Dawidek 	 * is unable to handle the event, because it waits for control
9468b70e6aeSPawel Jakub Dawidek 	 * request response.
9478b70e6aeSPawel Jakub Dawidek 	 */
9488b70e6aeSPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ctrl_thread, res);
9492ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
950ac0401e3SPawel Jakub Dawidek 	if (real_remote(res)) {
951ac0401e3SPawel Jakub Dawidek 		error = init_remote(res, NULL, NULL);
952ac0401e3SPawel Jakub Dawidek 		if (error == 0) {
9538b70e6aeSPawel Jakub Dawidek 			sync_start();
954ac0401e3SPawel Jakub Dawidek 		} else if (error == EBUSY) {
955ac0401e3SPawel Jakub Dawidek 			time_t start = time(NULL);
956ac0401e3SPawel Jakub Dawidek 
957ac0401e3SPawel Jakub Dawidek 			pjdlog_warning("Waiting for remote node to become %s for %ds.",
958ac0401e3SPawel Jakub Dawidek 			    role2str(HAST_ROLE_SECONDARY),
959ac0401e3SPawel Jakub Dawidek 			    res->hr_timeout);
960ac0401e3SPawel Jakub Dawidek 			for (;;) {
961ac0401e3SPawel Jakub Dawidek 				sleep(1);
962ac0401e3SPawel Jakub Dawidek 				error = init_remote(res, NULL, NULL);
963ac0401e3SPawel Jakub Dawidek 				if (error != EBUSY)
964ac0401e3SPawel Jakub Dawidek 					break;
965ac0401e3SPawel Jakub Dawidek 				if (time(NULL) > start + res->hr_timeout)
966ac0401e3SPawel Jakub Dawidek 					break;
967ac0401e3SPawel Jakub Dawidek 			}
968ac0401e3SPawel Jakub Dawidek 			if (error == EBUSY) {
969ac0401e3SPawel Jakub Dawidek 				pjdlog_warning("Remote node is still %s, starting anyway.",
970ac0401e3SPawel Jakub Dawidek 				    role2str(HAST_ROLE_PRIMARY));
971ac0401e3SPawel Jakub Dawidek 			}
972ac0401e3SPawel Jakub Dawidek 		}
973ac0401e3SPawel Jakub Dawidek 	}
97432115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_recv_thread, res);
9752ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
97632115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, local_send_thread, res);
9772ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
97832115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_send_thread, res);
9792ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
98032115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, remote_recv_thread, res);
9812ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
98232115b10SPawel Jakub Dawidek 	error = pthread_create(&td, NULL, ggate_send_thread, res);
9832ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(error == 0);
984ac0401e3SPawel Jakub Dawidek 	fullystarted = true;
9854a88128bSPawel Jakub Dawidek 	(void)sync_thread(res);
98632115b10SPawel Jakub Dawidek }
98732115b10SPawel Jakub Dawidek 
98832115b10SPawel Jakub Dawidek static void
98932115b10SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...)
99032115b10SPawel Jakub Dawidek {
99132115b10SPawel Jakub Dawidek 	char msg[1024];
99232115b10SPawel Jakub Dawidek 	va_list ap;
99332115b10SPawel Jakub Dawidek 	int len;
99432115b10SPawel Jakub Dawidek 
99532115b10SPawel Jakub Dawidek 	va_start(ap, fmt);
99632115b10SPawel Jakub Dawidek 	len = vsnprintf(msg, sizeof(msg), fmt, ap);
99732115b10SPawel Jakub Dawidek 	va_end(ap);
99832115b10SPawel Jakub Dawidek 	if ((size_t)len < sizeof(msg)) {
99932115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
100032115b10SPawel Jakub Dawidek 		case BIO_READ:
100132115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
100232115b10SPawel Jakub Dawidek 			    "READ(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
100332115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
100432115b10SPawel Jakub Dawidek 			break;
100532115b10SPawel Jakub Dawidek 		case BIO_DELETE:
100632115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
100732115b10SPawel Jakub Dawidek 			    "DELETE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
100832115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
100932115b10SPawel Jakub Dawidek 			break;
101032115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
101132115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
101232115b10SPawel Jakub Dawidek 			break;
101332115b10SPawel Jakub Dawidek 		case BIO_WRITE:
101432115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
101532115b10SPawel Jakub Dawidek 			    "WRITE(%ju, %ju).", (uintmax_t)ggio->gctl_offset,
101632115b10SPawel Jakub Dawidek 			    (uintmax_t)ggio->gctl_length);
101732115b10SPawel Jakub Dawidek 			break;
101832115b10SPawel Jakub Dawidek 		default:
101932115b10SPawel Jakub Dawidek 			(void)snprintf(msg + len, sizeof(msg) - len,
102032115b10SPawel Jakub Dawidek 			    "UNKNOWN(%u).", (unsigned int)ggio->gctl_cmd);
102132115b10SPawel Jakub Dawidek 			break;
102232115b10SPawel Jakub Dawidek 		}
102332115b10SPawel Jakub Dawidek 	}
102432115b10SPawel Jakub Dawidek 	pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
102532115b10SPawel Jakub Dawidek }
102632115b10SPawel Jakub Dawidek 
102732115b10SPawel Jakub Dawidek static void
102832115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp)
102932115b10SPawel Jakub Dawidek {
103032115b10SPawel Jakub Dawidek 
103132115b10SPawel Jakub Dawidek 	rw_wlock(&hio_remote_lock[ncomp]);
103232115b10SPawel Jakub Dawidek 	/*
10335a58d22aSPawel Jakub Dawidek 	 * Check for a race between dropping rlock and acquiring wlock -
103432115b10SPawel Jakub Dawidek 	 * another thread can close connection in-between.
103532115b10SPawel Jakub Dawidek 	 */
103632115b10SPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
10372ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
10382ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
103932115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
104032115b10SPawel Jakub Dawidek 		return;
104132115b10SPawel Jakub Dawidek 	}
104232115b10SPawel Jakub Dawidek 
10432ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
10442ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
104532115b10SPawel Jakub Dawidek 
10468f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing incoming connection to %s.",
104732115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
104832115b10SPawel Jakub Dawidek 	proto_close(res->hr_remotein);
104932115b10SPawel Jakub Dawidek 	res->hr_remotein = NULL;
10508f8c798cSPawel Jakub Dawidek 	pjdlog_debug(2, "Closing outgoing connection to %s.",
105132115b10SPawel Jakub Dawidek 	    res->hr_remoteaddr);
105232115b10SPawel Jakub Dawidek 	proto_close(res->hr_remoteout);
105332115b10SPawel Jakub Dawidek 	res->hr_remoteout = NULL;
105432115b10SPawel Jakub Dawidek 
105532115b10SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
105632115b10SPawel Jakub Dawidek 
10578f8c798cSPawel Jakub Dawidek 	pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
10588f8c798cSPawel Jakub Dawidek 
105932115b10SPawel Jakub Dawidek 	/*
106032115b10SPawel Jakub Dawidek 	 * Stop synchronization if in-progress.
106132115b10SPawel Jakub Dawidek 	 */
106255ce1e7cSPawel Jakub Dawidek 	sync_stop();
10635b41e644SPawel Jakub Dawidek 
10645bdff860SPawel Jakub Dawidek 	event_send(res, EVENT_DISCONNECT);
1065f7fe83f9SPawel Jakub Dawidek }
106632115b10SPawel Jakub Dawidek 
106732115b10SPawel Jakub Dawidek /*
106807ebc362SPawel Jakub Dawidek  * Acknowledge write completion to the kernel, but don't update activemap yet.
106907ebc362SPawel Jakub Dawidek  */
107007ebc362SPawel Jakub Dawidek static void
107107ebc362SPawel Jakub Dawidek write_complete(struct hast_resource *res, struct hio *hio)
107207ebc362SPawel Jakub Dawidek {
107307ebc362SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
107407ebc362SPawel Jakub Dawidek 	unsigned int ncomp;
107507ebc362SPawel Jakub Dawidek 
107607ebc362SPawel Jakub Dawidek 	PJDLOG_ASSERT(!hio->hio_done);
107707ebc362SPawel Jakub Dawidek 
107807ebc362SPawel Jakub Dawidek 	ggio = &hio->hio_ggio;
107907ebc362SPawel Jakub Dawidek 	PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
108007ebc362SPawel Jakub Dawidek 
108107ebc362SPawel Jakub Dawidek 	/*
108207ebc362SPawel Jakub Dawidek 	 * Bump local count if this is first write after
108307ebc362SPawel Jakub Dawidek 	 * connection failure with remote node.
108407ebc362SPawel Jakub Dawidek 	 */
108507ebc362SPawel Jakub Dawidek 	ncomp = 1;
108607ebc362SPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
108707ebc362SPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
108807ebc362SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
108907ebc362SPawel Jakub Dawidek 		if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
109007ebc362SPawel Jakub Dawidek 			res->hr_primary_localcnt++;
109107ebc362SPawel Jakub Dawidek 			pjdlog_debug(1, "Increasing localcnt to %ju.",
109207ebc362SPawel Jakub Dawidek 			    (uintmax_t)res->hr_primary_localcnt);
109307ebc362SPawel Jakub Dawidek 			(void)metadata_write(res);
109407ebc362SPawel Jakub Dawidek 		}
109507ebc362SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
109607ebc362SPawel Jakub Dawidek 	}
109707ebc362SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
109807ebc362SPawel Jakub Dawidek 	if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0)
109907ebc362SPawel Jakub Dawidek 		primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
110007ebc362SPawel Jakub Dawidek 	hio->hio_done = true;
110107ebc362SPawel Jakub Dawidek }
110207ebc362SPawel Jakub Dawidek 
110307ebc362SPawel Jakub Dawidek /*
110432115b10SPawel Jakub Dawidek  * Thread receives ggate I/O requests from the kernel and passes them to
110532115b10SPawel Jakub Dawidek  * appropriate threads:
110632115b10SPawel Jakub Dawidek  * WRITE - always goes to both local_send and remote_send threads
110732115b10SPawel Jakub Dawidek  * READ (when the block is up-to-date on local component) -
110832115b10SPawel Jakub Dawidek  *	only local_send thread
110932115b10SPawel Jakub Dawidek  * READ (when the block isn't up-to-date on local component) -
111032115b10SPawel Jakub Dawidek  *	only remote_send thread
111132115b10SPawel Jakub Dawidek  * DELETE - always goes to both local_send and remote_send threads
111232115b10SPawel Jakub Dawidek  * FLUSH - always goes to both local_send and remote_send threads
111332115b10SPawel Jakub Dawidek  */
111432115b10SPawel Jakub Dawidek static void *
111532115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg)
111632115b10SPawel Jakub Dawidek {
111732115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
111832115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
111932115b10SPawel Jakub Dawidek 	struct hio *hio;
112032115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
112132115b10SPawel Jakub Dawidek 	int error;
112232115b10SPawel Jakub Dawidek 
112332115b10SPawel Jakub Dawidek 	for (;;) {
112432115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: Taking free request.");
112532115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
112632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
112732115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
112832115b10SPawel Jakub Dawidek 		ggio->gctl_unit = res->hr_ggateunit;
112932115b10SPawel Jakub Dawidek 		ggio->gctl_length = MAXPHYS;
113032115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
113107ebc362SPawel Jakub Dawidek 		hio->hio_done = false;
113207ebc362SPawel Jakub Dawidek 		hio->hio_replication = res->hr_replication;
113332115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
113432115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Waiting for request from the kernel.",
113532115b10SPawel Jakub Dawidek 		    hio);
113632115b10SPawel Jakub Dawidek 		if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) {
113732115b10SPawel Jakub Dawidek 			if (sigexit_received)
113832115b10SPawel Jakub Dawidek 				pthread_exit(NULL);
113932115b10SPawel Jakub Dawidek 			primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
114032115b10SPawel Jakub Dawidek 		}
114132115b10SPawel Jakub Dawidek 		error = ggio->gctl_error;
114232115b10SPawel Jakub Dawidek 		switch (error) {
114332115b10SPawel Jakub Dawidek 		case 0:
114432115b10SPawel Jakub Dawidek 			break;
114532115b10SPawel Jakub Dawidek 		case ECANCELED:
114632115b10SPawel Jakub Dawidek 			/* Exit gracefully. */
114732115b10SPawel Jakub Dawidek 			if (!sigexit_received) {
114832115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
114932115b10SPawel Jakub Dawidek 				    "ggate_recv: (%p) Received cancel from the kernel.",
115032115b10SPawel Jakub Dawidek 				    hio);
115132115b10SPawel Jakub Dawidek 				pjdlog_info("Received cancel from the kernel, exiting.");
115232115b10SPawel Jakub Dawidek 			}
115332115b10SPawel Jakub Dawidek 			pthread_exit(NULL);
115432115b10SPawel Jakub Dawidek 		case ENOMEM:
115532115b10SPawel Jakub Dawidek 			/*
115632115b10SPawel Jakub Dawidek 			 * Buffer too small? Impossible, we allocate MAXPHYS
115732115b10SPawel Jakub Dawidek 			 * bytes - request can't be bigger than that.
115832115b10SPawel Jakub Dawidek 			 */
115932115b10SPawel Jakub Dawidek 			/* FALLTHROUGH */
116032115b10SPawel Jakub Dawidek 		case ENXIO:
116132115b10SPawel Jakub Dawidek 		default:
116232115b10SPawel Jakub Dawidek 			primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
116332115b10SPawel Jakub Dawidek 			    strerror(error));
116432115b10SPawel Jakub Dawidek 		}
116507ebc362SPawel Jakub Dawidek 
116607ebc362SPawel Jakub Dawidek 		ncomp = 0;
116707ebc362SPawel Jakub Dawidek 		ncomps = HAST_NCOMPONENTS;
116807ebc362SPawel Jakub Dawidek 
116932115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
117032115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
117132115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio,
117232115b10SPawel Jakub Dawidek 		    "ggate_recv: (%p) Request received from the kernel: ",
117332115b10SPawel Jakub Dawidek 		    hio);
117407ebc362SPawel Jakub Dawidek 
117532115b10SPawel Jakub Dawidek 		/*
117632115b10SPawel Jakub Dawidek 		 * Inform all components about new write request.
117732115b10SPawel Jakub Dawidek 		 * For read request prefer local component unless the given
117832115b10SPawel Jakub Dawidek 		 * range is out-of-date, then use remote component.
117932115b10SPawel Jakub Dawidek 		 */
118032115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
118132115b10SPawel Jakub Dawidek 		case BIO_READ:
11823db86c39SPawel Jakub Dawidek 			res->hr_stat_read++;
118307ebc362SPawel Jakub Dawidek 			ncomps = 1;
118432115b10SPawel Jakub Dawidek 			mtx_lock(&metadata_lock);
118532115b10SPawel Jakub Dawidek 			if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
118632115b10SPawel Jakub Dawidek 			    res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
118732115b10SPawel Jakub Dawidek 				/*
118832115b10SPawel Jakub Dawidek 				 * This range is up-to-date on local component,
118932115b10SPawel Jakub Dawidek 				 * so handle request locally.
119032115b10SPawel Jakub Dawidek 				 */
119132115b10SPawel Jakub Dawidek 				 /* Local component is 0 for now. */
119232115b10SPawel Jakub Dawidek 				ncomp = 0;
119332115b10SPawel Jakub Dawidek 			} else /* if (res->hr_syncsrc ==
119432115b10SPawel Jakub Dawidek 			    HAST_SYNCSRC_SECONDARY) */ {
11952ec483c5SPawel Jakub Dawidek 				PJDLOG_ASSERT(res->hr_syncsrc ==
119632115b10SPawel Jakub Dawidek 				    HAST_SYNCSRC_SECONDARY);
119732115b10SPawel Jakub Dawidek 				/*
119832115b10SPawel Jakub Dawidek 				 * This range is out-of-date on local component,
119932115b10SPawel Jakub Dawidek 				 * so send request to the remote node.
120032115b10SPawel Jakub Dawidek 				 */
120132115b10SPawel Jakub Dawidek 				 /* Remote component is 1 for now. */
120232115b10SPawel Jakub Dawidek 				ncomp = 1;
120332115b10SPawel Jakub Dawidek 			}
120432115b10SPawel Jakub Dawidek 			mtx_unlock(&metadata_lock);
120532115b10SPawel Jakub Dawidek 			break;
120632115b10SPawel Jakub Dawidek 		case BIO_WRITE:
12073db86c39SPawel Jakub Dawidek 			res->hr_stat_write++;
12088a34134aSPawel Jakub Dawidek 			if (res->hr_resuid == 0 &&
12098a34134aSPawel Jakub Dawidek 			    res->hr_primary_localcnt == 0) {
12108a34134aSPawel Jakub Dawidek 				/* This is first write. */
12119446b453SPawel Jakub Dawidek 				res->hr_primary_localcnt = 1;
1212ce837469SPawel Jakub Dawidek 			}
121332115b10SPawel Jakub Dawidek 			for (;;) {
121432115b10SPawel Jakub Dawidek 				mtx_lock(&range_lock);
121532115b10SPawel Jakub Dawidek 				if (rangelock_islocked(range_sync,
121632115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length)) {
121732115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
121832115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu locked.",
121932115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
122032115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
122132115b10SPawel Jakub Dawidek 					range_regular_wait = true;
122232115b10SPawel Jakub Dawidek 					cv_wait(&range_regular_cond, &range_lock);
122332115b10SPawel Jakub Dawidek 					range_regular_wait = false;
122432115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
122532115b10SPawel Jakub Dawidek 					continue;
122632115b10SPawel Jakub Dawidek 				}
122732115b10SPawel Jakub Dawidek 				if (rangelock_add(range_regular,
122832115b10SPawel Jakub Dawidek 				    ggio->gctl_offset, ggio->gctl_length) < 0) {
122932115b10SPawel Jakub Dawidek 					mtx_unlock(&range_lock);
123032115b10SPawel Jakub Dawidek 					pjdlog_debug(2,
123132115b10SPawel Jakub Dawidek 					    "regular: Range offset=%jd length=%zu is already locked, waiting.",
123232115b10SPawel Jakub Dawidek 					    (intmax_t)ggio->gctl_offset,
123332115b10SPawel Jakub Dawidek 					    (size_t)ggio->gctl_length);
123432115b10SPawel Jakub Dawidek 					sleep(1);
123532115b10SPawel Jakub Dawidek 					continue;
123632115b10SPawel Jakub Dawidek 				}
123732115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
123832115b10SPawel Jakub Dawidek 				break;
123932115b10SPawel Jakub Dawidek 			}
124032115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
124132115b10SPawel Jakub Dawidek 			if (activemap_write_start(res->hr_amp,
124232115b10SPawel Jakub Dawidek 			    ggio->gctl_offset, ggio->gctl_length)) {
12433db86c39SPawel Jakub Dawidek 				res->hr_stat_activemap_update++;
124432115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
124532115b10SPawel Jakub Dawidek 			}
124632115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
124707ebc362SPawel Jakub Dawidek 			break;
12483db86c39SPawel Jakub Dawidek 		case BIO_DELETE:
12493db86c39SPawel Jakub Dawidek 			res->hr_stat_delete++;
12503db86c39SPawel Jakub Dawidek 			break;
12513db86c39SPawel Jakub Dawidek 		case BIO_FLUSH:
12523db86c39SPawel Jakub Dawidek 			res->hr_stat_flush++;
12533db86c39SPawel Jakub Dawidek 			break;
12543db86c39SPawel Jakub Dawidek 		}
125532115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
125607ebc362SPawel Jakub Dawidek 		    "ggate_recv: (%p) Moving request to the send queues.", hio);
125732115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, ncomps);
125807ebc362SPawel Jakub Dawidek 		for (ii = ncomp; ii < ncomps; ii++)
125932115b10SPawel Jakub Dawidek 			QUEUE_INSERT1(hio, send, ii);
126032115b10SPawel Jakub Dawidek 	}
126132115b10SPawel Jakub Dawidek 	/* NOTREACHED */
126232115b10SPawel Jakub Dawidek 	return (NULL);
126332115b10SPawel Jakub Dawidek }
126432115b10SPawel Jakub Dawidek 
126532115b10SPawel Jakub Dawidek /*
126632115b10SPawel Jakub Dawidek  * Thread reads from or writes to local component.
126732115b10SPawel Jakub Dawidek  * If local read fails, it redirects it to remote_send thread.
126832115b10SPawel Jakub Dawidek  */
126932115b10SPawel Jakub Dawidek static void *
127032115b10SPawel Jakub Dawidek local_send_thread(void *arg)
127132115b10SPawel Jakub Dawidek {
127232115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
127332115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
127432115b10SPawel Jakub Dawidek 	struct hio *hio;
127532115b10SPawel Jakub Dawidek 	unsigned int ncomp, rncomp;
127632115b10SPawel Jakub Dawidek 	ssize_t ret;
127732115b10SPawel Jakub Dawidek 
127832115b10SPawel Jakub Dawidek 	/* Local component is 0 for now. */
127932115b10SPawel Jakub Dawidek 	ncomp = 0;
128032115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
128132115b10SPawel Jakub Dawidek 	rncomp = 1;
128232115b10SPawel Jakub Dawidek 
128332115b10SPawel Jakub Dawidek 	for (;;) {
128432115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: Taking request.");
1285448efa94SPawel Jakub Dawidek 		QUEUE_TAKE1(hio, send, ncomp, 0);
128632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "local_send: (%p) Got request.", hio);
128732115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
128832115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
128932115b10SPawel Jakub Dawidek 		case BIO_READ:
129032115b10SPawel Jakub Dawidek 			ret = pread(res->hr_localfd, ggio->gctl_data,
129132115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
129232115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
129332115b10SPawel Jakub Dawidek 			if (ret == ggio->gctl_length)
129432115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1295a01a750fSMikolaj Golub 			else if (!ISSYNCREQ(hio)) {
129632115b10SPawel Jakub Dawidek 				/*
129732115b10SPawel Jakub Dawidek 				 * If READ failed, try to read from remote node.
129832115b10SPawel Jakub Dawidek 				 */
1299cd7b7ee5SPawel Jakub Dawidek 				if (ret < 0) {
1300cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1301cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%s), trying remote node. ",
1302cd7b7ee5SPawel Jakub Dawidek 					    strerror(errno));
1303cd7b7ee5SPawel Jakub Dawidek 				} else if (ret != ggio->gctl_length) {
1304cd7b7ee5SPawel Jakub Dawidek 					reqlog(LOG_WARNING, 0, ggio,
1305cd7b7ee5SPawel Jakub Dawidek 					    "Local request failed (%zd != %jd), trying remote node. ",
1306fba1bf5aSPawel Jakub Dawidek 					    ret, (intmax_t)ggio->gctl_length);
1307cd7b7ee5SPawel Jakub Dawidek 				}
130832115b10SPawel Jakub Dawidek 				QUEUE_INSERT1(hio, send, rncomp);
130932115b10SPawel Jakub Dawidek 				continue;
131032115b10SPawel Jakub Dawidek 			}
131132115b10SPawel Jakub Dawidek 			break;
131232115b10SPawel Jakub Dawidek 		case BIO_WRITE:
131332115b10SPawel Jakub Dawidek 			ret = pwrite(res->hr_localfd, ggio->gctl_data,
131432115b10SPawel Jakub Dawidek 			    ggio->gctl_length,
131532115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff);
1316cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
131732115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1318cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1319cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1320cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1321cd7b7ee5SPawel Jakub Dawidek 			} else if (ret != ggio->gctl_length) {
132232115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = EIO;
1323cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1324cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%zd != %jd): ",
1325fba1bf5aSPawel Jakub Dawidek 				    ret, (intmax_t)ggio->gctl_length);
1326cd7b7ee5SPawel Jakub Dawidek 			} else {
132732115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
132807ebc362SPawel Jakub Dawidek 				if (hio->hio_replication ==
132907ebc362SPawel Jakub Dawidek 				    HAST_REPLICATION_ASYNC) {
133007ebc362SPawel Jakub Dawidek 					ggio->gctl_error = 0;
133107ebc362SPawel Jakub Dawidek 					write_complete(res, hio);
133207ebc362SPawel Jakub Dawidek 				}
1333cd7b7ee5SPawel Jakub Dawidek 			}
133432115b10SPawel Jakub Dawidek 			break;
133532115b10SPawel Jakub Dawidek 		case BIO_DELETE:
133632115b10SPawel Jakub Dawidek 			ret = g_delete(res->hr_localfd,
133732115b10SPawel Jakub Dawidek 			    ggio->gctl_offset + res->hr_localoff,
133832115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
1339cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
134032115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1341cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1342cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1343cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1344cd7b7ee5SPawel Jakub Dawidek 			} else {
134532115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1346cd7b7ee5SPawel Jakub Dawidek 			}
134732115b10SPawel Jakub Dawidek 			break;
134832115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
134912daf727SPawel Jakub Dawidek 			if (!res->hr_localflush) {
135012daf727SPawel Jakub Dawidek 				ret = -1;
135112daf727SPawel Jakub Dawidek 				errno = EOPNOTSUPP;
135212daf727SPawel Jakub Dawidek 				break;
135312daf727SPawel Jakub Dawidek 			}
135432115b10SPawel Jakub Dawidek 			ret = g_flush(res->hr_localfd);
1355cd7b7ee5SPawel Jakub Dawidek 			if (ret < 0) {
135612daf727SPawel Jakub Dawidek 				if (errno == EOPNOTSUPP)
135712daf727SPawel Jakub Dawidek 					res->hr_localflush = false;
135832115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
1359cd7b7ee5SPawel Jakub Dawidek 				reqlog(LOG_WARNING, 0, ggio,
1360cd7b7ee5SPawel Jakub Dawidek 				    "Local request failed (%s): ",
1361cd7b7ee5SPawel Jakub Dawidek 				    strerror(errno));
1362cd7b7ee5SPawel Jakub Dawidek 			} else {
136332115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = 0;
1364cd7b7ee5SPawel Jakub Dawidek 			}
136532115b10SPawel Jakub Dawidek 			break;
136632115b10SPawel Jakub Dawidek 		}
136743b8675bSPawel Jakub Dawidek 		if (!refcount_release(&hio->hio_countdown))
136843b8675bSPawel Jakub Dawidek 			continue;
136932115b10SPawel Jakub Dawidek 		if (ISSYNCREQ(hio)) {
137032115b10SPawel Jakub Dawidek 			mtx_lock(&sync_lock);
137132115b10SPawel Jakub Dawidek 			SYNCREQDONE(hio);
137232115b10SPawel Jakub Dawidek 			mtx_unlock(&sync_lock);
137332115b10SPawel Jakub Dawidek 			cv_signal(&sync_cond);
137432115b10SPawel Jakub Dawidek 		} else {
137532115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
137632115b10SPawel Jakub Dawidek 			    "local_send: (%p) Moving request to the done queue.",
137732115b10SPawel Jakub Dawidek 			    hio);
137832115b10SPawel Jakub Dawidek 			QUEUE_INSERT2(hio, done);
137932115b10SPawel Jakub Dawidek 		}
138032115b10SPawel Jakub Dawidek 	}
138132115b10SPawel Jakub Dawidek 	/* NOTREACHED */
138232115b10SPawel Jakub Dawidek 	return (NULL);
138332115b10SPawel Jakub Dawidek }
138432115b10SPawel Jakub Dawidek 
1385448efa94SPawel Jakub Dawidek static void
1386448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp)
1387448efa94SPawel Jakub Dawidek {
1388448efa94SPawel Jakub Dawidek 	struct nv *nv;
1389448efa94SPawel Jakub Dawidek 
139021e7bc5eSPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
139121e7bc5eSPawel Jakub Dawidek 
139221e7bc5eSPawel Jakub Dawidek 	if (!ISCONNECTED(res, ncomp)) {
139321e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1394448efa94SPawel Jakub Dawidek 		return;
139521e7bc5eSPawel Jakub Dawidek 	}
1396448efa94SPawel Jakub Dawidek 
13972ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein != NULL);
13982ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout != NULL);
1399448efa94SPawel Jakub Dawidek 
1400448efa94SPawel Jakub Dawidek 	nv = nv_alloc();
1401448efa94SPawel Jakub Dawidek 	nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1402448efa94SPawel Jakub Dawidek 	if (nv_error(nv) != 0) {
140321e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1404448efa94SPawel Jakub Dawidek 		nv_free(nv);
1405448efa94SPawel Jakub Dawidek 		pjdlog_debug(1,
1406448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to prepare header to send.");
1407448efa94SPawel Jakub Dawidek 		return;
1408448efa94SPawel Jakub Dawidek 	}
1409448efa94SPawel Jakub Dawidek 	if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) {
141021e7bc5eSPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
1411448efa94SPawel Jakub Dawidek 		pjdlog_common(LOG_DEBUG, 1, errno,
1412448efa94SPawel Jakub Dawidek 		    "keepalive_send: Unable to send request");
1413448efa94SPawel Jakub Dawidek 		nv_free(nv);
1414448efa94SPawel Jakub Dawidek 		remote_close(res, ncomp);
1415448efa94SPawel Jakub Dawidek 		return;
1416448efa94SPawel Jakub Dawidek 	}
141721e7bc5eSPawel Jakub Dawidek 
141821e7bc5eSPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
1419448efa94SPawel Jakub Dawidek 	nv_free(nv);
1420448efa94SPawel Jakub Dawidek 	pjdlog_debug(2, "keepalive_send: Request sent.");
1421448efa94SPawel Jakub Dawidek }
1422448efa94SPawel Jakub Dawidek 
142332115b10SPawel Jakub Dawidek /*
142432115b10SPawel Jakub Dawidek  * Thread sends request to secondary node.
142532115b10SPawel Jakub Dawidek  */
142632115b10SPawel Jakub Dawidek static void *
142732115b10SPawel Jakub Dawidek remote_send_thread(void *arg)
142832115b10SPawel Jakub Dawidek {
142932115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
143032115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1431448efa94SPawel Jakub Dawidek 	time_t lastcheck, now;
143232115b10SPawel Jakub Dawidek 	struct hio *hio;
143332115b10SPawel Jakub Dawidek 	struct nv *nv;
143432115b10SPawel Jakub Dawidek 	unsigned int ncomp;
143532115b10SPawel Jakub Dawidek 	bool wakeup;
143632115b10SPawel Jakub Dawidek 	uint64_t offset, length;
143732115b10SPawel Jakub Dawidek 	uint8_t cmd;
143832115b10SPawel Jakub Dawidek 	void *data;
143932115b10SPawel Jakub Dawidek 
144032115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
144132115b10SPawel Jakub Dawidek 	ncomp = 1;
1442448efa94SPawel Jakub Dawidek 	lastcheck = time(NULL);
144332115b10SPawel Jakub Dawidek 
144432115b10SPawel Jakub Dawidek 	for (;;) {
144532115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: Taking request.");
14468d7dcf14SMikolaj Golub 		QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1447448efa94SPawel Jakub Dawidek 		if (hio == NULL) {
1448448efa94SPawel Jakub Dawidek 			now = time(NULL);
14498d7dcf14SMikolaj Golub 			if (lastcheck + HAST_KEEPALIVE <= now) {
1450448efa94SPawel Jakub Dawidek 				keepalive_send(res, ncomp);
1451448efa94SPawel Jakub Dawidek 				lastcheck = now;
1452448efa94SPawel Jakub Dawidek 			}
1453448efa94SPawel Jakub Dawidek 			continue;
1454448efa94SPawel Jakub Dawidek 		}
145532115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
145632115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
145732115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
145832115b10SPawel Jakub Dawidek 		case BIO_READ:
145932115b10SPawel Jakub Dawidek 			cmd = HIO_READ;
146032115b10SPawel Jakub Dawidek 			data = NULL;
146132115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
146232115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
146332115b10SPawel Jakub Dawidek 			break;
146432115b10SPawel Jakub Dawidek 		case BIO_WRITE:
146532115b10SPawel Jakub Dawidek 			cmd = HIO_WRITE;
146632115b10SPawel Jakub Dawidek 			data = ggio->gctl_data;
146732115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
146832115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
146932115b10SPawel Jakub Dawidek 			break;
147032115b10SPawel Jakub Dawidek 		case BIO_DELETE:
147132115b10SPawel Jakub Dawidek 			cmd = HIO_DELETE;
147232115b10SPawel Jakub Dawidek 			data = NULL;
147332115b10SPawel Jakub Dawidek 			offset = ggio->gctl_offset;
147432115b10SPawel Jakub Dawidek 			length = ggio->gctl_length;
147532115b10SPawel Jakub Dawidek 			break;
147632115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
147732115b10SPawel Jakub Dawidek 			cmd = HIO_FLUSH;
147832115b10SPawel Jakub Dawidek 			data = NULL;
147932115b10SPawel Jakub Dawidek 			offset = 0;
148032115b10SPawel Jakub Dawidek 			length = 0;
148132115b10SPawel Jakub Dawidek 			break;
148232115b10SPawel Jakub Dawidek 		default:
148309c2e843SPawel Jakub Dawidek 			PJDLOG_ABORT("invalid condition");
148432115b10SPawel Jakub Dawidek 		}
148532115b10SPawel Jakub Dawidek 		nv = nv_alloc();
148632115b10SPawel Jakub Dawidek 		nv_add_uint8(nv, cmd, "cmd");
148732115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
148832115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, offset, "offset");
148932115b10SPawel Jakub Dawidek 		nv_add_uint64(nv, length, "length");
149032115b10SPawel Jakub Dawidek 		if (nv_error(nv) != 0) {
149132115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = nv_error(nv);
149232115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
149332115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to prepare header to send.",
149432115b10SPawel Jakub Dawidek 			    hio);
149532115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
149632115b10SPawel Jakub Dawidek 			    "Unable to prepare header to send (%s): ",
149732115b10SPawel Jakub Dawidek 			    strerror(nv_error(nv)));
149832115b10SPawel Jakub Dawidek 			/* Move failed request immediately to the done queue. */
149932115b10SPawel Jakub Dawidek 			goto done_queue;
150032115b10SPawel Jakub Dawidek 		}
150132115b10SPawel Jakub Dawidek 		/*
150232115b10SPawel Jakub Dawidek 		 * Protect connection from disappearing.
150332115b10SPawel Jakub Dawidek 		 */
150432115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
150532115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
150632115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
150732115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = ENOTCONN;
150832115b10SPawel Jakub Dawidek 			goto done_queue;
150932115b10SPawel Jakub Dawidek 		}
151032115b10SPawel Jakub Dawidek 		/*
151132115b10SPawel Jakub Dawidek 		 * Move the request to recv queue before sending it, because
151232115b10SPawel Jakub Dawidek 		 * in different order we can get reply before we move request
151332115b10SPawel Jakub Dawidek 		 * to recv queue.
151432115b10SPawel Jakub Dawidek 		 */
15151212a85cSPawel Jakub Dawidek 		pjdlog_debug(2,
15161212a85cSPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the recv queue.",
15171212a85cSPawel Jakub Dawidek 		    hio);
151832115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
151932115b10SPawel Jakub Dawidek 		wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
152032115b10SPawel Jakub Dawidek 		TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
152132115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
152232115b10SPawel Jakub Dawidek 		if (hast_proto_send(res, res->hr_remoteout, nv, data,
152332115b10SPawel Jakub Dawidek 		    data != NULL ? length : 0) < 0) {
152432115b10SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = errno;
152532115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
152632115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
152732115b10SPawel Jakub Dawidek 			    "remote_send: (%p) Unable to send request.", hio);
152832115b10SPawel Jakub Dawidek 			reqlog(LOG_ERR, 0, ggio,
152932115b10SPawel Jakub Dawidek 			    "Unable to send request (%s): ",
153032115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
1531ee087cdfSPawel Jakub Dawidek 			remote_close(res, ncomp);
153232115b10SPawel Jakub Dawidek 			/*
153332115b10SPawel Jakub Dawidek 			 * Take request back from the receive queue and move
153432115b10SPawel Jakub Dawidek 			 * it immediately to the done queue.
153532115b10SPawel Jakub Dawidek 			 */
153632115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
15371212a85cSPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
15381212a85cSPawel Jakub Dawidek 			    hio_next[ncomp]);
153932115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
154032115b10SPawel Jakub Dawidek 			goto done_queue;
154132115b10SPawel Jakub Dawidek 		}
154232115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
154332115b10SPawel Jakub Dawidek 		nv_free(nv);
154432115b10SPawel Jakub Dawidek 		if (wakeup)
154532115b10SPawel Jakub Dawidek 			cv_signal(&hio_recv_list_cond[ncomp]);
154632115b10SPawel Jakub Dawidek 		continue;
154732115b10SPawel Jakub Dawidek done_queue:
154832115b10SPawel Jakub Dawidek 		nv_free(nv);
154932115b10SPawel Jakub Dawidek 		if (ISSYNCREQ(hio)) {
155032115b10SPawel Jakub Dawidek 			if (!refcount_release(&hio->hio_countdown))
155132115b10SPawel Jakub Dawidek 				continue;
155232115b10SPawel Jakub Dawidek 			mtx_lock(&sync_lock);
155332115b10SPawel Jakub Dawidek 			SYNCREQDONE(hio);
155432115b10SPawel Jakub Dawidek 			mtx_unlock(&sync_lock);
155532115b10SPawel Jakub Dawidek 			cv_signal(&sync_cond);
155632115b10SPawel Jakub Dawidek 			continue;
155732115b10SPawel Jakub Dawidek 		}
155832115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
155932115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
156032115b10SPawel Jakub Dawidek 			if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
156132115b10SPawel Jakub Dawidek 			    ggio->gctl_length)) {
156232115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
156332115b10SPawel Jakub Dawidek 			}
156432115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
156532115b10SPawel Jakub Dawidek 		}
156632115b10SPawel Jakub Dawidek 		if (!refcount_release(&hio->hio_countdown))
156732115b10SPawel Jakub Dawidek 			continue;
156832115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
156932115b10SPawel Jakub Dawidek 		    "remote_send: (%p) Moving request to the done queue.",
157032115b10SPawel Jakub Dawidek 		    hio);
157132115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, done);
157232115b10SPawel Jakub Dawidek 	}
157332115b10SPawel Jakub Dawidek 	/* NOTREACHED */
157432115b10SPawel Jakub Dawidek 	return (NULL);
157532115b10SPawel Jakub Dawidek }
157632115b10SPawel Jakub Dawidek 
157732115b10SPawel Jakub Dawidek /*
157832115b10SPawel Jakub Dawidek  * Thread receives answer from secondary node and passes it to ggate_send
157932115b10SPawel Jakub Dawidek  * thread.
158032115b10SPawel Jakub Dawidek  */
158132115b10SPawel Jakub Dawidek static void *
158232115b10SPawel Jakub Dawidek remote_recv_thread(void *arg)
158332115b10SPawel Jakub Dawidek {
158432115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
158532115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
158632115b10SPawel Jakub Dawidek 	struct hio *hio;
158732115b10SPawel Jakub Dawidek 	struct nv *nv;
158832115b10SPawel Jakub Dawidek 	unsigned int ncomp;
158932115b10SPawel Jakub Dawidek 	uint64_t seq;
159032115b10SPawel Jakub Dawidek 	int error;
159132115b10SPawel Jakub Dawidek 
159232115b10SPawel Jakub Dawidek 	/* Remote component is 1 for now. */
159332115b10SPawel Jakub Dawidek 	ncomp = 1;
159432115b10SPawel Jakub Dawidek 
159532115b10SPawel Jakub Dawidek 	for (;;) {
159632115b10SPawel Jakub Dawidek 		/* Wait until there is anything to receive. */
159732115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
159832115b10SPawel Jakub Dawidek 		while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
159932115b10SPawel Jakub Dawidek 			pjdlog_debug(2, "remote_recv: No requests, waiting.");
160032115b10SPawel Jakub Dawidek 			cv_wait(&hio_recv_list_cond[ncomp],
160132115b10SPawel Jakub Dawidek 			    &hio_recv_list_lock[ncomp]);
160232115b10SPawel Jakub Dawidek 		}
160332115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
16043f5bce18SPawel Jakub Dawidek 
160532115b10SPawel Jakub Dawidek 		rw_rlock(&hio_remote_lock[ncomp]);
160632115b10SPawel Jakub Dawidek 		if (!ISCONNECTED(res, ncomp)) {
160732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
160832115b10SPawel Jakub Dawidek 			/*
160932115b10SPawel Jakub Dawidek 			 * Connection is dead, so move all pending requests to
161032115b10SPawel Jakub Dawidek 			 * the done queue (one-by-one).
161132115b10SPawel Jakub Dawidek 			 */
161232115b10SPawel Jakub Dawidek 			mtx_lock(&hio_recv_list_lock[ncomp]);
161332115b10SPawel Jakub Dawidek 			hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
16142ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(hio != NULL);
161532115b10SPawel Jakub Dawidek 			TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
161632115b10SPawel Jakub Dawidek 			    hio_next[ncomp]);
161732115b10SPawel Jakub Dawidek 			mtx_unlock(&hio_recv_list_lock[ncomp]);
161832115b10SPawel Jakub Dawidek 			goto done_queue;
161932115b10SPawel Jakub Dawidek 		}
162032115b10SPawel Jakub Dawidek 		if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) {
162132115b10SPawel Jakub Dawidek 			pjdlog_errno(LOG_ERR,
162232115b10SPawel Jakub Dawidek 			    "Unable to receive reply header");
162332115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
162432115b10SPawel Jakub Dawidek 			remote_close(res, ncomp);
162532115b10SPawel Jakub Dawidek 			continue;
162632115b10SPawel Jakub Dawidek 		}
162732115b10SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
162832115b10SPawel Jakub Dawidek 		seq = nv_get_uint64(nv, "seq");
162932115b10SPawel Jakub Dawidek 		if (seq == 0) {
163032115b10SPawel Jakub Dawidek 			pjdlog_error("Header contains no 'seq' field.");
163132115b10SPawel Jakub Dawidek 			nv_free(nv);
163232115b10SPawel Jakub Dawidek 			continue;
163332115b10SPawel Jakub Dawidek 		}
163432115b10SPawel Jakub Dawidek 		mtx_lock(&hio_recv_list_lock[ncomp]);
163532115b10SPawel Jakub Dawidek 		TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
163632115b10SPawel Jakub Dawidek 			if (hio->hio_ggio.gctl_seq == seq) {
163732115b10SPawel Jakub Dawidek 				TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
163832115b10SPawel Jakub Dawidek 				    hio_next[ncomp]);
163932115b10SPawel Jakub Dawidek 				break;
164032115b10SPawel Jakub Dawidek 			}
164132115b10SPawel Jakub Dawidek 		}
164232115b10SPawel Jakub Dawidek 		mtx_unlock(&hio_recv_list_lock[ncomp]);
164332115b10SPawel Jakub Dawidek 		if (hio == NULL) {
164432115b10SPawel Jakub Dawidek 			pjdlog_error("Found no request matching received 'seq' field (%ju).",
164532115b10SPawel Jakub Dawidek 			    (uintmax_t)seq);
164632115b10SPawel Jakub Dawidek 			nv_free(nv);
164732115b10SPawel Jakub Dawidek 			continue;
164832115b10SPawel Jakub Dawidek 		}
16491212a85cSPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
165032115b10SPawel Jakub Dawidek 		error = nv_get_int16(nv, "error");
165132115b10SPawel Jakub Dawidek 		if (error != 0) {
165232115b10SPawel Jakub Dawidek 			/* Request failed on remote side. */
165372089204SPawel Jakub Dawidek 			hio->hio_errors[ncomp] = error;
16541212a85cSPawel Jakub Dawidek 			reqlog(LOG_WARNING, 0, ggio,
1655cd7b7ee5SPawel Jakub Dawidek 			    "Remote request failed (%s): ", strerror(error));
165632115b10SPawel Jakub Dawidek 			nv_free(nv);
165732115b10SPawel Jakub Dawidek 			goto done_queue;
165832115b10SPawel Jakub Dawidek 		}
165932115b10SPawel Jakub Dawidek 		switch (ggio->gctl_cmd) {
166032115b10SPawel Jakub Dawidek 		case BIO_READ:
166132115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
166232115b10SPawel Jakub Dawidek 			if (!ISCONNECTED(res, ncomp)) {
166332115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
166432115b10SPawel Jakub Dawidek 				nv_free(nv);
166532115b10SPawel Jakub Dawidek 				goto done_queue;
166632115b10SPawel Jakub Dawidek 			}
166732115b10SPawel Jakub Dawidek 			if (hast_proto_recv_data(res, res->hr_remotein, nv,
166832115b10SPawel Jakub Dawidek 			    ggio->gctl_data, ggio->gctl_length) < 0) {
166932115b10SPawel Jakub Dawidek 				hio->hio_errors[ncomp] = errno;
167032115b10SPawel Jakub Dawidek 				pjdlog_errno(LOG_ERR,
167132115b10SPawel Jakub Dawidek 				    "Unable to receive reply data");
167232115b10SPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ncomp]);
167332115b10SPawel Jakub Dawidek 				nv_free(nv);
167432115b10SPawel Jakub Dawidek 				remote_close(res, ncomp);
167532115b10SPawel Jakub Dawidek 				goto done_queue;
167632115b10SPawel Jakub Dawidek 			}
167732115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
167832115b10SPawel Jakub Dawidek 			break;
167932115b10SPawel Jakub Dawidek 		case BIO_WRITE:
168032115b10SPawel Jakub Dawidek 		case BIO_DELETE:
168132115b10SPawel Jakub Dawidek 		case BIO_FLUSH:
168232115b10SPawel Jakub Dawidek 			break;
168332115b10SPawel Jakub Dawidek 		default:
168409c2e843SPawel Jakub Dawidek 			PJDLOG_ABORT("invalid condition");
168532115b10SPawel Jakub Dawidek 		}
168632115b10SPawel Jakub Dawidek 		hio->hio_errors[ncomp] = 0;
168732115b10SPawel Jakub Dawidek 		nv_free(nv);
168832115b10SPawel Jakub Dawidek done_queue:
168943b8675bSPawel Jakub Dawidek 		if (!refcount_release(&hio->hio_countdown))
169043b8675bSPawel Jakub Dawidek 			continue;
169132115b10SPawel Jakub Dawidek 		if (ISSYNCREQ(hio)) {
169232115b10SPawel Jakub Dawidek 			mtx_lock(&sync_lock);
169332115b10SPawel Jakub Dawidek 			SYNCREQDONE(hio);
169432115b10SPawel Jakub Dawidek 			mtx_unlock(&sync_lock);
169532115b10SPawel Jakub Dawidek 			cv_signal(&sync_cond);
169632115b10SPawel Jakub Dawidek 		} else {
169732115b10SPawel Jakub Dawidek 			pjdlog_debug(2,
169832115b10SPawel Jakub Dawidek 			    "remote_recv: (%p) Moving request to the done queue.",
169932115b10SPawel Jakub Dawidek 			    hio);
170032115b10SPawel Jakub Dawidek 			QUEUE_INSERT2(hio, done);
170132115b10SPawel Jakub Dawidek 		}
170232115b10SPawel Jakub Dawidek 	}
170332115b10SPawel Jakub Dawidek 	/* NOTREACHED */
170432115b10SPawel Jakub Dawidek 	return (NULL);
170532115b10SPawel Jakub Dawidek }
170632115b10SPawel Jakub Dawidek 
170732115b10SPawel Jakub Dawidek /*
170832115b10SPawel Jakub Dawidek  * Thread sends answer to the kernel.
170932115b10SPawel Jakub Dawidek  */
171032115b10SPawel Jakub Dawidek static void *
171132115b10SPawel Jakub Dawidek ggate_send_thread(void *arg)
171232115b10SPawel Jakub Dawidek {
171332115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
171432115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
171532115b10SPawel Jakub Dawidek 	struct hio *hio;
171607ebc362SPawel Jakub Dawidek 	unsigned int ii, ncomps;
171732115b10SPawel Jakub Dawidek 
171832115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
171932115b10SPawel Jakub Dawidek 
172032115b10SPawel Jakub Dawidek 	for (;;) {
172132115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: Taking request.");
172232115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, done);
172332115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
172432115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
172532115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
172632115b10SPawel Jakub Dawidek 			if (hio->hio_errors[ii] == 0) {
172732115b10SPawel Jakub Dawidek 				/*
172832115b10SPawel Jakub Dawidek 				 * One successful request is enough to declare
172932115b10SPawel Jakub Dawidek 				 * success.
173032115b10SPawel Jakub Dawidek 				 */
173132115b10SPawel Jakub Dawidek 				ggio->gctl_error = 0;
173232115b10SPawel Jakub Dawidek 				break;
173332115b10SPawel Jakub Dawidek 			}
173432115b10SPawel Jakub Dawidek 		}
173532115b10SPawel Jakub Dawidek 		if (ii == ncomps) {
173632115b10SPawel Jakub Dawidek 			/*
173732115b10SPawel Jakub Dawidek 			 * None of the requests were successful.
1738b068d5aaSMikolaj Golub 			 * Use the error from local component except the
1739b068d5aaSMikolaj Golub 			 * case when we did only remote request.
174032115b10SPawel Jakub Dawidek 			 */
1741b068d5aaSMikolaj Golub 			if (ggio->gctl_cmd == BIO_READ &&
1742b068d5aaSMikolaj Golub 			    res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1743b068d5aaSMikolaj Golub 				ggio->gctl_error = hio->hio_errors[1];
1744b068d5aaSMikolaj Golub 			else
174532115b10SPawel Jakub Dawidek 				ggio->gctl_error = hio->hio_errors[0];
174632115b10SPawel Jakub Dawidek 		}
174732115b10SPawel Jakub Dawidek 		if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
174832115b10SPawel Jakub Dawidek 			mtx_lock(&res->hr_amp_lock);
1749d9f039e0SMikolaj Golub 			if (activemap_write_complete(res->hr_amp,
1750d9f039e0SMikolaj Golub 			    ggio->gctl_offset, ggio->gctl_length)) {
1751d9f039e0SMikolaj Golub 				res->hr_stat_activemap_update++;
1752d9f039e0SMikolaj Golub 				(void)hast_activemap_flush(res);
1753d9f039e0SMikolaj Golub 			}
175432115b10SPawel Jakub Dawidek 			mtx_unlock(&res->hr_amp_lock);
175532115b10SPawel Jakub Dawidek 		}
175632115b10SPawel Jakub Dawidek 		if (ggio->gctl_cmd == BIO_WRITE) {
175732115b10SPawel Jakub Dawidek 			/*
175832115b10SPawel Jakub Dawidek 			 * Unlock range we locked.
175932115b10SPawel Jakub Dawidek 			 */
176032115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
176132115b10SPawel Jakub Dawidek 			rangelock_del(range_regular, ggio->gctl_offset,
176232115b10SPawel Jakub Dawidek 			    ggio->gctl_length);
176332115b10SPawel Jakub Dawidek 			if (range_sync_wait)
176432115b10SPawel Jakub Dawidek 				cv_signal(&range_sync_cond);
176532115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
176607ebc362SPawel Jakub Dawidek 			if (!hio->hio_done)
176707ebc362SPawel Jakub Dawidek 				write_complete(res, hio);
176807ebc362SPawel Jakub Dawidek 		} else {
176907ebc362SPawel Jakub Dawidek 			if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) {
177007ebc362SPawel Jakub Dawidek 				primary_exit(EX_OSERR,
177107ebc362SPawel Jakub Dawidek 				    "G_GATE_CMD_DONE failed");
177232115b10SPawel Jakub Dawidek 			}
177332115b10SPawel Jakub Dawidek 		}
177432115b10SPawel Jakub Dawidek 		pjdlog_debug(2,
177532115b10SPawel Jakub Dawidek 		    "ggate_send: (%p) Moving request to the free queue.", hio);
177632115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
177732115b10SPawel Jakub Dawidek 	}
177832115b10SPawel Jakub Dawidek 	/* NOTREACHED */
177932115b10SPawel Jakub Dawidek 	return (NULL);
178032115b10SPawel Jakub Dawidek }
178132115b10SPawel Jakub Dawidek 
178232115b10SPawel Jakub Dawidek /*
178332115b10SPawel Jakub Dawidek  * Thread synchronize local and remote components.
178432115b10SPawel Jakub Dawidek  */
178532115b10SPawel Jakub Dawidek static void *
178632115b10SPawel Jakub Dawidek sync_thread(void *arg __unused)
178732115b10SPawel Jakub Dawidek {
178832115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
178932115b10SPawel Jakub Dawidek 	struct hio *hio;
179032115b10SPawel Jakub Dawidek 	struct g_gate_ctl_io *ggio;
1791fa356f6cSPawel Jakub Dawidek 	struct timeval tstart, tend, tdiff;
179232115b10SPawel Jakub Dawidek 	unsigned int ii, ncomp, ncomps;
179332115b10SPawel Jakub Dawidek 	off_t offset, length, synced;
179432115b10SPawel Jakub Dawidek 	bool dorewind;
179532115b10SPawel Jakub Dawidek 	int syncext;
179632115b10SPawel Jakub Dawidek 
179732115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
179832115b10SPawel Jakub Dawidek 	dorewind = true;
1799b9cf0cf5SPawel Jakub Dawidek 	synced = 0;
1800b9cf0cf5SPawel Jakub Dawidek 	offset = -1;
180132115b10SPawel Jakub Dawidek 
180232115b10SPawel Jakub Dawidek 	for (;;) {
180332115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
1804b9cf0cf5SPawel Jakub Dawidek 		if (offset >= 0 && !sync_inprogress) {
1805fa356f6cSPawel Jakub Dawidek 			gettimeofday(&tend, NULL);
1806fa356f6cSPawel Jakub Dawidek 			timersub(&tend, &tstart, &tdiff);
1807fa356f6cSPawel Jakub Dawidek 			pjdlog_info("Synchronization interrupted after %#.0T. "
1808fa356f6cSPawel Jakub Dawidek 			    "%NB synchronized so far.", &tdiff,
180953d9b386SPawel Jakub Dawidek 			    (intmax_t)synced);
18105bdff860SPawel Jakub Dawidek 			event_send(res, EVENT_SYNCINTR);
181153d9b386SPawel Jakub Dawidek 		}
181232115b10SPawel Jakub Dawidek 		while (!sync_inprogress) {
181332115b10SPawel Jakub Dawidek 			dorewind = true;
181432115b10SPawel Jakub Dawidek 			synced = 0;
181532115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
181632115b10SPawel Jakub Dawidek 		}
181732115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
181832115b10SPawel Jakub Dawidek 		/*
181932115b10SPawel Jakub Dawidek 		 * Obtain offset at which we should synchronize.
182032115b10SPawel Jakub Dawidek 		 * Rewind synchronization if needed.
182132115b10SPawel Jakub Dawidek 		 */
182232115b10SPawel Jakub Dawidek 		mtx_lock(&res->hr_amp_lock);
182332115b10SPawel Jakub Dawidek 		if (dorewind)
182432115b10SPawel Jakub Dawidek 			activemap_sync_rewind(res->hr_amp);
182532115b10SPawel Jakub Dawidek 		offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
182632115b10SPawel Jakub Dawidek 		if (syncext != -1) {
182732115b10SPawel Jakub Dawidek 			/*
182832115b10SPawel Jakub Dawidek 			 * We synchronized entire syncext extent, we can mark
182932115b10SPawel Jakub Dawidek 			 * it as clean now.
183032115b10SPawel Jakub Dawidek 			 */
183132115b10SPawel Jakub Dawidek 			if (activemap_extent_complete(res->hr_amp, syncext))
183232115b10SPawel Jakub Dawidek 				(void)hast_activemap_flush(res);
183332115b10SPawel Jakub Dawidek 		}
183432115b10SPawel Jakub Dawidek 		mtx_unlock(&res->hr_amp_lock);
183532115b10SPawel Jakub Dawidek 		if (dorewind) {
183632115b10SPawel Jakub Dawidek 			dorewind = false;
183732115b10SPawel Jakub Dawidek 			if (offset < 0)
183832115b10SPawel Jakub Dawidek 				pjdlog_info("Nodes are in sync.");
183932115b10SPawel Jakub Dawidek 			else {
1840fa356f6cSPawel Jakub Dawidek 				pjdlog_info("Synchronization started. %NB to go.",
1841fa356f6cSPawel Jakub Dawidek 				    (intmax_t)(res->hr_extentsize *
184232115b10SPawel Jakub Dawidek 				    activemap_ndirty(res->hr_amp)));
18435bdff860SPawel Jakub Dawidek 				event_send(res, EVENT_SYNCSTART);
1844fa356f6cSPawel Jakub Dawidek 				gettimeofday(&tstart, NULL);
184532115b10SPawel Jakub Dawidek 			}
184632115b10SPawel Jakub Dawidek 		}
184732115b10SPawel Jakub Dawidek 		if (offset < 0) {
184855ce1e7cSPawel Jakub Dawidek 			sync_stop();
184932115b10SPawel Jakub Dawidek 			pjdlog_debug(1, "Nothing to synchronize.");
185032115b10SPawel Jakub Dawidek 			/*
185132115b10SPawel Jakub Dawidek 			 * Synchronization complete, make both localcnt and
185232115b10SPawel Jakub Dawidek 			 * remotecnt equal.
185332115b10SPawel Jakub Dawidek 			 */
185432115b10SPawel Jakub Dawidek 			ncomp = 1;
185532115b10SPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ncomp]);
185632115b10SPawel Jakub Dawidek 			if (ISCONNECTED(res, ncomp)) {
185732115b10SPawel Jakub Dawidek 				if (synced > 0) {
1858fa356f6cSPawel Jakub Dawidek 					int64_t bps;
1859fa356f6cSPawel Jakub Dawidek 
1860fa356f6cSPawel Jakub Dawidek 					gettimeofday(&tend, NULL);
1861fa356f6cSPawel Jakub Dawidek 					timersub(&tend, &tstart, &tdiff);
1862fa356f6cSPawel Jakub Dawidek 					bps = (int64_t)((double)synced /
1863fa356f6cSPawel Jakub Dawidek 					    ((double)tdiff.tv_sec +
1864fa356f6cSPawel Jakub Dawidek 					    (double)tdiff.tv_usec / 1000000));
186532115b10SPawel Jakub Dawidek 					pjdlog_info("Synchronization complete. "
1866fa356f6cSPawel Jakub Dawidek 					    "%NB synchronized in %#.0lT (%NB/sec).",
1867fa356f6cSPawel Jakub Dawidek 					    (intmax_t)synced, &tdiff,
1868fa356f6cSPawel Jakub Dawidek 					    (intmax_t)bps);
18695bdff860SPawel Jakub Dawidek 					event_send(res, EVENT_SYNCDONE);
187032115b10SPawel Jakub Dawidek 				}
187132115b10SPawel Jakub Dawidek 				mtx_lock(&metadata_lock);
187232115b10SPawel Jakub Dawidek 				res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
187332115b10SPawel Jakub Dawidek 				res->hr_primary_localcnt =
187432115b10SPawel Jakub Dawidek 				    res->hr_secondary_remotecnt;
18759237aa3fSMikolaj Golub 				res->hr_primary_remotecnt =
18769237aa3fSMikolaj Golub 				    res->hr_secondary_localcnt;
187732115b10SPawel Jakub Dawidek 				pjdlog_debug(1,
187832115b10SPawel Jakub Dawidek 				    "Setting localcnt to %ju and remotecnt to %ju.",
187932115b10SPawel Jakub Dawidek 				    (uintmax_t)res->hr_primary_localcnt,
18809237aa3fSMikolaj Golub 				    (uintmax_t)res->hr_primary_remotecnt);
188132115b10SPawel Jakub Dawidek 				(void)metadata_write(res);
188232115b10SPawel Jakub Dawidek 				mtx_unlock(&metadata_lock);
188332115b10SPawel Jakub Dawidek 			}
188432115b10SPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ncomp]);
188532115b10SPawel Jakub Dawidek 			continue;
188632115b10SPawel Jakub Dawidek 		}
188732115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: Taking free request.");
188832115b10SPawel Jakub Dawidek 		QUEUE_TAKE2(hio, free);
188932115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Got free request.", hio);
189032115b10SPawel Jakub Dawidek 		/*
189132115b10SPawel Jakub Dawidek 		 * Lock the range we are going to synchronize. We don't want
189232115b10SPawel Jakub Dawidek 		 * race where someone writes between our read and write.
189332115b10SPawel Jakub Dawidek 		 */
189432115b10SPawel Jakub Dawidek 		for (;;) {
189532115b10SPawel Jakub Dawidek 			mtx_lock(&range_lock);
189632115b10SPawel Jakub Dawidek 			if (rangelock_islocked(range_regular, offset, length)) {
189732115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
189832115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd locked.",
189932115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
190032115b10SPawel Jakub Dawidek 				range_sync_wait = true;
190132115b10SPawel Jakub Dawidek 				cv_wait(&range_sync_cond, &range_lock);
190232115b10SPawel Jakub Dawidek 				range_sync_wait = false;
190332115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
190432115b10SPawel Jakub Dawidek 				continue;
190532115b10SPawel Jakub Dawidek 			}
190632115b10SPawel Jakub Dawidek 			if (rangelock_add(range_sync, offset, length) < 0) {
190732115b10SPawel Jakub Dawidek 				mtx_unlock(&range_lock);
190832115b10SPawel Jakub Dawidek 				pjdlog_debug(2,
190932115b10SPawel Jakub Dawidek 				    "sync: Range offset=%jd length=%jd is already locked, waiting.",
191032115b10SPawel Jakub Dawidek 				    (intmax_t)offset, (intmax_t)length);
191132115b10SPawel Jakub Dawidek 				sleep(1);
191232115b10SPawel Jakub Dawidek 				continue;
191332115b10SPawel Jakub Dawidek 			}
191432115b10SPawel Jakub Dawidek 			mtx_unlock(&range_lock);
191532115b10SPawel Jakub Dawidek 			break;
191632115b10SPawel Jakub Dawidek 		}
191732115b10SPawel Jakub Dawidek 		/*
191832115b10SPawel Jakub Dawidek 		 * First read the data from synchronization source.
191932115b10SPawel Jakub Dawidek 		 */
192032115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
192132115b10SPawel Jakub Dawidek 		ggio = &hio->hio_ggio;
192232115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_READ;
192332115b10SPawel Jakub Dawidek 		ggio->gctl_offset = offset;
192432115b10SPawel Jakub Dawidek 		ggio->gctl_length = length;
192532115b10SPawel Jakub Dawidek 		ggio->gctl_error = 0;
192607ebc362SPawel Jakub Dawidek 		hio->hio_done = false;
192707ebc362SPawel Jakub Dawidek 		hio->hio_replication = res->hr_replication;
192832115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
192932115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
193032115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
193132115b10SPawel Jakub Dawidek 		    hio);
193232115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
193332115b10SPawel Jakub Dawidek 		    hio);
193432115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
193532115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
193632115b10SPawel Jakub Dawidek 			/*
193732115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
193832115b10SPawel Jakub Dawidek 			 * so handle request locally.
193932115b10SPawel Jakub Dawidek 			 */
194032115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
194132115b10SPawel Jakub Dawidek 			ncomp = 0;
194232115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
19432ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
194432115b10SPawel Jakub Dawidek 			/*
194532115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
194632115b10SPawel Jakub Dawidek 			 * so send request to the remote node.
194732115b10SPawel Jakub Dawidek 			 */
194832115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
194932115b10SPawel Jakub Dawidek 			ncomp = 1;
195032115b10SPawel Jakub Dawidek 		}
195132115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
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 READ 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 read synchronization data: %s.",
196532115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
196632115b10SPawel Jakub Dawidek 			goto free_queue;
196732115b10SPawel Jakub Dawidek 		}
196832115b10SPawel Jakub Dawidek 
196932115b10SPawel Jakub Dawidek 		/*
197032115b10SPawel Jakub Dawidek 		 * We read the data from synchronization source, now write it
197132115b10SPawel Jakub Dawidek 		 * to synchronization target.
197232115b10SPawel Jakub Dawidek 		 */
197332115b10SPawel Jakub Dawidek 		SYNCREQ(hio);
197432115b10SPawel Jakub Dawidek 		ggio->gctl_cmd = BIO_WRITE;
197532115b10SPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++)
197632115b10SPawel Jakub Dawidek 			hio->hio_errors[ii] = EINVAL;
197732115b10SPawel Jakub Dawidek 		reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
197832115b10SPawel Jakub Dawidek 		    hio);
197932115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
198032115b10SPawel Jakub Dawidek 		    hio);
198132115b10SPawel Jakub Dawidek 		mtx_lock(&metadata_lock);
198232115b10SPawel Jakub Dawidek 		if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
198332115b10SPawel Jakub Dawidek 			/*
198432115b10SPawel Jakub Dawidek 			 * This range is up-to-date on local component,
198532115b10SPawel Jakub Dawidek 			 * so we update remote component.
198632115b10SPawel Jakub Dawidek 			 */
198732115b10SPawel Jakub Dawidek 			 /* Remote component is 1 for now. */
198832115b10SPawel Jakub Dawidek 			ncomp = 1;
198932115b10SPawel Jakub Dawidek 		} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
19902ec483c5SPawel Jakub Dawidek 			PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
199132115b10SPawel Jakub Dawidek 			/*
199232115b10SPawel Jakub Dawidek 			 * This range is out-of-date on local component,
199332115b10SPawel Jakub Dawidek 			 * so we update it.
199432115b10SPawel Jakub Dawidek 			 */
199532115b10SPawel Jakub Dawidek 			 /* Local component is 0 for now. */
199632115b10SPawel Jakub Dawidek 			ncomp = 0;
199732115b10SPawel Jakub Dawidek 		}
199832115b10SPawel Jakub Dawidek 		mtx_unlock(&metadata_lock);
199932115b10SPawel Jakub Dawidek 
20003f5bce18SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
200132115b10SPawel Jakub Dawidek 		    hio);
200232115b10SPawel Jakub Dawidek 		refcount_init(&hio->hio_countdown, 1);
200332115b10SPawel Jakub Dawidek 		QUEUE_INSERT1(hio, send, ncomp);
200432115b10SPawel Jakub Dawidek 
200532115b10SPawel Jakub Dawidek 		/*
200632115b10SPawel Jakub Dawidek 		 * Let's wait for WRITE to finish.
200732115b10SPawel Jakub Dawidek 		 */
200832115b10SPawel Jakub Dawidek 		mtx_lock(&sync_lock);
200932115b10SPawel Jakub Dawidek 		while (!ISSYNCREQDONE(hio))
201032115b10SPawel Jakub Dawidek 			cv_wait(&sync_cond, &sync_lock);
201132115b10SPawel Jakub Dawidek 		mtx_unlock(&sync_lock);
201232115b10SPawel Jakub Dawidek 
201332115b10SPawel Jakub Dawidek 		if (hio->hio_errors[ncomp] != 0) {
201432115b10SPawel Jakub Dawidek 			pjdlog_error("Unable to write synchronization data: %s.",
201532115b10SPawel Jakub Dawidek 			    strerror(hio->hio_errors[ncomp]));
201632115b10SPawel Jakub Dawidek 			goto free_queue;
201732115b10SPawel Jakub Dawidek 		}
2018e23d2d01SPawel Jakub Dawidek 
2019e23d2d01SPawel Jakub Dawidek 		synced += length;
202032115b10SPawel Jakub Dawidek free_queue:
202132115b10SPawel Jakub Dawidek 		mtx_lock(&range_lock);
202232115b10SPawel Jakub Dawidek 		rangelock_del(range_sync, offset, length);
202332115b10SPawel Jakub Dawidek 		if (range_regular_wait)
202432115b10SPawel Jakub Dawidek 			cv_signal(&range_regular_cond);
202532115b10SPawel Jakub Dawidek 		mtx_unlock(&range_lock);
202632115b10SPawel Jakub Dawidek 		pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
202732115b10SPawel Jakub Dawidek 		    hio);
202832115b10SPawel Jakub Dawidek 		QUEUE_INSERT2(hio, free);
202932115b10SPawel Jakub Dawidek 	}
203032115b10SPawel Jakub Dawidek 	/* NOTREACHED */
203132115b10SPawel Jakub Dawidek 	return (NULL);
203232115b10SPawel Jakub Dawidek }
203332115b10SPawel Jakub Dawidek 
2034115f4e5cSPawel Jakub Dawidek void
2035115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv)
20360989854dSPawel Jakub Dawidek {
20370989854dSPawel Jakub Dawidek 	unsigned int ii, ncomps;
2038115f4e5cSPawel Jakub Dawidek 	int modified, vint;
2039115f4e5cSPawel Jakub Dawidek 	const char *vstr;
20400989854dSPawel Jakub Dawidek 
20410989854dSPawel Jakub Dawidek 	pjdlog_info("Reloading configuration...");
20420989854dSPawel Jakub Dawidek 
20432ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
20442ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(gres == res);
2045115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "remoteaddr");
20460b626a28SPawel Jakub Dawidek 	nv_assert(nv, "sourceaddr");
2047115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "replication");
20481fee97b0SPawel Jakub Dawidek 	nv_assert(nv, "checksum");
20498cd3d45aSPawel Jakub Dawidek 	nv_assert(nv, "compression");
2050115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "timeout");
2051115f4e5cSPawel Jakub Dawidek 	nv_assert(nv, "exec");
2052518dd4c0SPawel Jakub Dawidek 	nv_assert(nv, "metaflush");
2053115f4e5cSPawel Jakub Dawidek 
20540989854dSPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
20550989854dSPawel Jakub Dawidek 
20561fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR	0x01
20570b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR	0x02
20580b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION	0x04
20590b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM	0x08
20600b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION	0x10
20610b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT	0x20
20620b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC		0x40
2063518dd4c0SPawel Jakub Dawidek #define MODIFIED_METAFLUSH	0x80
20640989854dSPawel Jakub Dawidek 	modified = 0;
2065115f4e5cSPawel Jakub Dawidek 
2066115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "remoteaddr");
2067115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
20680989854dSPawel Jakub Dawidek 		/*
20690989854dSPawel Jakub Dawidek 		 * Don't copy res->hr_remoteaddr to gres just yet.
20700989854dSPawel Jakub Dawidek 		 * We want remote_close() to log disconnect from the old
20710989854dSPawel Jakub Dawidek 		 * addresses, not from the new ones.
20720989854dSPawel Jakub Dawidek 		 */
20730989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REMOTEADDR;
20740989854dSPawel Jakub Dawidek 	}
20750b626a28SPawel Jakub Dawidek 	vstr = nv_get_string(nv, "sourceaddr");
20760b626a28SPawel Jakub Dawidek 	if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
20770b626a28SPawel Jakub Dawidek 		strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
20780b626a28SPawel Jakub Dawidek 		modified |= MODIFIED_SOURCEADDR;
20790b626a28SPawel Jakub Dawidek 	}
2080115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "replication");
2081115f4e5cSPawel Jakub Dawidek 	if (gres->hr_replication != vint) {
2082115f4e5cSPawel Jakub Dawidek 		gres->hr_replication = vint;
20830989854dSPawel Jakub Dawidek 		modified |= MODIFIED_REPLICATION;
20840989854dSPawel Jakub Dawidek 	}
20851fee97b0SPawel Jakub Dawidek 	vint = nv_get_int32(nv, "checksum");
20861fee97b0SPawel Jakub Dawidek 	if (gres->hr_checksum != vint) {
20871fee97b0SPawel Jakub Dawidek 		gres->hr_checksum = vint;
20881fee97b0SPawel Jakub Dawidek 		modified |= MODIFIED_CHECKSUM;
20891fee97b0SPawel Jakub Dawidek 	}
20908cd3d45aSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "compression");
20918cd3d45aSPawel Jakub Dawidek 	if (gres->hr_compression != vint) {
20928cd3d45aSPawel Jakub Dawidek 		gres->hr_compression = vint;
20938cd3d45aSPawel Jakub Dawidek 		modified |= MODIFIED_COMPRESSION;
20948cd3d45aSPawel Jakub Dawidek 	}
2095115f4e5cSPawel Jakub Dawidek 	vint = nv_get_int32(nv, "timeout");
2096115f4e5cSPawel Jakub Dawidek 	if (gres->hr_timeout != vint) {
2097115f4e5cSPawel Jakub Dawidek 		gres->hr_timeout = vint;
20980989854dSPawel Jakub Dawidek 		modified |= MODIFIED_TIMEOUT;
20990989854dSPawel Jakub Dawidek 	}
2100115f4e5cSPawel Jakub Dawidek 	vstr = nv_get_string(nv, "exec");
2101115f4e5cSPawel Jakub Dawidek 	if (strcmp(gres->hr_exec, vstr) != 0) {
2102115f4e5cSPawel Jakub Dawidek 		strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
21030becad39SPawel Jakub Dawidek 		modified |= MODIFIED_EXEC;
21040becad39SPawel Jakub Dawidek 	}
2105518dd4c0SPawel Jakub Dawidek 	vint = nv_get_int32(nv, "metaflush");
2106518dd4c0SPawel Jakub Dawidek 	if (gres->hr_metaflush != vint) {
2107518dd4c0SPawel Jakub Dawidek 		gres->hr_metaflush = vint;
2108518dd4c0SPawel Jakub Dawidek 		modified |= MODIFIED_METAFLUSH;
2109518dd4c0SPawel Jakub Dawidek 	}
2110115f4e5cSPawel Jakub Dawidek 
21110989854dSPawel Jakub Dawidek 	/*
21121fee97b0SPawel Jakub Dawidek 	 * Change timeout for connected sockets.
21131fee97b0SPawel Jakub Dawidek 	 * Don't bother if we need to reconnect.
21140989854dSPawel Jakub Dawidek 	 */
21151fee97b0SPawel Jakub Dawidek 	if ((modified & MODIFIED_TIMEOUT) != 0 &&
211607ebc362SPawel Jakub Dawidek 	    (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
21170989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
21180989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
21190989854dSPawel Jakub Dawidek 				continue;
21200989854dSPawel Jakub Dawidek 			rw_rlock(&hio_remote_lock[ii]);
21210989854dSPawel Jakub Dawidek 			if (!ISCONNECTED(gres, ii)) {
21220989854dSPawel Jakub Dawidek 				rw_unlock(&hio_remote_lock[ii]);
21230989854dSPawel Jakub Dawidek 				continue;
21240989854dSPawel Jakub Dawidek 			}
21250989854dSPawel Jakub Dawidek 			rw_unlock(&hio_remote_lock[ii]);
21260989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remotein,
21270989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
21280989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
21290989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
21300989854dSPawel Jakub Dawidek 			}
21310989854dSPawel Jakub Dawidek 			if (proto_timeout(gres->hr_remoteout,
21320989854dSPawel Jakub Dawidek 			    gres->hr_timeout) < 0) {
21330989854dSPawel Jakub Dawidek 				pjdlog_errno(LOG_WARNING,
21340989854dSPawel Jakub Dawidek 				    "Unable to set connection timeout");
21350989854dSPawel Jakub Dawidek 			}
21360989854dSPawel Jakub Dawidek 		}
21371fee97b0SPawel Jakub Dawidek 	}
213807ebc362SPawel Jakub Dawidek 	if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
21390989854dSPawel Jakub Dawidek 		for (ii = 0; ii < ncomps; ii++) {
21400989854dSPawel Jakub Dawidek 			if (!ISREMOTE(ii))
21410989854dSPawel Jakub Dawidek 				continue;
21420989854dSPawel Jakub Dawidek 			remote_close(gres, ii);
21430989854dSPawel Jakub Dawidek 		}
21440989854dSPawel Jakub Dawidek 		if (modified & MODIFIED_REMOTEADDR) {
2145115f4e5cSPawel Jakub Dawidek 			vstr = nv_get_string(nv, "remoteaddr");
2146115f4e5cSPawel Jakub Dawidek 			strlcpy(gres->hr_remoteaddr, vstr,
21470989854dSPawel Jakub Dawidek 			    sizeof(gres->hr_remoteaddr));
21480989854dSPawel Jakub Dawidek 		}
21490989854dSPawel Jakub Dawidek 	}
21500989854dSPawel Jakub Dawidek #undef	MODIFIED_REMOTEADDR
21510b626a28SPawel Jakub Dawidek #undef	MODIFIED_SOURCEADDR
21520989854dSPawel Jakub Dawidek #undef	MODIFIED_REPLICATION
21531fee97b0SPawel Jakub Dawidek #undef	MODIFIED_CHECKSUM
21548cd3d45aSPawel Jakub Dawidek #undef	MODIFIED_COMPRESSION
21550989854dSPawel Jakub Dawidek #undef	MODIFIED_TIMEOUT
21560becad39SPawel Jakub Dawidek #undef	MODIFIED_EXEC
2157518dd4c0SPawel Jakub Dawidek #undef	MODIFIED_METAFLUSH
21580989854dSPawel Jakub Dawidek 
21590989854dSPawel Jakub Dawidek 	pjdlog_info("Configuration reloaded successfully.");
21600989854dSPawel Jakub Dawidek }
21610989854dSPawel Jakub Dawidek 
2162f7fe83f9SPawel Jakub Dawidek static void
2163ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp)
2164ff6bb1f8SPawel Jakub Dawidek {
2165ff6bb1f8SPawel Jakub Dawidek 	struct proto_conn *in, *out;
2166ff6bb1f8SPawel Jakub Dawidek 
2167ff6bb1f8SPawel Jakub Dawidek 	if (!ISREMOTE(ncomp))
2168ff6bb1f8SPawel Jakub Dawidek 		return;
2169ff6bb1f8SPawel Jakub Dawidek 
2170ff6bb1f8SPawel Jakub Dawidek 	rw_rlock(&hio_remote_lock[ncomp]);
2171ff6bb1f8SPawel Jakub Dawidek 
2172ff6bb1f8SPawel Jakub Dawidek 	if (!real_remote(res)) {
2173ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2174ff6bb1f8SPawel Jakub Dawidek 		return;
2175ff6bb1f8SPawel Jakub Dawidek 	}
2176ff6bb1f8SPawel Jakub Dawidek 
2177ff6bb1f8SPawel Jakub Dawidek 	if (ISCONNECTED(res, ncomp)) {
21782ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein != NULL);
21792ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout != NULL);
2180ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2181ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2182ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2183ff6bb1f8SPawel Jakub Dawidek 		return;
2184ff6bb1f8SPawel Jakub Dawidek 	}
2185ff6bb1f8SPawel Jakub Dawidek 
21862ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remotein == NULL);
21872ec483c5SPawel Jakub Dawidek 	PJDLOG_ASSERT(res->hr_remoteout == NULL);
2188ff6bb1f8SPawel Jakub Dawidek 	/*
2189ff6bb1f8SPawel Jakub Dawidek 	 * Upgrade the lock. It doesn't have to be atomic as no other thread
2190ff6bb1f8SPawel Jakub Dawidek 	 * can change connection status from disconnected to connected.
2191ff6bb1f8SPawel Jakub Dawidek 	 */
2192ff6bb1f8SPawel Jakub Dawidek 	rw_unlock(&hio_remote_lock[ncomp]);
2193ff6bb1f8SPawel Jakub Dawidek 	pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2194ff6bb1f8SPawel Jakub Dawidek 	    res->hr_remoteaddr);
2195ff6bb1f8SPawel Jakub Dawidek 	in = out = NULL;
2196ac0401e3SPawel Jakub Dawidek 	if (init_remote(res, &in, &out) == 0) {
2197ff6bb1f8SPawel Jakub Dawidek 		rw_wlock(&hio_remote_lock[ncomp]);
21982ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
21992ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
22002ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in != NULL && out != NULL);
2201ff6bb1f8SPawel Jakub Dawidek 		res->hr_remotein = in;
2202ff6bb1f8SPawel Jakub Dawidek 		res->hr_remoteout = out;
2203ff6bb1f8SPawel Jakub Dawidek 		rw_unlock(&hio_remote_lock[ncomp]);
2204ff6bb1f8SPawel Jakub Dawidek 		pjdlog_info("Successfully reconnected to %s.",
2205ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2206ff6bb1f8SPawel Jakub Dawidek 		sync_start();
2207ff6bb1f8SPawel Jakub Dawidek 	} else {
2208ff6bb1f8SPawel Jakub Dawidek 		/* Both connections should be NULL. */
22092ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remotein == NULL);
22102ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(res->hr_remoteout == NULL);
22112ec483c5SPawel Jakub Dawidek 		PJDLOG_ASSERT(in == NULL && out == NULL);
2212ff6bb1f8SPawel Jakub Dawidek 		pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2213ff6bb1f8SPawel Jakub Dawidek 		    res->hr_remoteaddr);
2214ff6bb1f8SPawel Jakub Dawidek 	}
2215ff6bb1f8SPawel Jakub Dawidek }
2216ff6bb1f8SPawel Jakub Dawidek 
221732115b10SPawel Jakub Dawidek /*
221832115b10SPawel Jakub Dawidek  * Thread guards remote connections and reconnects when needed, handles
221932115b10SPawel Jakub Dawidek  * signals, etc.
222032115b10SPawel Jakub Dawidek  */
222132115b10SPawel Jakub Dawidek static void *
222232115b10SPawel Jakub Dawidek guard_thread(void *arg)
222332115b10SPawel Jakub Dawidek {
222432115b10SPawel Jakub Dawidek 	struct hast_resource *res = arg;
222532115b10SPawel Jakub Dawidek 	unsigned int ii, ncomps;
22266d0c801eSPawel Jakub Dawidek 	struct timespec timeout;
2227ff6bb1f8SPawel Jakub Dawidek 	time_t lastcheck, now;
22286d0c801eSPawel Jakub Dawidek 	sigset_t mask;
22296d0c801eSPawel Jakub Dawidek 	int signo;
223032115b10SPawel Jakub Dawidek 
223132115b10SPawel Jakub Dawidek 	ncomps = HAST_NCOMPONENTS;
2232ff6bb1f8SPawel Jakub Dawidek 	lastcheck = time(NULL);
223332115b10SPawel Jakub Dawidek 
22346d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
22356d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
22366d0c801eSPawel Jakub Dawidek 	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
22376d0c801eSPawel Jakub Dawidek 
22388d7dcf14SMikolaj Golub 	timeout.tv_sec = HAST_KEEPALIVE;
22396d0c801eSPawel Jakub Dawidek 	timeout.tv_nsec = 0;
22406d0c801eSPawel Jakub Dawidek 	signo = -1;
22416d0c801eSPawel Jakub Dawidek 
224232115b10SPawel Jakub Dawidek 	for (;;) {
22436d0c801eSPawel Jakub Dawidek 		switch (signo) {
22446d0c801eSPawel Jakub Dawidek 		case SIGINT:
22456d0c801eSPawel Jakub Dawidek 		case SIGTERM:
22466d0c801eSPawel Jakub Dawidek 			sigexit_received = true;
224732115b10SPawel Jakub Dawidek 			primary_exitx(EX_OK,
224832115b10SPawel Jakub Dawidek 			    "Termination signal received, exiting.");
22496d0c801eSPawel Jakub Dawidek 			break;
22506d0c801eSPawel Jakub Dawidek 		default:
2251ff6bb1f8SPawel Jakub Dawidek 			break;
2252f7fe83f9SPawel Jakub Dawidek 		}
22536d0c801eSPawel Jakub Dawidek 
2254ac0401e3SPawel Jakub Dawidek 		/*
2255ac0401e3SPawel Jakub Dawidek 		 * Don't check connections until we fully started,
2256ac0401e3SPawel Jakub Dawidek 		 * as we may still be looping, waiting for remote node
2257ac0401e3SPawel Jakub Dawidek 		 * to switch from primary to secondary.
2258ac0401e3SPawel Jakub Dawidek 		 */
2259ac0401e3SPawel Jakub Dawidek 		if (fullystarted) {
22606d0c801eSPawel Jakub Dawidek 			pjdlog_debug(2, "remote_guard: Checking connections.");
2261ff6bb1f8SPawel Jakub Dawidek 			now = time(NULL);
22628d7dcf14SMikolaj Golub 			if (lastcheck + HAST_KEEPALIVE <= now) {
22636d0c801eSPawel Jakub Dawidek 				for (ii = 0; ii < ncomps; ii++)
2264ff6bb1f8SPawel Jakub Dawidek 					guard_one(res, ii);
2265ff6bb1f8SPawel Jakub Dawidek 				lastcheck = now;
226632115b10SPawel Jakub Dawidek 			}
2267ac0401e3SPawel Jakub Dawidek 		}
22686d0c801eSPawel Jakub Dawidek 		signo = sigtimedwait(&mask, NULL, &timeout);
226932115b10SPawel Jakub Dawidek 	}
227032115b10SPawel Jakub Dawidek 	/* NOTREACHED */
227132115b10SPawel Jakub Dawidek 	return (NULL);
227232115b10SPawel Jakub Dawidek }
2273