2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
41 #include <activemap.h>
44 #include "hast_proto.h"
51 /* Path to configuration file. */
52 static const char *cfgpath = HAST_CONFIG;
53 /* Hastd configuration. */
54 static struct hastd_config *cfg;
55 /* Control connection. */
56 static struct proto_conn *controlconn;
72 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
73 "\t\t[-m mediasize] name ...\n",
76 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
79 " %s list [-d] [-c config] [all | name ...]\n",
82 " %s status [-d] [-c config] [all | name ...]\n",
85 " %s dump [-d] [-c config] [all | name ...]\n",
91 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
99 pjdlog_prefix_set("[%s] ", res->hr_name);
101 if (provinfo(res, true) == -1) {
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.",
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);
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);
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);
133 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
134 extentsize, res->hr_local_sectorsize);
136 keepdirty = HAST_KEEPDIRTY;
137 res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
138 res->hr_extentsize = extentsize;
139 res->hr_keepdirty = keepdirty;
141 res->hr_localoff = METADATA_SIZE + mapsize;
143 if (metadata_write(res) == -1) {
147 buf = calloc(1, mapsize);
149 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
154 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
156 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
164 if (res->hr_localfd >= 0)
165 close(res->hr_localfd);
166 pjdlog_prefix_set("%s", "");
171 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
174 struct hast_resource *res;
177 /* Initialize the given resources. */
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)
187 pjdlog_error("Unknown resource %s.", argv[ii]);
192 ret = create_one(res, mediasize, extentsize, keepdirty);
193 if (ret != 0 && ec == 0)
200 dump_one(struct hast_resource *res)
204 ret = metadata_read(res, false);
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));
224 control_dump(int argc, char *argv[])
226 struct hast_resource *res;
229 /* Dump metadata of the given resource(s). */
232 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
233 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
235 if (ret != 0 && ec == 0)
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)
247 pjdlog_error("Unknown resource %s.", argv[ii]);
253 if (ret != 0 && ec == 0)
261 control_set_role(struct nv *nv, const char *newrole)
263 const char *res, *oldrole;
269 for (ii = 0; ; ii++) {
270 res = nv_get_string(nv, "resource%u", ii);
273 pjdlog_prefix_set("[%s] ", res);
274 error = nv_get_int16(nv, "error%u", ii);
278 pjdlog_warning("Received error %d from hastd.", error);
281 oldrole = nv_get_string(nv, "role%u", ii);
282 if (strcmp(oldrole, newrole) == 0)
283 pjdlog_debug(2, "Role unchanged (%s).", oldrole);
285 pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
289 pjdlog_prefix_set("%s", "");
294 control_status(struct nv *nv)
302 for (ii = 0; ; ii++) {
303 str = nv_get_string(nv, "resource%u", ii);
306 printf("%s:\n", str);
307 error = nv_get_int16(nv, "error%u", ii);
311 printf(" error: %d\n", error);
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);
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);
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));
359 main(int argc, char *argv[])
362 int64_t mediasize, extentsize, keepdirty;
363 int cmd, debug, error, ii;
367 mediasize = extentsize = keepdirty = 0;
372 if (strcmp(argv[1], "create") == 0) {
374 optstr = "c:de:k:m:h";
375 } else if (strcmp(argv[1], "role") == 0) {
378 } else if (strcmp(argv[1], "list") == 0) {
381 } else if (strcmp(argv[1], "status") == 0) {
384 } else if (strcmp(argv[1], "dump") == 0) {
396 ch = getopt(argc, argv, optstr);
407 if (expand_number(optarg, &extentsize) == -1)
408 errx(EX_USAGE, "Invalid extentsize");
411 if (expand_number(optarg, &keepdirty) == -1)
412 errx(EX_USAGE, "Invalid keepdirty");
415 if (expand_number(optarg, &mediasize) == -1)
416 errx(EX_USAGE, "Invalid mediasize");
434 pjdlog_init(PJDLOG_MODE_STD);
435 pjdlog_debug_set(debug);
437 cfg = yy_config_parse(cfgpath, true);
438 PJDLOG_ASSERT(cfg != NULL);
442 control_create(argc, argv, mediasize, extentsize, keepdirty);
444 PJDLOG_ABORT("What are we doing here?!");
447 /* Dump metadata from local component of the given resource. */
448 control_dump(argc, argv);
450 PJDLOG_ABORT("What are we doing here?!");
453 /* Change role for the given resources. */
457 nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
458 if (strcmp(argv[0], "init") == 0)
459 nv_add_uint8(nv, HAST_ROLE_INIT, "role");
460 else if (strcmp(argv[0], "primary") == 0)
461 nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
462 else if (strcmp(argv[0], "secondary") == 0)
463 nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
466 for (ii = 0; ii < argc - 1; ii++)
467 nv_add_string(nv, argv[ii + 1], "resource%d", ii);
471 /* Obtain status of the given resources. */
473 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
475 nv_add_string(nv, "all", "resource%d", 0);
477 for (ii = 0; ii < argc; ii++)
478 nv_add_string(nv, argv[ii], "resource%d", ii);
482 PJDLOG_ABORT("Impossible command!");
485 /* Setup control connection... */
486 if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
487 pjdlog_exit(EX_OSERR,
488 "Unable to setup control connection to %s",
489 cfg->hc_controladdr);
491 /* ...and connect to hastd. */
492 if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
493 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
494 cfg->hc_controladdr);
497 if (drop_privs(NULL) != 0)
500 /* Send the command to the server... */
501 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
502 pjdlog_exit(EX_UNAVAILABLE,
503 "Unable to send command to hastd via %s",
504 cfg->hc_controladdr);
507 /* ...and receive reply. */
508 if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
509 pjdlog_exit(EX_UNAVAILABLE,
510 "cannot receive reply from hastd via %s",
511 cfg->hc_controladdr);
514 error = nv_get_int16(nv, "error");
516 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
523 error = control_set_role(nv, argv[0]);
527 error = control_status(nv);
530 PJDLOG_ABORT("Impossible command!");