Skip to contentSkip to conversation

btrfs-progs: add quota group verify to btrfsck

PATCH In review Mark Fasheh 2 participants 6 messages May 7, 2014 – May 16, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover btrfs-progs: add quota group verify to btrfsck

Hi,
The following 3 patches add support to btrfsck to check the counts
in subvolume quota groups. With these patches a user can run btrfsck against a volume and if quota is enabled, qgroup data will be checked against the actual space used on disk. I also added a --qgroup-report option that will run the qgroup checker (only) and print out a full report of all qgroups.

The patches can be pulled from the following branch:
git://github.com/markfasheh/btrfs-progs-patches.git qgroup-verify

The patches can also be viewed:
https://github.com/markfasheh/btrfs-progs-patches/tree/qgroup-verify

The first two patches set up for qgroups:

- The change in patch #1 is optional. It corrects the print of qgroup bytes to be %llu as they are unsigned values. This means however that corrupted groups will no longer show a negative value but instead an unrealistically large one. It's my opinion that '-1' and '18446744073709551615' both look pretty obviously broken when put in 'qgroup show' output so I'm going for correctness. Here's the difference in output:

qgroupid rfer       excl                 
-------- ----       ----                 
0/5      16384      16384                

0/257 4109430784 -1429504

qgroupid rfer       excl                 
-------- ----       ----                 
0/5      16384      16384                

0/257 4109430784 18446744073708122112

- Patch 2 imports the ulist code from kernel. Any qgroup code that deals with resolving refs to roots needs this so that it can insert into a 'list' that guarantees unique items.

- Patch 3 adds the actual code to do the work of adding up referenced and exclusive bytecounts.

This involves walking the extent tree and recording refs. We then resolve implied refs by walking down from each interior node. Finally, shared ref roots are found and each extent is accounted to any roots that reference it.

Here's what it looks like now if you run btrfsck against a filesystem with a couple corrupted qgroups:

Checking filesystem on /dev/vdb2
UUID: 8203ca66-9858-4e3f-b447-5bbaacf79c02
checking extents
checking free space cache
checking fs roots
checking csums
checking root refs
checking quota groups

Counts for qgroup id: 257 are different
our:            referenced 4124762112 referenced compressed 4124762112
disk:           referenced 4109430784 referenced compressed 4109430784
diff:           referenced 15331328 referenced compressed 15331328
our:            exclusive 901120 exclusive compressed 901120
disk:           exclusive 18446744073708122112 exclusive compressed 18446744073708122112
diff:           exclusive 2330624 exclusive compressed 2330624
Counts for qgroup id: 280 are different
our:            referenced 3750768640 referenced compressed 3750768640
disk:           referenced 3750768640 referenced compressed 3750768640
our:            exclusive 14749696 exclusive compressed 14749696
disk:           exclusive 11882496 exclusive compressed 11882496
diff:           exclusive 2867200 exclusive compressed 2867200

found 1009512957 bytes used err is 0
total csum bytes: 3955388
total tree bytes: 346292224
total fs tree bytes: 331939840
total extent tree bytes: 9338880
btree space waste bytes: 48141929
file data blocks allocated: 6477553664
referenced 6062055424
Btrfs v3.14.1-3-gc8c1814

There's a minor issue in that we'll also print out qgroups for deleted subvolumes as they still persist on disk (not shown here). I'm pretty sure we can fix that with a followup patch to just check them against existing subvolumes when we initially read our qgroup info from disk.

Thanks,

	--Mark
#1 lore raw

PATCH 1/3 btrfs-progs: print qgroup excl as unsigned

It's unsigned in the structure definition.

---
 print-tree.c | 12 ++++++------
 qgroup.c     |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)
print-tree.c +6 -6
index 7263b09..adef94a 100644
@@ -884,18 +884,18 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
 			qg_info = btrfs_item_ptr(l, i,
 						 struct btrfs_qgroup_info_item);
 			printf("\t\tgeneration %llu\n"
-			     "\t\treferenced %lld referenced compressed %lld\n"
-			     "\t\texclusive %lld exclusive compressed %lld\n",
+			     "\t\treferenced %llu referenced compressed %llu\n"
+			     "\t\texclusive %llu exclusive compressed %llu\n",
 			       (unsigned long long)
 			       btrfs_qgroup_info_generation(l, qg_info),
-			       (long long)
+			       (unsigned long long)
 			       btrfs_qgroup_info_referenced(l, qg_info),
-			       (long long)
+			       (unsigned long long)
 			       btrfs_qgroup_info_referenced_compressed(l,
 								       qg_info),
-			       (long long)
+			       (unsigned long long)
 			       btrfs_qgroup_info_exclusive(l, qg_info),
-			       (long long)
+			       (unsigned long long)
 			       btrfs_qgroup_info_exclusive_compressed(l,
 								      qg_info));
 			break;
qgroup.c +2 -2
index 94d1feb..368b262 100644
@@ -203,11 +203,11 @@ static void print_qgroup_column(struct btrfs_qgroup *qgroup,
 		print_qgroup_column_add_blank(BTRFS_QGROUP_QGROUPID, len);
 		break;
 	case BTRFS_QGROUP_RFER:
-		len = printf("%lld", qgroup->rfer);
+		len = printf("%llu", qgroup->rfer);
 		print_qgroup_column_add_blank(BTRFS_QGROUP_RFER, len);
 		break;
 	case BTRFS_QGROUP_EXCL:
-		len = printf("%lld", qgroup->excl);
+		len = printf("%llu", qgroup->excl);
 		print_qgroup_column_add_blank(BTRFS_QGROUP_EXCL, len);
 		break;
 	case BTRFS_QGROUP_PARENT:
Signature · 1 line
1.8.4
#2 lore raw

PATCH 2/3 btrfs-progs: import ulist

In reply to Mark Fasheh

qgroup-verify.c wants this for walking root refs.

---
 Makefile     |   3 +-
 kerncompat.h |   2 +-
 ulist.c      | 253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ulist.h      |  66 ++++++++++++++++
 4 files changed, 322 insertions(+), 2 deletions(-)
 create mode 100644 ulist.c
 create mode 100644 ulist.h
Makefile +2 -1
index da05197..202013e 100644
@@ -9,7 +9,8 @@ CFLAGS = -g -O1 -fno-strict-aliasing
 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
 	  root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
 	  extent-cache.o extent_io.o volumes.o utils.o repair.o \
-	  qgroup.o raid6.o free-space-cache.o list_sort.o props.o
+	  qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
+	  ulist.o
 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
 	       cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
 	       cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
kerncompat.h +1 -1
index f370cd8..652275e 100644
@@ -235,7 +235,7 @@ static inline long IS_ERR(const void *ptr)
 
 #define BUG_ON(c) assert(!(c))
 #define WARN_ON(c) assert(!(c))
-
+#define	ASSERT(c) assert(c)
 
 #define container_of(ptr, type, member) ({                      \
         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
ulist.c new file +253 -0

253 lines not on this page · raw message

ulist.h new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
1.8.4
#3 lore raw

PATCH 3/3 btrfs-progs: add quota group verify code

In reply to Mark Fasheh

This patch adds functionality (in qgroup-verify.c) to compute bytecounts in subvolume quota groups. The original groups are read in and stored in memory so that after we compute our own bytecounts, we can compare them with those on disk. A print function is provided to do this comparison and show the results on the console.

A 'qgroup check' pass is added to btrfsck. If any subvolume quota groups differ from what we compute, the differences for them are printed. We also provide an option '--qgroup-report' which will run only the quota check code and print a report on all quota groups. Other than making it possible to verify that our qgroup changes work correctly, this mode can also be used in xfstests for automated checking after qgroup tests.

This patch does not address the following:
- compressed counts are identical to non compressed, because kernel doesn't make the distinction yet. Adding the code to verify compressed counts shouldn't be hard at all though once kernel can do this.
- It is only concerned with subvolume quota groups (like most of btrfs-progs).

---
 Makefile        |    2 +-
 cmds-check.c    |   24 ++
 ctree.h         |   10 +
 disk-io.c       |   16 +-
 print-tree.c    |    2 +-
 print-tree.h    |    1 +
 qgroup-verify.c | 1085 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qgroup-verify.h |   25 ++
 8 files changed, 1161 insertions(+), 4 deletions(-)
 create mode 100644 qgroup-verify.c
 create mode 100644 qgroup-verify.h
Makefile +1 -1
index 202013e..51e5264 100644
@@ -10,7 +10,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
 	  root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
 	  extent-cache.o extent_io.o volumes.o utils.o repair.o \
 	  qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
-	  ulist.o
+	  ulist.o qgroup-verify.o
 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
 	       cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
 	       cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
cmds-check.c +24 -0
index d195e7a..5401ad9 100644
@@ -38,6 +38,7 @@
 #include "commands.h"
 #include "free-space-cache.h"
 #include "btrfsck.h"
+#include "qgroup-verify.h"
 
 static u64 bytes_used = 0;
 static u64 total_csum_bytes = 0;
@@ -6427,6 +6428,7 @@ static struct option long_options[] = {
 	{ "init-csum-tree", 0, NULL, 0 },
 	{ "init-extent-tree", 0, NULL, 0 },
 	{ "backup", 0, NULL, 0 },
+	{ "qgroup-report", 0, NULL, 'Q' },
 	{ NULL, 0, NULL, 0}
 };
 
@@ -6439,6 +6441,7 @@ const char * const cmd_check_usage[] = {
 	"--repair                    try to repair the filesystem",
 	"--init-csum-tree            create a new CRC tree",
 	"--init-extent-tree          create a new extent tree",
+	"--qgroup-report             print a report on qgroup consistency",
 	NULL
 };
 
@@ -6453,6 +6456,7 @@ int cmd_check(int argc, char **argv)
 	u64 num;
 	int option_index = 0;
 	int init_csum_tree = 0;
+	int qgroup_report = 0;
 	enum btrfs_open_ctree_flags ctree_flags =
 		OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
 
@@ -6479,6 +6483,9 @@ int cmd_check(int argc, char **argv)
 				printf("using SB copy %llu, bytenr %llu\n", num,
 				       (unsigned long long)bytenr);
 				break;
+			case 'Q':
+				qgroup_report = 1;
+				break;
 			case '?':
 			case 'h':
 				usage(cmd_check_usage);
@@ -6526,6 +6533,14 @@ int cmd_check(int argc, char **argv)
 
 	root = info->fs_root;
 	uuid_unparse(info->super_copy->fsid, uuidbuf);
+	if (qgroup_report) {
+		printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
+		       uuidbuf);
+		ret = qgroup_verify_all(info);
+		if (ret == 0)
+			print_qgroup_report(1);
+		goto close_out;
+	}
 	printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
 
 	if (!extent_buffer_uptodate(info->tree_root->node) ||
@@ -6629,11 +6644,20 @@ int cmd_check(int argc, char **argv)
 		free(bad);
 	}
 
+	if (info->quota_enabled) {
+		int err;
+		fprintf(stderr, "checking quota groups\n");
+		err = qgroup_verify_all(info);
+		if (err)
+			goto out;
+	}
+
 	if (!list_empty(&root->fs_info->recow_ebs)) {
 		fprintf(stderr, "Transid errors in file system\n");
 		ret = 1;
 	}
 out:
+	print_qgroup_report(0);
 	if (found_old_backref) { /*
 		 * there was a disk format change when mixed
 		 * backref was in testing tree. The old format
ctree.h +10 -0
index a4d2cd1..9489338 100644
@@ -943,6 +943,7 @@ struct btrfs_fs_info {
 	struct btrfs_root *chunk_root;
 	struct btrfs_root *dev_root;
 	struct btrfs_root *csum_root;
+	struct btrfs_root *quota_root;
 
 	struct rb_root fs_root_tree;
 
@@ -988,6 +989,7 @@ struct btrfs_fs_info {
 	unsigned int readonly:1;
 	unsigned int on_restoring:1;
 	unsigned int is_chunk_recover:1;
+	unsigned int quota_enabled:1;
 
 	int (*free_extent_hook)(struct btrfs_trans_handle *trans,
 				struct btrfs_root *root,
@@ -2382,4 +2384,12 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
 int btrfs_lookup_uuid_subvol_item(int fd, const u8 *uuid, u64 *subvol_id);
 int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
 					   u64 *subvol_id);
+
+static inline int is_fstree(u64 rootid)
+{
+	if (rootid == BTRFS_FS_TREE_OBJECTID ||
+	    (signed long long)rootid >= (signed long long)BTRFS_FIRST_FREE_OBJECTID)
+		return 1;
+	return 0;
+}
 #endif
disk-io.c +14 -2
index 19b95a7..09efa31 100644
@@ -569,7 +569,6 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root,
 	return 0;
 }
 
-
 int btrfs_free_fs_root(struct btrfs_root *root)
 {
 	if (root->node)
@@ -695,6 +694,8 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
 		return fs_info->dev_root;
 	if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
 		return fs_info->csum_root;
+	if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
+		return fs_info->csum_root;
 
 	BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
 	       location->offset != (u64)-1);
@@ -721,6 +722,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 	free(fs_info->chunk_root);
 	free(fs_info->dev_root);
 	free(fs_info->csum_root);
+	free(fs_info->quota_root);
 	free(fs_info->super_copy);
 	free(fs_info->log_root_tree);
 	free(fs_info);
@@ -741,11 +743,13 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
 	fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
 	fs_info->dev_root = malloc(sizeof(struct btrfs_root));
 	fs_info->csum_root = malloc(sizeof(struct btrfs_root));
+	fs_info->quota_root = malloc(sizeof(struct btrfs_root));
 	fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
 
 	if (!fs_info->tree_root || !fs_info->extent_root ||
 	    !fs_info->chunk_root || !fs_info->dev_root ||
-	    !fs_info->csum_root || !fs_info->super_copy)
+	    !fs_info->csum_root || !fs_info->quota_root ||
+	    !fs_info->super_copy)
 		goto free_all;
 
 	memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
@@ -754,6 +758,7 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
 	memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
 	memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
 	memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
+	memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
 
 	extent_io_tree_init(&fs_info->extent_cache);
 	extent_io_tree_init(&fs_info->free_space_cache);
@@ -912,6 +917,11 @@ int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
 	}
 	fs_info->csum_root->track_dirty = 1;
 
+	ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
+				  fs_info->quota_root);
+	if (ret == 0)
+		fs_info->quota_enabled = 1;
+
 	ret = find_and_setup_log_root(root, fs_info, sb);
 	if (ret) {
 		printk("Couldn't setup log root tree\n");
@@ -936,6 +946,8 @@ int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
 
 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
 {
+	if (fs_info->quota_root)
+		free_extent_buffer(fs_info->quota_root->node);
 	if (fs_info->csum_root)
 		free_extent_buffer(fs_info->csum_root->node);
 	if (fs_info->dev_root)
print-tree.c +1 -1
index adef94a..d96bf52 100644
@@ -242,7 +242,7 @@ static void print_file_extent_item(struct extent_buffer *eb,
 	       btrfs_file_extent_compression(eb, fi));
 }
 
-static void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
+void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
 {
 	struct btrfs_extent_item *ei;
 	struct btrfs_extent_inline_ref *iref;
print-tree.h +1 -0
index 495b81a..e364fdf 100644
@@ -21,4 +21,5 @@
 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l);
 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *t, int follow);
 void btrfs_print_key(struct btrfs_disk_key *disk_key);
+void print_extent_item(struct extent_buffer *eb, int slot, int metadata);
 #endif
qgroup-verify.c new file +1085 -0

1,085 lines not on this page · raw message

qgroup-verify.h new file +25 -0
new file mode 100644
index 0000000..a222c17
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2014 SUSE.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will 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 to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef _BTRFS_QGROUP_VERIFY_H
+#define _BTRFS_QGROUP_VERIFY_H
+
+int qgroup_verify_all(struct btrfs_fs_info *info);
+void print_qgroup_report(int all);
+
+#endif	/* _BTRFS_QGROUP_VERIFY_H */
Signature · 1 line
1.8.4
#4 lore raw

Re: [PATCH 3/3] btrfs-progs: add quota group verify code

qgroup-verify.c line 83

Quoted 8 lines · Mark Fasheh
qgroup-verify.c new file +8 -0
+struct ref {
+	u64			bytenr;
+	u64			num_bytes;
+	u64			parent;
+	u64			root;
+
+	struct rb_node		bytenr_node;
+};

A way too generic name, though it's only local to the qgroup verifier, one can't easily grep for it etc.

The rest looks ok or can be addressed later.

#5 lore raw
In reply to Mark Fasheh
Quoted 11 lines · Mark Fasheh

The first two patches set up for qgroups:

- The change in patch #1 is optional. It corrects the print of qgroup bytes to be %llu as they are unsigned values. This means however that corrupted groups will no longer show a negative value but instead an unrealistically large one. It's my opinion that '-1' and '18446744073709551615' both look pretty obviously broken when put in 'qgroup show' output so I'm going for correctness. Here's the difference in output:

0/257 4109430784 -1429504
0/257 4109430784 18446744073708122112

Fair enough.

Quoted 4 lines

There's a minor issue in that we'll also print out qgroups for deleted subvolumes as they still persist on disk (not shown here). I'm pretty sure we can fix that with a followup patch to just check them against existing subvolumes when we initially read our qgroup info from disk.

I think the report from deleted subvols can help debugging, keep it there. We may decide to move it under its own option.

Patches added to integration, thanks.