X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fmdadm.git;a=blobdiff_plain;f=Incremental.c;h=869ae8396802f81a729e3678d60de55362f38b03;hp=df5314ddaeb8e7d057cade91d7b360305d32b162;hb=d7288ddc3a06a0912f5f0a3f23ccca76a66ff332;hpb=06c7f68e40134cbef3186426ebf63c4f6d4e30f5 diff --git a/Incremental.c b/Incremental.c index df5314dd..869ae839 100644 --- a/Incremental.c +++ b/Incremental.c @@ -32,7 +32,7 @@ static int count_active(struct supertype *st, int mdfd, char **availp, struct mdinfo *info); -static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra, +static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, int number, __u64 events, int verbose, char *array_name); @@ -40,7 +40,7 @@ int Incremental(char *devname, int verbose, int runstop, struct supertype *st, char *homehost, int autof) { /* Add this device to an array, creating the array if necessary - * and starting the array if sensibe or - if runstop>0 - if possible. + * and starting the array if sensible or - if runstop>0 - if possible. * * This has several steps: * @@ -56,6 +56,7 @@ int Incremental(char *devname, int verbose, int runstop, * - Choose a free, high number. * - Use a partitioned device unless strong suggestion not to. * e.g. auto=md + * Don't choose partitioned for containers. * 5/ Find out if array already exists * 5a/ if it does not * - choose a name, from mdadm.conf or 'name' field in array. @@ -67,6 +68,7 @@ int Incremental(char *devname, int verbose, int runstop, * - add the device * 6/ Make sure /var/run/mdadm.map contains this array. * 7/ Is there enough devices to possibly start the array? + * For a container, this means running Incremental_container. * 7a/ if not, finish with success. * 7b/ if yes, * - read all metadata and arrange devices like -A does @@ -74,7 +76,7 @@ int Incremental(char *devname, int verbose, int runstop, * start the array (auto-readonly). */ struct stat stb; - struct mdinfo info, info2; + struct mdinfo info; struct mddev_ident_s *array_list, *match; char chosen_name[1024]; int rv; @@ -83,14 +85,16 @@ int Incremental(char *devname, int verbose, int runstop, int dfd, mdfd; char *avail; int active_disks; - + int uuid_for_name = 0; + char *name_to_use; + char nbuf[64]; struct createinfo *ci = conf_get_create_info(); if (autof == 0) autof = ci->autof; - /* 1/ Check if devices is permitted by mdadm.conf */ + /* 1/ Check if device is permitted by mdadm.conf */ if (!conf_test_dev(devname)) { if (verbose >= 0) @@ -140,9 +144,18 @@ int Incremental(char *devname, int verbose, int runstop, close(dfd); return 1; } - st->ss->getinfo_super(st, &info); close (dfd); + if (st->ss->container_content && st->loaded_container) { + /* This is a pre-built container array, so we do something + * rather different. + */ + return Incremental_container(st, devname, verbose, runstop, + autof); + } + + memset(&info, 0, sizeof(info)); + st->ss->getinfo_super(st, &info); /* 3/ Check if there is a match in mdadm.conf */ array_list = conf_get_ident(NULL); @@ -206,12 +219,8 @@ int Incremental(char *devname, int verbose, int runstop, /* 3a/ if not, check for homehost match. If no match, reject. */ if (!match) { if (homehost == NULL || - st->ss->match_home(st, homehost) == 0) { - if (verbose >= 0) - fprintf(stderr, Name - ": not found in mdadm.conf and not identified by homehost.\n"); - return 2; - } + st->ss->match_home(st, homehost) != 1) + uuid_for_name = 1; } /* 4/ Determine device number. */ /* - If in mdadm.conf with std name, use that */ @@ -221,21 +230,37 @@ int Incremental(char *devname, int verbose, int runstop, /* - Choose a free, high number. */ /* - Use a partitioned device unless strong suggestion not to. */ /* e.g. auto=md */ + mp = map_by_uuid(&map, info.uuid); + + if (uuid_for_name && ! mp) { + name_to_use = fname_from_uuid(st, &info, nbuf); + if (verbose >= 0) + fprintf(stderr, Name + ": not found in mdadm.conf and not identified by homehost" + " - using uuid based name\n"); + } else + name_to_use = info.name; + if (match && is_standard(match->devname, &devnum)) /* We have devnum now */; - else if ((mp = map_by_uuid(&map, info.uuid)) != NULL) + else if (mp != NULL) devnum = mp->devnum; else { /* Have to guess a bit. */ int use_partitions = 1; char *np, *ep; + char *nm, nbuf[1024]; + struct stat stb2; + if ((autof&7) == 3 || (autof&7) == 5) use_partitions = 0; - np = strchr(info.name, ':'); + if (st->ss->external) + use_partitions = 0; + np = strchr(name_to_use, ':'); if (np) np++; else - np = info.name; + np = name_to_use; devnum = strtoul(np, &ep, 10); if (ep > np && *ep == 0) { /* This is a number. Let check that it is unused. */ @@ -244,61 +269,68 @@ int Incremental(char *devname, int verbose, int runstop, } else devnum = -1; + if (match) + nm = match->devname; + else { + sprintf(nbuf, "/dev/md/%s", np); + nm = nbuf; + } + if (stat(nm, &stb2) == 0 && + S_ISBLK(stb2.st_mode) && + major(stb2.st_rdev) == (use_partitions ? + get_mdp_major() : MD_MAJOR)) { + if (use_partitions) + devnum = minor(stb2.st_rdev) >> MdpMinorShift; + else + devnum = minor(stb2.st_rdev); + if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) + devnum = -1; + } + if (devnum < 0) { /* Haven't found anything yet, choose something free */ - /* There is similar code in mdopen.c - should unify */ - for (devnum = 127 ; devnum != 128 ; - devnum = devnum ? devnum-1 : (1<<22)-1) { - if (mddev_busy(use_partitions ? - (-1-devnum) : devnum)) - break; - } - if (devnum == 128) { + devnum = find_free_devnum(use_partitions); + + if (devnum == NoMdDev) { fprintf(stderr, Name ": No spare md devices!!\n"); return 2; } - } - devnum = use_partitions ? (-1-devnum) : devnum; + } else + devnum = use_partitions ? (-1-devnum) : devnum; } - mdfd = open_mddev_devnum(match ? match->devname : NULL, + + mdfd = open_mddev_devnum(match ? match->devname : mp ? mp->path : NULL, devnum, - info.name, - chosen_name); + name_to_use, + chosen_name, autof >> 3); if (mdfd < 0) { fprintf(stderr, Name ": failed to open %s: %s.\n", chosen_name, strerror(errno)); return 2; } + sysfs_init(&info, mdfd, 0); + /* 5/ Find out if array already exists */ if (! mddev_busy(devnum)) { /* 5a/ if it does not */ /* - choose a name, from mdadm.conf or 'name' field in array. */ /* - create the array */ /* - add the device */ - mdu_array_info_t ainf; - mdu_disk_info_t disk; - char md[20]; - struct sysarray *sra; - - memset(&ainf, 0, sizeof(ainf)); - ainf.major_version = st->ss->major; - ainf.minor_version = st->minor_version; - if (ioctl(mdfd, SET_ARRAY_INFO, &ainf) != 0) { - fprintf(stderr, Name - ": SET_ARRAY_INFO failed for %s: %s\b", + struct mdinfo *sra; + struct mdinfo dinfo; + + if (set_array_info(mdfd, st, &info) != 0) { + fprintf(stderr, Name ": failed to set array info for %s: %s\n", chosen_name, strerror(errno)); close(mdfd); return 2; } - sprintf(md, "%d.%d\n", st->ss->major, st->minor_version); - sra = sysfs_read(mdfd, devnum, GET_VERSION); - sysfs_set_str(sra, NULL, "metadata_version", md); - memset(&disk, 0, sizeof(disk)); - disk.major = major(stb.st_rdev); - disk.minor = minor(stb.st_rdev); - sysfs_free(sra); - if (ioctl(mdfd, ADD_NEW_DISK, &disk) != 0) { + + dinfo = info; + dinfo.disk.major = major(stb.st_rdev); + dinfo.disk.minor = minor(stb.st_rdev); + if (add_disk(mdfd, st, &info, &dinfo) != 0) { fprintf(stderr, Name ": failed to add %s to %s: %s.\n", devname, chosen_name, strerror(errno)); ioctl(mdfd, STOP_ARRAY, 0); @@ -319,6 +351,8 @@ int Incremental(char *devname, int verbose, int runstop, sysfs_free(sra); return 2; } + info.array.working_disks = 1; + sysfs_free(sra); } else { /* 5b/ if it does */ /* - check one drive in array to make sure metadata is a reasonably */ @@ -326,36 +360,28 @@ int Incremental(char *devname, int verbose, int runstop, /* - add the device */ char dn[20]; int dfd2; - mdu_disk_info_t disk; int err; - struct sysarray *sra; + struct mdinfo *sra; struct supertype *st2; - sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS | - GET_STATE)); - if (sra->major_version != st->ss->major || - sra->minor_version != st->minor_version) { - if (verbose >= 0) - fprintf(stderr, Name - ": %s has different metadata to chosen array %s %d.%d %d.%d.\n", - devname, chosen_name, - sra->major_version, sra->minor_version, - st->ss->major, st->minor_version); - close(mdfd); - return 1; - } + struct mdinfo info2, *d; + sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE)); + sprintf(dn, "%d:%d", sra->devs->disk.major, sra->devs->disk.minor); dfd2 = dev_open(dn, O_RDONLY); st2 = dup_super(st); - if (st2->ss->load_super(st2, dfd2, NULL)) { + if (st2->ss->load_super(st2, dfd2, NULL) || + st->ss->compare_super(st, st2) != 0) { fprintf(stderr, Name - ": Strange error loading metadata for %s.\n", - chosen_name); + ": metadata mismatch between %s and " + "chosen array %s\n", + devname, chosen_name); close(mdfd); close(dfd2); return 2; } close(dfd2); + memset(&info2, 0, sizeof(info2)); st2->ss->getinfo_super(st2, &info2); st2->ss->free_super(st2); if (info.array.level != info2.array.level || @@ -367,17 +393,19 @@ int Incremental(char *devname, int verbose, int runstop, close(mdfd); return 2; } - memset(&disk, 0, sizeof(disk)); - disk.major = major(stb.st_rdev); - disk.minor = minor(stb.st_rdev); - err = ioctl(mdfd, ADD_NEW_DISK, &disk); + info2.disk.major = major(stb.st_rdev); + info2.disk.minor = minor(stb.st_rdev); + /* add disk needs to know about containers */ + if (st->ss->external) + sra->array.level = LEVEL_CONTAINER; + err = add_disk(mdfd, st2, sra, &info2); if (err < 0 && errno == EBUSY) { /* could be another device present with the same * disk.number. Find and reject any such */ find_reject(mdfd, st, sra, info.disk.number, info.events, verbose, chosen_name); - err = ioctl(mdfd, ADD_NEW_DISK, &disk); + err = add_disk(mdfd, st2, sra, &info2); } if (err < 0) { fprintf(stderr, Name ": failed to add %s to %s: %s.\n", @@ -385,15 +413,28 @@ int Incremental(char *devname, int verbose, int runstop, close(mdfd); return 2; } + info.array.working_disks = 0; + for (d = sra->devs; d; d=d->next) + info.array.working_disks ++; + } /* 6/ Make sure /var/run/mdadm.map contains this array. */ map_update(&map, devnum, - info.array.major_version, - info.array.minor_version, + info.text_version, info.uuid, chosen_name); /* 7/ Is there enough devices to possibly start the array? */ /* 7a/ if not, finish with success. */ + if (info.array.level == LEVEL_CONTAINER) { + /* Try to assemble within the container */ + close(mdfd); + if (verbose >= 0) + fprintf(stderr, Name + ": container %s now has %d devices\n", + chosen_name, info.array.working_disks); + return Incremental(chosen_name, verbose, runstop, + NULL, homehost, autof); + } avail = NULL; active_disks = count_active(st, mdfd, &avail, &info); if (enough(info.array.level, info.array.raid_disks, @@ -427,7 +468,7 @@ int Incremental(char *devname, int verbose, int runstop, } } if (runstop > 0 || active_disks >= info.array.working_disks) { - struct sysarray *sra; + struct mdinfo *sra; /* Let's try to start it */ if (match && match->bitmap_file) { int bmfd = open(match->bitmap_file, O_RDWR); @@ -477,7 +518,7 @@ int Incremental(char *devname, int verbose, int runstop, return rv; } -static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra, +static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, int number, __u64 events, int verbose, char *array_name) { @@ -528,7 +569,7 @@ static int count_active(struct supertype *st, int mdfd, char **availp, struct mdinfo *d; int cnt = 0, cnt1 = 0; __u64 max_events = 0; - struct sysarray *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); + struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); char *avail = NULL; for (d = sra->devs ; d ; d = d->next) { @@ -595,7 +636,7 @@ void RebuildMap(void) int mdp = get_mdp_major(); for (md = mdstat ; md ; md = md->next) { - struct sysarray *sra = sysfs_read(-1, md->devnum, GET_DEVS); + struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); struct mdinfo *sd; for (sd = sra->devs ; sd ; sd = sd->next) { @@ -623,8 +664,8 @@ void RebuildMap(void) path = map_dev(MD_MAJOR, md->devnum, 0); else path = map_dev(mdp, (-1-md->devnum)<< 6, 0); - map_add(&map, md->devnum, st->ss->major, - st->minor_version, + map_add(&map, md->devnum, + info.text_version, info.uuid, path ? : "/unknown"); st->ss->free_super(st); break; @@ -654,8 +695,9 @@ int IncrementalScan(int verbose) char path[1024]; mdu_array_info_t array; mdu_bitmap_file_t bmf; - struct sysarray *sra; - int mdfd = open_mddev_devnum(me->path, me->devnum, NULL, path); + struct mdinfo *sra; + int mdfd = open_mddev_devnum(me->path, me->devnum, + NULL, path, 0); if (mdfd < 0) continue; if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || @@ -710,3 +752,161 @@ int IncrementalScan(int verbose) } return rv; } + +static char *container2devname(char *devname) +{ + int fd = open(devname, O_RDONLY); + char *mdname = NULL; + + if (fd >= 0) { + mdname = devnum2devname(fd2devnum(fd)); + close(fd); + } + + return mdname; +} + +int Incremental_container(struct supertype *st, char *devname, int verbose, + int runstop, int autof) +{ + /* Collect the contents of this container and for each + * array, choose a device name and assemble the array. + */ + + struct mdinfo *list = st->ss->container_content(st); + struct mdinfo *ra; + char *mdname = container2devname(devname); + + if (!mdname) { + fprintf(stderr, Name": failed to determine device name\n"); + return 2; + } + + for (ra = list ; ra ; ra = ra->next) { + struct mdinfo *dev; + int devnum = -1; + int mdfd; + char chosen_name[1024]; + int usepart = 1; + char *n; + int working = 0, preexist = 0; + struct map_ent *mp, *map = NULL; + char nbuf[64]; + char *name_to_use; + + if ((autof&7) == 3 || (autof&7) == 5) + usepart = 0; + + mp = map_by_uuid(&map, ra->uuid); + + name_to_use = ra->name; + if (! name_to_use || + ! *name_to_use || + (*devname != '/' || strncmp("UUID-", strrchr(devname,'/')+1,5) == 0) + ) + name_to_use = fname_from_uuid(st, ra, nbuf); + + if (mp) + devnum = mp->devnum; + else { + + n = name_to_use; + if (*n == 'd') + n++; + if (*n && devnum < 0) { + devnum = strtoul(n, &n, 10); + if (devnum >= 0 && (*n == 0 || *n == ' ')) { + /* Use this devnum */ + usepart = (name_to_use[0] == 'd'); + if (mddev_busy(usepart ? (-1-devnum) : devnum)) + devnum = -1; + } else + devnum = -1; + } + + if (devnum < 0) { + char *nm = name_to_use; + char nbuf[1024]; + struct stat stb; + if (strchr(nm, ':')) + nm = strchr(nm, ':')+1; + sprintf(nbuf, "/dev/md/%s", nm); + + if (stat(nbuf, &stb) == 0 && + S_ISBLK(stb.st_mode) && + major(stb.st_rdev) == (usepart ? + get_mdp_major() : MD_MAJOR)){ + if (usepart) + devnum = minor(stb.st_rdev) + >> MdpMinorShift; + else + devnum = minor(stb.st_rdev); + if (mddev_busy(usepart ? (-1-devnum) : devnum)) + devnum = -1; + } + } + + if (devnum >= 0) + devnum = usepart ? (-1-devnum) : devnum; + else + devnum = find_free_devnum(usepart); + } + mdfd = open_mddev_devnum(mp ? mp->path : NULL, devnum, name_to_use, + chosen_name, autof>>3); + + if (mdfd < 0) { + fprintf(stderr, Name ": failed to open %s: %s.\n", + chosen_name, strerror(errno)); + return 2; + } + + sysfs_init(ra, mdfd, 0); + sysfs_set_array(ra, md_get_version(mdfd)); + for (dev = ra->devs; dev; dev = dev->next) + if (sysfs_add_disk(ra, dev) == 0) + working++; + else if (errno == EEXIST) + preexist++; + if (working == 0) + /* Nothing new, don't try to start */ ; + else if (runstop > 0 || + (working + preexist) >= ra->array.working_disks) { + switch(ra->array.level) { + case LEVEL_LINEAR: + case LEVEL_MULTIPATH: + case 0: + sysfs_set_str(ra, NULL, "array_state", + "active"); + break; + default: + sysfs_set_str(ra, NULL, "array_state", + "readonly"); + /* start mdmon if needed. */ + if (!mdmon_running(st->container_dev)) + start_mdmon(st->container_dev); + ping_monitor(devnum2devname(st->container_dev)); + break; + } + sysfs_set_safemode(ra, ra->safe_mode_delay); + if (verbose >= 0) { + fprintf(stderr, Name + ": Started %s with %d devices", + chosen_name, working + preexist); + if (preexist) + fprintf(stderr, " (%d new)", working); + fprintf(stderr, "\n"); + } + /* FIXME should have an O_EXCL and wait for read-auto */ + } else + if (verbose >= 0) + fprintf(stderr, Name + ": %s assembled with %d devices but " + "not started\n", + chosen_name, working); + close(mdfd); + map_update(&map, devnum, + ra->text_version, + ra->uuid, chosen_name); + } + return 0; +}