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