btrfs-progs: create helper function to use lblkid to scan for btrfs disks
PATCH 1/1 btrfs-progs: create helper function to use lblkid to scan for btrfs disks
--- utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 2 ++ 2 files changed, 56 insertions(+)
utils.c +54 -0
@@ -1914,6 +1914,57 @@ 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); + fd =-1 ; +if (update_kernel) +btrfs_register_one_device (path); + } +blkid_dev_iterate_end (iter); +return 0 ; +} +/* * scans devs for the btrfs */
@@ -1928,6 +1979,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; }
utils.h +2 -0
@@ -28,6 +28,7 @@#define BTRFS_SCAN_PROC (1ULL << 0)#define BTRFS_SCAN_DEV (1ULL << 1)#define BTRFS_SCAN_MOUNTED (1ULL << 2) +#define BTRFS_SCAN_LBLKID (1ULL << 3)#define BTRFS_UPDATE_KERNEL 1
@@ -89,5 +90,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
Signature · 1 line
1.8.4.rc4.1.g0d8beaa
Following this patch the idea is to use lblkid to scan
for the btrfs disks by default which means we don't
use BTRFS_SCAN_PROC any more.
which implies commands btrfs filesystem show and btrfs device scan will use lblkid to scan disks instead of current /proc/partitions by default.
So now the question is, if there is any need to have option to scan using /proc/partitions ? and instead of removing it completely would we need it under a new option '-p' (in filesystem show and device scan) so that user can use /proc/partitions when needed, I really don't know what would be that circumstance though,
Any thoughts?
Thanks Anand
Quoted 98 lines · Anand Jain
Signed-off-by Anand Jain <[email protected]>--- utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 2 ++ 2 files changed, 56 insertions(+)utils.c +54
-0@@ -1914,6 +1914,57 @@ 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); + fd =-1 ; +if (update_kernel) +btrfs_register_one_device (path); + } +blkid_dev_iterate_end (iter); +return 0 ; +} +/* * scans devs for the btrfs */ @@ -1928,6 +1979,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; }utils.h +2
-0@@ -28,6 +28,7 @@#define BTRFS_SCAN_PROC (1ULL << 0)#define BTRFS_SCAN_DEV (1ULL << 1)#define BTRFS_SCAN_MOUNTED (1ULL << 2) +#define BTRFS_SCAN_LBLKID (1ULL << 3)#define BTRFS_UPDATE_KERNEL 1@@ -89,5 +90,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
Helo Anand,
Quoted 5 lines
Following this patch the idea is to use lblkid to scan for the btrfs disks by default which means we don't use
BTRFS_SCAN_PROCany more.
Firstly, i would like to know if we will get any different results between scanning /proc/partions and using lbkid.
If not, why we can use liblkid totally,since this is more simple.
Thanks,
Wang
Quoted 120 lines
which implies commands btrfs filesystem show and btrfs device scan will use lblkid to scan disks instead of current
/proc/partitionsby default.So now the question is, if there is any need to have option to scan using
/proc/partitions? and instead of removing it completely would we need it under a new option '-p' (in filesystem show and device scan) so that user can use/proc/partitionswhen needed, I really don't know what would be that circumstance though,Any thoughts?
Thanks Anand
Quoted 92 lines · Anand Jain
Signed-off-by Anand Jain <[email protected]>--- utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 2 ++ 2 files changed, 56 insertions(+)utils.c +54
-0@@ -1914,6 +1914,57 @@ 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); + fd =-1 ; +if (update_kernel) +btrfs_register_one_device (path); + } +blkid_dev_iterate_end (iter); +return 0 ; +} +/* * scans devs for the btrfs */ @@ -1928,6 +1979,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; }utils.h +1
-0@@ -28,6 +28,7 @@#define BTRFS_SCAN_PROC (1ULL << 0)#define BTRFS_SCAN_DEV (1ULL << 1)#define BTRFS_SCAN_MOUNTED (1ULL << 2) +#define BTRFS_SCAN_LBLKID (1ULL << 3)#define BTRFS_UPDATE_KERNEL 1utils.h int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, · line 95
Quoted 6 lines
utils.h +1
-0@@ -89,5 +90,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 --
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 6 lines · Wang Shilong
Quoted 3 lines
Following this patch the idea is to use lblkid to scan for the btrfs disks by default which means we don't use
BTRFS_SCAN_PROCany more.Firstly, i would like to know if we will get any different results between scanning
/proc/partionsand using lbkid.
I take blkid as a more authoritative source, /proc/partitions was a workaround to avoid scanning the whole /dev directory.
Quoted 1 line
If not, why we can use liblkid totally,since this is more simple.
Agreed, blkid by default and --all-devices as a last resort fallback. The proc workaround can go away.
david
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Following this patch the idea is to use lblkid to scan for the btrfs disks by default which means we don't use
BTRFS_SCAN_PROCany more.Firstly, i would like to know if we will get any different results between scanning
/proc/partionsand using lbkid.
Quoted 6 lines
I take blkid as a more authoritative source,
/proc/partitionswas a workaround to avoid scanning the whole /dev directory.Quoted 1 line
If not, why we can use liblkid totally,since this is more simple.
Agreed, blkid by default and --all-devices as a last resort fallback.
compiled using BTRFS_SCAN_LBLKID as default.
---
cmds-filesystem.c +1 -1
@@ -402,7 +402,8 @@ static int cmd_show(int argc, char **argv)struct list_head *cur_uuid;char *search =NULL ;int ret; -int where =BTRFS_SCAN_PROC ; +int where =BTRFS_SCAN_LBLKID ;int type =0 ;while (1 ) {
------
# btrfs fi show Label: none uuid: c114c6ce-58a1-4e2c-839e-b53c5ba5ba75 Total devices 1 FS bytes used 756.00KiB devid 1 size 1.10GiB used 324.00MiB path /dev/mapper/mpathg ---
recompiled to use BTRFS_SCAN_PROC as in original.
---
# btrfs fi show Label: none uuid: c114c6ce-58a1-4e2c-839e-b53c5ba5ba75 Total devices 1 FS bytes used 36.00KiB devid 1 size 1.10GiB used 148.62MiB path /dev/dm-2 ---
Quoted 1 line
The proc workaround can go away.
The other choice is to use both, 'BTRFS_SCAN_PROC | BTRFS_SCAN_DEV' for --all-devices option. in that way proc-way will still be in there.
Thanks, Anand