btrfs: add and use helper macros to print keys
cover btrfs: add and use helper macros to print keys
From: Filipe Manana <[email protected]>
Details in the change logs.
Filipe Manana (2):
btrfs: add macros to facilitate printing of keys
btrfs: use the key format macros when printing keys
fs/btrfs/backref.c | 11 +++++------ fs/btrfs/ctree.c | 17 +++++++---------- fs/btrfs/extent-tree.c | 14 +++++++------- fs/btrfs/fs.h | 3 +++ fs/btrfs/inode.c | 4 ++-- fs/btrfs/print-tree.c | 14 ++++++-------- fs/btrfs/qgroup.c | 6 ++---- fs/btrfs/relocation.c | 4 ++-- fs/btrfs/root-tree.c | 4 ++-- fs/btrfs/send.c | 10 ++++------ fs/btrfs/tree-checker.c | 21 +++++++++------------ fs/btrfs/tree-log.c | 38 ++++++++++++++++++-------------------- 12 files changed, 67 insertions(+), 79 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/2 btrfs: add macros to facilitate printing of keys
From: Filipe Manana <[email protected]>
There's a lot of places where we need to print a key, and it's tiresome to type the format specifier, typically "(%llu %u %llu)", as well as passing 3 arguments to a prink family function (key->objectid, key->type, key->offset).
So add a couple macros for this just like we have for csum values in btrfs_inode.h (CSUM_FMT and CSUM_FMT_VALUE).
This also ensures that we consistently print a key in the same format, always as "(%llu %llu %llu)", which is the most common format we use, but we have a few variations such as "[%llu %llu %llu]" for no good reason.
This patch introduces the macros while the next one makes use of it. This is to ease backports of future patches, since then we can backport this patch which is simple and short and then backport those future patches, as the next patch in the series that makes use of these new macros is quite large and may have some dependencies.
--- fs/btrfs/fs.h | 3 +++ 1 file changed, 3 insertions(+)
fs/btrfs/fs.h +3 -0
@@ -74,6 +74,9 @@ struct btrfs_space_info;#define BTRFS_SUPER_INFO_SIZE 4096static_assert (sizeof (struct btrfs_super_block ) ==BTRFS_SUPER_INFO_SIZE ); +#define BTRFS_KEY_FMT "(%llu %u %llu)" +#define BTRFS_KEY_FMT_VALUE (key) (key)->objectid, (key)->type, (key)->offset +/* * Number of metadata items necessary for an unlink operation: *
Signature · 1 line
2.47.2
PATCH 2/2 btrfs: use the key format macros when printing keys
In reply to [email protected]From: Filipe Manana <[email protected]>
Change all locations that print a key to use the new macros to print them in order to ensure a consistent style and avoid repetitive code.
--- fs/btrfs/backref.c | 11 +++++------ fs/btrfs/ctree.c | 17 +++++++---------- fs/btrfs/extent-tree.c | 14 +++++++------- fs/btrfs/inode.c | 4 ++-- fs/btrfs/print-tree.c | 14 ++++++-------- fs/btrfs/qgroup.c | 6 ++---- fs/btrfs/relocation.c | 4 ++-- fs/btrfs/root-tree.c | 4 ++-- fs/btrfs/send.c | 10 ++++------ fs/btrfs/tree-checker.c | 21 +++++++++------------ fs/btrfs/tree-log.c | 38 ++++++++++++++++++-------------------- 11 files changed, 64 insertions(+), 79 deletions(-)
fs/btrfs/backref.c +5 -6
@@ -666,10 +666,9 @@ static int resolve_indirect_ref(struct btrfs_backref_walk_ctx *ctx, ret =btrfs_search_old_slot (root, &search_key, path, ctx->time_seq);btrfs_debug (ctx->fs_info, -"search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)" , - ref->root_id, level, ref->count, ret, - ref->key_for_search.objectid, ref->key_for_search.type, - ref->key_for_search.offset); +"search slot in root %llu (level %d, ref count %d) returned %d for key " BTRFS_KEY_FMT , + ref->root_id, level, ref->count, ret, +BTRFS_KEY_FMT_VALUE (&ref->key_for_search));if (ret <0 ) goto out;
@@ -3323,9 +3322,9 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans, eb = path->nodes[level];if (btrfs_node_blockptr (eb, path->slots[level]) != cur->bytenr) {btrfs_err (fs_info, -"couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)" , +"couldn't find block (%llu) (level %d) in tree (%llu) with key " BTRFS_KEY_FMT , cur->bytenr, level -1 ,btrfs_root_id (root), - tree_key->objectid, tree_key->type, tree_key->offset); +BTRFS_KEY_FMT_VALUE (tree_key));btrfs_put_root (root); ret = -ENOENT ; goto out;
fs/btrfs/ctree.c +7 -10
@@ -2599,12 +2599,11 @@ void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,if (unlikely (btrfs_comp_keys (&disk_key, new_key) >=0 )) {btrfs_print_leaf (eb);btrfs_crit (fs_info, -"slot %u key (%llu %u %llu) new key (%llu %u %llu)" , +"slot %u key " BTRFS_KEY_FMT " new key " BTRFS_KEY_FMT , slot,btrfs_disk_key_objectid (&disk_key),btrfs_disk_key_type (&disk_key),btrfs_disk_key_offset (&disk_key), - new_key->objectid, new_key->type, - new_key->offset); +BTRFS_KEY_FMT_VALUE (new_key));BUG (); } }
@@ -2613,12 +2612,11 @@ void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,if (unlikely (btrfs_comp_keys (&disk_key, new_key) <=0 )) {btrfs_print_leaf (eb);btrfs_crit (fs_info, -"slot %u key (%llu %u %llu) new key (%llu %u %llu)" , +"slot %u key " BTRFS_KEY_FMT " new key " BTRFS_KEY_FMT , slot,btrfs_disk_key_objectid (&disk_key),btrfs_disk_key_type (&disk_key),btrfs_disk_key_offset (&disk_key), - new_key->objectid, new_key->type, - new_key->offset); +BTRFS_KEY_FMT_VALUE (new_key));BUG (); } }
@@ -2677,10 +2675,9 @@ static bool check_sibling_keys(const struct extent_buffer *left,btrfs_crit (left->fs_info,"right extent buffer:" );btrfs_print_tree (right, false);btrfs_crit (left->fs_info, -"bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)" , - left_last.objectid, left_last.type, - left_last.offset, right_first.objectid, - right_first.type, right_first.offset); +"bad key order, sibling blocks, left last " BTRFS_KEY_FMT " right first " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&left_last), +BTRFS_KEY_FMT_VALUE (&right_first));return true; }return false;
fs/btrfs/extent-tree.c +7 -7
@@ -164,8 +164,8 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,if (unlikely (num_refs ==0 )) { ret = -EUCLEAN ;btrfs_err (fs_info, -"unexpected zero reference count for extent item (%llu %u %llu)" , - key.objectid, key.type, key.offset); +"unexpected zero reference count for extent item " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&key));btrfs_abort_transaction (trans, ret);return ret; }
@@ -597,8 +597,8 @@ static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, num_refs =btrfs_shared_data_ref_count (leaf, ref2); }else {btrfs_err (trans->fs_info, -"unrecognized backref key (%llu %u %llu)" , - key.objectid, key.type, key.offset); +"unrecognized backref key " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&key));btrfs_abort_transaction (trans, -EUCLEAN );return -EUCLEAN ; }
@@ -3326,9 +3326,9 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,if (iref) {if (unlikely (path->slots[0 ] != extent_slot)) {abort_and_dump (trans, path, -"invalid iref, extent item key (%llu %u %llu) slot %u doesn't have wanted iref" , - key.objectid, key.type, - key.offset, path->slots[0 ]); +"invalid iref, extent item key " BTRFS_KEY_FMT " slot %u doesn't have wanted iref" , +BTRFS_KEY_FMT_VALUE (&key), + path->slots[0 ]); ret = -EUCLEAN ; goto out; }
fs/btrfs/inode.c +2 -2
@@ -5642,9 +5642,9 @@ static int btrfs_inode_by_name(struct btrfs_inode *dir, struct dentry *dentry, location->type != BTRFS_ROOT_ITEM_KEY)) { ret = -EUCLEAN ;btrfs_warn (root->fs_info, -"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))" , +"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location " BTRFS_KEY_FMT ")" , __func__, fname.disk_name.name,btrfs_ino (dir), - location->objectid, location->type, location->offset); +BTRFS_KEY_FMT_VALUE (location)); }if (!ret) *type =btrfs_dir_ftype (path->nodes[0 ], di);
fs/btrfs/print-tree.c +6 -8
@@ -131,7 +131,7 @@ static void print_extent_item(const struct extent_buffer *eb, int slot, int typestruct btrfs_tree_block_info *info; info = (struct btrfs_tree_block_info *)(ei +1 );btrfs_tree_block_key (eb, info, &key); -pr_info ("\t\ttree block key (%llu %u %llu) level %d\n" , +pr_info ("\t\ttree block key " BTRFS_KEY_FMT " level %d\n" ,btrfs_disk_key_objectid (&key), key.type,btrfs_disk_key_offset (&key),btrfs_tree_block_level (eb, info));
@@ -277,9 +277,8 @@ static void print_dir_item(const struct extent_buffer *eb, int i)struct btrfs_key location;btrfs_dir_item_key_to_cpu (eb, di, &location); -pr_info ("\t\tlocation key (%llu %u %llu) type %d\n" , - location.objectid, location.type, location.offset, -btrfs_dir_ftype (eb, di)); +pr_info ("\t\tlocation key " BTRFS_KEY_FMT " type %d\n" , +BTRFS_KEY_FMT_VALUE (&location),btrfs_dir_ftype (eb, di));pr_info ("\t\ttransid %llu data_len %u name_len %u\n" ,btrfs_dir_transid (eb, di), data_len, name_len); di = (struct btrfs_dir_item *)((char *)di + len);
@@ -598,10 +597,9 @@ void btrfs_print_tree(const struct extent_buffer *c, bool follow)print_eb_refs_lock (c);for (i =0 ; i < nr; i++) {btrfs_node_key_to_cpu (c, &key, i); -pr_info ("\tkey %d (%llu %u %llu) block %llu gen %llu\n" , - i, key.objectid, key.type, key.offset, -btrfs_node_blockptr (c, i), -btrfs_node_ptr_generation (c, i)); +pr_info ("\tkey %d " BTRFS_KEY_FMT " block %llu gen %llu\n" , + i,BTRFS_KEY_FMT_VALUE (&key),btrfs_node_blockptr (c, i), +btrfs_node_ptr_generation (c, i)); }if (!follow)return ;
fs/btrfs/qgroup.c +2 -4
@@ -3710,10 +3710,8 @@ static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans, path, 1, 0);btrfs_debug (fs_info, -"current progress key (%llu %u %llu), search_slot ret %d" , - fs_info->qgroup_rescan_progress.objectid, - fs_info->qgroup_rescan_progress.type, - fs_info->qgroup_rescan_progress.offset, ret); +"current progress key " BTRFS_KEY_FMT ", search_slot ret %d" , +BTRFS_KEY_FMT_VALUE (&fs_info->qgroup_rescan_progress), ret);if (ret) {/*
fs/btrfs/relocation.c +2 -2
@@ -615,8 +615,8 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,btrfs_disk_key_to_cpu (&cpu_key, &root->root_item.drop_progress);btrfs_err (fs_info, -"cannot relocate partially dropped subvolume %llu, drop progress key (%llu %u %llu)" , - objectid, cpu_key.objectid, cpu_key.type, cpu_key.offset); +"cannot relocate partially dropped subvolume %llu, drop progress key " BTRFS_KEY_FMT , + objectid,BTRFS_KEY_FMT_VALUE (&cpu_key)); ret = -EUCLEAN ; goto fail; }
fs/btrfs/root-tree.c +2 -2
@@ -147,8 +147,8 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_rootif (unlikely (ret >0 )) {btrfs_crit (fs_info, -"unable to find root key (%llu %u %llu) in tree %llu" , - key->objectid, key->type, key->offset,btrfs_root_id (root)); +"unable to find root key " BTRFS_KEY_FMT " in tree %llu" , +BTRFS_KEY_FMT_VALUE (key),btrfs_root_id (root)); ret = -EUCLEAN ;btrfs_abort_transaction (trans, ret);return ret;
fs/btrfs/send.c +4 -6
@@ -1053,10 +1053,8 @@ static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, }if (unlikely (start < p->buf)) {btrfs_err (root->fs_info, -"send: path ref buffer underflow for key (%llu %u %llu)" , - found_key->objectid, - found_key->type, - found_key->offset); +"send: path ref buffer underflow for key " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (found_key)); ret = -EINVAL ; goto out; }
@@ -7234,8 +7232,8 @@ static int search_key_again(const struct send_ctx *sctx,if (unlikely (ret >0 )) {btrfs_print_tree (path->nodes[path->lowest_level], false);btrfs_err (root->fs_info, -"send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d" , - key->objectid, key->type, key->offset, +"send: key " BTRFS_KEY_FMT " not found in %s root %llu, lowest_level %d, slot %d" , +BTRFS_KEY_FMT_VALUE (key), (root == sctx->parent_root ?"parent" :"send" ),btrfs_root_id (root), path->lowest_level, path->slots[path->lowest_level]);
fs/btrfs/tree-checker.c +9 -12
@@ -1618,10 +1618,9 @@ static int check_extent_item(struct extent_buffer *leaf,if (unlikely (prev_end > key->objectid)) {extent_err (leaf, slot, -"previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]" , - prev_key->objectid, prev_key->type, - prev_key->offset, key->objectid, key->type, - key->offset); +"previous extent " BTRFS_KEY_FMT " overlaps current extent " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (prev_key), +BTRFS_KEY_FMT_VALUE (key));return -EUCLEAN ; } }
@@ -2060,10 +2059,9 @@ enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)/* Make sure the keys are in the right order */ if (unlikely (btrfs_comp_cpu_keys (&prev_key, &key) >=0 )) {generic_err (leaf, slot, -"bad key order, prev (%llu %u %llu) current (%llu %u %llu)" , - prev_key.objectid, prev_key.type, - prev_key.offset, key.objectid, key.type, - key.offset); +"bad key order, prev " BTRFS_KEY_FMT " current " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&prev_key), +BTRFS_KEY_FMT_VALUE (&key));return BTRFS_TREE_BLOCK_BAD_KEY_ORDER ; }
@@ -2181,10 +2179,9 @@ enum btrfs_tree_block_status __btrfs_check_node(struct extent_buffer *node)if (unlikely (btrfs_comp_cpu_keys (&key, &next_key) >=0 )) {generic_err (node, slot, -"bad key order, current (%llu %u %llu) next (%llu %u %llu)" , - key.objectid, key.type, key.offset, - next_key.objectid, next_key.type, - next_key.offset); +"bad key order, current " BTRFS_KEY_FMT " next " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&key), +BTRFS_KEY_FMT_VALUE (&next_key));return BTRFS_TREE_BLOCK_BAD_KEY_ORDER ; } }
fs/btrfs/tree-log.c +18 -20
@@ -198,9 +198,9 @@ static void do_abort_log_replay(struct walk_control *wc, const char *function,if (wc->log_leaf) {btrfs_crit (fs_info, -"log tree (for root %llu) leaf currently being processed (slot %d key %llu %u %llu):" , +"log tree (for root %llu) leaf currently being processed (slot %d key " BTRFS_KEY_FMT "):" ,btrfs_root_id (wc->root), wc->log_slot, - wc->log_key.objectid, wc->log_key.type, wc->log_key.offset); +BTRFS_KEY_FMT_VALUE (&wc->log_key));btrfs_print_leaf (wc->log_leaf); }
@@ -510,9 +510,9 @@ static int overwrite_item(struct walk_control *wc) ret =btrfs_search_slot (NULL , root, &wc->log_key, wc->subvol_path,0 ,0 );if (ret <0 ) {btrfs_abort_log_replay (wc, ret, -"failed to search subvolume tree for key (%llu %u %llu) root %llu" , - wc->log_key.objectid, wc->log_key.type, - wc->log_key.offset,btrfs_root_id (root)); +"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu" , +BTRFS_KEY_FMT_VALUE (&wc->log_key), +btrfs_root_id (root));return ret; }
@@ -618,9 +618,8 @@ static int overwrite_item(struct walk_control *wc)btrfs_extend_item (trans, wc->subvol_path, item_size - found_size); }else if (ret) {btrfs_abort_log_replay (wc, ret, -"failed to insert item for key (%llu %u %llu)" , - wc->log_key.objectid, wc->log_key.type, - wc->log_key.offset); +"failed to insert item for key " BTRFS_KEY_FMT , +BTRFS_KEY_FMT_VALUE (&wc->log_key));return ret; } dst_ptr =btrfs_item_ptr_offset (dst_eb, dst_slot);
@@ -829,9 +828,9 @@ static noinline int replay_one_extent(struct walk_control *wc) &wc->log_key, sizeof(*item));if (ret) {btrfs_abort_log_replay (wc, ret, -"failed to insert item with key (%llu %u %llu) root %llu" , - wc->log_key.objectid, wc->log_key.type, - wc->log_key.offset,btrfs_root_id (root)); +"failed to insert item with key " BTRFS_KEY_FMT " root %llu" , +BTRFS_KEY_FMT_VALUE (&wc->log_key), +btrfs_root_id (root)); goto out; } dest_offset = btrfs_item_ptr_offset(wc->subvol_path->nodes[0],
@@ -1348,9 +1347,9 @@ static inline int __add_inode_ref(struct walk_control *wc, ret =btrfs_search_slot (NULL , root, &search_key, wc->subvol_path,0 ,0 );if (ret <0 ) {btrfs_abort_log_replay (wc, ret, -"failed to search subvolume tree for key (%llu %u %llu) root %llu" , - search_key.objectid, search_key.type, - search_key.offset,btrfs_root_id (root)); +"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu" , +BTRFS_KEY_FMT_VALUE (&search_key), +btrfs_root_id (root));return ret; }else if (ret ==0 ) {/*
@@ -1483,9 +1482,9 @@ static int unlink_old_inode_refs(struct walk_control *wc, struct btrfs_inode *in }if (ret <0 ) {btrfs_abort_log_replay (wc, ret, -"failed to search subvolume tree for key (%llu %u %llu) root %llu" , - wc->log_key.objectid, wc->log_key.type, - wc->log_key.offset,btrfs_root_id (root)); +"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu" , +BTRFS_KEY_FMT_VALUE (&wc->log_key), +btrfs_root_id (root)); goto out; }
@@ -2700,10 +2699,9 @@ static noinline int replay_dir_deletes(struct walk_control *wc, wc->subvol_path, 0, 0);if (ret <0 ) {btrfs_abort_log_replay (wc, ret, -"failed to search root %llu for key (%llu %u %llu)" , +"failed to search root %llu for key " BTRFS_KEY_FMT ,btrfs_root_id (root), - dir_key.objectid, dir_key.type, - dir_key.offset); +BTRFS_KEY_FMT_VALUE (&dir_key)); goto out; }
Signature · 1 line
2.47.2
Quoted 7 lines · On Wed, Oct 15, 2025 at 01:21:19PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Details in the change logs.
Filipe Manana (2):
btrfs: add macros to facilitate printing of keys
btrfs: use the key format macros when printing keys
Nice. I was thinking about naming it only KEY_FMT, as we have CSUM_FMT without the prefix but BTRFS_KEY_FMT is more clear, so maybe we should use it in csum format as well for consistency.
Quoted 3 lines · 在 2025/10/15 22:51, [email protected] 写道:
From: Filipe Manana <[email protected]>
Details in the change logs.
Thanks,
Qu
Quoted 19 lines
Filipe Manana (2):
btrfs: add macros to facilitate printing of keys
btrfs: use the key format macros when printing keysfs/btrfs/backref.c | 11 +++++------ fs/btrfs/ctree.c | 17 +++++++---------- fs/btrfs/extent-tree.c | 14 +++++++------- fs/btrfs/fs.h | 3 +++ fs/btrfs/inode.c | 4 ++-- fs/btrfs/print-tree.c | 14 ++++++-------- fs/btrfs/qgroup.c | 6 ++---- fs/btrfs/relocation.c | 4 ++-- fs/btrfs/root-tree.c | 4 ++-- fs/btrfs/send.c | 10 ++++------ fs/btrfs/tree-checker.c | 21 +++++++++------------ fs/btrfs/tree-log.c | 38 ++++++++++++++++++-------------------- 12 files changed, 67 insertions(+), 79 deletions(-)