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