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