Create missing /dev files where needed.
[thirdparty/mdadm.git] / super0.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
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 /*
33  * All handling for the 0.90.0 version superblock is in
34  * this file.
35  * This includes:
36  *   - finding, loading, and writing the superblock.
37  *   - initialising a new superblock
38  *   - printing the superblock for --examine
39  *   - printing part of the superblock for --detail
40  * .. other stuff 
41  */
42
43
44 static unsigned long calc_sb0_csum(mdp_super_t *super)
45 {
46         unsigned long csum = super->sb_csum;
47         unsigned long newcsum;
48         super->sb_csum= 0 ;
49         newcsum = calc_csum(super, MD_SB_BYTES);
50         super->sb_csum = csum;
51         return newcsum;
52 }
53
54
55 void super0_swap_endian(struct mdp_superblock_s *sb)
56 {
57         /* as super0 superblocks are host-endian, it is sometimes
58          * useful to be able to swap the endianness 
59          * as (almost) everything is u32's we byte-swap every 4byte
60          * number.
61          * We then also have to swap the events_hi and events_lo
62          */
63         char *sbc = (char *)sb;
64         __u32 t32;
65         int i;
66
67         for (i=0; i < MD_SB_BYTES ; i+=4) {
68                 char t = sbc[i];
69                 sbc[i] = sbc[i+3];
70                 sbc[i+3] = t;
71                 t=sbc[i+1];
72                 sbc[i+1]=sbc[i+2];
73                 sbc[i+2]=t;
74         }
75         t32 = sb->events_hi;
76         sb->events_hi = sb->events_lo;
77         sb->events_lo = t32;
78
79         t32 = sb->cp_events_hi;
80         sb->cp_events_hi = sb->cp_events_lo;
81         sb->cp_events_lo = t32;
82
83 }
84
85 #ifndef MDASSEMBLE
86
87 static void examine_super0(void *sbv)
88 {
89         mdp_super_t *sb = sbv;
90         time_t atime;
91         int d;
92         char *c;
93
94         printf("          Magic : %08x\n", sb->md_magic);
95         printf("        Version : %02d.%02d.%02d\n", sb->major_version, sb->minor_version,
96                sb->patch_version);
97         if (sb->minor_version >= 90)
98                 printf("           UUID : %08x:%08x:%08x:%08x\n", sb->set_uuid0, sb->set_uuid1,
99                        sb->set_uuid2, sb->set_uuid3);
100         else
101                 printf("           UUID : %08x\n", sb->set_uuid0);
102
103         atime = sb->ctime;
104         printf("  Creation Time : %.24s\n", ctime(&atime));
105         c=map_num(pers, sb->level);
106         printf("     Raid Level : %s\n", c?c:"-unknown-");
107         if ((int)sb->level >= 0) {
108                 int ddsks=0;
109                 printf("    Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
110                 switch(sb->level) {
111                 case 1: ddsks=1;break;
112                 case 4:
113                 case 5: ddsks = sb->raid_disks-1; break;
114                 case 6: ddsks = sb->raid_disks-2; break;
115                 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
116                 }
117                 if (ddsks)
118                         printf("     Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
119                                human_size(ddsks*(long long)sb->size<<10));
120         }
121         printf("   Raid Devices : %d\n", sb->raid_disks);
122         printf("  Total Devices : %d\n", sb->nr_disks);
123         printf("Preferred Minor : %d\n", sb->md_minor);
124         printf("\n");
125         if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
126                 printf("  Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
127                 if (sb->delta_disks) {
128                         printf("  Delta Devices : %d", sb->delta_disks);
129                         if (sb->delta_disks)
130                                 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
131                         else
132                                 printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks);
133                 }
134                 if (sb->new_level != sb->level) {
135                         c = map_num(pers, sb->new_level);
136                         printf("      New Level : %s\n", c?c:"-unknown-");
137                 }
138                 if (sb->new_layout != sb->layout) {
139                         if (sb->level == 5) {
140                                 c = map_num(r5layout, sb->new_layout);
141                                 printf("     New Layout : %s\n", c?c:"-unknown-");
142                         }
143                         if (sb->level == 10) {
144                                 printf("     New Layout : near=%d, far=%d\n",
145                                        sb->new_layout&255, (sb->new_layout>>8)&255);
146                         }
147                 }
148                 if (sb->new_chunk != sb->chunk_size)
149                         printf("  New Chunksize : %d\n", sb->new_chunk);
150                 printf("\n");
151         }
152         atime = sb->utime;
153         printf("    Update Time : %.24s\n", ctime(&atime));
154         printf("          State : %s\n",
155                (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
156         if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
157                 printf("Internal Bitmap : present\n");
158         printf(" Active Devices : %d\n", sb->active_disks);
159         printf("Working Devices : %d\n", sb->working_disks);
160         printf(" Failed Devices : %d\n", sb->failed_disks);
161         printf("  Spare Devices : %d\n", sb->spare_disks);
162         if (calc_sb0_csum(sb) == sb->sb_csum)
163                 printf("       Checksum : %x - correct\n", sb->sb_csum);
164         else
165                 printf("       Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
166         printf("         Events : %d.%d\n", sb->events_hi, sb->events_lo);
167         printf("\n");
168         if (sb->level == 5) {
169                 c = map_num(r5layout, sb->layout);
170                 printf("         Layout : %s\n", c?c:"-unknown-");
171         }
172         if (sb->level == 10) {
173                 printf("         Layout : near=%d, far=%d\n",
174                        sb->layout&255, (sb->layout>>8)&255);
175         }
176         switch(sb->level) {
177         case 0:
178         case 4:
179         case 5:
180                 printf("     Chunk Size : %dK\n", sb->chunk_size/1024);
181                 break;
182         case -1:
183                 printf("       Rounding : %dK\n", sb->chunk_size/1024);
184                 break;
185         default: break;
186         }
187         printf("\n");
188         printf("      Number   Major   Minor   RaidDevice State\n");
189         for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
190                 mdp_disk_t *dp;
191                 char *dv;
192                 char nb[5];
193                 int wonly;
194                 if (d>=0) dp = &sb->disks[d];
195                 else dp = &sb->this_disk;
196                 snprintf(nb, sizeof(nb), "%4d", d);
197                 printf("%4s %5d   %5d    %5d    %5d     ", d < 0 ? "this" :  nb,
198                        dp->number, dp->major, dp->minor, dp->raid_disk);
199                 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
200                 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
201                 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
202                 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
203                 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
204                 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
205                 if (wonly) printf(" write-mostly");
206                 if (dp->state == 0) printf(" spare");
207                 if ((dv=map_dev(dp->major, dp->minor, 0)))
208                         printf("   %s", dv);
209                 printf("\n");
210                 if (d == -1) printf("\n");
211         }
212 }
213
214 static void brief_examine_super0(void *sbv)
215 {
216         mdp_super_t *sb = sbv;
217         char *c=map_num(pers, sb->level);
218
219         printf("ARRAY %s level=%s num-devices=%d UUID=",
220                get_md_name(sb->md_minor),
221                c?c:"-unknown-", sb->raid_disks);
222         if (sb->minor_version >= 90)
223                 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
224                        sb->set_uuid2, sb->set_uuid3);
225         else
226                 printf("%08x", sb->set_uuid0);
227         printf("\n");
228 }
229
230 static void detail_super0(void *sbv)
231 {
232         mdp_super_t *sb = sbv;
233         printf("           UUID : ");
234         if (sb->minor_version >= 90)
235                 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
236                        sb->set_uuid2, sb->set_uuid3);
237         else
238                 printf("%08x", sb->set_uuid0);
239         printf("\n         Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
240 }
241
242 static void brief_detail_super0(void *sbv)
243 {
244         mdp_super_t *sb = sbv;
245         printf(" UUID=");
246         if (sb->minor_version >= 90)
247                 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
248                        sb->set_uuid2, sb->set_uuid3);
249         else
250                 printf("%08x", sb->set_uuid0);
251 }
252 #endif
253 static void uuid_from_super0(int uuid[4], void * sbv)
254 {
255         mdp_super_t *super = sbv;
256         uuid[0] = super->set_uuid0;
257         if (super->minor_version >= 90) {
258                 uuid[1] = super->set_uuid1;
259                 uuid[2] = super->set_uuid2;
260                 uuid[3] = super->set_uuid3;
261         } else {
262                 uuid[1] = 0;
263                 uuid[2] = 0;
264                 uuid[3] = 0;
265         }
266 }
267
268 static void getinfo_super0(struct mdinfo *info, void *sbv)
269 {
270         mdp_super_t *sb = sbv;
271         int working = 0;
272         int i;
273
274         info->array.major_version = sb->major_version;
275         info->array.minor_version = sb->minor_version;
276         info->array.patch_version = sb->patch_version;
277         info->array.raid_disks = sb->raid_disks;
278         info->array.level = sb->level;
279         info->array.layout = sb->layout;
280         info->array.md_minor = sb->md_minor;
281         info->array.ctime = sb->ctime;
282         info->array.utime = sb->utime;
283         info->array.chunk_size = sb->chunk_size;
284         info->component_size = sb->size*2;
285
286         info->disk.state = sb->this_disk.state;
287         info->disk.major = sb->this_disk.major;
288         info->disk.minor = sb->this_disk.minor;
289         info->disk.raid_disk = sb->this_disk.raid_disk;
290         info->disk.number = sb->this_disk.number;
291
292         info->events = md_event(sb);
293         info->data_offset = 0;
294
295         uuid_from_super0(info->uuid, sbv);
296
297         if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
298                 info->reshape_active = 1;
299                 info->reshape_progress = sb->reshape_position;
300                 info->new_level = sb->new_level;
301                 info->delta_disks = sb->delta_disks;
302                 info->new_layout = sb->new_layout;
303                 info->new_chunk = sb->new_chunk;
304         } else
305                 info->reshape_active = 0;
306
307         info->name[0] = 0;
308         /* work_disks is calculated rather than read directly */
309         for (i=0; i < MD_SB_DISKS; i++)
310                 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
311                     (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
312                     !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
313                         working ++;
314         info->array.working_disks = working;
315 }
316
317
318 static int update_super0(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
319 {
320         int rv = 0;
321         mdp_super_t *sb = sbv;
322         if (strcmp(update, "sparc2.2")==0 ) {
323                 /* 2.2 sparc put the events in the wrong place
324                  * So we copy the tail of the superblock
325                  * up 4 bytes before continuing
326                  */
327                 __u32 *sb32 = (__u32*)sb;
328                 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
329                        sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
330                        (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
331                 if (verbose >= 0)
332                         fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
333                                  devname);
334         }
335         if (strcmp(update, "super-minor") ==0) {
336                 sb->md_minor = info->array.md_minor;
337                 if (verbose > 0)
338                         fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
339                                 devname, info->array.md_minor);
340         }
341         if (strcmp(update, "summaries") == 0) {
342                 int i;
343                 /* set nr_disks, active_disks, working_disks,
344                  * failed_disks, spare_disks based on disks[] 
345                  * array in superblock.
346                  * Also make sure extra slots aren't 'failed'
347                  */
348                 sb->nr_disks = sb->active_disks =
349                         sb->working_disks = sb->failed_disks =
350                         sb->spare_disks = 0;
351                 for (i=0; i < MD_SB_DISKS ; i++) 
352                         if (sb->disks[i].major ||
353                             sb->disks[i].minor) {
354                                 int state = sb->disks[i].state;
355                                 if (state & (1<<MD_DISK_REMOVED))
356                                         continue;
357                                 sb->nr_disks++;
358                                 if (state & (1<<MD_DISK_ACTIVE))
359                                         sb->active_disks++;
360                                 if (state & (1<<MD_DISK_FAULTY))
361                                         sb->failed_disks++;
362                                 else
363                                         sb->working_disks++;
364                                 if (state == 0)
365                                         sb->spare_disks++;
366                         } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
367                                 sb->disks[i].state = 0;
368         }
369         if (strcmp(update, "force")==0) {
370                 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
371                 sb->events_lo = (info->events) & 0xFFFFFFFF;
372                 if (sb->level == 5 || sb->level == 4 || sb->level == 6)
373                         /* need to force clean */
374                         sb->state |= (1 << MD_SB_CLEAN);
375         }
376         if (strcmp(update, "assemble")==0) {
377                 int d = info->disk.number;
378                 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
379                 sb->disks[d].state &= ~(1<<MD_DISK_WRITEMOSTLY);
380                 if (sb->disks[d].state != info->disk.state) {
381                         sb->disks[d].state = info->disk.state & wonly;
382                         rv = 1;
383                 }
384         }
385         if (strcmp(update, "newdev") == 0) {
386                 int d = info->disk.number;
387                 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
388                 sb->disks[d].number = d;
389                 sb->disks[d].major = info->disk.major;
390                 sb->disks[d].minor = info->disk.minor;
391                 sb->disks[d].raid_disk = info->disk.raid_disk;
392                 sb->disks[d].state = info->disk.state;
393                 sb->this_disk = sb->disks[d];
394         }
395         if (strcmp(update, "grow") == 0) {
396                 sb->raid_disks = info->array.raid_disks;
397                 sb->nr_disks = info->array.nr_disks;
398                 sb->active_disks = info->array.active_disks;
399                 sb->working_disks = info->array.working_disks;
400                 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
401                 sb->disks[info->disk.number].number = info->disk.number;
402                 sb->disks[info->disk.number].major = info->disk.major;
403                 sb->disks[info->disk.number].minor = info->disk.minor;
404                 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
405                 sb->disks[info->disk.number].state = info->disk.state;
406                 if (sb->this_disk.number == info->disk.number)
407                         sb->this_disk = sb->disks[info->disk.number];
408         }
409         if (strcmp(update, "resync") == 0) {
410                 /* make sure resync happens */
411                 sb->state &= ~(1<<MD_SB_CLEAN);
412                 sb->recovery_cp = 0;
413         }
414         if (strcmp(update, "uuid") == 0) {
415                 sb->set_uuid0 = info->uuid[0];
416                 sb->set_uuid1 = info->uuid[1];
417                 sb->set_uuid2 = info->uuid[2];
418                 sb->set_uuid3 = info->uuid[3];
419         }
420         if (strcmp(update, "_reshape_progress")==0)
421                 sb->reshape_position = info->reshape_progress;
422
423         sb->sb_csum = calc_sb0_csum(sb);
424         return rv;
425 }
426
427 static __u64 event_super0(void *sbv)
428 {
429         mdp_super_t *sb = sbv;
430         return md_event(sb);
431 }
432
433
434
435 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info, unsigned long long size, char *ignored_name)
436 {
437         mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
438         int spares;
439         int rfd;
440         memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
441
442         if (info->major_version == -1) {
443                 /* zeroing the superblock */
444                 return 0;
445         }
446
447         spares = info->working_disks - info->active_disks;
448         if (info->raid_disks + spares  > MD_SB_DISKS) {
449                 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
450                         info->raid_disks , spares, MD_SB_DISKS);
451                 return 0;
452         }
453
454         rfd = open("/dev/urandom", O_RDONLY);
455         sb->md_magic = MD_SB_MAGIC;
456         sb->major_version = 0;
457         sb->minor_version = 90;
458         sb->patch_version = 0;
459         sb->gvalid_words = 0; /* ignored */
460         if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
461                 sb->set_uuid0 = random();
462         sb->ctime = time(0);
463         sb->level = info->level;
464         if (size != info->size)
465                 return 0;
466         sb->size = info->size;
467         sb->nr_disks = info->nr_disks;
468         sb->raid_disks = info->raid_disks;
469         sb->md_minor = info->md_minor;
470         sb->not_persistent = 0;
471         if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
472                 sb->set_uuid1 = random();
473                 sb->set_uuid2 = random();
474                 sb->set_uuid3 = random();
475         }
476         if (rfd >= 0)
477                 close(rfd);
478
479         sb->utime = sb->ctime;
480         sb->state = info->state;
481         sb->active_disks = info->active_disks;
482         sb->working_disks = info->working_disks;
483         sb->failed_disks = info->failed_disks;
484         sb->spare_disks = info->spare_disks;
485         sb->events_hi = 0;
486         sb->events_lo = 1;
487
488         sb->layout = info->layout;
489         sb->chunk_size = info->chunk_size;
490
491         *sbp = sb;
492         return 1;
493 }
494
495 /* Add a device to the superblock being created */
496 static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
497 {
498         mdp_super_t *sb = sbv;
499         mdp_disk_t *dk = &sb->disks[dinfo->number];
500
501         dk->number = dinfo->number;
502         dk->major = dinfo->major;
503         dk->minor = dinfo->minor;
504         dk->raid_disk = dinfo->raid_disk;
505         dk->state = dinfo->state;
506 }
507
508 static int store_super0(struct supertype *st, int fd, void *sbv)
509 {
510         unsigned long size;
511         unsigned long long dsize;
512         unsigned long long offset;
513         mdp_super_t *super = sbv;
514     
515 #ifdef BLKGETSIZE64
516         if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
517 #endif
518         {
519                 if (ioctl(fd, BLKGETSIZE, &size))
520                         return 1;
521                 else
522                         dsize = ((unsigned long long)size)<<9;
523         }
524
525         if (dsize < MD_RESERVED_SECTORS*2*512)
526                 return 2;
527
528         offset = MD_NEW_SIZE_SECTORS(dsize>>9);
529
530         offset *= 512;
531
532         if (lseek64(fd, offset, 0)< 0LL)
533                 return 3;
534
535         if (write(fd, super, sizeof(*super)) != sizeof(*super))
536                 return 4;
537
538         fsync(fd);
539         return 0;
540 }
541
542 static int write_init_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
543 {
544         mdp_super_t *sb = sbv;
545         int fd = open(devname, O_RDWR|O_EXCL);
546         int rv;
547
548         if (fd < 0) {
549                 fprintf(stderr, Name ": Failed to open %s to write superblock\n", devname);
550                 return -1;
551         }
552
553         sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY);
554         sb->disks[dinfo->number].state |= (1<<MD_DISK_SYNC);
555
556         sb->this_disk = sb->disks[dinfo->number];
557         sb->sb_csum = calc_sb0_csum(sb);
558         rv = store_super0(st, fd, sb);
559
560         if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
561                 rv = st->ss->write_bitmap(st, fd, sbv);
562
563         close(fd);
564         if (rv)
565                 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
566         return rv;
567 }
568
569 static int compare_super0(void **firstp, void *secondv)
570 {
571         /*
572          * return:
573          *  0 same, or first was empty, and second was copied
574          *  1 second had wrong number
575          *  2 wrong uuid
576          *  3 wrong other info
577          */
578         mdp_super_t *first = *firstp;
579         mdp_super_t *second = secondv;
580
581         int uuid1[4], uuid2[4];
582         if (second->md_magic != MD_SB_MAGIC)
583                 return 1;
584         if (!first) {
585                 first = malloc(MD_SB_BYTES);
586                 memcpy(first, second, MD_SB_BYTES);
587                 *firstp = first;
588                 return 0;
589         }
590
591         uuid_from_super0(uuid1, first);
592         uuid_from_super0(uuid2, second);
593         if (!same_uuid(uuid1, uuid2, 0))
594                 return 2;
595         if (first->major_version != second->major_version ||
596             first->minor_version != second->minor_version ||
597             first->patch_version != second->patch_version ||
598             first->gvalid_words  != second->gvalid_words  ||
599             first->ctime         != second->ctime         ||
600             first->level         != second->level         ||
601             first->size          != second->size          ||
602             first->raid_disks    != second->raid_disks    )
603                 return 3;
604
605         return 0;
606 }
607
608
609 static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
610 {
611         /* try to read in the superblock
612          * Return:
613          *  0 on success
614          *  1 on cannot get superblock
615          *  2 on superblock meaningless
616          */
617         unsigned long size;
618         unsigned long long dsize;
619         unsigned long long offset;
620         mdp_super_t *super;
621     
622 #ifdef BLKGETSIZE64
623         if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
624 #endif
625         {
626                 if (ioctl(fd, BLKGETSIZE, &size)) {
627                         if (devname)
628                                 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
629                                         devname, strerror(errno));
630                         return 1;
631                 } else
632                         dsize = size << 9;
633         }
634
635         if (dsize < MD_RESERVED_SECTORS*2) {
636                 if (devname)
637                         fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
638                                 devname, size);
639                 return 1;
640         }
641
642         offset = MD_NEW_SIZE_SECTORS(dsize>>9);
643
644         offset *= 512;
645
646         ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
647
648         if (lseek64(fd, offset, 0)< 0LL) {
649                 if (devname)
650                         fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
651                                 devname, strerror(errno));
652                 return 1;
653         }
654
655         super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
656
657         if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
658                 if (devname)
659                         fprintf(stderr, Name ": Cannot read superblock on %s\n",
660                                 devname);
661                 free(super);
662                 return 1;
663         }
664
665         if (st->ss && st->minor_version == 9)
666                 super0_swap_endian(super);
667
668         if (super->md_magic != MD_SB_MAGIC) {
669                 if (devname)
670                         fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
671                                 devname, MD_SB_MAGIC, super->md_magic);
672                 free(super);
673                 return 2;
674         }
675
676         if (super->major_version != 0) {
677                 if (devname)
678                         fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
679                                 devname, super->major_version);
680                 free(super);
681                 return 2;
682         }
683         *sbp = super;
684         if (st->ss == NULL) {
685                 st->ss = &super0;
686                 st->minor_version = 90;
687                 st->max_devs = MD_SB_DISKS;
688         }
689
690         return 0;
691 }
692
693 static struct supertype *match_metadata_desc0(char *arg)
694 {
695         struct supertype *st = malloc(sizeof(*st));
696         if (!st) return st;
697
698         st->ss = &super0;
699         st->minor_version = 90;
700         st->max_devs = MD_SB_DISKS;
701         if (strcmp(arg, "0") == 0 ||
702             strcmp(arg, "0.90") == 0 ||
703             strcmp(arg, "default") == 0
704                 )
705                 return st;
706
707         st->minor_version = 9; /* flag for 'byte-swapped' */
708         if (strcmp(arg, "0.swap")==0)
709                 return st;
710
711         free(st);
712         return NULL;
713 }
714
715 static __u64 avail_size0(struct supertype *st, __u64 devsize)
716 {
717         if (devsize < MD_RESERVED_SECTORS*2)
718                 return 0ULL;
719         return MD_NEW_SIZE_SECTORS(devsize);
720 }
721
722 static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major)
723 {
724         /*
725          * The bitmap comes immediately after the superblock and must be 60K in size
726          * at most.  The default size is between 30K and 60K
727          *
728          * size is in K,  chunk is in bytes !!!
729          */
730         unsigned long long bits;
731         unsigned long long max_bits = 60*1024*8;
732         unsigned long long min_chunk;
733         mdp_super_t *sb = sbv;
734         bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
735
736
737         min_chunk = 4096; /* sub-page chunks don't work yet.. */
738         bits = (size * 512)/ min_chunk +1;
739         while (bits > max_bits) {
740                 min_chunk *= 2;
741                 bits = (bits+1)/2;
742         }
743         if (chunk == UnSet)
744                 chunk = min_chunk;
745         else if (chunk < min_chunk)
746                 return 0; /* chunk size too small */
747
748         sb->state |= (1<<MD_SB_BITMAP_PRESENT);
749
750         memset(bms, 0, sizeof(*bms));
751         bms->magic = __cpu_to_le32(BITMAP_MAGIC);
752         bms->version = __cpu_to_le32(major);
753         uuid_from_super0((int*)bms->uuid, sb);
754         bms->chunksize = __cpu_to_le32(chunk);
755         bms->daemon_sleep = __cpu_to_le32(delay);
756         bms->sync_size = __cpu_to_le64(size);
757         bms->write_behind = __cpu_to_le32(write_behind);
758
759
760
761         return 1;
762 }
763
764
765 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
766 {
767         unsigned long long dsize;
768         unsigned long size;
769         unsigned long long offset;
770 #ifdef BLKGETSIZE64
771         if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
772 #endif
773         {
774                 if (ioctl(fd, BLKGETSIZE, &size))
775                         return;
776                 else
777                         dsize = ((unsigned long long)size)<<9;
778         }
779
780         if (dsize < MD_RESERVED_SECTORS*2)
781                 return;
782
783         offset = MD_NEW_SIZE_SECTORS(dsize>>9);
784
785         offset *= 512;
786
787         offset += MD_SB_BYTES;
788
789         lseek64(fd, offset, 0);
790 }
791
792 int write_bitmap0(struct supertype *st, int fd, void *sbv)
793 {
794         unsigned long size;
795         unsigned long long dsize;
796         unsigned long long offset;
797         mdp_super_t *sb = sbv;
798     
799         int rv = 0;
800
801         int towrite, n;
802         char buf[4096];
803
804 #ifdef BLKGETSIZE64
805         if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
806 #endif
807         {
808                 if (ioctl(fd, BLKGETSIZE, &size))
809                         return 1;
810                 else
811                         dsize = ((unsigned long long)size)<<9;
812         }
813
814         if (dsize < MD_RESERVED_SECTORS*2)
815         return -1;
816
817         offset = MD_NEW_SIZE_SECTORS(dsize>>9);
818
819         offset *= 512;
820
821         if (lseek64(fd, offset + 4096, 0)< 0LL)
822                 return 3;
823
824
825         if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
826             sizeof(bitmap_super_t))
827                 return -2;
828         towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
829         memset(buf, 0xff, sizeof(buf));
830         while (towrite > 0) {
831                 n = towrite;
832                 if (n > sizeof(buf)) 
833                         n = sizeof(buf);
834                 n = write(fd, buf, n);
835                 if (n > 0)
836                         towrite -= n;
837                 else
838                         break;
839         }
840         fsync(fd);
841         if (towrite)
842                 rv = -2;
843
844         return rv;
845 }
846
847 struct superswitch super0 = {
848 #ifndef MDASSEMBLE
849         .examine_super = examine_super0,
850         .brief_examine_super = brief_examine_super0,
851         .detail_super = detail_super0,
852         .brief_detail_super = brief_detail_super0,
853 #endif
854         .uuid_from_super = uuid_from_super0,
855         .getinfo_super = getinfo_super0,
856         .update_super = update_super0,
857         .event_super = event_super0,
858         .init_super = init_super0,
859         .add_to_super = add_to_super0,
860         .store_super = store_super0,
861         .write_init_super = write_init_super0,
862         .compare_super = compare_super0,
863         .load_super = load_super0,
864         .match_metadata_desc = match_metadata_desc0,
865         .avail_size = avail_size0,
866         .add_internal_bitmap = add_internal_bitmap0,
867         .locate_bitmap = locate_bitmap0,
868         .write_bitmap = write_bitmap0,
869         .major = 0,
870         .swapuuid = 0,
871 };