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