From beae1dfe2e5a3f11e6e52a93fbf617d644708415 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 14 Dec 2006 17:32:57 +1100 Subject: [PATCH] Central calls to ioctl BLKGETSIZE Instead of opencoding the same thing everywhere. --- Assemble.c | 4 ++-- Build.c | 11 ++++------ Create.c | 14 +------------ Detail.c | 14 ++----------- Grow.c | 4 +--- Manage.c | 13 +----------- Query.c | 12 ++--------- mdadm.h | 1 + super0.c | 60 +++++++++++++----------------------------------------- super1.c | 46 +++++++++-------------------------------- test | 2 ++ util.c | 22 ++++++++++++++++++++ 12 files changed, 62 insertions(+), 141 deletions(-) diff --git a/Assemble.c b/Assemble.c index c731105d..5e4e3941 100644 --- a/Assemble.c +++ b/Assemble.c @@ -951,8 +951,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd, while (usecs < 1000) { mdfd = open(mddev, O_RDONLY); if (mdfd >= 0) { - unsigned long size; - if (ioctl(mdfd, BLKGETSIZE, &size) == 0 && + unsigned long long size; + if (get_dev_size(mdfd, NULL, &size) && size > 0) break; close(mdfd); diff --git a/Build.c b/Build.c index ea314638..df9f4f2f 100644 --- a/Build.c +++ b/Build.c @@ -148,7 +148,7 @@ int Build(char *mddev, int mdfd, int chunk, int level, int layout, } /* now add the devices */ for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) { - unsigned long dsize; + unsigned long long dsize; int fd; if (strcmp("missing", dv->devname) == 0) continue; @@ -168,12 +168,9 @@ int Build(char *mddev, int mdfd, int chunk, int level, int layout, dv->devname, strerror(errno)); goto abort; } - if (ioctl(fd, BLKGETSIZE, &dsize) == 0 && dsize > 0) { - unsigned long long ldsize = dsize; - ldsize <<= 9; - if (size== 0 || ldsize < size) - size = ldsize; - } + if (get_dev_size(fd, NULL, &dsize) && + (size == 0 || dsize < size)) + size = dsize; close(fd); if (vers>= 9000) { mdu_disk_info_t disk; diff --git a/Create.c b/Create.c index cd4356cc..2a30ace7 100644 --- a/Create.c +++ b/Create.c @@ -180,7 +180,6 @@ int Create(struct supertype *st, char *mddev, int mdfd, dnum = 0; for (dv=devlist; dv; dv=dv->next, dnum++) { char *dname = dv->devname; - unsigned long dsize; unsigned long long ldsize, freesize; int fd; if (strcasecmp(dname, "missing")==0) { @@ -199,22 +198,11 @@ int Create(struct supertype *st, char *mddev, int mdfd, fail=1; continue; } -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &ldsize)==0) - ; - else -#endif - if (ioctl(fd, BLKGETSIZE, &dsize)) { - fprintf(stderr, Name ": Cannot get size of %s: %s\n", - dname, strerror(errno)); + if (!get_dev_size(fd, dname, &ldsize)) { fail = 1; close(fd); continue; } - else { - ldsize = dsize; - ldsize <<= 9; - } if (st == NULL) { struct createinfo *ci = conf_get_create_info(); if (ci) diff --git a/Detail.c b/Detail.c index 42e99077..ca34f1d4 100644 --- a/Detail.c +++ b/Detail.c @@ -132,7 +132,6 @@ int Detail(char *dev, int brief, int test, char *homehost) printf("ARRAY %s level=%s num-devices=%d", dev, c?c:"-unknown-",array.raid_disks ); else { mdu_bitmap_file_t bmf; - unsigned long array_size; unsigned long long larray_size; struct mdstat_ent *ms = mdstat_read(0, 0); struct mdstat_ent *e; @@ -143,17 +142,8 @@ int Detail(char *dev, int brief, int test, char *homehost) for (e=ms; e; e=e->next) if (e->devnum == devnum) break; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &larray_size)==0) - ; - else -#endif - if (ioctl(fd, BLKGETSIZE, &array_size)==0) { - larray_size = array_size; - larray_size <<= 9; - } - - else larray_size = 0; + if (!get_dev_size(fd, NULL, &larray_size)) + larray_size = 0; printf("%s:\n", dev); printf(" Version : %02d.%02d.%02d\n", diff --git a/Grow.c b/Grow.c index fec0d327..c160f2c6 100644 --- a/Grow.c +++ b/Grow.c @@ -257,15 +257,13 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int } bitmapsize = array.size; bitmapsize <<= 1; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &array_size) == 0 && + if (get_dev_size(fd, NULL, &array_size) && array_size > (0x7fffffffULL<<9)) { /* Array is big enough that we cannot trust array.size * try other approaches */ bitmapsize = get_component_size(fd); } -#endif if (bitmapsize == 0) { fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); return 1; diff --git a/Manage.c b/Manage.c index 144fb4fb..4b5ec806 100644 --- a/Manage.c +++ b/Manage.c @@ -187,7 +187,6 @@ int Manage_subdevs(char *devname, int fd, } for (dv = devlist ; dv; dv=dv->next) { unsigned long long ldsize; - unsigned long dsize; if (stat(dv->devname, &stb)) { fprintf(stderr, Name ": cannot find %s: %s\n", @@ -224,19 +223,9 @@ int Manage_subdevs(char *devname, int fd, if (array.not_persistent==0) st->ss->load_super(st, tfd, &osuper, NULL); /* will use osuper later */ -#ifdef BLKGETSIZE64 - if (ioctl(tfd, BLKGETSIZE64, &ldsize)==0) - ; - else -#endif - if (ioctl(tfd, BLKGETSIZE, &dsize)) { - fprintf(stderr, Name ": Cannot get size of %s: %s\n", - dv->devname, strerror(errno)); + if (!get_dev_size(tfd, dv->devname, &ldsize)) { close(tfd); return 1; - } else { - ldsize = dsize; - ldsize <<= 9; } close(tfd); diff --git a/Query.c b/Query.c index 46707edd..4cfcf578 100644 --- a/Query.c +++ b/Query.c @@ -47,7 +47,6 @@ int Query(char *dev) struct supertype *st = NULL; unsigned long long larray_size; - unsigned long array_size; struct stat stb; char *mddev; mdu_disk_info_t disc; @@ -67,15 +66,8 @@ int Query(char *dev) fstat(fd, &stb); if (vers>=9000 && !ioctlerr) { -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &larray_size)==0) - ; - else -#endif - if (ioctl(fd, BLKGETSIZE, &array_size)==0) { - larray_size = array_size; - larray_size <<= 9; - } else larray_size = 0; + if (!get_dev_size(fd, NULL, &larray_size)) + larray_size = 0; } if (vers < 0) diff --git a/mdadm.h b/mdadm.h index 318edd47..f88eb23f 100644 --- a/mdadm.h +++ b/mdadm.h @@ -344,6 +344,7 @@ struct supertype { extern struct supertype *super_by_version(int vers, int minor); extern struct supertype *guess_super(int fd); +extern int get_dev_size(int fd, char *dname, unsigned long long *sizep); #if __GNUC__ < 3 struct stat64; diff --git a/super0.c b/super0.c index 2408c25b..770d96e9 100644 --- a/super0.c +++ b/super0.c @@ -588,20 +588,12 @@ static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo) static int store_super0(struct supertype *st, int fd, void *sbv) { - unsigned long size; unsigned long long dsize; unsigned long long offset; mdp_super_t *super = sbv; - -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) - return 1; - else - dsize = ((unsigned long long)size)<<9; - } + + if (!get_dev_size(fd, NULL, &dsize)) + return 1; if (dsize < MD_RESERVED_SECTORS*2*512) return 2; @@ -708,20 +700,10 @@ static int load_super0(struct supertype *st, int fd, void **sbp, char *devname) int uuid[4]; struct bitmap_super_s *bsb; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) { - if (devname) - fprintf(stderr, Name ": cannot find device size for %s: %s\n", - devname, strerror(errno)); - return 1; - } else - dsize = size << 9; - } + if (!get_dev_size(fd, devname, &dsize)) + return 1; - if (dsize < MD_RESERVED_SECTORS*2) { + if (dsize < MD_RESERVED_SECTORS*512 * 2) { if (devname) fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n", devname, size); @@ -877,19 +859,12 @@ static int add_internal_bitmap0(struct supertype *st, void *sbv, int *chunkp, void locate_bitmap0(struct supertype *st, int fd, void *sbv) { unsigned long long dsize; - unsigned long size; unsigned long long offset; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) - return; - else - dsize = ((unsigned long long)size)<<9; - } - if (dsize < MD_RESERVED_SECTORS*2) + if (!get_dev_size(fd, NULL, &dsize)) + return; + + if (dsize < MD_RESERVED_SECTORS*512 * 2) return; offset = MD_NEW_SIZE_SECTORS(dsize>>9); @@ -903,7 +878,6 @@ void locate_bitmap0(struct supertype *st, int fd, void *sbv) int write_bitmap0(struct supertype *st, int fd, void *sbv) { - unsigned long size; unsigned long long dsize; unsigned long long offset; mdp_super_t *sb = sbv; @@ -913,17 +887,11 @@ int write_bitmap0(struct supertype *st, int fd, void *sbv) int towrite, n; char buf[4096]; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) - return 1; - else - dsize = ((unsigned long long)size)<<9; - } + if (!get_dev_size(fd, NULL, &dsize)) + return 1; + - if (dsize < MD_RESERVED_SECTORS*2) + if (dsize < MD_RESERVED_SECTORS*512 * 2) return -1; offset = MD_NEW_SIZE_SECTORS(dsize>>9); diff --git a/super1.c b/super1.c index 14f9c8af..f1732f7b 100644 --- a/super1.c +++ b/super1.c @@ -696,19 +696,12 @@ static int store_super1(struct supertype *st, int fd, void *sbv) struct mdp_superblock_1 *sb = sbv; unsigned long long sb_offset; int sbsize; - unsigned long size; unsigned long long dsize; -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) - return 1; - else - dsize = (unsigned long long)size; - } else - dsize >>= 9; + if (!get_dev_size(fd, NULL, &dsize)) + return 1; + + dsize >>= 9; if (dsize < 24) return 2; @@ -820,17 +813,9 @@ static int write_init_super1(struct supertype *st, void *sbv, free(refsb); } -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - unsigned long size; - if (ioctl(fd, BLKGETSIZE, &size)) - return 1; - else - dsize = size; - } else - dsize >>= 9; + if (!get_dev_size(fd, NULL, &dsize)) + return 1; + dsize >>= 9; if (dsize < 24) { close(fd); @@ -937,7 +922,6 @@ static int compare_super1(void **firstp, void *secondv) static int load_super1(struct supertype *st, int fd, void **sbp, char *devname) { - unsigned long size; unsigned long long dsize; unsigned long long sb_offset; struct mdp_superblock_1 *super; @@ -978,19 +962,9 @@ static int load_super1(struct supertype *st, int fd, void **sbp, char *devname) st->ss = NULL; return 2; } -#ifdef BLKGETSIZE64 - if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) -#endif - { - if (ioctl(fd, BLKGETSIZE, &size)) { - if (devname) - fprintf(stderr, Name ": cannot find device size for %s: %s\n", - devname, strerror(errno)); - return 1; - } - dsize = size; - } else - dsize >>= 9; + if (!get_dev_size(fd, devname, &dsize)) + return 1; + dsize >>= 9; if (dsize < 24) { if (devname) diff --git a/test b/test index f0a4388f..a83b68e6 100644 --- a/test +++ b/test @@ -65,6 +65,7 @@ fi # mdadm always adds --quiet, and we want to see any unexpected messages mdadm() { + rm -f $targetdir/stderr case $* in *-C* ) $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes;; * ) $mdadm 2> $targetdir/stderr --quiet "$@" @@ -161,6 +162,7 @@ for script in tests/$prefix tests/$prefix*[^~] do if [ -f "$script" ] then + rm -f $targetdir/stderr # source script in a subshell, so it has access to our # namespace, but cannot change it. if ( set -ex ; . $script ) 2> $targetdir/log diff --git a/util.c b/util.c index 6148ff8c..ffb113c3 100644 --- a/util.c +++ b/util.c @@ -773,6 +773,28 @@ struct supertype *guess_super(int fd) return NULL; } +/* Return size of device in bytes */ +int get_dev_size(int fd, char *dname, unsigned long long *sizep) +{ + unsigned long long ldsize; +#ifdef BLKGETSIZE64 + if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) +#endif + { + unsigned long dsize; + if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { + ldsize = dsize; + ldsize <<= 9; + } else { + if (dname) + fprintf(stderr, Name ": Cannot get size of %s: %s\b", + dname, strerror(errno)); + return 0; + } + } + *sizep = ldsize; + return 1; +} #ifdef __TINYC__ /* tinyc doesn't optimize this check in ioctl.h out ... */ -- 2.39.2