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