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