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