int major_num = MD_MAJOR;
int minor_num = 0;
int must_remove = 0;
- struct mdstat_ent *mdlist;
int num;
struct createinfo *ci = conf_get_create_info();
int parts;
"not compatable with device named %s\n", dev);
return -1;
}
+ major_num = get_mdp_major();
break;
case 5: /* default to md if not standard */
break;
must_remove = 1;
if (stb.st_mode && !must_remove) {
/* looks ok, see if it is available */
- mdfd = open(dev, O_RDWR, 0);
+ mdfd = open(dev, O_RDWR);
if (mdfd < 0) {
fprintf(stderr, Name ": error opening %s: %s\n",
dev, strerror(errno));
* If the device name is in a 'standard' format,
* intuit the minor from that, else
* easiest to read /proc/mdstat, and hunt through for
- * an unused number
+ * an unused number
*/
if (num < 0) {
/* need to pick an unused number */
- mdlist = mdstat_read(0, 0);
- /* 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_num != MD_MAJOR)
- devnum = -1-num;
+ int num = find_free_devnum(major_num != MD_MAJOR);
- for (me=mdlist; me; me=me->next)
- if (me->devnum == devnum)
- break;
- if (!me) {
- /* doesn't exist in mdstat.
- * make sure it is new to /dev too
- */
- char *dn;
- if (major_num != MD_MAJOR)
- minor_num = num << MdpMinorShift;
- else
- minor_num = num;
- dn = map_dev(major_num,minor_num, 0);
- if (dn==NULL || is_standard(dn, NULL)) {
- /* this number only used by a 'standard' name,
- * so it is safe to use
- */
- break;
- }
- }
- }
+ if (major_num == MD_MAJOR)
+ minor_num = num;
+ else
+ minor_num = (-1-num) << MdpMinorShift;
} else if (major_num == MD_MAJOR)
minor_num = num;
else
make_parts(dev,parts, ci->symlinks);
}
}
- mdfd = open(dev, O_RDWR, 0);
+ mdfd = open(dev, O_RDWR);
if (mdfd < 0)
fprintf(stderr, Name ": error opening %s: %s\n",
dev, strerror(errno));
}
-int open_mddev_devnum(char *devname, int devnum, char *name, char *chosen_name)
+int open_mddev_devnum(char *devname, int devnum, char *name,
+ char *chosen_name, int parts)
{
/* Open the md device with number 'devnum', possibly using 'devname',
* possibly constructing a name with 'name', but in any case, copying
*/
int major_num, minor_num;
struct stat stb;
+ int i;
+ struct createinfo *ci = conf_get_create_info();
if (devname)
strcpy(chosen_name, devname);
- else if (name && strchr(name,'/') == NULL) {
+ else if (name && *name && name[0] && strchr(name,'/') == NULL) {
char *n = strchr(name, ':');
if (n) n++; else n = name;
if (isdigit(*n) && devnum < 0)
return -1;
}
} else {
+ /* special case: if --incremental is suggesting a name
+ * in /dev/md/, we make sure the directory exists.
+ */
+ if (strncmp(chosen_name, "/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(chosen_name, S_IFBLK | 0600,
makedev(major_num, minor_num)) != 0) {
return -1;
}
/* FIXME chown/chmod ?? */
}
- return open(chosen_name, O_RDWR);
+
+ /* Simple locking to avoid --incr being called for the same
+ * array multiple times in parallel.
+ */
+ for (i = 0; i < 25 ; i++) {
+ int fd;
+
+ fd = open(chosen_name, O_RDWR|O_EXCL);
+ if (fd >= 0 || errno != EBUSY) {
+ if (devnum < 0)
+ make_parts(chosen_name, parts, ci->symlinks);
+ return fd;
+ }
+ usleep(200000);
+ }
+ return -1;
}