Skip to contentSkip to conversation

btrfs-progs: reserve < 0 return value for errnos

PATCH Superseded Anand Jain 3 participants 5 messages Oct 27, 2017 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

PATCH 1/1 btrfs-progs: reserve < 0 return value for errnos

btrfs_read_dev_super() returns -1 upon not finding a suitable SB, change that to return 1 instead, so that it can reserve the < 0 values for the errno communications.

---
An independent patch, not related to any recent patch sent to ML.

 disk-io.c | 2 +-
 utils.c   | 2 +-
 volumes.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
disk-io.c +1 -1
index f5edc4796619..ba87bb4ce6da 100644
@@ -1491,7 +1491,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 		}
 	}
 
-	return transid > 0 ? 0 : -1;
+	return transid > 0 ? 0 : 1;
 }
 
 static int write_dev_supers(struct btrfs_fs_info *fs_info,
utils.c +1 -1
index 524f463d3140..7574cda8151c 100644
@@ -1678,7 +1678,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		disk_super = (struct btrfs_super_block *)buf;
 		ret = btrfs_read_dev_super(fd, disk_super,
 					   BTRFS_SUPER_INFO_OFFSET, 0);
-		if (ret < 0) {
+		if (ret) {
 			ret = -EIO;
 			goto out;
 		}
volumes.c +1 -1
index 2209e5a9100b..87008bfdd586 100644
@@ -268,7 +268,7 @@ int btrfs_scan_one_device(int fd, const char *path,
 
 	disk_super = (struct btrfs_super_block *)buf;
 	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
-	if (ret < 0)
+	if (ret)
 		return -EIO;
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
Signature · 1 line
2.13.1
#1 lore raw
Quoted 5 lines · Anand Jain

btrfs_read_dev_super() returns -1 upon not finding a suitable SB, change that to return 1 instead, so that it can reserve the < 0 values for the errno communications.

I believe this is buggy since it will be masking errors. We call btrfs_read_dev_super in open_ctree_with_broken_chunk if the function returns 1 we eventually exit as: return ERR_PTR(ret); And then perform IS_ERR on the returned value. IS_ERR is essentially:
((x) >= (unsigned long)-MAX_ERRNO) where x could be 1 and MAX_ERRNO is 4095

So you just broke all functions which return the ret of btrfs_read_dev_super as ERR_PTR.

NAK

Quoted 48 lines

---
An independent patch, not related to any recent patch sent to ML.

 disk-io.c | 2 +-
 utils.c   | 2 +-
 volumes.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
disk-io.c +1 -1
index f5edc4796619..ba87bb4ce6da 100644
@@ -1491,7 +1491,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 		}
 	}
 
-	return transid > 0 ? 0 : -1;
+	return transid > 0 ? 0 : 1;
 }
 
 static int write_dev_supers(struct btrfs_fs_info *fs_info,
utils.c +1 -1
index 524f463d3140..7574cda8151c 100644
@@ -1678,7 +1678,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		disk_super = (struct btrfs_super_block *)buf;
 		ret = btrfs_read_dev_super(fd, disk_super,
 					   BTRFS_SUPER_INFO_OFFSET, 0);
-		if (ret < 0) {
+		if (ret) {
 			ret = -EIO;
 			goto out;
 		}
volumes.c +1 -1
index 2209e5a9100b..87008bfdd586 100644
@@ -268,7 +268,7 @@ int btrfs_scan_one_device(int fd, const char *path,
 
 	disk_super = (struct btrfs_super_block *)buf;
 	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
-	if (ret < 0)
+	if (ret)
 		return -EIO;
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
#2 lore raw
Quoted 15 lines · Nikolay Borisov
Quoted 5 lines · Anand Jain

btrfs_read_dev_super() returns -1 upon not finding a suitable SB, change that to return 1 instead, so that it can reserve the < 0 values for the errno communications.

I believe this is buggy since it will be masking errors. We call btrfs_read_dev_super in open_ctree_with_broken_chunk if the function returns 1 we eventually exit as: return ERR_PTR(ret); And then perform IS_ERR on the returned value. IS_ERR is essentially:
((x) >= (unsigned long)-MAX_ERRNO) where x could be 1 and MAX_ERRNO is 4095

Quoted 5 lines

So you just broke all functions which return the ret of btrfs_read_dev_super as ERR_PTR.

NAK

This doesn't looks good to me either.

No suitable superblock is already an error.
Why not just returning -ENOENT and let caller who really needs to distinguish this to catch that -ENOENT?

Thanks,
Qu

Quoted 54 lines
Quoted 41 lines

---
An independent patch, not related to any recent patch sent to ML.

 disk-io.c | 2 +-
 utils.c   | 2 +-
 volumes.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
disk-io.c +1 -1
index f5edc4796619..ba87bb4ce6da 100644
@@ -1491,7 +1491,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 		}
 	}
 
-	return transid > 0 ? 0 : -1;
+	return transid > 0 ? 0 : 1;
 }
 
 static int write_dev_supers(struct btrfs_fs_info *fs_info,
utils.c +1 -1
index 524f463d3140..7574cda8151c 100644
@@ -1678,7 +1678,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		disk_super = (struct btrfs_super_block *)buf;
 		ret = btrfs_read_dev_super(fd, disk_super,
 					   BTRFS_SUPER_INFO_OFFSET, 0);
-		if (ret < 0) {
+		if (ret) {
 			ret = -EIO;
 			goto out;
 		}
volumes.c +0 -0
index 2209e5a9100b..87008bfdd586 100644

volumes.c int btrfs_scan_one_device(int fd, const char *path, · line 274

Quoted 8 lines
volumes.c +1 -1
@@ -268,7 +268,7 @@ int btrfs_scan_one_device(int fd, const char *path,
 
 	disk_super = (struct btrfs_super_block *)buf;
 	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
-	if (ret < 0)
+	if (ret)
 		return -EIO;
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)

--
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

author
#3 lore raw
Quoted 22 lines · Qu Wenruo
Quoted 19 lines · Nikolay Borisov
Quoted 5 lines · Anand Jain

btrfs_read_dev_super() returns -1 upon not finding a suitable SB, change that to return 1 instead, so that it can reserve the < 0 values for the errno communications.

I believe this is buggy since it will be masking errors. We call btrfs_read_dev_super in open_ctree_with_broken_chunk if the function returns 1 we eventually exit as: return ERR_PTR(ret); And then perform IS_ERR on the returned value. IS_ERR is essentially:
((x) >= (unsigned long)-MAX_ERRNO) where x could be 1 and MAX_ERRNO is 4095

So you just broke all functions which return the ret of btrfs_read_dev_super as ERR_PTR.

Quoted 7 lines
Quoted 1 line

NAK

This doesn't looks good to me either.

No suitable superblock is already an error.
Why not just returning -ENOENT and let caller who really needs to distinguish this to catch that -ENOENT?

Thanks for pointing out. Will fix it. IMO, -ENOENT is good for now.

(Though the actual cleanup would be to..
There are two parts in btrfs_read_dev_super() one to read only the BTRFS_SUPER_INFO_OFFSET SB. And the other to pick the latest SB by generation #. So splitting them two separate functions will let us know where do we use what in a much clearer way. And for 2nd pread64 we should probably use errno.)

Thanks, Anand

Quoted 58 lines

Thanks,
Qu

Quoted 54 lines
Quoted 41 lines

---
An independent patch, not related to any recent patch sent to ML.

  disk-io.c | 2 +-
  utils.c   | 2 +-
  volumes.c | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
disk-io.c +1 -1
index f5edc4796619..ba87bb4ce6da 100644
@@ -1491,7 +1491,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
  		}
  	}
 
-	return transid > 0 ? 0 : -1;
+	return transid > 0 ? 0 : 1;
  }
 
  static int write_dev_supers(struct btrfs_fs_info *fs_info,
utils.c +1 -1
index 524f463d3140..7574cda8151c 100644
@@ -1678,7 +1678,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
  		disk_super = (struct btrfs_super_block *)buf;
  		ret = btrfs_read_dev_super(fd, disk_super,
  					   BTRFS_SUPER_INFO_OFFSET, 0);
-		if (ret < 0) {
+		if (ret) {
  			ret = -EIO;
  			goto out;
  		}
volumes.c +0 -0
index 2209e5a9100b..87008bfdd586 100644

volumes.c int btrfs_scan_one_device(int fd, const char *path, · line 274

Quoted 8 lines
volumes.c +1 -1
@@ -268,7 +268,7 @@ int btrfs_scan_one_device(int fd, const char *path,
 
  	disk_super = (struct btrfs_super_block *)buf;
  	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
-	if (ret < 0)
+	if (ret)
  		return -EIO;
  	devid = btrfs_stack_device_id(&disk_super->dev_item);
  	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)

--
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

author
#4 lore raw

PATCH v2 1/1 btrfs-progs: reserve < 0 return value for errnos

In reply to Anand Jain

btrfs_read_dev_super() returns -1 / -EPERM upon not finding a suitable SB, change that to return -ENOENT instead, which is much closer to the actual possible errors.

---
v2: use -ENOENT instead of 1.

 disk-io.c | 2 +-
 utils.c   | 2 +-
 volumes.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
disk-io.c +1 -1
index f5edc4796619..dcaaea898a3f 100644
@@ -1491,7 +1491,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 		}
 	}
 
-	return transid > 0 ? 0 : -1;
+	return transid > 0 ? 0 : -ENOENT;
 }
 
 static int write_dev_supers(struct btrfs_fs_info *fs_info,
utils.c +1 -1
index 524f463d3140..7574cda8151c 100644
@@ -1678,7 +1678,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		disk_super = (struct btrfs_super_block *)buf;
 		ret = btrfs_read_dev_super(fd, disk_super,
 					   BTRFS_SUPER_INFO_OFFSET, 0);
-		if (ret < 0) {
+		if (ret) {
 			ret = -EIO;
 			goto out;
 		}
volumes.c +1 -1
index 2209e5a9100b..87008bfdd586 100644
@@ -268,7 +268,7 @@ int btrfs_scan_one_device(int fd, const char *path,
 
 	disk_super = (struct btrfs_super_block *)buf;
 	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
-	if (ret < 0)
+	if (ret)
 		return -EIO;
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
Signature · 1 line
2.13.1