From 5c7454176ef1914d723a62c3d424be1913b51a97 Mon Sep 17 00:00:00 2001
From: Kent Overstreet <kent.overstreet@gmail.com>
Date: Fri, 14 Apr 2017 20:40:50 -0800
Subject: [PATCH] cmd_list improvements; use %m

---
 cmd_assemble.c | 10 ++--------
 cmd_debug.c    | 52 +++++++++++++++++++++++++++++++++++++++++++++-----
 cmd_device.c   |  2 +-
 cmd_migrate.c  | 21 ++++++++++----------
 crypto.c       |  2 +-
 libbcachefs.c  |  2 +-
 linux/blkdev.c |  9 +++------
 tools-util.c   |  2 +-
 tools-util.h   | 10 +++++-----
 9 files changed, 71 insertions(+), 39 deletions(-)

diff --git a/cmd_assemble.c b/cmd_assemble.c
index 5f981d71..735b368a 100644
--- a/cmd_assemble.c
+++ b/cmd_assemble.c
@@ -26,10 +26,7 @@ int cmd_assemble(int argc, char *argv[])
 	for (unsigned i = 1; i < argc; i++)
 	     assemble->devs[i] = (__u64) argv[i];
 
-	int ret = ioctl(bcachectl_open(), BCH_IOCTL_ASSEMBLE, assemble);
-	if (ret < 0)
-		die("BCH_IOCTL_ASSEMBLE error: %s", strerror(errno));
-
+	xioctl(bcachectl_open(), BCH_IOCTL_ASSEMBLE, assemble);
 	return 0;
 }
 
@@ -42,9 +39,6 @@ int cmd_incremental(int argc, char *argv[])
 		.dev = (__u64) argv[1],
 	};
 
-	int ret = ioctl(bcachectl_open(), BCH_IOCTL_INCREMENTAL, &incremental);
-	if (ret < 0)
-		die("BCH_IOCTL_INCREMENTAL error: %s", strerror(errno));
-
+	xioctl(bcachectl_open(), BCH_IOCTL_INCREMENTAL, &incremental);
 	return 0;
 }
diff --git a/cmd_debug.c b/cmd_debug.c
index ef4fa848..d825753d 100644
--- a/cmd_debug.c
+++ b/cmd_debug.c
@@ -13,6 +13,7 @@
 #include "btree_cache.h"
 #include "btree_iter.h"
 #include "buckets.h"
+#include "error.h"
 #include "journal.h"
 #include "super.h"
 
@@ -153,7 +154,7 @@ int cmd_dump(int argc, char *argv[])
 }
 
 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
-		      struct bpos start, struct bpos end, int mode)
+		      struct bpos start, struct bpos end)
 {
 	struct btree_iter iter;
 	struct bkey_s_c k;
@@ -171,7 +172,7 @@ static void list_keys(struct bch_fs *c, enum btree_id btree_id,
 }
 
 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
-			       struct bpos start, struct bpos end, int mode)
+			       struct bpos start, struct bpos end)
 {
 	struct btree_iter iter;
 	struct btree *b;
@@ -187,6 +188,36 @@ static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
 	bch2_btree_iter_unlock(&iter);
 }
 
+static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
+			    struct bpos start, struct bpos end)
+{
+	struct btree_iter iter;
+	struct btree_node_iter node_iter;
+	struct bkey unpacked;
+	struct bkey_s_c k;
+	struct btree *b;
+	char buf[4096];
+
+	for_each_btree_node(&iter, c, btree_id, start, 0, b) {
+		if (bkey_cmp(b->key.k.p, end) > 0)
+			break;
+
+		bch2_print_btree_node(c, b, buf, sizeof(buf));
+		fputs(buf, stdout);
+
+		buf[0] = '\t';
+
+		for_each_btree_node_key_unpack(b, k, &node_iter,
+					       btree_node_is_extents(b),
+					       &unpacked) {
+			bch2_bkey_val_to_text(c, bkey_type(0, btree_id),
+					      buf + 1, sizeof(buf) - 1, k);
+			puts(buf);
+		}
+	}
+	bch2_btree_iter_unlock(&iter);
+}
+
 static struct bpos parse_pos(char *buf)
 {
 	char *s = buf, *field;
@@ -224,6 +255,7 @@ static void list_keys_usage(void)
 static const char * const list_modes[] = {
 	"keys",
 	"formats",
+	"nodes",
 	NULL
 };
 
@@ -241,7 +273,7 @@ int cmd_list(int argc, char *argv[])
 	opts.norecovery	= true;
 	opts.errors	= BCH_ON_ERROR_CONTINUE;
 
-	while ((opt = getopt(argc, argv, "b:s:e:i:m:h")) != -1)
+	while ((opt = getopt(argc, argv, "b:s:e:i:m:fvh")) != -1)
 		switch (opt) {
 		case 'b':
 			btree_id = read_string_list_or_die(optarg,
@@ -263,6 +295,13 @@ int cmd_list(int argc, char *argv[])
 			mode = read_string_list_or_die(optarg,
 						list_modes, "list mode");
 			break;
+		case 'f':
+			opts.fix_errors = FSCK_ERR_YES;
+			opts.norecovery	= false;
+			break;
+		case 'v':
+			opts.verbose_recovery = true;
+			break;
 		case 'h':
 			list_keys_usage();
 			exit(EXIT_SUCCESS);
@@ -277,10 +316,13 @@ int cmd_list(int argc, char *argv[])
 
 	switch (mode) {
 	case 0:
-		list_keys(c, btree_id, start, end, mode);
+		list_keys(c, btree_id, start, end);
 		break;
 	case 1:
-		list_btree_formats(c, btree_id, start, end, mode);
+		list_btree_formats(c, btree_id, start, end);
+		break;
+	case 2:
+		list_nodes_keys(c, btree_id, start, end);
 		break;
 	default:
 		die("Invalid mode");
diff --git a/cmd_device.c b/cmd_device.c
index 2f7a7bc1..d45d18ba 100644
--- a/cmd_device.c
+++ b/cmd_device.c
@@ -119,7 +119,7 @@ int cmd_device_show(int argc, char *argv[])
 		char link[PATH_MAX];
 		if (readlinkat(dirfd(fs.sysfs), entry->d_name,
 			       link, sizeof(link)) < 0)
-			die("readlink error: %s\n", strerror(errno));
+			die("readlink error: %m\n");
 
 		char *dev_name = basename(dirname(link));
 
diff --git a/cmd_migrate.c b/cmd_migrate.c
index 8ce84d65..a18aae10 100644
--- a/cmd_migrate.c
+++ b/cmd_migrate.c
@@ -45,7 +45,7 @@ static char *dev_t_to_path(dev_t dev)
 	free(sysfs_dev);
 
 	if (ret < 0 || ret >= sizeof(link))
-		die("readlink error while looking up block device: %s", strerror(errno));
+		die("readlink error while looking up block device: %m");
 
 	link[ret] = '\0';
 
@@ -222,7 +222,7 @@ static void copy_xattrs(struct bch_fs *c, struct bch_inode_unpacked *dst,
 	char attrs[XATTR_LIST_MAX];
 	ssize_t attrs_size = llistxattr(src, attrs, sizeof(attrs));
 	if (attrs_size < 0)
-		die("listxattr error: %s", strerror(errno));
+		die("listxattr error: %m");
 
 	for (const char *next, *attr = attrs;
 	     attr < attrs + attrs_size;
@@ -233,7 +233,7 @@ static void copy_xattrs(struct bch_fs *c, struct bch_inode_unpacked *dst,
 		ssize_t val_size = lgetxattr(src, attr, val, sizeof(val));
 
 		if (val_size < 0)
-			die("error getting xattr val: %s", strerror(errno));
+			die("error getting xattr val: %m");
 
 		const struct xattr_handler *h = xattr_resolve_name(&attr);
 
@@ -356,7 +356,7 @@ static void copy_link(struct bch_fs *c, struct bch_inode_unpacked *dst,
 {
 	ssize_t ret = readlink(src, buf, sizeof(buf));
 	if (ret < 0)
-		die("readlink error: %s", strerror(errno));
+		die("readlink error: %m");
 
 	write_data(c, dst, 0, buf, round_up(ret, block_bytes(c)));
 }
@@ -428,7 +428,7 @@ static void copy_dir(struct copy_fs_state *s,
 		int fd;
 
 		if (fchdir(src_fd))
-			die("chdir error: %s", strerror(errno));
+			die("chdir error: %m");
 
 		struct stat stat =
 			xfstatat(src_fd, d->d_name, AT_SYMLINK_NOFOLLOW);
@@ -499,7 +499,7 @@ next:
 	}
 
 	if (errno)
-		die("readdir error: %s", strerror(errno));
+		die("readdir error: %m");
 }
 
 static ranges reserve_new_fs_space(const char *file_path, unsigned block_size,
@@ -510,8 +510,8 @@ static ranges reserve_new_fs_space(const char *file_path, unsigned block_size,
 		? open(file_path, O_RDWR|O_CREAT, 0600)
 		: open(file_path, O_RDWR|O_CREAT|O_EXCL, 0600);
 	if (fd < 0)
-		die("Error creating %s for bcachefs metadata: %s",
-		    file_path, strerror(errno));
+		die("Error creating %s for bcachefs metadata: %m",
+		    file_path);
 
 	struct stat statbuf = xfstat(fd);
 
@@ -521,8 +521,7 @@ static ranges reserve_new_fs_space(const char *file_path, unsigned block_size,
 	*bcachefs_inum = statbuf.st_ino;
 
 	if (fallocate(fd, 0, 0, size))
-		die("Error reserving space for bcachefs metadata: %s",
-		    strerror(errno));
+		die("Error reserving space for bcachefs metadata: %m");
 
 	fsync(fd);
 
@@ -581,7 +580,7 @@ static void copy_fs(struct bch_fs *c, int src_fd, const char *src_path,
 		die("error looking up root directory: %s", strerror(-ret));
 
 	if (fchdir(src_fd))
-		die("chdir error: %s", strerror(errno));
+		die("chdir error: %m");
 
 	struct stat stat = xfstat(src_fd);
 	copy_times(c, &root_inode, &stat);
diff --git a/crypto.c b/crypto.c
index c692c59a..f9ac57f7 100644
--- a/crypto.c
+++ b/crypto.c
@@ -109,7 +109,7 @@ void bch2_add_key(struct bch_sb *sb, const char *passphrase)
 	    add_key("user", description,
 		    &passphrase_key, sizeof(passphrase_key),
 		    KEY_SPEC_USER_KEYRING) < 0)
-		die("add_key error: %s", strerror(errno));
+		die("add_key error: %m");
 
 	memzero_explicit(description, strlen(description));
 	free(description);
diff --git a/libbcachefs.c b/libbcachefs.c
index 16bcd0c6..73ea2d13 100644
--- a/libbcachefs.c
+++ b/libbcachefs.c
@@ -186,7 +186,7 @@ struct bch_sb *bch2_format(struct format_opts opts,
 
 	struct timespec now;
 	if (clock_gettime(CLOCK_REALTIME, &now))
-		die("error getting current time: %s", strerror(errno));
+		die("error getting current time: %m");
 
 	sb->time_base_lo	= cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
 	sb->time_precision	= cpu_to_le32(1);
diff --git a/linux/blkdev.c b/linux/blkdev.c
index 93459d0b..c72df60f 100644
--- a/linux/blkdev.c
+++ b/linux/blkdev.c
@@ -23,8 +23,7 @@ int submit_bio_wait(struct bio *bio)
 	if (bio->bi_opf & REQ_PREFLUSH) {
 		ret = fdatasync(bio->bi_bdev->bd_fd);
 		if (ret) {
-			fprintf(stderr, "fsync error: %s\n",
-				strerror(errno));
+			fprintf(stderr, "fsync error: %m\n");
 			return -EIO;
 		}
 	}
@@ -56,16 +55,14 @@ int submit_bio_wait(struct bio *bio)
 	}
 
 	if (ret != bio->bi_iter.bi_size) {
-		fprintf(stderr, "IO error: %li (%s)\n",
-			ret, strerror(errno));
+		fprintf(stderr, "IO error: %li (%m)\n", ret);
 		return -EIO;
 	}
 
 	if (bio->bi_opf & REQ_FUA) {
 		ret = fdatasync(bio->bi_bdev->bd_fd);
 		if (ret) {
-			fprintf(stderr, "fsync error: %s\n",
-				strerror(errno));
+			fprintf(stderr, "fsync error: %m\n");
 			return -EIO;
 		}
 	}
diff --git a/tools-util.c b/tools-util.c
index bd114af3..f48b015b 100644
--- a/tools-util.c
+++ b/tools-util.c
@@ -67,7 +67,7 @@ char *read_file_str(int dirfd, const char *path)
 
 	len = read(fd, buf, len);
 	if (len < 0)
-		die("read error: %s", strerror(errno));
+		die("read error: %m");
 
 	buf[len] = '\0';
 	if (len && buf[len - 1] == '\n')
diff --git a/tools-util.h b/tools-util.h
index 732c5108..8f4e3d73 100644
--- a/tools-util.h
+++ b/tools-util.h
@@ -66,14 +66,14 @@ static inline void xpwrite(int fd, const void *buf, size_t count, off_t offset)
 	ssize_t r = pwrite(fd, buf, count, offset);
 
 	if (r != count)
-		die("write error (ret %zi err %s)", r, strerror(errno));
+		die("write error (ret %zi err %m)", r);
 }
 
 #define xopenat(_dirfd, _path, ...)					\
 ({									\
 	int _fd = openat((_dirfd), (_path), __VA_ARGS__);		\
 	if (_fd < 0)							\
-		die("Error opening %s: %s", (_path), strerror(errno));	\
+		die("Error opening %s: %m", (_path));			\
 	_fd;								\
 })
 
@@ -83,7 +83,7 @@ static inline struct stat xfstatat(int dirfd, const char *path, int flags)
 {
 	struct stat stat;
 	if (fstatat(dirfd, path, &stat, flags))
-		die("stat error: %s", strerror(errno));
+		die("stat error: %m");
 	return stat;
 }
 
@@ -91,14 +91,14 @@ static inline struct stat xfstat(int fd)
 {
 	struct stat stat;
 	if (fstat(fd, &stat))
-		die("stat error: %s", strerror(errno));
+		die("stat error: %m");
 	return stat;
 }
 
 #define xioctl(_fd, _nr, ...)						\
 do {									\
 	if (ioctl((_fd), (_nr), ##__VA_ARGS__))				\
-		die(#_nr " ioctl error: %s", strerror(errno));		\
+		die(#_nr " ioctl error: %m");				\
 } while (0)
 
 enum units {