/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
- * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
+ * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
*
*
* This program is free software; you can redistribute it and/or modify
* Australia
*/
+#define HAVE_STDINT_H 1
#include "mdadm.h"
-
+#include "sha1.h"
/*
* All handling for the 0.90.0 version superblock is in
* this file.
sb->cp_events_hi = sb->cp_events_lo;
sb->cp_events_lo = t32;
-}
+}
#ifndef MDASSEMBLE
-static void examine_super0(void *sbv)
+static void examine_super0(void *sbv, char *homehost)
{
mdp_super_t *sb = sbv;
time_t atime;
printf(" Magic : %08x\n", sb->md_magic);
printf(" Version : %02d.%02d.%02d\n", sb->major_version, sb->minor_version,
sb->patch_version);
- if (sb->minor_version >= 90)
- printf(" UUID : %08x:%08x:%08x:%08x\n", sb->set_uuid0, sb->set_uuid1,
+ if (sb->minor_version >= 90) {
+ printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
sb->set_uuid2, sb->set_uuid3);
- else
+ if (homehost) {
+ char buf[20];
+ void *hash = sha1_buffer(homehost,
+ strlen(homehost),
+ buf);
+ if (memcmp(&sb->set_uuid2, hash, 8)==0)
+ printf(" (local to host %s)", homehost);
+ }
+ printf("\n");
+ } else
printf(" UUID : %08x\n", sb->set_uuid0);
+ if (sb->not_persistent)
+ printf(" Eedk : not persistent\n");
+
atime = sb->ctime;
printf(" Creation Time : %.24s\n", ctime(&atime));
c=map_num(pers, sb->level);
printf(" Raid Level : %s\n", c?c:"-unknown-");
if ((int)sb->level >= 0) {
int ddsks=0;
- printf(" Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
+ printf(" Used Dev Size : %d%s\n", sb->size,
+ human_size((long long)sb->size<<10));
switch(sb->level) {
case 1: ddsks=1;break;
case 4:
printf(" New Layout : %s\n", c?c:"-unknown-");
}
if (sb->level == 10) {
- printf(" New Layout : near=%d, far=%d\n",
- sb->new_layout&255, (sb->new_layout>>8)&255);
+ printf(" New Layout : near=%d, %s=%d\n",
+ sb->new_layout&255,
+ (sb->new_layout&0x10000)?"offset":"far",
+ (sb->new_layout>>8)&255);
}
}
if (sb->new_chunk != sb->chunk_size)
printf(" Layout : %s\n", c?c:"-unknown-");
}
if (sb->level == 10) {
- printf(" Layout : near=%d, far=%d\n",
- sb->layout&255, (sb->layout>>8)&255);
+ printf(" Layout : near=%d, %s=%d\n",
+ sb->layout&255,
+ (sb->layout&0x10000)?"offset":"far",
+ (sb->layout>>8)&255);
}
switch(sb->level) {
case 0:
case 4:
case 5:
+ case 6:
+ case 10:
printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
break;
case -1:
printf(" Rounding : %dK\n", sb->chunk_size/1024);
break;
- default: break;
+ default: break;
}
printf("\n");
printf(" Number Major Minor RaidDevice State\n");
if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
if (wonly) printf(" write-mostly");
if (dp->state == 0) printf(" spare");
- if ((dv=map_dev(dp->major, dp->minor)))
+ if ((dv=map_dev(dp->major, dp->minor, 0)))
printf(" %s", dv);
printf("\n");
if (d == -1) printf("\n");
{
mdp_super_t *sb = sbv;
char *c=map_num(pers, sb->level);
+ char devname[20];
+
+ sprintf(devname, "/dev/md%d", sb->md_minor);
printf("ARRAY %s level=%s num-devices=%d UUID=",
- get_md_name(sb->md_minor),
+ devname,
c?c:"-unknown-", sb->raid_disks);
if (sb->minor_version >= 90)
printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
printf("\n");
}
-static void detail_super0(void *sbv)
+static void detail_super0(void *sbv, char *homehost)
{
mdp_super_t *sb = sbv;
printf(" UUID : ");
sb->set_uuid2, sb->set_uuid3);
else
printf("%08x", sb->set_uuid0);
+ if (homehost) {
+ char buf[20];
+ void *hash = sha1_buffer(homehost,
+ strlen(homehost),
+ buf);
+ if (memcmp(&sb->set_uuid2, hash, 8)==0)
+ printf(" (local to host %s)", homehost);
+ }
printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
}
else
printf("%08x", sb->set_uuid0);
}
+
+static void export_super0(void *sbv)
+{
+ mdp_super_t *sb = sbv;
+ printf("MD_UUID=");
+ if (sb->minor_version >= 90)
+ printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
+ sb->set_uuid2, sb->set_uuid3);
+ else
+ printf("%08x", sb->set_uuid0);
+ printf("\n");
+}
#endif
+
+static int match_home0(void *sbv, char *homehost)
+{
+ mdp_super_t *sb = sbv;
+ char buf[20];
+ char *hash = sha1_buffer(homehost,
+ strlen(homehost),
+ buf);
+
+ return (memcmp(&sb->set_uuid2, hash, 8)==0);
+}
+
static void uuid_from_super0(int uuid[4], void * sbv)
{
mdp_super_t *super = sbv;
}
}
-static void getinfo_super0(struct mdinfo *info, mddev_ident_t ident, void *sbv)
+static void getinfo_super0(struct mdinfo *info, void *sbv)
{
mdp_super_t *sb = sbv;
int working = 0;
info->array.layout = sb->layout;
info->array.md_minor = sb->md_minor;
info->array.ctime = sb->ctime;
+ info->array.utime = sb->utime;
+ info->array.chunk_size = sb->chunk_size;
+ info->array.state = sb->state;
+ info->component_size = sb->size*2;
info->disk.state = sb->this_disk.state;
info->disk.major = sb->this_disk.major;
info->disk.number = sb->this_disk.number;
info->events = md_event(sb);
+ info->data_offset = 0;
uuid_from_super0(info->uuid, sbv);
- ident->name[0] = 0;
+ if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
+ info->reshape_active = 1;
+ info->reshape_progress = sb->reshape_position;
+ info->new_level = sb->new_level;
+ info->delta_disks = sb->delta_disks;
+ info->new_layout = sb->new_layout;
+ info->new_chunk = sb->new_chunk;
+ } else
+ info->reshape_active = 0;
+
+ sprintf(info->name, "%d", sb->md_minor);
/* work_disks is calculated rather than read directly */
for (i=0; i < MD_SB_DISKS; i++)
if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
+ (sb->disks[i].raid_disk < info->array.raid_disks) &&
(sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
!(sb->disks[i].state & (1<<MD_DISK_FAULTY)))
working ++;
}
-static int update_super0(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
+static int update_super0(struct mdinfo *info, void *sbv, char *update,
+ char *devname, int verbose,
+ int uuid_set, char *homehost)
{
+ /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
+ * For others, the return value is ignored.
+ */
int rv = 0;
mdp_super_t *sb = sbv;
if (strcmp(update, "sparc2.2")==0 ) {
} else if (i >= sb->raid_disks && sb->disks[i].number == 0)
sb->disks[i].state = 0;
}
- if (strcmp(update, "force")==0) {
+ if (strcmp(update, "force-one")==0) {
+ /* Not enough devices for a working array, so
+ * bring this one up-to-date.
+ */
+ __u32 ehi = sb->events_hi, elo = sb->events_lo;
sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
sb->events_lo = (info->events) & 0xFFFFFFFF;
- if (sb->level == 5 || sb->level == 4 || sb->level == 6)
+ if (sb->events_hi != ehi ||
+ sb->events_lo != elo)
+ rv = 1;
+ }
+ if (strcmp(update, "force-array")==0) {
+ /* degraded array and 'force' requested, so
+ * maybe need to mark it 'clean'
+ */
+ if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
+ (sb->state & (1 << MD_SB_CLEAN)) == 0) {
/* need to force clean */
sb->state |= (1 << MD_SB_CLEAN);
+ rv = 1;
+ }
}
if (strcmp(update, "assemble")==0) {
int d = info->disk.number;
int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
- sb->disks[d].state &= ~(1<<MD_DISK_WRITEMOSTLY);
- if (sb->disks[d].state != info->disk.state) {
- sb->disks[d].state = info->disk.state & wonly;
+ if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
+ != info->disk.state) {
+ sb->disks[d].state = info->disk.state | wonly;
rv = 1;
}
}
- if (strcmp(update, "newdev") == 0) {
- int d = info->disk.number;
- memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
- sb->disks[d].number = d;
- sb->disks[d].major = info->disk.major;
- sb->disks[d].minor = info->disk.minor;
- sb->disks[d].raid_disk = info->disk.raid_disk;
- sb->disks[d].state = info->disk.state;
- sb->this_disk = sb->disks[d];
- }
if (strcmp(update, "grow") == 0) {
sb->raid_disks = info->array.raid_disks;
sb->nr_disks = info->array.nr_disks;
sb->state &= ~(1<<MD_SB_CLEAN);
sb->recovery_cp = 0;
}
+ if (strcmp(update, "homehost") == 0 &&
+ homehost) {
+ uuid_set = 0;
+ update = "uuid";
+ info->uuid[0] = sb->set_uuid0;
+ info->uuid[1] = sb->set_uuid1;
+ }
if (strcmp(update, "uuid") == 0) {
+ if (!uuid_set && homehost) {
+ char buf[20];
+ char *hash = sha1_buffer(homehost,
+ strlen(homehost),
+ buf);
+ memcpy(info->uuid+2, hash, 8);
+ }
sb->set_uuid0 = info->uuid[0];
sb->set_uuid1 = info->uuid[1];
sb->set_uuid2 = info->uuid[2];
sb->set_uuid3 = info->uuid[3];
+ if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
+ struct bitmap_super_s *bm;
+ bm = (struct bitmap_super_s*)(sb+1);
+ uuid_from_super0((int*)bm->uuid, sbv);
+ }
}
+ if (strcmp(update, "_reshape_progress")==0)
+ sb->reshape_position = info->reshape_progress;
sb->sb_csum = calc_sb0_csum(sb);
return rv;
}
-static __u64 event_super0(void *sbv)
-{
- mdp_super_t *sb = sbv;
- return md_event(sb);
-}
-
+/*
+ * For verion-0 superblock, the homehost is 'stored' in the
+ * uuid. 8 bytes for a hash of the host leaving 8 bytes
+ * of random material.
+ * We use the first 8 bytes (64bits) of the sha1 of the
+ * host name
+ */
-static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info, unsigned long long size, char *ignored_name)
+static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
+ unsigned long long size, char *ignored_name, char *homehost,
+ int *uuid)
{
mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
int spares;
- int rfd;
memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
if (info->major_version == -1) {
/* zeroing the superblock */
+ *sbp = sb;
return 0;
}
return 0;
}
- rfd = open("/dev/urandom", O_RDONLY);
sb->md_magic = MD_SB_MAGIC;
sb->major_version = 0;
sb->minor_version = 90;
sb->patch_version = 0;
sb->gvalid_words = 0; /* ignored */
- if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
- sb->set_uuid0 = random();
sb->ctime = time(0);
sb->level = info->level;
if (size != info->size)
sb->raid_disks = info->raid_disks;
sb->md_minor = info->md_minor;
sb->not_persistent = 0;
- if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
- sb->set_uuid1 = random();
- sb->set_uuid2 = random();
- sb->set_uuid3 = random();
+ if (uuid) {
+ sb->set_uuid0 = uuid[0];
+ sb->set_uuid1 = uuid[1];
+ sb->set_uuid2 = uuid[2];
+ sb->set_uuid3 = uuid[3];
+ } else {
+ int rfd = open("/dev/urandom", O_RDONLY);
+ if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
+ sb->set_uuid0 = random();
+ if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
+ sb->set_uuid1 = random();
+ sb->set_uuid2 = random();
+ sb->set_uuid3 = random();
+ }
+ if (rfd >= 0)
+ close(rfd);
+ }
+ if (homehost) {
+ char buf[20];
+ char *hash = sha1_buffer(homehost,
+ strlen(homehost),
+ buf);
+ memcpy(&sb->set_uuid2, hash, 8);
}
- if (rfd >= 0)
- close(rfd);
sb->utime = sb->ctime;
sb->state = info->state;
{
mdp_super_t *sb = sbv;
mdp_disk_t *dk = &sb->disks[dinfo->number];
-
+
dk->number = dinfo->number;
dk->major = dinfo->major;
dk->minor = dinfo->minor;
static int store_super0(struct supertype *st, int fd, void *sbv)
{
- unsigned long size;
unsigned long long dsize;
unsigned long long offset;
mdp_super_t *super = sbv;
-
-#ifdef BLKGETSIZE64
- if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
- {
- if (ioctl(fd, BLKGETSIZE, &size))
- return 1;
- else
- dsize = ((unsigned long long)size)<<9;
- }
+
+ if (!get_dev_size(fd, NULL, &dsize))
+ return 1;
if (dsize < MD_RESERVED_SECTORS*2*512)
return 2;
-
+
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
offset *= 512;
if (write(fd, super, sizeof(*super)) != sizeof(*super))
return 4;
+ if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
+ struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
+ if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
+ if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
+ return 5;
+ }
+
fsync(fd);
return 0;
}
}
sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY);
- sb->disks[dinfo->number].state |= (1<<MD_DISK_SYNC);
sb->this_disk = sb->disks[dinfo->number];
sb->sb_csum = calc_sb0_csum(sb);
if (second->md_magic != MD_SB_MAGIC)
return 1;
if (!first) {
- first = malloc(MD_SB_BYTES);
- memcpy(first, second, MD_SB_BYTES);
+ first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
+ memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
*firstp = first;
return 0;
}
* 1 on cannot get superblock
* 2 on superblock meaningless
*/
- unsigned long size;
unsigned long long dsize;
unsigned long long offset;
mdp_super_t *super;
+ int uuid[4];
+ struct bitmap_super_s *bsb;
-#ifdef BLKGETSIZE64
- if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
- {
- if (ioctl(fd, BLKGETSIZE, &size)) {
- if (devname)
- fprintf(stderr, Name ": cannot find device size for %s: %s\n",
- devname, strerror(errno));
- return 1;
- } else
- dsize = size << 9;
- }
+ if (!get_dev_size(fd, devname, &dsize))
+ return 1;
- if (dsize < MD_RESERVED_SECTORS*2) {
+ if (dsize < MD_RESERVED_SECTORS*512 * 2) {
if (devname)
- fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
- devname, size);
+ fprintf(stderr, Name
+ ": %s is too small for md: size is %llu sectors.\n",
+ devname, dsize);
return 1;
}
-
+
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
offset *= 512;
st->max_devs = MD_SB_DISKS;
}
+ /* Now check on the bitmap superblock */
+ if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
+ return 0;
+ /* Read the bitmap superblock and make sure it looks
+ * valid. If it doesn't clear the bit. An --assemble --force
+ * should get that written out.
+ */
+ if (read(fd, super+1, sizeof(struct bitmap_super_s))
+ != sizeof(struct bitmap_super_s))
+ goto no_bitmap;
+
+ uuid_from_super0(uuid, super);
+ bsb = (struct bitmap_super_s *)(super+1);
+ if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
+ memcmp(bsb->uuid, uuid, 16) != 0)
+ goto no_bitmap;
+ return 0;
+
+ no_bitmap:
+ super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
+
return 0;
}
return MD_NEW_SIZE_SECTORS(devsize);
}
-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)
+static int add_internal_bitmap0(struct supertype *st, void *sbv, int *chunkp,
+ int delay, int write_behind,
+ unsigned long long size, int may_change,
+ int major)
{
/*
* The bitmap comes immediately after the superblock and must be 60K in size
* at most. The default size is between 30K and 60K
*
- * size is in K, chunk is in bytes !!!
+ * size is in sectors, chunk is in bytes !!!
*/
unsigned long long bits;
unsigned long long max_bits = 60*1024*8;
unsigned long long min_chunk;
+ int chunk = *chunkp;
mdp_super_t *sb = sbv;
bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
-
+
min_chunk = 4096; /* sub-page chunks don't work yet.. */
- bits = (size * 512)/ min_chunk +1;
+ bits = (size * 512) / min_chunk + 1;
while (bits > max_bits) {
min_chunk *= 2;
bits = (bits+1)/2;
bms->daemon_sleep = __cpu_to_le32(delay);
bms->sync_size = __cpu_to_le64(size);
bms->write_behind = __cpu_to_le32(write_behind);
-
-
-
+ *chunkp = chunk;
return 1;
}
-
+
void locate_bitmap0(struct supertype *st, int fd, void *sbv)
{
unsigned long long dsize;
- unsigned long size;
unsigned long long offset;
-#ifdef BLKGETSIZE64
- if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
- {
- if (ioctl(fd, BLKGETSIZE, &size))
- return;
- else
- dsize = ((unsigned long long)size)<<9;
- }
- if (dsize < MD_RESERVED_SECTORS*2)
+ if (!get_dev_size(fd, NULL, &dsize))
return;
-
+
+ if (dsize < MD_RESERVED_SECTORS*512 * 2)
+ return;
+
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
offset *= 512;
int write_bitmap0(struct supertype *st, int fd, void *sbv)
{
- unsigned long size;
unsigned long long dsize;
unsigned long long offset;
mdp_super_t *sb = sbv;
int towrite, n;
char buf[4096];
-#ifdef BLKGETSIZE64
- if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
- {
- if (ioctl(fd, BLKGETSIZE, &size))
- return 1;
- else
- dsize = ((unsigned long long)size)<<9;
- }
+ if (!get_dev_size(fd, NULL, &dsize))
+ return 1;
+
+
+ if (dsize < MD_RESERVED_SECTORS*512 * 2)
+ return -1;
- if (dsize < MD_RESERVED_SECTORS*2)
- return -1;
-
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
offset *= 512;
.brief_examine_super = brief_examine_super0,
.detail_super = detail_super0,
.brief_detail_super = brief_detail_super0,
+ .export_super = export_super0,
#endif
+ .match_home = match_home0,
.uuid_from_super = uuid_from_super0,
.getinfo_super = getinfo_super0,
.update_super = update_super0,
- .event_super = event_super0,
.init_super = init_super0,
.add_to_super = add_to_super0,
.store_super = store_super0,