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