hastctl.c revision 252776
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/hastctl/hastctl.c 252776 2013-07-05 08:16:40Z marck $");
32
33#include <sys/param.h>
34
35#include <err.h>
36#include <libutil.h>
37#include <stdio.h>
38#include <string.h>
39#include <unistd.h>
40
41#include <activemap.h>
42
43#include "hast.h"
44#include "hast_proto.h"
45#include "metadata.h"
46#include "nv.h"
47#include "pjdlog.h"
48#include "proto.h"
49#include "subr.h"
50
51/* Path to configuration file. */
52static const char *cfgpath = HAST_CONFIG;
53/* Hastd configuration. */
54static struct hastd_config *cfg;
55/* Control connection. */
56static struct proto_conn *controlconn;
57
58enum {
59	CMD_INVALID,
60	CMD_CREATE,
61	CMD_ROLE,
62	CMD_STATUS,
63	CMD_DUMP,
64	CMD_LIST
65};
66
67static __dead2 void
68usage(void)
69{
70
71	fprintf(stderr,
72	    "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
73	    "\t\t[-m mediasize] name ...\n",
74	    getprogname());
75	fprintf(stderr,
76	    "       %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
77	    getprogname());
78	fprintf(stderr,
79	    "       %s list [-d] [-c config] [all | name ...]\n",
80	    getprogname());
81	fprintf(stderr,
82	    "       %s status [-d] [-c config] [all | name ...]\n",
83	    getprogname());
84	fprintf(stderr,
85	    "       %s dump [-d] [-c config] [all | name ...]\n",
86	    getprogname());
87	exit(EX_USAGE);
88}
89
90static int
91create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
92    intmax_t keepdirty)
93{
94	unsigned char *buf;
95	size_t mapsize;
96	int ec;
97
98	ec = 0;
99	pjdlog_prefix_set("[%s] ", res->hr_name);
100
101	if (provinfo(res, true) == -1) {
102		ec = EX_NOINPUT;
103		goto end;
104	}
105	if (mediasize == 0)
106		mediasize = res->hr_local_mediasize;
107	else if (mediasize > res->hr_local_mediasize) {
108		pjdlog_error("Provided mediasize is larger than provider %s size.",
109		    res->hr_localpath);
110		ec = EX_DATAERR;
111		goto end;
112	}
113	if (!powerof2(res->hr_local_sectorsize)) {
114		pjdlog_error("Sector size of provider %s is not power of 2 (%u).",
115		    res->hr_localpath, res->hr_local_sectorsize);
116		ec = EX_DATAERR;
117		goto end;
118	}
119	if (extentsize == 0)
120		extentsize = HAST_EXTENTSIZE;
121	if (extentsize < res->hr_local_sectorsize) {
122		pjdlog_error("Extent size (%jd) is less than sector size (%u).",
123		    (intmax_t)extentsize, res->hr_local_sectorsize);
124		ec = EX_DATAERR;
125		goto end;
126	}
127	if ((extentsize % res->hr_local_sectorsize) != 0) {
128		pjdlog_error("Extent size (%jd) is not multiple of sector size (%u).",
129		    (intmax_t)extentsize, res->hr_local_sectorsize);
130		ec = EX_DATAERR;
131		goto end;
132	}
133	mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
134	    extentsize, res->hr_local_sectorsize);
135	if (keepdirty == 0)
136		keepdirty = HAST_KEEPDIRTY;
137	res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
138	res->hr_extentsize = extentsize;
139	res->hr_keepdirty = keepdirty;
140
141	res->hr_localoff = METADATA_SIZE + mapsize;
142
143	if (metadata_write(res) == -1) {
144		ec = EX_IOERR;
145		goto end;
146	}
147	buf = calloc(1, mapsize);
148	if (buf == NULL) {
149		pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
150		    mapsize);
151		ec = EX_TEMPFAIL;
152		goto end;
153	}
154	if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
155	    (ssize_t)mapsize) {
156		pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
157		    res->hr_localpath);
158		free(buf);
159		ec = EX_IOERR;
160		goto end;
161	}
162	free(buf);
163end:
164	if (res->hr_localfd >= 0)
165		close(res->hr_localfd);
166	pjdlog_prefix_set("%s", "");
167	return (ec);
168}
169
170static void
171control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
172    intmax_t keepdirty)
173{
174	struct hast_resource *res;
175	int ec, ii, ret;
176
177	/* Initialize the given resources. */
178	if (argc < 1)
179		usage();
180	ec = 0;
181	for (ii = 0; ii < argc; ii++) {
182		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
183			if (strcmp(argv[ii], res->hr_name) == 0)
184				break;
185		}
186		if (res == NULL) {
187			pjdlog_error("Unknown resource %s.", argv[ii]);
188			if (ec == 0)
189				ec = EX_DATAERR;
190			continue;
191		}
192		ret = create_one(res, mediasize, extentsize, keepdirty);
193		if (ret != 0 && ec == 0)
194			ec = ret;
195	}
196	exit(ec);
197}
198
199static int
200dump_one(struct hast_resource *res)
201{
202	int ret;
203
204	ret = metadata_read(res, false);
205	if (ret != 0)
206		return (ret);
207
208	printf("resource: %s\n", res->hr_name);
209	printf("    datasize: %ju (%NB)\n", (uintmax_t)res->hr_datasize,
210	    (intmax_t)res->hr_datasize);
211	printf("    extentsize: %d (%NB)\n", res->hr_extentsize,
212	    (intmax_t)res->hr_extentsize);
213	printf("    keepdirty: %d\n", res->hr_keepdirty);
214	printf("    localoff: %ju\n", (uintmax_t)res->hr_localoff);
215	printf("    resuid: %ju\n", (uintmax_t)res->hr_resuid);
216	printf("    localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
217	printf("    remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
218	printf("    prevrole: %s\n", role2str(res->hr_previous_role));
219
220	return (0);
221}
222
223static void
224control_dump(int argc, char *argv[])
225{
226	struct hast_resource *res;
227	int ec, ret;
228
229	/* Dump metadata of the given resource(s). */
230
231	ec = 0;
232	if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
233		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
234			ret = dump_one(res);
235			if (ret != 0 && ec == 0)
236				ec = ret;
237		}
238	} else {
239		int ii;
240
241		for (ii = 0; ii < argc; ii++) {
242			TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
243				if (strcmp(argv[ii], res->hr_name) == 0)
244					break;
245			}
246			if (res == NULL) {
247				pjdlog_error("Unknown resource %s.", argv[ii]);
248				if (ec == 0)
249					ec = EX_DATAERR;
250				continue;
251			}
252			ret = dump_one(res);
253			if (ret != 0 && ec == 0)
254				ec = ret;
255		}
256	}
257	exit(ec);
258}
259
260static int
261control_set_role(struct nv *nv, const char *newrole)
262{
263	const char *res, *oldrole;
264	unsigned int ii;
265	int error, ret;
266
267	ret = 0;
268
269	for (ii = 0; ; ii++) {
270		res = nv_get_string(nv, "resource%u", ii);
271		if (res == NULL)
272			break;
273		pjdlog_prefix_set("[%s] ", res);
274		error = nv_get_int16(nv, "error%u", ii);
275		if (error != 0) {
276			if (ret == 0)
277				ret = error;
278			pjdlog_warning("Received error %d from hastd.", error);
279			continue;
280		}
281		oldrole = nv_get_string(nv, "role%u", ii);
282		if (strcmp(oldrole, newrole) == 0)
283			pjdlog_debug(2, "Role unchanged (%s).", oldrole);
284		else {
285			pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
286			    newrole);
287		}
288	}
289	pjdlog_prefix_set("%s", "");
290	return (ret);
291}
292
293static int
294control_list(struct nv *nv)
295{
296	unsigned int ii;
297	const char *str;
298	int error, ret;
299
300	ret = 0;
301
302	for (ii = 0; ; ii++) {
303		str = nv_get_string(nv, "resource%u", ii);
304		if (str == NULL)
305			break;
306		printf("%s:\n", str);
307		error = nv_get_int16(nv, "error%u", ii);
308		if (error != 0) {
309			if (ret == 0)
310				ret = error;
311			printf("  error: %d\n", error);
312			continue;
313		}
314		printf("  role: %s\n", nv_get_string(nv, "role%u", ii));
315		printf("  provname: %s\n",
316		    nv_get_string(nv, "provname%u", ii));
317		printf("  localpath: %s\n",
318		    nv_get_string(nv, "localpath%u", ii));
319		printf("  extentsize: %u (%NB)\n",
320		    (unsigned int)nv_get_uint32(nv, "extentsize%u", ii),
321		    (intmax_t)nv_get_uint32(nv, "extentsize%u", ii));
322		printf("  keepdirty: %u\n",
323		    (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
324		printf("  remoteaddr: %s\n",
325		    nv_get_string(nv, "remoteaddr%u", ii));
326		str = nv_get_string(nv, "sourceaddr%u", ii);
327		if (str != NULL)
328			printf("  sourceaddr: %s\n", str);
329		printf("  replication: %s\n",
330		    nv_get_string(nv, "replication%u", ii));
331		str = nv_get_string(nv, "status%u", ii);
332		if (str != NULL)
333			printf("  status: %s\n", str);
334		printf("  dirty: %ju (%NB)\n",
335		    (uintmax_t)nv_get_uint64(nv, "dirty%u", ii),
336		    (intmax_t)nv_get_uint64(nv, "dirty%u", ii));
337		printf("  statistics:\n");
338		printf("    reads: %ju\n",
339		    (uintmax_t)nv_get_uint64(nv, "stat_read%u", ii));
340		printf("    writes: %ju\n",
341		    (uintmax_t)nv_get_uint64(nv, "stat_write%u", ii));
342		printf("    deletes: %ju\n",
343		    (uintmax_t)nv_get_uint64(nv, "stat_delete%u", ii));
344		printf("    flushes: %ju\n",
345		    (uintmax_t)nv_get_uint64(nv, "stat_flush%u", ii));
346		printf("    activemap updates: %ju\n",
347		    (uintmax_t)nv_get_uint64(nv, "stat_activemap_update%u", ii));
348		printf("    local errors: "
349		    "read: %ju, write: %ju, delete: %ju, flush: %ju\n",
350		    (uintmax_t)nv_get_uint64(nv, "stat_read_error%u", ii),
351		    (uintmax_t)nv_get_uint64(nv, "stat_write_error%u", ii),
352		    (uintmax_t)nv_get_uint64(nv, "stat_delete_error%u", ii),
353		    (uintmax_t)nv_get_uint64(nv, "stat_flush_error%u", ii));
354	}
355	return (ret);
356}
357
358static int
359control_status(struct nv *nv)
360{
361	unsigned int ii;
362	const char *str;
363	int error, hprinted, ret;
364
365	hprinted = 0;
366	ret = 0;
367
368	for (ii = 0; ; ii++) {
369		str = nv_get_string(nv, "resource%u", ii);
370		if (str == NULL)
371			break;
372		if (!hprinted) {
373			printf("Name\tStatus\t Role\t\tComponents\n");
374			hprinted = 1;
375		}
376		printf("%s\t", str);
377		error = nv_get_int16(nv, "error%u", ii);
378		if (error != 0) {
379			if (ret == 0)
380				ret = error;
381			printf("ERR%d\n", error);
382			continue;
383		}
384		str = nv_get_string(nv, "status%u", ii);
385		printf("%-9s", (str != NULL) ? str : "-");
386		printf("%-15s", nv_get_string(nv, "role%u", ii));
387		printf("%s\t",
388		    nv_get_string(nv, "localpath%u", ii));
389		printf("%s\n",
390		    nv_get_string(nv, "remoteaddr%u", ii));
391	}
392	return (ret);
393}
394
395int
396main(int argc, char *argv[])
397{
398	struct nv *nv;
399	int64_t mediasize, extentsize, keepdirty;
400	int cmd, debug, error, ii;
401	const char *optstr;
402
403	debug = 0;
404	mediasize = extentsize = keepdirty = 0;
405
406	if (argc == 1)
407		usage();
408
409	if (strcmp(argv[1], "create") == 0) {
410		cmd = CMD_CREATE;
411		optstr = "c:de:k:m:h";
412	} else if (strcmp(argv[1], "role") == 0) {
413		cmd = CMD_ROLE;
414		optstr = "c:dh";
415	} else if (strcmp(argv[1], "list") == 0) {
416		cmd = CMD_LIST;
417		optstr = "c:dh";
418	} else if (strcmp(argv[1], "status") == 0) {
419		cmd = CMD_STATUS;
420		optstr = "c:dh";
421	} else if (strcmp(argv[1], "dump") == 0) {
422		cmd = CMD_DUMP;
423		optstr = "c:dh";
424	} else
425		usage();
426
427	argc--;
428	argv++;
429
430	for (;;) {
431		int ch;
432
433		ch = getopt(argc, argv, optstr);
434		if (ch == -1)
435			break;
436		switch (ch) {
437		case 'c':
438			cfgpath = optarg;
439			break;
440		case 'd':
441			debug++;
442			break;
443		case 'e':
444			if (expand_number(optarg, &extentsize) == -1)
445				errx(EX_USAGE, "Invalid extentsize");
446			break;
447		case 'k':
448			if (expand_number(optarg, &keepdirty) == -1)
449				errx(EX_USAGE, "Invalid keepdirty");
450			break;
451		case 'm':
452			if (expand_number(optarg, &mediasize) == -1)
453				errx(EX_USAGE, "Invalid mediasize");
454			break;
455		case 'h':
456		default:
457			usage();
458		}
459	}
460	argc -= optind;
461	argv += optind;
462
463	switch (cmd) {
464	case CMD_CREATE:
465	case CMD_ROLE:
466		if (argc == 0)
467			usage();
468		break;
469	}
470
471	pjdlog_init(PJDLOG_MODE_STD);
472	pjdlog_debug_set(debug);
473
474	cfg = yy_config_parse(cfgpath, true);
475	PJDLOG_ASSERT(cfg != NULL);
476
477	switch (cmd) {
478	case CMD_CREATE:
479		control_create(argc, argv, mediasize, extentsize, keepdirty);
480		/* NOTREACHED */
481		PJDLOG_ABORT("What are we doing here?!");
482		break;
483	case CMD_DUMP:
484		/* Dump metadata from local component of the given resource. */
485		control_dump(argc, argv);
486		/* NOTREACHED */
487		PJDLOG_ABORT("What are we doing here?!");
488		break;
489	case CMD_ROLE:
490		/* Change role for the given resources. */
491		if (argc < 2)
492			usage();
493		nv = nv_alloc();
494		nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
495		if (strcmp(argv[0], "init") == 0)
496			nv_add_uint8(nv, HAST_ROLE_INIT, "role");
497		else if (strcmp(argv[0], "primary") == 0)
498			nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
499		else if (strcmp(argv[0], "secondary") == 0)
500			nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
501		else
502			usage();
503		for (ii = 0; ii < argc - 1; ii++)
504			nv_add_string(nv, argv[ii + 1], "resource%d", ii);
505		break;
506	case CMD_LIST:
507	case CMD_STATUS:
508		/* Obtain status of the given resources. */
509		nv = nv_alloc();
510		nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
511		if (argc == 0)
512			nv_add_string(nv, "all", "resource%d", 0);
513		else {
514			for (ii = 0; ii < argc; ii++)
515				nv_add_string(nv, argv[ii], "resource%d", ii);
516		}
517		break;
518	default:
519		PJDLOG_ABORT("Impossible command!");
520	}
521
522	/* Setup control connection... */
523	if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
524		pjdlog_exit(EX_OSERR,
525		    "Unable to setup control connection to %s",
526		    cfg->hc_controladdr);
527	}
528	/* ...and connect to hastd. */
529	if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
530		pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
531		    cfg->hc_controladdr);
532	}
533
534	if (drop_privs(NULL) != 0)
535		exit(EX_CONFIG);
536
537	/* Send the command to the server... */
538	if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
539		pjdlog_exit(EX_UNAVAILABLE,
540		    "Unable to send command to hastd via %s",
541		    cfg->hc_controladdr);
542	}
543	nv_free(nv);
544	/* ...and receive reply. */
545	if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
546		pjdlog_exit(EX_UNAVAILABLE,
547		    "cannot receive reply from hastd via %s",
548		    cfg->hc_controladdr);
549	}
550
551	error = nv_get_int16(nv, "error");
552	if (error != 0) {
553		pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
554		    error);
555	}
556	nv_set_error(nv, 0);
557
558	switch (cmd) {
559	case CMD_ROLE:
560		error = control_set_role(nv, argv[0]);
561		break;
562	case CMD_LIST:
563		error = control_list(nv);
564		break;
565	case CMD_STATUS:
566		error = control_status(nv);
567		break;
568	default:
569		PJDLOG_ABORT("Impossible command!");
570	}
571
572	exit(error);
573}
574