From 52162700bb59663add809a6465ce2769d80b3664 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 17 Jan 2013 11:54:47 -0800 Subject: btrfs-progs: treat super.magic as an le64 The super block magic is a le64 whose value looks like an unterminated string in memory. The lack of null termination leads to clumsy use of string functions and causes static analysis tools to warn that the string will be unterminated. So let's just treat it as the le64 that it is. Endian wrappers are used on the constant so that they're compiled into run-time constants. Signed-off-by: Zach Brown --- disk-io.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'disk-io.c') diff --git a/disk-io.c b/disk-io.c index 0bf73f05..d3b8c518 100644 --- a/disk-io.c +++ b/disk-io.c @@ -932,8 +932,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) return -1; if (btrfs_super_bytenr(&buf) != sb_bytenr || - strncmp((char *)(&buf.magic), BTRFS_MAGIC, - sizeof(buf.magic))) + buf.magic != cpu_to_le64(BTRFS_MAGIC)) return -1; memcpy(sb, &buf, sizeof(*sb)); @@ -951,8 +950,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) /* if magic is NULL, the device was removed */ if (buf.magic == 0 && i == 0) return -1; - if (strncmp((char *)(&buf.magic), BTRFS_MAGIC, - sizeof(buf.magic))) + if (buf.magic != cpu_to_le64(BTRFS_MAGIC)) continue; if (!fsid_is_initialized) { -- cgit v1.2.3 From ea0ac9416fd13b7e20899d8c7c130e5375171ea6 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 17 Jan 2013 12:06:29 -0800 Subject: btrfs-progs: return error from commit_tree_roots() Errors cow-ing the root block are silently being dropped. This is just a step towards error handling because both the caller and calee assert on errors. Signed-off-by: Zach Brown --- disk-io.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'disk-io.c') diff --git a/disk-io.c b/disk-io.c index d3b8c518..dd06748b 100644 --- a/disk-io.c +++ b/disk-io.c @@ -345,14 +345,17 @@ static int commit_tree_roots(struct btrfs_trans_handle *trans, struct btrfs_root *root; struct list_head *next; struct extent_buffer *eb; + int ret; if (fs_info->readonly) return 0; eb = fs_info->tree_root->node; extent_buffer_get(eb); - btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); + ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); free_extent_buffer(eb); + if (ret) + return ret; while(!list_empty(&fs_info->dirty_cowonly_roots)) { next = fs_info->dirty_cowonly_roots.next; -- cgit v1.2.3 From 7d365c5a87cdb542c204086b4d1d7aa00cd7b09f Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Tue, 22 Jan 2013 15:03:46 -0800 Subject: btrfs-progs: don't write memory after sb to disk struct btrfs_super is about 3.5k but a few writing paths were writing it out as the full 4k BTRFS_SUPER_INFO_SIZE, leaking a few hundred bytes after the super_block onto disk. In practice this meant the memory after super_copy in struct btrfs_fs_info and whatever came after it in the heap. Signed-off-by: Zach Brown --- disk-io.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'disk-io.c') diff --git a/disk-io.c b/disk-io.c index dd06748b..eff49dac 100644 --- a/disk-io.c +++ b/disk-io.c @@ -983,6 +983,10 @@ int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb, u64 bytenr; u32 crc; int i, ret; + void *buf; + + buf = calloc(1, BTRFS_SUPER_INFO_SIZE); + BUG_ON(!buf); if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) { btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr); @@ -991,10 +995,11 @@ int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); btrfs_csum_final(crc, (char *)&sb->csum[0]); - ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, + memcpy(buf, sb, sizeof(*sb)); + ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, root->fs_info->super_bytenr); BUG_ON(ret != BTRFS_SUPER_INFO_SIZE); - return 0; + goto out; } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { @@ -1009,9 +1014,12 @@ int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); btrfs_csum_final(crc, (char *)&sb->csum[0]); - ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr); + memcpy(buf, sb, sizeof(*sb)); + ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr); BUG_ON(ret != BTRFS_SUPER_INFO_SIZE); } +out: + free(buf); return 0; } -- cgit v1.2.3 From dbeedbed2aeb87c9add3c147c46a574b0e34be61 Mon Sep 17 00:00:00 2001 From: Eric Sandeen Date: Thu, 24 Jan 2013 18:18:57 -0600 Subject: btrfs-progs: remove duplicate __setup_root __setup_root() was present in find-root.c as well as disk-io.c. No need for the cut and paste, just use the one in disk-io.c Signed-off-by: Eric Sandeen Signed-off-by: Zach Brown --- disk-io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'disk-io.c') diff --git a/disk-io.c b/disk-io.c index eff49dac..00444fdc 100644 --- a/disk-io.c +++ b/disk-io.c @@ -287,7 +287,7 @@ int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, return 0; } -static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, +int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize, struct btrfs_root *root, struct btrfs_fs_info *fs_info, u64 objectid) { -- cgit v1.2.3