Provide a mdstat_ent to subarray helper
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 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
e736b623 22 * Email: <neilb@suse.de>
82d9eba6
NB
23 */
24
25#include "mdadm.h"
82d9eba6
NB
26/*
27 * The version-1 superblock :
28 * All numeric fields are little-endian.
29 *
30 * total size: 256 bytes plus 2 per device.
31 * 1K allows 384 devices.
32 */
33struct mdp_superblock_1 {
34 /* constant array information - 128 bytes */
35 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
36 __u32 major_version; /* 1 */
37 __u32 feature_map; /* 0 for now */
38 __u32 pad0; /* always set to 0 when writing */
39
40 __u8 set_uuid[16]; /* user-space generated. */
41 char set_name[32]; /* set and interpreted by user-space */
42
43 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
44 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
45 __u32 layout; /* only for raid5 currently */
46 __u64 size; /* used size of component devices, in 512byte sectors */
47
48 __u32 chunksize; /* in 512byte sectors */
49 __u32 raid_disks;
34163fc7
NB
50 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
51 * NOTE: signed, so bitmap can be before superblock
52 * only meaningful of feature_map[0] is set.
53 */
b674b5b8
NB
54
55 /* These are only valid with feature bit '4' */
b674b5b8 56 __u32 new_level; /* new level we are reshaping to */
ac957baa 57 __u64 reshape_position; /* next address in array-space for reshape */
b674b5b8
NB
58 __u32 delta_disks; /* change in number of raid_disks */
59 __u32 new_layout; /* new layout */
60 __u32 new_chunk; /* new chunk size (bytes) */
61 __u8 pad1[128-124]; /* set to 0 when written */
82d9eba6
NB
62
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset; /* sector start of data, often 0 */
65 __u64 data_size; /* sectors in this device that can be used for data */
66 __u64 super_offset; /* sector start of this superblock */
67 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
dfd4d8ee
NB
71 __u8 devflags; /* per-device flags. Only one defined...*/
72#define WriteMostly1 1 /* mask for writemostly flag in above */
73 __u8 pad2[64-57]; /* set to 0 when writing */
82d9eba6
NB
74
75 /* array state information - 64 bytes */
76 __u64 utime; /* 40 bits second, 24 btes microseconds */
77 __u64 events; /* incremented when superblock updated */
78 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
4180aa4d
N
79 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
80 __u32 max_dev; /* size of dev_roles[] array to consider */
82d9eba6
NB
81 __u8 pad3[64-32]; /* set to 0 when writing */
82
83 /* device state information. Indexed by dev_number.
84 * 2 bytes per device
85 * Note there are no per-device state flags. State information is rolled
86 * into the 'roles' value. If a device is spare or faulty, then it doesn't
87 * have a meaningful role.
88 */
89 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
90};
91
bee8ec56
NB
92struct misc_dev_info {
93 __u64 device_size;
94};
95
d2cd3ffc
NB
96/* feature_map bits */
97#define MD_FEATURE_BITMAP_OFFSET 1
98#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
99 * must be honoured
100 */
b674b5b8 101#define MD_FEATURE_RESHAPE_ACTIVE 4
d2cd3ffc 102
b674b5b8 103#define MD_FEATURE_ALL (1|2|4)
d2cd3ffc 104
570c0542 105#ifndef offsetof
067db4df 106#define offsetof(t,f) ((size_t)&(((t*)0)->f))
570c0542 107#endif
82d9eba6
NB
108static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
109{
110 unsigned int disk_csum, csum;
111 unsigned long long newcsum;
112 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
113 unsigned int *isuper = (unsigned int*)sb;
114 int i;
115
116/* make sure I can count... */
117 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
118 offsetof(struct mdp_superblock_1, utime) != 192 ||
119 sizeof(struct mdp_superblock_1) != 256) {
120 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
121 }
122
123 disk_csum = sb->sb_csum;
124 sb->sb_csum = 0;
125 newcsum = 0;
722966c6
PC
126 for (i=0; size>=4; size -= 4 ) {
127 newcsum += __le32_to_cpu(*isuper);
128 isuper++;
129 }
82d9eba6
NB
130
131 if (size == 2)
132 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
133
134 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
135 sb->sb_csum = disk_csum;
83205b64 136 return __cpu_to_le32(csum);
82d9eba6
NB
137}
138
5ea022a1
N
139static char abuf[4096+4096];
140static int aread(int fd, void *buf, int len)
141{
142 /* aligned read.
143 * On devices with a 4K sector size, we need to read
144 * the full sector and copy relevant bits into
145 * the buffer
146 */
147 int bsize;
148 char *b;
149 int n;
150 if (ioctl(fd, BLKSSZGET, &bsize) != 0 ||
151 bsize <= len)
152 return read(fd, buf, len);
153 if (bsize > 4096)
154 return -1;
155 b = (char*)(((long)(abuf+4096))&~4095UL);
156
157 n = read(fd, b, bsize);
158 if (n <= 0)
159 return n;
160 lseek(fd, len - n, 1);
161 if (n > len)
162 n = len;
163 memcpy(buf, b, n);
164 return n;
165}
166
167static int awrite(int fd, void *buf, int len)
168{
169 /* aligned write.
170 * On devices with a 4K sector size, we need to write
171 * the full sector. We pre-read if the sector is larger
172 * than the write.
173 * The address must be sector-aligned.
174 */
175 int bsize;
176 char *b;
177 int n;
178 if (ioctl(fd, BLKSSZGET, &bsize) != 0 ||
179 bsize <= len)
180 return write(fd, buf, len);
181 if (bsize > 4096)
182 return -1;
183 b = (char*)(((long)(abuf+4096))&~4095UL);
184
185 n = read(fd, b, bsize);
186 if (n <= 0)
187 return n;
188 lseek(fd, -n, 1);
189 memcpy(b, buf, len);
190 n = write(fd, b, bsize);
191 if (n <= 0)
192 return n;
193 lseek(fd, len - n, 1);
194 return len;
195}
196
c7654afc 197#ifndef MDASSEMBLE
3da92f27 198static void examine_super1(struct supertype *st, char *homehost)
82d9eba6 199{
3da92f27 200 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 201 time_t atime;
f21e18ca 202 unsigned int d;
e8e1c176 203 int role;
4180aa4d 204 int delta_extra = 0;
82d9eba6
NB
205 int i;
206 char *c;
a1cbd7d0 207 int l = homehost ? strlen(homehost) : 0;
83205b64 208 int layout;
a17a3de3 209 unsigned long long sb_offset;
82d9eba6
NB
210
211 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
a17a3de3
DL
212 printf(" Version : 1");
213 sb_offset = __le64_to_cpu(sb->super_offset);
214 if (sb_offset <= 4)
215 printf(".1\n");
216 else if (sb_offset <= 8)
217 printf(".2\n");
218 else
219 printf(".0\n");
91eedefc 220 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
221 printf(" Array UUID : ");
222 for (i=0; i<16; i++) {
82d9eba6 223 if ((i&3)==0 && i != 0) printf(":");
34163fc7 224 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
225 }
226 printf("\n");
a1cbd7d0
NB
227 printf(" Name : %.32s", sb->set_name);
228 if (l > 0 && l < 32 &&
229 sb->set_name[l] == ':' &&
230 strncmp(sb->set_name, homehost, l) == 0)
231 printf(" (local to host %s)", homehost);
232 printf("\n");
82d9eba6
NB
233 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
234 printf(" Creation Time : %.24s\n", ctime(&atime));
235 c=map_num(pers, __le32_to_cpu(sb->level));
236 printf(" Raid Level : %s\n", c?c:"-unknown-");
237 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
238 printf("\n");
5cda0964 239 printf(" Avail Dev Size : %llu%s\n",
2fb749d1
NB
240 (unsigned long long)__le64_to_cpu(sb->data_size),
241 human_size(__le64_to_cpu(sb->data_size)<<9));
c43f7d91 242 if (__le32_to_cpu(sb->level) > 0) {
b674b5b8
NB
243 int ddsks=0;
244 switch(__le32_to_cpu(sb->level)) {
245 case 1: ddsks=1;break;
246 case 4:
83205b64
NB
247 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
248 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
249 case 10:
250 layout = __le32_to_cpu(sb->layout);
251 ddsks = __le32_to_cpu(sb->raid_disks)
252 / (layout&255) / ((layout>>8)&255);
b674b5b8
NB
253 }
254 if (ddsks)
83205b64 255 printf(" Array Size : %llu%s\n",
c1c05f7f 256 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
83205b64 257 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
b674b5b8 258 if (sb->size != sb->data_size)
5cda0964 259 printf(" Used Dev Size : %llu%s\n",
83205b64
NB
260 (unsigned long long)__le64_to_cpu(sb->size),
261 human_size(__le64_to_cpu(sb->size)<<9));
b674b5b8 262 }
82d9eba6 263 if (sb->data_offset)
199171a2
NB
264 printf(" Data Offset : %llu sectors\n",
265 (unsigned long long)__le64_to_cpu(sb->data_offset));
266 printf(" Super Offset : %llu sectors\n",
267 (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
268 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
269 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 270 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
271 printf(" Device UUID : ");
272 for (i=0; i<16; i++) {
82d9eba6 273 if ((i&3)==0 && i != 0) printf(":");
34163fc7 274 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
275 }
276 printf("\n");
b674b5b8 277 printf("\n");
91eedefc
NB
278 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
279 printf("Internal Bitmap : %ld sectors from superblock\n",
68754bd1 280 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
8fac0577 281 }
b674b5b8 282 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 283 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
284 human_size(__le64_to_cpu(sb->reshape_position)<<9));
285 if (__le32_to_cpu(sb->delta_disks)) {
286 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
4180aa4d
N
287 printf(" (%d->%d)\n",
288 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
289 __le32_to_cpu(sb->raid_disks));
290 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
291 delta_extra = -__le32_to_cpu(sb->delta_disks);
b674b5b8
NB
292 }
293 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
294 c = map_num(pers, __le32_to_cpu(sb->new_level));
295 printf(" New Level : %s\n", c?c:"-unknown-");
296 }
297 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
298 if (__le32_to_cpu(sb->level) == 5) {
299 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
300 printf(" New Layout : %s\n", c?c:"-unknown-");
301 }
fe77a154
N
302 if (__le32_to_cpu(sb->level) == 6) {
303 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
304 printf(" New Layout : %s\n", c?c:"-unknown-");
305 }
b674b5b8 306 if (__le32_to_cpu(sb->level) == 10) {
e4965ef8
N
307 printf(" New Layout :");
308 print_r10_layout(__le32_to_cpu(sb->new_layout));
309 printf("\n");
b674b5b8
NB
310 }
311 }
312 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
313 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
314 printf("\n");
315 }
dfd4d8ee
NB
316 if (sb->devflags) {
317 printf(" Flags :");
34163fc7 318 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
319 printf(" write-mostly");
320 printf("\n");
321 }
82d9eba6
NB
322
323 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
324 printf(" Update Time : %.24s\n", ctime(&atime));
325
326 if (calc_sb_1_csum(sb) == sb->sb_csum)
327 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
328 else
329 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
330 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 331 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
332 printf("\n");
333 if (__le32_to_cpu(sb->level) == 5) {
334 c = map_num(r5layout, __le32_to_cpu(sb->layout));
335 printf(" Layout : %s\n", c?c:"-unknown-");
336 }
fe77a154
N
337 if (__le32_to_cpu(sb->level) == 6) {
338 c = map_num(r6layout, __le32_to_cpu(sb->layout));
339 printf(" Layout : %s\n", c?c:"-unknown-");
340 }
265e0f17
NB
341 if (__le32_to_cpu(sb->level) == 10) {
342 int lo = __le32_to_cpu(sb->layout);
e4965ef8
N
343 printf(" Layout :");
344 print_r10_layout(lo);
345 printf("\n");
265e0f17 346 }
82d9eba6
NB
347 switch(__le32_to_cpu(sb->level)) {
348 case 0:
349 case 4:
350 case 5:
d2cd3ffc
NB
351 case 6:
352 case 10:
b674b5b8 353 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
354 break;
355 case -1:
b674b5b8 356 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
357 break;
358 default: break;
359 }
360 printf("\n");
e8e1c176
N
361#if 0
362 /* This turns out to just be confusing */
c3684618
NB
363 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
364 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
365 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
366 break;
367 for (d=0; d < i; d++) {
368 int role = __le16_to_cpu(sb->dev_roles[d]);
369 if (d) printf(", ");
370 if (role == 0xffff) printf("empty");
371 else if(role == 0xfffe) printf("failed");
372 else printf("%d", role);
373 }
374 printf(")\n");
e8e1c176
N
375#endif
376 printf(" Device Role : ");
377 d = __le32_to_cpu(sb->dev_number);
4180aa4d 378 if (d < __le32_to_cpu(sb->max_dev))
e8e1c176
N
379 role = __le16_to_cpu(sb->dev_roles[d]);
380 else
381 role = 0xFFFF;
382 if (role >= 0xFFFE)
383 printf("spare\n");
384 else
385 printf("Active device %d\n", role);
386
82d9eba6 387 printf(" Array State : ");
4180aa4d 388 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
82d9eba6
NB
389 int cnt = 0;
390 int me = 0;
f21e18ca 391 unsigned int i;
82d9eba6 392 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
f21e18ca 393 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
82d9eba6
NB
394 if (role == d) {
395 if (i == __le32_to_cpu(sb->dev_number))
396 me = 1;
397 cnt++;
398 }
399 }
400 if (cnt > 1) printf("?");
e8e1c176
N
401 else if (cnt == 1) printf("A");
402 else printf (".");
82d9eba6 403 }
e8e1c176
N
404#if 0
405 /* This is confusing too */
6fbba4c9 406 faulty = 0;
82d9eba6
NB
407 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
408 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
409 if (role == 0xFFFE)
410 faulty++;
82d9eba6 411 }
82d9eba6 412 if (faulty) printf(" %d failed", faulty);
e8e1c176
N
413#endif
414 printf(" ('A' == active, '.' == missing)");
82d9eba6
NB
415 printf("\n");
416}
417
418
061f2c6a 419static void brief_examine_super1(struct supertype *st, int verbose)
82d9eba6 420{
3da92f27 421 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 422 int i;
a17a3de3 423 unsigned long long sb_offset;
2998bc01 424 char *nm;
b015e6c2 425 char *c=map_num(pers, __le32_to_cpu(sb->level));
2998bc01
NB
426
427 nm = strchr(sb->set_name, ':');
428 if (nm)
429 nm++;
430 else if (sb->set_name[0])
431 nm = sb->set_name;
432 else
cf8de691 433 nm = NULL;
82d9eba6 434
061f2c6a
N
435 printf("ARRAY%s%s", nm ? " /dev/md/":"", nm);
436 if (verbose && c)
437 printf(" level=%s", c);
a17a3de3
DL
438 sb_offset = __le64_to_cpu(sb->super_offset);
439 if (sb_offset <= 4)
e9a0e728 440 printf(" metadata=1.1 ");
a17a3de3 441 else if (sb_offset <= 8)
e9a0e728 442 printf(" metadata=1.2 ");
a17a3de3 443 else
e9a0e728 444 printf(" metadata=1.0 ");
061f2c6a
N
445 if (verbose)
446 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
447 printf("UUID=");
82d9eba6 448 for (i=0; i<16; i++) {
82d9eba6 449 if ((i&3)==0 && i != 0) printf(":");
b7a708af 450 printf("%02x", sb->set_uuid[i]);
82d9eba6 451 }
947fd4dd
NB
452 if (sb->set_name[0])
453 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
454 printf("\n");
455}
456
0d726f17
KS
457static void export_examine_super1(struct supertype *st)
458{
459 struct mdp_superblock_1 *sb = st->sb;
460 int i;
461 int len = 32;
462
463 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
464 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
465 for (i=0; i<32; i++)
466 if (sb->set_name[i] == '\n' ||
467 sb->set_name[i] == '\0') {
468 len = i;
469 break;
470 }
471 if (len)
472 printf("MD_NAME=%.*s\n", len, sb->set_name);
473 printf("MD_UUID=");
474 for (i=0; i<16; i++) {
475 if ((i&3)==0 && i != 0) printf(":");
476 printf("%02x", sb->set_uuid[i]);
477 }
478 printf("\n");
479 printf("MD_UPDATE_TIME=%llu\n",
480 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
481 printf("MD_DEV_UUID=");
482 for (i=0; i<16; i++) {
483 if ((i&3)==0 && i != 0) printf(":");
484 printf("%02x", sb->device_uuid[i]);
485 }
486 printf("\n");
487 printf("MD_EVENTS=%llu\n",
488 (unsigned long long)__le64_to_cpu(sb->events));
489}
490
3da92f27 491static void detail_super1(struct supertype *st, char *homehost)
82d9eba6 492{
3da92f27 493 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 494 int i;
b6750aa8 495 int l = homehost ? strlen(homehost) : 0;
82d9eba6 496
b6750aa8
NB
497 printf(" Name : %.32s", sb->set_name);
498 if (l > 0 && l < 32 &&
499 sb->set_name[l] == ':' &&
500 strncmp(sb->set_name, homehost, l) == 0)
501 printf(" (local to host %s)", homehost);
502 printf("\n UUID : ");
82d9eba6 503 for (i=0; i<16; i++) {
82d9eba6 504 if ((i&3)==0 && i != 0) printf(":");
34163fc7 505 printf("%02x", sb->set_uuid[i]);
82d9eba6 506 }
570c0542 507 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
508}
509
3da92f27 510static void brief_detail_super1(struct supertype *st)
82d9eba6 511{
3da92f27 512 struct mdp_superblock_1 *sb = st->sb;
82d9eba6
NB
513 int i;
514
947fd4dd
NB
515 if (sb->set_name[0])
516 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
517 printf(" UUID=");
518 for (i=0; i<16; i++) {
82d9eba6 519 if ((i&3)==0 && i != 0) printf(":");
34163fc7 520 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
521 }
522}
523
0d726f17 524static void export_detail_super1(struct supertype *st)
54bad364 525{
3da92f27 526 struct mdp_superblock_1 *sb = st->sb;
54bad364
KS
527 int i;
528 int len = 32;
529
530 for (i=0; i<32; i++)
531 if (sb->set_name[i] == '\n' ||
532 sb->set_name[i] == '\0') {
533 len = i;
534 break;
535 }
536 if (len)
537 printf("MD_NAME=%.*s\n", len, sb->set_name);
54bad364
KS
538}
539
c7654afc
NB
540#endif
541
3da92f27 542static int match_home1(struct supertype *st, char *homehost)
83b6208e 543{
3da92f27 544 struct mdp_superblock_1 *sb = st->sb;
83b6208e
NB
545 int l = homehost ? strlen(homehost) : 0;
546
547 return (l > 0 && l < 32 &&
548 sb->set_name[l] == ':' &&
549 strncmp(sb->set_name, homehost, l) == 0);
550}
551
3da92f27 552static void uuid_from_super1(struct supertype *st, int uuid[4])
82d9eba6 553{
3da92f27 554 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
555 char *cuuid = (char*)uuid;
556 int i;
557 for (i=0; i<16; i++)
558 cuuid[i] = super->set_uuid[i];
559}
560
a5d85af7 561static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
82d9eba6 562{
3da92f27 563 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 564 int working = 0;
f21e18ca 565 unsigned int i;
a5d85af7
N
566 unsigned int role;
567 unsigned int map_disks = info->array.raid_disks;
82d9eba6
NB
568
569 info->array.major_version = 1;
b8ac1967 570 info->array.minor_version = st->minor_version;
82d9eba6
NB
571 info->array.patch_version = 0;
572 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
573 info->array.level = __le32_to_cpu(sb->level);
265e0f17 574 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
575 info->array.md_minor = -1;
576 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 577 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 578 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
4855f95c
NB
579 info->array.state =
580 (__le64_to_cpu(sb->resync_offset) >= __le64_to_cpu(sb->size))
581 ? 1 : 0;
353632d9
NB
582
583 info->data_offset = __le64_to_cpu(sb->data_offset);
584 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
585
586 info->disk.major = 0;
587 info->disk.minor = 0;
fbf8a0b7 588 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
589 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
590 __le32_to_cpu(sb->max_dev) > 512)
591 role = 0xfffe;
592 else
593 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
594
595 info->disk.raid_disk = -1;
596 switch(role) {
597 case 0xFFFF:
f22385f9 598 info->disk.state = 0; /* spare: not active, not sync, not faulty */
82d9eba6
NB
599 break;
600 case 0xFFFE:
601 info->disk.state = 1; /* faulty */
602 break;
603 default:
604 info->disk.state = 6; /* active and in sync */
605 info->disk.raid_disk = role;
606 }
607 info->events = __le64_to_cpu(sb->events);
1522c538 608 sprintf(info->text_version, "1.%d", st->minor_version);
a67dd8cc 609 info->safe_mode_delay = 200;
82d9eba6
NB
610
611 memcpy(info->uuid, sb->set_uuid, 16);
612
31317663
NB
613 strncpy(info->name, sb->set_name, 32);
614 info->name[32] = 0;
947fd4dd 615
921d9e16
N
616 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
617 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
618 else
619 info->recovery_start = MaxSector;
620
353632d9
NB
621 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
622 info->reshape_active = 1;
623 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
624 info->new_level = __le32_to_cpu(sb->new_level);
625 info->delta_disks = __le32_to_cpu(sb->delta_disks);
626 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 627 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
4180aa4d
N
628 if (info->delta_disks < 0)
629 info->array.raid_disks -= info->delta_disks;
353632d9
NB
630 } else
631 info->reshape_active = 0;
632
a5d85af7
N
633 if (map)
634 for (i=0; i<map_disks; i++)
635 map[i] = 0;
f21e18ca 636 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
82d9eba6 637 role = __le16_to_cpu(sb->dev_roles[i]);
a5d85af7 638 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
82d9eba6 639 working++;
a5d85af7
N
640 if (map && role < map_disks)
641 map[role] = 1;
642 }
82d9eba6
NB
643 }
644
645 info->array.working_disks = working;
646}
647
00bbdbda
N
648static struct mdinfo *container_content1(struct supertype *st, char *subarray)
649{
650 struct mdinfo *info;
651
652 if (subarray)
653 return NULL;
654
655 info = malloc(sizeof(*info));
656 getinfo_super1(st, info, NULL);
657 return info;
658}
659
68c7d6d7 660static int update_super1(struct supertype *st, struct mdinfo *info,
3da92f27 661 char *update,
e5eac01f
NB
662 char *devname, int verbose,
663 int uuid_set, char *homehost)
82d9eba6 664{
c4d831e1
NB
665 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
666 * For others, the return value is ignored.
667 */
82d9eba6 668 int rv = 0;
3da92f27 669 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 670
67a8c82d
NB
671 if (strcmp(update, "force-one")==0) {
672 /* Not enough devices for a working array,
673 * so bring this one up-to-date
674 */
c4d831e1
NB
675 if (sb->events != __cpu_to_le64(info->events))
676 rv = 1;
6d3d5804 677 sb->events = __cpu_to_le64(info->events);
1e2b2765 678 } else if (strcmp(update, "force-array")==0) {
67a8c82d
NB
679 /* Degraded array and 'force' requests to
680 * maybe need to mark it 'clean'.
681 */
82d9eba6
NB
682 switch(__le32_to_cpu(sb->level)) {
683 case 5: case 4: case 6:
684 /* need to force clean */
b7528a20 685 if (sb->resync_offset != MaxSector)
c4d831e1 686 rv = 1;
b7528a20 687 sb->resync_offset = MaxSector;
82d9eba6 688 }
1e2b2765 689 } else if (strcmp(update, "assemble")==0) {
82d9eba6
NB
690 int d = info->disk.number;
691 int want;
692 if (info->disk.state == 6)
693 want = __cpu_to_le32(info->disk.raid_disk);
694 else
695 want = 0xFFFF;
696 if (sb->dev_roles[d] != want) {
697 sb->dev_roles[d] = want;
698 rv = 1;
699 }
1e2b2765 700 } else if (strcmp(update, "linear-grow-new") == 0) {
f21e18ca 701 unsigned int i;
1c6cb603 702 int rfd, fd;
f21e18ca 703 unsigned int max = __le32_to_cpu(sb->max_dev);
f752781f
NB
704
705 for (i=0 ; i < max ; i++)
706 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
707 break;
708 sb->dev_number = __cpu_to_le32(i);
709 info->disk.number = i;
710 if (max >= __le32_to_cpu(sb->max_dev))
711 sb->max_dev = __cpu_to_le32(max+1);
712
713 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
714 read(rfd, sb->device_uuid, 16) != 16) {
caa0f6c6
N
715 __u32 r[4] = {random(), random(), random(), random()};
716 memcpy(sb->device_uuid, r, 16);
f752781f 717 }
7cbeb80e
N
718 if (rfd >= 0)
719 close(rfd);
f752781f
NB
720
721 sb->dev_roles[i] =
722 __cpu_to_le16(info->disk.raid_disk);
1c6cb603
N
723
724 fd = open(devname, O_RDONLY);
725 if (fd >= 0) {
726 unsigned long long ds;
727 get_dev_size(fd, devname, &ds);
728 close(fd);
729 ds >>= 9;
730 if (__le64_to_cpu(sb->super_offset) <
731 __le64_to_cpu(sb->data_offset)) {
732 sb->data_size = __cpu_to_le64(
733 ds - __le64_to_cpu(sb->data_offset));
734 } else {
735 ds -= 8*2;
736 ds &= ~(unsigned long long)(4*2-1);
737 sb->super_offset = __cpu_to_le64(ds);
738 sb->data_size = __cpu_to_le64(
739 ds - __le64_to_cpu(sb->data_offset));
740 }
741 }
1e2b2765 742 } else if (strcmp(update, "linear-grow-update") == 0) {
82d9eba6 743 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
f752781f
NB
744 sb->dev_roles[info->disk.number] =
745 __cpu_to_le16(info->disk.raid_disk);
1e2b2765 746 } else if (strcmp(update, "resync") == 0) {
82d9eba6 747 /* make sure resync happens */
434b7755 748 sb->resync_offset = 0ULL;
1e2b2765 749 } else if (strcmp(update, "uuid") == 0) {
350f29f9 750 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
bf4fb153 751
14263cf1
NB
752 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
753 struct bitmap_super_s *bm;
3da92f27 754 bm = (struct bitmap_super_s*)(st->sb+1024);
bf4fb153 755 memcpy(bm->uuid, sb->set_uuid, 16);
14263cf1 756 }
1e2b2765 757 } else if (strcmp(update, "homehost") == 0 &&
0237e0ca
NB
758 homehost) {
759 char *c;
760 update = "name";
761 c = strchr(sb->set_name, ':');
762 if (c)
763 strncpy(info->name, c+1, 31 - (c-sb->set_name));
764 else
765 strncpy(info->name, sb->set_name, 32);
26a0b8fd 766 info->name[32] = 0;
1e2b2765 767 } else if (strcmp(update, "name") == 0) {
c4f12c13
NB
768 if (info->name[0] == 0)
769 sprintf(info->name, "%d", info->array.md_minor);
770 memset(sb->set_name, 0, sizeof(sb->set_name));
771 if (homehost &&
772 strchr(info->name, ':') == NULL &&
773 strlen(homehost)+1+strlen(info->name) < 32) {
774 strcpy(sb->set_name, homehost);
775 strcat(sb->set_name, ":");
776 strcat(sb->set_name, info->name);
777 } else
778 strcpy(sb->set_name, info->name);
1e2b2765 779 } else if (strcmp(update, "devicesize") == 0 &&
bee8ec56
NB
780 __le64_to_cpu(sb->super_offset) <
781 __le64_to_cpu(sb->data_offset)) {
782 /* set data_size to device size less data_offset */
783 struct misc_dev_info *misc = (struct misc_dev_info*)
6416d527 784 (st->sb + 1024 + 512);
eb9199fb
NB
785 printf("Size was %llu\n", (unsigned long long)
786 __le64_to_cpu(sb->data_size));
bee8ec56
NB
787 sb->data_size = __cpu_to_le64(
788 misc->device_size - __le64_to_cpu(sb->data_offset));
eb9199fb
NB
789 printf("Size is %llu\n", (unsigned long long)
790 __le64_to_cpu(sb->data_size));
1e2b2765 791 } else if (strcmp(update, "_reshape_progress")==0)
353632d9 792 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1e2b2765
N
793 else
794 rv = -1;
82d9eba6
NB
795
796 sb->sb_csum = calc_sb_1_csum(sb);
797 return rv;
798}
799
3da92f27 800static int init_super1(struct supertype *st, mdu_array_info_t *info,
3d3dd91e 801 unsigned long long size, char *name, char *homehost, int *uuid)
82d9eba6 802{
6416d527 803 struct mdp_superblock_1 *sb;
82d9eba6 804 int spares;
34163fc7 805 int rfd;
05697ec1 806 char defname[10];
6416d527 807
3d2c4fc7
DW
808 if (posix_memalign((void**)&sb, 512, (1024 + 512 +
809 sizeof(struct misc_dev_info))) != 0) {
810 fprintf(stderr, Name
811 ": %s could not allocate superblock\n", __func__);
812 return 0;
813 }
82d9eba6
NB
814 memset(sb, 0, 1024);
815
64557c33 816 st->sb = sb;
ba7eb04f 817 if (info == NULL) {
82d9eba6
NB
818 /* zeroing superblock */
819 return 0;
05697ec1 820 }
82d9eba6
NB
821
822 spares = info->working_disks - info->active_disks;
823 if (info->raid_disks + spares > 384) {
824 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
825 info->raid_disks , spares, 384);
826 return 0;
827 }
828
82d9eba6
NB
829 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
830 sb->major_version = __cpu_to_le32(1);
831 sb->feature_map = 0;
832 sb->pad0 = 0;
34163fc7 833
350f29f9
NB
834 if (uuid)
835 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
836 else {
3d3dd91e
NB
837 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
838 read(rfd, sb->set_uuid, 16) != 16) {
caa0f6c6
N
839 __u32 r[4] = {random(), random(), random(), random()};
840 memcpy(sb->set_uuid, r, 16);
3d3dd91e
NB
841 }
842 if (rfd >= 0) close(rfd);
34163fc7 843 }
82d9eba6 844
05697ec1
NB
845 if (name == NULL || *name == 0) {
846 sprintf(defname, "%d", info->md_minor);
847 name = defname;
848 }
947fd4dd 849 memset(sb->set_name, 0, 32);
05697ec1
NB
850 if (homehost &&
851 strchr(name, ':')== NULL &&
852 strlen(homehost)+1+strlen(name) < 32) {
853 strcpy(sb->set_name, homehost);
854 strcat(sb->set_name, ":");
855 strcat(sb->set_name, name);
856 } else
857 strcpy(sb->set_name, name);
82d9eba6
NB
858
859 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
860 sb->level = __cpu_to_le32(info->level);
ea329559 861 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 862 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
863 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
864 sb->raid_disks = __cpu_to_le32(info->raid_disks);
865
866 sb->data_offset = __cpu_to_le64(0);
867 sb->data_size = __cpu_to_le64(0);
868 sb->super_offset = __cpu_to_le64(0);
869 sb->recovery_offset = __cpu_to_le64(0);
870
871 sb->utime = sb->ctime;
872 sb->events = __cpu_to_le64(1);
34321279 873 if (info->state & (1<<MD_SB_CLEAN))
b7528a20 874 sb->resync_offset = MaxSector;
82d9eba6
NB
875 else
876 sb->resync_offset = 0;
34163fc7 877 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
878 sizeof(sb->dev_roles[0]));
879 memset(sb->pad3, 0, sizeof(sb->pad3));
880
881 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
882
82d9eba6
NB
883 return 1;
884}
885
111d01fc
NB
886struct devinfo {
887 int fd;
888 char *devname;
889 mdu_disk_info_t disk;
890 struct devinfo *next;
891};
0e600426 892#ifndef MDASSEMBLE
82d9eba6 893/* Add a device to the superblock being created */
f20c3968 894static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
111d01fc 895 int fd, char *devname)
82d9eba6 896{
3da92f27 897 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 898 __u16 *rp = sb->dev_roles + dk->number;
111d01fc
NB
899 struct devinfo *di, **dip;
900
dfd4d8ee 901 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 902 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 903 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 904 *rp = 0xffff;
34163fc7 905 else
82d9eba6 906 *rp = 0xfffe;
111d01fc 907
f21e18ca 908 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
9df04a4f
N
909 __le32_to_cpu(sb->max_dev) < 384)
910 sb->max_dev = __cpu_to_le32(dk->number+1);
8844e291 911
d2ca6449
NB
912 sb->dev_number = __cpu_to_le32(dk->number);
913 sb->sb_csum = calc_sb_1_csum(sb);
914
111d01fc
NB
915 dip = (struct devinfo **)&st->info;
916 while (*dip)
917 dip = &(*dip)->next;
918 di = malloc(sizeof(struct devinfo));
919 di->fd = fd;
920 di->devname = devname;
921 di->disk = *dk;
922 di->next = NULL;
923 *dip = di;
f20c3968
DW
924
925 return 0;
82d9eba6 926}
0e600426 927#endif
82d9eba6 928
3da92f27 929static void locate_bitmap1(struct supertype *st, int fd);
14263cf1 930
3da92f27 931static int store_super1(struct supertype *st, int fd)
82d9eba6 932{
3da92f27 933 struct mdp_superblock_1 *sb = st->sb;
6fbba4c9 934 unsigned long long sb_offset;
82d9eba6 935 int sbsize;
5dd497ee 936 unsigned long long dsize;
96395475 937
beae1dfe
NB
938 if (!get_dev_size(fd, NULL, &dsize))
939 return 1;
940
941 dsize >>= 9;
96395475 942
5dd497ee 943 if (dsize < 24)
96395475
NB
944 return 2;
945
946 /*
947 * Calculate the position of the superblock.
948 * It is always aligned to a 4K boundary and
949 * depending on minor_version, it can be:
950 * 0: At least 8K, but less than 12K, from end of device
951 * 1: At start of device
952 * 2: 4K from start of device.
953 */
954 switch(st->minor_version) {
955 case 0:
5dd497ee 956 sb_offset = dsize;
96395475
NB
957 sb_offset -= 8*2;
958 sb_offset &= ~(4*2-1);
959 break;
960 case 1:
6fbba4c9 961 sb_offset = 0;
96395475
NB
962 break;
963 case 2:
964 sb_offset = 4*2;
965 break;
966 default:
967 return -EINVAL;
968 }
969
82d9eba6 970
34163fc7 971
6fbba4c9
NB
972 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
973 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
974 ) {
975 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
976 abort();
977 }
82d9eba6 978
6fbba4c9 979 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
980 return 3;
981
982 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
6416d527 983 sbsize = (sbsize+511)&(~511UL);
82d9eba6 984
5ea022a1 985 if (awrite(fd, sb, sbsize) != sbsize)
82d9eba6
NB
986 return 4;
987
14263cf1
NB
988 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
989 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 990 (((char*)sb)+1024);
14263cf1 991 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
3da92f27 992 locate_bitmap1(st, fd);
5ea022a1
N
993 if (awrite(fd, bm, sizeof(*bm)) !=
994 sizeof(*bm))
9fca7d62 995 return 5;
14263cf1
NB
996 }
997 }
570c0542 998 fsync(fd);
82d9eba6
NB
999 return 0;
1000}
1001
3da92f27 1002static int load_super1(struct supertype *st, int fd, char *devname);
892debc8 1003
2fb749d1
NB
1004static unsigned long choose_bm_space(unsigned long devsize)
1005{
1006 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1007 * if bigger than 200Gig, save 128k
a380e275
N
1008 * NOTE: result must be multiple of 4K else bad things happen
1009 * on 4K-sector devices.
2fb749d1 1010 */
69646c14 1011 if (devsize < 64*2) return 0;
2fb749d1
NB
1012 if (devsize - 64*2 >= 200*1024*1024*2)
1013 return 128*2;
1014 if (devsize - 4*2 > 8*1024*1024*2)
1015 return 64*2;
1016 return 4*2;
1017}
1018
111d01fc
NB
1019#ifndef MDASSEMBLE
1020static int write_init_super1(struct supertype *st)
82d9eba6 1021{
3da92f27
NB
1022 struct mdp_superblock_1 *sb = st->sb;
1023 struct supertype refst;
dfe47e00 1024 int rfd;
111d01fc 1025 int rv = 0;
f21e18ca 1026 unsigned long long bm_space;
a380e275 1027 unsigned long long reserved;
111d01fc 1028 struct devinfo *di;
8fac0577 1029 unsigned long long dsize, array_size;
f21e18ca 1030 unsigned long long sb_offset;
82d9eba6 1031
111d01fc
NB
1032 for (di = st->info; di && ! rv ; di = di->next) {
1033 if (di->disk.state == 1)
1034 continue;
a0c8a17f
NB
1035 if (di->fd < 0)
1036 continue;
82d9eba6 1037
9277cc77
N
1038 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
1039 ;
82d9eba6 1040
111d01fc
NB
1041 sb->dev_number = __cpu_to_le32(di->disk.number);
1042 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1043 sb->devflags |= __cpu_to_le32(WriteMostly1);
892debc8 1044
111d01fc
NB
1045 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1046 read(rfd, sb->device_uuid, 16) != 16) {
83208785
N
1047 __u32 r[4] = {random(), random(), random(), random()};
1048 memcpy(sb->device_uuid, r, 16);
111d01fc 1049 }
83208785
N
1050 if (rfd >= 0)
1051 close(rfd);
1052
111d01fc
NB
1053 sb->events = 0;
1054
1055 refst =*st;
1056 refst.sb = NULL;
1057 if (load_super1(&refst, di->fd, NULL)==0) {
1058 struct mdp_superblock_1 *refsb = refst.sb;
1059
1060 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1061 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1062 /* same array, so preserve events and
1063 * dev_number */
1064 sb->events = refsb->events;
1065 /* bugs in 2.6.17 and earlier mean the
1066 * dev_number chosen in Manage must be preserved
1067 */
1068 if (get_linux_version() >= 2006018)
1069 sb->dev_number = refsb->dev_number;
1070 }
1071 free(refsb);
892debc8 1072 }
34163fc7 1073
111d01fc
NB
1074 if (!get_dev_size(di->fd, NULL, &dsize))
1075 return 1;
1076 dsize >>= 9;
82d9eba6 1077
111d01fc
NB
1078 if (dsize < 24) {
1079 close(di->fd);
1080 return 2;
1081 }
82d9eba6
NB
1082
1083
111d01fc
NB
1084 /*
1085 * Calculate the position of the superblock.
1086 * It is always aligned to a 4K boundary and
1087 * depending on minor_version, it can be:
1088 * 0: At least 8K, but less than 12K, from end of device
1089 * 1: At start of device
1090 * 2: 4K from start of device.
1091 * Depending on the array size, we might leave extra space
1092 * for a bitmap.
1093 */
1094 array_size = __le64_to_cpu(sb->size);
1095 /* work out how much space we left for a bitmap */
1096 bm_space = choose_bm_space(array_size);
1097
1098 switch(st->minor_version) {
1099 case 0:
1100 sb_offset = dsize;
1101 sb_offset -= 8*2;
1102 sb_offset &= ~(4*2-1);
1103 sb->super_offset = __cpu_to_le64(sb_offset);
1104 sb->data_offset = __cpu_to_le64(0);
f21e18ca 1105 if (sb_offset < array_size + bm_space)
a380e275 1106 bm_space = sb_offset - array_size;
111d01fc
NB
1107 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1108 break;
1109 case 1:
1110 sb->super_offset = __cpu_to_le64(0);
a380e275
N
1111 reserved = bm_space + 4*2;
1112 /* Try for multiple of 1Meg so it is nicely aligned */
1113 #define ONE_MEG (2*1024)
1114 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1115 if (reserved + __le64_to_cpu(sb->size) > dsize)
1116 reserved = dsize - __le64_to_cpu(sb->size);
1117 /* force 4K alignment */
1118 reserved &= ~7ULL;
1119
1120 sb->data_offset = __cpu_to_le64(reserved);
1121 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1122 break;
1123 case 2:
1124 sb_offset = 4*2;
1125 sb->super_offset = __cpu_to_le64(4*2);
1126 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1127 > dsize)
1128 bm_space = dsize - __le64_to_cpu(sb->size)
1129 - 4*2 - 4*2;
a380e275
N
1130
1131 reserved = bm_space + 4*2 + 4*2;
1132 /* Try for multiple of 1Meg so it is nicely aligned */
1133 #define ONE_MEG (2*1024)
1134 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1135 if (reserved + __le64_to_cpu(sb->size) > dsize)
1136 reserved = dsize - __le64_to_cpu(sb->size);
1137 /* force 4K alignment */
1138 reserved &= ~7ULL;
1139
1140 sb->data_offset = __cpu_to_le64(reserved);
1141 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1142 break;
1143 default:
1144 return -EINVAL;
1145 }
82d9eba6
NB
1146
1147
111d01fc
NB
1148 sb->sb_csum = calc_sb_1_csum(sb);
1149 rv = store_super1(st, di->fd);
1150 if (rv)
1151 fprintf(stderr,
1152 Name ": failed to write superblock to %s\n",
1153 di->devname);
34163fc7 1154
111d01fc
NB
1155 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1156 rv = st->ss->write_bitmap(st, di->fd);
1157 close(di->fd);
1158 di->fd = -1;
1159 }
82d9eba6
NB
1160 return rv;
1161}
111d01fc 1162#endif
82d9eba6 1163
64557c33 1164static int compare_super1(struct supertype *st, struct supertype *tst)
82d9eba6
NB
1165{
1166 /*
1167 * return:
1168 * 0 same, or first was empty, and second was copied
1169 * 1 second had wrong number
1170 * 2 wrong uuid
1171 * 3 wrong other info
1172 */
64557c33
NB
1173 struct mdp_superblock_1 *first = st->sb;
1174 struct mdp_superblock_1 *second = tst->sb;
82d9eba6
NB
1175
1176 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1177 return 1;
1178 if (second->major_version != __cpu_to_le32(1))
1179 return 1;
1180
1181 if (!first) {
3d2c4fc7 1182 if (posix_memalign((void**)&first, 512,
6416d527 1183 1024 + 512 +
3d2c4fc7
DW
1184 sizeof(struct misc_dev_info)) != 0) {
1185 fprintf(stderr, Name
1186 ": %s could not allocate superblock\n", __func__);
1187 return 1;
1188 }
6416d527 1189 memcpy(first, second, 1024 + 512 +
bee8ec56 1190 sizeof(struct misc_dev_info));
64557c33 1191 st->sb = first;
82d9eba6
NB
1192 return 0;
1193 }
1194 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1195 return 2;
1196
1197 if (first->ctime != second->ctime ||
1198 first->level != second->level ||
1199 first->layout != second->layout ||
1200 first->size != second->size ||
1201 first->chunksize != second->chunksize ||
1202 first->raid_disks != second->raid_disks)
1203 return 3;
1204 return 0;
1205}
1206
3da92f27
NB
1207static void free_super1(struct supertype *st);
1208
1209static int load_super1(struct supertype *st, int fd, char *devname)
82d9eba6 1210{
5dd497ee 1211 unsigned long long dsize;
82d9eba6
NB
1212 unsigned long long sb_offset;
1213 struct mdp_superblock_1 *super;
14263cf1
NB
1214 int uuid[4];
1215 struct bitmap_super_s *bsb;
bee8ec56 1216 struct misc_dev_info *misc;
82d9eba6 1217
3da92f27 1218 free_super1(st);
82d9eba6 1219
a17a3de3 1220 if (st->ss == NULL || st->minor_version == -1) {
570c0542 1221 int bestvers = -1;
23dc1ae8 1222 struct supertype tst;
570c0542
NB
1223 __u64 bestctime = 0;
1224 /* guess... choose latest ctime */
ef609477 1225 memset(&tst, 0, sizeof(tst));
23dc1ae8
NB
1226 tst.ss = &super1;
1227 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
3da92f27
NB
1228 switch(load_super1(&tst, fd, devname)) {
1229 case 0: super = tst.sb;
570c0542
NB
1230 if (bestvers == -1 ||
1231 bestctime < __le64_to_cpu(super->ctime)) {
23dc1ae8 1232 bestvers = tst.minor_version;
570c0542
NB
1233 bestctime = __le64_to_cpu(super->ctime);
1234 }
1235 free(super);
3da92f27 1236 tst.sb = NULL;
570c0542 1237 break;
23dc1ae8 1238 case 1: return 1; /*bad device */
82d9eba6
NB
1239 case 2: break; /* bad, try next */
1240 }
1241 }
570c0542
NB
1242 if (bestvers != -1) {
1243 int rv;
23dc1ae8
NB
1244 tst.minor_version = bestvers;
1245 tst.ss = &super1;
1246 tst.max_devs = 384;
3da92f27 1247 rv = load_super1(&tst, fd, devname);
23dc1ae8
NB
1248 if (rv == 0)
1249 *st = tst;
570c0542
NB
1250 return rv;
1251 }
82d9eba6
NB
1252 return 2;
1253 }
beae1dfe
NB
1254 if (!get_dev_size(fd, devname, &dsize))
1255 return 1;
1256 dsize >>= 9;
82d9eba6 1257
5dd497ee 1258 if (dsize < 24) {
82d9eba6 1259 if (devname)
5dd497ee
NB
1260 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1261 devname, dsize);
82d9eba6
NB
1262 return 1;
1263 }
1264
1265 /*
1266 * Calculate the position of the superblock.
1267 * It is always aligned to a 4K boundary and
14263cf1 1268 * depending on minor_version, it can be:
82d9eba6
NB
1269 * 0: At least 8K, but less than 12K, from end of device
1270 * 1: At start of device
1271 * 2: 4K from start of device.
1272 */
1273 switch(st->minor_version) {
1274 case 0:
5dd497ee 1275 sb_offset = dsize;
82d9eba6
NB
1276 sb_offset -= 8*2;
1277 sb_offset &= ~(4*2-1);
1278 break;
1279 case 1:
1280 sb_offset = 0;
1281 break;
1282 case 2:
1283 sb_offset = 4*2;
1284 break;
1285 default:
1286 return -EINVAL;
1287 }
1288
1289 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1290
1291
1292 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1293 if (devname)
1294 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1295 devname, strerror(errno));
1296 return 1;
1297 }
1298
3d2c4fc7 1299 if (posix_memalign((void**)&super, 512,
6416d527 1300 1024 + 512 +
3d2c4fc7
DW
1301 sizeof(struct misc_dev_info)) != 0) {
1302 fprintf(stderr, Name ": %s could not allocate superblock\n",
1303 __func__);
1304 return 1;
1305 }
82d9eba6 1306
5ea022a1 1307 if (aread(fd, super, 1024) != 1024) {
82d9eba6
NB
1308 if (devname)
1309 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1310 devname);
1311 free(super);
1312 return 1;
1313 }
1314
1315 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1316 if (devname)
1317 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1318 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1319 free(super);
1320 return 2;
1321 }
1322
1323 if (__le32_to_cpu(super->major_version) != 1) {
1324 if (devname)
1325 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1326 devname, __le32_to_cpu(super->major_version));
1327 free(super);
1328 return 2;
1329 }
1330 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1331 if (devname)
1332 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1333 devname);
1334 free(super);
1335 return 2;
1336 }
64557c33 1337 st->sb = super;
14263cf1 1338
bee8ec56
NB
1339 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1340
6416d527 1341 misc = (struct misc_dev_info*) (((char*)super)+1024+512);
bee8ec56
NB
1342 misc->device_size = dsize;
1343
14263cf1
NB
1344 /* Now check on the bitmap superblock */
1345 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1346 return 0;
1347 /* Read the bitmap superblock and make sure it looks
1348 * valid. If it doesn't clear the bit. An --assemble --force
1349 * should get that written out.
1350 */
3da92f27 1351 locate_bitmap1(st, fd);
5ea022a1 1352 if (aread(fd, ((char*)super)+1024, 512)
6416d527 1353 != 512)
14263cf1
NB
1354 goto no_bitmap;
1355
3da92f27 1356 uuid_from_super1(st, uuid);
14263cf1
NB
1357 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1358 memcmp(bsb->uuid, uuid, 16) != 0)
1359 goto no_bitmap;
1360 return 0;
1361
1362 no_bitmap:
1363 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1364 return 0;
1365}
1366
1367
1368static struct supertype *match_metadata_desc1(char *arg)
1369{
1370 struct supertype *st = malloc(sizeof(*st));
1371 if (!st) return st;
1372
ef609477 1373 memset(st, 0, sizeof(*st));
d1d599ea 1374 st->container_dev = NoMdDev;
82d9eba6 1375 st->ss = &super1;
ea329559 1376 st->max_devs = 384;
64557c33 1377 st->sb = NULL;
9b2a22d3
N
1378 /* leading zeros can be safely ignored. --detail generates them. */
1379 while (*arg == '0')
1380 arg++;
1381 if (strcmp(arg, "1.0") == 0 ||
1382 strcmp(arg, "1.00") == 0) {
82d9eba6
NB
1383 st->minor_version = 0;
1384 return st;
1385 }
9b2a22d3 1386 if (strcmp(arg, "1.1") == 0 ||
50827504 1387 strcmp(arg, "1.01") == 0
7d5c3964 1388 ) {
82d9eba6
NB
1389 st->minor_version = 1;
1390 return st;
1391 }
9b2a22d3 1392 if (strcmp(arg, "1.2") == 0 ||
26f467a9 1393#ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
ad90adb6 1394 strcmp(arg, "default") == 0 ||
26f467a9 1395#endif /* DEFAULT_OLD_METADATA */
9b2a22d3 1396 strcmp(arg, "1.02") == 0) {
82d9eba6
NB
1397 st->minor_version = 2;
1398 return st;
1399 }
a17a3de3 1400 if (strcmp(arg, "1") == 0 ||
17f25ca6 1401 strcmp(arg, "default") == 0) {
a17a3de3
DL
1402 st->minor_version = -1;
1403 return st;
1404 }
82d9eba6
NB
1405
1406 free(st);
1407 return NULL;
1408}
1409
34163fc7
NB
1410/* find available size on device with this devsize, using
1411 * superblock type st, and reserving 'reserve' sectors for
1412 * a possible bitmap
1413 */
1bf4e2d9 1414static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6 1415{
2a528478 1416 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
1417 if (devsize < 24)
1418 return 0;
1419
2a528478
N
1420 if (super == NULL)
1421 /* creating: allow suitable space for bitmap */
1422 devsize -= choose_bm_space(devsize);
1423#ifndef MDASSEMBLE
1424 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1425 /* hot-add. allow for actual size of bitmap */
1426 struct bitmap_super_s *bsb;
1427 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1428 devsize -= bitmap_sectors(bsb);
1429 }
1430#endif
21e92547 1431
a380e275
N
1432 if (st->minor_version < 0)
1433 /* not specified, so time to set default */
1434 st->minor_version = 2;
1435 if (super == NULL && st->minor_version > 0) {
1436 /* haven't committed to a size yet, so allow some
1437 * slack for alignment of data_offset.
1438 * We haven't access to device details so allow
1439 * 1 Meg if bigger than 1Gig
1440 */
1441 if (devsize > 1024*1024*2)
1442 devsize -= 1024*2;
1443 }
34163fc7
NB
1444 switch(st->minor_version) {
1445 case 0:
1bf4e2d9
NB
1446 /* at end */
1447 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1448 case 1:
1bf4e2d9
NB
1449 /* at start, 4K for superblock and possible bitmap */
1450 return devsize - 4*2;
34163fc7 1451 case 2:
1bf4e2d9
NB
1452 /* 4k from start, 4K for superblock and possible bitmap */
1453 return devsize - (4+4)*2;
34163fc7
NB
1454 }
1455 return 0;
1456}
1457
1bf4e2d9 1458static int
3da92f27 1459add_internal_bitmap1(struct supertype *st,
199171a2
NB
1460 int *chunkp, int delay, int write_behind,
1461 unsigned long long size,
21e92547 1462 int may_change, int major)
34163fc7
NB
1463{
1464 /*
1bf4e2d9
NB
1465 * If not may_change, then this is a 'Grow', and the bitmap
1466 * must fit after the superblock.
1467 * If may_change, then this is create, and we can put the bitmap
1468 * before the superblock if we like, or may move the start.
199171a2
NB
1469 * If !may_change, the bitmap MUST live at offset of 1K, until
1470 * we get a sysfs interface.
34163fc7 1471 *
f9c25f1d 1472 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1473 */
1474
1bf4e2d9 1475 unsigned long long bits;
199171a2 1476 unsigned long long max_bits;
34163fc7 1477 unsigned long long min_chunk;
199171a2 1478 long offset;
f21e18ca 1479 unsigned long long chunk = *chunkp;
37dfc3d6 1480 int room = 0;
3da92f27 1481 struct mdp_superblock_1 *sb = st->sb;
34163fc7
NB
1482 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1483
199171a2
NB
1484 switch(st->minor_version) {
1485 case 0:
a31128d2
N
1486 /* either 3K after the superblock (when hot-add),
1487 * or some amount of space before.
199171a2
NB
1488 */
1489 if (may_change) {
1490 /* We are creating array, so we *know* how much room has
1491 * been left.
1492 */
1493 offset = 0;
2fb749d1 1494 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1495 } else {
1496 room = __le64_to_cpu(sb->super_offset)
1497 - __le64_to_cpu(sb->data_offset)
1498 - __le64_to_cpu(sb->data_size);
1499 /* remove '1 ||' when we can set offset via sysfs */
1500 if (1 || (room < 3*2 &&
1501 __le32_to_cpu(sb->max_dev) <= 384)) {
1502 room = 3*2;
1503 offset = 1*2;
1504 } else {
1505 offset = 0; /* means movable offset */
1506 }
1507 }
1508 break;
1509 case 1:
1510 case 2: /* between superblock and data */
1511 if (may_change) {
1512 offset = 4*2;
2fb749d1 1513 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1514 } else {
1515 room = __le64_to_cpu(sb->data_offset)
1516 - __le64_to_cpu(sb->super_offset);
1517 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1518 room -= 2;
1519 offset = 2;
1520 } else {
1521 room -= 4*2;
1522 offset = 4*2;
1523 }
1524 }
1525 break;
ae491d1e
NB
1526 default:
1527 return 0;
199171a2 1528 }
1bf4e2d9 1529
199171a2
NB
1530 if (chunk == UnSet && room > 128*2)
1531 /* Limit to 128K of bitmap when chunk size not requested */
1532 room = 128*2;
1bf4e2d9 1533
199171a2 1534 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1535
1536 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1537 bits = (size*512)/min_chunk +1;
34163fc7
NB
1538 while (bits > max_bits) {
1539 min_chunk *= 2;
1540 bits = (bits+1)/2;
1541 }
b8ab2a50
N
1542 if (chunk == UnSet) {
1543 /* For practical purpose, 64Meg is a good
1544 * default chunk size for internal bitmaps.
1545 */
34163fc7 1546 chunk = min_chunk;
b8ab2a50
N
1547 if (chunk < 64*1024*1024)
1548 chunk = 64*1024*1024;
1549 } else if (chunk < min_chunk)
34163fc7 1550 return 0; /* chunk size too small */
21e92547
NB
1551 if (chunk == 0) /* rounding problem */
1552 return 0;
34163fc7 1553
199171a2 1554 if (offset == 0) {
a31128d2
N
1555 /* start bitmap on a 4K boundary with enough space for
1556 * the bitmap
1557 */
199171a2 1558 bits = (size*512) / chunk + 1;
a31128d2
N
1559 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1560 room *= 8; /* convert 4K blocks to sectors */
199171a2
NB
1561 offset = -room;
1562 }
1563
1564 sb->bitmap_offset = __cpu_to_le32(offset);
34163fc7
NB
1565
1566 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1567 memset(bms, 0, sizeof(*bms));
34163fc7 1568 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1569 bms->version = __cpu_to_le32(major);
3da92f27 1570 uuid_from_super1(st, (int*)bms->uuid);
34163fc7
NB
1571 bms->chunksize = __cpu_to_le32(chunk);
1572 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1573 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1574 bms->write_behind = __cpu_to_le32(write_behind);
1575
199171a2 1576 *chunkp = chunk;
34163fc7
NB
1577 return 1;
1578}
1579
1580
3da92f27 1581static void locate_bitmap1(struct supertype *st, int fd)
34163fc7 1582{
34163fc7 1583 unsigned long long offset;
1e0d770c
NB
1584 struct mdp_superblock_1 *sb;
1585 int mustfree = 0;
34163fc7 1586
3da92f27
NB
1587 if (!st->sb) {
1588 if (st->ss->load_super(st, fd, NULL))
f6d75de8 1589 return; /* no error I hope... */
1e0d770c
NB
1590 mustfree = 1;
1591 }
3da92f27 1592 sb = st->sb;
e478dc86 1593
1bf4e2d9 1594 offset = __le64_to_cpu(sb->super_offset);
68754bd1 1595 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1596 if (mustfree)
f6d75de8 1597 free(sb);
1bf4e2d9 1598 lseek64(fd, offset<<9, 0);
34163fc7
NB
1599}
1600
3da92f27 1601static int write_bitmap1(struct supertype *st, int fd)
34163fc7 1602{
3da92f27 1603 struct mdp_superblock_1 *sb = st->sb;
1bf4e2d9 1604 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1605 int rv = 0;
1606
1607 int towrite, n;
5ea022a1 1608 char *buf = (char*)(((long)(abuf+4096))&~4095UL);
34163fc7 1609
3da92f27 1610 locate_bitmap1(st, fd);
34163fc7 1611
6416d527
NB
1612 memset(buf, 0xff, 4096);
1613 memcpy(buf, ((char*)sb)+1024, sizeof(bitmap_super_t));
1614
1bf4e2d9
NB
1615 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1616 towrite = (towrite+7) >> 3; /* bits to bytes */
6416d527
NB
1617 towrite += sizeof(bitmap_super_t);
1618 towrite = ROUND_UP(towrite, 512);
34163fc7
NB
1619 while (towrite > 0) {
1620 n = towrite;
6416d527
NB
1621 if (n > 4096)
1622 n = 4096;
34163fc7
NB
1623 n = write(fd, buf, n);
1624 if (n > 0)
1625 towrite -= n;
1626 else
1627 break;
6416d527 1628 memset(buf, 0xff, 4096);
34163fc7
NB
1629 }
1630 fsync(fd);
1631 if (towrite)
1632 rv = -2;
1633
1634 return rv;
82d9eba6
NB
1635}
1636
3da92f27 1637static void free_super1(struct supertype *st)
df37ffc0 1638{
3da92f27
NB
1639 if (st->sb)
1640 free(st->sb);
1641 st->sb = NULL;
df37ffc0
NB
1642}
1643
0e600426 1644#ifndef MDASSEMBLE
17f25ca6
NB
1645static int validate_geometry1(struct supertype *st, int level,
1646 int layout, int raiddisks,
1647 int chunk, unsigned long long size,
2c514b71
NB
1648 char *subdev, unsigned long long *freesize,
1649 int verbose)
17f25ca6
NB
1650{
1651 unsigned long long ldsize;
1652 int fd;
1653
b42f577a
N
1654 if (level == LEVEL_CONTAINER) {
1655 if (verbose)
1656 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
17f25ca6 1657 return 0;
b42f577a 1658 }
17f25ca6
NB
1659 if (!subdev)
1660 return 1;
1661
1662 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1663 if (fd < 0) {
2c514b71
NB
1664 if (verbose)
1665 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1666 subdev, strerror(errno));
17f25ca6
NB
1667 return 0;
1668 }
2c514b71 1669
17f25ca6
NB
1670 if (!get_dev_size(fd, subdev, &ldsize)) {
1671 close(fd);
1672 return 0;
1673 }
1674 close(fd);
1675
1676 *freesize = avail_size1(st, ldsize >> 9);
1677 return 1;
1678}
0e600426 1679#endif /* MDASSEMBLE */
17f25ca6 1680
82d9eba6 1681struct superswitch super1 = {
c7654afc 1682#ifndef MDASSEMBLE
82d9eba6
NB
1683 .examine_super = examine_super1,
1684 .brief_examine_super = brief_examine_super1,
0d726f17 1685 .export_examine_super = export_examine_super1,
82d9eba6
NB
1686 .detail_super = detail_super1,
1687 .brief_detail_super = brief_detail_super1,
0d726f17 1688 .export_detail_super = export_detail_super1,
111d01fc 1689 .write_init_super = write_init_super1,
0e600426
N
1690 .validate_geometry = validate_geometry1,
1691 .add_to_super = add_to_super1,
c7654afc 1692#endif
83b6208e 1693 .match_home = match_home1,
82d9eba6
NB
1694 .uuid_from_super = uuid_from_super1,
1695 .getinfo_super = getinfo_super1,
00bbdbda 1696 .container_content = container_content1,
82d9eba6 1697 .update_super = update_super1,
82d9eba6 1698 .init_super = init_super1,
82d9eba6 1699 .store_super = store_super1,
82d9eba6
NB
1700 .compare_super = compare_super1,
1701 .load_super = load_super1,
1702 .match_metadata_desc = match_metadata_desc1,
1703 .avail_size = avail_size1,
34163fc7
NB
1704 .add_internal_bitmap = add_internal_bitmap1,
1705 .locate_bitmap = locate_bitmap1,
1706 .write_bitmap = write_bitmap1,
df37ffc0 1707 .free_super = free_super1,
f277ce36
NB
1708#if __BYTE_ORDER == BIG_ENDIAN
1709 .swapuuid = 0,
1710#else
1711 .swapuuid = 1,
1712#endif
31015d57 1713 .name = "1.x",
82d9eba6 1714};