]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Assemble: allow --update=revert-reshape
[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 __u32 new_offset; /* signed number to add to data_offset in new
62 * layout. 0 == no-change. This can be
63 * different on each device in the array.
64 */
65
66 /* constant this-device information - 64 bytes */
67 __u64 data_offset; /* sector start of data, often 0 */
68 __u64 data_size; /* sectors in this device that can be used for data */
69 __u64 super_offset; /* sector start of this superblock */
70 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
71 __u32 dev_number; /* permanent identifier of this device - not role in raid */
72 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
73 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
74 __u8 devflags; /* per-device flags. Only one defined...*/
75 #define WriteMostly1 1 /* mask for writemostly flag in above */
76 /* bad block log. If there are any bad blocks the feature flag is set.
77 * if offset and size are non-zero, that space is reserved and available.
78 */
79 __u8 bblog_shift; /* shift from sectors to block size for badblocklist */
80 __u16 bblog_size; /* number of sectors reserved for badblocklist */
81 __u32 bblog_offset; /* sector offset from superblock to bblog, signed */
82
83 /* array state information - 64 bytes */
84 __u64 utime; /* 40 bits second, 24 btes microseconds */
85 __u64 events; /* incremented when superblock updated */
86 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
87 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
88 __u32 max_dev; /* size of dev_roles[] array to consider */
89 __u8 pad3[64-32]; /* set to 0 when writing */
90
91 /* device state information. Indexed by dev_number.
92 * 2 bytes per device
93 * Note there are no per-device state flags. State information is rolled
94 * into the 'roles' value. If a device is spare or faulty, then it doesn't
95 * have a meaningful role.
96 */
97 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
98 };
99
100 #define MAX_SB_SIZE 4096
101 /* bitmap super size is 256, but we round up to a sector for alignment */
102 #define BM_SUPER_SIZE 512
103 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
104 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
105 + sizeof(struct misc_dev_info))
106
107 struct misc_dev_info {
108 __u64 device_size;
109 };
110
111 /* feature_map bits */
112 #define MD_FEATURE_BITMAP_OFFSET 1
113 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
114 * must be honoured
115 */
116 #define MD_FEATURE_RESHAPE_ACTIVE 4
117 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
118 #define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
119 * active device with same 'role'.
120 * 'recovery_offset' is also set.
121 */
122 #define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
123 * of devices, but is going
124 * backwards anyway.
125 */
126 #define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
127 #define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
128 |MD_FEATURE_RECOVERY_OFFSET \
129 |MD_FEATURE_RESHAPE_ACTIVE \
130 |MD_FEATURE_BAD_BLOCKS \
131 |MD_FEATURE_REPLACEMENT \
132 |MD_FEATURE_RESHAPE_BACKWARDS \
133 |MD_FEATURE_NEW_OFFSET \
134 )
135
136 #ifndef offsetof
137 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
138 #endif
139 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
140 {
141 unsigned int disk_csum, csum;
142 unsigned long long newcsum;
143 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
144 unsigned int *isuper = (unsigned int*)sb;
145
146 /* make sure I can count... */
147 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
148 offsetof(struct mdp_superblock_1, utime) != 192 ||
149 sizeof(struct mdp_superblock_1) != 256) {
150 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
151 }
152
153 disk_csum = sb->sb_csum;
154 sb->sb_csum = 0;
155 newcsum = 0;
156 for (; size>=4; size -= 4 ) {
157 newcsum += __le32_to_cpu(*isuper);
158 isuper++;
159 }
160
161 if (size == 2)
162 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
163
164 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
165 sb->sb_csum = disk_csum;
166 return __cpu_to_le32(csum);
167 }
168
169 /*
170 * Information related to file descriptor used for aligned reads/writes.
171 * Cache the block size.
172 */
173 struct align_fd {
174 int fd;
175 int blk_sz;
176 };
177
178 static void init_afd(struct align_fd *afd, int fd)
179 {
180 afd->fd = fd;
181
182 if (ioctl(afd->fd, BLKSSZGET, &afd->blk_sz) != 0)
183 afd->blk_sz = 512;
184 }
185
186 static char abuf[4096+4096];
187 static int aread(struct align_fd *afd, void *buf, int len)
188 {
189 /* aligned read.
190 * On devices with a 4K sector size, we need to read
191 * the full sector and copy relevant bits into
192 * the buffer
193 */
194 int bsize, iosize;
195 char *b;
196 int n;
197
198 bsize = afd->blk_sz;
199
200 if (!bsize || bsize > 4096 || len > 4096) {
201 if (!bsize)
202 fprintf(stderr, "WARNING - aread() called with "
203 "invalid block size\n");
204 return -1;
205 }
206 b = ROUND_UP_PTR((char *)abuf, 4096);
207
208 for (iosize = 0; iosize < len; iosize += bsize)
209 ;
210 n = read(afd->fd, b, iosize);
211 if (n <= 0)
212 return n;
213 lseek(afd->fd, len - n, 1);
214 if (n > len)
215 n = len;
216 memcpy(buf, b, n);
217 return n;
218 }
219
220 static int awrite(struct align_fd *afd, void *buf, int len)
221 {
222 /* aligned write.
223 * On devices with a 4K sector size, we need to write
224 * the full sector. We pre-read if the sector is larger
225 * than the write.
226 * The address must be sector-aligned.
227 */
228 int bsize, iosize;
229 char *b;
230 int n;
231
232 bsize = afd->blk_sz;
233 if (!bsize || bsize > 4096 || len > 4096) {
234 if (!bsize)
235 fprintf(stderr, "WARNING - awrite() called with "
236 "invalid block size\n");
237 return -1;
238 }
239 b = ROUND_UP_PTR((char *)abuf, 4096);
240
241 for (iosize = 0; iosize < len ; iosize += bsize)
242 ;
243
244 if (len != iosize) {
245 n = read(afd->fd, b, iosize);
246 if (n <= 0)
247 return n;
248 lseek(afd->fd, -n, 1);
249 }
250
251 memcpy(b, buf, len);
252 n = write(afd->fd, b, iosize);
253 if (n <= 0)
254 return n;
255 lseek(afd->fd, len - n, 1);
256 return len;
257 }
258
259 #ifndef MDASSEMBLE
260 static void examine_super1(struct supertype *st, char *homehost)
261 {
262 struct mdp_superblock_1 *sb = st->sb;
263 time_t atime;
264 unsigned int d;
265 int role;
266 int delta_extra = 0;
267 int i;
268 char *c;
269 int l = homehost ? strlen(homehost) : 0;
270 int layout;
271 unsigned long long sb_offset;
272 struct mdinfo info;
273
274 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
275 printf(" Version : 1");
276 sb_offset = __le64_to_cpu(sb->super_offset);
277 if (sb_offset <= 4)
278 printf(".1\n");
279 else if (sb_offset <= 8)
280 printf(".2\n");
281 else
282 printf(".0\n");
283 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
284 printf(" Array UUID : ");
285 for (i=0; i<16; i++) {
286 if ((i&3)==0 && i != 0) printf(":");
287 printf("%02x", sb->set_uuid[i]);
288 }
289 printf("\n");
290 printf(" Name : %.32s", sb->set_name);
291 if (l > 0 && l < 32 &&
292 sb->set_name[l] == ':' &&
293 strncmp(sb->set_name, homehost, l) == 0)
294 printf(" (local to host %s)", homehost);
295 printf("\n");
296 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
297 printf(" Creation Time : %.24s\n", ctime(&atime));
298 c=map_num(pers, __le32_to_cpu(sb->level));
299 printf(" Raid Level : %s\n", c?c:"-unknown-");
300 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
301 printf("\n");
302 printf(" Avail Dev Size : %llu%s\n",
303 (unsigned long long)__le64_to_cpu(sb->data_size),
304 human_size(__le64_to_cpu(sb->data_size)<<9));
305 if (__le32_to_cpu(sb->level) > 0) {
306 int ddsks = 0, ddsks_denom = 1;
307 switch(__le32_to_cpu(sb->level)) {
308 case 1: ddsks=1;break;
309 case 4:
310 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
311 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
312 case 10:
313 layout = __le32_to_cpu(sb->layout);
314 ddsks = __le32_to_cpu(sb->raid_disks);
315 ddsks_denom = (layout&255) * ((layout>>8)&255);
316 }
317 if (ddsks) {
318 long long asize = __le64_to_cpu(sb->size);
319 asize = (asize << 9) * ddsks / ddsks_denom;
320 printf(" Array Size : %llu%s\n",
321 asize >> 10, human_size(asize));
322 }
323 if (sb->size != sb->data_size)
324 printf(" Used Dev Size : %llu%s\n",
325 (unsigned long long)__le64_to_cpu(sb->size),
326 human_size(__le64_to_cpu(sb->size)<<9));
327 }
328 if (sb->data_offset)
329 printf(" Data Offset : %llu sectors\n",
330 (unsigned long long)__le64_to_cpu(sb->data_offset));
331 if (sb->new_offset &&
332 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
333 unsigned long long offset = __le64_to_cpu(sb->data_offset);
334 offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
335 printf(" New Offset : %llu sectors\n", offset);
336 }
337 printf(" Super Offset : %llu sectors\n",
338 (unsigned long long)__le64_to_cpu(sb->super_offset));
339 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
340 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
341
342 st->ss->getinfo_super(st, &info, NULL);
343 if (info.space_after != 1 &&
344 !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
345 printf(" Unused Space : before=%llu sectors, after=%llu sectors\n",
346 info.space_before, info.space_after);
347
348 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
349 printf(" Device UUID : ");
350 for (i=0; i<16; i++) {
351 if ((i&3)==0 && i != 0) printf(":");
352 printf("%02x", sb->device_uuid[i]);
353 }
354 printf("\n");
355 printf("\n");
356 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
357 printf("Internal Bitmap : %ld sectors from superblock\n",
358 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
359 }
360 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
361 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
362 human_size(__le64_to_cpu(sb->reshape_position)<<9));
363 if (__le32_to_cpu(sb->delta_disks)) {
364 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
365 printf(" (%d->%d)\n",
366 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
367 __le32_to_cpu(sb->raid_disks));
368 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
369 delta_extra = -__le32_to_cpu(sb->delta_disks);
370 }
371 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
372 c = map_num(pers, __le32_to_cpu(sb->new_level));
373 printf(" New Level : %s\n", c?c:"-unknown-");
374 }
375 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
376 if (__le32_to_cpu(sb->level) == 5) {
377 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
378 printf(" New Layout : %s\n", c?c:"-unknown-");
379 }
380 if (__le32_to_cpu(sb->level) == 6) {
381 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
382 printf(" New Layout : %s\n", c?c:"-unknown-");
383 }
384 if (__le32_to_cpu(sb->level) == 10) {
385 printf(" New Layout :");
386 print_r10_layout(__le32_to_cpu(sb->new_layout));
387 printf("\n");
388 }
389 }
390 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
391 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
392 printf("\n");
393 }
394 if (sb->devflags) {
395 printf(" Flags :");
396 if (sb->devflags & WriteMostly1)
397 printf(" write-mostly");
398 printf("\n");
399 }
400
401 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
402 printf(" Update Time : %.24s\n", ctime(&atime));
403
404 if (sb->bblog_size && sb->bblog_offset) {
405 printf(" Bad Block Log : %d entries available at offset %ld sectors",
406 __le16_to_cpu(sb->bblog_size)*512/8,
407 (long)(int32_t)__le32_to_cpu(sb->bblog_offset));
408 if (sb->feature_map &
409 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
410 printf(" - bad blocks present.");
411 printf("\n");
412 }
413
414
415 if (calc_sb_1_csum(sb) == sb->sb_csum)
416 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
417 else
418 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
419 __le32_to_cpu(calc_sb_1_csum(sb)));
420 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
421 printf("\n");
422 if (__le32_to_cpu(sb->level) == 5) {
423 c = map_num(r5layout, __le32_to_cpu(sb->layout));
424 printf(" Layout : %s\n", c?c:"-unknown-");
425 }
426 if (__le32_to_cpu(sb->level) == 6) {
427 c = map_num(r6layout, __le32_to_cpu(sb->layout));
428 printf(" Layout : %s\n", c?c:"-unknown-");
429 }
430 if (__le32_to_cpu(sb->level) == 10) {
431 int lo = __le32_to_cpu(sb->layout);
432 printf(" Layout :");
433 print_r10_layout(lo);
434 printf("\n");
435 }
436 switch(__le32_to_cpu(sb->level)) {
437 case 0:
438 case 4:
439 case 5:
440 case 6:
441 case 10:
442 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
443 break;
444 case -1:
445 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
446 break;
447 default: break;
448 }
449 printf("\n");
450 #if 0
451 /* This turns out to just be confusing */
452 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
453 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
454 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
455 break;
456 for (d=0; d < i; d++) {
457 int role = __le16_to_cpu(sb->dev_roles[d]);
458 if (d) printf(", ");
459 if (role == 0xffff) printf("empty");
460 else if(role == 0xfffe) printf("failed");
461 else printf("%d", role);
462 }
463 printf(")\n");
464 #endif
465 printf(" Device Role : ");
466 d = __le32_to_cpu(sb->dev_number);
467 if (d < __le32_to_cpu(sb->max_dev))
468 role = __le16_to_cpu(sb->dev_roles[d]);
469 else
470 role = 0xFFFF;
471 if (role >= 0xFFFE)
472 printf("spare\n");
473 else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
474 printf("Replacement device %d\n", role);
475 else
476 printf("Active device %d\n", role);
477
478 printf(" Array State : ");
479 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
480 int cnt = 0;
481 unsigned int i;
482 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
483 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
484 if (role == d)
485 cnt++;
486 }
487 if (cnt == 2)
488 printf("R");
489 else if (cnt == 1)
490 printf("A");
491 else if (cnt == 0)
492 printf(".");
493 else
494 printf("?");
495 }
496 #if 0
497 /* This is confusing too */
498 faulty = 0;
499 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
500 int role = __le16_to_cpu(sb->dev_roles[i]);
501 if (role == 0xFFFE)
502 faulty++;
503 }
504 if (faulty) printf(" %d failed", faulty);
505 #endif
506 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
507 printf("\n");
508 }
509
510
511 static void brief_examine_super1(struct supertype *st, int verbose)
512 {
513 struct mdp_superblock_1 *sb = st->sb;
514 int i;
515 unsigned long long sb_offset;
516 char *nm;
517 char *c=map_num(pers, __le32_to_cpu(sb->level));
518
519 nm = strchr(sb->set_name, ':');
520 if (nm)
521 nm++;
522 else if (sb->set_name[0])
523 nm = sb->set_name;
524 else
525 nm = NULL;
526
527 printf("ARRAY ");
528 if (nm) {
529 printf("/dev/md/");
530 print_escape(nm);
531 putchar(' ');
532 }
533 if (verbose && c)
534 printf(" level=%s", c);
535 sb_offset = __le64_to_cpu(sb->super_offset);
536 if (sb_offset <= 4)
537 printf(" metadata=1.1 ");
538 else if (sb_offset <= 8)
539 printf(" metadata=1.2 ");
540 else
541 printf(" metadata=1.0 ");
542 if (verbose)
543 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
544 printf("UUID=");
545 for (i=0; i<16; i++) {
546 if ((i&3)==0 && i != 0) printf(":");
547 printf("%02x", sb->set_uuid[i]);
548 }
549 if (sb->set_name[0]) {
550 printf(" name=");
551 print_quoted(sb->set_name);
552 }
553 printf("\n");
554 }
555
556 static void export_examine_super1(struct supertype *st)
557 {
558 struct mdp_superblock_1 *sb = st->sb;
559 int i;
560 int len = 32;
561 int layout;
562
563 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
564 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
565 for (i=0; i<32; i++)
566 if (sb->set_name[i] == '\n' ||
567 sb->set_name[i] == '\0') {
568 len = i;
569 break;
570 }
571 if (len)
572 printf("MD_NAME=%.*s\n", len, sb->set_name);
573 if (__le32_to_cpu(sb->level) > 0) {
574 int ddsks = 0, ddsks_denom = 1;
575 switch(__le32_to_cpu(sb->level)) {
576 case 1: ddsks=1;break;
577 case 4:
578 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
579 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
580 case 10:
581 layout = __le32_to_cpu(sb->layout);
582 ddsks = __le32_to_cpu(sb->raid_disks);
583 ddsks_denom = (layout&255) * ((layout>>8)&255);
584 }
585 if (ddsks) {
586 long long asize = __le64_to_cpu(sb->size);
587 asize = (asize << 9) * ddsks / ddsks_denom;
588 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize,JEDEC));
589 }
590 }
591 printf("MD_UUID=");
592 for (i=0; i<16; i++) {
593 if ((i&3)==0 && i != 0) printf(":");
594 printf("%02x", sb->set_uuid[i]);
595 }
596 printf("\n");
597 printf("MD_UPDATE_TIME=%llu\n",
598 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
599 printf("MD_DEV_UUID=");
600 for (i=0; i<16; i++) {
601 if ((i&3)==0 && i != 0) printf(":");
602 printf("%02x", sb->device_uuid[i]);
603 }
604 printf("\n");
605 printf("MD_EVENTS=%llu\n",
606 (unsigned long long)__le64_to_cpu(sb->events));
607 }
608
609 static int copy_metadata1(struct supertype *st, int from, int to)
610 {
611 /* Read superblock. If it looks good, write it out.
612 * Then if a bitmap is present, copy that.
613 * And if a bad-block-list is present, copy that too.
614 */
615 void *buf;
616 unsigned long long dsize, sb_offset;
617 const int bufsize = 4*1024;
618 struct mdp_superblock_1 super, *sb;
619
620 if (posix_memalign(&buf, 4096, bufsize) != 0)
621 return 1;
622
623 if (!get_dev_size(from, NULL, &dsize))
624 goto err;
625
626 dsize >>= 9;
627 if (dsize < 24)
628 goto err;
629 switch(st->minor_version) {
630 case 0:
631 sb_offset = dsize;
632 sb_offset -= 8*2;
633 sb_offset &= ~(4*2-1);
634 break;
635 case 1:
636 sb_offset = 0;
637 break;
638 case 2:
639 sb_offset = 4*2;
640 break;
641 default:
642 goto err;
643 }
644
645 if (lseek64(from, sb_offset << 9, 0) < 0LL)
646 goto err;
647 if (read(from, buf, bufsize) != bufsize)
648 goto err;
649
650 sb = buf;
651 super = *sb; // save most of sb for when we reuse buf
652
653 if (__le32_to_cpu(super.magic) != MD_SB_MAGIC ||
654 __le32_to_cpu(super.major_version) != 1 ||
655 __le64_to_cpu(super.super_offset) != sb_offset ||
656 calc_sb_1_csum(sb) != super.sb_csum)
657 goto err;
658
659 if (lseek64(to, sb_offset << 9, 0) < 0LL)
660 goto err;
661 if (write(to, buf, bufsize) != bufsize)
662 goto err;
663
664 if (super.feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
665 unsigned long long bitmap_offset = sb_offset;
666 int bytes = 4096; // just an estimate.
667 int written = 0;
668 struct align_fd afrom, ato;
669
670 init_afd(&afrom, from);
671 init_afd(&ato, to);
672
673 bitmap_offset += (int32_t)__le32_to_cpu(super.bitmap_offset);
674
675 if (lseek64(from, bitmap_offset<<9, 0) < 0)
676 goto err;
677 if (lseek64(to, bitmap_offset<<9, 0) < 0)
678 goto err;
679
680 for (written = 0; written < bytes ; ) {
681 int n = bytes - written;
682 if (n > 4096)
683 n = 4096;
684 if (aread(&afrom, buf, n) != n)
685 goto err;
686 if (written == 0) {
687 /* have the header, can calculate
688 * correct bitmap bytes */
689 bitmap_super_t *bms;
690 int bits;
691 bms = (void*)buf;
692 bits = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
693 bytes = (bits+7) >> 3;
694 bytes += sizeof(bitmap_super_t);
695 bytes = ROUND_UP(bytes, 512);
696 if (n > bytes)
697 n = bytes;
698 }
699 if (awrite(&ato, buf, n) != n)
700 goto err;
701 written += n;
702 }
703 }
704
705 if (super.bblog_size != 0 &&
706 __le32_to_cpu(super.bblog_size) <= 100 &&
707 super.bblog_offset != 0 &&
708 (super.feature_map & __le32_to_cpu(MD_FEATURE_BAD_BLOCKS))) {
709 /* There is a bad block log */
710 unsigned long long bb_offset = sb_offset;
711 int bytes = __le32_to_cpu(super.bblog_size) * 512;
712 int written = 0;
713 struct align_fd afrom, ato;
714
715 init_afd(&afrom, from);
716 init_afd(&ato, to);
717
718 bb_offset += (int32_t)__le32_to_cpu(super.bblog_offset);
719
720 if (lseek64(from, bb_offset<<9, 0) < 0)
721 goto err;
722 if (lseek64(to, bb_offset<<9, 0) < 0)
723 goto err;
724
725 for (written = 0; written < bytes ; ) {
726 int n = bytes - written;
727 if (n > 4096)
728 n = 4096;
729 if (aread(&afrom, buf, n) != n)
730 goto err;
731
732 if (awrite(&ato, buf, n) != n)
733 goto err;
734 written += n;
735 }
736 }
737
738 free(buf);
739 return 0;
740
741 err:
742 free(buf);
743 return 1;
744 }
745
746 static void detail_super1(struct supertype *st, char *homehost)
747 {
748 struct mdp_superblock_1 *sb = st->sb;
749 int i;
750 int l = homehost ? strlen(homehost) : 0;
751
752 printf(" Name : %.32s", sb->set_name);
753 if (l > 0 && l < 32 &&
754 sb->set_name[l] == ':' &&
755 strncmp(sb->set_name, homehost, l) == 0)
756 printf(" (local to host %s)", homehost);
757 printf("\n UUID : ");
758 for (i=0; i<16; i++) {
759 if ((i&3)==0 && i != 0) printf(":");
760 printf("%02x", sb->set_uuid[i]);
761 }
762 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
763 }
764
765 static void brief_detail_super1(struct supertype *st)
766 {
767 struct mdp_superblock_1 *sb = st->sb;
768 int i;
769
770 if (sb->set_name[0]) {
771 printf(" name=");
772 print_quoted(sb->set_name);
773 }
774 printf(" UUID=");
775 for (i=0; i<16; i++) {
776 if ((i&3)==0 && i != 0) printf(":");
777 printf("%02x", sb->set_uuid[i]);
778 }
779 }
780
781 static void export_detail_super1(struct supertype *st)
782 {
783 struct mdp_superblock_1 *sb = st->sb;
784 int i;
785 int len = 32;
786
787 for (i=0; i<32; i++)
788 if (sb->set_name[i] == '\n' ||
789 sb->set_name[i] == '\0') {
790 len = i;
791 break;
792 }
793 if (len)
794 printf("MD_NAME=%.*s\n", len, sb->set_name);
795 }
796
797 static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
798 {
799 struct mdp_superblock_1 *sb = st->sb;
800 unsigned long long offset;
801 int size;
802 __u64 *bbl, *bbp;
803 int i;
804
805 if (!sb->bblog_size || __le32_to_cpu(sb->bblog_size) > 100
806 || !sb->bblog_offset){
807 printf("No bad-blocks list configured on %s\n", devname);
808 return 0;
809 }
810 if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
811 == 0) {
812 printf("Bad-blocks list is empty in %s\n", devname);
813 return 0;
814 }
815
816 size = __le32_to_cpu(sb->bblog_size)* 512;
817 if (posix_memalign((void**)&bbl, 4096, size) != 0) {
818 pr_err("%s could not allocate badblocks list\n", __func__);
819 return 0;
820 }
821 offset = __le64_to_cpu(sb->super_offset) +
822 (int)__le32_to_cpu(sb->bblog_offset);
823 offset <<= 9;
824 if (lseek64(fd, offset, 0) < 0) {
825 pr_err("Cannot seek to bad-blocks list\n");
826 return 1;
827 }
828 if (read(fd, bbl, size) != size) {
829 pr_err("Cannot read bad-blocks list\n");
830 return 1;
831 }
832 /* 64bits per entry. 10 bits is block-count, 54 bits is block
833 * offset. Blocks are sectors unless bblog->shift makes them bigger
834 */
835 bbp = (__u64*)bbl;
836 printf("Bad-blocks on %s:\n", devname);
837 for (i = 0; i < size/8; i++, bbp++) {
838 __u64 bb = __le64_to_cpu(*bbp);
839 int count = bb & 0x3ff;
840 unsigned long long sector = bb >> 10;
841
842 if (bb + 1 == 0)
843 break;
844
845 sector <<= sb->bblog_shift;
846 count <<= sb->bblog_shift;
847
848 printf("%20llu for %d sectors\n", sector, count);
849 }
850 return 0;
851 }
852
853 #endif
854
855 static int match_home1(struct supertype *st, char *homehost)
856 {
857 struct mdp_superblock_1 *sb = st->sb;
858 int l = homehost ? strlen(homehost) : 0;
859
860 return (l > 0 && l < 32 &&
861 sb->set_name[l] == ':' &&
862 strncmp(sb->set_name, homehost, l) == 0);
863 }
864
865 static void uuid_from_super1(struct supertype *st, int uuid[4])
866 {
867 struct mdp_superblock_1 *super = st->sb;
868 char *cuuid = (char*)uuid;
869 int i;
870 for (i=0; i<16; i++)
871 cuuid[i] = super->set_uuid[i];
872 }
873
874 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
875 {
876 struct mdp_superblock_1 *sb = st->sb;
877 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
878 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
879 int working = 0;
880 unsigned int i;
881 unsigned int role;
882 unsigned int map_disks = info->array.raid_disks;
883 unsigned long long super_offset;
884 unsigned long long data_size;
885
886 memset(info, 0, sizeof(*info));
887 info->array.major_version = 1;
888 info->array.minor_version = st->minor_version;
889 info->array.patch_version = 0;
890 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
891 info->array.level = __le32_to_cpu(sb->level);
892 info->array.layout = __le32_to_cpu(sb->layout);
893 info->array.md_minor = -1;
894 info->array.ctime = __le64_to_cpu(sb->ctime);
895 info->array.utime = __le64_to_cpu(sb->utime);
896 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
897 info->array.state =
898 (__le64_to_cpu(sb->resync_offset) == MaxSector)
899 ? 1 : 0;
900
901 info->data_offset = __le64_to_cpu(sb->data_offset);
902 info->component_size = __le64_to_cpu(sb->size);
903 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
904 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
905
906 info->disk.major = 0;
907 info->disk.minor = 0;
908 info->disk.number = __le32_to_cpu(sb->dev_number);
909 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
910 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
911 role = 0xfffe;
912 else
913 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
914
915 super_offset = __le64_to_cpu(sb->super_offset);
916 data_size = __le64_to_cpu(sb->size);
917 if (info->data_offset < super_offset) {
918 unsigned long long end;
919 info->space_before = info->data_offset;
920 end = super_offset;
921
922 if (sb->bblog_offset && sb->bblog_size) {
923 unsigned long long bboffset = super_offset;
924 bboffset += (int32_t)__le32_to_cpu(sb->bblog_offset);
925 if (bboffset < end)
926 end = bboffset;
927 }
928
929 if (super_offset + info->bitmap_offset < end)
930 end = super_offset + info->bitmap_offset;
931
932 if (info->data_offset + data_size < end)
933 info->space_after = end - data_size - info->data_offset;
934 else
935 info->space_after = 0;
936 } else {
937 unsigned long long earliest;
938 earliest = super_offset + (32+4)*2; /* match kernel */
939 if (info->bitmap_offset > 0) {
940 unsigned long long bmend = info->bitmap_offset;
941 unsigned long long size = __le64_to_cpu(bsb->sync_size);
942 size /= __le32_to_cpu(bsb->chunksize) >> 9;
943 size = (size + 7) >> 3;
944 size += sizeof(bitmap_super_t);
945 size = ROUND_UP(size, 4096);
946 size /= 512;
947 bmend += size;
948 if (bmend > earliest)
949 bmend = earliest;
950 }
951 if (sb->bblog_offset && sb->bblog_size) {
952 unsigned long long bbend = super_offset;
953 bbend += (int32_t)__le32_to_cpu(sb->bblog_offset);
954 bbend += __le32_to_cpu(sb->bblog_size);
955 if (bbend > earliest)
956 earliest = bbend;
957 }
958 if (earliest < info->data_offset)
959 info->space_before = info->data_offset - earliest;
960 else
961 info->space_before = 0;
962 info->space_after = misc->device_size - data_size - info->data_offset;
963 }
964 if (info->space_before == 0 && info->space_after == 0) {
965 /* It will look like we don't support data_offset changes,
966 * be we do - it's just that there is no room.
967 * A change that reduced the number of devices should
968 * still be allowed, so set the otherwise useless value of '1'
969 */
970 info->space_after = 1;
971 }
972
973 info->disk.raid_disk = -1;
974 switch(role) {
975 case 0xFFFF:
976 info->disk.state = 0; /* spare: not active, not sync, not faulty */
977 break;
978 case 0xFFFE:
979 info->disk.state = 1; /* faulty */
980 break;
981 default:
982 info->disk.state = 6; /* active and in sync */
983 info->disk.raid_disk = role;
984 }
985 if (sb->devflags & WriteMostly1)
986 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
987 info->events = __le64_to_cpu(sb->events);
988 sprintf(info->text_version, "1.%d", st->minor_version);
989 info->safe_mode_delay = 200;
990
991 memcpy(info->uuid, sb->set_uuid, 16);
992
993 strncpy(info->name, sb->set_name, 32);
994 info->name[32] = 0;
995
996 if ((__le32_to_cpu(sb->feature_map)&MD_FEATURE_REPLACEMENT)) {
997 info->disk.state &= ~(1 << MD_DISK_SYNC);
998 info->disk.state |= 1 << MD_DISK_REPLACEMENT;
999 }
1000
1001
1002 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
1003 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
1004 else
1005 info->recovery_start = MaxSector;
1006
1007 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1008 info->reshape_active = 1;
1009 if (info->array.level == 10)
1010 info->reshape_active |= RESHAPE_NO_BACKUP;
1011 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
1012 info->new_level = __le32_to_cpu(sb->new_level);
1013 info->delta_disks = __le32_to_cpu(sb->delta_disks);
1014 info->new_layout = __le32_to_cpu(sb->new_layout);
1015 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
1016 if (info->delta_disks < 0)
1017 info->array.raid_disks -= info->delta_disks;
1018 } else
1019 info->reshape_active = 0;
1020
1021 info->recovery_blocked = info->reshape_active;
1022
1023 if (map)
1024 for (i=0; i<map_disks; i++)
1025 map[i] = 0;
1026 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
1027 role = __le16_to_cpu(sb->dev_roles[i]);
1028 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
1029 working++;
1030 if (map && role < map_disks)
1031 map[role] = 1;
1032 }
1033 }
1034
1035 info->array.working_disks = working;
1036 }
1037
1038 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
1039 {
1040 struct mdinfo *info;
1041
1042 if (subarray)
1043 return NULL;
1044
1045 info = xmalloc(sizeof(*info));
1046 getinfo_super1(st, info, NULL);
1047 return info;
1048 }
1049
1050 static int update_super1(struct supertype *st, struct mdinfo *info,
1051 char *update,
1052 char *devname, int verbose,
1053 int uuid_set, char *homehost)
1054 {
1055 /* NOTE: for 'assemble' and 'force' we need to return non-zero
1056 * if any change was made. For others, the return value is
1057 * ignored.
1058 */
1059 int rv = 0;
1060 struct mdp_superblock_1 *sb = st->sb;
1061
1062 if (strcmp(update, "homehost") == 0 &&
1063 homehost) {
1064 /* Note that 'homehost' is special as it is really
1065 * a "name" update.
1066 */
1067 char *c;
1068 update = "name";
1069 c = strchr(sb->set_name, ':');
1070 if (c)
1071 strncpy(info->name, c+1, 31 - (c-sb->set_name));
1072 else
1073 strncpy(info->name, sb->set_name, 32);
1074 info->name[32] = 0;
1075 }
1076
1077 if (strcmp(update, "force-one")==0) {
1078 /* Not enough devices for a working array,
1079 * so bring this one up-to-date
1080 */
1081 if (sb->events != __cpu_to_le64(info->events))
1082 rv = 1;
1083 sb->events = __cpu_to_le64(info->events);
1084 } else if (strcmp(update, "force-array")==0) {
1085 /* Degraded array and 'force' requests to
1086 * maybe need to mark it 'clean'.
1087 */
1088 switch(__le32_to_cpu(sb->level)) {
1089 case 5: case 4: case 6:
1090 /* need to force clean */
1091 if (sb->resync_offset != MaxSector)
1092 rv = 1;
1093 sb->resync_offset = MaxSector;
1094 }
1095 } else if (strcmp(update, "assemble")==0) {
1096 int d = info->disk.number;
1097 int want;
1098 if (info->disk.state & (1<<MD_DISK_ACTIVE))
1099 want = info->disk.raid_disk;
1100 else
1101 want = 0xFFFF;
1102 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
1103 sb->dev_roles[d] = __cpu_to_le16(want);
1104 rv = 1;
1105 }
1106 if (info->reshape_active &&
1107 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1108 info->delta_disks >= 0 &&
1109 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
1110 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1111 rv = 1;
1112 }
1113 if (info->reshape_active &&
1114 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1115 info->delta_disks < 0 &&
1116 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
1117 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1118 rv = 1;
1119 }
1120 } else if (strcmp(update, "linear-grow-new") == 0) {
1121 unsigned int i;
1122 int rfd, fd;
1123 unsigned int max = __le32_to_cpu(sb->max_dev);
1124
1125 for (i=0 ; i < max ; i++)
1126 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
1127 break;
1128 sb->dev_number = __cpu_to_le32(i);
1129 info->disk.number = i;
1130 if (max >= __le32_to_cpu(sb->max_dev))
1131 sb->max_dev = __cpu_to_le32(max+1);
1132
1133 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1134 read(rfd, sb->device_uuid, 16) != 16) {
1135 __u32 r[4] = {random(), random(), random(), random()};
1136 memcpy(sb->device_uuid, r, 16);
1137 }
1138 if (rfd >= 0)
1139 close(rfd);
1140
1141 sb->dev_roles[i] =
1142 __cpu_to_le16(info->disk.raid_disk);
1143
1144 fd = open(devname, O_RDONLY);
1145 if (fd >= 0) {
1146 unsigned long long ds;
1147 get_dev_size(fd, devname, &ds);
1148 close(fd);
1149 ds >>= 9;
1150 if (__le64_to_cpu(sb->super_offset) <
1151 __le64_to_cpu(sb->data_offset)) {
1152 sb->data_size = __cpu_to_le64(
1153 ds - __le64_to_cpu(sb->data_offset));
1154 } else {
1155 ds -= 8*2;
1156 ds &= ~(unsigned long long)(4*2-1);
1157 sb->super_offset = __cpu_to_le64(ds);
1158 sb->data_size = __cpu_to_le64(
1159 ds - __le64_to_cpu(sb->data_offset));
1160 }
1161 }
1162 } else if (strcmp(update, "linear-grow-update") == 0) {
1163 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
1164 sb->dev_roles[info->disk.number] =
1165 __cpu_to_le16(info->disk.raid_disk);
1166 } else if (strcmp(update, "resync") == 0) {
1167 /* make sure resync happens */
1168 sb->resync_offset = 0ULL;
1169 } else if (strcmp(update, "uuid") == 0) {
1170 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
1171
1172 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1173 struct bitmap_super_s *bm;
1174 bm = (struct bitmap_super_s*)(st->sb+MAX_SB_SIZE);
1175 memcpy(bm->uuid, sb->set_uuid, 16);
1176 }
1177 } else if (strcmp(update, "no-bitmap") == 0) {
1178 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1179 } else if (strcmp(update, "bbl") == 0) {
1180 /* only possible if there is room after the bitmap, or if
1181 * there is no bitmap
1182 */
1183 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1184 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
1185 long bitmap_offset = (long)(int32_t)__le32_to_cpu(sb->bitmap_offset);
1186 long bm_sectors = 0;
1187 long space;
1188
1189 #ifndef MDASSEMBLE
1190 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1191 struct bitmap_super_s *bsb;
1192 bsb = (struct bitmap_super_s *)(((char*)sb)+MAX_SB_SIZE);
1193 bm_sectors = bitmap_sectors(bsb);
1194 }
1195 #endif
1196 if (sb_offset < data_offset) {
1197 /* 1.1 or 1.2. Put bbl after bitmap leaving at least 32K
1198 */
1199 long bb_offset;
1200 bb_offset = sb_offset + 8;
1201 if (bm_sectors && bitmap_offset > 0)
1202 bb_offset = bitmap_offset + bm_sectors;
1203 while (bb_offset < (long)sb_offset + 8 + 32*2
1204 && bb_offset + 8+8 <= (long)data_offset)
1205 /* too close to bitmap, and room to grow */
1206 bb_offset += 8;
1207 if (bb_offset + 8 <= (long)data_offset) {
1208 sb->bblog_size = __cpu_to_le16(8);
1209 sb->bblog_offset = __cpu_to_le32(bb_offset);
1210 }
1211 } else {
1212 /* 1.0 - Put bbl just before super block */
1213 if (bm_sectors && bitmap_offset < 0)
1214 space = -bitmap_offset - bm_sectors;
1215 else
1216 space = sb_offset - data_offset -
1217 __le64_to_cpu(sb->data_size);
1218 if (space >= 8) {
1219 sb->bblog_size = __cpu_to_le16(8);
1220 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1221 }
1222 }
1223 } else if (strcmp(update, "no-bbl") == 0) {
1224 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
1225 pr_err("Cannot remove active bbl from %s\n",devname);
1226 else {
1227 sb->bblog_size = 0;
1228 sb->bblog_shift = 0;
1229 sb->bblog_offset = 0;
1230 }
1231 } else if (strcmp(update, "name") == 0) {
1232 if (info->name[0] == 0)
1233 sprintf(info->name, "%d", info->array.md_minor);
1234 memset(sb->set_name, 0, sizeof(sb->set_name));
1235 if (homehost &&
1236 strchr(info->name, ':') == NULL &&
1237 strlen(homehost)+1+strlen(info->name) < 32) {
1238 strcpy(sb->set_name, homehost);
1239 strcat(sb->set_name, ":");
1240 strcat(sb->set_name, info->name);
1241 } else
1242 strcpy(sb->set_name, info->name);
1243 } else if (strcmp(update, "devicesize") == 0 &&
1244 __le64_to_cpu(sb->super_offset) <
1245 __le64_to_cpu(sb->data_offset)) {
1246 /* set data_size to device size less data_offset */
1247 struct misc_dev_info *misc = (struct misc_dev_info*)
1248 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
1249 printf("Size was %llu\n", (unsigned long long)
1250 __le64_to_cpu(sb->data_size));
1251 sb->data_size = __cpu_to_le64(
1252 misc->device_size - __le64_to_cpu(sb->data_offset));
1253 printf("Size is %llu\n", (unsigned long long)
1254 __le64_to_cpu(sb->data_size));
1255 } else if (strcmp(update, "revert-reshape") == 0) {
1256 rv = -2;
1257 if (!(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
1258 pr_err("No active reshape to revert on %s\n",
1259 devname);
1260 else {
1261 rv = 0;
1262 __u32 temp;
1263 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) +
1264 __le32_to_cpu(sb->delta_disks));
1265 if (sb->delta_disks == 0)
1266 sb->feature_map ^= __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1267 else
1268 sb->delta_disks = __cpu_to_le32(-__le32_to_cpu(sb->delta_disks));
1269
1270 temp = sb->new_layout;
1271 sb->new_layout = sb->layout;
1272 sb->layout = temp;
1273
1274 temp = sb->new_chunk;
1275 sb->new_chunk = sb->chunksize;
1276 sb->chunksize = temp;
1277
1278 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_NEW_OFFSET)) {
1279 sb->data_offset = __cpu_to_le64(__le64_to_cpu(sb->data_offset) +
1280 (long)(int32_t)__le32_to_cpu(sb->new_offset));
1281 sb->new_offset = __cpu_to_le32(-(int32_t)__le32_to_cpu(sb->new_offset));
1282 }
1283 }
1284 } else if (strcmp(update, "_reshape_progress")==0)
1285 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1286 else if (strcmp(update, "writemostly")==0)
1287 sb->devflags |= WriteMostly1;
1288 else if (strcmp(update, "readwrite")==0)
1289 sb->devflags &= ~WriteMostly1;
1290 else
1291 rv = -1;
1292
1293 sb->sb_csum = calc_sb_1_csum(sb);
1294 return rv;
1295 }
1296
1297 static int init_super1(struct supertype *st, mdu_array_info_t *info,
1298 unsigned long long size, char *name, char *homehost,
1299 int *uuid, unsigned long long data_offset)
1300 {
1301 struct mdp_superblock_1 *sb;
1302 int spares;
1303 int rfd;
1304 char defname[10];
1305 int sbsize;
1306
1307 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1308 pr_err("%s could not allocate superblock\n", __func__);
1309 return 0;
1310 }
1311 memset(sb, 0, SUPER1_SIZE);
1312
1313 st->sb = sb;
1314 if (info == NULL) {
1315 /* zeroing superblock */
1316 return 0;
1317 }
1318
1319 spares = info->working_disks - info->active_disks;
1320 if (info->raid_disks + spares > MAX_DEVS) {
1321 pr_err("too many devices requested: %d+%d > %d\n",
1322 info->raid_disks , spares, MAX_DEVS);
1323 return 0;
1324 }
1325
1326 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1327 sb->major_version = __cpu_to_le32(1);
1328 sb->feature_map = 0;
1329 sb->pad0 = 0;
1330
1331 if (uuid)
1332 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
1333 else {
1334 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1335 read(rfd, sb->set_uuid, 16) != 16) {
1336 __u32 r[4] = {random(), random(), random(), random()};
1337 memcpy(sb->set_uuid, r, 16);
1338 }
1339 if (rfd >= 0) close(rfd);
1340 }
1341
1342 if (name == NULL || *name == 0) {
1343 sprintf(defname, "%d", info->md_minor);
1344 name = defname;
1345 }
1346 if (homehost &&
1347 strchr(name, ':')== NULL &&
1348 strlen(homehost)+1+strlen(name) < 32) {
1349 strcpy(sb->set_name, homehost);
1350 strcat(sb->set_name, ":");
1351 strcat(sb->set_name, name);
1352 } else
1353 strcpy(sb->set_name, name);
1354
1355 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1356 sb->level = __cpu_to_le32(info->level);
1357 sb->layout = __cpu_to_le32(info->layout);
1358 sb->size = __cpu_to_le64(size*2ULL);
1359 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1360 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1361
1362 sb->data_offset = __cpu_to_le64(data_offset);
1363 sb->data_size = __cpu_to_le64(0);
1364 sb->super_offset = __cpu_to_le64(0);
1365 sb->recovery_offset = __cpu_to_le64(0);
1366
1367 sb->utime = sb->ctime;
1368 sb->events = __cpu_to_le64(1);
1369 if (info->state & (1<<MD_SB_CLEAN))
1370 sb->resync_offset = MaxSector;
1371 else
1372 sb->resync_offset = 0;
1373 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1374 sbsize = ROUND_UP(sbsize, 512);
1375 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
1376
1377 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
1378
1379 return 1;
1380 }
1381
1382 struct devinfo {
1383 int fd;
1384 char *devname;
1385 long long data_offset;
1386 mdu_disk_info_t disk;
1387 struct devinfo *next;
1388 };
1389 #ifndef MDASSEMBLE
1390 /* Add a device to the superblock being created */
1391 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
1392 int fd, char *devname, unsigned long long data_offset)
1393 {
1394 struct mdp_superblock_1 *sb = st->sb;
1395 __u16 *rp = sb->dev_roles + dk->number;
1396 struct devinfo *di, **dip;
1397
1398 if ((dk->state & 6) == 6) /* active, sync */
1399 *rp = __cpu_to_le16(dk->raid_disk);
1400 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
1401 *rp = 0xffff;
1402 else
1403 *rp = 0xfffe;
1404
1405 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
1406 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
1407 sb->max_dev = __cpu_to_le32(dk->number+1);
1408
1409 sb->dev_number = __cpu_to_le32(dk->number);
1410 sb->devflags = 0; /* don't copy another disks flags */
1411 sb->sb_csum = calc_sb_1_csum(sb);
1412
1413 dip = (struct devinfo **)&st->info;
1414 while (*dip)
1415 dip = &(*dip)->next;
1416 di = xmalloc(sizeof(struct devinfo));
1417 di->fd = fd;
1418 di->devname = devname;
1419 di->disk = *dk;
1420 di->data_offset = data_offset;
1421 di->next = NULL;
1422 *dip = di;
1423
1424 return 0;
1425 }
1426 #endif
1427
1428 static void locate_bitmap1(struct supertype *st, int fd);
1429
1430 static int store_super1(struct supertype *st, int fd)
1431 {
1432 struct mdp_superblock_1 *sb = st->sb;
1433 unsigned long long sb_offset;
1434 struct align_fd afd;
1435 int sbsize;
1436 unsigned long long dsize;
1437
1438 if (!get_dev_size(fd, NULL, &dsize))
1439 return 1;
1440
1441 dsize >>= 9;
1442
1443 if (dsize < 24)
1444 return 2;
1445
1446 init_afd(&afd, fd);
1447
1448 /*
1449 * Calculate the position of the superblock.
1450 * It is always aligned to a 4K boundary and
1451 * depending on minor_version, it can be:
1452 * 0: At least 8K, but less than 12K, from end of device
1453 * 1: At start of device
1454 * 2: 4K from start of device.
1455 */
1456 switch(st->minor_version) {
1457 case 0:
1458 sb_offset = dsize;
1459 sb_offset -= 8*2;
1460 sb_offset &= ~(4*2-1);
1461 break;
1462 case 1:
1463 sb_offset = 0;
1464 break;
1465 case 2:
1466 sb_offset = 4*2;
1467 break;
1468 default:
1469 return -EINVAL;
1470 }
1471
1472
1473
1474 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1475 0 != __le64_to_cpu(sb->super_offset)
1476 ) {
1477 pr_err("internal error - sb_offset is wrong\n");
1478 abort();
1479 }
1480
1481 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1482 return 3;
1483
1484 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1485
1486 if (awrite(&afd, sb, sbsize) != sbsize)
1487 return 4;
1488
1489 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1490 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1491 (((char*)sb)+MAX_SB_SIZE);
1492 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1493 locate_bitmap1(st, fd);
1494 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1495 return 5;
1496 }
1497 }
1498 fsync(fd);
1499 return 0;
1500 }
1501
1502 static int load_super1(struct supertype *st, int fd, char *devname);
1503
1504 static unsigned long choose_bm_space(unsigned long devsize)
1505 {
1506 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1507 * if bigger than 200Gig, save 128k
1508 * NOTE: result must be multiple of 4K else bad things happen
1509 * on 4K-sector devices.
1510 */
1511 if (devsize < 64*2) return 0;
1512 if (devsize - 64*2 >= 200*1024*1024*2)
1513 return 128*2;
1514 if (devsize - 4*2 > 8*1024*1024*2)
1515 return 64*2;
1516 return 4*2;
1517 }
1518
1519 static void free_super1(struct supertype *st);
1520
1521 #ifndef MDASSEMBLE
1522 static int write_init_super1(struct supertype *st)
1523 {
1524 struct mdp_superblock_1 *sb = st->sb;
1525 struct supertype *refst;
1526 int rfd;
1527 int rv = 0;
1528 unsigned long long bm_space;
1529 unsigned long long reserved;
1530 struct devinfo *di;
1531 unsigned long long dsize, array_size;
1532 unsigned long long sb_offset, headroom;
1533 unsigned long long data_offset;
1534
1535 for (di = st->info; di; di = di->next) {
1536 if (di->disk.state == 1)
1537 continue;
1538 if (di->fd < 0)
1539 continue;
1540
1541 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1542 ;
1543
1544 sb->dev_number = __cpu_to_le32(di->disk.number);
1545 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1546 sb->devflags |= WriteMostly1;
1547 else
1548 sb->devflags &= ~WriteMostly1;
1549
1550 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1551 read(rfd, sb->device_uuid, 16) != 16) {
1552 __u32 r[4] = {random(), random(), random(), random()};
1553 memcpy(sb->device_uuid, r, 16);
1554 }
1555 if (rfd >= 0)
1556 close(rfd);
1557
1558 sb->events = 0;
1559
1560 refst = dup_super(st);
1561 if (load_super1(refst, di->fd, NULL)==0) {
1562 struct mdp_superblock_1 *refsb = refst->sb;
1563
1564 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1565 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1566 /* same array, so preserve events and
1567 * dev_number */
1568 sb->events = refsb->events;
1569 /* bugs in 2.6.17 and earlier mean the
1570 * dev_number chosen in Manage must be preserved
1571 */
1572 if (get_linux_version() >= 2006018)
1573 sb->dev_number = refsb->dev_number;
1574 }
1575 free_super1(refst);
1576 }
1577 free(refst);
1578
1579 if (!get_dev_size(di->fd, NULL, &dsize)) {
1580 rv = 1;
1581 goto error_out;
1582 }
1583 dsize >>= 9;
1584
1585 if (dsize < 24) {
1586 close(di->fd);
1587 rv = 2;
1588 goto error_out;
1589 }
1590
1591
1592 /*
1593 * Calculate the position of the superblock.
1594 * It is always aligned to a 4K boundary and
1595 * depending on minor_version, it can be:
1596 * 0: At least 8K, but less than 12K, from end of device
1597 * 1: At start of device
1598 * 2: 4K from start of device.
1599 * Depending on the array size, we might leave extra space
1600 * for a bitmap.
1601 * Also leave 4K for bad-block log.
1602 */
1603 array_size = __le64_to_cpu(sb->size);
1604 /* work out how much space we left for a bitmap,
1605 * Add 8 sectors for bad block log */
1606 bm_space = choose_bm_space(array_size) + 8;
1607
1608 /* We try to leave 0.1% at the start for reshape
1609 * operations, but limit this to 128Meg (0.1% of 10Gig)
1610 * which is plenty for efficient reshapes
1611 * However we make it at least 2 chunks as one chunk
1612 * is minimum needed for reshape.
1613 */
1614 headroom = 128 * 1024 * 2;
1615 while (headroom << 10 > array_size &&
1616 headroom/2 >= __le32_to_cpu(sb->chunksize) * 2)
1617 headroom >>= 1;
1618
1619 data_offset = di->data_offset;
1620 switch(st->minor_version) {
1621 case 0:
1622 sb_offset = dsize;
1623 sb_offset -= 8*2;
1624 sb_offset &= ~(4*2-1);
1625 sb->super_offset = __cpu_to_le64(sb_offset);
1626 if (data_offset == INVALID_SECTORS)
1627 sb->data_offset = 0;
1628 if (sb_offset < array_size + bm_space)
1629 bm_space = sb_offset - array_size;
1630 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1631 if (bm_space >= 8) {
1632 sb->bblog_size = __cpu_to_le16(8);
1633 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1634 }
1635 break;
1636 case 1:
1637 sb->super_offset = __cpu_to_le64(0);
1638 if (data_offset == INVALID_SECTORS) {
1639 reserved = bm_space + 4*2;
1640 if (reserved < headroom)
1641 reserved = headroom;
1642 if (reserved + array_size > dsize)
1643 reserved = dsize - array_size;
1644 /* Try for multiple of 1Meg so it is nicely aligned */
1645 #define ONE_MEG (2*1024)
1646 if (reserved > ONE_MEG)
1647 reserved = (reserved/ONE_MEG) * ONE_MEG;
1648
1649 /* force 4K alignment */
1650 reserved &= ~7ULL;
1651
1652 } else
1653 reserved = data_offset;
1654
1655 sb->data_offset = __cpu_to_le64(reserved);
1656 sb->data_size = __cpu_to_le64(dsize - reserved);
1657 if (reserved >= 8 + 32*2 + 8) {
1658 sb->bblog_size = __cpu_to_le16(8);
1659 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1660 } else if (reserved >= 16) {
1661 sb->bblog_size = __cpu_to_le16(8);
1662 sb->bblog_offset = __cpu_to_le32(reserved-8);
1663 }
1664 break;
1665 case 2:
1666 sb_offset = 4*2;
1667 sb->super_offset = __cpu_to_le64(4*2);
1668 if (data_offset == INVALID_SECTORS) {
1669 if (4*2 + 4*2 + bm_space + array_size
1670 > dsize)
1671 bm_space = dsize - array_size
1672 - 4*2 - 4*2;
1673
1674 reserved = bm_space + 4*2 + 4*2;
1675 if (reserved < headroom)
1676 reserved = headroom;
1677 if (reserved + array_size > dsize)
1678 reserved = dsize - array_size;
1679 /* Try for multiple of 1Meg so it is nicely aligned */
1680 #define ONE_MEG (2*1024)
1681 if (reserved > ONE_MEG)
1682 reserved = (reserved/ONE_MEG) * ONE_MEG;
1683
1684 /* force 4K alignment */
1685 reserved &= ~7ULL;
1686
1687 } else
1688 reserved = data_offset;
1689
1690 sb->data_offset = __cpu_to_le64(reserved);
1691 sb->data_size = __cpu_to_le64(dsize - reserved);
1692 if (reserved >= 16 + 32*2 + 8) {
1693 sb->bblog_size = __cpu_to_le16(8);
1694 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1695 } else if (reserved >= 16+16) {
1696 sb->bblog_size = __cpu_to_le16(8);
1697 /* '8' sectors for the bblog, and another '8'
1698 * because we want offset from superblock, not
1699 * start of device.
1700 */
1701 sb->bblog_offset = __cpu_to_le32(reserved-8-8);
1702 }
1703 break;
1704 default:
1705 pr_err("Failed to write invalid "
1706 "metadata format 1.%i to %s\n",
1707 st->minor_version, di->devname);
1708 rv = -EINVAL;
1709 goto out;
1710 }
1711
1712 sb->sb_csum = calc_sb_1_csum(sb);
1713 rv = store_super1(st, di->fd);
1714 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1715 rv = st->ss->write_bitmap(st, di->fd);
1716 close(di->fd);
1717 di->fd = -1;
1718 if (rv)
1719 goto error_out;
1720 }
1721 error_out:
1722 if (rv)
1723 pr_err("Failed to write metadata to %s\n",
1724 di->devname);
1725 out:
1726 return rv;
1727 }
1728 #endif
1729
1730 static int compare_super1(struct supertype *st, struct supertype *tst)
1731 {
1732 /*
1733 * return:
1734 * 0 same, or first was empty, and second was copied
1735 * 1 second had wrong number
1736 * 2 wrong uuid
1737 * 3 wrong other info
1738 */
1739 struct mdp_superblock_1 *first = st->sb;
1740 struct mdp_superblock_1 *second = tst->sb;
1741
1742 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1743 return 1;
1744 if (second->major_version != __cpu_to_le32(1))
1745 return 1;
1746
1747 if (!first) {
1748 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1749 pr_err("%s could not allocate superblock\n", __func__);
1750 return 1;
1751 }
1752 memcpy(first, second, SUPER1_SIZE);
1753 st->sb = first;
1754 return 0;
1755 }
1756 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1757 return 2;
1758
1759 if (first->ctime != second->ctime ||
1760 first->level != second->level ||
1761 first->layout != second->layout ||
1762 first->size != second->size ||
1763 first->chunksize != second->chunksize ||
1764 first->raid_disks != second->raid_disks)
1765 return 3;
1766 return 0;
1767 }
1768
1769 static int load_super1(struct supertype *st, int fd, char *devname)
1770 {
1771 unsigned long long dsize;
1772 unsigned long long sb_offset;
1773 struct mdp_superblock_1 *super;
1774 int uuid[4];
1775 struct bitmap_super_s *bsb;
1776 struct misc_dev_info *misc;
1777 struct align_fd afd;
1778
1779 free_super1(st);
1780
1781 init_afd(&afd, fd);
1782
1783 if (st->ss == NULL || st->minor_version == -1) {
1784 int bestvers = -1;
1785 struct supertype tst;
1786 __u64 bestctime = 0;
1787 /* guess... choose latest ctime */
1788 memset(&tst, 0, sizeof(tst));
1789 tst.ss = &super1;
1790 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1791 switch(load_super1(&tst, fd, devname)) {
1792 case 0: super = tst.sb;
1793 if (bestvers == -1 ||
1794 bestctime < __le64_to_cpu(super->ctime)) {
1795 bestvers = tst.minor_version;
1796 bestctime = __le64_to_cpu(super->ctime);
1797 }
1798 free(super);
1799 tst.sb = NULL;
1800 break;
1801 case 1: return 1; /*bad device */
1802 case 2: break; /* bad, try next */
1803 }
1804 }
1805 if (bestvers != -1) {
1806 int rv;
1807 tst.minor_version = bestvers;
1808 tst.ss = &super1;
1809 tst.max_devs = MAX_DEVS;
1810 rv = load_super1(&tst, fd, devname);
1811 if (rv == 0)
1812 *st = tst;
1813 return rv;
1814 }
1815 return 2;
1816 }
1817 if (!get_dev_size(fd, devname, &dsize))
1818 return 1;
1819 dsize >>= 9;
1820
1821 if (dsize < 24) {
1822 if (devname)
1823 pr_err("%s is too small for md: size is %llu sectors.\n",
1824 devname, dsize);
1825 return 1;
1826 }
1827
1828 /*
1829 * Calculate the position of the superblock.
1830 * It is always aligned to a 4K boundary and
1831 * depending on minor_version, it can be:
1832 * 0: At least 8K, but less than 12K, from end of device
1833 * 1: At start of device
1834 * 2: 4K from start of device.
1835 */
1836 switch(st->minor_version) {
1837 case 0:
1838 sb_offset = dsize;
1839 sb_offset -= 8*2;
1840 sb_offset &= ~(4*2-1);
1841 break;
1842 case 1:
1843 sb_offset = 0;
1844 break;
1845 case 2:
1846 sb_offset = 4*2;
1847 break;
1848 default:
1849 return -EINVAL;
1850 }
1851
1852 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1853 if (devname)
1854 pr_err("Cannot seek to superblock on %s: %s\n",
1855 devname, strerror(errno));
1856 return 1;
1857 }
1858
1859 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1860 pr_err("%s could not allocate superblock\n",
1861 __func__);
1862 return 1;
1863 }
1864
1865 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
1866 if (devname)
1867 pr_err("Cannot read superblock on %s\n",
1868 devname);
1869 free(super);
1870 return 1;
1871 }
1872
1873 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1874 if (devname)
1875 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1876 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1877 free(super);
1878 return 2;
1879 }
1880
1881 if (__le32_to_cpu(super->major_version) != 1) {
1882 if (devname)
1883 pr_err("Cannot interpret superblock on %s - version is %d\n",
1884 devname, __le32_to_cpu(super->major_version));
1885 free(super);
1886 return 2;
1887 }
1888 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1889 if (devname)
1890 pr_err("No superblock found on %s (super_offset is wrong)\n",
1891 devname);
1892 free(super);
1893 return 2;
1894 }
1895 st->sb = super;
1896
1897 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1898
1899 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
1900 misc->device_size = dsize;
1901
1902 /* Now check on the bitmap superblock */
1903 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1904 return 0;
1905 /* Read the bitmap superblock and make sure it looks
1906 * valid. If it doesn't clear the bit. An --assemble --force
1907 * should get that written out.
1908 */
1909 locate_bitmap1(st, fd);
1910 if (aread(&afd, bsb, 512) != 512)
1911 goto no_bitmap;
1912
1913 uuid_from_super1(st, uuid);
1914 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1915 memcmp(bsb->uuid, uuid, 16) != 0)
1916 goto no_bitmap;
1917 return 0;
1918
1919 no_bitmap:
1920 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
1921 & ~MD_FEATURE_BITMAP_OFFSET);
1922 return 0;
1923 }
1924
1925
1926 static struct supertype *match_metadata_desc1(char *arg)
1927 {
1928 struct supertype *st = xcalloc(1, sizeof(*st));
1929
1930 st->container_devnm[0] = 0;
1931 st->ss = &super1;
1932 st->max_devs = MAX_DEVS;
1933 st->sb = NULL;
1934 /* leading zeros can be safely ignored. --detail generates them. */
1935 while (*arg == '0')
1936 arg++;
1937 if (strcmp(arg, "1.0") == 0 ||
1938 strcmp(arg, "1.00") == 0) {
1939 st->minor_version = 0;
1940 return st;
1941 }
1942 if (strcmp(arg, "1.1") == 0 ||
1943 strcmp(arg, "1.01") == 0
1944 ) {
1945 st->minor_version = 1;
1946 return st;
1947 }
1948 if (strcmp(arg, "1.2") == 0 ||
1949 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1950 strcmp(arg, "default") == 0 ||
1951 #endif /* DEFAULT_OLD_METADATA */
1952 strcmp(arg, "1.02") == 0) {
1953 st->minor_version = 2;
1954 return st;
1955 }
1956 if (strcmp(arg, "1") == 0 ||
1957 strcmp(arg, "default") == 0) {
1958 st->minor_version = -1;
1959 return st;
1960 }
1961
1962 free(st);
1963 return NULL;
1964 }
1965
1966 /* find available size on device with this devsize, using
1967 * superblock type st, and reserving 'reserve' sectors for
1968 * a possible bitmap
1969 */
1970 static __u64 _avail_size1(struct supertype *st, __u64 devsize,
1971 unsigned long long data_offset, int chunksize)
1972 {
1973 struct mdp_superblock_1 *super = st->sb;
1974 int bmspace = 0;
1975 if (devsize < 24)
1976 return 0;
1977
1978 if (super == NULL)
1979 /* creating: allow suitable space for bitmap */
1980 bmspace = choose_bm_space(devsize);
1981 #ifndef MDASSEMBLE
1982 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1983 /* hot-add. allow for actual size of bitmap */
1984 struct bitmap_super_s *bsb;
1985 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1986 bmspace = bitmap_sectors(bsb);
1987 }
1988 #endif
1989 /* Allow space for bad block log */
1990 if (super && super->bblog_size)
1991 devsize -= __le16_to_cpu(super->bblog_size);
1992 else
1993 devsize -= 8;
1994
1995
1996 if (st->minor_version < 0)
1997 /* not specified, so time to set default */
1998 st->minor_version = 2;
1999
2000 if (data_offset != INVALID_SECTORS)
2001 switch(st->minor_version) {
2002 case 0:
2003 return devsize - data_offset - 8*2;
2004 case 1:
2005 case 2:
2006 return devsize - data_offset;
2007 default:
2008 return 0;
2009 }
2010
2011 devsize -= bmspace;
2012
2013 if (super == NULL && st->minor_version > 0) {
2014 /* haven't committed to a size yet, so allow some
2015 * slack for space for reshape.
2016 * Limit slack to 128M, but aim for about 0.1%
2017 */
2018 unsigned long long headroom = 128*1024*2;
2019 while ((headroom << 10) > devsize &&
2020 (chunksize == 0 ||
2021 headroom / 2 >= ((unsigned)chunksize*2)*2))
2022 headroom >>= 1;
2023 devsize -= headroom;
2024 }
2025 switch(st->minor_version) {
2026 case 0:
2027 /* at end */
2028 return ((devsize - 8*2 ) & ~(4*2-1));
2029 case 1:
2030 /* at start, 4K for superblock and possible bitmap */
2031 return devsize - 4*2;
2032 case 2:
2033 /* 4k from start, 4K for superblock and possible bitmap */
2034 return devsize - (4+4)*2;
2035 }
2036 return 0;
2037 }
2038 static __u64 avail_size1(struct supertype *st, __u64 devsize,
2039 unsigned long long data_offset)
2040 {
2041 return _avail_size1(st, devsize, data_offset, 0);
2042 }
2043
2044 static int
2045 add_internal_bitmap1(struct supertype *st,
2046 int *chunkp, int delay, int write_behind,
2047 unsigned long long size,
2048 int may_change, int major)
2049 {
2050 /*
2051 * If not may_change, then this is a 'Grow' without sysfs support for
2052 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2053 * If may_change, then this is create or a Grow with sysfs syupport,
2054 * and we can put the bitmap wherever we like.
2055 *
2056 * size is in sectors, chunk is in bytes !!!
2057 */
2058
2059 unsigned long long bits;
2060 unsigned long long max_bits;
2061 unsigned long long min_chunk;
2062 long offset;
2063 long bbl_offset, bbl_size;
2064 unsigned long long chunk = *chunkp;
2065 int room = 0;
2066 int creating = 0;
2067 struct mdp_superblock_1 *sb = st->sb;
2068 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
2069 int uuid[4];
2070
2071
2072 if (__le64_to_cpu(sb->data_size) == 0)
2073 /* Must be creating the array, else data_size would be non-zero */
2074 creating = 1;
2075 switch(st->minor_version) {
2076 case 0:
2077 /* either 3K after the superblock (when hot-add),
2078 * or some amount of space before.
2079 */
2080 if (creating) {
2081 /* We are creating array, so we *know* how much room has
2082 * been left.
2083 */
2084 offset = 0;
2085 room = choose_bm_space(__le64_to_cpu(sb->size));
2086 bbl_size = 8;
2087 } else {
2088 room = __le64_to_cpu(sb->super_offset)
2089 - __le64_to_cpu(sb->data_offset)
2090 - __le64_to_cpu(sb->data_size);
2091 bbl_size = __le16_to_cpu(sb->bblog_size);
2092 if (bbl_size < 8)
2093 bbl_size = 8;
2094 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
2095 if (bbl_size < -bbl_offset)
2096 bbl_size = -bbl_offset;
2097
2098 if (!may_change || (room < 3*2 &&
2099 __le32_to_cpu(sb->max_dev) <= 384)) {
2100 room = 3*2;
2101 offset = 1*2;
2102 bbl_size = 0;
2103 } else {
2104 offset = 0; /* means movable offset */
2105 }
2106 }
2107 break;
2108 case 1:
2109 case 2: /* between superblock and data */
2110 if (creating) {
2111 offset = 4*2;
2112 room = choose_bm_space(__le64_to_cpu(sb->size));
2113 bbl_size = 8;
2114 } else {
2115 room = __le64_to_cpu(sb->data_offset)
2116 - __le64_to_cpu(sb->super_offset);
2117 bbl_size = __le16_to_cpu(sb->bblog_size);
2118 if (bbl_size)
2119 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
2120 else
2121 bbl_size = 8;
2122
2123 if (!may_change) {
2124 room -= 2; /* Leave 1K for superblock */
2125 offset = 2;
2126 bbl_size = 0;
2127 } else {
2128 room -= 4*2; /* leave 4K for superblock */
2129 offset = 4*2;
2130 }
2131 }
2132 break;
2133 default:
2134 return 0;
2135 }
2136
2137 room -= bbl_size;
2138 if (chunk == UnSet && room > 128*2)
2139 /* Limit to 128K of bitmap when chunk size not requested */
2140 room = 128*2;
2141
2142 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
2143
2144 min_chunk = 4096; /* sub-page chunks don't work yet.. */
2145 bits = (size*512)/min_chunk +1;
2146 while (bits > max_bits) {
2147 min_chunk *= 2;
2148 bits = (bits+1)/2;
2149 }
2150 if (chunk == UnSet) {
2151 /* For practical purpose, 64Meg is a good
2152 * default chunk size for internal bitmaps.
2153 */
2154 chunk = min_chunk;
2155 if (chunk < 64*1024*1024)
2156 chunk = 64*1024*1024;
2157 } else if (chunk < min_chunk)
2158 return 0; /* chunk size too small */
2159 if (chunk == 0) /* rounding problem */
2160 return 0;
2161
2162 if (offset == 0) {
2163 /* start bitmap on a 4K boundary with enough space for
2164 * the bitmap
2165 */
2166 bits = (size*512) / chunk + 1;
2167 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
2168 room *= 8; /* convert 4K blocks to sectors */
2169 offset = -room - bbl_size;
2170 }
2171
2172 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
2173
2174 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2175 | MD_FEATURE_BITMAP_OFFSET);
2176 memset(bms, 0, sizeof(*bms));
2177 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
2178 bms->version = __cpu_to_le32(major);
2179 uuid_from_super1(st, uuid);
2180 memcpy(bms->uuid, uuid, 16);
2181 bms->chunksize = __cpu_to_le32(chunk);
2182 bms->daemon_sleep = __cpu_to_le32(delay);
2183 bms->sync_size = __cpu_to_le64(size);
2184 bms->write_behind = __cpu_to_le32(write_behind);
2185
2186 *chunkp = chunk;
2187 return 1;
2188 }
2189
2190 static void locate_bitmap1(struct supertype *st, int fd)
2191 {
2192 unsigned long long offset;
2193 struct mdp_superblock_1 *sb;
2194 int mustfree = 0;
2195
2196 if (!st->sb) {
2197 if (st->ss->load_super(st, fd, NULL))
2198 return; /* no error I hope... */
2199 mustfree = 1;
2200 }
2201 sb = st->sb;
2202
2203 offset = __le64_to_cpu(sb->super_offset);
2204 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
2205 if (mustfree)
2206 free(sb);
2207 lseek64(fd, offset<<9, 0);
2208 }
2209
2210 static int write_bitmap1(struct supertype *st, int fd)
2211 {
2212 struct mdp_superblock_1 *sb = st->sb;
2213 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
2214 int rv = 0;
2215 void *buf;
2216 int towrite, n;
2217 struct align_fd afd;
2218
2219 init_afd(&afd, fd);
2220
2221 locate_bitmap1(st, fd);
2222
2223 if (posix_memalign(&buf, 4096, 4096))
2224 return -ENOMEM;
2225
2226 memset(buf, 0xff, 4096);
2227 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2228
2229 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
2230 towrite = (towrite+7) >> 3; /* bits to bytes */
2231 towrite += sizeof(bitmap_super_t);
2232 towrite = ROUND_UP(towrite, 512);
2233 while (towrite > 0) {
2234 n = towrite;
2235 if (n > 4096)
2236 n = 4096;
2237 n = awrite(&afd, buf, n);
2238 if (n > 0)
2239 towrite -= n;
2240 else
2241 break;
2242 memset(buf, 0xff, 4096);
2243 }
2244 fsync(fd);
2245 if (towrite)
2246 rv = -2;
2247
2248 free(buf);
2249 return rv;
2250 }
2251
2252 static void free_super1(struct supertype *st)
2253 {
2254 if (st->sb)
2255 free(st->sb);
2256 while (st->info) {
2257 struct devinfo *di = st->info;
2258 st->info = di->next;
2259 if (di->fd >= 0)
2260 close(di->fd);
2261 free(di);
2262 }
2263 st->sb = NULL;
2264 }
2265
2266 #ifndef MDASSEMBLE
2267 static int validate_geometry1(struct supertype *st, int level,
2268 int layout, int raiddisks,
2269 int *chunk, unsigned long long size,
2270 unsigned long long data_offset,
2271 char *subdev, unsigned long long *freesize,
2272 int verbose)
2273 {
2274 unsigned long long ldsize;
2275 int fd;
2276
2277 if (level == LEVEL_CONTAINER) {
2278 if (verbose)
2279 pr_err("1.x metadata does not support containers\n");
2280 return 0;
2281 }
2282 if (chunk && *chunk == UnSet)
2283 *chunk = DEFAULT_CHUNK;
2284
2285 if (!subdev)
2286 return 1;
2287
2288 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2289 if (fd < 0) {
2290 if (verbose)
2291 pr_err("super1.x cannot open %s: %s\n",
2292 subdev, strerror(errno));
2293 return 0;
2294 }
2295
2296 if (!get_dev_size(fd, subdev, &ldsize)) {
2297 close(fd);
2298 return 0;
2299 }
2300 close(fd);
2301
2302 *freesize = _avail_size1(st, ldsize >> 9, data_offset, *chunk);
2303 return 1;
2304 }
2305 #endif /* MDASSEMBLE */
2306
2307 void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
2308 {
2309 /* Create a v1.0 superblock based on 'info'*/
2310 void *ret;
2311 struct mdp_superblock_1 *sb;
2312 int i;
2313 int rfd;
2314 unsigned long long offset;
2315
2316 if (posix_memalign(&ret, 4096, 1024) != 0)
2317 return NULL;
2318 sb = ret;
2319 memset(ret, 0, 1024);
2320 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
2321 sb->major_version = __cpu_to_le32(1);
2322
2323 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
2324 sprintf(sb->set_name, "%d", sb0->md_minor);
2325 sb->ctime = __cpu_to_le32(info->array.ctime+1);
2326 sb->level = __cpu_to_le32(info->array.level);
2327 sb->layout = __cpu_to_le32(info->array.layout);
2328 sb->size = __cpu_to_le64(info->component_size);
2329 sb->chunksize = __cpu_to_le32(info->array.chunk_size/512);
2330 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
2331 sb->data_size = sb->size;
2332 sb->resync_offset = MaxSector;
2333 sb->max_dev = __cpu_to_le32(MD_SB_DISKS);
2334 sb->dev_number = __cpu_to_le32(info->disk.number);
2335 sb->utime = __cpu_to_le64(info->array.utime);
2336
2337 offset = st->devsize/512 - 8*2;
2338 offset &= ~(4*2-1);
2339 sb->super_offset = __cpu_to_le64(offset);
2340 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2341
2342 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
2343 read(rfd, sb->device_uuid, 16) != 16) {
2344 __u32 r[4] = {random(), random(), random(), random()};
2345 memcpy(sb->device_uuid, r, 16);
2346 }
2347 if (rfd >= 0)
2348 close(rfd);
2349
2350 for (i = 0; i < MD_SB_DISKS; i++) {
2351 int state = sb0->disks[i].state;
2352 sb->dev_roles[i] = 0xFFFF;
2353 if ((state & (1<<MD_DISK_SYNC)) &&
2354 !(state & (1<<MD_DISK_FAULTY)))
2355 sb->dev_roles[i] = __cpu_to_le16(sb0->disks[i].raid_disk);
2356 }
2357 sb->sb_csum = calc_sb_1_csum(sb);
2358 return ret;
2359 }
2360
2361 struct superswitch super1 = {
2362 #ifndef MDASSEMBLE
2363 .examine_super = examine_super1,
2364 .brief_examine_super = brief_examine_super1,
2365 .export_examine_super = export_examine_super1,
2366 .detail_super = detail_super1,
2367 .brief_detail_super = brief_detail_super1,
2368 .export_detail_super = export_detail_super1,
2369 .write_init_super = write_init_super1,
2370 .validate_geometry = validate_geometry1,
2371 .add_to_super = add_to_super1,
2372 .examine_badblocks = examine_badblocks_super1,
2373 .copy_metadata = copy_metadata1,
2374 #endif
2375 .match_home = match_home1,
2376 .uuid_from_super = uuid_from_super1,
2377 .getinfo_super = getinfo_super1,
2378 .container_content = container_content1,
2379 .update_super = update_super1,
2380 .init_super = init_super1,
2381 .store_super = store_super1,
2382 .compare_super = compare_super1,
2383 .load_super = load_super1,
2384 .match_metadata_desc = match_metadata_desc1,
2385 .avail_size = avail_size1,
2386 .add_internal_bitmap = add_internal_bitmap1,
2387 .locate_bitmap = locate_bitmap1,
2388 .write_bitmap = write_bitmap1,
2389 .free_super = free_super1,
2390 #if __BYTE_ORDER == BIG_ENDIAN
2391 .swapuuid = 0,
2392 #else
2393 .swapuuid = 1,
2394 #endif
2395 .name = "1.x",
2396 };