Instead of opencoding the same thing everywhere.
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);
}
/* 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;
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;
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) {
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)
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;
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",
}
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;
}
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",
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);
struct supertype *st = NULL;
unsigned long long larray_size;
- unsigned long array_size;
struct stat stb;
char *mddev;
mdu_disk_info_t disc;
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)
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;
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;
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);
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);
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;
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);
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;
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);
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;
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)
# 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 "$@"
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
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 ... */