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