[PATCH 3/4] btrfs-progs: Add more meaningful return value for btrfs_read_dev_super() and corresponding error string.

2014-07-03 Thread Qu Wenruo
Since btrfs_read_dev_super() now can distinguish non-btrfs fs and
corrupted superblock thanks for the newly introduced super csum check,
the return value and corresponding error string should also be updated
to print more meaningful errors for end users.

Signed-off-by: Qu Wenruo quwen...@cn.fujitsu.com
---
 btrfs-find-root.c |  5 -
 chunk-recover.c   | 10 --
 cmds-filesystem.c |  7 ++-
 disk-io.c | 36 ++--
 utils.c   |  5 -
 volumes.c |  4 +---
 6 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/btrfs-find-root.c b/btrfs-find-root.c
index e31a9b5..f3bf452 100644
--- a/btrfs-find-root.c
+++ b/btrfs-find-root.c
@@ -96,7 +96,10 @@ static struct btrfs_root *open_ctree_broken(int fd, const 
char *device)
ret = btrfs_read_dev_super(fs_devices-latest_bdev,
   disk_super, fs_info-super_bytenr, 1);
if (ret) {
-   printk(No valid btrfs found\n);
+   if (ret == -ENOENT)
+   printk(No valid btrfs found\n);
+   if (ret == -EIO)
+   printk(Superblock is corrupted\n);
goto out_devices;
}
 
diff --git a/chunk-recover.c b/chunk-recover.c
index 9baedd7..c8badf9 100644
--- a/chunk-recover.c
+++ b/chunk-recover.c
@@ -1285,7 +1285,10 @@ open_ctree_with_broken_chunk(struct recover_control *rc)
ret = btrfs_read_dev_super(fs_info-fs_devices-latest_bdev,
   disk_super, fs_info-super_bytenr, 1);
if (ret) {
-   fprintf(stderr, No valid btrfs found\n);
+   if (ret == -ENOENT)
+   printk(No valid btrfs found\n);
+   if (ret == -EIO)
+   printk(Superblock is corrupted\n);
goto out_devices;
}
 
@@ -1351,7 +1354,10 @@ static int recover_prepare(struct recover_control *rc, 
char *path)
 
ret = btrfs_read_dev_super(fd, sb, BTRFS_SUPER_INFO_OFFSET, 1);
if (ret) {
-   fprintf(stderr, read super block error\n);
+   if (ret == -ENOENT)
+   printk(No valid btrfs found\n);
+   if (ret == -EIO)
+   printk(Superblock is corrupted\n);
goto fail_free_sb;
}
 
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index d2e46dc..d58397d 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -604,9 +604,14 @@ static int cmd_show(int argc, char **argv)
} else {
ret = dev_to_fsid(search, fsid);
if (ret) {
-   fprintf(stderr,
+   if (ret == -ENOENT)
+   fprintf(stderr,
ERROR: No btrfs on 
%s\n,
search);
+   if (ret == -EIO)
+   fprintf(stderr,
+   Superblock is 
corrupted on %s\n,
+   search);
return 1;
}
uuid_unparse(fsid, uuid_buf);
diff --git a/disk-io.c b/disk-io.c
index 1bd9fae..4cc831b 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -990,7 +990,11 @@ int btrfs_scan_fs_devices(int fd, const char *path,
ret = btrfs_scan_one_device(fd, path, fs_devices,
total_devs, sb_bytenr, super_recover);
if (ret) {
-   fprintf(stderr, No valid Btrfs found on %s\n, path);
+   if (ret == -ENOENT)
+   fprintf(stderr, No valid Btrfs found on %s\n, path);
+   if (ret == -EIO)
+   fprintf(stderr, Superblock is corrupted on %s\n,
+   path);
return ret;
}
 
@@ -1101,7 +1105,10 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, 
const char *path,
else
ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
if (ret) {
-   printk(No valid btrfs found\n);
+   if (ret == -ENOENT)
+   printk(No valid btrfs found\n);
+   if (ret == -EIO)
+   printk(Superblock is corrupted\n);
goto out_devices;
}
 
@@ -1201,11 +1208,11 @@ int btrfs_read_dev_super(int fd, struct 
btrfs_super_block *sb, u64 sb_bytenr,
if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
ret = pread64(fd, data, sizeof(data), sb_bytenr);
if (ret  sizeof(data))
-   return -1;
+  

Re: [PATCH 3/4] btrfs-progs: Add more meaningful return value for btrfs_read_dev_super() and corresponding error string.

2014-07-03 Thread David Sterba
On Thu, Jul 03, 2014 at 05:36:37PM +0800, Qu Wenruo wrote:
 --- a/btrfs-find-root.c
 +++ b/btrfs-find-root.c
 @@ -96,7 +96,10 @@ static struct btrfs_root *open_ctree_broken(int fd, const 
 char *device)
   ret = btrfs_read_dev_super(fs_devices-latest_bdev,
  disk_super, fs_info-super_bytenr, 1);
   if (ret) {
 - printk(No valid btrfs found\n);
 + if (ret == -ENOENT)
 + printk(No valid btrfs found\n);

Please use fprintf(stderr, ...) for error messages, until we have a
better message logging helpers.
--
To unsubscribe from this list: send the line unsubscribe linux-btrfs in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html