+ int fd;
+ chosen_drive = -1;
+ for (i=0; i<info.array.raid_disks && i < bestcnt; i++) {
+ int j = best[i];
+ if (j>=0 &&
+ !devices[j].uptodate &&
+ devices[j].events > 0 &&
+ (chosen_drive < 0 ||
+ devices[j].events > devices[chosen_drive].events))
+ chosen_drive = j;
+ }
+ if (chosen_drive < 0)
+ break;
+ if (verbose >= 0)
+ fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
+ devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
+ (int)(devices[chosen_drive].events),
+ (int)(devices[most_recent].events));
+ fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
+ if (fd < 0) {
+ fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
+ devices[chosen_drive].devname);
+ devices[chosen_drive].events = 0;
+ continue;
+ }
+ if (st->ss->load_super(st,fd, &super, NULL)) {
+ close(fd);
+ fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
+ devices[chosen_drive].devname);
+ devices[chosen_drive].events = 0;
+ continue;
+ }
+ info.events = devices[most_recent].events;
+ st->ss->update_super(&info, super, "force", devices[chosen_drive].devname, verbose, 0, NULL);
+
+ if (st->ss->store_super(st, fd, super)) {
+ close(fd);
+ fprintf(stderr, Name ": Could not re-write superblock on %s\n",
+ devices[chosen_drive].devname);
+ devices[chosen_drive].events = 0;
+ free(super);
+ continue;
+ }
+ close(fd);
+ devices[chosen_drive].events = devices[most_recent].events;
+ devices[chosen_drive].uptodate = 1;
+ avail[chosen_drive] = 1;
+ okcnt++;
+ free(super);
+ }
+
+ /* Now we want to look at the superblock which the kernel will base things on
+ * and compare the devices that we think are working with the devices that the
+ * superblock thinks are working.
+ * If there are differences and --force is given, then update this chosen
+ * superblock.
+ */
+ chosen_drive = -1;
+ super = NULL;
+ for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
+ int j = best[i];
+ int fd;
+
+ if (j<0)
+ continue;
+ if (!devices[j].uptodate)
+ continue;
+ chosen_drive = j;
+ 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);
+ return 1;
+ }
+ if (st->ss->load_super(st,fd, &super, NULL)) {
+ close(fd);
+ fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
+ devices[j].devname);
+ if (must_close) close(mdfd);
+ return 1;
+ }
+ close(fd);
+ }
+ if (super == NULL) {
+ fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
+ if (must_close) close(mdfd);
+ return 1;
+ }
+ st->ss->getinfo_super(&info, super);
+ for (i=0; i<bestcnt; i++) {
+ int j = best[i];
+ unsigned int desired_state;
+
+ if (i < info.array.raid_disks)
+ desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
+ else
+ desired_state = 0;
+
+ if (j<0)
+ continue;
+ if (!devices[j].uptodate)
+ continue;
+ info.disk.number = devices[j].disk_nr;
+ info.disk.raid_disk = i;
+ info.disk.state = desired_state;
+
+ if (devices[j].uptodate &&
+ st->ss->update_super(&info, super, "assemble", NULL, verbose, 0, NULL)) {
+ if (force) {
+ if (verbose >= 0)
+ fprintf(stderr, Name ": "
+ "clearing FAULTY flag for device %d in %s for %s\n",
+ j, mddev, devices[j].devname);
+ change = 1;
+ } else {
+ if (verbose >= -1)
+ fprintf(stderr, Name ": "
+ "device %d in %s has wrong state in superblock, but %s seems ok\n",
+ i, mddev, devices[j].devname);
+ }
+ }
+#if 0
+ if (!devices[j].uptodate &&
+ !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
+ fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
+ i, mddev);
+ }
+#endif
+ }
+ if (force && okcnt == info.array.raid_disks-1) {
+ /* FIXME check event count */
+ change += st->ss->update_super(&info, super, "force",
+ devices[chosen_drive].devname, verbose, 0, NULL);
+ }
+
+ if (change) {
+ int fd;
+ fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
+ 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);
+ return 1;
+ }
+ if (st->ss->store_super(st, fd, super)) {
+ close(fd);
+ fprintf(stderr, Name ": Could not re-write superblock on %s\n",
+ devices[chosen_drive].devname);
+ if (must_close) close(mdfd);
+ return 1;
+ }
+ close(fd);
+ }
+
+ /* If we are in the middle of a reshape we may need to restore saved data
+ * that was moved aside due to the reshape overwriting live data
+ * The code of doing this lives in Grow.c
+ */
+#ifndef MDASSEMBLE
+ if (info.reshape_active) {
+ int err = 0;
+ int *fdlist = malloc(sizeof(int)* bestcnt);
+ for (i=0; i<bestcnt; i++) {
+ int j = best[i];
+ if (j >= 0) {
+ fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
+ if (fdlist[i] < 0) {
+ fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
+ devices[j].devname);
+ err = 1;
+ break;
+ }
+ } else
+ fdlist[i] = -1;
+ }
+ if (!err)
+ err = Grow_restart(st, &info, fdlist, bestcnt, backup_file);
+ while (i>0) {
+ i--;
+ if (fdlist[i]>=0) close(fdlist[i]);
+ }
+ if (err) {
+ fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
+ if (must_close) close(mdfd);
+ return err;
+ }