2004-11-01 05:49:34 +01:00
|
|
|
/*
|
|
|
|
* mdadm - manage Linux "md" devices aka RAID arrays.
|
|
|
|
*
|
2006-05-19 07:25:11 +02:00
|
|
|
* Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
|
2004-11-01 05:49:34 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Neil Brown
|
|
|
|
* Email: <neilb@cse.unsw.edu.au>
|
|
|
|
* Paper: Neil Brown
|
|
|
|
* School of Computer Science and Engineering
|
|
|
|
* The University of New South Wales
|
|
|
|
* Sydney, 2052
|
|
|
|
* Australia
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mdadm.h"
|
|
|
|
#include "md_p.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
void make_parts(char *dev, int cnt)
|
|
|
|
{
|
|
|
|
/* make 'cnt' partition devices for 'dev'
|
|
|
|
* We use the major/minor from dev and add 1..cnt
|
2005-04-04 07:16:10 +02:00
|
|
|
* If dev ends with a digit, we add "p%d" else "%d"
|
2004-11-01 05:49:34 +01:00
|
|
|
* If the name exists, we use it's owner/mode,
|
|
|
|
* else that of dev
|
|
|
|
*/
|
|
|
|
struct stat stb;
|
|
|
|
int major, minor;
|
|
|
|
int i;
|
2005-06-14 08:42:13 +02:00
|
|
|
int nlen = strlen(dev) + 20;
|
|
|
|
char *name = malloc(nlen);
|
2004-11-01 05:49:34 +01:00
|
|
|
int dig = isdigit(dev[strlen(dev)-1]);
|
|
|
|
|
2006-05-16 09:35:06 +02:00
|
|
|
if (cnt==0) cnt=4;
|
2004-11-01 05:49:34 +01:00
|
|
|
if (stat(dev, &stb)!= 0)
|
|
|
|
return;
|
|
|
|
if (!S_ISBLK(stb.st_mode))
|
|
|
|
return;
|
2005-04-04 07:16:13 +02:00
|
|
|
major = major(stb.st_rdev);
|
|
|
|
minor = minor(stb.st_rdev);
|
2004-11-01 05:49:34 +01:00
|
|
|
for (i=1; i <= cnt ; i++) {
|
|
|
|
struct stat stb2;
|
2005-06-14 08:42:13 +02:00
|
|
|
snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
|
2004-11-01 05:49:34 +01:00
|
|
|
if (stat(name, &stb2)==0) {
|
|
|
|
if (!S_ISBLK(stb2.st_mode))
|
|
|
|
continue;
|
2005-04-04 07:16:13 +02:00
|
|
|
if (stb2.st_rdev == makedev(major, minor+i))
|
2004-11-01 05:49:34 +01:00
|
|
|
continue;
|
|
|
|
unlink(name);
|
|
|
|
} else {
|
|
|
|
stb2 = stb;
|
|
|
|
}
|
2006-02-06 05:18:12 +01:00
|
|
|
if (mknod(name, S_IFBLK | 0600, makedev(major, minor+i)))
|
|
|
|
perror("mknod");
|
|
|
|
if (chown(name, stb2.st_uid, stb2.st_gid))
|
|
|
|
perror("chown");
|
|
|
|
if (chmod(name, stb2.st_mode & 07777))
|
|
|
|
perror("chmod");
|
2005-06-14 08:37:57 +02:00
|
|
|
stat(name, &stb2);
|
|
|
|
add_dev(name, &stb2, 0, NULL);
|
2004-11-01 05:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
* If the name already exists, and is not a block device, we fail.
|
|
|
|
* If it exists and is not an md device, is not the right type (partitioned or not),
|
|
|
|
* or is currently in-use, we remove the device, but remember the owner and mode.
|
2005-04-04 07:16:10 +02:00
|
|
|
* If it now doesn't exist, we find a new md array and create the device.
|
2006-05-15 08:02:41 +02:00
|
|
|
* Default ownership/mode comes from config file.
|
2004-11-01 05:49:34 +01:00
|
|
|
*/
|
|
|
|
int open_mddev(char *dev, int autof)
|
|
|
|
{
|
|
|
|
int mdfd;
|
|
|
|
struct stat stb;
|
|
|
|
int major = MD_MAJOR;
|
2005-04-04 07:16:31 +02:00
|
|
|
int minor = 0;
|
2004-11-01 05:49:34 +01:00
|
|
|
int must_remove = 0;
|
|
|
|
struct mdstat_ent *mdlist;
|
|
|
|
int num;
|
2006-05-15 08:02:41 +02:00
|
|
|
struct createinfo *ci = conf_get_create_info(NULL);
|
2006-05-16 09:35:06 +02:00
|
|
|
int parts;
|
2004-11-01 05:49:34 +01:00
|
|
|
|
2006-05-15 08:02:41 +02:00
|
|
|
if (autof == 0)
|
|
|
|
autof = ci->autof;
|
|
|
|
|
2006-05-16 09:35:06 +02:00
|
|
|
parts = autof >> 3;
|
|
|
|
autof &= 7;
|
|
|
|
|
|
|
|
if (autof && autof != 1) {
|
2004-11-01 05:49:34 +01:00
|
|
|
/* autof is set, so we need to check that the name is ok,
|
|
|
|
* and possibly create one if not
|
|
|
|
*/
|
2006-05-16 09:35:06 +02:00
|
|
|
int std;
|
2004-11-01 05:49:34 +01:00
|
|
|
stb.st_mode = 0;
|
2005-06-14 08:30:03 +02:00
|
|
|
if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
|
2004-11-01 05:49:34 +01:00
|
|
|
fprintf(stderr, Name ": %s is not a block device.\n",
|
|
|
|
dev);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-05-16 09:35:06 +02:00
|
|
|
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;
|
|
|
|
}
|
2004-11-01 05:49:34 +01:00
|
|
|
/* check major number is correct */
|
2006-05-16 09:35:06 +02:00
|
|
|
num = -1;
|
|
|
|
std = is_standard(dev, &num);
|
|
|
|
if (std>0) major = get_mdp_major();
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5: /* default to md if not standard */
|
|
|
|
break;
|
|
|
|
case 6: /* default to mdp if not standard */
|
|
|
|
if (std == 0) major = get_mdp_major();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* major is final. num is -1 if not standard */
|
2005-04-04 07:16:13 +02:00
|
|
|
if (stb.st_mode && major(stb.st_rdev) != major)
|
2004-11-01 05:49:34 +01:00
|
|
|
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) {
|
|
|
|
fprintf(stderr, Name ": error opening %s: %s\n",
|
|
|
|
dev, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else if (md_get_version(mdfd) <= 0) {
|
|
|
|
fprintf(stderr, Name ": %s does not appear to be an md device\n",
|
|
|
|
dev);
|
|
|
|
close(mdfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
|
|
|
|
/* already active */
|
|
|
|
must_remove = 1;
|
|
|
|
close(mdfd);
|
|
|
|
} else {
|
2006-05-16 09:35:06 +02:00
|
|
|
if (major != MD_MAJOR && parts > 0)
|
|
|
|
make_parts(dev, parts);
|
2004-11-01 05:49:34 +01:00
|
|
|
return mdfd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Ok, need to find a minor that is not in use.
|
2005-04-04 07:16:10 +02:00
|
|
|
* If the device name is in a 'standard' format,
|
|
|
|
* intuit the minor from that, else
|
|
|
|
* easiest to read /proc/mdstat, and hunt through for
|
2004-11-01 05:49:34 +01:00
|
|
|
* an unused number
|
|
|
|
*/
|
2006-05-16 09:35:06 +02:00
|
|
|
if (num < 0) {
|
|
|
|
/* need to pick an unused number */
|
2006-01-31 01:39:50 +01:00
|
|
|
mdlist = mdstat_read(0, 0);
|
2006-05-26 03:40:53 +02:00
|
|
|
/* 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) {
|
2005-04-04 07:16:10 +02:00
|
|
|
struct mdstat_ent *me;
|
2006-05-16 09:35:06 +02:00
|
|
|
int devnum = num;
|
|
|
|
if (major != MD_MAJOR)
|
|
|
|
devnum = -1-num;
|
|
|
|
|
2005-04-04 07:16:10 +02:00
|
|
|
for (me=mdlist; me; me=me->next)
|
2006-05-16 09:35:06 +02:00
|
|
|
if (me->devnum == devnum)
|
2005-04-04 07:16:10 +02:00
|
|
|
break;
|
|
|
|
if (!me) {
|
2006-05-26 03:40:53 +02:00
|
|
|
/* doesn't exist in mdstat.
|
2005-04-04 07:16:10 +02:00
|
|
|
* make sure it is new to /dev too
|
2004-11-01 05:49:34 +01:00
|
|
|
*/
|
2005-04-04 07:16:10 +02:00
|
|
|
char *dn;
|
2006-05-16 09:35:06 +02:00
|
|
|
if (major != MD_MAJOR)
|
|
|
|
minor = num << MdpMinorShift;
|
2005-04-04 07:16:10 +02:00
|
|
|
else
|
|
|
|
minor = num;
|
2006-03-28 08:26:53 +02:00
|
|
|
dn = map_dev(major,minor, 0);
|
2005-04-04 07:16:10 +02:00
|
|
|
if (dn==NULL || is_standard(dn, NULL)) {
|
|
|
|
/* this number only used by a 'standard' name,
|
|
|
|
* so it is safe to use
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
2004-11-01 05:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
2006-05-16 09:35:06 +02:00
|
|
|
} else if (major == MD_MAJOR)
|
|
|
|
minor = num;
|
|
|
|
else
|
|
|
|
minor = num << MdpMinorShift;
|
2005-04-04 07:16:10 +02:00
|
|
|
/* major and minor have been chosen */
|
2006-05-16 09:35:06 +02:00
|
|
|
|
2005-04-04 07:16:10 +02:00
|
|
|
/* If it was a 'standard' name and it is in-use, then
|
|
|
|
* the device could already be correct
|
|
|
|
*/
|
2005-04-04 07:16:13 +02:00
|
|
|
if (stb.st_mode && major(stb.st_rdev) == major &&
|
|
|
|
minor(stb.st_rdev) == minor)
|
2005-04-04 07:16:10 +02:00
|
|
|
;
|
|
|
|
else {
|
2005-04-04 07:16:13 +02:00
|
|
|
if (major(makedev(major,minor)) != major ||
|
|
|
|
minor(makedev(major,minor)) != minor) {
|
|
|
|
fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-04-04 07:16:10 +02:00
|
|
|
if (must_remove)
|
|
|
|
unlink(dev);
|
|
|
|
|
2005-04-04 07:16:13 +02:00
|
|
|
if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
|
2005-04-04 07:16:10 +02:00
|
|
|
fprintf(stderr, Name ": failed to create %s\n", dev);
|
2004-11-01 05:49:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2005-04-04 07:16:10 +02:00
|
|
|
if (must_remove) {
|
2006-02-06 05:18:12 +01:00
|
|
|
if (chown(dev, stb.st_uid, stb.st_gid))
|
|
|
|
perror("chown");
|
|
|
|
if (chmod(dev, stb.st_mode & 07777))
|
|
|
|
perror("chmod");
|
2006-05-15 08:02:41 +02:00
|
|
|
} else {
|
|
|
|
if (chown(dev, ci->uid, ci->gid))
|
|
|
|
perror("chown");
|
|
|
|
if (chmod(dev, ci->mode))
|
|
|
|
perror("chmod");
|
2005-04-04 07:16:10 +02:00
|
|
|
}
|
2005-06-14 08:37:57 +02:00
|
|
|
stat(dev, &stb);
|
|
|
|
add_dev(dev, &stb, 0, NULL);
|
2006-05-16 09:35:06 +02:00
|
|
|
if (major != MD_MAJOR)
|
|
|
|
make_parts(dev,parts);
|
2004-11-01 05:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mdfd = open(dev, O_RDWR, 0);
|
|
|
|
if (mdfd < 0)
|
|
|
|
fprintf(stderr, Name ": error opening %s: %s\n",
|
|
|
|
dev, strerror(errno));
|
|
|
|
else if (md_get_version(mdfd) <= 0) {
|
|
|
|
fprintf(stderr, Name ": %s does not appear to be an md device\n",
|
|
|
|
dev);
|
|
|
|
close(mdfd);
|
|
|
|
mdfd = -1;
|
|
|
|
}
|
|
|
|
return mdfd;
|
|
|
|
}
|
|
|
|
|