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