mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
if (mdi &&
mdi->array.major_version == -1 &&
- mdi->array.level > 0 &&
is_subarray(mdi->text_version)) {
char vers[64];
strcpy(vers, "external:");
if (*cp)
*cp = 0;
ping_monitor(vers+10);
+ if (mdi->array.level <= 0)
+ sysfs_set_str(mdi, NULL, "array_state", "active");
}
return 0;
}
}
int Manage_subdevs(char *devname, int fd,
- mddev_dev_t devlist, int verbose, int test)
+ struct mddev_dev *devlist, int verbose, int test,
+ char *update)
{
/* do something to each dev.
* devmode can be
* For 'f' and 'r', the device can also be a kernel-internal
* name such as 'sdb'.
*/
- mddev_dev_t add_devlist = NULL;
+ struct mddev_dev *add_devlist = NULL;
mdu_array_info_t array;
mdu_disk_info_t disc;
unsigned long long array_size;
- mddev_dev_t dv, next = NULL;
+ struct mddev_dev *dv, *next = NULL;
struct stat stb;
int j, jnext = 0;
int tfd = -1;
struct supertype *st, *tst;
+ char *subarray = NULL;
int duuid[4];
int ouuid[4];
int lfd = -1;
if (array_size <= 0)
array_size = array.size * 2;
- tst = super_by_fd(fd);
+ tst = super_by_fd(fd, &subarray);
if (!tst) {
fprintf(stderr, Name ": unsupport array - version %d.%d\n",
array.major_version, array.minor_version);
return 1;
case 'a':
/* add the device */
- if (tst->subarray[0]) {
+ if (subarray) {
fprintf(stderr, Name ": Cannot add disks to a"
" \'member\' array, perform this"
" operation on the parent container\n");
if (tst->sb)
/* already loaded */;
else if (tst->ss->external) {
- tst->ss->load_super(tst, fd, NULL);
+ tst->ss->load_container(tst, fd, NULL);
} else for (j = 0; j < tst->max_devs; j++) {
char *dev;
int dfd;
remove_partitions(tfd);
close(tfd);
tfd = -1;
+ if (update) {
+ int rv = -1;
+ tfd = dev_open(dv->devname, O_RDWR);
+
+ if (tfd >= 0)
+ rv = st->ss->update_super(
+ st, NULL, update,
+ devname, verbose, 0, NULL);
+ if (rv == 0)
+ rv = tst->ss->store_super(st, tfd);
+ close(tfd);
+ tfd = -1;
+ if (rv != 0) {
+ fprintf(stderr, Name ": failed to update"
+ " superblock during re-add\n");
+ return 1;
+ }
+ }
/* don't even try if disk is marked as faulty */
errno = 0;
if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
skip_re_add:
re_add_failed = 1;
}
+ st->ss->free_super(st);
}
if (add_dev != dv->devname) {
if (verbose > 0)
if (dv->writemostly == 1)
disc.state |= 1 << MD_DISK_WRITEMOSTLY;
dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
+ if (tst->ss->external &&
+ mdmon_running(tst->container_dev))
+ tst->update_tail = &tst->updates;
if (tst->ss->add_to_super(tst, &disc, dfd,
dv->devname)) {
close(dfd);
return 1;
}
- /* write_init_super will close 'dfd' */
- if (tst->ss->external)
- /* mdmon will write the metadata */
+ if (tst->ss->write_init_super(tst)) {
close(dfd);
- else if (tst->ss->write_init_super(tst))
return 1;
+ }
} else if (dv->re_add) {
/* this had better be raid1.
* As we are "--re-add"ing we must find a spare slot
if (dv->writemostly == 1)
disc.state |= (1 << MD_DISK_WRITEMOSTLY);
if (tst->ss->external) {
- /* add a disk to an external metadata container
- * only if mdmon is around to see it
- */
+ /* add a disk
+ * to an external metadata container */
struct mdinfo new_mdi;
struct mdinfo *sra;
int container_fd;
fprintf(stderr, Name ": add failed for %s:"
" could not get exclusive access to container\n",
dv->devname);
- return 1;
- }
-
- if (!mdmon_running(devnum)) {
- fprintf(stderr, Name ": add failed for %s: mdmon not running\n",
- dv->devname);
- close(container_fd);
+ tst->ss->free_super(tst);
return 1;
}
fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
dv->devname);
close(container_fd);
+ tst->ss->free_super(tst);
return 1;
}
sra->array.level = LEVEL_CONTAINER;
new_mdi.disk.major = disc.major;
new_mdi.disk.minor = disc.minor;
new_mdi.recovery_start = 0;
+ /* Make sure fds are closed as they are O_EXCL which
+ * would block add_disk */
+ tst->ss->free_super(tst);
if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
fprintf(stderr, Name ": add new device to external metadata"
" failed for %s\n", dv->devname);
close(container_fd);
+ sysfs_free(sra);
return 1;
}
ping_monitor(devnum2devname(devnum));
sysfs_free(sra);
close(container_fd);
- } else if (ioctl(fd, ADD_NEW_DISK, &disc)) {
- fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
- dv->devname, j, strerror(errno));
- return 1;
+ } else {
+ tst->ss->free_super(tst);
+ if (ioctl(fd, ADD_NEW_DISK, &disc)) {
+ fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
+ dv->devname, j, strerror(errno));
+ return 1;
+ }
}
if (verbose >= 0)
fprintf(stderr, Name ": added %s\n", dv->devname);
case 'r':
/* hot remove */
- if (tst->subarray[0]) {
+ if (subarray) {
fprintf(stderr, Name ": Cannot remove disks from a"
" \'member\' array, perform this"
" operation on the parent container\n");
return rv;
}
-int Update_subarray(char *dev, char *subarray, char *update, mddev_ident_t ident, int quiet)
+int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
{
struct supertype supertype, *st = &supertype;
int fd, rv = 2;
if (mdmon_running(st->devnum))
st->update_tail = &st->updates;
- rv = st->ss->update_subarray(st, update, ident);
+ rv = st->ss->update_subarray(st, subarray, update, ident);
if (rv) {
if (!quiet)
return rv;
}
+
+/* Move spare from one array to another
+ * If adding to destination array fails
+ * add back to original array
+ * Returns 1 on success, 0 on failure */
+int move_spare(char *from_devname, char *to_devname, dev_t devid)
+{
+ struct mddev_dev devlist;
+ char devname[20];
+
+ /* try to remove and add */
+ int fd1 = open(to_devname, O_RDONLY);
+ int fd2 = open(from_devname, O_RDONLY);
+
+ if (fd1 < 0 || fd2 < 0) {
+ if (fd1>=0) close(fd1);
+ if (fd2>=0) close(fd2);
+ return 0;
+ }
+
+ devlist.next = NULL;
+ devlist.used = 0;
+ devlist.re_add = 0;
+ devlist.writemostly = 0;
+ devlist.devname = devname;
+ sprintf(devname, "%d:%d", major(devid), minor(devid));
+
+ devlist.disposition = 'r';
+ if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL) == 0) {
+ devlist.disposition = 'a';
+ if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL) == 0) {
+ /* make sure manager is aware of changes */
+ ping_manager(to_devname);
+ ping_manager(from_devname);
+ close(fd1);
+ close(fd2);
+ return 1;
+ }
+ else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL);
+ }
+ close(fd1);
+ close(fd2);
+ return 0;
+}
#endif