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