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