]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super0.c
Improve partition table code.
[thirdparty/mdadm.git] / super0.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 */
24
25 #define HAVE_STDINT_H 1
26 #include "mdadm.h"
27 #include "sha1.h"
28 /*
29 * All handling for the 0.90.0 version superblock is in
30 * this file.
31 * This includes:
32 * - finding, loading, and writing the superblock.
33 * - initialising a new superblock
34 * - printing the superblock for --examine
35 * - printing part of the superblock for --detail
36 * .. other stuff
37 */
38
39
40 static unsigned long calc_sb0_csum(mdp_super_t *super)
41 {
42 unsigned long csum = super->sb_csum;
43 unsigned long newcsum;
44 super->sb_csum= 0 ;
45 newcsum = calc_csum(super, MD_SB_BYTES);
46 super->sb_csum = csum;
47 return newcsum;
48 }
49
50
51 static void super0_swap_endian(struct mdp_superblock_s *sb)
52 {
53 /* as super0 superblocks are host-endian, it is sometimes
54 * useful to be able to swap the endianness
55 * as (almost) everything is u32's we byte-swap every 4byte
56 * number.
57 * We then also have to swap the events_hi and events_lo
58 */
59 char *sbc = (char *)sb;
60 __u32 t32;
61 int i;
62
63 for (i=0; i < MD_SB_BYTES ; i+=4) {
64 char t = sbc[i];
65 sbc[i] = sbc[i+3];
66 sbc[i+3] = t;
67 t=sbc[i+1];
68 sbc[i+1]=sbc[i+2];
69 sbc[i+2]=t;
70 }
71 t32 = sb->events_hi;
72 sb->events_hi = sb->events_lo;
73 sb->events_lo = t32;
74
75 t32 = sb->cp_events_hi;
76 sb->cp_events_hi = sb->cp_events_lo;
77 sb->cp_events_lo = t32;
78
79 }
80
81 #ifndef MDASSEMBLE
82
83 static void examine_super0(struct supertype *st, char *homehost)
84 {
85 mdp_super_t *sb = st->sb;
86 time_t atime;
87 int d;
88 int delta_extra = 0;
89 char *c;
90
91 printf(" Magic : %08x\n", sb->md_magic);
92 printf(" Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version,
93 sb->patch_version);
94 if (sb->minor_version >= 90) {
95 printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
96 sb->set_uuid2, sb->set_uuid3);
97 if (homehost) {
98 char buf[20];
99 void *hash = sha1_buffer(homehost,
100 strlen(homehost),
101 buf);
102 if (memcmp(&sb->set_uuid2, hash, 8)==0)
103 printf(" (local to host %s)", homehost);
104 }
105 printf("\n");
106 } else
107 printf(" UUID : %08x\n", sb->set_uuid0);
108
109 if (sb->not_persistent)
110 printf(" Eedk : not persistent\n");
111
112 atime = sb->ctime;
113 printf(" Creation Time : %.24s\n", ctime(&atime));
114 c=map_num(pers, sb->level);
115 printf(" Raid Level : %s\n", c?c:"-unknown-");
116 if ((int)sb->level >= 0) {
117 int ddsks=0;
118 printf(" Used Dev Size : %d%s\n", sb->size,
119 human_size((long long)sb->size<<10));
120 switch(sb->level) {
121 case 1: ddsks=1;break;
122 case 4:
123 case 5: ddsks = sb->raid_disks-1; break;
124 case 6: ddsks = sb->raid_disks-2; break;
125 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
126 }
127 if (ddsks)
128 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
129 human_size(ddsks*(long long)sb->size<<10));
130 }
131 printf(" Raid Devices : %d\n", sb->raid_disks);
132 printf(" Total Devices : %d\n", sb->nr_disks);
133 printf("Preferred Minor : %d\n", sb->md_minor);
134 printf("\n");
135 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
136 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
137 if (sb->delta_disks) {
138 printf(" Delta Devices : %d", sb->delta_disks);
139 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
140 if (((int)sb->delta_disks) < 0)
141 delta_extra = - sb->delta_disks;
142 }
143 if (sb->new_level != sb->level) {
144 c = map_num(pers, sb->new_level);
145 printf(" New Level : %s\n", c?c:"-unknown-");
146 }
147 if (sb->new_layout != sb->layout) {
148 if (sb->level == 5) {
149 c = map_num(r5layout, sb->new_layout);
150 printf(" New Layout : %s\n", c?c:"-unknown-");
151 }
152 if (sb->level == 6) {
153 c = map_num(r6layout, sb->new_layout);
154 printf(" New Layout : %s\n", c?c:"-unknown-");
155 }
156 if (sb->level == 10) {
157 printf(" New Layout : near=%d, %s=%d\n",
158 sb->new_layout&255,
159 (sb->new_layout&0x10000)?"offset":"far",
160 (sb->new_layout>>8)&255);
161 }
162 }
163 if (sb->new_chunk != sb->chunk_size)
164 printf(" New Chunksize : %d\n", sb->new_chunk);
165 printf("\n");
166 }
167 atime = sb->utime;
168 printf(" Update Time : %.24s\n", ctime(&atime));
169 printf(" State : %s\n",
170 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
171 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
172 printf("Internal Bitmap : present\n");
173 printf(" Active Devices : %d\n", sb->active_disks);
174 printf("Working Devices : %d\n", sb->working_disks);
175 printf(" Failed Devices : %d\n", sb->failed_disks);
176 printf(" Spare Devices : %d\n", sb->spare_disks);
177 if (calc_sb0_csum(sb) == sb->sb_csum)
178 printf(" Checksum : %x - correct\n", sb->sb_csum);
179 else
180 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
181 printf(" Events : %llu\n",
182 ((unsigned long long)sb->events_hi << 32)
183 + sb->events_lo);
184 printf("\n");
185 if (sb->level == 5) {
186 c = map_num(r5layout, sb->layout);
187 printf(" Layout : %s\n", c?c:"-unknown-");
188 }
189 if (sb->level == 6) {
190 c = map_num(r6layout, sb->layout);
191 printf(" Layout : %s\n", c?c:"-unknown-");
192 }
193 if (sb->level == 10) {
194 printf(" Layout :");
195 print_r10_layout(sb->layout);
196 printf("\n");
197 }
198 switch(sb->level) {
199 case 0:
200 case 4:
201 case 5:
202 case 6:
203 case 10:
204 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
205 break;
206 case -1:
207 printf(" Rounding : %dK\n", sb->chunk_size/1024);
208 break;
209 default: break;
210 }
211 printf("\n");
212 printf(" Number Major Minor RaidDevice State\n");
213 for (d= -1; d<(signed int)(sb->raid_disks+delta_extra + sb->spare_disks); d++) {
214 mdp_disk_t *dp;
215 char *dv;
216 char nb[5];
217 int wonly;
218 if (d>=0) dp = &sb->disks[d];
219 else dp = &sb->this_disk;
220 snprintf(nb, sizeof(nb), "%4d", d);
221 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
222 dp->number, dp->major, dp->minor, dp->raid_disk);
223 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
224 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
225 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
226 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
227 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
228 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
229 if (wonly) printf(" write-mostly");
230 if (dp->state == 0) printf(" spare");
231 if ((dv=map_dev(dp->major, dp->minor, 0)))
232 printf(" %s", dv);
233 printf("\n");
234 if (d == -1) printf("\n");
235 }
236 }
237
238 static void brief_examine_super0(struct supertype *st, int verbose)
239 {
240 mdp_super_t *sb = st->sb;
241 char *c=map_num(pers, sb->level);
242 char devname[20];
243
244 sprintf(devname, "/dev/md%d", sb->md_minor);
245
246 if (verbose) {
247 printf("ARRAY %s level=%s num-devices=%d",
248 devname,
249 c?c:"-unknown-", sb->raid_disks);
250 } else
251 printf("ARRAY %s", devname);
252
253 if (sb->minor_version >= 90)
254 printf(" UUID=%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
255 sb->set_uuid2, sb->set_uuid3);
256 else
257 printf(" UUID=%08x", sb->set_uuid0);
258 printf("\n");
259 }
260
261 static void export_examine_super0(struct supertype *st)
262 {
263 mdp_super_t *sb = st->sb;
264
265 printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
266 printf("MD_DEVICES=%d\n", sb->raid_disks);
267 if (sb->minor_version >= 90)
268 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
269 sb->set_uuid0, sb->set_uuid1,
270 sb->set_uuid2, sb->set_uuid3);
271 else
272 printf("MD_UUID=%08x\n", sb->set_uuid0);
273 printf("MD_UPDATE_TIME=%llu\n",
274 __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL);
275 printf("MD_EVENTS=%llu\n",
276 ((unsigned long long)sb->events_hi << 32)
277 + sb->events_lo);
278 }
279
280 static void detail_super0(struct supertype *st, char *homehost)
281 {
282 mdp_super_t *sb = st->sb;
283 printf(" UUID : ");
284 if (sb->minor_version >= 90)
285 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
286 sb->set_uuid2, sb->set_uuid3);
287 else
288 printf("%08x", sb->set_uuid0);
289 if (homehost) {
290 char buf[20];
291 void *hash = sha1_buffer(homehost,
292 strlen(homehost),
293 buf);
294 if (memcmp(&sb->set_uuid2, hash, 8)==0)
295 printf(" (local to host %s)", homehost);
296 }
297 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
298 }
299
300 static void brief_detail_super0(struct supertype *st)
301 {
302 mdp_super_t *sb = st->sb;
303 printf(" UUID=");
304 if (sb->minor_version >= 90)
305 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
306 sb->set_uuid2, sb->set_uuid3);
307 else
308 printf("%08x", sb->set_uuid0);
309 }
310 #endif
311
312 static int match_home0(struct supertype *st, char *homehost)
313 {
314 mdp_super_t *sb = st->sb;
315 char buf[20];
316 char *hash;
317
318 if (!homehost)
319 return 0;
320 hash = sha1_buffer(homehost,
321 strlen(homehost),
322 buf);
323
324 return (memcmp(&sb->set_uuid2, hash, 8)==0);
325 }
326
327 static void uuid_from_super0(struct supertype *st, int uuid[4])
328 {
329 mdp_super_t *super = st->sb;
330 uuid[0] = super->set_uuid0;
331 if (super->minor_version >= 90) {
332 uuid[1] = super->set_uuid1;
333 uuid[2] = super->set_uuid2;
334 uuid[3] = super->set_uuid3;
335 } else {
336 uuid[1] = 0;
337 uuid[2] = 0;
338 uuid[3] = 0;
339 }
340 }
341
342 static void getinfo_super0(struct supertype *st, struct mdinfo *info)
343 {
344 mdp_super_t *sb = st->sb;
345 int working = 0;
346 int i;
347
348 info->array.major_version = sb->major_version;
349 info->array.minor_version = sb->minor_version;
350 info->array.patch_version = sb->patch_version;
351 info->array.raid_disks = sb->raid_disks;
352 info->array.level = sb->level;
353 info->array.layout = sb->layout;
354 info->array.md_minor = sb->md_minor;
355 info->array.ctime = sb->ctime;
356 info->array.utime = sb->utime;
357 info->array.chunk_size = sb->chunk_size;
358 info->array.state = sb->state;
359 info->component_size = sb->size*2;
360
361 info->disk.state = sb->this_disk.state;
362 info->disk.major = sb->this_disk.major;
363 info->disk.minor = sb->this_disk.minor;
364 info->disk.raid_disk = sb->this_disk.raid_disk;
365 info->disk.number = sb->this_disk.number;
366
367 info->events = md_event(sb);
368 info->data_offset = 0;
369
370 sprintf(info->text_version, "0.%d", sb->minor_version);
371 info->safe_mode_delay = 200;
372
373 uuid_from_super0(st, info->uuid);
374
375 info->recovery_start = MaxSector;
376 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
377 info->reshape_active = 1;
378 info->reshape_progress = sb->reshape_position;
379 info->new_level = sb->new_level;
380 info->delta_disks = sb->delta_disks;
381 info->new_layout = sb->new_layout;
382 info->new_chunk = sb->new_chunk;
383 if (info->delta_disks < 0)
384 info->array.raid_disks -= info->delta_disks;
385 } else
386 info->reshape_active = 0;
387
388 sprintf(info->name, "%d", sb->md_minor);
389 /* work_disks is calculated rather than read directly */
390 for (i=0; i < MD_SB_DISKS; i++)
391 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
392 (sb->disks[i].raid_disk < info->array.raid_disks) &&
393 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
394 !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
395 working ++;
396 info->array.working_disks = working;
397 }
398
399
400 static int update_super0(struct supertype *st, struct mdinfo *info,
401 char *update,
402 char *devname, int verbose,
403 int uuid_set, char *homehost)
404 {
405 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
406 * For others, the return value is ignored.
407 */
408 int rv = 0;
409 mdp_super_t *sb = st->sb;
410 if (strcmp(update, "sparc2.2")==0 ) {
411 /* 2.2 sparc put the events in the wrong place
412 * So we copy the tail of the superblock
413 * up 4 bytes before continuing
414 */
415 __u32 *sb32 = (__u32*)sb;
416 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
417 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
418 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
419 if (verbose >= 0)
420 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
421 devname);
422 }
423 if (strcmp(update, "super-minor") ==0) {
424 sb->md_minor = info->array.md_minor;
425 if (verbose > 0)
426 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
427 devname, info->array.md_minor);
428 }
429 if (strcmp(update, "summaries") == 0) {
430 int i;
431 /* set nr_disks, active_disks, working_disks,
432 * failed_disks, spare_disks based on disks[]
433 * array in superblock.
434 * Also make sure extra slots aren't 'failed'
435 */
436 sb->nr_disks = sb->active_disks =
437 sb->working_disks = sb->failed_disks =
438 sb->spare_disks = 0;
439 for (i=0; i < MD_SB_DISKS ; i++)
440 if (sb->disks[i].major ||
441 sb->disks[i].minor) {
442 int state = sb->disks[i].state;
443 if (state & (1<<MD_DISK_REMOVED))
444 continue;
445 sb->nr_disks++;
446 if (state & (1<<MD_DISK_ACTIVE))
447 sb->active_disks++;
448 if (state & (1<<MD_DISK_FAULTY))
449 sb->failed_disks++;
450 else
451 sb->working_disks++;
452 if (state == 0)
453 sb->spare_disks++;
454 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
455 sb->disks[i].state = 0;
456 }
457 if (strcmp(update, "force-one")==0) {
458 /* Not enough devices for a working array, so
459 * bring this one up-to-date.
460 */
461 __u32 ehi = sb->events_hi, elo = sb->events_lo;
462 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
463 sb->events_lo = (info->events) & 0xFFFFFFFF;
464 if (sb->events_hi != ehi ||
465 sb->events_lo != elo)
466 rv = 1;
467 }
468 if (strcmp(update, "force-array")==0) {
469 /* degraded array and 'force' requested, so
470 * maybe need to mark it 'clean'
471 */
472 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
473 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
474 /* need to force clean */
475 sb->state |= (1 << MD_SB_CLEAN);
476 rv = 1;
477 }
478 }
479 if (strcmp(update, "assemble")==0) {
480 int d = info->disk.number;
481 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
482 int mask = (1<<MD_DISK_WRITEMOSTLY);
483 int add = 0;
484 if (sb->minor_version >= 91)
485 /* During reshape we don't insist on everything
486 * being marked 'sync'
487 */
488 add = (1<<MD_DISK_SYNC);
489 if (((sb->disks[d].state & ~mask) | add)
490 != info->disk.state) {
491 sb->disks[d].state = info->disk.state | wonly;
492 rv = 1;
493 }
494 }
495 if (strcmp(update, "linear-grow-new") == 0) {
496 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
497 sb->disks[info->disk.number].number = info->disk.number;
498 sb->disks[info->disk.number].major = info->disk.major;
499 sb->disks[info->disk.number].minor = info->disk.minor;
500 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
501 sb->disks[info->disk.number].state = info->disk.state;
502 sb->this_disk = sb->disks[info->disk.number];
503 }
504 if (strcmp(update, "linear-grow-update") == 0) {
505 sb->raid_disks = info->array.raid_disks;
506 sb->nr_disks = info->array.nr_disks;
507 sb->active_disks = info->array.active_disks;
508 sb->working_disks = info->array.working_disks;
509 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
510 sb->disks[info->disk.number].number = info->disk.number;
511 sb->disks[info->disk.number].major = info->disk.major;
512 sb->disks[info->disk.number].minor = info->disk.minor;
513 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
514 sb->disks[info->disk.number].state = info->disk.state;
515 }
516 if (strcmp(update, "resync") == 0) {
517 /* make sure resync happens */
518 sb->state &= ~(1<<MD_SB_CLEAN);
519 sb->recovery_cp = 0;
520 }
521 if (strcmp(update, "homehost") == 0 &&
522 homehost) {
523 uuid_set = 0;
524 update = "uuid";
525 info->uuid[0] = sb->set_uuid0;
526 info->uuid[1] = sb->set_uuid1;
527 }
528 if (strcmp(update, "uuid") == 0) {
529 if (!uuid_set && homehost) {
530 char buf[20];
531 char *hash = sha1_buffer(homehost,
532 strlen(homehost),
533 buf);
534 memcpy(info->uuid+2, hash, 8);
535 }
536 sb->set_uuid0 = info->uuid[0];
537 sb->set_uuid1 = info->uuid[1];
538 sb->set_uuid2 = info->uuid[2];
539 sb->set_uuid3 = info->uuid[3];
540 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
541 struct bitmap_super_s *bm;
542 bm = (struct bitmap_super_s*)(sb+1);
543 uuid_from_super0(st, (int*)bm->uuid);
544 }
545 }
546 if (strcmp(update, "_reshape_progress")==0)
547 sb->reshape_position = info->reshape_progress;
548
549 sb->sb_csum = calc_sb0_csum(sb);
550 return rv;
551 }
552
553 /*
554 * For verion-0 superblock, the homehost is 'stored' in the
555 * uuid. 8 bytes for a hash of the host leaving 8 bytes
556 * of random material.
557 * We use the first 8 bytes (64bits) of the sha1 of the
558 * host name
559 */
560
561
562 static int init_super0(struct supertype *st, mdu_array_info_t *info,
563 unsigned long long size, char *ignored_name, char *homehost,
564 int *uuid)
565 {
566 mdp_super_t *sb;
567 int spares;
568
569 if (posix_memalign((void**)&sb, 4096,
570 MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
571 fprintf(stderr, Name ": %s could not allocate superblock\n", __func__);
572 return 0;
573 }
574 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
575
576 st->sb = sb;
577 if (info == NULL) {
578 /* zeroing the superblock */
579 return 0;
580 }
581
582 spares = info->working_disks - info->active_disks;
583 if (info->raid_disks + spares > MD_SB_DISKS) {
584 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
585 info->raid_disks , spares, MD_SB_DISKS);
586 return 0;
587 }
588
589 sb->md_magic = MD_SB_MAGIC;
590 sb->major_version = 0;
591 sb->minor_version = 90;
592 sb->patch_version = 0;
593 sb->gvalid_words = 0; /* ignored */
594 sb->ctime = time(0);
595 sb->level = info->level;
596 if (size != info->size)
597 return 0;
598 sb->size = info->size;
599 sb->nr_disks = info->nr_disks;
600 sb->raid_disks = info->raid_disks;
601 sb->md_minor = info->md_minor;
602 sb->not_persistent = 0;
603 if (uuid) {
604 sb->set_uuid0 = uuid[0];
605 sb->set_uuid1 = uuid[1];
606 sb->set_uuid2 = uuid[2];
607 sb->set_uuid3 = uuid[3];
608 } else {
609 int rfd = open("/dev/urandom", O_RDONLY);
610 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
611 sb->set_uuid0 = random();
612 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
613 sb->set_uuid1 = random();
614 sb->set_uuid2 = random();
615 sb->set_uuid3 = random();
616 }
617 if (rfd >= 0)
618 close(rfd);
619 }
620 if (homehost) {
621 char buf[20];
622 char *hash = sha1_buffer(homehost,
623 strlen(homehost),
624 buf);
625 memcpy(&sb->set_uuid2, hash, 8);
626 }
627
628 sb->utime = sb->ctime;
629 sb->state = info->state;
630 sb->active_disks = info->active_disks;
631 sb->working_disks = info->working_disks;
632 sb->failed_disks = info->failed_disks;
633 sb->spare_disks = info->spare_disks;
634 sb->events_hi = 0;
635 sb->events_lo = 1;
636
637 sb->layout = info->layout;
638 sb->chunk_size = info->chunk_size;
639
640 return 1;
641 }
642
643 struct devinfo {
644 int fd;
645 char *devname;
646 mdu_disk_info_t disk;
647 struct devinfo *next;
648 };
649
650 #ifndef MDASSEMBLE
651 /* Add a device to the superblock being created */
652 static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
653 int fd, char *devname)
654 {
655 mdp_super_t *sb = st->sb;
656 mdp_disk_t *dk = &sb->disks[dinfo->number];
657 struct devinfo *di, **dip;
658
659 dk->number = dinfo->number;
660 dk->major = dinfo->major;
661 dk->minor = dinfo->minor;
662 dk->raid_disk = dinfo->raid_disk;
663 dk->state = dinfo->state;
664
665 sb->this_disk = sb->disks[dinfo->number];
666 sb->sb_csum = calc_sb0_csum(sb);
667
668 dip = (struct devinfo **)&st->info;
669 while (*dip)
670 dip = &(*dip)->next;
671 di = malloc(sizeof(struct devinfo));
672 di->fd = fd;
673 di->devname = devname;
674 di->disk = *dinfo;
675 di->next = NULL;
676 *dip = di;
677
678 return 0;
679 }
680 #endif
681
682 static int store_super0(struct supertype *st, int fd)
683 {
684 unsigned long long dsize;
685 unsigned long long offset;
686 mdp_super_t *super = st->sb;
687
688 if (!get_dev_size(fd, NULL, &dsize))
689 return 1;
690
691 if (dsize < MD_RESERVED_SECTORS*512)
692 return 2;
693
694 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
695
696 offset *= 512;
697
698 if (lseek64(fd, offset, 0)< 0LL)
699 return 3;
700
701 if (write(fd, super, sizeof(*super)) != sizeof(*super))
702 return 4;
703
704 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
705 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
706 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
707 if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) !=
708 ROUND_UP(sizeof(*bm),4096))
709 return 5;
710 }
711
712 fsync(fd);
713 return 0;
714 }
715
716 #ifndef MDASSEMBLE
717 static int write_init_super0(struct supertype *st)
718 {
719 mdp_super_t *sb = st->sb;
720 int rv = 0;
721 struct devinfo *di;
722
723 for (di = st->info ; di && ! rv ; di = di->next) {
724
725 if (di->disk.state == 1)
726 continue;
727 if (di->fd == -1)
728 continue;
729 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
730 ;
731
732 sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY);
733
734 sb->this_disk = sb->disks[di->disk.number];
735 sb->sb_csum = calc_sb0_csum(sb);
736 rv = store_super0(st, di->fd);
737
738 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
739 rv = st->ss->write_bitmap(st, di->fd);
740
741 if (rv)
742 fprintf(stderr,
743 Name ": failed to write superblock to %s\n",
744 di->devname);
745 close(di->fd);
746 di->fd = -1;
747 }
748 return rv;
749 }
750 #endif
751
752 static int compare_super0(struct supertype *st, struct supertype *tst)
753 {
754 /*
755 * return:
756 * 0 same, or first was empty, and second was copied
757 * 1 second had wrong number
758 * 2 wrong uuid
759 * 3 wrong other info
760 */
761 mdp_super_t *first = st->sb;
762 mdp_super_t *second = tst->sb;
763 int uuid1[4], uuid2[4];
764
765 if (second->md_magic != MD_SB_MAGIC)
766 return 1;
767 if (!first) {
768 if (posix_memalign((void**)&first, 4096,
769 MD_SB_BYTES +
770 ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
771 fprintf(stderr, Name
772 ": %s could not allocate superblock\n", __func__);
773 return 1;
774 }
775 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
776 st->sb = first;
777 return 0;
778 }
779
780 uuid_from_super0(st, uuid1);
781 uuid_from_super0(tst, uuid2);
782 if (!same_uuid(uuid1, uuid2, 0))
783 return 2;
784 if (first->major_version != second->major_version ||
785 first->minor_version != second->minor_version ||
786 first->patch_version != second->patch_version ||
787 first->gvalid_words != second->gvalid_words ||
788 first->ctime != second->ctime ||
789 first->level != second->level ||
790 first->size != second->size ||
791 first->raid_disks != second->raid_disks )
792 return 3;
793
794 return 0;
795 }
796
797
798 static void free_super0(struct supertype *st);
799
800 static int load_super0(struct supertype *st, int fd, char *devname)
801 {
802 /* try to read in the superblock
803 * Return:
804 * 0 on success
805 * 1 on cannot get superblock
806 * 2 on superblock meaningless
807 */
808 unsigned long long dsize;
809 unsigned long long offset;
810 mdp_super_t *super;
811 int uuid[4];
812 struct bitmap_super_s *bsb;
813
814 free_super0(st);
815
816 if (st->subarray[0])
817 return 1;
818
819 if (!get_dev_size(fd, devname, &dsize))
820 return 1;
821
822 if (dsize < MD_RESERVED_SECTORS*512) {
823 if (devname)
824 fprintf(stderr, Name
825 ": %s is too small for md: size is %llu sectors.\n",
826 devname, dsize);
827 return 1;
828 }
829
830 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
831
832 offset *= 512;
833
834 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
835
836 if (lseek64(fd, offset, 0)< 0LL) {
837 if (devname)
838 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
839 devname, strerror(errno));
840 return 1;
841 }
842
843 if (posix_memalign((void**)&super, 4096,
844 MD_SB_BYTES +
845 ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
846 fprintf(stderr, Name
847 ": %s could not allocate superblock\n", __func__);
848 return 1;
849 }
850
851 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
852 if (devname)
853 fprintf(stderr, Name ": Cannot read superblock on %s\n",
854 devname);
855 free(super);
856 return 1;
857 }
858
859 if (st->ss && st->minor_version == 9)
860 super0_swap_endian(super);
861
862 if (super->md_magic != MD_SB_MAGIC) {
863 if (devname)
864 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
865 devname, MD_SB_MAGIC, super->md_magic);
866 free(super);
867 return 2;
868 }
869
870 if (super->major_version != 0) {
871 if (devname)
872 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
873 devname, super->major_version);
874 free(super);
875 return 2;
876 }
877 st->sb = super;
878
879 if (st->ss == NULL) {
880 st->ss = &super0;
881 st->minor_version = super->minor_version;
882 st->max_devs = MD_SB_DISKS;
883 st->info = NULL;
884 }
885
886 /* Now check on the bitmap superblock */
887 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
888 return 0;
889 /* Read the bitmap superblock and make sure it looks
890 * valid. If it doesn't clear the bit. An --assemble --force
891 * should get that written out.
892 */
893 if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096))
894 != ROUND_UP(sizeof(struct bitmap_super_s),4096))
895 goto no_bitmap;
896
897 uuid_from_super0(st, uuid);
898 bsb = (struct bitmap_super_s *)(super+1);
899 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
900 memcmp(bsb->uuid, uuid, 16) != 0)
901 goto no_bitmap;
902 return 0;
903
904 no_bitmap:
905 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
906
907 return 0;
908 }
909
910 static struct supertype *match_metadata_desc0(char *arg)
911 {
912 struct supertype *st = malloc(sizeof(*st));
913 if (!st) return st;
914
915 memset(st, 0, sizeof(*st));
916 st->ss = &super0;
917 st->info = NULL;
918 st->minor_version = 90;
919 st->max_devs = MD_SB_DISKS;
920 st->sb = NULL;
921 /* we sometimes get 00.90 */
922 while (arg[0] == '0' && arg[1] == '0')
923 arg++;
924 if (strcmp(arg, "0") == 0 ||
925 strcmp(arg, "0.90") == 0
926 )
927 return st;
928
929 st->minor_version = 91; /* reshape in progress */
930 if (strcmp(arg, "0.91") == 0) /* For dup_super support */
931 return st;
932
933 st->minor_version = 9; /* flag for 'byte-swapped' */
934 if (strcmp(arg, "0.swap")==0 ||
935 strcmp(arg, "0.9") == 0) /* For dup_super support */
936 return st;
937
938 free(st);
939 return NULL;
940 }
941
942 static __u64 avail_size0(struct supertype *st, __u64 devsize)
943 {
944 if (devsize < MD_RESERVED_SECTORS)
945 return 0ULL;
946 return MD_NEW_SIZE_SECTORS(devsize);
947 }
948
949 static int add_internal_bitmap0(struct supertype *st, int *chunkp,
950 int delay, int write_behind,
951 unsigned long long size, int may_change,
952 int major)
953 {
954 /*
955 * The bitmap comes immediately after the superblock and must be 60K in size
956 * at most. The default size is between 30K and 60K
957 *
958 * size is in sectors, chunk is in bytes !!!
959 */
960 unsigned long long bits;
961 unsigned long long max_bits = 60*1024*8;
962 unsigned long long min_chunk;
963 int chunk = *chunkp;
964 mdp_super_t *sb = st->sb;
965 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
966
967
968 min_chunk = 4096; /* sub-page chunks don't work yet.. */
969 bits = (size * 512) / min_chunk + 1;
970 while (bits > max_bits) {
971 min_chunk *= 2;
972 bits = (bits+1)/2;
973 }
974 if (chunk == UnSet) {
975 /* A chunk size less than a few Megabytes gives poor
976 * performance without increasing resync noticeably
977 */
978 chunk = min_chunk;
979 if (chunk < 64*1024*1024)
980 chunk = 64*1024*1024;
981 } else if (chunk < min_chunk)
982 return 0; /* chunk size too small */
983
984 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
985
986 memset(bms, 0, sizeof(*bms));
987 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
988 bms->version = __cpu_to_le32(major);
989 uuid_from_super0(st, (int*)bms->uuid);
990 bms->chunksize = __cpu_to_le32(chunk);
991 bms->daemon_sleep = __cpu_to_le32(delay);
992 bms->sync_size = __cpu_to_le64(size);
993 bms->write_behind = __cpu_to_le32(write_behind);
994 *chunkp = chunk;
995 return 1;
996 }
997
998
999 static void locate_bitmap0(struct supertype *st, int fd)
1000 {
1001 unsigned long long dsize;
1002 unsigned long long offset;
1003
1004 if (!get_dev_size(fd, NULL, &dsize))
1005 return;
1006
1007 if (dsize < MD_RESERVED_SECTORS*512)
1008 return;
1009
1010 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1011
1012 offset *= 512;
1013
1014 offset += MD_SB_BYTES;
1015
1016 lseek64(fd, offset, 0);
1017 }
1018
1019 static int write_bitmap0(struct supertype *st, int fd)
1020 {
1021 unsigned long long dsize;
1022 unsigned long long offset;
1023 mdp_super_t *sb = st->sb;
1024
1025 int rv = 0;
1026
1027 int towrite, n;
1028 char abuf[4096+4096];
1029 char *buf = (char*)(((long)(abuf+4096))&~4095L);
1030
1031 if (!get_dev_size(fd, NULL, &dsize))
1032 return 1;
1033
1034
1035 if (dsize < MD_RESERVED_SECTORS*512)
1036 return -1;
1037
1038 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1039
1040 offset *= 512;
1041
1042 if (lseek64(fd, offset + 4096, 0)< 0LL)
1043 return 3;
1044
1045 memset(buf, 0xff, 4096);
1046 memcpy(buf, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t));
1047 towrite = 64*1024;
1048 while (towrite > 0) {
1049 n = towrite;
1050 if (n > 4096)
1051 n = 4096;
1052 n = write(fd, buf, n);
1053 if (n > 0)
1054 towrite -= n;
1055 else
1056 break;
1057 memset(buf, 0xff, 4096);
1058 }
1059 fsync(fd);
1060 if (towrite)
1061 rv = -2;
1062
1063 return rv;
1064 }
1065
1066 static void free_super0(struct supertype *st)
1067 {
1068 if (st->sb)
1069 free(st->sb);
1070 st->sb = NULL;
1071 }
1072
1073 #ifndef MDASSEMBLE
1074 static int validate_geometry0(struct supertype *st, int level,
1075 int layout, int raiddisks,
1076 int chunk, unsigned long long size,
1077 char *subdev, unsigned long long *freesize,
1078 int verbose)
1079 {
1080 unsigned long long ldsize;
1081 int fd;
1082
1083 if (level == LEVEL_CONTAINER) {
1084 if (verbose)
1085 fprintf(stderr, Name ": 0.90 metadata does not support containers\n");
1086 return 0;
1087 }
1088 if (raiddisks > MD_SB_DISKS) {
1089 if (verbose)
1090 fprintf(stderr, Name ": 0.90 metadata supports at most %d devices per array\n",
1091 MD_SB_DISKS);
1092 return 0;
1093 }
1094 if (size > (0x7fffffffULL<<9)) {
1095 if (verbose)
1096 fprintf(stderr, Name ": 0.90 metadata supports at most 2 terrabytes per device\n");
1097 return 0;
1098 }
1099 if (!subdev)
1100 return 1;
1101
1102 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1103 if (fd < 0) {
1104 if (verbose)
1105 fprintf(stderr, Name ": super0.90 cannot open %s: %s\n",
1106 subdev, strerror(errno));
1107 return 0;
1108 }
1109
1110 if (!get_dev_size(fd, subdev, &ldsize)) {
1111 close(fd);
1112 return 0;
1113 }
1114 close(fd);
1115
1116 if (ldsize < MD_RESERVED_SECTORS * 512)
1117 return 0;
1118 if (size > (0x7fffffffULL<<9))
1119 return 0;
1120 *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
1121 return 1;
1122 }
1123 #endif /* MDASSEMBLE */
1124
1125 struct superswitch super0 = {
1126 #ifndef MDASSEMBLE
1127 .examine_super = examine_super0,
1128 .brief_examine_super = brief_examine_super0,
1129 .export_examine_super = export_examine_super0,
1130 .detail_super = detail_super0,
1131 .brief_detail_super = brief_detail_super0,
1132 .write_init_super = write_init_super0,
1133 .validate_geometry = validate_geometry0,
1134 .add_to_super = add_to_super0,
1135 #endif
1136 .match_home = match_home0,
1137 .uuid_from_super = uuid_from_super0,
1138 .getinfo_super = getinfo_super0,
1139 .update_super = update_super0,
1140 .init_super = init_super0,
1141 .store_super = store_super0,
1142 .compare_super = compare_super0,
1143 .load_super = load_super0,
1144 .match_metadata_desc = match_metadata_desc0,
1145 .avail_size = avail_size0,
1146 .add_internal_bitmap = add_internal_bitmap0,
1147 .locate_bitmap = locate_bitmap0,
1148 .write_bitmap = write_bitmap0,
1149 .free_super = free_super0,
1150 .name = "0.90",
1151 };