Blame device-mapper-persistent-data-0.2.1-new-thin_metadata_size.c.patch

Heinz Mauelshagen afcf52a
diff --git a/Makefile.in b/Makefile.in
Heinz Mauelshagen afcf52a
index a82ff67..1e7bd54 100644
Heinz Mauelshagen afcf52a
--- a/Makefile.in
Heinz Mauelshagen afcf52a
+++ b/Makefile.in
Heinz Mauelshagen afcf52a
@@ -24,7 +24,8 @@ PROGRAMS=\
Heinz Mauelshagen afcf52a
 	thin_dump \
Heinz Mauelshagen afcf52a
 	thin_restore \
Heinz Mauelshagen afcf52a
 	thin_repair \
Heinz Mauelshagen afcf52a
-	thin_rmap
Heinz Mauelshagen afcf52a
+	thin_rmap \
Heinz Mauelshagen afcf52a
+	thin_metadata_size
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 all: $(PROGRAMS)
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
@@ -64,7 +65,7 @@ SOURCE=\
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 PDATA_OBJECTS=$(subst .cc,.o,$(SOURCE))
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
-PROGRAM_SOURCE=\
Heinz Mauelshagen afcf52a
+CXX_PROGRAM_SOURCE=\
Heinz Mauelshagen afcf52a
 	cache/check.cc \
Heinz Mauelshagen afcf52a
 	\
Heinz Mauelshagen afcf52a
 	thin-provisioning/thin_check.cc \
Heinz Mauelshagen afcf52a
@@ -73,10 +74,15 @@ PROGRAM_SOURCE=\
Heinz Mauelshagen afcf52a
 	thin-provisioning/thin_repair.cc \
Heinz Mauelshagen afcf52a
 	thin-provisioning/thin_rmap.cc
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
+C_PROGRAM_SOURCE=\
Heinz Mauelshagen afcf52a
+	thin-provisioning/thin_metadata_size.c
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+CC:=@CC@
Heinz Mauelshagen afcf52a
 CXX:=@CXX@
Heinz Mauelshagen afcf52a
 OBJECTS:=$(subst .cc,.o,$(SOURCE))
Heinz Mauelshagen afcf52a
 TOP_DIR:=@top_srcdir@
Heinz Mauelshagen afcf52a
 TOP_BUILDDIR:=@top_builddir@
Heinz Mauelshagen afcf52a
+CFLAGS+=-g -Wall -O3
Heinz Mauelshagen afcf52a
 CXXFLAGS+=-std=c++11 -g -Wall -fno-strict-aliasing
Heinz Mauelshagen afcf52a
 CXXFLAGS+=@CXXOPTIMISE_FLAG@
Heinz Mauelshagen afcf52a
 CXXFLAGS+=@CXXDEBUG_FLAG@
Heinz Mauelshagen afcf52a
@@ -104,6 +110,14 @@ endif
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 .SUFFIXES: .d
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
+%.o: %.c
Heinz Mauelshagen afcf52a
+	@echo "    [CC] $<"
Heinz Mauelshagen afcf52a
+	$(V) $(CC) -c $(INCLUDES) $(CFLAGS) -o $@ $<
Heinz Mauelshagen afcf52a
+	@echo "    [DEP] $<"
Heinz Mauelshagen afcf52a
+	$(V) $(CC) -MM -MT $(subst .c,.o,$<) $(INCLUDES) $(CFLAGS) $< > $*.$$$$; \
Heinz Mauelshagen afcf52a
+	sed 's,\([^ :]*\)\.o[ :]*,\1.o \1.gmo $* : Makefile ,g' < $*.$$$$ > $*.d; \
Heinz Mauelshagen afcf52a
+	$(RM) $*.$$$$
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
 %.o: %.cc
Heinz Mauelshagen afcf52a
 	@echo "    [CXX] $<"
Heinz Mauelshagen afcf52a
 	$(V) $(CXX) -c $(INCLUDES) $(CXXFLAGS) -o $@ $<
Heinz Mauelshagen afcf52a
@@ -195,6 +209,10 @@ thin_rmap: $(THIN_RMAP_OBJECTS) thin-provisioning/thin_rmap.o
Heinz Mauelshagen afcf52a
 	@echo "    [LD]  $@"
Heinz Mauelshagen afcf52a
 	$(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS)
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
+thin_metadata_size: thin-provisioning/thin_metadata_size.o
Heinz Mauelshagen afcf52a
+	@echo "    [LD]  $@"
Heinz Mauelshagen afcf52a
+	$(V) $(CC) $(CFLAGS) -o $@ $+ -lm
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
 #----------------------------------------------------------------
Heinz Mauelshagen afcf52a
 # Cache tools
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
@@ -221,29 +239,33 @@ cache_check: $(CACHE_CHECK_OBJECTS) cache/check.o
Heinz Mauelshagen afcf52a
 DEPEND_FILES=\
Heinz Mauelshagen afcf52a
 	$(subst .cc,.d,$(SOURCE)) \
Heinz Mauelshagen afcf52a
 	$(subst .cc,.d,$(TEST_SOURCE)) \
Heinz Mauelshagen afcf52a
-	$(subst .cc,.d,$(PROGRAM_SOURCE))
Heinz Mauelshagen afcf52a
+	$(subst .cc,.d,$(CXX_PROGRAM_SOURCE)) \
Heinz Mauelshagen afcf52a
+	$(subst .c,.d,$(C_PROGRAM_SOURCE))
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 .PHONY: clean distclean
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 clean:
Heinz Mauelshagen afcf52a
 	find . -name \*.o -delete
Heinz Mauelshagen afcf52a
 	find . -name \*.gmo -delete
Heinz Mauelshagen afcf52a
-	$(RM) $(DEPEND_FILES) $(TEST_PROGRAMS) $(PROGRAMS) $(GMOCK_OBJECTS) lib/*.a
Heinz Mauelshagen afcf52a
+	find . -name \*.d -delete
Heinz Mauelshagen afcf52a
+	$(RM) $(TEST_PROGRAMS) $(PROGRAMS) $(GMOCK_OBJECTS) lib/*.a
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 distclean: clean
Heinz Mauelshagen afcf52a
 	$(RM) config.cache config.log config.status configure.h version.h Makefile unit-tests/Makefile
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 install: $(PROGRAMS)
Heinz Mauelshagen afcf52a
 	$(INSTALL_DIR) $(BINDIR)
Heinz Mauelshagen afcf52a
-	$(INSTALL_PROGRAM) thin_check $(BINDIR)
Heinz Mauelshagen afcf52a
-	$(INSTALL_PROGRAM) thin_dump $(BINDIR)
Heinz Mauelshagen afcf52a
-	$(INSTALL_PROGRAM) thin_repair $(BINDIR)
Heinz Mauelshagen afcf52a
-	$(INSTALL_PROGRAM) thin_restore $(BINDIR)
Heinz Mauelshagen afcf52a
-	$(INSTALL_PROGRAM) thin_rmap $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_check $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_dump $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_repair $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_restore $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_rmap $(BINDIR)
Heinz Mauelshagen afcf52a
+	$(INSTALL_PROGRAM) -s thin_metadata_size $(BINDIR)
Heinz Mauelshagen afcf52a
 	$(INSTALL_DIR) $(MANPATH)/man8
Heinz Mauelshagen afcf52a
 	$(INSTALL_DATA) man8/thin_check.8 $(MANPATH)/man8
Heinz Mauelshagen afcf52a
 	$(INSTALL_DATA) man8/thin_dump.8 $(MANPATH)/man8
Heinz Mauelshagen afcf52a
 	$(INSTALL_DATA) man8/thin_restore.8 $(MANPATH)/man8
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
 .PHONY: install
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 ifeq ("@TESTING@", "yes")
Heinz Mauelshagen afcf52a
diff --git a/configure.in b/configure.in
Heinz Mauelshagen afcf52a
index 78d88c1..8f25002 100644
Heinz Mauelshagen afcf52a
--- a/configure.in
Heinz Mauelshagen afcf52a
+++ b/configure.in
Heinz Mauelshagen afcf52a
@@ -30,6 +30,7 @@ dnl -- Setup the directory where autoconf has auxilary files
Heinz Mauelshagen afcf52a
 AC_CONFIG_AUX_DIR(autoconf)
Heinz Mauelshagen afcf52a
 AC_CANONICAL_TARGET([])
Heinz Mauelshagen afcf52a
 AC_PROG_CXX([g++])
Heinz Mauelshagen afcf52a
+AC_PROG_CC([gcc])
Heinz Mauelshagen afcf52a
 AC_LANG(C++)
Heinz Mauelshagen afcf52a
 
Heinz Mauelshagen afcf52a
 ################################################################
Heinz Mauelshagen afcf52a
diff --git a/thin-provisioning/thin_metadata_size.c b/thin-provisioning/thin_metadata_size.c
Heinz Mauelshagen afcf52a
new file mode 100755
Heinz Mauelshagen afcf52a
index 0000000..1f52d51
Heinz Mauelshagen afcf52a
--- /dev/null
Heinz Mauelshagen afcf52a
+++ b/thin-provisioning/thin_metadata_size.c
Heinz Mauelshagen afcf52a
@@ -0,0 +1,370 @@
Heinz Mauelshagen afcf52a
+/*
Heinz Mauelshagen afcf52a
+ * Copyright (C) 2013 Red Hat, GmbH
Heinz Mauelshagen afcf52a
+ * 
Heinz Mauelshagen afcf52a
+ * Calculates device-mapper thin privisioning
Heinz Mauelshagen afcf52a
+ * metadata device size based on pool, block size and
Heinz Mauelshagen afcf52a
+ * maximum expected thin provisioned devices and snapshots.
Heinz Mauelshagen afcf52a
+ *
Heinz Mauelshagen afcf52a
+ * This file is part of the thin-provisioning-tools source.
Heinz Mauelshagen afcf52a
+ *
Heinz Mauelshagen afcf52a
+ * thin-provisioning-tools is free software: you can redistribute it
Heinz Mauelshagen afcf52a
+ * and/or modify it under the terms of the GNU General Public License
Heinz Mauelshagen afcf52a
+ * as published by the Free Software Foundation, either version 3 of
Heinz Mauelshagen afcf52a
+ * the License, or (at your option) any later version.
Heinz Mauelshagen afcf52a
+ *
Heinz Mauelshagen afcf52a
+ * thin-provisioning-tools is distributed in the hope that it will be
Heinz Mauelshagen afcf52a
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
Heinz Mauelshagen afcf52a
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Heinz Mauelshagen afcf52a
+ * GNU General Public License for more details.
Heinz Mauelshagen afcf52a
+ *
Heinz Mauelshagen afcf52a
+ * You should have received a copy of the GNU General Public License along
Heinz Mauelshagen afcf52a
+ * with thin-provisioning-tools.  If not, see
Heinz Mauelshagen afcf52a
+ * <http://www.gnu.org/licenses/>.
Heinz Mauelshagen afcf52a
+ *
Heinz Mauelshagen afcf52a
+ */
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+#include <getopt.h>
Heinz Mauelshagen afcf52a
+#include <libgen.h>
Heinz Mauelshagen afcf52a
+#include <math.h>
Heinz Mauelshagen afcf52a
+#include <stdio.h>
Heinz Mauelshagen afcf52a
+#include <stdlib.h>
Heinz Mauelshagen afcf52a
+#include <string.h>
Heinz Mauelshagen afcf52a
+#include "version.h"
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+#include <search.h>
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+/*----------------------------------------------------------------*/
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+enum numeric_options { BLOCKSIZE, POOLSIZE, MAXTHINS, NUMERIC, OPT_END};
Heinz Mauelshagen afcf52a
+enum return_units { RETURN_BYTES, RETURN_SECTORS };
Heinz Mauelshagen afcf52a
+enum numeric_type { NO_NUMBER, NUMBER, NUMBER_SHORT, NUMBER_LONG };
Heinz Mauelshagen afcf52a
+typedef	unsigned bool;
Heinz Mauelshagen afcf52a
+enum bool_value { false = 0, true = 1};
Heinz Mauelshagen afcf52a
+struct global {
Heinz Mauelshagen afcf52a
+	char *prg; /* program name */
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	/* Unit representations in characters, strings and numeric factors. */
Heinz Mauelshagen afcf52a
+	struct {
Heinz Mauelshagen afcf52a
+		char *chars;
Heinz Mauelshagen afcf52a
+		char **strings;
Heinz Mauelshagen afcf52a
+		unsigned long long *factors;
Heinz Mauelshagen afcf52a
+	} unit;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	/* Command line option properties. */
Heinz Mauelshagen afcf52a
+	struct options {
Heinz Mauelshagen afcf52a
+		unsigned unit_idx;
Heinz Mauelshagen afcf52a
+		char *s[OPT_END];
Heinz Mauelshagen afcf52a
+		unsigned long long n[OPT_END];
Heinz Mauelshagen afcf52a
+	} options;
Heinz Mauelshagen afcf52a
+};
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void exit_prg(struct global *g, int ret)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	if (g) {
Heinz Mauelshagen afcf52a
+		unsigned u = OPT_END;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		while (u--) {
Heinz Mauelshagen afcf52a
+			if (g->options.s[u])
Heinz Mauelshagen afcf52a
+				free (g->options.s[u]);
Heinz Mauelshagen afcf52a
+		}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		free(g);
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	exit(ret);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void abort_prg(struct global *g, const char *msg)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	fprintf(stderr, "%s - %s\n", g ? g->prg : "fatal", msg);
Heinz Mauelshagen afcf52a
+	exit_prg(g, 1);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static int unit_index(struct global *g, char *unit_string)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	unsigned len;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (!unit_string)
Heinz Mauelshagen afcf52a
+		return unit_index(g, "sectors");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	len = strlen(unit_string);
Heinz Mauelshagen afcf52a
+	if (len == 1) {
Heinz Mauelshagen afcf52a
+		char *o = strchr(g->unit.chars, *unit_string);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		if (o)
Heinz Mauelshagen afcf52a
+			return o - g->unit.chars;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	} else {
Heinz Mauelshagen afcf52a
+		char **s;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		for (s = g->unit.strings; *s; s++)
Heinz Mauelshagen afcf52a
+			if (!strncmp(*s, unit_string, len))
Heinz Mauelshagen afcf52a
+				return s - g->unit.strings;
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	return -1;
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
Heinz Mauelshagen afcf52a
+static struct global *init_prg(char *prg_path)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	unsigned u;
Heinz Mauelshagen afcf52a
+	static char *unit_chars = "bskKmMgGtTpPeEzZyY";
Heinz Mauelshagen afcf52a
+	static char *unit_strings[] = { "bytes", "sectors",
Heinz Mauelshagen afcf52a
+					"kilobytes", "kibibytes", "megabytes",  "mebibytes",
Heinz Mauelshagen afcf52a
+					"gigabytes", "gibibytes", "terabytes",  "tebibytes",
Heinz Mauelshagen afcf52a
+					"petabytes", "pebibytes", "exabytes",   "ebibytes",
Heinz Mauelshagen afcf52a
+					"zetabytes", "zebibytes", "yottabytes", "yobibytes", NULL };
Heinz Mauelshagen afcf52a
+	static unsigned long long unit_factors[ARRAY_SIZE(unit_strings) - 1] = { 1, 512, 1024, 1000 };
Heinz Mauelshagen afcf52a
+	struct global *r = malloc(sizeof(*r));
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (!r)
Heinz Mauelshagen afcf52a
+		abort_prg(r, "failed to allocate global context!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	memset(r, 0, sizeof(*r));
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	for (u = 4; unit_strings[u]; u += 2) {
Heinz Mauelshagen afcf52a
+		unit_factors[u]   = unit_factors[u-2] * unit_factors[2];
Heinz Mauelshagen afcf52a
+		unit_factors[u+1] = unit_factors[u-1] * unit_factors[3];
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	r->prg = basename(prg_path);
Heinz Mauelshagen afcf52a
+	r->unit.chars = unit_chars;
Heinz Mauelshagen afcf52a
+	r->unit.strings = unit_strings;
Heinz Mauelshagen afcf52a
+	r->unit.factors = unit_factors;
Heinz Mauelshagen afcf52a
+	r->options.unit_idx = unit_index(r, NULL);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	return r;
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static unsigned long long bytes_per_sector(struct global *g)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	return g->unit.factors[unit_index(g, "sectors")];
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void check_opts(struct global *g)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	struct options *o = &g->options;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (!o->n[BLOCKSIZE])
Heinz Mauelshagen afcf52a
+		abort_prg(g, "block size required!");
Heinz Mauelshagen afcf52a
+	else if (!o->n[POOLSIZE])
Heinz Mauelshagen afcf52a
+		abort_prg(g, "pool size required!");
Heinz Mauelshagen afcf52a
+	else if (!o->n[MAXTHINS])
Heinz Mauelshagen afcf52a
+		abort_prg(g, "max thins required!");
Heinz Mauelshagen afcf52a
+	else if (o->n[BLOCKSIZE] & (o->n[BLOCKSIZE] - 1))
Heinz Mauelshagen afcf52a
+  		abort_prg(g, "block size must be 2^^N");
Heinz Mauelshagen afcf52a
+	else if (o->n[POOLSIZE] <= o->n[BLOCKSIZE])
Heinz Mauelshagen afcf52a
+  		abort_prg(g, "pool size must be larger than block size");
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static unsigned long long to_bytes(struct global *g, char *sz, enum return_units unit, int *index)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	int idx;
Heinz Mauelshagen afcf52a
+	unsigned long long r;
Heinz Mauelshagen afcf52a
+	char *us = sz;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	/* Get pointer to unit identifier. */
Heinz Mauelshagen afcf52a
+	us += strspn(sz, "0123456789");
Heinz Mauelshagen afcf52a
+	if (*us) {
Heinz Mauelshagen afcf52a
+		idx = unit_index(g, us);
Heinz Mauelshagen afcf52a
+		if (idx < 0)
Heinz Mauelshagen afcf52a
+			abort_prg(g, "Invalid unit specifier!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		*us = 0;
Heinz Mauelshagen afcf52a
+		*index = idx;
Heinz Mauelshagen afcf52a
+	} else {
Heinz Mauelshagen afcf52a
+		idx = unit_index(g, NULL);
Heinz Mauelshagen afcf52a
+		us = NULL;
Heinz Mauelshagen afcf52a
+		*index = -1;
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	r = atoll(sz) * g->unit.factors[idx];
Heinz Mauelshagen afcf52a
+	return (!us || unit == RETURN_SECTORS) ? r / bytes_per_sector(g) : r;
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void printf_aligned(struct global *g, char *a, char *b, char *c, bool units, bool mandatory)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	char buf[80];
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	strcpy(buf, b);
Heinz Mauelshagen afcf52a
+	if (units)
Heinz Mauelshagen afcf52a
+		strcat(buf, mandatory ? "{" :"["), strcat(buf, g->unit.chars), strcat(buf, mandatory ? "}" : "]");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	printf("\t%-4s%-44s%s\n", a, buf, c);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void help(struct global *g)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	printf ("Thin Provisioning Metadata Device Size Calculator.\nUsage: %s [options]\n", g->prg);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-b", "--block-size BLOCKSIZE", "Block size of thin provisioned devices.", true, false);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-s", "--pool-size SIZE", "Size of pool device.", true, false);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-m", "--max-thins #MAXTHINS", "Maximum sum of all thin devices and snapshots.", true, false);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-u", "--unit ", "Output unit specifier.", true, true);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-n", "--numeric-only [short|long]", "Output numeric value only (optionally with short/long unit identifier).", false, false);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-h", "--help", "This help.", false, false);
Heinz Mauelshagen afcf52a
+	printf_aligned(g, "-V", "--version", "Print thin provisioning tools version.", false, false);
Heinz Mauelshagen afcf52a
+	exit_prg(g, 0);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void version(struct global *g)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	printf("%s\n", THIN_PROVISIONING_TOOLS_VERSION);
Heinz Mauelshagen afcf52a
+	exit_prg(g, 1);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void check_unit(struct global *g, char *arg)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	int idx = unit_index(g, arg);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (idx < 0)
Heinz Mauelshagen afcf52a
+		abort_prg(g, "output unit specifier invalid!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+      	g->options.unit_idx = idx;
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void check_numeric_option(struct global *g, char *arg)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	if (g->options.n[NUMERIC] != NO_NUMBER)
Heinz Mauelshagen afcf52a
+		abort_prg(g, "-n already given!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	g->options.n[NUMERIC] = NUMBER;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (arg) {
Heinz Mauelshagen afcf52a
+		bool unit_long = !strncmp("long", arg, strlen(arg));
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		if (!*arg || (strncmp("short", arg, strlen(arg)) && !unit_long))
Heinz Mauelshagen afcf52a
+			abort_prg(g, "-n invalid option argument");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+		g->options.n[NUMERIC] = unit_long ? NUMBER_LONG : NUMBER_SHORT;
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void check_size(struct global *g, enum numeric_options o, char *arg)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	int idx;
Heinz Mauelshagen afcf52a
+	bool valid_index = true;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (g->options.n[o])
Heinz Mauelshagen afcf52a
+		abort_prg(g, "option already given!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	g->options.n[o] = to_bytes(g, arg, o == MAXTHINS ? RETURN_BYTES : RETURN_SECTORS, &idx);
Heinz Mauelshagen afcf52a
+	if (idx < 0) {
Heinz Mauelshagen afcf52a
+		valid_index = false;
Heinz Mauelshagen afcf52a
+		idx = g->options.unit_idx;
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	g->options.s[o] = malloc(strlen(arg) + strlen(g->unit.strings[idx]) + 1);
Heinz Mauelshagen afcf52a
+	if (!g->options.s[o])
Heinz Mauelshagen afcf52a
+		abort_prg(g, "failed to allocate string!");
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	strcpy(g->options.s[o], arg);
Heinz Mauelshagen afcf52a
+	if (o != MAXTHINS || valid_index)
Heinz Mauelshagen afcf52a
+		strcat(g->options.s[o], g->unit.strings[idx]);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void parse_command_line(struct global *g, int argc, char **argv)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	int c;
Heinz Mauelshagen afcf52a
+	static struct option long_options[] = {
Heinz Mauelshagen afcf52a
+		{"block-size",	required_argument, NULL, 'b' },
Heinz Mauelshagen afcf52a
+		{"pool-size",	required_argument, NULL, 's' },
Heinz Mauelshagen afcf52a
+		{"max-thins",	required_argument, NULL, 'm' },
Heinz Mauelshagen afcf52a
+		{"unit",	required_argument, NULL, 'u' },
Heinz Mauelshagen afcf52a
+		{"numeric-only",optional_argument, NULL, 'n' },
Heinz Mauelshagen afcf52a
+		{"help",	no_argument,       NULL, 'h' },
Heinz Mauelshagen afcf52a
+		{"version",	no_argument,       NULL, 'V' },
Heinz Mauelshagen afcf52a
+		{NULL,		0,		   NULL, 0   }
Heinz Mauelshagen afcf52a
+	};
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	while ((c = getopt_long(argc, argv, "b:s:m:u:n::hV", long_options, NULL)) != -1) {
Heinz Mauelshagen afcf52a
+		switch (c) {
Heinz Mauelshagen afcf52a
+		case 'b':
Heinz Mauelshagen afcf52a
+			check_size(g, BLOCKSIZE, optarg);
Heinz Mauelshagen afcf52a
+			break;
Heinz Mauelshagen afcf52a
+		case 's':
Heinz Mauelshagen afcf52a
+			check_size(g, POOLSIZE, optarg);
Heinz Mauelshagen afcf52a
+			break;
Heinz Mauelshagen afcf52a
+		case 'm':
Heinz Mauelshagen afcf52a
+			check_size(g, MAXTHINS, optarg);
Heinz Mauelshagen afcf52a
+			break;
Heinz Mauelshagen afcf52a
+		case 'u':
Heinz Mauelshagen afcf52a
+			check_unit(g, optarg);
Heinz Mauelshagen afcf52a
+			break;
Heinz Mauelshagen afcf52a
+		case 'n':
Heinz Mauelshagen afcf52a
+			check_numeric_option(g, optarg);
Heinz Mauelshagen afcf52a
+			break;
Heinz Mauelshagen afcf52a
+		case 'h':
Heinz Mauelshagen afcf52a
+			help(g); /* exits */
Heinz Mauelshagen afcf52a
+		case 'V':
Heinz Mauelshagen afcf52a
+			version(g); /* exits */
Heinz Mauelshagen afcf52a
+		default:
Heinz Mauelshagen afcf52a
+			exit_prg(g, 1);
Heinz Mauelshagen afcf52a
+		}
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	check_opts(g);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static const unsigned mappings_per_block(void)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	const struct {
Heinz Mauelshagen afcf52a
+		const unsigned node;
Heinz Mauelshagen afcf52a
+		const unsigned node_header;
Heinz Mauelshagen afcf52a
+		const unsigned entry;
Heinz Mauelshagen afcf52a
+	} btree_size = { 4096, 64, 16 };
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	return (btree_size.node - btree_size.node_header) / btree_size.entry;
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void print_precision(struct global *g, double r, unsigned idx)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	bool full = g->options.n[NUMERIC] == NO_NUMBER;
Heinz Mauelshagen afcf52a
+	double rtrunc = truncl(r);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (full)
Heinz Mauelshagen afcf52a
+		printf("%s - ", g->prg);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (r == rtrunc)
Heinz Mauelshagen afcf52a
+		printf("%llu", (unsigned long long) r);
Heinz Mauelshagen afcf52a
+	else
Heinz Mauelshagen afcf52a
+		printf(r - rtrunc < 1E-2 ? "%0.2e" : "%0.2f", r);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (full)
Heinz Mauelshagen afcf52a
+		printf(" %s", g->unit.strings[idx]);
Heinz Mauelshagen afcf52a
+	else if (g->options.n[NUMERIC] > NUMBER) {
Heinz Mauelshagen afcf52a
+		if (g->options.n[NUMERIC] == NUMBER_SHORT)
Heinz Mauelshagen afcf52a
+			printf("%c", g->unit.chars[idx]);
Heinz Mauelshagen afcf52a
+		else
Heinz Mauelshagen afcf52a
+			printf("%s", g->unit.strings[idx]);
Heinz Mauelshagen afcf52a
+	}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	if (full)
Heinz Mauelshagen afcf52a
+		printf(" estimated metadata area size for \"--block-size=%s --pool-size=%s --max-thins=%s\"",
Heinz Mauelshagen afcf52a
+		       g->options.s[BLOCKSIZE], g->options.s[POOLSIZE], g->options.s[MAXTHINS]);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	putchar('\n');
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+static void print_estimated_result(struct global *g)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	double r;
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	/* double-fold # of nodes, because they aren't fully populated in average */
Heinz Mauelshagen afcf52a
+	r = (1.0 + (2 * g->options.n[POOLSIZE] / g->options.n[BLOCKSIZE] / mappings_per_block() + g->options.n[MAXTHINS])); /* in 4k blocks */
Heinz Mauelshagen afcf52a
+	r *=  8 * bytes_per_sector(g); /* in bytes! */
Heinz Mauelshagen afcf52a
+	r /= g->unit.factors[g->options.unit_idx]; /* in requested unit */
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	print_precision(g, r, g->options.unit_idx);
Heinz Mauelshagen afcf52a
+}
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+int main(int argc, char **argv)
Heinz Mauelshagen afcf52a
+{
Heinz Mauelshagen afcf52a
+	struct global *g = init_prg(*argv);
Heinz Mauelshagen afcf52a
+
Heinz Mauelshagen afcf52a
+	parse_command_line(g, argc, argv);
Heinz Mauelshagen afcf52a
+	print_estimated_result(g);
Heinz Mauelshagen afcf52a
+	exit_prg(g, 0);
Heinz Mauelshagen afcf52a
+	return 0; /* Doesn't get here... */
Heinz Mauelshagen afcf52a
+}