Skip to contentSkip to conversation

Btrfs-progs: add a free space cache checker to fsck V2

PATCH In review Josef Bacik 2 participants 2 messages Apr 4, 2013 – Apr 15, 2013

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

PATCH 1/1 Btrfs-progs: add a free space cache checker to fsck V2

In trying to track down a weird tree log problem I wanted to make sure that the free space cache was actually valid, which we currently have no way of doing. So this patch adds a bunch of support for the free space cache code and then a checker to fsck. Basically we go through and if we can actually load the free space cache then we will walk the extent tree and verify that the free space cache exactly matches what is in the extent tree. Hopefully this will always be correct, the only time it wouldn't is if the extent tree is corrupt or we have some sort of awful bug in the free space cache. Thanks,

---
V1->V2:
- fix ret == 1 from btrfs_search_slot leaking out of verify_space_cache
- check the last bit of free space from the last extent to the end of the block group
- unlink any entries we find so we can check and make sure the free space cache tree is empty once we're done.

 Makefile           |    3 +-
 bitops.h           |  220 +++++++++++++
 cmds-check.c       |  223 ++++++++++++++
 ctree.h            |   14 +
 extent-tree.c      |   12 +-
 extent_io.c        |   51 +++
 extent_io.h        |    4 +
 free-space-cache.c |  869 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 free-space-cache.h |   55 ++++
 9 files changed, 1448 insertions(+), 3 deletions(-)
 create mode 100644 bitops.h
 create mode 100644 free-space-cache.c
 create mode 100644 free-space-cache.h
Makefile +2 -1
index 66d677c..a4f9374 100644
@@ -6,7 +6,8 @@ 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 crc32c.o rbtree.o extent-cache.o extent_io.o \
 	  volumes.o utils.o btrfs-list.o btrfslabel.o repair.o \
-	  send-stream.o send-utils.o qgroup.o raid6.o
+	  send-stream.o send-utils.o qgroup.o raid6.o free-space-cache.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 \
bitops.h new file +220 -0

220 lines not on this page · raw message

cmds-check.c +223 -0

241 lines not on this page · raw message

ctree.h +14 -0
index 702de2c..67fa650 100644
@@ -28,6 +28,7 @@
 
 struct btrfs_root;
 struct btrfs_trans_handle;
+struct btrfs_free_space_ctl;
 #define BTRFS_MAGIC 0x4D5F53665248425F /* ascii _BHRfS_M, no null */
 
 #define BTRFS_MAX_LEVEL 8
@@ -268,6 +269,15 @@ struct btrfs_chunk {
 	/* additional stripes go here */
 } __attribute__ ((__packed__));
 
+#define BTRFS_FREE_SPACE_EXTENT	1
+#define BTRFS_FREE_SPACE_BITMAP	2
+
+struct btrfs_free_space_entry {
+	__le64 offset;
+	__le64 bytes;
+	u8 type;
+} __attribute__ ((__packed__));
+
 struct btrfs_free_space_header {
 	struct btrfs_disk_key location;
 	__le64 generation;
@@ -866,6 +876,7 @@ struct btrfs_block_group_cache {
 	struct btrfs_key key;
 	struct btrfs_block_group_item item;
 	struct btrfs_space_info *space_info;
+	struct btrfs_free_space_ctl *free_space_ctl;
 	u64 pinned;
 	u64 flags;
 	int cached;
@@ -2055,6 +2066,9 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);
 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
 							 btrfs_fs_info *info,
 							 u64 bytenr);
+struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
+						       btrfs_fs_info *info,
+						       u64 bytenr);
 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
 						 struct btrfs_block_group_cache
 						 *hint, u64 search_start,
extent-tree.c +10 -2
index 2e0524b..4ddf48c 100644
@@ -26,6 +26,7 @@
 #include "transaction.h"
 #include "crc32c.h"
 #include "volumes.h"
+#include "free-space-cache.h"
 
 #define BLOCK_GROUP_DATA     EXTENT_WRITEBACK
 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
@@ -3176,6 +3177,7 @@ out:
 int btrfs_free_block_groups(struct btrfs_fs_info *info)
 {
 	struct btrfs_space_info *sinfo;
+	struct btrfs_block_group_cache *cache;
 	u64 start;
 	u64 end;
 	u64 ptr;
@@ -3187,8 +3189,14 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 		if (ret)
 			break;
 		ret = get_state_private(&info->block_group_cache, start, &ptr);
-		if (!ret)
-			kfree((void *)(unsigned long)ptr);
+		if (!ret) {
+			cache = (struct btrfs_block_group_cache *)ptr;
+			if (cache->free_space_ctl) {
+				btrfs_remove_free_space_cache(cache);
+				kfree(cache->free_space_ctl);
+			}
+			kfree(cache);
+		}
 		clear_extent_bits(&info->block_group_cache, start,
 				  end, (unsigned int)-1, GFP_NOFS);
 	}
extent_io.c +51 -0

63 lines not on this page · raw message

extent_io.h +4 -0
index 63e9004..1c7999a 100644
@@ -34,6 +34,8 @@
 #define EXTENT_CSUM (1 << 9)
 #define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
 
+struct btrfs_fs_info;
+
 struct extent_io_tree {
 	struct cache_tree state;
 	struct cache_tree cache;
@@ -115,4 +117,6 @@ void memset_extent_buffer(struct extent_buffer *eb, char c,
 			  unsigned long start, unsigned long len);
 int set_extent_buffer_dirty(struct extent_buffer *eb);
 int clear_extent_buffer_dirty(struct extent_buffer *eb);
+int read_data_from_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
+			u64 bytes, int mirror);
 #endif
free-space-cache.c new file +869 -0

869 lines not on this page · raw message

free-space-cache.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 9 lines · Josef Bacik

In trying to track down a weird tree log problem I wanted to make sure that the free space cache was actually valid, which we currently have no way of doing. So this patch adds a bunch of support for the free space cache code and then a checker to fsck. Basically we go through and if we can actually load the free space cache then we will walk the extent tree and verify that the free space cache exactly matches what is in the extent tree. Hopefully this will always be correct, the only time it wouldn't is if the extent tree is corrupt or we have some sort of awful bug in the free space cache. Thanks,

Quoted 9 lines

---
V1->V2:
- fix ret == 1 from btrfs_search_slot leaking out of verify_space_cache
- check the last bit of free space from the last extent to the end of the block group
- unlink any entries we find so we can check and make sure the free space cache tree is empty once we're done.

Josef, xfstests 013, 068 and 113 fail with your new fsck free space cache checker, see below. $TEST_DEV /dev/sdc is a 50GB SSD, $SCRATCH_DEV /dev/sds is a 100GB SSD. Default mount options.

# mkfs.btrfs -f -n 16384 $SCRATCH_DEV
# mkfs.btrfs -f -n 16384 $TEST_DEV
# ./check 013
# btrfsck $TEST_DEV
Checking filesystem on /dev/sdc
UUID: 1c91dbb9-2495-4324-bb53-f96e15cff0dc
checking extents
checking free space cache
Wanted bytes 606208, found 16384 for off 191184896
Wanted bytes 911917056, found 16384 for off 191184896
cache appears valid but isnt 29360128
btrfs: unable to add free space :-17
btrfsck: free-space-cache.c:815: btrfs_add_free_space: Assertion `!(ret == -17)' failed.
Aborted
# mkfs.btrfs -f -n 16384 $SCRATCH_DEV
# mkfs.btrfs -f -n 16384 $TEST_DEV
# ./check 013
# btrfsck $TEST_DEV
Checking filesystem on /dev/sdc
UUID: 18046b2a-9c98-4b34-9294-954d094049c2
checking extents
checking free space cache
Wanted bytes 16203776, found 9355264 for off 168624128
Wanted bytes 934477824, found 9355264 for off 168624128
cache appears valid but isnt 29360128
There is no free space entry for 1115480064-1115504640
There is no free space entry for 1115480064-2176843776
cache appears valid but isnt 1103101952
found 380784640 bytes used err is -22
total csum bytes: 87084
total tree bytes: 4833280
total fs tree bytes: 4390912
total extent tree bytes: 245760
btree space waste bytes: 952741
file data blocks allocated: 500023296
 referenced 363667456
Btrfs v0.20-rc1-298-gf65141e
# mkfs.btrfs -f -n 16384 $SCRATCH_DEV
# mkfs.btrfs -f -n 16384 $TEST_DEV
# ./check 068
_check_btrfs_filesystem: filesystem on /dev/sds is inconsistent
*** fsck.btrfs output ***
checking extents
checking free space cache
Wanted bytes 490909696, found 536870912 for off 1103101952
Wanted bytes 1073741824, found 536870912 for off 1103101952
cache appears valid but isnt 1103101952
Checking filesystem on /dev/sds
UUID: d5340e2c-d1e5-4155-afc2-70afb6bbbe85
found 638976 bytes used err is -22
total csum bytes: 0
total tree bytes: 114688
total fs tree bytes: 32768
total extent tree bytes: 16384
btree space waste bytes: 107558
file data blocks allocated: 524288
 referenced 524288
Btrfs v0.20-rc1-298-gf65141e
*** end fsck.btrfs output
# mkfs.btrfs -f -n 16384 $SCRATCH_DEV
# mkfs.btrfs -f -n 16384 $TEST_DEV
# ./check 113
_check_btrfs_filesystem: filesystem on /dev/sdc is inconsistent
*** fsck.btrfs output ***
checking extents
checking free space cache
btrfs: unable to add free space :-17
btrfsck: free-space-cache.c:815: btrfs_add_free_space: Assertion `!(ret == -17)' failed.
Checking filesystem on /dev/sdc
UUID: 9fef532c-2cf2-4bad-8b00-aa997403b415
*** end fsck.btrfs output