Always initialise a struct super_type to zero
[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) {
b578481c
NB
191 printf(" Layout : near=%d, %s=%d\n",
192 sb->layout&255,
193 (sb->layout&0x10000)?"offset":"far",
194 (sb->layout>>8)&255);
265e0f17 195 }
4b1ac34b
NB
196 switch(sb->level) {
197 case 0:
198 case 4:
199 case 5:
2c102711
NB
200 case 6:
201 case 10:
4b1ac34b
NB
202 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
203 break;
204 case -1:
205 printf(" Rounding : %dK\n", sb->chunk_size/1024);
206 break;
353632d9 207 default: break;
4b1ac34b
NB
208 }
209 printf("\n");
210 printf(" Number Major Minor RaidDevice State\n");
211 for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
212 mdp_disk_t *dp;
213 char *dv;
214 char nb[5];
dfd4d8ee 215 int wonly;
4b1ac34b
NB
216 if (d>=0) dp = &sb->disks[d];
217 else dp = &sb->this_disk;
8f23b0b3 218 snprintf(nb, sizeof(nb), "%4d", d);
4b1ac34b
NB
219 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
220 dp->number, dp->major, dp->minor, dp->raid_disk);
dfd4d8ee
NB
221 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
222 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
4b1ac34b
NB
223 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
224 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
225 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
226 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
dfd4d8ee 227 if (wonly) printf(" write-mostly");
4b1ac34b 228 if (dp->state == 0) printf(" spare");
16c6fa80 229 if ((dv=map_dev(dp->major, dp->minor, 0)))
4b1ac34b
NB
230 printf(" %s", dv);
231 printf("\n");
232 if (d == -1) printf("\n");
233 }
234}
235
3da92f27 236static void brief_examine_super0(struct supertype *st)
4b1ac34b 237{
3da92f27 238 mdp_super_t *sb = st->sb;
4b1ac34b 239 char *c=map_num(pers, sb->level);
2998bc01 240 char devname[20];
2998bc01
NB
241
242 sprintf(devname, "/dev/md%d", sb->md_minor);
4b1ac34b
NB
243
244 printf("ARRAY %s level=%s num-devices=%d UUID=",
2998bc01 245 devname,
4b1ac34b
NB
246 c?c:"-unknown-", sb->raid_disks);
247 if (sb->minor_version >= 90)
248 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
249 sb->set_uuid2, sb->set_uuid3);
250 else
251 printf("%08x", sb->set_uuid0);
252 printf("\n");
253}
254
0d726f17
KS
255static void export_examine_super0(struct supertype *st)
256{
257 mdp_super_t *sb = st->sb;
258
259 printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
260 printf("MD_DEVICES=%d\n", sb->raid_disks);
261 if (sb->minor_version >= 90)
262 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
263 sb->set_uuid0, sb->set_uuid1,
264 sb->set_uuid2, sb->set_uuid3);
265 else
266 printf("MD_UUID=%08x\n", sb->set_uuid0);
267 printf("MD_UPDATE_TIME=%llu\n",
268 __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL);
269 printf("MD_EVENTS=%llu\n",
270 ((unsigned long long)sb->events_hi << 32)
271 + sb->events_lo);
272}
273
3da92f27 274static void detail_super0(struct supertype *st, char *homehost)
4b1ac34b 275{
3da92f27 276 mdp_super_t *sb = st->sb;
4b1ac34b
NB
277 printf(" UUID : ");
278 if (sb->minor_version >= 90)
279 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
280 sb->set_uuid2, sb->set_uuid3);
281 else
282 printf("%08x", sb->set_uuid0);
b6750aa8 283 if (homehost) {
8268ed74
NB
284 char buf[20];
285 void *hash = sha1_buffer(homehost,
286 strlen(homehost),
287 buf);
b6750aa8
NB
288 if (memcmp(&sb->set_uuid2, hash, 8)==0)
289 printf(" (local to host %s)", homehost);
290 }
4b1ac34b
NB
291 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
292}
293
3da92f27 294static void brief_detail_super0(struct supertype *st)
4b1ac34b 295{
3da92f27 296 mdp_super_t *sb = st->sb;
4b1ac34b
NB
297 printf(" UUID=");
298 if (sb->minor_version >= 90)
299 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
300 sb->set_uuid2, sb->set_uuid3);
301 else
302 printf("%08x", sb->set_uuid0);
303}
54bad364 304
0d726f17 305static void export_detail_super0(struct supertype *st)
54bad364 306{
3da92f27 307 mdp_super_t *sb = st->sb;
54bad364
KS
308 printf("MD_UUID=");
309 if (sb->minor_version >= 90)
310 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
311 sb->set_uuid2, sb->set_uuid3);
312 else
313 printf("%08x", sb->set_uuid0);
314 printf("\n");
315}
c7654afc 316#endif
83b6208e 317
3da92f27 318static int match_home0(struct supertype *st, char *homehost)
83b6208e 319{
3da92f27 320 mdp_super_t *sb = st->sb;
8268ed74
NB
321 char buf[20];
322 char *hash = sha1_buffer(homehost,
323 strlen(homehost),
324 buf);
83b6208e
NB
325
326 return (memcmp(&sb->set_uuid2, hash, 8)==0);
327}
328
3da92f27 329static void uuid_from_super0(struct supertype *st, int uuid[4])
4b1ac34b 330{
3da92f27 331 mdp_super_t *super = st->sb;
4b1ac34b
NB
332 uuid[0] = super->set_uuid0;
333 if (super->minor_version >= 90) {
334 uuid[1] = super->set_uuid1;
335 uuid[2] = super->set_uuid2;
336 uuid[3] = super->set_uuid3;
337 } else {
338 uuid[1] = 0;
339 uuid[2] = 0;
340 uuid[3] = 0;
341 }
342}
343
3da92f27 344static void getinfo_super0(struct supertype *st, struct mdinfo *info)
4b1ac34b 345{
3da92f27 346 mdp_super_t *sb = st->sb;
4b1ac34b
NB
347 int working = 0;
348 int i;
349
350 info->array.major_version = sb->major_version;
351 info->array.minor_version = sb->minor_version;
352 info->array.patch_version = sb->patch_version;
353 info->array.raid_disks = sb->raid_disks;
354 info->array.level = sb->level;
265e0f17 355 info->array.layout = sb->layout;
4b1ac34b
NB
356 info->array.md_minor = sb->md_minor;
357 info->array.ctime = sb->ctime;
353632d9
NB
358 info->array.utime = sb->utime;
359 info->array.chunk_size = sb->chunk_size;
583315d9 360 info->array.state = sb->state;
353632d9 361 info->component_size = sb->size*2;
4b1ac34b
NB
362
363 info->disk.state = sb->this_disk.state;
364 info->disk.major = sb->this_disk.major;
365 info->disk.minor = sb->this_disk.minor;
366 info->disk.raid_disk = sb->this_disk.raid_disk;
fbf8a0b7 367 info->disk.number = sb->this_disk.number;
4b1ac34b
NB
368
369 info->events = md_event(sb);
353632d9 370 info->data_offset = 0;
4b1ac34b 371
3da92f27 372 uuid_from_super0(st, info->uuid);
947fd4dd 373
353632d9
NB
374 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
375 info->reshape_active = 1;
376 info->reshape_progress = sb->reshape_position;
377 info->new_level = sb->new_level;
378 info->delta_disks = sb->delta_disks;
379 info->new_layout = sb->new_layout;
380 info->new_chunk = sb->new_chunk;
381 } else
382 info->reshape_active = 0;
383
8a46fe84 384 sprintf(info->name, "%d", sb->md_minor);
4b1ac34b
NB
385 /* work_disks is calculated rather than read directly */
386 for (i=0; i < MD_SB_DISKS; i++)
387 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
08110d41 388 (sb->disks[i].raid_disk < info->array.raid_disks) &&
4b1ac34b
NB
389 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
390 !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
391 working ++;
392 info->array.working_disks = working;
393}
394
395
68c7d6d7 396static int update_super0(struct supertype *st, struct mdinfo *info,
3da92f27 397 char *update,
e5eac01f
NB
398 char *devname, int verbose,
399 int uuid_set, char *homehost)
4b1ac34b 400{
c4d831e1
NB
401 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
402 * For others, the return value is ignored.
403 */
4b1ac34b 404 int rv = 0;
3da92f27 405 mdp_super_t *sb = st->sb;
4b1ac34b
NB
406 if (strcmp(update, "sparc2.2")==0 ) {
407 /* 2.2 sparc put the events in the wrong place
408 * So we copy the tail of the superblock
409 * up 4 bytes before continuing
410 */
411 __u32 *sb32 = (__u32*)sb;
412 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
413 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
414 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
dab6685f
NB
415 if (verbose >= 0)
416 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
417 devname);
4b1ac34b
NB
418 }
419 if (strcmp(update, "super-minor") ==0) {
420 sb->md_minor = info->array.md_minor;
dab6685f 421 if (verbose > 0)
4b1ac34b
NB
422 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
423 devname, info->array.md_minor);
424 }
425 if (strcmp(update, "summaries") == 0) {
426 int i;
427 /* set nr_disks, active_disks, working_disks,
aba69144 428 * failed_disks, spare_disks based on disks[]
4b1ac34b
NB
429 * array in superblock.
430 * Also make sure extra slots aren't 'failed'
431 */
432 sb->nr_disks = sb->active_disks =
433 sb->working_disks = sb->failed_disks =
434 sb->spare_disks = 0;
aba69144 435 for (i=0; i < MD_SB_DISKS ; i++)
4b1ac34b
NB
436 if (sb->disks[i].major ||
437 sb->disks[i].minor) {
438 int state = sb->disks[i].state;
439 if (state & (1<<MD_DISK_REMOVED))
440 continue;
441 sb->nr_disks++;
442 if (state & (1<<MD_DISK_ACTIVE))
443 sb->active_disks++;
444 if (state & (1<<MD_DISK_FAULTY))
445 sb->failed_disks++;
446 else
447 sb->working_disks++;
448 if (state == 0)
449 sb->spare_disks++;
450 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
451 sb->disks[i].state = 0;
452 }
67a8c82d
NB
453 if (strcmp(update, "force-one")==0) {
454 /* Not enough devices for a working array, so
455 * bring this one up-to-date.
456 */
c4d831e1 457 __u32 ehi = sb->events_hi, elo = sb->events_lo;
4b1ac34b
NB
458 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
459 sb->events_lo = (info->events) & 0xFFFFFFFF;
c4d831e1
NB
460 if (sb->events_hi != ehi ||
461 sb->events_lo != elo)
462 rv = 1;
67a8c82d
NB
463 }
464 if (strcmp(update, "force-array")==0) {
465 /* degraded array and 'force' requested, so
466 * maybe need to mark it 'clean'
467 */
c4d831e1
NB
468 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
469 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
4b1ac34b
NB
470 /* need to force clean */
471 sb->state |= (1 << MD_SB_CLEAN);
c4d831e1
NB
472 rv = 1;
473 }
4b1ac34b
NB
474 }
475 if (strcmp(update, "assemble")==0) {
476 int d = info->disk.number;
dfd4d8ee 477 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
c4d831e1
NB
478 if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
479 != info->disk.state) {
480 sb->disks[d].state = info->disk.state | wonly;
4b1ac34b
NB
481 rv = 1;
482 }
483 }
f752781f
NB
484 if (strcmp(update, "linear-grow-new") == 0) {
485 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
486 sb->disks[info->disk.number].number = info->disk.number;
487 sb->disks[info->disk.number].major = info->disk.major;
488 sb->disks[info->disk.number].minor = info->disk.minor;
489 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
490 sb->disks[info->disk.number].state = info->disk.state;
491 sb->this_disk = sb->disks[info->disk.number];
492 }
493 if (strcmp(update, "linear-grow-update") == 0) {
4b1ac34b
NB
494 sb->raid_disks = info->array.raid_disks;
495 sb->nr_disks = info->array.nr_disks;
496 sb->active_disks = info->array.active_disks;
497 sb->working_disks = info->array.working_disks;
498 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
499 sb->disks[info->disk.number].number = info->disk.number;
500 sb->disks[info->disk.number].major = info->disk.major;
501 sb->disks[info->disk.number].minor = info->disk.minor;
502 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
503 sb->disks[info->disk.number].state = info->disk.state;
4b1ac34b
NB
504 }
505 if (strcmp(update, "resync") == 0) {
506 /* make sure resync happens */
507 sb->state &= ~(1<<MD_SB_CLEAN);
508 sb->recovery_cp = 0;
509 }
0237e0ca
NB
510 if (strcmp(update, "homehost") == 0 &&
511 homehost) {
512 uuid_set = 0;
513 update = "uuid";
514 info->uuid[0] = sb->set_uuid0;
515 info->uuid[1] = sb->set_uuid1;
516 }
7d99579f 517 if (strcmp(update, "uuid") == 0) {
e5eac01f 518 if (!uuid_set && homehost) {
8268ed74
NB
519 char buf[20];
520 char *hash = sha1_buffer(homehost,
521 strlen(homehost),
522 buf);
e5eac01f
NB
523 memcpy(info->uuid+2, hash, 8);
524 }
7d99579f
NB
525 sb->set_uuid0 = info->uuid[0];
526 sb->set_uuid1 = info->uuid[1];
527 sb->set_uuid2 = info->uuid[2];
528 sb->set_uuid3 = info->uuid[3];
14263cf1
NB
529 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
530 struct bitmap_super_s *bm;
531 bm = (struct bitmap_super_s*)(sb+1);
3da92f27 532 uuid_from_super0(st, (int*)bm->uuid);
14263cf1 533 }
7d99579f 534 }
353632d9
NB
535 if (strcmp(update, "_reshape_progress")==0)
536 sb->reshape_position = info->reshape_progress;
4b1ac34b
NB
537
538 sb->sb_csum = calc_sb0_csum(sb);
539 return rv;
540}
541
05697ec1
NB
542/*
543 * For verion-0 superblock, the homehost is 'stored' in the
544 * uuid. 8 bytes for a hash of the host leaving 8 bytes
545 * of random material.
546 * We use the first 8 bytes (64bits) of the sha1 of the
547 * host name
548 */
4b1ac34b
NB
549
550
3da92f27 551static int init_super0(struct supertype *st, mdu_array_info_t *info,
3d3dd91e
NB
552 unsigned long long size, char *ignored_name, char *homehost,
553 int *uuid)
4b1ac34b 554{
55935d51 555 mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
82d9eba6 556 int spares;
55935d51 557 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
4b1ac34b 558
64557c33 559 st->sb = sb;
f9ce90ba
NB
560 if (info->major_version == -1) {
561 /* zeroing the superblock */
82d9eba6
NB
562 return 0;
563 }
564
565 spares = info->working_disks - info->active_disks;
566 if (info->raid_disks + spares > MD_SB_DISKS) {
567 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
568 info->raid_disks , spares, MD_SB_DISKS);
569 return 0;
f9ce90ba
NB
570 }
571
4b1ac34b
NB
572 sb->md_magic = MD_SB_MAGIC;
573 sb->major_version = 0;
574 sb->minor_version = 90;
575 sb->patch_version = 0;
576 sb->gvalid_words = 0; /* ignored */
4b1ac34b
NB
577 sb->ctime = time(0);
578 sb->level = info->level;
5dd497ee
NB
579 if (size != info->size)
580 return 0;
4b1ac34b
NB
581 sb->size = info->size;
582 sb->nr_disks = info->nr_disks;
583 sb->raid_disks = info->raid_disks;
584 sb->md_minor = info->md_minor;
585 sb->not_persistent = 0;
3d3dd91e
NB
586 if (uuid) {
587 sb->set_uuid0 = uuid[0];
588 sb->set_uuid1 = uuid[1];
589 sb->set_uuid2 = uuid[2];
590 sb->set_uuid3 = uuid[3];
591 } else {
592 int rfd = open("/dev/urandom", O_RDONLY);
593 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
594 sb->set_uuid0 = random();
595 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
596 sb->set_uuid1 = random();
597 sb->set_uuid2 = random();
598 sb->set_uuid3 = random();
599 }
600 if (rfd >= 0)
601 close(rfd);
dfe47e00 602 }
05697ec1 603 if (homehost) {
8268ed74
NB
604 char buf[20];
605 char *hash = sha1_buffer(homehost,
606 strlen(homehost),
607 buf);
05697ec1
NB
608 memcpy(&sb->set_uuid2, hash, 8);
609 }
4b1ac34b
NB
610
611 sb->utime = sb->ctime;
612 sb->state = info->state;
613 sb->active_disks = info->active_disks;
614 sb->working_disks = info->working_disks;
615 sb->failed_disks = info->failed_disks;
234a131a 616 sb->spare_disks = info->spare_disks;
4b1ac34b
NB
617 sb->events_hi = 0;
618 sb->events_lo = 1;
619
620 sb->layout = info->layout;
621 sb->chunk_size = info->chunk_size;
622
82d9eba6 623 return 1;
4b1ac34b
NB
624}
625
111d01fc
NB
626struct devinfo {
627 int fd;
628 char *devname;
629 mdu_disk_info_t disk;
630 struct devinfo *next;
631};
4b1ac34b 632/* Add a device to the superblock being created */
111d01fc
NB
633static void add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
634 int fd, char *devname)
4b1ac34b 635{
3da92f27 636 mdp_super_t *sb = st->sb;
4b1ac34b 637 mdp_disk_t *dk = &sb->disks[dinfo->number];
111d01fc 638 struct devinfo *di, **dip;
353632d9 639
4b1ac34b
NB
640 dk->number = dinfo->number;
641 dk->major = dinfo->major;
642 dk->minor = dinfo->minor;
643 dk->raid_disk = dinfo->raid_disk;
644 dk->state = dinfo->state;
111d01fc
NB
645
646 dip = (struct devinfo **)&st->info;
647 while (*dip)
648 dip = &(*dip)->next;
649 di = malloc(sizeof(struct devinfo));
650 di->fd = fd;
651 di->devname = devname;
652 di->disk = *dinfo;
653 di->next = NULL;
654 *dip = di;
4b1ac34b
NB
655}
656
3da92f27 657static int store_super0(struct supertype *st, int fd)
4b1ac34b 658{
4b1ac34b
NB
659 unsigned long long dsize;
660 unsigned long long offset;
3da92f27 661 mdp_super_t *super = st->sb;
beae1dfe
NB
662
663 if (!get_dev_size(fd, NULL, &dsize))
664 return 1;
4b1ac34b 665
eb6dae98 666 if (dsize < MD_RESERVED_SECTORS*512)
4b1ac34b 667 return 2;
353632d9 668
4b1ac34b
NB
669 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
670
671 offset *= 512;
672
673 if (lseek64(fd, offset, 0)< 0LL)
674 return 3;
675
676 if (write(fd, super, sizeof(*super)) != sizeof(*super))
677 return 4;
678
14263cf1
NB
679 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
680 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
681 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
9fca7d62
NB
682 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
683 return 5;
14263cf1
NB
684 }
685
570c0542 686 fsync(fd);
4b1ac34b
NB
687 return 0;
688}
689
111d01fc
NB
690#ifndef MDASSEMBLE
691static int write_init_super0(struct supertype *st)
4b1ac34b 692{
3da92f27 693 mdp_super_t *sb = st->sb;
111d01fc
NB
694 int rv = 0;
695 struct devinfo *di;
4b1ac34b 696
111d01fc 697 for (di = st->info ; di && ! rv ; di = di->next) {
4b1ac34b 698
111d01fc
NB
699 if (di->disk.state == 1)
700 continue;
701 Kill(di->devname, 0, 1, 1);
702 Kill(di->devname, 0, 1, 1);
8d75b7fc 703
111d01fc 704 sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY);
55935d51 705
111d01fc
NB
706 sb->this_disk = sb->disks[di->disk.number];
707 sb->sb_csum = calc_sb0_csum(sb);
708 rv = store_super0(st, di->fd);
55935d51 709
111d01fc
NB
710 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
711 rv = st->ss->write_bitmap(st, di->fd);
712
713 if (rv)
714 fprintf(stderr,
715 Name ": failed to write superblock to %s\n",
716 di->devname);
717 close(di->fd);
718 di->fd = -1;
719 }
4b1ac34b
NB
720 return rv;
721}
111d01fc 722#endif
4b1ac34b 723
64557c33 724static int compare_super0(struct supertype *st, struct supertype *tst)
4b1ac34b
NB
725{
726 /*
727 * return:
728 * 0 same, or first was empty, and second was copied
729 * 1 second had wrong number
730 * 2 wrong uuid
731 * 3 wrong other info
732 */
64557c33
NB
733 mdp_super_t *first = st->sb;
734 mdp_super_t *second = tst->sb;
4b1ac34b 735 int uuid1[4], uuid2[4];
64557c33 736
4b1ac34b
NB
737 if (second->md_magic != MD_SB_MAGIC)
738 return 1;
739 if (!first) {
14263cf1
NB
740 first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
741 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
64557c33 742 st->sb = first;
4b1ac34b
NB
743 return 0;
744 }
745
3da92f27
NB
746 uuid_from_super0(st, uuid1);
747 uuid_from_super0(tst, uuid2);
f277ce36 748 if (!same_uuid(uuid1, uuid2, 0))
4b1ac34b
NB
749 return 2;
750 if (first->major_version != second->major_version ||
751 first->minor_version != second->minor_version ||
752 first->patch_version != second->patch_version ||
753 first->gvalid_words != second->gvalid_words ||
754 first->ctime != second->ctime ||
755 first->level != second->level ||
756 first->size != second->size ||
757 first->raid_disks != second->raid_disks )
758 return 3;
759
760 return 0;
761}
762
763
3da92f27
NB
764static void free_super0(struct supertype *st);
765
766static int load_super0(struct supertype *st, int fd, char *devname)
4b1ac34b
NB
767{
768 /* try to read in the superblock
769 * Return:
770 * 0 on success
771 * 1 on cannot get superblock
772 * 2 on superblock meaningless
773 */
4b1ac34b
NB
774 unsigned long long dsize;
775 unsigned long long offset;
776 mdp_super_t *super;
14263cf1
NB
777 int uuid[4];
778 struct bitmap_super_s *bsb;
aba69144 779
3da92f27
NB
780 free_super0(st);
781
beae1dfe
NB
782 if (!get_dev_size(fd, devname, &dsize))
783 return 1;
4b1ac34b 784
eb6dae98 785 if (dsize < MD_RESERVED_SECTORS*512) {
4b1ac34b 786 if (devname)
f8409e54
NB
787 fprintf(stderr, Name
788 ": %s is too small for md: size is %llu sectors.\n",
789 devname, dsize);
4b1ac34b
NB
790 return 1;
791 }
353632d9 792
4b1ac34b
NB
793 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
794
795 offset *= 512;
796
797 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
798
799 if (lseek64(fd, offset, 0)< 0LL) {
800 if (devname)
801 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
802 devname, strerror(errno));
803 return 1;
804 }
805
8d75b7fc 806 super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
4b1ac34b
NB
807
808 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
809 if (devname)
810 fprintf(stderr, Name ": Cannot read superblock on %s\n",
811 devname);
812 free(super);
813 return 1;
814 }
815
586ed405
NB
816 if (st->ss && st->minor_version == 9)
817 super0_swap_endian(super);
818
4b1ac34b
NB
819 if (super->md_magic != MD_SB_MAGIC) {
820 if (devname)
821 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
822 devname, MD_SB_MAGIC, super->md_magic);
823 free(super);
824 return 2;
825 }
826
827 if (super->major_version != 0) {
828 if (devname)
829 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
830 devname, super->major_version);
831 free(super);
832 return 2;
833 }
64557c33 834 st->sb = super;
3da92f27 835
82d9eba6
NB
836 if (st->ss == NULL) {
837 st->ss = &super0;
5f98d3cb 838 st->minor_version = super->minor_version;
ea329559 839 st->max_devs = MD_SB_DISKS;
111d01fc 840 st->info = NULL;
82d9eba6
NB
841 }
842
14263cf1
NB
843 /* Now check on the bitmap superblock */
844 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
845 return 0;
846 /* Read the bitmap superblock and make sure it looks
847 * valid. If it doesn't clear the bit. An --assemble --force
848 * should get that written out.
849 */
850 if (read(fd, super+1, sizeof(struct bitmap_super_s))
851 != sizeof(struct bitmap_super_s))
852 goto no_bitmap;
853
3da92f27 854 uuid_from_super0(st, uuid);
14263cf1
NB
855 bsb = (struct bitmap_super_s *)(super+1);
856 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
857 memcmp(bsb->uuid, uuid, 16) != 0)
858 goto no_bitmap;
859 return 0;
860
861 no_bitmap:
862 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
863
4b1ac34b
NB
864 return 0;
865}
f9ce90ba 866
82d9eba6 867static struct supertype *match_metadata_desc0(char *arg)
f9ce90ba 868{
82d9eba6
NB
869 struct supertype *st = malloc(sizeof(*st));
870 if (!st) return st;
871
ef609477 872 memset(st, 0, sizeof(*st));
82d9eba6 873 st->ss = &super0;
111d01fc 874 st->info = NULL;
82d9eba6 875 st->minor_version = 90;
ea329559 876 st->max_devs = MD_SB_DISKS;
64557c33 877 st->sb = NULL;
f9ce90ba
NB
878 if (strcmp(arg, "0") == 0 ||
879 strcmp(arg, "0.90") == 0 ||
294d6f45
NB
880 strcmp(arg, "default") == 0 ||
881 strcmp(arg, "") == 0 /* no metadata */
f9ce90ba 882 )
82d9eba6
NB
883 return st;
884
56f8add2
NB
885 st->minor_version = 91; /* reshape in progress */
886 if (strcmp(arg, "0.91") == 0) /* For dup_super support */
887 return st;
888
586ed405 889 st->minor_version = 9; /* flag for 'byte-swapped' */
ff1f6545
NB
890 if (strcmp(arg, "0.swap")==0 ||
891 strcmp(arg, "0.9") == 0) /* For dup_super support */
586ed405
NB
892 return st;
893
82d9eba6
NB
894 free(st);
895 return NULL;
896}
897
1bf4e2d9 898static __u64 avail_size0(struct supertype *st, __u64 devsize)
82d9eba6 899{
eb6dae98 900 if (devsize < MD_RESERVED_SECTORS)
82d9eba6
NB
901 return 0ULL;
902 return MD_NEW_SIZE_SECTORS(devsize);
f9ce90ba
NB
903}
904
3da92f27 905static int add_internal_bitmap0(struct supertype *st, int *chunkp,
199171a2
NB
906 int delay, int write_behind,
907 unsigned long long size, int may_change,
908 int major)
55935d51
NB
909{
910 /*
911 * The bitmap comes immediately after the superblock and must be 60K in size
912 * at most. The default size is between 30K and 60K
913 *
8686f3ed 914 * size is in sectors, chunk is in bytes !!!
55935d51 915 */
1bf4e2d9 916 unsigned long long bits;
55935d51
NB
917 unsigned long long max_bits = 60*1024*8;
918 unsigned long long min_chunk;
199171a2 919 int chunk = *chunkp;
3da92f27 920 mdp_super_t *sb = st->sb;
55935d51
NB
921 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
922
353632d9 923
f277ce36 924 min_chunk = 4096; /* sub-page chunks don't work yet.. */
8686f3ed 925 bits = (size * 512) / min_chunk + 1;
55935d51
NB
926 while (bits > max_bits) {
927 min_chunk *= 2;
928 bits = (bits+1)/2;
929 }
930 if (chunk == UnSet)
931 chunk = min_chunk;
932 else if (chunk < min_chunk)
933 return 0; /* chunk size too small */
934
935 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
936
29e766a5 937 memset(bms, 0, sizeof(*bms));
dfd4d8ee 938 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 939 bms->version = __cpu_to_le32(major);
3da92f27 940 uuid_from_super0(st, (int*)bms->uuid);
dfd4d8ee
NB
941 bms->chunksize = __cpu_to_le32(chunk);
942 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 943 bms->sync_size = __cpu_to_le64(size);
dfd4d8ee 944 bms->write_behind = __cpu_to_le32(write_behind);
199171a2 945 *chunkp = chunk;
55935d51
NB
946 return 1;
947}
353632d9 948
55935d51 949
3da92f27 950void locate_bitmap0(struct supertype *st, int fd)
55935d51
NB
951{
952 unsigned long long dsize;
55935d51 953 unsigned long long offset;
55935d51 954
beae1dfe
NB
955 if (!get_dev_size(fd, NULL, &dsize))
956 return;
957
eb6dae98 958 if (dsize < MD_RESERVED_SECTORS*512)
55935d51 959 return;
353632d9 960
55935d51
NB
961 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
962
963 offset *= 512;
964
965 offset += MD_SB_BYTES;
966
967 lseek64(fd, offset, 0);
968}
969
3da92f27 970int write_bitmap0(struct supertype *st, int fd)
8d75b7fc 971{
8d75b7fc
NB
972 unsigned long long dsize;
973 unsigned long long offset;
3da92f27 974 mdp_super_t *sb = st->sb;
aba69144 975
8d75b7fc
NB
976 int rv = 0;
977
978 int towrite, n;
979 char buf[4096];
980
beae1dfe
NB
981 if (!get_dev_size(fd, NULL, &dsize))
982 return 1;
983
8d75b7fc 984
eb6dae98
NB
985 if (dsize < MD_RESERVED_SECTORS*512)
986 return -1;
353632d9 987
8d75b7fc
NB
988 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
989
990 offset *= 512;
991
992 if (lseek64(fd, offset + 4096, 0)< 0LL)
993 return 3;
994
995
1e0d770c
NB
996 if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
997 sizeof(bitmap_super_t))
998 return -2;
8d75b7fc
NB
999 towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
1000 memset(buf, 0xff, sizeof(buf));
1001 while (towrite > 0) {
1002 n = towrite;
aba69144 1003 if (n > sizeof(buf))
8d75b7fc
NB
1004 n = sizeof(buf);
1005 n = write(fd, buf, n);
1006 if (n > 0)
1007 towrite -= n;
1008 else
1009 break;
1010 }
dfd4d8ee 1011 fsync(fd);
8d75b7fc
NB
1012 if (towrite)
1013 rv = -2;
1014
1015 return rv;
1016}
55935d51 1017
3da92f27 1018static void free_super0(struct supertype *st)
df37ffc0 1019{
3da92f27
NB
1020 if (st->sb)
1021 free(st->sb);
1022 st->sb = NULL;
df37ffc0
NB
1023}
1024
17f25ca6
NB
1025static int validate_geometry0(struct supertype *st, int level,
1026 int layout, int raiddisks,
1027 int chunk, unsigned long long size,
1028 char *subdev, unsigned long long *freesize)
1029{
1030 unsigned long long ldsize;
1031 int fd;
1032
1033 if (level == LEVEL_CONTAINER)
1034 return 0;
1035 if (raiddisks > MD_SB_DISKS)
1036 return 0;
1037 if (size > (0x7fffffffULL<<10))
1038 return 0;
1039 if (!subdev)
1040 return 1;
1041
1042 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1043 if (fd < 0) {
1044 fprintf(stderr, Name ": Cannot open %s: %s\n",
1045 subdev, strerror(errno));
1046 return 0;
1047 }
1048 if (!get_dev_size(fd, subdev, &ldsize)) {
1049 close(fd);
1050 return 0;
1051 }
1052 close(fd);
1053
1054 if (ldsize < MD_RESERVED_SECTORS * 512)
1055 return 0;
1056 if (size > (0x7fffffffULL<<10))
1057 return 0;
1058 *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
1059 return 1;
1060}
1061
f9ce90ba 1062struct superswitch super0 = {
c7654afc 1063#ifndef MDASSEMBLE
f9ce90ba
NB
1064 .examine_super = examine_super0,
1065 .brief_examine_super = brief_examine_super0,
0d726f17 1066 .export_examine_super = export_examine_super0,
f9ce90ba
NB
1067 .detail_super = detail_super0,
1068 .brief_detail_super = brief_detail_super0,
0d726f17 1069 .export_detail_super = export_detail_super0,
111d01fc 1070 .write_init_super = write_init_super0,
c7654afc 1071#endif
83b6208e 1072 .match_home = match_home0,
f9ce90ba
NB
1073 .uuid_from_super = uuid_from_super0,
1074 .getinfo_super = getinfo_super0,
1075 .update_super = update_super0,
f9ce90ba
NB
1076 .init_super = init_super0,
1077 .add_to_super = add_to_super0,
1078 .store_super = store_super0,
f9ce90ba
NB
1079 .compare_super = compare_super0,
1080 .load_super = load_super0,
1081 .match_metadata_desc = match_metadata_desc0,
82d9eba6 1082 .avail_size = avail_size0,
55935d51
NB
1083 .add_internal_bitmap = add_internal_bitmap0,
1084 .locate_bitmap = locate_bitmap0,
8d75b7fc 1085 .write_bitmap = write_bitmap0,
df37ffc0 1086 .free_super = free_super0,
17f25ca6 1087 .validate_geometry = validate_geometry0,
82d9eba6 1088 .major = 0,
f277ce36 1089 .swapuuid = 0,
f9ce90ba 1090};