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