+ int fd;
+ struct supertype *tst;
+ long long current_events;
+ 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].i.events > 0 &&
+ (chosen_drive < 0 ||
+ devices[j].i.events
+ > devices[chosen_drive].i.events))
+ chosen_drive = j;
+ }
+ if (chosen_drive < 0)
+ break;
+ current_events = devices[chosen_drive].i.events;
+ add_another:
+ if (verbose >= 0)
+ fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
+ devices[chosen_drive].devname,
+ devices[chosen_drive].i.disk.raid_disk,
+ (int)(devices[chosen_drive].i.events),
+ (int)(devices[most_recent].i.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].i.events = 0;
+ continue;
+ }
+ tst = dup_super(st);
+ if (tst->ss->load_super(tst,fd, NULL)) {
+ close(fd);
+ fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
+ devices[chosen_drive].devname);
+ devices[chosen_drive].i.events = 0;
+ continue;
+ }
+ info.events = devices[most_recent].i.events;
+ tst->ss->update_super(tst, &info, "force-one",
+ devices[chosen_drive].devname, verbose,
+ 0, NULL);
+
+ if (tst->ss->store_super(tst, fd)) {
+ close(fd);
+ fprintf(stderr, Name ": Could not re-write superblock on %s\n",
+ devices[chosen_drive].devname);
+ devices[chosen_drive].i.events = 0;
+ tst->ss->free_super(tst);
+ continue;
+ }
+ close(fd);
+ devices[chosen_drive].i.events = devices[most_recent].i.events;
+ devices[chosen_drive].uptodate = 1;
+ avail[chosen_drive] = 1;
+ okcnt++;
+ tst->ss->free_super(tst);
+
+ /* If there are any other drives of the same vintage,
+ * add them in as well. We can't lose and we might gain
+ */
+ for (i=0; i<info.array.raid_disks && i < bestcnt ; i++) {
+ int j = best[i];
+ if (j >= 0 &&
+ !devices[j].uptodate &&
+ devices[j].i.events > 0 &&
+ devices[j].i.events == current_events) {
+ chosen_drive = j;
+ goto add_another;
+ }
+ }
+ }
+
+ /* 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;
+ st->ss->free_super(st);
+ 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, 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 (st->sb == NULL) {
+ fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
+ if (must_close) close(mdfd);
+ return 1;
+ }
+ st->ss->getinfo_super(st, &info);
+ 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;
+
+ devices[j].i.disk.state = desired_state;
+
+ if (st->ss->update_super(st, &devices[j].i, "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 (!(super.disks[i].i.disk.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