c51089970585919e0cc2a66631bd4caf3b0bdd88
[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 /* Add a device to the superblock being created */
771 static void add_to_super1(struct supertype *st, mdu_disk_info_t *dk)
772 {
773 struct mdp_superblock_1 *sb = st->sb;
774 __u16 *rp = sb->dev_roles + dk->number;
775 if ((dk->state & 6) == 6) /* active, sync */
776 *rp = __cpu_to_le16(dk->raid_disk);
777 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
778 *rp = 0xffff;
779 else
780 *rp = 0xfffe;
781 }
782
783 static void locate_bitmap1(struct supertype *st, int fd);
784
785 static int store_super1(struct supertype *st, int fd)
786 {
787 struct mdp_superblock_1 *sb = st->sb;
788 unsigned long long sb_offset;
789 int sbsize;
790 unsigned long long dsize;
791
792 if (!get_dev_size(fd, NULL, &dsize))
793 return 1;
794
795 dsize >>= 9;
796
797 if (dsize < 24)
798 return 2;
799
800 /*
801 * Calculate the position of the superblock.
802 * It is always aligned to a 4K boundary and
803 * depending on minor_version, it can be:
804 * 0: At least 8K, but less than 12K, from end of device
805 * 1: At start of device
806 * 2: 4K from start of device.
807 */
808 switch(st->minor_version) {
809 case 0:
810 sb_offset = dsize;
811 sb_offset -= 8*2;
812 sb_offset &= ~(4*2-1);
813 break;
814 case 1:
815 sb_offset = 0;
816 break;
817 case 2:
818 sb_offset = 4*2;
819 break;
820 default:
821 return -EINVAL;
822 }
823
824
825
826 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
827 0 != __le64_to_cpu(sb->super_offset)
828 ) {
829 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
830 abort();
831 }
832
833 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
834 return 3;
835
836 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
837
838 if (write(fd, sb, sbsize) != sbsize)
839 return 4;
840
841 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
842 struct bitmap_super_s *bm = (struct bitmap_super_s*)
843 (((char*)sb)+1024);
844 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
845 locate_bitmap1(st, fd);
846 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
847 return 5;
848 }
849 }
850 fsync(fd);
851 return 0;
852 }
853
854 static int load_super1(struct supertype *st, int fd, char *devname);
855
856 static unsigned long choose_bm_space(unsigned long devsize)
857 {
858 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
859 * if bigger than 200Gig, save 128k
860 */
861 if (devsize < 64*2) return 0;
862 if (devsize - 64*2 >= 200*1024*1024*2)
863 return 128*2;
864 if (devsize - 4*2 > 8*1024*1024*2)
865 return 64*2;
866 return 4*2;
867 }
868
869 static int write_init_super1(struct supertype *st,
870 mdu_disk_info_t *dinfo, char *devname)
871 {
872 struct mdp_superblock_1 *sb = st->sb;
873 struct supertype refst;
874 int fd = open(devname, O_RDWR | O_EXCL);
875 int rfd;
876 int rv;
877 int bm_space;
878
879 unsigned long long dsize, array_size;
880 long long sb_offset;
881
882
883 if (fd < 0) {
884 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
885 devname);
886 return -1;
887 }
888
889 sb->dev_number = __cpu_to_le32(dinfo->number);
890 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
891 sb->devflags |= __cpu_to_le32(WriteMostly1);
892
893 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
894 read(rfd, sb->device_uuid, 16) != 16) {
895 *(__u32*)(sb->device_uuid) = random();
896 *(__u32*)(sb->device_uuid+4) = random();
897 *(__u32*)(sb->device_uuid+8) = random();
898 *(__u32*)(sb->device_uuid+12) = random();
899 }
900 if (rfd >= 0) close(rfd);
901 sb->events = 0;
902
903 refst =*st;
904 refst.sb = NULL;
905 if (load_super1(&refst, fd, NULL)==0) {
906 struct mdp_superblock_1 *refsb = refst.sb;
907
908 memcpy(sb->device_uuid, refsb->device_uuid, 16);
909 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
910 /* same array, so preserve events and dev_number */
911 sb->events = refsb->events;
912 /* bugs in 2.6.17 and earlier mean the dev_number
913 * chosen in Manage must be preserved
914 */
915 if (get_linux_version() >= 2006018)
916 sb->dev_number = refsb->dev_number;
917 }
918 free(refsb);
919 }
920
921 if (!get_dev_size(fd, NULL, &dsize))
922 return 1;
923 dsize >>= 9;
924
925 if (dsize < 24) {
926 close(fd);
927 return 2;
928 }
929
930
931 /*
932 * Calculate the position of the superblock.
933 * It is always aligned to a 4K boundary and
934 * depending on minor_version, it can be:
935 * 0: At least 8K, but less than 12K, from end of device
936 * 1: At start of device
937 * 2: 4K from start of device.
938 * Depending on the array size, we might leave extra space
939 * for a bitmap.
940 */
941 array_size = __le64_to_cpu(sb->size);
942 /* work out how much space we left for a bitmap */
943 bm_space = choose_bm_space(array_size);
944
945 switch(st->minor_version) {
946 case 0:
947 sb_offset = dsize;
948 sb_offset -= 8*2;
949 sb_offset &= ~(4*2-1);
950 sb->super_offset = __cpu_to_le64(sb_offset);
951 sb->data_offset = __cpu_to_le64(0);
952 if (sb_offset - bm_space < array_size)
953 bm_space = sb_offset - array_size;
954 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
955 break;
956 case 1:
957 sb->super_offset = __cpu_to_le64(0);
958 if (4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
959 bm_space = dsize - __le64_to_cpu(sb->size) - 4*2;
960 sb->data_offset = __cpu_to_le64(bm_space + 4*2);
961 sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
962 break;
963 case 2:
964 sb_offset = 4*2;
965 sb->super_offset = __cpu_to_le64(4*2);
966 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
967 bm_space = dsize - __le64_to_cpu(sb->size) - 4*2 - 4*2;
968 sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
969 sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space );
970 break;
971 default:
972 return -EINVAL;
973 }
974
975
976 sb->sb_csum = calc_sb_1_csum(sb);
977 rv = store_super1(st, fd);
978 if (rv)
979 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
980
981 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
982 rv = st->ss->write_bitmap(st, fd);
983 close(fd);
984 return rv;
985 }
986
987 static int compare_super1(struct supertype *st, struct supertype *tst)
988 {
989 /*
990 * return:
991 * 0 same, or first was empty, and second was copied
992 * 1 second had wrong number
993 * 2 wrong uuid
994 * 3 wrong other info
995 */
996 struct mdp_superblock_1 *first = st->sb;
997 struct mdp_superblock_1 *second = tst->sb;
998
999 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1000 return 1;
1001 if (second->major_version != __cpu_to_le32(1))
1002 return 1;
1003
1004 if (!first) {
1005 first = malloc(1024+sizeof(bitmap_super_t) +
1006 sizeof(struct misc_dev_info));
1007 memcpy(first, second, 1024+sizeof(bitmap_super_t) +
1008 sizeof(struct misc_dev_info));
1009 st->sb = first;
1010 return 0;
1011 }
1012 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1013 return 2;
1014
1015 if (first->ctime != second->ctime ||
1016 first->level != second->level ||
1017 first->layout != second->layout ||
1018 first->size != second->size ||
1019 first->chunksize != second->chunksize ||
1020 first->raid_disks != second->raid_disks)
1021 return 3;
1022 return 0;
1023 }
1024
1025 static void free_super1(struct supertype *st);
1026
1027 static int load_super1(struct supertype *st, int fd, char *devname)
1028 {
1029 unsigned long long dsize;
1030 unsigned long long sb_offset;
1031 struct mdp_superblock_1 *super;
1032 int uuid[4];
1033 struct bitmap_super_s *bsb;
1034 struct misc_dev_info *misc;
1035
1036 free_super1(st);
1037
1038 if (st->ss == NULL || st->minor_version == -1) {
1039 int bestvers = -1;
1040 struct supertype tst;
1041 __u64 bestctime = 0;
1042 /* guess... choose latest ctime */
1043 tst.ss = &super1;
1044 tst.sb = NULL;
1045 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1046 switch(load_super1(&tst, fd, devname)) {
1047 case 0: super = tst.sb;
1048 if (bestvers == -1 ||
1049 bestctime < __le64_to_cpu(super->ctime)) {
1050 bestvers = tst.minor_version;
1051 bestctime = __le64_to_cpu(super->ctime);
1052 }
1053 free(super);
1054 tst.sb = NULL;
1055 break;
1056 case 1: return 1; /*bad device */
1057 case 2: break; /* bad, try next */
1058 }
1059 }
1060 if (bestvers != -1) {
1061 int rv;
1062 tst.minor_version = bestvers;
1063 tst.ss = &super1;
1064 tst.max_devs = 384;
1065 rv = load_super1(&tst, fd, devname);
1066 if (rv == 0)
1067 *st = tst;
1068 return rv;
1069 }
1070 return 2;
1071 }
1072 if (!get_dev_size(fd, devname, &dsize))
1073 return 1;
1074 dsize >>= 9;
1075
1076 if (dsize < 24) {
1077 if (devname)
1078 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1079 devname, dsize);
1080 return 1;
1081 }
1082
1083 /*
1084 * Calculate the position of the superblock.
1085 * It is always aligned to a 4K boundary and
1086 * depending on minor_version, it can be:
1087 * 0: At least 8K, but less than 12K, from end of device
1088 * 1: At start of device
1089 * 2: 4K from start of device.
1090 */
1091 switch(st->minor_version) {
1092 case 0:
1093 sb_offset = dsize;
1094 sb_offset -= 8*2;
1095 sb_offset &= ~(4*2-1);
1096 break;
1097 case 1:
1098 sb_offset = 0;
1099 break;
1100 case 2:
1101 sb_offset = 4*2;
1102 break;
1103 default:
1104 return -EINVAL;
1105 }
1106
1107 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1108
1109
1110 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1111 if (devname)
1112 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1113 devname, strerror(errno));
1114 return 1;
1115 }
1116
1117 super = malloc(1024 + sizeof(bitmap_super_t) +
1118 sizeof(struct misc_dev_info));
1119
1120 if (read(fd, super, 1024) != 1024) {
1121 if (devname)
1122 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1123 devname);
1124 free(super);
1125 return 1;
1126 }
1127
1128 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1129 if (devname)
1130 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1131 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1132 free(super);
1133 return 2;
1134 }
1135
1136 if (__le32_to_cpu(super->major_version) != 1) {
1137 if (devname)
1138 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1139 devname, __le32_to_cpu(super->major_version));
1140 free(super);
1141 return 2;
1142 }
1143 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1144 if (devname)
1145 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1146 devname);
1147 free(super);
1148 return 2;
1149 }
1150 st->sb = super;
1151
1152 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1153
1154 misc = (struct misc_dev_info*) (bsb+1);
1155 misc->device_size = dsize;
1156
1157 /* Now check on the bitmap superblock */
1158 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1159 return 0;
1160 /* Read the bitmap superblock and make sure it looks
1161 * valid. If it doesn't clear the bit. An --assemble --force
1162 * should get that written out.
1163 */
1164 locate_bitmap1(st, fd);
1165 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1166 != sizeof(struct bitmap_super_s))
1167 goto no_bitmap;
1168
1169 uuid_from_super1(st, uuid);
1170 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1171 memcmp(bsb->uuid, uuid, 16) != 0)
1172 goto no_bitmap;
1173 return 0;
1174
1175 no_bitmap:
1176 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
1177 return 0;
1178 }
1179
1180
1181 static struct supertype *match_metadata_desc1(char *arg)
1182 {
1183 struct supertype *st = malloc(sizeof(*st));
1184 if (!st) return st;
1185
1186 st->ss = &super1;
1187 st->max_devs = 384;
1188 st->sb = NULL;
1189 if (strcmp(arg, "1.0") == 0) {
1190 st->minor_version = 0;
1191 return st;
1192 }
1193 if (strcmp(arg, "1.1") == 0) {
1194 st->minor_version = 1;
1195 return st;
1196 }
1197 if (strcmp(arg, "1.2") == 0) {
1198 st->minor_version = 2;
1199 return st;
1200 }
1201 if (strcmp(arg, "1") == 0 ||
1202 strcmp(arg, "default") == 0) {
1203 st->minor_version = -1;
1204 return st;
1205 }
1206
1207 free(st);
1208 return NULL;
1209 }
1210
1211 /* find available size on device with this devsize, using
1212 * superblock type st, and reserving 'reserve' sectors for
1213 * a possible bitmap
1214 */
1215 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1216 {
1217 if (devsize < 24)
1218 return 0;
1219
1220 devsize -= choose_bm_space(devsize);
1221
1222 switch(st->minor_version) {
1223 case -1: /* no specified. Now time to set default */
1224 st->minor_version = 0;
1225 /* FALL THROUGH */
1226 case 0:
1227 /* at end */
1228 return ((devsize - 8*2 ) & ~(4*2-1));
1229 case 1:
1230 /* at start, 4K for superblock and possible bitmap */
1231 return devsize - 4*2;
1232 case 2:
1233 /* 4k from start, 4K for superblock and possible bitmap */
1234 return devsize - (4+4)*2;
1235 }
1236 return 0;
1237 }
1238
1239 static int
1240 add_internal_bitmap1(struct supertype *st,
1241 int *chunkp, int delay, int write_behind,
1242 unsigned long long size,
1243 int may_change, int major)
1244 {
1245 /*
1246 * If not may_change, then this is a 'Grow', and the bitmap
1247 * must fit after the superblock.
1248 * If may_change, then this is create, and we can put the bitmap
1249 * before the superblock if we like, or may move the start.
1250 * If !may_change, the bitmap MUST live at offset of 1K, until
1251 * we get a sysfs interface.
1252 *
1253 * size is in sectors, chunk is in bytes !!!
1254 */
1255
1256 unsigned long long bits;
1257 unsigned long long max_bits;
1258 unsigned long long min_chunk;
1259 long offset;
1260 int chunk = *chunkp;
1261 int room = 0;
1262 struct mdp_superblock_1 *sb = st->sb;
1263 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1264
1265 switch(st->minor_version) {
1266 case 0:
1267 /* either 3K after the superblock, or some amount of space
1268 * before.
1269 */
1270 if (may_change) {
1271 /* We are creating array, so we *know* how much room has
1272 * been left.
1273 */
1274 offset = 0;
1275 room = choose_bm_space(__le64_to_cpu(sb->size));
1276 if (room == 4*2) {
1277 /* make it 3K after the superblock */
1278 room = 3*2;
1279 offset = 2;
1280 }
1281 } else {
1282 room = __le64_to_cpu(sb->super_offset)
1283 - __le64_to_cpu(sb->data_offset)
1284 - __le64_to_cpu(sb->data_size);
1285 /* remove '1 ||' when we can set offset via sysfs */
1286 if (1 || (room < 3*2 &&
1287 __le32_to_cpu(sb->max_dev) <= 384)) {
1288 room = 3*2;
1289 offset = 1*2;
1290 } else {
1291 offset = 0; /* means movable offset */
1292 }
1293 }
1294 break;
1295 case 1:
1296 case 2: /* between superblock and data */
1297 if (may_change) {
1298 offset = 4*2;
1299 room = choose_bm_space(__le64_to_cpu(sb->size));
1300 } else {
1301 room = __le64_to_cpu(sb->data_offset)
1302 - __le64_to_cpu(sb->super_offset);
1303 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1304 room -= 2;
1305 offset = 2;
1306 } else {
1307 room -= 4*2;
1308 offset = 4*2;
1309 }
1310 }
1311 break;
1312 default:
1313 return 0;
1314 }
1315
1316 if (chunk == UnSet && room > 128*2)
1317 /* Limit to 128K of bitmap when chunk size not requested */
1318 room = 128*2;
1319
1320 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1321
1322 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1323 bits = (size*512)/min_chunk +1;
1324 while (bits > max_bits) {
1325 min_chunk *= 2;
1326 bits = (bits+1)/2;
1327 }
1328 if (chunk == UnSet)
1329 chunk = min_chunk;
1330 else if (chunk < min_chunk)
1331 return 0; /* chunk size too small */
1332 if (chunk == 0) /* rounding problem */
1333 return 0;
1334
1335 if (offset == 0) {
1336 bits = (size*512) / chunk + 1;
1337 room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512;
1338 offset = -room;
1339 }
1340
1341 sb->bitmap_offset = __cpu_to_le32(offset);
1342
1343 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
1344 memset(bms, 0, sizeof(*bms));
1345 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1346 bms->version = __cpu_to_le32(major);
1347 uuid_from_super1(st, (int*)bms->uuid);
1348 bms->chunksize = __cpu_to_le32(chunk);
1349 bms->daemon_sleep = __cpu_to_le32(delay);
1350 bms->sync_size = __cpu_to_le64(size);
1351 bms->write_behind = __cpu_to_le32(write_behind);
1352
1353 *chunkp = chunk;
1354 return 1;
1355 }
1356
1357
1358 static void locate_bitmap1(struct supertype *st, int fd)
1359 {
1360 unsigned long long offset;
1361 struct mdp_superblock_1 *sb;
1362 int mustfree = 0;
1363
1364 if (!st->sb) {
1365 if (st->ss->load_super(st, fd, NULL))
1366 return; /* no error I hope... */
1367 mustfree = 1;
1368 }
1369 sb = st->sb;
1370
1371 offset = __le64_to_cpu(sb->super_offset);
1372 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1373 if (mustfree)
1374 free(sb);
1375 lseek64(fd, offset<<9, 0);
1376 }
1377
1378 static int write_bitmap1(struct supertype *st, int fd)
1379 {
1380 struct mdp_superblock_1 *sb = st->sb;
1381 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
1382 int rv = 0;
1383
1384 int towrite, n;
1385 char buf[4096];
1386
1387 locate_bitmap1(st, fd);
1388
1389 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1390 sizeof(bitmap_super_t))
1391 return -2;
1392 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1393 towrite = (towrite+7) >> 3; /* bits to bytes */
1394 memset(buf, 0xff, sizeof(buf));
1395 while (towrite > 0) {
1396 n = towrite;
1397 if (n > sizeof(buf))
1398 n = sizeof(buf);
1399 n = write(fd, buf, n);
1400 if (n > 0)
1401 towrite -= n;
1402 else
1403 break;
1404 }
1405 fsync(fd);
1406 if (towrite)
1407 rv = -2;
1408
1409 return rv;
1410 }
1411
1412 static void free_super1(struct supertype *st)
1413 {
1414 if (st->sb)
1415 free(st->sb);
1416 st->sb = NULL;
1417 }
1418
1419 static int validate_geometry1(struct supertype *st, int level,
1420 int layout, int raiddisks,
1421 int chunk, unsigned long long size,
1422 char *subdev, unsigned long long *freesize)
1423 {
1424 unsigned long long ldsize;
1425 int fd;
1426
1427 if (level == LEVEL_CONTAINER)
1428 return 0;
1429 if (!subdev)
1430 return 1;
1431
1432 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1433 if (fd < 0) {
1434 fprintf(stderr, Name ": Cannot open %s: %s\n",
1435 subdev, strerror(errno));
1436 return 0;
1437 }
1438 if (!get_dev_size(fd, subdev, &ldsize)) {
1439 close(fd);
1440 return 0;
1441 }
1442 close(fd);
1443
1444 *freesize = avail_size1(st, ldsize >> 9);
1445 return 1;
1446 }
1447
1448 struct superswitch super1 = {
1449 #ifndef MDASSEMBLE
1450 .examine_super = examine_super1,
1451 .brief_examine_super = brief_examine_super1,
1452 .export_examine_super = export_examine_super1,
1453 .detail_super = detail_super1,
1454 .brief_detail_super = brief_detail_super1,
1455 .export_detail_super = export_detail_super1,
1456 #endif
1457 .match_home = match_home1,
1458 .uuid_from_super = uuid_from_super1,
1459 .getinfo_super = getinfo_super1,
1460 .update_super = update_super1,
1461 .init_super = init_super1,
1462 .add_to_super = add_to_super1,
1463 .store_super = store_super1,
1464 .write_init_super = write_init_super1,
1465 .compare_super = compare_super1,
1466 .load_super = load_super1,
1467 .match_metadata_desc = match_metadata_desc1,
1468 .avail_size = avail_size1,
1469 .add_internal_bitmap = add_internal_bitmap1,
1470 .locate_bitmap = locate_bitmap1,
1471 .write_bitmap = write_bitmap1,
1472 .free_super = free_super1,
1473 .validate_geometry = validate_geometry1,
1474 .major = 1,
1475 #if __BYTE_ORDER == BIG_ENDIAN
1476 .swapuuid = 0,
1477 #else
1478 .swapuuid = 1,
1479 #endif
1480 };