X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fmdadm.git;a=blobdiff_plain;f=util.c;h=818f8392177e4ff584067d8e9ffec2c30e98cb31;hp=f1b0b952ab535c25f77236c8e9d020d0802c8091;hb=41b06495ba3c6da9bddef7ae89c2c633c4c21c5c;hpb=81a8a6941510b28fa9d6862e6bf42bbd48ee92d3 diff --git a/util.c b/util.c index f1b0b952..818f8392 100644 --- a/util.c +++ b/util.c @@ -24,7 +24,6 @@ #include "mdadm.h" #include "md_p.h" -#include #include #include #include @@ -32,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -82,8 +82,15 @@ struct blkpg_partition { aren't permitted). */ #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) -static struct dlm_hooks *dlm_hooks = NULL; static int is_dlm_hooks_ready = 0; + +int dlm_funs_ready(void) +{ + return is_dlm_hooks_ready ? 1 : 0; +} + +#ifndef MDASSEMBLE +static struct dlm_hooks *dlm_hooks = NULL; struct dlm_lock_resource *dlm_lock_res = NULL; static int ast_called = 0; @@ -92,11 +99,6 @@ struct dlm_lock_resource { struct dlm_lksb lksb; }; -int dlm_funs_ready(void) -{ - return is_dlm_hooks_ready ? 1 : 0; -} - /* Using poll(2) to wait for and dispatch ASTs */ static int poll_for_ast(dlm_lshandle_t ls) { @@ -142,36 +144,24 @@ int cluster_get_dlmlock(int *lockid) dlm_lock_res->ls = dlm_hooks->create_lockspace(cluster_name, O_RDWR); if (!dlm_lock_res->ls) { pr_err("%s failed to create lockspace\n", cluster_name); - goto out; + return -ENOMEM; } - /* Conversions need the lockid in the LKSB */ - if (flags & LKF_CONVERT) - dlm_lock_res->lksb.sb_lkid = *lockid; - snprintf(str, 64, "bitmap%s", cluster_name); - /* if flags with LKF_CONVERT causes below return ENOENT which means - * "No such file or directory" */ ret = dlm_hooks->ls_lock(dlm_lock_res->ls, LKM_PWMODE, &dlm_lock_res->lksb, flags, str, strlen(str), 0, dlm_ast, dlm_lock_res, NULL, NULL); if (ret) { pr_err("error %d when get PW mode on lock %s\n", errno, str); - goto out; + dlm_hooks->release_lockspace(cluster_name, dlm_lock_res->ls, 1); + return ret; } /* Wait for it to complete */ poll_for_ast(dlm_lock_res->ls); *lockid = dlm_lock_res->lksb.sb_lkid; - errno = dlm_lock_res->lksb.sb_status; - if (errno) { - pr_err("error %d happened in ast with lock %s\n", errno, str); - goto out; - } - -out: - return ret; + return dlm_lock_res->lksb.sb_status; } int cluster_release_dlmlock(int lockid) @@ -181,8 +171,6 @@ int cluster_release_dlmlock(int lockid) if (!cluster_name) return -1; - /* if flags with LKF_CONVERT causes below return EINVAL which means - * "Invalid argument" */ ret = dlm_hooks->ls_unlock(dlm_lock_res->ls, lockid, 0, &dlm_lock_res->lksb, dlm_lock_res); if (ret) { @@ -212,6 +200,16 @@ int cluster_release_dlmlock(int lockid) out: return ret; } +#else +int cluster_get_dlmlock(int *lockid) +{ + return -1; +} +int cluster_release_dlmlock(int lockid) +{ + return -1; +} +#endif /* * Parse a 128 bit uuid in 4 integers @@ -712,17 +710,22 @@ int check_raid(int fd, char *name) if (!st) return 0; - st->ss->load_super(st, fd, name); - /* Looks like a raid array .. */ - pr_err("%s appears to be part of a raid array:\n", - name); - st->ss->getinfo_super(st, &info, NULL); - st->ss->free_super(st); - crtime = info.array.ctime; - level = map_num(pers, info.array.level); - if (!level) level = "-unknown-"; - cont_err("level=%s devices=%d ctime=%s", - level, info.array.raid_disks, ctime(&crtime)); + if (st->ss->add_to_super != NULL) { + st->ss->load_super(st, fd, name); + /* Looks like a raid array .. */ + pr_err("%s appears to be part of a raid array:\n", name); + st->ss->getinfo_super(st, &info, NULL); + st->ss->free_super(st); + crtime = info.array.ctime; + level = map_num(pers, info.array.level); + if (!level) + level = "-unknown-"; + cont_err("level=%s devices=%d ctime=%s", + level, info.array.raid_disks, ctime(&crtime)); + } else { + /* Looks like GPT or MBR */ + pr_err("partition table exists on %s\n", name); + } return 1; } @@ -930,7 +933,7 @@ int get_data_disks(int level, int layout, int raid_disks) return data_disks; } -int devnm2devid(char *devnm) +dev_t devnm2devid(char *devnm) { /* First look in /sys/block/$DEVNM/dev for %d:%d * If that fails, try parsing out a number @@ -1041,7 +1044,8 @@ int dev_open(char *dev, int flags) int major; int minor; - if (!dev) return -1; + if (!dev) + return -1; flags |= O_DIRECT; if (get_maj_min(dev, &major, &minor)) { @@ -1067,7 +1071,7 @@ int dev_open(char *dev, int flags) int open_dev_flags(char *devnm, int flags) { - int devid; + dev_t devid; char buf[20]; devid = devnm2devid(devnm); @@ -1085,7 +1089,7 @@ int open_dev_excl(char *devnm) char buf[20]; int i; int flags = O_RDWR; - int devid = devnm2devid(devnm); + dev_t devid = devnm2devid(devnm); long delay = 1000; sprintf(buf, "%d:%d", major(devid), minor(devid)); @@ -1193,8 +1197,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp) subarray = xstrdup(subarray); } strcpy(container, dev); - if (sra) - sysfs_free(sra); + sysfs_free(sra); sra = sysfs_read(-1, container, GET_VERSION); if (sra && sra->text_version[0]) verstr = sra->text_version; @@ -1205,8 +1208,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp) for (i = 0; st == NULL && superlist[i] ; i++) st = superlist[i]->match_metadata_desc(verstr); - if (sra) - sysfs_free(sra); + sysfs_free(sra); if (st) { st->sb = NULL; if (subarrayp) @@ -1261,7 +1263,7 @@ struct supertype *guess_super_type(int fd, enum guess_types guess_type) */ struct superswitch *ss; struct supertype *st; - time_t besttime = 0; + unsigned int besttime = 0; int bestsuper = -1; int i; @@ -1322,7 +1324,7 @@ int get_dev_size(int fd, char *dname, unsigned long long *sizep) ldsize <<= 9; } else { if (dname) - pr_err("Cannot get size of %s: %s\b", + pr_err("Cannot get size of %s: %s\n", dname, strerror(errno)); return 0; } @@ -1331,6 +1333,22 @@ int get_dev_size(int fd, char *dname, unsigned long long *sizep) return 1; } +/* Return sector size of device in bytes */ +int get_dev_sector_size(int fd, char *dname, unsigned int *sectsizep) +{ + unsigned int sectsize; + + if (ioctl(fd, BLKSSZGET, §size) != 0) { + if (dname) + pr_err("Cannot get sector size of %s: %s\n", + dname, strerror(errno)); + return 0; + } + + *sectsizep = sectsize; + return 1; +} + /* Return true if this can only be a container, not a member device. * i.e. is and md device and size is zero */ @@ -1360,12 +1378,15 @@ static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart) unsigned long long curr_part_end; unsigned all_partitions, entry_size; unsigned part_nr; + unsigned int sector_size = 0; *endofpart = 0; BUILD_BUG_ON(sizeof(gpt) != 512); /* skip protective MBR */ - lseek(fd, 512, SEEK_SET); + if (!get_dev_sector_size(fd, NULL, §or_size)) + return 0; + lseek(fd, sector_size, SEEK_SET); /* read GPT header */ if (read(fd, &gpt, 512) != 512) return 0; @@ -1385,6 +1406,8 @@ static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart) part = (struct GPT_part_entry *)buf; + /* set offset to third block (GPT entries) */ + lseek(fd, sector_size*2, SEEK_SET); for (part_nr = 0; part_nr < all_partitions; part_nr++) { /* read partition entry */ if (read(fd, buf, entry_size) != (ssize_t)entry_size) @@ -1410,7 +1433,6 @@ static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart) static int get_last_partition_end(int fd, unsigned long long *endofpart) { struct MBR boot_sect; - struct MBR_part_record *part; unsigned long long curr_part_end; unsigned part_nr; int retval = 0; @@ -1427,21 +1449,26 @@ static int get_last_partition_end(int fd, unsigned long long *endofpart) if (boot_sect.magic == MBR_SIGNATURE_MAGIC) { retval = 1; /* found the correct signature */ - part = boot_sect.parts; for (part_nr = 0; part_nr < MBR_PARTITIONS; part_nr++) { + /* + * Have to make every access through boot_sect rather + * than using a pointer to the partition table (or an + * entry), since the entries are not properly aligned. + */ + /* check for GPT type */ - if (part->part_type == MBR_GPT_PARTITION_TYPE) { + if (boot_sect.parts[part_nr].part_type == + MBR_GPT_PARTITION_TYPE) { retval = get_gpt_last_partition_end(fd, endofpart); break; } /* check the last used lba for the current partition */ - curr_part_end = __le32_to_cpu(part->first_sect_lba) + - __le32_to_cpu(part->blocks_num); + curr_part_end = + __le32_to_cpu(boot_sect.parts[part_nr].first_sect_lba) + + __le32_to_cpu(boot_sect.parts[part_nr].blocks_num); if (curr_part_end > *endofpart) *endofpart = curr_part_end; - - part++; } } else { /* Unknown partition table */ @@ -1939,6 +1966,27 @@ __u32 random32(void) return rv; } +void random_uuid(__u8 *buf) +{ + int fd, i, len; + __u32 r[4]; + + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) + goto use_random; + len = read(fd, buf, 16); + close(fd); + if (len != 16) + goto use_random; + + return; + +use_random: + for (i = 0; i < 4; i++) + r[i] = random(); + memcpy(buf, r, 16); +} + #ifndef MDASSEMBLE int flush_metadata_updates(struct supertype *st) { @@ -2121,10 +2169,10 @@ void reopen_mddev(int mdfd) dup2(fd, mdfd); } +#ifndef MDASSEMBLE static struct cmap_hooks *cmap_hooks = NULL; static int is_cmap_hooks_ready = 0; -#ifndef MDASSEMBLE void set_cmap_hooks(void) { cmap_hooks = xmalloc(sizeof(struct cmap_hooks));