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