Add a check to ensure that the all devices have differents device uuid.
In case of conflict the program ends.
Pay attention that it is still possible to insert two device with
the same dev uuid via the "btrfs device scan <device>" command.

Signed-off-by: Goffredo Baroncelli <kreij...@inwind.it>
---
 volumes.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/volumes.c b/volumes.c
index 5b007fc..6ff4b4a 100644
--- a/volumes.c
+++ b/volumes.c
@@ -17,6 +17,7 @@
  */
 #define _XOPEN_SOURCE 600
 #define __USE_XOPEN2K
+#define _BSD_SOURCE            /* for minor/major macro */
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -139,7 +140,31 @@ static int device_list_add(const char *path,
                list_add(&device->dev_list, &fs_devices->devices);
                device->fs_devices = fs_devices;
        } else if (!device->name || strcmp(device->name, path)) {
-               char *name = strdup(path);
+               char *name;
+               struct stat statbuf1;
+
+               /*
+                * check if the old device and the new device are differents
+                * if so abort because we can't say which one is the right one
+                */
+               if (device->name && !stat(device->name, &statbuf1)) {
+                       struct stat statbuf2;
+
+                       if (stat(path, &statbuf2))
+                               return -ENOENT;
+
+                       if (major(statbuf1.st_rdev) != major(statbuf2.st_rdev) 
||
+                               minor(statbuf1.st_rdev) != 
minor(statbuf2.st_rdev)) {
+                               fprintf(stderr, "ERROR: two devices (%s and %s) 
have the same dev uuid !!\n",
+                                       device->name, path);
+                               fprintf(stderr, "ERROR: remove one of the 
device and retry\n");
+                               fprintf(stderr, "ERROR: the program will abort 
now\n");
+
+                               exit(100);
+                       }
+               }
+
+               name = strdup(path);
                 if (!name)
                         return -ENOMEM;
                 kfree(device->name);
-- 
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to