btrfs-progs: create helper function to use lblkid to scan for btrfs disks
authorAnand Jain <anand.jain@oracle.com>
Fri, 27 Sep 2013 15:45:00 +0000 (23:45 +0800)
committerChris Mason <chris.mason@fusionio.com>
Wed, 16 Oct 2013 12:23:11 +0000 (08:23 -0400)
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
utils.c
utils.h

diff --git a/utils.c b/utils.c
index accecef..2f1d54f 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1914,6 +1914,58 @@ int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
        return 0;
 }
 
+int test_skip_this_disk(char *path)
+{
+       int fd;
+
+       /*
+        * this will eliminate disks which are mounted (btrfs)
+        * and non-dm disk path when dm is enabled
+        */
+       fd = open(path, O_RDWR|O_EXCL);
+       if (fd < 0)
+               return 1;
+       close(fd);
+       return 0;
+}
+
+int btrfs_scan_lblkid(int update_kernel)
+{
+       int fd = -1;
+       u64 num_devices;
+       struct btrfs_fs_devices *tmp_devices;
+       blkid_dev_iterate iter = NULL;
+       blkid_dev dev = NULL;
+       blkid_cache cache = NULL;
+       char path[PATH_MAX];
+
+       if (blkid_get_cache(&cache, 0) < 0) {
+               printf("ERROR: lblkid cache get failed\n");
+               return 1;
+       }
+       blkid_probe_all(cache);
+       iter = blkid_dev_iterate_begin(cache);
+       blkid_dev_set_search(iter, "TYPE", "btrfs");
+       while (blkid_dev_next(iter, &dev) == 0) {
+               dev = blkid_verify(cache, dev);
+               if (!dev)
+                       continue;
+               /* if we are here its definitly a btrfs disk*/
+               strcpy(path, blkid_dev_devname(dev));
+               if (test_skip_this_disk(path))
+                       continue;
+
+               fd = open(path, O_RDONLY);
+               btrfs_scan_one_device(fd, path, &tmp_devices,
+                               &num_devices, BTRFS_SUPER_INFO_OFFSET);
+               close(fd);
+               if (update_kernel)
+                       btrfs_register_one_device(path);
+       }
+       blkid_dev_iterate_end(iter);
+       return 0;
+}
+
 /*
  * scans devs for the btrfs
 */
@@ -1928,6 +1980,9 @@ int scan_for_btrfs(int where, int update_kernel)
        case BTRFS_SCAN_DEV:
                ret = btrfs_scan_one_dir("/dev", update_kernel);
                break;
+       case BTRFS_SCAN_LBLKID:
+               ret = btrfs_scan_lblkid(update_kernel);
+               break;
        }
        return ret;
 }
diff --git a/utils.h b/utils.h
index 19f028f..a6d3e5e 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -27,6 +27,7 @@
 
 #define BTRFS_SCAN_PROC        1
 #define BTRFS_SCAN_DEV         2
+#define BTRFS_SCAN_LBLKID      (1ULL << 3)
 
 int make_btrfs(int fd, const char *device, const char *label,
               u64 blocks[6], u64 num_bytes, u32 nodesize,
@@ -82,5 +83,6 @@ int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
                           int verify);
 int ask_user(char *question);
 int lookup_ino_rootid(int fd, u64 *rootid);
+int btrfs_scan_lblkid(int update_kernel);
 
 #endif