]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super1.c
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 528static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 529 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
530{
531 struct mdp_superblock_1 *sb = sbv;
892debc8
NB
532 struct mdp_superblock_1 *refsb = NULL;
533 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 534 int rfd;
82d9eba6
NB
535 int rv;
536
537 long size;
538 long long sb_offset;
539
540
541 if (fd < 0) {
542 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
543 devname);
544 return -1;
545 }
546
547 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 548 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
dfd4d8ee 549 sb->devflags |= WriteMostly1;
892debc8 550
dfe47e00
NB
551 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
552 read(rfd, sb->device_uuid, 16) != 16) {
553 *(__u32*)(sb->device_uuid) = random();
554 *(__u32*)(sb->device_uuid+4) = random();
555 *(__u32*)(sb->device_uuid+8) = random();
556 *(__u32*)(sb->device_uuid+12) = random();
557 }
558 if (rfd >= 0) close(rfd);
892debc8
NB
559 sb->events = 0;
560
561 if (load_super1(st, fd, (void**)&refsb, NULL)==0) {
562 memcpy(sb->device_uuid, refsb->device_uuid, 16);
563 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
564 /* same array, so preserve events and dev_number */
565 sb->events = refsb->events;
566 sb->dev_number = refsb->dev_number;
567 }
568 free(refsb);
569 }
34163fc7 570
024ce7fe
NB
571 if (ioctl(fd, BLKGETSIZE, &size)) {
572 close(fd);
82d9eba6 573 return 1;
024ce7fe 574 }
82d9eba6 575
024ce7fe
NB
576 if (size < 24) {
577 close(fd);
82d9eba6 578 return 2;
024ce7fe 579 }
82d9eba6
NB
580
581
582 /*
583 * Calculate the position of the superblock.
584 * It is always aligned to a 4K boundary and
585 * depending on minor_version, it can be:
586 * 0: At least 8K, but less than 12K, from end of device
587 * 1: At start of device
588 * 2: 4K from start of device.
589 */
590 switch(st->minor_version) {
591 case 0:
592 sb_offset = size;
593 sb_offset -= 8*2;
594 sb_offset &= ~(4*2-1);
595 sb->super_offset = __cpu_to_le64(sb_offset);
596 sb->data_offset = __cpu_to_le64(0);
1bf4e2d9 597 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
598 break;
599 case 1:
600 sb->super_offset = __cpu_to_le64(0);
1bf4e2d9
NB
601 sb->data_offset = __cpu_to_le64(4*2); /* leave 4k for super and bitmap */
602 sb->data_size = __cpu_to_le64(size - 4*2);
82d9eba6
NB
603 break;
604 case 2:
605 sb_offset = 4*2;
606 sb->super_offset = __cpu_to_le64(sb_offset);
1bf4e2d9
NB
607 sb->data_offset = __cpu_to_le64(sb_offset+4*2);
608 sb->data_size = __cpu_to_le64(size - 4*2 - 4*2);
82d9eba6
NB
609 break;
610 default:
611 return -EINVAL;
612 }
613
614
615 sb->sb_csum = calc_sb_1_csum(sb);
96395475 616 rv = store_super1(st, fd, sb);
82d9eba6
NB
617 if (rv)
618 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 619
1bf4e2d9
NB
620 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
621 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 622 close(fd);
82d9eba6
NB
623 return rv;
624}
625
626static int compare_super1(void **firstp, void *secondv)
627{
628 /*
629 * return:
630 * 0 same, or first was empty, and second was copied
631 * 1 second had wrong number
632 * 2 wrong uuid
633 * 3 wrong other info
634 */
635 struct mdp_superblock_1 *first = *firstp;
636 struct mdp_superblock_1 *second = secondv;
637
638 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
639 return 1;
640 if (second->major_version != __cpu_to_le32(1))
641 return 1;
642
643 if (!first) {
644 first = malloc(1024);
645 memcpy(first, second, 1024);
646 *firstp = first;
647 return 0;
648 }
649 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
650 return 2;
651
652 if (first->ctime != second->ctime ||
653 first->level != second->level ||
654 first->layout != second->layout ||
655 first->size != second->size ||
656 first->chunksize != second->chunksize ||
657 first->raid_disks != second->raid_disks)
658 return 3;
659 return 0;
660}
661
662static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
663{
664 unsigned long size;
665 unsigned long long sb_offset;
666 struct mdp_superblock_1 *super;
667
668
669
670 if (st->ss == NULL) {
570c0542
NB
671 int bestvers = -1;
672 __u64 bestctime = 0;
673 /* guess... choose latest ctime */
82d9eba6
NB
674 st->ss = &super1;
675 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
676 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
677 case 0: super = *sbp;
678 if (bestvers == -1 ||
679 bestctime < __le64_to_cpu(super->ctime)) {
680 bestvers = st->minor_version;
681 bestctime = __le64_to_cpu(super->ctime);
682 }
683 free(super);
684 *sbp = NULL;
685 break;
82d9eba6
NB
686 case 1: st->ss = NULL; return 1; /*bad device */
687 case 2: break; /* bad, try next */
688 }
689 }
570c0542
NB
690 if (bestvers != -1) {
691 int rv;
692 st->minor_version = bestvers;
693 st->ss = &super1;
ea329559 694 st->max_devs = 384;
570c0542
NB
695 rv = load_super1(st, fd, sbp, devname);
696 if (rv) st->ss = NULL;
697 return rv;
698 }
82d9eba6
NB
699 st->ss = NULL;
700 return 2;
701 }
702 if (ioctl(fd, BLKGETSIZE, &size)) {
34163fc7 703 if (devname)
82d9eba6
NB
704 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
705 devname, strerror(errno));
706 return 1;
707 }
708
709 if (size < 24) {
710 if (devname)
711 fprintf(stderr, Name ": %s is too small for md: size is %lu sectors.\n",
712 devname, size);
713 return 1;
714 }
715
716 /*
717 * Calculate the position of the superblock.
718 * It is always aligned to a 4K boundary and
719 * depeding on minor_version, it can be:
720 * 0: At least 8K, but less than 12K, from end of device
721 * 1: At start of device
722 * 2: 4K from start of device.
723 */
724 switch(st->minor_version) {
725 case 0:
726 sb_offset = size;
727 sb_offset -= 8*2;
728 sb_offset &= ~(4*2-1);
729 break;
730 case 1:
731 sb_offset = 0;
732 break;
733 case 2:
734 sb_offset = 4*2;
735 break;
736 default:
737 return -EINVAL;
738 }
739
740 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
741
742
743 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
744 if (devname)
745 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
746 devname, strerror(errno));
747 return 1;
748 }
749
34163fc7 750 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
751
752 if (read(fd, super, 1024) != 1024) {
753 if (devname)
754 fprintf(stderr, Name ": Cannot read superblock on %s\n",
755 devname);
756 free(super);
757 return 1;
758 }
759
760 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
761 if (devname)
762 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
763 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
764 free(super);
765 return 2;
766 }
767
768 if (__le32_to_cpu(super->major_version) != 1) {
769 if (devname)
770 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
771 devname, __le32_to_cpu(super->major_version));
772 free(super);
773 return 2;
774 }
775 if (__le64_to_cpu(super->super_offset) != sb_offset) {
776 if (devname)
777 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
778 devname);
779 free(super);
780 return 2;
781 }
782 *sbp = super;
783 return 0;
784}
785
786
787static struct supertype *match_metadata_desc1(char *arg)
788{
789 struct supertype *st = malloc(sizeof(*st));
790 if (!st) return st;
791
792 st->ss = &super1;
ea329559 793 st->max_devs = 384;
82d9eba6
NB
794 if (strcmp(arg, "1") == 0 ||
795 strcmp(arg, "1.0") == 0) {
796 st->minor_version = 0;
797 return st;
798 }
799 if (strcmp(arg, "1.1") == 0) {
800 st->minor_version = 1;
801 return st;
802 }
803 if (strcmp(arg, "1.2") == 0) {
804 st->minor_version = 2;
805 return st;
806 }
807
808 free(st);
809 return NULL;
810}
811
34163fc7
NB
812/* find available size on device with this devsize, using
813 * superblock type st, and reserving 'reserve' sectors for
814 * a possible bitmap
815 */
1bf4e2d9 816static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
817{
818 if (devsize < 24)
819 return 0;
820
34163fc7
NB
821 switch(st->minor_version) {
822 case 0:
1bf4e2d9
NB
823 /* at end */
824 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 825 case 1:
1bf4e2d9
NB
826 /* at start, 4K for superblock and possible bitmap */
827 return devsize - 4*2;
34163fc7 828 case 2:
1bf4e2d9
NB
829 /* 4k from start, 4K for superblock and possible bitmap */
830 return devsize - (4+4)*2;
34163fc7
NB
831 }
832 return 0;
833}
834
1bf4e2d9
NB
835static int
836add_internal_bitmap1(struct supertype *st, void *sbv,
837 int chunk, int delay, int write_behind, int *sizep, int may_change)
34163fc7
NB
838{
839 /*
1bf4e2d9
NB
840 * If not may_change, then this is a 'Grow', and the bitmap
841 * must fit after the superblock.
842 * If may_change, then this is create, and we can put the bitmap
843 * before the superblock if we like, or may move the start.
844 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7
NB
845 *
846 * size is in K, chunk is in bytes !!!
847 */
848
1bf4e2d9
NB
849 unsigned long long size = *sizep;
850 unsigned long long bits;
851 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
852 unsigned long long min_chunk;
853 struct mdp_superblock_1 *sb = sbv;
854 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
855
1bf4e2d9
NB
856 if (st->minor_version && !may_change &&
857 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
858 return 0; /* doesn't fit */
859
860
34163fc7
NB
861
862 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1bf4e2d9 863 bits = (size*1024)/min_chunk +1;
34163fc7
NB
864 while (bits > max_bits) {
865 min_chunk *= 2;
866 bits = (bits+1)/2;
867 }
868 if (chunk == UnSet)
869 chunk = min_chunk;
870 else if (chunk < min_chunk)
871 return 0; /* chunk size too small */
872
1bf4e2d9 873 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
874
875 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
876 memset(bms, sizeof(*bms), 0);
877 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
878 bms->version = __cpu_to_le32(BITMAP_MAJOR);
879 uuid_from_super1((int*)bms->uuid, sb);
880 bms->chunksize = __cpu_to_le32(chunk);
881 bms->daemon_sleep = __cpu_to_le32(delay);
1bf4e2d9 882 bms->sync_size = __cpu_to_le64(size<<1);
34163fc7
NB
883 bms->write_behind = __cpu_to_le32(write_behind);
884
34163fc7
NB
885 return 1;
886}
887
888
889void locate_bitmap1(struct supertype *st, int fd)
890{
34163fc7 891 unsigned long long offset;
1bf4e2d9 892 struct mdp_superblock_1 *sb;
34163fc7 893
1bf4e2d9
NB
894 if (st->ss->load_super(st, fd, (void**)&sb, NULL))
895 return; /* no error I hope... */
896 offset = __le64_to_cpu(sb->super_offset);
897 offset += (long) __le32_to_cpu(sb->bitmap_offset);
34163fc7 898
1bf4e2d9 899 lseek64(fd, offset<<9, 0);
34163fc7
NB
900}
901
902int write_bitmap1(struct supertype *st, int fd, void *sbv)
903{
904 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 905 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
906 int rv = 0;
907
908 int towrite, n;
909 char buf[4096];
910
911 locate_bitmap1(st, fd);
912
913 write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t));
1bf4e2d9
NB
914 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
915 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
916 memset(buf, 0xff, sizeof(buf));
917 while (towrite > 0) {
918 n = towrite;
919 if (n > sizeof(buf))
920 n = sizeof(buf);
921 n = write(fd, buf, n);
922 if (n > 0)
923 towrite -= n;
924 else
925 break;
926 }
927 fsync(fd);
928 if (towrite)
929 rv = -2;
930
931 return rv;
82d9eba6
NB
932}
933
934struct superswitch super1 = {
c7654afc 935#ifndef MDASSEMBLE
82d9eba6
NB
936 .examine_super = examine_super1,
937 .brief_examine_super = brief_examine_super1,
938 .detail_super = detail_super1,
939 .brief_detail_super = brief_detail_super1,
c7654afc 940#endif
82d9eba6
NB
941 .uuid_from_super = uuid_from_super1,
942 .getinfo_super = getinfo_super1,
943 .update_super = update_super1,
944 .event_super = event_super1,
945 .init_super = init_super1,
946 .add_to_super = add_to_super1,
947 .store_super = store_super1,
948 .write_init_super = write_init_super1,
949 .compare_super = compare_super1,
950 .load_super = load_super1,
951 .match_metadata_desc = match_metadata_desc1,
952 .avail_size = avail_size1,
34163fc7
NB
953 .add_internal_bitmap = add_internal_bitmap1,
954 .locate_bitmap = locate_bitmap1,
955 .write_bitmap = write_bitmap1,
82d9eba6 956 .major = 1,
f277ce36
NB
957#if __BYTE_ORDER == BIG_ENDIAN
958 .swapuuid = 0,
959#else
960 .swapuuid = 1,
961#endif
82d9eba6 962};