hastctl.c revision 223976
1147997Srwatson/*-
2147997Srwatson * Copyright (c) 2009-2010 The FreeBSD Foundation
3147997Srwatson * All rights reserved.
4147997Srwatson *
5147997Srwatson * This software was developed by Pawel Jakub Dawidek under sponsorship from
6147997Srwatson * the FreeBSD Foundation.
7147997Srwatson *
8147997Srwatson * Redistribution and use in source and binary forms, with or without
9147997Srwatson * modification, are permitted provided that the following conditions
10147997Srwatson * are met:
11147997Srwatson * 1. Redistributions of source code must retain the above copyright
12147997Srwatson *    notice, this list of conditions and the following disclaimer.
13147997Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14147997Srwatson *    notice, this list of conditions and the following disclaimer in the
15147997Srwatson *    documentation and/or other materials provided with the distribution.
16147997Srwatson *
17147997Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18147997Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19147997Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20147997Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21147997Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22147997Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23147997Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24147997Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25147997Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26147997Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27147997Srwatson * SUCH DAMAGE.
28147997Srwatson */
29147997Srwatson
30147997Srwatson#include <sys/cdefs.h>
31147997Srwatson__FBSDID("$FreeBSD: head/sbin/hastctl/hastctl.c 223976 2011-07-13 05:56:51Z trociny $");
32147997Srwatson
33147997Srwatson#include <sys/param.h>
34147997Srwatson#include <sys/disk.h>
35147997Srwatson#include <sys/ioctl.h>
36147997Srwatson#include <sys/stat.h>
37147997Srwatson#include <sys/sysctl.h>
38147997Srwatson
39147997Srwatson#include <assert.h>
40147997Srwatson#include <err.h>
41147997Srwatson#include <errno.h>
42147997Srwatson#include <fcntl.h>
43147997Srwatson#include <libutil.h>
44147997Srwatson#include <limits.h>
45147997Srwatson#include <signal.h>
46147997Srwatson#include <stdio.h>
47147997Srwatson#include <stdlib.h>
48148627Srwatson#include <string.h>
49148627Srwatson#include <sysexits.h>
50148627Srwatson#include <unistd.h>
51148627Srwatson
52148627Srwatson#include <activemap.h>
53148627Srwatson
54148627Srwatson#include "hast.h"
55148627Srwatson#include "hast_proto.h"
56148627Srwatson#include "metadata.h"
57148627Srwatson#include "nv.h"
58148627Srwatson#include "pjdlog.h"
59#include "proto.h"
60#include "subr.h"
61
62/* Path to configuration file. */
63static const char *cfgpath = HAST_CONFIG;
64/* Hastd configuration. */
65static struct hastd_config *cfg;
66/* Control connection. */
67static struct proto_conn *controlconn;
68
69enum {
70	CMD_INVALID,
71	CMD_CREATE,
72	CMD_ROLE,
73	CMD_STATUS,
74	CMD_DUMP
75};
76
77static __dead2 void
78usage(void)
79{
80
81	fprintf(stderr,
82	    "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
83	    "\t\t[-m mediasize] name ...\n",
84	    getprogname());
85	fprintf(stderr,
86	    "       %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
87	    getprogname());
88	fprintf(stderr,
89	    "       %s status [-d] [-c config] [all | name ...]\n",
90	    getprogname());
91	fprintf(stderr,
92	    "       %s dump [-d] [-c config] [all | name ...]\n",
93	    getprogname());
94	exit(EX_USAGE);
95}
96
97static int
98create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
99    intmax_t keepdirty)
100{
101	unsigned char *buf;
102	size_t mapsize;
103	int ec;
104
105	ec = 0;
106	pjdlog_prefix_set("[%s] ", res->hr_name);
107
108	if (provinfo(res, true) < 0) {
109		ec = EX_NOINPUT;
110		goto end;
111	}
112	if (mediasize == 0)
113		mediasize = res->hr_local_mediasize;
114	else if (mediasize > res->hr_local_mediasize) {
115		pjdlog_error("Provided mediasize is larger than provider %s size.",
116		    res->hr_localpath);
117		ec = EX_DATAERR;
118		goto end;
119	}
120	if (!powerof2(res->hr_local_sectorsize)) {
121		pjdlog_error("Sector size of provider %s is not power of 2 (%u).",
122		    res->hr_localpath, res->hr_local_sectorsize);
123		ec = EX_DATAERR;
124		goto end;
125	}
126	if (extentsize == 0)
127		extentsize = HAST_EXTENTSIZE;
128	if (extentsize < res->hr_local_sectorsize) {
129		pjdlog_error("Extent size (%jd) is less than sector size (%u).",
130		    (intmax_t)extentsize, res->hr_local_sectorsize);
131		ec = EX_DATAERR;
132		goto end;
133	}
134	if ((extentsize % res->hr_local_sectorsize) != 0) {
135		pjdlog_error("Extent size (%jd) is not multiple of sector size (%u).",
136		    (intmax_t)extentsize, res->hr_local_sectorsize);
137		ec = EX_DATAERR;
138		goto end;
139	}
140	mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
141	    extentsize, res->hr_local_sectorsize);
142	if (keepdirty == 0)
143		keepdirty = HAST_KEEPDIRTY;
144	res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
145	res->hr_extentsize = extentsize;
146	res->hr_keepdirty = keepdirty;
147
148	res->hr_localoff = METADATA_SIZE + mapsize;
149
150	if (metadata_write(res) < 0) {
151		ec = EX_IOERR;
152		goto end;
153	}
154	buf = calloc(1, mapsize);
155	if (buf == NULL) {
156		pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
157		    mapsize);
158		ec = EX_TEMPFAIL;
159		goto end;
160	}
161	if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
162	    (ssize_t)mapsize) {
163		pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
164		    res->hr_localpath);
165		free(buf);
166		ec = EX_IOERR;
167		goto end;
168	}
169	free(buf);
170end:
171	if (res->hr_localfd >= 0)
172		close(res->hr_localfd);
173	pjdlog_prefix_set("%s", "");
174	return (ec);
175}
176
177static void
178control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
179    intmax_t keepdirty)
180{
181	struct hast_resource *res;
182	int ec, ii, ret;
183
184	/* Initialize the given resources. */
185	if (argc < 1)
186		usage();
187	ec = 0;
188	for (ii = 0; ii < argc; ii++) {
189		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
190			if (strcmp(argv[ii], res->hr_name) == 0)
191				break;
192		}
193		if (res == NULL) {
194			pjdlog_error("Unknown resource %s.", argv[ii]);
195			if (ec == 0)
196				ec = EX_DATAERR;
197			continue;
198		}
199		ret = create_one(res, mediasize, extentsize, keepdirty);
200		if (ret != 0 && ec == 0)
201			ec = ret;
202	}
203	exit(ec);
204}
205
206static int
207dump_one(struct hast_resource *res)
208{
209	int ret;
210
211	ret = metadata_read(res, false);
212	if (ret != 0)
213		return (ret);
214
215	printf("resource: %s\n", res->hr_name);
216	printf("    datasize: %ju (%NB)\n", (uintmax_t)res->hr_datasize,
217	    (intmax_t)res->hr_datasize);
218	printf("    extentsize: %d (%NB)\n", res->hr_extentsize,
219	    (intmax_t)res->hr_extentsize);
220	printf("    keepdirty: %d\n", res->hr_keepdirty);
221	printf("    localoff: %ju\n", (uintmax_t)res->hr_localoff);
222	printf("    resuid: %ju\n", (uintmax_t)res->hr_resuid);
223	printf("    localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
224	printf("    remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
225	printf("    prevrole: %s\n", role2str(res->hr_previous_role));
226
227	return (0);
228}
229
230static void
231control_dump(int argc, char *argv[])
232{
233	struct hast_resource *res;
234	int ec, ret;
235
236	/* Dump metadata of the given resource(s). */
237
238	ec = 0;
239	if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
240		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
241			ret = dump_one(res);
242			if (ret != 0 && ec == 0)
243				ec = ret;
244		}
245	} else {
246		int ii;
247
248		for (ii = 0; ii < argc; ii++) {
249			TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
250				if (strcmp(argv[ii], res->hr_name) == 0)
251					break;
252			}
253			if (res == NULL) {
254				pjdlog_error("Unknown resource %s.", argv[ii]);
255				if (ec == 0)
256					ec = EX_DATAERR;
257				continue;
258			}
259			ret = dump_one(res);
260			if (ret != 0 && ec == 0)
261				ec = ret;
262		}
263	}
264	exit(ec);
265}
266
267static int
268control_set_role(struct nv *nv, const char *newrole)
269{
270	const char *res, *oldrole;
271	unsigned int ii;
272	int error, ret;
273
274	ret = 0;
275
276	for (ii = 0; ; ii++) {
277		res = nv_get_string(nv, "resource%u", ii);
278		if (res == NULL)
279			break;
280		pjdlog_prefix_set("[%s] ", res);
281		error = nv_get_int16(nv, "error%u", ii);
282		if (error != 0) {
283			if (ret == 0)
284				ret = error;
285			pjdlog_warning("Received error %d from hastd.", error);
286			continue;
287		}
288		oldrole = nv_get_string(nv, "role%u", ii);
289		if (strcmp(oldrole, newrole) == 0)
290			pjdlog_debug(2, "Role unchanged (%s).", oldrole);
291		else {
292			pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
293			    newrole);
294		}
295	}
296	pjdlog_prefix_set("%s", "");
297	return (ret);
298}
299
300static int
301control_status(struct nv *nv)
302{
303	unsigned int ii;
304	const char *str;
305	int error, ret;
306
307	ret = 0;
308
309	for (ii = 0; ; ii++) {
310		str = nv_get_string(nv, "resource%u", ii);
311		if (str == NULL)
312			break;
313		printf("%s:\n", str);
314		error = nv_get_int16(nv, "error%u", ii);
315		if (error != 0) {
316			if (ret == 0)
317				ret = error;
318			printf("  error: %d\n", error);
319			continue;
320		}
321		printf("  role: %s\n", nv_get_string(nv, "role%u", ii));
322		printf("  provname: %s\n",
323		    nv_get_string(nv, "provname%u", ii));
324		printf("  localpath: %s\n",
325		    nv_get_string(nv, "localpath%u", ii));
326		printf("  extentsize: %u (%NB)\n",
327		    (unsigned int)nv_get_uint32(nv, "extentsize%u", ii),
328		    (intmax_t)nv_get_uint32(nv, "extentsize%u", ii));
329		printf("  keepdirty: %u\n",
330		    (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
331		printf("  remoteaddr: %s\n",
332		    nv_get_string(nv, "remoteaddr%u", ii));
333		str = nv_get_string(nv, "sourceaddr%u", ii);
334		if (str != NULL)
335			printf("  sourceaddr: %s\n", str);
336		printf("  replication: %s\n",
337		    nv_get_string(nv, "replication%u", ii));
338		str = nv_get_string(nv, "status%u", ii);
339		if (str != NULL)
340			printf("  status: %s\n", str);
341		printf("  dirty: %ju (%NB)\n",
342		    (uintmax_t)nv_get_uint64(nv, "dirty%u", ii),
343		    (intmax_t)nv_get_uint64(nv, "dirty%u", ii));
344		printf("  statistics:\n");
345		printf("    reads: %ju\n",
346		    (uint64_t)nv_get_uint64(nv, "stat_read%u", ii));
347		printf("    writes: %ju\n",
348		    (uint64_t)nv_get_uint64(nv, "stat_write%u", ii));
349		printf("    deletes: %ju\n",
350		    (uint64_t)nv_get_uint64(nv, "stat_delete%u", ii));
351		printf("    flushes: %ju\n",
352		    (uint64_t)nv_get_uint64(nv, "stat_flush%u", ii));
353		printf("    activemap updates: %ju\n",
354		    (uint64_t)nv_get_uint64(nv, "stat_activemap_update%u", ii));
355	}
356	return (ret);
357}
358
359int
360main(int argc, char *argv[])
361{
362	struct nv *nv;
363	int64_t mediasize, extentsize, keepdirty;
364	int cmd, debug, error, ii;
365	const char *optstr;
366
367	debug = 0;
368	mediasize = extentsize = keepdirty = 0;
369
370	if (argc == 1)
371		usage();
372
373	if (strcmp(argv[1], "create") == 0) {
374		cmd = CMD_CREATE;
375		optstr = "c:de:k:m:h";
376	} else if (strcmp(argv[1], "role") == 0) {
377		cmd = CMD_ROLE;
378		optstr = "c:dh";
379	} else if (strcmp(argv[1], "status") == 0) {
380		cmd = CMD_STATUS;
381		optstr = "c:dh";
382	} else if (strcmp(argv[1], "dump") == 0) {
383		cmd = CMD_DUMP;
384		optstr = "c:dh";
385	} else
386		usage();
387
388	argc--;
389	argv++;
390
391	for (;;) {
392		int ch;
393
394		ch = getopt(argc, argv, optstr);
395		if (ch == -1)
396			break;
397		switch (ch) {
398		case 'c':
399			cfgpath = optarg;
400			break;
401		case 'd':
402			debug++;
403			break;
404		case 'e':
405			if (expand_number(optarg, &extentsize) < 0)
406				errx(EX_USAGE, "Invalid extentsize");
407			break;
408		case 'k':
409			if (expand_number(optarg, &keepdirty) < 0)
410				errx(EX_USAGE, "Invalid keepdirty");
411			break;
412		case 'm':
413			if (expand_number(optarg, &mediasize) < 0)
414				errx(EX_USAGE, "Invalid mediasize");
415			break;
416		case 'h':
417		default:
418			usage();
419		}
420	}
421	argc -= optind;
422	argv += optind;
423
424	switch (cmd) {
425	case CMD_CREATE:
426	case CMD_ROLE:
427		if (argc == 0)
428			usage();
429		break;
430	}
431
432	pjdlog_init(PJDLOG_MODE_STD);
433	pjdlog_debug_set(debug);
434
435	cfg = yy_config_parse(cfgpath, true);
436	assert(cfg != NULL);
437
438	switch (cmd) {
439	case CMD_CREATE:
440		control_create(argc, argv, mediasize, extentsize, keepdirty);
441		/* NOTREACHED */
442		assert(!"What are we doing here?!");
443		break;
444	case CMD_DUMP:
445		/* Dump metadata from local component of the given resource. */
446		control_dump(argc, argv);
447		/* NOTREACHED */
448		assert(!"What are we doing here?!");
449		break;
450	case CMD_ROLE:
451		/* Change role for the given resources. */
452		if (argc < 2)
453			usage();
454		nv = nv_alloc();
455		nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
456		if (strcmp(argv[0], "init") == 0)
457			nv_add_uint8(nv, HAST_ROLE_INIT, "role");
458		else if (strcmp(argv[0], "primary") == 0)
459			nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
460		else if (strcmp(argv[0], "secondary") == 0)
461			nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
462		else
463			usage();
464		for (ii = 0; ii < argc - 1; ii++)
465			nv_add_string(nv, argv[ii + 1], "resource%d", ii);
466		break;
467	case CMD_STATUS:
468		/* Obtain status of the given resources. */
469		nv = nv_alloc();
470		nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
471		if (argc == 0)
472			nv_add_string(nv, "all", "resource%d", 0);
473		else {
474			for (ii = 0; ii < argc; ii++)
475				nv_add_string(nv, argv[ii], "resource%d", ii);
476		}
477		break;
478	default:
479		assert(!"Impossible command!");
480	}
481
482	/* Setup control connection... */
483	if (proto_client(NULL, cfg->hc_controladdr, &controlconn) < 0) {
484		pjdlog_exit(EX_OSERR,
485		    "Unable to setup control connection to %s",
486		    cfg->hc_controladdr);
487	}
488	/* ...and connect to hastd. */
489	if (proto_connect(controlconn, HAST_TIMEOUT) < 0) {
490		pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
491		    cfg->hc_controladdr);
492	}
493
494	if (drop_privs(NULL) != 0)
495		exit(EX_CONFIG);
496
497	/* Send the command to the server... */
498	if (hast_proto_send(NULL, controlconn, nv, NULL, 0) < 0) {
499		pjdlog_exit(EX_UNAVAILABLE,
500		    "Unable to send command to hastd via %s",
501		    cfg->hc_controladdr);
502	}
503	nv_free(nv);
504	/* ...and receive reply. */
505	if (hast_proto_recv_hdr(controlconn, &nv) < 0) {
506		pjdlog_exit(EX_UNAVAILABLE,
507		    "cannot receive reply from hastd via %s",
508		    cfg->hc_controladdr);
509	}
510
511	error = nv_get_int16(nv, "error");
512	if (error != 0) {
513		pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
514		    error);
515	}
516	nv_set_error(nv, 0);
517
518	switch (cmd) {
519	case CMD_ROLE:
520		error = control_set_role(nv, argv[0]);
521		break;
522	case CMD_STATUS:
523		error = control_status(nv);
524		break;
525	default:
526		assert(!"Impossible command!");
527	}
528
529	exit(error);
530}
531