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