+
+
+int is_standard(char *dev, int *nump)
+{
+ /* tests if dev is a "standard" md dev name.
+ * i.e if the last component is "/dNN" or "/mdNN",
+ * where NN is a string of digits
+ */
+ char *d = strrchr(dev, '/');
+ int type=0;
+ int num;
+ if (!d)
+ return 0;
+ if (strncmp(d, "/d",2)==0)
+ d += 2, type=1; /* /dev/md/dN{pM} */
+ else if (strncmp(d, "/md_d", 5)==0)
+ d += 5, type=1; /* /dev/md_dNpM */
+ else if (strncmp(d, "/md", 3)==0)
+ d += 3, type=-1; /* /dev/mdN */
+ else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
+ d += 1, type=-1; /* /dev/md/N */
+ else
+ return 0;
+ if (!*d)
+ return 0;
+ num = atoi(d);
+ while (isdigit(*d))
+ d++;
+ if (*d)
+ return 0;
+ if (nump) *nump = num;
+
+ return type;
+}
+
+
+/*
+ * convert a major/minor pair for a block device into a name in /dev, if possible.
+ * On the first call, walk /dev collecting name.
+ * Put them in a simple linked listfor now.
+ */
+struct devmap {
+ int major, minor;
+ char *name;
+ struct devmap *next;
+} *devlist = NULL;
+int devlist_ready = 0;
+
+int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
+{
+ struct stat st;
+ if (S_ISLNK(stb->st_mode)) {
+ stat(name, &st);
+ stb = &st;
+ }
+
+ if ((stb->st_mode&S_IFMT)== S_IFBLK) {
+ char *n = strdup(name);
+ struct devmap *dm = malloc(sizeof(*dm));
+ if (strncmp(n, "/dev/./", 7)==0)
+ strcpy(n+4, name+6);
+ if (dm) {
+ dm->major = major(stb->st_rdev);
+ dm->minor = minor(stb->st_rdev);
+ dm->name = n;
+ dm->next = devlist;
+ devlist = dm;
+ }
+ }
+ return 0;
+}
+
+#ifndef HAVE_NFTW
+#ifdef HAVE_FTW
+int add_dev_1(const char *name, const struct stat *stb, int flag)
+{
+ return add_dev(name, stb, flag, NULL);
+}
+int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
+{
+ return ftw(path, add_dev_1, nopenfd);
+}
+#else
+int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
+{
+ return 0;
+}
+#endif /* HAVE_FTW */
+#endif /* HAVE_NFTW */
+
+/*
+ * Find a block device with the right major/minor number.
+ * If we find multiple names, choose the shortest.
+ * If we find a non-standard name, it is probably there
+ * deliberately so prefer it over a standard name.
+ * This applies only to names for MD devices.
+ */
+char *map_dev(int major, int minor, int create)
+{
+ struct devmap *p;
+ char *std = NULL, *nonstd=NULL;
+ int did_check = 0;
+
+ if (major == 0 && minor == 0)
+ return NULL;
+
+ retry:
+ if (!devlist_ready) {
+ char *dev = "/dev";
+ struct stat stb;
+ while(devlist) {
+ struct devmap *d = devlist;
+ devlist = d->next;
+ free(d->name);
+ free(d);
+ }
+ if (lstat(dev, &stb)==0 &&
+ S_ISLNK(stb.st_mode))
+ dev = "/dev/.";
+ nftw(dev, add_dev, 10, FTW_PHYS);
+ devlist_ready=1;
+ did_check = 1;
+ }
+
+ for (p=devlist; p; p=p->next)
+ if (p->major == major &&
+ p->minor == minor) {
+ if (is_standard(p->name, NULL)) {
+ if (std == NULL ||
+ strlen(p->name) < strlen(std))
+ std = p->name;
+ } else {
+ if (nonstd == NULL ||
+ strlen(p->name) < strlen(nonstd))
+ nonstd = p->name;
+ }
+ }
+ if (!std && !nonstd && !did_check) {
+ devlist_ready = 0;
+ goto retry;
+ }
+ if (create && !std && !nonstd) {
+ static char buf[30];
+ snprintf(buf, sizeof(buf), "%d:%d", major, minor);
+ nonstd = buf;
+ }
+
+ return nonstd ? nonstd : std;
+}
+
+unsigned long calc_csum(void *super, int bytes)
+{
+ unsigned long long newcsum = 0;
+ int i;
+ unsigned int csum;
+ unsigned int *superc = (unsigned int*) super;
+
+ for(i=0; i<bytes/4; i++)
+ newcsum+= superc[i];
+ csum = (newcsum& 0xffffffff) + (newcsum>>32);
+#ifdef __alpha__
+/* The in-kernel checksum calculation is always 16bit on
+ * the alpha, though it is 32 bit on i386...
+ * I wonder what it is elsewhere... (it uses and API in
+ * a way that it shouldn't).
+ */
+ csum = (csum & 0xffff) + (csum >> 16);
+ csum = (csum & 0xffff) + (csum >> 16);
+#endif
+ return csum;
+}
+
+#ifndef MDASSEMBLE
+char *human_size(long long bytes)
+{
+ static char buf[30];
+
+ /* We convert bytes to either centi-M{ega,ibi}bytes or
+ * centi-G{igi,ibi}bytes, with appropriate rounding,
+ * and then print 1/100th of those as a decimal.
+ * We allow upto 2048Megabytes before converting to
+ * gigabytes, as that shows more precision and isn't
+ * too large a number.
+ * Terrabytes are not yet handled.
+ */
+
+ if (bytes < 5000*1024)
+ buf[0]=0;
+ else if (bytes < 2*1024LL*1024LL*1024LL) {
+ long cMiB = (bytes / ( (1LL<<20) / 200LL ) +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 cGB = (bytes / (1000000000LL/200LL ) +1) /2;
+ snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
+ cGiB/100 , cGiB % 100,
+ cGB/100, cGB % 100);
+ }
+ return buf;
+}
+
+char *human_size_brief(long long bytes)
+{
+ static char buf[30];
+
+ if (bytes < 5000*1024)
+ snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
+ (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
+ );
+ else if (bytes < 2*1024LL*1024LL*1024LL)
+ snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
+ (long)(bytes>>20),
+ (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
+ );
+ else
+ snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
+ (long)(bytes>>30),
+ (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
+ );
+ return buf;
+}
+#endif
+
+unsigned long long calc_array_size(int level, int raid_disks, int layout,
+ int chunksize, unsigned long long devsize)
+{
+ int data_disks = 0;
+ switch (level) {
+ case 0: data_disks = raid_disks; break;
+ case 1: data_disks = 1; break;
+ case 4:
+ case 5: data_disks = raid_disks - 1; break;
+ case 6: data_disks = raid_disks - 2; break;
+ case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
+ break;
+ }
+ devsize &= ~(unsigned long long)((chunksize>>9)-1);
+ return data_disks * devsize;
+}
+
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
+int get_mdp_major(void)
+{
+static int mdp_major = -1;
+ FILE *fl;
+ char *w;
+ int have_block = 0;
+ int have_devices = 0;
+ int last_num = -1;
+
+ if (mdp_major != -1)
+ return mdp_major;
+ fl = fopen("/proc/devices", "r");
+ if (!fl)
+ return -1;
+ while ((w = conf_word(fl, 1))) {
+ if (have_block && strcmp(w, "devices:")==0)
+ have_devices = 1;
+ have_block = (strcmp(w, "Block")==0);
+ if (isdigit(w[0]))
+ last_num = atoi(w);
+ if (have_devices && strcmp(w, "mdp")==0)
+ mdp_major = last_num;
+ free(w);
+ }
+ fclose(fl);
+ return mdp_major;
+}
+
+
+
+char *get_md_name(int dev)
+{
+ /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
+ /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
+ static char devname[50];
+ struct stat stb;
+ dev_t rdev;
+ char *dn;
+
+ if (dev < 0) {
+ int mdp = get_mdp_major();
+ if (mdp < 0) return NULL;
+ rdev = makedev(mdp, (-1-dev)<<6);
+ snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
+ if (stat(devname, &stb) == 0
+ && (S_IFMT&stb.st_mode) == S_IFBLK
+ && (stb.st_rdev == rdev))
+ return devname;
+ } else {
+ rdev = makedev(MD_MAJOR, dev);
+ snprintf(devname, sizeof(devname), "/dev/md%d", dev);
+ if (stat(devname, &stb) == 0
+ && (S_IFMT&stb.st_mode) == S_IFBLK
+ && (stb.st_rdev == rdev))
+ return devname;
+
+ snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
+ if (stat(devname, &stb) == 0
+ && (S_IFMT&stb.st_mode) == S_IFBLK
+ && (stb.st_rdev == rdev))
+ return devname;
+ }
+ dn = map_dev(major(rdev), minor(rdev), 0);
+ if (dn)
+ return dn;
+ snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
+ if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
+ if (errno != EEXIST)
+ return NULL;
+
+ if (stat(devname, &stb) == 0
+ && (S_IFMT&stb.st_mode) == S_IFBLK
+ && (stb.st_rdev == rdev))
+ return devname;
+ unlink(devname);
+ return NULL;
+}
+
+void put_md_name(char *name)
+{
+ if (strncmp(name, "/dev/.tmp.md", 12)==0)
+ unlink(name);
+}
+
+static int dev2major(int d)
+{
+ if (d >= 0)
+ return MD_MAJOR;
+ else
+ return get_mdp_major();
+}
+
+static int dev2minor(int d)
+{
+ if (d >= 0)
+ return d;
+ return (-1-d) << MdpMinorShift;
+}
+
+int find_free_devnum(int use_partitions)
+{
+ int devnum;
+ for (devnum = 127; devnum != 128;
+ devnum = devnum ? devnum-1 : (1<<22)-1) {
+ char *dn;
+ int _devnum;
+
+ _devnum = use_partitions ? (-1-devnum) : devnum;
+ if (mddev_busy(_devnum))
+ continue;
+ /* make sure it is new to /dev too, at least as a
+ * non-standard */
+ dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
+ if (dn && ! is_standard(dn, NULL))
+ continue;
+ break;
+ }
+ if (devnum == 128)
+ return NoMdDev;
+ return use_partitions ? (-1-devnum) : devnum;
+}
+#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
+
+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;
+ int minor;
+
+ if (!dev) return -1;
+
+ major = strtoul(dev, &e, 0);
+ if (e > dev && *e == ':' && e[1] &&
+ (minor = strtoul(e+1, &e, 0)) >= 0 &&
+ *e == 0) {
+ snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor);
+ if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
+ fd = open(devname, flags);
+ unlink(devname);
+ }
+ } else
+ fd = open(dev, flags);
+ return fd;
+}
+
+struct superswitch *superlist[] = { &super0, &super1, &super_ddf, NULL };
+
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
+struct supertype *super_by_fd(int fd)
+{
+ mdu_array_info_t array;
+ int vers;
+ int minor;
+ struct supertype *st = NULL;
+ struct mdinfo *sra;
+ char *verstr;
+ char version[20];
+ int i;
+
+ sra = sysfs_read(fd, 0, GET_VERSION);
+
+ if (sra) {
+ vers = sra->array.major_version;
+ minor = sra->array.minor_version;
+ verstr = sra->text_version;
+ } else {
+ if (ioctl(fd, GET_ARRAY_INFO, &array))
+ array.major_version = array.minor_version = 0;
+ vers = array.major_version;
+ minor = array.minor_version;
+ verstr = "";
+ }
+
+ if (vers != -1) {
+ sprintf(version, "%d.%d", vers, minor);
+ verstr = version;
+ }
+ for (i = 0; st == NULL && superlist[i] ; i++)
+ st = superlist[i]->match_metadata_desc(verstr);
+
+ if (sra)
+ sysfs_free(sra);
+ if (st)
+ st->sb = NULL;
+ return st;
+}
+#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
+
+
+struct supertype *dup_super(struct supertype *st)
+{
+ struct supertype *stnew = NULL;
+ char *verstr = NULL;
+ char version[20];
+ int i;
+
+ if (!st)
+ return st;
+
+ if (st->ss->text_version)
+ strcpy(version, st->ss->text_version);
+ else if (st->minor_version == -1)
+ sprintf(version, "%d", st->ss->major);
+ else
+ sprintf(version, "%d.%d", st->ss->major, st->minor_version);
+ verstr = version;
+
+ for (i = 0; stnew == NULL && superlist[i] ; i++)
+ stnew = superlist[i]->match_metadata_desc(verstr);
+
+ if (stnew)
+ stnew->sb = NULL;
+ return stnew;
+}
+
+struct supertype *guess_super(int fd)
+{
+ /* try each load_super to find the best match,
+ * and return the best superswitch
+ */
+ struct superswitch *ss;
+ struct supertype *st;
+ unsigned long besttime = 0;
+ int bestsuper = -1;
+ int i;
+
+ st = malloc(sizeof(*st));
+ memset(st, 0, sizeof(*st));
+ for (i=0 ; superlist[i]; i++) {
+ int rv;
+ ss = superlist[i];
+ st->ss = NULL;
+ rv = ss->load_super(st, fd, NULL);
+ if (rv == 0) {
+ struct mdinfo info;
+ st->ss->getinfo_super(st, &info);
+ if (bestsuper == -1 ||
+ besttime < info.array.ctime) {
+ bestsuper = i;
+ besttime = info.array.ctime;
+ }
+ ss->free_super(st);
+ }
+ }
+ if (bestsuper != -1) {
+ int rv;
+ st->ss = NULL;
+ rv = superlist[bestsuper]->load_super(st, fd, NULL);
+ if (rv == 0) {
+ superlist[bestsuper]->free_super(st);
+ return st;
+ }
+ }
+ free(st);
+ return NULL;
+}
+
+/* Return size of device in bytes */
+int get_dev_size(int fd, char *dname, unsigned long long *sizep)
+{
+ unsigned long long ldsize;
+ struct stat st;
+
+ if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
+ ldsize = (unsigned long long)st.st_size;
+ else
+#ifdef BLKGETSIZE64
+ if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
+#endif
+ {
+ unsigned long dsize;
+ if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
+ ldsize = dsize;
+ ldsize <<= 9;
+ } else {
+ if (dname)
+ fprintf(stderr, Name ": Cannot get size of %s: %s\b",
+ dname, strerror(errno));
+ return 0;
+ }
+ }
+ *sizep = ldsize;
+ return 1;
+}
+
+void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
+{
+ int d;
+ ioctl(mdfd, GET_ARRAY_INFO, ainf);
+ for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
+ if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
+ return;
+}
+
+int open_container(int fd)
+{
+ /* 'fd' is a block device. Find out if it is in use
+ * by a container, and return an open fd on that container.
+ */
+ char path[256];
+ char *e;
+ DIR *dir;
+ struct dirent *de;
+ int dfd, n;
+ char buf[200];
+ int major, minor;
+ struct stat st;
+
+ if (fstat(fd, &st) != 0)
+ return -1;
+ sprintf(path, "/sys/dev/block/%d:%d/holders",
+ (int)major(st.st_rdev), (int)minor(st.st_rdev));
+ e = path + strlen(path);
+
+ dir = opendir(path);
+ if (!dir)
+ return -1;
+ while ((de = readdir(dir))) {
+ if (de->d_ino == 0)
+ continue;
+ if (de->d_name[0] == '.')
+ continue;
+ sprintf(e, "/%s/dev", de->d_name);
+ dfd = open(path, O_RDONLY);
+ if (dfd < 0)
+ continue;
+ n = read(dfd, buf, sizeof(buf));
+ close(dfd);
+ if (n <= 0 || n >= sizeof(buf))
+ continue;
+ buf[n] = 0;
+ if (sscanf(buf, "%d:%d", &major, &minor) != 2)
+ continue;
+ sprintf(buf, "%d:%d", major, minor);
+ dfd = dev_open(buf, O_RDONLY);
+ if (dfd >= 0) {
+ closedir(dir);
+ return dfd;
+ }
+ }
+ return -1;
+}
+
+#ifdef __TINYC__
+/* tinyc doesn't optimize this check in ioctl.h out ... */
+unsigned int __invalid_size_argument_for_IOC = 0;
+#endif
+