]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
super1: leave more space in front of data by default.
[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 = (long)__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 && ! rv ; 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 /* Try for multiple of 1Meg so it is nicely aligned */
1193 #define ONE_MEG (2*1024)
1194 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1195 if (reserved + __le64_to_cpu(sb->size) > dsize)
1196 reserved = dsize - __le64_to_cpu(sb->size);
1197 /* force 4K alignment */
1198 reserved &= ~7ULL;
1199
1200 if (reserved < headroom)
1201 reserved = headroom;
1202
1203 sb->data_offset = __cpu_to_le64(reserved);
1204 sb->data_size = __cpu_to_le64(dsize - reserved);
1205 break;
1206 case 2:
1207 sb_offset = 4*2;
1208 sb->super_offset = __cpu_to_le64(4*2);
1209 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1210 > dsize)
1211 bm_space = dsize - __le64_to_cpu(sb->size)
1212 - 4*2 - 4*2;
1213
1214 reserved = bm_space + 4*2 + 4*2;
1215 /* Try for multiple of 1Meg so it is nicely aligned */
1216 #define ONE_MEG (2*1024)
1217 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1218 if (reserved + __le64_to_cpu(sb->size) > dsize)
1219 reserved = dsize - __le64_to_cpu(sb->size);
1220 /* force 4K alignment */
1221 reserved &= ~7ULL;
1222
1223 if (reserved < headroom)
1224 reserved = headroom;
1225
1226 sb->data_offset = __cpu_to_le64(reserved);
1227 sb->data_size = __cpu_to_le64(dsize - reserved);
1228 break;
1229 default:
1230 fprintf(stderr, Name ": Failed to write invalid "
1231 "metadata format 1.%i to %s\n",
1232 st->minor_version, di->devname);
1233 rv = -EINVAL;
1234 goto out;
1235 }
1236
1237
1238 sb->sb_csum = calc_sb_1_csum(sb);
1239 rv = store_super1(st, di->fd);
1240 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1241 rv = st->ss->write_bitmap(st, di->fd);
1242 close(di->fd);
1243 di->fd = -1;
1244 }
1245 error_out:
1246 if (rv)
1247 fprintf(stderr, Name ": Failed to write metadata to %s\n",
1248 di->devname);
1249 out:
1250 return rv;
1251 }
1252 #endif
1253
1254 static int compare_super1(struct supertype *st, struct supertype *tst)
1255 {
1256 /*
1257 * return:
1258 * 0 same, or first was empty, and second was copied
1259 * 1 second had wrong number
1260 * 2 wrong uuid
1261 * 3 wrong other info
1262 */
1263 struct mdp_superblock_1 *first = st->sb;
1264 struct mdp_superblock_1 *second = tst->sb;
1265
1266 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1267 return 1;
1268 if (second->major_version != __cpu_to_le32(1))
1269 return 1;
1270
1271 if (!first) {
1272 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1273 fprintf(stderr, Name
1274 ": %s could not allocate superblock\n", __func__);
1275 return 1;
1276 }
1277 memcpy(first, second, SUPER1_SIZE);
1278 st->sb = first;
1279 return 0;
1280 }
1281 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1282 return 2;
1283
1284 if (first->ctime != second->ctime ||
1285 first->level != second->level ||
1286 first->layout != second->layout ||
1287 first->size != second->size ||
1288 first->chunksize != second->chunksize ||
1289 first->raid_disks != second->raid_disks)
1290 return 3;
1291 return 0;
1292 }
1293
1294 static int load_super1(struct supertype *st, int fd, char *devname)
1295 {
1296 unsigned long long dsize;
1297 unsigned long long sb_offset;
1298 struct mdp_superblock_1 *super;
1299 int uuid[4];
1300 struct bitmap_super_s *bsb;
1301 struct misc_dev_info *misc;
1302 struct align_fd afd;
1303
1304 free_super1(st);
1305
1306 init_afd(&afd, fd);
1307
1308 if (st->ss == NULL || st->minor_version == -1) {
1309 int bestvers = -1;
1310 struct supertype tst;
1311 __u64 bestctime = 0;
1312 /* guess... choose latest ctime */
1313 memset(&tst, 0, sizeof(tst));
1314 tst.ss = &super1;
1315 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1316 switch(load_super1(&tst, fd, devname)) {
1317 case 0: super = tst.sb;
1318 if (bestvers == -1 ||
1319 bestctime < __le64_to_cpu(super->ctime)) {
1320 bestvers = tst.minor_version;
1321 bestctime = __le64_to_cpu(super->ctime);
1322 }
1323 free(super);
1324 tst.sb = NULL;
1325 break;
1326 case 1: return 1; /*bad device */
1327 case 2: break; /* bad, try next */
1328 }
1329 }
1330 if (bestvers != -1) {
1331 int rv;
1332 tst.minor_version = bestvers;
1333 tst.ss = &super1;
1334 tst.max_devs = MAX_DEVS;
1335 rv = load_super1(&tst, fd, devname);
1336 if (rv == 0)
1337 *st = tst;
1338 return rv;
1339 }
1340 return 2;
1341 }
1342 if (!get_dev_size(fd, devname, &dsize))
1343 return 1;
1344 dsize >>= 9;
1345
1346 if (dsize < 24) {
1347 if (devname)
1348 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1349 devname, dsize);
1350 return 1;
1351 }
1352
1353 /*
1354 * Calculate the position of the superblock.
1355 * It is always aligned to a 4K boundary and
1356 * depending on minor_version, it can be:
1357 * 0: At least 8K, but less than 12K, from end of device
1358 * 1: At start of device
1359 * 2: 4K from start of device.
1360 */
1361 switch(st->minor_version) {
1362 case 0:
1363 sb_offset = dsize;
1364 sb_offset -= 8*2;
1365 sb_offset &= ~(4*2-1);
1366 break;
1367 case 1:
1368 sb_offset = 0;
1369 break;
1370 case 2:
1371 sb_offset = 4*2;
1372 break;
1373 default:
1374 return -EINVAL;
1375 }
1376
1377 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1378
1379
1380 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1381 if (devname)
1382 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1383 devname, strerror(errno));
1384 return 1;
1385 }
1386
1387 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1388 fprintf(stderr, Name ": %s could not allocate superblock\n",
1389 __func__);
1390 return 1;
1391 }
1392
1393 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
1394 if (devname)
1395 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1396 devname);
1397 free(super);
1398 return 1;
1399 }
1400
1401 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1402 if (devname)
1403 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1404 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1405 free(super);
1406 return 2;
1407 }
1408
1409 if (__le32_to_cpu(super->major_version) != 1) {
1410 if (devname)
1411 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1412 devname, __le32_to_cpu(super->major_version));
1413 free(super);
1414 return 2;
1415 }
1416 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1417 if (devname)
1418 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1419 devname);
1420 free(super);
1421 return 2;
1422 }
1423 st->sb = super;
1424
1425 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1426
1427 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
1428 misc->device_size = dsize;
1429
1430 /* Now check on the bitmap superblock */
1431 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1432 return 0;
1433 /* Read the bitmap superblock and make sure it looks
1434 * valid. If it doesn't clear the bit. An --assemble --force
1435 * should get that written out.
1436 */
1437 locate_bitmap1(st, fd);
1438 if (aread(&afd, bsb, 512) != 512)
1439 goto no_bitmap;
1440
1441 uuid_from_super1(st, uuid);
1442 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1443 memcmp(bsb->uuid, uuid, 16) != 0)
1444 goto no_bitmap;
1445 return 0;
1446
1447 no_bitmap:
1448 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
1449 & ~MD_FEATURE_BITMAP_OFFSET);
1450 return 0;
1451 }
1452
1453
1454 static struct supertype *match_metadata_desc1(char *arg)
1455 {
1456 struct supertype *st = calloc(1, sizeof(*st));
1457 if (!st)
1458 return st;
1459
1460 st->container_dev = NoMdDev;
1461 st->ss = &super1;
1462 st->max_devs = MAX_DEVS;
1463 st->sb = NULL;
1464 /* leading zeros can be safely ignored. --detail generates them. */
1465 while (*arg == '0')
1466 arg++;
1467 if (strcmp(arg, "1.0") == 0 ||
1468 strcmp(arg, "1.00") == 0) {
1469 st->minor_version = 0;
1470 return st;
1471 }
1472 if (strcmp(arg, "1.1") == 0 ||
1473 strcmp(arg, "1.01") == 0
1474 ) {
1475 st->minor_version = 1;
1476 return st;
1477 }
1478 if (strcmp(arg, "1.2") == 0 ||
1479 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1480 strcmp(arg, "default") == 0 ||
1481 #endif /* DEFAULT_OLD_METADATA */
1482 strcmp(arg, "1.02") == 0) {
1483 st->minor_version = 2;
1484 return st;
1485 }
1486 if (strcmp(arg, "1") == 0 ||
1487 strcmp(arg, "default") == 0) {
1488 st->minor_version = -1;
1489 return st;
1490 }
1491
1492 free(st);
1493 return NULL;
1494 }
1495
1496 /* find available size on device with this devsize, using
1497 * superblock type st, and reserving 'reserve' sectors for
1498 * a possible bitmap
1499 */
1500 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1501 {
1502 struct mdp_superblock_1 *super = st->sb;
1503 if (devsize < 24)
1504 return 0;
1505
1506 if (super == NULL)
1507 /* creating: allow suitable space for bitmap */
1508 devsize -= choose_bm_space(devsize);
1509 #ifndef MDASSEMBLE
1510 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1511 /* hot-add. allow for actual size of bitmap */
1512 struct bitmap_super_s *bsb;
1513 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1514 devsize -= bitmap_sectors(bsb);
1515 }
1516 #endif
1517
1518 if (st->minor_version < 0)
1519 /* not specified, so time to set default */
1520 st->minor_version = 2;
1521 if (super == NULL && st->minor_version > 0) {
1522 /* haven't committed to a size yet, so allow some
1523 * slack for space for reshape.
1524 * Limit slack to 128M, but aim for about 0.1%
1525 */
1526 unsigned long long headroom = 128*1024*2;
1527 while ((headroom << 10) > devsize)
1528 headroom >>= 1;
1529 devsize -= headroom;
1530 }
1531 switch(st->minor_version) {
1532 case 0:
1533 /* at end */
1534 return ((devsize - 8*2 ) & ~(4*2-1));
1535 case 1:
1536 /* at start, 4K for superblock and possible bitmap */
1537 return devsize - 4*2;
1538 case 2:
1539 /* 4k from start, 4K for superblock and possible bitmap */
1540 return devsize - (4+4)*2;
1541 }
1542 return 0;
1543 }
1544
1545 static int
1546 add_internal_bitmap1(struct supertype *st,
1547 int *chunkp, int delay, int write_behind,
1548 unsigned long long size,
1549 int may_change, int major)
1550 {
1551 /*
1552 * If not may_change, then this is a 'Grow' without sysfs support for
1553 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1554 * If may_change, then this is create or a Grow with sysfs syupport,
1555 * and we can put the bitmap wherever we like.
1556 *
1557 * size is in sectors, chunk is in bytes !!!
1558 */
1559
1560 unsigned long long bits;
1561 unsigned long long max_bits;
1562 unsigned long long min_chunk;
1563 long offset;
1564 unsigned long long chunk = *chunkp;
1565 int room = 0;
1566 int creating = 0;
1567 struct mdp_superblock_1 *sb = st->sb;
1568 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1569 int uuid[4];
1570
1571 if (__le64_to_cpu(sb->data_size) == 0)
1572 /* Must be creating the array, else data_size would be non-zero */
1573 creating = 1;
1574 switch(st->minor_version) {
1575 case 0:
1576 /* either 3K after the superblock (when hot-add),
1577 * or some amount of space before.
1578 */
1579 if (creating) {
1580 /* We are creating array, so we *know* how much room has
1581 * been left.
1582 */
1583 offset = 0;
1584 room = choose_bm_space(__le64_to_cpu(sb->size));
1585 } else {
1586 room = __le64_to_cpu(sb->super_offset)
1587 - __le64_to_cpu(sb->data_offset)
1588 - __le64_to_cpu(sb->data_size);
1589
1590 if (!may_change || (room < 3*2 &&
1591 __le32_to_cpu(sb->max_dev) <= 384)) {
1592 room = 3*2;
1593 offset = 1*2;
1594 } else {
1595 offset = 0; /* means movable offset */
1596 }
1597 }
1598 break;
1599 case 1:
1600 case 2: /* between superblock and data */
1601 if (creating) {
1602 offset = 4*2;
1603 room = choose_bm_space(__le64_to_cpu(sb->size));
1604 } else {
1605 room = __le64_to_cpu(sb->data_offset)
1606 - __le64_to_cpu(sb->super_offset);
1607 if (!may_change) {
1608 room -= 2; /* Leave 1K for superblock */
1609 offset = 2;
1610 } else {
1611 room -= 4*2; /* leave 4K for superblock */
1612 offset = 4*2;
1613 }
1614 }
1615 break;
1616 default:
1617 return 0;
1618 }
1619
1620 if (chunk == UnSet && room > 128*2)
1621 /* Limit to 128K of bitmap when chunk size not requested */
1622 room = 128*2;
1623
1624 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1625
1626 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1627 bits = (size*512)/min_chunk +1;
1628 while (bits > max_bits) {
1629 min_chunk *= 2;
1630 bits = (bits+1)/2;
1631 }
1632 if (chunk == UnSet) {
1633 /* For practical purpose, 64Meg is a good
1634 * default chunk size for internal bitmaps.
1635 */
1636 chunk = min_chunk;
1637 if (chunk < 64*1024*1024)
1638 chunk = 64*1024*1024;
1639 } else if (chunk < min_chunk)
1640 return 0; /* chunk size too small */
1641 if (chunk == 0) /* rounding problem */
1642 return 0;
1643
1644 if (offset == 0) {
1645 /* start bitmap on a 4K boundary with enough space for
1646 * the bitmap
1647 */
1648 bits = (size*512) / chunk + 1;
1649 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1650 room *= 8; /* convert 4K blocks to sectors */
1651 offset = -room;
1652 }
1653
1654 sb->bitmap_offset = (long)__cpu_to_le32(offset);
1655
1656 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
1657 | MD_FEATURE_BITMAP_OFFSET);
1658 memset(bms, 0, sizeof(*bms));
1659 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1660 bms->version = __cpu_to_le32(major);
1661 uuid_from_super1(st, uuid);
1662 memcpy(bms->uuid, uuid, 16);
1663 bms->chunksize = __cpu_to_le32(chunk);
1664 bms->daemon_sleep = __cpu_to_le32(delay);
1665 bms->sync_size = __cpu_to_le64(size);
1666 bms->write_behind = __cpu_to_le32(write_behind);
1667
1668 *chunkp = chunk;
1669 return 1;
1670 }
1671
1672 static void locate_bitmap1(struct supertype *st, int fd)
1673 {
1674 unsigned long long offset;
1675 struct mdp_superblock_1 *sb;
1676 int mustfree = 0;
1677
1678 if (!st->sb) {
1679 if (st->ss->load_super(st, fd, NULL))
1680 return; /* no error I hope... */
1681 mustfree = 1;
1682 }
1683 sb = st->sb;
1684
1685 offset = __le64_to_cpu(sb->super_offset);
1686 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1687 if (mustfree)
1688 free(sb);
1689 lseek64(fd, offset<<9, 0);
1690 }
1691
1692 static int write_bitmap1(struct supertype *st, int fd)
1693 {
1694 struct mdp_superblock_1 *sb = st->sb;
1695 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
1696 int rv = 0;
1697 void *buf;
1698 int towrite, n;
1699 struct align_fd afd;
1700
1701 init_afd(&afd, fd);
1702
1703 locate_bitmap1(st, fd);
1704
1705 if (posix_memalign(&buf, 4096, 4096))
1706 return -ENOMEM;
1707
1708 memset(buf, 0xff, 4096);
1709 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
1710
1711 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1712 towrite = (towrite+7) >> 3; /* bits to bytes */
1713 towrite += sizeof(bitmap_super_t);
1714 towrite = ROUND_UP(towrite, 512);
1715 while (towrite > 0) {
1716 n = towrite;
1717 if (n > 4096)
1718 n = 4096;
1719 n = awrite(&afd, buf, n);
1720 if (n > 0)
1721 towrite -= n;
1722 else
1723 break;
1724 memset(buf, 0xff, 4096);
1725 }
1726 fsync(fd);
1727 if (towrite)
1728 rv = -2;
1729
1730 free(buf);
1731 return rv;
1732 }
1733
1734 static void free_super1(struct supertype *st)
1735 {
1736 if (st->sb)
1737 free(st->sb);
1738 while (st->info) {
1739 struct devinfo *di = st->info;
1740 st->info = di->next;
1741 if (di->fd >= 0)
1742 close(di->fd);
1743 free(di);
1744 }
1745 st->sb = NULL;
1746 }
1747
1748 #ifndef MDASSEMBLE
1749 static int validate_geometry1(struct supertype *st, int level,
1750 int layout, int raiddisks,
1751 int *chunk, unsigned long long size,
1752 char *subdev, unsigned long long *freesize,
1753 int verbose)
1754 {
1755 unsigned long long ldsize;
1756 int fd;
1757
1758 if (level == LEVEL_CONTAINER) {
1759 if (verbose)
1760 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
1761 return 0;
1762 }
1763 if (chunk && *chunk == UnSet)
1764 *chunk = DEFAULT_CHUNK;
1765
1766 if (!subdev)
1767 return 1;
1768
1769 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1770 if (fd < 0) {
1771 if (verbose)
1772 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1773 subdev, strerror(errno));
1774 return 0;
1775 }
1776
1777 if (!get_dev_size(fd, subdev, &ldsize)) {
1778 close(fd);
1779 return 0;
1780 }
1781 close(fd);
1782
1783 *freesize = avail_size1(st, ldsize >> 9);
1784 return 1;
1785 }
1786 #endif /* MDASSEMBLE */
1787
1788 struct superswitch super1 = {
1789 #ifndef MDASSEMBLE
1790 .examine_super = examine_super1,
1791 .brief_examine_super = brief_examine_super1,
1792 .export_examine_super = export_examine_super1,
1793 .detail_super = detail_super1,
1794 .brief_detail_super = brief_detail_super1,
1795 .export_detail_super = export_detail_super1,
1796 .write_init_super = write_init_super1,
1797 .validate_geometry = validate_geometry1,
1798 .add_to_super = add_to_super1,
1799 #endif
1800 .match_home = match_home1,
1801 .uuid_from_super = uuid_from_super1,
1802 .getinfo_super = getinfo_super1,
1803 .container_content = container_content1,
1804 .update_super = update_super1,
1805 .init_super = init_super1,
1806 .store_super = store_super1,
1807 .compare_super = compare_super1,
1808 .load_super = load_super1,
1809 .match_metadata_desc = match_metadata_desc1,
1810 .avail_size = avail_size1,
1811 .add_internal_bitmap = add_internal_bitmap1,
1812 .locate_bitmap = locate_bitmap1,
1813 .write_bitmap = write_bitmap1,
1814 .free_super = free_super1,
1815 #if __BYTE_ORDER == BIG_ENDIAN
1816 .swapuuid = 0,
1817 #else
1818 .swapuuid = 1,
1819 #endif
1820 .name = "1.x",
1821 };