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