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