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