]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - restripe.c
Create: over-ride "start_ro" setting when creating an array.
[thirdparty/mdadm.git] / restripe.c
index 1c42b603da70607f147d45c519627a4f4a88dbb2..90896c89de0260a4c610d8ebdab7c5ffe3d031f8 100644 (file)
@@ -211,7 +211,7 @@ static int is_ddf(int layout)
 }
 
 
-static void xor_blocks(char *target, char **sources, int disks, int size)
+void xor_blocks(char *target, char **sources, int disks, int size)
 {
        int i, j;
        /* Amazingly inefficient... */
@@ -335,6 +335,17 @@ void make_tables(void)
 
 uint8_t *zero;
 int zero_size;
+
+void ensure_zero_has_size(int chunk_size)
+{
+       if (zero == NULL || chunk_size > zero_size) {
+               if (zero)
+                       free(zero);
+               zero = xcalloc(1, chunk_size);
+               zero_size = chunk_size;
+       }
+}
+
 /* Following was taken from linux/drivers/md/raid6recov.c */
 
 /* Recover two failed data blocks. */
@@ -510,15 +521,7 @@ int save_stripes(int *source, unsigned long long *offsets,
 
        if (!tables_ready)
                make_tables();
-
-       if (zero == NULL || chunk_size > zero_size) {
-               if (zero)
-                       free(zero);
-               zero = malloc(chunk_size);
-               if (zero)
-                       memset(zero, 0, chunk_size);
-               zero_size = chunk_size;
-       }
+       ensure_zero_has_size(chunk_size);
 
        len = data_disks * chunk_size;
        length_test = length / len;
@@ -652,10 +655,14 @@ int save_stripes(int *source, unsigned long long *offsets,
                                                  fdisk[0], fdisk[1], bufs);
                        }
                }
-               if (dest)
+               if (dest) {
                        for (i = 0; i < nwrites; i++)
                                if (write(dest[i], buf, len) != len)
                                        return -1;
+               } else {
+                       /* build next stripe in buffer */
+                       buf += len;
+               }
                length -= len;
                start += len;
        }
@@ -680,9 +687,10 @@ int restore_stripes(int *dest, unsigned long long *offsets,
                    char *src_buf)
 {
        char *stripe_buf;
-       char **stripes = malloc(raid_disks * sizeof(char*));
-       char **blocks = malloc(raid_disks * sizeof(char*));
+       char **stripes = xmalloc(raid_disks * sizeof(char*));
+       char **blocks = xmalloc(raid_disks * sizeof(char*));
        int i;
+       int rv;
 
        int data_disks = raid_disks - (level == 0 ? 0 : level <= 5 ? 1 : 2);
 
@@ -692,19 +700,14 @@ int restore_stripes(int *dest, unsigned long long *offsets,
        if (zero == NULL || chunk_size > zero_size) {
                if (zero)
                        free(zero);
-               zero = malloc(chunk_size);
-               if (zero)
-                       memset(zero, 0, chunk_size);
+               zero = xcalloc(1, chunk_size);
                zero_size = chunk_size;
        }
 
        if (stripe_buf == NULL || stripes == NULL || blocks == NULL
            || zero == NULL) {
-               free(stripe_buf);
-               free(stripes);
-               free(blocks);
-               free(zero);
-               return -2;
+               rv = -2;
+               goto abort;
        }
        for (i = 0; i < raid_disks; i++)
                stripes[i] = stripe_buf + i * chunk_size;
@@ -713,20 +716,26 @@ int restore_stripes(int *dest, unsigned long long *offsets,
                unsigned long long offset;
                int disk, qdisk;
                int syndrome_disks;
-               if (length < len)
-                       return -3;
+               if (length < len) {
+                       rv = -3;
+                       goto abort;
+               }
                for (i = 0; i < data_disks; i++) {
                        int disk = geo_map(i, start/chunk_size/data_disks,
                                           raid_disks, level, layout);
                        if (src_buf == NULL) {
                                /* read from file */
-                               if (lseek64(source,
-                                       read_offset, 0) != (off64_t)read_offset)
-                                       return -1;
+                               if (lseek64(source, read_offset, 0) !=
+                                        (off64_t)read_offset) {
+                                       rv = -1;
+                                       goto abort;
+                               }
                                if (read(source,
                                         stripes[disk],
-                                        chunk_size) != chunk_size)
-                                       return -1;
+                                        chunk_size) != chunk_size) {
+                                       rv = -1;
+                                       goto abort;
+                               }
                        } else {
                                /* read from input buffer */
                                memcpy(stripes[disk],
@@ -778,15 +787,27 @@ int restore_stripes(int *dest, unsigned long long *offsets,
                }
                for (i=0; i < raid_disks ; i++)
                        if (dest[i] >= 0) {
-                               if (lseek64(dest[i], offsets[i]+offset, 0) < 0)
-                                       return -1;
-                               if (write(dest[i], stripes[i], chunk_size) != chunk_size)
-                                       return -1;
+                               if (lseek64(dest[i],
+                                        offsets[i]+offset, 0) < 0) {
+                                       rv = -1;
+                                       goto abort;
+                               }
+                               if (write(dest[i], stripes[i],
+                                        chunk_size) != chunk_size) {
+                                       rv = -1;
+                                       goto abort;
+                               }
                        }
                length -= len;
                start += len;
        }
-       return 0;
+       rv = 0;
+
+abort:
+       free(stripe_buf);
+       free(stripes);
+       free(blocks);
+       return rv;
 }
 
 #ifdef MAIN
@@ -796,11 +817,11 @@ int test_stripes(int *source, unsigned long long *offsets,
                 unsigned long long start, unsigned long long length)
 {
        /* ready the data and p (and q) blocks, and check we got them right */
-       char *stripe_buf = malloc(raid_disks * chunk_size);
-       char **stripes = malloc(raid_disks * sizeof(char*));
-       char **blocks = malloc(raid_disks * sizeof(char*));
-       char *p = malloc(chunk_size);
-       char *q = malloc(chunk_size);
+       char *stripe_buf = xmalloc(raid_disks * chunk_size);
+       char **stripes = xmalloc(raid_disks * sizeof(char*));
+       char **blocks = xmalloc(raid_disks * sizeof(char*));
+       char *p = xmalloc(chunk_size);
+       char *q = xmalloc(chunk_size);
 
        int i;
        int diskP, diskQ;
@@ -915,9 +936,8 @@ main(int argc, char *argv[])
                        raid_disks, argc-9);
                exit(2);
        }
-       fds = malloc(raid_disks * sizeof(*fds));
-       offsets = malloc(raid_disks * sizeof(*offsets));
-       memset(offsets, 0, raid_disks * sizeof(*offsets));
+       fds = xmalloc(raid_disks * sizeof(*fds));
+       offsets = xcalloc(raid_disks, sizeof(*offsets));
 
        storefd = open(file, O_RDWR);
        if (storefd < 0) {
@@ -942,7 +962,7 @@ main(int argc, char *argv[])
                }
        }
 
-       buf = malloc(raid_disks * chunk_size);
+       buf = xmalloc(raid_disks * chunk_size);
 
        if (save == 1) {
                int rv = save_stripes(fds, offsets,
@@ -967,7 +987,7 @@ main(int argc, char *argv[])
                int rv = restore_stripes(fds, offsets,
                                         raid_disks, chunk_size, level, layout,
                                         storefd, 0ULL,
-                                        start, length);
+                                        start, length, NULL);
                if (rv != 0) {
                        fprintf(stderr,
                                "test_stripe: restore_stripes returned %d\n",