All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements
@ 2017-04-08  3:03 Darrick J. Wong
  2017-04-08  3:03 ` [PATCH 1/4] xfs_db: don't print arrays off the end of a buffer Darrick J. Wong
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-08  3:03 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

Hi all,

Here are a bunch of patches improving various aspects of btree dumping
in xfs_db.  The first few patches fix up numerous crashes that occur
when the user navigates to a btree block with an unknown magic number.
Instead of blowing up, now we can display the (corrupt) contents for
further analysis.

The last patch in this patchset adds a 'btdump' command, which dumps
whatever metadata subtree is rooted at the current iocursor location.
(For inodes it will default to the data for bmbt if there is one.)  This
makes it much easier to display all records of any btree for debugging
or forensic analysis, because the user no longer has to navigate the
btree manually.

Questions?  Comments?

--D

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/4] xfs_db: don't print arrays off the end of a buffer
  2017-04-08  3:03 [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
@ 2017-04-08  3:03 ` Darrick J. Wong
  2017-04-08  3:03 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-08  3:03 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

Before printing an array, clamp the array count against the size of the
buffer so that we don't print random heap contents.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 db/print.c |   11 +++++++++++
 1 file changed, 11 insertions(+)


diff --git a/db/print.c b/db/print.c
index e31372f..0caad8f 100644
--- a/db/print.c
+++ b/db/print.c
@@ -144,6 +144,17 @@ print_flist_1(
 			if (fl->flags & FL_OKHIGH)
 				count = min(count, fl->high - low + 1);
 			if (fa->prfunc) {
+				int	fsz;
+				int	bitlen;
+
+				/* Don't read an array off the end of the buffer */
+				fsz = fsize(f, iocur_top->data, parentoff, 0);
+				bitlen = iocur_top->len * NBBY;
+				if ((f->flags & FLD_ARRAY) &&
+				    fl->offset + (count * fsz) > bitlen) {
+					count = (bitlen - fl->offset) / fsz;
+				}
+
 				neednl = fa->prfunc(iocur_top->data, fl->offset,
 					count, fa->fmtstr,
 					fsize(f, iocur_top->data, parentoff, 0),


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-08  3:03 [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
  2017-04-08  3:03 ` [PATCH 1/4] xfs_db: don't print arrays off the end of a buffer Darrick J. Wong
@ 2017-04-08  3:03 ` Darrick J. Wong
  2017-04-08 23:19   ` Dave Chinner
  2017-04-08  3:04 ` [PATCH 3/4] xfs_db: complain about dir/attr blocks with bad magics Darrick J. Wong
  2017-04-08  3:04 ` [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump' Darrick J. Wong
  3 siblings, 1 reply; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-08  3:03 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

The function block_to_bt plays an integral role in determining the btree
geometry of a block that we want to manipulate with the debugger.
Normally we use the block magic to find the geometry profile, but if the
magic is bad we'll never find it and return NULL.  The callers of this
function do not check for NULL and crash.

Therefore, if we can't find a geometry profile matching the magic
number, use the iocursor type to guess the profile and scowl about that
to stdout.  This makes it so that even with a corrupt magic we can try
to print the fields instead of crashing the debugger.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 db/btblock.c |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 43 insertions(+), 8 deletions(-)


diff --git a/db/btblock.c b/db/btblock.c
index 835a5f0..ba81645 100644
--- a/db/btblock.c
+++ b/db/btblock.c
@@ -25,6 +25,8 @@
 #include "print.h"
 #include "bit.h"
 #include "init.h"
+#include "io.h"
+#include "output.h"
 
 /*
  * Definition of the possible btree block layouts.
@@ -122,13 +124,49 @@ static struct xfs_db_btree *
 block_to_bt(
 	struct xfs_btree_block	*bb)
 {
-	struct xfs_db_btree *btp = &btrees[0];
+	struct xfs_db_btree	*btp;
+	uint32_t		magic;
+
+	magic = be32_to_cpu((bb)->bb_magic);
+	for (btp = &btrees[0]; btp->magic != 0; btp++)
+		if (magic == btp->magic)
+			return btp;
 
-	do {
-		if (be32_to_cpu((bb)->bb_magic) == btp->magic)
+#define M(a,b) (!xfs_sb_version_hascrc(&mp->m_sb) ? (a) : (b))
+	switch (iocur_top->typ->typnm) {
+	case TYP_BMAPBTA:
+	case TYP_BMAPBTD:
+		magic = M(XFS_BMAP_MAGIC, XFS_BMAP_CRC_MAGIC);
+		break;
+	case TYP_BNOBT:
+		magic = M(XFS_ABTB_MAGIC, XFS_ABTB_CRC_MAGIC);
+		break;
+	case TYP_CNTBT:
+		magic = M(XFS_ABTC_MAGIC, XFS_ABTC_CRC_MAGIC);
+		break;
+	case TYP_INOBT:
+		magic = M(XFS_IBT_MAGIC, XFS_IBT_CRC_MAGIC);
+		break;
+	case TYP_FINOBT:
+		magic = M(XFS_FIBT_MAGIC, XFS_FIBT_CRC_MAGIC);
+		break;
+	case TYP_RMAPBT:
+		magic = M(0, XFS_RMAP_CRC_MAGIC);
+		break;
+	case TYP_REFCBT:
+		magic = M(0, XFS_REFC_CRC_MAGIC);
+		break;
+	default:
+		ASSERT(0);
+	}
+#undef M
+	dbprintf(_("Bad btree magic 0x%x; coercing to %s.\n"),
+		be32_to_cpu((bb)->bb_magic),
+		iocur_top->typ->name);
+
+	for (btp = &btrees[0]; btp->magic != 0; btp++)
+		if (magic == btp->magic)
 			return btp;
-		btp++;
-	} while (btp->magic != 0);
 
 	return NULL;
 }
@@ -193,7 +231,6 @@ btblock_key_offset(
 	int			offset;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level != 0);
 
 	offset = bt->block_len + (idx - 1) * bt->key_len;
 	return bitize(offset);
@@ -214,7 +251,6 @@ btblock_ptr_offset(
 	int			maxrecs;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level != 0);
 
 	maxrecs = btblock_maxrecs(bt, mp->m_sb.sb_blocksize);
 	offset = bt->block_len +
@@ -238,7 +274,6 @@ btblock_rec_offset(
 	int			offset;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level == 0);
 
 	offset = bt->block_len + (idx - 1) * bt->rec_len;
 	return bitize(offset);


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/4] xfs_db: complain about dir/attr blocks with bad magics
  2017-04-08  3:03 [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
  2017-04-08  3:03 ` [PATCH 1/4] xfs_db: don't print arrays off the end of a buffer Darrick J. Wong
  2017-04-08  3:03 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
@ 2017-04-08  3:04 ` Darrick J. Wong
  2017-04-08  3:04 ` [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump' Darrick J. Wong
  3 siblings, 0 replies; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-08  3:04 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

The directory and attribute manipulation code also use the block's magic
number to figure out the real type of the block we're analyzing.  If the
magic doesn't match any of the known magics, we end up with the field
type set to the raw attr3/dir3 type and no child type to descend into.
Since there's no print function, we trip over the assertion about
FTARG_OKEMPTY and crash.  Instead provide a print function that
complains about the bad magic number.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 db/attr.c  |   21 +++++++++++++++++++++
 db/attr.h  |    3 +++
 db/dir2.c  |   21 +++++++++++++++++++++
 db/dir2.h  |    3 +++
 db/field.c |   16 ++++++++--------
 5 files changed, 56 insertions(+), 8 deletions(-)


diff --git a/db/attr.c b/db/attr.c
index e26ac67..68a3116 100644
--- a/db/attr.c
+++ b/db/attr.c
@@ -574,3 +574,24 @@ const struct xfs_buf_ops xfs_attr3_db_buf_ops = {
 	.verify_read = xfs_attr3_db_read_verify,
 	.verify_write = xfs_attr3_db_write_verify,
 };
+
+int
+attr_print_func(
+	void		*obj,
+	int		bit,
+	int		count,
+	char		*fmtstr,
+	int		size,
+	int		arg,
+	int		base,
+	int		array)
+{
+	__be32			magic32;
+	__be16			magic16;
+
+	magic32 = *(__be32 *)obj;
+	magic16 = ((struct xfs_da_blkinfo *)obj)->magic;
+	dbprintf(_("Unknown attribute buffer magic (0x%x:0x%x)!\n"),
+		magic32, magic16);
+	return 0;
+}
diff --git a/db/attr.h b/db/attr.h
index bc3431f..525b144 100644
--- a/db/attr.h
+++ b/db/attr.h
@@ -35,3 +35,6 @@ extern int	attr_leaf_name_size(void *obj, int startoff, int idx);
 extern int	attr_size(void *obj, int startoff, int idx);
 
 extern const struct xfs_buf_ops xfs_attr3_db_buf_ops;
+
+extern int	attr_print_func(void *obj, int bit, int count, char *fmtstr,
+		int size, int arg, int base, int array);
diff --git a/db/dir2.c b/db/dir2.c
index 533f705..3b89330 100644
--- a/db/dir2.c
+++ b/db/dir2.c
@@ -1041,3 +1041,24 @@ const struct xfs_buf_ops xfs_dir3_db_buf_ops = {
 	.verify_read = xfs_dir3_db_read_verify,
 	.verify_write = xfs_dir3_db_write_verify,
 };
+
+int
+dir2_print_func(
+	void		*obj,
+	int		bit,
+	int		count,
+	char		*fmtstr,
+	int		size,
+	int		arg,
+	int		base,
+	int		array)
+{
+	__be32			magic32;
+	__be16			magic16;
+
+	magic32 = *(__be32 *)obj;
+	magic16 = ((struct xfs_da_blkinfo *)obj)->magic;
+	dbprintf(_("Unknown directory buffer magic (0x%x:0x%x)!\n"),
+		magic32, magic16);
+	return 0;
+}
diff --git a/db/dir2.h b/db/dir2.h
index 0c2a62e..6d12cc1 100644
--- a/db/dir2.h
+++ b/db/dir2.h
@@ -62,3 +62,6 @@ extern int	dir2_data_union_size(void *obj, int startoff, int idx);
 extern int	dir2_size(void *obj, int startoff, int idx);
 
 extern const struct xfs_buf_ops xfs_dir3_db_buf_ops;
+
+extern int	dir2_print_func(void *obj, int bit, int count, char *fmtstr,
+		int size, int arg, int base, int array);
diff --git a/db/field.c b/db/field.c
index 1968dd5..5496789 100644
--- a/db/field.c
+++ b/db/field.c
@@ -59,8 +59,8 @@ const ftattr_t	ftattrtab[] = {
 	  FTARG_DONULL, NULL, NULL },
 
 /* attr fields */
-	{ FLDT_ATTR, "attr", NULL, (char *)attr_flds, attr_size, FTARG_SIZE,
-	  NULL, attr_flds },
+	{ FLDT_ATTR, "attr", attr_print_func, (char *)attr_flds, attr_size,
+	  FTARG_SIZE, NULL, attr_flds },
 	{ FLDT_ATTR_BLKINFO, "attr_blkinfo", NULL, (char *)attr_blkinfo_flds,
 	  SI(bitsz(struct xfs_da_blkinfo)), 0, NULL, attr_blkinfo_flds },
 	{ FLDT_ATTR_LEAF_ENTRY, "attr_leaf_entry", fp_sarray,
@@ -89,8 +89,8 @@ const ftattr_t	ftattrtab[] = {
 	  attrshort_size, FTARG_SIZE, NULL, attr_shortform_flds },
 
 /* attr3 specific fields */
-	{ FLDT_ATTR3, "attr3", NULL, (char *)attr3_flds, attr_size, FTARG_SIZE,
-	  NULL, attr3_flds },
+	{ FLDT_ATTR3, "attr3", attr_print_func, (char *)attr3_flds, attr_size,
+	  FTARG_SIZE, NULL, attr3_flds },
 	{ FLDT_ATTR3_LEAF_HDR, "attr3_leaf_hdr", NULL,
 	  (char *)attr3_leaf_hdr_flds, SI(bitsz(struct xfs_attr3_leaf_hdr)),
 	  0, NULL, attr3_leaf_hdr_flds },
@@ -219,8 +219,8 @@ const ftattr_t	ftattrtab[] = {
 	  SI(bitsz(xfs_dinode_t)), 0, NULL, inode_v3_flds },
 
 /* dir v2 fields */
-	{ FLDT_DIR2, "dir2", NULL, (char *)dir2_flds, dir2_size, FTARG_SIZE,
-	  NULL, dir2_flds },
+	{ FLDT_DIR2, "dir2", dir2_print_func, (char *)dir2_flds, dir2_size,
+	  FTARG_SIZE, NULL, dir2_flds },
 	{ FLDT_DIR2_BLOCK_TAIL, "dir2_block_tail", NULL,
 	  (char *)dir2_block_tail_flds, SI(bitsz(xfs_dir2_block_tail_t)), 0,
 	  NULL, dir2_block_tail_flds },
@@ -262,8 +262,8 @@ const ftattr_t	ftattrtab[] = {
 	  FTARG_SIZE, NULL, dir2sf_flds },
 
 /* dir v3 fields */
-	{ FLDT_DIR3, "dir3", NULL, (char *)dir3_flds, dir2_size, FTARG_SIZE,
-	  NULL, dir3_flds },
+	{ FLDT_DIR3, "dir3", dir2_print_func, (char *)dir3_flds, dir2_size,
+	  FTARG_SIZE, NULL, dir3_flds },
 	{ FLDT_DIR3_BLKHDR, "dir3_blk_hdr", NULL, (char *)dir3_blkhdr_flds,
 	  SI(bitsz(struct xfs_dir3_blk_hdr)), 0, NULL, dir3_blkhdr_flds },
 	{ FLDT_DIR3_DATA_HDR, "dir3_data_hdr", NULL, (char *)dir3_data_hdr_flds,


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump'
  2017-04-08  3:03 [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
                   ` (2 preceding siblings ...)
  2017-04-08  3:04 ` [PATCH 3/4] xfs_db: complain about dir/attr blocks with bad magics Darrick J. Wong
@ 2017-04-08  3:04 ` Darrick J. Wong
  2017-04-08 23:11   ` Dave Chinner
  3 siblings, 1 reply; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-08  3:04 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

Introduce a new 'btdump' command that can print the contents of all
blocks of any metadata subtree in the filesystem.  This enables
developers and forensic analyst to view a metadata structure without
having to navigate the btree manually.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 db/Makefile  |    2 
 db/btdump.c  |  272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 db/btdump.h  |   20 ++++
 db/command.c |    2 
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 db/btdump.c
 create mode 100644 db/btdump.h


diff --git a/db/Makefile b/db/Makefile
index cdc0b99..97dd898 100644
--- a/db/Makefile
+++ b/db/Makefile
@@ -8,7 +8,7 @@ include $(TOPDIR)/include/builddefs
 LTCOMMAND = xfs_db
 
 HFILES = addr.h agf.h agfl.h agi.h attr.h attrshort.h bit.h block.h bmap.h \
-	btblock.h bmroot.h check.h command.h convert.h crc.h debug.h \
+	btblock.h btdump.h bmroot.h check.h command.h convert.h crc.h debug.h \
 	dir2.h dir2sf.h dquot.h echo.h faddr.h field.h \
 	flist.h fprint.h frag.h freesp.h hash.h help.h init.h inode.h input.h \
 	io.h logformat.h malloc.h metadump.h output.h print.h quit.h sb.h \
diff --git a/db/btdump.c b/db/btdump.c
new file mode 100644
index 0000000..7824c33
--- /dev/null
+++ b/db/btdump.c
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2017 Oracle.  All Rights Reserved.
+ *
+ * Author: Darrick J. Wong <darrick.wong@oracle.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+#include "libxfs.h"
+#include "command.h"
+#include "output.h"
+#include "init.h"
+#include "io.h"
+#include "type.h"
+#include "btdump.h"
+#include "input.h"
+
+static void
+btdump_help(void)
+{
+	dbprintf(_(
+"\n"
+" 'btdump' dumps the btree rooted at the current io cursor location.\n"
+" In the case of an inode, the data fork bmbt is displayed.\n"
+" Options:\n"
+"   -a -- Display the extended attribute bmbt.\n"
+"   -i -- Print internal btree nodes.\n"
+"\n"
+));
+
+}
+
+static int
+eval(
+	const char	*fmt, ...)
+{
+	va_list		ap;
+	char		buf[PATH_MAX];
+	char		**v;
+	int		c;
+	int		ret;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+
+	v = breakline(buf, &c);
+	ret = command(c, v);
+	free(v);
+	return ret;
+}
+
+static bool
+btblock_has_rightsib(
+	struct xfs_btree_block	*block,
+	bool			long_format)
+{
+	if (long_format)
+		return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
+	return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
+}
+
+static int
+dump_btlevel(
+	int			level,
+	bool			long_format)
+{
+	xfs_daddr_t		orig_daddr = iocur_top->bb;
+	xfs_daddr_t		last_daddr;
+	unsigned int		nr;
+	int			ret;
+
+	ret = eval("push");
+	if (ret)
+		return ret;
+
+	nr = 1;
+	do {
+		last_daddr = iocur_top->bb;
+		dbprintf(_("%s level %u block %u daddr %llu\n"),
+			 iocur_top->typ->name, level, nr, last_daddr);
+		if (level > 0) {
+			ret = eval("print keys");
+			if (ret)
+				goto err;
+			ret = eval("print ptrs");
+		} else {
+			ret = eval("print recs");
+		}
+		if (ret)
+			goto err;
+		if (btblock_has_rightsib(iocur_top->data, long_format)) {
+			ret = eval("addr rightsib");
+			if (ret)
+				goto err;
+		}
+		nr++;
+	} while (iocur_top->bb != orig_daddr && iocur_top->bb != last_daddr);
+
+	ret = eval("pop");
+	return ret;
+err:
+	eval("pop");
+	return ret;
+}
+
+static int
+dump_btree(
+	bool		dump_node_blocks,
+	bool		long_format)
+{
+	xfs_daddr_t	orig_daddr = iocur_top->bb;
+	xfs_daddr_t	last_daddr;
+	int		level;
+	int		ret;
+
+	ret = eval("push");
+	if (ret)
+		return ret;
+
+	cur_agno = XFS_FSB_TO_AGNO(mp, XFS_DADDR_TO_FSB(mp, iocur_top->bb));
+	level = xfs_btree_get_level(iocur_top->data);
+	do {
+		last_daddr = iocur_top->bb;
+		if (level > 0) {
+			if (dump_node_blocks) {
+				ret = dump_btlevel(level, long_format);
+				if (ret)
+					goto err;
+			}
+			ret = eval("addr ptrs[1]");
+		} else {
+			ret = dump_btlevel(level, long_format);
+		}
+		if (ret)
+			goto err;
+		level--;
+	} while (level >= 0 &&
+		 iocur_top->bb != orig_daddr &&
+		 iocur_top->bb != last_daddr);
+
+	ret = eval("pop");
+	return ret;
+err:
+	eval("pop");
+	return ret;
+}
+
+static int
+dump_inode(
+	bool			dump_node_blocks,
+	bool			attrfork)
+{
+	char			*prefix;
+	struct xfs_dinode	*dip;
+	int			ret;
+
+	if (attrfork)
+		prefix = "a.bmbt";
+	else if (xfs_sb_version_hascrc(&mp->m_sb))
+		prefix = "u3.bmbt";
+	else
+		prefix = "u.bmbt";
+
+	dip = iocur_top->data;
+	if (attrfork) {
+		if (!dip->di_anextents ||
+		    dip->di_aformat != XFS_DINODE_FMT_BTREE)
+			return 0;
+	} else {
+		if (!dip->di_nextents ||
+		    dip->di_format != XFS_DINODE_FMT_BTREE)
+			return 0;
+	}
+
+	ret = eval("push");
+	if (ret)
+		return ret;
+
+	if (dump_node_blocks) {
+		ret = eval("print %s.keys", prefix);
+		if (ret)
+			goto err;
+		ret = eval("print %s.ptrs", prefix);
+		if (ret)
+			goto err;
+	}
+
+	ret = eval("addr %s.ptrs[1]", prefix);
+	if (ret)
+		goto err;
+
+	ret = dump_btree(dump_node_blocks, true);
+	if (ret)
+		goto err;
+
+	ret = eval("pop");
+	return ret;
+err:
+	eval("pop");
+	return ret;
+}
+
+static int
+btdump_f(
+	int		argc,
+	char		**argv)
+{
+	bool		aflag = false;
+	bool		iflag = false;
+	int		c;
+
+	if (cur_typ == NULL) {
+		dbprintf(_("no current type\n"));
+		return 0;
+	}
+	while ((c = getopt(argc, argv, "ai")) != EOF) {
+		switch (c) {
+		case 'a':
+			aflag = true;
+			break;
+		case 'i':
+			iflag = true;
+			break;
+		default:
+			dbprintf(_("bad option for btdump command\n"));
+			return 0;
+		}
+	}
+	if (aflag && cur_typ->typnm != TYP_INODE) {
+		dbprintf(_("attrfork flag doesn't apply here\n"));
+		return 0;
+	}
+	switch (cur_typ->typnm) {
+	case TYP_BNOBT:
+	case TYP_CNTBT:
+	case TYP_INOBT:
+	case TYP_FINOBT:
+	case TYP_RMAPBT:
+	case TYP_REFCBT:
+		return dump_btree(iflag, false);
+	case TYP_BMAPBTA:
+	case TYP_BMAPBTD:
+		return dump_btree(iflag, true);
+	case TYP_INODE:
+		return dump_inode(iflag, aflag);
+	default:
+		dbprintf(_("Not a btree.\n"));
+		return 0;
+	}
+}
+
+static const cmdinfo_t btdump_cmd =
+	{ "btdump", "b", btdump_f, 0, 1, 0, "",
+	  N_("dump btree"), btdump_help };
+
+void
+btdump_init(void)
+{
+	add_command(&btdump_cmd);
+}
diff --git a/db/btdump.h b/db/btdump.h
new file mode 100644
index 0000000..45a65aa
--- /dev/null
+++ b/db/btdump.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2017 Oracle.  All Rights Reserved.
+ *
+ * Author: Darrick J. Wong <darrick.wong@oracle.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+extern void	btdump_init(void);
diff --git a/db/command.c b/db/command.c
index 3d7cfd7..c2ed7b4 100644
--- a/db/command.c
+++ b/db/command.c
@@ -51,6 +51,7 @@
 #include "dquot.h"
 #include "fsmap.h"
 #include "crc.h"
+#include "btdump.h"
 
 cmdinfo_t	*cmdtab;
 int		ncmds;
@@ -124,6 +125,7 @@ init_commands(void)
 	attrset_init();
 	block_init();
 	bmap_init();
+	btdump_init();
 	check_init();
 	convert_init();
 	crc_init();


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump'
  2017-04-08  3:04 ` [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump' Darrick J. Wong
@ 2017-04-08 23:11   ` Dave Chinner
  0 siblings, 0 replies; 11+ messages in thread
From: Dave Chinner @ 2017-04-08 23:11 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: sandeen, linux-xfs

On Fri, Apr 07, 2017 at 08:04:07PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> Introduce a new 'btdump' command that can print the contents of all
> blocks of any metadata subtree in the filesystem.  This enables
> developers and forensic analyst to view a metadata structure without
> having to navigate the btree manually.
....

Something I've noticed recently:

> --- /dev/null
> +++ b/db/btdump.h
> @@ -0,0 +1,20 @@
> +/*
> + * Copyright (C) 2017 Oracle.  All Rights Reserved.
> + *
> + * Author: Darrick J. Wong <darrick.wong@oracle.com>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; either version 2
> + * of the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it would be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write the Free Software Foundation,
> + * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
> + */
> +extern void	btdump_init(void);

Adding a new header file just for an extern init function
declarationi is a pretty silly pattern. Just consolidate all the
init function declarations db/init.h, and get rid of all the
one-line headers...

We should really do the same for xfs_io, too...

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-08  3:03 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
@ 2017-04-08 23:19   ` Dave Chinner
  2017-04-10 21:08     ` Darrick J. Wong
  0 siblings, 1 reply; 11+ messages in thread
From: Dave Chinner @ 2017-04-08 23:19 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: sandeen, linux-xfs

On Fri, Apr 07, 2017 at 08:03:55PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> The function block_to_bt plays an integral role in determining the btree
> geometry of a block that we want to manipulate with the debugger.
> Normally we use the block magic to find the geometry profile, but if the
> magic is bad we'll never find it and return NULL.  The callers of this
> function do not check for NULL and crash.
> 
> Therefore, if we can't find a geometry profile matching the magic
> number, use the iocursor type to guess the profile and scowl about that
> to stdout.  This makes it so that even with a corrupt magic we can try
> to print the fields instead of crashing the debugger.

....

> +#define M(a,b) (!xfs_sb_version_hascrc(&mp->m_sb) ? (a) : (b))
> +	switch (iocur_top->typ->typnm) {
> +	case TYP_BMAPBTA:
> +	case TYP_BMAPBTD:
> +		magic = M(XFS_BMAP_MAGIC, XFS_BMAP_CRC_MAGIC);
> +		break;

That's really quite special, Darrick. :P

This:

	(!xfs_sb_version_hascrc(&mp->m_sb) ? (a) : (b))

Could written more simply as:

	(xfs_sb_version_hascrc(&mp->m_sb) ? (b) : (a))

or you could swap the macro args so you only need to drop the
negation.

Even better, though, would be to use a local variable to only
evaluate the CRC status once and kill the macro altogether:

	has_crc = xfs_sb_version_hascrc(&mp->m_sb);
	switch (iocur_top->typ->typnm) {
	case TYP_BMAPBTA:
	case TYP_BMAPBTD:
		magic = has_crc ? XFS_ABTB_CRC_MAGIC : XFS_ABTB_MAGIC;
		break;
.....

-Dave.
-- 
Dave Chinner
david@fromorbit.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-08 23:19   ` Dave Chinner
@ 2017-04-10 21:08     ` Darrick J. Wong
  0 siblings, 0 replies; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-10 21:08 UTC (permalink / raw)
  To: Dave Chinner; +Cc: sandeen, linux-xfs

On Sun, Apr 09, 2017 at 09:19:53AM +1000, Dave Chinner wrote:
> On Fri, Apr 07, 2017 at 08:03:55PM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <darrick.wong@oracle.com>
> > 
> > The function block_to_bt plays an integral role in determining the btree
> > geometry of a block that we want to manipulate with the debugger.
> > Normally we use the block magic to find the geometry profile, but if the
> > magic is bad we'll never find it and return NULL.  The callers of this
> > function do not check for NULL and crash.
> > 
> > Therefore, if we can't find a geometry profile matching the magic
> > number, use the iocursor type to guess the profile and scowl about that
> > to stdout.  This makes it so that even with a corrupt magic we can try
> > to print the fields instead of crashing the debugger.
> 
> ....
> 
> > +#define M(a,b) (!xfs_sb_version_hascrc(&mp->m_sb) ? (a) : (b))
> > +	switch (iocur_top->typ->typnm) {
> > +	case TYP_BMAPBTA:
> > +	case TYP_BMAPBTD:
> > +		magic = M(XFS_BMAP_MAGIC, XFS_BMAP_CRC_MAGIC);
> > +		break;
> 
> That's really quite special, Darrick. :P
> 
> This:
> 
> 	(!xfs_sb_version_hascrc(&mp->m_sb) ? (a) : (b))
> 
> Could written more simply as:
> 
> 	(xfs_sb_version_hascrc(&mp->m_sb) ? (b) : (a))
> 
> or you could swap the macro args so you only need to drop the
> negation.
> 
> Even better, though, would be to use a local variable to only
> evaluate the CRC status once and kill the macro altogether:
> 
> 	has_crc = xfs_sb_version_hascrc(&mp->m_sb);
> 	switch (iocur_top->typ->typnm) {
> 	case TYP_BMAPBTA:
> 	case TYP_BMAPBTD:
> 		magic = has_crc ? XFS_ABTB_CRC_MAGIC : XFS_ABTB_MAGIC;
> 		break;
> .....

Ok, done.

--D

> 
> -Dave.
> -- 
> Dave Chinner
> david@fromorbit.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-11  0:03   ` Dave Chinner
@ 2017-04-11  0:19     ` Darrick J. Wong
  0 siblings, 0 replies; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-11  0:19 UTC (permalink / raw)
  To: Dave Chinner; +Cc: sandeen, linux-xfs

On Tue, Apr 11, 2017 at 10:03:32AM +1000, Dave Chinner wrote:
> On Mon, Apr 10, 2017 at 03:47:53PM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <darrick.wong@oracle.com>
> > 
> > The function block_to_bt plays an integral role in determining the btree
> > geometry of a block that we want to manipulate with the debugger.
> > Normally we use the block magic to find the geometry profile, but if the
> > magic is bad we'll never find it and return NULL.  The callers of this
> > function do not check for NULL and crash.
> > 
> > Therefore, if we can't find a geometry profile matching the magic
> > number, use the iocursor type to guess the profile and scowl about that
> > to stdout.  This makes it so that even with a corrupt magic we can try
> > to print the fields instead of crashing the debugger.
> > 
> > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > ---
> > v2: be less macro-happy and only evaluate hascrc once
> > ---
> >  db/btblock.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
> >  1 file changed, 44 insertions(+), 8 deletions(-)
> > 
> > 
> > diff --git a/db/btblock.c b/db/btblock.c
> > index 835a5f0..e494f3e 100644
> > --- a/db/btblock.c
> > +++ b/db/btblock.c
> > @@ -25,6 +25,8 @@
> >  #include "print.h"
> >  #include "bit.h"
> >  #include "init.h"
> > +#include "io.h"
> > +#include "output.h"
> >  
> >  /*
> >   * Definition of the possible btree block layouts.
> > @@ -122,13 +124,50 @@ static struct xfs_db_btree *
> >  block_to_bt(
> >  	struct xfs_btree_block	*bb)
> >  {
> > -	struct xfs_db_btree *btp = &btrees[0];
> > +	struct xfs_db_btree	*btp;
> > +	uint32_t		magic;
> > +	bool			crc;
> > +
> > +	magic = be32_to_cpu((bb)->bb_magic);
> > +	for (btp = &btrees[0]; btp->magic != 0; btp++)
> > +		if (magic == btp->magic)
> > +			return btp;
> 
> {}

Sure.

--D

> 
> -Dave.
> -- 
> Dave Chinner
> david@fromorbit.com
> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-10 22:47 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
@ 2017-04-11  0:03   ` Dave Chinner
  2017-04-11  0:19     ` Darrick J. Wong
  0 siblings, 1 reply; 11+ messages in thread
From: Dave Chinner @ 2017-04-11  0:03 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: sandeen, linux-xfs

On Mon, Apr 10, 2017 at 03:47:53PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> The function block_to_bt plays an integral role in determining the btree
> geometry of a block that we want to manipulate with the debugger.
> Normally we use the block magic to find the geometry profile, but if the
> magic is bad we'll never find it and return NULL.  The callers of this
> function do not check for NULL and crash.
> 
> Therefore, if we can't find a geometry profile matching the magic
> number, use the iocursor type to guess the profile and scowl about that
> to stdout.  This makes it so that even with a corrupt magic we can try
> to print the fields instead of crashing the debugger.
> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
> v2: be less macro-happy and only evaluate hascrc once
> ---
>  db/btblock.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 44 insertions(+), 8 deletions(-)
> 
> 
> diff --git a/db/btblock.c b/db/btblock.c
> index 835a5f0..e494f3e 100644
> --- a/db/btblock.c
> +++ b/db/btblock.c
> @@ -25,6 +25,8 @@
>  #include "print.h"
>  #include "bit.h"
>  #include "init.h"
> +#include "io.h"
> +#include "output.h"
>  
>  /*
>   * Definition of the possible btree block layouts.
> @@ -122,13 +124,50 @@ static struct xfs_db_btree *
>  block_to_bt(
>  	struct xfs_btree_block	*bb)
>  {
> -	struct xfs_db_btree *btp = &btrees[0];
> +	struct xfs_db_btree	*btp;
> +	uint32_t		magic;
> +	bool			crc;
> +
> +	magic = be32_to_cpu((bb)->bb_magic);
> +	for (btp = &btrees[0]; btp->magic != 0; btp++)
> +		if (magic == btp->magic)
> +			return btp;

{}

-Dave.
-- 
Dave Chinner
david@fromorbit.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic
  2017-04-10 22:47 [PATCH v2 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
@ 2017-04-10 22:47 ` Darrick J. Wong
  2017-04-11  0:03   ` Dave Chinner
  0 siblings, 1 reply; 11+ messages in thread
From: Darrick J. Wong @ 2017-04-10 22:47 UTC (permalink / raw)
  To: sandeen, darrick.wong; +Cc: linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

The function block_to_bt plays an integral role in determining the btree
geometry of a block that we want to manipulate with the debugger.
Normally we use the block magic to find the geometry profile, but if the
magic is bad we'll never find it and return NULL.  The callers of this
function do not check for NULL and crash.

Therefore, if we can't find a geometry profile matching the magic
number, use the iocursor type to guess the profile and scowl about that
to stdout.  This makes it so that even with a corrupt magic we can try
to print the fields instead of crashing the debugger.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
v2: be less macro-happy and only evaluate hascrc once
---
 db/btblock.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 44 insertions(+), 8 deletions(-)


diff --git a/db/btblock.c b/db/btblock.c
index 835a5f0..e494f3e 100644
--- a/db/btblock.c
+++ b/db/btblock.c
@@ -25,6 +25,8 @@
 #include "print.h"
 #include "bit.h"
 #include "init.h"
+#include "io.h"
+#include "output.h"
 
 /*
  * Definition of the possible btree block layouts.
@@ -122,13 +124,50 @@ static struct xfs_db_btree *
 block_to_bt(
 	struct xfs_btree_block	*bb)
 {
-	struct xfs_db_btree *btp = &btrees[0];
+	struct xfs_db_btree	*btp;
+	uint32_t		magic;
+	bool			crc;
+
+	magic = be32_to_cpu((bb)->bb_magic);
+	for (btp = &btrees[0]; btp->magic != 0; btp++)
+		if (magic == btp->magic)
+			return btp;
 
-	do {
-		if (be32_to_cpu((bb)->bb_magic) == btp->magic)
+	crc = xfs_sb_version_hascrc(&mp->m_sb);
+	switch (iocur_top->typ->typnm) {
+	case TYP_BMAPBTA:
+	case TYP_BMAPBTD:
+		magic = crc ? XFS_BMAP_CRC_MAGIC : XFS_BMAP_MAGIC;
+		break;
+	case TYP_BNOBT:
+		magic = crc ? XFS_ABTB_CRC_MAGIC : XFS_ABTB_MAGIC;
+		break;
+	case TYP_CNTBT:
+		magic = crc ? XFS_ABTC_CRC_MAGIC : XFS_ABTC_MAGIC;
+		break;
+	case TYP_INOBT:
+		magic = crc ? XFS_IBT_CRC_MAGIC : XFS_IBT_MAGIC;
+		break;
+	case TYP_FINOBT:
+		magic = crc ? XFS_FIBT_CRC_MAGIC : XFS_FIBT_MAGIC;
+		break;
+	case TYP_RMAPBT:
+		magic = crc ? XFS_RMAP_CRC_MAGIC : 0;
+		break;
+	case TYP_REFCBT:
+		magic = crc ? XFS_REFC_CRC_MAGIC : 0;
+		break;
+	default:
+		ASSERT(0);
+	}
+
+	dbprintf(_("Bad btree magic 0x%x; coercing to %s.\n"),
+		be32_to_cpu((bb)->bb_magic),
+		iocur_top->typ->name);
+
+	for (btp = &btrees[0]; btp->magic != 0; btp++)
+		if (magic == btp->magic)
 			return btp;
-		btp++;
-	} while (btp->magic != 0);
 
 	return NULL;
 }
@@ -193,7 +232,6 @@ btblock_key_offset(
 	int			offset;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level != 0);
 
 	offset = bt->block_len + (idx - 1) * bt->key_len;
 	return bitize(offset);
@@ -214,7 +252,6 @@ btblock_ptr_offset(
 	int			maxrecs;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level != 0);
 
 	maxrecs = btblock_maxrecs(bt, mp->m_sb.sb_blocksize);
 	offset = bt->block_len +
@@ -238,7 +275,6 @@ btblock_rec_offset(
 	int			offset;
 
 	ASSERT(startoff == 0);
-	ASSERT(block->bb_level == 0);
 
 	offset = bt->block_len + (idx - 1) * bt->rec_len;
 	return bitize(offset);


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-04-11  0:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-08  3:03 [RFC PATCH v1 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
2017-04-08  3:03 ` [PATCH 1/4] xfs_db: don't print arrays off the end of a buffer Darrick J. Wong
2017-04-08  3:03 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
2017-04-08 23:19   ` Dave Chinner
2017-04-10 21:08     ` Darrick J. Wong
2017-04-08  3:04 ` [PATCH 3/4] xfs_db: complain about dir/attr blocks with bad magics Darrick J. Wong
2017-04-08  3:04 ` [PATCH 4/4] xfs_db: dump metadata btrees via 'btdump' Darrick J. Wong
2017-04-08 23:11   ` Dave Chinner
2017-04-10 22:47 [PATCH v2 0/4] xfs_db misc. btree dumping improvements Darrick J. Wong
2017-04-10 22:47 ` [PATCH 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic Darrick J. Wong
2017-04-11  0:03   ` Dave Chinner
2017-04-11  0:19     ` Darrick J. Wong

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.