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