]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
--wait or -W will wait for resync activity to finish on the given devices.
[thirdparty/mdadm.git] / Manage.c
index 8c2da3b99b3253dbc838e77912da4db7b0e3e903..0378b6ee1f68f2446214782911c1a71af0029c6b 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
+ * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
@@ -72,6 +72,8 @@ int Manage_ro(char *devname, int fd, int readonly)
        return 0;                       
 }
 
+#ifndef MDASSEMBLE
+
 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
 {
        /* Run or stop the array. array must already be configured
@@ -106,11 +108,13 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                }
        } else if (runstop < 0){
                if (ioctl(fd, STOP_ARRAY, NULL)) {
-                       if (!quiet)
+                       if (quiet==0)
                                fprintf(stderr, Name ": fail to stop array %s: %s\n",
                                        devname, strerror(errno));
                        return 1;
                }
+               if (quiet <= 0)
+                       fprintf(stderr, Name ": stopped %s\n", devname);
        }
        return 0;
 }
@@ -172,6 +176,9 @@ int Manage_subdevs(char *devname, int fd,
        int tfd;
        struct supertype *st;
        void *dsuper = NULL;
+       void *osuper = NULL; /* original super */
+       int duuid[4];
+       int ouuid[4];
 
        if (ioctl(fd, GET_ARRAY_INFO, &array)) {
                fprintf(stderr, Name ": cannot get array info for %s\n",
@@ -196,6 +203,14 @@ int Manage_subdevs(char *devname, int fd,
                        return 1;
                case 'a':
                        /* add the device - hot or cold */
+                       st = super_by_version(array.major_version,
+                                             array.minor_version);
+                       if (!st) {
+                               fprintf(stderr, Name ": unsupport array - version %d.%d\n",
+                                       array.major_version, array.minor_version);
+                               return 1;
+                       }
+
                        /* Make sure it isn't in use (in 2.6 or later) */
                        tfd = open(dv->devname, O_RDONLY|O_EXCL);
                        if (tfd < 0) {
@@ -203,7 +218,11 @@ int Manage_subdevs(char *devname, int fd,
                                        dv->devname, strerror(errno));
                                return 1;
                        }
+                       if (array.not_persistent==0)
+                               st->ss->load_super(st, tfd, &osuper, NULL);
+                       /* will use osuper later */
                        close(tfd);
+
                        if (array.major_version == 0 &&
                            md_get_version(fd)%100 < 2) {
                                if (ioctl(fd, HOT_ADD_DISK,
@@ -219,42 +238,78 @@ int Manage_subdevs(char *devname, int fd,
                                return 1;
                        }
 
-                       /* need to find a sample superblock to copy, and
-                        * a spare slot to use 
-                        */
-                       st = super_by_version(array.major_version,
-                                             array.minor_version);
-                       if (!st) {
-                               fprintf(stderr, Name ": unsupport array - version %d.%d\n",
-                                       array.major_version, array.minor_version);
-                               return 1;
-                       }
-                       for (j=0; j<st->max_devs; j++) {
-                               char *dev;
-                               int dfd;
-                               disc.number = j;
-                               if (ioctl(fd, GET_DISK_INFO, &disc))
-                                       continue;
-                               if (disc.major==0 && disc.minor==0)
-                                       continue;
-                               if ((disc.state & 4)==0) continue; /* sync */
-                               /* Looks like a good device to try */
-                               dev = map_dev(disc.major, disc.minor);
-                               if (!dev) continue;
-                               dfd = open(dev, O_RDONLY);
-                               if (dfd < 0) continue;
-                               if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
+                       if (array.not_persistent == 0) {
+
+                               /* need to find a sample superblock to copy, and
+                                * a spare slot to use 
+                                */
+                               for (j=0; j<st->max_devs; j++) {
+                                       char *dev;
+                                       int dfd;
+                                       disc.number = j;
+                                       if (ioctl(fd, GET_DISK_INFO, &disc))
+                                               continue;
+                                       if (disc.major==0 && disc.minor==0)
+                                               continue;
+                                       if ((disc.state & 4)==0) continue; /* sync */
+                                       /* Looks like a good device to try */
+                                       dev = map_dev(disc.major, disc.minor, 1);
+                                       if (!dev) continue;
+                                       dfd = dev_open(dev, O_RDONLY);
+                                       if (dfd < 0) continue;
+                                       if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
+                                               close(dfd);
+                                               continue;
+                                       }
+                                       remove_partitions(dfd);
                                        close(dfd);
-                                       continue;
+                                       break;
+                               }
+                               if (!dsuper) {
+                                       fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
+                                       return 1;
+                               }
+                               /* Possibly this device was recently part of the array
+                                * and was temporarily removed, and is now being re-added.
+                                * If so, we can simply re-add it.
+                                */
+                               st->ss->uuid_from_super(duuid, dsuper);
+
+                               /* re-add doesn't work for version-1 superblocks
+                                * before 2.6.18 :-(
+                                */
+                               if (array.major_version == 1 &&
+                                   get_linux_version() <= 2006018)
+                                       ;
+                               else if (osuper) {
+                                       st->ss->uuid_from_super(ouuid, osuper);
+                                       if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
+                                               /* look close enough for now.  Kernel
+                                                * will worry about where a bitmap
+                                                * based reconstruct is possible
+                                                */
+                                               struct mdinfo mdi;
+                                               st->ss->getinfo_super(&mdi, osuper);
+                                               disc.major = major(stb.st_rdev);
+                                               disc.minor = minor(stb.st_rdev);
+                                               disc.number = mdi.disk.number;
+                                               disc.raid_disk = mdi.disk.raid_disk;
+                                               disc.state = mdi.disk.state;
+                                               if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
+                                                       if (verbose >= 0)
+                                                               fprintf(stderr, Name ": re-added %s\n", dv->devname);
+                                                       continue;
+                                               }
+                                               /* fall back on normal-add */
+                                       }
                                }
-                               close(dfd);
-                               break;
-                       }
-                       if (!dsuper) {
-                               fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
-                               return 1;
                        }
-                       for (j=0; j< st->max_devs; j++) {
+                       /* in 2.6.17 and earlier, version-1 superblocks won't
+                        * use the number we write, but will choose a free number.
+                        * we must choose the same free number, which requires
+                        * starting at 'raid_disks' and counting up
+                        */
+                       for (j = array.raid_disks; j< st->max_devs; j++) {
                                disc.number = j;
                                if (ioctl(fd, GET_DISK_INFO, &disc))
                                        break;
@@ -267,11 +322,43 @@ int Manage_subdevs(char *devname, int fd,
                        disc.minor = minor(stb.st_rdev);
                        disc.number =j;
                        disc.state = 0;
+                       if (array.not_persistent==0) {
+                               if (dv->writemostly)
+                                       disc.state |= 1 << MD_DISK_WRITEMOSTLY;
+                               st->ss->add_to_super(dsuper, &disc);
+                               if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
+                                       return 1;
+                       } else if (dv->re_add) {
+                               /*  this had better be raid1.
+                                * As we are "--re-add"ing we must find a spare slot
+                                * to fill.
+                                */
+                               char *used = malloc(array.raid_disks);
+                               memset(used, 0, array.raid_disks);
+                               for (j=0; j< st->max_devs; j++) {
+                                       mdu_disk_info_t disc2;
+                                       disc2.number = j;
+                                       if (ioctl(fd, GET_DISK_INFO, &disc2))
+                                               continue;
+                                       if (disc2.major==0 && disc2.minor==0)
+                                               continue;
+                                       if (disc2.state & 8) /* removed */
+                                               continue;
+                                       if (disc2.raid_disk < 0)
+                                               continue;
+                                       if (disc2.raid_disk > array.raid_disks)
+                                               continue;
+                                       used[disc2.raid_disk] = 1;
+                               }
+                               for (j=0 ; j<array.raid_disks; j++)
+                                       if (!used[j]) {
+                                               disc.raid_disk = j;
+                                               disc.state |= (1<<MD_DISK_SYNC);
+                                               break;
+                                       }
+                       }
                        if (dv->writemostly)
-                               disc.state |= 1 << MD_DISK_WRITEMOSTLY;
-                       st->ss->add_to_super(dsuper, &disc);
-                       if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
-                               return 1;
+                               disc.state |= (1 << MD_DISK_WRITEMOSTLY);
                        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));
@@ -309,3 +396,4 @@ int Manage_subdevs(char *devname, int fd,
        return 0;
        
 }
+#endif