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