static int count_active(struct supertype *st, int mdfd, char **availp,
struct mdinfo *info);
-static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
+static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
int number, __u64 events, int verbose,
char *array_name);
struct supertype *st, char *homehost, int autof)
{
/* Add this device to an array, creating the array if necessary
- * and starting the array if sensibe or - if runstop>0 - if possible.
+ * and starting the array if sensible or - if runstop>0 - if possible.
*
* This has several steps:
*
close(dfd);
return 1;
}
- st->ss->getinfo_super(st, &info);
close (dfd);
+ if (st->ss->container_content) {
+ /* This is a pre-built container array, so we do something
+ * rather different.
+ */
+ return Incremental_container(st, devname, verbose, runstop,
+ autof);
+ }
+
+ st->ss->getinfo_super(st, &info);
/* 3/ Check if there is a match in mdadm.conf */
array_list = conf_get_ident(NULL);
/* Have to guess a bit. */
int use_partitions = 1;
char *np, *ep;
+ char *nm, nbuf[1024];
if ((autof&7) == 3 || (autof&7) == 5)
use_partitions = 0;
np = strchr(info.name, ':');
} else
devnum = -1;
+ if (match)
+ nm = match->devname;
+ else {
+ sprintf(nbuf, "/dev/md/%s", np);
+ nm = nbuf;
+ }
+ if (stat(nm, &stb) == 0 &&
+ S_ISBLK(stb.st_mode) &&
+ major(stb.st_rdev) == (use_partitions ?
+ get_mdp_major() : MD_MAJOR)) {
+ if (use_partitions)
+ devnum = minor(stb.st_rdev) >> MdpMinorShift;
+ else
+ devnum = minor(stb.st_rdev);
+ if (mddev_busy(use_partitions ? (-1-devnum) : devnum))
+ devnum = -1;
+ }
+
if (devnum < 0) {
/* Haven't found anything yet, choose something free */
- /* There is similar code in mdopen.c - should unify */
- for (devnum = 127 ; devnum != 128 ;
- devnum = devnum ? devnum-1 : (1<<22)-1) {
- if (mddev_busy(use_partitions ?
- (-1-devnum) : devnum))
- break;
- }
- if (devnum == 128) {
+ devnum = find_free_devnum(use_partitions);
+
+ if (devnum == NoMdDev) {
fprintf(stderr, Name
": No spare md devices!!\n");
return 2;
}
- }
- devnum = use_partitions ? (-1-devnum) : devnum;
+ } else
+ devnum = use_partitions ? (-1-devnum) : devnum;
}
mdfd = open_mddev_devnum(match ? match->devname : NULL,
devnum,
info.name,
- chosen_name);
+ chosen_name, autof >> 3);
if (mdfd < 0) {
fprintf(stderr, Name ": failed to open %s: %s.\n",
chosen_name, strerror(errno));
/* - add the device */
mdu_array_info_t ainf;
mdu_disk_info_t disk;
- char md[20];
- struct sysarray *sra;
+ struct mdinfo *sra;
memset(&ainf, 0, sizeof(ainf));
- ainf.major_version = st->ss->major;
- ainf.minor_version = st->minor_version;
+ ainf.major_version = info.array.major_version;
+ ainf.minor_version = info.array.minor_version;
if (ioctl(mdfd, SET_ARRAY_INFO, &ainf) != 0) {
fprintf(stderr, Name
": SET_ARRAY_INFO failed for %s: %s\b",
close(mdfd);
return 2;
}
- sprintf(md, "%d.%d\n", st->ss->major, st->minor_version);
sra = sysfs_read(mdfd, devnum, GET_VERSION);
- sysfs_set_str(sra, NULL, "metadata_version", md);
+ sysfs_set_str(sra, NULL, "metadata_version", info.text_version);
memset(&disk, 0, sizeof(disk));
disk.major = major(stb.st_rdev);
disk.minor = minor(stb.st_rdev);
return 2;
}
sra = sysfs_read(mdfd, devnum, GET_DEVS);
- if (!sra || !sra->devs || sra->devs->role >= 0) {
+ if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
/* It really should be 'none' - must be old buggy
* kernel, and mdadm -I may not be able to complete.
* So reject it.
int dfd2;
mdu_disk_info_t disk;
int err;
- struct sysarray *sra;
+ struct mdinfo *sra;
struct supertype *st2;
- sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS |
- GET_STATE));
- if (sra->major_version != st->ss->major ||
- sra->minor_version != st->minor_version) {
- if (verbose >= 0)
- fprintf(stderr, Name
- ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
- devname, chosen_name,
- sra->major_version, sra->minor_version,
- st->ss->major, st->minor_version);
- close(mdfd);
- return 1;
- }
- sprintf(dn, "%d:%d", sra->devs->major, sra->devs->minor);
+ sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE));
+
+ sprintf(dn, "%d:%d", sra->devs->disk.major,
+ sra->devs->disk.minor);
dfd2 = dev_open(dn, O_RDONLY);
st2 = dup_super(st);
- if (st2->ss->load_super(st2, dfd2, NULL)) {
+ if (st2->ss->load_super(st2, dfd2, NULL) ||
+ st->ss->compare_super(st, st2) != 0) {
fprintf(stderr, Name
- ": Strange error loading metadata for %s.\n",
- chosen_name);
+ ": metadata mismatch between %s and "
+ "chosen array %s\n",
+ devname, chosen_name);
close(mdfd);
close(dfd2);
return 2;
}
/* 6/ Make sure /var/run/mdadm.map contains this array. */
map_update(&map, devnum,
- info.array.major_version,
- info.array.minor_version,
+ info.text_version,
info.uuid, chosen_name);
/* 7/ Is there enough devices to possibly start the array? */
}
}
if (runstop > 0 || active_disks >= info.array.working_disks) {
- struct sysarray *sra;
+ struct mdinfo *sra;
/* Let's try to start it */
if (match && match->bitmap_file) {
int bmfd = open(match->bitmap_file, O_RDWR);
return rv;
}
-static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
+static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
int number, __u64 events, int verbose,
char *array_name)
{
/* Find a device attached to this array with a disk.number of number
* and events less than the passed events, and remove the device.
*/
- struct sysdev *d;
+ struct mdinfo *d;
mdu_array_info_t ra;
if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
char dn[10];
int dfd;
struct mdinfo info;
- sprintf(dn, "%d:%d", d->major, d->minor);
+ sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
dfd = dev_open(dn, O_RDONLY);
if (dfd < 0)
continue;
info.events >= events)
continue;
- if (d->role > -1)
+ if (d->disk.raid_disk > -1)
sysfs_set_str(sra, d, "slot", "none");
if (sysfs_set_str(sra, d, "state", "remove") == 0)
if (verbose >= 0)
fprintf(stderr, Name
": removing old device %s from %s\n",
- d->name+4, array_name);
+ d->sys_name+4, array_name);
}
}
struct mdinfo *bestinfo)
{
/* count how many devices in sra think they are active */
- struct sysdev *d;
+ struct mdinfo *d;
int cnt = 0, cnt1 = 0;
__u64 max_events = 0;
- struct sysarray *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
+ struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
char *avail = NULL;
for (d = sra->devs ; d ; d = d->next) {
int ok;
struct mdinfo info;
- sprintf(dn, "%d:%d", d->major, d->minor);
+ sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
dfd = dev_open(dn, O_RDONLY);
if (dfd < 0)
continue;
int mdp = get_mdp_major();
for (md = mdstat ; md ; md = md->next) {
- struct sysarray *sra = sysfs_read(-1, md->devnum, GET_DEVS);
- struct sysdev *sd;
+ struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
+ struct mdinfo *sd;
for (sd = sra->devs ; sd ; sd = sd->next) {
char dn[30];
char *path;
struct mdinfo info;
- sprintf(dn, "%d:%d", sd->major, sd->minor);
+ sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
dfd = dev_open(dn, O_RDONLY);
if (dfd < 0)
continue;
path = map_dev(MD_MAJOR, md->devnum, 0);
else
path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
- map_add(&map, md->devnum, st->ss->major,
- st->minor_version,
+ map_add(&map, md->devnum,
+ info.text_version,
info.uuid, path ? : "/unknown");
st->ss->free_super(st);
break;
char path[1024];
mdu_array_info_t array;
mdu_bitmap_file_t bmf;
- struct sysarray *sra;
- int mdfd = open_mddev_devnum(me->path, me->devnum, NULL, path);
+ struct mdinfo *sra;
+ int mdfd = open_mddev_devnum(me->path, me->devnum,
+ NULL, path, 0);
if (mdfd < 0)
continue;
if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
}
return rv;
}
+
+static char *container2devname(char *devname)
+{
+ int fd = open(devname, O_RDONLY);
+ char *mdname = NULL;
+
+ if (fd >= 0) {
+ mdname = devnum2devname(fd2devnum(fd));
+ close(fd);
+ }
+
+ return mdname;
+}
+
+int Incremental_container(struct supertype *st, char *devname, int verbose,
+ int runstop, int autof)
+{
+ /* Collect the contents of this container and for each
+ * array, choose a device name and assemble the array.
+ */
+
+ struct mdinfo *list = st->ss->container_content(st);
+ struct mdinfo *ra;
+ char *mdname = container2devname(devname);
+
+ if (!mdname) {
+ fprintf(stderr, Name": failed to determine device name\n");
+ return 2;
+ }
+
+ for (ra = list ; ra ; ra = ra->next) {
+ struct mdinfo *sra;
+ struct mdinfo *dev;
+ int devnum = -1;
+ int mdfd;
+ char chosen_name[1024];
+ int usepart = 1;
+ char *n;
+ int working = 0;
+ char ver[100];
+
+ if ((autof&7) == 3 || (autof&7) == 5)
+ usepart = 0;
+
+ n = ra->name;
+ if (*n == 'd')
+ n++;
+ if (*n) {
+ devnum = strtoul(n, &n, 10);
+ if (devnum >= 0 && (*n == 0 || *n == ' ')) {
+ /* Use this devnum */
+ usepart = (ra->name[0] == 'd');
+ if (mddev_busy(usepart ? (-1-devnum) : devnum))
+ devnum = -1;
+ } else
+ devnum = -1;
+ }
+
+ if (devnum < 0) {
+ char *nm = ra->name;
+ char nbuf[1024];
+ struct stat stb;
+ if (strchr(nm, ':'))
+ nm = strchr(nm, ':')+1;
+ sprintf(nbuf, "/dev/md/%s", nm);
+
+ if (stat(nbuf, &stb) == 0 &&
+ S_ISBLK(stb.st_mode) &&
+ major(stb.st_rdev) == (usepart ?
+ get_mdp_major() : MD_MAJOR)){
+ if (usepart)
+ devnum = minor(stb.st_rdev)
+ >> MdpMinorShift;
+ else
+ devnum = minor(stb.st_rdev);
+ if (mddev_busy(usepart ? (-1-devnum) : devnum))
+ devnum = -1;
+ }
+ }
+
+ if (devnum >= 0)
+ devnum = usepart ? (-1-devnum) : devnum;
+ else
+ devnum = find_free_devnum(usepart);
+ mdfd = open_mddev_devnum(NULL, devnum, ra->name,
+ chosen_name, autof>>3);
+
+ if (mdfd < 0) {
+ fprintf(stderr, Name ": failed to open %s: %s.\n",
+ chosen_name, strerror(errno));
+ return 2;
+ }
+
+ sra = sysfs_read(mdfd, 0, 0);
+
+ sprintf(ver, "external:%s", ra->text_version);
+ sysfs_set_str(sra, NULL, "metadata_version", ver);
+
+ sysfs_set_array(sra, ra);
+ for (dev = ra->devs; dev; dev = dev->next)
+ if (sysfs_add_disk(sra, dev) == 0)
+ working++;
+
+ if (runstop > 0 || working >= ra->array.working_disks) {
+ switch(ra->array.level) {
+ case LEVEL_LINEAR:
+ case LEVEL_MULTIPATH:
+ case 0:
+ sysfs_set_str(sra, NULL, "array_state",
+ "active");
+ break;
+ default:
+ sysfs_set_str(sra, NULL, "array_state",
+ "readonly");
+ /* start mdmon if needed. */
+ if (mdmon_running(st->container_dev))
+ signal_mdmon(st->container_dev);
+ else if (!env_no_mdmon()) {
+ int dn = st->container_dev;
+ int i;
+ switch(fork()) {
+ case 0:
+ /* FIXME yuk. CLOSE_EXEC?? */
+ for (i=3; i < 100; i++)
+ close(i);
+ execl("./mdmon", "mdmon",
+ map_dev(dev2major(dn),
+ dev2minor(dn),
+ 1), NULL);
+ exit(1);
+ case -1: fprintf(stderr, Name
+ ": cannot fork. "
+ "Array remains readonly\n");
+ return 1;
+ default: ; /* parent - good */
+ }
+ }
+ break;
+ }
+ if (verbose >= 0)
+ printf("Started %s with %d devices\n",
+ chosen_name, working);
+ /* FIXME should have an O_EXCL and wait for read-auto */
+ } else
+ if (verbose >= 0)
+ printf("%s assembled with %d devices but "
+ "not started\n",
+ chosen_name, working);
+ close(mdfd);
+ }
+ return 0;
+}