diff mbox

[1/2] btrfs-progs: Avoid reading bad fd in case of missing device.

Message ID 1440127287-21368-1-git-send-email-quwenruo@cn.fujitsu.com (mailing list archive)
State Accepted
Headers show

Commit Message

Qu Wenruo Aug. 21, 2015, 3:21 a.m. UTC
Offline btrfs tools, like btrfs-image, will infinitely loop when there
is missing device.

The reason is, for missing device, it's fd will be set to -1, but before
we reading, we only check the fd validation by checking if it's 0.
So in that case, -1 will pass the validation check, and cause pread to
return 0, and loop to read.

Just change the validation check from "== 0" to "<= 0" to avoid such
problem.

Reported-by: Timothy Normand Miller <theosib@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfs-image.c | 2 +-
 disk-io.c     | 4 ++--
 extent_io.c   | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

Comments

David Sterba Aug. 25, 2015, 4:24 p.m. UTC | #1
On Fri, Aug 21, 2015 at 11:21:26AM +0800, Qu Wenruo wrote:
> Offline btrfs tools, like btrfs-image, will infinitely loop when there
> is missing device.
> 
> The reason is, for missing device, it's fd will be set to -1, but before
> we reading, we only check the fd validation by checking if it's 0.
> So in that case, -1 will pass the validation check, and cause pread to
> return 0, and loop to read.
> 
> Just change the validation check from "== 0" to "<= 0" to avoid such
> problem.
> 
> Reported-by: Timothy Normand Miller <theosib@gmail.com>
> Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>

Applied, thanks. I'll add a test for that.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/btrfs-image.c b/btrfs-image.c
index 3684a05..b225325 100644
--- a/btrfs-image.c
+++ b/btrfs-image.c
@@ -894,7 +894,7 @@  static int read_data_extent(struct metadump_struct *md,
 
 		device = multi->stripes[0].dev;
 
-		if (device->fd == 0) {
+		if (device->fd <= 0) {
 			fprintf(stderr,
 				"Device we need to read from is not open\n");
 			free(multi);
diff --git a/disk-io.c b/disk-io.c
index fdcfd6d..d8ca101 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -266,7 +266,7 @@  int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirr
 			}
 			device = multi->stripes[0].dev;
 
-			if (device->fd == 0) {
+			if (device->fd <= 0) {
 				kfree(multi);
 				return -EIO;
 			}
@@ -385,7 +385,7 @@  int read_extent_data(struct btrfs_root *root, char *data,
 	}
 	device = multi->stripes[0].dev;
 
-	if (device->fd == 0)
+	if (device->fd <= 0)
 		goto err;
 	if (*len > max_len)
 		*len = max_len;
diff --git a/extent_io.c b/extent_io.c
index 5d49710..07695ef 100644
--- a/extent_io.c
+++ b/extent_io.c
@@ -714,7 +714,7 @@  int read_data_from_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
 		device = multi->stripes[0].dev;
 
 		read_len = min(bytes_left, read_len);
-		if (device->fd == 0) {
+		if (device->fd <= 0) {
 			kfree(multi);
 			return -EIO;
 		}
@@ -790,7 +790,7 @@  int write_data_to_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
 			raid_map = NULL;
 		} else while (dev_nr < multi->num_stripes) {
 			device = multi->stripes[dev_nr].dev;
-			if (device->fd == 0) {
+			if (device->fd <= 0) {
 				kfree(multi);
 				return -EIO;
 			}