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