Choose better devnumbers and tidy up some issues with finding names.
[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
83b6208e
NB
360static int match_home1(void *sbv, char *homehost)
361{
362 struct mdp_superblock_1 *sb = sbv;
363 int l = homehost ? strlen(homehost) : 0;
364
365 return (l > 0 && l < 32 &&
366 sb->set_name[l] == ':' &&
367 strncmp(sb->set_name, homehost, l) == 0);
368}
369
82d9eba6
NB
370static void uuid_from_super1(int uuid[4], void * sbv)
371{
372 struct mdp_superblock_1 *super = sbv;
373 char *cuuid = (char*)uuid;
374 int i;
375 for (i=0; i<16; i++)
376 cuuid[i] = super->set_uuid[i];
377}
378
31317663 379static void getinfo_super1(struct mdinfo *info, void *sbv)
82d9eba6
NB
380{
381 struct mdp_superblock_1 *sb = sbv;
382 int working = 0;
383 int i;
384 int role;
385
386 info->array.major_version = 1;
387 info->array.minor_version = __le32_to_cpu(sb->feature_map);
388 info->array.patch_version = 0;
389 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
390 info->array.level = __le32_to_cpu(sb->level);
265e0f17 391 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
392 info->array.md_minor = -1;
393 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 394 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 395 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
353632d9
NB
396
397 info->data_offset = __le64_to_cpu(sb->data_offset);
398 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
399
400 info->disk.major = 0;
401 info->disk.minor = 0;
fbf8a0b7 402 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
403 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
404 __le32_to_cpu(sb->max_dev) > 512)
405 role = 0xfffe;
406 else
407 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
408
409 info->disk.raid_disk = -1;
410 switch(role) {
411 case 0xFFFF:
412 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
413 break;
414 case 0xFFFE:
415 info->disk.state = 1; /* faulty */
416 break;
417 default:
418 info->disk.state = 6; /* active and in sync */
419 info->disk.raid_disk = role;
420 }
421 info->events = __le64_to_cpu(sb->events);
422
423 memcpy(info->uuid, sb->set_uuid, 16);
424
31317663
NB
425 strncpy(info->name, sb->set_name, 32);
426 info->name[32] = 0;
947fd4dd 427
353632d9
NB
428 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
429 info->reshape_active = 1;
430 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
431 info->new_level = __le32_to_cpu(sb->new_level);
432 info->delta_disks = __le32_to_cpu(sb->delta_disks);
433 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 434 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
353632d9
NB
435 } else
436 info->reshape_active = 0;
437
82d9eba6
NB
438 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
439 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 440 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
441 working++;
442 }
443
444 info->array.working_disks = working;
445}
446
e5eac01f
NB
447static int update_super1(struct mdinfo *info, void *sbv, char *update,
448 char *devname, int verbose,
449 int uuid_set, char *homehost)
82d9eba6 450{
c4d831e1
NB
451 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
452 * For others, the return value is ignored.
453 */
82d9eba6
NB
454 int rv = 0;
455 struct mdp_superblock_1 *sb = sbv;
456
457 if (strcmp(update, "force")==0) {
c4d831e1
NB
458 if (sb->events != __cpu_to_le64(info->events))
459 rv = 1;
6d3d5804 460 sb->events = __cpu_to_le64(info->events);
82d9eba6
NB
461 switch(__le32_to_cpu(sb->level)) {
462 case 5: case 4: case 6:
463 /* need to force clean */
c4d831e1
NB
464 if (sb->resync_offset != ~0ULL)
465 rv = 1;
82d9eba6
NB
466 sb->resync_offset = ~0ULL;
467 }
468 }
469 if (strcmp(update, "assemble")==0) {
470 int d = info->disk.number;
471 int want;
472 if (info->disk.state == 6)
473 want = __cpu_to_le32(info->disk.raid_disk);
474 else
475 want = 0xFFFF;
476 if (sb->dev_roles[d] != want) {
477 sb->dev_roles[d] = want;
478 rv = 1;
479 }
480 }
481#if 0
482 if (strcmp(update, "newdev") == 0) {
483 int d = info->disk.number;
484 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
485 sb->disks[d].number = d;
486 sb->disks[d].major = info->disk.major;
487 sb->disks[d].minor = info->disk.minor;
488 sb->disks[d].raid_disk = info->disk.raid_disk;
489 sb->disks[d].state = info->disk.state;
490 sb->this_disk = sb->disks[d];
491 }
492#endif
493 if (strcmp(update, "grow") == 0) {
494 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
495 /* FIXME */
496 }
497 if (strcmp(update, "resync") == 0) {
498 /* make sure resync happens */
499 sb->resync_offset = ~0ULL;
500 }
14263cf1 501 if (strcmp(update, "uuid") == 0) {
838acbc2 502 memcpy(sb->set_uuid, info->uuid, 16);
14263cf1
NB
503 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
504 struct bitmap_super_s *bm;
505 bm = (struct bitmap_super_s*)(sbv+1024);
506 memcpy(bm->uuid, info->uuid, 16);
507 }
508 }
0237e0ca
NB
509 if (strcmp(update, "homehost") == 0 &&
510 homehost) {
511 char *c;
512 update = "name";
513 c = strchr(sb->set_name, ':');
514 if (c)
515 strncpy(info->name, c+1, 31 - (c-sb->set_name));
516 else
517 strncpy(info->name, sb->set_name, 32);
518 info->name[33] = 0;
519 }
c4f12c13
NB
520 if (strcmp(update, "name") == 0) {
521 if (info->name[0] == 0)
522 sprintf(info->name, "%d", info->array.md_minor);
523 memset(sb->set_name, 0, sizeof(sb->set_name));
524 if (homehost &&
525 strchr(info->name, ':') == NULL &&
526 strlen(homehost)+1+strlen(info->name) < 32) {
527 strcpy(sb->set_name, homehost);
528 strcat(sb->set_name, ":");
529 strcat(sb->set_name, info->name);
530 } else
531 strcpy(sb->set_name, info->name);
532 }
353632d9
NB
533 if (strcmp(update, "_reshape_progress")==0)
534 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
535
536 sb->sb_csum = calc_sb_1_csum(sb);
537 return rv;
538}
539
540
541static __u64 event_super1(void *sbv)
542{
543 struct mdp_superblock_1 *sb = sbv;
544 return __le64_to_cpu(sb->events);
545}
546
05697ec1
NB
547static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
548 unsigned long long size, char *name, char *homehost)
82d9eba6 549{
34163fc7 550 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 551 int spares;
34163fc7 552 int rfd;
05697ec1 553 char defname[10];
82d9eba6
NB
554 memset(sb, 0, 1024);
555
05697ec1 556 if (info->major_version == -1) {
82d9eba6 557 /* zeroing superblock */
05697ec1 558 *sbp = sb;
82d9eba6 559 return 0;
05697ec1 560 }
82d9eba6
NB
561
562 spares = info->working_disks - info->active_disks;
563 if (info->raid_disks + spares > 384) {
564 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
565 info->raid_disks , spares, 384);
566 return 0;
567 }
568
82d9eba6
NB
569 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
570 sb->major_version = __cpu_to_le32(1);
571 sb->feature_map = 0;
572 sb->pad0 = 0;
34163fc7
NB
573
574 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
575 read(rfd, sb->set_uuid, 16) != 16) {
576 *(__u32*)(sb->set_uuid) = random();
577 *(__u32*)(sb->set_uuid+4) = random();
578 *(__u32*)(sb->set_uuid+8) = random();
579 *(__u32*)(sb->set_uuid+12) = random();
580 }
581 if (rfd >= 0) close(rfd);
82d9eba6 582
05697ec1
NB
583 if (name == NULL || *name == 0) {
584 sprintf(defname, "%d", info->md_minor);
585 name = defname;
586 }
947fd4dd 587 memset(sb->set_name, 0, 32);
05697ec1
NB
588 if (homehost &&
589 strchr(name, ':')== NULL &&
590 strlen(homehost)+1+strlen(name) < 32) {
591 strcpy(sb->set_name, homehost);
592 strcat(sb->set_name, ":");
593 strcat(sb->set_name, name);
594 } else
595 strcpy(sb->set_name, name);
82d9eba6
NB
596
597 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
598 sb->level = __cpu_to_le32(info->level);
ea329559 599 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 600 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
601 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
602 sb->raid_disks = __cpu_to_le32(info->raid_disks);
603
604 sb->data_offset = __cpu_to_le64(0);
605 sb->data_size = __cpu_to_le64(0);
606 sb->super_offset = __cpu_to_le64(0);
607 sb->recovery_offset = __cpu_to_le64(0);
608
609 sb->utime = sb->ctime;
610 sb->events = __cpu_to_le64(1);
34321279 611 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
612 sb->resync_offset = ~0ULL;
613 else
614 sb->resync_offset = 0;
34163fc7 615 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
616 sizeof(sb->dev_roles[0]));
617 memset(sb->pad3, 0, sizeof(sb->pad3));
618
619 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
620
621 *sbp = sb;
622 return 1;
623}
624
625/* Add a device to the superblock being created */
626static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
627{
628 struct mdp_superblock_1 *sb = sbv;
629 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 630 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 631 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 632 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 633 *rp = 0xffff;
34163fc7 634 else
82d9eba6
NB
635 *rp = 0xfffe;
636}
637
14263cf1
NB
638static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
639
96395475 640static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
641{
642 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 643 unsigned long long sb_offset;
82d9eba6 644 int sbsize;
5dd497ee
NB
645 unsigned long size;
646 unsigned long long dsize;
96395475 647
5dd497ee
NB
648#ifdef BLKGETSIZE64
649 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
650#endif
651 {
652 if (ioctl(fd, BLKGETSIZE, &size))
653 return 1;
654 else
655 dsize = (unsigned long long)size;
656 } else
657 dsize >>= 9;
96395475 658
5dd497ee 659 if (dsize < 24)
96395475
NB
660 return 2;
661
662 /*
663 * Calculate the position of the superblock.
664 * It is always aligned to a 4K boundary and
665 * depending on minor_version, it can be:
666 * 0: At least 8K, but less than 12K, from end of device
667 * 1: At start of device
668 * 2: 4K from start of device.
669 */
670 switch(st->minor_version) {
671 case 0:
5dd497ee 672 sb_offset = dsize;
96395475
NB
673 sb_offset -= 8*2;
674 sb_offset &= ~(4*2-1);
675 break;
676 case 1:
6fbba4c9 677 sb_offset = 0;
96395475
NB
678 break;
679 case 2:
680 sb_offset = 4*2;
681 break;
682 default:
683 return -EINVAL;
684 }
685
82d9eba6 686
34163fc7 687
6fbba4c9
NB
688 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
689 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
690 ) {
691 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
692 abort();
693 }
82d9eba6 694
6fbba4c9 695 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
696 return 3;
697
698 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
699
700 if (write(fd, sb, sbsize) != sbsize)
701 return 4;
702
14263cf1
NB
703 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
704 struct bitmap_super_s *bm = (struct bitmap_super_s*)
705 ((char*)sb)+1024;
706 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
707 locate_bitmap1(st, fd, sbv);
708 write(fd, bm, sizeof(*bm));
709 }
710 }
570c0542 711 fsync(fd);
82d9eba6
NB
712 return 0;
713}
714
892debc8
NB
715static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
716
34163fc7 717static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 718 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
719{
720 struct mdp_superblock_1 *sb = sbv;
e478dc86 721 void *refsbv = NULL;
892debc8 722 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 723 int rfd;
82d9eba6
NB
724 int rv;
725
8fac0577
NB
726 unsigned long size, space;
727 unsigned long long dsize, array_size;
82d9eba6
NB
728 long long sb_offset;
729
730
731 if (fd < 0) {
732 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
733 devname);
734 return -1;
735 }
736
737 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 738 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
dfd4d8ee 739 sb->devflags |= WriteMostly1;
892debc8 740
dfe47e00
NB
741 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
742 read(rfd, sb->device_uuid, 16) != 16) {
743 *(__u32*)(sb->device_uuid) = random();
744 *(__u32*)(sb->device_uuid+4) = random();
745 *(__u32*)(sb->device_uuid+8) = random();
746 *(__u32*)(sb->device_uuid+12) = random();
747 }
748 if (rfd >= 0) close(rfd);
892debc8
NB
749 sb->events = 0;
750
e478dc86
NB
751 if (load_super1(st, fd, &refsbv, NULL)==0) {
752 struct mdp_superblock_1 *refsb = refsbv;
753
892debc8
NB
754 memcpy(sb->device_uuid, refsb->device_uuid, 16);
755 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
756 /* same array, so preserve events and dev_number */
757 sb->events = refsb->events;
758 sb->dev_number = refsb->dev_number;
759 }
760 free(refsb);
761 }
34163fc7 762
5dd497ee
NB
763#ifdef BLKGETSIZE64
764 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
765#endif
766 {
767 if (ioctl(fd, BLKGETSIZE, &size))
768 return 1;
769 else
770 dsize = size;
771 } else
772 dsize >>= 9;
82d9eba6 773
5dd497ee 774 if (dsize < 24) {
024ce7fe 775 close(fd);
82d9eba6 776 return 2;
024ce7fe 777 }
82d9eba6
NB
778
779
780 /*
781 * Calculate the position of the superblock.
782 * It is always aligned to a 4K boundary and
783 * depending on minor_version, it can be:
784 * 0: At least 8K, but less than 12K, from end of device
785 * 1: At start of device
786 * 2: 4K from start of device.
21e92547
NB
787 * Depending on the array size, we might leave extra space
788 * for a bitmap.
82d9eba6 789 */
8fac0577 790 array_size = __le64_to_cpu(sb->size);
82d9eba6
NB
791 switch(st->minor_version) {
792 case 0:
5dd497ee 793 sb_offset = dsize;
82d9eba6
NB
794 sb_offset -= 8*2;
795 sb_offset &= ~(4*2-1);
796 sb->super_offset = __cpu_to_le64(sb_offset);
797 sb->data_offset = __cpu_to_le64(0);
561832f9 798 if (sb_offset-64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
799 sb->data_size = __cpu_to_le64(sb_offset-64*2);
800 else
801 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
802 break;
803 case 1:
804 sb->super_offset = __cpu_to_le64(0);
561832f9 805 if (dsize - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
806 space = 64*2;
807 else
808 space = 4*2;
809 sb->data_offset = __cpu_to_le64(space); /* leave space for super and bitmap */
810 sb->data_size = __cpu_to_le64(dsize - space);
82d9eba6
NB
811 break;
812 case 2:
813 sb_offset = 4*2;
561832f9 814 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
815 space = 64*2;
816 else
817 space = 4*2;
82d9eba6 818 sb->super_offset = __cpu_to_le64(sb_offset);
21e92547
NB
819 sb->data_offset = __cpu_to_le64(sb_offset+space);
820 sb->data_size = __cpu_to_le64(dsize - 4*2 - space);
82d9eba6
NB
821 break;
822 default:
823 return -EINVAL;
824 }
825
826
827 sb->sb_csum = calc_sb_1_csum(sb);
96395475 828 rv = store_super1(st, fd, sb);
82d9eba6
NB
829 if (rv)
830 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 831
1bf4e2d9
NB
832 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
833 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 834 close(fd);
82d9eba6
NB
835 return rv;
836}
837
838static int compare_super1(void **firstp, void *secondv)
839{
840 /*
841 * return:
842 * 0 same, or first was empty, and second was copied
843 * 1 second had wrong number
844 * 2 wrong uuid
845 * 3 wrong other info
846 */
847 struct mdp_superblock_1 *first = *firstp;
848 struct mdp_superblock_1 *second = secondv;
849
850 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
851 return 1;
852 if (second->major_version != __cpu_to_le32(1))
853 return 1;
854
855 if (!first) {
14263cf1
NB
856 first = malloc(1024+sizeof(bitmap_super_t));
857 memcpy(first, second, 1024+sizeof(bitmap_super_t));
82d9eba6
NB
858 *firstp = first;
859 return 0;
860 }
861 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
862 return 2;
863
864 if (first->ctime != second->ctime ||
865 first->level != second->level ||
866 first->layout != second->layout ||
867 first->size != second->size ||
868 first->chunksize != second->chunksize ||
869 first->raid_disks != second->raid_disks)
870 return 3;
871 return 0;
872}
873
874static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
875{
876 unsigned long size;
5dd497ee 877 unsigned long long dsize;
82d9eba6
NB
878 unsigned long long sb_offset;
879 struct mdp_superblock_1 *super;
14263cf1
NB
880 int uuid[4];
881 struct bitmap_super_s *bsb;
82d9eba6
NB
882
883
884 if (st->ss == NULL) {
570c0542
NB
885 int bestvers = -1;
886 __u64 bestctime = 0;
887 /* guess... choose latest ctime */
82d9eba6
NB
888 st->ss = &super1;
889 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
890 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
891 case 0: super = *sbp;
892 if (bestvers == -1 ||
893 bestctime < __le64_to_cpu(super->ctime)) {
894 bestvers = st->minor_version;
895 bestctime = __le64_to_cpu(super->ctime);
896 }
897 free(super);
898 *sbp = NULL;
899 break;
82d9eba6
NB
900 case 1: st->ss = NULL; return 1; /*bad device */
901 case 2: break; /* bad, try next */
902 }
903 }
570c0542
NB
904 if (bestvers != -1) {
905 int rv;
906 st->minor_version = bestvers;
907 st->ss = &super1;
ea329559 908 st->max_devs = 384;
570c0542
NB
909 rv = load_super1(st, fd, sbp, devname);
910 if (rv) st->ss = NULL;
911 return rv;
912 }
82d9eba6
NB
913 st->ss = NULL;
914 return 2;
915 }
5dd497ee
NB
916#ifdef BLKGETSIZE64
917 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
918#endif
919 {
920 if (ioctl(fd, BLKGETSIZE, &size)) {
921 if (devname)
922 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
923 devname, strerror(errno));
924 return 1;
925 }
926 dsize = size;
927 } else
928 dsize >>= 9;
82d9eba6 929
5dd497ee 930 if (dsize < 24) {
82d9eba6 931 if (devname)
5dd497ee
NB
932 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
933 devname, dsize);
82d9eba6
NB
934 return 1;
935 }
936
937 /*
938 * Calculate the position of the superblock.
939 * It is always aligned to a 4K boundary and
14263cf1 940 * depending on minor_version, it can be:
82d9eba6
NB
941 * 0: At least 8K, but less than 12K, from end of device
942 * 1: At start of device
943 * 2: 4K from start of device.
944 */
945 switch(st->minor_version) {
946 case 0:
5dd497ee 947 sb_offset = dsize;
82d9eba6
NB
948 sb_offset -= 8*2;
949 sb_offset &= ~(4*2-1);
950 break;
951 case 1:
952 sb_offset = 0;
953 break;
954 case 2:
955 sb_offset = 4*2;
956 break;
957 default:
958 return -EINVAL;
959 }
960
961 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
962
963
964 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
965 if (devname)
966 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
967 devname, strerror(errno));
968 return 1;
969 }
970
34163fc7 971 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
972
973 if (read(fd, super, 1024) != 1024) {
974 if (devname)
975 fprintf(stderr, Name ": Cannot read superblock on %s\n",
976 devname);
977 free(super);
978 return 1;
979 }
980
981 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
982 if (devname)
983 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
984 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
985 free(super);
986 return 2;
987 }
988
989 if (__le32_to_cpu(super->major_version) != 1) {
990 if (devname)
991 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
992 devname, __le32_to_cpu(super->major_version));
993 free(super);
994 return 2;
995 }
996 if (__le64_to_cpu(super->super_offset) != sb_offset) {
997 if (devname)
998 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
999 devname);
1000 free(super);
1001 return 2;
1002 }
1003 *sbp = super;
14263cf1
NB
1004
1005 /* Now check on the bitmap superblock */
1006 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1007 return 0;
1008 /* Read the bitmap superblock and make sure it looks
1009 * valid. If it doesn't clear the bit. An --assemble --force
1010 * should get that written out.
1011 */
1012 locate_bitmap1(st, fd, super);
1013 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1014 != sizeof(struct bitmap_super_s))
1015 goto no_bitmap;
1016
1017 uuid_from_super1(uuid, super);
1018 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1019 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1020 memcmp(bsb->uuid, uuid, 16) != 0)
1021 goto no_bitmap;
1022 return 0;
1023
1024 no_bitmap:
1025 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1026 return 0;
1027}
1028
1029
1030static struct supertype *match_metadata_desc1(char *arg)
1031{
1032 struct supertype *st = malloc(sizeof(*st));
1033 if (!st) return st;
1034
1035 st->ss = &super1;
ea329559 1036 st->max_devs = 384;
82d9eba6 1037 if (strcmp(arg, "1") == 0 ||
576d6d83
NB
1038 strcmp(arg, "1.0") == 0 ||
1039 strcmp(arg, "default/large") == 0) {
82d9eba6
NB
1040 st->minor_version = 0;
1041 return st;
1042 }
1043 if (strcmp(arg, "1.1") == 0) {
1044 st->minor_version = 1;
1045 return st;
1046 }
1047 if (strcmp(arg, "1.2") == 0) {
1048 st->minor_version = 2;
1049 return st;
1050 }
1051
1052 free(st);
1053 return NULL;
1054}
1055
34163fc7
NB
1056/* find available size on device with this devsize, using
1057 * superblock type st, and reserving 'reserve' sectors for
1058 * a possible bitmap
1059 */
1bf4e2d9 1060static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
1061{
1062 if (devsize < 24)
1063 return 0;
1064
21e92547
NB
1065 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1066 * if biffer than 200Gig, save 128k
1067 */
1068 if (devsize > 200*1024*1024*2)
1069 devsize -= 128*2;
1070 else if (devsize > 8*1024*1024*2)
1071 devsize -= 64*2;
1072
34163fc7
NB
1073 switch(st->minor_version) {
1074 case 0:
1bf4e2d9
NB
1075 /* at end */
1076 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1077 case 1:
1bf4e2d9
NB
1078 /* at start, 4K for superblock and possible bitmap */
1079 return devsize - 4*2;
34163fc7 1080 case 2:
1bf4e2d9
NB
1081 /* 4k from start, 4K for superblock and possible bitmap */
1082 return devsize - (4+4)*2;
34163fc7
NB
1083 }
1084 return 0;
1085}
1086
1bf4e2d9
NB
1087static int
1088add_internal_bitmap1(struct supertype *st, void *sbv,
21e92547
NB
1089 int chunk, int delay, int write_behind, unsigned long long size,
1090 int may_change, int major)
34163fc7
NB
1091{
1092 /*
1bf4e2d9
NB
1093 * If not may_change, then this is a 'Grow', and the bitmap
1094 * must fit after the superblock.
1095 * If may_change, then this is create, and we can put the bitmap
1096 * before the superblock if we like, or may move the start.
1097 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7 1098 *
f9c25f1d 1099 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1100 */
1101
1bf4e2d9
NB
1102 unsigned long long bits;
1103 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1104 unsigned long long min_chunk;
1105 struct mdp_superblock_1 *sb = sbv;
1106 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1107
1bf4e2d9
NB
1108 if (st->minor_version && !may_change &&
1109 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
1110 return 0; /* doesn't fit */
1111
1112
34163fc7
NB
1113
1114 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1115 bits = (size*512)/min_chunk +1;
34163fc7
NB
1116 while (bits > max_bits) {
1117 min_chunk *= 2;
1118 bits = (bits+1)/2;
1119 }
1120 if (chunk == UnSet)
1121 chunk = min_chunk;
1122 else if (chunk < min_chunk)
1123 return 0; /* chunk size too small */
21e92547
NB
1124 if (chunk == 0) /* rounding problem */
1125 return 0;
34163fc7 1126
1bf4e2d9 1127 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
1128
1129 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1130 memset(bms, 0, sizeof(*bms));
34163fc7 1131 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1132 bms->version = __cpu_to_le32(major);
34163fc7
NB
1133 uuid_from_super1((int*)bms->uuid, sb);
1134 bms->chunksize = __cpu_to_le32(chunk);
1135 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1136 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1137 bms->write_behind = __cpu_to_le32(write_behind);
1138
34163fc7
NB
1139 return 1;
1140}
1141
1142
14263cf1 1143static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 1144{
34163fc7 1145 unsigned long long offset;
1e0d770c
NB
1146 struct mdp_superblock_1 *sb;
1147 int mustfree = 0;
34163fc7 1148
1e0d770c
NB
1149 if (!sbv) {
1150 if (st->ss->load_super(st, fd, &sbv, NULL))
f6d75de8 1151 return; /* no error I hope... */
1e0d770c
NB
1152 mustfree = 1;
1153 }
1154 sb = sbv;
e478dc86 1155
1bf4e2d9
NB
1156 offset = __le64_to_cpu(sb->super_offset);
1157 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1158 if (mustfree)
f6d75de8 1159 free(sb);
1bf4e2d9 1160 lseek64(fd, offset<<9, 0);
34163fc7
NB
1161}
1162
14263cf1 1163static int write_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7
NB
1164{
1165 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 1166 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1167 int rv = 0;
1168
1169 int towrite, n;
1170 char buf[4096];
1171
f6d75de8 1172 locate_bitmap1(st, fd, sbv);
34163fc7 1173
1e0d770c
NB
1174 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1175 sizeof(bitmap_super_t))
1176 return -2;
1bf4e2d9
NB
1177 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1178 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1179 memset(buf, 0xff, sizeof(buf));
1180 while (towrite > 0) {
1181 n = towrite;
1182 if (n > sizeof(buf))
1183 n = sizeof(buf);
1184 n = write(fd, buf, n);
1185 if (n > 0)
1186 towrite -= n;
1187 else
1188 break;
1189 }
1190 fsync(fd);
1191 if (towrite)
1192 rv = -2;
1193
1194 return rv;
82d9eba6
NB
1195}
1196
1197struct superswitch super1 = {
c7654afc 1198#ifndef MDASSEMBLE
82d9eba6
NB
1199 .examine_super = examine_super1,
1200 .brief_examine_super = brief_examine_super1,
1201 .detail_super = detail_super1,
1202 .brief_detail_super = brief_detail_super1,
c7654afc 1203#endif
83b6208e 1204 .match_home = match_home1,
82d9eba6
NB
1205 .uuid_from_super = uuid_from_super1,
1206 .getinfo_super = getinfo_super1,
1207 .update_super = update_super1,
1208 .event_super = event_super1,
1209 .init_super = init_super1,
1210 .add_to_super = add_to_super1,
1211 .store_super = store_super1,
1212 .write_init_super = write_init_super1,
1213 .compare_super = compare_super1,
1214 .load_super = load_super1,
1215 .match_metadata_desc = match_metadata_desc1,
1216 .avail_size = avail_size1,
34163fc7
NB
1217 .add_internal_bitmap = add_internal_bitmap1,
1218 .locate_bitmap = locate_bitmap1,
1219 .write_bitmap = write_bitmap1,
82d9eba6 1220 .major = 1,
f277ce36
NB
1221#if __BYTE_ORDER == BIG_ENDIAN
1222 .swapuuid = 0,
1223#else
1224 .swapuuid = 1,
1225#endif
82d9eba6 1226};