2001-06-08 04:36:23 +02:00
|
|
|
/*
|
2002-03-08 01:03:52 +01:00
|
|
|
* mdadm - manage Linux "md" devices aka RAID arrays.
|
2001-06-08 04:36:23 +02:00
|
|
|
*
|
2009-06-02 06:35:45 +02:00
|
|
|
* Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
|
2001-06-08 04:36:23 +02: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
|
2009-06-02 06:35:45 +02:00
|
|
|
* Email: <neilb@suse.de>
|
2001-06-08 04:36:23 +02:00
|
|
|
*/
|
|
|
|
|
2002-03-08 01:03:52 +01:00
|
|
|
#include "mdadm.h"
|
2001-06-08 04:36:23 +02:00
|
|
|
|
2013-06-19 04:31:45 +02:00
|
|
|
#define REGISTER_DEV _IO (MD_MAJOR, 1)
|
|
|
|
#define START_MD _IO (MD_MAJOR, 2)
|
|
|
|
#define STOP_MD _IO (MD_MAJOR, 3)
|
2001-07-26 09:00:09 +02:00
|
|
|
|
2012-07-09 09:21:57 +02:00
|
|
|
int Build(char *mddev, struct mddev_dev *devlist,
|
|
|
|
struct shape *s, struct context *c)
|
2001-06-08 04:36:23 +02:00
|
|
|
{
|
2001-07-26 09:00:09 +02:00
|
|
|
/* Build a linear or raid0 arrays without superblocks
|
|
|
|
* We cannot really do any checks, we just do it.
|
|
|
|
* For md_version < 0.90.0, we call REGISTER_DEV
|
|
|
|
* with the device numbers, and then
|
|
|
|
* START_MD giving the "geometry"
|
|
|
|
* geometry is 0xpp00cc
|
|
|
|
* where pp is personality: 1==linear, 2=raid0
|
|
|
|
* cc = chunk size factor: 0==4k, 1==8k etc.
|
|
|
|
*
|
|
|
|
* For md_version >= 0.90.0 we call
|
|
|
|
* SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int i;
|
|
|
|
int vers;
|
|
|
|
struct stat stb;
|
2005-06-14 08:29:20 +02:00
|
|
|
int subdevs = 0, missing_disks = 0;
|
2010-11-22 10:58:05 +01:00
|
|
|
struct mddev_dev *dv;
|
2005-06-08 01:16:35 +02:00
|
|
|
int bitmap_fd;
|
2005-11-22 04:37:14 +01:00
|
|
|
unsigned long long bitmapsize;
|
2008-11-04 00:35:37 +01:00
|
|
|
int mdfd;
|
2008-11-04 10:50:38 +01:00
|
|
|
char chosen_name[1024];
|
|
|
|
int uuid[4] = {0,0,0,0};
|
2008-11-04 10:50:39 +01:00
|
|
|
struct map_ent *map = NULL;
|
2001-07-26 09:00:09 +02:00
|
|
|
|
|
|
|
/* scan all devices, make sure they really are block devices */
|
2002-03-07 00:17:40 +01:00
|
|
|
for (dv = devlist; dv; dv=dv->next) {
|
2005-06-14 08:29:20 +02:00
|
|
|
subdevs++;
|
|
|
|
if (strcmp("missing", dv->devname) == 0) {
|
|
|
|
missing_disks++;
|
|
|
|
continue;
|
|
|
|
}
|
2002-03-07 00:17:40 +01:00
|
|
|
if (stat(dv->devname, &stb)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("Cannot find %s: %s\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname, strerror(errno));
|
2001-07-26 09:00:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((stb.st_mode & S_IFMT) != S_IFBLK) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("%s is not a block device.\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname);
|
2001-07-26 09:00:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2002-03-07 00:17:40 +01:00
|
|
|
}
|
|
|
|
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->raiddisks != subdevs) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("requested %d devices in array but listed %d\n",
|
2012-07-09 09:21:57 +02:00
|
|
|
s->raiddisks, subdevs);
|
2002-03-07 00:17:40 +01:00
|
|
|
return 1;
|
2001-07-26 09:00:09 +02:00
|
|
|
}
|
|
|
|
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->layout == UnSet)
|
|
|
|
switch(s->level) {
|
2004-11-01 05:49:34 +01:00
|
|
|
default: /* no layout */
|
2012-07-09 09:21:57 +02:00
|
|
|
s->layout = 0;
|
2004-11-01 05:49:34 +01:00
|
|
|
break;
|
|
|
|
case 10:
|
2012-07-09 09:21:57 +02:00
|
|
|
s->layout = 0x102; /* near=2, far=1 */
|
2012-07-09 09:19:21 +02:00
|
|
|
if (c->verbose > 0)
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("layout defaults to n1\n");
|
2004-11-01 05:49:34 +01:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 6:
|
2012-07-09 09:21:57 +02:00
|
|
|
s->layout = map_name(r5layout, "default");
|
2012-07-09 09:19:21 +02:00
|
|
|
if (c->verbose > 0)
|
2012-07-09 09:21:57 +02:00
|
|
|
pr_err("layout defaults to %s\n", map_num(r5layout, s->layout));
|
2004-11-01 05:49:34 +01:00
|
|
|
break;
|
|
|
|
case LEVEL_FAULTY:
|
2012-07-09 09:21:57 +02:00
|
|
|
s->layout = map_name(faultylayout, "default");
|
2004-11-01 05:49:34 +01:00
|
|
|
|
2012-07-09 09:19:21 +02:00
|
|
|
if (c->verbose > 0)
|
2012-07-09 09:21:57 +02:00
|
|
|
pr_err("layout defaults to %s\n", map_num(faultylayout, s->layout));
|
2004-11-01 05:49:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-04 10:50:21 +01:00
|
|
|
/* We need to create the device. It can have no name. */
|
2008-11-04 10:50:39 +01:00
|
|
|
map_lock(&map);
|
2012-07-09 09:19:21 +02:00
|
|
|
mdfd = create_mddev(mddev, NULL, c->autof, LOCAL,
|
2008-11-04 10:50:38 +01:00
|
|
|
chosen_name);
|
2008-11-04 10:50:39 +01:00
|
|
|
if (mdfd < 0) {
|
|
|
|
map_unlock(&map);
|
2008-11-04 00:35:37 +01:00
|
|
|
return 1;
|
2008-11-04 10:50:39 +01:00
|
|
|
}
|
2008-11-04 11:56:42 +01:00
|
|
|
mddev = chosen_name;
|
2004-11-01 05:49:34 +01:00
|
|
|
|
2012-11-01 06:14:01 +01:00
|
|
|
map_update(&map, fd2devnm(mdfd), "none", uuid, chosen_name);
|
2008-11-04 10:50:39 +01:00
|
|
|
map_unlock(&map);
|
2008-11-04 10:50:38 +01:00
|
|
|
|
2001-07-26 09:00:09 +02:00
|
|
|
vers = md_get_version(mdfd);
|
2007-12-14 10:13:43 +01:00
|
|
|
|
2001-07-26 09:00:09 +02:00
|
|
|
/* looks Ok, go for it */
|
2002-03-07 00:17:40 +01:00
|
|
|
if (vers >= 9000) {
|
2001-07-26 09:00:09 +02:00
|
|
|
mdu_array_info_t array;
|
2012-07-09 09:21:57 +02:00
|
|
|
array.level = s->level;
|
2012-08-13 00:00:18 +02:00
|
|
|
if (s->size == MAX_SIZE)
|
|
|
|
s->size = 0;
|
|
|
|
array.size = s->size;
|
2012-07-09 09:21:57 +02:00
|
|
|
array.nr_disks = s->raiddisks;
|
|
|
|
array.raid_disks = s->raiddisks;
|
2001-07-26 09:00:09 +02:00
|
|
|
array.md_minor = 0;
|
|
|
|
if (fstat(mdfd, &stb)==0)
|
2005-04-04 07:16:13 +02:00
|
|
|
array.md_minor = minor(stb.st_rdev);
|
2002-03-07 00:17:40 +01:00
|
|
|
array.not_persistent = 1;
|
2001-07-26 09:00:09 +02:00
|
|
|
array.state = 0; /* not clean, but no errors */
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->assume_clean)
|
2004-06-04 14:03:19 +02:00
|
|
|
array.state |= 1;
|
2012-07-09 09:21:57 +02:00
|
|
|
array.active_disks = s->raiddisks - missing_disks;
|
|
|
|
array.working_disks = s->raiddisks - missing_disks;
|
2001-07-26 09:00:09 +02:00
|
|
|
array.spare_disks = 0;
|
2005-06-14 08:29:20 +02:00
|
|
|
array.failed_disks = missing_disks;
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->chunk == 0 && (s->level==0 || s->level==LEVEL_LINEAR))
|
|
|
|
s->chunk = 64;
|
|
|
|
array.chunk_size = s->chunk*1024;
|
|
|
|
array.layout = s->layout;
|
2001-07-26 09:00:09 +02:00
|
|
|
if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("SET_ARRAY_INFO failed for %s: %s\n",
|
2001-07-26 09:00:09 +02:00
|
|
|
mddev, strerror(errno));
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2001-07-26 09:00:09 +02:00
|
|
|
}
|
2012-07-09 09:21:57 +02:00
|
|
|
} else if (s->bitmap_file) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("bitmaps not supported with this kernel\n");
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2001-07-26 09:00:09 +02:00
|
|
|
}
|
2007-05-21 06:25:47 +02:00
|
|
|
|
2013-03-05 00:36:21 +01:00
|
|
|
if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
|
|
|
|
s->bitmap_file = NULL;
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->bitmap_file && s->level <= 0) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("bitmaps not meaningful with level %s\n",
|
2012-07-09 09:21:57 +02:00
|
|
|
map_num(pers, s->level)?:"given");
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2007-05-21 06:25:47 +02:00
|
|
|
}
|
2001-07-26 09:00:09 +02:00
|
|
|
/* now add the devices */
|
2002-03-07 00:17:40 +01:00
|
|
|
for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
|
2006-12-14 07:32:57 +01:00
|
|
|
unsigned long long dsize;
|
2005-08-16 08:45:23 +02:00
|
|
|
int fd;
|
2005-06-14 08:29:20 +02:00
|
|
|
if (strcmp("missing", dv->devname) == 0)
|
|
|
|
continue;
|
2002-03-07 00:17:40 +01:00
|
|
|
if (stat(dv->devname, &stb)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("Weird: %s has disappeared.\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname);
|
2001-07-26 09:00:09 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
2002-03-07 00:17:40 +01:00
|
|
|
if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
|
2012-10-20 13:44:14 +02:00
|
|
|
pr_err("Weird: %s is no longer a block device.\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname);
|
2001-07-26 09:00:09 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
2005-08-16 08:45:23 +02:00
|
|
|
fd = open(dv->devname, O_RDONLY|O_EXCL);
|
|
|
|
if (fd < 0) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("Cannot open %s: %s\n",
|
2005-08-16 08:45:23 +02:00
|
|
|
dv->devname, strerror(errno));
|
|
|
|
goto abort;
|
|
|
|
}
|
2006-12-14 07:32:57 +01:00
|
|
|
if (get_dev_size(fd, NULL, &dsize) &&
|
2012-07-09 09:21:57 +02:00
|
|
|
(s->size == 0 || s->size == MAX_SIZE || dsize < s->size))
|
|
|
|
s->size = dsize;
|
2005-08-16 08:45:23 +02:00
|
|
|
close(fd);
|
2009-02-11 19:49:26 +01:00
|
|
|
if (vers >= 9000) {
|
2001-07-26 09:00:09 +02:00
|
|
|
mdu_disk_info_t disk;
|
|
|
|
disk.number = i;
|
|
|
|
disk.raid_disk = i;
|
2005-08-09 06:25:47 +02:00
|
|
|
disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
|
2008-10-25 09:20:49 +02:00
|
|
|
if (dv->writemostly == 1)
|
2005-08-09 06:25:47 +02:00
|
|
|
disk.state |= 1<<MD_DISK_WRITEMOSTLY;
|
2005-04-04 07:16:13 +02:00
|
|
|
disk.major = major(stb.st_rdev);
|
|
|
|
disk.minor = minor(stb.st_rdev);
|
2001-07-26 09:00:09 +02:00
|
|
|
if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("ADD_NEW_DISK failed for %s: %s\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname, strerror(errno));
|
2001-07-26 09:00:09 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("REGISTER_DEV failed for %s: %s.\n",
|
2002-03-07 00:17:40 +01:00
|
|
|
dv->devname, strerror(errno));
|
2001-07-26 09:00:09 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now to start it */
|
2002-03-07 00:17:40 +01:00
|
|
|
if (vers >= 9000) {
|
2001-07-26 09:00:09 +02:00
|
|
|
mdu_param_t param; /* not used by syscall */
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->bitmap_file) {
|
|
|
|
bitmap_fd = open(s->bitmap_file, O_RDWR);
|
2005-06-08 01:16:35 +02:00
|
|
|
if (bitmap_fd < 0) {
|
2005-10-11 06:44:44 +02:00
|
|
|
int major = BITMAP_MAJOR_HI;
|
2006-05-15 06:21:33 +02:00
|
|
|
#if 0
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->bitmap_chunk == UnSet) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("%s cannot be openned.",
|
2012-07-09 09:21:57 +02:00
|
|
|
s->bitmap_file);
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2005-06-08 01:16:35 +02:00
|
|
|
}
|
2006-05-15 06:21:33 +02:00
|
|
|
#endif
|
2005-10-11 06:44:44 +02:00
|
|
|
if (vers < 9003) {
|
|
|
|
major = BITMAP_MAJOR_HOSTENDIAN;
|
|
|
|
#ifdef __BIG_ENDIAN
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("Warning - bitmaps created on this kernel are not portable\n"
|
2006-05-15 06:21:33 +02:00
|
|
|
" between different architectures. Consider upgrading the Linux kernel.\n");
|
2005-10-11 06:44:44 +02:00
|
|
|
#endif
|
|
|
|
}
|
2012-07-09 09:21:57 +02:00
|
|
|
bitmapsize = s->size>>9; /* FIXME wrong for RAID10 */
|
|
|
|
if (CreateBitmap(s->bitmap_file, 1, NULL, s->bitmap_chunk,
|
|
|
|
c->delay, s->write_behind, bitmapsize, major)) {
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2005-06-08 01:16:35 +02:00
|
|
|
}
|
2012-07-09 09:21:57 +02:00
|
|
|
bitmap_fd = open(s->bitmap_file, O_RDWR);
|
2005-06-08 01:16:35 +02:00
|
|
|
if (bitmap_fd < 0) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("%s cannot be openned.",
|
2012-07-09 09:21:57 +02:00
|
|
|
s->bitmap_file);
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2005-06-08 01:16:35 +02:00
|
|
|
}
|
2007-12-14 10:13:43 +01:00
|
|
|
}
|
2005-06-08 01:16:35 +02:00
|
|
|
if (bitmap_fd >= 0) {
|
|
|
|
if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("Cannot set bitmap file for %s: %s\n",
|
2005-06-08 01:16:35 +02:00
|
|
|
mddev, strerror(errno));
|
2008-11-04 00:35:37 +01:00
|
|
|
goto abort;
|
2005-06-08 01:16:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-07 00:17:40 +01:00
|
|
|
if (ioctl(mdfd, RUN_ARRAY, ¶m)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("RUN_ARRAY failed: %s\n",
|
2001-07-26 09:00:09 +02:00
|
|
|
strerror(errno));
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->chunk & (s->chunk-1)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
cont_err("Problem may be that chunk size"
|
|
|
|
" is not a power of 2\n");
|
2011-05-10 09:35:41 +02:00
|
|
|
}
|
2001-07-26 09:00:09 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
} else {
|
2002-03-07 00:17:40 +01:00
|
|
|
unsigned long arg;
|
2001-07-26 09:00:09 +02:00
|
|
|
arg=0;
|
2012-07-09 09:21:57 +02:00
|
|
|
while (s->chunk > 4096) {
|
2001-07-26 09:00:09 +02:00
|
|
|
arg++;
|
2012-07-09 09:21:57 +02:00
|
|
|
s->chunk >>= 1;
|
2001-07-26 09:00:09 +02:00
|
|
|
}
|
2012-07-09 09:21:57 +02:00
|
|
|
if (s->level == 0)
|
2012-07-09 09:21:50 +02:00
|
|
|
arg |= 0x20000;
|
|
|
|
else
|
|
|
|
arg |= 0x10000;
|
2001-07-26 09:00:09 +02:00
|
|
|
if (ioctl(mdfd, START_MD, arg)) {
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("START_MD failed: %s\n",
|
2001-07-26 09:00:09 +02:00
|
|
|
strerror(errno));
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
}
|
2012-07-09 09:19:21 +02:00
|
|
|
if (c->verbose >= 0)
|
2012-07-09 09:14:16 +02:00
|
|
|
pr_err("array %s built and started.\n",
|
2005-08-15 08:14:27 +02:00
|
|
|
mddev);
|
2009-04-07 09:34:38 +02:00
|
|
|
wait_for(mddev, mdfd);
|
2008-11-04 00:35:37 +01:00
|
|
|
close(mdfd);
|
2001-07-26 09:00:09 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
abort:
|
2002-03-07 00:17:40 +01:00
|
|
|
if (vers >= 9000)
|
2001-07-26 09:00:09 +02:00
|
|
|
ioctl(mdfd, STOP_ARRAY, 0);
|
|
|
|
else
|
|
|
|
ioctl(mdfd, STOP_MD, 0);
|
2008-11-04 00:35:37 +01:00
|
|
|
close(mdfd);
|
2001-07-26 09:00:09 +02:00
|
|
|
return 1;
|
2001-06-08 04:36:23 +02:00
|
|
|
}
|