Improve reporting of layout for raid10.
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
82d9eba6
NB
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
bee8ec56
NB
97struct misc_dev_info {
98 __u64 device_size;
99};
100
d2cd3ffc
NB
101/* feature_map bits */
102#define MD_FEATURE_BITMAP_OFFSET 1
103#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
104 * must be honoured
105 */
b674b5b8 106#define MD_FEATURE_RESHAPE_ACTIVE 4
d2cd3ffc 107
b674b5b8 108#define MD_FEATURE_ALL (1|2|4)
d2cd3ffc 109
570c0542 110#ifndef offsetof
067db4df 111#define offsetof(t,f) ((size_t)&(((t*)0)->f))
570c0542 112#endif
82d9eba6
NB
113static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
114{
115 unsigned int disk_csum, csum;
116 unsigned long long newcsum;
117 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
118 unsigned int *isuper = (unsigned int*)sb;
119 int i;
120
121/* make sure I can count... */
122 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
123 offsetof(struct mdp_superblock_1, utime) != 192 ||
124 sizeof(struct mdp_superblock_1) != 256) {
125 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
126 }
127
128 disk_csum = sb->sb_csum;
129 sb->sb_csum = 0;
130 newcsum = 0;
722966c6
PC
131 for (i=0; size>=4; size -= 4 ) {
132 newcsum += __le32_to_cpu(*isuper);
133 isuper++;
134 }
82d9eba6
NB
135
136 if (size == 2)
137 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
138
139 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
140 sb->sb_csum = disk_csum;
83205b64 141 return __cpu_to_le32(csum);
82d9eba6
NB
142}
143
c7654afc 144#ifndef MDASSEMBLE
3da92f27 145static void examine_super1(struct supertype *st, char *homehost)
82d9eba6 146{
3da92f27 147 struct mdp_superblock_1 *sb = st->sb;
82d9eba6
NB
148 time_t atime;
149 int d;
6fbba4c9 150 int faulty;
82d9eba6
NB
151 int i;
152 char *c;
a1cbd7d0 153 int l = homehost ? strlen(homehost) : 0;
83205b64 154 int layout;
a17a3de3 155 unsigned long long sb_offset;
82d9eba6
NB
156
157 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
a17a3de3
DL
158 printf(" Version : 1");
159 sb_offset = __le64_to_cpu(sb->super_offset);
160 if (sb_offset <= 4)
161 printf(".1\n");
162 else if (sb_offset <= 8)
163 printf(".2\n");
164 else
165 printf(".0\n");
91eedefc 166 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
167 printf(" Array UUID : ");
168 for (i=0; i<16; i++) {
82d9eba6 169 if ((i&3)==0 && i != 0) printf(":");
34163fc7 170 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
171 }
172 printf("\n");
a1cbd7d0
NB
173 printf(" Name : %.32s", sb->set_name);
174 if (l > 0 && l < 32 &&
175 sb->set_name[l] == ':' &&
176 strncmp(sb->set_name, homehost, l) == 0)
177 printf(" (local to host %s)", homehost);
178 printf("\n");
82d9eba6
NB
179 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
180 printf(" Creation Time : %.24s\n", ctime(&atime));
181 c=map_num(pers, __le32_to_cpu(sb->level));
182 printf(" Raid Level : %s\n", c?c:"-unknown-");
183 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
184 printf("\n");
5cda0964 185 printf(" Avail Dev Size : %llu%s\n",
2fb749d1
NB
186 (unsigned long long)__le64_to_cpu(sb->data_size),
187 human_size(__le64_to_cpu(sb->data_size)<<9));
b674b5b8
NB
188 if (__le32_to_cpu(sb->level) >= 0) {
189 int ddsks=0;
190 switch(__le32_to_cpu(sb->level)) {
191 case 1: ddsks=1;break;
192 case 4:
83205b64
NB
193 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
194 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
195 case 10:
196 layout = __le32_to_cpu(sb->layout);
197 ddsks = __le32_to_cpu(sb->raid_disks)
198 / (layout&255) / ((layout>>8)&255);
b674b5b8
NB
199 }
200 if (ddsks)
83205b64 201 printf(" Array Size : %llu%s\n",
c1c05f7f 202 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
83205b64 203 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
b674b5b8 204 if (sb->size != sb->data_size)
5cda0964 205 printf(" Used Dev Size : %llu%s\n",
83205b64
NB
206 (unsigned long long)__le64_to_cpu(sb->size),
207 human_size(__le64_to_cpu(sb->size)<<9));
b674b5b8 208 }
82d9eba6 209 if (sb->data_offset)
199171a2
NB
210 printf(" Data Offset : %llu sectors\n",
211 (unsigned long long)__le64_to_cpu(sb->data_offset));
212 printf(" Super Offset : %llu sectors\n",
213 (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
214 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
215 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 216 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
217 printf(" Device UUID : ");
218 for (i=0; i<16; i++) {
82d9eba6 219 if ((i&3)==0 && i != 0) printf(":");
34163fc7 220 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
221 }
222 printf("\n");
b674b5b8 223 printf("\n");
91eedefc
NB
224 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
225 printf("Internal Bitmap : %ld sectors from superblock\n",
68754bd1 226 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
8fac0577 227 }
b674b5b8 228 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 229 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
230 human_size(__le64_to_cpu(sb->reshape_position)<<9));
231 if (__le32_to_cpu(sb->delta_disks)) {
232 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
233 if (__le32_to_cpu(sb->delta_disks))
234 printf(" (%d->%d)\n",
235 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
236 __le32_to_cpu(sb->raid_disks));
237 else
238 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
239 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
240 }
241 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
242 c = map_num(pers, __le32_to_cpu(sb->new_level));
243 printf(" New Level : %s\n", c?c:"-unknown-");
244 }
245 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
246 if (__le32_to_cpu(sb->level) == 5) {
247 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
248 printf(" New Layout : %s\n", c?c:"-unknown-");
249 }
250 if (__le32_to_cpu(sb->level) == 10) {
e4965ef8
N
251 printf(" New Layout :");
252 print_r10_layout(__le32_to_cpu(sb->new_layout));
253 printf("\n");
b674b5b8
NB
254 }
255 }
256 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
257 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
258 printf("\n");
259 }
dfd4d8ee
NB
260 if (sb->devflags) {
261 printf(" Flags :");
34163fc7 262 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
263 printf(" write-mostly");
264 printf("\n");
265 }
82d9eba6
NB
266
267 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
268 printf(" Update Time : %.24s\n", ctime(&atime));
269
270 if (calc_sb_1_csum(sb) == sb->sb_csum)
271 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
272 else
273 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
274 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 275 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
276 printf("\n");
277 if (__le32_to_cpu(sb->level) == 5) {
278 c = map_num(r5layout, __le32_to_cpu(sb->layout));
279 printf(" Layout : %s\n", c?c:"-unknown-");
280 }
265e0f17
NB
281 if (__le32_to_cpu(sb->level) == 10) {
282 int lo = __le32_to_cpu(sb->layout);
e4965ef8
N
283 printf(" Layout :");
284 print_r10_layout(lo);
285 printf("\n");
265e0f17 286 }
82d9eba6
NB
287 switch(__le32_to_cpu(sb->level)) {
288 case 0:
289 case 4:
290 case 5:
d2cd3ffc
NB
291 case 6:
292 case 10:
b674b5b8 293 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
294 break;
295 case -1:
b674b5b8 296 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
297 break;
298 default: break;
299 }
300 printf("\n");
c3684618
NB
301 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
302 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
303 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
304 break;
305 for (d=0; d < i; d++) {
306 int role = __le16_to_cpu(sb->dev_roles[d]);
307 if (d) printf(", ");
308 if (role == 0xffff) printf("empty");
309 else if(role == 0xfffe) printf("failed");
310 else printf("%d", role);
311 }
312 printf(")\n");
82d9eba6
NB
313 printf(" Array State : ");
314 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
315 int cnt = 0;
316 int me = 0;
317 int i;
318 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
319 int role = __le16_to_cpu(sb->dev_roles[i]);
320 if (role == d) {
321 if (i == __le32_to_cpu(sb->dev_number))
322 me = 1;
323 cnt++;
324 }
325 }
326 if (cnt > 1) printf("?");
327 else if (cnt == 1 && me) printf("U");
328 else if (cnt == 1) printf("u");
329 else printf ("_");
330 }
6fbba4c9 331 faulty = 0;
82d9eba6
NB
332 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
333 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
334 if (role == 0xFFFE)
335 faulty++;
82d9eba6 336 }
82d9eba6
NB
337 if (faulty) printf(" %d failed", faulty);
338 printf("\n");
339}
340
341
3da92f27 342static void brief_examine_super1(struct supertype *st)
82d9eba6 343{
3da92f27 344 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 345 int i;
a17a3de3 346 unsigned long long sb_offset;
2998bc01 347 char *nm;
b015e6c2 348 char *c=map_num(pers, __le32_to_cpu(sb->level));
2998bc01
NB
349
350 nm = strchr(sb->set_name, ':');
351 if (nm)
352 nm++;
353 else if (sb->set_name[0])
354 nm = sb->set_name;
355 else
356 nm = "??";
82d9eba6 357
a17a3de3
DL
358 printf("ARRAY /dev/md/%s level=%s ", nm, c?c:"-unknown-");
359 sb_offset = __le64_to_cpu(sb->super_offset);
360 if (sb_offset <= 4)
361 printf("metadata=1.1 ");
362 else if (sb_offset <= 8)
363 printf("metadata=1.2 ");
364 else
365 printf("metadata=1.0 ");
366 printf("num-devices=%d UUID=", __le32_to_cpu(sb->raid_disks));
82d9eba6 367 for (i=0; i<16; i++) {
82d9eba6 368 if ((i&3)==0 && i != 0) printf(":");
b7a708af 369 printf("%02x", sb->set_uuid[i]);
82d9eba6 370 }
947fd4dd
NB
371 if (sb->set_name[0])
372 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
373 printf("\n");
374}
375
0d726f17
KS
376static void export_examine_super1(struct supertype *st)
377{
378 struct mdp_superblock_1 *sb = st->sb;
379 int i;
380 int len = 32;
381
382 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
383 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
384 for (i=0; i<32; i++)
385 if (sb->set_name[i] == '\n' ||
386 sb->set_name[i] == '\0') {
387 len = i;
388 break;
389 }
390 if (len)
391 printf("MD_NAME=%.*s\n", len, sb->set_name);
392 printf("MD_UUID=");
393 for (i=0; i<16; i++) {
394 if ((i&3)==0 && i != 0) printf(":");
395 printf("%02x", sb->set_uuid[i]);
396 }
397 printf("\n");
398 printf("MD_UPDATE_TIME=%llu\n",
399 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
400 printf("MD_DEV_UUID=");
401 for (i=0; i<16; i++) {
402 if ((i&3)==0 && i != 0) printf(":");
403 printf("%02x", sb->device_uuid[i]);
404 }
405 printf("\n");
406 printf("MD_EVENTS=%llu\n",
407 (unsigned long long)__le64_to_cpu(sb->events));
408}
409
3da92f27 410static void detail_super1(struct supertype *st, char *homehost)
82d9eba6 411{
3da92f27 412 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 413 int i;
b6750aa8 414 int l = homehost ? strlen(homehost) : 0;
82d9eba6 415
b6750aa8
NB
416 printf(" Name : %.32s", sb->set_name);
417 if (l > 0 && l < 32 &&
418 sb->set_name[l] == ':' &&
419 strncmp(sb->set_name, homehost, l) == 0)
420 printf(" (local to host %s)", homehost);
421 printf("\n UUID : ");
82d9eba6 422 for (i=0; i<16; i++) {
82d9eba6 423 if ((i&3)==0 && i != 0) printf(":");
34163fc7 424 printf("%02x", sb->set_uuid[i]);
82d9eba6 425 }
570c0542 426 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
427}
428
3da92f27 429static void brief_detail_super1(struct supertype *st)
82d9eba6 430{
3da92f27 431 struct mdp_superblock_1 *sb = st->sb;
82d9eba6
NB
432 int i;
433
947fd4dd
NB
434 if (sb->set_name[0])
435 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
436 printf(" UUID=");
437 for (i=0; i<16; i++) {
82d9eba6 438 if ((i&3)==0 && i != 0) printf(":");
34163fc7 439 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
440 }
441}
442
0d726f17 443static void export_detail_super1(struct supertype *st)
54bad364 444{
3da92f27 445 struct mdp_superblock_1 *sb = st->sb;
54bad364
KS
446 int i;
447 int len = 32;
448
449 for (i=0; i<32; i++)
450 if (sb->set_name[i] == '\n' ||
451 sb->set_name[i] == '\0') {
452 len = i;
453 break;
454 }
455 if (len)
456 printf("MD_NAME=%.*s\n", len, sb->set_name);
457 printf("MD_UUID=");
458 for (i=0; i<16; i++) {
459 if ((i&3)==0 && i != 0) printf(":");
460 printf("%02x", sb->set_uuid[i]);
461 }
462 printf("\n");
463}
464
c7654afc
NB
465#endif
466
3da92f27 467static int match_home1(struct supertype *st, char *homehost)
83b6208e 468{
3da92f27 469 struct mdp_superblock_1 *sb = st->sb;
83b6208e
NB
470 int l = homehost ? strlen(homehost) : 0;
471
472 return (l > 0 && l < 32 &&
473 sb->set_name[l] == ':' &&
474 strncmp(sb->set_name, homehost, l) == 0);
475}
476
3da92f27 477static void uuid_from_super1(struct supertype *st, int uuid[4])
82d9eba6 478{
3da92f27 479 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
480 char *cuuid = (char*)uuid;
481 int i;
482 for (i=0; i<16; i++)
483 cuuid[i] = super->set_uuid[i];
484}
485
3da92f27 486static void getinfo_super1(struct supertype *st, struct mdinfo *info)
82d9eba6 487{
3da92f27 488 struct mdp_superblock_1 *sb = st->sb;
82d9eba6
NB
489 int working = 0;
490 int i;
491 int role;
492
493 info->array.major_version = 1;
494 info->array.minor_version = __le32_to_cpu(sb->feature_map);
495 info->array.patch_version = 0;
496 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
497 info->array.level = __le32_to_cpu(sb->level);
265e0f17 498 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
499 info->array.md_minor = -1;
500 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 501 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 502 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
4855f95c
NB
503 info->array.state =
504 (__le64_to_cpu(sb->resync_offset) >= __le64_to_cpu(sb->size))
505 ? 1 : 0;
353632d9
NB
506
507 info->data_offset = __le64_to_cpu(sb->data_offset);
508 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
509
510 info->disk.major = 0;
511 info->disk.minor = 0;
fbf8a0b7 512 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
513 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
514 __le32_to_cpu(sb->max_dev) > 512)
515 role = 0xfffe;
516 else
517 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
518
519 info->disk.raid_disk = -1;
520 switch(role) {
521 case 0xFFFF:
522 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
523 break;
524 case 0xFFFE:
525 info->disk.state = 1; /* faulty */
526 break;
527 default:
528 info->disk.state = 6; /* active and in sync */
529 info->disk.raid_disk = role;
530 }
531 info->events = __le64_to_cpu(sb->events);
532
533 memcpy(info->uuid, sb->set_uuid, 16);
534
31317663
NB
535 strncpy(info->name, sb->set_name, 32);
536 info->name[32] = 0;
947fd4dd 537
353632d9
NB
538 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
539 info->reshape_active = 1;
540 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
541 info->new_level = __le32_to_cpu(sb->new_level);
542 info->delta_disks = __le32_to_cpu(sb->delta_disks);
543 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 544 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
353632d9
NB
545 } else
546 info->reshape_active = 0;
547
82d9eba6
NB
548 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
549 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 550 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
551 working++;
552 }
553
554 info->array.working_disks = working;
555}
556
68c7d6d7 557static int update_super1(struct supertype *st, struct mdinfo *info,
3da92f27 558 char *update,
e5eac01f
NB
559 char *devname, int verbose,
560 int uuid_set, char *homehost)
82d9eba6 561{
c4d831e1
NB
562 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
563 * For others, the return value is ignored.
564 */
82d9eba6 565 int rv = 0;
3da92f27 566 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 567
67a8c82d
NB
568 if (strcmp(update, "force-one")==0) {
569 /* Not enough devices for a working array,
570 * so bring this one up-to-date
571 */
c4d831e1
NB
572 if (sb->events != __cpu_to_le64(info->events))
573 rv = 1;
6d3d5804 574 sb->events = __cpu_to_le64(info->events);
67a8c82d
NB
575 }
576 if (strcmp(update, "force-array")==0) {
577 /* Degraded array and 'force' requests to
578 * maybe need to mark it 'clean'.
579 */
82d9eba6
NB
580 switch(__le32_to_cpu(sb->level)) {
581 case 5: case 4: case 6:
582 /* need to force clean */
c4d831e1
NB
583 if (sb->resync_offset != ~0ULL)
584 rv = 1;
82d9eba6
NB
585 sb->resync_offset = ~0ULL;
586 }
587 }
588 if (strcmp(update, "assemble")==0) {
589 int d = info->disk.number;
590 int want;
591 if (info->disk.state == 6)
592 want = __cpu_to_le32(info->disk.raid_disk);
593 else
594 want = 0xFFFF;
595 if (sb->dev_roles[d] != want) {
596 sb->dev_roles[d] = want;
597 rv = 1;
598 }
599 }
f752781f
NB
600 if (strcmp(update, "linear-grow-new") == 0) {
601 int i;
602 int rfd;
603 int max = __le32_to_cpu(sb->max_dev);
604
605 for (i=0 ; i < max ; i++)
606 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
607 break;
608 sb->dev_number = __cpu_to_le32(i);
609 info->disk.number = i;
610 if (max >= __le32_to_cpu(sb->max_dev))
611 sb->max_dev = __cpu_to_le32(max+1);
612
613 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
614 read(rfd, sb->device_uuid, 16) != 16) {
615 *(__u32*)(sb->device_uuid) = random();
616 *(__u32*)(sb->device_uuid+4) = random();
617 *(__u32*)(sb->device_uuid+8) = random();
618 *(__u32*)(sb->device_uuid+12) = random();
619 }
620
621 sb->dev_roles[i] =
622 __cpu_to_le16(info->disk.raid_disk);
623 }
624 if (strcmp(update, "linear-grow-update") == 0) {
82d9eba6 625 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
f752781f
NB
626 sb->dev_roles[info->disk.number] =
627 __cpu_to_le16(info->disk.raid_disk);
82d9eba6
NB
628 }
629 if (strcmp(update, "resync") == 0) {
630 /* make sure resync happens */
434b7755 631 sb->resync_offset = 0ULL;
82d9eba6 632 }
14263cf1 633 if (strcmp(update, "uuid") == 0) {
350f29f9 634 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
bf4fb153 635
14263cf1
NB
636 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
637 struct bitmap_super_s *bm;
3da92f27 638 bm = (struct bitmap_super_s*)(st->sb+1024);
bf4fb153 639 memcpy(bm->uuid, sb->set_uuid, 16);
14263cf1
NB
640 }
641 }
0237e0ca
NB
642 if (strcmp(update, "homehost") == 0 &&
643 homehost) {
644 char *c;
645 update = "name";
646 c = strchr(sb->set_name, ':');
647 if (c)
648 strncpy(info->name, c+1, 31 - (c-sb->set_name));
649 else
650 strncpy(info->name, sb->set_name, 32);
26a0b8fd 651 info->name[32] = 0;
0237e0ca 652 }
c4f12c13
NB
653 if (strcmp(update, "name") == 0) {
654 if (info->name[0] == 0)
655 sprintf(info->name, "%d", info->array.md_minor);
656 memset(sb->set_name, 0, sizeof(sb->set_name));
657 if (homehost &&
658 strchr(info->name, ':') == NULL &&
659 strlen(homehost)+1+strlen(info->name) < 32) {
660 strcpy(sb->set_name, homehost);
661 strcat(sb->set_name, ":");
662 strcat(sb->set_name, info->name);
663 } else
664 strcpy(sb->set_name, info->name);
665 }
bee8ec56
NB
666 if (strcmp(update, "devicesize") == 0 &&
667 __le64_to_cpu(sb->super_offset) <
668 __le64_to_cpu(sb->data_offset)) {
669 /* set data_size to device size less data_offset */
670 struct misc_dev_info *misc = (struct misc_dev_info*)
3da92f27 671 (st->sb + 1024 + sizeof(struct bitmap_super_s));
eb9199fb
NB
672 printf("Size was %llu\n", (unsigned long long)
673 __le64_to_cpu(sb->data_size));
bee8ec56
NB
674 sb->data_size = __cpu_to_le64(
675 misc->device_size - __le64_to_cpu(sb->data_offset));
eb9199fb
NB
676 printf("Size is %llu\n", (unsigned long long)
677 __le64_to_cpu(sb->data_size));
bee8ec56 678 }
353632d9
NB
679 if (strcmp(update, "_reshape_progress")==0)
680 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
681
682 sb->sb_csum = calc_sb_1_csum(sb);
683 return rv;
684}
685
3da92f27 686static int init_super1(struct supertype *st, mdu_array_info_t *info,
3d3dd91e 687 unsigned long long size, char *name, char *homehost, int *uuid)
82d9eba6 688{
bee8ec56
NB
689 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t) +
690 sizeof(struct misc_dev_info));
82d9eba6 691 int spares;
34163fc7 692 int rfd;
05697ec1 693 char defname[10];
82d9eba6
NB
694 memset(sb, 0, 1024);
695
64557c33 696 st->sb = sb;
05697ec1 697 if (info->major_version == -1) {
82d9eba6
NB
698 /* zeroing superblock */
699 return 0;
05697ec1 700 }
82d9eba6
NB
701
702 spares = info->working_disks - info->active_disks;
703 if (info->raid_disks + spares > 384) {
704 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
705 info->raid_disks , spares, 384);
706 return 0;
707 }
708
82d9eba6
NB
709 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
710 sb->major_version = __cpu_to_le32(1);
711 sb->feature_map = 0;
712 sb->pad0 = 0;
34163fc7 713
350f29f9
NB
714 if (uuid)
715 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
716 else {
3d3dd91e
NB
717 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
718 read(rfd, sb->set_uuid, 16) != 16) {
719 *(__u32*)(sb->set_uuid) = random();
720 *(__u32*)(sb->set_uuid+4) = random();
721 *(__u32*)(sb->set_uuid+8) = random();
722 *(__u32*)(sb->set_uuid+12) = random();
723 }
724 if (rfd >= 0) close(rfd);
34163fc7 725 }
82d9eba6 726
05697ec1
NB
727 if (name == NULL || *name == 0) {
728 sprintf(defname, "%d", info->md_minor);
729 name = defname;
730 }
947fd4dd 731 memset(sb->set_name, 0, 32);
05697ec1
NB
732 if (homehost &&
733 strchr(name, ':')== NULL &&
734 strlen(homehost)+1+strlen(name) < 32) {
735 strcpy(sb->set_name, homehost);
736 strcat(sb->set_name, ":");
737 strcat(sb->set_name, name);
738 } else
739 strcpy(sb->set_name, name);
82d9eba6
NB
740
741 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
742 sb->level = __cpu_to_le32(info->level);
ea329559 743 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 744 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
745 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
746 sb->raid_disks = __cpu_to_le32(info->raid_disks);
747
748 sb->data_offset = __cpu_to_le64(0);
749 sb->data_size = __cpu_to_le64(0);
750 sb->super_offset = __cpu_to_le64(0);
751 sb->recovery_offset = __cpu_to_le64(0);
752
753 sb->utime = sb->ctime;
754 sb->events = __cpu_to_le64(1);
34321279 755 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
756 sb->resync_offset = ~0ULL;
757 else
758 sb->resync_offset = 0;
34163fc7 759 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
760 sizeof(sb->dev_roles[0]));
761 memset(sb->pad3, 0, sizeof(sb->pad3));
762
763 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
764
82d9eba6
NB
765 return 1;
766}
767
768/* Add a device to the superblock being created */
3da92f27 769static void add_to_super1(struct supertype *st, mdu_disk_info_t *dk)
82d9eba6 770{
3da92f27 771 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 772 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 773 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 774 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 775 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 776 *rp = 0xffff;
34163fc7 777 else
82d9eba6
NB
778 *rp = 0xfffe;
779}
780
3da92f27 781static void locate_bitmap1(struct supertype *st, int fd);
14263cf1 782
3da92f27 783static int store_super1(struct supertype *st, int fd)
82d9eba6 784{
3da92f27 785 struct mdp_superblock_1 *sb = st->sb;
6fbba4c9 786 unsigned long long sb_offset;
82d9eba6 787 int sbsize;
5dd497ee 788 unsigned long long dsize;
96395475 789
beae1dfe
NB
790 if (!get_dev_size(fd, NULL, &dsize))
791 return 1;
792
793 dsize >>= 9;
96395475 794
5dd497ee 795 if (dsize < 24)
96395475
NB
796 return 2;
797
798 /*
799 * Calculate the position of the superblock.
800 * It is always aligned to a 4K boundary and
801 * depending on minor_version, it can be:
802 * 0: At least 8K, but less than 12K, from end of device
803 * 1: At start of device
804 * 2: 4K from start of device.
805 */
806 switch(st->minor_version) {
807 case 0:
5dd497ee 808 sb_offset = dsize;
96395475
NB
809 sb_offset -= 8*2;
810 sb_offset &= ~(4*2-1);
811 break;
812 case 1:
6fbba4c9 813 sb_offset = 0;
96395475
NB
814 break;
815 case 2:
816 sb_offset = 4*2;
817 break;
818 default:
819 return -EINVAL;
820 }
821
82d9eba6 822
34163fc7 823
6fbba4c9
NB
824 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
825 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
826 ) {
827 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
828 abort();
829 }
82d9eba6 830
6fbba4c9 831 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
832 return 3;
833
834 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
835
836 if (write(fd, sb, sbsize) != sbsize)
837 return 4;
838
14263cf1
NB
839 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
840 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 841 (((char*)sb)+1024);
14263cf1 842 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
3da92f27 843 locate_bitmap1(st, fd);
9fca7d62
NB
844 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
845 return 5;
14263cf1
NB
846 }
847 }
570c0542 848 fsync(fd);
82d9eba6
NB
849 return 0;
850}
851
3da92f27 852static int load_super1(struct supertype *st, int fd, char *devname);
892debc8 853
2fb749d1
NB
854static unsigned long choose_bm_space(unsigned long devsize)
855{
856 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
857 * if bigger than 200Gig, save 128k
858 */
69646c14 859 if (devsize < 64*2) return 0;
2fb749d1
NB
860 if (devsize - 64*2 >= 200*1024*1024*2)
861 return 128*2;
862 if (devsize - 4*2 > 8*1024*1024*2)
863 return 64*2;
864 return 4*2;
865}
866
3da92f27 867static int write_init_super1(struct supertype *st,
1bf4e2d9 868 mdu_disk_info_t *dinfo, char *devname)
82d9eba6 869{
3da92f27
NB
870 struct mdp_superblock_1 *sb = st->sb;
871 struct supertype refst;
892debc8 872 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 873 int rfd;
82d9eba6 874 int rv;
199171a2 875 int bm_space;
82d9eba6 876
8fac0577 877 unsigned long long dsize, array_size;
82d9eba6
NB
878 long long sb_offset;
879
880
881 if (fd < 0) {
882 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
883 devname);
884 return -1;
885 }
886
887 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 888 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
83205b64 889 sb->devflags |= __cpu_to_le32(WriteMostly1);
892debc8 890
dfe47e00
NB
891 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
892 read(rfd, sb->device_uuid, 16) != 16) {
893 *(__u32*)(sb->device_uuid) = random();
894 *(__u32*)(sb->device_uuid+4) = random();
895 *(__u32*)(sb->device_uuid+8) = random();
896 *(__u32*)(sb->device_uuid+12) = random();
897 }
898 if (rfd >= 0) close(rfd);
892debc8
NB
899 sb->events = 0;
900
3da92f27
NB
901 refst =*st;
902 refst.sb = NULL;
903 if (load_super1(&refst, fd, NULL)==0) {
904 struct mdp_superblock_1 *refsb = refst.sb;
e478dc86 905
892debc8
NB
906 memcpy(sb->device_uuid, refsb->device_uuid, 16);
907 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
908 /* same array, so preserve events and dev_number */
909 sb->events = refsb->events;
7eae7080
NB
910 /* bugs in 2.6.17 and earlier mean the dev_number
911 * chosen in Manage must be preserved
912 */
913 if (get_linux_version() >= 2006018)
914 sb->dev_number = refsb->dev_number;
892debc8
NB
915 }
916 free(refsb);
917 }
34163fc7 918
beae1dfe
NB
919 if (!get_dev_size(fd, NULL, &dsize))
920 return 1;
921 dsize >>= 9;
82d9eba6 922
5dd497ee 923 if (dsize < 24) {
024ce7fe 924 close(fd);
82d9eba6 925 return 2;
024ce7fe 926 }
82d9eba6
NB
927
928
929 /*
930 * Calculate the position of the superblock.
931 * It is always aligned to a 4K boundary and
932 * depending on minor_version, it can be:
933 * 0: At least 8K, but less than 12K, from end of device
934 * 1: At start of device
935 * 2: 4K from start of device.
21e92547
NB
936 * Depending on the array size, we might leave extra space
937 * for a bitmap.
82d9eba6 938 */
8fac0577 939 array_size = __le64_to_cpu(sb->size);
519561f7 940 /* work out how much space we left for a bitmap */
2fb749d1 941 bm_space = choose_bm_space(array_size);
199171a2 942
82d9eba6
NB
943 switch(st->minor_version) {
944 case 0:
5dd497ee 945 sb_offset = dsize;
82d9eba6
NB
946 sb_offset -= 8*2;
947 sb_offset &= ~(4*2-1);
948 sb->super_offset = __cpu_to_le64(sb_offset);
949 sb->data_offset = __cpu_to_le64(0);
519561f7
NB
950 if (sb_offset - bm_space < array_size)
951 bm_space = sb_offset - array_size;
199171a2 952 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
82d9eba6
NB
953 break;
954 case 1:
955 sb->super_offset = __cpu_to_le64(0);
005debfc
NB
956 if (4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
957 bm_space = dsize - __le64_to_cpu(sb->size) - 4*2;
199171a2
NB
958 sb->data_offset = __cpu_to_le64(bm_space + 4*2);
959 sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
82d9eba6
NB
960 break;
961 case 2:
962 sb_offset = 4*2;
199171a2 963 sb->super_offset = __cpu_to_le64(4*2);
005debfc
NB
964 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
965 bm_space = dsize - __le64_to_cpu(sb->size) - 4*2 - 4*2;
199171a2
NB
966 sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
967 sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space );
82d9eba6
NB
968 break;
969 default:
970 return -EINVAL;
971 }
972
973
974 sb->sb_csum = calc_sb_1_csum(sb);
3da92f27 975 rv = store_super1(st, fd);
82d9eba6
NB
976 if (rv)
977 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 978
1bf4e2d9 979 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
3da92f27 980 rv = st->ss->write_bitmap(st, fd);
024ce7fe 981 close(fd);
82d9eba6
NB
982 return rv;
983}
984
64557c33 985static int compare_super1(struct supertype *st, struct supertype *tst)
82d9eba6
NB
986{
987 /*
988 * return:
989 * 0 same, or first was empty, and second was copied
990 * 1 second had wrong number
991 * 2 wrong uuid
992 * 3 wrong other info
993 */
64557c33
NB
994 struct mdp_superblock_1 *first = st->sb;
995 struct mdp_superblock_1 *second = tst->sb;
82d9eba6
NB
996
997 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
998 return 1;
999 if (second->major_version != __cpu_to_le32(1))
1000 return 1;
1001
1002 if (!first) {
bee8ec56
NB
1003 first = malloc(1024+sizeof(bitmap_super_t) +
1004 sizeof(struct misc_dev_info));
1005 memcpy(first, second, 1024+sizeof(bitmap_super_t) +
1006 sizeof(struct misc_dev_info));
64557c33 1007 st->sb = first;
82d9eba6
NB
1008 return 0;
1009 }
1010 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1011 return 2;
1012
1013 if (first->ctime != second->ctime ||
1014 first->level != second->level ||
1015 first->layout != second->layout ||
1016 first->size != second->size ||
1017 first->chunksize != second->chunksize ||
1018 first->raid_disks != second->raid_disks)
1019 return 3;
1020 return 0;
1021}
1022
3da92f27
NB
1023static void free_super1(struct supertype *st);
1024
1025static int load_super1(struct supertype *st, int fd, char *devname)
82d9eba6 1026{
5dd497ee 1027 unsigned long long dsize;
82d9eba6
NB
1028 unsigned long long sb_offset;
1029 struct mdp_superblock_1 *super;
14263cf1
NB
1030 int uuid[4];
1031 struct bitmap_super_s *bsb;
bee8ec56 1032 struct misc_dev_info *misc;
82d9eba6 1033
3da92f27 1034 free_super1(st);
82d9eba6 1035
a17a3de3 1036 if (st->ss == NULL || st->minor_version == -1) {
570c0542 1037 int bestvers = -1;
23dc1ae8 1038 struct supertype tst;
570c0542
NB
1039 __u64 bestctime = 0;
1040 /* guess... choose latest ctime */
23dc1ae8 1041 tst.ss = &super1;
3da92f27 1042 tst.sb = NULL;
23dc1ae8 1043 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
3da92f27
NB
1044 switch(load_super1(&tst, fd, devname)) {
1045 case 0: super = tst.sb;
570c0542
NB
1046 if (bestvers == -1 ||
1047 bestctime < __le64_to_cpu(super->ctime)) {
23dc1ae8 1048 bestvers = tst.minor_version;
570c0542
NB
1049 bestctime = __le64_to_cpu(super->ctime);
1050 }
1051 free(super);
3da92f27 1052 tst.sb = NULL;
570c0542 1053 break;
23dc1ae8 1054 case 1: return 1; /*bad device */
82d9eba6
NB
1055 case 2: break; /* bad, try next */
1056 }
1057 }
570c0542
NB
1058 if (bestvers != -1) {
1059 int rv;
23dc1ae8
NB
1060 tst.minor_version = bestvers;
1061 tst.ss = &super1;
1062 tst.max_devs = 384;
3da92f27 1063 rv = load_super1(&tst, fd, devname);
23dc1ae8
NB
1064 if (rv == 0)
1065 *st = tst;
570c0542
NB
1066 return rv;
1067 }
82d9eba6
NB
1068 return 2;
1069 }
beae1dfe
NB
1070 if (!get_dev_size(fd, devname, &dsize))
1071 return 1;
1072 dsize >>= 9;
82d9eba6 1073
5dd497ee 1074 if (dsize < 24) {
82d9eba6 1075 if (devname)
5dd497ee
NB
1076 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1077 devname, dsize);
82d9eba6
NB
1078 return 1;
1079 }
1080
1081 /*
1082 * Calculate the position of the superblock.
1083 * It is always aligned to a 4K boundary and
14263cf1 1084 * depending on minor_version, it can be:
82d9eba6
NB
1085 * 0: At least 8K, but less than 12K, from end of device
1086 * 1: At start of device
1087 * 2: 4K from start of device.
1088 */
1089 switch(st->minor_version) {
1090 case 0:
5dd497ee 1091 sb_offset = dsize;
82d9eba6
NB
1092 sb_offset -= 8*2;
1093 sb_offset &= ~(4*2-1);
1094 break;
1095 case 1:
1096 sb_offset = 0;
1097 break;
1098 case 2:
1099 sb_offset = 4*2;
1100 break;
1101 default:
1102 return -EINVAL;
1103 }
1104
1105 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1106
1107
1108 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1109 if (devname)
1110 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1111 devname, strerror(errno));
1112 return 1;
1113 }
1114
bee8ec56
NB
1115 super = malloc(1024 + sizeof(bitmap_super_t) +
1116 sizeof(struct misc_dev_info));
82d9eba6
NB
1117
1118 if (read(fd, super, 1024) != 1024) {
1119 if (devname)
1120 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1121 devname);
1122 free(super);
1123 return 1;
1124 }
1125
1126 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1127 if (devname)
1128 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1129 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1130 free(super);
1131 return 2;
1132 }
1133
1134 if (__le32_to_cpu(super->major_version) != 1) {
1135 if (devname)
1136 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1137 devname, __le32_to_cpu(super->major_version));
1138 free(super);
1139 return 2;
1140 }
1141 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1142 if (devname)
1143 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1144 devname);
1145 free(super);
1146 return 2;
1147 }
64557c33 1148 st->sb = super;
14263cf1 1149
bee8ec56
NB
1150 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1151
1152 misc = (struct misc_dev_info*) (bsb+1);
1153 misc->device_size = dsize;
1154
14263cf1
NB
1155 /* Now check on the bitmap superblock */
1156 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1157 return 0;
1158 /* Read the bitmap superblock and make sure it looks
1159 * valid. If it doesn't clear the bit. An --assemble --force
1160 * should get that written out.
1161 */
3da92f27 1162 locate_bitmap1(st, fd);
14263cf1
NB
1163 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1164 != sizeof(struct bitmap_super_s))
1165 goto no_bitmap;
1166
3da92f27 1167 uuid_from_super1(st, uuid);
14263cf1
NB
1168 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1169 memcmp(bsb->uuid, uuid, 16) != 0)
1170 goto no_bitmap;
1171 return 0;
1172
1173 no_bitmap:
1174 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1175 return 0;
1176}
1177
1178
1179static struct supertype *match_metadata_desc1(char *arg)
1180{
1181 struct supertype *st = malloc(sizeof(*st));
1182 if (!st) return st;
1183
1184 st->ss = &super1;
ea329559 1185 st->max_devs = 384;
64557c33 1186 st->sb = NULL;
a17a3de3 1187 if (strcmp(arg, "1.0") == 0) {
82d9eba6
NB
1188 st->minor_version = 0;
1189 return st;
1190 }
1191 if (strcmp(arg, "1.1") == 0) {
1192 st->minor_version = 1;
1193 return st;
1194 }
1195 if (strcmp(arg, "1.2") == 0) {
1196 st->minor_version = 2;
1197 return st;
1198 }
a17a3de3
DL
1199 if (strcmp(arg, "1") == 0 ||
1200 strcmp(arg, "default/large") == 0) {
1201 st->minor_version = -1;
1202 return st;
1203 }
82d9eba6
NB
1204
1205 free(st);
1206 return NULL;
1207}
1208
34163fc7
NB
1209/* find available size on device with this devsize, using
1210 * superblock type st, and reserving 'reserve' sectors for
1211 * a possible bitmap
1212 */
1bf4e2d9 1213static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6 1214{
2a528478 1215 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
1216 if (devsize < 24)
1217 return 0;
1218
2a528478
N
1219 if (super == NULL)
1220 /* creating: allow suitable space for bitmap */
1221 devsize -= choose_bm_space(devsize);
1222#ifndef MDASSEMBLE
1223 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1224 /* hot-add. allow for actual size of bitmap */
1225 struct bitmap_super_s *bsb;
1226 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1227 devsize -= bitmap_sectors(bsb);
1228 }
1229#endif
21e92547 1230
34163fc7 1231 switch(st->minor_version) {
a17a3de3
DL
1232 case -1: /* no specified. Now time to set default */
1233 st->minor_version = 0;
1234 /* FALL THROUGH */
34163fc7 1235 case 0:
1bf4e2d9
NB
1236 /* at end */
1237 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1238 case 1:
1bf4e2d9
NB
1239 /* at start, 4K for superblock and possible bitmap */
1240 return devsize - 4*2;
34163fc7 1241 case 2:
1bf4e2d9
NB
1242 /* 4k from start, 4K for superblock and possible bitmap */
1243 return devsize - (4+4)*2;
34163fc7
NB
1244 }
1245 return 0;
1246}
1247
1bf4e2d9 1248static int
3da92f27 1249add_internal_bitmap1(struct supertype *st,
199171a2
NB
1250 int *chunkp, int delay, int write_behind,
1251 unsigned long long size,
21e92547 1252 int may_change, int major)
34163fc7
NB
1253{
1254 /*
1bf4e2d9
NB
1255 * If not may_change, then this is a 'Grow', and the bitmap
1256 * must fit after the superblock.
1257 * If may_change, then this is create, and we can put the bitmap
1258 * before the superblock if we like, or may move the start.
199171a2
NB
1259 * If !may_change, the bitmap MUST live at offset of 1K, until
1260 * we get a sysfs interface.
34163fc7 1261 *
f9c25f1d 1262 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1263 */
1264
1bf4e2d9 1265 unsigned long long bits;
199171a2 1266 unsigned long long max_bits;
34163fc7 1267 unsigned long long min_chunk;
199171a2
NB
1268 long offset;
1269 int chunk = *chunkp;
37dfc3d6 1270 int room = 0;
3da92f27 1271 struct mdp_superblock_1 *sb = st->sb;
34163fc7
NB
1272 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1273
199171a2
NB
1274 switch(st->minor_version) {
1275 case 0:
1276 /* either 3K after the superblock, or some amount of space
1277 * before.
1278 */
1279 if (may_change) {
1280 /* We are creating array, so we *know* how much room has
1281 * been left.
1282 */
1283 offset = 0;
2fb749d1
NB
1284 room = choose_bm_space(__le64_to_cpu(sb->size));
1285 if (room == 4*2) {
1286 /* make it 3K after the superblock */
199171a2
NB
1287 room = 3*2;
1288 offset = 2;
1289 }
1290 } else {
1291 room = __le64_to_cpu(sb->super_offset)
1292 - __le64_to_cpu(sb->data_offset)
1293 - __le64_to_cpu(sb->data_size);
1294 /* remove '1 ||' when we can set offset via sysfs */
1295 if (1 || (room < 3*2 &&
1296 __le32_to_cpu(sb->max_dev) <= 384)) {
1297 room = 3*2;
1298 offset = 1*2;
1299 } else {
1300 offset = 0; /* means movable offset */
1301 }
1302 }
1303 break;
1304 case 1:
1305 case 2: /* between superblock and data */
1306 if (may_change) {
1307 offset = 4*2;
2fb749d1 1308 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1309 } else {
1310 room = __le64_to_cpu(sb->data_offset)
1311 - __le64_to_cpu(sb->super_offset);
1312 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1313 room -= 2;
1314 offset = 2;
1315 } else {
1316 room -= 4*2;
1317 offset = 4*2;
1318 }
1319 }
1320 break;
ae491d1e
NB
1321 default:
1322 return 0;
199171a2 1323 }
1bf4e2d9 1324
199171a2
NB
1325 if (chunk == UnSet && room > 128*2)
1326 /* Limit to 128K of bitmap when chunk size not requested */
1327 room = 128*2;
1bf4e2d9 1328
199171a2 1329 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1330
1331 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1332 bits = (size*512)/min_chunk +1;
34163fc7
NB
1333 while (bits > max_bits) {
1334 min_chunk *= 2;
1335 bits = (bits+1)/2;
1336 }
1337 if (chunk == UnSet)
1338 chunk = min_chunk;
1339 else if (chunk < min_chunk)
1340 return 0; /* chunk size too small */
21e92547
NB
1341 if (chunk == 0) /* rounding problem */
1342 return 0;
34163fc7 1343
199171a2
NB
1344 if (offset == 0) {
1345 bits = (size*512) / chunk + 1;
1346 room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512;
1347 offset = -room;
1348 }
1349
1350 sb->bitmap_offset = __cpu_to_le32(offset);
34163fc7
NB
1351
1352 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1353 memset(bms, 0, sizeof(*bms));
34163fc7 1354 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1355 bms->version = __cpu_to_le32(major);
3da92f27 1356 uuid_from_super1(st, (int*)bms->uuid);
34163fc7
NB
1357 bms->chunksize = __cpu_to_le32(chunk);
1358 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1359 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1360 bms->write_behind = __cpu_to_le32(write_behind);
1361
199171a2 1362 *chunkp = chunk;
34163fc7
NB
1363 return 1;
1364}
1365
1366
3da92f27 1367static void locate_bitmap1(struct supertype *st, int fd)
34163fc7 1368{
34163fc7 1369 unsigned long long offset;
1e0d770c
NB
1370 struct mdp_superblock_1 *sb;
1371 int mustfree = 0;
34163fc7 1372
3da92f27
NB
1373 if (!st->sb) {
1374 if (st->ss->load_super(st, fd, NULL))
f6d75de8 1375 return; /* no error I hope... */
1e0d770c
NB
1376 mustfree = 1;
1377 }
3da92f27 1378 sb = st->sb;
e478dc86 1379
1bf4e2d9 1380 offset = __le64_to_cpu(sb->super_offset);
68754bd1 1381 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1382 if (mustfree)
f6d75de8 1383 free(sb);
1bf4e2d9 1384 lseek64(fd, offset<<9, 0);
34163fc7
NB
1385}
1386
3da92f27 1387static int write_bitmap1(struct supertype *st, int fd)
34163fc7 1388{
3da92f27 1389 struct mdp_superblock_1 *sb = st->sb;
1bf4e2d9 1390 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1391 int rv = 0;
1392
1393 int towrite, n;
1394 char buf[4096];
1395
3da92f27 1396 locate_bitmap1(st, fd);
34163fc7 1397
1e0d770c
NB
1398 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1399 sizeof(bitmap_super_t))
1400 return -2;
1bf4e2d9
NB
1401 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1402 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1403 memset(buf, 0xff, sizeof(buf));
1404 while (towrite > 0) {
1405 n = towrite;
1406 if (n > sizeof(buf))
1407 n = sizeof(buf);
1408 n = write(fd, buf, n);
1409 if (n > 0)
1410 towrite -= n;
1411 else
1412 break;
1413 }
1414 fsync(fd);
1415 if (towrite)
1416 rv = -2;
1417
1418 return rv;
82d9eba6
NB
1419}
1420
3da92f27 1421static void free_super1(struct supertype *st)
df37ffc0 1422{
3da92f27
NB
1423 if (st->sb)
1424 free(st->sb);
1425 st->sb = NULL;
df37ffc0
NB
1426}
1427
82d9eba6 1428struct superswitch super1 = {
c7654afc 1429#ifndef MDASSEMBLE
82d9eba6
NB
1430 .examine_super = examine_super1,
1431 .brief_examine_super = brief_examine_super1,
0d726f17 1432 .export_examine_super = export_examine_super1,
82d9eba6
NB
1433 .detail_super = detail_super1,
1434 .brief_detail_super = brief_detail_super1,
0d726f17 1435 .export_detail_super = export_detail_super1,
c7654afc 1436#endif
83b6208e 1437 .match_home = match_home1,
82d9eba6
NB
1438 .uuid_from_super = uuid_from_super1,
1439 .getinfo_super = getinfo_super1,
1440 .update_super = update_super1,
82d9eba6
NB
1441 .init_super = init_super1,
1442 .add_to_super = add_to_super1,
1443 .store_super = store_super1,
1444 .write_init_super = write_init_super1,
1445 .compare_super = compare_super1,
1446 .load_super = load_super1,
1447 .match_metadata_desc = match_metadata_desc1,
1448 .avail_size = avail_size1,
34163fc7
NB
1449 .add_internal_bitmap = add_internal_bitmap1,
1450 .locate_bitmap = locate_bitmap1,
1451 .write_bitmap = write_bitmap1,
df37ffc0 1452 .free_super = free_super1,
82d9eba6 1453 .major = 1,
f277ce36
NB
1454#if __BYTE_ORDER == BIG_ENDIAN
1455 .swapuuid = 0,
1456#else
1457 .swapuuid = 1,
1458#endif
82d9eba6 1459};