]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super0.c
Include homehost information in --detail where appropriate.
[thirdparty/mdadm.git] / super0.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 #include <openssl/sha.h> /* for SHA1 */
32 /*
33 * All handling for the 0.90.0 version superblock is in
34 * this file.
35 * This includes:
36 * - finding, loading, and writing the superblock.
37 * - initialising a new superblock
38 * - printing the superblock for --examine
39 * - printing part of the superblock for --detail
40 * .. other stuff
41 */
42
43
44 static unsigned long calc_sb0_csum(mdp_super_t *super)
45 {
46 unsigned long csum = super->sb_csum;
47 unsigned long newcsum;
48 super->sb_csum= 0 ;
49 newcsum = calc_csum(super, MD_SB_BYTES);
50 super->sb_csum = csum;
51 return newcsum;
52 }
53
54
55 void super0_swap_endian(struct mdp_superblock_s *sb)
56 {
57 /* as super0 superblocks are host-endian, it is sometimes
58 * useful to be able to swap the endianness
59 * as (almost) everything is u32's we byte-swap every 4byte
60 * number.
61 * We then also have to swap the events_hi and events_lo
62 */
63 char *sbc = (char *)sb;
64 __u32 t32;
65 int i;
66
67 for (i=0; i < MD_SB_BYTES ; i+=4) {
68 char t = sbc[i];
69 sbc[i] = sbc[i+3];
70 sbc[i+3] = t;
71 t=sbc[i+1];
72 sbc[i+1]=sbc[i+2];
73 sbc[i+2]=t;
74 }
75 t32 = sb->events_hi;
76 sb->events_hi = sb->events_lo;
77 sb->events_lo = t32;
78
79 t32 = sb->cp_events_hi;
80 sb->cp_events_hi = sb->cp_events_lo;
81 sb->cp_events_lo = t32;
82
83 }
84
85 #ifndef MDASSEMBLE
86
87 static void examine_super0(void *sbv)
88 {
89 mdp_super_t *sb = sbv;
90 time_t atime;
91 int d;
92 char *c;
93
94 printf(" Magic : %08x\n", sb->md_magic);
95 printf(" Version : %02d.%02d.%02d\n", sb->major_version, sb->minor_version,
96 sb->patch_version);
97 if (sb->minor_version >= 90)
98 printf(" UUID : %08x:%08x:%08x:%08x\n", sb->set_uuid0, sb->set_uuid1,
99 sb->set_uuid2, sb->set_uuid3);
100 else
101 printf(" UUID : %08x\n", sb->set_uuid0);
102
103 atime = sb->ctime;
104 printf(" Creation Time : %.24s\n", ctime(&atime));
105 c=map_num(pers, sb->level);
106 printf(" Raid Level : %s\n", c?c:"-unknown-");
107 if ((int)sb->level >= 0) {
108 int ddsks=0;
109 printf(" Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
110 switch(sb->level) {
111 case 1: ddsks=1;break;
112 case 4:
113 case 5: ddsks = sb->raid_disks-1; break;
114 case 6: ddsks = sb->raid_disks-2; break;
115 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
116 }
117 if (ddsks)
118 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size,
119 human_size(ddsks*(long long)sb->size<<10));
120 }
121 printf(" Raid Devices : %d\n", sb->raid_disks);
122 printf(" Total Devices : %d\n", sb->nr_disks);
123 printf("Preferred Minor : %d\n", sb->md_minor);
124 printf("\n");
125 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
126 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
127 if (sb->delta_disks) {
128 printf(" Delta Devices : %d", sb->delta_disks);
129 if (sb->delta_disks)
130 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
131 else
132 printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks);
133 }
134 if (sb->new_level != sb->level) {
135 c = map_num(pers, sb->new_level);
136 printf(" New Level : %s\n", c?c:"-unknown-");
137 }
138 if (sb->new_layout != sb->layout) {
139 if (sb->level == 5) {
140 c = map_num(r5layout, sb->new_layout);
141 printf(" New Layout : %s\n", c?c:"-unknown-");
142 }
143 if (sb->level == 10) {
144 printf(" New Layout : near=%d, %s=%d\n",
145 sb->new_layout&255,
146 (sb->new_layout&0x10000)?"offset":"far",
147 (sb->new_layout>>8)&255);
148 }
149 }
150 if (sb->new_chunk != sb->chunk_size)
151 printf(" New Chunksize : %d\n", sb->new_chunk);
152 printf("\n");
153 }
154 atime = sb->utime;
155 printf(" Update Time : %.24s\n", ctime(&atime));
156 printf(" State : %s\n",
157 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
158 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
159 printf("Internal Bitmap : present\n");
160 printf(" Active Devices : %d\n", sb->active_disks);
161 printf("Working Devices : %d\n", sb->working_disks);
162 printf(" Failed Devices : %d\n", sb->failed_disks);
163 printf(" Spare Devices : %d\n", sb->spare_disks);
164 if (calc_sb0_csum(sb) == sb->sb_csum)
165 printf(" Checksum : %x - correct\n", sb->sb_csum);
166 else
167 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
168 printf(" Events : %d.%d\n", sb->events_hi, sb->events_lo);
169 printf("\n");
170 if (sb->level == 5) {
171 c = map_num(r5layout, sb->layout);
172 printf(" Layout : %s\n", c?c:"-unknown-");
173 }
174 if (sb->level == 10) {
175 printf(" Layout : near=%d, %s=%d\n",
176 sb->layout&255,
177 (sb->layout&0x10000)?"offset":"far",
178 (sb->layout>>8)&255);
179 }
180 switch(sb->level) {
181 case 0:
182 case 4:
183 case 5:
184 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
185 break;
186 case -1:
187 printf(" Rounding : %dK\n", sb->chunk_size/1024);
188 break;
189 default: break;
190 }
191 printf("\n");
192 printf(" Number Major Minor RaidDevice State\n");
193 for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
194 mdp_disk_t *dp;
195 char *dv;
196 char nb[5];
197 int wonly;
198 if (d>=0) dp = &sb->disks[d];
199 else dp = &sb->this_disk;
200 snprintf(nb, sizeof(nb), "%4d", d);
201 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
202 dp->number, dp->major, dp->minor, dp->raid_disk);
203 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
204 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
205 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
206 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
207 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
208 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
209 if (wonly) printf(" write-mostly");
210 if (dp->state == 0) printf(" spare");
211 if ((dv=map_dev(dp->major, dp->minor, 0)))
212 printf(" %s", dv);
213 printf("\n");
214 if (d == -1) printf("\n");
215 }
216 }
217
218 static void brief_examine_super0(void *sbv)
219 {
220 mdp_super_t *sb = sbv;
221 char *c=map_num(pers, sb->level);
222
223 printf("ARRAY %s level=%s num-devices=%d UUID=",
224 get_md_name(sb->md_minor),
225 c?c:"-unknown-", sb->raid_disks);
226 if (sb->minor_version >= 90)
227 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
228 sb->set_uuid2, sb->set_uuid3);
229 else
230 printf("%08x", sb->set_uuid0);
231 printf("\n");
232 }
233
234 static void detail_super0(void *sbv, char *homehost)
235 {
236 mdp_super_t *sb = sbv;
237 printf(" UUID : ");
238 if (sb->minor_version >= 90)
239 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
240 sb->set_uuid2, sb->set_uuid3);
241 else
242 printf("%08x", sb->set_uuid0);
243 if (homehost) {
244 unsigned char *hash = SHA1((unsigned char *)homehost,
245 strlen(homehost),
246 NULL);
247 if (memcmp(&sb->set_uuid2, hash, 8)==0)
248 printf(" (local to host %s)", homehost);
249 }
250 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
251 }
252
253 static void brief_detail_super0(void *sbv)
254 {
255 mdp_super_t *sb = sbv;
256 printf(" UUID=");
257 if (sb->minor_version >= 90)
258 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
259 sb->set_uuid2, sb->set_uuid3);
260 else
261 printf("%08x", sb->set_uuid0);
262 }
263 #endif
264 static void uuid_from_super0(int uuid[4], void * sbv)
265 {
266 mdp_super_t *super = sbv;
267 uuid[0] = super->set_uuid0;
268 if (super->minor_version >= 90) {
269 uuid[1] = super->set_uuid1;
270 uuid[2] = super->set_uuid2;
271 uuid[3] = super->set_uuid3;
272 } else {
273 uuid[1] = 0;
274 uuid[2] = 0;
275 uuid[3] = 0;
276 }
277 }
278
279 static void getinfo_super0(struct mdinfo *info, void *sbv)
280 {
281 mdp_super_t *sb = sbv;
282 int working = 0;
283 int i;
284
285 info->array.major_version = sb->major_version;
286 info->array.minor_version = sb->minor_version;
287 info->array.patch_version = sb->patch_version;
288 info->array.raid_disks = sb->raid_disks;
289 info->array.level = sb->level;
290 info->array.layout = sb->layout;
291 info->array.md_minor = sb->md_minor;
292 info->array.ctime = sb->ctime;
293 info->array.utime = sb->utime;
294 info->array.chunk_size = sb->chunk_size;
295 info->component_size = sb->size*2;
296
297 info->disk.state = sb->this_disk.state;
298 info->disk.major = sb->this_disk.major;
299 info->disk.minor = sb->this_disk.minor;
300 info->disk.raid_disk = sb->this_disk.raid_disk;
301 info->disk.number = sb->this_disk.number;
302
303 info->events = md_event(sb);
304 info->data_offset = 0;
305
306 uuid_from_super0(info->uuid, sbv);
307
308 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
309 info->reshape_active = 1;
310 info->reshape_progress = sb->reshape_position;
311 info->new_level = sb->new_level;
312 info->delta_disks = sb->delta_disks;
313 info->new_layout = sb->new_layout;
314 info->new_chunk = sb->new_chunk;
315 } else
316 info->reshape_active = 0;
317
318 info->name[0] = 0;
319 /* work_disks is calculated rather than read directly */
320 for (i=0; i < MD_SB_DISKS; i++)
321 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
322 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
323 !(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
324 working ++;
325 info->array.working_disks = working;
326 }
327
328
329 static int update_super0(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
330 {
331 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
332 * For others, the return value is ignored.
333 */
334 int rv = 0;
335 mdp_super_t *sb = sbv;
336 if (strcmp(update, "sparc2.2")==0 ) {
337 /* 2.2 sparc put the events in the wrong place
338 * So we copy the tail of the superblock
339 * up 4 bytes before continuing
340 */
341 __u32 *sb32 = (__u32*)sb;
342 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
343 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
344 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
345 if (verbose >= 0)
346 fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n",
347 devname);
348 }
349 if (strcmp(update, "super-minor") ==0) {
350 sb->md_minor = info->array.md_minor;
351 if (verbose > 0)
352 fprintf(stderr, Name ": updating superblock of %s with minor number %d\n",
353 devname, info->array.md_minor);
354 }
355 if (strcmp(update, "summaries") == 0) {
356 int i;
357 /* set nr_disks, active_disks, working_disks,
358 * failed_disks, spare_disks based on disks[]
359 * array in superblock.
360 * Also make sure extra slots aren't 'failed'
361 */
362 sb->nr_disks = sb->active_disks =
363 sb->working_disks = sb->failed_disks =
364 sb->spare_disks = 0;
365 for (i=0; i < MD_SB_DISKS ; i++)
366 if (sb->disks[i].major ||
367 sb->disks[i].minor) {
368 int state = sb->disks[i].state;
369 if (state & (1<<MD_DISK_REMOVED))
370 continue;
371 sb->nr_disks++;
372 if (state & (1<<MD_DISK_ACTIVE))
373 sb->active_disks++;
374 if (state & (1<<MD_DISK_FAULTY))
375 sb->failed_disks++;
376 else
377 sb->working_disks++;
378 if (state == 0)
379 sb->spare_disks++;
380 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
381 sb->disks[i].state = 0;
382 }
383 if (strcmp(update, "force")==0) {
384 __u32 ehi = sb->events_hi, elo = sb->events_lo;
385 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
386 sb->events_lo = (info->events) & 0xFFFFFFFF;
387 if (sb->events_hi != ehi ||
388 sb->events_lo != elo)
389 rv = 1;
390 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
391 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
392 /* need to force clean */
393 sb->state |= (1 << MD_SB_CLEAN);
394 rv = 1;
395 }
396 }
397 if (strcmp(update, "assemble")==0) {
398 int d = info->disk.number;
399 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
400 if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
401 != info->disk.state) {
402 sb->disks[d].state = info->disk.state | wonly;
403 rv = 1;
404 }
405 }
406 if (strcmp(update, "newdev") == 0) {
407 int d = info->disk.number;
408 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
409 sb->disks[d].number = d;
410 sb->disks[d].major = info->disk.major;
411 sb->disks[d].minor = info->disk.minor;
412 sb->disks[d].raid_disk = info->disk.raid_disk;
413 sb->disks[d].state = info->disk.state;
414 sb->this_disk = sb->disks[d];
415 }
416 if (strcmp(update, "grow") == 0) {
417 sb->raid_disks = info->array.raid_disks;
418 sb->nr_disks = info->array.nr_disks;
419 sb->active_disks = info->array.active_disks;
420 sb->working_disks = info->array.working_disks;
421 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
422 sb->disks[info->disk.number].number = info->disk.number;
423 sb->disks[info->disk.number].major = info->disk.major;
424 sb->disks[info->disk.number].minor = info->disk.minor;
425 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
426 sb->disks[info->disk.number].state = info->disk.state;
427 if (sb->this_disk.number == info->disk.number)
428 sb->this_disk = sb->disks[info->disk.number];
429 }
430 if (strcmp(update, "resync") == 0) {
431 /* make sure resync happens */
432 sb->state &= ~(1<<MD_SB_CLEAN);
433 sb->recovery_cp = 0;
434 }
435 if (strcmp(update, "uuid") == 0) {
436 sb->set_uuid0 = info->uuid[0];
437 sb->set_uuid1 = info->uuid[1];
438 sb->set_uuid2 = info->uuid[2];
439 sb->set_uuid3 = info->uuid[3];
440 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
441 struct bitmap_super_s *bm;
442 bm = (struct bitmap_super_s*)(sb+1);
443 uuid_from_super0((int*)bm->uuid, sbv);
444 }
445 }
446 if (strcmp(update, "_reshape_progress")==0)
447 sb->reshape_position = info->reshape_progress;
448
449 sb->sb_csum = calc_sb0_csum(sb);
450 return rv;
451 }
452
453 static __u64 event_super0(void *sbv)
454 {
455 mdp_super_t *sb = sbv;
456 return md_event(sb);
457 }
458
459 /*
460 * For verion-0 superblock, the homehost is 'stored' in the
461 * uuid. 8 bytes for a hash of the host leaving 8 bytes
462 * of random material.
463 * We use the first 8 bytes (64bits) of the sha1 of the
464 * host name
465 */
466
467
468 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
469 unsigned long long size, char *ignored_name, char *homehost)
470 {
471 mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
472 int spares;
473 int rfd;
474 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
475
476 if (info->major_version == -1) {
477 /* zeroing the superblock */
478 *sbp = sb;
479 return 0;
480 }
481
482 spares = info->working_disks - info->active_disks;
483 if (info->raid_disks + spares > MD_SB_DISKS) {
484 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
485 info->raid_disks , spares, MD_SB_DISKS);
486 return 0;
487 }
488
489 rfd = open("/dev/urandom", O_RDONLY);
490 sb->md_magic = MD_SB_MAGIC;
491 sb->major_version = 0;
492 sb->minor_version = 90;
493 sb->patch_version = 0;
494 sb->gvalid_words = 0; /* ignored */
495 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
496 sb->set_uuid0 = random();
497 sb->ctime = time(0);
498 sb->level = info->level;
499 if (size != info->size)
500 return 0;
501 sb->size = info->size;
502 sb->nr_disks = info->nr_disks;
503 sb->raid_disks = info->raid_disks;
504 sb->md_minor = info->md_minor;
505 sb->not_persistent = 0;
506 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
507 sb->set_uuid1 = random();
508 sb->set_uuid2 = random();
509 sb->set_uuid3 = random();
510 }
511 if (rfd >= 0)
512 close(rfd);
513 if (homehost) {
514 unsigned char *hash = SHA1((unsigned char*)homehost,
515 strlen(homehost),
516 NULL);
517 memcpy(&sb->set_uuid2, hash, 8);
518 }
519
520 sb->utime = sb->ctime;
521 sb->state = info->state;
522 sb->active_disks = info->active_disks;
523 sb->working_disks = info->working_disks;
524 sb->failed_disks = info->failed_disks;
525 sb->spare_disks = info->spare_disks;
526 sb->events_hi = 0;
527 sb->events_lo = 1;
528
529 sb->layout = info->layout;
530 sb->chunk_size = info->chunk_size;
531
532 *sbp = sb;
533 return 1;
534 }
535
536 /* Add a device to the superblock being created */
537 static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
538 {
539 mdp_super_t *sb = sbv;
540 mdp_disk_t *dk = &sb->disks[dinfo->number];
541
542 dk->number = dinfo->number;
543 dk->major = dinfo->major;
544 dk->minor = dinfo->minor;
545 dk->raid_disk = dinfo->raid_disk;
546 dk->state = dinfo->state;
547 }
548
549 static int store_super0(struct supertype *st, int fd, void *sbv)
550 {
551 unsigned long size;
552 unsigned long long dsize;
553 unsigned long long offset;
554 mdp_super_t *super = sbv;
555
556 #ifdef BLKGETSIZE64
557 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
558 #endif
559 {
560 if (ioctl(fd, BLKGETSIZE, &size))
561 return 1;
562 else
563 dsize = ((unsigned long long)size)<<9;
564 }
565
566 if (dsize < MD_RESERVED_SECTORS*2*512)
567 return 2;
568
569 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
570
571 offset *= 512;
572
573 if (lseek64(fd, offset, 0)< 0LL)
574 return 3;
575
576 if (write(fd, super, sizeof(*super)) != sizeof(*super))
577 return 4;
578
579 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
580 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
581 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
582 write(fd, bm, sizeof(*bm));
583 }
584
585 fsync(fd);
586 return 0;
587 }
588
589 static int write_init_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
590 {
591 mdp_super_t *sb = sbv;
592 int fd = open(devname, O_RDWR|O_EXCL);
593 int rv;
594
595 if (fd < 0) {
596 fprintf(stderr, Name ": Failed to open %s to write superblock\n", devname);
597 return -1;
598 }
599
600 sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY);
601 sb->disks[dinfo->number].state |= (1<<MD_DISK_SYNC);
602
603 sb->this_disk = sb->disks[dinfo->number];
604 sb->sb_csum = calc_sb0_csum(sb);
605 rv = store_super0(st, fd, sb);
606
607 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
608 rv = st->ss->write_bitmap(st, fd, sbv);
609
610 close(fd);
611 if (rv)
612 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
613 return rv;
614 }
615
616 static int compare_super0(void **firstp, void *secondv)
617 {
618 /*
619 * return:
620 * 0 same, or first was empty, and second was copied
621 * 1 second had wrong number
622 * 2 wrong uuid
623 * 3 wrong other info
624 */
625 mdp_super_t *first = *firstp;
626 mdp_super_t *second = secondv;
627
628 int uuid1[4], uuid2[4];
629 if (second->md_magic != MD_SB_MAGIC)
630 return 1;
631 if (!first) {
632 first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
633 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
634 *firstp = first;
635 return 0;
636 }
637
638 uuid_from_super0(uuid1, first);
639 uuid_from_super0(uuid2, second);
640 if (!same_uuid(uuid1, uuid2, 0))
641 return 2;
642 if (first->major_version != second->major_version ||
643 first->minor_version != second->minor_version ||
644 first->patch_version != second->patch_version ||
645 first->gvalid_words != second->gvalid_words ||
646 first->ctime != second->ctime ||
647 first->level != second->level ||
648 first->size != second->size ||
649 first->raid_disks != second->raid_disks )
650 return 3;
651
652 return 0;
653 }
654
655
656 static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
657 {
658 /* try to read in the superblock
659 * Return:
660 * 0 on success
661 * 1 on cannot get superblock
662 * 2 on superblock meaningless
663 */
664 unsigned long size;
665 unsigned long long dsize;
666 unsigned long long offset;
667 mdp_super_t *super;
668 int uuid[4];
669 struct bitmap_super_s *bsb;
670
671 #ifdef BLKGETSIZE64
672 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
673 #endif
674 {
675 if (ioctl(fd, BLKGETSIZE, &size)) {
676 if (devname)
677 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
678 devname, strerror(errno));
679 return 1;
680 } else
681 dsize = size << 9;
682 }
683
684 if (dsize < MD_RESERVED_SECTORS*2) {
685 if (devname)
686 fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
687 devname, size);
688 return 1;
689 }
690
691 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
692
693 offset *= 512;
694
695 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
696
697 if (lseek64(fd, offset, 0)< 0LL) {
698 if (devname)
699 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
700 devname, strerror(errno));
701 return 1;
702 }
703
704 super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
705
706 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
707 if (devname)
708 fprintf(stderr, Name ": Cannot read superblock on %s\n",
709 devname);
710 free(super);
711 return 1;
712 }
713
714 if (st->ss && st->minor_version == 9)
715 super0_swap_endian(super);
716
717 if (super->md_magic != MD_SB_MAGIC) {
718 if (devname)
719 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
720 devname, MD_SB_MAGIC, super->md_magic);
721 free(super);
722 return 2;
723 }
724
725 if (super->major_version != 0) {
726 if (devname)
727 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
728 devname, super->major_version);
729 free(super);
730 return 2;
731 }
732 *sbp = super;
733 if (st->ss == NULL) {
734 st->ss = &super0;
735 st->minor_version = 90;
736 st->max_devs = MD_SB_DISKS;
737 }
738
739 /* Now check on the bitmap superblock */
740 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
741 return 0;
742 /* Read the bitmap superblock and make sure it looks
743 * valid. If it doesn't clear the bit. An --assemble --force
744 * should get that written out.
745 */
746 if (read(fd, super+1, sizeof(struct bitmap_super_s))
747 != sizeof(struct bitmap_super_s))
748 goto no_bitmap;
749
750 uuid_from_super0(uuid, super);
751 bsb = (struct bitmap_super_s *)(super+1);
752 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
753 memcmp(bsb->uuid, uuid, 16) != 0)
754 goto no_bitmap;
755 return 0;
756
757 no_bitmap:
758 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
759
760 return 0;
761 }
762
763 static struct supertype *match_metadata_desc0(char *arg)
764 {
765 struct supertype *st = malloc(sizeof(*st));
766 if (!st) return st;
767
768 st->ss = &super0;
769 st->minor_version = 90;
770 st->max_devs = MD_SB_DISKS;
771 if (strcmp(arg, "0") == 0 ||
772 strcmp(arg, "0.90") == 0 ||
773 strcmp(arg, "default") == 0
774 )
775 return st;
776
777 st->minor_version = 9; /* flag for 'byte-swapped' */
778 if (strcmp(arg, "0.swap")==0)
779 return st;
780
781 free(st);
782 return NULL;
783 }
784
785 static __u64 avail_size0(struct supertype *st, __u64 devsize)
786 {
787 if (devsize < MD_RESERVED_SECTORS*2)
788 return 0ULL;
789 return MD_NEW_SIZE_SECTORS(devsize);
790 }
791
792 static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major)
793 {
794 /*
795 * The bitmap comes immediately after the superblock and must be 60K in size
796 * at most. The default size is between 30K and 60K
797 *
798 * size is in sectors, chunk is in bytes !!!
799 */
800 unsigned long long bits;
801 unsigned long long max_bits = 60*1024*8;
802 unsigned long long min_chunk;
803 mdp_super_t *sb = sbv;
804 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
805
806
807 min_chunk = 4096; /* sub-page chunks don't work yet.. */
808 bits = (size * 512) / min_chunk + 1;
809 while (bits > max_bits) {
810 min_chunk *= 2;
811 bits = (bits+1)/2;
812 }
813 if (chunk == UnSet)
814 chunk = min_chunk;
815 else if (chunk < min_chunk)
816 return 0; /* chunk size too small */
817
818 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
819
820 memset(bms, 0, sizeof(*bms));
821 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
822 bms->version = __cpu_to_le32(major);
823 uuid_from_super0((int*)bms->uuid, sb);
824 bms->chunksize = __cpu_to_le32(chunk);
825 bms->daemon_sleep = __cpu_to_le32(delay);
826 bms->sync_size = __cpu_to_le64(size);
827 bms->write_behind = __cpu_to_le32(write_behind);
828
829 return 1;
830 }
831
832
833 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
834 {
835 unsigned long long dsize;
836 unsigned long size;
837 unsigned long long offset;
838 #ifdef BLKGETSIZE64
839 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
840 #endif
841 {
842 if (ioctl(fd, BLKGETSIZE, &size))
843 return;
844 else
845 dsize = ((unsigned long long)size)<<9;
846 }
847
848 if (dsize < MD_RESERVED_SECTORS*2)
849 return;
850
851 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
852
853 offset *= 512;
854
855 offset += MD_SB_BYTES;
856
857 lseek64(fd, offset, 0);
858 }
859
860 int write_bitmap0(struct supertype *st, int fd, void *sbv)
861 {
862 unsigned long size;
863 unsigned long long dsize;
864 unsigned long long offset;
865 mdp_super_t *sb = sbv;
866
867 int rv = 0;
868
869 int towrite, n;
870 char buf[4096];
871
872 #ifdef BLKGETSIZE64
873 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
874 #endif
875 {
876 if (ioctl(fd, BLKGETSIZE, &size))
877 return 1;
878 else
879 dsize = ((unsigned long long)size)<<9;
880 }
881
882 if (dsize < MD_RESERVED_SECTORS*2)
883 return -1;
884
885 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
886
887 offset *= 512;
888
889 if (lseek64(fd, offset + 4096, 0)< 0LL)
890 return 3;
891
892
893 if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
894 sizeof(bitmap_super_t))
895 return -2;
896 towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
897 memset(buf, 0xff, sizeof(buf));
898 while (towrite > 0) {
899 n = towrite;
900 if (n > sizeof(buf))
901 n = sizeof(buf);
902 n = write(fd, buf, n);
903 if (n > 0)
904 towrite -= n;
905 else
906 break;
907 }
908 fsync(fd);
909 if (towrite)
910 rv = -2;
911
912 return rv;
913 }
914
915 struct superswitch super0 = {
916 #ifndef MDASSEMBLE
917 .examine_super = examine_super0,
918 .brief_examine_super = brief_examine_super0,
919 .detail_super = detail_super0,
920 .brief_detail_super = brief_detail_super0,
921 #endif
922 .uuid_from_super = uuid_from_super0,
923 .getinfo_super = getinfo_super0,
924 .update_super = update_super0,
925 .event_super = event_super0,
926 .init_super = init_super0,
927 .add_to_super = add_to_super0,
928 .store_super = store_super0,
929 .write_init_super = write_init_super0,
930 .compare_super = compare_super0,
931 .load_super = load_super0,
932 .match_metadata_desc = match_metadata_desc0,
933 .avail_size = avail_size0,
934 .add_internal_bitmap = add_internal_bitmap0,
935 .locate_bitmap = locate_bitmap0,
936 .write_bitmap = write_bitmap0,
937 .major = 0,
938 .swapuuid = 0,
939 };