#include <sys/utsname.h>
#include <ctype.h>
+/*
+ * following taken from linux/blkpg.h because they aren't
+ * anywhere else and it isn't safe to #include linux/ * stuff.
+ */
+
+#define BLKPG _IO(0x12,105)
+
+/* The argument structure */
+struct blkpg_ioctl_arg {
+ int op;
+ int flags;
+ int datalen;
+ void *data;
+};
+
+/* The subfunctions (for the op field) */
+#define BLKPG_ADD_PARTITION 1
+#define BLKPG_DEL_PARTITION 2
+
+/* Sizes of name fields. Unused at present. */
+#define BLKPG_DEVNAMELTH 64
+#define BLKPG_VOLNAMELTH 64
+
+/* The data structure for ADD_PARTITION and DEL_PARTITION */
+struct blkpg_partition {
+ long long start; /* starting offset in bytes */
+ long long length; /* length in bytes */
+ int pno; /* partition number */
+ char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
+ to be used in kernel messages */
+ char volname[BLKPG_VOLNAMELTH]; /* volume label */
+};
+
/*
* Parse a 128 bit uuid in 4 integers
* format is 32 hexx nibbles with options :.<space> separator
*/
int parse_uuid(char *str, int uuid[4])
{
- int hit = 0; /* number of Hex digIT */
- int i;
- char c;
- for (i=0; i<4; i++) uuid[i]=0;
-
- while ((c= *str++)) {
- int n;
- if (c>='0' && c<='9')
- n = c-'0';
- else if (c>='a' && c <= 'f')
- n = 10 + c - 'a';
- else if (c>='A' && c <= 'F')
- n = 10 + c - 'A';
- else if (strchr(":. -", c))
- continue;
- else return 0;
-
- if (hit<32) {
- uuid[hit/8] <<= 4;
- uuid[hit/8] += n;
+ int hit = 0; /* number of Hex digIT */
+ int i;
+ char c;
+ for (i=0; i<4; i++) uuid[i]=0;
+
+ while ((c= *str++)) {
+ int n;
+ if (c>='0' && c<='9')
+ n = c-'0';
+ else if (c>='a' && c <= 'f')
+ n = 10 + c - 'a';
+ else if (c>='A' && c <= 'F')
+ n = 10 + c - 'A';
+ else if (strchr(":. -", c))
+ continue;
+ else return 0;
+
+ if (hit<32) {
+ uuid[hit/8] <<= 4;
+ uuid[hit/8] += n;
+ }
+ hit++;
}
- hit++;
- }
- if (hit == 32)
- return 1;
- return 0;
-
+ if (hit == 32)
+ return 1;
+ return 0;
}
return -1;
}
-
int get_linux_version()
{
struct utsname name;
return (a*1000000)+(b*1000)+c;
}
-int enough(int level, int raid_disks, int layout,
+void remove_partitions(int fd)
+{
+ /* remove partitions from this block devices.
+ * This is used for components added to an array
+ */
+#ifdef BLKPG_DEL_PARTITION
+ struct blkpg_ioctl_arg a;
+ struct blkpg_partition p;
+
+ a.op = BLKPG_DEL_PARTITION;
+ a.data = (void*)&p;
+ a.datalen = sizeof(p);
+ a.flags = 0;
+ memset(a.data, 0, a.datalen);
+ for (p.pno=0; p.pno < 16; p.pno++)
+ ioctl(fd, BLKPG, &a);
+#endif
+}
+
+int enough(int level, int raid_disks, int layout, int clean,
char *avail, int avail_disks)
{
int copies, first;
/* This is the tricky one - we need to check
* which actual disks are present.
*/
- copies = (layout&255)* (layout>>8);
+ copies = (layout&255)* ((layout>>8) & 255);
first=0;
do {
/* there must be one of the 'copies' form 'first' */
return avail_disks >= 1;
case 4:
case 5:
- return avail_disks >= raid_disks-1;
+ if (clean)
+ return avail_disks >= raid_disks-1;
+ else
+ return avail_disks >= raid_disks;
case 6:
- return avail_disks >= raid_disks-2;
+ if (clean)
+ return avail_disks >= raid_disks-2;
+ else
+ return avail_disks >= raid_disks;
default:
return 0;
}
if (swapuuid) {
/* parse uuids are hostendian.
* uuid's from some superblocks are big-ending
- * if there is a difference, we need to swap..
+ * if there is a difference, we need to swap..
*/
unsigned char *ac = (unsigned char *)a;
unsigned char *bc = (unsigned char *)b;
return 0;
}
}
+void copy_uuid(void *a, int b[4], int swapuuid)
+{
+ if (swapuuid) {
+ /* parse uuids are hostendian.
+ * uuid's from some superblocks are big-ending
+ * if there is a difference, we need to swap..
+ */
+ unsigned char *ac = (unsigned char *)a;
+ unsigned char *bc = (unsigned char *)b;
+ int i;
+ for (i=0; i<16; i+= 4) {
+ ac[i+0] = bc[i+3];
+ ac[i+1] = bc[i+2];
+ ac[i+2] = bc[i+1];
+ ac[i+3] = bc[i+0];
+ }
+ } else
+ memcpy(a, b, 16);
+}
+#ifndef MDASSEMBLE
int check_ext2(int fd, char *name)
{
/*
fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
fprintf(stderr, " size = %luK\n", size*4);
-
+
return 1;
}
int check_raid(int fd, char *name)
{
- void *super;
struct mdinfo info;
time_t crtime;
char *level;
struct supertype *st = guess_super(fd);
if (!st) return 0;
- st->ss->load_super(st, fd, &super, name);
+ st->ss->load_super(st, fd, name);
/* Looks like a raid array .. */
fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
name);
- st->ss->getinfo_super(&info, super);
- free(super);
+ st->ss->getinfo_super(st, &info);
+ st->ss->free_super(st);
crtime = info.array.ctime;
level = map_num(pers, info.array.level);
if (!level) level = "-unknown-";
fprintf(stderr, Name ": assuming 'no'\n");
return 0;
}
+#endif /* MDASSEMBLE */
char *map_num(mapping_t *map, int num)
{
{
/* tests if dev is a "standard" md dev name.
* i.e if the last component is "/dNN" or "/mdNN",
- * where NN is a string of digits
+ * where NN is a string of digits
*/
char *d = strrchr(dev, '/');
int type=0;
} *devlist = NULL;
int devlist_ready = 0;
-#ifdef UCLIBC
-int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
-{
- return 0;
-}
-char *map_dev(int major, int minor, int create)
-{
-#if 0
- fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
- major, minor);
-#endif
- return NULL;
-}
-#else
-
-#ifdef __dietlibc__
-int add_dev_1(const char *name, const struct stat *stb, int flag)
-{
- return add_dev(name, stb, flag, NULL);
-}
-int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
-{
- return ftw(path, add_dev_1, nopenfd);
-}
-#endif
-
int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
{
struct stat st;
return 0;
}
+#ifndef HAVE_NFTW
+#ifdef HAVE_FTW
+int add_dev_1(const char *name, const struct stat *stb, int flag)
+{
+ return add_dev(name, stb, flag, NULL);
+}
+int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
+{
+ return ftw(path, add_dev_1, nopenfd);
+}
+#else
+int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
+{
+ return 0;
+}
+#endif /* HAVE_FTW */
+#endif /* HAVE_NFTW */
+
/*
* Find a block device with the right major/minor number.
* If we find multiple names, choose the shortest.
struct devmap *p;
char *std = NULL, *nonstd=NULL;
int did_check = 0;
+
+ if (major == 0 && minor == 0)
+ return NULL;
+
retry:
if (!devlist_ready) {
char *dev = "/dev";
struct stat stb;
+ while(devlist) {
+ struct devmap *d = devlist;
+ devlist = d->next;
+ free(d->name);
+ free(d);
+ }
if (lstat(dev, &stb)==0 &&
S_ISLNK(stb.st_mode))
dev = "/dev/.";
return nonstd ? nonstd : std;
}
-#endif
-
unsigned long calc_csum(void *super, int bytes)
{
unsigned long long newcsum = 0;
newcsum+= superc[i];
csum = (newcsum& 0xffffffff) + (newcsum>>32);
#ifdef __alpha__
-/* The in-kernel checksum calculation is always 16bit on
+/* The in-kernel checksum calculation is always 16bit on
* the alpha, though it is 32 bit on i386...
* I wonder what it is elsewhere... (it uses and API in
* a way that it shouldn't).
return csum;
}
+#ifndef MDASSEMBLE
char *human_size(long long bytes)
{
static char buf[30];
char *human_size_brief(long long bytes)
{
static char buf[30];
-
if (bytes < 5000*1024)
snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
);
return buf;
}
+#endif
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
int get_mdp_major(void)
{
static int mdp_major = -1;
if (strncmp(name, "/dev/.tmp.md", 12)==0)
unlink(name);
}
+#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
int dev_open(char *dev, int flags)
{
char *e;
int fd = -1;
char devname[32];
- int major = strtoul(dev, &e, 0);
+ int major;
int minor;
+
+ if (!dev) return -1;
+
+ major = strtoul(dev, &e, 0);
if (e > dev && *e == ':' && e[1] &&
(minor = strtoul(e+1, &e, 0)) >= 0 &&
*e == 0) {
struct superswitch *superlist[] = { &super0, &super1, NULL };
-struct supertype *super_by_version(int vers, int minor)
+struct supertype *super_by_fd(int fd)
{
- struct supertype *st = malloc(sizeof(*st));
- if (!st) return st;
- if (vers == 0) {
- st->ss = &super0;
- st->max_devs = MD_SB_DISKS;
+ mdu_array_info_t array;
+ int vers;
+ int minor;
+ struct supertype *st = NULL;
+ struct mdinfo *sra;
+ char *verstr;
+ char version[20];
+ int i;
+
+ sra = sysfs_read(fd, 0, GET_VERSION);
+
+ if (sra) {
+ vers = sra->array.major_version;
+ minor = sra->array.minor_version;
+ verstr = sra->text_version;
+ } else {
+ if (ioctl(fd, GET_ARRAY_INFO, &array))
+ array.major_version = array.minor_version = 0;
+ vers = array.major_version;
+ minor = array.minor_version;
+ verstr = "";
}
- if (vers == 1) {
- st->ss = &super1;
- st->max_devs = 384;
+ if (vers != -1) {
+ sprintf(version, "%d.%d", vers, minor);
+ verstr = version;
}
- st->minor_version = minor;
+ for (i = 0; st == NULL && superlist[i] ; i++)
+ st = superlist[i]->match_metadata_desc(verstr);
+
+ if (sra)
+ sysfs_free(sra);
+ st->sb = NULL;
return st;
}
+struct supertype *dup_super(struct supertype *st)
+{
+ struct supertype *stnew = NULL;
+ char *verstr = NULL;
+ char version[20];
+ int i;
+
+ if (!st)
+ return st;
+
+ if (st->minor_version == -1)
+ sprintf(version, "%d", st->ss->major);
+ else
+ sprintf(version, "%d.%d", st->ss->major, st->minor_version);
+ verstr = version;
+
+ for (i = 0; stnew == NULL && superlist[i] ; i++)
+ stnew = superlist[i]->match_metadata_desc(verstr);
+
+ stnew->sb = NULL;
+ return stnew;
+}
+
struct supertype *guess_super(int fd)
{
/* try each load_super to find the best match,
struct supertype *st;
unsigned long besttime = 0;
int bestsuper = -1;
-
- void *sbp = NULL;
int i;
st = malloc(sizeof(*st));
int rv;
ss = superlist[i];
st->ss = NULL;
- rv = ss->load_super(st, fd, &sbp, NULL);
+ rv = ss->load_super(st, fd, NULL);
if (rv == 0) {
struct mdinfo info;
- ss->getinfo_super(&info, sbp);
+ st->ss->getinfo_super(st, &info);
if (bestsuper == -1 ||
besttime < info.array.ctime) {
bestsuper = i;
besttime = info.array.ctime;
}
- free(sbp);
+ ss->free_super(st);
}
}
if (bestsuper != -1) {
int rv;
st->ss = NULL;
- rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL);
+ rv = superlist[bestsuper]->load_super(st, fd, NULL);
if (rv == 0) {
- free(sbp);
+ superlist[bestsuper]->free_super(st);
return st;
}
}
return NULL;
}
+/* Return size of device in bytes */
+int get_dev_size(int fd, char *dname, unsigned long long *sizep)
+{
+ unsigned long long ldsize;
+ struct stat st;
+
+ if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
+ ldsize = (unsigned long long)st.st_size;
+ else
+#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;
+}
+
+void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
+{
+ int d;
+ ioctl(mdfd, GET_ARRAY_INFO, ainf);
+ for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
+ if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
+ return;
+}
+
+static int dev2major(int d)
+{
+ if (d >= 0)
+ return MD_MAJOR;
+ else
+ return get_mdp_major();
+}
+
+static int dev2minor(int d)
+{
+ if (d >= 0)
+ return d;
+ return (-1-d) << MdpMinorShift;
+}
+
+int find_free_devnum(int use_partitions)
+{
+ int devnum;
+ for (devnum = 127; devnum != 128;
+ devnum = devnum ? devnum-1 : (1<<22)-1) {
+ char *dn;
+ int _devnum;
+
+ _devnum = use_partitions ? (-1-devnum) : devnum;
+ if (mddev_busy(_devnum))
+ continue;
+ /* make sure it is new to /dev too, at least as a
+ * non-standard */
+ dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
+ if (dn && ! is_standard(dn, NULL))
+ continue;
+ break;
+ }
+ if (devnum == 128)
+ return NoMdDev;
+ return use_partitions ? (-1-devnum) : devnum;
+}
+
#ifdef __TINYC__
/* tinyc doesn't optimize this check in ioctl.h out ... */