]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super1.c
--add incorrectly sets writemostly
[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);
9a88e7b6
SS
1059 else
1060 sb->devflags &= ~(__cpu_to_le32(WriteMostly1));
892debc8 1061
111d01fc
NB
1062 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1063 read(rfd, sb->device_uuid, 16) != 16) {
83208785
N
1064 __u32 r[4] = {random(), random(), random(), random()};
1065 memcpy(sb->device_uuid, r, 16);
111d01fc 1066 }
83208785
N
1067 if (rfd >= 0)
1068 close(rfd);
1069
111d01fc
NB
1070 sb->events = 0;
1071
e8090005
N
1072 refst = dup_super(st);
1073 if (load_super1(refst, di->fd, NULL)==0) {
1074 struct mdp_superblock_1 *refsb = refst->sb;
111d01fc
NB
1075
1076 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1077 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1078 /* same array, so preserve events and
1079 * dev_number */
1080 sb->events = refsb->events;
1081 /* bugs in 2.6.17 and earlier mean the
1082 * dev_number chosen in Manage must be preserved
1083 */
1084 if (get_linux_version() >= 2006018)
1085 sb->dev_number = refsb->dev_number;
1086 }
e8090005 1087 free_super1(refst);
892debc8 1088 }
e8090005 1089 free(refst);
34163fc7 1090
111d01fc
NB
1091 if (!get_dev_size(di->fd, NULL, &dsize))
1092 return 1;
1093 dsize >>= 9;
82d9eba6 1094
111d01fc
NB
1095 if (dsize < 24) {
1096 close(di->fd);
1097 return 2;
1098 }
82d9eba6
NB
1099
1100
111d01fc
NB
1101 /*
1102 * Calculate the position of the superblock.
1103 * It is always aligned to a 4K boundary and
1104 * depending on minor_version, it can be:
1105 * 0: At least 8K, but less than 12K, from end of device
1106 * 1: At start of device
1107 * 2: 4K from start of device.
1108 * Depending on the array size, we might leave extra space
1109 * for a bitmap.
1110 */
1111 array_size = __le64_to_cpu(sb->size);
1112 /* work out how much space we left for a bitmap */
1113 bm_space = choose_bm_space(array_size);
1114
1115 switch(st->minor_version) {
1116 case 0:
1117 sb_offset = dsize;
1118 sb_offset -= 8*2;
1119 sb_offset &= ~(4*2-1);
1120 sb->super_offset = __cpu_to_le64(sb_offset);
1121 sb->data_offset = __cpu_to_le64(0);
f21e18ca 1122 if (sb_offset < array_size + bm_space)
a380e275 1123 bm_space = sb_offset - array_size;
111d01fc
NB
1124 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1125 break;
1126 case 1:
1127 sb->super_offset = __cpu_to_le64(0);
a380e275
N
1128 reserved = bm_space + 4*2;
1129 /* Try for multiple of 1Meg so it is nicely aligned */
1130 #define ONE_MEG (2*1024)
1131 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1132 if (reserved + __le64_to_cpu(sb->size) > dsize)
1133 reserved = dsize - __le64_to_cpu(sb->size);
1134 /* force 4K alignment */
1135 reserved &= ~7ULL;
1136
1137 sb->data_offset = __cpu_to_le64(reserved);
1138 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1139 break;
1140 case 2:
1141 sb_offset = 4*2;
1142 sb->super_offset = __cpu_to_le64(4*2);
1143 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1144 > dsize)
1145 bm_space = dsize - __le64_to_cpu(sb->size)
1146 - 4*2 - 4*2;
a380e275
N
1147
1148 reserved = bm_space + 4*2 + 4*2;
1149 /* Try for multiple of 1Meg so it is nicely aligned */
1150 #define ONE_MEG (2*1024)
1151 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1152 if (reserved + __le64_to_cpu(sb->size) > dsize)
1153 reserved = dsize - __le64_to_cpu(sb->size);
1154 /* force 4K alignment */
1155 reserved &= ~7ULL;
1156
1157 sb->data_offset = __cpu_to_le64(reserved);
1158 sb->data_size = __cpu_to_le64(dsize - reserved);
111d01fc
NB
1159 break;
1160 default:
1161 return -EINVAL;
1162 }
82d9eba6
NB
1163
1164
111d01fc
NB
1165 sb->sb_csum = calc_sb_1_csum(sb);
1166 rv = store_super1(st, di->fd);
1167 if (rv)
1168 fprintf(stderr,
1169 Name ": failed to write superblock to %s\n",
1170 di->devname);
34163fc7 1171
111d01fc
NB
1172 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1173 rv = st->ss->write_bitmap(st, di->fd);
1174 close(di->fd);
1175 di->fd = -1;
1176 }
82d9eba6
NB
1177 return rv;
1178}
111d01fc 1179#endif
82d9eba6 1180
64557c33 1181static int compare_super1(struct supertype *st, struct supertype *tst)
82d9eba6
NB
1182{
1183 /*
1184 * return:
1185 * 0 same, or first was empty, and second was copied
1186 * 1 second had wrong number
1187 * 2 wrong uuid
1188 * 3 wrong other info
1189 */
64557c33
NB
1190 struct mdp_superblock_1 *first = st->sb;
1191 struct mdp_superblock_1 *second = tst->sb;
82d9eba6
NB
1192
1193 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1194 return 1;
1195 if (second->major_version != __cpu_to_le32(1))
1196 return 1;
1197
1198 if (!first) {
3d2c4fc7 1199 if (posix_memalign((void**)&first, 512,
6416d527 1200 1024 + 512 +
3d2c4fc7
DW
1201 sizeof(struct misc_dev_info)) != 0) {
1202 fprintf(stderr, Name
1203 ": %s could not allocate superblock\n", __func__);
1204 return 1;
1205 }
6416d527 1206 memcpy(first, second, 1024 + 512 +
bee8ec56 1207 sizeof(struct misc_dev_info));
64557c33 1208 st->sb = first;
82d9eba6
NB
1209 return 0;
1210 }
1211 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1212 return 2;
1213
1214 if (first->ctime != second->ctime ||
1215 first->level != second->level ||
1216 first->layout != second->layout ||
1217 first->size != second->size ||
1218 first->chunksize != second->chunksize ||
1219 first->raid_disks != second->raid_disks)
1220 return 3;
1221 return 0;
1222}
1223
3da92f27 1224static int load_super1(struct supertype *st, int fd, char *devname)
82d9eba6 1225{
5dd497ee 1226 unsigned long long dsize;
82d9eba6
NB
1227 unsigned long long sb_offset;
1228 struct mdp_superblock_1 *super;
14263cf1
NB
1229 int uuid[4];
1230 struct bitmap_super_s *bsb;
bee8ec56 1231 struct misc_dev_info *misc;
82d9eba6 1232
3da92f27 1233 free_super1(st);
82d9eba6 1234
a17a3de3 1235 if (st->ss == NULL || st->minor_version == -1) {
570c0542 1236 int bestvers = -1;
23dc1ae8 1237 struct supertype tst;
570c0542
NB
1238 __u64 bestctime = 0;
1239 /* guess... choose latest ctime */
ef609477 1240 memset(&tst, 0, sizeof(tst));
23dc1ae8
NB
1241 tst.ss = &super1;
1242 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
3da92f27
NB
1243 switch(load_super1(&tst, fd, devname)) {
1244 case 0: super = tst.sb;
570c0542
NB
1245 if (bestvers == -1 ||
1246 bestctime < __le64_to_cpu(super->ctime)) {
23dc1ae8 1247 bestvers = tst.minor_version;
570c0542
NB
1248 bestctime = __le64_to_cpu(super->ctime);
1249 }
1250 free(super);
3da92f27 1251 tst.sb = NULL;
570c0542 1252 break;
23dc1ae8 1253 case 1: return 1; /*bad device */
82d9eba6
NB
1254 case 2: break; /* bad, try next */
1255 }
1256 }
570c0542
NB
1257 if (bestvers != -1) {
1258 int rv;
23dc1ae8
NB
1259 tst.minor_version = bestvers;
1260 tst.ss = &super1;
1261 tst.max_devs = 384;
3da92f27 1262 rv = load_super1(&tst, fd, devname);
23dc1ae8
NB
1263 if (rv == 0)
1264 *st = tst;
570c0542
NB
1265 return rv;
1266 }
82d9eba6
NB
1267 return 2;
1268 }
beae1dfe
NB
1269 if (!get_dev_size(fd, devname, &dsize))
1270 return 1;
1271 dsize >>= 9;
82d9eba6 1272
5dd497ee 1273 if (dsize < 24) {
82d9eba6 1274 if (devname)
5dd497ee
NB
1275 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1276 devname, dsize);
82d9eba6
NB
1277 return 1;
1278 }
1279
1280 /*
1281 * Calculate the position of the superblock.
1282 * It is always aligned to a 4K boundary and
14263cf1 1283 * depending on minor_version, it can be:
82d9eba6
NB
1284 * 0: At least 8K, but less than 12K, from end of device
1285 * 1: At start of device
1286 * 2: 4K from start of device.
1287 */
1288 switch(st->minor_version) {
1289 case 0:
5dd497ee 1290 sb_offset = dsize;
82d9eba6
NB
1291 sb_offset -= 8*2;
1292 sb_offset &= ~(4*2-1);
1293 break;
1294 case 1:
1295 sb_offset = 0;
1296 break;
1297 case 2:
1298 sb_offset = 4*2;
1299 break;
1300 default:
1301 return -EINVAL;
1302 }
1303
1304 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1305
1306
1307 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1308 if (devname)
1309 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1310 devname, strerror(errno));
1311 return 1;
1312 }
1313
3d2c4fc7 1314 if (posix_memalign((void**)&super, 512,
6416d527 1315 1024 + 512 +
3d2c4fc7
DW
1316 sizeof(struct misc_dev_info)) != 0) {
1317 fprintf(stderr, Name ": %s could not allocate superblock\n",
1318 __func__);
1319 return 1;
1320 }
82d9eba6 1321
5ea022a1 1322 if (aread(fd, super, 1024) != 1024) {
82d9eba6
NB
1323 if (devname)
1324 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1325 devname);
1326 free(super);
1327 return 1;
1328 }
1329
1330 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1331 if (devname)
1332 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1333 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1334 free(super);
1335 return 2;
1336 }
1337
1338 if (__le32_to_cpu(super->major_version) != 1) {
1339 if (devname)
1340 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1341 devname, __le32_to_cpu(super->major_version));
1342 free(super);
1343 return 2;
1344 }
1345 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1346 if (devname)
1347 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1348 devname);
1349 free(super);
1350 return 2;
1351 }
64557c33 1352 st->sb = super;
14263cf1 1353
bee8ec56
NB
1354 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1355
6416d527 1356 misc = (struct misc_dev_info*) (((char*)super)+1024+512);
bee8ec56
NB
1357 misc->device_size = dsize;
1358
14263cf1
NB
1359 /* Now check on the bitmap superblock */
1360 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1361 return 0;
1362 /* Read the bitmap superblock and make sure it looks
1363 * valid. If it doesn't clear the bit. An --assemble --force
1364 * should get that written out.
1365 */
3da92f27 1366 locate_bitmap1(st, fd);
5ea022a1 1367 if (aread(fd, ((char*)super)+1024, 512)
6416d527 1368 != 512)
14263cf1
NB
1369 goto no_bitmap;
1370
3da92f27 1371 uuid_from_super1(st, uuid);
14263cf1
NB
1372 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1373 memcmp(bsb->uuid, uuid, 16) != 0)
1374 goto no_bitmap;
1375 return 0;
1376
1377 no_bitmap:
1378 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1379 return 0;
1380}
1381
1382
1383static struct supertype *match_metadata_desc1(char *arg)
1384{
1385 struct supertype *st = malloc(sizeof(*st));
1386 if (!st) return st;
1387
ef609477 1388 memset(st, 0, sizeof(*st));
d1d599ea 1389 st->container_dev = NoMdDev;
82d9eba6 1390 st->ss = &super1;
ea329559 1391 st->max_devs = 384;
64557c33 1392 st->sb = NULL;
9b2a22d3
N
1393 /* leading zeros can be safely ignored. --detail generates them. */
1394 while (*arg == '0')
1395 arg++;
1396 if (strcmp(arg, "1.0") == 0 ||
1397 strcmp(arg, "1.00") == 0) {
82d9eba6
NB
1398 st->minor_version = 0;
1399 return st;
1400 }
9b2a22d3 1401 if (strcmp(arg, "1.1") == 0 ||
50827504 1402 strcmp(arg, "1.01") == 0
7d5c3964 1403 ) {
82d9eba6
NB
1404 st->minor_version = 1;
1405 return st;
1406 }
9b2a22d3 1407 if (strcmp(arg, "1.2") == 0 ||
26f467a9 1408#ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
ad90adb6 1409 strcmp(arg, "default") == 0 ||
26f467a9 1410#endif /* DEFAULT_OLD_METADATA */
9b2a22d3 1411 strcmp(arg, "1.02") == 0) {
82d9eba6
NB
1412 st->minor_version = 2;
1413 return st;
1414 }
a17a3de3 1415 if (strcmp(arg, "1") == 0 ||
17f25ca6 1416 strcmp(arg, "default") == 0) {
a17a3de3
DL
1417 st->minor_version = -1;
1418 return st;
1419 }
82d9eba6
NB
1420
1421 free(st);
1422 return NULL;
1423}
1424
34163fc7
NB
1425/* find available size on device with this devsize, using
1426 * superblock type st, and reserving 'reserve' sectors for
1427 * a possible bitmap
1428 */
1bf4e2d9 1429static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6 1430{
2a528478 1431 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
1432 if (devsize < 24)
1433 return 0;
1434
2a528478
N
1435 if (super == NULL)
1436 /* creating: allow suitable space for bitmap */
1437 devsize -= choose_bm_space(devsize);
1438#ifndef MDASSEMBLE
1439 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1440 /* hot-add. allow for actual size of bitmap */
1441 struct bitmap_super_s *bsb;
1442 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1443 devsize -= bitmap_sectors(bsb);
1444 }
1445#endif
21e92547 1446
a380e275
N
1447 if (st->minor_version < 0)
1448 /* not specified, so time to set default */
1449 st->minor_version = 2;
1450 if (super == NULL && st->minor_version > 0) {
1451 /* haven't committed to a size yet, so allow some
1452 * slack for alignment of data_offset.
1453 * We haven't access to device details so allow
1454 * 1 Meg if bigger than 1Gig
1455 */
1456 if (devsize > 1024*1024*2)
1457 devsize -= 1024*2;
1458 }
34163fc7
NB
1459 switch(st->minor_version) {
1460 case 0:
1bf4e2d9
NB
1461 /* at end */
1462 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1463 case 1:
1bf4e2d9
NB
1464 /* at start, 4K for superblock and possible bitmap */
1465 return devsize - 4*2;
34163fc7 1466 case 2:
1bf4e2d9
NB
1467 /* 4k from start, 4K for superblock and possible bitmap */
1468 return devsize - (4+4)*2;
34163fc7
NB
1469 }
1470 return 0;
1471}
1472
1bf4e2d9 1473static int
3da92f27 1474add_internal_bitmap1(struct supertype *st,
199171a2
NB
1475 int *chunkp, int delay, int write_behind,
1476 unsigned long long size,
21e92547 1477 int may_change, int major)
34163fc7
NB
1478{
1479 /*
1bf4e2d9
NB
1480 * If not may_change, then this is a 'Grow', and the bitmap
1481 * must fit after the superblock.
1482 * If may_change, then this is create, and we can put the bitmap
1483 * before the superblock if we like, or may move the start.
199171a2
NB
1484 * If !may_change, the bitmap MUST live at offset of 1K, until
1485 * we get a sysfs interface.
34163fc7 1486 *
f9c25f1d 1487 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1488 */
1489
1bf4e2d9 1490 unsigned long long bits;
199171a2 1491 unsigned long long max_bits;
34163fc7 1492 unsigned long long min_chunk;
199171a2 1493 long offset;
f21e18ca 1494 unsigned long long chunk = *chunkp;
37dfc3d6 1495 int room = 0;
3da92f27 1496 struct mdp_superblock_1 *sb = st->sb;
34163fc7 1497 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
3b7e9d0c 1498 int uuid[4];
34163fc7 1499
199171a2
NB
1500 switch(st->minor_version) {
1501 case 0:
a31128d2
N
1502 /* either 3K after the superblock (when hot-add),
1503 * or some amount of space before.
199171a2
NB
1504 */
1505 if (may_change) {
1506 /* We are creating array, so we *know* how much room has
1507 * been left.
1508 */
1509 offset = 0;
2fb749d1 1510 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1511 } else {
1512 room = __le64_to_cpu(sb->super_offset)
1513 - __le64_to_cpu(sb->data_offset)
1514 - __le64_to_cpu(sb->data_size);
1515 /* remove '1 ||' when we can set offset via sysfs */
1516 if (1 || (room < 3*2 &&
1517 __le32_to_cpu(sb->max_dev) <= 384)) {
1518 room = 3*2;
1519 offset = 1*2;
1520 } else {
1521 offset = 0; /* means movable offset */
1522 }
1523 }
1524 break;
1525 case 1:
1526 case 2: /* between superblock and data */
1527 if (may_change) {
1528 offset = 4*2;
2fb749d1 1529 room = choose_bm_space(__le64_to_cpu(sb->size));
199171a2
NB
1530 } else {
1531 room = __le64_to_cpu(sb->data_offset)
1532 - __le64_to_cpu(sb->super_offset);
1533 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1534 room -= 2;
1535 offset = 2;
1536 } else {
1537 room -= 4*2;
1538 offset = 4*2;
1539 }
1540 }
1541 break;
ae491d1e
NB
1542 default:
1543 return 0;
199171a2 1544 }
1bf4e2d9 1545
199171a2
NB
1546 if (chunk == UnSet && room > 128*2)
1547 /* Limit to 128K of bitmap when chunk size not requested */
1548 room = 128*2;
1bf4e2d9 1549
199171a2 1550 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1551
1552 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1553 bits = (size*512)/min_chunk +1;
34163fc7
NB
1554 while (bits > max_bits) {
1555 min_chunk *= 2;
1556 bits = (bits+1)/2;
1557 }
b8ab2a50
N
1558 if (chunk == UnSet) {
1559 /* For practical purpose, 64Meg is a good
1560 * default chunk size for internal bitmaps.
1561 */
34163fc7 1562 chunk = min_chunk;
b8ab2a50
N
1563 if (chunk < 64*1024*1024)
1564 chunk = 64*1024*1024;
1565 } else if (chunk < min_chunk)
34163fc7 1566 return 0; /* chunk size too small */
21e92547
NB
1567 if (chunk == 0) /* rounding problem */
1568 return 0;
34163fc7 1569
199171a2 1570 if (offset == 0) {
a31128d2
N
1571 /* start bitmap on a 4K boundary with enough space for
1572 * the bitmap
1573 */
199171a2 1574 bits = (size*512) / chunk + 1;
a31128d2
N
1575 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1576 room *= 8; /* convert 4K blocks to sectors */
199171a2
NB
1577 offset = -room;
1578 }
1579
1580 sb->bitmap_offset = __cpu_to_le32(offset);
34163fc7
NB
1581
1582 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1583 memset(bms, 0, sizeof(*bms));
34163fc7 1584 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1585 bms->version = __cpu_to_le32(major);
3b7e9d0c
LB
1586 uuid_from_super1(st, uuid);
1587 memcpy(bms->uuid, uuid, 16);
34163fc7
NB
1588 bms->chunksize = __cpu_to_le32(chunk);
1589 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1590 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1591 bms->write_behind = __cpu_to_le32(write_behind);
1592
199171a2 1593 *chunkp = chunk;
34163fc7
NB
1594 return 1;
1595}
1596
1597
3da92f27 1598static void locate_bitmap1(struct supertype *st, int fd)
34163fc7 1599{
34163fc7 1600 unsigned long long offset;
1e0d770c
NB
1601 struct mdp_superblock_1 *sb;
1602 int mustfree = 0;
34163fc7 1603
3da92f27
NB
1604 if (!st->sb) {
1605 if (st->ss->load_super(st, fd, NULL))
f6d75de8 1606 return; /* no error I hope... */
1e0d770c
NB
1607 mustfree = 1;
1608 }
3da92f27 1609 sb = st->sb;
e478dc86 1610
1bf4e2d9 1611 offset = __le64_to_cpu(sb->super_offset);
68754bd1 1612 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1613 if (mustfree)
f6d75de8 1614 free(sb);
1bf4e2d9 1615 lseek64(fd, offset<<9, 0);
34163fc7
NB
1616}
1617
3da92f27 1618static int write_bitmap1(struct supertype *st, int fd)
34163fc7 1619{
3da92f27 1620 struct mdp_superblock_1 *sb = st->sb;
1bf4e2d9 1621 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1622 int rv = 0;
1623
1624 int towrite, n;
5ea022a1 1625 char *buf = (char*)(((long)(abuf+4096))&~4095UL);
34163fc7 1626
3da92f27 1627 locate_bitmap1(st, fd);
34163fc7 1628
6416d527
NB
1629 memset(buf, 0xff, 4096);
1630 memcpy(buf, ((char*)sb)+1024, sizeof(bitmap_super_t));
1631
1bf4e2d9
NB
1632 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1633 towrite = (towrite+7) >> 3; /* bits to bytes */
6416d527
NB
1634 towrite += sizeof(bitmap_super_t);
1635 towrite = ROUND_UP(towrite, 512);
34163fc7
NB
1636 while (towrite > 0) {
1637 n = towrite;
6416d527
NB
1638 if (n > 4096)
1639 n = 4096;
34163fc7
NB
1640 n = write(fd, buf, n);
1641 if (n > 0)
1642 towrite -= n;
1643 else
1644 break;
6416d527 1645 memset(buf, 0xff, 4096);
34163fc7
NB
1646 }
1647 fsync(fd);
1648 if (towrite)
1649 rv = -2;
1650
1651 return rv;
82d9eba6
NB
1652}
1653
3da92f27 1654static void free_super1(struct supertype *st)
df37ffc0 1655{
3da92f27
NB
1656 if (st->sb)
1657 free(st->sb);
1cc7f4fe
N
1658 while (st->info) {
1659 struct devinfo *di = st->info;
1660 st->info = di->next;
1661 if (di->fd >= 0)
1662 close(di->fd);
1663 free(di);
1664 }
3da92f27 1665 st->sb = NULL;
df37ffc0
NB
1666}
1667
0e600426 1668#ifndef MDASSEMBLE
17f25ca6
NB
1669static int validate_geometry1(struct supertype *st, int level,
1670 int layout, int raiddisks,
c21e737b 1671 int *chunk, unsigned long long size,
2c514b71
NB
1672 char *subdev, unsigned long long *freesize,
1673 int verbose)
17f25ca6
NB
1674{
1675 unsigned long long ldsize;
1676 int fd;
1677
b42f577a
N
1678 if (level == LEVEL_CONTAINER) {
1679 if (verbose)
1680 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
17f25ca6 1681 return 0;
b42f577a 1682 }
bb7295f1
N
1683 if (chunk && *chunk == UnSet)
1684 *chunk = DEFAULT_CHUNK;
1685
17f25ca6
NB
1686 if (!subdev)
1687 return 1;
1688
1689 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1690 if (fd < 0) {
2c514b71
NB
1691 if (verbose)
1692 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1693 subdev, strerror(errno));
17f25ca6
NB
1694 return 0;
1695 }
2c514b71 1696
17f25ca6
NB
1697 if (!get_dev_size(fd, subdev, &ldsize)) {
1698 close(fd);
1699 return 0;
1700 }
1701 close(fd);
1702
1703 *freesize = avail_size1(st, ldsize >> 9);
1704 return 1;
1705}
0e600426 1706#endif /* MDASSEMBLE */
17f25ca6 1707
82d9eba6 1708struct superswitch super1 = {
c7654afc 1709#ifndef MDASSEMBLE
82d9eba6
NB
1710 .examine_super = examine_super1,
1711 .brief_examine_super = brief_examine_super1,
0d726f17 1712 .export_examine_super = export_examine_super1,
82d9eba6
NB
1713 .detail_super = detail_super1,
1714 .brief_detail_super = brief_detail_super1,
0d726f17 1715 .export_detail_super = export_detail_super1,
111d01fc 1716 .write_init_super = write_init_super1,
0e600426
N
1717 .validate_geometry = validate_geometry1,
1718 .add_to_super = add_to_super1,
c7654afc 1719#endif
83b6208e 1720 .match_home = match_home1,
82d9eba6
NB
1721 .uuid_from_super = uuid_from_super1,
1722 .getinfo_super = getinfo_super1,
00bbdbda 1723 .container_content = container_content1,
82d9eba6 1724 .update_super = update_super1,
82d9eba6 1725 .init_super = init_super1,
82d9eba6 1726 .store_super = store_super1,
82d9eba6
NB
1727 .compare_super = compare_super1,
1728 .load_super = load_super1,
1729 .match_metadata_desc = match_metadata_desc1,
1730 .avail_size = avail_size1,
34163fc7
NB
1731 .add_internal_bitmap = add_internal_bitmap1,
1732 .locate_bitmap = locate_bitmap1,
1733 .write_bitmap = write_bitmap1,
df37ffc0 1734 .free_super = free_super1,
f277ce36
NB
1735#if __BYTE_ORDER == BIG_ENDIAN
1736 .swapuuid = 0,
1737#else
1738 .swapuuid = 1,
1739#endif
31015d57 1740 .name = "1.x",
82d9eba6 1741};