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