Remove getinfo_super_n and do some other cleaning up.
[thirdparty/mdadm.git] / super1.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
5  *
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *    Author: Neil Brown
22  *    Email: <neilb@cse.unsw.edu.au>
23  *    Paper: Neil Brown
24  *           School of Computer Science and Engineering
25  *           The University of New South Wales
26  *           Sydney, 2052
27  *           Australia
28  */
29
30 #include "mdadm.h"
31 /*
32  * The version-1 superblock :
33  * All numeric fields are little-endian.
34  *
35  * total size: 256 bytes plus 2 per device.
36  *  1K allows 384 devices.
37  */
38 struct mdp_superblock_1 {
39         /* constant array information - 128 bytes */
40         __u32   magic;          /* MD_SB_MAGIC: 0xa92b4efc - little endian */
41         __u32   major_version;  /* 1 */
42         __u32   feature_map;    /* 0 for now */
43         __u32   pad0;           /* always set to 0 when writing */
44
45         __u8    set_uuid[16];   /* user-space generated. */
46         char    set_name[32];   /* set and interpreted by user-space */
47
48         __u64   ctime;          /* lo 40 bits are seconds, top 24 are microseconds or 0*/
49         __u32   level;          /* -4 (multipath), -1 (linear), 0,1,4,5 */
50         __u32   layout;         /* only for raid5 currently */
51         __u64   size;           /* used size of component devices, in 512byte sectors */
52
53         __u32   chunksize;      /* in 512byte sectors */
54         __u32   raid_disks;
55         __u32   bitmap_offset;  /* sectors after start of superblock that bitmap starts
56                                  * NOTE: signed, so bitmap can be before superblock
57                                  * only meaningful of feature_map[0] is set.
58                                  */
59
60         /* These are only valid with feature bit '4' */
61         __u32   new_level;      /* new level we are reshaping to                */
62         __u64   reshape_position;       /* next address in array-space for reshape */
63         __u32   delta_disks;    /* change in number of raid_disks               */
64         __u32   new_layout;     /* new layout                                   */
65         __u32   new_chunk;      /* new chunk size (bytes)                       */
66         __u8    pad1[128-124];  /* set to 0 when written */
67
68         /* constant this-device information - 64 bytes */
69         __u64   data_offset;    /* sector start of data, often 0 */
70         __u64   data_size;      /* sectors in this device that can be used for data */
71         __u64   super_offset;   /* sector start of this superblock */
72         __u64   recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
73         __u32   dev_number;     /* permanent identifier of this  device - not role in raid */
74         __u32   cnt_corrected_read; /* number of read errors that were corrected by re-writing */
75         __u8    device_uuid[16]; /* user-space setable, ignored by kernel */
76         __u8    devflags;        /* per-device flags.  Only one defined...*/
77 #define WriteMostly1    1        /* mask for writemostly flag in above */
78         __u8    pad2[64-57];    /* set to 0 when writing */
79
80         /* array state information - 64 bytes */
81         __u64   utime;          /* 40 bits second, 24 btes microseconds */
82         __u64   events;         /* incremented when superblock updated */
83         __u64   resync_offset;  /* data before this offset (from data_offset) known to be in sync */
84         __u32   sb_csum;        /* checksum upto devs[max_dev] */
85         __u32   max_dev;        /* size of devs[] array to consider */
86         __u8    pad3[64-32];    /* set to 0 when writing */
87
88         /* device state information. Indexed by dev_number.
89          * 2 bytes per device
90          * Note there are no per-device state flags. State information is rolled
91          * into the 'roles' value.  If a device is spare or faulty, then it doesn't
92          * have a meaningful role.
93          */
94         __u16   dev_roles[0];   /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
95 };
96
97 struct misc_dev_info {
98         __u64 device_size;
99 };
100
101 /* feature_map bits */
102 #define MD_FEATURE_BITMAP_OFFSET        1
103 #define MD_FEATURE_RECOVERY_OFFSET      2 /* recovery_offset is present and
104                                            * must be honoured
105                                            */
106 #define MD_FEATURE_RESHAPE_ACTIVE       4
107
108 #define MD_FEATURE_ALL                  (1|2|4)
109
110 #ifndef offsetof
111 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
112 #endif
113 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
114 {
115         unsigned int disk_csum, csum;
116         unsigned long long newcsum;
117         int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
118         unsigned int *isuper = (unsigned int*)sb;
119         int i;
120
121 /* make sure I can count... */
122         if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
123             offsetof(struct mdp_superblock_1, utime) != 192 ||
124             sizeof(struct mdp_superblock_1) != 256) {
125                 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
126         }
127
128         disk_csum = sb->sb_csum;
129         sb->sb_csum = 0;
130         newcsum = 0;
131         for (i=0; size>=4; size -= 4 ) {
132                 newcsum += __le32_to_cpu(*isuper);
133                 isuper++;
134         }
135
136         if (size == 2)
137                 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
138
139         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
140         sb->sb_csum = disk_csum;
141         return __cpu_to_le32(csum);
142 }
143
144 #ifndef MDASSEMBLE
145 static void examine_super1(struct supertype *st, char *homehost)
146 {
147         struct mdp_superblock_1 *sb = st->sb;
148         time_t atime;
149         int d;
150         int faulty;
151         int i;
152         char *c;
153         int l = homehost ? strlen(homehost) : 0;
154         int layout;
155         unsigned long long sb_offset;
156
157         printf("          Magic : %08x\n", __le32_to_cpu(sb->magic));
158         printf("        Version : 1");
159         sb_offset = __le64_to_cpu(sb->super_offset);
160         if (sb_offset <= 4)
161                 printf(".1\n");
162         else if (sb_offset <= 8)
163                 printf(".2\n");
164         else
165                 printf(".0\n");
166         printf("    Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
167         printf("     Array UUID : ");
168         for (i=0; i<16; i++) {
169                 if ((i&3)==0 && i != 0) printf(":");
170                 printf("%02x", sb->set_uuid[i]);
171         }
172         printf("\n");
173         printf("           Name : %.32s", sb->set_name);
174         if (l > 0 && l < 32 &&
175             sb->set_name[l] == ':' &&
176             strncmp(sb->set_name, homehost, l) == 0)
177                 printf("  (local to host %s)", homehost);
178         printf("\n");
179         atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
180         printf("  Creation Time : %.24s\n", ctime(&atime));
181         c=map_num(pers, __le32_to_cpu(sb->level));
182         printf("     Raid Level : %s\n", c?c:"-unknown-");
183         printf("   Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
184         printf("\n");
185         printf(" Avail Dev Size : %llu%s\n",
186                (unsigned long long)__le64_to_cpu(sb->data_size),
187                human_size(__le64_to_cpu(sb->data_size)<<9));
188         if (__le32_to_cpu(sb->level) >= 0) {
189                 int ddsks=0;
190                 switch(__le32_to_cpu(sb->level)) {
191                 case 1: ddsks=1;break;
192                 case 4:
193                 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
194                 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
195                 case 10:
196                         layout = __le32_to_cpu(sb->layout);
197                         ddsks = __le32_to_cpu(sb->raid_disks)
198                                  / (layout&255) / ((layout>>8)&255);
199                 }
200                 if (ddsks)
201                         printf("     Array Size : %llu%s\n",
202                                ddsks*(unsigned long long)__le64_to_cpu(sb->size),
203                                human_size(ddsks*__le64_to_cpu(sb->size)<<9));
204                 if (sb->size != sb->data_size)
205                         printf("  Used Dev Size : %llu%s\n",
206                                (unsigned long long)__le64_to_cpu(sb->size),
207                                human_size(__le64_to_cpu(sb->size)<<9));
208         }
209         if (sb->data_offset)
210                 printf("    Data Offset : %llu sectors\n",
211                        (unsigned long long)__le64_to_cpu(sb->data_offset));
212         printf("   Super Offset : %llu sectors\n",
213                (unsigned long long)__le64_to_cpu(sb->super_offset));
214         if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
215                 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
216         printf("          State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
217         printf("    Device UUID : ");
218         for (i=0; i<16; i++) {
219                 if ((i&3)==0 && i != 0) printf(":");
220                 printf("%02x", sb->device_uuid[i]);
221         }
222         printf("\n");
223         printf("\n");
224         if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
225                 printf("Internal Bitmap : %ld sectors from superblock\n",
226                        (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
227         }
228         if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
229                 printf("  Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
230                        human_size(__le64_to_cpu(sb->reshape_position)<<9));
231                 if (__le32_to_cpu(sb->delta_disks)) {
232                         printf("  Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
233                         if (__le32_to_cpu(sb->delta_disks))
234                                 printf(" (%d->%d)\n",
235                                        __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
236                                        __le32_to_cpu(sb->raid_disks));
237                         else
238                                 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
239                                        __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
240                 }
241                 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
242                         c = map_num(pers, __le32_to_cpu(sb->new_level));
243                         printf("      New Level : %s\n", c?c:"-unknown-");
244                 }
245                 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
246                         if (__le32_to_cpu(sb->level) == 5) {
247                                 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
248                                 printf("     New Layout : %s\n", c?c:"-unknown-");
249                         }
250                         if (__le32_to_cpu(sb->level) == 10) {
251                                 printf("     New Layout : near=%d, %s=%d\n",
252                                        __le32_to_cpu(sb->new_layout)&255,
253                                        (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
254                                        (__le32_to_cpu(sb->new_layout)>>8)&255);
255                         }
256                 }
257                 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
258                         printf("  New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
259                 printf("\n");
260         }
261         if (sb->devflags) {
262                 printf("      Flags :");
263                 if (sb->devflags & WriteMostly1)
264                         printf(" write-mostly");
265                 printf("\n");
266         }
267
268         atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
269         printf("    Update Time : %.24s\n", ctime(&atime));
270
271         if (calc_sb_1_csum(sb) == sb->sb_csum)
272                 printf("       Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
273         else
274                 printf("       Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
275                        __le32_to_cpu(calc_sb_1_csum(sb)));
276         printf("         Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
277         printf("\n");
278         if (__le32_to_cpu(sb->level) == 5) {
279                 c = map_num(r5layout, __le32_to_cpu(sb->layout));
280                 printf("         Layout : %s\n", c?c:"-unknown-");
281         }
282         if (__le32_to_cpu(sb->level) == 10) {
283                 int lo = __le32_to_cpu(sb->layout);
284                 printf("         Layout : near=%d, %s=%d\n",
285                        lo&255,
286                        (lo&0x10000)?"offset":"far",
287                        (lo>>8)&255);
288         }
289         switch(__le32_to_cpu(sb->level)) {
290         case 0:
291         case 4:
292         case 5:
293         case 6:
294         case 10:
295                 printf("     Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
296                 break;
297         case -1:
298                 printf("       Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
299                 break;
300         default: break;
301         }
302         printf("\n");
303         printf("    Array Slot : %d (", __le32_to_cpu(sb->dev_number));
304         for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
305                 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
306                         break;
307         for (d=0; d < i; d++) {
308                 int role = __le16_to_cpu(sb->dev_roles[d]);
309                 if (d) printf(", ");
310                 if (role == 0xffff) printf("empty");
311                 else if(role == 0xfffe) printf("failed");
312                 else printf("%d", role);
313         }
314         printf(")\n");
315         printf("   Array State : ");
316         for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
317                 int cnt = 0;
318                 int me = 0;
319                 int i;
320                 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
321                         int role = __le16_to_cpu(sb->dev_roles[i]);
322                         if (role == d) {
323                                 if (i == __le32_to_cpu(sb->dev_number))
324                                         me = 1;
325                                 cnt++;
326                         }
327                 }
328                 if (cnt > 1) printf("?");
329                 else if (cnt == 1 && me) printf("U");
330                 else if (cnt == 1) printf("u");
331                 else printf ("_");
332         }
333         faulty = 0;
334         for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
335                 int role = __le16_to_cpu(sb->dev_roles[i]);
336                 if (role == 0xFFFE)
337                         faulty++;
338         }
339         if (faulty) printf(" %d failed", faulty);
340         printf("\n");
341 }
342
343
344 static void brief_examine_super1(struct supertype *st)
345 {
346         struct mdp_superblock_1 *sb = st->sb;
347         int i;
348         unsigned long long sb_offset;
349         char *nm;
350         char *c=map_num(pers, __le32_to_cpu(sb->level));
351
352         nm = strchr(sb->set_name, ':');
353         if (nm)
354                 nm++;
355         else if (sb->set_name[0])
356                 nm = sb->set_name;
357         else
358                 nm = "??";
359
360         printf("ARRAY /dev/md/%s level=%s ", nm, c?c:"-unknown-");
361         sb_offset = __le64_to_cpu(sb->super_offset);
362         if (sb_offset <= 4)
363                 printf("metadata=1.1 ");
364         else if (sb_offset <= 8)
365                 printf("metadata=1.2 ");
366         else
367                 printf("metadata=1.0 ");
368         printf("num-devices=%d UUID=", __le32_to_cpu(sb->raid_disks));
369         for (i=0; i<16; i++) {
370                 if ((i&3)==0 && i != 0) printf(":");
371                 printf("%02x", sb->set_uuid[i]);
372         }
373         if (sb->set_name[0])
374                 printf(" name=%.32s", sb->set_name);
375         printf("\n");
376 }
377
378 static void export_examine_super1(struct supertype *st)
379 {
380         struct mdp_superblock_1 *sb = st->sb;
381         int i;
382         int len = 32;
383
384         printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
385         printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
386         for (i=0; i<32; i++)
387                 if (sb->set_name[i] == '\n' ||
388                     sb->set_name[i] == '\0') {
389                         len = i;
390                         break;
391                 }
392         if (len)
393                 printf("MD_NAME=%.*s\n", len, sb->set_name);
394         printf("MD_UUID=");
395         for (i=0; i<16; i++) {
396                 if ((i&3)==0 && i != 0) printf(":");
397                 printf("%02x", sb->set_uuid[i]);
398         }
399         printf("\n");
400         printf("MD_UPDATE_TIME=%llu\n",
401                __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
402         printf("MD_DEV_UUID=");
403         for (i=0; i<16; i++) {
404                 if ((i&3)==0 && i != 0) printf(":");
405                 printf("%02x", sb->device_uuid[i]);
406         }
407         printf("\n");
408         printf("MD_EVENTS=%llu\n",
409                (unsigned long long)__le64_to_cpu(sb->events));
410 }
411
412 static void detail_super1(struct supertype *st, char *homehost)
413 {
414         struct mdp_superblock_1 *sb = st->sb;
415         int i;
416         int l = homehost ? strlen(homehost) : 0;
417
418         printf("           Name : %.32s", sb->set_name);
419         if (l > 0 && l < 32 &&
420             sb->set_name[l] == ':' &&
421             strncmp(sb->set_name, homehost, l) == 0)
422                 printf("  (local to host %s)", homehost);
423         printf("\n           UUID : ");
424         for (i=0; i<16; i++) {
425                 if ((i&3)==0 && i != 0) printf(":");
426                 printf("%02x", sb->set_uuid[i]);
427         }
428         printf("\n         Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
429 }
430
431 static void brief_detail_super1(struct supertype *st)
432 {
433         struct mdp_superblock_1 *sb = st->sb;
434         int i;
435
436         if (sb->set_name[0])
437                 printf(" name=%.32s", sb->set_name);
438         printf(" UUID=");
439         for (i=0; i<16; i++) {
440                 if ((i&3)==0 && i != 0) printf(":");
441                 printf("%02x", sb->set_uuid[i]);
442         }
443 }
444
445 static void export_detail_super1(struct supertype *st)
446 {
447         struct mdp_superblock_1 *sb = st->sb;
448         int i;
449         int len = 32;
450
451         for (i=0; i<32; i++)
452                 if (sb->set_name[i] == '\n' ||
453                     sb->set_name[i] == '\0') {
454                         len = i;
455                         break;
456                 }
457         if (len)
458                 printf("MD_NAME=%.*s\n", len, sb->set_name);
459         printf("MD_UUID=");
460         for (i=0; i<16; i++) {
461                 if ((i&3)==0 && i != 0) printf(":");
462                 printf("%02x", sb->set_uuid[i]);
463         }
464         printf("\n");
465 }
466
467 #endif
468
469 static int match_home1(struct supertype *st, char *homehost)
470 {
471         struct mdp_superblock_1 *sb = st->sb;
472         int l = homehost ? strlen(homehost) : 0;
473
474         return (l > 0 && l < 32 &&
475                 sb->set_name[l] == ':' &&
476                 strncmp(sb->set_name, homehost, l) == 0);
477 }
478
479 static void uuid_from_super1(struct supertype *st, int uuid[4])
480 {
481         struct mdp_superblock_1 *super = st->sb;
482         char *cuuid = (char*)uuid;
483         int i;
484         for (i=0; i<16; i++)
485                 cuuid[i] = super->set_uuid[i];
486 }
487
488 static void getinfo_super1(struct supertype *st, struct mdinfo *info)
489 {
490         struct mdp_superblock_1 *sb = st->sb;
491         int working = 0;
492         int i;
493         int role;
494
495         info->array.major_version = 1;
496         info->array.minor_version = st->minor_version;
497         info->array.patch_version = 0;
498         info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
499         info->array.level = __le32_to_cpu(sb->level);
500         info->array.layout = __le32_to_cpu(sb->layout);
501         info->array.md_minor = -1;
502         info->array.ctime = __le64_to_cpu(sb->ctime);
503         info->array.utime = __le64_to_cpu(sb->utime);
504         info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
505         info->array.state =
506                 (__le64_to_cpu(sb->resync_offset) >= __le64_to_cpu(sb->size))
507                 ? 1 : 0;
508
509         info->data_offset = __le64_to_cpu(sb->data_offset);
510         info->component_size = __le64_to_cpu(sb->size);
511
512         info->disk.major = 0;
513         info->disk.minor = 0;
514         info->disk.number = __le32_to_cpu(sb->dev_number);
515         if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
516             __le32_to_cpu(sb->max_dev) > 512)
517                 role = 0xfffe;
518         else
519                 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
520
521         info->disk.raid_disk = -1;
522         switch(role) {
523         case 0xFFFF:
524                 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
525                 break;
526         case 0xFFFE:
527                 info->disk.state = 1; /* faulty */
528                 break;
529         default:
530                 info->disk.state = 6; /* active and in sync */
531                 info->disk.raid_disk = role;
532         }
533         info->events = __le64_to_cpu(sb->events);
534         sprintf(info->text_version, "1.%d", st->minor_version);
535
536         memcpy(info->uuid, sb->set_uuid, 16);
537
538         strncpy(info->name, sb->set_name, 32);
539         info->name[32] = 0;
540
541         if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
542                 info->reshape_active = 1;
543                 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
544                 info->new_level = __le32_to_cpu(sb->new_level);
545                 info->delta_disks = __le32_to_cpu(sb->delta_disks);
546                 info->new_layout = __le32_to_cpu(sb->new_layout);
547                 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
548         } else
549                 info->reshape_active = 0;
550
551         for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
552                 role = __le16_to_cpu(sb->dev_roles[i]);
553                 if (/*role == 0xFFFF || */role < info->array.raid_disks)
554                         working++;
555         }
556
557         info->array.working_disks = working;
558 }
559
560 static int update_super1(struct supertype *st, struct mdinfo *info,
561                          char *update,
562                          char *devname, int verbose,
563                          int uuid_set, char *homehost)
564 {
565         /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
566          * For others, the return value is ignored.
567          */
568         int rv = 0;
569         struct mdp_superblock_1 *sb = st->sb;
570
571         if (strcmp(update, "force-one")==0) {
572                 /* Not enough devices for a working array,
573                  * so bring this one up-to-date
574                  */
575                 if (sb->events != __cpu_to_le64(info->events))
576                         rv = 1;
577                 sb->events = __cpu_to_le64(info->events);
578         }
579         if (strcmp(update, "force-array")==0) {
580                 /* Degraded array and 'force' requests to
581                  * maybe need to mark it 'clean'.
582                  */
583                 switch(__le32_to_cpu(sb->level)) {
584                 case 5: case 4: case 6:
585                         /* need to force clean */
586                         if (sb->resync_offset != ~0ULL)
587                                 rv = 1;
588                         sb->resync_offset = ~0ULL;
589                 }
590         }
591         if (strcmp(update, "assemble")==0) {
592                 int d = info->disk.number;
593                 int want;
594                 if (info->disk.state == 6)
595                         want = __cpu_to_le32(info->disk.raid_disk);
596                 else
597                         want = 0xFFFF;
598                 if (sb->dev_roles[d] != want) {
599                         sb->dev_roles[d] = want;
600                         rv = 1;
601                 }
602         }
603         if (strcmp(update, "linear-grow-new") == 0) {
604                 int i;
605                 int rfd;
606                 int max = __le32_to_cpu(sb->max_dev);
607
608                 for (i=0 ; i < max ; i++)
609                         if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
610                                 break;
611                 sb->dev_number = __cpu_to_le32(i);
612                 info->disk.number = i;
613                 if (max >= __le32_to_cpu(sb->max_dev))
614                         sb->max_dev = __cpu_to_le32(max+1);
615
616                 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
617                     read(rfd, sb->device_uuid, 16) != 16) {
618                         *(__u32*)(sb->device_uuid) = random();
619                         *(__u32*)(sb->device_uuid+4) = random();
620                         *(__u32*)(sb->device_uuid+8) = random();
621                         *(__u32*)(sb->device_uuid+12) = random();
622                 }
623
624                 sb->dev_roles[i] =
625                         __cpu_to_le16(info->disk.raid_disk);
626         }
627         if (strcmp(update, "linear-grow-update") == 0) {
628                 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
629                 sb->dev_roles[info->disk.number] =
630                         __cpu_to_le16(info->disk.raid_disk);
631         }
632         if (strcmp(update, "resync") == 0) {
633                 /* make sure resync happens */
634                 sb->resync_offset = 0ULL;
635         }
636         if (strcmp(update, "uuid") == 0) {
637                 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
638
639                 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
640                         struct bitmap_super_s *bm;
641                         bm = (struct bitmap_super_s*)(st->sb+1024);
642                         memcpy(bm->uuid, sb->set_uuid, 16);
643                 }
644         }
645         if (strcmp(update, "homehost") == 0 &&
646             homehost) {
647                 char *c;
648                 update = "name";
649                 c = strchr(sb->set_name, ':');
650                 if (c)
651                         strncpy(info->name, c+1, 31 - (c-sb->set_name));
652                 else
653                         strncpy(info->name, sb->set_name, 32);
654                 info->name[32] = 0;
655         }
656         if (strcmp(update, "name") == 0) {
657                 if (info->name[0] == 0)
658                         sprintf(info->name, "%d", info->array.md_minor);
659                 memset(sb->set_name, 0, sizeof(sb->set_name));
660                 if (homehost &&
661                     strchr(info->name, ':') == NULL &&
662                     strlen(homehost)+1+strlen(info->name) < 32) {
663                         strcpy(sb->set_name, homehost);
664                         strcat(sb->set_name, ":");
665                         strcat(sb->set_name, info->name);
666                 } else
667                         strcpy(sb->set_name, info->name);
668         }
669         if (strcmp(update, "devicesize") == 0 &&
670             __le64_to_cpu(sb->super_offset) <
671             __le64_to_cpu(sb->data_offset)) {
672                 /* set data_size to device size less data_offset */
673                 struct misc_dev_info *misc = (struct misc_dev_info*)
674                         (st->sb + 1024 + sizeof(struct bitmap_super_s));
675                 printf("Size was %llu\n", (unsigned long long)
676                        __le64_to_cpu(sb->data_size));
677                 sb->data_size = __cpu_to_le64(
678                         misc->device_size - __le64_to_cpu(sb->data_offset));
679                 printf("Size is %llu\n", (unsigned long long)
680                        __le64_to_cpu(sb->data_size));
681         }
682         if (strcmp(update, "_reshape_progress")==0)
683                 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
684
685         sb->sb_csum = calc_sb_1_csum(sb);
686         return rv;
687 }
688
689 static int init_super1(struct supertype *st, mdu_array_info_t *info,
690                        unsigned long long size, char *name, char *homehost, int *uuid)
691 {
692         struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t) +
693                                              sizeof(struct misc_dev_info));
694         int spares;
695         int rfd;
696         char defname[10];
697         memset(sb, 0, 1024);
698
699         st->sb = sb;
700         if (info->major_version == -1) {
701                 /* zeroing superblock */
702                 return 0;
703         }
704
705         spares = info->working_disks - info->active_disks;
706         if (info->raid_disks + spares  > 384) {
707                 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
708                         info->raid_disks , spares, 384);
709                 return 0;
710         }
711
712         sb->magic = __cpu_to_le32(MD_SB_MAGIC);
713         sb->major_version = __cpu_to_le32(1);
714         sb->feature_map = 0;
715         sb->pad0 = 0;
716
717         if (uuid)
718                 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
719         else {
720                 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
721                     read(rfd, sb->set_uuid, 16) != 16) {
722                         *(__u32*)(sb->set_uuid) = random();
723                         *(__u32*)(sb->set_uuid+4) = random();
724                         *(__u32*)(sb->set_uuid+8) = random();
725                         *(__u32*)(sb->set_uuid+12) = random();
726                 }
727                 if (rfd >= 0) close(rfd);
728         }
729
730         if (name == NULL || *name == 0) {
731                 sprintf(defname, "%d", info->md_minor);
732                 name = defname;
733         }
734         memset(sb->set_name, 0, 32);
735         if (homehost &&
736             strchr(name, ':')== NULL &&
737             strlen(homehost)+1+strlen(name) < 32) {
738                 strcpy(sb->set_name, homehost);
739                 strcat(sb->set_name, ":");
740                 strcat(sb->set_name, name);
741         } else
742                 strcpy(sb->set_name, name);
743
744         sb->ctime = __cpu_to_le64((unsigned long long)time(0));
745         sb->level = __cpu_to_le32(info->level);
746         sb->layout = __cpu_to_le32(info->layout);
747         sb->size = __cpu_to_le64(size*2ULL);
748         sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
749         sb->raid_disks = __cpu_to_le32(info->raid_disks);
750
751         sb->data_offset = __cpu_to_le64(0);
752         sb->data_size = __cpu_to_le64(0);
753         sb->super_offset = __cpu_to_le64(0);
754         sb->recovery_offset = __cpu_to_le64(0);
755
756         sb->utime = sb->ctime;
757         sb->events = __cpu_to_le64(1);
758         if (info->state & (1<<MD_SB_CLEAN))
759                 sb->resync_offset = ~0ULL;
760         else
761                 sb->resync_offset = 0;
762         sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
763                                     sizeof(sb->dev_roles[0]));
764         memset(sb->pad3, 0, sizeof(sb->pad3));
765
766         memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
767
768         return 1;
769 }
770
771 struct devinfo {
772         int fd;
773         char *devname;
774         mdu_disk_info_t disk;
775         struct devinfo *next;
776 };
777 /* Add a device to the superblock being created */
778 static void add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
779                           int fd, char *devname)
780 {
781         struct mdp_superblock_1 *sb = st->sb;
782         __u16 *rp = sb->dev_roles + dk->number;
783         struct devinfo *di, **dip;
784
785         if ((dk->state & 6) == 6) /* active, sync */
786                 *rp = __cpu_to_le16(dk->raid_disk);
787         else if ((dk->state & ~2) == 0) /* active or idle -> spare */
788                 *rp = 0xffff;
789         else
790                 *rp = 0xfffe;
791
792         sb->dev_number = __cpu_to_le32(dk->number);
793         sb->sb_csum = calc_sb_1_csum(sb);
794
795         dip = (struct devinfo **)&st->info;
796         while (*dip)
797                 dip = &(*dip)->next;
798         di = malloc(sizeof(struct devinfo));
799         di->fd = fd;
800         di->devname = devname;
801         di->disk = *dk;
802         di->next = NULL;
803         *dip = di;
804 }
805
806 static void locate_bitmap1(struct supertype *st, int fd);
807
808 static int store_super1(struct supertype *st, int fd)
809 {
810         struct mdp_superblock_1 *sb = st->sb;
811         unsigned long long sb_offset;
812         int sbsize;
813         unsigned long long dsize;
814
815         if (!get_dev_size(fd, NULL, &dsize))
816                 return 1;
817
818         dsize >>= 9;
819
820         if (dsize < 24)
821                 return 2;
822
823         /*
824          * Calculate the position of the superblock.
825          * It is always aligned to a 4K boundary and
826          * depending on minor_version, it can be:
827          * 0: At least 8K, but less than 12K, from end of device
828          * 1: At start of device
829          * 2: 4K from start of device.
830          */
831         switch(st->minor_version) {
832         case 0:
833                 sb_offset = dsize;
834                 sb_offset -= 8*2;
835                 sb_offset &= ~(4*2-1);
836                 break;
837         case 1:
838                 sb_offset = 0;
839                 break;
840         case 2:
841                 sb_offset = 4*2;
842                 break;
843         default:
844                 return -EINVAL;
845         }
846
847
848
849         if (sb_offset != __le64_to_cpu(sb->super_offset) &&
850             0 != __le64_to_cpu(sb->super_offset)
851                 ) {
852                 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
853                 abort();
854         }
855
856         if (lseek64(fd, sb_offset << 9, 0)< 0LL)
857                 return 3;
858
859         sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
860
861         if (write(fd, sb, sbsize) != sbsize)
862                 return 4;
863
864         if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
865                 struct bitmap_super_s *bm = (struct bitmap_super_s*)
866                         (((char*)sb)+1024);
867                 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
868                         locate_bitmap1(st, fd);
869                         if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
870                             return 5;
871                 }
872         }
873         fsync(fd);
874         return 0;
875 }
876
877 static int load_super1(struct supertype *st, int fd, char *devname);
878
879 static unsigned long choose_bm_space(unsigned long devsize)
880 {
881         /* if the device is bigger than 8Gig, save 64k for bitmap usage,
882          * if bigger than 200Gig, save 128k
883          */
884         if (devsize < 64*2) return 0;
885         if (devsize - 64*2 >= 200*1024*1024*2)
886                 return 128*2;
887         if (devsize - 4*2 > 8*1024*1024*2)
888                 return 64*2;
889         return 4*2;
890 }
891
892 #ifndef MDASSEMBLE
893 static int write_init_super1(struct supertype *st)
894 {
895         struct mdp_superblock_1 *sb = st->sb;
896         struct supertype refst;
897         int rfd;
898         int rv = 0;
899         int bm_space;
900         struct devinfo *di;
901         unsigned long long dsize, array_size;
902         long long sb_offset;
903
904         for (di = st->info; di && ! rv ; di = di->next) {
905                 if (di->disk.state == 1)
906                         continue;
907                 if (di->fd < 0)
908                         continue;
909
910                 Kill(di->devname, 0, 1, 1);
911                 Kill(di->devname, 0, 1, 1);
912
913                 sb->dev_number = __cpu_to_le32(di->disk.number);
914                 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
915                         sb->devflags |= __cpu_to_le32(WriteMostly1);
916
917                 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
918                     read(rfd, sb->device_uuid, 16) != 16) {
919                         *(__u32*)(sb->device_uuid) = random();
920                         *(__u32*)(sb->device_uuid+4) = random();
921                         *(__u32*)(sb->device_uuid+8) = random();
922                         *(__u32*)(sb->device_uuid+12) = random();
923                 }
924                 if (rfd >= 0) close(rfd);
925                 sb->events = 0;
926
927                 refst =*st;
928                 refst.sb = NULL;
929                 if (load_super1(&refst, di->fd, NULL)==0) {
930                         struct mdp_superblock_1 *refsb = refst.sb;
931
932                         memcpy(sb->device_uuid, refsb->device_uuid, 16);
933                         if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
934                                 /* same array, so preserve events and
935                                  * dev_number */
936                                 sb->events = refsb->events;
937                                 /* bugs in 2.6.17 and earlier mean the
938                                  * dev_number chosen in Manage must be preserved
939                                  */
940                                 if (get_linux_version() >= 2006018)
941                                         sb->dev_number = refsb->dev_number;
942                         }
943                         free(refsb);
944                 }
945
946                 if (!get_dev_size(di->fd, NULL, &dsize))
947                         return 1;
948                 dsize >>= 9;
949
950                 if (dsize < 24) {
951                         close(di->fd);
952                         return 2;
953                 }
954
955
956                 /*
957                  * Calculate the position of the superblock.
958                  * It is always aligned to a 4K boundary and
959                  * depending on minor_version, it can be:
960                  * 0: At least 8K, but less than 12K, from end of device
961                  * 1: At start of device
962                  * 2: 4K from start of device.
963                  * Depending on the array size, we might leave extra space
964                  * for a bitmap.
965                  */
966                 array_size = __le64_to_cpu(sb->size);
967                 /* work out how much space we left for a bitmap */
968                 bm_space = choose_bm_space(array_size);
969
970                 switch(st->minor_version) {
971                 case 0:
972                         sb_offset = dsize;
973                         sb_offset -= 8*2;
974                         sb_offset &= ~(4*2-1);
975                         sb->super_offset = __cpu_to_le64(sb_offset);
976                         sb->data_offset = __cpu_to_le64(0);
977                 if (sb_offset - bm_space < array_size)
978                         bm_space = sb_offset - array_size;
979                         sb->data_size = __cpu_to_le64(sb_offset - bm_space);
980                         break;
981                 case 1:
982                         sb->super_offset = __cpu_to_le64(0);
983                         if (4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
984                                 bm_space = dsize - __le64_to_cpu(sb->size) -4*2;
985                         sb->data_offset = __cpu_to_le64(bm_space + 4*2);
986                         sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
987                         break;
988                 case 2:
989                         sb_offset = 4*2;
990                         sb->super_offset = __cpu_to_le64(4*2);
991                         if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
992                             > dsize)
993                                 bm_space = dsize - __le64_to_cpu(sb->size)
994                                         - 4*2 - 4*2;
995                         sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
996                         sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2
997                                                       - bm_space );
998                         break;
999                 default:
1000                         return -EINVAL;
1001                 }
1002
1003
1004                 sb->sb_csum = calc_sb_1_csum(sb);
1005                 rv = store_super1(st, di->fd);
1006                 if (rv)
1007                         fprintf(stderr,
1008                                 Name ": failed to write superblock to %s\n",
1009                                 di->devname);
1010
1011                 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1012                         rv = st->ss->write_bitmap(st, di->fd);
1013                 close(di->fd);
1014                 di->fd = -1;
1015         }
1016         return rv;
1017 }
1018 #endif
1019
1020 static int compare_super1(struct supertype *st, struct supertype *tst)
1021 {
1022         /*
1023          * return:
1024          *  0 same, or first was empty, and second was copied
1025          *  1 second had wrong number
1026          *  2 wrong uuid
1027          *  3 wrong other info
1028          */
1029         struct mdp_superblock_1 *first = st->sb;
1030         struct mdp_superblock_1 *second = tst->sb;
1031
1032         if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1033                 return 1;
1034         if (second->major_version != __cpu_to_le32(1))
1035                 return 1;
1036
1037         if (!first) {
1038                 first = malloc(1024+sizeof(bitmap_super_t) +
1039                                sizeof(struct misc_dev_info));
1040                 memcpy(first, second, 1024+sizeof(bitmap_super_t) +
1041                        sizeof(struct misc_dev_info));
1042                 st->sb = first;
1043                 return 0;
1044         }
1045         if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1046                 return 2;
1047
1048         if (first->ctime      != second->ctime     ||
1049             first->level      != second->level     ||
1050             first->layout     != second->layout    ||
1051             first->size       != second->size      ||
1052             first->chunksize  != second->chunksize ||
1053             first->raid_disks != second->raid_disks)
1054                 return 3;
1055         return 0;
1056 }
1057
1058 static void free_super1(struct supertype *st);
1059
1060 static int load_super1(struct supertype *st, int fd, char *devname)
1061 {
1062         unsigned long long dsize;
1063         unsigned long long sb_offset;
1064         struct mdp_superblock_1 *super;
1065         int uuid[4];
1066         struct bitmap_super_s *bsb;
1067         struct misc_dev_info *misc;
1068
1069         free_super1(st);
1070
1071         if (st->subarray[0])
1072                 return 1;
1073
1074         if (st->ss == NULL || st->minor_version == -1) {
1075                 int bestvers = -1;
1076                 struct supertype tst;
1077                 __u64 bestctime = 0;
1078                 /* guess... choose latest ctime */
1079                 memset(&tst, 0, sizeof(tst));
1080                 tst.ss = &super1;
1081                 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1082                         switch(load_super1(&tst, fd, devname)) {
1083                         case 0: super = tst.sb;
1084                                 if (bestvers == -1 ||
1085                                     bestctime < __le64_to_cpu(super->ctime)) {
1086                                         bestvers = tst.minor_version;
1087                                         bestctime = __le64_to_cpu(super->ctime);
1088                                 }
1089                                 free(super);
1090                                 tst.sb = NULL;
1091                                 break;
1092                         case 1: return 1; /*bad device */
1093                         case 2: break; /* bad, try next */
1094                         }
1095                 }
1096                 if (bestvers != -1) {
1097                         int rv;
1098                         tst.minor_version = bestvers;
1099                         tst.ss = &super1;
1100                         tst.max_devs = 384;
1101                         rv = load_super1(&tst, fd, devname);
1102                         if (rv == 0)
1103                                 *st = tst;
1104                         return rv;
1105                 }
1106                 return 2;
1107         }
1108         if (!get_dev_size(fd, devname, &dsize))
1109                 return 1;
1110         dsize >>= 9;
1111
1112         if (dsize < 24) {
1113                 if (devname)
1114                         fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1115                                 devname, dsize);
1116                 return 1;
1117         }
1118
1119         /*
1120          * Calculate the position of the superblock.
1121          * It is always aligned to a 4K boundary and
1122          * depending on minor_version, it can be:
1123          * 0: At least 8K, but less than 12K, from end of device
1124          * 1: At start of device
1125          * 2: 4K from start of device.
1126          */
1127         switch(st->minor_version) {
1128         case 0:
1129                 sb_offset = dsize;
1130                 sb_offset -= 8*2;
1131                 sb_offset &= ~(4*2-1);
1132                 break;
1133         case 1:
1134                 sb_offset = 0;
1135                 break;
1136         case 2:
1137                 sb_offset = 4*2;
1138                 break;
1139         default:
1140                 return -EINVAL;
1141         }
1142
1143         ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1144
1145
1146         if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1147                 if (devname)
1148                         fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1149                                 devname, strerror(errno));
1150                 return 1;
1151         }
1152
1153         super = malloc(1024 + sizeof(bitmap_super_t) +
1154                        sizeof(struct misc_dev_info));
1155
1156         if (read(fd, super, 1024) != 1024) {
1157                 if (devname)
1158                         fprintf(stderr, Name ": Cannot read superblock on %s\n",
1159                                 devname);
1160                 free(super);
1161                 return 1;
1162         }
1163
1164         if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1165                 if (devname)
1166                         fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1167                                 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1168                 free(super);
1169                 return 2;
1170         }
1171
1172         if (__le32_to_cpu(super->major_version) != 1) {
1173                 if (devname)
1174                         fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1175                                 devname, __le32_to_cpu(super->major_version));
1176                 free(super);
1177                 return 2;
1178         }
1179         if (__le64_to_cpu(super->super_offset) != sb_offset) {
1180                 if (devname)
1181                         fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1182                                 devname);
1183                 free(super);
1184                 return 2;
1185         }
1186         st->sb = super;
1187
1188         bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1189
1190         misc = (struct misc_dev_info*) (bsb+1);
1191         misc->device_size = dsize;
1192
1193         /* Now check on the bitmap superblock */
1194         if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1195                 return 0;
1196         /* Read the bitmap superblock and make sure it looks
1197          * valid.  If it doesn't clear the bit.  An --assemble --force
1198          * should get that written out.
1199          */
1200         locate_bitmap1(st, fd);
1201         if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1202             != sizeof(struct bitmap_super_s))
1203                 goto no_bitmap;
1204
1205         uuid_from_super1(st, uuid);
1206         if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1207             memcmp(bsb->uuid, uuid, 16) != 0)
1208                 goto no_bitmap;
1209         return 0;
1210
1211  no_bitmap:
1212         super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
1213         return 0;
1214 }
1215
1216
1217 static struct supertype *match_metadata_desc1(char *arg)
1218 {
1219         struct supertype *st = malloc(sizeof(*st));
1220         if (!st) return st;
1221
1222         memset(st, 0, sizeof(*st));
1223         st->ss = &super1;
1224         st->max_devs = 384;
1225         st->sb = NULL;
1226         if (strcmp(arg, "1.0") == 0) {
1227                 st->minor_version = 0;
1228                 return st;
1229         }
1230         if (strcmp(arg, "1.1") == 0) {
1231                 st->minor_version = 1;
1232                 return st;
1233         }
1234         if (strcmp(arg, "1.2") == 0) {
1235                 st->minor_version = 2;
1236                 return st;
1237         }
1238         if (strcmp(arg, "1") == 0 ||
1239             strcmp(arg, "default") == 0) {
1240                 st->minor_version = -1;
1241                 return st;
1242         }
1243
1244         free(st);
1245         return NULL;
1246 }
1247
1248 /* find available size on device with this devsize, using
1249  * superblock type st, and reserving 'reserve' sectors for
1250  * a possible bitmap
1251  */
1252 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1253 {
1254         if (devsize < 24)
1255                 return 0;
1256
1257         devsize -= choose_bm_space(devsize);
1258
1259         switch(st->minor_version) {
1260         case -1: /* no specified.  Now time to set default */
1261                 st->minor_version = 0;
1262                 /* FALL THROUGH */
1263         case 0:
1264                 /* at end */
1265                 return ((devsize - 8*2 ) & ~(4*2-1));
1266         case 1:
1267                 /* at start, 4K for superblock and possible bitmap */
1268                 return devsize - 4*2;
1269         case 2:
1270                 /* 4k from start, 4K for superblock and possible bitmap */
1271                 return devsize - (4+4)*2;
1272         }
1273         return 0;
1274 }
1275
1276 static int
1277 add_internal_bitmap1(struct supertype *st,
1278                      int *chunkp, int delay, int write_behind,
1279                      unsigned long long size,
1280                      int may_change, int major)
1281 {
1282         /*
1283          * If not may_change, then this is a 'Grow', and the bitmap
1284          * must fit after the superblock.
1285          * If may_change, then this is create, and we can put the bitmap
1286          * before the superblock if we like, or may move the start.
1287          * If !may_change, the bitmap MUST live at offset of 1K, until
1288          * we get a sysfs interface.
1289          *
1290          * size is in sectors,  chunk is in bytes !!!
1291          */
1292
1293         unsigned long long bits;
1294         unsigned long long max_bits;
1295         unsigned long long min_chunk;
1296         long offset;
1297         int chunk = *chunkp;
1298         int room = 0;
1299         struct mdp_superblock_1 *sb = st->sb;
1300         bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1301
1302         switch(st->minor_version) {
1303         case 0:
1304                 /* either 3K after the superblock, or some amount of space
1305                  * before.
1306                  */
1307                 if (may_change) {
1308                         /* We are creating array, so we *know* how much room has
1309                          * been left.
1310                          */
1311                         offset = 0;
1312                         room = choose_bm_space(__le64_to_cpu(sb->size));
1313                         if (room == 4*2) {
1314                                 /* make it 3K after the superblock */
1315                                 room = 3*2;
1316                                 offset = 2;
1317                         }
1318                 } else {
1319                         room = __le64_to_cpu(sb->super_offset)
1320                                 - __le64_to_cpu(sb->data_offset)
1321                                 - __le64_to_cpu(sb->data_size);
1322                         /* remove '1 ||' when we can set offset via sysfs */
1323                         if (1 || (room < 3*2 &&
1324                                   __le32_to_cpu(sb->max_dev) <= 384)) {
1325                                 room = 3*2;
1326                                 offset = 1*2;
1327                         } else {
1328                                 offset = 0; /* means movable offset */
1329                         }
1330                 }
1331                 break;
1332         case 1:
1333         case 2: /* between superblock and data */
1334                 if (may_change) {
1335                         offset = 4*2;
1336                         room = choose_bm_space(__le64_to_cpu(sb->size));
1337                 } else {
1338                         room = __le64_to_cpu(sb->data_offset)
1339                                 - __le64_to_cpu(sb->super_offset);
1340                         if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1341                                 room -= 2;
1342                                 offset = 2;
1343                         } else {
1344                                 room -= 4*2;
1345                                 offset = 4*2;
1346                         }
1347                 }
1348                 break;
1349         default:
1350                 return 0;
1351         }
1352
1353         if (chunk == UnSet && room > 128*2)
1354                 /* Limit to 128K of bitmap when chunk size not requested */
1355                 room = 128*2;
1356
1357         max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1358
1359         min_chunk = 4096; /* sub-page chunks don't work yet.. */
1360         bits = (size*512)/min_chunk +1;
1361         while (bits > max_bits) {
1362                 min_chunk *= 2;
1363                 bits = (bits+1)/2;
1364         }
1365         if (chunk == UnSet)
1366                 chunk = min_chunk;
1367         else if (chunk < min_chunk)
1368                 return 0; /* chunk size too small */
1369         if (chunk == 0) /* rounding problem */
1370                 return 0;
1371
1372         if (offset == 0) {
1373                 bits = (size*512) / chunk + 1;
1374                 room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512;
1375                 offset = -room;
1376         }
1377
1378         sb->bitmap_offset = __cpu_to_le32(offset);
1379
1380         sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
1381         memset(bms, 0, sizeof(*bms));
1382         bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1383         bms->version = __cpu_to_le32(major);
1384         uuid_from_super1(st, (int*)bms->uuid);
1385         bms->chunksize = __cpu_to_le32(chunk);
1386         bms->daemon_sleep = __cpu_to_le32(delay);
1387         bms->sync_size = __cpu_to_le64(size);
1388         bms->write_behind = __cpu_to_le32(write_behind);
1389
1390         *chunkp = chunk;
1391         return 1;
1392 }
1393
1394
1395 static void locate_bitmap1(struct supertype *st, int fd)
1396 {
1397         unsigned long long offset;
1398         struct mdp_superblock_1 *sb;
1399         int mustfree = 0;
1400
1401         if (!st->sb) {
1402                 if (st->ss->load_super(st, fd, NULL))
1403                         return; /* no error I hope... */
1404                 mustfree = 1;
1405         }
1406         sb = st->sb;
1407
1408         offset = __le64_to_cpu(sb->super_offset);
1409         offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1410         if (mustfree)
1411                 free(sb);
1412         lseek64(fd, offset<<9, 0);
1413 }
1414
1415 static int write_bitmap1(struct supertype *st, int fd)
1416 {
1417         struct mdp_superblock_1 *sb = st->sb;
1418         bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
1419         int rv = 0;
1420
1421         int towrite, n;
1422         char buf[4096];
1423
1424         locate_bitmap1(st, fd);
1425
1426         if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1427             sizeof(bitmap_super_t))
1428                 return -2;
1429         towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1430         towrite = (towrite+7) >> 3; /* bits to bytes */
1431         memset(buf, 0xff, sizeof(buf));
1432         while (towrite > 0) {
1433                 n = towrite;
1434                 if (n > sizeof(buf))
1435                         n = sizeof(buf);
1436                 n = write(fd, buf, n);
1437                 if (n > 0)
1438                         towrite -= n;
1439                 else
1440                         break;
1441         }
1442         fsync(fd);
1443         if (towrite)
1444                 rv = -2;
1445
1446         return rv;
1447 }
1448
1449 static void free_super1(struct supertype *st)
1450 {
1451         if (st->sb)
1452                 free(st->sb);
1453         st->sb = NULL;
1454 }
1455
1456 static int validate_geometry1(struct supertype *st, int level,
1457                               int layout, int raiddisks,
1458                               int chunk, unsigned long long size,
1459                               char *subdev, unsigned long long *freesize)
1460 {
1461         unsigned long long ldsize;
1462         int fd;
1463
1464         if (level == LEVEL_CONTAINER)
1465                 return 0;
1466         if (!subdev)
1467                 return 1;
1468
1469         fd = open(subdev, O_RDONLY|O_EXCL, 0);
1470         if (fd < 0) {
1471                 fprintf(stderr, Name ": Cannot open %s: %s\n",
1472                         subdev, strerror(errno));
1473                 return 0;
1474         }
1475         if (!get_dev_size(fd, subdev, &ldsize)) {
1476                 close(fd);
1477                 return 0;
1478         }
1479         close(fd);
1480
1481         *freesize = avail_size1(st, ldsize >> 9);
1482         return 1;
1483 }
1484
1485 struct superswitch super1 = {
1486 #ifndef MDASSEMBLE
1487         .examine_super = examine_super1,
1488         .brief_examine_super = brief_examine_super1,
1489         .export_examine_super = export_examine_super1,
1490         .detail_super = detail_super1,
1491         .brief_detail_super = brief_detail_super1,
1492         .export_detail_super = export_detail_super1,
1493         .write_init_super = write_init_super1,
1494 #endif
1495         .match_home = match_home1,
1496         .uuid_from_super = uuid_from_super1,
1497         .getinfo_super = getinfo_super1,
1498         .update_super = update_super1,
1499         .init_super = init_super1,
1500         .add_to_super = add_to_super1,
1501         .store_super = store_super1,
1502         .compare_super = compare_super1,
1503         .load_super = load_super1,
1504         .match_metadata_desc = match_metadata_desc1,
1505         .avail_size = avail_size1,
1506         .add_internal_bitmap = add_internal_bitmap1,
1507         .locate_bitmap = locate_bitmap1,
1508         .write_bitmap = write_bitmap1,
1509         .free_super = free_super1,
1510         .validate_geometry = validate_geometry1,
1511 #if __BYTE_ORDER == BIG_ENDIAN
1512         .swapuuid = 0,
1513 #else
1514         .swapuuid = 1,
1515 #endif
1516 };