control.c revision 247866
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/9/sbin/hastd/control.c 247866 2013-03-06 06:57:18Z trociny $");
32
33#include <sys/types.h>
34#include <sys/wait.h>
35
36#include <errno.h>
37#include <pthread.h>
38#include <signal.h>
39#include <stdio.h>
40#include <string.h>
41#include <unistd.h>
42
43#include "hast.h"
44#include "hastd.h"
45#include "hast_checksum.h"
46#include "hast_compression.h"
47#include "hast_proto.h"
48#include "hooks.h"
49#include "nv.h"
50#include "pjdlog.h"
51#include "proto.h"
52#include "subr.h"
53
54#include "control.h"
55
56void
57child_cleanup(struct hast_resource *res)
58{
59
60	proto_close(res->hr_ctrl);
61	res->hr_ctrl = NULL;
62	if (res->hr_event != NULL) {
63		proto_close(res->hr_event);
64		res->hr_event = NULL;
65	}
66	if (res->hr_conn != NULL) {
67		proto_close(res->hr_conn);
68		res->hr_conn = NULL;
69	}
70	res->hr_workerpid = 0;
71}
72
73static void
74control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
75    uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
76{
77	int oldrole;
78
79	/* Name is always needed. */
80	if (name != NULL)
81		nv_add_string(nvout, name, "resource%u", no);
82
83	if (res == NULL) {
84		PJDLOG_ASSERT(cfg != NULL);
85		PJDLOG_ASSERT(name != NULL);
86
87		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
88			if (strcmp(res->hr_name, name) == 0)
89				break;
90		}
91		if (res == NULL) {
92			nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
93			return;
94		}
95	}
96	PJDLOG_ASSERT(res != NULL);
97
98	/* Send previous role back. */
99	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
100
101	/* Nothing changed, return here. */
102	if (role == res->hr_role)
103		return;
104
105	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
106	pjdlog_info("Role changed to %s.", role2str(role));
107
108	/* Change role to the new one. */
109	oldrole = res->hr_role;
110	res->hr_role = role;
111	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
112
113	/*
114	 * If previous role was primary or secondary we have to kill process
115	 * doing that work.
116	 */
117	if (res->hr_workerpid != 0) {
118		if (kill(res->hr_workerpid, SIGTERM) == -1) {
119			pjdlog_errno(LOG_WARNING,
120			    "Unable to kill worker process %u",
121			    (unsigned int)res->hr_workerpid);
122		} else if (waitpid(res->hr_workerpid, NULL, 0) !=
123		    res->hr_workerpid) {
124			pjdlog_errno(LOG_WARNING,
125			    "Error while waiting for worker process %u",
126			    (unsigned int)res->hr_workerpid);
127		} else {
128			pjdlog_debug(1, "Worker process %u stopped.",
129			    (unsigned int)res->hr_workerpid);
130		}
131		child_cleanup(res);
132	}
133
134	/* Start worker process if we are changing to primary. */
135	if (role == HAST_ROLE_PRIMARY)
136		hastd_primary(res);
137	pjdlog_prefix_set("%s", "");
138	hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
139	    role2str(res->hr_role), NULL);
140}
141
142void
143control_set_role(struct hast_resource *res, uint8_t role)
144{
145
146	control_set_role_common(NULL, NULL, role, res, NULL, 0);
147}
148
149static void
150control_status_worker(struct hast_resource *res, struct nv *nvout,
151    unsigned int no)
152{
153	struct nv *cnvin, *cnvout;
154	const char *str;
155	int error;
156
157	cnvin = NULL;
158
159	/*
160	 * Prepare and send command to worker process.
161	 */
162	cnvout = nv_alloc();
163	nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
164	error = nv_error(cnvout);
165	if (error != 0) {
166		pjdlog_common(LOG_ERR, 0, error,
167		    "Unable to prepare control header");
168		goto end;
169	}
170	if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) == -1) {
171		error = errno;
172		pjdlog_errno(LOG_ERR, "Unable to send control header");
173		goto end;
174	}
175
176	/*
177	 * Receive response.
178	 */
179	if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) == -1) {
180		error = errno;
181		pjdlog_errno(LOG_ERR, "Unable to receive control header");
182		goto end;
183	}
184
185	error = nv_get_int16(cnvin, "error");
186	if (error != 0)
187		goto end;
188
189	if ((str = nv_get_string(cnvin, "status")) == NULL) {
190		error = ENOENT;
191		pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
192		goto end;
193	}
194	nv_add_string(nvout, str, "status%u", no);
195	nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
196	nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
197	    "extentsize%u", no);
198	nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
199	    "keepdirty%u", no);
200	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read"),
201	    "stat_read%u", no);
202	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write"),
203	    "stat_write%u", no);
204	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete"),
205	    "stat_delete%u", no);
206	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush"),
207	    "stat_flush%u", no);
208	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_activemap_update"),
209	    "stat_activemap_update%u", no);
210	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read_error"),
211	    "stat_read_error%u", no);
212	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write_error"),
213	    "stat_write_error%u", no);
214	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete_error"),
215	    "stat_delete_error%u", no);
216	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush_error"),
217	    "stat_flush_error%u", no);
218end:
219	if (cnvin != NULL)
220		nv_free(cnvin);
221	if (cnvout != NULL)
222		nv_free(cnvout);
223	if (error != 0)
224		nv_add_int16(nvout, error, "error");
225}
226
227static void
228control_status(struct hastd_config *cfg, struct nv *nvout,
229    struct hast_resource *res, const char *name, unsigned int no)
230{
231
232	PJDLOG_ASSERT(cfg != NULL);
233	PJDLOG_ASSERT(nvout != NULL);
234	PJDLOG_ASSERT(name != NULL);
235
236	/* Name is always needed. */
237	nv_add_string(nvout, name, "resource%u", no);
238
239	if (res == NULL) {
240		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
241			if (strcmp(res->hr_name, name) == 0)
242				break;
243		}
244		if (res == NULL) {
245			nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
246			return;
247		}
248	}
249	PJDLOG_ASSERT(res != NULL);
250	nv_add_string(nvout, res->hr_provname, "provname%u", no);
251	nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
252	nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
253	if (res->hr_sourceaddr[0] != '\0')
254		nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
255	switch (res->hr_replication) {
256	case HAST_REPLICATION_FULLSYNC:
257		nv_add_string(nvout, "fullsync", "replication%u", no);
258		break;
259	case HAST_REPLICATION_MEMSYNC:
260		nv_add_string(nvout, "memsync", "replication%u", no);
261		break;
262	case HAST_REPLICATION_ASYNC:
263		nv_add_string(nvout, "async", "replication%u", no);
264		break;
265	default:
266		nv_add_string(nvout, "unknown", "replication%u", no);
267		break;
268	}
269	nv_add_string(nvout, checksum_name(res->hr_checksum),
270	    "checksum%u", no);
271	nv_add_string(nvout, compression_name(res->hr_compression),
272	    "compression%u", no);
273	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
274
275	switch (res->hr_role) {
276	case HAST_ROLE_PRIMARY:
277		PJDLOG_ASSERT(res->hr_workerpid != 0);
278		/* FALLTHROUGH */
279	case HAST_ROLE_SECONDARY:
280		if (res->hr_workerpid != 0)
281			break;
282		/* FALLTHROUGH */
283	default:
284		return;
285	}
286
287	/*
288	 * If we are here, it means that we have a worker process, which we
289	 * want to ask some questions.
290	 */
291	control_status_worker(res, nvout, no);
292}
293
294void
295control_handle(struct hastd_config *cfg)
296{
297	struct proto_conn *conn;
298	struct nv *nvin, *nvout;
299	unsigned int ii;
300	const char *str;
301	uint8_t cmd, role;
302	int error;
303
304	if (proto_accept(cfg->hc_controlconn, &conn) == -1) {
305		pjdlog_errno(LOG_ERR, "Unable to accept control connection");
306		return;
307	}
308
309	cfg->hc_controlin = conn;
310	nvin = nvout = NULL;
311	role = HAST_ROLE_UNDEF;
312
313	if (hast_proto_recv_hdr(conn, &nvin) == -1) {
314		pjdlog_errno(LOG_ERR, "Unable to receive control header");
315		nvin = NULL;
316		goto close;
317	}
318
319	/* Obtain command code. 0 means that nv_get_uint8() failed. */
320	cmd = nv_get_uint8(nvin, "cmd");
321	if (cmd == 0) {
322		pjdlog_error("Control header is missing 'cmd' field.");
323		goto close;
324	}
325
326	/* Allocate outgoing nv structure. */
327	nvout = nv_alloc();
328	if (nvout == NULL) {
329		pjdlog_error("Unable to allocate header for control response.");
330		goto close;
331	}
332
333	error = 0;
334
335	str = nv_get_string(nvin, "resource0");
336	if (str == NULL) {
337		pjdlog_error("Control header is missing 'resource0' field.");
338		error = EHAST_INVALID;
339		goto fail;
340	}
341	if (cmd == HASTCTL_CMD_SETROLE) {
342		role = nv_get_uint8(nvin, "role");
343		switch (role) {
344		case HAST_ROLE_INIT:
345		case HAST_ROLE_PRIMARY:
346		case HAST_ROLE_SECONDARY:
347			break;
348		default:
349			pjdlog_error("Invalid role received (%hhu).", role);
350			error = EHAST_INVALID;
351			goto fail;
352		}
353	}
354	if (strcmp(str, "all") == 0) {
355		struct hast_resource *res;
356
357		/* All configured resources. */
358
359		ii = 0;
360		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
361			switch (cmd) {
362			case HASTCTL_CMD_SETROLE:
363				control_set_role_common(cfg, nvout, role, res,
364				    res->hr_name, ii++);
365				break;
366			case HASTCTL_CMD_STATUS:
367				control_status(cfg, nvout, res, res->hr_name,
368				    ii++);
369				break;
370			default:
371				pjdlog_error("Invalid command received (%hhu).",
372				    cmd);
373				error = EHAST_UNIMPLEMENTED;
374				goto fail;
375			}
376		}
377	} else {
378		/* Only selected resources. */
379
380		for (ii = 0; ; ii++) {
381			str = nv_get_string(nvin, "resource%u", ii);
382			if (str == NULL)
383				break;
384			switch (cmd) {
385			case HASTCTL_CMD_SETROLE:
386				control_set_role_common(cfg, nvout, role, NULL,
387				    str, ii);
388				break;
389			case HASTCTL_CMD_STATUS:
390				control_status(cfg, nvout, NULL, str, ii);
391				break;
392			default:
393				pjdlog_error("Invalid command received (%hhu).",
394				    cmd);
395				error = EHAST_UNIMPLEMENTED;
396				goto fail;
397			}
398		}
399	}
400	if (nv_error(nvout) != 0)
401		goto close;
402fail:
403	if (error != 0)
404		nv_add_int16(nvout, error, "error");
405
406	if (hast_proto_send(NULL, conn, nvout, NULL, 0) == -1)
407		pjdlog_errno(LOG_ERR, "Unable to send control response");
408close:
409	if (nvin != NULL)
410		nv_free(nvin);
411	if (nvout != NULL)
412		nv_free(nvout);
413	proto_close(conn);
414	cfg->hc_controlin = NULL;
415}
416
417/*
418 * Thread handles control requests from the parent.
419 */
420void *
421ctrl_thread(void *arg)
422{
423	struct hast_resource *res = arg;
424	struct nv *nvin, *nvout;
425	uint8_t cmd;
426
427	for (;;) {
428		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
429			if (sigexit_received)
430				pthread_exit(NULL);
431			pjdlog_errno(LOG_ERR,
432			    "Unable to receive control message");
433			kill(getpid(), SIGTERM);
434			pthread_exit(NULL);
435		}
436		cmd = nv_get_uint8(nvin, "cmd");
437		if (cmd == 0) {
438			pjdlog_error("Control message is missing 'cmd' field.");
439			nv_free(nvin);
440			continue;
441		}
442		nvout = nv_alloc();
443		switch (cmd) {
444		case CONTROL_STATUS:
445			if (res->hr_remotein != NULL &&
446			    res->hr_remoteout != NULL) {
447				nv_add_string(nvout, "complete", "status");
448			} else {
449				nv_add_string(nvout, "degraded", "status");
450			}
451			nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
452			    "extentsize");
453			if (res->hr_role == HAST_ROLE_PRIMARY) {
454				nv_add_uint32(nvout,
455				    (uint32_t)res->hr_keepdirty, "keepdirty");
456				nv_add_uint64(nvout,
457				    (uint64_t)(activemap_ndirty(res->hr_amp) *
458				    res->hr_extentsize), "dirty");
459			} else {
460				nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
461				nv_add_uint64(nvout, (uint64_t)0, "dirty");
462			}
463			nv_add_uint64(nvout, res->hr_stat_read, "stat_read");
464			nv_add_uint64(nvout, res->hr_stat_write, "stat_write");
465			nv_add_uint64(nvout, res->hr_stat_delete,
466			    "stat_delete");
467			nv_add_uint64(nvout, res->hr_stat_flush, "stat_flush");
468			nv_add_uint64(nvout, res->hr_stat_activemap_update,
469			    "stat_activemap_update");
470			nv_add_uint64(nvout, res->hr_stat_read_error,
471			    "stat_read_error");
472			nv_add_uint64(nvout, res->hr_stat_write_error +
473			    res->hr_stat_activemap_write_error,
474			    "stat_write_error");
475			nv_add_uint64(nvout, res->hr_stat_delete_error,
476			    "stat_delete_error");
477			nv_add_uint64(nvout, res->hr_stat_flush_error +
478			    res->hr_stat_activemap_flush_error,
479			    "stat_flush_error");
480			nv_add_int16(nvout, 0, "error");
481			break;
482		case CONTROL_RELOAD:
483			/*
484			 * When parent receives SIGHUP and discovers that
485			 * something related to us has changes, it sends reload
486			 * message to us.
487			 */
488			PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
489			primary_config_reload(res, nvin);
490			nv_add_int16(nvout, 0, "error");
491			break;
492		default:
493			nv_add_int16(nvout, EINVAL, "error");
494			break;
495		}
496		nv_free(nvin);
497		if (nv_error(nvout) != 0) {
498			pjdlog_error("Unable to create answer on control message.");
499			nv_free(nvout);
500			continue;
501		}
502		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) == -1) {
503			pjdlog_errno(LOG_ERR,
504			    "Unable to send reply to control message");
505		}
506		nv_free(nvout);
507	}
508	/* NOTREACHED */
509	return (NULL);
510}
511