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