]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super0.c
Choose better devnumbers and tidy up some issues with finding names.
[thirdparty/mdadm.git] / super0.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 #include <openssl/sha.h> /* for SHA1 */
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, char *homehost)
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", sb->set_uuid0, sb->set_uuid1,
99 sb->set_uuid2, sb->set_uuid3);
100 if (homehost) {
101 unsigned char *hash = SHA1((unsigned char *)homehost,
102 strlen(homehost),
103 NULL);
104 if (memcmp(&sb->set_uuid2, hash, 8)==0)
105 printf(" (local to host %s)", homehost);
106 }
107 printf("\n");
108 } else
109 printf(" UUID : %08x\n", sb->set_uuid0);
110
111 atime = sb->ctime;
112 printf(" Creation Time : %.24s\n", ctime(&atime));
113 c=map_num(pers, sb->level);
114 printf(" Raid Level : %s\n", c?c:"-unknown-");
115 if ((int)sb->level >= 0) {
116 int ddsks=0;
117 printf(" Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
118 switch(sb->level) {
119 case 1: ddsks=1;break;
120 case 4:
121 case 5: ddsks = sb->raid_disks-1; break;
122 case 6: ddsks = sb->raid_disks-2; break;
123 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
124 }
125 if (ddsks)
126 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
127 human_size(ddsks*(long long)sb->size<<10));
128 }
129 printf(" Raid Devices : %d\n", sb->raid_disks);
130 printf(" Total Devices : %d\n", sb->nr_disks);
131 printf("Preferred Minor : %d\n", sb->md_minor);
132 printf("\n");
133 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
134 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
135 if (sb->delta_disks) {
136 printf(" Delta Devices : %d", sb->delta_disks);
137 if (sb->delta_disks)
138 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
139 else
140 printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks);
141 }
142 if (sb->new_level != sb->level) {
143 c = map_num(pers, sb->new_level);
144 printf(" New Level : %s\n", c?c:"-unknown-");
145 }
146 if (sb->new_layout != sb->layout) {
147 if (sb->level == 5) {
148 c = map_num(r5layout, sb->new_layout);
149 printf(" New Layout : %s\n", c?c:"-unknown-");
150 }
151 if (sb->level == 10) {
152 printf(" New Layout : near=%d, %s=%d\n",
153 sb->new_layout&255,
154 (sb->new_layout&0x10000)?"offset":"far",
155 (sb->new_layout>>8)&255);
156 }
157 }
158 if (sb->new_chunk != sb->chunk_size)
159 printf(" New Chunksize : %d\n", sb->new_chunk);
160 printf("\n");
161 }
162 atime = sb->utime;
163 printf(" Update Time : %.24s\n", ctime(&atime));
164 printf(" State : %s\n",
165 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
166 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
167 printf("Internal Bitmap : present\n");
168 printf(" Active Devices : %d\n", sb->active_disks);
169 printf("Working Devices : %d\n", sb->working_disks);
170 printf(" Failed Devices : %d\n", sb->failed_disks);
171 printf(" Spare Devices : %d\n", sb->spare_disks);
172 if (calc_sb0_csum(sb) == sb->sb_csum)
173 printf(" Checksum : %x - correct\n", sb->sb_csum);
174 else
175 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
176 printf(" Events : %d.%d\n", sb->events_hi, sb->events_lo);
177 printf("\n");
178 if (sb->level == 5) {
179 c = map_num(r5layout, sb->layout);
180 printf(" Layout : %s\n", c?c:"-unknown-");
181 }
182 if (sb->level == 10) {
183 printf(" Layout : near=%d, %s=%d\n",
184 sb->layout&255,
185 (sb->layout&0x10000)?"offset":"far",
186 (sb->layout>>8)&255);
187 }
188 switch(sb->level) {
189 case 0:
190 case 4:
191 case 5:
192 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
193 break;
194 case -1:
195 printf(" Rounding : %dK\n", sb->chunk_size/1024);
196 break;
197 default: break;
198 }
199 printf("\n");
200 printf(" Number Major Minor RaidDevice State\n");
201 for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
202 mdp_disk_t *dp;
203 char *dv;
204 char nb[5];
205 int wonly;
206 if (d>=0) dp = &sb->disks[d];
207 else dp = &sb->this_disk;
208 snprintf(nb, sizeof(nb), "%4d", d);
209 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
210 dp->number, dp->major, dp->minor, dp->raid_disk);
211 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
212 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
213 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
214 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
215 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
216 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
217 if (wonly) printf(" write-mostly");
218 if (dp->state == 0) printf(" spare");
219 if ((dv=map_dev(dp->major, dp->minor, 0)))
220 printf(" %s", dv);
221 printf("\n");
222 if (d == -1) printf("\n");
223 }
224 }
225
226 static void brief_examine_super0(void *sbv)
227 {
228 mdp_super_t *sb = sbv;
229 char *c=map_num(pers, sb->level);
230 char *nm;
231
232 printf("ARRAY %s level=%s num-devices=%d UUID=",
233 nm = get_md_name(sb->md_minor),
234 c?c:"-unknown-", sb->raid_disks);
235 if (sb->minor_version >= 90)
236 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
237 sb->set_uuid2, sb->set_uuid3);
238 else
239 printf("%08x", sb->set_uuid0);
240 printf("\n");
241 put_md_name(nm);
242 }
243
244 static void detail_super0(void *sbv, char *homehost)
245 {
246 mdp_super_t *sb = sbv;
247 printf(" UUID : ");
248 if (sb->minor_version >= 90)
249 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
250 sb->set_uuid2, sb->set_uuid3);
251 else
252 printf("%08x", sb->set_uuid0);
253 if (homehost) {
254 unsigned char *hash = SHA1((unsigned char *)homehost,
255 strlen(homehost),
256 NULL);
257 if (memcmp(&sb->set_uuid2, hash, 8)==0)
258 printf(" (local to host %s)", homehost);
259 }
260 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
261 }
262
263 static void brief_detail_super0(void *sbv)
264 {
265 mdp_super_t *sb = sbv;
266 printf(" UUID=");
267 if (sb->minor_version >= 90)
268 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
269 sb->set_uuid2, sb->set_uuid3);
270 else
271 printf("%08x", sb->set_uuid0);
272 }
273 #endif
274
275 static int match_home0(void *sbv, char *homehost)
276 {
277 mdp_super_t *sb = sbv;
278 unsigned char *hash = SHA1((unsigned char *)homehost,
279 strlen(homehost),
280 NULL);
281
282 return (memcmp(&sb->set_uuid2, hash, 8)==0);
283 }
284
285 static void uuid_from_super0(int uuid[4], void * sbv)
286 {
287 mdp_super_t *super = sbv;
288 uuid[0] = super->set_uuid0;
289 if (super->minor_version >= 90) {
290 uuid[1] = super->set_uuid1;
291 uuid[2] = super->set_uuid2;
292 uuid[3] = super->set_uuid3;
293 } else {
294 uuid[1] = 0;
295 uuid[2] = 0;
296 uuid[3] = 0;
297 }
298 }
299
300 static void getinfo_super0(struct mdinfo *info, void *sbv)
301 {
302 mdp_super_t *sb = sbv;
303 int working = 0;
304 int i;
305
306 info->array.major_version = sb->major_version;
307 info->array.minor_version = sb->minor_version;
308 info->array.patch_version = sb->patch_version;
309 info->array.raid_disks = sb->raid_disks;
310 info->array.level = sb->level;
311 info->array.layout = sb->layout;
312 info->array.md_minor = sb->md_minor;
313 info->array.ctime = sb->ctime;
314 info->array.utime = sb->utime;
315 info->array.chunk_size = sb->chunk_size;
316 info->component_size = sb->size*2;
317
318 info->disk.state = sb->this_disk.state;
319 info->disk.major = sb->this_disk.major;
320 info->disk.minor = sb->this_disk.minor;
321 info->disk.raid_disk = sb->this_disk.raid_disk;
322 info->disk.number = sb->this_disk.number;
323
324 info->events = md_event(sb);
325 info->data_offset = 0;
326
327 uuid_from_super0(info->uuid, sbv);
328
329 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
330 info->reshape_active = 1;
331 info->reshape_progress = sb->reshape_position;
332 info->new_level = sb->new_level;
333 info->delta_disks = sb->delta_disks;
334 info->new_layout = sb->new_layout;
335 info->new_chunk = sb->new_chunk;
336 } else
337 info->reshape_active = 0;
338
339 sprintf(info->name, "%d", sb->md_minor);
340 /* work_disks is calculated rather than read directly */
341 for (i=0; i < MD_SB_DISKS; i++)
342 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
343 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
344 !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
345 working ++;
346 info->array.working_disks = working;
347 }
348
349
350 static int update_super0(struct mdinfo *info, void *sbv, char *update,
351 char *devname, int verbose,
352 int uuid_set, char *homehost)
353 {
354 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
355 * For others, the return value is ignored.
356 */
357 int rv = 0;
358 mdp_super_t *sb = sbv;
359 if (strcmp(update, "sparc2.2")==0 ) {
360 /* 2.2 sparc put the events in the wrong place
361 * So we copy the tail of the superblock
362 * up 4 bytes before continuing
363 */
364 __u32 *sb32 = (__u32*)sb;
365 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
366 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
367 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
368 if (verbose >= 0)
369 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
370 devname);
371 }
372 if (strcmp(update, "super-minor") ==0) {
373 sb->md_minor = info->array.md_minor;
374 if (verbose > 0)
375 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
376 devname, info->array.md_minor);
377 }
378 if (strcmp(update, "summaries") == 0) {
379 int i;
380 /* set nr_disks, active_disks, working_disks,
381 * failed_disks, spare_disks based on disks[]
382 * array in superblock.
383 * Also make sure extra slots aren't 'failed'
384 */
385 sb->nr_disks = sb->active_disks =
386 sb->working_disks = sb->failed_disks =
387 sb->spare_disks = 0;
388 for (i=0; i < MD_SB_DISKS ; i++)
389 if (sb->disks[i].major ||
390 sb->disks[i].minor) {
391 int state = sb->disks[i].state;
392 if (state & (1<<MD_DISK_REMOVED))
393 continue;
394 sb->nr_disks++;
395 if (state & (1<<MD_DISK_ACTIVE))
396 sb->active_disks++;
397 if (state & (1<<MD_DISK_FAULTY))
398 sb->failed_disks++;
399 else
400 sb->working_disks++;
401 if (state == 0)
402 sb->spare_disks++;
403 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
404 sb->disks[i].state = 0;
405 }
406 if (strcmp(update, "force")==0) {
407 __u32 ehi = sb->events_hi, elo = sb->events_lo;
408 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
409 sb->events_lo = (info->events) & 0xFFFFFFFF;
410 if (sb->events_hi != ehi ||
411 sb->events_lo != elo)
412 rv = 1;
413 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
414 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
415 /* need to force clean */
416 sb->state |= (1 << MD_SB_CLEAN);
417 rv = 1;
418 }
419 }
420 if (strcmp(update, "assemble")==0) {
421 int d = info->disk.number;
422 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
423 if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
424 != info->disk.state) {
425 sb->disks[d].state = info->disk.state | wonly;
426 rv = 1;
427 }
428 }
429 if (strcmp(update, "newdev") == 0) {
430 int d = info->disk.number;
431 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
432 sb->disks[d].number = d;
433 sb->disks[d].major = info->disk.major;
434 sb->disks[d].minor = info->disk.minor;
435 sb->disks[d].raid_disk = info->disk.raid_disk;
436 sb->disks[d].state = info->disk.state;
437 sb->this_disk = sb->disks[d];
438 }
439 if (strcmp(update, "grow") == 0) {
440 sb->raid_disks = info->array.raid_disks;
441 sb->nr_disks = info->array.nr_disks;
442 sb->active_disks = info->array.active_disks;
443 sb->working_disks = info->array.working_disks;
444 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
445 sb->disks[info->disk.number].number = info->disk.number;
446 sb->disks[info->disk.number].major = info->disk.major;
447 sb->disks[info->disk.number].minor = info->disk.minor;
448 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
449 sb->disks[info->disk.number].state = info->disk.state;
450 if (sb->this_disk.number == info->disk.number)
451 sb->this_disk = sb->disks[info->disk.number];
452 }
453 if (strcmp(update, "resync") == 0) {
454 /* make sure resync happens */
455 sb->state &= ~(1<<MD_SB_CLEAN);
456 sb->recovery_cp = 0;
457 }
458 if (strcmp(update, "homehost") == 0 &&
459 homehost) {
460 uuid_set = 0;
461 update = "uuid";
462 info->uuid[0] = sb->set_uuid0;
463 info->uuid[1] = sb->set_uuid1;
464 }
465 if (strcmp(update, "uuid") == 0) {
466 if (!uuid_set && homehost) {
467 unsigned char *hash = SHA1((unsigned char*)homehost,
468 strlen(homehost),
469 NULL);
470 memcpy(info->uuid+2, hash, 8);
471 }
472 sb->set_uuid0 = info->uuid[0];
473 sb->set_uuid1 = info->uuid[1];
474 sb->set_uuid2 = info->uuid[2];
475 sb->set_uuid3 = info->uuid[3];
476 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
477 struct bitmap_super_s *bm;
478 bm = (struct bitmap_super_s*)(sb+1);
479 uuid_from_super0((int*)bm->uuid, sbv);
480 }
481 }
482 if (strcmp(update, "_reshape_progress")==0)
483 sb->reshape_position = info->reshape_progress;
484
485 sb->sb_csum = calc_sb0_csum(sb);
486 return rv;
487 }
488
489 static __u64 event_super0(void *sbv)
490 {
491 mdp_super_t *sb = sbv;
492 return md_event(sb);
493 }
494
495 /*
496 * For verion-0 superblock, the homehost is 'stored' in the
497 * uuid. 8 bytes for a hash of the host leaving 8 bytes
498 * of random material.
499 * We use the first 8 bytes (64bits) of the sha1 of the
500 * host name
501 */
502
503
504 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
505 unsigned long long size, char *ignored_name, char *homehost)
506 {
507 mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
508 int spares;
509 int rfd;
510 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
511
512 if (info->major_version == -1) {
513 /* zeroing the superblock */
514 *sbp = sb;
515 return 0;
516 }
517
518 spares = info->working_disks - info->active_disks;
519 if (info->raid_disks + spares > MD_SB_DISKS) {
520 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
521 info->raid_disks , spares, MD_SB_DISKS);
522 return 0;
523 }
524
525 rfd = open("/dev/urandom", O_RDONLY);
526 sb->md_magic = MD_SB_MAGIC;
527 sb->major_version = 0;
528 sb->minor_version = 90;
529 sb->patch_version = 0;
530 sb->gvalid_words = 0; /* ignored */
531 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
532 sb->set_uuid0 = random();
533 sb->ctime = time(0);
534 sb->level = info->level;
535 if (size != info->size)
536 return 0;
537 sb->size = info->size;
538 sb->nr_disks = info->nr_disks;
539 sb->raid_disks = info->raid_disks;
540 sb->md_minor = info->md_minor;
541 sb->not_persistent = 0;
542 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
543 sb->set_uuid1 = random();
544 sb->set_uuid2 = random();
545 sb->set_uuid3 = random();
546 }
547 if (rfd >= 0)
548 close(rfd);
549 if (homehost) {
550 unsigned char *hash = SHA1((unsigned char*)homehost,
551 strlen(homehost),
552 NULL);
553 memcpy(&sb->set_uuid2, hash, 8);
554 }
555
556 sb->utime = sb->ctime;
557 sb->state = info->state;
558 sb->active_disks = info->active_disks;
559 sb->working_disks = info->working_disks;
560 sb->failed_disks = info->failed_disks;
561 sb->spare_disks = info->spare_disks;
562 sb->events_hi = 0;
563 sb->events_lo = 1;
564
565 sb->layout = info->layout;
566 sb->chunk_size = info->chunk_size;
567
568 *sbp = sb;
569 return 1;
570 }
571
572 /* Add a device to the superblock being created */
573 static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
574 {
575 mdp_super_t *sb = sbv;
576 mdp_disk_t *dk = &sb->disks[dinfo->number];
577
578 dk->number = dinfo->number;
579 dk->major = dinfo->major;
580 dk->minor = dinfo->minor;
581 dk->raid_disk = dinfo->raid_disk;
582 dk->state = dinfo->state;
583 }
584
585 static int store_super0(struct supertype *st, int fd, void *sbv)
586 {
587 unsigned long size;
588 unsigned long long dsize;
589 unsigned long long offset;
590 mdp_super_t *super = sbv;
591
592 #ifdef BLKGETSIZE64
593 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
594 #endif
595 {
596 if (ioctl(fd, BLKGETSIZE, &size))
597 return 1;
598 else
599 dsize = ((unsigned long long)size)<<9;
600 }
601
602 if (dsize < MD_RESERVED_SECTORS*2*512)
603 return 2;
604
605 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
606
607 offset *= 512;
608
609 if (lseek64(fd, offset, 0)< 0LL)
610 return 3;
611
612 if (write(fd, super, sizeof(*super)) != sizeof(*super))
613 return 4;
614
615 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
616 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
617 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
618 write(fd, bm, sizeof(*bm));
619 }
620
621 fsync(fd);
622 return 0;
623 }
624
625 static int write_init_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
626 {
627 mdp_super_t *sb = sbv;
628 int fd = open(devname, O_RDWR|O_EXCL);
629 int rv;
630
631 if (fd < 0) {
632 fprintf(stderr, Name ": Failed to open %s to write superblock\n", devname);
633 return -1;
634 }
635
636 sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY);
637 sb->disks[dinfo->number].state |= (1<<MD_DISK_SYNC);
638
639 sb->this_disk = sb->disks[dinfo->number];
640 sb->sb_csum = calc_sb0_csum(sb);
641 rv = store_super0(st, fd, sb);
642
643 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
644 rv = st->ss->write_bitmap(st, fd, sbv);
645
646 close(fd);
647 if (rv)
648 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
649 return rv;
650 }
651
652 static int compare_super0(void **firstp, void *secondv)
653 {
654 /*
655 * return:
656 * 0 same, or first was empty, and second was copied
657 * 1 second had wrong number
658 * 2 wrong uuid
659 * 3 wrong other info
660 */
661 mdp_super_t *first = *firstp;
662 mdp_super_t *second = secondv;
663
664 int uuid1[4], uuid2[4];
665 if (second->md_magic != MD_SB_MAGIC)
666 return 1;
667 if (!first) {
668 first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
669 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
670 *firstp = first;
671 return 0;
672 }
673
674 uuid_from_super0(uuid1, first);
675 uuid_from_super0(uuid2, second);
676 if (!same_uuid(uuid1, uuid2, 0))
677 return 2;
678 if (first->major_version != second->major_version ||
679 first->minor_version != second->minor_version ||
680 first->patch_version != second->patch_version ||
681 first->gvalid_words != second->gvalid_words ||
682 first->ctime != second->ctime ||
683 first->level != second->level ||
684 first->size != second->size ||
685 first->raid_disks != second->raid_disks )
686 return 3;
687
688 return 0;
689 }
690
691
692 static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
693 {
694 /* try to read in the superblock
695 * Return:
696 * 0 on success
697 * 1 on cannot get superblock
698 * 2 on superblock meaningless
699 */
700 unsigned long size;
701 unsigned long long dsize;
702 unsigned long long offset;
703 mdp_super_t *super;
704 int uuid[4];
705 struct bitmap_super_s *bsb;
706
707 #ifdef BLKGETSIZE64
708 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
709 #endif
710 {
711 if (ioctl(fd, BLKGETSIZE, &size)) {
712 if (devname)
713 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
714 devname, strerror(errno));
715 return 1;
716 } else
717 dsize = size << 9;
718 }
719
720 if (dsize < MD_RESERVED_SECTORS*2) {
721 if (devname)
722 fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
723 devname, size);
724 return 1;
725 }
726
727 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
728
729 offset *= 512;
730
731 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
732
733 if (lseek64(fd, offset, 0)< 0LL) {
734 if (devname)
735 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
736 devname, strerror(errno));
737 return 1;
738 }
739
740 super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
741
742 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
743 if (devname)
744 fprintf(stderr, Name ": Cannot read superblock on %s\n",
745 devname);
746 free(super);
747 return 1;
748 }
749
750 if (st->ss && st->minor_version == 9)
751 super0_swap_endian(super);
752
753 if (super->md_magic != MD_SB_MAGIC) {
754 if (devname)
755 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
756 devname, MD_SB_MAGIC, super->md_magic);
757 free(super);
758 return 2;
759 }
760
761 if (super->major_version != 0) {
762 if (devname)
763 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
764 devname, super->major_version);
765 free(super);
766 return 2;
767 }
768 *sbp = super;
769 if (st->ss == NULL) {
770 st->ss = &super0;
771 st->minor_version = 90;
772 st->max_devs = MD_SB_DISKS;
773 }
774
775 /* Now check on the bitmap superblock */
776 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
777 return 0;
778 /* Read the bitmap superblock and make sure it looks
779 * valid. If it doesn't clear the bit. An --assemble --force
780 * should get that written out.
781 */
782 if (read(fd, super+1, sizeof(struct bitmap_super_s))
783 != sizeof(struct bitmap_super_s))
784 goto no_bitmap;
785
786 uuid_from_super0(uuid, super);
787 bsb = (struct bitmap_super_s *)(super+1);
788 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
789 memcmp(bsb->uuid, uuid, 16) != 0)
790 goto no_bitmap;
791 return 0;
792
793 no_bitmap:
794 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
795
796 return 0;
797 }
798
799 static struct supertype *match_metadata_desc0(char *arg)
800 {
801 struct supertype *st = malloc(sizeof(*st));
802 if (!st) return st;
803
804 st->ss = &super0;
805 st->minor_version = 90;
806 st->max_devs = MD_SB_DISKS;
807 if (strcmp(arg, "0") == 0 ||
808 strcmp(arg, "0.90") == 0 ||
809 strcmp(arg, "default") == 0
810 )
811 return st;
812
813 st->minor_version = 9; /* flag for 'byte-swapped' */
814 if (strcmp(arg, "0.swap")==0)
815 return st;
816
817 free(st);
818 return NULL;
819 }
820
821 static __u64 avail_size0(struct supertype *st, __u64 devsize)
822 {
823 if (devsize < MD_RESERVED_SECTORS*2)
824 return 0ULL;
825 return MD_NEW_SIZE_SECTORS(devsize);
826 }
827
828 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)
829 {
830 /*
831 * The bitmap comes immediately after the superblock and must be 60K in size
832 * at most. The default size is between 30K and 60K
833 *
834 * size is in sectors, chunk is in bytes !!!
835 */
836 unsigned long long bits;
837 unsigned long long max_bits = 60*1024*8;
838 unsigned long long min_chunk;
839 mdp_super_t *sb = sbv;
840 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
841
842
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;
848 }
849 if (chunk == UnSet)
850 chunk = min_chunk;
851 else if (chunk < min_chunk)
852 return 0; /* chunk size too small */
853
854 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
855
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
865 return 1;
866 }
867
868
869 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
870 {
871 unsigned long long dsize;
872 unsigned long size;
873 unsigned long long offset;
874 #ifdef BLKGETSIZE64
875 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
876 #endif
877 {
878 if (ioctl(fd, BLKGETSIZE, &size))
879 return;
880 else
881 dsize = ((unsigned long long)size)<<9;
882 }
883
884 if (dsize < MD_RESERVED_SECTORS*2)
885 return;
886
887 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
888
889 offset *= 512;
890
891 offset += MD_SB_BYTES;
892
893 lseek64(fd, offset, 0);
894 }
895
896 int write_bitmap0(struct supertype *st, int fd, void *sbv)
897 {
898 unsigned long size;
899 unsigned long long dsize;
900 unsigned long long offset;
901 mdp_super_t *sb = sbv;
902
903 int rv = 0;
904
905 int towrite, n;
906 char buf[4096];
907
908 #ifdef BLKGETSIZE64
909 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
910 #endif
911 {
912 if (ioctl(fd, BLKGETSIZE, &size))
913 return 1;
914 else
915 dsize = ((unsigned long long)size)<<9;
916 }
917
918 if (dsize < MD_RESERVED_SECTORS*2)
919 return -1;
920
921 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
922
923 offset *= 512;
924
925 if (lseek64(fd, offset + 4096, 0)< 0LL)
926 return 3;
927
928
929 if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
930 sizeof(bitmap_super_t))
931 return -2;
932 towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
933 memset(buf, 0xff, sizeof(buf));
934 while (towrite > 0) {
935 n = towrite;
936 if (n > sizeof(buf))
937 n = sizeof(buf);
938 n = write(fd, buf, n);
939 if (n > 0)
940 towrite -= n;
941 else
942 break;
943 }
944 fsync(fd);
945 if (towrite)
946 rv = -2;
947
948 return rv;
949 }
950
951 struct superswitch super0 = {
952 #ifndef MDASSEMBLE
953 .examine_super = examine_super0,
954 .brief_examine_super = brief_examine_super0,
955 .detail_super = detail_super0,
956 .brief_detail_super = brief_detail_super0,
957 #endif
958 .match_home = match_home0,
959 .uuid_from_super = uuid_from_super0,
960 .getinfo_super = getinfo_super0,
961 .update_super = update_super0,
962 .event_super = event_super0,
963 .init_super = init_super0,
964 .add_to_super = add_to_super0,
965 .store_super = store_super0,
966 .write_init_super = write_init_super0,
967 .compare_super = compare_super0,
968 .load_super = load_super0,
969 .match_metadata_desc = match_metadata_desc0,
970 .avail_size = avail_size0,
971 .add_internal_bitmap = add_internal_bitmap0,
972 .locate_bitmap = locate_bitmap0,
973 .write_bitmap = write_bitmap0,
974 .major = 0,
975 .swapuuid = 0,
976 };