]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super1.c
Factor common code into new "start_mdmon".
[thirdparty/mdadm.git] / super1.c
index ae6c8a371f315e72f0e3d9d8177f54869e0d67d9..06d0a1876cf1f678f27661c14dfc8649576d8c89 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -671,7 +671,7 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
            __le64_to_cpu(sb->data_offset)) {
                /* set data_size to device size less data_offset */
                struct misc_dev_info *misc = (struct misc_dev_info*)
-                       (st->sb + 1024 + sizeof(struct bitmap_super_s));
+                       (st->sb + 1024 + 512);
                printf("Size was %llu\n", (unsigned long long)
                       __le64_to_cpu(sb->data_size));
                sb->data_size = __cpu_to_le64(
@@ -689,15 +689,17 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 static int init_super1(struct supertype *st, mdu_array_info_t *info,
                       unsigned long long size, char *name, char *homehost, int *uuid)
 {
-       struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t) +
-                                            sizeof(struct misc_dev_info));
+       struct mdp_superblock_1 *sb;
        int spares;
        int rfd;
        char defname[10];
+
+       posix_memalign((void**)&sb, 512, (1024 + 512 + 
+                                         sizeof(struct misc_dev_info)));
        memset(sb, 0, 1024);
 
        st->sb = sb;
-       if (info->major_version == -1) {
+       if (info == NULL) {
                /* zeroing superblock */
                return 0;
        }
@@ -789,6 +791,9 @@ static void add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
        else
                *rp = 0xfffe;
 
+       sb->dev_number = __cpu_to_le32(dk->number);
+       sb->sb_csum = calc_sb_1_csum(sb);
+
        dip = (struct devinfo **)&st->info;
        while (*dip)
                dip = &(*dip)->next;
@@ -854,6 +859,7 @@ static int store_super1(struct supertype *st, int fd)
                return 3;
 
        sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
+       sbsize = (sbsize+511)&(~511UL);
 
        if (write(fd, sb, sbsize) != sbsize)
                return 4;
@@ -863,7 +869,8 @@ static int store_super1(struct supertype *st, int fd)
                        (((char*)sb)+1024);
                if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
                        locate_bitmap1(st, fd);
-                       if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
+                       if (write(fd, bm, ROUND_UP(sizeof(*bm),512)) !=
+                           ROUND_UP(sizeof(*bm),512))
                            return 5;
                }
        }
@@ -1032,9 +1039,10 @@ static int compare_super1(struct supertype *st, struct supertype *tst)
                return 1;
 
        if (!first) {
-               first = malloc(1024+sizeof(bitmap_super_t) +
+               posix_memalign((void**)&first, 512,
+                              1024 + 512 +
                               sizeof(struct misc_dev_info));
-               memcpy(first, second, 1024+sizeof(bitmap_super_t) +
+               memcpy(first, second, 1024 + 512 + 
                       sizeof(struct misc_dev_info));
                st->sb = first;
                return 0;
@@ -1065,6 +1073,9 @@ static int load_super1(struct supertype *st, int fd, char *devname)
 
        free_super1(st);
 
+       if (st->subarray[0])
+               return 1;
+
        if (st->ss == NULL || st->minor_version == -1) {
                int bestvers = -1;
                struct supertype tst;
@@ -1144,7 +1155,8 @@ static int load_super1(struct supertype *st, int fd, char *devname)
                return 1;
        }
 
-       super = malloc(1024 + sizeof(bitmap_super_t) +
+       posix_memalign((void**)&super, 512,
+                      1024 + 512 +
                       sizeof(struct misc_dev_info));
 
        if (read(fd, super, 1024) != 1024) {
@@ -1181,7 +1193,7 @@ static int load_super1(struct supertype *st, int fd, char *devname)
 
        bsb = (struct bitmap_super_s *)(((char*)super)+1024);
 
-       misc = (struct misc_dev_info*) (bsb+1);
+       misc = (struct misc_dev_info*) (((char*)super)+1024+512);
        misc->device_size = dsize;
 
        /* Now check on the bitmap superblock */
@@ -1192,8 +1204,8 @@ static int load_super1(struct supertype *st, int fd, char *devname)
         * should get that written out.
         */
        locate_bitmap1(st, fd);
-       if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
-           != sizeof(struct bitmap_super_s))
+       if (read(fd, ((char*)super)+1024, 512)
+           != 512)
                goto no_bitmap;
 
        uuid_from_super1(st, uuid);
@@ -1413,25 +1425,28 @@ static int write_bitmap1(struct supertype *st, int fd)
        int rv = 0;
 
        int towrite, n;
-       char buf[4096];
+       char abuf[4096+512];
+       char *buf = (char*)(((long)(abuf+512))&~511UL);
 
        locate_bitmap1(st, fd);
 
-       if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
-           sizeof(bitmap_super_t))
-               return -2;
+       memset(buf, 0xff, 4096);
+       memcpy(buf, ((char*)sb)+1024, sizeof(bitmap_super_t));
+
        towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
        towrite = (towrite+7) >> 3; /* bits to bytes */
-       memset(buf, 0xff, sizeof(buf));
+       towrite += sizeof(bitmap_super_t);
+       towrite = ROUND_UP(towrite, 512);
        while (towrite > 0) {
                n = towrite;
-               if (n > sizeof(buf))
-                       n = sizeof(buf);
+               if (n > 4096)
+                       n = 4096;
                n = write(fd, buf, n);
                if (n > 0)
                        towrite -= n;
                else
                        break;
+               memset(buf, 0xff, 4096);
        }
        fsync(fd);
        if (towrite)
@@ -1450,7 +1465,8 @@ static void free_super1(struct supertype *st)
 static int validate_geometry1(struct supertype *st, int level,
                              int layout, int raiddisks,
                              int chunk, unsigned long long size,
-                             char *subdev, unsigned long long *freesize)
+                             char *subdev, unsigned long long *freesize,
+                             int verbose)
 {
        unsigned long long ldsize;
        int fd;
@@ -1462,10 +1478,12 @@ static int validate_geometry1(struct supertype *st, int level,
 
        fd = open(subdev, O_RDONLY|O_EXCL, 0);
        if (fd < 0) {
-               fprintf(stderr, Name ": Cannot open %s: %s\n",
-                       subdev, strerror(errno));
+               if (verbose)
+                       fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
+                               subdev, strerror(errno));
                return 0;
        }
+
        if (!get_dev_size(fd, subdev, &ldsize)) {
                close(fd);
                return 0;