]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super1.c
restore_backup() throws core dump
[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 }
603 info->events = __le64_to_cpu(sb->events);
1522c538 604 sprintf(info->text_version, "1.%d", st->minor_version);
a67dd8cc 605 info->safe_mode_delay = 200;
82d9eba6
NB
606
607 memcpy(info->uuid, sb->set_uuid, 16);
608
31317663
NB
609 strncpy(info->name, sb->set_name, 32);
610 info->name[32] = 0;
947fd4dd 611
921d9e16
N
612 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
613 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
614 else
615 info->recovery_start = MaxSector;
616
353632d9
NB
617 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
618 info->reshape_active = 1;
619 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
620 info->new_level = __le32_to_cpu(sb->new_level);
621 info->delta_disks = __le32_to_cpu(sb->delta_disks);
622 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 623 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
4180aa4d
N
624 if (info->delta_disks < 0)
625 info->array.raid_disks -= info->delta_disks;
353632d9
NB
626 } else
627 info->reshape_active = 0;
628
a5d85af7
N
629 if (map)
630 for (i=0; i<map_disks; i++)
631 map[i] = 0;
f21e18ca 632 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
82d9eba6 633 role = __le16_to_cpu(sb->dev_roles[i]);
a5d85af7 634 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
82d9eba6 635 working++;
a5d85af7
N
636 if (map && role < map_disks)
637 map[role] = 1;
638 }
82d9eba6
NB
639 }
640
641 info->array.working_disks = working;
642}
643
00bbdbda
N
644static struct mdinfo *container_content1(struct supertype *st, char *subarray)
645{
646 struct mdinfo *info;
647
648 if (subarray)
649 return NULL;
650
651 info = malloc(sizeof(*info));
652 getinfo_super1(st, info, NULL);
653 return info;
654}
655
68c7d6d7 656static int update_super1(struct supertype *st, struct mdinfo *info,
3da92f27 657 char *update,
e5eac01f
NB
658 char *devname, int verbose,
659 int uuid_set, char *homehost)
82d9eba6 660{
5a31170d
N
661 /* NOTE: for 'assemble' and 'force' we need to return non-zero
662 * if any change was made. For others, the return value is
663 * ignored.
c4d831e1 664 */
82d9eba6 665 int rv = 0;
3da92f27 666 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 667
67a8c82d
NB
668 if (strcmp(update, "force-one")==0) {
669 /* Not enough devices for a working array,
670 * so bring this one up-to-date
671 */
c4d831e1
NB
672 if (sb->events != __cpu_to_le64(info->events))
673 rv = 1;
6d3d5804 674 sb->events = __cpu_to_le64(info->events);
1e2b2765 675 } else if (strcmp(update, "force-array")==0) {
67a8c82d
NB
676 /* Degraded array and 'force' requests to
677 * maybe need to mark it 'clean'.
678 */
82d9eba6
NB
679 switch(__le32_to_cpu(sb->level)) {
680 case 5: case 4: case 6:
681 /* need to force clean */
b7528a20 682 if (sb->resync_offset != MaxSector)
c4d831e1 683 rv = 1;
b7528a20 684 sb->resync_offset = MaxSector;
82d9eba6 685 }
1e2b2765 686 } else if (strcmp(update, "assemble")==0) {
82d9eba6
NB
687 int d = info->disk.number;
688 int want;
689 if (info->disk.state == 6)
a2ce5a1a 690 want = info->disk.raid_disk;
82d9eba6
NB
691 else
692 want = 0xFFFF;
a2ce5a1a
N
693 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
694 sb->dev_roles[d] = __cpu_to_le16(want);
82d9eba6
NB
695 rv = 1;
696 }
d43494fc
N
697 if (info->reshape_active &&
698 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
699 info->delta_disks >= 0 &&
700 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
701 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
702 rv = 1;
703 }
704 if (info->reshape_active &&
705 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
706 info->delta_disks < 0 &&
707 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
708 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
709 rv = 1;
710 }
1e2b2765 711 } else if (strcmp(update, "linear-grow-new") == 0) {
f21e18ca 712 unsigned int i;
1c6cb603 713 int rfd, fd;
f21e18ca 714 unsigned int max = __le32_to_cpu(sb->max_dev);
f752781f
NB
715
716 for (i=0 ; i < max ; i++)
717 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
718 break;
719 sb->dev_number = __cpu_to_le32(i);
720 info->disk.number = i;
721 if (max >= __le32_to_cpu(sb->max_dev))
722 sb->max_dev = __cpu_to_le32(max+1);
723
724 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
725 read(rfd, sb->device_uuid, 16) != 16) {
caa0f6c6
N
726 __u32 r[4] = {random(), random(), random(), random()};
727 memcpy(sb->device_uuid, r, 16);
f752781f 728 }
7cbeb80e
N
729 if (rfd >= 0)
730 close(rfd);
f752781f
NB
731
732 sb->dev_roles[i] =
733 __cpu_to_le16(info->disk.raid_disk);
1c6cb603
N
734
735 fd = open(devname, O_RDONLY);
736 if (fd >= 0) {
737 unsigned long long ds;
738 get_dev_size(fd, devname, &ds);
739 close(fd);
740 ds >>= 9;
741 if (__le64_to_cpu(sb->super_offset) <
742 __le64_to_cpu(sb->data_offset)) {
743 sb->data_size = __cpu_to_le64(
744 ds - __le64_to_cpu(sb->data_offset));
745 } else {
746 ds -= 8*2;
747 ds &= ~(unsigned long long)(4*2-1);
748 sb->super_offset = __cpu_to_le64(ds);
749 sb->data_size = __cpu_to_le64(
750 ds - __le64_to_cpu(sb->data_offset));
751 }
752 }
1e2b2765 753 } else if (strcmp(update, "linear-grow-update") == 0) {
82d9eba6 754 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
f752781f
NB
755 sb->dev_roles[info->disk.number] =
756 __cpu_to_le16(info->disk.raid_disk);
1e2b2765 757 } else if (strcmp(update, "resync") == 0) {
82d9eba6 758 /* make sure resync happens */
434b7755 759 sb->resync_offset = 0ULL;
1e2b2765 760 } else if (strcmp(update, "uuid") == 0) {
350f29f9 761 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
bf4fb153 762
14263cf1
NB
763 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
764 struct bitmap_super_s *bm;
3da92f27 765 bm = (struct bitmap_super_s*)(st->sb+1024);
bf4fb153 766 memcpy(bm->uuid, sb->set_uuid, 16);
14263cf1 767 }
5a31170d
N
768 } else if (strcmp(update, "no-bitmap") == 0) {
769 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1e2b2765 770 } else if (strcmp(update, "homehost") == 0 &&
0f23aa88 771 homehost) {
0237e0ca
NB
772 char *c;
773 update = "name";
774 c = strchr(sb->set_name, ':');
775 if (c)
776 strncpy(info->name, c+1, 31 - (c-sb->set_name));
777 else
778 strncpy(info->name, sb->set_name, 32);
26a0b8fd 779 info->name[32] = 0;
1e2b2765 780 } else if (strcmp(update, "name") == 0) {
c4f12c13
NB
781 if (info->name[0] == 0)
782 sprintf(info->name, "%d", info->array.md_minor);
783 memset(sb->set_name, 0, sizeof(sb->set_name));
784 if (homehost &&
785 strchr(info->name, ':') == NULL &&
786 strlen(homehost)+1+strlen(info->name) < 32) {
787 strcpy(sb->set_name, homehost);
788 strcat(sb->set_name, ":");
789 strcat(sb->set_name, info->name);
790 } else
791 strcpy(sb->set_name, info->name);
1e2b2765 792 } else if (strcmp(update, "devicesize") == 0 &&
bee8ec56
NB
793 __le64_to_cpu(sb->super_offset) <
794 __le64_to_cpu(sb->data_offset)) {
795 /* set data_size to device size less data_offset */
796 struct misc_dev_info *misc = (struct misc_dev_info*)
6416d527 797 (st->sb + 1024 + 512);
eb9199fb
NB
798 printf("Size was %llu\n", (unsigned long long)
799 __le64_to_cpu(sb->data_size));
bee8ec56
NB
800 sb->data_size = __cpu_to_le64(
801 misc->device_size - __le64_to_cpu(sb->data_offset));
eb9199fb
NB
802 printf("Size is %llu\n", (unsigned long long)
803 __le64_to_cpu(sb->data_size));
1e2b2765 804 } else if (strcmp(update, "_reshape_progress")==0)
353632d9 805 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
16715c01
DL
806 else if (strcmp(update, "writemostly")==0)
807 sb->devflags |= WriteMostly1;
808 else if (strcmp(update, "readwrite")==0)
809 sb->devflags &= ~WriteMostly1;
1e2b2765
N
810 else
811 rv = -1;
82d9eba6
NB
812
813 sb->sb_csum = calc_sb_1_csum(sb);
814 return rv;
815}
816
3da92f27 817static int init_super1(struct supertype *st, mdu_array_info_t *info,
3d3dd91e 818 unsigned long long size, char *name, char *homehost, int *uuid)
82d9eba6 819{
6416d527 820 struct mdp_superblock_1 *sb;
82d9eba6 821 int spares;
34163fc7 822 int rfd;
05697ec1 823 char defname[10];
6416d527 824
3d2c4fc7
DW
825 if (posix_memalign((void**)&sb, 512, (1024 + 512 +
826 sizeof(struct misc_dev_info))) != 0) {
827 fprintf(stderr, Name
828 ": %s could not allocate superblock\n", __func__);
829 return 0;
830 }
82d9eba6
NB
831 memset(sb, 0, 1024);
832
64557c33 833 st->sb = sb;
ba7eb04f 834 if (info == NULL) {
82d9eba6
NB
835 /* zeroing superblock */
836 return 0;
05697ec1 837 }
82d9eba6
NB
838
839 spares = info->working_disks - info->active_disks;
840 if (info->raid_disks + spares > 384) {
841 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
842 info->raid_disks , spares, 384);
843 return 0;
844 }
845
82d9eba6
NB
846 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
847 sb->major_version = __cpu_to_le32(1);
848 sb->feature_map = 0;
849 sb->pad0 = 0;
34163fc7 850
350f29f9
NB
851 if (uuid)
852 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
853 else {
3d3dd91e
NB
854 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
855 read(rfd, sb->set_uuid, 16) != 16) {
caa0f6c6
N
856 __u32 r[4] = {random(), random(), random(), random()};
857 memcpy(sb->set_uuid, r, 16);
3d3dd91e
NB
858 }
859 if (rfd >= 0) close(rfd);
34163fc7 860 }
82d9eba6 861
05697ec1
NB
862 if (name == NULL || *name == 0) {
863 sprintf(defname, "%d", info->md_minor);
864 name = defname;
865 }
947fd4dd 866 memset(sb->set_name, 0, 32);
05697ec1
NB
867 if (homehost &&
868 strchr(name, ':')== NULL &&
869 strlen(homehost)+1+strlen(name) < 32) {
870 strcpy(sb->set_name, homehost);
871 strcat(sb->set_name, ":");
872 strcat(sb->set_name, name);
873 } else
874 strcpy(sb->set_name, name);
82d9eba6
NB
875
876 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
877 sb->level = __cpu_to_le32(info->level);
ea329559 878 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 879 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
880 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
881 sb->raid_disks = __cpu_to_le32(info->raid_disks);
882
883 sb->data_offset = __cpu_to_le64(0);
884 sb->data_size = __cpu_to_le64(0);
885 sb->super_offset = __cpu_to_le64(0);
886 sb->recovery_offset = __cpu_to_le64(0);
887
888 sb->utime = sb->ctime;
889 sb->events = __cpu_to_le64(1);
34321279 890 if (info->state & (1<<MD_SB_CLEAN))
b7528a20 891 sb->resync_offset = MaxSector;
82d9eba6
NB
892 else
893 sb->resync_offset = 0;
34163fc7 894 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
895 sizeof(sb->dev_roles[0]));
896 memset(sb->pad3, 0, sizeof(sb->pad3));
897
898 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
899
82d9eba6
NB
900 return 1;
901}
902
111d01fc
NB
903struct devinfo {
904 int fd;
905 char *devname;
906 mdu_disk_info_t disk;
907 struct devinfo *next;
908};
0e600426 909#ifndef MDASSEMBLE
82d9eba6 910/* Add a device to the superblock being created */
f20c3968 911static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
111d01fc 912 int fd, char *devname)
82d9eba6 913{
3da92f27 914 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 915 __u16 *rp = sb->dev_roles + dk->number;
111d01fc
NB
916 struct devinfo *di, **dip;
917
dfd4d8ee 918 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 919 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 920 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 921 *rp = 0xffff;
34163fc7 922 else
82d9eba6 923 *rp = 0xfffe;
111d01fc 924
f21e18ca 925 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
9df04a4f
N
926 __le32_to_cpu(sb->max_dev) < 384)
927 sb->max_dev = __cpu_to_le32(dk->number+1);
8844e291 928
d2ca6449 929 sb->dev_number = __cpu_to_le32(dk->number);
16715c01 930 sb->devflags = 0; /* don't copy another disks flags */
d2ca6449
NB
931 sb->sb_csum = calc_sb_1_csum(sb);
932
111d01fc
NB
933 dip = (struct devinfo **)&st->info;
934 while (*dip)
935 dip = &(*dip)->next;
936 di = malloc(sizeof(struct devinfo));
937 di->fd = fd;
938 di->devname = devname;
939 di->disk = *dk;
940 di->next = NULL;
941 *dip = di;
f20c3968
DW
942
943 return 0;
82d9eba6 944}
0e600426 945#endif
82d9eba6 946
3da92f27 947static void locate_bitmap1(struct supertype *st, int fd);
14263cf1 948
3da92f27 949static int store_super1(struct supertype *st, int fd)
82d9eba6 950{
3da92f27 951 struct mdp_superblock_1 *sb = st->sb;
6fbba4c9 952 unsigned long long sb_offset;
82d9eba6 953 int sbsize;
5dd497ee 954 unsigned long long dsize;
96395475 955
beae1dfe
NB
956 if (!get_dev_size(fd, NULL, &dsize))
957 return 1;
958
959 dsize >>= 9;
96395475 960
5dd497ee 961 if (dsize < 24)
96395475
NB
962 return 2;
963
964 /*
965 * Calculate the position of the superblock.
966 * It is always aligned to a 4K boundary and
967 * depending on minor_version, it can be:
968 * 0: At least 8K, but less than 12K, from end of device
969 * 1: At start of device
970 * 2: 4K from start of device.
971 */
972 switch(st->minor_version) {
973 case 0:
5dd497ee 974 sb_offset = dsize;
96395475
NB
975 sb_offset -= 8*2;
976 sb_offset &= ~(4*2-1);
977 break;
978 case 1:
6fbba4c9 979 sb_offset = 0;
96395475
NB
980 break;
981 case 2:
982 sb_offset = 4*2;
983 break;
984 default:
985 return -EINVAL;
986 }
987
82d9eba6 988
34163fc7 989
6fbba4c9
NB
990 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
991 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
992 ) {
993 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
994 abort();
995 }
82d9eba6 996
6fbba4c9 997 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
998 return 3;
999
1000 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
6416d527 1001 sbsize = (sbsize+511)&(~511UL);
82d9eba6 1002
5ea022a1 1003 if (awrite(fd, sb, sbsize) != sbsize)
82d9eba6
NB
1004 return 4;
1005
14263cf1
NB
1006 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1007 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 1008 (((char*)sb)+1024);
14263cf1 1009 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
3da92f27 1010 locate_bitmap1(st, fd);
5ea022a1
N
1011 if (awrite(fd, bm, sizeof(*bm)) !=
1012 sizeof(*bm))
9fca7d62 1013 return 5;
14263cf1
NB
1014 }
1015 }
570c0542 1016 fsync(fd);
82d9eba6
NB
1017 return 0;
1018}
1019
3da92f27 1020static int load_super1(struct supertype *st, int fd, char *devname);
892debc8 1021
2fb749d1
NB
1022static unsigned long choose_bm_space(unsigned long devsize)
1023{
1024 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1025 * if bigger than 200Gig, save 128k
a380e275
N
1026 * NOTE: result must be multiple of 4K else bad things happen
1027 * on 4K-sector devices.
2fb749d1 1028 */
69646c14 1029 if (devsize < 64*2) return 0;
2fb749d1
NB
1030 if (devsize - 64*2 >= 200*1024*1024*2)
1031 return 128*2;
1032 if (devsize - 4*2 > 8*1024*1024*2)
1033 return 64*2;
1034 return 4*2;
1035}
1036
e8090005
N
1037static void free_super1(struct supertype *st);
1038
111d01fc
NB
1039#ifndef MDASSEMBLE
1040static int write_init_super1(struct supertype *st)
82d9eba6 1041{
3da92f27 1042 struct mdp_superblock_1 *sb = st->sb;
e8090005 1043 struct supertype *refst;
dfe47e00 1044 int rfd;
111d01fc 1045 int rv = 0;
f21e18ca 1046 unsigned long long bm_space;
a380e275 1047 unsigned long long reserved;
111d01fc 1048 struct devinfo *di;
8fac0577 1049 unsigned long long dsize, array_size;
f21e18ca 1050 unsigned long long sb_offset;
82d9eba6 1051
111d01fc
NB
1052 for (di = st->info; di && ! rv ; di = di->next) {
1053 if (di->disk.state == 1)
1054 continue;
a0c8a17f
NB
1055 if (di->fd < 0)
1056 continue;
82d9eba6 1057
9277cc77
N
1058 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
1059 ;
82d9eba6 1060
111d01fc
NB
1061 sb->dev_number = __cpu_to_le32(di->disk.number);
1062 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1063 sb->devflags |= __cpu_to_le32(WriteMostly1);
9a88e7b6
SS
1064 else
1065 sb->devflags &= ~(__cpu_to_le32(WriteMostly1));
892debc8 1066
111d01fc
NB
1067 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1068 read(rfd, sb->device_uuid, 16) != 16) {
83208785
N
1069 __u32 r[4] = {random(), random(), random(), random()};
1070 memcpy(sb->device_uuid, r, 16);
111d01fc 1071 }
83208785
N
1072 if (rfd >= 0)
1073 close(rfd);
1074
111d01fc
NB
1075 sb->events = 0;
1076
e8090005
N
1077 refst = dup_super(st);
1078 if (load_super1(refst, di->fd, NULL)==0) {
1079 struct mdp_superblock_1 *refsb = refst->sb;
111d01fc
NB
1080
1081 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1082 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1083 /* same array, so preserve events and
1084 * dev_number */
1085 sb->events = refsb->events;
1086 /* bugs in 2.6.17 and earlier mean the
1087 * dev_number chosen in Manage must be preserved
1088 */
1089 if (get_linux_version() >= 2006018)
1090 sb->dev_number = refsb->dev_number;
1091 }
e8090005 1092 free_super1(refst);
892debc8 1093 }
e8090005 1094 free(refst);
34163fc7 1095
111d01fc
NB
1096 if (!get_dev_size(di->fd, NULL, &dsize))
1097 return 1;
1098 dsize >>= 9;
82d9eba6 1099
111d01fc
NB
1100 if (dsize < 24) {
1101 close(di->fd);
1102 return 2;
1103 }
82d9eba6
NB
1104
1105
111d01fc
NB
1106 /*
1107 * Calculate the position of the superblock.
1108 * It is always aligned to a 4K boundary and
1109 * depending on minor_version, it can be:
1110 * 0: At least 8K, but less than 12K, from end of device
1111 * 1: At start of device
1112 * 2: 4K from start of device.
1113 * Depending on the array size, we might leave extra space
1114 * for a bitmap.
1115 */
1116 array_size = __le64_to_cpu(sb->size);
1117 /* work out how much space we left for a bitmap */
1118 bm_space = choose_bm_space(array_size);
1119
1120 switch(st->minor_version) {
1121 case 0:
1122 sb_offset = dsize;
1123 sb_offset -= 8*2;
1124 sb_offset &= ~(4*2-1);
1125 sb->super_offset = __cpu_to_le64(sb_offset);
1126 sb->data_offset = __cpu_to_le64(0);
f21e18ca 1127 if (sb_offset < array_size + bm_space)
a380e275 1128 bm_space = sb_offset - array_size;
111d01fc
NB
1129 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1130 break;
1131 case 1:
1132 sb->super_offset = __cpu_to_le64(0);
a380e275
N
1133 reserved = bm_space + 4*2;
1134 /* Try for multiple of 1Meg so it is nicely aligned */
1135 #define ONE_MEG (2*1024)
1136 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1137 if (reserved + __le64_to_cpu(sb->size) > dsize)
1138 reserved = dsize - __le64_to_cpu(sb->size);
1139 /* force 4K alignment */
1140 reserved &= ~7ULL;
1141
1142 sb->data_offset = __cpu_to_le64(reserved);
1143 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1144 break;
1145 case 2:
1146 sb_offset = 4*2;
1147 sb->super_offset = __cpu_to_le64(4*2);
1148 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1149 > dsize)
1150 bm_space = dsize - __le64_to_cpu(sb->size)
1151 - 4*2 - 4*2;
a380e275
N
1152
1153 reserved = bm_space + 4*2 + 4*2;
1154 /* Try for multiple of 1Meg so it is nicely aligned */
1155 #define ONE_MEG (2*1024)
1156 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1157 if (reserved + __le64_to_cpu(sb->size) > dsize)
1158 reserved = dsize - __le64_to_cpu(sb->size);
1159 /* force 4K alignment */
1160 reserved &= ~7ULL;
1161
1162 sb->data_offset = __cpu_to_le64(reserved);
1163 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1164 break;
1165 default:
1166 return -EINVAL;
1167 }
82d9eba6
NB
1168
1169
111d01fc
NB
1170 sb->sb_csum = calc_sb_1_csum(sb);
1171 rv = store_super1(st, di->fd);
1172 if (rv)
1173 fprintf(stderr,
1174 Name ": failed to write superblock to %s\n",
1175 di->devname);
34163fc7 1176
111d01fc
NB
1177 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1178 rv = st->ss->write_bitmap(st, di->fd);
1179 close(di->fd);
1180 di->fd = -1;
1181 }
82d9eba6
NB
1182 return rv;
1183}
111d01fc 1184#endif
82d9eba6 1185
64557c33 1186static int compare_super1(struct supertype *st, struct supertype *tst)
82d9eba6
NB
1187{
1188 /*
1189 * return:
1190 * 0 same, or first was empty, and second was copied
1191 * 1 second had wrong number
1192 * 2 wrong uuid
1193 * 3 wrong other info
1194 */
64557c33
NB
1195 struct mdp_superblock_1 *first = st->sb;
1196 struct mdp_superblock_1 *second = tst->sb;
82d9eba6
NB
1197
1198 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1199 return 1;
1200 if (second->major_version != __cpu_to_le32(1))
1201 return 1;
1202
1203 if (!first) {
3d2c4fc7 1204 if (posix_memalign((void**)&first, 512,
6416d527 1205 1024 + 512 +
3d2c4fc7
DW
1206 sizeof(struct misc_dev_info)) != 0) {
1207 fprintf(stderr, Name
1208 ": %s could not allocate superblock\n", __func__);
1209 return 1;
1210 }
6416d527 1211 memcpy(first, second, 1024 + 512 +
bee8ec56 1212 sizeof(struct misc_dev_info));
64557c33 1213 st->sb = first;
82d9eba6
NB
1214 return 0;
1215 }
1216 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1217 return 2;
1218
1219 if (first->ctime != second->ctime ||
1220 first->level != second->level ||
1221 first->layout != second->layout ||
1222 first->size != second->size ||
1223 first->chunksize != second->chunksize ||
1224 first->raid_disks != second->raid_disks)
1225 return 3;
1226 return 0;
1227}
1228
3da92f27 1229static int load_super1(struct supertype *st, int fd, char *devname)
82d9eba6 1230{
5dd497ee 1231 unsigned long long dsize;
82d9eba6
NB
1232 unsigned long long sb_offset;
1233 struct mdp_superblock_1 *super;
14263cf1
NB
1234 int uuid[4];
1235 struct bitmap_super_s *bsb;
bee8ec56 1236 struct misc_dev_info *misc;
82d9eba6 1237
3da92f27 1238 free_super1(st);
82d9eba6 1239
a17a3de3 1240 if (st->ss == NULL || st->minor_version == -1) {
570c0542 1241 int bestvers = -1;
23dc1ae8 1242 struct supertype tst;
570c0542
NB
1243 __u64 bestctime = 0;
1244 /* guess... choose latest ctime */
ef609477 1245 memset(&tst, 0, sizeof(tst));
23dc1ae8
NB
1246 tst.ss = &super1;
1247 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
3da92f27
NB
1248 switch(load_super1(&tst, fd, devname)) {
1249 case 0: super = tst.sb;
570c0542
NB
1250 if (bestvers == -1 ||
1251 bestctime < __le64_to_cpu(super->ctime)) {
23dc1ae8 1252 bestvers = tst.minor_version;
570c0542
NB
1253 bestctime = __le64_to_cpu(super->ctime);
1254 }
1255 free(super);
3da92f27 1256 tst.sb = NULL;
570c0542 1257 break;
23dc1ae8 1258 case 1: return 1; /*bad device */
82d9eba6
NB
1259 case 2: break; /* bad, try next */
1260 }
1261 }
570c0542
NB
1262 if (bestvers != -1) {
1263 int rv;
23dc1ae8
NB
1264 tst.minor_version = bestvers;
1265 tst.ss = &super1;
1266 tst.max_devs = 384;
3da92f27 1267 rv = load_super1(&tst, fd, devname);
23dc1ae8
NB
1268 if (rv == 0)
1269 *st = tst;
570c0542
NB
1270 return rv;
1271 }
82d9eba6
NB
1272 return 2;
1273 }
beae1dfe
NB
1274 if (!get_dev_size(fd, devname, &dsize))
1275 return 1;
1276 dsize >>= 9;
82d9eba6 1277
5dd497ee 1278 if (dsize < 24) {
82d9eba6 1279 if (devname)
5dd497ee
NB
1280 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1281 devname, dsize);
82d9eba6
NB
1282 return 1;
1283 }
1284
1285 /*
1286 * Calculate the position of the superblock.
1287 * It is always aligned to a 4K boundary and
14263cf1 1288 * depending on minor_version, it can be:
82d9eba6
NB
1289 * 0: At least 8K, but less than 12K, from end of device
1290 * 1: At start of device
1291 * 2: 4K from start of device.
1292 */
1293 switch(st->minor_version) {
1294 case 0:
5dd497ee 1295 sb_offset = dsize;
82d9eba6
NB
1296 sb_offset -= 8*2;
1297 sb_offset &= ~(4*2-1);
1298 break;
1299 case 1:
1300 sb_offset = 0;
1301 break;
1302 case 2:
1303 sb_offset = 4*2;
1304 break;
1305 default:
1306 return -EINVAL;
1307 }
1308
1309 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1310
1311
1312 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1313 if (devname)
1314 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1315 devname, strerror(errno));
1316 return 1;
1317 }
1318
3d2c4fc7 1319 if (posix_memalign((void**)&super, 512,
6416d527 1320 1024 + 512 +
3d2c4fc7
DW
1321 sizeof(struct misc_dev_info)) != 0) {
1322 fprintf(stderr, Name ": %s could not allocate superblock\n",
1323 __func__);
1324 return 1;
1325 }
82d9eba6 1326
5ea022a1 1327 if (aread(fd, super, 1024) != 1024) {
82d9eba6
NB
1328 if (devname)
1329 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1330 devname);
1331 free(super);
1332 return 1;
1333 }
1334
1335 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1336 if (devname)
1337 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1338 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1339 free(super);
1340 return 2;
1341 }
1342
1343 if (__le32_to_cpu(super->major_version) != 1) {
1344 if (devname)
1345 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1346 devname, __le32_to_cpu(super->major_version));
1347 free(super);
1348 return 2;
1349 }
1350 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1351 if (devname)
1352 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1353 devname);
1354 free(super);
1355 return 2;
1356 }
64557c33 1357 st->sb = super;
14263cf1 1358
bee8ec56
NB
1359 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1360
6416d527 1361 misc = (struct misc_dev_info*) (((char*)super)+1024+512);
bee8ec56
NB
1362 misc->device_size = dsize;
1363
14263cf1
NB
1364 /* Now check on the bitmap superblock */
1365 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1366 return 0;
1367 /* Read the bitmap superblock and make sure it looks
1368 * valid. If it doesn't clear the bit. An --assemble --force
1369 * should get that written out.
1370 */
3da92f27 1371 locate_bitmap1(st, fd);
5ea022a1 1372 if (aread(fd, ((char*)super)+1024, 512)
6416d527 1373 != 512)
14263cf1
NB
1374 goto no_bitmap;
1375
3da92f27 1376 uuid_from_super1(st, uuid);
14263cf1
NB
1377 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1378 memcmp(bsb->uuid, uuid, 16) != 0)
1379 goto no_bitmap;
1380 return 0;
1381
1382 no_bitmap:
1383 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1384 return 0;
1385}
1386
1387
1388static struct supertype *match_metadata_desc1(char *arg)
1389{
1390 struct supertype *st = malloc(sizeof(*st));
1391 if (!st) return st;
1392
ef609477 1393 memset(st, 0, sizeof(*st));
d1d599ea 1394 st->container_dev = NoMdDev;
82d9eba6 1395 st->ss = &super1;
ea329559 1396 st->max_devs = 384;
64557c33 1397 st->sb = NULL;
9b2a22d3
N
1398 /* leading zeros can be safely ignored. --detail generates them. */
1399 while (*arg == '0')
1400 arg++;
1401 if (strcmp(arg, "1.0") == 0 ||
1402 strcmp(arg, "1.00") == 0) {
82d9eba6
NB
1403 st->minor_version = 0;
1404 return st;
1405 }
9b2a22d3 1406 if (strcmp(arg, "1.1") == 0 ||
50827504 1407 strcmp(arg, "1.01") == 0
7d5c3964 1408 ) {
82d9eba6
NB
1409 st->minor_version = 1;
1410 return st;
1411 }
9b2a22d3 1412 if (strcmp(arg, "1.2") == 0 ||
26f467a9 1413#ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
ad90adb6 1414 strcmp(arg, "default") == 0 ||
26f467a9 1415#endif /* DEFAULT_OLD_METADATA */
9b2a22d3 1416 strcmp(arg, "1.02") == 0) {
82d9eba6
NB
1417 st->minor_version = 2;
1418 return st;
1419 }
a17a3de3 1420 if (strcmp(arg, "1") == 0 ||
17f25ca6 1421 strcmp(arg, "default") == 0) {
a17a3de3
DL
1422 st->minor_version = -1;
1423 return st;
1424 }
82d9eba6
NB
1425
1426 free(st);
1427 return NULL;
1428}
1429
34163fc7
NB
1430/* find available size on device with this devsize, using
1431 * superblock type st, and reserving 'reserve' sectors for
1432 * a possible bitmap
1433 */
1bf4e2d9 1434static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6 1435{
2a528478 1436 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
1437 if (devsize < 24)
1438 return 0;
1439
2a528478
N
1440 if (super == NULL)
1441 /* creating: allow suitable space for bitmap */
1442 devsize -= choose_bm_space(devsize);
1443#ifndef MDASSEMBLE
1444 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1445 /* hot-add. allow for actual size of bitmap */
1446 struct bitmap_super_s *bsb;
1447 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1448 devsize -= bitmap_sectors(bsb);
1449 }
1450#endif
21e92547 1451
a380e275
N
1452 if (st->minor_version < 0)
1453 /* not specified, so time to set default */
1454 st->minor_version = 2;
1455 if (super == NULL && st->minor_version > 0) {
1456 /* haven't committed to a size yet, so allow some
1457 * slack for alignment of data_offset.
1458 * We haven't access to device details so allow
1459 * 1 Meg if bigger than 1Gig
1460 */
1461 if (devsize > 1024*1024*2)
1462 devsize -= 1024*2;
1463 }
34163fc7
NB
1464 switch(st->minor_version) {
1465 case 0:
1bf4e2d9
NB
1466 /* at end */
1467 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1468 case 1:
1bf4e2d9
NB
1469 /* at start, 4K for superblock and possible bitmap */
1470 return devsize - 4*2;
34163fc7 1471 case 2:
1bf4e2d9
NB
1472 /* 4k from start, 4K for superblock and possible bitmap */
1473 return devsize - (4+4)*2;
34163fc7
NB
1474 }
1475 return 0;
1476}
1477
1bf4e2d9 1478static int
3da92f27 1479add_internal_bitmap1(struct supertype *st,
199171a2
NB
1480 int *chunkp, int delay, int write_behind,
1481 unsigned long long size,
21e92547 1482 int may_change, int major)
34163fc7
NB
1483{
1484 /*
1bf4e2d9
NB
1485 * If not may_change, then this is a 'Grow', and the bitmap
1486 * must fit after the superblock.
1487 * If may_change, then this is create, and we can put the bitmap
1488 * before the superblock if we like, or may move the start.
199171a2
NB
1489 * If !may_change, the bitmap MUST live at offset of 1K, until
1490 * we get a sysfs interface.
34163fc7 1491 *
f9c25f1d 1492 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1493 */
1494
1bf4e2d9 1495 unsigned long long bits;
199171a2 1496 unsigned long long max_bits;
34163fc7 1497 unsigned long long min_chunk;
199171a2 1498 long offset;
f21e18ca 1499 unsigned long long chunk = *chunkp;
37dfc3d6 1500 int room = 0;
3da92f27 1501 struct mdp_superblock_1 *sb = st->sb;
34163fc7 1502 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
3b7e9d0c 1503 int uuid[4];
34163fc7 1504
199171a2
NB
1505 switch(st->minor_version) {
1506 case 0:
a31128d2
N
1507 /* either 3K after the superblock (when hot-add),
1508 * or some amount of space before.
199171a2
NB
1509 */
1510 if (may_change) {
1511 /* We are creating array, so we *know* how much room has
1512 * been left.
1513 */
1514 offset = 0;
2fb749d1 1515 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1516 } else {
1517 room = __le64_to_cpu(sb->super_offset)
1518 - __le64_to_cpu(sb->data_offset)
1519 - __le64_to_cpu(sb->data_size);
1520 /* remove '1 ||' when we can set offset via sysfs */
1521 if (1 || (room < 3*2 &&
1522 __le32_to_cpu(sb->max_dev) <= 384)) {
1523 room = 3*2;
1524 offset = 1*2;
1525 } else {
1526 offset = 0; /* means movable offset */
1527 }
1528 }
1529 break;
1530 case 1:
1531 case 2: /* between superblock and data */
1532 if (may_change) {
1533 offset = 4*2;
2fb749d1 1534 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1535 } else {
1536 room = __le64_to_cpu(sb->data_offset)
1537 - __le64_to_cpu(sb->super_offset);
1538 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1539 room -= 2;
1540 offset = 2;
1541 } else {
1542 room -= 4*2;
1543 offset = 4*2;
1544 }
1545 }
1546 break;
ae491d1e
NB
1547 default:
1548 return 0;
199171a2 1549 }
1bf4e2d9 1550
199171a2
NB
1551 if (chunk == UnSet && room > 128*2)
1552 /* Limit to 128K of bitmap when chunk size not requested */
1553 room = 128*2;
1bf4e2d9 1554
199171a2 1555 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1556
1557 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1558 bits = (size*512)/min_chunk +1;
34163fc7
NB
1559 while (bits > max_bits) {
1560 min_chunk *= 2;
1561 bits = (bits+1)/2;
1562 }
b8ab2a50
N
1563 if (chunk == UnSet) {
1564 /* For practical purpose, 64Meg is a good
1565 * default chunk size for internal bitmaps.
1566 */
34163fc7 1567 chunk = min_chunk;
b8ab2a50
N
1568 if (chunk < 64*1024*1024)
1569 chunk = 64*1024*1024;
1570 } else if (chunk < min_chunk)
34163fc7 1571 return 0; /* chunk size too small */
21e92547
NB
1572 if (chunk == 0) /* rounding problem */
1573 return 0;
34163fc7 1574
199171a2 1575 if (offset == 0) {
a31128d2
N
1576 /* start bitmap on a 4K boundary with enough space for
1577 * the bitmap
1578 */
199171a2 1579 bits = (size*512) / chunk + 1;
a31128d2
N
1580 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1581 room *= 8; /* convert 4K blocks to sectors */
199171a2
NB
1582 offset = -room;
1583 }
1584
1585 sb->bitmap_offset = __cpu_to_le32(offset);
34163fc7
NB
1586
1587 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1588 memset(bms, 0, sizeof(*bms));
34163fc7 1589 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1590 bms->version = __cpu_to_le32(major);
3b7e9d0c
LB
1591 uuid_from_super1(st, uuid);
1592 memcpy(bms->uuid, uuid, 16);
34163fc7
NB
1593 bms->chunksize = __cpu_to_le32(chunk);
1594 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1595 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1596 bms->write_behind = __cpu_to_le32(write_behind);
1597
199171a2 1598 *chunkp = chunk;
34163fc7
NB
1599 return 1;
1600}
1601
1602
3da92f27 1603static void locate_bitmap1(struct supertype *st, int fd)
34163fc7 1604{
34163fc7 1605 unsigned long long offset;
1e0d770c
NB
1606 struct mdp_superblock_1 *sb;
1607 int mustfree = 0;
34163fc7 1608
3da92f27
NB
1609 if (!st->sb) {
1610 if (st->ss->load_super(st, fd, NULL))
f6d75de8 1611 return; /* no error I hope... */
1e0d770c
NB
1612 mustfree = 1;
1613 }
3da92f27 1614 sb = st->sb;
e478dc86 1615
1bf4e2d9 1616 offset = __le64_to_cpu(sb->super_offset);
68754bd1 1617 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1618 if (mustfree)
f6d75de8 1619 free(sb);
1bf4e2d9 1620 lseek64(fd, offset<<9, 0);
34163fc7
NB
1621}
1622
3da92f27 1623static int write_bitmap1(struct supertype *st, int fd)
34163fc7 1624{
3da92f27 1625 struct mdp_superblock_1 *sb = st->sb;
1bf4e2d9 1626 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1627 int rv = 0;
1628
1629 int towrite, n;
5ea022a1 1630 char *buf = (char*)(((long)(abuf+4096))&~4095UL);
34163fc7 1631
3da92f27 1632 locate_bitmap1(st, fd);
34163fc7 1633
6416d527
NB
1634 memset(buf, 0xff, 4096);
1635 memcpy(buf, ((char*)sb)+1024, sizeof(bitmap_super_t));
1636
1bf4e2d9
NB
1637 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1638 towrite = (towrite+7) >> 3; /* bits to bytes */
6416d527
NB
1639 towrite += sizeof(bitmap_super_t);
1640 towrite = ROUND_UP(towrite, 512);
34163fc7
NB
1641 while (towrite > 0) {
1642 n = towrite;
6416d527
NB
1643 if (n > 4096)
1644 n = 4096;
34163fc7
NB
1645 n = write(fd, buf, n);
1646 if (n > 0)
1647 towrite -= n;
1648 else
1649 break;
6416d527 1650 memset(buf, 0xff, 4096);
34163fc7
NB
1651 }
1652 fsync(fd);
1653 if (towrite)
1654 rv = -2;
1655
1656 return rv;
82d9eba6
NB
1657}
1658
3da92f27 1659static void free_super1(struct supertype *st)
df37ffc0 1660{
3da92f27
NB
1661 if (st->sb)
1662 free(st->sb);
1cc7f4fe
N
1663 while (st->info) {
1664 struct devinfo *di = st->info;
1665 st->info = di->next;
1666 if (di->fd >= 0)
1667 close(di->fd);
1668 free(di);
1669 }
3da92f27 1670 st->sb = NULL;
df37ffc0
NB
1671}
1672
0e600426 1673#ifndef MDASSEMBLE
17f25ca6
NB
1674static int validate_geometry1(struct supertype *st, int level,
1675 int layout, int raiddisks,
c21e737b 1676 int *chunk, unsigned long long size,
2c514b71
NB
1677 char *subdev, unsigned long long *freesize,
1678 int verbose)
17f25ca6
NB
1679{
1680 unsigned long long ldsize;
1681 int fd;
1682
b42f577a
N
1683 if (level == LEVEL_CONTAINER) {
1684 if (verbose)
1685 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
17f25ca6 1686 return 0;
b42f577a 1687 }
bb7295f1
N
1688 if (chunk && *chunk == UnSet)
1689 *chunk = DEFAULT_CHUNK;
1690
17f25ca6
NB
1691 if (!subdev)
1692 return 1;
1693
1694 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1695 if (fd < 0) {
2c514b71
NB
1696 if (verbose)
1697 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1698 subdev, strerror(errno));
17f25ca6
NB
1699 return 0;
1700 }
2c514b71 1701
17f25ca6
NB
1702 if (!get_dev_size(fd, subdev, &ldsize)) {
1703 close(fd);
1704 return 0;
1705 }
1706 close(fd);
1707
1708 *freesize = avail_size1(st, ldsize >> 9);
1709 return 1;
1710}
0e600426 1711#endif /* MDASSEMBLE */
17f25ca6 1712
82d9eba6 1713struct superswitch super1 = {
c7654afc 1714#ifndef MDASSEMBLE
82d9eba6
NB
1715 .examine_super = examine_super1,
1716 .brief_examine_super = brief_examine_super1,
0d726f17 1717 .export_examine_super = export_examine_super1,
82d9eba6
NB
1718 .detail_super = detail_super1,
1719 .brief_detail_super = brief_detail_super1,
0d726f17 1720 .export_detail_super = export_detail_super1,
111d01fc 1721 .write_init_super = write_init_super1,
0e600426
N
1722 .validate_geometry = validate_geometry1,
1723 .add_to_super = add_to_super1,
c7654afc 1724#endif
83b6208e 1725 .match_home = match_home1,
82d9eba6
NB
1726 .uuid_from_super = uuid_from_super1,
1727 .getinfo_super = getinfo_super1,
00bbdbda 1728 .container_content = container_content1,
82d9eba6 1729 .update_super = update_super1,
82d9eba6 1730 .init_super = init_super1,
82d9eba6 1731 .store_super = store_super1,
82d9eba6
NB
1732 .compare_super = compare_super1,
1733 .load_super = load_super1,
1734 .match_metadata_desc = match_metadata_desc1,
1735 .avail_size = avail_size1,
34163fc7
NB
1736 .add_internal_bitmap = add_internal_bitmap1,
1737 .locate_bitmap = locate_bitmap1,
1738 .write_bitmap = write_bitmap1,
df37ffc0 1739 .free_super = free_super1,
f277ce36
NB
1740#if __BYTE_ORDER == BIG_ENDIAN
1741 .swapuuid = 0,
1742#else
1743 .swapuuid = 1,
1744#endif
31015d57 1745 .name = "1.x",
82d9eba6 1746};