Use O_EXCL when opening component devices to be assembled into an array

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>
This commit is contained in:
Neil Brown 2005-04-04 06:02:49 +00:00
parent b568741513
commit d7eaf49f65
5 changed files with 20 additions and 8 deletions

View 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);

View File

@ -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

View File

@ -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);

2
Kill.c
View File

@ -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);

View File

@ -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);