Provide error message if --examine does find a valid superblock
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
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
30#include "mdadm.h"
f277ce36 31#include <endian.h>
82d9eba6
NB
32#include "asm/byteorder.h"
33/*
34 * The version-1 superblock :
35 * All numeric fields are little-endian.
36 *
37 * total size: 256 bytes plus 2 per device.
38 * 1K allows 384 devices.
39 */
40struct mdp_superblock_1 {
41 /* constant array information - 128 bytes */
42 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
43 __u32 major_version; /* 1 */
44 __u32 feature_map; /* 0 for now */
45 __u32 pad0; /* always set to 0 when writing */
46
47 __u8 set_uuid[16]; /* user-space generated. */
48 char set_name[32]; /* set and interpreted by user-space */
49
50 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
51 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
52 __u32 layout; /* only for raid5 currently */
53 __u64 size; /* used size of component devices, in 512byte sectors */
54
55 __u32 chunksize; /* in 512byte sectors */
56 __u32 raid_disks;
34163fc7
NB
57 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
58 * NOTE: signed, so bitmap can be before superblock
59 * only meaningful of feature_map[0] is set.
60 */
61 __u8 pad1[128-100]; /* set to 0 when written */
82d9eba6
NB
62
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset; /* sector start of data, often 0 */
65 __u64 data_size; /* sectors in this device that can be used for data */
66 __u64 super_offset; /* sector start of this superblock */
67 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
dfd4d8ee
NB
71 __u8 devflags; /* per-device flags. Only one defined...*/
72#define WriteMostly1 1 /* mask for writemostly flag in above */
73 __u8 pad2[64-57]; /* set to 0 when writing */
82d9eba6
NB
74
75 /* array state information - 64 bytes */
76 __u64 utime; /* 40 bits second, 24 btes microseconds */
77 __u64 events; /* incremented when superblock updated */
78 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
79 __u32 sb_csum; /* checksum upto devs[max_dev] */
80 __u32 max_dev; /* size of devs[] array to consider */
81 __u8 pad3[64-32]; /* set to 0 when writing */
82
83 /* device state information. Indexed by dev_number.
84 * 2 bytes per device
85 * Note there are no per-device state flags. State information is rolled
86 * into the 'roles' value. If a device is spare or faulty, then it doesn't
87 * have a meaningful role.
88 */
89 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
90};
91
d2cd3ffc
NB
92/* feature_map bits */
93#define MD_FEATURE_BITMAP_OFFSET 1
94#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
95 * must be honoured
96 */
97
98#define MD_FEATURE_ALL (1|2)
99
570c0542 100#ifndef offsetof
82d9eba6 101#define offsetof(t,f) ((int)&(((t*)0)->f))
570c0542 102#endif
82d9eba6
NB
103static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
104{
105 unsigned int disk_csum, csum;
106 unsigned long long newcsum;
107 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
108 unsigned int *isuper = (unsigned int*)sb;
109 int i;
110
111/* make sure I can count... */
112 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
113 offsetof(struct mdp_superblock_1, utime) != 192 ||
114 sizeof(struct mdp_superblock_1) != 256) {
115 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
116 }
117
118 disk_csum = sb->sb_csum;
119 sb->sb_csum = 0;
120 newcsum = 0;
121 for (i=0; size>=4; size -= 4 )
122 newcsum += __le32_to_cpu(*isuper++);
123
124 if (size == 2)
125 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
126
127 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
128 sb->sb_csum = disk_csum;
129 return csum;
130}
131
c7654afc 132#ifndef MDASSEMBLE
82d9eba6
NB
133static void examine_super1(void *sbv)
134{
135 struct mdp_superblock_1 *sb = sbv;
136 time_t atime;
137 int d;
6fbba4c9 138 int faulty;
82d9eba6
NB
139 int i;
140 char *c;
141
142 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
143 printf(" Version : %02d.%02d\n", 1, __le32_to_cpu(sb->feature_map));
144 printf(" Array UUID : ");
145 for (i=0; i<16; i++) {
82d9eba6 146 if ((i&3)==0 && i != 0) printf(":");
34163fc7 147 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
148 }
149 printf("\n");
150 printf(" Name : %.32s\n", sb->set_name);
151
152 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
153 printf(" Creation Time : %.24s\n", ctime(&atime));
154 c=map_num(pers, __le32_to_cpu(sb->level));
155 printf(" Raid Level : %s\n", c?c:"-unknown-");
156 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
157 printf("\n");
570c0542 158 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
d2cd3ffc
NB
159 if (sb->size != sb->data_size)
160 printf(" Used Size : %llu%s\n", (unsigned long long)sb->size, human_size(sb->size<<9));
82d9eba6 161 if (sb->data_offset)
570c0542 162 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
82d9eba6 163 if (sb->super_offset)
570c0542 164 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
165 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
166 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 167 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
168 printf(" Device UUID : ");
169 for (i=0; i<16; i++) {
82d9eba6 170 if ((i&3)==0 && i != 0) printf(":");
34163fc7 171 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
172 }
173 printf("\n");
dfd4d8ee
NB
174 if (sb->devflags) {
175 printf(" Flags :");
34163fc7 176 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
177 printf(" write-mostly");
178 printf("\n");
179 }
82d9eba6
NB
180
181 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
182 printf(" Update Time : %.24s\n", ctime(&atime));
183
184 if (calc_sb_1_csum(sb) == sb->sb_csum)
185 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
186 else
187 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
188 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 189 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
190 printf("\n");
191 if (__le32_to_cpu(sb->level) == 5) {
192 c = map_num(r5layout, __le32_to_cpu(sb->layout));
193 printf(" Layout : %s\n", c?c:"-unknown-");
194 }
265e0f17
NB
195 if (__le32_to_cpu(sb->level) == 10) {
196 int lo = __le32_to_cpu(sb->layout);
197 printf(" Layout : near=%d, far=%d\n",
198 lo&255, (lo>>8)&255);
199 }
82d9eba6
NB
200 switch(__le32_to_cpu(sb->level)) {
201 case 0:
202 case 4:
203 case 5:
d2cd3ffc
NB
204 case 6:
205 case 10:
82d9eba6
NB
206 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize/2));
207 break;
208 case -1:
209 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize/2));
210 break;
211 default: break;
212 }
213 printf("\n");
214 printf(" Array State : ");
215 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
216 int cnt = 0;
217 int me = 0;
218 int i;
219 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
220 int role = __le16_to_cpu(sb->dev_roles[i]);
221 if (role == d) {
222 if (i == __le32_to_cpu(sb->dev_number))
223 me = 1;
224 cnt++;
225 }
226 }
227 if (cnt > 1) printf("?");
228 else if (cnt == 1 && me) printf("U");
229 else if (cnt == 1) printf("u");
230 else printf ("_");
231 }
6fbba4c9 232 faulty = 0;
82d9eba6
NB
233 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
234 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
235 if (role == 0xFFFE)
236 faulty++;
82d9eba6 237 }
82d9eba6
NB
238 if (faulty) printf(" %d failed", faulty);
239 printf("\n");
240}
241
242
243static void brief_examine_super1(void *sbv)
244{
245 struct mdp_superblock_1 *sb = sbv;
246 int i;
247
248 char *c=map_num(pers, __le32_to_cpu(sb->level));
249
250 printf("ARRAY /dev/?? level=%s metadata=1 num-devices=%d UUID=",
251 c?c:"-unknown-", sb->raid_disks);
252 for (i=0; i<16; i++) {
253 printf("%02x", sb->set_uuid[i]);
254 if ((i&3)==0 && i != 0) printf(":");
255 }
947fd4dd
NB
256 if (sb->set_name[0])
257 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
258 printf("\n");
259}
260
261static void detail_super1(void *sbv)
262{
263 struct mdp_superblock_1 *sb = sbv;
264 int i;
265
947fd4dd 266 printf(" Name : %.32s\n", sb->set_name);
82d9eba6
NB
267 printf(" UUID : ");
268 for (i=0; i<16; i++) {
82d9eba6 269 if ((i&3)==0 && i != 0) printf(":");
34163fc7 270 printf("%02x", sb->set_uuid[i]);
82d9eba6 271 }
570c0542 272 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
273}
274
275static void brief_detail_super1(void *sbv)
276{
277 struct mdp_superblock_1 *sb = sbv;
278 int i;
279
947fd4dd
NB
280 if (sb->set_name[0])
281 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
282 printf(" UUID=");
283 for (i=0; i<16; i++) {
82d9eba6 284 if ((i&3)==0 && i != 0) printf(":");
34163fc7 285 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
286 }
287}
288
c7654afc
NB
289#endif
290
82d9eba6
NB
291static void uuid_from_super1(int uuid[4], void * sbv)
292{
293 struct mdp_superblock_1 *super = sbv;
294 char *cuuid = (char*)uuid;
295 int i;
296 for (i=0; i<16; i++)
297 cuuid[i] = super->set_uuid[i];
298}
299
947fd4dd 300static void getinfo_super1(struct mdinfo *info, mddev_ident_t ident, void *sbv)
82d9eba6
NB
301{
302 struct mdp_superblock_1 *sb = sbv;
303 int working = 0;
304 int i;
305 int role;
306
307 info->array.major_version = 1;
308 info->array.minor_version = __le32_to_cpu(sb->feature_map);
309 info->array.patch_version = 0;
310 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
311 info->array.level = __le32_to_cpu(sb->level);
265e0f17 312 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
313 info->array.md_minor = -1;
314 info->array.ctime = __le64_to_cpu(sb->ctime);
315
316 info->disk.major = 0;
317 info->disk.minor = 0;
fbf8a0b7 318 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
319 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
320 __le32_to_cpu(sb->max_dev) > 512)
321 role = 0xfffe;
322 else
323 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
324
325 info->disk.raid_disk = -1;
326 switch(role) {
327 case 0xFFFF:
328 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
329 break;
330 case 0xFFFE:
331 info->disk.state = 1; /* faulty */
332 break;
333 default:
334 info->disk.state = 6; /* active and in sync */
335 info->disk.raid_disk = role;
336 }
337 info->events = __le64_to_cpu(sb->events);
338
339 memcpy(info->uuid, sb->set_uuid, 16);
340
947fd4dd
NB
341 strncpy(ident->name, sb->set_name, 32);
342 ident->name[32] = 0;
343
82d9eba6
NB
344 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
345 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 346 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
347 working++;
348 }
349
350 info->array.working_disks = working;
351}
352
353static int update_super1(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
354{
355 int rv = 0;
356 struct mdp_superblock_1 *sb = sbv;
357
358 if (strcmp(update, "force")==0) {
6d3d5804 359 sb->events = __cpu_to_le64(info->events);
82d9eba6
NB
360 switch(__le32_to_cpu(sb->level)) {
361 case 5: case 4: case 6:
362 /* need to force clean */
363 sb->resync_offset = ~0ULL;
364 }
365 }
366 if (strcmp(update, "assemble")==0) {
367 int d = info->disk.number;
368 int want;
369 if (info->disk.state == 6)
370 want = __cpu_to_le32(info->disk.raid_disk);
371 else
372 want = 0xFFFF;
373 if (sb->dev_roles[d] != want) {
374 sb->dev_roles[d] = want;
375 rv = 1;
376 }
377 }
378#if 0
379 if (strcmp(update, "newdev") == 0) {
380 int d = info->disk.number;
381 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
382 sb->disks[d].number = d;
383 sb->disks[d].major = info->disk.major;
384 sb->disks[d].minor = info->disk.minor;
385 sb->disks[d].raid_disk = info->disk.raid_disk;
386 sb->disks[d].state = info->disk.state;
387 sb->this_disk = sb->disks[d];
388 }
389#endif
390 if (strcmp(update, "grow") == 0) {
391 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
392 /* FIXME */
393 }
394 if (strcmp(update, "resync") == 0) {
395 /* make sure resync happens */
396 sb->resync_offset = ~0ULL;
397 }
7d99579f 398 if (strcmp(update, "uuid") == 0)
838acbc2 399 memcpy(sb->set_uuid, info->uuid, 16);
82d9eba6
NB
400
401 sb->sb_csum = calc_sb_1_csum(sb);
402 return rv;
403}
404
405
406static __u64 event_super1(void *sbv)
407{
408 struct mdp_superblock_1 *sb = sbv;
409 return __le64_to_cpu(sb->events);
410}
411
5dd497ee 412static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info, unsigned long long size, char *name)
82d9eba6 413{
34163fc7 414 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 415 int spares;
34163fc7 416 int rfd;
82d9eba6
NB
417 memset(sb, 0, 1024);
418
419 if (info->major_version == -1)
420 /* zeroing superblock */
421 return 0;
422
423 spares = info->working_disks - info->active_disks;
424 if (info->raid_disks + spares > 384) {
425 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
426 info->raid_disks , spares, 384);
427 return 0;
428 }
429
430
431 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
432 sb->major_version = __cpu_to_le32(1);
433 sb->feature_map = 0;
434 sb->pad0 = 0;
34163fc7
NB
435
436 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
437 read(rfd, sb->set_uuid, 16) != 16) {
438 *(__u32*)(sb->set_uuid) = random();
439 *(__u32*)(sb->set_uuid+4) = random();
440 *(__u32*)(sb->set_uuid+8) = random();
441 *(__u32*)(sb->set_uuid+12) = random();
442 }
443 if (rfd >= 0) close(rfd);
82d9eba6 444
947fd4dd
NB
445 memset(sb->set_name, 0, 32);
446 strcpy(sb->set_name, name);
82d9eba6
NB
447
448 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
449 sb->level = __cpu_to_le32(info->level);
ea329559 450 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 451 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
452 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
453 sb->raid_disks = __cpu_to_le32(info->raid_disks);
454
455 sb->data_offset = __cpu_to_le64(0);
456 sb->data_size = __cpu_to_le64(0);
457 sb->super_offset = __cpu_to_le64(0);
458 sb->recovery_offset = __cpu_to_le64(0);
459
460 sb->utime = sb->ctime;
461 sb->events = __cpu_to_le64(1);
34321279 462 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
463 sb->resync_offset = ~0ULL;
464 else
465 sb->resync_offset = 0;
34163fc7 466 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
467 sizeof(sb->dev_roles[0]));
468 memset(sb->pad3, 0, sizeof(sb->pad3));
469
470 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
471
472 *sbp = sb;
473 return 1;
474}
475
476/* Add a device to the superblock being created */
477static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
478{
479 struct mdp_superblock_1 *sb = sbv;
480 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 481 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 482 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 483 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 484 *rp = 0xffff;
34163fc7 485 else
82d9eba6
NB
486 *rp = 0xfffe;
487}
488
96395475 489static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
490{
491 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 492 unsigned long long sb_offset;
82d9eba6 493 int sbsize;
5dd497ee
NB
494 unsigned long size;
495 unsigned long long dsize;
96395475 496
5dd497ee
NB
497#ifdef BLKGETSIZE64
498 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
499#endif
500 {
501 if (ioctl(fd, BLKGETSIZE, &size))
502 return 1;
503 else
504 dsize = (unsigned long long)size;
505 } else
506 dsize >>= 9;
96395475 507
5dd497ee 508 if (dsize < 24)
96395475
NB
509 return 2;
510
511 /*
512 * Calculate the position of the superblock.
513 * It is always aligned to a 4K boundary and
514 * depending on minor_version, it can be:
515 * 0: At least 8K, but less than 12K, from end of device
516 * 1: At start of device
517 * 2: 4K from start of device.
518 */
519 switch(st->minor_version) {
520 case 0:
5dd497ee 521 sb_offset = dsize;
96395475
NB
522 sb_offset -= 8*2;
523 sb_offset &= ~(4*2-1);
524 break;
525 case 1:
6fbba4c9 526 sb_offset = 0;
96395475
NB
527 break;
528 case 2:
529 sb_offset = 4*2;
530 break;
531 default:
532 return -EINVAL;
533 }
534
82d9eba6 535
34163fc7 536
6fbba4c9
NB
537 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
538 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
539 ) {
540 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
541 abort();
542 }
82d9eba6 543
6fbba4c9 544 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
545 return 3;
546
547 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
548
549 if (write(fd, sb, sbsize) != sbsize)
550 return 4;
551
570c0542 552 fsync(fd);
82d9eba6
NB
553 return 0;
554}
555
892debc8
NB
556static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
557
34163fc7 558static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 559 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
560{
561 struct mdp_superblock_1 *sb = sbv;
e478dc86 562 void *refsbv = NULL;
892debc8 563 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 564 int rfd;
82d9eba6
NB
565 int rv;
566
5dd497ee
NB
567 unsigned long size;
568 unsigned long long dsize;
82d9eba6
NB
569 long long sb_offset;
570
571
572 if (fd < 0) {
573 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
574 devname);
575 return -1;
576 }
577
578 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 579 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
dfd4d8ee 580 sb->devflags |= WriteMostly1;
892debc8 581
dfe47e00
NB
582 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
583 read(rfd, sb->device_uuid, 16) != 16) {
584 *(__u32*)(sb->device_uuid) = random();
585 *(__u32*)(sb->device_uuid+4) = random();
586 *(__u32*)(sb->device_uuid+8) = random();
587 *(__u32*)(sb->device_uuid+12) = random();
588 }
589 if (rfd >= 0) close(rfd);
892debc8
NB
590 sb->events = 0;
591
e478dc86
NB
592 if (load_super1(st, fd, &refsbv, NULL)==0) {
593 struct mdp_superblock_1 *refsb = refsbv;
594
892debc8
NB
595 memcpy(sb->device_uuid, refsb->device_uuid, 16);
596 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
597 /* same array, so preserve events and dev_number */
598 sb->events = refsb->events;
599 sb->dev_number = refsb->dev_number;
600 }
601 free(refsb);
602 }
34163fc7 603
5dd497ee
NB
604#ifdef BLKGETSIZE64
605 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
606#endif
607 {
608 if (ioctl(fd, BLKGETSIZE, &size))
609 return 1;
610 else
611 dsize = size;
612 } else
613 dsize >>= 9;
82d9eba6 614
5dd497ee 615 if (dsize < 24) {
024ce7fe 616 close(fd);
82d9eba6 617 return 2;
024ce7fe 618 }
82d9eba6
NB
619
620
621 /*
622 * Calculate the position of the superblock.
623 * It is always aligned to a 4K boundary and
624 * depending on minor_version, it can be:
625 * 0: At least 8K, but less than 12K, from end of device
626 * 1: At start of device
627 * 2: 4K from start of device.
628 */
629 switch(st->minor_version) {
630 case 0:
5dd497ee 631 sb_offset = dsize;
82d9eba6
NB
632 sb_offset -= 8*2;
633 sb_offset &= ~(4*2-1);
634 sb->super_offset = __cpu_to_le64(sb_offset);
635 sb->data_offset = __cpu_to_le64(0);
1bf4e2d9 636 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
637 break;
638 case 1:
639 sb->super_offset = __cpu_to_le64(0);
1bf4e2d9 640 sb->data_offset = __cpu_to_le64(4*2); /* leave 4k for super and bitmap */
5dd497ee 641 sb->data_size = __cpu_to_le64(dsize - 4*2);
82d9eba6
NB
642 break;
643 case 2:
644 sb_offset = 4*2;
645 sb->super_offset = __cpu_to_le64(sb_offset);
1bf4e2d9 646 sb->data_offset = __cpu_to_le64(sb_offset+4*2);
5dd497ee 647 sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2);
82d9eba6
NB
648 break;
649 default:
650 return -EINVAL;
651 }
652
653
654 sb->sb_csum = calc_sb_1_csum(sb);
96395475 655 rv = store_super1(st, fd, sb);
82d9eba6
NB
656 if (rv)
657 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 658
1bf4e2d9
NB
659 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
660 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 661 close(fd);
82d9eba6
NB
662 return rv;
663}
664
665static int compare_super1(void **firstp, void *secondv)
666{
667 /*
668 * return:
669 * 0 same, or first was empty, and second was copied
670 * 1 second had wrong number
671 * 2 wrong uuid
672 * 3 wrong other info
673 */
674 struct mdp_superblock_1 *first = *firstp;
675 struct mdp_superblock_1 *second = secondv;
676
677 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
678 return 1;
679 if (second->major_version != __cpu_to_le32(1))
680 return 1;
681
682 if (!first) {
683 first = malloc(1024);
684 memcpy(first, second, 1024);
685 *firstp = first;
686 return 0;
687 }
688 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
689 return 2;
690
691 if (first->ctime != second->ctime ||
692 first->level != second->level ||
693 first->layout != second->layout ||
694 first->size != second->size ||
695 first->chunksize != second->chunksize ||
696 first->raid_disks != second->raid_disks)
697 return 3;
698 return 0;
699}
700
701static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
702{
703 unsigned long size;
5dd497ee 704 unsigned long long dsize;
82d9eba6
NB
705 unsigned long long sb_offset;
706 struct mdp_superblock_1 *super;
707
708
709
710 if (st->ss == NULL) {
570c0542
NB
711 int bestvers = -1;
712 __u64 bestctime = 0;
713 /* guess... choose latest ctime */
82d9eba6
NB
714 st->ss = &super1;
715 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
716 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
717 case 0: super = *sbp;
718 if (bestvers == -1 ||
719 bestctime < __le64_to_cpu(super->ctime)) {
720 bestvers = st->minor_version;
721 bestctime = __le64_to_cpu(super->ctime);
722 }
723 free(super);
724 *sbp = NULL;
725 break;
82d9eba6
NB
726 case 1: st->ss = NULL; return 1; /*bad device */
727 case 2: break; /* bad, try next */
728 }
729 }
570c0542
NB
730 if (bestvers != -1) {
731 int rv;
732 st->minor_version = bestvers;
733 st->ss = &super1;
ea329559 734 st->max_devs = 384;
570c0542
NB
735 rv = load_super1(st, fd, sbp, devname);
736 if (rv) st->ss = NULL;
737 return rv;
738 }
82d9eba6
NB
739 st->ss = NULL;
740 return 2;
741 }
5dd497ee
NB
742#ifdef BLKGETSIZE64
743 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
744#endif
745 {
746 if (ioctl(fd, BLKGETSIZE, &size)) {
747 if (devname)
748 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
749 devname, strerror(errno));
750 return 1;
751 }
752 dsize = size;
753 } else
754 dsize >>= 9;
82d9eba6 755
5dd497ee 756 if (dsize < 24) {
82d9eba6 757 if (devname)
5dd497ee
NB
758 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
759 devname, dsize);
82d9eba6
NB
760 return 1;
761 }
762
763 /*
764 * Calculate the position of the superblock.
765 * It is always aligned to a 4K boundary and
766 * depeding on minor_version, it can be:
767 * 0: At least 8K, but less than 12K, from end of device
768 * 1: At start of device
769 * 2: 4K from start of device.
770 */
771 switch(st->minor_version) {
772 case 0:
5dd497ee 773 sb_offset = dsize;
82d9eba6
NB
774 sb_offset -= 8*2;
775 sb_offset &= ~(4*2-1);
776 break;
777 case 1:
778 sb_offset = 0;
779 break;
780 case 2:
781 sb_offset = 4*2;
782 break;
783 default:
784 return -EINVAL;
785 }
786
787 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
788
789
790 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
791 if (devname)
792 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
793 devname, strerror(errno));
794 return 1;
795 }
796
34163fc7 797 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
798
799 if (read(fd, super, 1024) != 1024) {
800 if (devname)
801 fprintf(stderr, Name ": Cannot read superblock on %s\n",
802 devname);
803 free(super);
804 return 1;
805 }
806
807 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
808 if (devname)
809 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
810 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
811 free(super);
812 return 2;
813 }
814
815 if (__le32_to_cpu(super->major_version) != 1) {
816 if (devname)
817 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
818 devname, __le32_to_cpu(super->major_version));
819 free(super);
820 return 2;
821 }
822 if (__le64_to_cpu(super->super_offset) != sb_offset) {
823 if (devname)
824 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
825 devname);
826 free(super);
827 return 2;
828 }
829 *sbp = super;
830 return 0;
831}
832
833
834static struct supertype *match_metadata_desc1(char *arg)
835{
836 struct supertype *st = malloc(sizeof(*st));
837 if (!st) return st;
838
839 st->ss = &super1;
ea329559 840 st->max_devs = 384;
82d9eba6
NB
841 if (strcmp(arg, "1") == 0 ||
842 strcmp(arg, "1.0") == 0) {
843 st->minor_version = 0;
844 return st;
845 }
846 if (strcmp(arg, "1.1") == 0) {
847 st->minor_version = 1;
848 return st;
849 }
850 if (strcmp(arg, "1.2") == 0) {
851 st->minor_version = 2;
852 return st;
853 }
854
855 free(st);
856 return NULL;
857}
858
34163fc7
NB
859/* find available size on device with this devsize, using
860 * superblock type st, and reserving 'reserve' sectors for
861 * a possible bitmap
862 */
1bf4e2d9 863static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
864{
865 if (devsize < 24)
866 return 0;
867
34163fc7
NB
868 switch(st->minor_version) {
869 case 0:
1bf4e2d9
NB
870 /* at end */
871 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 872 case 1:
1bf4e2d9
NB
873 /* at start, 4K for superblock and possible bitmap */
874 return devsize - 4*2;
34163fc7 875 case 2:
1bf4e2d9
NB
876 /* 4k from start, 4K for superblock and possible bitmap */
877 return devsize - (4+4)*2;
34163fc7
NB
878 }
879 return 0;
880}
881
1bf4e2d9
NB
882static int
883add_internal_bitmap1(struct supertype *st, void *sbv,
f9c25f1d 884 int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major)
34163fc7
NB
885{
886 /*
1bf4e2d9
NB
887 * If not may_change, then this is a 'Grow', and the bitmap
888 * must fit after the superblock.
889 * If may_change, then this is create, and we can put the bitmap
890 * before the superblock if we like, or may move the start.
891 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7 892 *
f9c25f1d 893 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
894 */
895
1bf4e2d9
NB
896 unsigned long long bits;
897 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
898 unsigned long long min_chunk;
899 struct mdp_superblock_1 *sb = sbv;
900 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
901
1bf4e2d9
NB
902 if (st->minor_version && !may_change &&
903 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
904 return 0; /* doesn't fit */
905
906
34163fc7
NB
907
908 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 909 bits = (size*512)/min_chunk +1;
34163fc7
NB
910 while (bits > max_bits) {
911 min_chunk *= 2;
912 bits = (bits+1)/2;
913 }
914 if (chunk == UnSet)
915 chunk = min_chunk;
916 else if (chunk < min_chunk)
917 return 0; /* chunk size too small */
918
1bf4e2d9 919 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
920
921 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 922 memset(bms, 0, sizeof(*bms));
34163fc7 923 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 924 bms->version = __cpu_to_le32(major);
34163fc7
NB
925 uuid_from_super1((int*)bms->uuid, sb);
926 bms->chunksize = __cpu_to_le32(chunk);
927 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 928 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
929 bms->write_behind = __cpu_to_le32(write_behind);
930
34163fc7
NB
931 return 1;
932}
933
934
f6d75de8 935void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 936{
34163fc7 937 unsigned long long offset;
1bf4e2d9 938 struct mdp_superblock_1 *sb;
34163fc7 939
e478dc86
NB
940 if (!sbv)
941 if (st->ss->load_super(st, fd, sbv, NULL))
f6d75de8 942 return; /* no error I hope... */
e478dc86
NB
943
944 sb = sbv;
945
1bf4e2d9
NB
946 offset = __le64_to_cpu(sb->super_offset);
947 offset += (long) __le32_to_cpu(sb->bitmap_offset);
f6d75de8
NB
948 if (!sbv)
949 free(sb);
1bf4e2d9 950 lseek64(fd, offset<<9, 0);
34163fc7
NB
951}
952
953int write_bitmap1(struct supertype *st, int fd, void *sbv)
954{
955 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 956 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
957 int rv = 0;
958
959 int towrite, n;
960 char buf[4096];
961
f6d75de8 962 locate_bitmap1(st, fd, sbv);
34163fc7
NB
963
964 write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t));
1bf4e2d9
NB
965 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
966 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
967 memset(buf, 0xff, sizeof(buf));
968 while (towrite > 0) {
969 n = towrite;
970 if (n > sizeof(buf))
971 n = sizeof(buf);
972 n = write(fd, buf, n);
973 if (n > 0)
974 towrite -= n;
975 else
976 break;
977 }
978 fsync(fd);
979 if (towrite)
980 rv = -2;
981
982 return rv;
82d9eba6
NB
983}
984
985struct superswitch super1 = {
c7654afc 986#ifndef MDASSEMBLE
82d9eba6
NB
987 .examine_super = examine_super1,
988 .brief_examine_super = brief_examine_super1,
989 .detail_super = detail_super1,
990 .brief_detail_super = brief_detail_super1,
c7654afc 991#endif
82d9eba6
NB
992 .uuid_from_super = uuid_from_super1,
993 .getinfo_super = getinfo_super1,
994 .update_super = update_super1,
995 .event_super = event_super1,
996 .init_super = init_super1,
997 .add_to_super = add_to_super1,
998 .store_super = store_super1,
999 .write_init_super = write_init_super1,
1000 .compare_super = compare_super1,
1001 .load_super = load_super1,
1002 .match_metadata_desc = match_metadata_desc1,
1003 .avail_size = avail_size1,
34163fc7
NB
1004 .add_internal_bitmap = add_internal_bitmap1,
1005 .locate_bitmap = locate_bitmap1,
1006 .write_bitmap = write_bitmap1,
82d9eba6 1007 .major = 1,
f277ce36
NB
1008#if __BYTE_ORDER == BIG_ENDIAN
1009 .swapuuid = 0,
1010#else
1011 .swapuuid = 1,
1012#endif
82d9eba6 1013};