secondary.c revision 211984
1204076Spjd/*-
2204076Spjd * Copyright (c) 2009-2010 The FreeBSD Foundation
3211877Spjd * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4204076Spjd * All rights reserved.
5204076Spjd *
6204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from
7204076Spjd * the FreeBSD Foundation.
8204076Spjd *
9204076Spjd * Redistribution and use in source and binary forms, with or without
10204076Spjd * modification, are permitted provided that the following conditions
11204076Spjd * are met:
12204076Spjd * 1. Redistributions of source code must retain the above copyright
13204076Spjd *    notice, this list of conditions and the following disclaimer.
14204076Spjd * 2. Redistributions in binary form must reproduce the above copyright
15204076Spjd *    notice, this list of conditions and the following disclaimer in the
16204076Spjd *    documentation and/or other materials provided with the distribution.
17204076Spjd *
18204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21204076Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28204076Spjd * SUCH DAMAGE.
29204076Spjd */
30204076Spjd
31204076Spjd#include <sys/cdefs.h>
32204076Spjd__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 211984 2010-08-30 00:31:30Z pjd $");
33204076Spjd
34204076Spjd#include <sys/param.h>
35204076Spjd#include <sys/time.h>
36204076Spjd#include <sys/bio.h>
37204076Spjd#include <sys/disk.h>
38204076Spjd#include <sys/stat.h>
39204076Spjd
40204076Spjd#include <assert.h>
41204076Spjd#include <err.h>
42204076Spjd#include <errno.h>
43204076Spjd#include <fcntl.h>
44204076Spjd#include <libgeom.h>
45204076Spjd#include <pthread.h>
46204076Spjd#include <stdint.h>
47204076Spjd#include <stdio.h>
48204076Spjd#include <string.h>
49204076Spjd#include <sysexits.h>
50204076Spjd#include <unistd.h>
51204076Spjd
52204076Spjd#include <activemap.h>
53204076Spjd#include <nv.h>
54204076Spjd#include <pjdlog.h>
55204076Spjd
56204076Spjd#include "control.h"
57204076Spjd#include "hast.h"
58204076Spjd#include "hast_proto.h"
59204076Spjd#include "hastd.h"
60211977Spjd#include "hooks.h"
61204076Spjd#include "metadata.h"
62204076Spjd#include "proto.h"
63204076Spjd#include "subr.h"
64204076Spjd#include "synch.h"
65204076Spjd
66204076Spjdstruct hio {
67204076Spjd	uint64_t 	 hio_seq;
68204076Spjd	int	 	 hio_error;
69204076Spjd	struct nv	*hio_nv;
70204076Spjd	void		*hio_data;
71204076Spjd	uint8_t		 hio_cmd;
72204076Spjd	uint64_t	 hio_offset;
73204076Spjd	uint64_t	 hio_length;
74204076Spjd	TAILQ_ENTRY(hio) hio_next;
75204076Spjd};
76204076Spjd
77211984Spjdstatic struct hast_resource *gres;
78211984Spjd
79204076Spjd/*
80204076Spjd * Free list holds unused structures. When free list is empty, we have to wait
81204076Spjd * until some in-progress requests are freed.
82204076Spjd */
83204076Spjdstatic TAILQ_HEAD(, hio) hio_free_list;
84204076Spjdstatic pthread_mutex_t hio_free_list_lock;
85204076Spjdstatic pthread_cond_t hio_free_list_cond;
86204076Spjd/*
87204076Spjd * Disk thread (the one that do I/O requests) takes requests from this list.
88204076Spjd */
89204076Spjdstatic TAILQ_HEAD(, hio) hio_disk_list;
90204076Spjdstatic pthread_mutex_t hio_disk_list_lock;
91204076Spjdstatic pthread_cond_t hio_disk_list_cond;
92204076Spjd/*
93204076Spjd * There is one recv list for every component, although local components don't
94204076Spjd * use recv lists as local requests are done synchronously.
95204076Spjd */
96204076Spjdstatic TAILQ_HEAD(, hio) hio_send_list;
97204076Spjdstatic pthread_mutex_t hio_send_list_lock;
98204076Spjdstatic pthread_cond_t hio_send_list_cond;
99204076Spjd
100204076Spjd/*
101204076Spjd * Maximum number of outstanding I/O requests.
102204076Spjd */
103204076Spjd#define	HAST_HIO_MAX	256
104204076Spjd
105204076Spjdstatic void *recv_thread(void *arg);
106204076Spjdstatic void *disk_thread(void *arg);
107204076Spjdstatic void *send_thread(void *arg);
108204076Spjd
109211877Spjd#define	QUEUE_INSERT(name, hio)	do {					\
110211877Spjd	bool _wakeup;							\
111211877Spjd									\
112211877Spjd	mtx_lock(&hio_##name##_list_lock);				\
113211877Spjd	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
114211877Spjd	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next);		\
115211877Spjd	mtx_unlock(&hio_##name##_list_lock);				\
116211877Spjd	if (_wakeup)							\
117211877Spjd		cv_signal(&hio_##name##_list_cond);			\
118211877Spjd} while (0)
119211877Spjd#define	QUEUE_TAKE(name, hio)	do {					\
120211877Spjd	mtx_lock(&hio_##name##_list_lock);				\
121211877Spjd	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
122211877Spjd		cv_wait(&hio_##name##_list_cond,			\
123211877Spjd		    &hio_##name##_list_lock);				\
124211877Spjd	}								\
125211877Spjd	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next);		\
126211877Spjd	mtx_unlock(&hio_##name##_list_lock);				\
127211877Spjd} while (0)
128211877Spjd
129204076Spjdstatic void
130204076Spjdinit_environment(void)
131204076Spjd{
132204076Spjd	struct hio *hio;
133204076Spjd	unsigned int ii;
134204076Spjd
135204076Spjd	/*
136204076Spjd	 * Initialize lists, their locks and theirs condition variables.
137204076Spjd	 */
138204076Spjd	TAILQ_INIT(&hio_free_list);
139204076Spjd	mtx_init(&hio_free_list_lock);
140204076Spjd	cv_init(&hio_free_list_cond);
141204076Spjd	TAILQ_INIT(&hio_disk_list);
142204076Spjd	mtx_init(&hio_disk_list_lock);
143204076Spjd	cv_init(&hio_disk_list_cond);
144204076Spjd	TAILQ_INIT(&hio_send_list);
145204076Spjd	mtx_init(&hio_send_list_lock);
146204076Spjd	cv_init(&hio_send_list_cond);
147204076Spjd
148204076Spjd	/*
149204076Spjd	 * Allocate requests pool and initialize requests.
150204076Spjd	 */
151204076Spjd	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
152204076Spjd		hio = malloc(sizeof(*hio));
153204076Spjd		if (hio == NULL) {
154210879Spjd			pjdlog_exitx(EX_TEMPFAIL,
155210879Spjd			    "Unable to allocate memory (%zu bytes) for hio request.",
156210879Spjd			    sizeof(*hio));
157204076Spjd		}
158204076Spjd		hio->hio_error = 0;
159204076Spjd		hio->hio_data = malloc(MAXPHYS);
160204076Spjd		if (hio->hio_data == NULL) {
161210879Spjd			pjdlog_exitx(EX_TEMPFAIL,
162210879Spjd			    "Unable to allocate memory (%zu bytes) for gctl_data.",
163210879Spjd			    (size_t)MAXPHYS);
164204076Spjd		}
165204076Spjd		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
166204076Spjd	}
167204076Spjd}
168204076Spjd
169204076Spjdstatic void
170204076Spjdinit_local(struct hast_resource *res)
171204076Spjd{
172204076Spjd
173204076Spjd	if (metadata_read(res, true) < 0)
174204076Spjd		exit(EX_NOINPUT);
175204076Spjd}
176204076Spjd
177204076Spjdstatic void
178204076Spjdinit_remote(struct hast_resource *res, struct nv *nvin)
179204076Spjd{
180204076Spjd	uint64_t resuid;
181204076Spjd	struct nv *nvout;
182204076Spjd	unsigned char *map;
183204076Spjd	size_t mapsize;
184204076Spjd
185204076Spjd	map = NULL;
186204076Spjd	mapsize = 0;
187204076Spjd	nvout = nv_alloc();
188204076Spjd	nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
189204076Spjd	nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
190204076Spjd	resuid = nv_get_uint64(nvin, "resuid");
191204076Spjd	res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
192204076Spjd	res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
193204076Spjd	nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
194204076Spjd	nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
195204076Spjd	mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
196204076Spjd	    METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
197204076Spjd	map = malloc(mapsize);
198204076Spjd	if (map == NULL) {
199204076Spjd		pjdlog_exitx(EX_TEMPFAIL,
200204076Spjd		    "Unable to allocate memory (%zu bytes) for activemap.",
201204076Spjd		    mapsize);
202204076Spjd	}
203204076Spjd	nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
204204076Spjd	/*
205204076Spjd	 * When we work as primary and secondary is missing we will increase
206204076Spjd	 * localcnt in our metadata. When secondary is connected and synced
207204076Spjd	 * we make localcnt be equal to remotecnt, which means nodes are more
208204076Spjd	 * or less in sync.
209204076Spjd	 * Split-brain condition is when both nodes are not able to communicate
210204076Spjd	 * and are both configured as primary nodes. In turn, they can both
211204076Spjd	 * make incompatible changes to the data and we have to detect that.
212204076Spjd	 * Under split-brain condition we will increase our localcnt on first
213204076Spjd	 * write and remote node will increase its localcnt on first write.
214204076Spjd	 * When we connect we can see that primary's localcnt is greater than
215204076Spjd	 * our remotecnt (primary was modified while we weren't watching) and
216204076Spjd	 * our localcnt is greater than primary's remotecnt (we were modified
217204076Spjd	 * while primary wasn't watching).
218204076Spjd	 * There are many possible combinations which are all gathered below.
219204076Spjd	 * Don't pay too much attention to exact numbers, the more important
220204076Spjd	 * is to compare them. We compare secondary's local with primary's
221204076Spjd	 * remote and secondary's remote with primary's local.
222204076Spjd	 * Note that every case where primary's localcnt is smaller than
223204076Spjd	 * secondary's remotecnt and where secondary's localcnt is smaller than
224204076Spjd	 * primary's remotecnt should be impossible in practise. We will perform
225204076Spjd	 * full synchronization then. Those cases are marked with an asterisk.
226204076Spjd	 * Regular synchronization means that only extents marked as dirty are
227204076Spjd	 * synchronized (regular synchronization).
228204076Spjd	 *
229204076Spjd	 * SECONDARY METADATA PRIMARY METADATA
230204076Spjd	 * local=3 remote=3   local=2 remote=2*  ?! Full sync from secondary.
231204076Spjd	 * local=3 remote=3   local=2 remote=3*  ?! Full sync from primary.
232204076Spjd	 * local=3 remote=3   local=2 remote=4*  ?! Full sync from primary.
233204076Spjd	 * local=3 remote=3   local=3 remote=2   Primary is out-of-date,
234204076Spjd	 *                                       regular sync from secondary.
235204076Spjd	 * local=3 remote=3   local=3 remote=3   Regular sync just in case.
236204076Spjd	 * local=3 remote=3   local=3 remote=4*  ?! Full sync from primary.
237204076Spjd	 * local=3 remote=3   local=4 remote=2   Split-brain condition.
238204076Spjd	 * local=3 remote=3   local=4 remote=3   Secondary out-of-date,
239204076Spjd	 *                                       regular sync from primary.
240204076Spjd	 * local=3 remote=3   local=4 remote=4*  ?! Full sync from primary.
241204076Spjd	 */
242204076Spjd	if (res->hr_resuid == 0) {
243204076Spjd		/*
244204076Spjd		 * Provider is used for the first time. Initialize everything.
245204076Spjd		 */
246204076Spjd		assert(res->hr_secondary_localcnt == 0);
247204076Spjd		res->hr_resuid = resuid;
248204076Spjd		if (metadata_write(res) < 0)
249204076Spjd			exit(EX_NOINPUT);
250204076Spjd		memset(map, 0xff, mapsize);
251204076Spjd		nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
252204076Spjd	} else if (
253204076Spjd	    /* Is primary is out-of-date? */
254204076Spjd	    (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
255204076Spjd	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
256204076Spjd	    /* Node are more or less in sync? */
257204076Spjd	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
258204076Spjd	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
259204076Spjd	    /* Is secondary is out-of-date? */
260204076Spjd	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
261204076Spjd	     res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
262204076Spjd		/*
263204076Spjd		 * Nodes are more or less in sync or one of the nodes is
264204076Spjd		 * out-of-date.
265204076Spjd		 * It doesn't matter at this point which one, we just have to
266204076Spjd		 * send out local bitmap to the remote node.
267204076Spjd		 */
268204076Spjd		if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
269204076Spjd		    (ssize_t)mapsize) {
270204076Spjd			pjdlog_exit(LOG_ERR, "Unable to read activemap");
271204076Spjd		}
272204076Spjd		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
273204076Spjd		     res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
274204076Spjd			/* Primary is out-of-date, sync from secondary. */
275204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
276204076Spjd		} else {
277204076Spjd			/*
278204076Spjd			 * Secondary is out-of-date or counts match.
279204076Spjd			 * Sync from primary.
280204076Spjd			 */
281204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
282204076Spjd		}
283204076Spjd	} else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
284204076Spjd	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
285204076Spjd		/*
286204076Spjd		 * Not good, we have split-brain condition.
287204076Spjd		 */
288204076Spjd		pjdlog_error("Split-brain detected, exiting.");
289204076Spjd		nv_add_string(nvout, "Split-brain condition!", "errmsg");
290204076Spjd		free(map);
291204076Spjd		map = NULL;
292204076Spjd		mapsize = 0;
293204076Spjd	} else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
294204076Spjd	    res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
295204076Spjd		/*
296204076Spjd		 * This should never happen in practise, but we will perform
297204076Spjd		 * full synchronization.
298204076Spjd		 */
299204076Spjd		assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
300204076Spjd		    res->hr_primary_localcnt < res->hr_secondary_remotecnt);
301204076Spjd		mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
302204076Spjd		    METADATA_SIZE, res->hr_extentsize,
303204076Spjd		    res->hr_local_sectorsize);
304204076Spjd		memset(map, 0xff, mapsize);
305204076Spjd		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
306204076Spjd			/* In this one of five cases sync from secondary. */
307204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
308204076Spjd		} else {
309204076Spjd			/* For the rest four cases sync from primary. */
310204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
311204076Spjd		}
312204076Spjd		pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
313204076Spjd		    (uintmax_t)res->hr_primary_localcnt,
314204076Spjd		    (uintmax_t)res->hr_primary_remotecnt,
315204076Spjd		    (uintmax_t)res->hr_secondary_localcnt,
316204076Spjd		    (uintmax_t)res->hr_secondary_remotecnt);
317204076Spjd	}
318204076Spjd	if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) {
319204076Spjd		pjdlog_errno(LOG_WARNING, "Unable to send activemap to %s",
320204076Spjd		    res->hr_remoteaddr);
321204076Spjd		nv_free(nvout);
322204076Spjd		exit(EX_TEMPFAIL);
323204076Spjd	}
324209182Spjd	nv_free(nvout);
325204076Spjd	if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
326204076Spjd	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
327204076Spjd		/* Exit on split-brain. */
328211983Spjd		hook_exec(res->hr_exec, "split-brain", res->hr_name, NULL);
329204076Spjd		exit(EX_CONFIG);
330204076Spjd	}
331204076Spjd}
332204076Spjd
333204076Spjdvoid
334204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin)
335204076Spjd{
336204076Spjd	pthread_t td;
337204076Spjd	pid_t pid;
338204076Spjd	int error;
339204076Spjd
340204076Spjd	/*
341204076Spjd	 * Create communication channel between parent and child.
342204076Spjd	 */
343204076Spjd	if (proto_client("socketpair://", &res->hr_ctrl) < 0) {
344204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
345204076Spjd		pjdlog_exit(EX_OSERR,
346204076Spjd		    "Unable to create control sockets between parent and child");
347204076Spjd	}
348204076Spjd
349204076Spjd	pid = fork();
350204076Spjd	if (pid < 0) {
351204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
352204076Spjd		pjdlog_exit(EX_OSERR, "Unable to fork");
353204076Spjd	}
354204076Spjd
355204076Spjd	if (pid > 0) {
356204076Spjd		/* This is parent. */
357204076Spjd		proto_close(res->hr_remotein);
358204076Spjd		res->hr_remotein = NULL;
359204076Spjd		proto_close(res->hr_remoteout);
360204076Spjd		res->hr_remoteout = NULL;
361204076Spjd		res->hr_workerpid = pid;
362204076Spjd		return;
363204076Spjd	}
364211977Spjd
365211984Spjd	gres = res;
366211984Spjd
367204076Spjd	(void)pidfile_close(pfh);
368211977Spjd	hook_fini();
369204076Spjd
370204076Spjd	setproctitle("%s (secondary)", res->hr_name);
371204076Spjd
372210880Spjd	signal(SIGHUP, SIG_DFL);
373210880Spjd	signal(SIGCHLD, SIG_DFL);
374210880Spjd
375207371Spjd	/* Error in setting timeout is not critical, but why should it fail? */
376207371Spjd	if (proto_timeout(res->hr_remotein, 0) < 0)
377207371Spjd		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
378207371Spjd	if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0)
379207371Spjd		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
380207371Spjd
381211983Spjd	hook_init();
382204076Spjd	init_local(res);
383204076Spjd	init_remote(res, nvin);
384204076Spjd	init_environment();
385211984Spjd	hook_exec(res->hr_exec, "connect", res->hr_name, NULL);
386204076Spjd
387204076Spjd	error = pthread_create(&td, NULL, recv_thread, res);
388204076Spjd	assert(error == 0);
389204076Spjd	error = pthread_create(&td, NULL, disk_thread, res);
390204076Spjd	assert(error == 0);
391204076Spjd	error = pthread_create(&td, NULL, send_thread, res);
392204076Spjd	assert(error == 0);
393204076Spjd	(void)ctrl_thread(res);
394204076Spjd}
395204076Spjd
396204076Spjdstatic void
397204076Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...)
398204076Spjd{
399204076Spjd	char msg[1024];
400204076Spjd	va_list ap;
401204076Spjd	int len;
402204076Spjd
403204076Spjd	va_start(ap, fmt);
404204076Spjd	len = vsnprintf(msg, sizeof(msg), fmt, ap);
405204076Spjd	va_end(ap);
406204076Spjd	if ((size_t)len < sizeof(msg)) {
407204076Spjd		switch (hio->hio_cmd) {
408204076Spjd		case HIO_READ:
409204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
410204076Spjd			    "READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
411204076Spjd			    (uintmax_t)hio->hio_length);
412204076Spjd			break;
413204076Spjd		case HIO_DELETE:
414204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
415204076Spjd			    "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
416204076Spjd			    (uintmax_t)hio->hio_length);
417204076Spjd			break;
418204076Spjd		case HIO_FLUSH:
419204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
420204076Spjd			break;
421204076Spjd		case HIO_WRITE:
422204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
423204076Spjd			    "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
424204076Spjd			    (uintmax_t)hio->hio_length);
425204076Spjd			break;
426211882Spjd		case HIO_KEEPALIVE:
427211882Spjd			(void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
428211882Spjd			break;
429204076Spjd		default:
430204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
431204076Spjd			    "UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
432204076Spjd			break;
433204076Spjd		}
434204076Spjd	}
435204076Spjd	pjdlog_common(loglevel, debuglevel, error, "%s", msg);
436204076Spjd}
437204076Spjd
438204076Spjdstatic int
439204076Spjdrequnpack(struct hast_resource *res, struct hio *hio)
440204076Spjd{
441204076Spjd
442204076Spjd	hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd");
443204076Spjd	if (hio->hio_cmd == 0) {
444204076Spjd		pjdlog_error("Header contains no 'cmd' field.");
445204076Spjd		hio->hio_error = EINVAL;
446204076Spjd		goto end;
447204076Spjd	}
448204076Spjd	switch (hio->hio_cmd) {
449211882Spjd	case HIO_KEEPALIVE:
450211882Spjd		break;
451204076Spjd	case HIO_READ:
452204076Spjd	case HIO_WRITE:
453204076Spjd	case HIO_DELETE:
454204076Spjd		hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset");
455204076Spjd		if (nv_error(hio->hio_nv) != 0) {
456204076Spjd			pjdlog_error("Header is missing 'offset' field.");
457204076Spjd			hio->hio_error = EINVAL;
458204076Spjd			goto end;
459204076Spjd		}
460204076Spjd		hio->hio_length = nv_get_uint64(hio->hio_nv, "length");
461204076Spjd		if (nv_error(hio->hio_nv) != 0) {
462204076Spjd			pjdlog_error("Header is missing 'length' field.");
463204076Spjd			hio->hio_error = EINVAL;
464204076Spjd			goto end;
465204076Spjd		}
466204076Spjd		if (hio->hio_length == 0) {
467204076Spjd			pjdlog_error("Data length is zero.");
468204076Spjd			hio->hio_error = EINVAL;
469204076Spjd			goto end;
470204076Spjd		}
471204076Spjd		if (hio->hio_length > MAXPHYS) {
472204076Spjd			pjdlog_error("Data length is too large (%ju > %ju).",
473204076Spjd			    (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
474204076Spjd			hio->hio_error = EINVAL;
475204076Spjd			goto end;
476204076Spjd		}
477204076Spjd		if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
478204076Spjd			pjdlog_error("Offset %ju is not multiple of sector size.",
479204076Spjd			    (uintmax_t)hio->hio_offset);
480204076Spjd			hio->hio_error = EINVAL;
481204076Spjd			goto end;
482204076Spjd		}
483204076Spjd		if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
484204076Spjd			pjdlog_error("Length %ju is not multiple of sector size.",
485204076Spjd			    (uintmax_t)hio->hio_length);
486204076Spjd			hio->hio_error = EINVAL;
487204076Spjd			goto end;
488204076Spjd		}
489204076Spjd		if (hio->hio_offset + hio->hio_length >
490204076Spjd		    (uint64_t)res->hr_datasize) {
491204076Spjd			pjdlog_error("Data offset is too large (%ju > %ju).",
492204076Spjd			    (uintmax_t)(hio->hio_offset + hio->hio_length),
493204076Spjd			    (uintmax_t)res->hr_datasize);
494204076Spjd			hio->hio_error = EINVAL;
495204076Spjd			goto end;
496204076Spjd		}
497204076Spjd		break;
498204076Spjd	default:
499204076Spjd		pjdlog_error("Header contains invalid 'cmd' (%hhu).",
500204076Spjd		    hio->hio_cmd);
501204076Spjd		hio->hio_error = EINVAL;
502204076Spjd		goto end;
503204076Spjd	}
504204076Spjd	hio->hio_error = 0;
505204076Spjdend:
506204076Spjd	return (hio->hio_error);
507204076Spjd}
508204076Spjd
509211984Spjdstatic void
510211984Spjdsecondary_exit(int exitcode, const char *fmt, ...)
511211984Spjd{
512211984Spjd	va_list ap;
513211984Spjd
514211984Spjd	assert(exitcode != EX_OK);
515211984Spjd	va_start(ap, fmt);
516211984Spjd	pjdlogv_errno(LOG_ERR, fmt, ap);
517211984Spjd	va_end(ap);
518211984Spjd	hook_exec(gres->hr_exec, "disconnect", gres->hr_name, NULL);
519211984Spjd	exit(exitcode);
520211984Spjd}
521211984Spjd
522204076Spjd/*
523204076Spjd * Thread receives requests from the primary node.
524204076Spjd */
525204076Spjdstatic void *
526204076Spjdrecv_thread(void *arg)
527204076Spjd{
528204076Spjd	struct hast_resource *res = arg;
529204076Spjd	struct hio *hio;
530204076Spjd
531204076Spjd	for (;;) {
532204076Spjd		pjdlog_debug(2, "recv: Taking free request.");
533211877Spjd		QUEUE_TAKE(free, hio);
534204076Spjd		pjdlog_debug(2, "recv: (%p) Got request.", hio);
535204076Spjd		if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) {
536211984Spjd			secondary_exit(EX_TEMPFAIL,
537204076Spjd			    "Unable to receive request header");
538204076Spjd		}
539211877Spjd		if (requnpack(res, hio) != 0) {
540211877Spjd			pjdlog_debug(2,
541211877Spjd			    "recv: (%p) Moving request to the send queue.",
542211877Spjd			    hio);
543211877Spjd			QUEUE_INSERT(send, hio);
544211877Spjd			continue;
545211877Spjd		}
546204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio,
547204076Spjd		    "recv: (%p) Got request header: ", hio);
548211882Spjd		if (hio->hio_cmd == HIO_KEEPALIVE) {
549211882Spjd			pjdlog_debug(2,
550211882Spjd			    "recv: (%p) Moving request to the free queue.",
551211882Spjd			    hio);
552211882Spjd			nv_free(hio->hio_nv);
553211882Spjd			QUEUE_INSERT(free, hio);
554211882Spjd			continue;
555211882Spjd		} else if (hio->hio_cmd == HIO_WRITE) {
556204076Spjd			if (hast_proto_recv_data(res, res->hr_remotein,
557204076Spjd			    hio->hio_nv, hio->hio_data, MAXPHYS) < 0) {
558211984Spjd				secondary_exit(EX_TEMPFAIL,
559204076Spjd				    "Unable to receive reply data");
560204076Spjd			}
561204076Spjd		}
562204076Spjd		pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
563204076Spjd		    hio);
564211877Spjd		QUEUE_INSERT(disk, hio);
565204076Spjd	}
566204076Spjd	/* NOTREACHED */
567204076Spjd	return (NULL);
568204076Spjd}
569204076Spjd
570204076Spjd/*
571204076Spjd * Thread reads from or writes to local component and also handles DELETE and
572204076Spjd * FLUSH requests.
573204076Spjd */
574204076Spjdstatic void *
575204076Spjddisk_thread(void *arg)
576204076Spjd{
577204076Spjd	struct hast_resource *res = arg;
578204076Spjd	struct hio *hio;
579204076Spjd	ssize_t ret;
580211877Spjd	bool clear_activemap;
581204076Spjd
582204076Spjd	clear_activemap = true;
583204076Spjd
584204076Spjd	for (;;) {
585204076Spjd		pjdlog_debug(2, "disk: Taking request.");
586211877Spjd		QUEUE_TAKE(disk, hio);
587204076Spjd		while (clear_activemap) {
588204076Spjd			unsigned char *map;
589204076Spjd			size_t mapsize;
590204076Spjd
591204076Spjd			/*
592204076Spjd			 * When first request is received, it means that primary
593204076Spjd			 * already received our activemap, merged it and stored
594204076Spjd			 * locally. We can now safely clear our activemap.
595204076Spjd			 */
596204076Spjd			mapsize =
597204076Spjd			    activemap_calc_ondisk_size(res->hr_local_mediasize -
598204076Spjd			    METADATA_SIZE, res->hr_extentsize,
599204076Spjd			    res->hr_local_sectorsize);
600204076Spjd			map = calloc(1, mapsize);
601204076Spjd			if (map == NULL) {
602204076Spjd				pjdlog_warning("Unable to allocate memory to clear local activemap.");
603204076Spjd				break;
604204076Spjd			}
605204076Spjd			if (pwrite(res->hr_localfd, map, mapsize,
606204076Spjd			    METADATA_SIZE) != (ssize_t)mapsize) {
607204076Spjd				pjdlog_errno(LOG_WARNING,
608204076Spjd				    "Unable to store cleared activemap");
609204076Spjd				free(map);
610204076Spjd				break;
611204076Spjd			}
612204076Spjd			free(map);
613204076Spjd			clear_activemap = false;
614204076Spjd			pjdlog_debug(1, "Local activemap cleared.");
615204076Spjd		}
616204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
617204076Spjd		/* Handle the actual request. */
618204076Spjd		switch (hio->hio_cmd) {
619204076Spjd		case HIO_READ:
620204076Spjd			ret = pread(res->hr_localfd, hio->hio_data,
621204076Spjd			    hio->hio_length,
622204076Spjd			    hio->hio_offset + res->hr_localoff);
623204076Spjd			if (ret < 0)
624204076Spjd				hio->hio_error = errno;
625204076Spjd			else if (ret != (int64_t)hio->hio_length)
626204076Spjd				hio->hio_error = EIO;
627204076Spjd			else
628204076Spjd				hio->hio_error = 0;
629204076Spjd			break;
630204076Spjd		case HIO_WRITE:
631204076Spjd			ret = pwrite(res->hr_localfd, hio->hio_data,
632204076Spjd			    hio->hio_length,
633204076Spjd			    hio->hio_offset + res->hr_localoff);
634204076Spjd			if (ret < 0)
635204076Spjd				hio->hio_error = errno;
636204076Spjd			else if (ret != (int64_t)hio->hio_length)
637204076Spjd				hio->hio_error = EIO;
638204076Spjd			else
639204076Spjd				hio->hio_error = 0;
640204076Spjd			break;
641204076Spjd		case HIO_DELETE:
642204076Spjd			ret = g_delete(res->hr_localfd,
643204076Spjd			    hio->hio_offset + res->hr_localoff,
644204076Spjd			    hio->hio_length);
645204076Spjd			if (ret < 0)
646204076Spjd				hio->hio_error = errno;
647204076Spjd			else
648204076Spjd				hio->hio_error = 0;
649204076Spjd			break;
650204076Spjd		case HIO_FLUSH:
651204076Spjd			ret = g_flush(res->hr_localfd);
652204076Spjd			if (ret < 0)
653204076Spjd				hio->hio_error = errno;
654204076Spjd			else
655204076Spjd				hio->hio_error = 0;
656204076Spjd			break;
657204076Spjd		}
658204076Spjd		if (hio->hio_error != 0) {
659204076Spjd			reqlog(LOG_ERR, 0, hio->hio_error, hio,
660204076Spjd			    "Request failed: ");
661204076Spjd		}
662204076Spjd		pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
663204076Spjd		    hio);
664211877Spjd		QUEUE_INSERT(send, hio);
665204076Spjd	}
666204076Spjd	/* NOTREACHED */
667204076Spjd	return (NULL);
668204076Spjd}
669204076Spjd
670204076Spjd/*
671204076Spjd * Thread sends requests back to primary node.
672204076Spjd */
673204076Spjdstatic void *
674204076Spjdsend_thread(void *arg)
675204076Spjd{
676204076Spjd	struct hast_resource *res = arg;
677204076Spjd	struct nv *nvout;
678204076Spjd	struct hio *hio;
679204076Spjd	void *data;
680204076Spjd	size_t length;
681204076Spjd
682204076Spjd	for (;;) {
683204076Spjd		pjdlog_debug(2, "send: Taking request.");
684211877Spjd		QUEUE_TAKE(send, hio);
685204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
686204076Spjd		nvout = nv_alloc();
687204076Spjd		/* Copy sequence number. */
688204076Spjd		nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq");
689204076Spjd		switch (hio->hio_cmd) {
690204076Spjd		case HIO_READ:
691204076Spjd			if (hio->hio_error == 0) {
692204076Spjd				data = hio->hio_data;
693204076Spjd				length = hio->hio_length;
694204076Spjd				break;
695204076Spjd			}
696204076Spjd			/*
697204076Spjd			 * We send no data in case of an error.
698204076Spjd			 */
699204076Spjd			/* FALLTHROUGH */
700204076Spjd		case HIO_DELETE:
701204076Spjd		case HIO_FLUSH:
702204076Spjd		case HIO_WRITE:
703204076Spjd			data = NULL;
704204076Spjd			length = 0;
705204076Spjd			break;
706204076Spjd		default:
707204076Spjd			abort();
708204076Spjd			break;
709204076Spjd		}
710204076Spjd		if (hio->hio_error != 0)
711204076Spjd			nv_add_int16(nvout, hio->hio_error, "error");
712204076Spjd		if (hast_proto_send(res, res->hr_remoteout, nvout, data,
713204076Spjd		    length) < 0) {
714211984Spjd			secondary_exit(EX_TEMPFAIL, "Unable to send reply.");
715204076Spjd		}
716204076Spjd		nv_free(nvout);
717209185Spjd		pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
718204076Spjd		    hio);
719204076Spjd		nv_free(hio->hio_nv);
720204076Spjd		hio->hio_error = 0;
721211877Spjd		QUEUE_INSERT(free, hio);
722204076Spjd	}
723204076Spjd	/* NOTREACHED */
724204076Spjd	return (NULL);
725204076Spjd}
726