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