From fe5050bf798f87e94c75fbe468899b630cfae524 Mon Sep 17 00:00:00 2001 From: Surbhi Palande Date: Tue, 15 Mar 2011 20:21:56 +0200 Subject: [PATCH] Stop an array before trying to scan assemble it. Device name corresponding to the name stored in the name field of the array meta data is created only after the array becomes active. The member devices associated with the array cannot be opened in O_EXCL mode nor can the unformed array be. To start such an inactive array in a degraded mode then can be made possible by reading the array name from /proc/mdstat and stopping any array which is in active. --- mdadm.c | 1 + mdadm.h | 2 ++ mdstat.c | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 0 deletions(-) diff --git a/mdadm.c b/mdadm.c index efba8dd..61f8047 100644 --- a/mdadm.c +++ b/mdadm.c @@ -1151,6 +1151,7 @@ int main(int argc, char *argv[]) mddev_dev_t devlist = conf_get_devs(); int cnt = 0; int failures, successes; + mdstat_stop_inactive_arr(); if (devlist == NULL) { fprintf(stderr, Name ": No devices listed in conf file were found.\n"); exit(1); diff --git a/mdadm.h b/mdadm.h index f5367bf..6ac2f1f 100644 --- a/mdadm.h +++ b/mdadm.h @@ -950,6 +950,8 @@ extern char *devnum2devname(int num); extern int devname2devnum(char *name); extern int stat2devnum(struct stat *st); extern int fd2devnum(int fd); +extern int mdstat_stop_inactive_arr(void); + static inline int dev2major(int d) { diff --git a/mdstat.c b/mdstat.c index 580e87c..9f2861f 100644 --- a/mdstat.c +++ b/mdstat.c @@ -125,6 +125,33 @@ void free_mdstat(struct mdstat_ent *ms) } } +/* Read the existing array names from /proc/mdstat */ +int mdstat_stop_inactive_arr(void) +{ + int fd; + struct mdstat_ent * ent, *mdstat = mdstat_read(0, 1); + char * devname; + if (mdstat == NULL) { + perror("\n Could not open /proc/mdstat because: "); + return(-1); + } + for(ent=mdstat; ent!=NULL; ent=ent->next) { + if(ent->active == 1) + continue; + asprintf(&devname, "/dev/%s", ent->dev); + if(devname) { + fd = open(devname, O_EXCL | O_RDWR); + if( fd > 0) { + /* So that --assemble --scan --run work as expected */ + ioctl(fd, STOP_ARRAY, NULL); + close(fd); + } + } + } + free_mdstat(mdstat); + return 0; +} + static int mdstat_fd = -1; struct mdstat_ent *mdstat_read(int hold, int start) { -- 1.7.1