/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
- * Copyright (C) 2001-2002 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
#include "md_p.h"
#include <ctype.h>
-void make_parts(char *dev, int cnt)
+
+void make_dev_symlink(char *dev)
+{
+ char *new = strdup(dev);
+
+ if (!new) return;
+ /* /dev/md/0 -> /dev/md0
+ * /dev/md/d0 -> /dev/md_d0
+ */
+ if (isdigit(new[8]))
+ strcpy(new+7, new+8);
+ else
+ new[7] = '_';
+ if (symlink(dev+5, new))
+ perror(new);
+}
+
+
+void make_parts(char *dev, int cnt, int symlinks)
{
/* make 'cnt' partition devices for 'dev'
* We use the major/minor from dev and add 1..cnt
perror("chown");
if (chmod(name, stb2.st_mode & 07777))
perror("chmod");
+ if (symlinks && strncmp(name, "/dev/md/", 8) == 0)
+ make_dev_symlink(name);
stat(name, &stb2);
add_dev(name, &stb2, 0, NULL);
}
}
+
/*
* Open a given md device, and check that it really is one.
* If 'autof' is given, then we need to create, or recreate, the md device.
int must_remove = 0;
struct mdstat_ent *mdlist;
int num;
- struct createinfo *ci = conf_get_create_info(NULL);
+ struct createinfo *ci = conf_get_create_info();
int parts;
if (autof == 0)
dev);
return -1;
}
- if (autof == 2 && stb.st_mode == 0 && !is_standard(dev, NULL)) {
- fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
- return -1;
- }
/* check major number is correct */
num = -1;
std = is_standard(dev, &num);
switch(autof) {
case 2: /* only create is_standard names */
if (!std && !stb.st_mode) {
- fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
+ fprintf(stderr, Name
+ ": %s does not exist and is not a 'standard' name "
+ "so it cannot be created\n", dev);
return -1;
}
break;
case 3: /* create md, reject std>0 */
if (std > 0) {
- fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+ fprintf(stderr, Name ": that --auto option "
+ "not compatable with device named %s\n", dev);
return -1;
}
break;
case 4: /* create mdp, reject std<0 */
if (std < 0) {
- fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+ fprintf(stderr, Name ": that --auto option "
+ "not compatable with device named %s\n", dev);
return -1;
}
break;
if (stb.st_mode && major(stb.st_rdev) != major)
must_remove = 1;
if (stb.st_mode && !must_remove) {
- mdu_array_info_t array;
/* looks ok, see if it is available */
mdfd = open(dev, O_RDWR, 0);
if (mdfd < 0) {
close(mdfd);
return -1;
}
- if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
- /* already active */
- must_remove = 1;
- close(mdfd);
- } else {
- if (major != MD_MAJOR && parts > 0)
- make_parts(dev, parts);
- return mdfd;
- }
+ if (major != MD_MAJOR && parts > 0)
+ make_parts(dev, parts, ci->symlinks);
+ return mdfd;
}
/* Ok, need to find a minor that is not in use.
* If the device name is in a 'standard' format,
if (num < 0) {
/* need to pick an unused number */
mdlist = mdstat_read(0, 0);
- for (num = 0 ; ; num++) {
+ /* Choose a large number. Start from 127 and search down,
+ * but if nothing is found, start really big
+ */
+ for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) {
struct mdstat_ent *me;
int devnum = num;
if (major != MD_MAJOR)
if (me->devnum == devnum)
break;
if (!me) {
- /* doesn't exist if mdstat.
+ /* doesn't exist in mdstat.
* make sure it is new to /dev too
*/
char *dn;
if (must_remove)
unlink(dev);
+ if (strncmp(dev, "/dev/md/", 8) == 0) {
+ if (mkdir("/dev/md",0700)==0) {
+ if (chown("/dev/md", ci->uid, ci->gid))
+ perror("chown /dev/md");
+ if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)))
+ perror("chmod /dev/md");
+ }
+ }
if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
fprintf(stderr, Name ": failed to create %s\n", dev);
return -1;
}
stat(dev, &stb);
add_dev(dev, &stb, 0, NULL);
+ if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0)
+ make_dev_symlink(dev);
if (major != MD_MAJOR)
- make_parts(dev,parts);
+ make_parts(dev,parts, ci->symlinks);
}
}
mdfd = open(dev, O_RDWR, 0);
return mdfd;
}
+
+int open_mddev_devnum(char *devname, int devnum, char *name, char *chosen_name)
+{
+ /* Open the md device with number 'devnum', possibly using 'devname',
+ * possibly constructing a name with 'name', but in any case, copying
+ * the name into 'chosen_name'
+ */
+ int major, minor;
+ struct stat stb;
+
+ if (devname)
+ strcpy(chosen_name, devname);
+ else if (name && strchr(name,'/') == NULL) {
+ char *n = strchr(name, ':');
+ if (n) n++; else n = name;
+ if (isdigit(*n) && devnum < 0)
+ sprintf(chosen_name, "/dev/md/d%s", n);
+ else
+ sprintf(chosen_name, "/dev/md/%s", n);
+ } else {
+ if (devnum >= 0)
+ sprintf(chosen_name, "/dev/md%d", devnum);
+ else
+ sprintf(chosen_name, "/dev/md/d%d", -1-devnum);
+ }
+ if (devnum >= 0) {
+ major = MD_MAJOR;
+ minor = devnum;
+ } else {
+ major = get_mdp_major();
+ minor = (-1-devnum) << 6;
+ }
+ if (stat(chosen_name, &stb) == 0) {
+ /* It already exists. Check it is right. */
+ if ( ! S_ISBLK(stb.st_mode) ||
+ stb.st_rdev != makedev(major, minor)) {
+ errno = EEXIST;
+ return -1;
+ }
+ } else {
+ if (mknod(chosen_name, S_IFBLK | 0600,
+ makedev(major, minor)) != 0) {
+ return -1;
+ }
+ /* FIXME chown/chmod ?? */
+ }
+ return open(chosen_name, O_RDWR);
+}