]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Fix and test --update=uuid
[thirdparty/mdadm.git] / super1.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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"
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 */
38 struct 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;
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 */
59
60 /* These are only valid with feature bit '4' */
61 __u32 new_level; /* new level we are reshaping to */
62 __u64 reshape_position; /* next address in array-space for reshape */
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 */
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 */
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 */
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
97 struct misc_dev_info {
98 __u64 device_size;
99 };
100
101 /* feature_map bits */
102 #define MD_FEATURE_BITMAP_OFFSET 1
103 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
104 * must be honoured
105 */
106 #define MD_FEATURE_RESHAPE_ACTIVE 4
107
108 #define MD_FEATURE_ALL (1|2|4)
109
110 #ifndef offsetof
111 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
112 #endif
113 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
114 {
115 unsigned int disk_csum, csum;
116 unsigned long long newcsum;
117 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
118 unsigned int *isuper = (unsigned int*)sb;
119 int i;
120
121 /* make sure I can count... */
122 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
123 offsetof(struct mdp_superblock_1, utime) != 192 ||
124 sizeof(struct mdp_superblock_1) != 256) {
125 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
126 }
127
128 disk_csum = sb->sb_csum;
129 sb->sb_csum = 0;
130 newcsum = 0;
131 for (i=0; size>=4; size -= 4 ) {
132 newcsum += __le32_to_cpu(*isuper);
133 isuper++;
134 }
135
136 if (size == 2)
137 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
138
139 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
140 sb->sb_csum = disk_csum;
141 return __cpu_to_le32(csum);
142 }
143
144 #ifndef MDASSEMBLE
145 static void examine_super1(void *sbv, char *homehost)
146 {
147 struct mdp_superblock_1 *sb = sbv;
148 time_t atime;
149 int d;
150 int faulty;
151 int i;
152 char *c;
153 int l = homehost ? strlen(homehost) : 0;
154 int layout;
155
156 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
157 printf(" Version : %02d\n", 1);
158 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
159 printf(" Array UUID : ");
160 for (i=0; i<16; i++) {
161 if ((i&3)==0 && i != 0) printf(":");
162 printf("%02x", sb->set_uuid[i]);
163 }
164 printf("\n");
165 printf(" Name : %.32s", sb->set_name);
166 if (l > 0 && l < 32 &&
167 sb->set_name[l] == ':' &&
168 strncmp(sb->set_name, homehost, l) == 0)
169 printf(" (local to host %s)", homehost);
170 printf("\n");
171 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
172 printf(" Creation Time : %.24s\n", ctime(&atime));
173 c=map_num(pers, __le32_to_cpu(sb->level));
174 printf(" Raid Level : %s\n", c?c:"-unknown-");
175 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
176 printf("\n");
177 printf(" Used Dev Size : %llu%s\n",
178 (unsigned long long)sb->data_size,
179 human_size(sb->data_size<<9));
180 if (__le32_to_cpu(sb->level) >= 0) {
181 int ddsks=0;
182 switch(__le32_to_cpu(sb->level)) {
183 case 1: ddsks=1;break;
184 case 4:
185 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
186 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
187 case 10:
188 layout = __le32_to_cpu(sb->layout);
189 ddsks = __le32_to_cpu(sb->raid_disks)
190 / (layout&255) / ((layout>>8)&255);
191 }
192 if (ddsks)
193 printf(" Array Size : %llu%s\n",
194 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
195 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
196 if (sb->size != sb->data_size)
197 printf(" Used Size : %llu%s\n",
198 (unsigned long long)__le64_to_cpu(sb->size),
199 human_size(__le64_to_cpu(sb->size)<<9));
200 }
201 if (sb->data_offset)
202 printf(" Data Offset : %llu sectors\n",
203 (unsigned long long)__le64_to_cpu(sb->data_offset));
204 printf(" Super Offset : %llu sectors\n",
205 (unsigned long long)__le64_to_cpu(sb->super_offset));
206 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
207 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
208 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
209 printf(" Device UUID : ");
210 for (i=0; i<16; i++) {
211 if ((i&3)==0 && i != 0) printf(":");
212 printf("%02x", sb->device_uuid[i]);
213 }
214 printf("\n");
215 printf("\n");
216 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
217 printf("Internal Bitmap : %ld sectors from superblock\n",
218 (long)__le32_to_cpu(sb->bitmap_offset));
219 }
220 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
221 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
222 human_size(__le64_to_cpu(sb->reshape_position)<<9));
223 if (__le32_to_cpu(sb->delta_disks)) {
224 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
225 if (__le32_to_cpu(sb->delta_disks))
226 printf(" (%d->%d)\n",
227 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
228 __le32_to_cpu(sb->raid_disks));
229 else
230 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
231 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
232 }
233 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
234 c = map_num(pers, __le32_to_cpu(sb->new_level));
235 printf(" New Level : %s\n", c?c:"-unknown-");
236 }
237 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
238 if (__le32_to_cpu(sb->level) == 5) {
239 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
240 printf(" New Layout : %s\n", c?c:"-unknown-");
241 }
242 if (__le32_to_cpu(sb->level) == 10) {
243 printf(" New Layout : near=%d, %s=%d\n",
244 __le32_to_cpu(sb->new_layout)&255,
245 (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
246 (__le32_to_cpu(sb->new_layout)>>8)&255);
247 }
248 }
249 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
250 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
251 printf("\n");
252 }
253 if (sb->devflags) {
254 printf(" Flags :");
255 if (sb->devflags & WriteMostly1)
256 printf(" write-mostly");
257 printf("\n");
258 }
259
260 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
261 printf(" Update Time : %.24s\n", ctime(&atime));
262
263 if (calc_sb_1_csum(sb) == sb->sb_csum)
264 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
265 else
266 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
267 __le32_to_cpu(calc_sb_1_csum(sb)));
268 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
269 printf("\n");
270 if (__le32_to_cpu(sb->level) == 5) {
271 c = map_num(r5layout, __le32_to_cpu(sb->layout));
272 printf(" Layout : %s\n", c?c:"-unknown-");
273 }
274 if (__le32_to_cpu(sb->level) == 10) {
275 int lo = __le32_to_cpu(sb->layout);
276 printf(" Layout : near=%d, %s=%d\n",
277 lo&255,
278 (lo&0x10000)?"offset":"far",
279 (lo>>8)&255);
280 }
281 switch(__le32_to_cpu(sb->level)) {
282 case 0:
283 case 4:
284 case 5:
285 case 6:
286 case 10:
287 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
288 break;
289 case -1:
290 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
291 break;
292 default: break;
293 }
294 printf("\n");
295 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
296 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
297 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
298 break;
299 for (d=0; d < i; d++) {
300 int role = __le16_to_cpu(sb->dev_roles[d]);
301 if (d) printf(", ");
302 if (role == 0xffff) printf("empty");
303 else if(role == 0xfffe) printf("failed");
304 else printf("%d", role);
305 }
306 printf(")\n");
307 printf(" Array State : ");
308 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
309 int cnt = 0;
310 int me = 0;
311 int i;
312 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
313 int role = __le16_to_cpu(sb->dev_roles[i]);
314 if (role == d) {
315 if (i == __le32_to_cpu(sb->dev_number))
316 me = 1;
317 cnt++;
318 }
319 }
320 if (cnt > 1) printf("?");
321 else if (cnt == 1 && me) printf("U");
322 else if (cnt == 1) printf("u");
323 else printf ("_");
324 }
325 faulty = 0;
326 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
327 int role = __le16_to_cpu(sb->dev_roles[i]);
328 if (role == 0xFFFE)
329 faulty++;
330 }
331 if (faulty) printf(" %d failed", faulty);
332 printf("\n");
333 }
334
335
336 static void brief_examine_super1(void *sbv)
337 {
338 struct mdp_superblock_1 *sb = sbv;
339 int i;
340 char *nm;
341 char *c=map_num(pers, __le32_to_cpu(sb->level));
342
343 nm = strchr(sb->set_name, ':');
344 if (nm)
345 nm++;
346 else if (sb->set_name[0])
347 nm = sb->set_name;
348 else
349 nm = "??";
350
351 printf("ARRAY /dev/md/%s level=%s metadata=1 num-devices=%d UUID=",
352 nm,
353 c?c:"-unknown-", __le32_to_cpu(sb->raid_disks));
354 for (i=0; i<16; i++) {
355 if ((i&3)==0 && i != 0) printf(":");
356 printf("%02x", sb->set_uuid[i]);
357 }
358 if (sb->set_name[0])
359 printf(" name=%.32s", sb->set_name);
360 printf("\n");
361 }
362
363 static void detail_super1(void *sbv, char *homehost)
364 {
365 struct mdp_superblock_1 *sb = sbv;
366 int i;
367 int l = homehost ? strlen(homehost) : 0;
368
369 printf(" Name : %.32s", sb->set_name);
370 if (l > 0 && l < 32 &&
371 sb->set_name[l] == ':' &&
372 strncmp(sb->set_name, homehost, l) == 0)
373 printf(" (local to host %s)", homehost);
374 printf("\n UUID : ");
375 for (i=0; i<16; i++) {
376 if ((i&3)==0 && i != 0) printf(":");
377 printf("%02x", sb->set_uuid[i]);
378 }
379 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
380 }
381
382 static void brief_detail_super1(void *sbv)
383 {
384 struct mdp_superblock_1 *sb = sbv;
385 int i;
386
387 if (sb->set_name[0])
388 printf(" name=%.32s", sb->set_name);
389 printf(" UUID=");
390 for (i=0; i<16; i++) {
391 if ((i&3)==0 && i != 0) printf(":");
392 printf("%02x", sb->set_uuid[i]);
393 }
394 }
395
396 #endif
397
398 static int match_home1(void *sbv, char *homehost)
399 {
400 struct mdp_superblock_1 *sb = sbv;
401 int l = homehost ? strlen(homehost) : 0;
402
403 return (l > 0 && l < 32 &&
404 sb->set_name[l] == ':' &&
405 strncmp(sb->set_name, homehost, l) == 0);
406 }
407
408 static void uuid_from_super1(int uuid[4], void * sbv)
409 {
410 struct mdp_superblock_1 *super = sbv;
411 char *cuuid = (char*)uuid;
412 int i;
413 for (i=0; i<16; i++)
414 cuuid[i] = super->set_uuid[i];
415 }
416
417 static void getinfo_super1(struct mdinfo *info, void *sbv)
418 {
419 struct mdp_superblock_1 *sb = sbv;
420 int working = 0;
421 int i;
422 int role;
423
424 info->array.major_version = 1;
425 info->array.minor_version = __le32_to_cpu(sb->feature_map);
426 info->array.patch_version = 0;
427 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
428 info->array.level = __le32_to_cpu(sb->level);
429 info->array.layout = __le32_to_cpu(sb->layout);
430 info->array.md_minor = -1;
431 info->array.ctime = __le64_to_cpu(sb->ctime);
432 info->array.utime = __le64_to_cpu(sb->utime);
433 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
434 info->array.state = (__le64_to_cpu(sb->resync_offset)+1) ? 0 : 1;
435
436 info->data_offset = __le64_to_cpu(sb->data_offset);
437 info->component_size = __le64_to_cpu(sb->size);
438
439 info->disk.major = 0;
440 info->disk.minor = 0;
441 info->disk.number = __le32_to_cpu(sb->dev_number);
442 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
443 __le32_to_cpu(sb->max_dev) > 512)
444 role = 0xfffe;
445 else
446 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
447
448 info->disk.raid_disk = -1;
449 switch(role) {
450 case 0xFFFF:
451 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
452 break;
453 case 0xFFFE:
454 info->disk.state = 1; /* faulty */
455 break;
456 default:
457 info->disk.state = 6; /* active and in sync */
458 info->disk.raid_disk = role;
459 }
460 info->events = __le64_to_cpu(sb->events);
461
462 memcpy(info->uuid, sb->set_uuid, 16);
463
464 strncpy(info->name, sb->set_name, 32);
465 info->name[32] = 0;
466
467 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
468 info->reshape_active = 1;
469 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
470 info->new_level = __le32_to_cpu(sb->new_level);
471 info->delta_disks = __le32_to_cpu(sb->delta_disks);
472 info->new_layout = __le32_to_cpu(sb->new_layout);
473 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
474 } else
475 info->reshape_active = 0;
476
477 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
478 role = __le16_to_cpu(sb->dev_roles[i]);
479 if (/*role == 0xFFFF || */role < info->array.raid_disks)
480 working++;
481 }
482
483 info->array.working_disks = working;
484 }
485
486 static int update_super1(struct mdinfo *info, void *sbv, char *update,
487 char *devname, int verbose,
488 int uuid_set, char *homehost)
489 {
490 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
491 * For others, the return value is ignored.
492 */
493 int rv = 0;
494 struct mdp_superblock_1 *sb = sbv;
495
496 if (strcmp(update, "force-one")==0) {
497 /* Not enough devices for a working array,
498 * so bring this one up-to-date
499 */
500 if (sb->events != __cpu_to_le64(info->events))
501 rv = 1;
502 sb->events = __cpu_to_le64(info->events);
503 }
504 if (strcmp(update, "force-array")==0) {
505 /* Degraded array and 'force' requests to
506 * maybe need to mark it 'clean'.
507 */
508 switch(__le32_to_cpu(sb->level)) {
509 case 5: case 4: case 6:
510 /* need to force clean */
511 if (sb->resync_offset != ~0ULL)
512 rv = 1;
513 sb->resync_offset = ~0ULL;
514 }
515 }
516 if (strcmp(update, "assemble")==0) {
517 int d = info->disk.number;
518 int want;
519 if (info->disk.state == 6)
520 want = __cpu_to_le32(info->disk.raid_disk);
521 else
522 want = 0xFFFF;
523 if (sb->dev_roles[d] != want) {
524 sb->dev_roles[d] = want;
525 rv = 1;
526 }
527 }
528 if (strcmp(update, "grow") == 0) {
529 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
530 /* As we are just adding a spare, there is no need to
531 * make any change to the dev_roles array
532 */
533 }
534 if (strcmp(update, "resync") == 0) {
535 /* make sure resync happens */
536 sb->resync_offset = 0ULL;
537 }
538 if (strcmp(update, "uuid") == 0) {
539 if (super1.swapuuid) {
540 unsigned char *ac = (unsigned char *)sb->set_uuid;
541 unsigned char *bc = (unsigned char *)info->uuid;
542 int i;
543 for (i=0; i<16; i+= 4) {
544 ac[i+0] = bc[i+3];
545 ac[i+1] = bc[i+2];
546 ac[i+2] = bc[i+1];
547 ac[i+3] = bc[i+0];
548 }
549 } else
550 memcpy(sb->set_uuid, info->uuid, 16);
551
552 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
553 struct bitmap_super_s *bm;
554 bm = (struct bitmap_super_s*)(sbv+1024);
555 memcpy(bm->uuid, sb->set_uuid, 16);
556 }
557 }
558 if (strcmp(update, "homehost") == 0 &&
559 homehost) {
560 char *c;
561 update = "name";
562 c = strchr(sb->set_name, ':');
563 if (c)
564 strncpy(info->name, c+1, 31 - (c-sb->set_name));
565 else
566 strncpy(info->name, sb->set_name, 32);
567 info->name[32] = 0;
568 }
569 if (strcmp(update, "name") == 0) {
570 if (info->name[0] == 0)
571 sprintf(info->name, "%d", info->array.md_minor);
572 memset(sb->set_name, 0, sizeof(sb->set_name));
573 if (homehost &&
574 strchr(info->name, ':') == NULL &&
575 strlen(homehost)+1+strlen(info->name) < 32) {
576 strcpy(sb->set_name, homehost);
577 strcat(sb->set_name, ":");
578 strcat(sb->set_name, info->name);
579 } else
580 strcpy(sb->set_name, info->name);
581 }
582 if (strcmp(update, "devicesize") == 0 &&
583 __le64_to_cpu(sb->super_offset) <
584 __le64_to_cpu(sb->data_offset)) {
585 /* set data_size to device size less data_offset */
586 struct misc_dev_info *misc = (struct misc_dev_info*)
587 (sbv + 1024 + sizeof(struct bitmap_super_s));
588 printf("Size was %llu\n", __le64_to_cpu(sb->data_size));
589 sb->data_size = __cpu_to_le64(
590 misc->device_size - __le64_to_cpu(sb->data_offset));
591 printf("Size is %llu\n", __le64_to_cpu(sb->data_size));
592 }
593 if (strcmp(update, "_reshape_progress")==0)
594 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
595
596 sb->sb_csum = calc_sb_1_csum(sb);
597 return rv;
598 }
599
600 static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
601 unsigned long long size, char *name, char *homehost)
602 {
603 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t) +
604 sizeof(struct misc_dev_info));
605 int spares;
606 int rfd;
607 char defname[10];
608 memset(sb, 0, 1024);
609
610 if (info->major_version == -1) {
611 /* zeroing superblock */
612 *sbp = sb;
613 return 0;
614 }
615
616 spares = info->working_disks - info->active_disks;
617 if (info->raid_disks + spares > 384) {
618 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
619 info->raid_disks , spares, 384);
620 return 0;
621 }
622
623 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
624 sb->major_version = __cpu_to_le32(1);
625 sb->feature_map = 0;
626 sb->pad0 = 0;
627
628 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
629 read(rfd, sb->set_uuid, 16) != 16) {
630 *(__u32*)(sb->set_uuid) = random();
631 *(__u32*)(sb->set_uuid+4) = random();
632 *(__u32*)(sb->set_uuid+8) = random();
633 *(__u32*)(sb->set_uuid+12) = random();
634 }
635 if (rfd >= 0) close(rfd);
636
637 if (name == NULL || *name == 0) {
638 sprintf(defname, "%d", info->md_minor);
639 name = defname;
640 }
641 memset(sb->set_name, 0, 32);
642 if (homehost &&
643 strchr(name, ':')== NULL &&
644 strlen(homehost)+1+strlen(name) < 32) {
645 strcpy(sb->set_name, homehost);
646 strcat(sb->set_name, ":");
647 strcat(sb->set_name, name);
648 } else
649 strcpy(sb->set_name, name);
650
651 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
652 sb->level = __cpu_to_le32(info->level);
653 sb->layout = __cpu_to_le32(info->layout);
654 sb->size = __cpu_to_le64(size*2ULL);
655 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
656 sb->raid_disks = __cpu_to_le32(info->raid_disks);
657
658 sb->data_offset = __cpu_to_le64(0);
659 sb->data_size = __cpu_to_le64(0);
660 sb->super_offset = __cpu_to_le64(0);
661 sb->recovery_offset = __cpu_to_le64(0);
662
663 sb->utime = sb->ctime;
664 sb->events = __cpu_to_le64(1);
665 if (info->state & (1<<MD_SB_CLEAN))
666 sb->resync_offset = ~0ULL;
667 else
668 sb->resync_offset = 0;
669 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
670 sizeof(sb->dev_roles[0]));
671 memset(sb->pad3, 0, sizeof(sb->pad3));
672
673 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
674
675 *sbp = sb;
676 return 1;
677 }
678
679 /* Add a device to the superblock being created */
680 static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
681 {
682 struct mdp_superblock_1 *sb = sbv;
683 __u16 *rp = sb->dev_roles + dk->number;
684 if ((dk->state & 6) == 6) /* active, sync */
685 *rp = __cpu_to_le16(dk->raid_disk);
686 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
687 *rp = 0xffff;
688 else
689 *rp = 0xfffe;
690 }
691
692 static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
693
694 static int store_super1(struct supertype *st, int fd, void *sbv)
695 {
696 struct mdp_superblock_1 *sb = sbv;
697 unsigned long long sb_offset;
698 int sbsize;
699 unsigned long size;
700 unsigned long long dsize;
701
702 #ifdef BLKGETSIZE64
703 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
704 #endif
705 {
706 if (ioctl(fd, BLKGETSIZE, &size))
707 return 1;
708 else
709 dsize = (unsigned long long)size;
710 } else
711 dsize >>= 9;
712
713 if (dsize < 24)
714 return 2;
715
716 /*
717 * Calculate the position of the superblock.
718 * It is always aligned to a 4K boundary and
719 * depending on minor_version, it can be:
720 * 0: At least 8K, but less than 12K, from end of device
721 * 1: At start of device
722 * 2: 4K from start of device.
723 */
724 switch(st->minor_version) {
725 case 0:
726 sb_offset = dsize;
727 sb_offset -= 8*2;
728 sb_offset &= ~(4*2-1);
729 break;
730 case 1:
731 sb_offset = 0;
732 break;
733 case 2:
734 sb_offset = 4*2;
735 break;
736 default:
737 return -EINVAL;
738 }
739
740
741
742 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
743 0 != __le64_to_cpu(sb->super_offset)
744 ) {
745 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
746 abort();
747 }
748
749 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
750 return 3;
751
752 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
753
754 if (write(fd, sb, sbsize) != sbsize)
755 return 4;
756
757 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
758 struct bitmap_super_s *bm = (struct bitmap_super_s*)
759 (((char*)sb)+1024);
760 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
761 locate_bitmap1(st, fd, sbv);
762 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
763 return 5;
764 }
765 }
766 fsync(fd);
767 return 0;
768 }
769
770 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
771
772 static int write_init_super1(struct supertype *st, void *sbv,
773 mdu_disk_info_t *dinfo, char *devname)
774 {
775 struct mdp_superblock_1 *sb = sbv;
776 void *refsbv = NULL;
777 int fd = open(devname, O_RDWR | O_EXCL);
778 int rfd;
779 int rv;
780 int bm_space;
781
782 unsigned long space;
783 unsigned long long dsize, array_size;
784 long long sb_offset;
785
786
787 if (fd < 0) {
788 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
789 devname);
790 return -1;
791 }
792
793 sb->dev_number = __cpu_to_le32(dinfo->number);
794 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
795 sb->devflags |= __cpu_to_le32(WriteMostly1);
796
797 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
798 read(rfd, sb->device_uuid, 16) != 16) {
799 *(__u32*)(sb->device_uuid) = random();
800 *(__u32*)(sb->device_uuid+4) = random();
801 *(__u32*)(sb->device_uuid+8) = random();
802 *(__u32*)(sb->device_uuid+12) = random();
803 }
804 if (rfd >= 0) close(rfd);
805 sb->events = 0;
806
807 if (load_super1(st, fd, &refsbv, NULL)==0) {
808 struct mdp_superblock_1 *refsb = refsbv;
809
810 memcpy(sb->device_uuid, refsb->device_uuid, 16);
811 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
812 /* same array, so preserve events and dev_number */
813 sb->events = refsb->events;
814 /* bugs in 2.6.17 and earlier mean the dev_number
815 * chosen in Manage must be preserved
816 */
817 if (get_linux_version() >= 2006018)
818 sb->dev_number = refsb->dev_number;
819 }
820 free(refsb);
821 }
822
823 #ifdef BLKGETSIZE64
824 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
825 #endif
826 {
827 unsigned long size;
828 if (ioctl(fd, BLKGETSIZE, &size))
829 return 1;
830 else
831 dsize = size;
832 } else
833 dsize >>= 9;
834
835 if (dsize < 24) {
836 close(fd);
837 return 2;
838 }
839
840
841 /*
842 * Calculate the position of the superblock.
843 * It is always aligned to a 4K boundary and
844 * depending on minor_version, it can be:
845 * 0: At least 8K, but less than 12K, from end of device
846 * 1: At start of device
847 * 2: 4K from start of device.
848 * Depending on the array size, we might leave extra space
849 * for a bitmap.
850 */
851 array_size = __le64_to_cpu(sb->size);
852 /* work out how much space we left of a bitmap */
853 if (array_size >= 200*1024*1024*2)
854 bm_space = 128*2;
855 else if (array_size > 8*1024*1024*2)
856 bm_space = 64*2;
857 else
858 bm_space = 0;
859
860 switch(st->minor_version) {
861 case 0:
862 sb_offset = dsize;
863 sb_offset -= 8*2;
864 sb_offset &= ~(4*2-1);
865 sb->super_offset = __cpu_to_le64(sb_offset);
866 sb->data_offset = __cpu_to_le64(0);
867 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
868 break;
869 case 1:
870 sb->super_offset = __cpu_to_le64(0);
871 sb->data_offset = __cpu_to_le64(bm_space + 4*2);
872 sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
873 break;
874 case 2:
875 sb_offset = 4*2;
876 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
877 space = 64*2;
878 else
879 space = 4*2;
880 sb->super_offset = __cpu_to_le64(4*2);
881 sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
882 sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space );
883 break;
884 default:
885 return -EINVAL;
886 }
887
888
889 sb->sb_csum = calc_sb_1_csum(sb);
890 rv = store_super1(st, fd, sb);
891 if (rv)
892 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
893
894 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
895 rv = st->ss->write_bitmap(st, fd, sbv);
896 close(fd);
897 return rv;
898 }
899
900 static int compare_super1(void **firstp, void *secondv)
901 {
902 /*
903 * return:
904 * 0 same, or first was empty, and second was copied
905 * 1 second had wrong number
906 * 2 wrong uuid
907 * 3 wrong other info
908 */
909 struct mdp_superblock_1 *first = *firstp;
910 struct mdp_superblock_1 *second = secondv;
911
912 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
913 return 1;
914 if (second->major_version != __cpu_to_le32(1))
915 return 1;
916
917 if (!first) {
918 first = malloc(1024+sizeof(bitmap_super_t) +
919 sizeof(struct misc_dev_info));
920 memcpy(first, second, 1024+sizeof(bitmap_super_t) +
921 sizeof(struct misc_dev_info));
922 *firstp = first;
923 return 0;
924 }
925 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
926 return 2;
927
928 if (first->ctime != second->ctime ||
929 first->level != second->level ||
930 first->layout != second->layout ||
931 first->size != second->size ||
932 first->chunksize != second->chunksize ||
933 first->raid_disks != second->raid_disks)
934 return 3;
935 return 0;
936 }
937
938 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
939 {
940 unsigned long size;
941 unsigned long long dsize;
942 unsigned long long sb_offset;
943 struct mdp_superblock_1 *super;
944 int uuid[4];
945 struct bitmap_super_s *bsb;
946 struct misc_dev_info *misc;
947
948
949 if (st->ss == NULL) {
950 int bestvers = -1;
951 __u64 bestctime = 0;
952 /* guess... choose latest ctime */
953 st->ss = &super1;
954 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
955 switch(load_super1(st, fd, sbp, devname)) {
956 case 0: super = *sbp;
957 if (bestvers == -1 ||
958 bestctime < __le64_to_cpu(super->ctime)) {
959 bestvers = st->minor_version;
960 bestctime = __le64_to_cpu(super->ctime);
961 }
962 free(super);
963 *sbp = NULL;
964 break;
965 case 1: st->ss = NULL; return 1; /*bad device */
966 case 2: break; /* bad, try next */
967 }
968 }
969 if (bestvers != -1) {
970 int rv;
971 st->minor_version = bestvers;
972 st->ss = &super1;
973 st->max_devs = 384;
974 rv = load_super1(st, fd, sbp, devname);
975 if (rv) st->ss = NULL;
976 return rv;
977 }
978 st->ss = NULL;
979 return 2;
980 }
981 #ifdef BLKGETSIZE64
982 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
983 #endif
984 {
985 if (ioctl(fd, BLKGETSIZE, &size)) {
986 if (devname)
987 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
988 devname, strerror(errno));
989 return 1;
990 }
991 dsize = size;
992 } else
993 dsize >>= 9;
994
995 if (dsize < 24) {
996 if (devname)
997 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
998 devname, dsize);
999 return 1;
1000 }
1001
1002 /*
1003 * Calculate the position of the superblock.
1004 * It is always aligned to a 4K boundary and
1005 * depending on minor_version, it can be:
1006 * 0: At least 8K, but less than 12K, from end of device
1007 * 1: At start of device
1008 * 2: 4K from start of device.
1009 */
1010 switch(st->minor_version) {
1011 case 0:
1012 sb_offset = dsize;
1013 sb_offset -= 8*2;
1014 sb_offset &= ~(4*2-1);
1015 break;
1016 case 1:
1017 sb_offset = 0;
1018 break;
1019 case 2:
1020 sb_offset = 4*2;
1021 break;
1022 default:
1023 return -EINVAL;
1024 }
1025
1026 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1027
1028
1029 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1030 if (devname)
1031 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1032 devname, strerror(errno));
1033 return 1;
1034 }
1035
1036 super = malloc(1024 + sizeof(bitmap_super_t) +
1037 sizeof(struct misc_dev_info));
1038
1039 if (read(fd, super, 1024) != 1024) {
1040 if (devname)
1041 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1042 devname);
1043 free(super);
1044 return 1;
1045 }
1046
1047 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1048 if (devname)
1049 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1050 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1051 free(super);
1052 return 2;
1053 }
1054
1055 if (__le32_to_cpu(super->major_version) != 1) {
1056 if (devname)
1057 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1058 devname, __le32_to_cpu(super->major_version));
1059 free(super);
1060 return 2;
1061 }
1062 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1063 if (devname)
1064 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1065 devname);
1066 free(super);
1067 return 2;
1068 }
1069 *sbp = super;
1070
1071 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1072
1073 misc = (struct misc_dev_info*) (bsb+1);
1074 misc->device_size = dsize;
1075
1076 /* Now check on the bitmap superblock */
1077 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1078 return 0;
1079 /* Read the bitmap superblock and make sure it looks
1080 * valid. If it doesn't clear the bit. An --assemble --force
1081 * should get that written out.
1082 */
1083 locate_bitmap1(st, fd, super);
1084 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1085 != sizeof(struct bitmap_super_s))
1086 goto no_bitmap;
1087
1088 uuid_from_super1(uuid, super);
1089 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1090 memcmp(bsb->uuid, uuid, 16) != 0)
1091 goto no_bitmap;
1092 return 0;
1093
1094 no_bitmap:
1095 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
1096 return 0;
1097 }
1098
1099
1100 static struct supertype *match_metadata_desc1(char *arg)
1101 {
1102 struct supertype *st = malloc(sizeof(*st));
1103 if (!st) return st;
1104
1105 st->ss = &super1;
1106 st->max_devs = 384;
1107 if (strcmp(arg, "1") == 0 ||
1108 strcmp(arg, "1.0") == 0 ||
1109 strcmp(arg, "default/large") == 0) {
1110 st->minor_version = 0;
1111 return st;
1112 }
1113 if (strcmp(arg, "1.1") == 0) {
1114 st->minor_version = 1;
1115 return st;
1116 }
1117 if (strcmp(arg, "1.2") == 0) {
1118 st->minor_version = 2;
1119 return st;
1120 }
1121
1122 free(st);
1123 return NULL;
1124 }
1125
1126 /* find available size on device with this devsize, using
1127 * superblock type st, and reserving 'reserve' sectors for
1128 * a possible bitmap
1129 */
1130 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1131 {
1132 if (devsize < 24)
1133 return 0;
1134
1135 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1136 * if bigger than 200Gig, save 128k
1137 */
1138 if (devsize-64*2 >= 200*1024*1024*2)
1139 devsize -= 128*2;
1140 else if (devsize >= 8*1024*1024*2)
1141 devsize -= 64*2;
1142
1143 switch(st->minor_version) {
1144 case 0:
1145 /* at end */
1146 return ((devsize - 8*2 ) & ~(4*2-1));
1147 case 1:
1148 /* at start, 4K for superblock and possible bitmap */
1149 return devsize - 4*2;
1150 case 2:
1151 /* 4k from start, 4K for superblock and possible bitmap */
1152 return devsize - (4+4)*2;
1153 }
1154 return 0;
1155 }
1156
1157 static int
1158 add_internal_bitmap1(struct supertype *st, void *sbv,
1159 int *chunkp, int delay, int write_behind,
1160 unsigned long long size,
1161 int may_change, int major)
1162 {
1163 /*
1164 * If not may_change, then this is a 'Grow', and the bitmap
1165 * must fit after the superblock.
1166 * If may_change, then this is create, and we can put the bitmap
1167 * before the superblock if we like, or may move the start.
1168 * If !may_change, the bitmap MUST live at offset of 1K, until
1169 * we get a sysfs interface.
1170 *
1171 * size is in sectors, chunk is in bytes !!!
1172 */
1173
1174 unsigned long long bits;
1175 unsigned long long max_bits;
1176 unsigned long long min_chunk;
1177 long offset;
1178 int chunk = *chunkp;
1179 int room = 0;
1180 struct mdp_superblock_1 *sb = sbv;
1181 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1182
1183 switch(st->minor_version) {
1184 case 0:
1185 /* either 3K after the superblock, or some amount of space
1186 * before.
1187 */
1188 if (may_change) {
1189 /* We are creating array, so we *know* how much room has
1190 * been left.
1191 */
1192 offset = 0;
1193 if (__le64_to_cpu(sb->size) >= 200*1024*1024*2)
1194 room = 128*2;
1195 else if (__le64_to_cpu(sb->size) > 8*1024*1024*2)
1196 room = 64*2;
1197 else {
1198 room = 3*2;
1199 offset = 2;
1200 }
1201 } else {
1202 room = __le64_to_cpu(sb->super_offset)
1203 - __le64_to_cpu(sb->data_offset)
1204 - __le64_to_cpu(sb->data_size);
1205 /* remove '1 ||' when we can set offset via sysfs */
1206 if (1 || (room < 3*2 &&
1207 __le32_to_cpu(sb->max_dev) <= 384)) {
1208 room = 3*2;
1209 offset = 1*2;
1210 } else {
1211 offset = 0; /* means movable offset */
1212 }
1213 }
1214 break;
1215 case 1:
1216 case 2: /* between superblock and data */
1217 if (may_change) {
1218 offset = 4*2;
1219 if (__le64_to_cpu(sb->size) >= 200*1024*1024*2)
1220 room = 128*2;
1221 else if (__le64_to_cpu(sb->size) > 8*1024*1024*2)
1222 room = 64*2;
1223 else
1224 room = 3*2;
1225 } else {
1226 room = __le64_to_cpu(sb->data_offset)
1227 - __le64_to_cpu(sb->super_offset);
1228 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1229 room -= 2;
1230 offset = 2;
1231 } else {
1232 room -= 4*2;
1233 offset = 4*2;
1234 }
1235 }
1236 break;
1237 }
1238
1239 if (chunk == UnSet && room > 128*2)
1240 /* Limit to 128K of bitmap when chunk size not requested */
1241 room = 128*2;
1242
1243 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1244
1245 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1246 bits = (size*512)/min_chunk +1;
1247 while (bits > max_bits) {
1248 min_chunk *= 2;
1249 bits = (bits+1)/2;
1250 }
1251 if (chunk == UnSet)
1252 chunk = min_chunk;
1253 else if (chunk < min_chunk)
1254 return 0; /* chunk size too small */
1255 if (chunk == 0) /* rounding problem */
1256 return 0;
1257
1258 if (offset == 0) {
1259 bits = (size*512) / chunk + 1;
1260 room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512;
1261 offset = -room;
1262 }
1263
1264 sb->bitmap_offset = __cpu_to_le32(offset);
1265
1266 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
1267 memset(bms, 0, sizeof(*bms));
1268 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1269 bms->version = __cpu_to_le32(major);
1270 uuid_from_super1((int*)bms->uuid, sb);
1271 bms->chunksize = __cpu_to_le32(chunk);
1272 bms->daemon_sleep = __cpu_to_le32(delay);
1273 bms->sync_size = __cpu_to_le64(size);
1274 bms->write_behind = __cpu_to_le32(write_behind);
1275
1276 *chunkp = chunk;
1277 return 1;
1278 }
1279
1280
1281 static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
1282 {
1283 unsigned long long offset;
1284 struct mdp_superblock_1 *sb;
1285 int mustfree = 0;
1286
1287 if (!sbv) {
1288 if (st->ss->load_super(st, fd, &sbv, NULL))
1289 return; /* no error I hope... */
1290 mustfree = 1;
1291 }
1292 sb = sbv;
1293
1294 offset = __le64_to_cpu(sb->super_offset);
1295 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1296 if (mustfree)
1297 free(sb);
1298 lseek64(fd, offset<<9, 0);
1299 }
1300
1301 static int write_bitmap1(struct supertype *st, int fd, void *sbv)
1302 {
1303 struct mdp_superblock_1 *sb = sbv;
1304 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
1305 int rv = 0;
1306
1307 int towrite, n;
1308 char buf[4096];
1309
1310 locate_bitmap1(st, fd, sbv);
1311
1312 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1313 sizeof(bitmap_super_t))
1314 return -2;
1315 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1316 towrite = (towrite+7) >> 3; /* bits to bytes */
1317 memset(buf, 0xff, sizeof(buf));
1318 while (towrite > 0) {
1319 n = towrite;
1320 if (n > sizeof(buf))
1321 n = sizeof(buf);
1322 n = write(fd, buf, n);
1323 if (n > 0)
1324 towrite -= n;
1325 else
1326 break;
1327 }
1328 fsync(fd);
1329 if (towrite)
1330 rv = -2;
1331
1332 return rv;
1333 }
1334
1335 struct superswitch super1 = {
1336 #ifndef MDASSEMBLE
1337 .examine_super = examine_super1,
1338 .brief_examine_super = brief_examine_super1,
1339 .detail_super = detail_super1,
1340 .brief_detail_super = brief_detail_super1,
1341 #endif
1342 .match_home = match_home1,
1343 .uuid_from_super = uuid_from_super1,
1344 .getinfo_super = getinfo_super1,
1345 .update_super = update_super1,
1346 .init_super = init_super1,
1347 .add_to_super = add_to_super1,
1348 .store_super = store_super1,
1349 .write_init_super = write_init_super1,
1350 .compare_super = compare_super1,
1351 .load_super = load_super1,
1352 .match_metadata_desc = match_metadata_desc1,
1353 .avail_size = avail_size1,
1354 .add_internal_bitmap = add_internal_bitmap1,
1355 .locate_bitmap = locate_bitmap1,
1356 .write_bitmap = write_bitmap1,
1357 .major = 1,
1358 #if __BYTE_ORDER == BIG_ENDIAN
1359 .swapuuid = 0,
1360 #else
1361 .swapuuid = 1,
1362 #endif
1363 };