[Btrfs-devel] [PATCH 1/2] btrfs-progs support for root block accounting

Josef Bacik jbacik at redhat.com
Fri Aug 17 14:44:40 PDT 2007


Hello,

This adds support for keeping track of the number of blocks used by root_item's.
This makes it so that mkfs lays down the "default" subvol with the correct block
accounting in place.  Thank you,

Josef

diff -r da120892c4b5 ctree.h
--- a/ctree.h	Tue Aug 07 16:35:34 2007 -0400
+++ b/ctree.h	Fri Aug 10 14:38:40 2007 -0400
@@ -782,6 +782,17 @@ static inline void btrfs_set_root_flags(
 static inline void btrfs_set_root_flags(struct btrfs_root_item *item, u32 val)
 {
 	item->flags = cpu_to_le32(val);
+}
+
+static inline void btrfs_set_root_blocks_used(struct btrfs_root_item *item,
+					      u64 val)
+{
+	item->blocks_used = cpu_to_le64(val);
+}
+
+static inline u64 btrfs_root_blocks_used(struct btrfs_root_item *item)
+{
+	return le64_to_cpu(item->blocks_used);
 }
 
 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
diff -r da120892c4b5 extent-tree.c
--- a/extent-tree.c	Tue Aug 07 16:35:34 2007 -0400
+++ b/extent-tree.c	Fri Aug 10 14:37:25 2007 -0400
@@ -252,7 +252,7 @@ static int finish_current_insert(struct 
 	struct btrfs_extent_item extent_item;
 	int i;
 	int ret;
-	u64 super_blocks_used;
+	u64 super_blocks_used, root_blocks_used;
 	struct btrfs_fs_info *info = extent_root->fs_info;
 
 	btrfs_set_extent_refs(&extent_item, 1);
@@ -267,6 +267,9 @@ static int finish_current_insert(struct 
 		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
 		btrfs_set_super_blocks_used(info->disk_super,
 					    super_blocks_used + 1);
+		root_blocks_used = btrfs_root_blocks_used(&extent_root->root_item);
+		btrfs_set_root_blocks_used(&extent_root->root_item,
+					   root_blocks_used + 1);
 		ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
 					sizeof(extent_item));
 		if (ret) {
@@ -314,7 +317,7 @@ static int __free_extent(struct btrfs_tr
 	refs = btrfs_extent_refs(ei) - 1;
 	btrfs_set_extent_refs(ei, refs);
 	if (refs == 0) {
-		u64 super_blocks_used;
+		u64 super_blocks_used, root_blocks_used;
 		if (pin) {
 			int err;
 			unsigned long bl = blocknr;
@@ -327,6 +330,10 @@ static int __free_extent(struct btrfs_tr
 		super_blocks_used = btrfs_super_blocks_used(info->disk_super);
 		btrfs_set_super_blocks_used(info->disk_super,
 					    super_blocks_used - num_blocks);
+		root_blocks_used = btrfs_root_blocks_used(&root->root_item);
+		btrfs_set_root_blocks_used(&root->root_item, 
+					   root_blocks_used - num_blocks);
+						
 		ret = btrfs_del_item(trans, extent_root, &path);
 		if (!pin && extent_root->fs_info->last_insert.objectid >
 		    blocknr)
@@ -527,7 +534,7 @@ static int alloc_extent(struct btrfs_tra
 {
 	int ret;
 	int pending_ret;
-	u64 super_blocks_used;
+	u64 super_blocks_used, root_blocks_used;
 	struct btrfs_fs_info *info = root->fs_info;
 	struct btrfs_root *extent_root = info->extent_root;
 	struct btrfs_extent_item extent_item;
@@ -553,6 +560,10 @@ static int alloc_extent(struct btrfs_tra
 	super_blocks_used = btrfs_super_blocks_used(info->disk_super);
 	btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
 				    num_blocks);
+	root_blocks_used = btrfs_root_blocks_used(&root->root_item);
+	btrfs_set_root_blocks_used(&root->root_item, root_blocks_used +
+				   num_blocks);
+
 	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
 				sizeof(extent_item));
 
diff -r da120892c4b5 mkfs.c
--- a/mkfs.c	Tue Aug 07 16:35:34 2007 -0400
+++ b/mkfs.c	Fri Aug 17 17:45:24 2007 -0400
@@ -166,7 +166,7 @@ static int make_root_dir(int fd) {
 	memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
 	location.offset = (u64)-1;
 	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
-			"default", strlen("default"),
+			"default", 7,
 			btrfs_super_root_dir(root->fs_info->disk_super),
 			&location, BTRFS_FT_DIR);
 	if (ret)
@@ -242,6 +242,7 @@ int mkfs(int fd, char *pathname, u64 num
 		&root_item, sizeof(root_item));
 
 	btrfs_set_root_blocknr(&root_item, start_block + 3);
+	btrfs_set_root_blocks_used(&root_item, 1);
 	itemoff = itemoff - sizeof(root_item);
 	btrfs_set_item_offset(&item, itemoff);
 	btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);



More information about the Btrfs-devel mailing list