Skip to contentSkip to conversation

Decrease Metadata Fragment Using A Caterpillar Band Method

RFC PATCH New WeiFeng Liu 1 participant 1 message May 27, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Decrease Metadata Fragment Using A Caterpillar Band Method

I made an attempt to partly decrease fragment by using a preallocated
area of multiple size of a std blocksize for a tree block when it is COWed.
The basic idea is that if any a tree block need to be created,
we offer, say, 2 or 4 multiples of a std blocksize for it, then use the first block in the continuous blocks. When this block need a cow in the future, a new free block in these continuous blocks is grabbed, and the old one is freed for next cow. In the most ideal condition only 2 continuous blocks are kept for any times of cowing a block -- image a caterpillar band by which my method is named.
Following graphic demostrate the way in the real world by using a area
of 4 multipled std blocksize. The reason I use 4 rather than 2 blocks is that we can pick up another free block in the area if the old one is not freed before it can be used in a cow, this give some tolerance to avoid always to create a new caterpillar instantly if an old block is occupied.

A --> B --> C --> D -->|
^                      |
|                      v
|---------<------------|

Pros:
1 Lower computing and searching cost when alloc a tree block. 2 In cows of a block based on finite compact blocks like a ca in theory, and thereby decrease the chances of fragment.

Cons:
1 Acquire more space for metadata and not suit for user data, obviously. 2 Conflict with SSD wearing leveling optimizing, obviously.

Implementation Notes:
struct btrfs_fs_info has a field cater_factor to store the multiples from mount option, and struct btrfs_header has a u8 member, it's left-hand 4 bits to record this block's location index and right-hand 4 bits for multiples, such a combination indicates how many continuous blocks form a caterpillar and which slot this tree block resides in the caterpillar.

When cow a block that is already in a caterpillar, simply search a free block from the first block(index 0) untill the last block(max index) in the caterpillar.

Cow a block that has not yet been in a caterpillar, firstly we create a caterpillar using multiples(fs_info->cater_factor), then pick up the first block in the created caterpillar and give index 0 for the block.

Discard a block that is in a caterpillar, the whole caterpillar can be freed if no other block is occupied or needed in the caterpillar.

By the time, little tests havw done for the initial patch, both further reviews and buf-fixs are needed before mature if there are some interesting appeared on it.

signed-off-by WeiFeng Liu 523f28f9b3d9c710cacc31dbba644efb1678cf62 ([email protected])
---

fs/btrfs/ctree.c +13 -1
@@ -444,9 +444,21 @@ static noinline int __btrfs_cow_block(st
 	} else
 		parent_start = 0;
 
-	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
+	if (root->fs_info->cater_factor > 1) {	
+		if (btrfs_cater_factor(btrfs_header_cater(buf)) > 1)			
+			cow = btrfs_grab_cater_block(trans, root, buf, parent_start,
+								root->root_key.objectid, &disk_key,
+								level, search_start, empty_size, 1);
+		else			
+			cow = btrfs_alloc_free_block_cater(trans, root, buf->len, parent_start,
+								root->root_key.objectid, &disk_key,
+								level, search_start, empty_size, 1);
+	} else {
+		cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
 				     root->root_key.objectid, &disk_key,
 				     level, search_start, empty_size, 1);
+	}
+	
 	if (IS_ERR(cow))
 		return PTR_ERR(cow);
 
fs/btrfs/ctree.h +30 -0
diff -urpN a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
@@ -341,6 +341,7 @@ struct btrfs_header {
 	__le64 owner;
 	__le32 nritems;
 	u8 level;
+	u8 cater_index_factor;
 } __attribute__ ((__packed__));
 
 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
@@ -544,6 +545,7 @@ struct btrfs_extent_item {
 	__le64 refs;
 	__le64 generation;
 	__le64 flags;
+	u8 cater_index_factor;
 } __attribute__ ((__packed__));
 
 struct btrfs_extent_item_v0 {
@@ -1222,6 +1224,7 @@ struct btrfs_fs_info {
 
 	unsigned data_chunk_allocations;
 	unsigned metadata_ratio;
+	unsigned cater_factor;
 
 	void *bdev_holder;
 
@@ -1761,6 +1764,8 @@ BTRFS_SETGET_FUNCS(extent_refs, struct b
 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
 		   generation, 64);
 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
+BTRFS_SETGET_FUNCS(extent_cater, struct btrfs_extent_item,
+			cater_index_factor, 8);
 
 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
 
@@ -1781,6 +1786,16 @@ static inline void btrfs_set_tree_block_
 	write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
 }
 
+static inline u8 btrfs_cater_factor(u8 cater_index_factor)
+{
+	return (cater_index_factor & 15);
+}
+
+static inline u8 btrfs_cater_index(u8 cater_index_factor)
+{
+	return (cater_index_factor >> 4);
+}
+
 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
 		   root, 64);
 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
@@ -2042,6 +2057,8 @@ BTRFS_SETGET_HEADER_FUNCS(header_owner,
 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
+BTRFS_SETGET_HEADER_FUNCS(header_cater, struct btrfs_header,
+			cater_index_factor, 8);
 
 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
 {
@@ -2445,6 +2462,19 @@ struct extent_buffer *btrfs_alloc_free_b
 					struct btrfs_root *root, u32 blocksize,
 					u64 parent, u64 root_objectid,
 					struct btrfs_disk_key *key, int level,
+					u64 hint, u64 empty_size, int for_cow);
+int btrfs_cater_blocks_free(struct btrfs_root *root, u64 start, u64 len,
+					u8 factor, u8 index);
+struct extent_buffer *btrfs_grab_cater_block(struct btrfs_trans_handle *trans,
+					struct btrfs_root *root, struct extent_buffer *buf,
+					u64 parent, u64 root_objectid,
+					struct btrfs_disk_key *key, int level,
+					u64 hint, u64 empty_size, int for_cow);
+struct extent_buffer *btrfs_alloc_free_block_cater(
+					struct btrfs_trans_handle *trans,
+					struct btrfs_root *root, u32 blocksize,
+					u64 parent, u64 root_objectid,
+					struct btrfs_disk_key *key, int level,
 					u64 hint, u64 empty_size, int for_cow);
 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
 			   struct btrfs_root *root,
fs/btrfs/delayed-ref.h +1 -0
diff -urpN a/fs/btrfs/delayed-ref.h b/fs/btrfs/delayed-ref.h
@@ -63,6 +63,7 @@ struct btrfs_delayed_extent_op {
 	unsigned int update_key:1;
 	unsigned int update_flags:1;
 	unsigned int is_data:1;
+	u8 cater_index_factor;
 };
 
 /*
fs/btrfs/extent-tree.c +373 -3

448 lines not on this page · raw message

fs/btrfs/super.c +10 -1
diff -urpN a/fs/btrfs/super.c b/fs/btrfs/super.c
@@ -166,7 +166,7 @@ enum {
 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
-	Opt_check_integrity_print_mask,
+	Opt_check_integrity_print_mask, Opt_cater_factor,
 	Opt_err,
 };
 
@@ -206,6 +206,7 @@ static match_table_t tokens = {
 	{Opt_check_integrity, "check_int"},
 	{Opt_check_integrity_including_extent_data, "check_int_data"},
 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
+	{Opt_cater_factor, "cater_factor=%d"},
 	{Opt_err, NULL},
 };
 
@@ -407,6 +408,14 @@ int btrfs_parse_options(struct btrfs_roo
 		case Opt_skip_balance:
 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
 			break;
+		case Opt_cater_factor:
+			match_int(&args[0], &intarg);
+			if (intarg > 16 || intarg < 1)
+				info->cater_factor = 1;
+			else
+				info->cater_factor = intarg;
+			printk(KERN_INFO "btrfs: cater_factor is %d\n",
+						(unsigned char)info->cater_factor);
 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
 		case Opt_check_integrity_including_extent_data:
 			printk(KERN_INFO "btrfs: enabling check integrity"