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