Make sure homehost is set correctly when --update=uuid
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4f589ad0 4 * Copyright (C) 2001-2006 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
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30#include "mdadm.h"
82d9eba6
NB
31/*
32 * The version-1 superblock :
33 * All numeric fields are little-endian.
34 *
35 * total size: 256 bytes plus 2 per device.
36 * 1K allows 384 devices.
37 */
38struct mdp_superblock_1 {
39 /* constant array information - 128 bytes */
40 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
41 __u32 major_version; /* 1 */
42 __u32 feature_map; /* 0 for now */
43 __u32 pad0; /* always set to 0 when writing */
44
45 __u8 set_uuid[16]; /* user-space generated. */
46 char set_name[32]; /* set and interpreted by user-space */
47
48 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
49 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
50 __u32 layout; /* only for raid5 currently */
51 __u64 size; /* used size of component devices, in 512byte sectors */
52
53 __u32 chunksize; /* in 512byte sectors */
54 __u32 raid_disks;
34163fc7
NB
55 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
56 * NOTE: signed, so bitmap can be before superblock
57 * only meaningful of feature_map[0] is set.
58 */
b674b5b8
NB
59
60 /* These are only valid with feature bit '4' */
b674b5b8 61 __u32 new_level; /* new level we are reshaping to */
ac957baa 62 __u64 reshape_position; /* next address in array-space for reshape */
b674b5b8
NB
63 __u32 delta_disks; /* change in number of raid_disks */
64 __u32 new_layout; /* new layout */
65 __u32 new_chunk; /* new chunk size (bytes) */
66 __u8 pad1[128-124]; /* set to 0 when written */
82d9eba6
NB
67
68 /* constant this-device information - 64 bytes */
69 __u64 data_offset; /* sector start of data, often 0 */
70 __u64 data_size; /* sectors in this device that can be used for data */
71 __u64 super_offset; /* sector start of this superblock */
72 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
73 __u32 dev_number; /* permanent identifier of this device - not role in raid */
74 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
75 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
dfd4d8ee
NB
76 __u8 devflags; /* per-device flags. Only one defined...*/
77#define WriteMostly1 1 /* mask for writemostly flag in above */
78 __u8 pad2[64-57]; /* set to 0 when writing */
82d9eba6
NB
79
80 /* array state information - 64 bytes */
81 __u64 utime; /* 40 bits second, 24 btes microseconds */
82 __u64 events; /* incremented when superblock updated */
83 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
84 __u32 sb_csum; /* checksum upto devs[max_dev] */
85 __u32 max_dev; /* size of devs[] array to consider */
86 __u8 pad3[64-32]; /* set to 0 when writing */
87
88 /* device state information. Indexed by dev_number.
89 * 2 bytes per device
90 * Note there are no per-device state flags. State information is rolled
91 * into the 'roles' value. If a device is spare or faulty, then it doesn't
92 * have a meaningful role.
93 */
94 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
95};
96
d2cd3ffc
NB
97/* feature_map bits */
98#define MD_FEATURE_BITMAP_OFFSET 1
99#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
100 * must be honoured
101 */
b674b5b8 102#define MD_FEATURE_RESHAPE_ACTIVE 4
d2cd3ffc 103
b674b5b8 104#define MD_FEATURE_ALL (1|2|4)
d2cd3ffc 105
570c0542 106#ifndef offsetof
82d9eba6 107#define offsetof(t,f) ((int)&(((t*)0)->f))
570c0542 108#endif
82d9eba6
NB
109static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
110{
111 unsigned int disk_csum, csum;
112 unsigned long long newcsum;
113 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
114 unsigned int *isuper = (unsigned int*)sb;
115 int i;
116
117/* make sure I can count... */
118 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
119 offsetof(struct mdp_superblock_1, utime) != 192 ||
120 sizeof(struct mdp_superblock_1) != 256) {
121 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
122 }
123
124 disk_csum = sb->sb_csum;
125 sb->sb_csum = 0;
126 newcsum = 0;
127 for (i=0; size>=4; size -= 4 )
128 newcsum += __le32_to_cpu(*isuper++);
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;
135 return csum;
136}
137
c7654afc 138#ifndef MDASSEMBLE
a1cbd7d0 139static void examine_super1(void *sbv, char *homehost)
82d9eba6
NB
140{
141 struct mdp_superblock_1 *sb = sbv;
142 time_t atime;
143 int d;
6fbba4c9 144 int faulty;
82d9eba6
NB
145 int i;
146 char *c;
a1cbd7d0 147 int l = homehost ? strlen(homehost) : 0;
82d9eba6
NB
148
149 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
91eedefc
NB
150 printf(" Version : %02d\n", 1);
151 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
152 printf(" Array UUID : ");
153 for (i=0; i<16; i++) {
82d9eba6 154 if ((i&3)==0 && i != 0) printf(":");
34163fc7 155 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
156 }
157 printf("\n");
a1cbd7d0
NB
158 printf(" Name : %.32s", sb->set_name);
159 if (l > 0 && l < 32 &&
160 sb->set_name[l] == ':' &&
161 strncmp(sb->set_name, homehost, l) == 0)
162 printf(" (local to host %s)", homehost);
163 printf("\n");
82d9eba6
NB
164 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
165 printf(" Creation Time : %.24s\n", ctime(&atime));
166 c=map_num(pers, __le32_to_cpu(sb->level));
167 printf(" Raid Level : %s\n", c?c:"-unknown-");
168 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
169 printf("\n");
570c0542 170 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
b674b5b8
NB
171 if (__le32_to_cpu(sb->level) >= 0) {
172 int ddsks=0;
173 switch(__le32_to_cpu(sb->level)) {
174 case 1: ddsks=1;break;
175 case 4:
176 case 5: ddsks = sb->raid_disks-1; break;
177 case 6: ddsks = sb->raid_disks-2; break;
178 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
179 }
180 if (ddsks)
181 printf(" Array Size : %llu%s\n", ddsks*(unsigned long long)sb->size, human_size(ddsks*sb->size<<9));
182 if (sb->size != sb->data_size)
183 printf(" Used Size : %llu%s\n", (unsigned long long)sb->size, human_size(sb->size<<9));
184 }
82d9eba6 185 if (sb->data_offset)
570c0542 186 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
82d9eba6 187 if (sb->super_offset)
570c0542 188 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
189 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
190 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 191 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
192 printf(" Device UUID : ");
193 for (i=0; i<16; i++) {
82d9eba6 194 if ((i&3)==0 && i != 0) printf(":");
34163fc7 195 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
196 }
197 printf("\n");
b674b5b8 198 printf("\n");
91eedefc
NB
199 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
200 printf("Internal Bitmap : %ld sectors from superblock\n",
8fac0577
NB
201 (long)__le32_to_cpu(sb->bitmap_offset));
202 }
b674b5b8 203 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 204 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
205 human_size(__le64_to_cpu(sb->reshape_position)<<9));
206 if (__le32_to_cpu(sb->delta_disks)) {
207 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
208 if (__le32_to_cpu(sb->delta_disks))
209 printf(" (%d->%d)\n",
210 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
211 __le32_to_cpu(sb->raid_disks));
212 else
213 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
214 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
215 }
216 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
217 c = map_num(pers, __le32_to_cpu(sb->new_level));
218 printf(" New Level : %s\n", c?c:"-unknown-");
219 }
220 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
221 if (__le32_to_cpu(sb->level) == 5) {
222 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
223 printf(" New Layout : %s\n", c?c:"-unknown-");
224 }
225 if (__le32_to_cpu(sb->level) == 10) {
b578481c 226 printf(" New Layout : near=%d, %s=%d\n",
b674b5b8 227 __le32_to_cpu(sb->new_layout)&255,
b578481c 228 (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
b674b5b8
NB
229 (__le32_to_cpu(sb->new_layout)>>8)&255);
230 }
231 }
232 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
233 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
234 printf("\n");
235 }
dfd4d8ee
NB
236 if (sb->devflags) {
237 printf(" Flags :");
34163fc7 238 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
239 printf(" write-mostly");
240 printf("\n");
241 }
82d9eba6
NB
242
243 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
244 printf(" Update Time : %.24s\n", ctime(&atime));
245
246 if (calc_sb_1_csum(sb) == sb->sb_csum)
247 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
248 else
249 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
250 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 251 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
252 printf("\n");
253 if (__le32_to_cpu(sb->level) == 5) {
254 c = map_num(r5layout, __le32_to_cpu(sb->layout));
255 printf(" Layout : %s\n", c?c:"-unknown-");
256 }
265e0f17
NB
257 if (__le32_to_cpu(sb->level) == 10) {
258 int lo = __le32_to_cpu(sb->layout);
b578481c
NB
259 printf(" Layout : near=%d, %s=%d\n",
260 lo&255,
261 (lo&0x10000)?"offset":"far",
262 (lo>>8)&255);
265e0f17 263 }
82d9eba6
NB
264 switch(__le32_to_cpu(sb->level)) {
265 case 0:
266 case 4:
267 case 5:
d2cd3ffc
NB
268 case 6:
269 case 10:
b674b5b8 270 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
271 break;
272 case -1:
b674b5b8 273 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
274 break;
275 default: break;
276 }
277 printf("\n");
278 printf(" Array State : ");
279 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
280 int cnt = 0;
281 int me = 0;
282 int i;
283 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
284 int role = __le16_to_cpu(sb->dev_roles[i]);
285 if (role == d) {
286 if (i == __le32_to_cpu(sb->dev_number))
287 me = 1;
288 cnt++;
289 }
290 }
291 if (cnt > 1) printf("?");
292 else if (cnt == 1 && me) printf("U");
293 else if (cnt == 1) printf("u");
294 else printf ("_");
295 }
6fbba4c9 296 faulty = 0;
82d9eba6
NB
297 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
298 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
299 if (role == 0xFFFE)
300 faulty++;
82d9eba6 301 }
82d9eba6
NB
302 if (faulty) printf(" %d failed", faulty);
303 printf("\n");
304}
305
306
307static void brief_examine_super1(void *sbv)
308{
309 struct mdp_superblock_1 *sb = sbv;
310 int i;
311
312 char *c=map_num(pers, __le32_to_cpu(sb->level));
313
314 printf("ARRAY /dev/?? level=%s metadata=1 num-devices=%d UUID=",
315 c?c:"-unknown-", sb->raid_disks);
316 for (i=0; i<16; i++) {
317 printf("%02x", sb->set_uuid[i]);
318 if ((i&3)==0 && i != 0) printf(":");
319 }
947fd4dd
NB
320 if (sb->set_name[0])
321 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
322 printf("\n");
323}
324
b6750aa8 325static void detail_super1(void *sbv, char *homehost)
82d9eba6
NB
326{
327 struct mdp_superblock_1 *sb = sbv;
328 int i;
b6750aa8 329 int l = homehost ? strlen(homehost) : 0;
82d9eba6 330
b6750aa8
NB
331 printf(" Name : %.32s", sb->set_name);
332 if (l > 0 && l < 32 &&
333 sb->set_name[l] == ':' &&
334 strncmp(sb->set_name, homehost, l) == 0)
335 printf(" (local to host %s)", homehost);
336 printf("\n UUID : ");
82d9eba6 337 for (i=0; i<16; i++) {
82d9eba6 338 if ((i&3)==0 && i != 0) printf(":");
34163fc7 339 printf("%02x", sb->set_uuid[i]);
82d9eba6 340 }
570c0542 341 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
342}
343
344static void brief_detail_super1(void *sbv)
345{
346 struct mdp_superblock_1 *sb = sbv;
347 int i;
348
947fd4dd
NB
349 if (sb->set_name[0])
350 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
351 printf(" UUID=");
352 for (i=0; i<16; i++) {
82d9eba6 353 if ((i&3)==0 && i != 0) printf(":");
34163fc7 354 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
355 }
356}
357
c7654afc
NB
358#endif
359
82d9eba6
NB
360static void uuid_from_super1(int uuid[4], void * sbv)
361{
362 struct mdp_superblock_1 *super = sbv;
363 char *cuuid = (char*)uuid;
364 int i;
365 for (i=0; i<16; i++)
366 cuuid[i] = super->set_uuid[i];
367}
368
31317663 369static void getinfo_super1(struct mdinfo *info, void *sbv)
82d9eba6
NB
370{
371 struct mdp_superblock_1 *sb = sbv;
372 int working = 0;
373 int i;
374 int role;
375
376 info->array.major_version = 1;
377 info->array.minor_version = __le32_to_cpu(sb->feature_map);
378 info->array.patch_version = 0;
379 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
380 info->array.level = __le32_to_cpu(sb->level);
265e0f17 381 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
382 info->array.md_minor = -1;
383 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 384 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 385 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
353632d9
NB
386
387 info->data_offset = __le64_to_cpu(sb->data_offset);
388 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
389
390 info->disk.major = 0;
391 info->disk.minor = 0;
fbf8a0b7 392 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
393 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
394 __le32_to_cpu(sb->max_dev) > 512)
395 role = 0xfffe;
396 else
397 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
398
399 info->disk.raid_disk = -1;
400 switch(role) {
401 case 0xFFFF:
402 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
403 break;
404 case 0xFFFE:
405 info->disk.state = 1; /* faulty */
406 break;
407 default:
408 info->disk.state = 6; /* active and in sync */
409 info->disk.raid_disk = role;
410 }
411 info->events = __le64_to_cpu(sb->events);
412
413 memcpy(info->uuid, sb->set_uuid, 16);
414
31317663
NB
415 strncpy(info->name, sb->set_name, 32);
416 info->name[32] = 0;
947fd4dd 417
353632d9
NB
418 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
419 info->reshape_active = 1;
420 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
421 info->new_level = __le32_to_cpu(sb->new_level);
422 info->delta_disks = __le32_to_cpu(sb->delta_disks);
423 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 424 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
353632d9
NB
425 } else
426 info->reshape_active = 0;
427
82d9eba6
NB
428 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
429 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 430 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
431 working++;
432 }
433
434 info->array.working_disks = working;
435}
436
e5eac01f
NB
437static int update_super1(struct mdinfo *info, void *sbv, char *update,
438 char *devname, int verbose,
439 int uuid_set, char *homehost)
82d9eba6 440{
c4d831e1
NB
441 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
442 * For others, the return value is ignored.
443 */
82d9eba6
NB
444 int rv = 0;
445 struct mdp_superblock_1 *sb = sbv;
446
447 if (strcmp(update, "force")==0) {
c4d831e1
NB
448 if (sb->events != __cpu_to_le64(info->events))
449 rv = 1;
6d3d5804 450 sb->events = __cpu_to_le64(info->events);
82d9eba6
NB
451 switch(__le32_to_cpu(sb->level)) {
452 case 5: case 4: case 6:
453 /* need to force clean */
c4d831e1
NB
454 if (sb->resync_offset != ~0ULL)
455 rv = 1;
82d9eba6
NB
456 sb->resync_offset = ~0ULL;
457 }
458 }
459 if (strcmp(update, "assemble")==0) {
460 int d = info->disk.number;
461 int want;
462 if (info->disk.state == 6)
463 want = __cpu_to_le32(info->disk.raid_disk);
464 else
465 want = 0xFFFF;
466 if (sb->dev_roles[d] != want) {
467 sb->dev_roles[d] = want;
468 rv = 1;
469 }
470 }
471#if 0
472 if (strcmp(update, "newdev") == 0) {
473 int d = info->disk.number;
474 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
475 sb->disks[d].number = d;
476 sb->disks[d].major = info->disk.major;
477 sb->disks[d].minor = info->disk.minor;
478 sb->disks[d].raid_disk = info->disk.raid_disk;
479 sb->disks[d].state = info->disk.state;
480 sb->this_disk = sb->disks[d];
481 }
482#endif
483 if (strcmp(update, "grow") == 0) {
484 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
485 /* FIXME */
486 }
487 if (strcmp(update, "resync") == 0) {
488 /* make sure resync happens */
489 sb->resync_offset = ~0ULL;
490 }
14263cf1 491 if (strcmp(update, "uuid") == 0) {
838acbc2 492 memcpy(sb->set_uuid, info->uuid, 16);
14263cf1
NB
493 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
494 struct bitmap_super_s *bm;
495 bm = (struct bitmap_super_s*)(sbv+1024);
496 memcpy(bm->uuid, info->uuid, 16);
497 }
498 }
353632d9
NB
499 if (strcmp(update, "_reshape_progress")==0)
500 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
501
502 sb->sb_csum = calc_sb_1_csum(sb);
503 return rv;
504}
505
506
507static __u64 event_super1(void *sbv)
508{
509 struct mdp_superblock_1 *sb = sbv;
510 return __le64_to_cpu(sb->events);
511}
512
05697ec1
NB
513static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
514 unsigned long long size, char *name, char *homehost)
82d9eba6 515{
34163fc7 516 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 517 int spares;
34163fc7 518 int rfd;
05697ec1 519 char defname[10];
82d9eba6
NB
520 memset(sb, 0, 1024);
521
05697ec1 522 if (info->major_version == -1) {
82d9eba6 523 /* zeroing superblock */
05697ec1 524 *sbp = sb;
82d9eba6 525 return 0;
05697ec1 526 }
82d9eba6
NB
527
528 spares = info->working_disks - info->active_disks;
529 if (info->raid_disks + spares > 384) {
530 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
531 info->raid_disks , spares, 384);
532 return 0;
533 }
534
82d9eba6
NB
535 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
536 sb->major_version = __cpu_to_le32(1);
537 sb->feature_map = 0;
538 sb->pad0 = 0;
34163fc7
NB
539
540 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
541 read(rfd, sb->set_uuid, 16) != 16) {
542 *(__u32*)(sb->set_uuid) = random();
543 *(__u32*)(sb->set_uuid+4) = random();
544 *(__u32*)(sb->set_uuid+8) = random();
545 *(__u32*)(sb->set_uuid+12) = random();
546 }
547 if (rfd >= 0) close(rfd);
82d9eba6 548
05697ec1
NB
549 if (name == NULL || *name == 0) {
550 sprintf(defname, "%d", info->md_minor);
551 name = defname;
552 }
947fd4dd 553 memset(sb->set_name, 0, 32);
05697ec1
NB
554 if (homehost &&
555 strchr(name, ':')== NULL &&
556 strlen(homehost)+1+strlen(name) < 32) {
557 strcpy(sb->set_name, homehost);
558 strcat(sb->set_name, ":");
559 strcat(sb->set_name, name);
560 } else
561 strcpy(sb->set_name, name);
82d9eba6
NB
562
563 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
564 sb->level = __cpu_to_le32(info->level);
ea329559 565 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 566 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
567 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
568 sb->raid_disks = __cpu_to_le32(info->raid_disks);
569
570 sb->data_offset = __cpu_to_le64(0);
571 sb->data_size = __cpu_to_le64(0);
572 sb->super_offset = __cpu_to_le64(0);
573 sb->recovery_offset = __cpu_to_le64(0);
574
575 sb->utime = sb->ctime;
576 sb->events = __cpu_to_le64(1);
34321279 577 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
578 sb->resync_offset = ~0ULL;
579 else
580 sb->resync_offset = 0;
34163fc7 581 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
582 sizeof(sb->dev_roles[0]));
583 memset(sb->pad3, 0, sizeof(sb->pad3));
584
585 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
586
587 *sbp = sb;
588 return 1;
589}
590
591/* Add a device to the superblock being created */
592static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
593{
594 struct mdp_superblock_1 *sb = sbv;
595 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 596 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 597 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 598 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 599 *rp = 0xffff;
34163fc7 600 else
82d9eba6
NB
601 *rp = 0xfffe;
602}
603
14263cf1
NB
604static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
605
96395475 606static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
607{
608 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 609 unsigned long long sb_offset;
82d9eba6 610 int sbsize;
5dd497ee
NB
611 unsigned long size;
612 unsigned long long dsize;
96395475 613
5dd497ee
NB
614#ifdef BLKGETSIZE64
615 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
616#endif
617 {
618 if (ioctl(fd, BLKGETSIZE, &size))
619 return 1;
620 else
621 dsize = (unsigned long long)size;
622 } else
623 dsize >>= 9;
96395475 624
5dd497ee 625 if (dsize < 24)
96395475
NB
626 return 2;
627
628 /*
629 * Calculate the position of the superblock.
630 * It is always aligned to a 4K boundary and
631 * depending on minor_version, it can be:
632 * 0: At least 8K, but less than 12K, from end of device
633 * 1: At start of device
634 * 2: 4K from start of device.
635 */
636 switch(st->minor_version) {
637 case 0:
5dd497ee 638 sb_offset = dsize;
96395475
NB
639 sb_offset -= 8*2;
640 sb_offset &= ~(4*2-1);
641 break;
642 case 1:
6fbba4c9 643 sb_offset = 0;
96395475
NB
644 break;
645 case 2:
646 sb_offset = 4*2;
647 break;
648 default:
649 return -EINVAL;
650 }
651
82d9eba6 652
34163fc7 653
6fbba4c9
NB
654 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
655 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
656 ) {
657 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
658 abort();
659 }
82d9eba6 660
6fbba4c9 661 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
662 return 3;
663
664 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
665
666 if (write(fd, sb, sbsize) != sbsize)
667 return 4;
668
14263cf1
NB
669 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
670 struct bitmap_super_s *bm = (struct bitmap_super_s*)
671 ((char*)sb)+1024;
672 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
673 locate_bitmap1(st, fd, sbv);
674 write(fd, bm, sizeof(*bm));
675 }
676 }
570c0542 677 fsync(fd);
82d9eba6
NB
678 return 0;
679}
680
892debc8
NB
681static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
682
34163fc7 683static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 684 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
685{
686 struct mdp_superblock_1 *sb = sbv;
e478dc86 687 void *refsbv = NULL;
892debc8 688 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 689 int rfd;
82d9eba6
NB
690 int rv;
691
8fac0577
NB
692 unsigned long size, space;
693 unsigned long long dsize, array_size;
82d9eba6
NB
694 long long sb_offset;
695
696
697 if (fd < 0) {
698 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
699 devname);
700 return -1;
701 }
702
703 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 704 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
dfd4d8ee 705 sb->devflags |= WriteMostly1;
892debc8 706
dfe47e00
NB
707 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
708 read(rfd, sb->device_uuid, 16) != 16) {
709 *(__u32*)(sb->device_uuid) = random();
710 *(__u32*)(sb->device_uuid+4) = random();
711 *(__u32*)(sb->device_uuid+8) = random();
712 *(__u32*)(sb->device_uuid+12) = random();
713 }
714 if (rfd >= 0) close(rfd);
892debc8
NB
715 sb->events = 0;
716
e478dc86
NB
717 if (load_super1(st, fd, &refsbv, NULL)==0) {
718 struct mdp_superblock_1 *refsb = refsbv;
719
892debc8
NB
720 memcpy(sb->device_uuid, refsb->device_uuid, 16);
721 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
722 /* same array, so preserve events and dev_number */
723 sb->events = refsb->events;
724 sb->dev_number = refsb->dev_number;
725 }
726 free(refsb);
727 }
34163fc7 728
5dd497ee
NB
729#ifdef BLKGETSIZE64
730 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
731#endif
732 {
733 if (ioctl(fd, BLKGETSIZE, &size))
734 return 1;
735 else
736 dsize = size;
737 } else
738 dsize >>= 9;
82d9eba6 739
5dd497ee 740 if (dsize < 24) {
024ce7fe 741 close(fd);
82d9eba6 742 return 2;
024ce7fe 743 }
82d9eba6
NB
744
745
746 /*
747 * Calculate the position of the superblock.
748 * It is always aligned to a 4K boundary and
749 * depending on minor_version, it can be:
750 * 0: At least 8K, but less than 12K, from end of device
751 * 1: At start of device
752 * 2: 4K from start of device.
21e92547
NB
753 * Depending on the array size, we might leave extra space
754 * for a bitmap.
82d9eba6 755 */
8fac0577 756 array_size = __le64_to_cpu(sb->size);
82d9eba6
NB
757 switch(st->minor_version) {
758 case 0:
5dd497ee 759 sb_offset = dsize;
82d9eba6
NB
760 sb_offset -= 8*2;
761 sb_offset &= ~(4*2-1);
762 sb->super_offset = __cpu_to_le64(sb_offset);
763 sb->data_offset = __cpu_to_le64(0);
561832f9 764 if (sb_offset-64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
765 sb->data_size = __cpu_to_le64(sb_offset-64*2);
766 else
767 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
768 break;
769 case 1:
770 sb->super_offset = __cpu_to_le64(0);
561832f9 771 if (dsize - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
772 space = 64*2;
773 else
774 space = 4*2;
775 sb->data_offset = __cpu_to_le64(space); /* leave space for super and bitmap */
776 sb->data_size = __cpu_to_le64(dsize - space);
82d9eba6
NB
777 break;
778 case 2:
779 sb_offset = 4*2;
561832f9 780 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
781 space = 64*2;
782 else
783 space = 4*2;
82d9eba6 784 sb->super_offset = __cpu_to_le64(sb_offset);
21e92547
NB
785 sb->data_offset = __cpu_to_le64(sb_offset+space);
786 sb->data_size = __cpu_to_le64(dsize - 4*2 - space);
82d9eba6
NB
787 break;
788 default:
789 return -EINVAL;
790 }
791
792
793 sb->sb_csum = calc_sb_1_csum(sb);
96395475 794 rv = store_super1(st, fd, sb);
82d9eba6
NB
795 if (rv)
796 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 797
1bf4e2d9
NB
798 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
799 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 800 close(fd);
82d9eba6
NB
801 return rv;
802}
803
804static int compare_super1(void **firstp, void *secondv)
805{
806 /*
807 * return:
808 * 0 same, or first was empty, and second was copied
809 * 1 second had wrong number
810 * 2 wrong uuid
811 * 3 wrong other info
812 */
813 struct mdp_superblock_1 *first = *firstp;
814 struct mdp_superblock_1 *second = secondv;
815
816 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
817 return 1;
818 if (second->major_version != __cpu_to_le32(1))
819 return 1;
820
821 if (!first) {
14263cf1
NB
822 first = malloc(1024+sizeof(bitmap_super_t));
823 memcpy(first, second, 1024+sizeof(bitmap_super_t));
82d9eba6
NB
824 *firstp = first;
825 return 0;
826 }
827 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
828 return 2;
829
830 if (first->ctime != second->ctime ||
831 first->level != second->level ||
832 first->layout != second->layout ||
833 first->size != second->size ||
834 first->chunksize != second->chunksize ||
835 first->raid_disks != second->raid_disks)
836 return 3;
837 return 0;
838}
839
840static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
841{
842 unsigned long size;
5dd497ee 843 unsigned long long dsize;
82d9eba6
NB
844 unsigned long long sb_offset;
845 struct mdp_superblock_1 *super;
14263cf1
NB
846 int uuid[4];
847 struct bitmap_super_s *bsb;
82d9eba6
NB
848
849
850 if (st->ss == NULL) {
570c0542
NB
851 int bestvers = -1;
852 __u64 bestctime = 0;
853 /* guess... choose latest ctime */
82d9eba6
NB
854 st->ss = &super1;
855 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
856 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
857 case 0: super = *sbp;
858 if (bestvers == -1 ||
859 bestctime < __le64_to_cpu(super->ctime)) {
860 bestvers = st->minor_version;
861 bestctime = __le64_to_cpu(super->ctime);
862 }
863 free(super);
864 *sbp = NULL;
865 break;
82d9eba6
NB
866 case 1: st->ss = NULL; return 1; /*bad device */
867 case 2: break; /* bad, try next */
868 }
869 }
570c0542
NB
870 if (bestvers != -1) {
871 int rv;
872 st->minor_version = bestvers;
873 st->ss = &super1;
ea329559 874 st->max_devs = 384;
570c0542
NB
875 rv = load_super1(st, fd, sbp, devname);
876 if (rv) st->ss = NULL;
877 return rv;
878 }
82d9eba6
NB
879 st->ss = NULL;
880 return 2;
881 }
5dd497ee
NB
882#ifdef BLKGETSIZE64
883 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
884#endif
885 {
886 if (ioctl(fd, BLKGETSIZE, &size)) {
887 if (devname)
888 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
889 devname, strerror(errno));
890 return 1;
891 }
892 dsize = size;
893 } else
894 dsize >>= 9;
82d9eba6 895
5dd497ee 896 if (dsize < 24) {
82d9eba6 897 if (devname)
5dd497ee
NB
898 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
899 devname, dsize);
82d9eba6
NB
900 return 1;
901 }
902
903 /*
904 * Calculate the position of the superblock.
905 * It is always aligned to a 4K boundary and
14263cf1 906 * depending on minor_version, it can be:
82d9eba6
NB
907 * 0: At least 8K, but less than 12K, from end of device
908 * 1: At start of device
909 * 2: 4K from start of device.
910 */
911 switch(st->minor_version) {
912 case 0:
5dd497ee 913 sb_offset = dsize;
82d9eba6
NB
914 sb_offset -= 8*2;
915 sb_offset &= ~(4*2-1);
916 break;
917 case 1:
918 sb_offset = 0;
919 break;
920 case 2:
921 sb_offset = 4*2;
922 break;
923 default:
924 return -EINVAL;
925 }
926
927 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
928
929
930 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
931 if (devname)
932 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
933 devname, strerror(errno));
934 return 1;
935 }
936
34163fc7 937 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
938
939 if (read(fd, super, 1024) != 1024) {
940 if (devname)
941 fprintf(stderr, Name ": Cannot read superblock on %s\n",
942 devname);
943 free(super);
944 return 1;
945 }
946
947 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
948 if (devname)
949 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
950 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
951 free(super);
952 return 2;
953 }
954
955 if (__le32_to_cpu(super->major_version) != 1) {
956 if (devname)
957 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
958 devname, __le32_to_cpu(super->major_version));
959 free(super);
960 return 2;
961 }
962 if (__le64_to_cpu(super->super_offset) != sb_offset) {
963 if (devname)
964 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
965 devname);
966 free(super);
967 return 2;
968 }
969 *sbp = super;
14263cf1
NB
970
971 /* Now check on the bitmap superblock */
972 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
973 return 0;
974 /* Read the bitmap superblock and make sure it looks
975 * valid. If it doesn't clear the bit. An --assemble --force
976 * should get that written out.
977 */
978 locate_bitmap1(st, fd, super);
979 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
980 != sizeof(struct bitmap_super_s))
981 goto no_bitmap;
982
983 uuid_from_super1(uuid, super);
984 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
985 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
986 memcmp(bsb->uuid, uuid, 16) != 0)
987 goto no_bitmap;
988 return 0;
989
990 no_bitmap:
991 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
992 return 0;
993}
994
995
996static struct supertype *match_metadata_desc1(char *arg)
997{
998 struct supertype *st = malloc(sizeof(*st));
999 if (!st) return st;
1000
1001 st->ss = &super1;
ea329559 1002 st->max_devs = 384;
82d9eba6 1003 if (strcmp(arg, "1") == 0 ||
576d6d83
NB
1004 strcmp(arg, "1.0") == 0 ||
1005 strcmp(arg, "default/large") == 0) {
82d9eba6
NB
1006 st->minor_version = 0;
1007 return st;
1008 }
1009 if (strcmp(arg, "1.1") == 0) {
1010 st->minor_version = 1;
1011 return st;
1012 }
1013 if (strcmp(arg, "1.2") == 0) {
1014 st->minor_version = 2;
1015 return st;
1016 }
1017
1018 free(st);
1019 return NULL;
1020}
1021
34163fc7
NB
1022/* find available size on device with this devsize, using
1023 * superblock type st, and reserving 'reserve' sectors for
1024 * a possible bitmap
1025 */
1bf4e2d9 1026static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
1027{
1028 if (devsize < 24)
1029 return 0;
1030
21e92547
NB
1031 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1032 * if biffer than 200Gig, save 128k
1033 */
1034 if (devsize > 200*1024*1024*2)
1035 devsize -= 128*2;
1036 else if (devsize > 8*1024*1024*2)
1037 devsize -= 64*2;
1038
34163fc7
NB
1039 switch(st->minor_version) {
1040 case 0:
1bf4e2d9
NB
1041 /* at end */
1042 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1043 case 1:
1bf4e2d9
NB
1044 /* at start, 4K for superblock and possible bitmap */
1045 return devsize - 4*2;
34163fc7 1046 case 2:
1bf4e2d9
NB
1047 /* 4k from start, 4K for superblock and possible bitmap */
1048 return devsize - (4+4)*2;
34163fc7
NB
1049 }
1050 return 0;
1051}
1052
1bf4e2d9
NB
1053static int
1054add_internal_bitmap1(struct supertype *st, void *sbv,
21e92547
NB
1055 int chunk, int delay, int write_behind, unsigned long long size,
1056 int may_change, int major)
34163fc7
NB
1057{
1058 /*
1bf4e2d9
NB
1059 * If not may_change, then this is a 'Grow', and the bitmap
1060 * must fit after the superblock.
1061 * If may_change, then this is create, and we can put the bitmap
1062 * before the superblock if we like, or may move the start.
1063 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7 1064 *
f9c25f1d 1065 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1066 */
1067
1bf4e2d9
NB
1068 unsigned long long bits;
1069 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1070 unsigned long long min_chunk;
1071 struct mdp_superblock_1 *sb = sbv;
1072 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1073
1bf4e2d9
NB
1074 if (st->minor_version && !may_change &&
1075 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
1076 return 0; /* doesn't fit */
1077
1078
34163fc7
NB
1079
1080 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1081 bits = (size*512)/min_chunk +1;
34163fc7
NB
1082 while (bits > max_bits) {
1083 min_chunk *= 2;
1084 bits = (bits+1)/2;
1085 }
1086 if (chunk == UnSet)
1087 chunk = min_chunk;
1088 else if (chunk < min_chunk)
1089 return 0; /* chunk size too small */
21e92547
NB
1090 if (chunk == 0) /* rounding problem */
1091 return 0;
34163fc7 1092
1bf4e2d9 1093 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
1094
1095 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1096 memset(bms, 0, sizeof(*bms));
34163fc7 1097 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1098 bms->version = __cpu_to_le32(major);
34163fc7
NB
1099 uuid_from_super1((int*)bms->uuid, sb);
1100 bms->chunksize = __cpu_to_le32(chunk);
1101 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1102 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1103 bms->write_behind = __cpu_to_le32(write_behind);
1104
34163fc7
NB
1105 return 1;
1106}
1107
1108
14263cf1 1109static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 1110{
34163fc7 1111 unsigned long long offset;
1e0d770c
NB
1112 struct mdp_superblock_1 *sb;
1113 int mustfree = 0;
34163fc7 1114
1e0d770c
NB
1115 if (!sbv) {
1116 if (st->ss->load_super(st, fd, &sbv, NULL))
f6d75de8 1117 return; /* no error I hope... */
1e0d770c
NB
1118 mustfree = 1;
1119 }
1120 sb = sbv;
e478dc86 1121
1bf4e2d9
NB
1122 offset = __le64_to_cpu(sb->super_offset);
1123 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1124 if (mustfree)
f6d75de8 1125 free(sb);
1bf4e2d9 1126 lseek64(fd, offset<<9, 0);
34163fc7
NB
1127}
1128
14263cf1 1129static int write_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7
NB
1130{
1131 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 1132 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1133 int rv = 0;
1134
1135 int towrite, n;
1136 char buf[4096];
1137
f6d75de8 1138 locate_bitmap1(st, fd, sbv);
34163fc7 1139
1e0d770c
NB
1140 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1141 sizeof(bitmap_super_t))
1142 return -2;
1bf4e2d9
NB
1143 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1144 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1145 memset(buf, 0xff, sizeof(buf));
1146 while (towrite > 0) {
1147 n = towrite;
1148 if (n > sizeof(buf))
1149 n = sizeof(buf);
1150 n = write(fd, buf, n);
1151 if (n > 0)
1152 towrite -= n;
1153 else
1154 break;
1155 }
1156 fsync(fd);
1157 if (towrite)
1158 rv = -2;
1159
1160 return rv;
82d9eba6
NB
1161}
1162
1163struct superswitch super1 = {
c7654afc 1164#ifndef MDASSEMBLE
82d9eba6
NB
1165 .examine_super = examine_super1,
1166 .brief_examine_super = brief_examine_super1,
1167 .detail_super = detail_super1,
1168 .brief_detail_super = brief_detail_super1,
c7654afc 1169#endif
82d9eba6
NB
1170 .uuid_from_super = uuid_from_super1,
1171 .getinfo_super = getinfo_super1,
1172 .update_super = update_super1,
1173 .event_super = event_super1,
1174 .init_super = init_super1,
1175 .add_to_super = add_to_super1,
1176 .store_super = store_super1,
1177 .write_init_super = write_init_super1,
1178 .compare_super = compare_super1,
1179 .load_super = load_super1,
1180 .match_metadata_desc = match_metadata_desc1,
1181 .avail_size = avail_size1,
34163fc7
NB
1182 .add_internal_bitmap = add_internal_bitmap1,
1183 .locate_bitmap = locate_bitmap1,
1184 .write_bitmap = write_bitmap1,
82d9eba6 1185 .major = 1,
f277ce36
NB
1186#if __BYTE_ORDER == BIG_ENDIAN
1187 .swapuuid = 0,
1188#else
1189 .swapuuid = 1,
1190#endif
82d9eba6 1191};