]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Create missing /dev files where needed.
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index ece2bda384f5bc3c36012a78b3a9fc0597e43b95..de53df18559c69fabcabe2f8daa5d627b594e6a1 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -92,13 +92,13 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
                                d);
                        return 1;
                }
-               dv = map_dev(disk.major, disk.minor);
+               dv = map_dev(disk.major, disk.minor, 1);
                if (!dv) {
                        fprintf(stderr, Name ": cannot find device file for device %d\n",
                                d);
                        return 1;
                }
-               fd2 = open(dv, O_RDWR);
+               fd2 = dev_open(dv, O_RDWR);
                if (!fd2) {
                        fprintf(stderr, Name ": cannot open device file %s\n", dv);
                        return 1;
@@ -154,13 +154,13 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
                                d);
                        return 1;
                }
-               dv = map_dev(disk.major, disk.minor);
+               dv = map_dev(disk.major, disk.minor, 1);
                if (!dv) {
                        fprintf(stderr, Name ": cannot find device file for device %d\n",
                                d);
                        return 1;
                }
-               fd2 = open(dv, O_RDWR);
+               fd2 = dev_open(dv, O_RDWR);
                if (fd2 < 0) {
                        fprintf(stderr, Name ": cannot open device file %s\n", dv);
                        return 1;
@@ -219,7 +219,7 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
        }
 
        if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
-               if (errno == ENOMEM) 
+               if (errno == ENOMEM)
                        fprintf(stderr, Name ": Memory allocation failure.\n");
                else
                        fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
@@ -298,10 +298,10 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
                                continue;
                        if ((disk.state & (1<<MD_DISK_SYNC))==0)
                                continue;
-                       dv = map_dev(disk.major, disk.minor);
+                       dv = map_dev(disk.major, disk.minor, 1);
                        if (dv) {
                                void *super;
-                               int fd2 = open(dv, O_RDWR);
+                               int fd2 = dev_open(dv, O_RDWR);
                                if (fd2 < 0)
                                        continue;
                                if (st->ss->load_super(st, fd2, &super, NULL)==0) {
@@ -343,9 +343,9 @@ int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int
                        if ((disk.major==0 && disk.minor==0) ||
                            (disk.state & (1<<MD_DISK_REMOVED)))
                                continue;
-                       dv = map_dev(disk.major, disk.minor);
+                       dv = map_dev(disk.major, disk.minor, 1);
                        if (!dv) continue;
-                       fd2 = open(dv, O_RDONLY);
+                       fd2 = dev_open(dv, O_RDONLY);
                        if (fd2 >= 0 &&
                            st->ss->load_super(st, fd2, &super, NULL) == 0) {
                                close(fd2);
@@ -408,7 +408,7 @@ int bsb_csum(char *buf, int len)
        return __cpu_to_le32(csum);
 }
 
-int Grow_reshape(char *devname, int fd, int quiet,
+int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                 long long size,
                 int level, int layout, int chunksize, int raid_disks)
 {
@@ -605,12 +605,12 @@ int Grow_reshape(char *devname, int fd, int quiet,
                 * from
                 */
                nstripe = ostripe = 0;
-               while (nstripe+ochunk/512 >= ostripe) {
+               while (nstripe >= ostripe) {
                        nstripe += nchunk/512;
                        last_block = nstripe * ndata;
-                       ostripe = last_block / odata;
+                       ostripe = last_block / odata / (ochunk/512) * (ochunk/512);
                }
-               printf("Need to backup to stripe %llu sectors, %lluK\n", nstripe, last_block/2);
+               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);
@@ -625,25 +625,30 @@ int Grow_reshape(char *devname, int fd, int quiet,
                                devname);
                        return 1;
                }
+               if (sra->spares == 0 && backup_file == NULL) {
+                       fprintf(stderr, Name ": %s: Cannot grow - need a spare or backup-file to backup critical section\n",
+                               devname);
+                       return 1;
+               }
 
                nrdisks = array.nr_disks + sra->spares;
                /* Now we need to open all these devices so we can read/write.
                 */
-               fdlist = malloc(nrdisks * sizeof(int));
-               offsets = malloc(nrdisks * sizeof(offsets[0]));
+               fdlist = malloc((1+nrdisks) * sizeof(int));
+               offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
                if (!fdlist || !offsets) {
                        fprintf(stderr, Name ": malloc failed: grow aborted\n");
                        return 1;
                }
-               for (d=0; d< nrdisks; d++)
+               for (d=0; d <= nrdisks; d++)
                        fdlist[d] = -1;
                d = array.raid_disks;
                for (sd = sra->devs; sd; sd=sd->next) {
                        if (sd->state & (1<<MD_DISK_FAULTY))
                                continue;
                        if (sd->state & (1<<MD_DISK_SYNC)) {
-                               char *dn = map_dev(sd->major, sd->minor);
-                               fdlist[sd->role] = open(dn, O_RDONLY);
+                               char *dn = map_dev(sd->major, sd->minor, 1);
+                               fdlist[sd->role] = dev_open(dn, O_RDONLY);
                                offsets[sd->role] = sd->offset;
                                if (fdlist[sd->role] < 0) {
                                        fprintf(stderr, Name ": %s: cannot open component %s\n",
@@ -652,8 +657,8 @@ int Grow_reshape(char *devname, int fd, int quiet,
                                }
                        } else {
                                /* spare */
-                               char *dn = map_dev(sd->major, sd->minor);
-                               fdlist[d] = open(dn, O_RDWR);
+                               char *dn = map_dev(sd->major, sd->minor, 1);
+                               fdlist[d] = dev_open(dn, O_RDWR);
                                offsets[d] = sd->offset;
                                if (fdlist[d]<0) {
                                        fprintf(stderr, Name ": %s: cannot open component %s\n",
@@ -669,8 +674,20 @@ int Grow_reshape(char *devname, int fd, int quiet,
                                        " --grow aborted\n", devname, i);
                                goto abort;
                        }
+               spares = sra->spares;
+               if (backup_file) {
+                       fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL, 0600);
+                       if (fdlist[d] < 0) {
+                               fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
+                                       devname, backup_file, strerror(errno));
+                               goto abort;
+                       }
+                       offsets[d] = 8;
+                       d++;
+                       spares++;
+               }
                if (fdlist[array.raid_disks] < 0) {
-                       fprintf(stderr, Name ": %s: failed to find a spare - --grow aborted\n",
+                       fprintf(stderr, Name ": %s: failed to find a spare and no backup-file given - --grow aborted\n",
                                devname);
                        goto abort;
                }
@@ -682,15 +699,36 @@ int Grow_reshape(char *devname, int fd, int quiet,
                        goto abort;
                }
 
-               spares = sra->spares;
 
-               /* Decide offset for the backup and llseek the spares */
+               memcpy(bsb.magic, "md_backup_data-1", 16);
+               st->ss->uuid_from_super((int*)&bsb.set_uuid, super);
+               bsb.mtime = __cpu_to_le64(time(0));
+               bsb.arraystart = 0;
+               bsb.length = __cpu_to_le64(last_block);
+
+               /* Decide offset for the backup, llseek the spares, and write
+                * a leading superblock 4K earlier.
+                */
                for (i=array.raid_disks; i<d; i++) {
-                       offsets[i] += sra->component_size - last_block - 8;
-                       if (lseek64(fdlist[i], offsets[i]<<9, 0) != offsets[i]<<9) {
+                       char buf[4096];
+                       if (i==d-1 && backup_file) {
+                               /* This is the backup file */
+                               offsets[i] = 8;
+                       } else
+                               offsets[i] += sra->component_size - last_block - 8;
+                       if (lseek64(fdlist[i], (offsets[i]<<9) - 4096, 0)
+                           != (offsets[i]<<9) - 4096) {
                                fprintf(stderr, Name ": could not seek...\n");
                                goto abort;
                        }
+                       memset(buf, 0, sizeof(buf));
+                       bsb.devstart = __cpu_to_le64(offsets[i]);
+                       bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
+                       memcpy(buf, &bsb, sizeof(bsb));
+                       if (write(fdlist[i], buf, 4096) != 4096) {
+                               fprintf(stderr, Name ": could not write leading superblock\n");
+                               goto abort;
+                       }
                }
                array.level = nlevel;
                array.raid_disks = ndisks;
@@ -715,7 +753,7 @@ int Grow_reshape(char *devname, int fd, int quiet,
                err = save_stripes(fdlist, offsets,
                                   odisks, ochunk, olevel, olayout,
                                   spares, fdlist+odisks,
-                                  0ULL, nstripe*512);
+                                  0ULL, last_block*512);
 
                /* abort if there was an error */
                if (err < 0) {
@@ -723,18 +761,17 @@ int Grow_reshape(char *devname, int fd, int quiet,
                                devname);
                        goto abort_resume;
                }
-               /* FIXME write superblocks */
-               memcpy(bsb.magic, "md_backups_data-1", 16);
-               st->ss->uuid_from_super((int*)&bsb.set_uuid, super);
-               bsb.mtime = time(0);
-               bsb.arraystart = 0;
-               bsb.length = last_block;
+
                for (i=odisks; i<d ; i++) {
-                       bsb.devstart = offsets[i];
+                       bsb.devstart = __cpu_to_le64(offsets[i]);
                        bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
-                       lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0);
-                       write(fdlist[i], &bsb, sizeof(bsb));
-                       /* FIXME error check */
+                       if (lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0) < 0 ||
+                           write(fdlist[i], &bsb, sizeof(bsb)) != sizeof(bsb) ||
+                           fsync(fdlist[i]) != 0) {
+                               fprintf(stderr, Name ": %s: fail to save metadata for critical region backups.\n",
+                                       devname);
+                               goto abort_resume;
+                       }
                }
 
                /* start the reshape happening */
@@ -746,8 +783,10 @@ int Grow_reshape(char *devname, int fd, int quiet,
                /* wait for reshape to pass the critical region */
                while(1) {
                        unsigned long long comp;
-                       if (sysfs_get_ll(sra, NULL, "sync_completed", &comp)<0)
+                       if (sysfs_get_ll(sra, NULL, "sync_completed", &comp)<0) {
+                               sleep(5);
                                break;
+                       }
                        if (comp >= nstripe)
                                break;
                        sleep(1);
@@ -768,7 +807,10 @@ int Grow_reshape(char *devname, int fd, int quiet,
                                close(fdlist[i]);
                free(fdlist);
                free(offsets);
+               if (backup_file)
+                       unlink(backup_file);
 
+               printf(Name ": ... critical section passed.\n");
                break;
        }
        return 0;
@@ -782,6 +824,134 @@ int Grow_reshape(char *devname, int fd, int quiet,
                        close(fdlist[i]);
        free(fdlist);
        free(offsets);
+       if (backup_file)
+               unlink(backup_file);
        return 1;
 
 }
+
+/*
+ * If any spare contains md_back_data-1 which is recent wrt mtime,
+ * write that data into the array and update the super blocks with
+ * the new reshape_progress
+ */
+int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file)
+{
+       int i, j;
+       int old_disks;
+       int err = 0;
+       unsigned long long *offsets;
+
+       if (info->delta_disks < 0)
+               return 1; /* cannot handle a shrink */
+       if (info->new_level != info->array.level ||
+           info->new_layout != info->array.layout ||
+           info->new_chunk != info->array.chunk_size)
+               return 1; /* Can only handle change in disks */
+
+       old_disks = info->array.raid_disks - info->delta_disks;
+
+       for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
+               void *super = NULL;
+               struct mdinfo dinfo;
+               struct mdp_backup_super bsb;
+               char buf[4096];
+               int fd;
+
+               /* This was a spare and may have some saved data on it.
+                * Load the superblock, find and load the
+                * backup_super_block.
+                * If either fail, go on to next device.
+                * If the backup contains no new info, just return
+                * else restore data and update all superblocks
+                */
+               if (i == old_disks-1) {
+                       fd = open(backup_file, O_RDONLY);
+                       if (fd<0)
+                               continue;
+                       if (lseek(fd, 4096, 0) != 4096)
+                               continue;
+               } else {
+                       fd = fdlist[i];
+                       if (fd < 0)
+                               continue;
+                       if (st->ss->load_super(st, fd, &super, NULL))
+                               continue;
+
+                       st->ss->getinfo_super(&dinfo, super);
+                       free(super); super = NULL;
+                       if (lseek64(fd,
+                                   (dinfo.data_offset + dinfo.component_size - 8) <<9,
+                                   0) < 0)
+                               continue; /* Cannot seek */
+               }
+               if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb))
+                       continue; /* Cannot read */
+               if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0)
+                       continue;
+               if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)))
+                       continue; /* bad checksum */
+               if (memcmp(bsb.set_uuid,info->uuid, 16) != 0)
+                       continue; /* Wrong uuid */
+
+               if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 ||
+                   info->array.utime < __le64_to_cpu(bsb.mtime))
+                       continue; /* time stamp is too bad */
+
+               if (__le64_to_cpu(bsb.arraystart) != 0)
+                       continue; /* Can only handle backup from start of array */
+               if (__le64_to_cpu(bsb.length) <
+                   info->reshape_progress)
+                       continue; /* No new data here */
+
+               if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0)
+                       continue; /* Cannot seek */
+               /* There should be a duplicate backup superblock 4k before here */
+               if (lseek64(fd, -4096, 1) < 0 ||
+                   read(fd, buf, 4096) != 4096 ||
+                   memcmp(buf, &bsb, sizeof(buf)) != 0)
+                       continue; /* Cannot find leading superblock */
+
+               /* Now need the data offsets for all devices. */
+               offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
+               for(j=0; j<info->array.raid_disks; j++) {
+                       if (fdlist[j] < 0)
+                               continue;
+                       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;
+                       offsets[j] = dinfo.data_offset;
+               }
+               printf(Name ": restoring critical section\n");
+
+               if (restore_stripes(fdlist, offsets,
+                                   info->array.raid_disks,
+                                   info->new_chunk,
+                                   info->new_level,
+                                   info->new_layout,
+                                   fd, __le64_to_cpu(bsb.devstart)*512,
+                                   0, __le64_to_cpu(bsb.length)*512)) {
+                       /* didn't succeed, so giveup */
+                       return -1;
+               }
+
+               /* Ok, so the data is restored. Let's update those superblocks. */
+
+               for (j=0; j<info->array.raid_disks; j++) {
+                       if (fdlist[j] < 0) continue;
+                       if (st->ss->load_super(st, fdlist[j], &super, NULL))
+                               continue;
+                       st->ss->getinfo_super(&dinfo, super);
+                       dinfo.reshape_progress = __le64_to_cpu(bsb.length);
+                       st->ss->update_super(&dinfo, super, "_reshape_progress",NULL,0);
+                       st->ss->store_super(st, fdlist[j], super);
+                       free(super);
+               }
+
+               /* And we are done! */
+               return 0;
+       }
+       return err;
+}