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