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