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