]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Introduce random_uuid() helper function
authorJes Sorensen <Jes.Sorensen@redhat.com>
Mon, 15 Aug 2016 19:41:34 +0000 (15:41 -0400)
committerJes Sorensen <Jes.Sorensen@redhat.com>
Mon, 15 Aug 2016 19:41:34 +0000 (15:41 -0400)
This gets rid of 5 nearly identical copies of the same code, and
reduces the binary size of mdadm by over 700 bytes on x86_64.

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
Assemble.c
mdadm.h
super1.c
util.c

index d274e6e0cfd7f368100253eaa596d6903932bb0c..3da090330019928021e5a5d999b5575ae541a1fe 100644 (file)
@@ -599,18 +599,9 @@ static int load_devices(struct devs *devices, char *devmap,
                        int err;
                        fstat(mdfd, &stb2);
 
-                       if (strcmp(c->update, "uuid")==0 &&
-                           !ident->uuid_set) {
-                               int rfd;
-                               if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
-                                   read(rfd, ident->uuid, 16) != 16) {
-                                       *(__u32*)(ident->uuid) = random();
-                                       *(__u32*)(ident->uuid+1) = random();
-                                       *(__u32*)(ident->uuid+2) = random();
-                                       *(__u32*)(ident->uuid+3) = random();
-                               }
-                               if (rfd >= 0) close(rfd);
-                       }
+                       if (strcmp(c->update, "uuid") == 0 && !ident->uuid_set)
+                               random_uuid((__u8 *)ident->uuid);
+
                        dfd = dev_open(devname,
                                       tmpdev->disposition == 'I'
                                       ? O_RDWR : (O_RDWR|O_EXCL));
diff --git a/mdadm.h b/mdadm.h
index cfa5bebe8cffbe576530640aa75949fc8ffae99c..0516c826f9d5fd03280b41d49f7fd2dcc0b5853b 100755 (executable)
--- a/mdadm.h
+++ b/mdadm.h
@@ -1470,6 +1470,7 @@ extern int mdmon_running(char *devnm);
 extern int mdmon_pid(char *devnm);
 extern int check_env(char *name);
 extern __u32 random32(void);
+extern void random_uuid(__u8 *buf);
 extern int start_mdmon(char *devnm);
 
 extern int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
index b7974031f232b7620c837c144f1e36ddd83ca9e8..f3e40232cf56c1ed42ee178edea5fd4779f0c8ab 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -1175,7 +1175,7 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
                }
        } else if (strcmp(update, "linear-grow-new") == 0) {
                unsigned int i;
-               int rfd, fd;
+               int fd;
                unsigned int max = __le32_to_cpu(sb->max_dev);
 
                for (i=0 ; i < max ; i++)
@@ -1186,13 +1186,7 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
                if (max >= __le32_to_cpu(sb->max_dev))
                        sb->max_dev = __cpu_to_le32(max+1);
 
-               if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
-                   read(rfd, sb->device_uuid, 16) != 16) {
-                       __u32 r[4] = {random(), random(), random(), random()};
-                       memcpy(sb->device_uuid, r, 16);
-               }
-               if (rfd >= 0)
-                       close(rfd);
+               random_uuid(sb->device_uuid);
 
                sb->dev_roles[i] =
                        __cpu_to_le16(info->disk.raid_disk);
@@ -1407,7 +1401,6 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
 {
        struct mdp_superblock_1 *sb;
        int spares;
-       int rfd;
        char defname[10];
        int sbsize;
 
@@ -1437,14 +1430,8 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
 
        if (uuid)
                copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
-       else {
-               if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
-                   read(rfd, sb->set_uuid, 16) != 16) {
-                       __u32 r[4] = {random(), random(), random(), random()};
-                       memcpy(sb->set_uuid, r, 16);
-               }
-               if (rfd >= 0) close(rfd);
-       }
+       else
+               random_uuid(sb->set_uuid);;
 
        if (name == NULL || *name == 0) {
                sprintf(defname, "%d", info->md_minor);
@@ -1707,7 +1694,6 @@ static int write_init_super1(struct supertype *st)
 {
        struct mdp_superblock_1 *sb = st->sb;
        struct supertype *refst;
-       int rfd;
        int rv = 0;
        unsigned long long bm_space;
        struct devinfo *di;
@@ -1735,13 +1721,7 @@ static int write_init_super1(struct supertype *st)
                else
                        sb->devflags &= ~WriteMostly1;
 
-               if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
-                   read(rfd, sb->device_uuid, 16) != 16) {
-                       __u32 r[4] = {random(), random(), random(), random()};
-                       memcpy(sb->device_uuid, r, 16);
-               }
-               if (rfd >= 0)
-                       close(rfd);
+               random_uuid(sb->device_uuid);
 
                if (!(di->disk.state & (1<<MD_DISK_JOURNAL)))
                        sb->events = 0;
@@ -2597,7 +2577,6 @@ void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0
        void *ret;
        struct mdp_superblock_1 *sb;
        int i;
-       int rfd;
        unsigned long long offset;
 
        if (posix_memalign(&ret, 4096, 1024) != 0)
@@ -2629,13 +2608,7 @@ void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0
        sb->super_offset = __cpu_to_le64(offset);
        //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
 
-       if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
-           read(rfd, sb->device_uuid, 16) != 16) {
-               __u32 r[4] = {random(), random(), random(), random()};
-               memcpy(sb->device_uuid, r, 16);
-       }
-       if (rfd >= 0)
-               close(rfd);
+       random_uuid(sb->device_uuid);
 
        for (i = 0; i < MD_SB_DISKS; i++) {
                int state = sb0->disks[i].state;
diff --git a/util.c b/util.c
index 9fcc4b0d4f9d121996ec4aca67540397ce655e41..8b5224234862f67717c8373fc0265dd6c30cb2d4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1936,6 +1936,27 @@ __u32 random32(void)
        return rv;
 }
 
+void random_uuid(__u8 *buf)
+{
+       int fd, i, len;
+       __u32 r[4];
+
+       fd = open("/dev/urandom", O_RDONLY);
+       if (fd < 0)
+               goto use_random;
+       len = read(fd, buf, 16);
+       close(fd);
+       if (len != 16)
+               goto use_random;
+
+       return;
+
+use_random:
+       for (i = 0; i < 4; i++)
+               r[i] = random();
+       memcpy(buf, r, 16);
+}
+
 #ifndef MDASSEMBLE
 int flush_metadata_updates(struct supertype *st)
 {