* The University of New South Wales
* Sydney, 2052
* Australia
+ *
+ * Additions for bitmap and async RAID options, Copyright (C) 2003-2004,
+ * Paul Clements, SteelEye Technology, Inc.
*/
#include "mdadm.h"
#include <ctype.h>
-
int main(int argc, char *argv[])
{
int mode = 0;
char *help_text;
char *c;
int rv;
+ int i;
int chunk = 0;
int size = -1;
char devmode = 0;
int runstop = 0;
int readonly = 0;
+ int bitmap_fd = -1;
+ char *bitmap_file = NULL;
+ int bitmap_chunk = UnSet;
int SparcAdjust = 0;
mddev_dev_t devlist = NULL;
mddev_dev_t *devlistend = & devlist;
int daemonise = 0;
char *pidfile = NULL;
int oneshot = 0;
+ struct supertype *ss = NULL;
int copies;
int mdfd = -1;
+ srandom(time(0) ^ getpid());
+
ident.uuid_set=0;
ident.level = UnSet;
ident.raid_disks = UnSet;
ident.devices=0;
ident.spare_group = NULL;
ident.autof = 0;
+ ident.st = NULL;
+ ident.bitmap_fd = -1;
while ((option_index = -1) ,
(opt=getopt_long(argc, argv,
case 'v': verbose = 1;
continue;
- case 'b': brief = 1;
+ case 'b':
+ if (mode == ASSEMBLE || mode == BUILD || mode == CREATE)
+ break; /* b means bitmap */
+ brief = 1;
continue;
case ':':
case 'a':
case 'r':
case 'f':
- case 1 : if (!mode) newmode = MANAGE; break;
+ if (!mode) newmode = MANAGE;
+ break;
case 'A': newmode = ASSEMBLE; break;
case 'B': newmode = BUILD; break;
case '#':
case 'D':
case 'E':
+ case 'X':
case 'Q': newmode = MISC; break;
case 'R':
case 'S':
fprintf(stderr, "--%s", long_options[option_index].name);
else
fprintf(stderr, "-%c", opt);
- fprintf(stderr, " would set mode to %s, but it is already %s.\n",
+ fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
map_num(modes, newmode),
map_num(modes, mode));
exit(2);
fputs(Help_config, stderr);
exit(0);
}
+
+ /* If first option is a device, don't force the mode yet */
+ if (opt == 1) {
+ if (devs_found == 0) {
+ dv = malloc(sizeof(*dv));
+ if (dv == NULL) {
+ fprintf(stderr, Name ": malloc failed\n");
+ exit(3);
+ }
+ dv->devname = optarg;
+ dv->disposition = devmode;
+ dv->next = NULL;
+ *devlistend = dv;
+ devlistend = &dv->next;
+
+ devs_found++;
+ continue;
+ }
+ /* No mode yet, and this is the second device ... */
+ fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
+ exit(2);
+ }
if (option_index >= 0)
- fprintf(stderr, "--%s", long_options[option_index].name);
+ fprintf(stderr, Name ": --%s", long_options[option_index].name);
else
- fprintf(stderr, "-%c", opt);
- fprintf(stderr, " does not set the mode, and so cannot be first.\n");
+ fprintf(stderr, Name ": -%c", opt);
+ fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
exit(2);
}
}
continue;
+ case O(CREATE,'e'):
+ case O(ASSEMBLE,'e'):
+ case O(MISC,'e'): /* set metadata (superblock) information */
+ if (ss) {
+ fprintf(stderr, Name ": metadata information already given\n");
+ exit(2);
+ }
+ for(i=0; !ss && superlist[i]; i++)
+ ss = superlist[i]->match_metadata_desc(optarg);
+
+ if (!ss) {
+ fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
+ exit(2);
+ }
+ continue;
+
case O(GROW,'z'):
case O(CREATE,'z'): /* size */
if (size >= 0) {
continue;
case O(MONITOR,'d'): /* delay in seconds */
+ case O(BUILD,'d'): /* delay for bitmap updates */
+ case O(CREATE,'d'):
if (delay)
fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
optarg);
case O(MISC,'K'):
case O(MISC,'R'):
case O(MISC,'S'):
+ case O(MISC,'X'):
case O(MISC,'o'):
case O(MISC,'w'):
if (devmode && devmode != opt &&
}
SparcAdjust = 1;
continue;
+
+ case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
+ if (!optarg) {
+ fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
+ exit(2);
+ }
+ bitmap_fd = open(optarg, O_RDWR);
+ if (!*optarg || bitmap_fd < 0) {
+ fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
+ exit(2);
+ }
+ ident.bitmap_fd = bitmap_fd; /* for Assemble */
+ continue;
+ case O(BUILD,'b'):
+ case O(CREATE,'b'): /* here we create the bitmap */
+ bitmap_file = optarg;
+ continue;
+
+ case O(BUILD,4):
+ case O(CREATE,4): /* bitmap chunksize */
+ bitmap_chunk = strtol(optarg, &c, 10);
+ if (!optarg[0] || *c || bitmap_chunk < 0 ||
+ bitmap_chunk & (bitmap_chunk - 1)) {
+ fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
+ optarg);
+ exit(2);
+ }
+ /* convert K to B, chunk of 0K means 512B */
+ bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
+ continue;
}
/* We have now processed all the valid options. Anything else is
* an error
}
+ if (!mode && devs_found) {
+ mode = MISC;
+ devmode = 'Q';
+ if (devlist->disposition == 0)
+ devlist->disposition = devmode;
+ }
if (!mode) {
fputs(Usage, stderr);
exit(2);
if ((int)ident.super_minor == -2) {
struct stat stb;
fstat(mdfd, &stb);
- ident.super_minor = MINOR(stb.st_rdev);
+ ident.super_minor = minor(stb.st_rdev);
}
}
+
rv = 0;
switch(mode) {
case MANAGE:
if (mdfd < 0)
rv |= 1;
else {
- rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
+ rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
NULL,
readonly, runstop, update, verbose, force);
close(mdfd);
}
}
} else if (!scan)
- rv = Assemble(devlist->devname, mdfd, &ident, configfile,
+ rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
devlist->next,
readonly, runstop, update, verbose, force);
else if (devs_found>0) {
rv |= 1;
continue;
}
- rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
+ rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
NULL,
readonly, runstop, update, verbose, force);
close(mdfd);
}
if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
/* already assembled, skip */
- continue;
- rv |= Assemble(array_list->devname, mdfd,
- array_list, configfile,
- NULL,
- readonly, runstop, NULL, verbose, force);
+ ;
+ else
+ rv |= Assemble(ss, array_list->devname, mdfd,
+ array_list, configfile,
+ NULL,
+ readonly, runstop, NULL, verbose, force);
+ close(mdfd);
}
}
break;
case BUILD:
- rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
+ if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK;
+ if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
+ if (bitmap_file) {
+ bitmap_fd = open(bitmap_file, O_RDWR,0);
+ if (bitmap_fd < 0 && errno != ENOENT) {
+ perror(Name ": cannot create bitmap file");
+ rv |= 1;
+ break;
+ }
+ if (bitmap_fd < 0) {
+ bitmap_fd = CreateBitmap(bitmap_file, force, NULL,
+ bitmap_chunk, delay, size);
+ }
+ }
+ rv = Build(devlist->devname, mdfd, chunk, level, layout,
+ raiddisks, devlist->next, assume_clean,
+ bitmap_file, bitmap_chunk, delay);
break;
case CREATE:
- rv = Create(devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
+ if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK;
+ if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
+ if (ss == NULL) {
+ for(i=0; !ss && superlist[i]; i++)
+ ss = superlist[i]->match_metadata_desc("default");
+ }
+ if (!ss) {
+ fprintf(stderr, Name ": internal error - no default metadata style\n");
+ exit(2);
+ }
+
+ rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
raiddisks, sparedisks,
- devs_found-1, devlist->next, runstop, verbose, force);
+ devs_found-1, devlist->next, runstop, verbose, force,
+ bitmap_file, bitmap_chunk, delay);
break;
case MISC:
fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
exit(1);
}
- rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
+ rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust, ss);
} else {
if (devlist == NULL) {
if ((devmode == 'S' ||devmode=='D') && scan) {
/* apply to all devices in /proc/mdstat */
struct mdstat_ent *ms = mdstat_read(0);
struct mdstat_ent *e;
+ if (devmode == 'S') {
+ /* reverse order so that arrays made of arrays are stopped properly */
+ struct mdstat_ent *sm = NULL;
+ while ((e=ms) != NULL) {
+ ms = e->next;
+ e->next = sm;
+ sm = e;
+ }
+ ms = sm;
+ }
for (e=ms ; e ; e=e->next) {
char *name = get_md_name(e->devnum);
rv |= Detail(name, !verbose, test);
else if (devmode=='S') {
mdfd = open_mddev(name, 0);
- if (mdfd >= 0)
+ if (mdfd >= 0) {
rv |= Manage_runstop(name, mdfd, -1);
+ close(mdfd);
+ }
}
put_md_name(name);
}
rv |= Kill(dv->devname, force); continue;
case 'Q':
rv |= Query(dv->devname); continue;
+ case 'X':
+ rv |= ExamineBitmap(dv->devname, brief); continue;
}
mdfd = open_mddev(dv->devname, 0);
- if (mdfd>=0)
+ if (mdfd>=0) {
switch(dv->disposition) {
case 'R':
rv |= Manage_runstop(dv->devname, mdfd, 1); break;
case 'w':
rv |= Manage_ro(dv->devname, mdfd, -1); break;
}
+ close(mdfd);
+ }
}
}
break;