secondary.c revision 211984
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 211984 2010-08-30 00:31:30Z pjd $");
33
34#include <sys/param.h>
35#include <sys/time.h>
36#include <sys/bio.h>
37#include <sys/disk.h>
38#include <sys/stat.h>
39
40#include <assert.h>
41#include <err.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <libgeom.h>
45#include <pthread.h>
46#include <stdint.h>
47#include <stdio.h>
48#include <string.h>
49#include <sysexits.h>
50#include <unistd.h>
51
52#include <activemap.h>
53#include <nv.h>
54#include <pjdlog.h>
55
56#include "control.h"
57#include "hast.h"
58#include "hast_proto.h"
59#include "hastd.h"
60#include "hooks.h"
61#include "metadata.h"
62#include "proto.h"
63#include "subr.h"
64#include "synch.h"
65
66struct hio {
67	uint64_t 	 hio_seq;
68	int	 	 hio_error;
69	struct nv	*hio_nv;
70	void		*hio_data;
71	uint8_t		 hio_cmd;
72	uint64_t	 hio_offset;
73	uint64_t	 hio_length;
74	TAILQ_ENTRY(hio) hio_next;
75};
76
77static struct hast_resource *gres;
78
79/*
80 * Free list holds unused structures. When free list is empty, we have to wait
81 * until some in-progress requests are freed.
82 */
83static TAILQ_HEAD(, hio) hio_free_list;
84static pthread_mutex_t hio_free_list_lock;
85static pthread_cond_t hio_free_list_cond;
86/*
87 * Disk thread (the one that do I/O requests) takes requests from this list.
88 */
89static TAILQ_HEAD(, hio) hio_disk_list;
90static pthread_mutex_t hio_disk_list_lock;
91static pthread_cond_t hio_disk_list_cond;
92/*
93 * There is one recv list for every component, although local components don't
94 * use recv lists as local requests are done synchronously.
95 */
96static TAILQ_HEAD(, hio) hio_send_list;
97static pthread_mutex_t hio_send_list_lock;
98static pthread_cond_t hio_send_list_cond;
99
100/*
101 * Maximum number of outstanding I/O requests.
102 */
103#define	HAST_HIO_MAX	256
104
105static void *recv_thread(void *arg);
106static void *disk_thread(void *arg);
107static void *send_thread(void *arg);
108
109#define	QUEUE_INSERT(name, hio)	do {					\
110	bool _wakeup;							\
111									\
112	mtx_lock(&hio_##name##_list_lock);				\
113	_wakeup = TAILQ_EMPTY(&hio_##name##_list);			\
114	TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next);		\
115	mtx_unlock(&hio_##name##_list_lock);				\
116	if (_wakeup)							\
117		cv_signal(&hio_##name##_list_cond);			\
118} while (0)
119#define	QUEUE_TAKE(name, hio)	do {					\
120	mtx_lock(&hio_##name##_list_lock);				\
121	while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) {	\
122		cv_wait(&hio_##name##_list_cond,			\
123		    &hio_##name##_list_lock);				\
124	}								\
125	TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next);		\
126	mtx_unlock(&hio_##name##_list_lock);				\
127} while (0)
128
129static void
130init_environment(void)
131{
132	struct hio *hio;
133	unsigned int ii;
134
135	/*
136	 * Initialize lists, their locks and theirs condition variables.
137	 */
138	TAILQ_INIT(&hio_free_list);
139	mtx_init(&hio_free_list_lock);
140	cv_init(&hio_free_list_cond);
141	TAILQ_INIT(&hio_disk_list);
142	mtx_init(&hio_disk_list_lock);
143	cv_init(&hio_disk_list_cond);
144	TAILQ_INIT(&hio_send_list);
145	mtx_init(&hio_send_list_lock);
146	cv_init(&hio_send_list_cond);
147
148	/*
149	 * Allocate requests pool and initialize requests.
150	 */
151	for (ii = 0; ii < HAST_HIO_MAX; ii++) {
152		hio = malloc(sizeof(*hio));
153		if (hio == NULL) {
154			pjdlog_exitx(EX_TEMPFAIL,
155			    "Unable to allocate memory (%zu bytes) for hio request.",
156			    sizeof(*hio));
157		}
158		hio->hio_error = 0;
159		hio->hio_data = malloc(MAXPHYS);
160		if (hio->hio_data == NULL) {
161			pjdlog_exitx(EX_TEMPFAIL,
162			    "Unable to allocate memory (%zu bytes) for gctl_data.",
163			    (size_t)MAXPHYS);
164		}
165		TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
166	}
167}
168
169static void
170init_local(struct hast_resource *res)
171{
172
173	if (metadata_read(res, true) < 0)
174		exit(EX_NOINPUT);
175}
176
177static void
178init_remote(struct hast_resource *res, struct nv *nvin)
179{
180	uint64_t resuid;
181	struct nv *nvout;
182	unsigned char *map;
183	size_t mapsize;
184
185	map = NULL;
186	mapsize = 0;
187	nvout = nv_alloc();
188	nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
189	nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
190	resuid = nv_get_uint64(nvin, "resuid");
191	res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
192	res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
193	nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
194	nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
195	mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
196	    METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
197	map = malloc(mapsize);
198	if (map == NULL) {
199		pjdlog_exitx(EX_TEMPFAIL,
200		    "Unable to allocate memory (%zu bytes) for activemap.",
201		    mapsize);
202	}
203	nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
204	/*
205	 * When we work as primary and secondary is missing we will increase
206	 * localcnt in our metadata. When secondary is connected and synced
207	 * we make localcnt be equal to remotecnt, which means nodes are more
208	 * or less in sync.
209	 * Split-brain condition is when both nodes are not able to communicate
210	 * and are both configured as primary nodes. In turn, they can both
211	 * make incompatible changes to the data and we have to detect that.
212	 * Under split-brain condition we will increase our localcnt on first
213	 * write and remote node will increase its localcnt on first write.
214	 * When we connect we can see that primary's localcnt is greater than
215	 * our remotecnt (primary was modified while we weren't watching) and
216	 * our localcnt is greater than primary's remotecnt (we were modified
217	 * while primary wasn't watching).
218	 * There are many possible combinations which are all gathered below.
219	 * Don't pay too much attention to exact numbers, the more important
220	 * is to compare them. We compare secondary's local with primary's
221	 * remote and secondary's remote with primary's local.
222	 * Note that every case where primary's localcnt is smaller than
223	 * secondary's remotecnt and where secondary's localcnt is smaller than
224	 * primary's remotecnt should be impossible in practise. We will perform
225	 * full synchronization then. Those cases are marked with an asterisk.
226	 * Regular synchronization means that only extents marked as dirty are
227	 * synchronized (regular synchronization).
228	 *
229	 * SECONDARY METADATA PRIMARY METADATA
230	 * local=3 remote=3   local=2 remote=2*  ?! Full sync from secondary.
231	 * local=3 remote=3   local=2 remote=3*  ?! Full sync from primary.
232	 * local=3 remote=3   local=2 remote=4*  ?! Full sync from primary.
233	 * local=3 remote=3   local=3 remote=2   Primary is out-of-date,
234	 *                                       regular sync from secondary.
235	 * local=3 remote=3   local=3 remote=3   Regular sync just in case.
236	 * local=3 remote=3   local=3 remote=4*  ?! Full sync from primary.
237	 * local=3 remote=3   local=4 remote=2   Split-brain condition.
238	 * local=3 remote=3   local=4 remote=3   Secondary out-of-date,
239	 *                                       regular sync from primary.
240	 * local=3 remote=3   local=4 remote=4*  ?! Full sync from primary.
241	 */
242	if (res->hr_resuid == 0) {
243		/*
244		 * Provider is used for the first time. Initialize everything.
245		 */
246		assert(res->hr_secondary_localcnt == 0);
247		res->hr_resuid = resuid;
248		if (metadata_write(res) < 0)
249			exit(EX_NOINPUT);
250		memset(map, 0xff, mapsize);
251		nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
252	} else if (
253	    /* Is primary is out-of-date? */
254	    (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
255	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
256	    /* Node are more or less in sync? */
257	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
258	     res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
259	    /* Is secondary is out-of-date? */
260	    (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
261	     res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
262		/*
263		 * Nodes are more or less in sync or one of the nodes is
264		 * out-of-date.
265		 * It doesn't matter at this point which one, we just have to
266		 * send out local bitmap to the remote node.
267		 */
268		if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
269		    (ssize_t)mapsize) {
270			pjdlog_exit(LOG_ERR, "Unable to read activemap");
271		}
272		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
273		     res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
274			/* Primary is out-of-date, sync from secondary. */
275			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
276		} else {
277			/*
278			 * Secondary is out-of-date or counts match.
279			 * Sync from primary.
280			 */
281			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
282		}
283	} else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
284	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
285		/*
286		 * Not good, we have split-brain condition.
287		 */
288		pjdlog_error("Split-brain detected, exiting.");
289		nv_add_string(nvout, "Split-brain condition!", "errmsg");
290		free(map);
291		map = NULL;
292		mapsize = 0;
293	} else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
294	    res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
295		/*
296		 * This should never happen in practise, but we will perform
297		 * full synchronization.
298		 */
299		assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
300		    res->hr_primary_localcnt < res->hr_secondary_remotecnt);
301		mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
302		    METADATA_SIZE, res->hr_extentsize,
303		    res->hr_local_sectorsize);
304		memset(map, 0xff, mapsize);
305		if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
306			/* In this one of five cases sync from secondary. */
307			nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
308		} else {
309			/* For the rest four cases sync from primary. */
310			nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
311		}
312		pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
313		    (uintmax_t)res->hr_primary_localcnt,
314		    (uintmax_t)res->hr_primary_remotecnt,
315		    (uintmax_t)res->hr_secondary_localcnt,
316		    (uintmax_t)res->hr_secondary_remotecnt);
317	}
318	if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) {
319		pjdlog_errno(LOG_WARNING, "Unable to send activemap to %s",
320		    res->hr_remoteaddr);
321		nv_free(nvout);
322		exit(EX_TEMPFAIL);
323	}
324	nv_free(nvout);
325	if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
326	     res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
327		/* Exit on split-brain. */
328		hook_exec(res->hr_exec, "split-brain", res->hr_name, NULL);
329		exit(EX_CONFIG);
330	}
331}
332
333void
334hastd_secondary(struct hast_resource *res, struct nv *nvin)
335{
336	pthread_t td;
337	pid_t pid;
338	int error;
339
340	/*
341	 * Create communication channel between parent and child.
342	 */
343	if (proto_client("socketpair://", &res->hr_ctrl) < 0) {
344		KEEP_ERRNO((void)pidfile_remove(pfh));
345		pjdlog_exit(EX_OSERR,
346		    "Unable to create control sockets between parent and child");
347	}
348
349	pid = fork();
350	if (pid < 0) {
351		KEEP_ERRNO((void)pidfile_remove(pfh));
352		pjdlog_exit(EX_OSERR, "Unable to fork");
353	}
354
355	if (pid > 0) {
356		/* This is parent. */
357		proto_close(res->hr_remotein);
358		res->hr_remotein = NULL;
359		proto_close(res->hr_remoteout);
360		res->hr_remoteout = NULL;
361		res->hr_workerpid = pid;
362		return;
363	}
364
365	gres = res;
366
367	(void)pidfile_close(pfh);
368	hook_fini();
369
370	setproctitle("%s (secondary)", res->hr_name);
371
372	signal(SIGHUP, SIG_DFL);
373	signal(SIGCHLD, SIG_DFL);
374
375	/* Error in setting timeout is not critical, but why should it fail? */
376	if (proto_timeout(res->hr_remotein, 0) < 0)
377		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
378	if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0)
379		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
380
381	hook_init();
382	init_local(res);
383	init_remote(res, nvin);
384	init_environment();
385	hook_exec(res->hr_exec, "connect", res->hr_name, NULL);
386
387	error = pthread_create(&td, NULL, recv_thread, res);
388	assert(error == 0);
389	error = pthread_create(&td, NULL, disk_thread, res);
390	assert(error == 0);
391	error = pthread_create(&td, NULL, send_thread, res);
392	assert(error == 0);
393	(void)ctrl_thread(res);
394}
395
396static void
397reqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...)
398{
399	char msg[1024];
400	va_list ap;
401	int len;
402
403	va_start(ap, fmt);
404	len = vsnprintf(msg, sizeof(msg), fmt, ap);
405	va_end(ap);
406	if ((size_t)len < sizeof(msg)) {
407		switch (hio->hio_cmd) {
408		case HIO_READ:
409			(void)snprintf(msg + len, sizeof(msg) - len,
410			    "READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
411			    (uintmax_t)hio->hio_length);
412			break;
413		case HIO_DELETE:
414			(void)snprintf(msg + len, sizeof(msg) - len,
415			    "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
416			    (uintmax_t)hio->hio_length);
417			break;
418		case HIO_FLUSH:
419			(void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
420			break;
421		case HIO_WRITE:
422			(void)snprintf(msg + len, sizeof(msg) - len,
423			    "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
424			    (uintmax_t)hio->hio_length);
425			break;
426		case HIO_KEEPALIVE:
427			(void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
428			break;
429		default:
430			(void)snprintf(msg + len, sizeof(msg) - len,
431			    "UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
432			break;
433		}
434	}
435	pjdlog_common(loglevel, debuglevel, error, "%s", msg);
436}
437
438static int
439requnpack(struct hast_resource *res, struct hio *hio)
440{
441
442	hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd");
443	if (hio->hio_cmd == 0) {
444		pjdlog_error("Header contains no 'cmd' field.");
445		hio->hio_error = EINVAL;
446		goto end;
447	}
448	switch (hio->hio_cmd) {
449	case HIO_KEEPALIVE:
450		break;
451	case HIO_READ:
452	case HIO_WRITE:
453	case HIO_DELETE:
454		hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset");
455		if (nv_error(hio->hio_nv) != 0) {
456			pjdlog_error("Header is missing 'offset' field.");
457			hio->hio_error = EINVAL;
458			goto end;
459		}
460		hio->hio_length = nv_get_uint64(hio->hio_nv, "length");
461		if (nv_error(hio->hio_nv) != 0) {
462			pjdlog_error("Header is missing 'length' field.");
463			hio->hio_error = EINVAL;
464			goto end;
465		}
466		if (hio->hio_length == 0) {
467			pjdlog_error("Data length is zero.");
468			hio->hio_error = EINVAL;
469			goto end;
470		}
471		if (hio->hio_length > MAXPHYS) {
472			pjdlog_error("Data length is too large (%ju > %ju).",
473			    (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
474			hio->hio_error = EINVAL;
475			goto end;
476		}
477		if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
478			pjdlog_error("Offset %ju is not multiple of sector size.",
479			    (uintmax_t)hio->hio_offset);
480			hio->hio_error = EINVAL;
481			goto end;
482		}
483		if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
484			pjdlog_error("Length %ju is not multiple of sector size.",
485			    (uintmax_t)hio->hio_length);
486			hio->hio_error = EINVAL;
487			goto end;
488		}
489		if (hio->hio_offset + hio->hio_length >
490		    (uint64_t)res->hr_datasize) {
491			pjdlog_error("Data offset is too large (%ju > %ju).",
492			    (uintmax_t)(hio->hio_offset + hio->hio_length),
493			    (uintmax_t)res->hr_datasize);
494			hio->hio_error = EINVAL;
495			goto end;
496		}
497		break;
498	default:
499		pjdlog_error("Header contains invalid 'cmd' (%hhu).",
500		    hio->hio_cmd);
501		hio->hio_error = EINVAL;
502		goto end;
503	}
504	hio->hio_error = 0;
505end:
506	return (hio->hio_error);
507}
508
509static void
510secondary_exit(int exitcode, const char *fmt, ...)
511{
512	va_list ap;
513
514	assert(exitcode != EX_OK);
515	va_start(ap, fmt);
516	pjdlogv_errno(LOG_ERR, fmt, ap);
517	va_end(ap);
518	hook_exec(gres->hr_exec, "disconnect", gres->hr_name, NULL);
519	exit(exitcode);
520}
521
522/*
523 * Thread receives requests from the primary node.
524 */
525static void *
526recv_thread(void *arg)
527{
528	struct hast_resource *res = arg;
529	struct hio *hio;
530
531	for (;;) {
532		pjdlog_debug(2, "recv: Taking free request.");
533		QUEUE_TAKE(free, hio);
534		pjdlog_debug(2, "recv: (%p) Got request.", hio);
535		if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) {
536			secondary_exit(EX_TEMPFAIL,
537			    "Unable to receive request header");
538		}
539		if (requnpack(res, hio) != 0) {
540			pjdlog_debug(2,
541			    "recv: (%p) Moving request to the send queue.",
542			    hio);
543			QUEUE_INSERT(send, hio);
544			continue;
545		}
546		reqlog(LOG_DEBUG, 2, -1, hio,
547		    "recv: (%p) Got request header: ", hio);
548		if (hio->hio_cmd == HIO_KEEPALIVE) {
549			pjdlog_debug(2,
550			    "recv: (%p) Moving request to the free queue.",
551			    hio);
552			nv_free(hio->hio_nv);
553			QUEUE_INSERT(free, hio);
554			continue;
555		} else if (hio->hio_cmd == HIO_WRITE) {
556			if (hast_proto_recv_data(res, res->hr_remotein,
557			    hio->hio_nv, hio->hio_data, MAXPHYS) < 0) {
558				secondary_exit(EX_TEMPFAIL,
559				    "Unable to receive reply data");
560			}
561		}
562		pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
563		    hio);
564		QUEUE_INSERT(disk, hio);
565	}
566	/* NOTREACHED */
567	return (NULL);
568}
569
570/*
571 * Thread reads from or writes to local component and also handles DELETE and
572 * FLUSH requests.
573 */
574static void *
575disk_thread(void *arg)
576{
577	struct hast_resource *res = arg;
578	struct hio *hio;
579	ssize_t ret;
580	bool clear_activemap;
581
582	clear_activemap = true;
583
584	for (;;) {
585		pjdlog_debug(2, "disk: Taking request.");
586		QUEUE_TAKE(disk, hio);
587		while (clear_activemap) {
588			unsigned char *map;
589			size_t mapsize;
590
591			/*
592			 * When first request is received, it means that primary
593			 * already received our activemap, merged it and stored
594			 * locally. We can now safely clear our activemap.
595			 */
596			mapsize =
597			    activemap_calc_ondisk_size(res->hr_local_mediasize -
598			    METADATA_SIZE, res->hr_extentsize,
599			    res->hr_local_sectorsize);
600			map = calloc(1, mapsize);
601			if (map == NULL) {
602				pjdlog_warning("Unable to allocate memory to clear local activemap.");
603				break;
604			}
605			if (pwrite(res->hr_localfd, map, mapsize,
606			    METADATA_SIZE) != (ssize_t)mapsize) {
607				pjdlog_errno(LOG_WARNING,
608				    "Unable to store cleared activemap");
609				free(map);
610				break;
611			}
612			free(map);
613			clear_activemap = false;
614			pjdlog_debug(1, "Local activemap cleared.");
615		}
616		reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
617		/* Handle the actual request. */
618		switch (hio->hio_cmd) {
619		case HIO_READ:
620			ret = pread(res->hr_localfd, hio->hio_data,
621			    hio->hio_length,
622			    hio->hio_offset + res->hr_localoff);
623			if (ret < 0)
624				hio->hio_error = errno;
625			else if (ret != (int64_t)hio->hio_length)
626				hio->hio_error = EIO;
627			else
628				hio->hio_error = 0;
629			break;
630		case HIO_WRITE:
631			ret = pwrite(res->hr_localfd, hio->hio_data,
632			    hio->hio_length,
633			    hio->hio_offset + res->hr_localoff);
634			if (ret < 0)
635				hio->hio_error = errno;
636			else if (ret != (int64_t)hio->hio_length)
637				hio->hio_error = EIO;
638			else
639				hio->hio_error = 0;
640			break;
641		case HIO_DELETE:
642			ret = g_delete(res->hr_localfd,
643			    hio->hio_offset + res->hr_localoff,
644			    hio->hio_length);
645			if (ret < 0)
646				hio->hio_error = errno;
647			else
648				hio->hio_error = 0;
649			break;
650		case HIO_FLUSH:
651			ret = g_flush(res->hr_localfd);
652			if (ret < 0)
653				hio->hio_error = errno;
654			else
655				hio->hio_error = 0;
656			break;
657		}
658		if (hio->hio_error != 0) {
659			reqlog(LOG_ERR, 0, hio->hio_error, hio,
660			    "Request failed: ");
661		}
662		pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
663		    hio);
664		QUEUE_INSERT(send, hio);
665	}
666	/* NOTREACHED */
667	return (NULL);
668}
669
670/*
671 * Thread sends requests back to primary node.
672 */
673static void *
674send_thread(void *arg)
675{
676	struct hast_resource *res = arg;
677	struct nv *nvout;
678	struct hio *hio;
679	void *data;
680	size_t length;
681
682	for (;;) {
683		pjdlog_debug(2, "send: Taking request.");
684		QUEUE_TAKE(send, hio);
685		reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
686		nvout = nv_alloc();
687		/* Copy sequence number. */
688		nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq");
689		switch (hio->hio_cmd) {
690		case HIO_READ:
691			if (hio->hio_error == 0) {
692				data = hio->hio_data;
693				length = hio->hio_length;
694				break;
695			}
696			/*
697			 * We send no data in case of an error.
698			 */
699			/* FALLTHROUGH */
700		case HIO_DELETE:
701		case HIO_FLUSH:
702		case HIO_WRITE:
703			data = NULL;
704			length = 0;
705			break;
706		default:
707			abort();
708			break;
709		}
710		if (hio->hio_error != 0)
711			nv_add_int16(nvout, hio->hio_error, "error");
712		if (hast_proto_send(res, res->hr_remoteout, nvout, data,
713		    length) < 0) {
714			secondary_exit(EX_TEMPFAIL, "Unable to send reply.");
715		}
716		nv_free(nvout);
717		pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
718		    hio);
719		nv_free(hio->hio_nv);
720		hio->hio_error = 0;
721		QUEUE_INSERT(free, hio);
722	}
723	/* NOTREACHED */
724	return (NULL);
725}
726