]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Passes all tests, nearly ready for release.
[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 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 }
213 faulty = 0;
214 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
215 int role = __le16_to_cpu(sb->dev_roles[i]);
216 if (role == 0xFFFE)
217 faulty++;
218 }
219 if (faulty) printf(" %d failed", faulty);
220 printf("\n");
221 }
222
223
224 static 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 }
237 if (sb->set_name[0])
238 printf(" name=%.32s", sb->set_name);
239 printf("\n");
240 }
241
242 static void detail_super1(void *sbv)
243 {
244 struct mdp_superblock_1 *sb = sbv;
245 int i;
246
247 printf(" Name : %.32s\n", sb->set_name);
248 printf(" UUID : ");
249 for (i=0; i<16; i++) {
250 if ((i&3)==0 && i != 0) printf(":");
251 printf("%02x", sb->set_uuid[i]);
252 }
253 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
254 }
255
256 static void brief_detail_super1(void *sbv)
257 {
258 struct mdp_superblock_1 *sb = sbv;
259 int i;
260
261 if (sb->set_name[0])
262 printf(" name=%.32s", sb->set_name);
263 printf(" UUID=");
264 for (i=0; i<16; i++) {
265 if ((i&3)==0 && i != 0) printf(":");
266 printf("%02x", sb->set_uuid[i]);
267 }
268 }
269
270 #endif
271
272 static 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
281 static void getinfo_super1(struct mdinfo *info, mddev_ident_t ident, void *sbv)
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;
298 info->disk.number = __le32_to_cpu(sb->dev_number);
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
321 strncpy(ident->name, sb->set_name, 32);
322 ident->name[32] = 0;
323
324 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
325 role = __le16_to_cpu(sb->dev_roles[i]);
326 if (/*role == 0xFFFF || */role < info->array.raid_disks)
327 working++;
328 }
329
330 info->array.working_disks = working;
331 }
332
333 static 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
384 static __u64 event_super1(void *sbv)
385 {
386 struct mdp_superblock_1 *sb = sbv;
387 return __le64_to_cpu(sb->events);
388 }
389
390 static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info, char *name)
391 {
392 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
393 int spares;
394 int rfd;
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;
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);
422
423 memset(sb->set_name, 0, 32);
424 strcpy(sb->set_name, name);
425
426 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
427 sb->level = __cpu_to_le32(info->level);
428 sb->layout = __cpu_to_le32(info->layout);
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);
440 if (info->state & (1<<MD_SB_CLEAN))
441 sb->resync_offset = ~0ULL;
442 else
443 sb->resync_offset = 0;
444 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
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 */
455 static 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;
459 if ((dk->state & 6) == 6) /* active, sync */
460 *rp = __cpu_to_le16(dk->raid_disk);
461 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
462 *rp = 0xffff;
463 else
464 *rp = 0xfffe;
465 }
466
467 static int store_super1(struct supertype *st, int fd, void *sbv)
468 {
469 struct mdp_superblock_1 *sb = sbv;
470 unsigned long long sb_offset;
471 int sbsize;
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:
496 sb_offset = 0;
497 break;
498 case 2:
499 sb_offset = 4*2;
500 break;
501 default:
502 return -EINVAL;
503 }
504
505
506
507 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
508 0 != __le64_to_cpu(sb->super_offset)
509 ) {
510 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
511 abort();
512 }
513
514 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
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
522 fsync(fd);
523 return 0;
524 }
525
526 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
527
528 static int write_init_super1(struct supertype *st, void *sbv,
529 mdu_disk_info_t *dinfo, char *devname)
530 {
531 struct mdp_superblock_1 *sb = sbv;
532 struct mdp_superblock_1 *refsb = NULL;
533 int fd = open(devname, O_RDWR | O_EXCL);
534 int rfd;
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);
548 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
549 sb->devflags |= WriteMostly1;
550
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);
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 }
570
571 if (ioctl(fd, BLKGETSIZE, &size)) {
572 close(fd);
573 return 1;
574 }
575
576 if (size < 24) {
577 close(fd);
578 return 2;
579 }
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);
597 sb->data_size = __cpu_to_le64(sb_offset);
598 break;
599 case 1:
600 sb->super_offset = __cpu_to_le64(0);
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);
603 break;
604 case 2:
605 sb_offset = 4*2;
606 sb->super_offset = __cpu_to_le64(sb_offset);
607 sb->data_offset = __cpu_to_le64(sb_offset+4*2);
608 sb->data_size = __cpu_to_le64(size - 4*2 - 4*2);
609 break;
610 default:
611 return -EINVAL;
612 }
613
614
615 sb->sb_csum = calc_sb_1_csum(sb);
616 rv = store_super1(st, fd, sb);
617 if (rv)
618 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
619
620 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
621 rv = st->ss->write_bitmap(st, fd, sbv);
622 close(fd);
623 return rv;
624 }
625
626 static 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
662 static 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) {
671 int bestvers = -1;
672 __u64 bestctime = 0;
673 /* guess... choose latest ctime */
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)) {
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;
686 case 1: st->ss = NULL; return 1; /*bad device */
687 case 2: break; /* bad, try next */
688 }
689 }
690 if (bestvers != -1) {
691 int rv;
692 st->minor_version = bestvers;
693 st->ss = &super1;
694 st->max_devs = 384;
695 rv = load_super1(st, fd, sbp, devname);
696 if (rv) st->ss = NULL;
697 return rv;
698 }
699 st->ss = NULL;
700 return 2;
701 }
702 if (ioctl(fd, BLKGETSIZE, &size)) {
703 if (devname)
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
750 super = malloc(1024 + sizeof(bitmap_super_t));
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
787 static 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;
793 st->max_devs = 384;
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
812 /* find available size on device with this devsize, using
813 * superblock type st, and reserving 'reserve' sectors for
814 * a possible bitmap
815 */
816 static __u64 avail_size1(struct supertype *st, __u64 devsize)
817 {
818 if (devsize < 24)
819 return 0;
820
821 switch(st->minor_version) {
822 case 0:
823 /* at end */
824 return ((devsize - 8*2 ) & ~(4*2-1));
825 case 1:
826 /* at start, 4K for superblock and possible bitmap */
827 return devsize - 4*2;
828 case 2:
829 /* 4k from start, 4K for superblock and possible bitmap */
830 return devsize - (4+4)*2;
831 }
832 return 0;
833 }
834
835 static int
836 add_internal_bitmap1(struct supertype *st, void *sbv,
837 int chunk, int delay, int write_behind, int *sizep, int may_change)
838 {
839 /*
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.
845 *
846 * size is in K, chunk is in bytes !!!
847 */
848
849 unsigned long long size = *sizep;
850 unsigned long long bits;
851 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
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
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
861
862 min_chunk = 4096; /* sub-page chunks don't work yet.. */
863 bits = (size*1024)/min_chunk +1;
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
873 sb->bitmap_offset = __cpu_to_le32(2);
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);
882 bms->sync_size = __cpu_to_le64(size<<1);
883 bms->write_behind = __cpu_to_le32(write_behind);
884
885 return 1;
886 }
887
888
889 void locate_bitmap1(struct supertype *st, int fd)
890 {
891 unsigned long long offset;
892 struct mdp_superblock_1 *sb;
893
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);
898
899 lseek64(fd, offset<<9, 0);
900 }
901
902 int write_bitmap1(struct supertype *st, int fd, void *sbv)
903 {
904 struct mdp_superblock_1 *sb = sbv;
905 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
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));
914 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
915 towrite = (towrite+7) >> 3; /* bits to bytes */
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;
932 }
933
934 struct superswitch super1 = {
935 #ifndef MDASSEMBLE
936 .examine_super = examine_super1,
937 .brief_examine_super = brief_examine_super1,
938 .detail_super = detail_super1,
939 .brief_detail_super = brief_detail_super1,
940 #endif
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,
953 .add_internal_bitmap = add_internal_bitmap1,
954 .locate_bitmap = locate_bitmap1,
955 .write_bitmap = write_bitmap1,
956 .major = 1,
957 #if __BYTE_ORDER == BIG_ENDIAN
958 .swapuuid = 0,
959 #else
960 .swapuuid = 1,
961 #endif
962 };