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