Use O_EXCL when opening component devices to be assembled into an array
authorNeil Brown <neilb@suse.de>
Mon, 4 Apr 2005 06:02:49 +0000 (06:02 +0000)
committerNeil Brown <neilb@suse.de>
Mon, 4 Apr 2005 06:02:49 +0000 (06:02 +0000)
In 2.6, this will fail if the device is already in use, so we can detect this error
more easily.

Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au>
Assemble.c
ChangeLog
Create.c
Kill.c
Manage.c

index 4e4e123..ace5717 100644 (file)
@@ -193,7 +193,7 @@ int Assemble(char *mddev, int mdfd,
                        continue;
                }
                
-               dfd = open(devname, O_RDONLY, 0);
+               dfd = open(devname, O_RDONLY|O_EXCL, 0);
                if (dfd < 0) {
                        if (inargv || verbose)
                                fprintf(stderr, Name ": cannot open device %s: %s\n",
@@ -326,7 +326,7 @@ int Assemble(char *mddev, int mdfd,
                                super.recovery_cp = 0;
                        }
                        super.sb_csum = calc_sb_csum(&super);
-                       dfd = open(devname, O_RDWR, 0);
+                       dfd = open(devname, O_RDWR|O_EXCL, 0);
                        if (dfd < 0) 
                                fprintf(stderr, Name ": Cannot open %s for superblock update\n",
                                        devname);
@@ -435,7 +435,7 @@ int Assemble(char *mddev, int mdfd,
                        devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
                        (int)(devices[chosen_drive].events),
                        (int)(devices[most_recent].events));
-               fd = open(devices[chosen_drive].devname, O_RDWR);
+               fd = open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
                if (fd < 0) {
                        fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
                                devices[chosen_drive].devname);
@@ -484,7 +484,7 @@ int Assemble(char *mddev, int mdfd,
                if (!devices[j].uptodate)
                        continue;
                chosen_drive = j;
-               if ((fd=open(devices[j].devname, O_RDONLY))< 0) {
+               if ((fd=open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
                        fprintf(stderr, Name ": Cannot open %s: %s\n",
                                devices[j].devname, strerror(errno));
                        return 1;
@@ -556,7 +556,7 @@ This doesnt work yet
            || (old_linux && (change & 1))) {
                int fd;
                super.sb_csum = calc_sb_csum(&super);
-               fd = open(devices[chosen_drive].devname, O_RDWR);
+               fd = open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
                if (fd < 0) {
                        fprintf(stderr, Name ": Could open %s for write - cannot Assemble array.\n",
                                devices[chosen_drive].devname);
index 7e03a5c..6854858 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Changes Prior to 1.10.0 release
+    -   Fix bug with --config=partitions
+    -   Open sub-devices with O_EXCL to detect if already in use
+
 Changes Prior to 1.9.0 release
     -   Fix rpm build problem (stray %)
     -   Minor manpage updates
index 1c814f7..66a88d2 100644 (file)
--- a/Create.c
+++ b/Create.c
@@ -187,7 +187,7 @@ int Create(char *mddev, int mdfd,
                array.working_disks++;
                if (dnum < raiddisks)
                        array.active_disks++;
-               fd = open(dname, O_RDONLY, 0);
+               fd = open(dname, O_RDONLY|O_EXCL, 0);
                if (fd <0 ) {
                        fprintf(stderr, Name ": Cannot open %s: %s\n",
                                dname, strerror(errno));
@@ -368,7 +368,7 @@ int Create(char *mddev, int mdfd,
                        disk.minor = 0;
                        disk.state = 1; /* faulty */
                } else {
-                       fd = open(dv->devname, O_RDONLY, 0);
+                       fd = open(dv->devname, O_RDONLY|O_EXCL, 0);
                        if (fd < 0) {
                                fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
                                        dv->devname);
diff --git a/Kill.c b/Kill.c
index a57bdf8..9441f73 100644 (file)
--- a/Kill.c
+++ b/Kill.c
@@ -44,7 +44,7 @@ int Kill(char *dev, int force)
        mdp_super_t super;
        int fd, rv = 0;
                
-       fd = open(dev, O_RDWR);
+       fd = open(dev, O_RDWR|O_EXCL);
        if (fd < 0) {
                fprintf(stderr, Name ": Couldn't open %s for write - not zeroing\n",
                        dev);
index 8835095..b323fd3 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -194,6 +194,14 @@ int Manage_subdevs(char *devname, int fd,
                        return 1;
                case 'a':
                        /* add the device - hot or cold */
+                       /* Make sure it isn' in use (in 2.6 or later) */
+                       fd = open(dv->devname, O_RDONLY|O_EXCL);
+                       if (fd < 0) {
+                               fprintf(stderr, Name ": Cannot open %s: %s\n",
+                                       dv->devname, strerror(errno));
+                               return 1;
+                       }
+                       close(fd);
                        if (ioctl(fd, HOT_ADD_DISK, (unsigned long)stb.st_rdev)==0) {
                                fprintf(stderr, Name ": hot added %s\n",
                                        dv->devname);