/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
- * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
*
*
* This program is free software; you can redistribute it and/or modify
#include <ctype.h>
#include <dirent.h>
#include <signal.h>
+#include <dlfcn.h>
+#include <stdint.h>
+#ifdef NO_COROSYNC
+ typedef uint64_t cmap_handle_t;
+ #define CS_OK 1
+#else
+ #include <corosync/cmap.h>
+#endif
+
/*
* following taken from linux/blkpg.h because they aren't
if (ioctl(fd, BLKPG, &a) == 0)
/* Very unlikely, but not a partition */
return 0;
- if (errno == ENXIO)
+ if (errno == ENXIO || errno == ENOTTY)
/* not a partition */
return 0;
case 1:
return avail_disks >= 1;
case 4:
+ if (avail_disks == raid_disks - 1 &&
+ !avail[raid_disks - 1])
+ /* If just the parity device is missing, then we
+ * have enough, even if not clean
+ */
+ return 1;
+ /* FALL THROUGH */
case 5:
if (clean)
return avail_disks >= raid_disks-1;
{
struct mdu_array_info_s array;
struct mdu_disk_info_s disk;
- int avail_disks = 0;
int i, rv;
char *avail;
continue;
if (disk.raid_disk < 0 || disk.raid_disk >= array.raid_disks)
continue;
- avail_disks++;
avail[disk.raid_disk] = 1;
}
/* This is used on an active array, so assume it is clean */
*/
unsigned char sb[1024];
time_t mtime;
- int size, bsize;
+ unsigned long long size;
+ int bsize;
if (lseek(fd, 1024,0)!= 1024)
return 0;
if (read(fd, sb, 1024)!= 1024)
mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
+ size <<= bsize;
pr_err("%s appears to contain an ext2fs file system\n",
name);
- cont_err("size=%dK mtime=%s",
- size*(1<<bsize), ctime(&mtime));
+ cont_err("size=%lluK mtime=%s", size, ctime(&mtime));
return 1;
}
*
*/
unsigned char sb[1024];
- unsigned long size;
+ unsigned long long size;
if (lseek(fd, 64*1024, 0) != 64*1024)
return 0;
if (read(fd, sb, 1024) != 1024)
return 0;
pr_err("%s appears to contain a reiserfs file system\n",name);
size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
- cont_err("size = %luK\n", size*4);
+ cont_err("size = %lluK\n", size*4);
return 1;
}
if (bytes < 5000*1024)
buf[0] = 0;
else if (bytes < 2*1024LL*1024LL*1024LL) {
- long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+ long cMiB = (bytes * 200LL / (1LL<<20) + 1) / 2;
long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
cMiB/100 , cMiB % 100,
cMB/100, cMB % 100);
} else {
- long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+ long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2;
long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
cGiB/100 , cGiB % 100,
buf[0] = 0;
else if (prefix == IEC) {
if (bytes < 2*1024LL*1024LL*1024LL) {
- long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+ long cMiB = (bytes * 200LL / (1LL<<20) +1) /2;
snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
cMiB/100 , cMiB % 100);
} else {
- long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+ long cGiB = (bytes * 200LL / (1LL<<30) +1) /2;
snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
cGiB/100 , cGiB % 100);
}
}
#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
+int get_maj_min(char *dev, int *major, int *minor)
+{
+ char *e;
+ *major = strtoul(dev, &e, 0);
+ return (e > dev && *e == ':' && e[1] &&
+ (*minor = strtoul(e+1, &e, 0)) >= 0 &&
+ *e == 0);
+}
+
int dev_open(char *dev, int flags)
{
/* like 'open', but if 'dev' matches %d:%d, create a temp
* block device and open that
*/
- char *e;
int fd = -1;
char devname[32];
int major;
if (!dev) return -1;
flags |= O_DIRECT;
- major = strtoul(dev, &e, 0);
- if (e > dev && *e == ':' && e[1] &&
- (minor = strtoul(e+1, &e, 0)) >= 0 &&
- *e == 0) {
+ if (get_maj_min(dev, &major, &minor)) {
snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
(int)getpid(), major, minor);
if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) {
delay *= 2;
}
if (i == 25)
- dprintf("%s: timeout waiting for %s\n", __func__, dev);
+ dprintf("timeout waiting for %s\n", dev);
}
struct superswitch *superlist[] =
char pathbuf[1024];
char *paths[4] = {
pathbuf,
- "/sbin/mdmon",
- "mdmon",
+ BINDIR "/mdmon",
+ "./mdmon",
NULL
};
status = execl("/bin/systemctl", "systemctl", "start",
pathbuf, NULL);
exit(1);
- case -1: pr_err("cannot run mdmon. "
- "Array remains readonly\n");
+ case -1: pr_err("cannot run mdmon. Array remains readonly\n");
return -1;
default: /* parent - good */
pid = wait(&status);
for (i = 0; paths[i]; i++)
if (paths[i][0]) {
- execl(paths[i], "mdmon",
+ execl(paths[i], paths[i],
devnm, NULL);
}
exit(1);
- case -1: pr_err("cannot run mdmon. "
- "Array remains readonly\n");
+ case -1: pr_err("cannot run mdmon. Array remains readonly\n");
return -1;
default: /* parent - good */
pid = wait(&status);
if (pid < 0 || status != 0) {
- pr_err("failed to launch mdmon. "
- "Array remains readonly\n");
+ pr_err("failed to launch mdmon. Array remains readonly\n");
return -1;
}
}
if (check_env("MDADM_EXPERIMENTAL"))
return 1;
else {
- pr_err("To use this feature MDADM_EXPERIMENTAL"
- " environment variable has to be defined.\n");
+ pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n");
return 0;
}
}
{
/* This is based on similar function in systemd. */
struct statfs s;
+ /* statfs.f_type is signed long on s390x and MIPS, causing all
+ sorts of sign extension problems with RAMFS_MAGIC being
+ defined as 0x858458f6 */
return statfs("/", &s) >= 0 &&
- (s.f_type == TMPFS_MAGIC ||
- s.f_type == RAMFS_MAGIC);
+ ((unsigned long)s.f_type == TMPFS_MAGIC ||
+ ((unsigned long)s.f_type & 0xFFFFFFFFUL) ==
+ ((unsigned long)RAMFS_MAGIC & 0xFFFFFFFFUL));
+}
+
+void reopen_mddev(int mdfd)
+{
+ /* Re-open without any O_EXCL, but keep
+ * the same fd
+ */
+ char *devnm;
+ int fd;
+ devnm = fd2devnm(mdfd);
+ close(mdfd);
+ fd = open_dev(devnm);
+ if (fd >= 0 && fd != mdfd)
+ dup2(fd, mdfd);
+}
+
+int get_cluster_name(char **cluster_name)
+{
+ void *lib_handle = NULL;
+ int rv = -1;
+
+ cmap_handle_t handle;
+ static int (*initialize)(cmap_handle_t *handle);
+ static int (*get_string)(cmap_handle_t handle,
+ const char *string,
+ char **name);
+ static int (*finalize)(cmap_handle_t handle);
+
+
+ lib_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
+ if (!lib_handle)
+ return rv;
+
+ initialize = dlsym(lib_handle, "cmap_initialize");
+ if (!initialize)
+ goto out;
+
+ get_string = dlsym(lib_handle, "cmap_get_string");
+ if (!get_string)
+ goto out;
+
+ finalize = dlsym(lib_handle, "cmap_finalize");
+ if (!finalize)
+ goto out;
+
+ rv = initialize(&handle);
+ if (rv != CS_OK)
+ goto out;
+
+ rv = get_string(handle, "totem.cluster_name", cluster_name);
+ if (rv != CS_OK) {
+ free(*cluster_name);
+ rv = -1;
+ goto name_err;
+ }
+
+ rv = 0;
+name_err:
+ finalize(handle);
+out:
+ dlclose(lib_handle);
+ return rv;
}