#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
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;
}
}
}
+#ifndef MDASSEMBLE
int check_ext2(int fd, char *name)
{
/*
fprintf(stderr, Name ": assuming 'no'\n");
return 0;
}
+#endif /* MDASSEMBLE */
char *map_num(mapping_t *map, int num)
{
} *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 add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
+{
+ return 0;
+}
+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.
char *std = NULL, *nonstd=NULL;
int did_check = 0;
- if (major == 0 && minor == 0) {
- if (!create)
+ if (major == 0 && minor == 0)
return NULL;
- else
- return "0:0";
- }
+
retry:
if (!devlist_ready) {
char *dev = "/dev";
return nonstd ? nonstd : std;
}
-#endif
-
unsigned long calc_csum(void *super, int bytes)
{
unsigned long long newcsum = 0;
return csum;
}
+#ifndef MDASSEMBLE
char *human_size(long long bytes)
{
static char buf[30];
);
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) {
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 ... */