Btrfs-progs: coalesce of patches
cover Btrfs-progs: coalesce of patches
Some fixes and minor enhancements.
Filipe David Borba Manana (5):
Btrfs-progs: fix closing of devices
Btrfs-progs: Add missing free_extent_buffer() call to debug-tree
Btrfs-progs: Add missing close_ctree() calls to debug-tree
Btrfs-progs: pretty print dir_item type
Btrfs-progs: Validate super block checksum
btrfs-debug-tree.c | 10 ++++--- disk-io.c | 80 +++++++++++++++++++++++++++++++++++++++++----------- print-tree.c | 40 +++++++++++++++++++++++++- volumes.c | 4 +-- 4 files changed, 111 insertions(+), 23 deletions(-)
Signature · 6 lines
1.7.9.5 -- 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-
PATCH 1/5 Btrfs-progs: fix closing of devices
If a device could not be opened in volumes.c:read_one_dev(), a btrfs_device instance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tries to close the (invalid) fd of the device and kfree() its name and label, which point to random memory locations.
Thread 1 (Thread 0x7f0a3d2d1740 (LWP 23585)):
#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295
--- disk-io.c | 4 ++-- volumes.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
disk-io.c +2 -2
@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >=0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);
volumes.c +2 -2
@@ -1628,10 +1628,10 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ; + device->fd =-1 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }
Signature · 6 lines
1.7.9.5 -- 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-
PATCH 1/5 Btrfs-progs: fix closing of devices
If a device could not be opened in volumes.c:read_one_dev(), a btrfs_device instance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tries to close the (invalid) fd of the device and kfree() its name and label, which point to random memory locations.
Thread 1 (Thread 0x7f0a3d2d1740 (LWP 23585)):
#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295
--- disk-io.c | 4 ++-- volumes.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-)
disk-io.c +2 -2
@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);
volumes.c +1 -2
@@ -1628,10 +1628,9 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }
Signature · 6 lines
1.7.9.5 -- 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-
Re: [PATCH 1/5] Btrfs-progs: fix closing of devices
Quoted 4 lines · Filipe David Borba Manana
disk-io.c +0
-0@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) {
disk-io.c static int close_all_devices(struct btrfs_fs_info *fs_info) · line 1275
Quoted 10 lines
disk-io.c +2
-3device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >0 ) { - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ;
Can you please describe the incremental change between the two patches? Why do you exclude fd == 0 ?
david
Re: [PATCH 1/5] Btrfs-progs: fix closing of devices
Quoted 10 lines · David Sterba
Quoted 8 lines · Filipe David Borba Manana
disk-io.c +1
-1@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >0 ) {
Quoted 8 lines
disk-io.c static int close_all_devices(struct btrfs_fs_info *fs_info) · line 1274
Quoted 5 lines
disk-io.c +1
-2- device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ;Can you please describe the incremental change between the two patches? Why do you exclude fd == 0 ?
Sorry, I messed up my git send-email foo.
I excluded 0 because the original code did it too, however it was logically incorrect. The last patch version (v2, 3rd email) does not exclude 0 and ensures that -1 is used everywhere as a marker for invalid fd.
(Hopefully now I figured how to use git send-email to update a patch correctly)
Thanks
Quoted 2 lines
david
PATCH 2/5 Btrfs-progs: Add missing free_extent_buffer() call to debug-tree
In reply to Filipe David Borba Manana--- btrfs-debug-tree.c | 1 + 1 file changed, 1 insertion(+)
btrfs-debug-tree.c +1 -0
@@ -355,6 +355,7 @@ again:btrfs_print_tree (tree_root_scan, buf,1 ); } } +free_extent_buffer (buf); } next: path.slots[0 ]++;
Signature · 6 lines
1.7.9.5 -- 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-
PATCH 3/5 Btrfs-progs: Add missing close_ctree() calls to debug-tree
In reply to Filipe David Borba Manana--- btrfs-debug-tree.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
btrfs-debug-tree.c +5 -4
@@ -195,10 +195,10 @@ int main(int ac, char **av)if (!leaf) {fprintf (stderr,"failed to read %llu\n" , (unsigned long long )block_only); -return 0 ; + goto close_root; }btrfs_print_tree (root, leaf,0 ); -return 0 ; + goto close_root; }if (!extent_only) {
@@ -370,7 +370,7 @@ no_node: }if (extent_only || device_only) -return 0 ; + goto close_root;if (root_backups)print_old_roots (info->super_copy);
@@ -383,5 +383,6 @@ no_node:uuid_unparse (info->super_copy->fsid, uuidbuf);printf ("uuid %s\n" , uuidbuf);printf ("%s\n" ,BTRFS_BUILD_VERSION ); -return 0 ; +close_root: +return close_ctree (root); }
Signature · 6 lines
1.7.9.5 -- 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-
PATCH 4/5 Btrfs-progs: pretty print dir_item type
In reply to Filipe David Borba MananaInstead of printing an integer, print a symbolic name which is more human friendly. Particularly useful when using the program btrfs-debug-tree.
--- print-tree.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)
print-tree.c +39 -1
@@ -25,6 +25,42 @@#include "disk-io.h" #include "print-tree.h" + +static void print_dir_item_type (struct extent_buffer *eb, +struct btrfs_dir_item *di) +{ +u8 type =btrfs_dir_type (eb, di); + +switch (type) { +case BTRFS_FT_REG_FILE : +printf ("FILE" ); +break ; +case BTRFS_FT_DIR : +printf ("DIR" ); +break ; +case BTRFS_FT_CHRDEV : +printf ("CHRDEV" ); +break ; +case BTRFS_FT_BLKDEV : +printf ("BLKDEV" ); +break ; +case BTRFS_FT_FIFO : +printf ("FIFO" ); +break ; +case BTRFS_FT_SOCK : +printf ("SOCK" ); +break ; +case BTRFS_FT_SYMLINK : +printf ("SYMLINK" ); +break ; +case BTRFS_FT_XATTR : +printf ("XATTR" ); +break ; +default : +printf ("%u" , type); + } +} +static int print_dir_item (struct extent_buffer *eb,struct btrfs_item *item,struct btrfs_dir_item *di) {
@@ -41,7 +77,9 @@ static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,btrfs_dir_item_key (eb, di, &location);printf ("\t\tlocation " );btrfs_print_key (&location); -printf (" type %u\n" ,btrfs_dir_type (eb, di)); +printf (" type " ); +print_dir_item_type (eb, di); +printf ("\n" ); name_len =btrfs_dir_name_len (eb, di); data_len =btrfs_dir_data_len (eb, di); len = (name_len <=sizeof (namebuf))? name_len:sizeof (namebuf);
Signature · 6 lines
1.7.9.5 -- 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-
PATCH 5/5 Btrfs-progs: Validate super block checksum
In reply to Filipe David Borba MananaAfter finding a super block in a device also validate its checksum. This validation is done in the kernel but it was missing in btrfs-progs.
The function btrfs_check_super_csum() is imported from the file fs/btrfs/disk-io.c in the kernel source tree.
--- disk-io.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 14 deletions(-)
disk-io.c +62 -14
@@ -1085,47 +1085,95 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,return info->fs_root; } +static int btrfs_check_super_csum (char *raw_disk_sb) +{ +struct btrfs_super_block *disk_sb = + (struct btrfs_super_block *)raw_disk_sb; +u16 csum_type =btrfs_super_csum_type (disk_sb); +int ret =0 ; + +if (csum_type ==BTRFS_CSUM_TYPE_CRC32 ) { +u32 crc = ~(u32 )0 ; +const int csum_size =sizeof (crc); +char result[csum_size]; + +/* +* The super_block structure does not span the whole +* BTRFS_SUPER_INFO_SIZE range, we expect that the unused space +* is filled with zeros and is included in the checkum. +*/ + crc =btrfs_csum_data (NULL , raw_disk_sb +BTRFS_CSUM_SIZE , + crc,BTRFS_SUPER_INFO_SIZE -BTRFS_CSUM_SIZE ); +btrfs_csum_final (crc, result); + +if (memcmp (raw_disk_sb, result, csum_size)) + ret =1 ; + +if (ret &&btrfs_super_generation (disk_sb) <10 ) { +fprintf (stderr,"btrfs: super block crcs don't match, " +"older mkfs detected\n" ); + ret =0 ; + } + } + +if (csum_type >=ARRAY_SIZE (btrfs_csum_sizes)) { +fprintf (stderr,"btrfs: unsupported checksum algorithm %u\n" , + csum_type); + ret =1 ; + } + +return ret; +} +int btrfs_read_dev_super (int fd,struct btrfs_super_block *sb,u64 sb_bytenr) {u8 fsid[BTRFS_FSID_SIZE ];int fsid_is_initialized =0 ; -struct btrfs_super_block buf; +char buf[BTRFS_SUPER_INFO_SIZE ]; +struct btrfs_super_block *tmp_sb;int i;int ret;u64 transid =0 ;u64 bytenr;if (sb_bytenr !=BTRFS_SUPER_INFO_OFFSET ) { - ret =pread64 (fd, &buf,sizeof (buf), sb_bytenr); + ret =pread64 (fd, buf,sizeof (buf), sb_bytenr);if (ret <sizeof (buf))return -1 ; -if (btrfs_super_bytenr (&buf) != sb_bytenr || - buf.magic !=cpu_to_le64 (BTRFS_MAGIC )) + tmp_sb = (struct btrfs_super_block *)buf; + +if (btrfs_super_bytenr (tmp_sb) != sb_bytenr || + tmp_sb->magic !=cpu_to_le64 (BTRFS_MAGIC ) || +btrfs_check_super_csum (buf))return -1 ; -memcpy (sb, &buf,sizeof (*sb)); +memcpy (sb, buf,sizeof (*sb));return 0 ; }for (i =0 ; i <BTRFS_SUPER_MIRROR_MAX ; i++) { bytenr =btrfs_sb_offset (i); - ret =pread64 (fd, &buf,sizeof (buf), bytenr); + ret =pread64 (fd, buf,sizeof (buf), bytenr);if (ret <sizeof (buf))break ; -if (btrfs_super_bytenr (&buf) != bytenr ) + tmp_sb = (struct btrfs_super_block *)buf; + +if (btrfs_super_bytenr (tmp_sb) != bytenr )continue ;/* if magic is NULL, the device was removed */ -if (buf.magic ==0 && i ==0 ) +if (tmp_sb->magic ==0 && i ==0 )return -1 ; -if (buf.magic !=cpu_to_le64 (BTRFS_MAGIC )) +if (tmp_sb->magic !=cpu_to_le64 (BTRFS_MAGIC )) +continue ; +if (btrfs_check_super_csum (buf))continue ;if (!fsid_is_initialized) { -memcpy (fsid, buf.fsid,sizeof (fsid)); +memcpy (fsid, tmp_sb->fsid,sizeof (fsid)); fsid_is_initialized =1 ; - }else if (memcmp (fsid, buf.fsid,sizeof (fsid))) { + }else if (memcmp (fsid, tmp_sb->fsid,sizeof (fsid))) {/* * the superblocks (the original one and * its backups) contain data of different
@@ -1134,9 +1182,9 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)continue ; } -if (btrfs_super_generation (&buf) > transid) { -memcpy (sb, &buf,sizeof (*sb)); - transid =btrfs_super_generation (&buf); +if (btrfs_super_generation (tmp_sb) > transid) { +memcpy (sb, buf,sizeof (*sb)); + transid =btrfs_super_generation (tmp_sb); } }
Signature · 6 lines
1.7.9.5 -- 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
Re: [PATCH 5/5] Btrfs-progs: Validate super block checksum
Ping.
Is there any reason why the btrfs progs (except for btrfs-show-super) don't validate the super block's checksum?
thanks
Quoted 139 lines · Filipe David Borba Manana
After finding a super block in a device also validate its checksum. This validation is done in the kernel but it was missing in btrfs-progs.
The function
btrfs_check_super_csum()is imported from the filefs/btrfs/disk-io.cin the kernel source tree.Signed-off-by Filipe David Borba Manana <[email protected]>--- disk-io.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 14 deletions(-)disk-io.c +62
-15@@ -1085,47 +1085,95 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,return info->fs_root; } +static int btrfs_check_super_csum (char *raw_disk_sb) +{ +struct btrfs_super_block *disk_sb = + (struct btrfs_super_block *)raw_disk_sb; +u16 csum_type =btrfs_super_csum_type (disk_sb); +int ret =0 ; + +if (csum_type ==BTRFS_CSUM_TYPE_CRC32 ) { +u32 crc = ~(u32 )0 ; +const int csum_size =sizeof (crc); +char result[csum_size]; + +/* +* The super_block structure does not span the whole +* BTRFS_SUPER_INFO_SIZE range, we expect that the unused space +* is filled with zeros and is included in the checkum. +*/ + crc =btrfs_csum_data (NULL , raw_disk_sb +BTRFS_CSUM_SIZE , + crc,BTRFS_SUPER_INFO_SIZE -BTRFS_CSUM_SIZE ); +btrfs_csum_final (crc, result); + +if (memcmp (raw_disk_sb, result, csum_size)) + ret =1 ; + +if (ret &&btrfs_super_generation (disk_sb) <10 ) { +fprintf (stderr,"btrfs: super block crcs don't match, " +"older mkfs detected\n" ); + ret =0 ; + } + } + +if (csum_type >=ARRAY_SIZE (btrfs_csum_sizes)) { +fprintf (stderr,"btrfs: unsupported checksum algorithm %u\n" , + csum_type); + ret =1 ; + } + +return ret; +} +int btrfs_read_dev_super (int fd,struct btrfs_super_block *sb,u64 sb_bytenr) {u8 fsid[BTRFS_FSID_SIZE ];int fsid_is_initialized =0 ; -struct btrfs_super_block buf; +char buf[BTRFS_SUPER_INFO_SIZE ]; +struct btrfs_super_block *tmp_sb;int i;int ret;u64 transid =0 ;u64 bytenr;if (sb_bytenr !=BTRFS_SUPER_INFO_OFFSET ) { - ret =pread64 (fd, &buf,sizeof (buf), sb_bytenr); + ret =pread64 (fd, buf,sizeof (buf), sb_bytenr);if (ret <sizeof (buf))return -1 ; -if (btrfs_super_bytenr (&buf) != sb_bytenr || - buf.magic !=cpu_to_le64 (BTRFS_MAGIC )) + tmp_sb = (struct btrfs_super_block *)buf; + +if (btrfs_super_bytenr (tmp_sb) != sb_bytenr || + tmp_sb->magic !=cpu_to_le64 (BTRFS_MAGIC ) || +btrfs_check_super_csum (buf))return -1 ; -memcpy (sb, &buf,sizeof (*sb)); +memcpy (sb, buf,sizeof (*sb));return 0 ; }for (i =0 ; i <BTRFS_SUPER_MIRROR_MAX ; i++) { bytenr =btrfs_sb_offset (i); - ret =pread64 (fd, &buf,sizeof (buf), bytenr); + ret =pread64 (fd, buf,sizeof (buf), bytenr);if (ret <sizeof (buf))break ; -if (btrfs_super_bytenr (&buf) != bytenr ) + tmp_sb = (struct btrfs_super_block *)buf; + +if (btrfs_super_bytenr (tmp_sb) != bytenr )continue ;/* if magic is NULL, the device was removed */ -if (buf.magic ==0 && i ==0 ) +if (tmp_sb->magic ==0 && i ==0 )return -1 ; -if (buf.magic !=cpu_to_le64 (BTRFS_MAGIC )) +if (tmp_sb->magic !=cpu_to_le64 (BTRFS_MAGIC )) +continue ; +if (btrfs_check_super_csum (buf))continue ;if (!fsid_is_initialized) { -memcpy (fsid, buf.fsid,sizeof (fsid)); +memcpy (fsid, tmp_sb->fsid,sizeof (fsid)); fsid_is_initialized =1 ; - }else if (memcmp (fsid, buf.fsid,sizeof (fsid))) { + }else if (memcmp (fsid, tmp_sb->fsid,sizeof (fsid))) {/* * the superblocks (the original one and * its backups) contain data of different @@ -1134,9 +1182,9 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)continue ; } -if (btrfs_super_generation (&buf) > transid) { -memcpy (sb, &buf,sizeof (*sb)); - transid =btrfs_super_generation (&buf); +if (btrfs_super_generation (tmp_sb) > transid) { +memcpy (sb, buf,sizeof (*sb)); + transid =btrfs_super_generation (tmp_sb); } } --1.7.9.5
Signature · 5 lines
Filipe David Manana, "Reasonable men adapt themselves to the world. Unreasonable men adapt the world to themselves. That's why all progress depends on unreasonable men."
Re: [PATCH 5/5] Btrfs-progs: Validate super block checksum
Quoted 2 lines · Filipe David Manana
Is there any reason why the btrfs progs (except for btrfs-show-super) don't validate the super block's checksum?
btrfsck accepts broken filesystems, so open_ctree_broken should possibly detect but skip the checksum mismatch.
david
Re: [PATCH 5/5] Btrfs-progs: Validate super block checksum
Quoted 6 lines · David Sterba
Quoted 2 lines · Filipe David Manana
Is there any reason why the btrfs progs (except for btrfs-show-super) don't validate the super block's checksum?
btrfsck accepts broken filesystems, so
open_ctree_brokenshould possibly detect but skip the checksum mismatch.
So, when running btrfsck just report if there's a super block checksum mismatch (like the patch does) but use it (don't skip it). For any other tools in btrfs-progs just report the mismatch and skip to the next super block (like what the patch does now) ?
thanks
Quoted 2 lines
david
--
Filipe David Manana,
"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves. That's why all progress depends on unreasonable men."
PATCH v2 1/5 Btrfs-progs: fix closing of devices
In reply to Filipe David Borba MananaIf a device could not be opened in volumes.c:read_one_dev(), a btrfs_device instance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tried to sync, fadvise and close the (invalid) fd of the device, and kfree() its name and label, which pointed to random memory locations.
Thread 1 (Thread 0x7f0a3d2d1740 (LWP 23585)):
#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295
--- disk-io.c | 4 ++-- volumes.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
disk-io.c +2 -2
@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >=0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);
volumes.c +3 -2
@@ -116,6 +116,7 @@ static int device_list_add(const char *path,/* we can safely leave the fs_devices entry around */ return -ENOMEM ; } + device->fd =-1 ; device->devid = devid;memcpy (device->uuid, disk_super->dev_item.uuid,BTRFS_UUID_SIZE );
@@ -1628,10 +1629,10 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ; + device->fd =-1 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }
Signature · 6 lines
1.7.9.5 -- 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-
Re: [PATCH 1/5 v2] Btrfs-progs: fix closing of devices
Quoted 8 lines · Filipe David Borba Manana
If a device could not be opened in volumes.c:read_one_dev(), a
btrfs_deviceinstance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tried to sync, fadvise and close the (invalid) fd of the device, andkfree()its name and label, which pointed to random memory locations.
Quoted 6 lines
Thread 1 (Thread
0x7f0a3d2d1740(LWP 23585)):#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295
Good catch! This addresses one of my problems.
thanks,
liubo
Quoted 58 lines
Signed-off-by Filipe David Borba Manana <[email protected]>--- disk-io.c | 4 ++-- volumes.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)disk-io.c +2
-2@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >=0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);volumes.c +3
-2@@ -116,6 +116,7 @@ static int device_list_add(const char *path,/* we can safely leave the fs_devices entry around */ return -ENOMEM ; } + device->fd =-1 ; device->devid = devid;memcpy (device->uuid, disk_super->dev_item.uuid,BTRFS_UUID_SIZE );@@ -1628,10 +1629,10 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ; + device->fd =-1 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }Signature · 6 lines
1.7.9.5 -- 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
Re: [PATCH 1/5 v2] Btrfs-progs: fix closing of devices
In reply to Filipe David Borba MananaQuoted 45 lines · Filipe David Borba Manana
If a device could not be opened in volumes.c:read_one_dev(), a
btrfs_deviceinstance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tried to sync, fadvise and close the (invalid) fd of the device, andkfree()its name and label, which pointed to random memory locations.Thread 1 (Thread
0x7f0a3d2d1740(LWP 23585)):#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295Signed-off-by Filipe David Borba Manana <[email protected]>--- disk-io.c | 4 ++-- volumes.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)disk-io.c +2
-2@@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >=0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);volumes.c +0
-0
volumes.c static int device_list_add(const char *path, · line 119
Quoted 4 lines
volumes.c +1
-0@@ -116,6 +116,7 @@ static int device_list_add(const char *path,/* we can safely leave the fs_devices entry around */ return -ENOMEM ; } + device->fd =-1 ;
One nit here,
I think it's ok without this since if we get the right device->name, it means this device is not missing and its fd will be valid.
thanks,
liubo
Quoted 23 lines
volumes.c +2
-2device->devid = devid;memcpy (device->uuid, disk_super->dev_item.uuid,BTRFS_UUID_SIZE );@@ -1628,10 +1629,10 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ; + device->fd =-1 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }Signature · 6 lines
1.7.9.5 -- 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
Re: [PATCH 1/5 v2] Btrfs-progs: fix closing of devices
Quoted 11 lines · Liu Bo
volumes.c static int device_list_add(const char *path, · line 119
Quoted 7 lines
volumes.c +1
-0@@ -116,6 +116,7 @@ static int device_list_add(const char *path,/* we can safely leave the fs_devices entry around */ return -ENOMEM ; } + device->fd =-1 ;One nit here,
I think it's ok without this since if we get the rightdevice->name, it means this device is not missing and its fd will be valid.
Good point.
Only reason I left it like that, is because besides being more clear (subjective) there seems to be possible to have device.name as non-null but no valid fd - i.e. the open() call in volumes.c:btrfs_open_devices() failed.
thanks
Quoted 3 lines
thanks,
liubo
PATCH v2 1/5 Btrfs-progs: fix closing of devices
In reply to Filipe David Borba MananaIf a device could not be opened in volumes.c:read_one_dev(), a btrfs_device instance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tried to sync, fadvise and close the (invalid) fd of the device, and kfree() its name and label, which pointed to random memory locations.
Thread 1 (Thread 0x7f0a3d2d1740 (LWP 23585)):
#0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=<optimized out>) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=<optimized out>, argv=<optimized out>) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295
v2: Added Liu Bo's review mention.
--- disk-io.c | 4 ++-- volumes.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
disk-io.c +2 -2
@@ -1270,12 +1270,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)while (!list_empty (list)) { device = list_entry(list->next, struct btrfs_device, dev_list);list_del_init (&device->dev_list); -if (device->fd) { +if (device->fd >=0 ) {fsync (device->fd);if (posix_fadvise (device->fd,0 ,0 ,POSIX_FADV_DONTNEED ))fprintf (stderr,"Warning, could not drop caches\n" ); +close (device->fd); } -close (device->fd);kfree (device->name);kfree (device->label);kfree (device);
volumes.c +3 -2
@@ -116,6 +116,7 @@ static int device_list_add(const char *path,/* we can safely leave the fs_devices entry around */ return -ENOMEM ; } + device->fd =-1 ; device->devid = devid;memcpy (device->uuid, disk_super->dev_item.uuid,BTRFS_UUID_SIZE );
@@ -1628,10 +1629,10 @@ static int read_one_dev(struct btrfs_root *root,if (!device) {printk ("warning devid %llu not found already\n" , (unsigned long long )devid); - device =kmalloc (sizeof (*device),GFP_NOFS ); + device =kzalloc (sizeof (*device),GFP_NOFS );if (!device)return -ENOMEM ; - device->total_ios =0 ; + device->fd =-1 ;list_add (&device->dev_list, &root->fs_info->fs_devices->devices); }
Signature · 1 line
1.7.9.5
PATCH v2 5/5 Btrfs-progs: Validate super block checksum
In reply to Filipe David Borba MananaAfter finding a super block in a device also validate its checksum. This validation is done in the kernel but it was missing in btrfs-progs.
The function btrfs_check_super_csum() is imported from the file fs/btrfs/disk-io.c in the kernel source tree.
v2:
When finding the super block for a device ignore checksum validation failures if we're running the btrfsck tool, the restore or check commands of the btrfs tool, btrfs-debug-tree or btrfs-find-root tools. For all cases, print a warning message whenever the checksum validation fails, mentioning the superblock offset and device path.
--- btrfs-debug-tree.c | 2 +- btrfs-find-root.c | 8 ++-- cmds-check.c | 2 +- cmds-device.c | 2 +- cmds-filesystem.c | 2 +- cmds-replace.c | 2 +- cmds-restore.c | 2 +- disk-io.c | 103 ++++++++++++++++++++++++++++++++++++++++------------ disk-io.h | 7 +++- utils.c | 15 ++++---- utils.h | 4 +- volumes.c | 6 ++- volumes.h | 3 +- 13 files changed, 110 insertions(+), 48 deletions(-)
btrfs-debug-tree.c +1 -1
@@ -166,7 +166,7 @@ int main(int ac, char **av)if (ac !=1 )print_usage (); - info =open_ctree_fs_info (av[optind],0 ,0 ,0 ,1 ); + info =open_ctree_fs_info (av[optind],0 ,0 ,0 ,1 ,1 );if (!info) {fprintf (stderr,"unable to open %s\n" , av[optind]);exit (1 );
btrfs-find-root.c +4 -4
@@ -102,7 +102,7 @@ static struct btrfs_root *open_ctree_broken(int fd, const char *device)u64 features; ret =btrfs_scan_one_device (fd, device, &fs_devices, - &total_devs,BTRFS_SUPER_INFO_OFFSET ); + &total_devs,BTRFS_SUPER_INFO_OFFSET ,1 );if (ret) {fprintf (stderr,"No valid Btrfs found on %s\n" , device);
@@ -110,7 +110,7 @@ static struct btrfs_root *open_ctree_broken(int fd, const char *device) }if (total_devs !=1 ) { - ret =btrfs_scan_for_fsid (fs_devices, total_devs,1 ); + ret =btrfs_scan_for_fsid (fs_devices, total_devs,1 ,1 );if (ret) goto out; }
@@ -149,8 +149,8 @@ static struct btrfs_root *open_ctree_broken(int fd, const char *device) fs_info->super_bytenr =BTRFS_SUPER_INFO_OFFSET ; disk_super = fs_info->super_copy; - ret =btrfs_read_dev_super (fs_devices->latest_bdev, - disk_super,BTRFS_SUPER_INFO_OFFSET ); + ret =btrfs_read_dev_super (fs_devices->latest_bdev, device, + disk_super,BTRFS_SUPER_INFO_OFFSET ,1 );if (ret) {printk ("No valid btrfs found\n" ); goto out_devices;
cmds-check.c +1 -1
@@ -5185,7 +5185,7 @@ int cmd_check(int argc, char **argv)return -EBUSY ; } - info =open_ctree_fs_info (argv[optind], bytenr,0 , rw,1 ); + info =open_ctree_fs_info (argv[optind], bytenr,0 , rw,1 ,1 );if (!info) {fprintf (stderr,"Couldn't open file system\n" );return -EIO ;
cmds-device.c +1 -1
@@ -203,7 +203,7 @@ static int cmd_scan_dev(int argc, char **argv)printf ("Scanning for Btrfs filesystems\n" );if (checklist) - ret =btrfs_scan_block_devices (1 ); + ret =btrfs_scan_block_devices (1 ,0 );else ret =btrfs_scan_one_dir ("/dev" ,1 );if (ret){
cmds-filesystem.c +1 -1
@@ -257,7 +257,7 @@ static int cmd_show(int argc, char **argv)usage (cmd_show_usage);if (checklist) - ret =btrfs_scan_block_devices (0 ); + ret =btrfs_scan_block_devices (0 ,0 );else ret =btrfs_scan_one_dir ("/dev" ,0 );
cmds-replace.c +1 -1
@@ -280,7 +280,7 @@ static int cmd_start_replace(int argc, char **argv) goto leave_with_error; } ret =btrfs_scan_one_device (fddstdev, dstdev, &fs_devices_mnt, - &total_devs,BTRFS_SUPER_INFO_OFFSET ); + &total_devs,BTRFS_SUPER_INFO_OFFSET ,0 );if (ret >=0 && !force_using_targetdev) { fprintf(stderr, "Error, target device %s contains filesystem, use '-f' to force overwriting.\n",
cmds-restore.c +1 -1
@@ -842,7 +842,7 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location,for (i = super_mirror; i <BTRFS_SUPER_MIRROR_MAX ; i++) { bytenr =btrfs_sb_offset (i); - fs_info =open_ctree_fs_info (dev, bytenr, root_location,0 ,1 ); + fs_info =open_ctree_fs_info (dev, bytenr, root_location,0 ,1 ,1 );if (fs_info)break ;fprintf (stderr,"Could not open root, trying backup super\n" );
disk-io.c +79 -24
191 lines not on this page · raw message
disk-io.h +5 -2
@@ -52,12 +52,15 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr, int writes);struct btrfs_fs_info *open_ctree_fs_info (const char *filename,u64 sb_bytenr,u64 root_tree_bytenr, -int writes,int partial); +int writes,int partial, +int ignore_sb_csum);int close_ctree (struct btrfs_root *root);int write_all_supers (struct btrfs_root *root);int write_ctree_super (struct btrfs_trans_handle *trans,struct btrfs_root *root); -int btrfs_read_dev_super (int fd,struct btrfs_super_block *sb,u64 sb_bytenr); +int btrfs_read_dev_super (int fd,const char *path, +struct btrfs_super_block *sb,u64 sb_bytenr, +int ignore_sb_csum);int btrfs_map_bh_to_logical (struct btrfs_root *root,struct extent_buffer *bh,u64 logical); struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
utils.c +8 -7
@@ -923,12 +923,12 @@ int check_mounted_where(int fd, const char *file, char *where, int size,/* scan the initial device */ ret =btrfs_scan_one_device (fd, file, &fs_devices_mnt, - &total_devs,BTRFS_SUPER_INFO_OFFSET ); + &total_devs,BTRFS_SUPER_INFO_OFFSET ,1 ); is_btrfs = (ret >=0 );/* scan other devices */ if (is_btrfs && total_devs >1 ) { -if ((ret =btrfs_scan_for_fsid (fs_devices_mnt, total_devs,1 ))) +if ((ret =btrfs_scan_for_fsid (fs_devices_mnt, total_devs,1 ,1 )))return ret; }
@@ -1085,7 +1085,7 @@ again: } ret =btrfs_scan_one_device (fd, fullpath, &tmp_devices, &num_devices, -BTRFS_SUPER_INFO_OFFSET ); +BTRFS_SUPER_INFO_OFFSET ,0 );if (ret ==0 && run_ioctl >0 ) {btrfs_register_one_device (fullpath); }
@@ -1111,11 +1111,11 @@ fail: }int btrfs_scan_for_fsid (struct btrfs_fs_devices *fs_devices,u64 total_devs, -int run_ioctls) +int run_ioctls,int ignore_sb_csum) {int ret; - ret =btrfs_scan_block_devices (run_ioctls); + ret =btrfs_scan_block_devices (run_ioctls, ignore_sb_csum);if (ret) ret =btrfs_scan_one_dir ("/dev" , run_ioctls);return ret;
@@ -1350,7 +1350,7 @@ int set_label(const char *btrfs_dev, const char *label)set_label_mounted (btrfs_dev, label); } -int btrfs_scan_block_devices (int run_ioctl) +int btrfs_scan_block_devices (int run_ioctl,int ignore_sb_csum) {struct stat st;
@@ -1416,7 +1416,8 @@ scan_again: } ret =btrfs_scan_one_device (fd, fullpath, &tmp_devices, &num_devices, -BTRFS_SUPER_INFO_OFFSET ); +BTRFS_SUPER_INFO_OFFSET , + ignore_sb_csum);if (ret ==0 && run_ioctl >0 ) {btrfs_register_one_device (fullpath); }
utils.h +2 -2
@@ -36,7 +36,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, u64 block_count, u32 io_width, u32 io_align, u32 sectorsize);int btrfs_scan_for_fsid (struct btrfs_fs_devices *fs_devices,u64 total_devs, -int run_ioctls); +int run_ioctls,int ignore_sb_csum);void btrfs_register_one_device (char *fname);int btrfs_scan_one_dir (char *dirname,int run_ioctl);int check_mounted (const char *devicename);
@@ -46,7 +46,7 @@ int btrfs_device_already_in_root(struct btrfs_root *root, int fd, int super_offset);char *pretty_sizes (u64 size);int get_mountpt (char *dev,char *mntpt,size_t size); -int btrfs_scan_block_devices (int run_ioctl); +int btrfs_scan_block_devices (int run_ioctl,int ignore_sb_csum);u64 parse_size (char *s);int open_file_or_dir (const char *fname); int get_device_info(int fd, u64 devid,
volumes.c +4 -2
@@ -213,7 +213,8 @@ fail:int btrfs_scan_one_device (int fd,const char *path,struct btrfs_fs_devices **fs_devices_ret, -u64 *total_devs,u64 super_offset) +u64 *total_devs,u64 super_offset, +int ignore_sb_csum) {struct btrfs_super_block *disk_super;char *buf;
@@ -227,7 +228,8 @@ int btrfs_scan_one_device(int fd, const char *path, goto error; } disk_super = (struct btrfs_super_block *)buf; - ret =btrfs_read_dev_super (fd, disk_super, super_offset); + ret =btrfs_read_dev_super (fd, path, + disk_super, super_offset, ignore_sb_csum);if (ret <0 ) { ret = -EIO ; goto error_brelse;
volumes.h +2 -1
@@ -179,7 +179,8 @@ int btrfs_update_device(struct btrfs_trans_handle *trans, struct btrfs_device *device);int btrfs_scan_one_device (int fd,const char *path,struct btrfs_fs_devices **fs_devices_ret, -u64 *total_devs,u64 super_offset); +u64 *total_devs,u64 super_offset, +int ignore_sb_csum);int btrfs_num_copies (struct btrfs_mapping_tree *map_tree,u64 logical,u64 len);int btrfs_bootstrap_super_map (struct btrfs_mapping_tree *map_tree,struct btrfs_fs_devices *fs_devices);
Signature · 1 line
1.7.9.5