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