]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
super1.c: use ROUND_UP/ROUND_UP_PTR
[thirdparty/mdadm.git] / super1.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 /*
27 * The version-1 superblock :
28 * All numeric fields are little-endian.
29 *
30 * total size: 256 bytes plus 2 per device.
31 * 1K allows 384 devices.
32 */
33 struct mdp_superblock_1 {
34 /* constant array information - 128 bytes */
35 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
36 __u32 major_version; /* 1 */
37 __u32 feature_map; /* 0 for now */
38 __u32 pad0; /* always set to 0 when writing */
39
40 __u8 set_uuid[16]; /* user-space generated. */
41 char set_name[32]; /* set and interpreted by user-space */
42
43 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
44 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
45 __u32 layout; /* only for raid5 currently */
46 __u64 size; /* used size of component devices, in 512byte sectors */
47
48 __u32 chunksize; /* in 512byte sectors */
49 __u32 raid_disks;
50 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
51 * NOTE: signed, so bitmap can be before superblock
52 * only meaningful of feature_map[0] is set.
53 */
54
55 /* These are only valid with feature bit '4' */
56 __u32 new_level; /* new level we are reshaping to */
57 __u64 reshape_position; /* next address in array-space for reshape */
58 __u32 delta_disks; /* change in number of raid_disks */
59 __u32 new_layout; /* new layout */
60 __u32 new_chunk; /* new chunk size (bytes) */
61 __u8 pad1[128-124]; /* set to 0 when written */
62
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset; /* sector start of data, often 0 */
65 __u64 data_size; /* sectors in this device that can be used for data */
66 __u64 super_offset; /* sector start of this superblock */
67 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
71 __u8 devflags; /* per-device flags. Only one defined...*/
72 #define WriteMostly1 1 /* mask for writemostly flag in above */
73 __u8 pad2[64-57]; /* set to 0 when writing */
74
75 /* array state information - 64 bytes */
76 __u64 utime; /* 40 bits second, 24 btes microseconds */
77 __u64 events; /* incremented when superblock updated */
78 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
79 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
80 __u32 max_dev; /* size of dev_roles[] array to consider */
81 __u8 pad3[64-32]; /* set to 0 when writing */
82
83 /* device state information. Indexed by dev_number.
84 * 2 bytes per device
85 * Note there are no per-device state flags. State information is rolled
86 * into the 'roles' value. If a device is spare or faulty, then it doesn't
87 * have a meaningful role.
88 */
89 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
90 };
91
92 #define MAX_SB_SIZE 4096
93 /* bitmap super size is 256, but we round up to a sector for alignment */
94 #define BM_SUPER_SIZE 512
95 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
96 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
97 + sizeof(struct misc_dev_info))
98
99 struct misc_dev_info {
100 __u64 device_size;
101 };
102
103 /* feature_map bits */
104 #define MD_FEATURE_BITMAP_OFFSET 1
105 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
106 * must be honoured
107 */
108 #define MD_FEATURE_RESHAPE_ACTIVE 4
109
110 #define MD_FEATURE_ALL (1|2|4)
111
112 #ifndef offsetof
113 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
114 #endif
115 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
116 {
117 unsigned int disk_csum, csum;
118 unsigned long long newcsum;
119 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
120 unsigned int *isuper = (unsigned int*)sb;
121
122 /* make sure I can count... */
123 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
124 offsetof(struct mdp_superblock_1, utime) != 192 ||
125 sizeof(struct mdp_superblock_1) != 256) {
126 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
127 }
128
129 disk_csum = sb->sb_csum;
130 sb->sb_csum = 0;
131 newcsum = 0;
132 for (; size>=4; size -= 4 ) {
133 newcsum += __le32_to_cpu(*isuper);
134 isuper++;
135 }
136
137 if (size == 2)
138 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
139
140 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
141 sb->sb_csum = disk_csum;
142 return __cpu_to_le32(csum);
143 }
144
145 /*
146 * Information related to file descriptor used for aligned reads/writes.
147 * Cache the block size.
148 */
149 struct align_fd {
150 int fd;
151 int blk_sz;
152 };
153
154 static void init_afd(struct align_fd *afd, int fd)
155 {
156 afd->fd = fd;
157
158 if (ioctl(afd->fd, BLKSSZGET, &afd->blk_sz) != 0)
159 afd->blk_sz = 512;
160 }
161
162 static char abuf[4096+4096];
163 static int aread(struct align_fd *afd, void *buf, int len)
164 {
165 /* aligned read.
166 * On devices with a 4K sector size, we need to read
167 * the full sector and copy relevant bits into
168 * the buffer
169 */
170 int bsize, iosize;
171 char *b;
172 int n;
173
174 bsize = afd->blk_sz;
175
176 if (!bsize || bsize > 4096 || len > 4096) {
177 if (!bsize)
178 fprintf(stderr, "WARNING - aread() called with "
179 "invalid block size\n");
180 return -1;
181 }
182 b = ROUND_UP_PTR((char *)abuf, 4096);
183
184 for (iosize = 0; iosize < len; iosize += bsize)
185 ;
186 n = read(afd->fd, b, iosize);
187 if (n <= 0)
188 return n;
189 lseek(afd->fd, len - n, 1);
190 if (n > len)
191 n = len;
192 memcpy(buf, b, n);
193 return n;
194 }
195
196 static int awrite(struct align_fd *afd, void *buf, int len)
197 {
198 /* aligned write.
199 * On devices with a 4K sector size, we need to write
200 * the full sector. We pre-read if the sector is larger
201 * than the write.
202 * The address must be sector-aligned.
203 */
204 int bsize, iosize;
205 char *b;
206 int n;
207
208 bsize = afd->blk_sz;
209 if (!bsize || bsize > 4096 || len > 4096) {
210 if (!bsize)
211 fprintf(stderr, "WARNING - awrite() called with "
212 "invalid block size\n");
213 return -1;
214 }
215 b = ROUND_UP_PTR((char *)abuf, 4096);
216
217 for (iosize = 0; iosize < len ; iosize += bsize)
218 ;
219
220 if (len != iosize) {
221 n = read(afd->fd, b, iosize);
222 if (n <= 0)
223 return n;
224 lseek(afd->fd, -n, 1);
225 }
226
227 memcpy(b, buf, len);
228 n = write(afd->fd, b, iosize);
229 if (n <= 0)
230 return n;
231 lseek(afd->fd, len - n, 1);
232 return len;
233 }
234
235 #ifndef MDASSEMBLE
236 static void examine_super1(struct supertype *st, char *homehost)
237 {
238 struct mdp_superblock_1 *sb = st->sb;
239 time_t atime;
240 unsigned int d;
241 int role;
242 int delta_extra = 0;
243 int i;
244 char *c;
245 int l = homehost ? strlen(homehost) : 0;
246 int layout;
247 unsigned long long sb_offset;
248
249 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
250 printf(" Version : 1");
251 sb_offset = __le64_to_cpu(sb->super_offset);
252 if (sb_offset <= 4)
253 printf(".1\n");
254 else if (sb_offset <= 8)
255 printf(".2\n");
256 else
257 printf(".0\n");
258 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
259 printf(" Array UUID : ");
260 for (i=0; i<16; i++) {
261 if ((i&3)==0 && i != 0) printf(":");
262 printf("%02x", sb->set_uuid[i]);
263 }
264 printf("\n");
265 printf(" Name : %.32s", sb->set_name);
266 if (l > 0 && l < 32 &&
267 sb->set_name[l] == ':' &&
268 strncmp(sb->set_name, homehost, l) == 0)
269 printf(" (local to host %s)", homehost);
270 printf("\n");
271 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
272 printf(" Creation Time : %.24s\n", ctime(&atime));
273 c=map_num(pers, __le32_to_cpu(sb->level));
274 printf(" Raid Level : %s\n", c?c:"-unknown-");
275 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
276 printf("\n");
277 printf(" Avail Dev Size : %llu%s\n",
278 (unsigned long long)__le64_to_cpu(sb->data_size),
279 human_size(__le64_to_cpu(sb->data_size)<<9));
280 if (__le32_to_cpu(sb->level) > 0) {
281 int ddsks=0;
282 switch(__le32_to_cpu(sb->level)) {
283 case 1: ddsks=1;break;
284 case 4:
285 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
286 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
287 case 10:
288 layout = __le32_to_cpu(sb->layout);
289 ddsks = __le32_to_cpu(sb->raid_disks)
290 / (layout&255) / ((layout>>8)&255);
291 }
292 if (ddsks)
293 printf(" Array Size : %llu%s\n",
294 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
295 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
296 if (sb->size != sb->data_size)
297 printf(" Used Dev Size : %llu%s\n",
298 (unsigned long long)__le64_to_cpu(sb->size),
299 human_size(__le64_to_cpu(sb->size)<<9));
300 }
301 if (sb->data_offset)
302 printf(" Data Offset : %llu sectors\n",
303 (unsigned long long)__le64_to_cpu(sb->data_offset));
304 printf(" Super Offset : %llu sectors\n",
305 (unsigned long long)__le64_to_cpu(sb->super_offset));
306 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
307 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
308 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
309 printf(" Device UUID : ");
310 for (i=0; i<16; i++) {
311 if ((i&3)==0 && i != 0) printf(":");
312 printf("%02x", sb->device_uuid[i]);
313 }
314 printf("\n");
315 printf("\n");
316 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
317 printf("Internal Bitmap : %ld sectors from superblock\n",
318 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
319 }
320 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
321 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
322 human_size(__le64_to_cpu(sb->reshape_position)<<9));
323 if (__le32_to_cpu(sb->delta_disks)) {
324 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
325 printf(" (%d->%d)\n",
326 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
327 __le32_to_cpu(sb->raid_disks));
328 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
329 delta_extra = -__le32_to_cpu(sb->delta_disks);
330 }
331 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
332 c = map_num(pers, __le32_to_cpu(sb->new_level));
333 printf(" New Level : %s\n", c?c:"-unknown-");
334 }
335 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
336 if (__le32_to_cpu(sb->level) == 5) {
337 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
338 printf(" New Layout : %s\n", c?c:"-unknown-");
339 }
340 if (__le32_to_cpu(sb->level) == 6) {
341 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
342 printf(" New Layout : %s\n", c?c:"-unknown-");
343 }
344 if (__le32_to_cpu(sb->level) == 10) {
345 printf(" New Layout :");
346 print_r10_layout(__le32_to_cpu(sb->new_layout));
347 printf("\n");
348 }
349 }
350 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
351 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
352 printf("\n");
353 }
354 if (sb->devflags) {
355 printf(" Flags :");
356 if (sb->devflags & WriteMostly1)
357 printf(" write-mostly");
358 printf("\n");
359 }
360
361 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
362 printf(" Update Time : %.24s\n", ctime(&atime));
363
364 if (calc_sb_1_csum(sb) == sb->sb_csum)
365 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
366 else
367 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
368 __le32_to_cpu(calc_sb_1_csum(sb)));
369 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
370 printf("\n");
371 if (__le32_to_cpu(sb->level) == 5) {
372 c = map_num(r5layout, __le32_to_cpu(sb->layout));
373 printf(" Layout : %s\n", c?c:"-unknown-");
374 }
375 if (__le32_to_cpu(sb->level) == 6) {
376 c = map_num(r6layout, __le32_to_cpu(sb->layout));
377 printf(" Layout : %s\n", c?c:"-unknown-");
378 }
379 if (__le32_to_cpu(sb->level) == 10) {
380 int lo = __le32_to_cpu(sb->layout);
381 printf(" Layout :");
382 print_r10_layout(lo);
383 printf("\n");
384 }
385 switch(__le32_to_cpu(sb->level)) {
386 case 0:
387 case 4:
388 case 5:
389 case 6:
390 case 10:
391 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
392 break;
393 case -1:
394 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
395 break;
396 default: break;
397 }
398 printf("\n");
399 #if 0
400 /* This turns out to just be confusing */
401 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
402 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
403 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
404 break;
405 for (d=0; d < i; d++) {
406 int role = __le16_to_cpu(sb->dev_roles[d]);
407 if (d) printf(", ");
408 if (role == 0xffff) printf("empty");
409 else if(role == 0xfffe) printf("failed");
410 else printf("%d", role);
411 }
412 printf(")\n");
413 #endif
414 printf(" Device Role : ");
415 d = __le32_to_cpu(sb->dev_number);
416 if (d < __le32_to_cpu(sb->max_dev))
417 role = __le16_to_cpu(sb->dev_roles[d]);
418 else
419 role = 0xFFFF;
420 if (role >= 0xFFFE)
421 printf("spare\n");
422 else
423 printf("Active device %d\n", role);
424
425 printf(" Array State : ");
426 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
427 int cnt = 0;
428 unsigned int i;
429 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
430 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
431 if (role == d)
432 cnt++;
433 }
434 if (cnt > 1) printf("?");
435 else if (cnt == 1) printf("A");
436 else printf (".");
437 }
438 #if 0
439 /* This is confusing too */
440 faulty = 0;
441 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
442 int role = __le16_to_cpu(sb->dev_roles[i]);
443 if (role == 0xFFFE)
444 faulty++;
445 }
446 if (faulty) printf(" %d failed", faulty);
447 #endif
448 printf(" ('A' == active, '.' == missing)");
449 printf("\n");
450 }
451
452
453 static void brief_examine_super1(struct supertype *st, int verbose)
454 {
455 struct mdp_superblock_1 *sb = st->sb;
456 int i;
457 unsigned long long sb_offset;
458 char *nm;
459 char *c=map_num(pers, __le32_to_cpu(sb->level));
460
461 nm = strchr(sb->set_name, ':');
462 if (nm)
463 nm++;
464 else if (sb->set_name[0])
465 nm = sb->set_name;
466 else
467 nm = NULL;
468
469 printf("ARRAY%s%s", nm ? " /dev/md/":"", nm);
470 if (verbose && c)
471 printf(" level=%s", c);
472 sb_offset = __le64_to_cpu(sb->super_offset);
473 if (sb_offset <= 4)
474 printf(" metadata=1.1 ");
475 else if (sb_offset <= 8)
476 printf(" metadata=1.2 ");
477 else
478 printf(" metadata=1.0 ");
479 if (verbose)
480 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
481 printf("UUID=");
482 for (i=0; i<16; i++) {
483 if ((i&3)==0 && i != 0) printf(":");
484 printf("%02x", sb->set_uuid[i]);
485 }
486 if (sb->set_name[0])
487 printf(" name=%.32s", sb->set_name);
488 printf("\n");
489 }
490
491 static void export_examine_super1(struct supertype *st)
492 {
493 struct mdp_superblock_1 *sb = st->sb;
494 int i;
495 int len = 32;
496
497 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
498 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
499 for (i=0; i<32; i++)
500 if (sb->set_name[i] == '\n' ||
501 sb->set_name[i] == '\0') {
502 len = i;
503 break;
504 }
505 if (len)
506 printf("MD_NAME=%.*s\n", len, sb->set_name);
507 printf("MD_UUID=");
508 for (i=0; i<16; i++) {
509 if ((i&3)==0 && i != 0) printf(":");
510 printf("%02x", sb->set_uuid[i]);
511 }
512 printf("\n");
513 printf("MD_UPDATE_TIME=%llu\n",
514 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
515 printf("MD_DEV_UUID=");
516 for (i=0; i<16; i++) {
517 if ((i&3)==0 && i != 0) printf(":");
518 printf("%02x", sb->device_uuid[i]);
519 }
520 printf("\n");
521 printf("MD_EVENTS=%llu\n",
522 (unsigned long long)__le64_to_cpu(sb->events));
523 }
524
525 static void detail_super1(struct supertype *st, char *homehost)
526 {
527 struct mdp_superblock_1 *sb = st->sb;
528 int i;
529 int l = homehost ? strlen(homehost) : 0;
530
531 printf(" Name : %.32s", sb->set_name);
532 if (l > 0 && l < 32 &&
533 sb->set_name[l] == ':' &&
534 strncmp(sb->set_name, homehost, l) == 0)
535 printf(" (local to host %s)", homehost);
536 printf("\n UUID : ");
537 for (i=0; i<16; i++) {
538 if ((i&3)==0 && i != 0) printf(":");
539 printf("%02x", sb->set_uuid[i]);
540 }
541 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
542 }
543
544 static void brief_detail_super1(struct supertype *st)
545 {
546 struct mdp_superblock_1 *sb = st->sb;
547 int i;
548
549 if (sb->set_name[0])
550 printf(" name=%.32s", sb->set_name);
551 printf(" UUID=");
552 for (i=0; i<16; i++) {
553 if ((i&3)==0 && i != 0) printf(":");
554 printf("%02x", sb->set_uuid[i]);
555 }
556 }
557
558 static void export_detail_super1(struct supertype *st)
559 {
560 struct mdp_superblock_1 *sb = st->sb;
561 int i;
562 int len = 32;
563
564 for (i=0; i<32; i++)
565 if (sb->set_name[i] == '\n' ||
566 sb->set_name[i] == '\0') {
567 len = i;
568 break;
569 }
570 if (len)
571 printf("MD_NAME=%.*s\n", len, sb->set_name);
572 }
573
574 #endif
575
576 static int match_home1(struct supertype *st, char *homehost)
577 {
578 struct mdp_superblock_1 *sb = st->sb;
579 int l = homehost ? strlen(homehost) : 0;
580
581 return (l > 0 && l < 32 &&
582 sb->set_name[l] == ':' &&
583 strncmp(sb->set_name, homehost, l) == 0);
584 }
585
586 static void uuid_from_super1(struct supertype *st, int uuid[4])
587 {
588 struct mdp_superblock_1 *super = st->sb;
589 char *cuuid = (char*)uuid;
590 int i;
591 for (i=0; i<16; i++)
592 cuuid[i] = super->set_uuid[i];
593 }
594
595 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
596 {
597 struct mdp_superblock_1 *sb = st->sb;
598 int working = 0;
599 unsigned int i;
600 unsigned int role;
601 unsigned int map_disks = info->array.raid_disks;
602
603 memset(info, 0, sizeof(*info));
604 info->array.major_version = 1;
605 info->array.minor_version = st->minor_version;
606 info->array.patch_version = 0;
607 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
608 info->array.level = __le32_to_cpu(sb->level);
609 info->array.layout = __le32_to_cpu(sb->layout);
610 info->array.md_minor = -1;
611 info->array.ctime = __le64_to_cpu(sb->ctime);
612 info->array.utime = __le64_to_cpu(sb->utime);
613 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
614 info->array.state =
615 (__le64_to_cpu(sb->resync_offset) == MaxSector)
616 ? 1 : 0;
617
618 info->data_offset = __le64_to_cpu(sb->data_offset);
619 info->component_size = __le64_to_cpu(sb->size);
620 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
621 info->bitmap_offset = __le32_to_cpu(sb->bitmap_offset);
622
623 info->disk.major = 0;
624 info->disk.minor = 0;
625 info->disk.number = __le32_to_cpu(sb->dev_number);
626 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
627 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
628 role = 0xfffe;
629 else
630 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
631
632 info->disk.raid_disk = -1;
633 switch(role) {
634 case 0xFFFF:
635 info->disk.state = 0; /* spare: not active, not sync, not faulty */
636 break;
637 case 0xFFFE:
638 info->disk.state = 1; /* faulty */
639 break;
640 default:
641 info->disk.state = 6; /* active and in sync */
642 info->disk.raid_disk = role;
643 }
644 if (sb->devflags & WriteMostly1)
645 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
646 info->events = __le64_to_cpu(sb->events);
647 sprintf(info->text_version, "1.%d", st->minor_version);
648 info->safe_mode_delay = 200;
649
650 memcpy(info->uuid, sb->set_uuid, 16);
651
652 strncpy(info->name, sb->set_name, 32);
653 info->name[32] = 0;
654
655 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
656 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
657 else
658 info->recovery_start = MaxSector;
659
660 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
661 info->reshape_active = 1;
662 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
663 info->new_level = __le32_to_cpu(sb->new_level);
664 info->delta_disks = __le32_to_cpu(sb->delta_disks);
665 info->new_layout = __le32_to_cpu(sb->new_layout);
666 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
667 if (info->delta_disks < 0)
668 info->array.raid_disks -= info->delta_disks;
669 } else
670 info->reshape_active = 0;
671
672 info->recovery_blocked = info->reshape_active;
673
674 if (map)
675 for (i=0; i<map_disks; i++)
676 map[i] = 0;
677 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
678 role = __le16_to_cpu(sb->dev_roles[i]);
679 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
680 working++;
681 if (map && role < map_disks)
682 map[role] = 1;
683 }
684 }
685
686 info->array.working_disks = working;
687 }
688
689 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
690 {
691 struct mdinfo *info;
692
693 if (subarray)
694 return NULL;
695
696 info = malloc(sizeof(*info));
697 getinfo_super1(st, info, NULL);
698 return info;
699 }
700
701 static int update_super1(struct supertype *st, struct mdinfo *info,
702 char *update,
703 char *devname, int verbose,
704 int uuid_set, char *homehost)
705 {
706 /* NOTE: for 'assemble' and 'force' we need to return non-zero
707 * if any change was made. For others, the return value is
708 * ignored.
709 */
710 int rv = 0;
711 struct mdp_superblock_1 *sb = st->sb;
712
713 if (strcmp(update, "force-one")==0) {
714 /* Not enough devices for a working array,
715 * so bring this one up-to-date
716 */
717 if (sb->events != __cpu_to_le64(info->events))
718 rv = 1;
719 sb->events = __cpu_to_le64(info->events);
720 } else if (strcmp(update, "force-array")==0) {
721 /* Degraded array and 'force' requests to
722 * maybe need to mark it 'clean'.
723 */
724 switch(__le32_to_cpu(sb->level)) {
725 case 5: case 4: case 6:
726 /* need to force clean */
727 if (sb->resync_offset != MaxSector)
728 rv = 1;
729 sb->resync_offset = MaxSector;
730 }
731 } else if (strcmp(update, "assemble")==0) {
732 int d = info->disk.number;
733 int want;
734 if (info->disk.state == 6)
735 want = info->disk.raid_disk;
736 else
737 want = 0xFFFF;
738 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
739 sb->dev_roles[d] = __cpu_to_le16(want);
740 rv = 1;
741 }
742 if (info->reshape_active &&
743 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
744 info->delta_disks >= 0 &&
745 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
746 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
747 rv = 1;
748 }
749 if (info->reshape_active &&
750 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
751 info->delta_disks < 0 &&
752 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
753 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
754 rv = 1;
755 }
756 } else if (strcmp(update, "linear-grow-new") == 0) {
757 unsigned int i;
758 int rfd, fd;
759 unsigned int max = __le32_to_cpu(sb->max_dev);
760
761 for (i=0 ; i < max ; i++)
762 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
763 break;
764 sb->dev_number = __cpu_to_le32(i);
765 info->disk.number = i;
766 if (max >= __le32_to_cpu(sb->max_dev))
767 sb->max_dev = __cpu_to_le32(max+1);
768
769 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
770 read(rfd, sb->device_uuid, 16) != 16) {
771 __u32 r[4] = {random(), random(), random(), random()};
772 memcpy(sb->device_uuid, r, 16);
773 }
774 if (rfd >= 0)
775 close(rfd);
776
777 sb->dev_roles[i] =
778 __cpu_to_le16(info->disk.raid_disk);
779
780 fd = open(devname, O_RDONLY);
781 if (fd >= 0) {
782 unsigned long long ds;
783 get_dev_size(fd, devname, &ds);
784 close(fd);
785 ds >>= 9;
786 if (__le64_to_cpu(sb->super_offset) <
787 __le64_to_cpu(sb->data_offset)) {
788 sb->data_size = __cpu_to_le64(
789 ds - __le64_to_cpu(sb->data_offset));
790 } else {
791 ds -= 8*2;
792 ds &= ~(unsigned long long)(4*2-1);
793 sb->super_offset = __cpu_to_le64(ds);
794 sb->data_size = __cpu_to_le64(
795 ds - __le64_to_cpu(sb->data_offset));
796 }
797 }
798 } else if (strcmp(update, "linear-grow-update") == 0) {
799 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
800 sb->dev_roles[info->disk.number] =
801 __cpu_to_le16(info->disk.raid_disk);
802 } else if (strcmp(update, "resync") == 0) {
803 /* make sure resync happens */
804 sb->resync_offset = 0ULL;
805 } else if (strcmp(update, "uuid") == 0) {
806 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
807
808 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
809 struct bitmap_super_s *bm;
810 bm = (struct bitmap_super_s*)(st->sb+MAX_SB_SIZE);
811 memcpy(bm->uuid, sb->set_uuid, 16);
812 }
813 } else if (strcmp(update, "no-bitmap") == 0) {
814 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
815 } else if (strcmp(update, "homehost") == 0 &&
816 homehost) {
817 char *c;
818 update = "name";
819 c = strchr(sb->set_name, ':');
820 if (c)
821 strncpy(info->name, c+1, 31 - (c-sb->set_name));
822 else
823 strncpy(info->name, sb->set_name, 32);
824 info->name[32] = 0;
825 } else if (strcmp(update, "name") == 0) {
826 if (info->name[0] == 0)
827 sprintf(info->name, "%d", info->array.md_minor);
828 memset(sb->set_name, 0, sizeof(sb->set_name));
829 if (homehost &&
830 strchr(info->name, ':') == NULL &&
831 strlen(homehost)+1+strlen(info->name) < 32) {
832 strcpy(sb->set_name, homehost);
833 strcat(sb->set_name, ":");
834 strcat(sb->set_name, info->name);
835 } else
836 strcpy(sb->set_name, info->name);
837 } else if (strcmp(update, "devicesize") == 0 &&
838 __le64_to_cpu(sb->super_offset) <
839 __le64_to_cpu(sb->data_offset)) {
840 /* set data_size to device size less data_offset */
841 struct misc_dev_info *misc = (struct misc_dev_info*)
842 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
843 printf("Size was %llu\n", (unsigned long long)
844 __le64_to_cpu(sb->data_size));
845 sb->data_size = __cpu_to_le64(
846 misc->device_size - __le64_to_cpu(sb->data_offset));
847 printf("Size is %llu\n", (unsigned long long)
848 __le64_to_cpu(sb->data_size));
849 } else if (strcmp(update, "_reshape_progress")==0)
850 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
851 else if (strcmp(update, "writemostly")==0)
852 sb->devflags |= WriteMostly1;
853 else if (strcmp(update, "readwrite")==0)
854 sb->devflags &= ~WriteMostly1;
855 else
856 rv = -1;
857
858 sb->sb_csum = calc_sb_1_csum(sb);
859 return rv;
860 }
861
862 static int init_super1(struct supertype *st, mdu_array_info_t *info,
863 unsigned long long size, char *name, char *homehost, int *uuid)
864 {
865 struct mdp_superblock_1 *sb;
866 int spares;
867 int rfd;
868 char defname[10];
869 int sbsize;
870
871 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
872 fprintf(stderr, Name
873 ": %s could not allocate superblock\n", __func__);
874 return 0;
875 }
876 memset(sb, 0, SUPER1_SIZE);
877
878 st->sb = sb;
879 if (info == NULL) {
880 /* zeroing superblock */
881 return 0;
882 }
883
884 spares = info->working_disks - info->active_disks;
885 if (info->raid_disks + spares > MAX_DEVS) {
886 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
887 info->raid_disks , spares, MAX_DEVS);
888 return 0;
889 }
890
891 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
892 sb->major_version = __cpu_to_le32(1);
893 sb->feature_map = 0;
894 sb->pad0 = 0;
895
896 if (uuid)
897 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
898 else {
899 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
900 read(rfd, sb->set_uuid, 16) != 16) {
901 __u32 r[4] = {random(), random(), random(), random()};
902 memcpy(sb->set_uuid, r, 16);
903 }
904 if (rfd >= 0) close(rfd);
905 }
906
907 if (name == NULL || *name == 0) {
908 sprintf(defname, "%d", info->md_minor);
909 name = defname;
910 }
911 if (homehost &&
912 strchr(name, ':')== NULL &&
913 strlen(homehost)+1+strlen(name) < 32) {
914 strcpy(sb->set_name, homehost);
915 strcat(sb->set_name, ":");
916 strcat(sb->set_name, name);
917 } else
918 strcpy(sb->set_name, name);
919
920 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
921 sb->level = __cpu_to_le32(info->level);
922 sb->layout = __cpu_to_le32(info->layout);
923 sb->size = __cpu_to_le64(size*2ULL);
924 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
925 sb->raid_disks = __cpu_to_le32(info->raid_disks);
926
927 sb->data_offset = __cpu_to_le64(0);
928 sb->data_size = __cpu_to_le64(0);
929 sb->super_offset = __cpu_to_le64(0);
930 sb->recovery_offset = __cpu_to_le64(0);
931
932 sb->utime = sb->ctime;
933 sb->events = __cpu_to_le64(1);
934 if (info->state & (1<<MD_SB_CLEAN))
935 sb->resync_offset = MaxSector;
936 else
937 sb->resync_offset = 0;
938 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
939 sbsize = ROUND_UP(sbsize, 512);
940 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
941
942 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
943
944 return 1;
945 }
946
947 struct devinfo {
948 int fd;
949 char *devname;
950 mdu_disk_info_t disk;
951 struct devinfo *next;
952 };
953 #ifndef MDASSEMBLE
954 /* Add a device to the superblock being created */
955 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
956 int fd, char *devname)
957 {
958 struct mdp_superblock_1 *sb = st->sb;
959 __u16 *rp = sb->dev_roles + dk->number;
960 struct devinfo *di, **dip;
961
962 if ((dk->state & 6) == 6) /* active, sync */
963 *rp = __cpu_to_le16(dk->raid_disk);
964 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
965 *rp = 0xffff;
966 else
967 *rp = 0xfffe;
968
969 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
970 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
971 sb->max_dev = __cpu_to_le32(dk->number+1);
972
973 sb->dev_number = __cpu_to_le32(dk->number);
974 sb->devflags = 0; /* don't copy another disks flags */
975 sb->sb_csum = calc_sb_1_csum(sb);
976
977 dip = (struct devinfo **)&st->info;
978 while (*dip)
979 dip = &(*dip)->next;
980 di = malloc(sizeof(struct devinfo));
981 di->fd = fd;
982 di->devname = devname;
983 di->disk = *dk;
984 di->next = NULL;
985 *dip = di;
986
987 return 0;
988 }
989 #endif
990
991 static void locate_bitmap1(struct supertype *st, int fd);
992
993 static int store_super1(struct supertype *st, int fd)
994 {
995 struct mdp_superblock_1 *sb = st->sb;
996 unsigned long long sb_offset;
997 struct align_fd afd;
998 int sbsize;
999 unsigned long long dsize;
1000
1001 if (!get_dev_size(fd, NULL, &dsize))
1002 return 1;
1003
1004 dsize >>= 9;
1005
1006 if (dsize < 24)
1007 return 2;
1008
1009 init_afd(&afd, fd);
1010
1011 /*
1012 * Calculate the position of the superblock.
1013 * It is always aligned to a 4K boundary and
1014 * depending on minor_version, it can be:
1015 * 0: At least 8K, but less than 12K, from end of device
1016 * 1: At start of device
1017 * 2: 4K from start of device.
1018 */
1019 switch(st->minor_version) {
1020 case 0:
1021 sb_offset = dsize;
1022 sb_offset -= 8*2;
1023 sb_offset &= ~(4*2-1);
1024 break;
1025 case 1:
1026 sb_offset = 0;
1027 break;
1028 case 2:
1029 sb_offset = 4*2;
1030 break;
1031 default:
1032 return -EINVAL;
1033 }
1034
1035
1036
1037 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1038 0 != __le64_to_cpu(sb->super_offset)
1039 ) {
1040 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
1041 abort();
1042 }
1043
1044 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1045 return 3;
1046
1047 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1048
1049 if (awrite(&afd, sb, sbsize) != sbsize)
1050 return 4;
1051
1052 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1053 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1054 (((char*)sb)+MAX_SB_SIZE);
1055 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1056 locate_bitmap1(st, fd);
1057 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1058 return 5;
1059 }
1060 }
1061 fsync(fd);
1062 return 0;
1063 }
1064
1065 static int load_super1(struct supertype *st, int fd, char *devname);
1066
1067 static unsigned long choose_bm_space(unsigned long devsize)
1068 {
1069 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1070 * if bigger than 200Gig, save 128k
1071 * NOTE: result must be multiple of 4K else bad things happen
1072 * on 4K-sector devices.
1073 */
1074 if (devsize < 64*2) return 0;
1075 if (devsize - 64*2 >= 200*1024*1024*2)
1076 return 128*2;
1077 if (devsize - 4*2 > 8*1024*1024*2)
1078 return 64*2;
1079 return 4*2;
1080 }
1081
1082 static void free_super1(struct supertype *st);
1083
1084 #ifndef MDASSEMBLE
1085 static int write_init_super1(struct supertype *st)
1086 {
1087 struct mdp_superblock_1 *sb = st->sb;
1088 struct supertype *refst;
1089 int rfd;
1090 int rv = 0;
1091 unsigned long long bm_space;
1092 unsigned long long reserved;
1093 struct devinfo *di;
1094 unsigned long long dsize, array_size;
1095 unsigned long long sb_offset;
1096
1097 for (di = st->info; di && ! rv ; di = di->next) {
1098 if (di->disk.state == 1)
1099 continue;
1100 if (di->fd < 0)
1101 continue;
1102
1103 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
1104 ;
1105
1106 sb->dev_number = __cpu_to_le32(di->disk.number);
1107 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1108 sb->devflags |= WriteMostly1;
1109 else
1110 sb->devflags &= ~WriteMostly1;
1111
1112 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1113 read(rfd, sb->device_uuid, 16) != 16) {
1114 __u32 r[4] = {random(), random(), random(), random()};
1115 memcpy(sb->device_uuid, r, 16);
1116 }
1117 if (rfd >= 0)
1118 close(rfd);
1119
1120 sb->events = 0;
1121
1122 refst = dup_super(st);
1123 if (load_super1(refst, di->fd, NULL)==0) {
1124 struct mdp_superblock_1 *refsb = refst->sb;
1125
1126 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1127 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1128 /* same array, so preserve events and
1129 * dev_number */
1130 sb->events = refsb->events;
1131 /* bugs in 2.6.17 and earlier mean the
1132 * dev_number chosen in Manage must be preserved
1133 */
1134 if (get_linux_version() >= 2006018)
1135 sb->dev_number = refsb->dev_number;
1136 }
1137 free_super1(refst);
1138 }
1139 free(refst);
1140
1141 if (!get_dev_size(di->fd, NULL, &dsize)) {
1142 rv = 1;
1143 goto error_out;
1144 }
1145 dsize >>= 9;
1146
1147 if (dsize < 24) {
1148 close(di->fd);
1149 rv = 2;
1150 goto error_out;
1151 }
1152
1153
1154 /*
1155 * Calculate the position of the superblock.
1156 * It is always aligned to a 4K boundary and
1157 * depending on minor_version, it can be:
1158 * 0: At least 8K, but less than 12K, from end of device
1159 * 1: At start of device
1160 * 2: 4K from start of device.
1161 * Depending on the array size, we might leave extra space
1162 * for a bitmap.
1163 */
1164 array_size = __le64_to_cpu(sb->size);
1165 /* work out how much space we left for a bitmap */
1166 bm_space = choose_bm_space(array_size);
1167
1168 switch(st->minor_version) {
1169 case 0:
1170 sb_offset = dsize;
1171 sb_offset -= 8*2;
1172 sb_offset &= ~(4*2-1);
1173 sb->super_offset = __cpu_to_le64(sb_offset);
1174 sb->data_offset = __cpu_to_le64(0);
1175 if (sb_offset < array_size + bm_space)
1176 bm_space = sb_offset - array_size;
1177 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1178 break;
1179 case 1:
1180 sb->super_offset = __cpu_to_le64(0);
1181 reserved = bm_space + 4*2;
1182 /* Try for multiple of 1Meg so it is nicely aligned */
1183 #define ONE_MEG (2*1024)
1184 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1185 if (reserved + __le64_to_cpu(sb->size) > dsize)
1186 reserved = dsize - __le64_to_cpu(sb->size);
1187 /* force 4K alignment */
1188 reserved &= ~7ULL;
1189
1190 sb->data_offset = __cpu_to_le64(reserved);
1191 sb->data_size = __cpu_to_le64(dsize - reserved);
1192 break;
1193 case 2:
1194 sb_offset = 4*2;
1195 sb->super_offset = __cpu_to_le64(4*2);
1196 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1197 > dsize)
1198 bm_space = dsize - __le64_to_cpu(sb->size)
1199 - 4*2 - 4*2;
1200
1201 reserved = bm_space + 4*2 + 4*2;
1202 /* Try for multiple of 1Meg so it is nicely aligned */
1203 #define ONE_MEG (2*1024)
1204 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1205 if (reserved + __le64_to_cpu(sb->size) > dsize)
1206 reserved = dsize - __le64_to_cpu(sb->size);
1207 /* force 4K alignment */
1208 reserved &= ~7ULL;
1209
1210 sb->data_offset = __cpu_to_le64(reserved);
1211 sb->data_size = __cpu_to_le64(dsize - reserved);
1212 break;
1213 default:
1214 fprintf(stderr, Name ": Failed to write invalid "
1215 "metadata format 1.%i to %s\n",
1216 st->minor_version, di->devname);
1217 rv = -EINVAL;
1218 goto out;
1219 }
1220
1221
1222 sb->sb_csum = calc_sb_1_csum(sb);
1223 rv = store_super1(st, di->fd);
1224 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1225 rv = st->ss->write_bitmap(st, di->fd);
1226 close(di->fd);
1227 di->fd = -1;
1228 }
1229 error_out:
1230 if (rv)
1231 fprintf(stderr, Name ": Failed to write metadata to %s\n",
1232 di->devname);
1233 out:
1234 return rv;
1235 }
1236 #endif
1237
1238 static int compare_super1(struct supertype *st, struct supertype *tst)
1239 {
1240 /*
1241 * return:
1242 * 0 same, or first was empty, and second was copied
1243 * 1 second had wrong number
1244 * 2 wrong uuid
1245 * 3 wrong other info
1246 */
1247 struct mdp_superblock_1 *first = st->sb;
1248 struct mdp_superblock_1 *second = tst->sb;
1249
1250 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1251 return 1;
1252 if (second->major_version != __cpu_to_le32(1))
1253 return 1;
1254
1255 if (!first) {
1256 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1257 fprintf(stderr, Name
1258 ": %s could not allocate superblock\n", __func__);
1259 return 1;
1260 }
1261 memcpy(first, second, SUPER1_SIZE);
1262 st->sb = first;
1263 return 0;
1264 }
1265 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1266 return 2;
1267
1268 if (first->ctime != second->ctime ||
1269 first->level != second->level ||
1270 first->layout != second->layout ||
1271 first->size != second->size ||
1272 first->chunksize != second->chunksize ||
1273 first->raid_disks != second->raid_disks)
1274 return 3;
1275 return 0;
1276 }
1277
1278 static int load_super1(struct supertype *st, int fd, char *devname)
1279 {
1280 unsigned long long dsize;
1281 unsigned long long sb_offset;
1282 struct mdp_superblock_1 *super;
1283 int uuid[4];
1284 struct bitmap_super_s *bsb;
1285 struct misc_dev_info *misc;
1286 struct align_fd afd;
1287
1288 free_super1(st);
1289
1290 init_afd(&afd, fd);
1291
1292 if (st->ss == NULL || st->minor_version == -1) {
1293 int bestvers = -1;
1294 struct supertype tst;
1295 __u64 bestctime = 0;
1296 /* guess... choose latest ctime */
1297 memset(&tst, 0, sizeof(tst));
1298 tst.ss = &super1;
1299 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1300 switch(load_super1(&tst, fd, devname)) {
1301 case 0: super = tst.sb;
1302 if (bestvers == -1 ||
1303 bestctime < __le64_to_cpu(super->ctime)) {
1304 bestvers = tst.minor_version;
1305 bestctime = __le64_to_cpu(super->ctime);
1306 }
1307 free(super);
1308 tst.sb = NULL;
1309 break;
1310 case 1: return 1; /*bad device */
1311 case 2: break; /* bad, try next */
1312 }
1313 }
1314 if (bestvers != -1) {
1315 int rv;
1316 tst.minor_version = bestvers;
1317 tst.ss = &super1;
1318 tst.max_devs = MAX_DEVS;
1319 rv = load_super1(&tst, fd, devname);
1320 if (rv == 0)
1321 *st = tst;
1322 return rv;
1323 }
1324 return 2;
1325 }
1326 if (!get_dev_size(fd, devname, &dsize))
1327 return 1;
1328 dsize >>= 9;
1329
1330 if (dsize < 24) {
1331 if (devname)
1332 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1333 devname, dsize);
1334 return 1;
1335 }
1336
1337 /*
1338 * Calculate the position of the superblock.
1339 * It is always aligned to a 4K boundary and
1340 * depending on minor_version, it can be:
1341 * 0: At least 8K, but less than 12K, from end of device
1342 * 1: At start of device
1343 * 2: 4K from start of device.
1344 */
1345 switch(st->minor_version) {
1346 case 0:
1347 sb_offset = dsize;
1348 sb_offset -= 8*2;
1349 sb_offset &= ~(4*2-1);
1350 break;
1351 case 1:
1352 sb_offset = 0;
1353 break;
1354 case 2:
1355 sb_offset = 4*2;
1356 break;
1357 default:
1358 return -EINVAL;
1359 }
1360
1361 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1362
1363
1364 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1365 if (devname)
1366 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1367 devname, strerror(errno));
1368 return 1;
1369 }
1370
1371 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1372 fprintf(stderr, Name ": %s could not allocate superblock\n",
1373 __func__);
1374 return 1;
1375 }
1376
1377 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
1378 if (devname)
1379 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1380 devname);
1381 free(super);
1382 return 1;
1383 }
1384
1385 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1386 if (devname)
1387 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1388 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1389 free(super);
1390 return 2;
1391 }
1392
1393 if (__le32_to_cpu(super->major_version) != 1) {
1394 if (devname)
1395 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1396 devname, __le32_to_cpu(super->major_version));
1397 free(super);
1398 return 2;
1399 }
1400 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1401 if (devname)
1402 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1403 devname);
1404 free(super);
1405 return 2;
1406 }
1407 st->sb = super;
1408
1409 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1410
1411 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
1412 misc->device_size = dsize;
1413
1414 /* Now check on the bitmap superblock */
1415 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1416 return 0;
1417 /* Read the bitmap superblock and make sure it looks
1418 * valid. If it doesn't clear the bit. An --assemble --force
1419 * should get that written out.
1420 */
1421 locate_bitmap1(st, fd);
1422 if (aread(&afd, bsb, 512) != 512)
1423 goto no_bitmap;
1424
1425 uuid_from_super1(st, uuid);
1426 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1427 memcmp(bsb->uuid, uuid, 16) != 0)
1428 goto no_bitmap;
1429 return 0;
1430
1431 no_bitmap:
1432 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
1433 & ~MD_FEATURE_BITMAP_OFFSET);
1434 return 0;
1435 }
1436
1437
1438 static struct supertype *match_metadata_desc1(char *arg)
1439 {
1440 struct supertype *st = calloc(1, sizeof(*st));
1441 if (!st)
1442 return st;
1443
1444 st->container_dev = NoMdDev;
1445 st->ss = &super1;
1446 st->max_devs = MAX_DEVS;
1447 st->sb = NULL;
1448 /* leading zeros can be safely ignored. --detail generates them. */
1449 while (*arg == '0')
1450 arg++;
1451 if (strcmp(arg, "1.0") == 0 ||
1452 strcmp(arg, "1.00") == 0) {
1453 st->minor_version = 0;
1454 return st;
1455 }
1456 if (strcmp(arg, "1.1") == 0 ||
1457 strcmp(arg, "1.01") == 0
1458 ) {
1459 st->minor_version = 1;
1460 return st;
1461 }
1462 if (strcmp(arg, "1.2") == 0 ||
1463 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1464 strcmp(arg, "default") == 0 ||
1465 #endif /* DEFAULT_OLD_METADATA */
1466 strcmp(arg, "1.02") == 0) {
1467 st->minor_version = 2;
1468 return st;
1469 }
1470 if (strcmp(arg, "1") == 0 ||
1471 strcmp(arg, "default") == 0) {
1472 st->minor_version = -1;
1473 return st;
1474 }
1475
1476 free(st);
1477 return NULL;
1478 }
1479
1480 /* find available size on device with this devsize, using
1481 * superblock type st, and reserving 'reserve' sectors for
1482 * a possible bitmap
1483 */
1484 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1485 {
1486 struct mdp_superblock_1 *super = st->sb;
1487 if (devsize < 24)
1488 return 0;
1489
1490 if (super == NULL)
1491 /* creating: allow suitable space for bitmap */
1492 devsize -= choose_bm_space(devsize);
1493 #ifndef MDASSEMBLE
1494 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1495 /* hot-add. allow for actual size of bitmap */
1496 struct bitmap_super_s *bsb;
1497 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1498 devsize -= bitmap_sectors(bsb);
1499 }
1500 #endif
1501
1502 if (st->minor_version < 0)
1503 /* not specified, so time to set default */
1504 st->minor_version = 2;
1505 if (super == NULL && st->minor_version > 0) {
1506 /* haven't committed to a size yet, so allow some
1507 * slack for alignment of data_offset.
1508 * We haven't access to device details so allow
1509 * 1 Meg if bigger than 1Gig
1510 */
1511 if (devsize > 1024*1024*2)
1512 devsize -= 1024*2;
1513 }
1514 switch(st->minor_version) {
1515 case 0:
1516 /* at end */
1517 return ((devsize - 8*2 ) & ~(4*2-1));
1518 case 1:
1519 /* at start, 4K for superblock and possible bitmap */
1520 return devsize - 4*2;
1521 case 2:
1522 /* 4k from start, 4K for superblock and possible bitmap */
1523 return devsize - (4+4)*2;
1524 }
1525 return 0;
1526 }
1527
1528 static int
1529 add_internal_bitmap1(struct supertype *st,
1530 int *chunkp, int delay, int write_behind,
1531 unsigned long long size,
1532 int may_change, int major)
1533 {
1534 /*
1535 * If not may_change, then this is a 'Grow' without sysfs support for
1536 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1537 * If may_change, then this is create or a Grow with sysfs syupport,
1538 * and we can put the bitmap wherever we like.
1539 *
1540 * size is in sectors, chunk is in bytes !!!
1541 */
1542
1543 unsigned long long bits;
1544 unsigned long long max_bits;
1545 unsigned long long min_chunk;
1546 long offset;
1547 unsigned long long chunk = *chunkp;
1548 int room = 0;
1549 int creating = 0;
1550 struct mdp_superblock_1 *sb = st->sb;
1551 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1552 int uuid[4];
1553
1554 if (__le64_to_cpu(sb->data_size) == 0)
1555 /* Must be creating the array, else data_size would be non-zero */
1556 creating = 1;
1557 switch(st->minor_version) {
1558 case 0:
1559 /* either 3K after the superblock (when hot-add),
1560 * or some amount of space before.
1561 */
1562 if (creating) {
1563 /* We are creating array, so we *know* how much room has
1564 * been left.
1565 */
1566 offset = 0;
1567 room = choose_bm_space(__le64_to_cpu(sb->size));
1568 } else {
1569 room = __le64_to_cpu(sb->super_offset)
1570 - __le64_to_cpu(sb->data_offset)
1571 - __le64_to_cpu(sb->data_size);
1572
1573 if (!may_change || (room < 3*2 &&
1574 __le32_to_cpu(sb->max_dev) <= 384)) {
1575 room = 3*2;
1576 offset = 1*2;
1577 } else {
1578 offset = 0; /* means movable offset */
1579 }
1580 }
1581 break;
1582 case 1:
1583 case 2: /* between superblock and data */
1584 if (creating) {
1585 offset = 4*2;
1586 room = choose_bm_space(__le64_to_cpu(sb->size));
1587 } else {
1588 room = __le64_to_cpu(sb->data_offset)
1589 - __le64_to_cpu(sb->super_offset);
1590 if (!may_change) {
1591 room -= 2; /* Leave 1K for superblock */
1592 offset = 2;
1593 } else {
1594 room -= 4*2; /* leave 4K for superblock */
1595 offset = 4*2;
1596 }
1597 }
1598 break;
1599 default:
1600 return 0;
1601 }
1602
1603 if (chunk == UnSet && room > 128*2)
1604 /* Limit to 128K of bitmap when chunk size not requested */
1605 room = 128*2;
1606
1607 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1608
1609 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1610 bits = (size*512)/min_chunk +1;
1611 while (bits > max_bits) {
1612 min_chunk *= 2;
1613 bits = (bits+1)/2;
1614 }
1615 if (chunk == UnSet) {
1616 /* For practical purpose, 64Meg is a good
1617 * default chunk size for internal bitmaps.
1618 */
1619 chunk = min_chunk;
1620 if (chunk < 64*1024*1024)
1621 chunk = 64*1024*1024;
1622 } else if (chunk < min_chunk)
1623 return 0; /* chunk size too small */
1624 if (chunk == 0) /* rounding problem */
1625 return 0;
1626
1627 if (offset == 0) {
1628 /* start bitmap on a 4K boundary with enough space for
1629 * the bitmap
1630 */
1631 bits = (size*512) / chunk + 1;
1632 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1633 room *= 8; /* convert 4K blocks to sectors */
1634 offset = -room;
1635 }
1636
1637 sb->bitmap_offset = __cpu_to_le32(offset);
1638
1639 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
1640 | MD_FEATURE_BITMAP_OFFSET);
1641 memset(bms, 0, sizeof(*bms));
1642 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1643 bms->version = __cpu_to_le32(major);
1644 uuid_from_super1(st, uuid);
1645 memcpy(bms->uuid, uuid, 16);
1646 bms->chunksize = __cpu_to_le32(chunk);
1647 bms->daemon_sleep = __cpu_to_le32(delay);
1648 bms->sync_size = __cpu_to_le64(size);
1649 bms->write_behind = __cpu_to_le32(write_behind);
1650
1651 *chunkp = chunk;
1652 return 1;
1653 }
1654
1655 static void locate_bitmap1(struct supertype *st, int fd)
1656 {
1657 unsigned long long offset;
1658 struct mdp_superblock_1 *sb;
1659 int mustfree = 0;
1660
1661 if (!st->sb) {
1662 if (st->ss->load_super(st, fd, NULL))
1663 return; /* no error I hope... */
1664 mustfree = 1;
1665 }
1666 sb = st->sb;
1667
1668 offset = __le64_to_cpu(sb->super_offset);
1669 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1670 if (mustfree)
1671 free(sb);
1672 lseek64(fd, offset<<9, 0);
1673 }
1674
1675 static int write_bitmap1(struct supertype *st, int fd)
1676 {
1677 struct mdp_superblock_1 *sb = st->sb;
1678 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
1679 int rv = 0;
1680 void *buf;
1681 int towrite, n;
1682 struct align_fd afd;
1683
1684 init_afd(&afd, fd);
1685
1686 locate_bitmap1(st, fd);
1687
1688 if (posix_memalign(&buf, 4096, 4096))
1689 return -ENOMEM;
1690
1691 memset(buf, 0xff, 4096);
1692 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
1693
1694 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1695 towrite = (towrite+7) >> 3; /* bits to bytes */
1696 towrite += sizeof(bitmap_super_t);
1697 towrite = ROUND_UP(towrite, 512);
1698 while (towrite > 0) {
1699 n = towrite;
1700 if (n > 4096)
1701 n = 4096;
1702 n = awrite(&afd, buf, n);
1703 if (n > 0)
1704 towrite -= n;
1705 else
1706 break;
1707 memset(buf, 0xff, 4096);
1708 }
1709 fsync(fd);
1710 if (towrite)
1711 rv = -2;
1712
1713 free(buf);
1714 return rv;
1715 }
1716
1717 static void free_super1(struct supertype *st)
1718 {
1719 if (st->sb)
1720 free(st->sb);
1721 while (st->info) {
1722 struct devinfo *di = st->info;
1723 st->info = di->next;
1724 if (di->fd >= 0)
1725 close(di->fd);
1726 free(di);
1727 }
1728 st->sb = NULL;
1729 }
1730
1731 #ifndef MDASSEMBLE
1732 static int validate_geometry1(struct supertype *st, int level,
1733 int layout, int raiddisks,
1734 int *chunk, unsigned long long size,
1735 char *subdev, unsigned long long *freesize,
1736 int verbose)
1737 {
1738 unsigned long long ldsize;
1739 int fd;
1740
1741 if (level == LEVEL_CONTAINER) {
1742 if (verbose)
1743 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
1744 return 0;
1745 }
1746 if (chunk && *chunk == UnSet)
1747 *chunk = DEFAULT_CHUNK;
1748
1749 if (!subdev)
1750 return 1;
1751
1752 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1753 if (fd < 0) {
1754 if (verbose)
1755 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1756 subdev, strerror(errno));
1757 return 0;
1758 }
1759
1760 if (!get_dev_size(fd, subdev, &ldsize)) {
1761 close(fd);
1762 return 0;
1763 }
1764 close(fd);
1765
1766 *freesize = avail_size1(st, ldsize >> 9);
1767 return 1;
1768 }
1769 #endif /* MDASSEMBLE */
1770
1771 struct superswitch super1 = {
1772 #ifndef MDASSEMBLE
1773 .examine_super = examine_super1,
1774 .brief_examine_super = brief_examine_super1,
1775 .export_examine_super = export_examine_super1,
1776 .detail_super = detail_super1,
1777 .brief_detail_super = brief_detail_super1,
1778 .export_detail_super = export_detail_super1,
1779 .write_init_super = write_init_super1,
1780 .validate_geometry = validate_geometry1,
1781 .add_to_super = add_to_super1,
1782 #endif
1783 .match_home = match_home1,
1784 .uuid_from_super = uuid_from_super1,
1785 .getinfo_super = getinfo_super1,
1786 .container_content = container_content1,
1787 .update_super = update_super1,
1788 .init_super = init_super1,
1789 .store_super = store_super1,
1790 .compare_super = compare_super1,
1791 .load_super = load_super1,
1792 .match_metadata_desc = match_metadata_desc1,
1793 .avail_size = avail_size1,
1794 .add_internal_bitmap = add_internal_bitmap1,
1795 .locate_bitmap = locate_bitmap1,
1796 .write_bitmap = write_bitmap1,
1797 .free_super = free_super1,
1798 #if __BYTE_ORDER == BIG_ENDIAN
1799 .swapuuid = 0,
1800 #else
1801 .swapuuid = 1,
1802 #endif
1803 .name = "1.x",
1804 };