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