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>
35 #include <sys/ioctl.h>
37 #include <sys/sysctl.h>
52 #include <activemap.h>
55 #include "hast_proto.h"
62 /* Path to configuration file. */
63 static const char *cfgpath = HAST_CONFIG;
64 /* Hastd configuration. */
65 static struct hastd_config *cfg;
66 /* Control connection. */
67 static struct proto_conn *controlconn;
82 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
83 "\t\t[-m mediasize] name ...\n",
86 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
89 " %s status [-d] [-c config] [all | name ...]\n",
92 " %s dump [-d] [-c config] [all | name ...]\n",
98 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
106 pjdlog_prefix_set("[%s] ", res->hr_name);
108 if (provinfo(res, true) < 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.",
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);
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);
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);
140 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
141 extentsize, res->hr_local_sectorsize);
143 keepdirty = HAST_KEEPDIRTY;
144 res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
145 res->hr_extentsize = extentsize;
146 res->hr_keepdirty = keepdirty;
148 res->hr_localoff = METADATA_SIZE + mapsize;
150 if (metadata_write(res) < 0) {
154 buf = calloc(1, mapsize);
156 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
161 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
163 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
171 if (res->hr_localfd >= 0)
172 close(res->hr_localfd);
173 pjdlog_prefix_set("%s", "");
178 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
181 struct hast_resource *res;
184 /* Initialize the given resources. */
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)
194 pjdlog_error("Unknown resource %s.", argv[ii]);
199 ret = create_one(res, mediasize, extentsize, keepdirty);
200 if (ret != 0 && ec == 0)
207 dump_one(struct hast_resource *res)
211 ret = metadata_read(res, false);
215 printf("resource: %s\n", res->hr_name);
216 printf(" datasize: %ju\n", (uintmax_t)res->hr_datasize);
217 printf(" extentsize: %d\n", res->hr_extentsize);
218 printf(" keepdirty: %d\n", res->hr_keepdirty);
219 printf(" localoff: %ju\n", (uintmax_t)res->hr_localoff);
220 printf(" resuid: %ju\n", (uintmax_t)res->hr_resuid);
221 printf(" localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
222 printf(" remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
223 printf(" prevrole: %s\n", role2str(res->hr_previous_role));
229 control_dump(int argc, char *argv[])
231 struct hast_resource *res;
234 /* Dump metadata of the given resource(s). */
237 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
238 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
240 if (ret != 0 && ec == 0)
246 for (ii = 0; ii < argc; ii++) {
247 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
248 if (strcmp(argv[ii], res->hr_name) == 0)
252 pjdlog_error("Unknown resource %s.", argv[ii]);
258 if (ret != 0 && ec == 0)
266 control_set_role(struct nv *nv, const char *newrole)
268 const char *res, *oldrole;
274 for (ii = 0; ; ii++) {
275 res = nv_get_string(nv, "resource%u", ii);
278 pjdlog_prefix_set("[%s] ", res);
279 error = nv_get_int16(nv, "error%u", ii);
283 pjdlog_warning("Received error %d from hastd.", error);
286 oldrole = nv_get_string(nv, "role%u", ii);
287 if (strcmp(oldrole, newrole) == 0)
288 pjdlog_debug(2, "Role unchanged (%s).", oldrole);
290 pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
294 pjdlog_prefix_set("%s", "");
299 control_status(struct nv *nv)
307 for (ii = 0; ; ii++) {
308 str = nv_get_string(nv, "resource%u", ii);
311 printf("%s:\n", str);
312 error = nv_get_int16(nv, "error%u", ii);
316 printf(" error: %d\n", error);
319 printf(" role: %s\n", nv_get_string(nv, "role%u", ii));
320 printf(" provname: %s\n",
321 nv_get_string(nv, "provname%u", ii));
322 printf(" localpath: %s\n",
323 nv_get_string(nv, "localpath%u", ii));
324 printf(" extentsize: %u\n",
325 (unsigned int)nv_get_uint32(nv, "extentsize%u", ii));
326 printf(" keepdirty: %u\n",
327 (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
328 printf(" remoteaddr: %s\n",
329 nv_get_string(nv, "remoteaddr%u", ii));
330 printf(" replication: %s\n",
331 nv_get_string(nv, "replication%u", ii));
332 str = nv_get_string(nv, "status%u", ii);
334 printf(" status: %s\n", str);
335 printf(" dirty: %ju bytes\n",
336 (uintmax_t)nv_get_uint64(nv, "dirty%u", ii));
342 numfromstr(const char *str, intmax_t *nump)
350 num = strtoimax(str, &suffix, 0);
351 if (errno == 0 && *suffix != '\0')
361 main(int argc, char *argv[])
364 intmax_t mediasize, extentsize, keepdirty;
365 int cmd, debug, error, ii;
369 mediasize = extentsize = keepdirty = 0;
374 if (strcmp(argv[1], "create") == 0) {
376 optstr = "c:de:k:m:h";
377 } else if (strcmp(argv[1], "role") == 0) {
380 } else if (strcmp(argv[1], "status") == 0) {
383 } else if (strcmp(argv[1], "dump") == 0) {
395 ch = getopt(argc, argv, optstr);
406 if (numfromstr(optarg, &extentsize) < 0)
407 err(1, "Invalid extentsize");
410 if (numfromstr(optarg, &keepdirty) < 0)
411 err(1, "Invalid keepdirty");
414 if (numfromstr(optarg, &mediasize) < 0)
415 err(1, "Invalid mediasize");
433 pjdlog_debug_set(debug);
435 cfg = yy_config_parse(cfgpath, true);
440 control_create(argc, argv, mediasize, extentsize, keepdirty);
442 assert(!"What are we doing here?!");
445 /* Dump metadata from local component of the given resource. */
446 control_dump(argc, argv);
448 assert(!"What are we doing here?!");
451 /* Change role for the given resources. */
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");
464 for (ii = 0; ii < argc - 1; ii++)
465 nv_add_string(nv, argv[ii + 1], "resource%d", ii);
468 /* Obtain status of the given resources. */
470 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
472 nv_add_string(nv, "all", "resource%d", 0);
474 for (ii = 0; ii < argc; ii++)
475 nv_add_string(nv, argv[ii], "resource%d", ii);
479 assert(!"Impossible role!");
482 /* Setup control connection... */
483 if (proto_client(cfg->hc_controladdr, &controlconn) < 0) {
484 pjdlog_exit(EX_OSERR,
485 "Unable to setup control connection to %s",
486 cfg->hc_controladdr);
488 /* ...and connect to hastd. */
489 if (proto_connect(controlconn) < 0) {
490 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
491 cfg->hc_controladdr);
493 /* Send the command to the server... */
494 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) < 0) {
495 pjdlog_exit(EX_UNAVAILABLE,
496 "Unable to send command to hastd via %s",
497 cfg->hc_controladdr);
500 /* ...and receive reply. */
501 if (hast_proto_recv(NULL, controlconn, &nv, NULL, 0) < 0) {
502 pjdlog_exit(EX_UNAVAILABLE,
503 "cannot receive reply from hastd via %s",
504 cfg->hc_controladdr);
507 error = nv_get_int16(nv, "error");
509 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
516 error = control_set_role(nv, argv[0]);
519 error = control_status(nv);
522 assert(!"Impossible role!");