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