diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index d01528bf..4da70545 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -31,6 +31,7 @@ #include "btree.h" #include "leaf_item_hash.h" #include "blkid.h" +#include "quorum.h" /* @@ -139,7 +140,6 @@ static int do_mkfs(struct mkfs_args *args) int data_fd = -1; char uuid_str[37]; void *zeros = NULL; - char *indent; u64 blkno; u64 meta_size; u64 data_size; @@ -368,20 +368,8 @@ static int do_mkfs(struct mkfs_args *args) SIZE_ARGS(le64_to_cpu(super->total_data_blocks), SCOUTFS_BLOCK_SM_SIZE)); - indent = ""; - for (i = 0; i < SCOUTFS_QUORUM_MAX_SLOTS; i++) { - struct scoutfs_quorum_slot *sl = &super->qconf.slots[i]; - struct in_addr in; - - if (sl->addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) - continue; - - in.s_addr = htonl(le32_to_cpu(sl->addr.v4.addr)); - printf("%s%u: %s:%u", indent, - i, inet_ntoa(in), le16_to_cpu(sl->addr.v4.port)); - indent = "\n "; - } - printf("\n"); + print_quorum_slots(super->qconf.slots, array_size(super->qconf.slots), + " "); ret = 0; out: @@ -398,45 +386,6 @@ out: return ret; } -static bool valid_quorum_slots(struct scoutfs_quorum_slot *slots) -{ - struct in_addr in; - bool valid = true; - char *addr; - int i; - int j; - - for (i = 0; i < SCOUTFS_QUORUM_MAX_SLOTS; i++) { - if (slots[i].addr.v4.family == cpu_to_le16(SCOUTFS_AF_NONE)) - continue; - - if (slots[i].addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) { - fprintf(stderr, "quorum slot nr %u has invalid family %u\n", - i, le16_to_cpu(slots[i].addr.v4.family)); - valid = false; - } - - for (j = i + 1; j < SCOUTFS_QUORUM_MAX_SLOTS; j++) { - if (slots[i].addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) - continue; - - if (slots[i].addr.v4.addr == slots[j].addr.v4.addr && - slots[i].addr.v4.port == slots[j].addr.v4.port) { - - in.s_addr = - htonl(le32_to_cpu(slots[i].addr.v4.addr)); - addr = inet_ntoa(in); - fprintf(stderr, "quorum slot nr %u and %u have the same address %s:%u\n", - i, j, addr, - le16_to_cpu(slots[i].addr.v4.port)); - valid = false; - } - } - } - - return valid; -} - static int parse_opt(int key, char *arg, struct argp_state *state) { struct mkfs_args *args = state->input; diff --git a/utils/src/quorum.c b/utils/src/quorum.c index 092c4f37..935114ab 100644 --- a/utils/src/quorum.c +++ b/utils/src/quorum.c @@ -1,3 +1,7 @@ +#include +#include +#include + #include "sparse.h" #include "util.h" #include "format.h" @@ -8,3 +12,68 @@ bool quorum_slot_present(struct scoutfs_super_block *super, int i) { return super->qconf.slots[i].addr.v4.family == cpu_to_le16(SCOUTFS_AF_IPV4); } + +bool valid_quorum_slots(struct scoutfs_quorum_slot *slots) +{ + struct in_addr in; + bool valid = true; + char *addr; + int i; + int j; + + for (i = 0; i < SCOUTFS_QUORUM_MAX_SLOTS; i++) { + if (slots[i].addr.v4.family == cpu_to_le16(SCOUTFS_AF_NONE)) + continue; + + if (slots[i].addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) { + fprintf(stderr, "quorum slot nr %u has invalid family %u\n", + i, le16_to_cpu(slots[i].addr.v4.family)); + valid = false; + } + + for (j = i + 1; j < SCOUTFS_QUORUM_MAX_SLOTS; j++) { + if (slots[i].addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) + continue; + + if (slots[i].addr.v4.addr == slots[j].addr.v4.addr && + slots[i].addr.v4.port == slots[j].addr.v4.port) { + + in.s_addr = + htonl(le32_to_cpu(slots[i].addr.v4.addr)); + addr = inet_ntoa(in); + fprintf(stderr, "quorum slot nr %u and %u have the same address %s:%u\n", + i, j, addr, + le16_to_cpu(slots[i].addr.v4.port)); + valid = false; + } + } + } + + return valid; +} + +/* + * Print quorum slots to stdout, a line at a time. The first line is + * not indented and the rest of the lines use the indent string from the + * caller. + */ +void print_quorum_slots(struct scoutfs_quorum_slot *slots, int nr, char *indent) +{ + struct scoutfs_quorum_slot *sl; + struct in_addr in; + bool first = true; + int i; + + for (i = 0, sl = slots; i < SCOUTFS_QUORUM_MAX_SLOTS; i++, sl++) { + + if (sl->addr.v4.family != cpu_to_le16(SCOUTFS_AF_IPV4)) + continue; + + in.s_addr = htonl(le32_to_cpu(sl->addr.v4.addr)); + printf("%s%u: %s:%u\n", first ? "" : indent, + i, inet_ntoa(in), le16_to_cpu(sl->addr.v4.port)); + + first = false; + } +} + diff --git a/utils/src/quorum.h b/utils/src/quorum.h index 1297dce1..5e467bf2 100644 --- a/utils/src/quorum.h +++ b/utils/src/quorum.h @@ -4,5 +4,7 @@ #include bool quorum_slot_present(struct scoutfs_super_block *super, int i); +bool valid_quorum_slots(struct scoutfs_quorum_slot *slots); +void print_quorum_slots(struct scoutfs_quorum_slot *slots, int nr, char *indent); #endif