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