Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / mdadm / super0.c
blobaeee0b7b4c8694127853f279626f3efbb7b37d2d
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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.
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.
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
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
30 #define HAVE_STDINT_H 1
31 #include "mdadm.h"
32 #include "sha1.h"
34 * All handling for the 0.90.0 version superblock is in
35 * this file.
36 * This includes:
37 * - finding, loading, and writing the superblock.
38 * - initialising a new superblock
39 * - printing the superblock for --examine
40 * - printing part of the superblock for --detail
41 * .. other stuff
45 static unsigned long calc_sb0_csum(mdp_super_t *super)
47 unsigned long csum = super->sb_csum;
48 unsigned long newcsum;
49 super->sb_csum= 0 ;
50 newcsum = calc_csum(super, MD_SB_BYTES);
51 super->sb_csum = csum;
52 return newcsum;
56 void super0_swap_endian(struct mdp_superblock_s *sb)
58 /* as super0 superblocks are host-endian, it is sometimes
59 * useful to be able to swap the endianness
60 * as (almost) everything is u32's we byte-swap every 4byte
61 * number.
62 * We then also have to swap the events_hi and events_lo
64 char *sbc = (char *)sb;
65 __u32 t32;
66 int i;
68 for (i=0; i < MD_SB_BYTES ; i+=4) {
69 char t = sbc[i];
70 sbc[i] = sbc[i+3];
71 sbc[i+3] = t;
72 t=sbc[i+1];
73 sbc[i+1]=sbc[i+2];
74 sbc[i+2]=t;
76 t32 = sb->events_hi;
77 sb->events_hi = sb->events_lo;
78 sb->events_lo = t32;
80 t32 = sb->cp_events_hi;
81 sb->cp_events_hi = sb->cp_events_lo;
82 sb->cp_events_lo = t32;
86 #ifndef MDASSEMBLE
88 static void examine_super0(void *sbv, char *homehost)
90 mdp_super_t *sb = sbv;
91 time_t atime;
92 int d;
93 char *c;
95 printf(" Magic : %08x\n", sb->md_magic);
96 printf(" Version : %02d.%02d.%02d\n", sb->major_version, sb->minor_version,
97 sb->patch_version);
98 if (sb->minor_version >= 90) {
99 printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
100 sb->set_uuid2, sb->set_uuid3);
101 if (homehost) {
102 char buf[20];
103 void *hash = sha1_buffer(homehost,
104 strlen(homehost),
105 buf);
106 if (memcmp(&sb->set_uuid2, hash, 8)==0)
107 printf(" (local to host %s)", homehost);
109 printf("\n");
110 } else
111 printf(" UUID : %08x\n", sb->set_uuid0);
113 if (sb->not_persistent)
114 printf(" Eedk : not persistent\n");
116 atime = sb->ctime;
117 printf(" Creation Time : %.24s\n", ctime(&atime));
118 c=map_num(pers, sb->level);
119 printf(" Raid Level : %s\n", c?c:"-unknown-");
120 if ((int)sb->level >= 0) {
121 int ddsks=0;
122 printf(" Used Dev Size : %d%s\n", sb->size,
123 human_size((long long)sb->size<<10));
124 switch(sb->level) {
125 case 1: ddsks=1;break;
126 case 4:
127 case 5: ddsks = sb->raid_disks-1; break;
128 case 6: ddsks = sb->raid_disks-2; break;
129 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
131 if (ddsks)
132 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
133 human_size(ddsks*(long long)sb->size<<10));
135 printf(" Raid Devices : %d\n", sb->raid_disks);
136 printf(" Total Devices : %d\n", sb->nr_disks);
137 printf("Preferred Minor : %d\n", sb->md_minor);
138 printf("\n");
139 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
140 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
141 if (sb->delta_disks) {
142 printf(" Delta Devices : %d", sb->delta_disks);
143 if (sb->delta_disks)
144 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
145 else
146 printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks);
148 if (sb->new_level != sb->level) {
149 c = map_num(pers, sb->new_level);
150 printf(" New Level : %s\n", c?c:"-unknown-");
152 if (sb->new_layout != sb->layout) {
153 if (sb->level == 5) {
154 c = map_num(r5layout, sb->new_layout);
155 printf(" New Layout : %s\n", c?c:"-unknown-");
157 if (sb->level == 10) {
158 printf(" New Layout : near=%d, %s=%d\n",
159 sb->new_layout&255,
160 (sb->new_layout&0x10000)?"offset":"far",
161 (sb->new_layout>>8)&255);
164 if (sb->new_chunk != sb->chunk_size)
165 printf(" New Chunksize : %d\n", sb->new_chunk);
166 printf("\n");
168 atime = sb->utime;
169 printf(" Update Time : %.24s\n", ctime(&atime));
170 printf(" State : %s\n",
171 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
172 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
173 printf("Internal Bitmap : present\n");
174 printf(" Active Devices : %d\n", sb->active_disks);
175 printf("Working Devices : %d\n", sb->working_disks);
176 printf(" Failed Devices : %d\n", sb->failed_disks);
177 printf(" Spare Devices : %d\n", sb->spare_disks);
178 if (calc_sb0_csum(sb) == sb->sb_csum)
179 printf(" Checksum : %x - correct\n", sb->sb_csum);
180 else
181 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
182 printf(" Events : %d.%d\n", sb->events_hi, sb->events_lo);
183 printf("\n");
184 if (sb->level == 5) {
185 c = map_num(r5layout, sb->layout);
186 printf(" Layout : %s\n", c?c:"-unknown-");
188 if (sb->level == 10) {
189 printf(" Layout : near=%d, %s=%d\n",
190 sb->layout&255,
191 (sb->layout&0x10000)?"offset":"far",
192 (sb->layout>>8)&255);
194 switch(sb->level) {
195 case 0:
196 case 4:
197 case 5:
198 case 6:
199 case 10:
200 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
201 break;
202 case -1:
203 printf(" Rounding : %dK\n", sb->chunk_size/1024);
204 break;
205 default: break;
207 printf("\n");
208 printf(" Number Major Minor RaidDevice State\n");
209 for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
210 mdp_disk_t *dp;
211 char *dv;
212 char nb[5];
213 int wonly;
214 if (d>=0) dp = &sb->disks[d];
215 else dp = &sb->this_disk;
216 snprintf(nb, sizeof(nb), "%4d", d);
217 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
218 dp->number, dp->major, dp->minor, dp->raid_disk);
219 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
220 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
221 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
222 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
223 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
224 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
225 if (wonly) printf(" write-mostly");
226 if (dp->state == 0) printf(" spare");
227 if ((dv=map_dev(dp->major, dp->minor, 0)))
228 printf(" %s", dv);
229 printf("\n");
230 if (d == -1) printf("\n");
234 static void brief_examine_super0(void *sbv)
236 mdp_super_t *sb = sbv;
237 char *c=map_num(pers, sb->level);
238 char devname[20];
240 sprintf(devname, "/dev/md%d", sb->md_minor);
242 printf("ARRAY %s level=%s num-devices=%d UUID=",
243 devname,
244 c?c:"-unknown-", sb->raid_disks);
245 if (sb->minor_version >= 90)
246 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
247 sb->set_uuid2, sb->set_uuid3);
248 else
249 printf("%08x", sb->set_uuid0);
250 printf("\n");
253 static void detail_super0(void *sbv, char *homehost)
255 mdp_super_t *sb = sbv;
256 printf(" UUID : ");
257 if (sb->minor_version >= 90)
258 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
259 sb->set_uuid2, sb->set_uuid3);
260 else
261 printf("%08x", sb->set_uuid0);
262 if (homehost) {
263 char buf[20];
264 void *hash = sha1_buffer(homehost,
265 strlen(homehost),
266 buf);
267 if (memcmp(&sb->set_uuid2, hash, 8)==0)
268 printf(" (local to host %s)", homehost);
270 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
273 static void brief_detail_super0(void *sbv)
275 mdp_super_t *sb = sbv;
276 printf(" UUID=");
277 if (sb->minor_version >= 90)
278 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
279 sb->set_uuid2, sb->set_uuid3);
280 else
281 printf("%08x", sb->set_uuid0);
283 #endif
285 static int match_home0(void *sbv, char *homehost)
287 mdp_super_t *sb = sbv;
288 char buf[20];
289 char *hash = sha1_buffer(homehost,
290 strlen(homehost),
291 buf);
293 return (memcmp(&sb->set_uuid2, hash, 8)==0);
296 static void uuid_from_super0(int uuid[4], void * sbv)
298 mdp_super_t *super = sbv;
299 uuid[0] = super->set_uuid0;
300 if (super->minor_version >= 90) {
301 uuid[1] = super->set_uuid1;
302 uuid[2] = super->set_uuid2;
303 uuid[3] = super->set_uuid3;
304 } else {
305 uuid[1] = 0;
306 uuid[2] = 0;
307 uuid[3] = 0;
311 static void getinfo_super0(struct mdinfo *info, void *sbv)
313 mdp_super_t *sb = sbv;
314 int working = 0;
315 int i;
317 info->array.major_version = sb->major_version;
318 info->array.minor_version = sb->minor_version;
319 info->array.patch_version = sb->patch_version;
320 info->array.raid_disks = sb->raid_disks;
321 info->array.level = sb->level;
322 info->array.layout = sb->layout;
323 info->array.md_minor = sb->md_minor;
324 info->array.ctime = sb->ctime;
325 info->array.utime = sb->utime;
326 info->array.chunk_size = sb->chunk_size;
327 info->array.state = sb->state;
328 info->component_size = sb->size*2;
330 info->disk.state = sb->this_disk.state;
331 info->disk.major = sb->this_disk.major;
332 info->disk.minor = sb->this_disk.minor;
333 info->disk.raid_disk = sb->this_disk.raid_disk;
334 info->disk.number = sb->this_disk.number;
336 info->events = md_event(sb);
337 info->data_offset = 0;
339 uuid_from_super0(info->uuid, sbv);
341 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
342 info->reshape_active = 1;
343 info->reshape_progress = sb->reshape_position;
344 info->new_level = sb->new_level;
345 info->delta_disks = sb->delta_disks;
346 info->new_layout = sb->new_layout;
347 info->new_chunk = sb->new_chunk;
348 } else
349 info->reshape_active = 0;
351 sprintf(info->name, "%d", sb->md_minor);
352 /* work_disks is calculated rather than read directly */
353 for (i=0; i < MD_SB_DISKS; i++)
354 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
355 (sb->disks[i].raid_disk < info->array.raid_disks) &&
356 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
357 !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
358 working ++;
359 info->array.working_disks = working;
363 static int update_super0(struct mdinfo *info, void *sbv, char *update,
364 char *devname, int verbose,
365 int uuid_set, char *homehost)
367 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
368 * For others, the return value is ignored.
370 int rv = 0;
371 mdp_super_t *sb = sbv;
372 if (strcmp(update, "sparc2.2")==0 ) {
373 /* 2.2 sparc put the events in the wrong place
374 * So we copy the tail of the superblock
375 * up 4 bytes before continuing
377 __u32 *sb32 = (__u32*)sb;
378 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
379 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
380 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
381 if (verbose >= 0)
382 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
383 devname);
385 if (strcmp(update, "super-minor") ==0) {
386 sb->md_minor = info->array.md_minor;
387 if (verbose > 0)
388 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
389 devname, info->array.md_minor);
391 if (strcmp(update, "summaries") == 0) {
392 int i;
393 /* set nr_disks, active_disks, working_disks,
394 * failed_disks, spare_disks based on disks[]
395 * array in superblock.
396 * Also make sure extra slots aren't 'failed'
398 sb->nr_disks = sb->active_disks =
399 sb->working_disks = sb->failed_disks =
400 sb->spare_disks = 0;
401 for (i=0; i < MD_SB_DISKS ; i++)
402 if (sb->disks[i].major ||
403 sb->disks[i].minor) {
404 int state = sb->disks[i].state;
405 if (state & (1<<MD_DISK_REMOVED))
406 continue;
407 sb->nr_disks++;
408 if (state & (1<<MD_DISK_ACTIVE))
409 sb->active_disks++;
410 if (state & (1<<MD_DISK_FAULTY))
411 sb->failed_disks++;
412 else
413 sb->working_disks++;
414 if (state == 0)
415 sb->spare_disks++;
416 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
417 sb->disks[i].state = 0;
419 if (strcmp(update, "force-one")==0) {
420 /* Not enough devices for a working array, so
421 * bring this one up-to-date.
423 __u32 ehi = sb->events_hi, elo = sb->events_lo;
424 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
425 sb->events_lo = (info->events) & 0xFFFFFFFF;
426 if (sb->events_hi != ehi ||
427 sb->events_lo != elo)
428 rv = 1;
430 if (strcmp(update, "force-array")==0) {
431 /* degraded array and 'force' requested, so
432 * maybe need to mark it 'clean'
434 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
435 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
436 /* need to force clean */
437 sb->state |= (1 << MD_SB_CLEAN);
438 rv = 1;
441 if (strcmp(update, "assemble")==0) {
442 int d = info->disk.number;
443 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
444 if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
445 != info->disk.state) {
446 sb->disks[d].state = info->disk.state | wonly;
447 rv = 1;
450 if (strcmp(update, "grow") == 0) {
451 sb->raid_disks = info->array.raid_disks;
452 sb->nr_disks = info->array.nr_disks;
453 sb->active_disks = info->array.active_disks;
454 sb->working_disks = info->array.working_disks;
455 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
456 sb->disks[info->disk.number].number = info->disk.number;
457 sb->disks[info->disk.number].major = info->disk.major;
458 sb->disks[info->disk.number].minor = info->disk.minor;
459 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
460 sb->disks[info->disk.number].state = info->disk.state;
461 if (sb->this_disk.number == info->disk.number)
462 sb->this_disk = sb->disks[info->disk.number];
464 if (strcmp(update, "resync") == 0) {
465 /* make sure resync happens */
466 sb->state &= ~(1<<MD_SB_CLEAN);
467 sb->recovery_cp = 0;
469 if (strcmp(update, "homehost") == 0 &&
470 homehost) {
471 uuid_set = 0;
472 update = "uuid";
473 info->uuid[0] = sb->set_uuid0;
474 info->uuid[1] = sb->set_uuid1;
476 if (strcmp(update, "uuid") == 0) {
477 if (!uuid_set && homehost) {
478 char buf[20];
479 char *hash = sha1_buffer(homehost,
480 strlen(homehost),
481 buf);
482 memcpy(info->uuid+2, hash, 8);
484 sb->set_uuid0 = info->uuid[0];
485 sb->set_uuid1 = info->uuid[1];
486 sb->set_uuid2 = info->uuid[2];
487 sb->set_uuid3 = info->uuid[3];
488 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
489 struct bitmap_super_s *bm;
490 bm = (struct bitmap_super_s*)(sb+1);
491 uuid_from_super0((int*)bm->uuid, sbv);
494 if (strcmp(update, "_reshape_progress")==0)
495 sb->reshape_position = info->reshape_progress;
497 sb->sb_csum = calc_sb0_csum(sb);
498 return rv;
502 * For verion-0 superblock, the homehost is 'stored' in the
503 * uuid. 8 bytes for a hash of the host leaving 8 bytes
504 * of random material.
505 * We use the first 8 bytes (64bits) of the sha1 of the
506 * host name
510 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
511 unsigned long long size, char *ignored_name, char *homehost,
512 int *uuid)
514 mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
515 int spares;
516 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
518 if (info->major_version == -1) {
519 /* zeroing the superblock */
520 *sbp = sb;
521 return 0;
524 spares = info->working_disks - info->active_disks;
525 if (info->raid_disks + spares > MD_SB_DISKS) {
526 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
527 info->raid_disks , spares, MD_SB_DISKS);
528 return 0;
531 sb->md_magic = MD_SB_MAGIC;
532 sb->major_version = 0;
533 sb->minor_version = 90;
534 sb->patch_version = 0;
535 sb->gvalid_words = 0; /* ignored */
536 sb->ctime = time(0);
537 sb->level = info->level;
538 if (size != info->size)
539 return 0;
540 sb->size = info->size;
541 sb->nr_disks = info->nr_disks;
542 sb->raid_disks = info->raid_disks;
543 sb->md_minor = info->md_minor;
544 sb->not_persistent = 0;
545 if (uuid) {
546 sb->set_uuid0 = uuid[0];
547 sb->set_uuid1 = uuid[1];
548 sb->set_uuid2 = uuid[2];
549 sb->set_uuid3 = uuid[3];
550 } else {
551 int rfd = open("/dev/urandom", O_RDONLY);
552 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
553 sb->set_uuid0 = random();
554 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
555 sb->set_uuid1 = random();
556 sb->set_uuid2 = random();
557 sb->set_uuid3 = random();
559 if (rfd >= 0)
560 close(rfd);
562 if (homehost) {
563 char buf[20];
564 char *hash = sha1_buffer(homehost,
565 strlen(homehost),
566 buf);
567 memcpy(&sb->set_uuid2, hash, 8);
570 sb->utime = sb->ctime;
571 sb->state = info->state;
572 sb->active_disks = info->active_disks;
573 sb->working_disks = info->working_disks;
574 sb->failed_disks = info->failed_disks;
575 sb->spare_disks = info->spare_disks;
576 sb->events_hi = 0;
577 sb->events_lo = 1;
579 sb->layout = info->layout;
580 sb->chunk_size = info->chunk_size;
582 *sbp = sb;
583 return 1;
586 /* Add a device to the superblock being created */
587 static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
589 mdp_super_t *sb = sbv;
590 mdp_disk_t *dk = &sb->disks[dinfo->number];
592 dk->number = dinfo->number;
593 dk->major = dinfo->major;
594 dk->minor = dinfo->minor;
595 dk->raid_disk = dinfo->raid_disk;
596 dk->state = dinfo->state;
599 static int store_super0(struct supertype *st, int fd, void *sbv)
601 unsigned long long dsize;
602 unsigned long long offset;
603 mdp_super_t *super = sbv;
605 if (!get_dev_size(fd, NULL, &dsize))
606 return 1;
608 if (dsize < MD_RESERVED_SECTORS*2*512)
609 return 2;
611 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
613 offset *= 512;
615 if (lseek64(fd, offset, 0)< 0LL)
616 return 3;
618 if (write(fd, super, sizeof(*super)) != sizeof(*super))
619 return 4;
621 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
622 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
623 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
624 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
625 return 5;
628 fsync(fd);
629 return 0;
632 static int write_init_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
634 mdp_super_t *sb = sbv;
635 int fd = open(devname, O_RDWR|O_EXCL);
636 int rv;
638 if (fd < 0) {
639 fprintf(stderr, Name ": Failed to open %s to write superblock\n", devname);
640 return -1;
643 sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY);
645 sb->this_disk = sb->disks[dinfo->number];
646 sb->sb_csum = calc_sb0_csum(sb);
647 rv = store_super0(st, fd, sb);
649 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
650 rv = st->ss->write_bitmap(st, fd, sbv);
652 close(fd);
653 if (rv)
654 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
655 return rv;
658 static int compare_super0(void **firstp, void *secondv)
661 * return:
662 * 0 same, or first was empty, and second was copied
663 * 1 second had wrong number
664 * 2 wrong uuid
665 * 3 wrong other info
667 mdp_super_t *first = *firstp;
668 mdp_super_t *second = secondv;
670 int uuid1[4], uuid2[4];
671 if (second->md_magic != MD_SB_MAGIC)
672 return 1;
673 if (!first) {
674 first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
675 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
676 *firstp = first;
677 return 0;
680 uuid_from_super0(uuid1, first);
681 uuid_from_super0(uuid2, second);
682 if (!same_uuid(uuid1, uuid2, 0))
683 return 2;
684 if (first->major_version != second->major_version ||
685 first->minor_version != second->minor_version ||
686 first->patch_version != second->patch_version ||
687 first->gvalid_words != second->gvalid_words ||
688 first->ctime != second->ctime ||
689 first->level != second->level ||
690 first->size != second->size ||
691 first->raid_disks != second->raid_disks )
692 return 3;
694 return 0;
698 static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
700 /* try to read in the superblock
701 * Return:
702 * 0 on success
703 * 1 on cannot get superblock
704 * 2 on superblock meaningless
706 unsigned long long dsize;
707 unsigned long long offset;
708 mdp_super_t *super;
709 int uuid[4];
710 struct bitmap_super_s *bsb;
712 if (!get_dev_size(fd, devname, &dsize))
713 return 1;
715 if (dsize < MD_RESERVED_SECTORS*512 * 2) {
716 if (devname)
717 fprintf(stderr, Name
718 ": %s is too small for md: size is %llu sectors.\n",
719 devname, dsize);
720 return 1;
723 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
725 offset *= 512;
727 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
729 if (lseek64(fd, offset, 0)< 0LL) {
730 if (devname)
731 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
732 devname, strerror(errno));
733 return 1;
736 super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
738 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
739 if (devname)
740 fprintf(stderr, Name ": Cannot read superblock on %s\n",
741 devname);
742 free(super);
743 return 1;
746 if (st->ss && st->minor_version == 9)
747 super0_swap_endian(super);
749 if (super->md_magic != MD_SB_MAGIC) {
750 if (devname)
751 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
752 devname, MD_SB_MAGIC, super->md_magic);
753 free(super);
754 return 2;
757 if (super->major_version != 0) {
758 if (devname)
759 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
760 devname, super->major_version);
761 free(super);
762 return 2;
764 *sbp = super;
765 if (st->ss == NULL) {
766 st->ss = &super0;
767 st->minor_version = 90;
768 st->max_devs = MD_SB_DISKS;
771 /* Now check on the bitmap superblock */
772 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
773 return 0;
774 /* Read the bitmap superblock and make sure it looks
775 * valid. If it doesn't clear the bit. An --assemble --force
776 * should get that written out.
778 if (read(fd, super+1, sizeof(struct bitmap_super_s))
779 != sizeof(struct bitmap_super_s))
780 goto no_bitmap;
782 uuid_from_super0(uuid, super);
783 bsb = (struct bitmap_super_s *)(super+1);
784 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
785 memcmp(bsb->uuid, uuid, 16) != 0)
786 goto no_bitmap;
787 return 0;
789 no_bitmap:
790 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
792 return 0;
795 static struct supertype *match_metadata_desc0(char *arg)
797 struct supertype *st = malloc(sizeof(*st));
798 if (!st) return st;
800 st->ss = &super0;
801 st->minor_version = 90;
802 st->max_devs = MD_SB_DISKS;
803 if (strcmp(arg, "0") == 0 ||
804 strcmp(arg, "0.90") == 0 ||
805 strcmp(arg, "default") == 0
807 return st;
809 st->minor_version = 9; /* flag for 'byte-swapped' */
810 if (strcmp(arg, "0.swap")==0)
811 return st;
813 free(st);
814 return NULL;
817 static __u64 avail_size0(struct supertype *st, __u64 devsize)
819 if (devsize < MD_RESERVED_SECTORS*2)
820 return 0ULL;
821 return MD_NEW_SIZE_SECTORS(devsize);
824 static int add_internal_bitmap0(struct supertype *st, void *sbv, int *chunkp,
825 int delay, int write_behind,
826 unsigned long long size, int may_change,
827 int major)
830 * The bitmap comes immediately after the superblock and must be 60K in size
831 * at most. The default size is between 30K and 60K
833 * size is in sectors, chunk is in bytes !!!
835 unsigned long long bits;
836 unsigned long long max_bits = 60*1024*8;
837 unsigned long long min_chunk;
838 int chunk = *chunkp;
839 mdp_super_t *sb = sbv;
840 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
843 min_chunk = 4096; /* sub-page chunks don't work yet.. */
844 bits = (size * 512) / min_chunk + 1;
845 while (bits > max_bits) {
846 min_chunk *= 2;
847 bits = (bits+1)/2;
849 if (chunk == UnSet)
850 chunk = min_chunk;
851 else if (chunk < min_chunk)
852 return 0; /* chunk size too small */
854 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
856 memset(bms, 0, sizeof(*bms));
857 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
858 bms->version = __cpu_to_le32(major);
859 uuid_from_super0((int*)bms->uuid, sb);
860 bms->chunksize = __cpu_to_le32(chunk);
861 bms->daemon_sleep = __cpu_to_le32(delay);
862 bms->sync_size = __cpu_to_le64(size);
863 bms->write_behind = __cpu_to_le32(write_behind);
864 *chunkp = chunk;
865 return 1;
869 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
871 unsigned long long dsize;
872 unsigned long long offset;
874 if (!get_dev_size(fd, NULL, &dsize))
875 return;
877 if (dsize < MD_RESERVED_SECTORS*512 * 2)
878 return;
880 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
882 offset *= 512;
884 offset += MD_SB_BYTES;
886 lseek64(fd, offset, 0);
889 int write_bitmap0(struct supertype *st, int fd, void *sbv)
891 unsigned long long dsize;
892 unsigned long long offset;
893 mdp_super_t *sb = sbv;
895 int rv = 0;
897 int towrite, n;
898 char buf[4096];
900 if (!get_dev_size(fd, NULL, &dsize))
901 return 1;
904 if (dsize < MD_RESERVED_SECTORS*512 * 2)
905 return -1;
907 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
909 offset *= 512;
911 if (lseek64(fd, offset + 4096, 0)< 0LL)
912 return 3;
915 if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
916 sizeof(bitmap_super_t))
917 return -2;
918 towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
919 memset(buf, 0xff, sizeof(buf));
920 while (towrite > 0) {
921 n = towrite;
922 if (n > sizeof(buf))
923 n = sizeof(buf);
924 n = write(fd, buf, n);
925 if (n > 0)
926 towrite -= n;
927 else
928 break;
930 fsync(fd);
931 if (towrite)
932 rv = -2;
934 return rv;
937 struct superswitch super0 = {
938 #ifndef MDASSEMBLE
939 .examine_super = examine_super0,
940 .brief_examine_super = brief_examine_super0,
941 .detail_super = detail_super0,
942 .brief_detail_super = brief_detail_super0,
943 #endif
944 .match_home = match_home0,
945 .uuid_from_super = uuid_from_super0,
946 .getinfo_super = getinfo_super0,
947 .update_super = update_super0,
948 .init_super = init_super0,
949 .add_to_super = add_to_super0,
950 .store_super = store_super0,
951 .write_init_super = write_init_super0,
952 .compare_super = compare_super0,
953 .load_super = load_super0,
954 .match_metadata_desc = match_metadata_desc0,
955 .avail_size = avail_size0,
956 .add_internal_bitmap = add_internal_bitmap0,
957 .locate_bitmap = locate_bitmap0,
958 .write_bitmap = write_bitmap0,
959 .major = 0,
960 .swapuuid = 0,