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