From: Andrew Price Date: Mon, 15 Aug 2016 12:37:41 +0100 Subject: [Cluster-devel] [PATCH 2/2] gfs2-utils: Use libuuid for uuid handling In-Reply-To: <1471261061-16287-1-git-send-email-anprice@redhat.com> References: <1471261061-16287-1-git-send-email-anprice@redhat.com> Message-ID: <1471261061-16287-2-git-send-email-anprice@redhat.com> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Use libuuid to generate, parse, to-string and copy uuids when the sb_uuid field is available. This allows seven custom uuid-related functions to be removed. Also add some missing #ifdef GFS2_HAS_UUID guards which fixes building against a gfs2_ondisk.h without sb_uuid. Signed-off-by: Andrew Price --- gfs2/convert/Makefile.am | 1 + gfs2/edit/Makefile.am | 6 ++- gfs2/edit/gfs2hex.c | 10 ++++- gfs2/fsck/Makefile.am | 5 ++- gfs2/glocktop/Makefile.am | 3 +- gfs2/libgfs2/Makefile.am | 4 +- gfs2/libgfs2/lang.c | 60 ++++++++++------------------- gfs2/libgfs2/libgfs2.h | 2 - gfs2/libgfs2/meta.c | 15 +++++++- gfs2/libgfs2/ondisk.c | 37 +++++------------- gfs2/libgfs2/structures.c | 51 +++--------------------- gfs2/mkfs/Makefile.am | 13 +++++-- gfs2/mkfs/main_mkfs.c | 11 +++++- gfs2/tune/Makefile.am | 5 ++- gfs2/tune/super.c | 98 ++++++++++------------------------------------- tests/Makefile.am | 4 ++ 16 files changed, 120 insertions(+), 205 deletions(-) diff --git a/gfs2/convert/Makefile.am b/gfs2/convert/Makefile.am index 30924d7..4db643f 100644 --- a/gfs2/convert/Makefile.am +++ b/gfs2/convert/Makefile.am @@ -8,3 +8,4 @@ gfs2_convert_CPPFLAGS = \ -I$(top_srcdir)/gfs2/libgfs2 gfs2_convert_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la +gfs2_convert_LDFLAGS = $(uuid_LIBS) diff --git a/gfs2/edit/Makefile.am b/gfs2/edit/Makefile.am index 5d2942e..a9b177e 100644 --- a/gfs2/edit/Makefile.am +++ b/gfs2/edit/Makefile.am @@ -22,10 +22,12 @@ gfs2_edit_CPPFLAGS = \ gfs2_edit_CFLAGS = \ $(ncurses_CFLAGS) \ - $(zlib_CFLAGS) + $(zlib_CFLAGS) \ + $(uuid_CFLAGS) gfs2_edit_LDFLAGS = \ $(ncurses_LIBS) \ - $(zlib_LIBS) + $(zlib_LIBS) \ + $(uuid_LIBS) gfs2_edit_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la diff --git a/gfs2/edit/gfs2hex.c b/gfs2/edit/gfs2hex.c index 3d280b5..3d0f10d 100644 --- a/gfs2/edit/gfs2hex.c +++ b/gfs2/edit/gfs2hex.c @@ -20,6 +20,9 @@ #include "extended.h" #include "gfs2hex.h" #include "libgfs2.h" +#ifdef GFS2_HAS_UUID +#include +#endif #define pv(struct, member, fmt, fmt2) do { \ print_it(" "#member, fmt, fmt2, struct->member); \ @@ -415,7 +418,12 @@ static void gfs2_sb_print2(struct gfs2_sb *sbp2) gfs2_inum_print2("license ", &gfs1_license_di); } #ifdef GFS2_HAS_UUID - print_it(" sb_uuid", "%s", NULL, str_uuid(sbp2->sb_uuid)); + { + char readable_uuid[36+1]; + + uuid_unparse(sbp2->sb_uuid, readable_uuid); + print_it(" sb_uuid", "%s", NULL, readable_uuid); + } #endif } diff --git a/gfs2/fsck/Makefile.am b/gfs2/fsck/Makefile.am index 4c3498b..97fcd40 100644 --- a/gfs2/fsck/Makefile.am +++ b/gfs2/fsck/Makefile.am @@ -35,4 +35,7 @@ fsck_gfs2_CPPFLAGS = \ -I$(top_srcdir)/gfs2/include \ -I$(top_srcdir)/gfs2/libgfs2 -fsck_gfs2_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la +fsck_gfs2_LDADD = \ + $(top_builddir)/gfs2/libgfs2/libgfs2.la +fsck_gfs2_LDFLAGS = \ + $(uuid_LIBS) diff --git a/gfs2/glocktop/Makefile.am b/gfs2/glocktop/Makefile.am index a43c519..1102c8e 100644 --- a/gfs2/glocktop/Makefile.am +++ b/gfs2/glocktop/Makefile.am @@ -10,7 +10,8 @@ glocktop_CFLAGS = \ $(ncurses_CFLAGS) glocktop_LDFLAGS = \ - $(ncurses_LIBS) + $(ncurses_LIBS) \ + $(uuid_LIBS) glocktop_CPPFLAGS = \ -D_FILE_OFFSET_BITS=64 \ diff --git a/gfs2/libgfs2/Makefile.am b/gfs2/libgfs2/Makefile.am index 2b7aa16..4321a67 100644 --- a/gfs2/libgfs2/Makefile.am +++ b/gfs2/libgfs2/Makefile.am @@ -48,10 +48,12 @@ libgfs2_la_CPPFLAGS = \ -D_FILE_OFFSET_BITS=64 \ -D_LARGEFILE64_SOURCE \ -D_GNU_SOURCE \ - -I$(top_srcdir)/gfs2/include + -I$(top_srcdir)/gfs2/include \ + $(uuid_CFLAGS) gfs2l_SOURCES = gfs2l.c gfs2l_LDADD = libgfs2.la +gfs2l_LDFLAGS = $(uuid_LIBS) gfs2l_CPPFLAGS = \ -I$(top_srcdir)/gfs2/include \ -D_FILE_OFFSET_BITS=64 diff --git a/gfs2/libgfs2/lang.c b/gfs2/libgfs2/lang.c index 13333a1..2f93c56 100644 --- a/gfs2/libgfs2/lang.c +++ b/gfs2/libgfs2/lang.c @@ -9,6 +9,9 @@ #include "lang.h" #include "parser.h" +#ifdef GFS2_HAS_UUID +#include +#endif const char* ast_type_string[] = { [AST_NONE] = "NONE", @@ -312,7 +315,14 @@ static int field_print(const struct gfs2_buffer_head *bh, const struct lgfs2_met printf("%s\t%"PRIu64"\t%u\t%u\t%s\t", mtype->name, bh->b_blocknr, field->offset, field->length, field->name); if (field->flags & LGFS2_MFF_UUID) { - printf("'%s'\n", str_uuid((const unsigned char *)fieldp)); +#ifdef GFS2_HAS_UUID + char readable_uuid[36+1]; + uuid_t uuid; + + memcpy(uuid, fieldp, sizeof(uuid_t)); + uuid_unparse(uuid, readable_uuid); + printf("'%s'\n", readable_uuid); +#endif } else if (field->flags & LGFS2_MFF_STRING) { printf("'%s'\n", fieldp); } else { @@ -431,40 +441,6 @@ static struct lgfs2_lang_result *ast_interp_get(struct lgfs2_lang_state *state, } /** - * Interpret a UUID string by removing hyphens from the string and then - * interprets 16 pairs of hex digits as octets. - */ -static int ast_str_to_uuid(const char *str, uint8_t *uuid) -{ - char s[33]; - int head, tail, tmp; - - for (head = tail = 0; head < strlen(str) && tail < 33; head++) { - if (str[head] == '-') - continue; - s[tail] = tolower(str[head]); - if (!((s[tail] >= 'a' && s[tail] <= 'f') || - (s[tail] >= '0' && s[tail] <= '9'))) - goto invalid; - tail++; - } - if (tail != 32) { - goto invalid; - } - s[tail] = '\0'; - for (head = 0; head < 16; head++) { - if (sscanf(s+(head*2), "%02x", &tmp) != 1) { - goto invalid; - } - *(uuid + head) = tmp; - } - return AST_INTERP_SUCCESS; -invalid: - fprintf(stderr, "Invalid UUID\n"); - return AST_INTERP_INVAL; -} - -/** * Set a field of a gfs2 block of a given type to a given value. * Returns AST_INTERP_* to signal success, an invalid field/value or an error. */ @@ -474,12 +450,18 @@ static int ast_field_set(struct gfs2_buffer_head *bh, const struct lgfs2_metafie int err = 0; if (field->flags & LGFS2_MFF_UUID) { - uint8_t uuid[16]; - int ret = ast_str_to_uuid(val->ast_str, uuid); +#ifdef GFS2_HAS_UUID + uuid_t uuid; - if (ret != AST_INTERP_SUCCESS) - return ret; + if (uuid_parse(val->ast_str, uuid) != 0) { + fprintf(stderr, "Invalid UUID\n"); + return AST_INTERP_INVAL; + } err = lgfs2_field_assign(bh->b_data, field, uuid); +#else + fprintf(stderr, "No UUID support\n"); + err = 1; +#endif } else if (field->flags & LGFS2_MFF_STRING) { err = lgfs2_field_assign(bh->b_data, field, val->ast_str); } else { diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h index 5414a20..ebf6bca 100644 --- a/gfs2/libgfs2/libgfs2.h +++ b/gfs2/libgfs2/libgfs2.h @@ -711,8 +711,6 @@ extern int write_sb(struct gfs2_sbd *sdp); /* ondisk.c */ extern uint32_t gfs2_disk_hash(const char *data, int len); -extern const char *str_uuid(const unsigned char *uuid); -extern void gfs2_print_uuid(const unsigned char *uuid); extern void print_it(const char *label, const char *fmt, const char *fmt2, ...) __attribute__((format(printf,2,4))); diff --git a/gfs2/libgfs2/meta.c b/gfs2/libgfs2/meta.c index 90d5647..500757d 100644 --- a/gfs2/libgfs2/meta.c +++ b/gfs2/libgfs2/meta.c @@ -3,6 +3,10 @@ #include "libgfs2.h" #include "clusterautoconfig.h" +#ifdef GFS2_HAS_UUID +#include +#endif + #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) #define SYM(x) { x, #x }, @@ -150,7 +154,9 @@ F(sb_lockproto, .flags = LGFS2_MFF_STRING) F(sb_locktable, .flags = LGFS2_MFF_STRING) INR(__pad3, .points_to = (1 << LGFS2_MT_GFS2_DINODE)) INR(__pad4, .points_to = (1 << LGFS2_MT_GFS2_DINODE)) +#ifdef GFS2_HAS_UUID F(sb_uuid, .flags = LGFS2_MFF_UUID) +#endif }; #undef STRUCT @@ -878,7 +884,14 @@ int lgfs2_field_str(char *str, const size_t size, const char *blk, const struct return 1; if (field->flags & LGFS2_MFF_UUID) { - snprintf(str, size, "%s", str_uuid((unsigned char *)fieldp)); +#ifdef GFS2_HAS_UUID + char readable_uuid[36+1]; + uuid_t uuid; + + memcpy(uuid, fieldp, sizeof(uuid_t)); + uuid_unparse(uuid, readable_uuid); + snprintf(str, size, "%s", readable_uuid); +#endif } else if (field->flags & LGFS2_MFF_STRING) { snprintf(str, size, "%s", fieldp); } else { diff --git a/gfs2/libgfs2/ondisk.c b/gfs2/libgfs2/ondisk.c index d49455e..66de223 100644 --- a/gfs2/libgfs2/ondisk.c +++ b/gfs2/libgfs2/ondisk.c @@ -6,9 +6,11 @@ #include #include #include - #include #include "libgfs2.h" +#ifdef GFS2_HAS_UUID +#include +#endif #define pv(struct, member, fmt, fmt2) do { \ print_it(" "#member, fmt, fmt2, struct->member); \ @@ -151,32 +153,6 @@ void gfs2_sb_out(const struct gfs2_sb *sb, char *buf) #endif } -const char *str_uuid(const unsigned char *uuid) -{ - static char str[64]; - char *ch; - int i; - - memset(str, 0, sizeof(str)); - ch = str; - for (i = 0; i < 16; i++) { - sprintf(ch, "%02x", uuid[i]); - ch += 2; - if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) { - *ch = '-'; - ch++; - } - } - return str; -} - -#ifdef GFS2_HAS_UUID -void gfs2_print_uuid(const unsigned char *uuid) -{ - print_it(" uuid", "%s", NULL, str_uuid(uuid)); -} -#endif - void gfs2_sb_print(const struct gfs2_sb *sb) { gfs2_meta_header_print(&sb->sb_header); @@ -194,7 +170,12 @@ void gfs2_sb_print(const struct gfs2_sb *sb) pv(sb, sb_locktable, "%s", NULL); #ifdef GFS2_HAS_UUID - gfs2_print_uuid(sb->sb_uuid); + { + char readable_uuid[36+1]; + + uuid_unparse(sb->sb_uuid, readable_uuid); + print_it(" uuid", "%36s", NULL, readable_uuid); + } #endif } diff --git a/gfs2/libgfs2/structures.c b/gfs2/libgfs2/structures.c index 87ffde7..1cc88b5 100644 --- a/gfs2/libgfs2/structures.c +++ b/gfs2/libgfs2/structures.c @@ -16,6 +16,10 @@ #include "libgfs2.h" #include "config.h" +#ifdef GFS2_HAS_UUID +#include +#endif + int build_master(struct gfs2_sbd *sdp) { struct gfs2_inum inum; @@ -45,51 +49,6 @@ int build_master(struct gfs2_sbd *sdp) return 0; } -#ifdef GFS2_HAS_UUID -/** - * Generate a series of random bytes using /dev/urandom. - * Modified from original code in gen_uuid.c in e2fsprogs/lib - */ -static void get_random_bytes(void *buf, int nbytes) -{ - int i, n = nbytes, fd; - int lose_counter = 0; - unsigned char *cp = (unsigned char *) buf; - struct timeval tv; - - gettimeofday(&tv, 0); - fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); - srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); - /* Crank the random number generator a few times */ - gettimeofday(&tv, 0); - for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) - rand(); - if (fd >= 0) { - while (n > 0) { - i = read(fd, cp, n); - if (i <= 0) { - if (lose_counter++ > 16) - break; - continue; - } - n -= i; - cp += i; - lose_counter = 0; - } - close(fd); - } - - /* - * We do this all the time, but this is the only source of - * randomness if /dev/random/urandom is out to lunch. - */ - for (cp = buf, i = 0; i < nbytes; i++) - *cp++ ^= (rand() >> 7) & 0xFF; - - return; -} -#endif - /** * Initialise a gfs2_sb structure with sensible defaults. */ @@ -104,7 +63,7 @@ void lgfs2_sb_init(struct gfs2_sb *sb, unsigned bsize) sb->sb_bsize = bsize; sb->sb_bsize_shift = ffs(bsize) - 1; #ifdef GFS2_HAS_UUID - get_random_bytes(&sb->sb_uuid, sizeof(sb->sb_uuid)); + uuid_generate(sb->sb_uuid); #endif } diff --git a/gfs2/mkfs/Makefile.am b/gfs2/mkfs/Makefile.am index 074c4c1..f0dd961 100644 --- a/gfs2/mkfs/Makefile.am +++ b/gfs2/mkfs/Makefile.am @@ -22,8 +22,12 @@ mkfs_gfs2_SOURCES = \ progress.h mkfs_gfs2_CPPFLAGS = $(COMMON_CPPFLAGS) -mkfs_gfs2_CFLAGS = $(blkid_CFLAGS) -mkfs_gfs2_LDFLAGS = $(blkid_LIBS) +mkfs_gfs2_CFLAGS = \ + $(blkid_CFLAGS) \ + $(uuid_CFLAGS) +mkfs_gfs2_LDFLAGS = \ + $(blkid_LIBS) \ + $(uuid_LIBS) mkfs_gfs2_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la gfs2_grow_SOURCES = \ @@ -32,7 +36,9 @@ gfs2_grow_SOURCES = \ gfs2_grow_CPPFLAGS = $(COMMON_CPPFLAGS) gfs2_grow_CFLAGS = $(blkid_CFLAGS) -gfs2_grow_LDFLAGS = $(blkid_LIBS) +gfs2_grow_LDFLAGS = \ + $(blkid_LIBS) \ + $(uuid_LIBS) gfs2_grow_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la gfs2_jadd_SOURCES = \ @@ -41,4 +47,5 @@ gfs2_jadd_SOURCES = \ gfs2_jadd_CPPFLAGS = $(COMMON_CPPFLAGS) gfs2_jadd_LDADD = $(top_builddir)/gfs2/libgfs2/libgfs2.la +gfs2_jadd_LDFLAGS = $(uuid_LIBS) diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c index 7ede5ad..363878c 100644 --- a/gfs2/mkfs/main_mkfs.c +++ b/gfs2/mkfs/main_mkfs.c @@ -30,6 +30,10 @@ #include "gfs2_mkfs.h" #include "progress.h" +#ifdef GFS2_HAS_UUID +#include +#endif + static void print_usage(const char *prog_name) { int i; @@ -537,8 +541,13 @@ static void print_results(struct gfs2_sb *sb, struct mkfs_opts *opts, uint64_t r printf("%-27s\"%s\"\n", _("Locking protocol:"), opts->lockproto); printf("%-27s\"%s\"\n", _("Lock table:"), opts->locktable); #ifdef GFS2_HAS_UUID + { + char readable_uuid[36+1]; + + uuid_unparse(sb->sb_uuid, readable_uuid); /* Translators: "UUID" = universally unique identifier. */ - printf("%-27s%s\n", _("UUID:"), str_uuid(sb->sb_uuid)); + printf("%-27s%s\n", _("UUID:"), readable_uuid); + } #endif } diff --git a/gfs2/tune/Makefile.am b/gfs2/tune/Makefile.am index c060183..7628f42 100644 --- a/gfs2/tune/Makefile.am +++ b/gfs2/tune/Makefile.am @@ -7,8 +7,11 @@ noinst_HEADERS = tunegfs2.h tunegfs2_SOURCES = \ main.c \ super.c - tunegfs2_CPPFLAGS = \ -D_FILE_OFFSET_BITS=64 \ -I$(top_srcdir)/gfs2/include \ -I$(top_srcdir)/group/include +tunegfs2_CFLAGS = \ + $(uuid_CFLAGS) +tunegfs2_LDFLAGS = \ + $(uuid_LIBS) diff --git a/gfs2/tune/super.c b/gfs2/tune/super.c index 560ce68..c4ffd7e 100644 --- a/gfs2/tune/super.c +++ b/gfs2/tune/super.c @@ -14,78 +14,9 @@ #include #include "tunegfs2.h" -static int str_to_hexchar(const char *estring) -{ - int ch = 0; - - if (isdigit(*estring)) - ch = (*estring - '0') * 0x10; - else if (*estring >= 'a' && *estring <= 'f') - ch = (*estring - 'a' + 0x0a) * 0x10; - else if (*estring >= 'A' && *estring <= 'F') - ch = (*estring - 'A' + 0x0a) * 0x10; - - estring++; - if (isdigit(*estring)) - ch += (*estring - '0'); - else if (*estring >= 'a' && *estring <= 'f') - ch += (*estring - 'a' + 0x0a); - else if (*estring >= 'A' && *estring <= 'F') - ch += (*estring - 'A' + 0x0a); - return ch; -} - - - -static const char *uuid2str(const unsigned char *uuid) -{ - static char str[64]; - char *ch; - int i; - - memset(str, 0, 64); - ch = str; - for (i = 0; i < 16; i++) { - sprintf(ch, "%02x", uuid[i]); - ch += 2; - if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) { - *ch = '-'; - ch++; - } - } - return str; -} - -static int str2uuid(const char *newval, char *uuid) -{ - char *cp; - int i; - - if (strlen(newval) != 36) { - fprintf(stderr, _("Invalid UUID: %s\n"), newval); - return EX_DATAERR; - } - - cp = uuid; - for (i = 0; i < 36; i++) { - if ((i == 8) || (i == 13) || - (i == 18) || (i == 23)) { - if (newval[i] == '-') - continue; - fprintf(stderr, _("Invalid UUID: %s\n"), newval); - return EX_DATAERR; - } - if (!isxdigit(newval[i])) { - fprintf(stderr, _("Invalid UUID: %s\n"), newval); - fprintf(stderr, _("Bad digit '%c'@position %d\n"), - newval[i], i + 1); - return EX_DATAERR; - } - *cp = str_to_hexchar(&newval[i++]); - cp++; - } - return 0; -} +#ifdef GFS2_HAS_UUID +#include +#endif int read_super(struct tunegfs2 *tfs) { @@ -125,7 +56,12 @@ int print_super(const struct tunegfs2 *tfs) { printf(_("File system volume name: %s\n"), tfs->sb->sb_locktable); #ifdef GFS2_HAS_UUID - printf(_("File system UUID: %s\n"), uuid2str(tfs->sb->sb_uuid)); + { + char readable_uuid[36+1]; + + uuid_unparse(tfs->sb->sb_uuid, readable_uuid); + printf(_("File system UUID: %s\n"), readable_uuid); + } #endif printf( _("File system magic number: 0x%X\n"), be32_to_cpu(tfs->sb->sb_header.mh_magic)); printf(_("Block size: %d\n"), be32_to_cpu(tfs->sb->sb_bsize)); @@ -152,12 +88,18 @@ int write_super(const struct tunegfs2 *tfs) int change_uuid(struct tunegfs2 *tfs, const char *str) { - char uuid[16]; - int status = 0; - status = str2uuid(str, uuid); - if (!status) - memcpy(tfs->sb->sb_uuid, uuid, 16); +#ifdef GFS2_HAS_UUID + uuid_t uuid; + int status; + + status = uuid_parse(str, uuid); + if (status == 0) + uuid_copy(tfs->sb->sb_uuid, uuid); return status; +#else + fprintf(stderr, _("UUID support unavailable in this build\n")); + return 1; +#endif } int change_lockproto(struct tunegfs2 *tfs, const char *lockproto) diff --git a/tests/Makefile.am b/tests/Makefile.am index 273be71..1dedc2b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,6 +23,8 @@ nukerg_CFLAGS = \ -I$(top_srcdir)/gfs2/include nukerg_LDADD = \ $(top_builddir)/gfs2/libgfs2/libgfs2.la +nukerg_LDFLAGS = \ + $(uuid_LIBS) if HAVE_CHECK UNIT_TESTS = \ @@ -49,6 +51,7 @@ check_meta_SOURCES = \ check_meta.c check_meta_CFLAGS = $(UNIT_CFLAGS) check_meta_LDADD = $(UNIT_LDADD) +check_meta_LDFLAGS = $(uuid_LIBS) check_meta_CPPFLAGS = $(UNIT_CPPFLAGS) check_rgrp_SOURCES = \ @@ -56,6 +59,7 @@ check_rgrp_SOURCES = \ check_rgrp.c check_rgrp_CFLAGS = $(UNIT_CFLAGS) check_rgrp_LDADD = $(UNIT_LDADD) +check_rgrp_LDFLAGS = $(uuid_LIBS) check_rgrp_CPPFLAGS = $(UNIT_CPPFLAGS) endif -- 2.7.4