]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
Centralise code for copying uuid
[thirdparty/mdadm.git] / Manage.c
index 67e72107f4b97c1e2aacc114a75587e3ec957918..4b5ec80667b90ccd41f84f1640ccff3eaa74b5ab 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;
 }
@@ -182,6 +186,8 @@ int Manage_subdevs(char *devname, int fd,
                return 1;
        }
        for (dv = devlist ; dv; dv=dv->next) {
+               unsigned long long ldsize;
+
                if (stat(dv->devname, &stb)) {
                        fprintf(stderr, Name ": cannot find %s: %s\n",
                                dv->devname, strerror(errno));
@@ -198,7 +204,7 @@ int Manage_subdevs(char *devname, int fd,
                                dv->devname, dv->disposition);
                        return 1;
                case 'a':
-                       /* add the device - hot or cold */
+                       /* add the device */
                        st = super_by_version(array.major_version,
                                              array.minor_version);
                        if (!st) {
@@ -217,6 +223,10 @@ int Manage_subdevs(char *devname, int fd,
                        if (array.not_persistent==0)
                                st->ss->load_super(st, tfd, &osuper, NULL);
                        /* will use osuper later */
+                       if (!get_dev_size(tfd, dv->devname, &ldsize)) {
+                               close(tfd);
+                               return 1;
+                       }
                        close(tfd);
 
                        if (array.major_version == 0 &&
@@ -236,6 +246,14 @@ int Manage_subdevs(char *devname, int fd,
 
                        if (array.not_persistent == 0) {
 
+                               /* Make sure device is large enough */
+                               if (st->ss->avail_size(st, ldsize/512) <
+                                   array.size) {
+                                       fprintf(stderr, Name ": %s not large enough to join array\n",
+                                               dv->devname);
+                                       return 1;
+                               }
+
                                /* need to find a sample superblock to copy, and
                                 * a spare slot to use 
                                 */
@@ -249,14 +267,15 @@ int Manage_subdevs(char *devname, int fd,
                                                continue;
                                        if ((disc.state & 4)==0) continue; /* sync */
                                        /* Looks like a good device to try */
-                                       dev = map_dev(disc.major, disc.minor);
+                                       dev = map_dev(disc.major, disc.minor, 1);
                                        if (!dev) continue;
-                                       dfd = open(dev, O_RDONLY);
+                                       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);
                                        break;
                                }
@@ -269,8 +288,14 @@ int Manage_subdevs(char *devname, int fd,
                                 * If so, we can simply re-add it.
                                 */
                                st->ss->uuid_from_super(duuid, dsuper);
-                       
-                               if (osuper) {
+
+                               /* 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
@@ -278,8 +303,7 @@ int Manage_subdevs(char *devname, int fd,
                                                 * based reconstruct is possible
                                                 */
                                                struct mdinfo mdi;
-                                               struct mddev_ident_s ident;
-                                               st->ss->getinfo_super(&mdi, &ident, osuper);
+                                               st->ss->getinfo_super(&mdi, osuper);
                                                disc.major = major(stb.st_rdev);
                                                disc.minor = minor(stb.st_rdev);
                                                disc.number = mdi.disk.number;
@@ -293,8 +317,22 @@ int Manage_subdevs(char *devname, int fd,
                                                /* fall back on normal-add */
                                        }
                                }
+                       } else {
+                               /* non-persistent. Must ensure that new drive
+                                * is at least array.size big.
+                                */
+                               if (ldsize/512 < array.size) {
+                                       fprintf(stderr, Name ": %s not large enough to join array\n",
+                                               dv->devname);
+                                       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;
@@ -342,6 +380,8 @@ int Manage_subdevs(char *devname, int fd,
                                                break;
                                        }
                        }
+                       if (dv->writemostly)
+                               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));
@@ -379,3 +419,4 @@ int Manage_subdevs(char *devname, int fd,
        return 0;
        
 }
+#endif