secondary.c revision 211882
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 211882 2010-08-27 14:26:37Z 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"
60204076Spjd#include "metadata.h"
61204076Spjd#include "proto.h"
62204076Spjd#include "subr.h"
63204076Spjd#include "synch.h"
64204076Spjd
65204076Spjdstruct hio {
66204076Spjd	uint64_t 	 hio_seq;
67204076Spjd	int	 	 hio_error;
68204076Spjd	struct nv	*hio_nv;
69204076Spjd	void		*hio_data;
70204076Spjd	uint8_t		 hio_cmd;
71204076Spjd	uint64_t	 hio_offset;
72204076Spjd	uint64_t	 hio_length;
73204076Spjd	TAILQ_ENTRY(hio) hio_next;
74204076Spjd};
75204076Spjd
76204076Spjd/*
77204076Spjd * Free list holds unused structures. When free list is empty, we have to wait
78204076Spjd * until some in-progress requests are freed.
79204076Spjd */
80204076Spjdstatic TAILQ_HEAD(, hio) hio_free_list;
81204076Spjdstatic pthread_mutex_t hio_free_list_lock;
82204076Spjdstatic pthread_cond_t hio_free_list_cond;
83204076Spjd/*
84204076Spjd * Disk thread (the one that do I/O requests) takes requests from this list.
85204076Spjd */
86204076Spjdstatic TAILQ_HEAD(, hio) hio_disk_list;
87204076Spjdstatic pthread_mutex_t hio_disk_list_lock;
88204076Spjdstatic pthread_cond_t hio_disk_list_cond;
89204076Spjd/*
90204076Spjd * There is one recv list for every component, although local components don't
91204076Spjd * use recv lists as local requests are done synchronously.
92204076Spjd */
93204076Spjdstatic TAILQ_HEAD(, hio) hio_send_list;
94204076Spjdstatic pthread_mutex_t hio_send_list_lock;
95204076Spjdstatic pthread_cond_t hio_send_list_cond;
96204076Spjd
97204076Spjd/*
98204076Spjd * Maximum number of outstanding I/O requests.
99204076Spjd */
100204076Spjd#define	HAST_HIO_MAX	256
101204076Spjd
102204076Spjdstatic void *recv_thread(void *arg);
103204076Spjdstatic void *disk_thread(void *arg);
104204076Spjdstatic void *send_thread(void *arg);
105204076Spjd
106211877Spjd#define	QUEUE_INSERT(name, hio)	do {					\
107211877Spjd	bool _wakeup;							\
108211877Spjd									\
109211877Spjd	mtx_lock(&hio_##name##_list_lock);				\
110211877Spjd	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
111211877Spjd	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next);		\
112211877Spjd	mtx_unlock(&hio_##name##_list_lock);				\
113211877Spjd	if (_wakeup)							\
114211877Spjd		cv_signal(&hio_##name##_list_cond);			\
115211877Spjd} while (0)
116211877Spjd#define	QUEUE_TAKE(name, hio)	do {					\
117211877Spjd	mtx_lock(&hio_##name##_list_lock);				\
118211877Spjd	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
119211877Spjd		cv_wait(&hio_##name##_list_cond,			\
120211877Spjd		    &hio_##name##_list_lock);				\
121211877Spjd	}								\
122211877Spjd	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next);		\
123211877Spjd	mtx_unlock(&hio_##name##_list_lock);				\
124211877Spjd} while (0)
125211877Spjd
126204076Spjdstatic void
127204076Spjdinit_environment(void)
128204076Spjd{
129204076Spjd	struct hio *hio;
130204076Spjd	unsigned int ii;
131204076Spjd
132204076Spjd	/*
133204076Spjd	 * Initialize lists, their locks and theirs condition variables.
134204076Spjd	 */
135204076Spjd	TAILQ_INIT(&hio_free_list);
136204076Spjd	mtx_init(&hio_free_list_lock);
137204076Spjd	cv_init(&hio_free_list_cond);
138204076Spjd	TAILQ_INIT(&hio_disk_list);
139204076Spjd	mtx_init(&hio_disk_list_lock);
140204076Spjd	cv_init(&hio_disk_list_cond);
141204076Spjd	TAILQ_INIT(&hio_send_list);
142204076Spjd	mtx_init(&hio_send_list_lock);
143204076Spjd	cv_init(&hio_send_list_cond);
144204076Spjd
145204076Spjd	/*
146204076Spjd	 * Allocate requests pool and initialize requests.
147204076Spjd	 */
148204076Spjd	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
149204076Spjd		hio = malloc(sizeof(*hio));
150204076Spjd		if (hio == NULL) {
151210879Spjd			pjdlog_exitx(EX_TEMPFAIL,
152210879Spjd			    "Unable to allocate memory (%zu bytes) for hio request.",
153210879Spjd			    sizeof(*hio));
154204076Spjd		}
155204076Spjd		hio->hio_error = 0;
156204076Spjd		hio->hio_data = malloc(MAXPHYS);
157204076Spjd		if (hio->hio_data == NULL) {
158210879Spjd			pjdlog_exitx(EX_TEMPFAIL,
159210879Spjd			    "Unable to allocate memory (%zu bytes) for gctl_data.",
160210879Spjd			    (size_t)MAXPHYS);
161204076Spjd		}
162204076Spjd		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
163204076Spjd	}
164204076Spjd}
165204076Spjd
166204076Spjdstatic void
167204076Spjdinit_local(struct hast_resource *res)
168204076Spjd{
169204076Spjd
170204076Spjd	if (metadata_read(res, true) < 0)
171204076Spjd		exit(EX_NOINPUT);
172204076Spjd}
173204076Spjd
174204076Spjdstatic void
175204076Spjdinit_remote(struct hast_resource *res, struct nv *nvin)
176204076Spjd{
177204076Spjd	uint64_t resuid;
178204076Spjd	struct nv *nvout;
179204076Spjd	unsigned char *map;
180204076Spjd	size_t mapsize;
181204076Spjd
182204076Spjd	map = NULL;
183204076Spjd	mapsize = 0;
184204076Spjd	nvout = nv_alloc();
185204076Spjd	nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
186204076Spjd	nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
187204076Spjd	resuid = nv_get_uint64(nvin, "resuid");
188204076Spjd	res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
189204076Spjd	res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
190204076Spjd	nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
191204076Spjd	nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
192204076Spjd	mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
193204076Spjd	    METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
194204076Spjd	map = malloc(mapsize);
195204076Spjd	if (map == NULL) {
196204076Spjd		pjdlog_exitx(EX_TEMPFAIL,
197204076Spjd		    "Unable to allocate memory (%zu bytes) for activemap.",
198204076Spjd		    mapsize);
199204076Spjd	}
200204076Spjd	nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
201204076Spjd	/*
202204076Spjd	 * When we work as primary and secondary is missing we will increase
203204076Spjd	 * localcnt in our metadata. When secondary is connected and synced
204204076Spjd	 * we make localcnt be equal to remotecnt, which means nodes are more
205204076Spjd	 * or less in sync.
206204076Spjd	 * Split-brain condition is when both nodes are not able to communicate
207204076Spjd	 * and are both configured as primary nodes. In turn, they can both
208204076Spjd	 * make incompatible changes to the data and we have to detect that.
209204076Spjd	 * Under split-brain condition we will increase our localcnt on first
210204076Spjd	 * write and remote node will increase its localcnt on first write.
211204076Spjd	 * When we connect we can see that primary's localcnt is greater than
212204076Spjd	 * our remotecnt (primary was modified while we weren't watching) and
213204076Spjd	 * our localcnt is greater than primary's remotecnt (we were modified
214204076Spjd	 * while primary wasn't watching).
215204076Spjd	 * There are many possible combinations which are all gathered below.
216204076Spjd	 * Don't pay too much attention to exact numbers, the more important
217204076Spjd	 * is to compare them. We compare secondary's local with primary's
218204076Spjd	 * remote and secondary's remote with primary's local.
219204076Spjd	 * Note that every case where primary's localcnt is smaller than
220204076Spjd	 * secondary's remotecnt and where secondary's localcnt is smaller than
221204076Spjd	 * primary's remotecnt should be impossible in practise. We will perform
222204076Spjd	 * full synchronization then. Those cases are marked with an asterisk.
223204076Spjd	 * Regular synchronization means that only extents marked as dirty are
224204076Spjd	 * synchronized (regular synchronization).
225204076Spjd	 *
226204076Spjd	 * SECONDARY METADATA PRIMARY METADATA
227204076Spjd	 * local=3 remote=3   local=2 remote=2*  ?! Full sync from secondary.
228204076Spjd	 * local=3 remote=3   local=2 remote=3*  ?! Full sync from primary.
229204076Spjd	 * local=3 remote=3   local=2 remote=4*  ?! Full sync from primary.
230204076Spjd	 * local=3 remote=3   local=3 remote=2   Primary is out-of-date,
231204076Spjd	 *                                       regular sync from secondary.
232204076Spjd	 * local=3 remote=3   local=3 remote=3   Regular sync just in case.
233204076Spjd	 * local=3 remote=3   local=3 remote=4*  ?! Full sync from primary.
234204076Spjd	 * local=3 remote=3   local=4 remote=2   Split-brain condition.
235204076Spjd	 * local=3 remote=3   local=4 remote=3   Secondary out-of-date,
236204076Spjd	 *                                       regular sync from primary.
237204076Spjd	 * local=3 remote=3   local=4 remote=4*  ?! Full sync from primary.
238204076Spjd	 */
239204076Spjd	if (res->hr_resuid == 0) {
240204076Spjd		/*
241204076Spjd		 * Provider is used for the first time. Initialize everything.
242204076Spjd		 */
243204076Spjd		assert(res->hr_secondary_localcnt == 0);
244204076Spjd		res->hr_resuid = resuid;
245204076Spjd		if (metadata_write(res) < 0)
246204076Spjd			exit(EX_NOINPUT);
247204076Spjd		memset(map, 0xff, mapsize);
248204076Spjd		nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
249204076Spjd	} else if (
250204076Spjd	    /* Is primary is out-of-date? */
251204076Spjd	    (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
252204076Spjd	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
253204076Spjd	    /* Node are more or less in sync? */
254204076Spjd	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
255204076Spjd	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
256204076Spjd	    /* Is secondary is out-of-date? */
257204076Spjd	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
258204076Spjd	     res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
259204076Spjd		/*
260204076Spjd		 * Nodes are more or less in sync or one of the nodes is
261204076Spjd		 * out-of-date.
262204076Spjd		 * It doesn't matter at this point which one, we just have to
263204076Spjd		 * send out local bitmap to the remote node.
264204076Spjd		 */
265204076Spjd		if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
266204076Spjd		    (ssize_t)mapsize) {
267204076Spjd			pjdlog_exit(LOG_ERR, "Unable to read activemap");
268204076Spjd		}
269204076Spjd		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
270204076Spjd		     res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
271204076Spjd			/* Primary is out-of-date, sync from secondary. */
272204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
273204076Spjd		} else {
274204076Spjd			/*
275204076Spjd			 * Secondary is out-of-date or counts match.
276204076Spjd			 * Sync from primary.
277204076Spjd			 */
278204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
279204076Spjd		}
280204076Spjd	} else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
281204076Spjd	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
282204076Spjd		/*
283204076Spjd		 * Not good, we have split-brain condition.
284204076Spjd		 */
285204076Spjd		pjdlog_error("Split-brain detected, exiting.");
286204076Spjd		nv_add_string(nvout, "Split-brain condition!", "errmsg");
287204076Spjd		free(map);
288204076Spjd		map = NULL;
289204076Spjd		mapsize = 0;
290204076Spjd	} else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
291204076Spjd	    res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
292204076Spjd		/*
293204076Spjd		 * This should never happen in practise, but we will perform
294204076Spjd		 * full synchronization.
295204076Spjd		 */
296204076Spjd		assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
297204076Spjd		    res->hr_primary_localcnt < res->hr_secondary_remotecnt);
298204076Spjd		mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
299204076Spjd		    METADATA_SIZE, res->hr_extentsize,
300204076Spjd		    res->hr_local_sectorsize);
301204076Spjd		memset(map, 0xff, mapsize);
302204076Spjd		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
303204076Spjd			/* In this one of five cases sync from secondary. */
304204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
305204076Spjd		} else {
306204076Spjd			/* For the rest four cases sync from primary. */
307204076Spjd			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
308204076Spjd		}
309204076Spjd		pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
310204076Spjd		    (uintmax_t)res->hr_primary_localcnt,
311204076Spjd		    (uintmax_t)res->hr_primary_remotecnt,
312204076Spjd		    (uintmax_t)res->hr_secondary_localcnt,
313204076Spjd		    (uintmax_t)res->hr_secondary_remotecnt);
314204076Spjd	}
315204076Spjd	if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) {
316204076Spjd		pjdlog_errno(LOG_WARNING, "Unable to send activemap to %s",
317204076Spjd		    res->hr_remoteaddr);
318204076Spjd		nv_free(nvout);
319204076Spjd		exit(EX_TEMPFAIL);
320204076Spjd	}
321209182Spjd	nv_free(nvout);
322204076Spjd	if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
323204076Spjd	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
324204076Spjd		/* Exit on split-brain. */
325204076Spjd		exit(EX_CONFIG);
326204076Spjd	}
327204076Spjd}
328204076Spjd
329204076Spjdvoid
330204076Spjdhastd_secondary(struct hast_resource *res, struct nv *nvin)
331204076Spjd{
332204076Spjd	pthread_t td;
333204076Spjd	pid_t pid;
334204076Spjd	int error;
335204076Spjd
336204076Spjd	/*
337204076Spjd	 * Create communication channel between parent and child.
338204076Spjd	 */
339204076Spjd	if (proto_client("socketpair://", &res->hr_ctrl) < 0) {
340204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
341204076Spjd		pjdlog_exit(EX_OSERR,
342204076Spjd		    "Unable to create control sockets between parent and child");
343204076Spjd	}
344204076Spjd
345204076Spjd	pid = fork();
346204076Spjd	if (pid < 0) {
347204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
348204076Spjd		pjdlog_exit(EX_OSERR, "Unable to fork");
349204076Spjd	}
350204076Spjd
351204076Spjd	if (pid > 0) {
352204076Spjd		/* This is parent. */
353204076Spjd		proto_close(res->hr_remotein);
354204076Spjd		res->hr_remotein = NULL;
355204076Spjd		proto_close(res->hr_remoteout);
356204076Spjd		res->hr_remoteout = NULL;
357204076Spjd		res->hr_workerpid = pid;
358204076Spjd		return;
359204076Spjd	}
360204076Spjd	(void)pidfile_close(pfh);
361204076Spjd
362204076Spjd	setproctitle("%s (secondary)", res->hr_name);
363204076Spjd
364210880Spjd	signal(SIGHUP, SIG_DFL);
365210880Spjd	signal(SIGCHLD, SIG_DFL);
366210880Spjd
367207371Spjd	/* Error in setting timeout is not critical, but why should it fail? */
368207371Spjd	if (proto_timeout(res->hr_remotein, 0) < 0)
369207371Spjd		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
370207371Spjd	if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0)
371207371Spjd		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
372207371Spjd
373204076Spjd	init_local(res);
374204076Spjd	init_remote(res, nvin);
375204076Spjd	init_environment();
376204076Spjd
377204076Spjd	error = pthread_create(&td, NULL, recv_thread, res);
378204076Spjd	assert(error == 0);
379204076Spjd	error = pthread_create(&td, NULL, disk_thread, res);
380204076Spjd	assert(error == 0);
381204076Spjd	error = pthread_create(&td, NULL, send_thread, res);
382204076Spjd	assert(error == 0);
383204076Spjd	(void)ctrl_thread(res);
384204076Spjd}
385204076Spjd
386204076Spjdstatic void
387204076Spjdreqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...)
388204076Spjd{
389204076Spjd	char msg[1024];
390204076Spjd	va_list ap;
391204076Spjd	int len;
392204076Spjd
393204076Spjd	va_start(ap, fmt);
394204076Spjd	len = vsnprintf(msg, sizeof(msg), fmt, ap);
395204076Spjd	va_end(ap);
396204076Spjd	if ((size_t)len < sizeof(msg)) {
397204076Spjd		switch (hio->hio_cmd) {
398204076Spjd		case HIO_READ:
399204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
400204076Spjd			    "READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
401204076Spjd			    (uintmax_t)hio->hio_length);
402204076Spjd			break;
403204076Spjd		case HIO_DELETE:
404204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
405204076Spjd			    "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
406204076Spjd			    (uintmax_t)hio->hio_length);
407204076Spjd			break;
408204076Spjd		case HIO_FLUSH:
409204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
410204076Spjd			break;
411204076Spjd		case HIO_WRITE:
412204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
413204076Spjd			    "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
414204076Spjd			    (uintmax_t)hio->hio_length);
415204076Spjd			break;
416211882Spjd		case HIO_KEEPALIVE:
417211882Spjd			(void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
418211882Spjd			break;
419204076Spjd		default:
420204076Spjd			(void)snprintf(msg + len, sizeof(msg) - len,
421204076Spjd			    "UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
422204076Spjd			break;
423204076Spjd		}
424204076Spjd	}
425204076Spjd	pjdlog_common(loglevel, debuglevel, error, "%s", msg);
426204076Spjd}
427204076Spjd
428204076Spjdstatic int
429204076Spjdrequnpack(struct hast_resource *res, struct hio *hio)
430204076Spjd{
431204076Spjd
432204076Spjd	hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd");
433204076Spjd	if (hio->hio_cmd == 0) {
434204076Spjd		pjdlog_error("Header contains no 'cmd' field.");
435204076Spjd		hio->hio_error = EINVAL;
436204076Spjd		goto end;
437204076Spjd	}
438204076Spjd	switch (hio->hio_cmd) {
439211882Spjd	case HIO_KEEPALIVE:
440211882Spjd		break;
441204076Spjd	case HIO_READ:
442204076Spjd	case HIO_WRITE:
443204076Spjd	case HIO_DELETE:
444204076Spjd		hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset");
445204076Spjd		if (nv_error(hio->hio_nv) != 0) {
446204076Spjd			pjdlog_error("Header is missing 'offset' field.");
447204076Spjd			hio->hio_error = EINVAL;
448204076Spjd			goto end;
449204076Spjd		}
450204076Spjd		hio->hio_length = nv_get_uint64(hio->hio_nv, "length");
451204076Spjd		if (nv_error(hio->hio_nv) != 0) {
452204076Spjd			pjdlog_error("Header is missing 'length' field.");
453204076Spjd			hio->hio_error = EINVAL;
454204076Spjd			goto end;
455204076Spjd		}
456204076Spjd		if (hio->hio_length == 0) {
457204076Spjd			pjdlog_error("Data length is zero.");
458204076Spjd			hio->hio_error = EINVAL;
459204076Spjd			goto end;
460204076Spjd		}
461204076Spjd		if (hio->hio_length > MAXPHYS) {
462204076Spjd			pjdlog_error("Data length is too large (%ju > %ju).",
463204076Spjd			    (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
464204076Spjd			hio->hio_error = EINVAL;
465204076Spjd			goto end;
466204076Spjd		}
467204076Spjd		if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
468204076Spjd			pjdlog_error("Offset %ju is not multiple of sector size.",
469204076Spjd			    (uintmax_t)hio->hio_offset);
470204076Spjd			hio->hio_error = EINVAL;
471204076Spjd			goto end;
472204076Spjd		}
473204076Spjd		if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
474204076Spjd			pjdlog_error("Length %ju is not multiple of sector size.",
475204076Spjd			    (uintmax_t)hio->hio_length);
476204076Spjd			hio->hio_error = EINVAL;
477204076Spjd			goto end;
478204076Spjd		}
479204076Spjd		if (hio->hio_offset + hio->hio_length >
480204076Spjd		    (uint64_t)res->hr_datasize) {
481204076Spjd			pjdlog_error("Data offset is too large (%ju > %ju).",
482204076Spjd			    (uintmax_t)(hio->hio_offset + hio->hio_length),
483204076Spjd			    (uintmax_t)res->hr_datasize);
484204076Spjd			hio->hio_error = EINVAL;
485204076Spjd			goto end;
486204076Spjd		}
487204076Spjd		break;
488204076Spjd	default:
489204076Spjd		pjdlog_error("Header contains invalid 'cmd' (%hhu).",
490204076Spjd		    hio->hio_cmd);
491204076Spjd		hio->hio_error = EINVAL;
492204076Spjd		goto end;
493204076Spjd	}
494204076Spjd	hio->hio_error = 0;
495204076Spjdend:
496204076Spjd	return (hio->hio_error);
497204076Spjd}
498204076Spjd
499204076Spjd/*
500204076Spjd * Thread receives requests from the primary node.
501204076Spjd */
502204076Spjdstatic void *
503204076Spjdrecv_thread(void *arg)
504204076Spjd{
505204076Spjd	struct hast_resource *res = arg;
506204076Spjd	struct hio *hio;
507204076Spjd
508204076Spjd	for (;;) {
509204076Spjd		pjdlog_debug(2, "recv: Taking free request.");
510211877Spjd		QUEUE_TAKE(free, hio);
511204076Spjd		pjdlog_debug(2, "recv: (%p) Got request.", hio);
512204076Spjd		if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) {
513204076Spjd			pjdlog_exit(EX_TEMPFAIL,
514204076Spjd			    "Unable to receive request header");
515204076Spjd		}
516211877Spjd		if (requnpack(res, hio) != 0) {
517211877Spjd			pjdlog_debug(2,
518211877Spjd			    "recv: (%p) Moving request to the send queue.",
519211877Spjd			    hio);
520211877Spjd			QUEUE_INSERT(send, hio);
521211877Spjd			continue;
522211877Spjd		}
523204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio,
524204076Spjd		    "recv: (%p) Got request header: ", hio);
525211882Spjd		if (hio->hio_cmd == HIO_KEEPALIVE) {
526211882Spjd			pjdlog_debug(2,
527211882Spjd			    "recv: (%p) Moving request to the free queue.",
528211882Spjd			    hio);
529211882Spjd			nv_free(hio->hio_nv);
530211882Spjd			QUEUE_INSERT(free, hio);
531211882Spjd			continue;
532211882Spjd		} else if (hio->hio_cmd == HIO_WRITE) {
533204076Spjd			if (hast_proto_recv_data(res, res->hr_remotein,
534204076Spjd			    hio->hio_nv, hio->hio_data, MAXPHYS) < 0) {
535204076Spjd				pjdlog_exit(EX_TEMPFAIL,
536204076Spjd				    "Unable to receive reply data");
537204076Spjd			}
538204076Spjd		}
539204076Spjd		pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
540204076Spjd		    hio);
541211877Spjd		QUEUE_INSERT(disk, hio);
542204076Spjd	}
543204076Spjd	/* NOTREACHED */
544204076Spjd	return (NULL);
545204076Spjd}
546204076Spjd
547204076Spjd/*
548204076Spjd * Thread reads from or writes to local component and also handles DELETE and
549204076Spjd * FLUSH requests.
550204076Spjd */
551204076Spjdstatic void *
552204076Spjddisk_thread(void *arg)
553204076Spjd{
554204076Spjd	struct hast_resource *res = arg;
555204076Spjd	struct hio *hio;
556204076Spjd	ssize_t ret;
557211877Spjd	bool clear_activemap;
558204076Spjd
559204076Spjd	clear_activemap = true;
560204076Spjd
561204076Spjd	for (;;) {
562204076Spjd		pjdlog_debug(2, "disk: Taking request.");
563211877Spjd		QUEUE_TAKE(disk, hio);
564204076Spjd		while (clear_activemap) {
565204076Spjd			unsigned char *map;
566204076Spjd			size_t mapsize;
567204076Spjd
568204076Spjd			/*
569204076Spjd			 * When first request is received, it means that primary
570204076Spjd			 * already received our activemap, merged it and stored
571204076Spjd			 * locally. We can now safely clear our activemap.
572204076Spjd			 */
573204076Spjd			mapsize =
574204076Spjd			    activemap_calc_ondisk_size(res->hr_local_mediasize -
575204076Spjd			    METADATA_SIZE, res->hr_extentsize,
576204076Spjd			    res->hr_local_sectorsize);
577204076Spjd			map = calloc(1, mapsize);
578204076Spjd			if (map == NULL) {
579204076Spjd				pjdlog_warning("Unable to allocate memory to clear local activemap.");
580204076Spjd				break;
581204076Spjd			}
582204076Spjd			if (pwrite(res->hr_localfd, map, mapsize,
583204076Spjd			    METADATA_SIZE) != (ssize_t)mapsize) {
584204076Spjd				pjdlog_errno(LOG_WARNING,
585204076Spjd				    "Unable to store cleared activemap");
586204076Spjd				free(map);
587204076Spjd				break;
588204076Spjd			}
589204076Spjd			free(map);
590204076Spjd			clear_activemap = false;
591204076Spjd			pjdlog_debug(1, "Local activemap cleared.");
592204076Spjd		}
593204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
594204076Spjd		/* Handle the actual request. */
595204076Spjd		switch (hio->hio_cmd) {
596204076Spjd		case HIO_READ:
597204076Spjd			ret = pread(res->hr_localfd, hio->hio_data,
598204076Spjd			    hio->hio_length,
599204076Spjd			    hio->hio_offset + res->hr_localoff);
600204076Spjd			if (ret < 0)
601204076Spjd				hio->hio_error = errno;
602204076Spjd			else if (ret != (int64_t)hio->hio_length)
603204076Spjd				hio->hio_error = EIO;
604204076Spjd			else
605204076Spjd				hio->hio_error = 0;
606204076Spjd			break;
607204076Spjd		case HIO_WRITE:
608204076Spjd			ret = pwrite(res->hr_localfd, hio->hio_data,
609204076Spjd			    hio->hio_length,
610204076Spjd			    hio->hio_offset + res->hr_localoff);
611204076Spjd			if (ret < 0)
612204076Spjd				hio->hio_error = errno;
613204076Spjd			else if (ret != (int64_t)hio->hio_length)
614204076Spjd				hio->hio_error = EIO;
615204076Spjd			else
616204076Spjd				hio->hio_error = 0;
617204076Spjd			break;
618204076Spjd		case HIO_DELETE:
619204076Spjd			ret = g_delete(res->hr_localfd,
620204076Spjd			    hio->hio_offset + res->hr_localoff,
621204076Spjd			    hio->hio_length);
622204076Spjd			if (ret < 0)
623204076Spjd				hio->hio_error = errno;
624204076Spjd			else
625204076Spjd				hio->hio_error = 0;
626204076Spjd			break;
627204076Spjd		case HIO_FLUSH:
628204076Spjd			ret = g_flush(res->hr_localfd);
629204076Spjd			if (ret < 0)
630204076Spjd				hio->hio_error = errno;
631204076Spjd			else
632204076Spjd				hio->hio_error = 0;
633204076Spjd			break;
634204076Spjd		}
635204076Spjd		if (hio->hio_error != 0) {
636204076Spjd			reqlog(LOG_ERR, 0, hio->hio_error, hio,
637204076Spjd			    "Request failed: ");
638204076Spjd		}
639204076Spjd		pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
640204076Spjd		    hio);
641211877Spjd		QUEUE_INSERT(send, hio);
642204076Spjd	}
643204076Spjd	/* NOTREACHED */
644204076Spjd	return (NULL);
645204076Spjd}
646204076Spjd
647204076Spjd/*
648204076Spjd * Thread sends requests back to primary node.
649204076Spjd */
650204076Spjdstatic void *
651204076Spjdsend_thread(void *arg)
652204076Spjd{
653204076Spjd	struct hast_resource *res = arg;
654204076Spjd	struct nv *nvout;
655204076Spjd	struct hio *hio;
656204076Spjd	void *data;
657204076Spjd	size_t length;
658204076Spjd
659204076Spjd	for (;;) {
660204076Spjd		pjdlog_debug(2, "send: Taking request.");
661211877Spjd		QUEUE_TAKE(send, hio);
662204076Spjd		reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
663204076Spjd		nvout = nv_alloc();
664204076Spjd		/* Copy sequence number. */
665204076Spjd		nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq");
666204076Spjd		switch (hio->hio_cmd) {
667204076Spjd		case HIO_READ:
668204076Spjd			if (hio->hio_error == 0) {
669204076Spjd				data = hio->hio_data;
670204076Spjd				length = hio->hio_length;
671204076Spjd				break;
672204076Spjd			}
673204076Spjd			/*
674204076Spjd			 * We send no data in case of an error.
675204076Spjd			 */
676204076Spjd			/* FALLTHROUGH */
677204076Spjd		case HIO_DELETE:
678204076Spjd		case HIO_FLUSH:
679204076Spjd		case HIO_WRITE:
680204076Spjd			data = NULL;
681204076Spjd			length = 0;
682204076Spjd			break;
683204076Spjd		default:
684204076Spjd			abort();
685204076Spjd			break;
686204076Spjd		}
687204076Spjd		if (hio->hio_error != 0)
688204076Spjd			nv_add_int16(nvout, hio->hio_error, "error");
689204076Spjd		if (hast_proto_send(res, res->hr_remoteout, nvout, data,
690204076Spjd		    length) < 0) {
691204076Spjd			pjdlog_exit(EX_TEMPFAIL, "Unable to send reply.");
692204076Spjd		}
693204076Spjd		nv_free(nvout);
694209185Spjd		pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
695204076Spjd		    hio);
696204076Spjd		nv_free(hio->hio_nv);
697204076Spjd		hio->hio_error = 0;
698211877Spjd		QUEUE_INSERT(free, hio);
699204076Spjd	}
700204076Spjd	/* NOTREACHED */
701204076Spjd	return (NULL);
702204076Spjd}
703