]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Actually support --syslog
[thirdparty/mdadm.git] / super1.c
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 */
38 struct 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
97 /* feature_map bits */
98 #define MD_FEATURE_BITMAP_OFFSET 1
99 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
100 * must be honoured
101 */
102 #define MD_FEATURE_RESHAPE_ACTIVE 4
103
104 #define MD_FEATURE_ALL (1|2|4)
105
106 #ifndef offsetof
107 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
108 #endif
109 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
110 {
111 unsigned int disk_csum, csum;
112 unsigned long long newcsum;
113 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
114 unsigned int *isuper = (unsigned int*)sb;
115 int i;
116
117 /* make sure I can count... */
118 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
119 offsetof(struct mdp_superblock_1, utime) != 192 ||
120 sizeof(struct mdp_superblock_1) != 256) {
121 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
122 }
123
124 disk_csum = sb->sb_csum;
125 sb->sb_csum = 0;
126 newcsum = 0;
127 for (i=0; size>=4; size -= 4 ) {
128 newcsum += __le32_to_cpu(*isuper);
129 isuper++;
130 }
131
132 if (size == 2)
133 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
134
135 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
136 sb->sb_csum = disk_csum;
137 return __cpu_to_le32(csum);
138 }
139
140 #ifndef MDASSEMBLE
141 static void examine_super1(void *sbv, char *homehost)
142 {
143 struct mdp_superblock_1 *sb = sbv;
144 time_t atime;
145 int d;
146 int faulty;
147 int i;
148 char *c;
149 int l = homehost ? strlen(homehost) : 0;
150 int layout;
151
152 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
153 printf(" Version : %02d\n", 1);
154 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
155 printf(" Array UUID : ");
156 for (i=0; i<16; i++) {
157 if ((i&3)==0 && i != 0) printf(":");
158 printf("%02x", sb->set_uuid[i]);
159 }
160 printf("\n");
161 printf(" Name : %.32s", sb->set_name);
162 if (l > 0 && l < 32 &&
163 sb->set_name[l] == ':' &&
164 strncmp(sb->set_name, homehost, l) == 0)
165 printf(" (local to host %s)", homehost);
166 printf("\n");
167 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
168 printf(" Creation Time : %.24s\n", ctime(&atime));
169 c=map_num(pers, __le32_to_cpu(sb->level));
170 printf(" Raid Level : %s\n", c?c:"-unknown-");
171 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
172 printf("\n");
173 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
174 if (__le32_to_cpu(sb->level) >= 0) {
175 int ddsks=0;
176 switch(__le32_to_cpu(sb->level)) {
177 case 1: ddsks=1;break;
178 case 4:
179 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
180 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
181 case 10:
182 layout = __le32_to_cpu(sb->layout);
183 ddsks = __le32_to_cpu(sb->raid_disks)
184 / (layout&255) / ((layout>>8)&255);
185 }
186 if (ddsks)
187 printf(" Array Size : %llu%s\n",
188 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
189 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
190 if (sb->size != sb->data_size)
191 printf(" Used Size : %llu%s\n",
192 (unsigned long long)__le64_to_cpu(sb->size),
193 human_size(__le64_to_cpu(sb->size)<<9));
194 }
195 if (sb->data_offset)
196 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
197 if (sb->super_offset)
198 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
199 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
200 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
201 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
202 printf(" Device UUID : ");
203 for (i=0; i<16; i++) {
204 if ((i&3)==0 && i != 0) printf(":");
205 printf("%02x", sb->device_uuid[i]);
206 }
207 printf("\n");
208 printf("\n");
209 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
210 printf("Internal Bitmap : %ld sectors from superblock\n",
211 (long)__le32_to_cpu(sb->bitmap_offset));
212 }
213 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
214 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
215 human_size(__le64_to_cpu(sb->reshape_position)<<9));
216 if (__le32_to_cpu(sb->delta_disks)) {
217 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
218 if (__le32_to_cpu(sb->delta_disks))
219 printf(" (%d->%d)\n",
220 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
221 __le32_to_cpu(sb->raid_disks));
222 else
223 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
224 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
225 }
226 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
227 c = map_num(pers, __le32_to_cpu(sb->new_level));
228 printf(" New Level : %s\n", c?c:"-unknown-");
229 }
230 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
231 if (__le32_to_cpu(sb->level) == 5) {
232 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
233 printf(" New Layout : %s\n", c?c:"-unknown-");
234 }
235 if (__le32_to_cpu(sb->level) == 10) {
236 printf(" New Layout : near=%d, %s=%d\n",
237 __le32_to_cpu(sb->new_layout)&255,
238 (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
239 (__le32_to_cpu(sb->new_layout)>>8)&255);
240 }
241 }
242 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
243 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
244 printf("\n");
245 }
246 if (sb->devflags) {
247 printf(" Flags :");
248 if (sb->devflags & WriteMostly1)
249 printf(" write-mostly");
250 printf("\n");
251 }
252
253 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
254 printf(" Update Time : %.24s\n", ctime(&atime));
255
256 if (calc_sb_1_csum(sb) == sb->sb_csum)
257 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
258 else
259 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
260 __le32_to_cpu(calc_sb_1_csum(sb)));
261 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
262 printf("\n");
263 if (__le32_to_cpu(sb->level) == 5) {
264 c = map_num(r5layout, __le32_to_cpu(sb->layout));
265 printf(" Layout : %s\n", c?c:"-unknown-");
266 }
267 if (__le32_to_cpu(sb->level) == 10) {
268 int lo = __le32_to_cpu(sb->layout);
269 printf(" Layout : near=%d, %s=%d\n",
270 lo&255,
271 (lo&0x10000)?"offset":"far",
272 (lo>>8)&255);
273 }
274 switch(__le32_to_cpu(sb->level)) {
275 case 0:
276 case 4:
277 case 5:
278 case 6:
279 case 10:
280 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
281 break;
282 case -1:
283 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
284 break;
285 default: break;
286 }
287 printf("\n");
288 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
289 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
290 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
291 break;
292 for (d=0; d < i; d++) {
293 int role = __le16_to_cpu(sb->dev_roles[d]);
294 if (d) printf(", ");
295 if (role == 0xffff) printf("empty");
296 else if(role == 0xfffe) printf("failed");
297 else printf("%d", role);
298 }
299 printf(")\n");
300 printf(" Array State : ");
301 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
302 int cnt = 0;
303 int me = 0;
304 int i;
305 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
306 int role = __le16_to_cpu(sb->dev_roles[i]);
307 if (role == d) {
308 if (i == __le32_to_cpu(sb->dev_number))
309 me = 1;
310 cnt++;
311 }
312 }
313 if (cnt > 1) printf("?");
314 else if (cnt == 1 && me) printf("U");
315 else if (cnt == 1) printf("u");
316 else printf ("_");
317 }
318 faulty = 0;
319 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
320 int role = __le16_to_cpu(sb->dev_roles[i]);
321 if (role == 0xFFFE)
322 faulty++;
323 }
324 if (faulty) printf(" %d failed", faulty);
325 printf("\n");
326 }
327
328
329 static void brief_examine_super1(void *sbv)
330 {
331 struct mdp_superblock_1 *sb = sbv;
332 int i;
333 char *nm;
334 char *c=map_num(pers, __le32_to_cpu(sb->level));
335
336 nm = strchr(sb->set_name, ':');
337 if (nm)
338 nm++;
339 else if (sb->set_name[0])
340 nm = sb->set_name;
341 else
342 nm = "??";
343
344 printf("ARRAY /dev/md/%s level=%s metadata=1 num-devices=%d UUID=",
345 nm,
346 c?c:"-unknown-", __le32_to_cpu(sb->raid_disks));
347 for (i=0; i<16; i++) {
348 if ((i&3)==0 && i != 0) printf(":");
349 printf("%02x", sb->set_uuid[i]);
350 }
351 if (sb->set_name[0])
352 printf(" name=%.32s", sb->set_name);
353 printf("\n");
354 }
355
356 static void detail_super1(void *sbv, char *homehost)
357 {
358 struct mdp_superblock_1 *sb = sbv;
359 int i;
360 int l = homehost ? strlen(homehost) : 0;
361
362 printf(" Name : %.32s", sb->set_name);
363 if (l > 0 && l < 32 &&
364 sb->set_name[l] == ':' &&
365 strncmp(sb->set_name, homehost, l) == 0)
366 printf(" (local to host %s)", homehost);
367 printf("\n UUID : ");
368 for (i=0; i<16; i++) {
369 if ((i&3)==0 && i != 0) printf(":");
370 printf("%02x", sb->set_uuid[i]);
371 }
372 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
373 }
374
375 static void brief_detail_super1(void *sbv)
376 {
377 struct mdp_superblock_1 *sb = sbv;
378 int i;
379
380 if (sb->set_name[0])
381 printf(" name=%.32s", sb->set_name);
382 printf(" UUID=");
383 for (i=0; i<16; i++) {
384 if ((i&3)==0 && i != 0) printf(":");
385 printf("%02x", sb->set_uuid[i]);
386 }
387 }
388
389 #endif
390
391 static int match_home1(void *sbv, char *homehost)
392 {
393 struct mdp_superblock_1 *sb = sbv;
394 int l = homehost ? strlen(homehost) : 0;
395
396 return (l > 0 && l < 32 &&
397 sb->set_name[l] == ':' &&
398 strncmp(sb->set_name, homehost, l) == 0);
399 }
400
401 static void uuid_from_super1(int uuid[4], void * sbv)
402 {
403 struct mdp_superblock_1 *super = sbv;
404 char *cuuid = (char*)uuid;
405 int i;
406 for (i=0; i<16; i++)
407 cuuid[i] = super->set_uuid[i];
408 }
409
410 static void getinfo_super1(struct mdinfo *info, void *sbv)
411 {
412 struct mdp_superblock_1 *sb = sbv;
413 int working = 0;
414 int i;
415 int role;
416
417 info->array.major_version = 1;
418 info->array.minor_version = __le32_to_cpu(sb->feature_map);
419 info->array.patch_version = 0;
420 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
421 info->array.level = __le32_to_cpu(sb->level);
422 info->array.layout = __le32_to_cpu(sb->layout);
423 info->array.md_minor = -1;
424 info->array.ctime = __le64_to_cpu(sb->ctime);
425 info->array.utime = __le64_to_cpu(sb->utime);
426 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
427
428 info->data_offset = __le64_to_cpu(sb->data_offset);
429 info->component_size = __le64_to_cpu(sb->size);
430
431 info->disk.major = 0;
432 info->disk.minor = 0;
433 info->disk.number = __le32_to_cpu(sb->dev_number);
434 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
435 __le32_to_cpu(sb->max_dev) > 512)
436 role = 0xfffe;
437 else
438 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
439
440 info->disk.raid_disk = -1;
441 switch(role) {
442 case 0xFFFF:
443 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
444 break;
445 case 0xFFFE:
446 info->disk.state = 1; /* faulty */
447 break;
448 default:
449 info->disk.state = 6; /* active and in sync */
450 info->disk.raid_disk = role;
451 }
452 info->events = __le64_to_cpu(sb->events);
453
454 memcpy(info->uuid, sb->set_uuid, 16);
455
456 strncpy(info->name, sb->set_name, 32);
457 info->name[32] = 0;
458
459 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
460 info->reshape_active = 1;
461 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
462 info->new_level = __le32_to_cpu(sb->new_level);
463 info->delta_disks = __le32_to_cpu(sb->delta_disks);
464 info->new_layout = __le32_to_cpu(sb->new_layout);
465 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
466 } else
467 info->reshape_active = 0;
468
469 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
470 role = __le16_to_cpu(sb->dev_roles[i]);
471 if (/*role == 0xFFFF || */role < info->array.raid_disks)
472 working++;
473 }
474
475 info->array.working_disks = working;
476 }
477
478 static int update_super1(struct mdinfo *info, void *sbv, char *update,
479 char *devname, int verbose,
480 int uuid_set, char *homehost)
481 {
482 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
483 * For others, the return value is ignored.
484 */
485 int rv = 0;
486 struct mdp_superblock_1 *sb = sbv;
487
488 if (strcmp(update, "force-one")==0) {
489 /* Not enough devices for a working array,
490 * so bring this one up-to-date
491 */
492 if (sb->events != __cpu_to_le64(info->events))
493 rv = 1;
494 sb->events = __cpu_to_le64(info->events);
495 }
496 if (strcmp(update, "force-array")==0) {
497 /* Degraded array and 'force' requests to
498 * maybe need to mark it 'clean'.
499 */
500 switch(__le32_to_cpu(sb->level)) {
501 case 5: case 4: case 6:
502 /* need to force clean */
503 if (sb->resync_offset != ~0ULL)
504 rv = 1;
505 sb->resync_offset = ~0ULL;
506 }
507 }
508 if (strcmp(update, "assemble")==0) {
509 int d = info->disk.number;
510 int want;
511 if (info->disk.state == 6)
512 want = __cpu_to_le32(info->disk.raid_disk);
513 else
514 want = 0xFFFF;
515 if (sb->dev_roles[d] != want) {
516 sb->dev_roles[d] = want;
517 rv = 1;
518 }
519 }
520 if (strcmp(update, "grow") == 0) {
521 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
522 /* FIXME */
523 }
524 if (strcmp(update, "resync") == 0) {
525 /* make sure resync happens */
526 sb->resync_offset = 0ULL;
527 }
528 if (strcmp(update, "uuid") == 0) {
529 memcpy(sb->set_uuid, info->uuid, 16);
530 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
531 struct bitmap_super_s *bm;
532 bm = (struct bitmap_super_s*)(sbv+1024);
533 memcpy(bm->uuid, info->uuid, 16);
534 }
535 }
536 if (strcmp(update, "homehost") == 0 &&
537 homehost) {
538 char *c;
539 update = "name";
540 c = strchr(sb->set_name, ':');
541 if (c)
542 strncpy(info->name, c+1, 31 - (c-sb->set_name));
543 else
544 strncpy(info->name, sb->set_name, 32);
545 info->name[32] = 0;
546 }
547 if (strcmp(update, "name") == 0) {
548 if (info->name[0] == 0)
549 sprintf(info->name, "%d", info->array.md_minor);
550 memset(sb->set_name, 0, sizeof(sb->set_name));
551 if (homehost &&
552 strchr(info->name, ':') == NULL &&
553 strlen(homehost)+1+strlen(info->name) < 32) {
554 strcpy(sb->set_name, homehost);
555 strcat(sb->set_name, ":");
556 strcat(sb->set_name, info->name);
557 } else
558 strcpy(sb->set_name, info->name);
559 }
560 if (strcmp(update, "_reshape_progress")==0)
561 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
562
563 sb->sb_csum = calc_sb_1_csum(sb);
564 return rv;
565 }
566
567 static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
568 unsigned long long size, char *name, char *homehost)
569 {
570 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
571 int spares;
572 int rfd;
573 char defname[10];
574 memset(sb, 0, 1024);
575
576 if (info->major_version == -1) {
577 /* zeroing superblock */
578 *sbp = sb;
579 return 0;
580 }
581
582 spares = info->working_disks - info->active_disks;
583 if (info->raid_disks + spares > 384) {
584 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
585 info->raid_disks , spares, 384);
586 return 0;
587 }
588
589 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
590 sb->major_version = __cpu_to_le32(1);
591 sb->feature_map = 0;
592 sb->pad0 = 0;
593
594 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
595 read(rfd, sb->set_uuid, 16) != 16) {
596 *(__u32*)(sb->set_uuid) = random();
597 *(__u32*)(sb->set_uuid+4) = random();
598 *(__u32*)(sb->set_uuid+8) = random();
599 *(__u32*)(sb->set_uuid+12) = random();
600 }
601 if (rfd >= 0) close(rfd);
602
603 if (name == NULL || *name == 0) {
604 sprintf(defname, "%d", info->md_minor);
605 name = defname;
606 }
607 memset(sb->set_name, 0, 32);
608 if (homehost &&
609 strchr(name, ':')== NULL &&
610 strlen(homehost)+1+strlen(name) < 32) {
611 strcpy(sb->set_name, homehost);
612 strcat(sb->set_name, ":");
613 strcat(sb->set_name, name);
614 } else
615 strcpy(sb->set_name, name);
616
617 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
618 sb->level = __cpu_to_le32(info->level);
619 sb->layout = __cpu_to_le32(info->layout);
620 sb->size = __cpu_to_le64(size*2ULL);
621 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
622 sb->raid_disks = __cpu_to_le32(info->raid_disks);
623
624 sb->data_offset = __cpu_to_le64(0);
625 sb->data_size = __cpu_to_le64(0);
626 sb->super_offset = __cpu_to_le64(0);
627 sb->recovery_offset = __cpu_to_le64(0);
628
629 sb->utime = sb->ctime;
630 sb->events = __cpu_to_le64(1);
631 if (info->state & (1<<MD_SB_CLEAN))
632 sb->resync_offset = ~0ULL;
633 else
634 sb->resync_offset = 0;
635 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
636 sizeof(sb->dev_roles[0]));
637 memset(sb->pad3, 0, sizeof(sb->pad3));
638
639 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
640
641 *sbp = sb;
642 return 1;
643 }
644
645 /* Add a device to the superblock being created */
646 static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
647 {
648 struct mdp_superblock_1 *sb = sbv;
649 __u16 *rp = sb->dev_roles + dk->number;
650 if ((dk->state & 6) == 6) /* active, sync */
651 *rp = __cpu_to_le16(dk->raid_disk);
652 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
653 *rp = 0xffff;
654 else
655 *rp = 0xfffe;
656 }
657
658 static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
659
660 static int store_super1(struct supertype *st, int fd, void *sbv)
661 {
662 struct mdp_superblock_1 *sb = sbv;
663 unsigned long long sb_offset;
664 int sbsize;
665 unsigned long size;
666 unsigned long long dsize;
667
668 #ifdef BLKGETSIZE64
669 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
670 #endif
671 {
672 if (ioctl(fd, BLKGETSIZE, &size))
673 return 1;
674 else
675 dsize = (unsigned long long)size;
676 } else
677 dsize >>= 9;
678
679 if (dsize < 24)
680 return 2;
681
682 /*
683 * Calculate the position of the superblock.
684 * It is always aligned to a 4K boundary and
685 * depending on minor_version, it can be:
686 * 0: At least 8K, but less than 12K, from end of device
687 * 1: At start of device
688 * 2: 4K from start of device.
689 */
690 switch(st->minor_version) {
691 case 0:
692 sb_offset = dsize;
693 sb_offset -= 8*2;
694 sb_offset &= ~(4*2-1);
695 break;
696 case 1:
697 sb_offset = 0;
698 break;
699 case 2:
700 sb_offset = 4*2;
701 break;
702 default:
703 return -EINVAL;
704 }
705
706
707
708 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
709 0 != __le64_to_cpu(sb->super_offset)
710 ) {
711 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
712 abort();
713 }
714
715 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
716 return 3;
717
718 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
719
720 if (write(fd, sb, sbsize) != sbsize)
721 return 4;
722
723 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
724 struct bitmap_super_s *bm = (struct bitmap_super_s*)
725 (((char*)sb)+1024);
726 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
727 locate_bitmap1(st, fd, sbv);
728 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
729 return 5;
730 }
731 }
732 fsync(fd);
733 return 0;
734 }
735
736 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
737
738 static int write_init_super1(struct supertype *st, void *sbv,
739 mdu_disk_info_t *dinfo, char *devname)
740 {
741 struct mdp_superblock_1 *sb = sbv;
742 void *refsbv = NULL;
743 int fd = open(devname, O_RDWR | O_EXCL);
744 int rfd;
745 int rv;
746
747 unsigned long size, space;
748 unsigned long long dsize, array_size;
749 long long sb_offset;
750
751
752 if (fd < 0) {
753 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
754 devname);
755 return -1;
756 }
757
758 sb->dev_number = __cpu_to_le32(dinfo->number);
759 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
760 sb->devflags |= __cpu_to_le32(WriteMostly1);
761
762 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
763 read(rfd, sb->device_uuid, 16) != 16) {
764 *(__u32*)(sb->device_uuid) = random();
765 *(__u32*)(sb->device_uuid+4) = random();
766 *(__u32*)(sb->device_uuid+8) = random();
767 *(__u32*)(sb->device_uuid+12) = random();
768 }
769 if (rfd >= 0) close(rfd);
770 sb->events = 0;
771
772 if (load_super1(st, fd, &refsbv, NULL)==0) {
773 struct mdp_superblock_1 *refsb = refsbv;
774
775 memcpy(sb->device_uuid, refsb->device_uuid, 16);
776 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
777 /* same array, so preserve events and dev_number */
778 sb->events = refsb->events;
779 /* bugs in 2.6.17 and earlier mean the dev_number
780 * chosen in Manage must be preserved
781 */
782 if (get_linux_version() >= 2006018)
783 sb->dev_number = refsb->dev_number;
784 }
785 free(refsb);
786 }
787
788 #ifdef BLKGETSIZE64
789 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
790 #endif
791 {
792 if (ioctl(fd, BLKGETSIZE, &size))
793 return 1;
794 else
795 dsize = size;
796 } else
797 dsize >>= 9;
798
799 if (dsize < 24) {
800 close(fd);
801 return 2;
802 }
803
804
805 /*
806 * Calculate the position of the superblock.
807 * It is always aligned to a 4K boundary and
808 * depending on minor_version, it can be:
809 * 0: At least 8K, but less than 12K, from end of device
810 * 1: At start of device
811 * 2: 4K from start of device.
812 * Depending on the array size, we might leave extra space
813 * for a bitmap.
814 */
815 array_size = __le64_to_cpu(sb->size);
816 switch(st->minor_version) {
817 case 0:
818 sb_offset = dsize;
819 sb_offset -= 8*2;
820 sb_offset &= ~(4*2-1);
821 sb->super_offset = __cpu_to_le64(sb_offset);
822 sb->data_offset = __cpu_to_le64(0);
823 if (sb_offset-64*2 >= array_size && array_size > 8*1024*1024*2)
824 sb->data_size = __cpu_to_le64(sb_offset-64*2);
825 else
826 sb->data_size = __cpu_to_le64(sb_offset);
827 break;
828 case 1:
829 sb->super_offset = __cpu_to_le64(0);
830 if (dsize - 64*2 >= array_size && array_size > 8*1024*1024*2)
831 space = 64*2;
832 else
833 space = 4*2;
834 sb->data_offset = __cpu_to_le64(space); /* leave space for super and bitmap */
835 sb->data_size = __cpu_to_le64(dsize - space);
836 break;
837 case 2:
838 sb_offset = 4*2;
839 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
840 space = 64*2;
841 else
842 space = 4*2;
843 sb->super_offset = __cpu_to_le64(sb_offset);
844 sb->data_offset = __cpu_to_le64(sb_offset+space);
845 sb->data_size = __cpu_to_le64(dsize - 4*2 - space);
846 break;
847 default:
848 return -EINVAL;
849 }
850
851
852 sb->sb_csum = calc_sb_1_csum(sb);
853 rv = store_super1(st, fd, sb);
854 if (rv)
855 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
856
857 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
858 rv = st->ss->write_bitmap(st, fd, sbv);
859 close(fd);
860 return rv;
861 }
862
863 static int compare_super1(void **firstp, void *secondv)
864 {
865 /*
866 * return:
867 * 0 same, or first was empty, and second was copied
868 * 1 second had wrong number
869 * 2 wrong uuid
870 * 3 wrong other info
871 */
872 struct mdp_superblock_1 *first = *firstp;
873 struct mdp_superblock_1 *second = secondv;
874
875 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
876 return 1;
877 if (second->major_version != __cpu_to_le32(1))
878 return 1;
879
880 if (!first) {
881 first = malloc(1024+sizeof(bitmap_super_t));
882 memcpy(first, second, 1024+sizeof(bitmap_super_t));
883 *firstp = first;
884 return 0;
885 }
886 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
887 return 2;
888
889 if (first->ctime != second->ctime ||
890 first->level != second->level ||
891 first->layout != second->layout ||
892 first->size != second->size ||
893 first->chunksize != second->chunksize ||
894 first->raid_disks != second->raid_disks)
895 return 3;
896 return 0;
897 }
898
899 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
900 {
901 unsigned long size;
902 unsigned long long dsize;
903 unsigned long long sb_offset;
904 struct mdp_superblock_1 *super;
905 int uuid[4];
906 struct bitmap_super_s *bsb;
907
908
909 if (st->ss == NULL) {
910 int bestvers = -1;
911 __u64 bestctime = 0;
912 /* guess... choose latest ctime */
913 st->ss = &super1;
914 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
915 switch(load_super1(st, fd, sbp, devname)) {
916 case 0: super = *sbp;
917 if (bestvers == -1 ||
918 bestctime < __le64_to_cpu(super->ctime)) {
919 bestvers = st->minor_version;
920 bestctime = __le64_to_cpu(super->ctime);
921 }
922 free(super);
923 *sbp = NULL;
924 break;
925 case 1: st->ss = NULL; return 1; /*bad device */
926 case 2: break; /* bad, try next */
927 }
928 }
929 if (bestvers != -1) {
930 int rv;
931 st->minor_version = bestvers;
932 st->ss = &super1;
933 st->max_devs = 384;
934 rv = load_super1(st, fd, sbp, devname);
935 if (rv) st->ss = NULL;
936 return rv;
937 }
938 st->ss = NULL;
939 return 2;
940 }
941 #ifdef BLKGETSIZE64
942 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
943 #endif
944 {
945 if (ioctl(fd, BLKGETSIZE, &size)) {
946 if (devname)
947 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
948 devname, strerror(errno));
949 return 1;
950 }
951 dsize = size;
952 } else
953 dsize >>= 9;
954
955 if (dsize < 24) {
956 if (devname)
957 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
958 devname, dsize);
959 return 1;
960 }
961
962 /*
963 * Calculate the position of the superblock.
964 * It is always aligned to a 4K boundary and
965 * depending on minor_version, it can be:
966 * 0: At least 8K, but less than 12K, from end of device
967 * 1: At start of device
968 * 2: 4K from start of device.
969 */
970 switch(st->minor_version) {
971 case 0:
972 sb_offset = dsize;
973 sb_offset -= 8*2;
974 sb_offset &= ~(4*2-1);
975 break;
976 case 1:
977 sb_offset = 0;
978 break;
979 case 2:
980 sb_offset = 4*2;
981 break;
982 default:
983 return -EINVAL;
984 }
985
986 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
987
988
989 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
990 if (devname)
991 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
992 devname, strerror(errno));
993 return 1;
994 }
995
996 super = malloc(1024 + sizeof(bitmap_super_t));
997
998 if (read(fd, super, 1024) != 1024) {
999 if (devname)
1000 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1001 devname);
1002 free(super);
1003 return 1;
1004 }
1005
1006 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1007 if (devname)
1008 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1009 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1010 free(super);
1011 return 2;
1012 }
1013
1014 if (__le32_to_cpu(super->major_version) != 1) {
1015 if (devname)
1016 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1017 devname, __le32_to_cpu(super->major_version));
1018 free(super);
1019 return 2;
1020 }
1021 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1022 if (devname)
1023 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1024 devname);
1025 free(super);
1026 return 2;
1027 }
1028 *sbp = super;
1029
1030 /* Now check on the bitmap superblock */
1031 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1032 return 0;
1033 /* Read the bitmap superblock and make sure it looks
1034 * valid. If it doesn't clear the bit. An --assemble --force
1035 * should get that written out.
1036 */
1037 locate_bitmap1(st, fd, super);
1038 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1039 != sizeof(struct bitmap_super_s))
1040 goto no_bitmap;
1041
1042 uuid_from_super1(uuid, super);
1043 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1044 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1045 memcmp(bsb->uuid, uuid, 16) != 0)
1046 goto no_bitmap;
1047 return 0;
1048
1049 no_bitmap:
1050 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
1051 return 0;
1052 }
1053
1054
1055 static struct supertype *match_metadata_desc1(char *arg)
1056 {
1057 struct supertype *st = malloc(sizeof(*st));
1058 if (!st) return st;
1059
1060 st->ss = &super1;
1061 st->max_devs = 384;
1062 if (strcmp(arg, "1") == 0 ||
1063 strcmp(arg, "1.0") == 0 ||
1064 strcmp(arg, "default/large") == 0) {
1065 st->minor_version = 0;
1066 return st;
1067 }
1068 if (strcmp(arg, "1.1") == 0) {
1069 st->minor_version = 1;
1070 return st;
1071 }
1072 if (strcmp(arg, "1.2") == 0) {
1073 st->minor_version = 2;
1074 return st;
1075 }
1076
1077 free(st);
1078 return NULL;
1079 }
1080
1081 /* find available size on device with this devsize, using
1082 * superblock type st, and reserving 'reserve' sectors for
1083 * a possible bitmap
1084 */
1085 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1086 {
1087 if (devsize < 24)
1088 return 0;
1089
1090 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1091 * if biffer than 200Gig, save 128k
1092 */
1093 if (devsize > 200*1024*1024*2)
1094 devsize -= 128*2;
1095 else if (devsize > 8*1024*1024*2)
1096 devsize -= 64*2;
1097
1098 switch(st->minor_version) {
1099 case 0:
1100 /* at end */
1101 return ((devsize - 8*2 ) & ~(4*2-1));
1102 case 1:
1103 /* at start, 4K for superblock and possible bitmap */
1104 return devsize - 4*2;
1105 case 2:
1106 /* 4k from start, 4K for superblock and possible bitmap */
1107 return devsize - (4+4)*2;
1108 }
1109 return 0;
1110 }
1111
1112 static int
1113 add_internal_bitmap1(struct supertype *st, void *sbv,
1114 int chunk, int delay, int write_behind, unsigned long long size,
1115 int may_change, int major)
1116 {
1117 /*
1118 * If not may_change, then this is a 'Grow', and the bitmap
1119 * must fit after the superblock.
1120 * If may_change, then this is create, and we can put the bitmap
1121 * before the superblock if we like, or may move the start.
1122 * For now, just squeeze the bitmap into 3k and don't change anything.
1123 *
1124 * size is in sectors, chunk is in bytes !!!
1125 */
1126
1127 unsigned long long bits;
1128 unsigned long long max_bits = (6*512 - sizeof(bitmap_super_t)) * 8;
1129 unsigned long long min_chunk;
1130 struct mdp_superblock_1 *sb = sbv;
1131 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1132
1133 if (st->minor_version && !may_change &&
1134 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
1135 return 0; /* doesn't fit */
1136
1137
1138
1139 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1140 bits = (size*512)/min_chunk +1;
1141 while (bits > max_bits) {
1142 min_chunk *= 2;
1143 bits = (bits+1)/2;
1144 }
1145 if (chunk == UnSet)
1146 chunk = min_chunk;
1147 else if (chunk < min_chunk)
1148 return 0; /* chunk size too small */
1149 if (chunk == 0) /* rounding problem */
1150 return 0;
1151
1152 sb->bitmap_offset = __cpu_to_le32(2);
1153
1154 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
1155 memset(bms, 0, sizeof(*bms));
1156 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1157 bms->version = __cpu_to_le32(major);
1158 uuid_from_super1((int*)bms->uuid, sb);
1159 bms->chunksize = __cpu_to_le32(chunk);
1160 bms->daemon_sleep = __cpu_to_le32(delay);
1161 bms->sync_size = __cpu_to_le64(size);
1162 bms->write_behind = __cpu_to_le32(write_behind);
1163
1164 return 1;
1165 }
1166
1167
1168 static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
1169 {
1170 unsigned long long offset;
1171 struct mdp_superblock_1 *sb;
1172 int mustfree = 0;
1173
1174 if (!sbv) {
1175 if (st->ss->load_super(st, fd, &sbv, NULL))
1176 return; /* no error I hope... */
1177 mustfree = 1;
1178 }
1179 sb = sbv;
1180
1181 offset = __le64_to_cpu(sb->super_offset);
1182 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1183 if (mustfree)
1184 free(sb);
1185 lseek64(fd, offset<<9, 0);
1186 }
1187
1188 static int write_bitmap1(struct supertype *st, int fd, void *sbv)
1189 {
1190 struct mdp_superblock_1 *sb = sbv;
1191 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
1192 int rv = 0;
1193
1194 int towrite, n;
1195 char buf[4096];
1196
1197 locate_bitmap1(st, fd, sbv);
1198
1199 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1200 sizeof(bitmap_super_t))
1201 return -2;
1202 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1203 towrite = (towrite+7) >> 3; /* bits to bytes */
1204 memset(buf, 0xff, sizeof(buf));
1205 while (towrite > 0) {
1206 n = towrite;
1207 if (n > sizeof(buf))
1208 n = sizeof(buf);
1209 n = write(fd, buf, n);
1210 if (n > 0)
1211 towrite -= n;
1212 else
1213 break;
1214 }
1215 fsync(fd);
1216 if (towrite)
1217 rv = -2;
1218
1219 return rv;
1220 }
1221
1222 struct superswitch super1 = {
1223 #ifndef MDASSEMBLE
1224 .examine_super = examine_super1,
1225 .brief_examine_super = brief_examine_super1,
1226 .detail_super = detail_super1,
1227 .brief_detail_super = brief_detail_super1,
1228 #endif
1229 .match_home = match_home1,
1230 .uuid_from_super = uuid_from_super1,
1231 .getinfo_super = getinfo_super1,
1232 .update_super = update_super1,
1233 .init_super = init_super1,
1234 .add_to_super = add_to_super1,
1235 .store_super = store_super1,
1236 .write_init_super = write_init_super1,
1237 .compare_super = compare_super1,
1238 .load_super = load_super1,
1239 .match_metadata_desc = match_metadata_desc1,
1240 .avail_size = avail_size1,
1241 .add_internal_bitmap = add_internal_bitmap1,
1242 .locate_bitmap = locate_bitmap1,
1243 .write_bitmap = write_bitmap1,
1244 .major = 1,
1245 #if __BYTE_ORDER == BIG_ENDIAN
1246 .swapuuid = 0,
1247 #else
1248 .swapuuid = 1,
1249 #endif
1250 };