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