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