]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Add 'supertype' arg to almost all metadata methods.
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index 742f656f02f72b0aefa7e1e8bdc1bf78910f2069..0aa7287b6e15a79eb401068448e107f9314e4746 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2004 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
@@ -52,7 +52,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
        int nfd, fd2;
        int d, nd;
        struct supertype *st = NULL;
-       
+
 
        if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
                fprintf(stderr, Name ": cannot get array info for %s\n", devname);
@@ -103,7 +103,8 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
                        fprintf(stderr, Name ": cannot open device file %s\n", dv);
                        return 1;
                }
-               if (super) free(super);
+               if (super)
+                       st->ss->free_super(st, super);
                super= NULL;
                if (st->ss->load_super(st, fd2, &super, NULL)) {
                        fprintf(stderr, Name ": cannot find super block on %s\n", dv);
@@ -115,16 +116,18 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
        /* Ok, looks good. Lets update the superblock and write it out to
         * newdev.
         */
-       
+
        info.disk.number = d;
        info.disk.major = major(stb.st_rdev);
        info.disk.minor = minor(stb.st_rdev);
        info.disk.raid_disk = d;
        info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
-       st->ss->update_super(&info, super, "grow", newdev, 0);
+       st->ss->update_super(st, &info, super, "linear-grow-new", newdev,
+                            0, 0, NULL);
 
        if (st->ss->store_super(st, nfd, super)) {
-               fprintf(stderr, Name ": Cannot store new superblock on %s\n", newdev);
+               fprintf(stderr, Name ": Cannot store new superblock on %s\n",
+                       newdev);
                close(nfd);
                return 1;
        }
@@ -174,13 +177,10 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
                info.array.nr_disks = nd+1;
                info.array.active_disks = nd+1;
                info.array.working_disks = nd+1;
-               info.disk.number = nd;
-               info.disk.major = major(stb.st_rdev);
-               info.disk.minor = minor(stb.st_rdev);
-               info.disk.raid_disk = nd;
-               info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
-               st->ss->update_super(&info, super, "grow", dv, 0);
-               
+
+               st->ss->update_super(st, &info, super, "linear-grow-update", dv,
+                                    0, 0, NULL);
+
                if (st->ss->store_super(st, fd2, super)) {
                        fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
                        close(fd2);
@@ -255,17 +255,20 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
                        devname);
                return 1;
        }
+       if (array.level <= 0) {
+               fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
+                       map_num(pers, array.level)?:"of this array");
+               return 1;
+       }
        bitmapsize = array.size;
        bitmapsize <<= 1;
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &array_size) == 0 &&
+       if (get_dev_size(fd, NULL, &array_size) &&
            array_size > (0x7fffffffULL<<9)) {
                /* Array is big enough that we cannot trust array.size
                 * try other approaches
                 */
                bitmapsize = get_component_size(fd);
        }
-#endif
        if (bitmapsize == 0) {
                fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
                return 1;
@@ -305,9 +308,11 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
                                if (fd2 < 0)
                                        continue;
                                if (st->ss->load_super(st, fd2, &super, NULL)==0) {
-                                       if (st->ss->add_internal_bitmap(st, super,
-                                                                       chunk, delay, write_behind,
-                                                                       bitmapsize, 0, major))
+                                       if (st->ss->add_internal_bitmap(
+                                                   st, super,
+                                                   &chunk, delay, write_behind,
+                                                   bitmapsize, 0, major)
+                                               )
                                                st->ss->write_bitmap(st, fd2, super);
                                        else {
                                                fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
@@ -347,7 +352,7 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
                        if (fd2 >= 0 &&
                            st->ss->load_super(st, fd2, &super, NULL) == 0) {
                                close(fd2);
-                               st->ss->uuid_from_super(uuid, super);
+                               st->ss->uuid_from_super(st, uuid, super);
                                break;
                        }
                        close(fd2);
@@ -521,7 +526,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
        case 6:
                st = super_by_version(array.major_version,
                                      array.minor_version);
-               /* size can be changed independantly.
+               /* size can be changed independently.
                 * layout/chunksize/raid_disks/level can be changed
                 * though the kernel may not support it all.
                 * If 'suspend_lo' is not present in devfs, then
@@ -618,7 +623,8 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                printf("mdadm: Need to backup %lluK of critical section..\n", last_block/2);
 
                sra = sysfs_read(fd, 0,
-                                GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE);
+                                GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
+                                GET_CACHE);
                if (!sra) {
                        fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
                                devname);
@@ -657,7 +663,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                                offsets[sd->role] = sd->offset;
                                if (fdlist[sd->role] < 0) {
                                        fprintf(stderr, Name ": %s: cannot open component %s\n",
-                                               devname, dn);
+                                               devname, dn?dn:"-unknown-");
                                        goto abort;
                                }
                        } else {
@@ -667,7 +673,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                                offsets[d] = sd->offset;
                                if (fdlist[d]<0) {
                                        fprintf(stderr, Name ": %s: cannot open component %s\n",
-                                               devname, dn);
+                                               devname, dn?dn:"-unknown");
                                        goto abort;
                                }
                                d++;
@@ -706,7 +712,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
 
 
                memcpy(bsb.magic, "md_backup_data-1", 16);
-               st->ss->uuid_from_super((int*)&bsb.set_uuid, super);
+               st->ss->uuid_from_super(st, (int*)&bsb.set_uuid, super);
                bsb.mtime = __cpu_to_le64(time(0));
                bsb.arraystart = 0;
                bsb.length = __cpu_to_le64(last_block);
@@ -740,10 +746,25 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                array.chunk_size = nchunk;
                array.layout = nlayout;
                if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                       if (errno == ENOSPC) {
+                               /* stripe cache is not big enough.
+                                * It needs to be 4 times chunksize_size,
+                                * and we assume pagesize is 4K
+                                */
+                               if (sra->cache_size < 4 * (nchunk/4096)) {
+                                       sysfs_set_num(sra, NULL,
+                                                     "stripe_cache_size",
+                                                     4 * (nchunk/4096) +1);
+                                       if (ioctl(fd, SET_ARRAY_INFO,
+                                                 &array) == 0)
+                                               goto ok;
+                               }
+                       }
                        fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
                                devname, strerror(errno));
                        goto abort;
                }
+               ok: ;
 
                /* suspend the relevant region */
                sysfs_set_num(sra, NULL, "suspend_hi", 0); /* just in case */
@@ -796,12 +817,15 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                                break;
                        sleep(1);
                }
-               
+
                /* invalidate superblocks */
                memset(&bsb, 0, sizeof(bsb));
                for (i=odisks; i<d ; i++) {
                        lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0);
-                       write(fdlist[i], &bsb, sizeof(bsb));
+                       if (write(fdlist[i], &bsb, sizeof(bsb)) < 0) {
+                               fprintf(stderr, Name ": %s: failed to invalidate metadata for raid disk %d\n",
+                                       devname, i);
+                       }
                }
 
                /* unsuspend. */
@@ -844,8 +868,9 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
 {
        int i, j;
        int old_disks;
-       int err = 0;
        unsigned long long *offsets;
+       unsigned long long  nstripe, ostripe, last_block;
+       int ndata, odata;
 
        if (info->delta_disks < 0)
                return 1; /* cannot handle a shrink */
@@ -881,8 +906,9 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                        if (st->ss->load_super(st, fd, &super, NULL))
                                continue;
 
-                       st->ss->getinfo_super(&dinfo, super);
-                       free(super); super = NULL;
+                       st->ss->getinfo_super(st, &dinfo, super);
+                       st->ss->free_super(st, super);
+                       super = NULL;
                        if (lseek64(fd,
                                    (dinfo.data_offset + dinfo.component_size - 8) <<9,
                                    0) < 0)
@@ -923,8 +949,9 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                        if (st->ss->load_super(st, fdlist[j], &super, NULL))
                                /* FIXME should be this be an error */
                                continue;
-                       st->ss->getinfo_super(&dinfo, super);
-                       free(super); super = NULL;
+                       st->ss->getinfo_super(st, &dinfo, super);
+                       st->ss->free_super(st, super);
+                       super = NULL;
                        offsets[j] = dinfo.data_offset;
                }
                printf(Name ": restoring critical section\n");
@@ -937,7 +964,7 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                                    fd, __le64_to_cpu(bsb.devstart)*512,
                                    0, __le64_to_cpu(bsb.length)*512)) {
                        /* didn't succeed, so giveup */
-                       return -1;
+                       return 1;
                }
 
                /* Ok, so the data is restored. Let's update those superblocks. */
@@ -946,15 +973,36 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                        if (fdlist[j] < 0) continue;
                        if (st->ss->load_super(st, fdlist[j], &super, NULL))
                                continue;
-                       st->ss->getinfo_super(&dinfo, super);
+                       st->ss->getinfo_super(st, &dinfo, super);
                        dinfo.reshape_progress = __le64_to_cpu(bsb.length);
-                       st->ss->update_super(&dinfo, super, "_reshape_progress",NULL,0);
+                       st->ss->update_super(st, &dinfo, super,
+                                            "_reshape_progress",
+                                            NULL,0, 0, NULL);
                        st->ss->store_super(st, fdlist[j], super);
-                       free(super);
+                       st->ss->free_super(st, super);
                }
 
                /* And we are done! */
                return 0;
        }
-       return err;
+       /* Didn't find any backup data, try to see if any
+        * was needed.
+        */
+       nstripe = ostripe = 0;
+       odata = info->array.raid_disks - info->delta_disks - 1;
+       if (info->array.level == 6) odata--; /* number of data disks */
+       ndata = info->array.raid_disks - 1;
+       if (info->new_level == 6) ndata--;
+       last_block = 0;
+       while (nstripe >= ostripe) {
+               nstripe += info->new_chunk / 512;
+               last_block = nstripe * ndata;
+               ostripe = last_block / odata / (info->array.chunk_size/512) *
+                       (info->array.chunk_size/512);
+       }
+
+       if (info->reshape_progress >= last_block)
+               return 0;
+       /* needed to recover critical section! */
+       return 1;
 }