return 0;
}
-int Assemble(struct supertype *st, char *mddev, int mdfd,
+int Assemble(struct supertype *st, char *mddev,
mddev_ident_t ident,
mddev_dev_t devlist, char *backup_file,
int readonly, int runstop,
* START_ARRAY
*
*/
- int clean = 0;
- int must_close = 0;
+ int mdfd;
+ int clean;
+ int auto_assem = (mddev == NULL);
int old_linux = 0;
- int vers = 0; /* Keep gcc quite - it really is initialised */
+ int vers = vers; /* Keep gcc quite - it really is initialised */
struct {
char *devname;
int uptodate; /* set once we decide that this device is as
int change = 0;
int inargv = 0;
int bitmap_done;
- int start_partial_ok = (runstop >= 0) && (force || devlist==NULL || mdfd < 0);
+ int start_partial_ok = (runstop >= 0) &&
+ (force || devlist==NULL || auto_assem);
unsigned int num_devs;
mddev_dev_t tmpdev;
struct mdinfo info;
+ mdu_array_info_t tmp_inf;
char *avail;
int nextspare = 0;
int uuid_for_name = 0;
if (get_linux_version() < 2004000)
old_linux = 1;
- if (mdfd >= 0) {
- vers = md_get_version(mdfd);
- if (vers <= 0) {
- fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
- return 1;
- }
- if (vers < 9000) {
- fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
- " Upgrade your kernel or try --build\n");
- return 1;
- }
-
- if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
- fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
- mddev);
- return 1;
- }
- ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
- }
/*
* If any subdevs are listed, then any that don't
* match ident are discarded. Remainder must all match and
#endif
if (devlist == NULL)
devlist = conf_get_devs();
- else if (mdfd >= 0)
+ else if (mddev)
inargv = 1;
try_again:
+ /* We come back here when doing auto-assembly and attempting some
+ * set of devices failed. Those are now marked as ->used==2 and
+ * we ignore them and try again
+ */
tmpdev = devlist; num_devs = 0;
while (tmpdev) {
/* first walk the list of devices to find a consistent set
* that match the criterea, if that is possible.
- * We flag the one we like with 'used'.
+ * We flag the ones we like with 'used'.
*/
for (tmpdev = devlist;
tmpdev;
devname);
goto loop;
}
- if (mdfd < 0) {
+ if (auto_assem) {
if (tst == NULL || tst->sb == NULL)
continue;
switch(tst->ss->match_home(tst, homehost))
* Or, if we are auto assembling, we just ignore the second
* for now.
*/
- if (mdfd < 0)
+ if (auto_assem)
goto loop;
if (homehost) {
int first = st->ss->match_home(st, homehost);
tst->ss->free_super(tst);
}
- if (mdfd < 0) {
- /* So... it is up to me to open the device.
- * We create a name '/dev/md/XXX' based on the info in the
- * superblock, and call create_mddev on that
- */
- mdu_array_info_t inf;
- char *c;
+ if (!st || !st->sb)
+ return 2;
+
+ /* Now need to open array the device.
+ * We create a name '/dev/md/XXX' based on the info in the
+ * superblock, and call create_mddev on that
+ */
+
+ if (mddev == NULL) {
char nbuf[64];
+ char *c;
int rc;
- if (!st || !st->sb) {
- return 2;
- }
st->ss->getinfo_super(st, &info);
if (uuid_for_name)
c = fname_from_uuid(st, &info, nbuf, '-');
rc = asprintf(&mddev, "/dev/md/d%s", c);
else
rc = asprintf(&mddev, "/dev/md/%s", c);
- if (rc < 0)
- mdfd = -1;
- else
- mdfd = create_mddev(mddev, ident->autof);
- if (mdfd < 0) {
+ if (rc < 0) {
st->ss->free_super(st);
free(devices);
+ mddev = NULL;
goto try_again;
}
- vers = md_get_version(mdfd);
- if (ioctl(mdfd, GET_ARRAY_INFO, &inf)==0) {
- for (tmpdev = devlist ;
- tmpdev && tmpdev->used != 1;
- tmpdev = tmpdev->next)
- ;
- fprintf(stderr, Name ": %s already active, cannot restart it!\n", mddev);
- if (tmpdev)
- fprintf(stderr, Name ": %s needed for %s...\n",
- mddev, tmpdev->devname);
- close(mdfd);
- mdfd = -1;
- st->ss->free_super(st);
- free(devices);
+ }
+ mdfd = create_mddev(mddev, ident->autof);
+ if (mdfd < 0) {
+ st->ss->free_super(st);
+ free(devices);
+ if (auto_assem)
goto try_again;
- }
- must_close = 1;
+ return 1;
}
+ vers = md_get_version(mdfd);
+ if (vers < 9000) {
+ fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
+ " Upgrade your kernel or try --build\n");
+ close(mdfd);
+ return 1;
+ }
+ if (ioctl(mdfd, GET_ARRAY_INFO, &tmp_inf)==0) {
+ fprintf(stderr, Name ": %s already active, cannot restart it!\n",
+ mddev);
+ for (tmpdev = devlist ;
+ tmpdev && tmpdev->used != 1;
+ tmpdev = tmpdev->next)
+ ;
+ if (tmpdev && auto_assem)
+ fprintf(stderr, Name ": %s needed for %s...\n",
+ mddev, tmpdev->devname);
+ close(mdfd);
+ mdfd = -3;
+ st->ss->free_super(st);
+ free(devices);
+ if (auto_assem)
+ goto try_again;
+ return 1;
+ }
+ ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
/* Ok, no bad inconsistancy, we can try updating etc */
bitmap_done = 0;
inargv ? "the list" :
"the\n DEVICE list in mdadm.conf"
);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if (best[i] == -1
mddev);
if (st)
st->ss->free_super(st);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
fprintf(stderr, Name ": Cannot open %s: %s\n",
devices[j].devname, strerror(errno));
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if (st->ss->load_super(st,fd, NULL)) {
close(fd);
fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
devices[j].devname);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
close(fd);
}
if (st->sb == NULL) {
fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
st->ss->getinfo_super(st, &info);
if (fd < 0) {
fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
devices[chosen_drive].devname);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if (st->ss->store_super(st, fd)) {
close(fd);
fprintf(stderr, Name ": Could not re-write superblock on %s\n",
devices[chosen_drive].devname);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
close(fd);
}
if (err) {
fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
- if (must_close) close(mdfd);
+ close(mdfd);
return err;
}
}
if (rv) {
fprintf(stderr, Name ": failed to set array info for %s: %s\n",
mddev, strerror(errno));
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if (ident->bitmap_fd >= 0) {
if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
} else if (ident->bitmap_file) {
if (bmfd < 0) {
fprintf(stderr, Name ": Could not open bitmap file %s\n",
ident->bitmap_file);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
close(bmfd);
- if (must_close) close(mdfd);
+ close(mdfd);
return 1;
}
close(bmfd);
info.array.raid_disks);
fprintf(stderr, "\n");
}
- if (must_close)
- close(mdfd);
+ close(mdfd);
return 0;
}
fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
fprintf(stderr, ".\n");
}
- if (must_close) {
+ close(mdfd);
+ if (auto_assem) {
int usecs = 1;
- close(mdfd);
/* There is a nasty race with 'mdadm --monitor'.
* If it opens this device before we close it,
* it gets an incomplete open on which IO
"start the array while not clean "
"- consider --force.\n");
- if (must_close) {
+ if (auto_assem)
ioctl(mdfd, STOP_ARRAY, NULL);
- close(mdfd);
- }
+ close(mdfd);
return 1;
}
if (runstop == -1) {
if (okcnt != info.array.raid_disks)
fprintf(stderr, " (out of %d)", info.array.raid_disks);
fprintf(stderr, ", but not started.\n");
- if (must_close) close(mdfd);
+ close(mdfd);
return 0;
}
if (verbose >= -1) {
fprintf(stderr, " (use --run to insist).\n");
}
}
- if (must_close) {
+ if (auto_assem)
ioctl(mdfd, STOP_ARRAY, NULL);
- close(mdfd);
- }
return 1;
} else {
/* The "chosen_drive" is a good choice, and if necessary, the superblock has
}
}
- if (must_close) close(mdfd);
+ close(mdfd);
return 0;
}