]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super0.c
validate_geometry_imsm_volume(): Avoid NULL pointer dereference
[thirdparty/mdadm.git] / super0.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 #define HAVE_STDINT_H 1
26 #include "mdadm.h"
27 #include "sha1.h"
28 /*
29 * All handling for the 0.90.0 version superblock is in
30 * this file.
31 * This includes:
32 * - finding, loading, and writing the superblock.
33 * - initialising a new superblock
34 * - printing the superblock for --examine
35 * - printing part of the superblock for --detail
36 * .. other stuff
37 */
38
39
40 static unsigned long calc_sb0_csum(mdp_super_t *super)
41 {
42 unsigned long csum = super->sb_csum;
43 unsigned long newcsum;
44 super->sb_csum= 0 ;
45 newcsum = calc_csum(super, MD_SB_BYTES);
46 super->sb_csum = csum;
47 return newcsum;
48 }
49
50
51 static void super0_swap_endian(struct mdp_superblock_s *sb)
52 {
53 /* as super0 superblocks are host-endian, it is sometimes
54 * useful to be able to swap the endianness
55 * as (almost) everything is u32's we byte-swap every 4byte
56 * number.
57 * We then also have to swap the events_hi and events_lo
58 */
59 char *sbc = (char *)sb;
60 __u32 t32;
61 int i;
62
63 for (i=0; i < MD_SB_BYTES ; i+=4) {
64 char t = sbc[i];
65 sbc[i] = sbc[i+3];
66 sbc[i+3] = t;
67 t=sbc[i+1];
68 sbc[i+1]=sbc[i+2];
69 sbc[i+2]=t;
70 }
71 t32 = sb->events_hi;
72 sb->events_hi = sb->events_lo;
73 sb->events_lo = t32;
74
75 t32 = sb->cp_events_hi;
76 sb->cp_events_hi = sb->cp_events_lo;
77 sb->cp_events_lo = t32;
78
79 }
80
81 #ifndef MDASSEMBLE
82
83 static void examine_super0(struct supertype *st, char *homehost)
84 {
85 mdp_super_t *sb = st->sb;
86 time_t atime;
87 int d;
88 int delta_extra = 0;
89 char *c;
90
91 printf(" Magic : %08x\n", sb->md_magic);
92 printf(" Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version,
93 sb->patch_version);
94 if (sb->minor_version >= 90) {
95 printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
96 sb->set_uuid2, sb->set_uuid3);
97 if (homehost) {
98 char buf[20];
99 void *hash = sha1_buffer(homehost,
100 strlen(homehost),
101 buf);
102 if (memcmp(&sb->set_uuid2, hash, 8)==0)
103 printf(" (local to host %s)", homehost);
104 }
105 printf("\n");
106 } else
107 printf(" UUID : %08x\n", sb->set_uuid0);
108
109 if (sb->not_persistent)
110 printf(" Eedk : not persistent\n");
111
112 atime = sb->ctime;
113 printf(" Creation Time : %.24s\n", ctime(&atime));
114 c=map_num(pers, sb->level);
115 printf(" Raid Level : %s\n", c?c:"-unknown-");
116 if ((int)sb->level > 0) {
117 int ddsks=0;
118 printf(" Used Dev Size : %d%s\n", sb->size,
119 human_size((long long)sb->size<<10));
120 switch(sb->level) {
121 case 1: ddsks=1;break;
122 case 4:
123 case 5: ddsks = sb->raid_disks-1; break;
124 case 6: ddsks = sb->raid_disks-2; break;
125 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
126 }
127 if (ddsks)
128 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
129 human_size(ddsks*(long long)sb->size<<10));
130 }
131 printf(" Raid Devices : %d\n", sb->raid_disks);
132 printf(" Total Devices : %d\n", sb->nr_disks);
133 printf("Preferred Minor : %d\n", sb->md_minor);
134 printf("\n");
135 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
136 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
137 if (sb->delta_disks) {
138 printf(" Delta Devices : %d", sb->delta_disks);
139 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
140 if (((int)sb->delta_disks) < 0)
141 delta_extra = - sb->delta_disks;
142 }
143 if (sb->new_level != sb->level) {
144 c = map_num(pers, sb->new_level);
145 printf(" New Level : %s\n", c?c:"-unknown-");
146 }
147 if (sb->new_layout != sb->layout) {
148 if (sb->level == 5) {
149 c = map_num(r5layout, sb->new_layout);
150 printf(" New Layout : %s\n", c?c:"-unknown-");
151 }
152 if (sb->level == 6) {
153 c = map_num(r6layout, sb->new_layout);
154 printf(" New Layout : %s\n", c?c:"-unknown-");
155 }
156 if (sb->level == 10) {
157 printf(" New Layout : near=%d, %s=%d\n",
158 sb->new_layout&255,
159 (sb->new_layout&0x10000)?"offset":"far",
160 (sb->new_layout>>8)&255);
161 }
162 }
163 if (sb->new_chunk != sb->chunk_size)
164 printf(" New Chunksize : %d\n", sb->new_chunk);
165 printf("\n");
166 }
167 atime = sb->utime;
168 printf(" Update Time : %.24s\n", ctime(&atime));
169 printf(" State : %s\n",
170 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
171 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
172 printf("Internal Bitmap : present\n");
173 printf(" Active Devices : %d\n", sb->active_disks);
174 printf("Working Devices : %d\n", sb->working_disks);
175 printf(" Failed Devices : %d\n", sb->failed_disks);
176 printf(" Spare Devices : %d\n", sb->spare_disks);
177 if (calc_sb0_csum(sb) == sb->sb_csum)
178 printf(" Checksum : %x - correct\n", sb->sb_csum);
179 else
180 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
181 printf(" Events : %llu\n",
182 ((unsigned long long)sb->events_hi << 32)
183 + sb->events_lo);
184 printf("\n");
185 if (sb->level == 5) {
186 c = map_num(r5layout, sb->layout);
187 printf(" Layout : %s\n", c?c:"-unknown-");
188 }
189 if (sb->level == 6) {
190 c = map_num(r6layout, sb->layout);
191 printf(" Layout : %s\n", c?c:"-unknown-");
192 }
193 if (sb->level == 10) {
194 printf(" Layout :");
195 print_r10_layout(sb->layout);
196 printf("\n");
197 }
198 switch(sb->level) {
199 case 0:
200 case 4:
201 case 5:
202 case 6:
203 case 10:
204 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
205 break;
206 case -1:
207 printf(" Rounding : %dK\n", sb->chunk_size/1024);
208 break;
209 default: break;
210 }
211 printf("\n");
212 printf(" Number Major Minor RaidDevice State\n");
213 for (d= -1; d<(signed int)(sb->raid_disks+delta_extra + sb->spare_disks); d++) {
214 mdp_disk_t *dp;
215 char *dv;
216 char nb[5];
217 int wonly;
218 if (d>=0) dp = &sb->disks[d];
219 else dp = &sb->this_disk;
220 snprintf(nb, sizeof(nb), "%4d", d);
221 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
222 dp->number, dp->major, dp->minor, dp->raid_disk);
223 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
224 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
225 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
226 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
227 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
228 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
229 if (wonly) printf(" write-mostly");
230 if (dp->state == 0) printf(" spare");
231 if ((dv=map_dev(dp->major, dp->minor, 0)))
232 printf(" %s", dv);
233 printf("\n");
234 if (d == -1) printf("\n");
235 }
236 }
237
238 static void brief_examine_super0(struct supertype *st, int verbose)
239 {
240 mdp_super_t *sb = st->sb;
241 char *c=map_num(pers, sb->level);
242 char devname[20];
243
244 sprintf(devname, "/dev/md%d", sb->md_minor);
245
246 if (verbose) {
247 printf("ARRAY %s level=%s num-devices=%d",
248 devname,
249 c?c:"-unknown-", sb->raid_disks);
250 } else
251 printf("ARRAY %s", devname);
252
253 if (sb->minor_version >= 90)
254 printf(" UUID=%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
255 sb->set_uuid2, sb->set_uuid3);
256 else
257 printf(" UUID=%08x", sb->set_uuid0);
258 printf("\n");
259 }
260
261 static void export_examine_super0(struct supertype *st)
262 {
263 mdp_super_t *sb = st->sb;
264
265 printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
266 printf("MD_DEVICES=%d\n", sb->raid_disks);
267 if (sb->minor_version >= 90)
268 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
269 sb->set_uuid0, sb->set_uuid1,
270 sb->set_uuid2, sb->set_uuid3);
271 else
272 printf("MD_UUID=%08x\n", sb->set_uuid0);
273 printf("MD_UPDATE_TIME=%llu\n",
274 __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL);
275 printf("MD_EVENTS=%llu\n",
276 ((unsigned long long)sb->events_hi << 32)
277 + sb->events_lo);
278 }
279
280 static void detail_super0(struct supertype *st, char *homehost)
281 {
282 mdp_super_t *sb = st->sb;
283 printf(" UUID : ");
284 if (sb->minor_version >= 90)
285 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
286 sb->set_uuid2, sb->set_uuid3);
287 else
288 printf("%08x", sb->set_uuid0);
289 if (homehost) {
290 char buf[20];
291 void *hash = sha1_buffer(homehost,
292 strlen(homehost),
293 buf);
294 if (memcmp(&sb->set_uuid2, hash, 8)==0)
295 printf(" (local to host %s)", homehost);
296 }
297 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
298 }
299
300 static void brief_detail_super0(struct supertype *st)
301 {
302 mdp_super_t *sb = st->sb;
303 printf(" UUID=");
304 if (sb->minor_version >= 90)
305 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
306 sb->set_uuid2, sb->set_uuid3);
307 else
308 printf("%08x", sb->set_uuid0);
309 }
310 #endif
311
312 static int match_home0(struct supertype *st, char *homehost)
313 {
314 mdp_super_t *sb = st->sb;
315 char buf[20];
316 char *hash;
317
318 if (!homehost)
319 return 0;
320 hash = sha1_buffer(homehost,
321 strlen(homehost),
322 buf);
323
324 return (memcmp(&sb->set_uuid2, hash, 8)==0);
325 }
326
327 static void uuid_from_super0(struct supertype *st, int uuid[4])
328 {
329 mdp_super_t *super = st->sb;
330 uuid[0] = super->set_uuid0;
331 if (super->minor_version >= 90) {
332 uuid[1] = super->set_uuid1;
333 uuid[2] = super->set_uuid2;
334 uuid[3] = super->set_uuid3;
335 } else {
336 uuid[1] = 0;
337 uuid[2] = 0;
338 uuid[3] = 0;
339 }
340 }
341
342 static void getinfo_super0(struct supertype *st, struct mdinfo *info, char *map)
343 {
344 mdp_super_t *sb = st->sb;
345 int working = 0;
346 int i;
347 int map_disks = info->array.raid_disks;
348
349 memset(info, 0, sizeof(*info));
350 info->array.major_version = sb->major_version;
351 info->array.minor_version = sb->minor_version;
352 info->array.patch_version = sb->patch_version;
353 info->array.raid_disks = sb->raid_disks;
354 info->array.level = sb->level;
355 info->array.layout = sb->layout;
356 info->array.md_minor = sb->md_minor;
357 info->array.ctime = sb->ctime;
358 info->array.utime = sb->utime;
359 info->array.chunk_size = sb->chunk_size;
360 info->array.state = sb->state;
361 info->component_size = sb->size*2;
362
363 info->disk.state = sb->this_disk.state;
364 info->disk.major = sb->this_disk.major;
365 info->disk.minor = sb->this_disk.minor;
366 info->disk.raid_disk = sb->this_disk.raid_disk;
367 info->disk.number = sb->this_disk.number;
368
369 info->events = md_event(sb);
370 info->data_offset = 0;
371
372 sprintf(info->text_version, "0.%d", sb->minor_version);
373 info->safe_mode_delay = 200;
374
375 uuid_from_super0(st, info->uuid);
376
377 info->recovery_start = MaxSector;
378 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
379 info->reshape_active = 1;
380 info->reshape_progress = sb->reshape_position;
381 info->new_level = sb->new_level;
382 info->delta_disks = sb->delta_disks;
383 info->new_layout = sb->new_layout;
384 info->new_chunk = sb->new_chunk;
385 if (info->delta_disks < 0)
386 info->array.raid_disks -= info->delta_disks;
387 } else
388 info->reshape_active = 0;
389
390 info->recovery_blocked = info->reshape_active;
391
392 sprintf(info->name, "%d", sb->md_minor);
393 /* work_disks is calculated rather than read directly */
394 for (i=0; i < MD_SB_DISKS; i++)
395 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
396 (sb->disks[i].raid_disk < (unsigned)info->array.raid_disks) &&
397 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
398 !(sb->disks[i].state & (1<<MD_DISK_FAULTY))) {
399 working ++;
400 if (map && i < map_disks)
401 map[i] = 1;
402 } else if (map && i < map_disks)
403 map[i] = 0;
404 info->array.working_disks = working;
405 }
406
407 static struct mdinfo *container_content0(struct supertype *st, char *subarray)
408 {
409 struct mdinfo *info;
410
411 if (subarray)
412 return NULL;
413
414 info = malloc(sizeof(*info));
415 getinfo_super0(st, info, NULL);
416 return info;
417 }
418
419 static int update_super0(struct supertype *st, struct mdinfo *info,
420 char *update,
421 char *devname, int verbose,
422 int uuid_set, char *homehost)
423 {
424 /* NOTE: for 'assemble' and 'force' we need to return non-zero
425 * if any change was made. For others, the return value is
426 * ignored.
427 */
428 int rv = 0;
429 int uuid[4];
430 mdp_super_t *sb = st->sb;
431 if (strcmp(update, "sparc2.2")==0 ) {
432 /* 2.2 sparc put the events in the wrong place
433 * So we copy the tail of the superblock
434 * up 4 bytes before continuing
435 */
436 __u32 *sb32 = (__u32*)sb;
437 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
438 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
439 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
440 if (verbose >= 0)
441 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
442 devname);
443 } else if (strcmp(update, "super-minor") ==0) {
444 sb->md_minor = info->array.md_minor;
445 if (verbose > 0)
446 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
447 devname, info->array.md_minor);
448 } else if (strcmp(update, "summaries") == 0) {
449 unsigned int i;
450 /* set nr_disks, active_disks, working_disks,
451 * failed_disks, spare_disks based on disks[]
452 * array in superblock.
453 * Also make sure extra slots aren't 'failed'
454 */
455 sb->nr_disks = sb->active_disks =
456 sb->working_disks = sb->failed_disks =
457 sb->spare_disks = 0;
458 for (i=0; i < MD_SB_DISKS ; i++)
459 if (sb->disks[i].major ||
460 sb->disks[i].minor) {
461 int state = sb->disks[i].state;
462 if (state & (1<<MD_DISK_REMOVED))
463 continue;
464 sb->nr_disks++;
465 if (state & (1<<MD_DISK_ACTIVE))
466 sb->active_disks++;
467 if (state & (1<<MD_DISK_FAULTY))
468 sb->failed_disks++;
469 else
470 sb->working_disks++;
471 if (state == 0)
472 sb->spare_disks++;
473 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
474 sb->disks[i].state = 0;
475 } else if (strcmp(update, "force-one")==0) {
476 /* Not enough devices for a working array, so
477 * bring this one up-to-date.
478 */
479 __u32 ehi = sb->events_hi, elo = sb->events_lo;
480 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
481 sb->events_lo = (info->events) & 0xFFFFFFFF;
482 if (sb->events_hi != ehi ||
483 sb->events_lo != elo)
484 rv = 1;
485 } else if (strcmp(update, "force-array")==0) {
486 /* degraded array and 'force' requested, so
487 * maybe need to mark it 'clean'
488 */
489 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
490 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
491 /* need to force clean */
492 sb->state |= (1 << MD_SB_CLEAN);
493 rv = 1;
494 }
495 } else if (strcmp(update, "assemble")==0) {
496 int d = info->disk.number;
497 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
498 int mask = (1<<MD_DISK_WRITEMOSTLY);
499 int add = 0;
500 if (sb->minor_version >= 91)
501 /* During reshape we don't insist on everything
502 * being marked 'sync'
503 */
504 add = (1<<MD_DISK_SYNC);
505 if (((sb->disks[d].state & ~mask) | add)
506 != (unsigned)info->disk.state) {
507 sb->disks[d].state = info->disk.state | wonly;
508 rv = 1;
509 }
510 if (info->reshape_active &&
511 sb->minor_version > 90 && (sb->reshape_position+1) != 0 &&
512 info->delta_disks >= 0 &&
513 info->reshape_progress < sb->reshape_position) {
514 sb->reshape_position = info->reshape_progress;
515 rv = 1;
516 }
517 if (info->reshape_active &&
518 sb->minor_version > 90 && (sb->reshape_position+1) != 0 &&
519 info->delta_disks < 0 &&
520 info->reshape_progress > sb->reshape_position) {
521 sb->reshape_position = info->reshape_progress;
522 rv = 1;
523 }
524 } else if (strcmp(update, "linear-grow-new") == 0) {
525 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
526 sb->disks[info->disk.number].number = info->disk.number;
527 sb->disks[info->disk.number].major = info->disk.major;
528 sb->disks[info->disk.number].minor = info->disk.minor;
529 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
530 sb->disks[info->disk.number].state = info->disk.state;
531 sb->this_disk = sb->disks[info->disk.number];
532 } else if (strcmp(update, "linear-grow-update") == 0) {
533 sb->raid_disks = info->array.raid_disks;
534 sb->nr_disks = info->array.nr_disks;
535 sb->active_disks = info->array.active_disks;
536 sb->working_disks = info->array.working_disks;
537 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
538 sb->disks[info->disk.number].number = info->disk.number;
539 sb->disks[info->disk.number].major = info->disk.major;
540 sb->disks[info->disk.number].minor = info->disk.minor;
541 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
542 sb->disks[info->disk.number].state = info->disk.state;
543 } else if (strcmp(update, "resync") == 0) {
544 /* make sure resync happens */
545 sb->state &= ~(1<<MD_SB_CLEAN);
546 sb->recovery_cp = 0;
547 } else if (strcmp(update, "homehost") == 0 &&
548 homehost) {
549 uuid_set = 0;
550 update = "uuid";
551 info->uuid[0] = sb->set_uuid0;
552 info->uuid[1] = sb->set_uuid1;
553 } else if (strcmp(update, "uuid") == 0) {
554 if (!uuid_set && homehost) {
555 char buf[20];
556 char *hash = sha1_buffer(homehost,
557 strlen(homehost),
558 buf);
559 memcpy(info->uuid+2, hash, 8);
560 }
561 sb->set_uuid0 = info->uuid[0];
562 sb->set_uuid1 = info->uuid[1];
563 sb->set_uuid2 = info->uuid[2];
564 sb->set_uuid3 = info->uuid[3];
565 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
566 struct bitmap_super_s *bm;
567 bm = (struct bitmap_super_s*)(sb+1);
568 uuid_from_super0(st, uuid);
569 memcpy(bm->uuid, uuid, 16);
570 }
571 } else if (strcmp(update, "no-bitmap") == 0) {
572 sb->state &= ~(1<<MD_SB_BITMAP_PRESENT);
573 } else if (strcmp(update, "_reshape_progress")==0)
574 sb->reshape_position = info->reshape_progress;
575 else if (strcmp(update, "writemostly")==0)
576 sb->state |= (1<<MD_DISK_WRITEMOSTLY);
577 else if (strcmp(update, "readwrite")==0)
578 sb->state &= ~(1<<MD_DISK_WRITEMOSTLY);
579 else
580 rv = -1;
581
582 sb->sb_csum = calc_sb0_csum(sb);
583 return rv;
584 }
585
586 /*
587 * For verion-0 superblock, the homehost is 'stored' in the
588 * uuid. 8 bytes for a hash of the host leaving 8 bytes
589 * of random material.
590 * We use the first 8 bytes (64bits) of the sha1 of the
591 * host name
592 */
593
594
595 static int init_super0(struct supertype *st, mdu_array_info_t *info,
596 unsigned long long size, char *ignored_name, char *homehost,
597 int *uuid)
598 {
599 mdp_super_t *sb;
600 int spares;
601
602 if (posix_memalign((void**)&sb, 4096,
603 MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
604 fprintf(stderr, Name ": %s could not allocate superblock\n", __func__);
605 return 0;
606 }
607 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
608
609 st->sb = sb;
610 if (info == NULL) {
611 /* zeroing the superblock */
612 return 0;
613 }
614
615 spares = info->working_disks - info->active_disks;
616 if (info->raid_disks + spares > MD_SB_DISKS) {
617 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
618 info->raid_disks , spares, MD_SB_DISKS);
619 return 0;
620 }
621
622 sb->md_magic = MD_SB_MAGIC;
623 sb->major_version = 0;
624 sb->minor_version = 90;
625 sb->patch_version = 0;
626 sb->gvalid_words = 0; /* ignored */
627 sb->ctime = time(0);
628 sb->level = info->level;
629 if (size != (unsigned long long)info->size)
630 return 0;
631 sb->size = info->size;
632 sb->nr_disks = info->nr_disks;
633 sb->raid_disks = info->raid_disks;
634 sb->md_minor = info->md_minor;
635 sb->not_persistent = 0;
636 if (uuid) {
637 sb->set_uuid0 = uuid[0];
638 sb->set_uuid1 = uuid[1];
639 sb->set_uuid2 = uuid[2];
640 sb->set_uuid3 = uuid[3];
641 } else {
642 int rfd = open("/dev/urandom", O_RDONLY);
643 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
644 sb->set_uuid0 = random();
645 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
646 sb->set_uuid1 = random();
647 sb->set_uuid2 = random();
648 sb->set_uuid3 = random();
649 }
650 if (rfd >= 0)
651 close(rfd);
652 }
653 if (homehost) {
654 char buf[20];
655 char *hash = sha1_buffer(homehost,
656 strlen(homehost),
657 buf);
658 memcpy(&sb->set_uuid2, hash, 8);
659 }
660
661 sb->utime = sb->ctime;
662 sb->state = info->state;
663 sb->active_disks = info->active_disks;
664 sb->working_disks = info->working_disks;
665 sb->failed_disks = info->failed_disks;
666 sb->spare_disks = info->spare_disks;
667 sb->events_hi = 0;
668 sb->events_lo = 1;
669
670 sb->layout = info->layout;
671 sb->chunk_size = info->chunk_size;
672
673 return 1;
674 }
675
676 struct devinfo {
677 int fd;
678 char *devname;
679 mdu_disk_info_t disk;
680 struct devinfo *next;
681 };
682
683 #ifndef MDASSEMBLE
684 /* Add a device to the superblock being created */
685 static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
686 int fd, char *devname)
687 {
688 mdp_super_t *sb = st->sb;
689 mdp_disk_t *dk = &sb->disks[dinfo->number];
690 struct devinfo *di, **dip;
691
692 dk->number = dinfo->number;
693 dk->major = dinfo->major;
694 dk->minor = dinfo->minor;
695 dk->raid_disk = dinfo->raid_disk;
696 dk->state = dinfo->state;
697 /* In case our source disk was writemostly, don't copy that bit */
698 dk->state &= ~(1<<MD_DISK_WRITEMOSTLY);
699
700 sb->this_disk = sb->disks[dinfo->number];
701 sb->sb_csum = calc_sb0_csum(sb);
702
703 dip = (struct devinfo **)&st->info;
704 while (*dip)
705 dip = &(*dip)->next;
706 di = malloc(sizeof(struct devinfo));
707 di->fd = fd;
708 di->devname = devname;
709 di->disk = *dinfo;
710 di->next = NULL;
711 *dip = di;
712
713 return 0;
714 }
715 #endif
716
717 static int store_super0(struct supertype *st, int fd)
718 {
719 unsigned long long dsize;
720 unsigned long long offset;
721 mdp_super_t *super = st->sb;
722
723 if (!get_dev_size(fd, NULL, &dsize))
724 return 1;
725
726 if (dsize < MD_RESERVED_SECTORS*512)
727 return 2;
728
729 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
730
731 offset *= 512;
732
733 if (lseek64(fd, offset, 0)< 0LL)
734 return 3;
735
736 if (write(fd, super, sizeof(*super)) != sizeof(*super))
737 return 4;
738
739 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
740 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
741 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
742 if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) !=
743 ROUND_UP(sizeof(*bm),4096))
744 return 5;
745 }
746
747 fsync(fd);
748 return 0;
749 }
750
751 #ifndef MDASSEMBLE
752 static int write_init_super0(struct supertype *st)
753 {
754 mdp_super_t *sb = st->sb;
755 int rv = 0;
756 struct devinfo *di;
757
758 for (di = st->info ; di && ! rv ; di = di->next) {
759
760 if (di->disk.state == 1)
761 continue;
762 if (di->fd == -1)
763 continue;
764 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
765 ;
766
767 sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY);
768
769 sb->this_disk = sb->disks[di->disk.number];
770 sb->sb_csum = calc_sb0_csum(sb);
771 rv = store_super0(st, di->fd);
772
773 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
774 rv = st->ss->write_bitmap(st, di->fd);
775
776 if (rv)
777 fprintf(stderr,
778 Name ": failed to write superblock to %s\n",
779 di->devname);
780 }
781 return rv;
782 }
783 #endif
784
785 static int compare_super0(struct supertype *st, struct supertype *tst)
786 {
787 /*
788 * return:
789 * 0 same, or first was empty, and second was copied
790 * 1 second had wrong number
791 * 2 wrong uuid
792 * 3 wrong other info
793 */
794 mdp_super_t *first = st->sb;
795 mdp_super_t *second = tst->sb;
796 int uuid1[4], uuid2[4];
797
798 if (second->md_magic != MD_SB_MAGIC)
799 return 1;
800 if (!first) {
801 if (posix_memalign((void**)&first, 4096,
802 MD_SB_BYTES +
803 ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
804 fprintf(stderr, Name
805 ": %s could not allocate superblock\n", __func__);
806 return 1;
807 }
808 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
809 st->sb = first;
810 return 0;
811 }
812
813 uuid_from_super0(st, uuid1);
814 uuid_from_super0(tst, uuid2);
815 if (!same_uuid(uuid1, uuid2, 0))
816 return 2;
817 if (first->major_version != second->major_version ||
818 first->minor_version != second->minor_version ||
819 first->patch_version != second->patch_version ||
820 first->gvalid_words != second->gvalid_words ||
821 first->ctime != second->ctime ||
822 first->level != second->level ||
823 first->size != second->size ||
824 first->raid_disks != second->raid_disks )
825 return 3;
826
827 return 0;
828 }
829
830
831 static void free_super0(struct supertype *st);
832
833 static int load_super0(struct supertype *st, int fd, char *devname)
834 {
835 /* try to read in the superblock
836 * Return:
837 * 0 on success
838 * 1 on cannot get superblock
839 * 2 on superblock meaningless
840 */
841 unsigned long long dsize;
842 unsigned long long offset;
843 mdp_super_t *super;
844 int uuid[4];
845 struct bitmap_super_s *bsb;
846
847 free_super0(st);
848
849 if (!get_dev_size(fd, devname, &dsize))
850 return 1;
851
852 if (dsize < MD_RESERVED_SECTORS*512) {
853 if (devname)
854 fprintf(stderr, Name
855 ": %s is too small for md: size is %llu sectors.\n",
856 devname, dsize);
857 return 1;
858 }
859
860 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
861
862 offset *= 512;
863
864 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
865
866 if (lseek64(fd, offset, 0)< 0LL) {
867 if (devname)
868 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
869 devname, strerror(errno));
870 return 1;
871 }
872
873 if (posix_memalign((void**)&super, 4096,
874 MD_SB_BYTES +
875 ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
876 fprintf(stderr, Name
877 ": %s could not allocate superblock\n", __func__);
878 return 1;
879 }
880
881 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
882 if (devname)
883 fprintf(stderr, Name ": Cannot read superblock on %s\n",
884 devname);
885 free(super);
886 return 1;
887 }
888
889 if (st->ss && st->minor_version == 9)
890 super0_swap_endian(super);
891
892 if (super->md_magic != MD_SB_MAGIC) {
893 if (devname)
894 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
895 devname, MD_SB_MAGIC, super->md_magic);
896 free(super);
897 return 2;
898 }
899
900 if (super->major_version != 0) {
901 if (devname)
902 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
903 devname, super->major_version);
904 free(super);
905 return 2;
906 }
907 st->sb = super;
908
909 if (st->ss == NULL) {
910 st->ss = &super0;
911 st->minor_version = super->minor_version;
912 st->max_devs = MD_SB_DISKS;
913 st->info = NULL;
914 }
915
916 /* Now check on the bitmap superblock */
917 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
918 return 0;
919 /* Read the bitmap superblock and make sure it looks
920 * valid. If it doesn't clear the bit. An --assemble --force
921 * should get that written out.
922 */
923 if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096))
924 != ROUND_UP(sizeof(struct bitmap_super_s),4096))
925 goto no_bitmap;
926
927 uuid_from_super0(st, uuid);
928 bsb = (struct bitmap_super_s *)(super+1);
929 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
930 memcmp(bsb->uuid, uuid, 16) != 0)
931 goto no_bitmap;
932 return 0;
933
934 no_bitmap:
935 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
936
937 return 0;
938 }
939
940 static struct supertype *match_metadata_desc0(char *arg)
941 {
942 struct supertype *st = malloc(sizeof(*st));
943 if (!st) return st;
944
945 memset(st, 0, sizeof(*st));
946 st->container_dev = NoMdDev;
947 st->ss = &super0;
948 st->info = NULL;
949 st->minor_version = 90;
950 st->max_devs = MD_SB_DISKS;
951 st->sb = NULL;
952 /* we sometimes get 00.90 */
953 while (arg[0] == '0' && arg[1] == '0')
954 arg++;
955 if (strcmp(arg, "0") == 0 ||
956 #ifdef DEFAULT_OLD_METADATA /* ifndef in super1.c */
957 strcmp(arg, "default") == 0 ||
958 #endif /* DEFAULT_OLD_METADATA */
959 strcmp(arg, "0.90") == 0 ||
960 strcmp(arg, "") == 0 /* no metadata - i.e. non_persistent */
961 )
962 return st;
963
964 st->minor_version = 91; /* reshape in progress */
965 if (strcmp(arg, "0.91") == 0) /* For dup_super support */
966 return st;
967
968 st->minor_version = 9; /* flag for 'byte-swapped' */
969 if (strcmp(arg, "0.swap")==0 ||
970 strcmp(arg, "0.9") == 0) /* For dup_super support */
971 return st;
972
973 free(st);
974 return NULL;
975 }
976
977 static __u64 avail_size0(struct supertype *st, __u64 devsize)
978 {
979 if (devsize < MD_RESERVED_SECTORS)
980 return 0ULL;
981 return MD_NEW_SIZE_SECTORS(devsize);
982 }
983
984 static int add_internal_bitmap0(struct supertype *st, int *chunkp,
985 int delay, int write_behind,
986 unsigned long long size, int may_change,
987 int major)
988 {
989 /*
990 * The bitmap comes immediately after the superblock and must be 60K in size
991 * at most. The default size is between 30K and 60K
992 *
993 * size is in sectors, chunk is in bytes !!!
994 */
995 unsigned long long bits;
996 unsigned long long max_bits = (60*1024 - sizeof(bitmap_super_t))*8;
997 unsigned long long min_chunk;
998 int chunk = *chunkp;
999 mdp_super_t *sb = st->sb;
1000 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
1001 int uuid[4];
1002
1003
1004 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1005 bits = (size * 512) / min_chunk + 1;
1006 while (bits > max_bits) {
1007 min_chunk *= 2;
1008 bits = (bits+1)/2;
1009 }
1010 if (chunk == UnSet) {
1011 /* A chunk size less than a few Megabytes gives poor
1012 * performance without increasing resync noticeably
1013 */
1014 chunk = min_chunk;
1015 if (chunk < 64*1024*1024)
1016 chunk = 64*1024*1024;
1017 } else if ((unsigned long long)chunk < min_chunk)
1018 return 0; /* chunk size too small */
1019
1020 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1021
1022 memset(bms, 0, sizeof(*bms));
1023 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1024 bms->version = __cpu_to_le32(major);
1025 uuid_from_super0(st, uuid);
1026 memcpy(bms->uuid, uuid, 16);
1027 bms->chunksize = __cpu_to_le32(chunk);
1028 bms->daemon_sleep = __cpu_to_le32(delay);
1029 bms->sync_size = __cpu_to_le64(size);
1030 bms->write_behind = __cpu_to_le32(write_behind);
1031 *chunkp = chunk;
1032 return 1;
1033 }
1034
1035
1036 static void locate_bitmap0(struct supertype *st, int fd)
1037 {
1038 unsigned long long dsize;
1039 unsigned long long offset;
1040
1041 if (!get_dev_size(fd, NULL, &dsize))
1042 return;
1043
1044 if (dsize < MD_RESERVED_SECTORS*512)
1045 return;
1046
1047 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1048
1049 offset *= 512;
1050
1051 offset += MD_SB_BYTES;
1052
1053 lseek64(fd, offset, 0);
1054 }
1055
1056 static int write_bitmap0(struct supertype *st, int fd)
1057 {
1058 unsigned long long dsize;
1059 unsigned long long offset;
1060 mdp_super_t *sb = st->sb;
1061
1062 int rv = 0;
1063
1064 int towrite, n;
1065 char abuf[4096+4096];
1066 char *buf = (char*)(((long)(abuf+4096))&~4095L);
1067
1068 if (!get_dev_size(fd, NULL, &dsize))
1069 return 1;
1070
1071
1072 if (dsize < MD_RESERVED_SECTORS*512)
1073 return -1;
1074
1075 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1076
1077 offset *= 512;
1078
1079 if (lseek64(fd, offset + 4096, 0)< 0LL)
1080 return 3;
1081
1082 memset(buf, 0xff, 4096);
1083 memcpy(buf, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t));
1084 towrite = 60*1024;
1085 while (towrite > 0) {
1086 n = towrite;
1087 if (n > 4096)
1088 n = 4096;
1089 n = write(fd, buf, n);
1090 if (n > 0)
1091 towrite -= n;
1092 else
1093 break;
1094 memset(buf, 0xff, 4096);
1095 }
1096 fsync(fd);
1097 if (towrite)
1098 rv = -2;
1099
1100 return rv;
1101 }
1102
1103 static void free_super0(struct supertype *st)
1104 {
1105 if (st->sb)
1106 free(st->sb);
1107 while (st->info) {
1108 struct devinfo *di = st->info;
1109 st->info = di->next;
1110 if (di->fd >= 0)
1111 close(di->fd);
1112 free(di);
1113 }
1114 st->sb = NULL;
1115 }
1116
1117 #ifndef MDASSEMBLE
1118 static int validate_geometry0(struct supertype *st, int level,
1119 int layout, int raiddisks,
1120 int *chunk, unsigned long long size,
1121 char *subdev, unsigned long long *freesize,
1122 int verbose)
1123 {
1124 unsigned long long ldsize;
1125 int fd;
1126 unsigned int tbmax = 4;
1127
1128 /* prior to linux 3.1, a but limits usable device size to 2TB.
1129 * It was introduced in 2.6.29, but we won't worry about that detail
1130 */
1131 if (get_linux_version() < 3001000)
1132 tbmax = 2;
1133
1134 if (level == LEVEL_CONTAINER) {
1135 if (verbose)
1136 fprintf(stderr, Name ": 0.90 metadata does not support containers\n");
1137 return 0;
1138 }
1139 if (raiddisks > MD_SB_DISKS) {
1140 if (verbose)
1141 fprintf(stderr, Name ": 0.90 metadata supports at most %d devices per array\n",
1142 MD_SB_DISKS);
1143 return 0;
1144 }
1145 if (size >= tbmax * 2ULL*1024*1024*1024) {
1146 if (verbose)
1147 fprintf(stderr, Name ": 0.90 metadata supports at most "
1148 "%d terabytes per device\n", tbmax);
1149 return 0;
1150 }
1151 if (chunk && *chunk == UnSet)
1152 *chunk = DEFAULT_CHUNK;
1153
1154 if (!subdev)
1155 return 1;
1156
1157 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1158 if (fd < 0) {
1159 if (verbose)
1160 fprintf(stderr, Name ": super0.90 cannot open %s: %s\n",
1161 subdev, strerror(errno));
1162 return 0;
1163 }
1164
1165 if (!get_dev_size(fd, subdev, &ldsize)) {
1166 close(fd);
1167 return 0;
1168 }
1169 close(fd);
1170
1171 if (ldsize < MD_RESERVED_SECTORS * 512)
1172 return 0;
1173 *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
1174 return 1;
1175 }
1176 #endif /* MDASSEMBLE */
1177
1178 struct superswitch super0 = {
1179 #ifndef MDASSEMBLE
1180 .examine_super = examine_super0,
1181 .brief_examine_super = brief_examine_super0,
1182 .export_examine_super = export_examine_super0,
1183 .detail_super = detail_super0,
1184 .brief_detail_super = brief_detail_super0,
1185 .write_init_super = write_init_super0,
1186 .validate_geometry = validate_geometry0,
1187 .add_to_super = add_to_super0,
1188 #endif
1189 .match_home = match_home0,
1190 .uuid_from_super = uuid_from_super0,
1191 .getinfo_super = getinfo_super0,
1192 .container_content = container_content0,
1193 .update_super = update_super0,
1194 .init_super = init_super0,
1195 .store_super = store_super0,
1196 .compare_super = compare_super0,
1197 .load_super = load_super0,
1198 .match_metadata_desc = match_metadata_desc0,
1199 .avail_size = avail_size0,
1200 .add_internal_bitmap = add_internal_bitmap0,
1201 .locate_bitmap = locate_bitmap0,
1202 .write_bitmap = write_bitmap0,
1203 .free_super = free_super0,
1204 .name = "0.90",
1205 };