From 1c551e22cf90dfa3ceff6a49898a33120203ffd9 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Tue, 12 Mar 2024 21:43:54 +0100 Subject: [PATCH] btrfs-progs: make all parameters of rb_tree search/insert const Tree comparators never change parameters, make them all const and also change the rb-tree prototypes. Signed-off-by: David Sterba --- check/main.c | 44 ++++++++++++++++++++--------------------- common/extent-cache.c | 24 +++++++++++----------- common/rbtree-utils.c | 2 +- common/rbtree-utils.h | 9 ++++----- kernel-shared/disk-io.c | 28 +++++++++++++------------- kernel-shared/disk-io.h | 2 +- 6 files changed, 54 insertions(+), 55 deletions(-) diff --git a/check/main.c b/check/main.c index ac120ddf..a7439d61 100644 --- a/check/main.c +++ b/check/main.c @@ -114,12 +114,12 @@ struct device_record { bool bad_block_dev_size; }; -static int compare_data_backref(struct rb_node *node1, struct rb_node *node2) +static int compare_data_backref(const struct rb_node *node1, const struct rb_node *node2) { - struct extent_backref *ext1 = rb_node_to_extent_backref(node1); - struct extent_backref *ext2 = rb_node_to_extent_backref(node2); - struct data_backref *back1 = to_data_backref(ext1); - struct data_backref *back2 = to_data_backref(ext2); + const struct extent_backref *ext1 = rb_entry(node1, struct extent_backref, node); + const struct extent_backref *ext2 = rb_entry(node2, struct extent_backref, node); + const struct data_backref *back1 = container_of(ext1, struct data_backref, node); + const struct data_backref *back2 = container_of(ext2, struct data_backref, node); WARN_ON(!ext1->is_data); WARN_ON(!ext2->is_data); @@ -159,12 +159,12 @@ static int compare_data_backref(struct rb_node *node1, struct rb_node *node2) return 0; } -static int compare_tree_backref(struct rb_node *node1, struct rb_node *node2) +static int compare_tree_backref(const struct rb_node *node1, const struct rb_node *node2) { - struct extent_backref *ext1 = rb_node_to_extent_backref(node1); - struct extent_backref *ext2 = rb_node_to_extent_backref(node2); - struct tree_backref *back1 = to_tree_backref(ext1); - struct tree_backref *back2 = to_tree_backref(ext2); + const struct extent_backref *ext1 = rb_entry(node1, struct extent_backref, node); + const struct extent_backref *ext2 = rb_entry(node2, struct extent_backref, node); + const struct data_backref *back1 = container_of(ext1, struct data_backref, node); + const struct data_backref *back2 = container_of(ext2, struct data_backref, node); WARN_ON(ext1->is_data); WARN_ON(ext2->is_data); @@ -178,10 +178,10 @@ static int compare_tree_backref(struct rb_node *node1, struct rb_node *node2) return 0; } -static int compare_extent_backref(struct rb_node *node1, struct rb_node *node2) +static int compare_extent_backref(const struct rb_node *node1, const struct rb_node *node2) { - struct extent_backref *ext1 = rb_node_to_extent_backref(node1); - struct extent_backref *ext2 = rb_node_to_extent_backref(node2); + const struct extent_backref *ext1 = rb_entry(node1, struct extent_backref, node); + const struct extent_backref *ext2 = rb_entry(node2, struct extent_backref, node); if (ext1->is_data > ext2->is_data) return 1; @@ -287,10 +287,10 @@ static u64 first_extent_gap(struct rb_root *holes) return hole->start; } -static int compare_hole(struct rb_node *node1, struct rb_node *node2) +static int compare_hole(const struct rb_node *node1, const struct rb_node *node2) { - struct file_extent_hole *hole1; - struct file_extent_hole *hole2; + const struct file_extent_hole *hole1; + const struct file_extent_hole *hole2; hole1 = rb_entry(node1, struct file_extent_hole, node); hole2 = rb_entry(node2, struct file_extent_hole, node); @@ -362,12 +362,12 @@ static int add_file_extent_hole(struct rb_root *holes, return 0; } -static int compare_hole_range(struct rb_node *node, void *data) +static int compare_hole_range(const struct rb_node *node, const void *data) { - struct file_extent_hole *hole; + const struct file_extent_hole *hole; u64 start; - hole = (struct file_extent_hole *)data; + hole = (const struct file_extent_hole *)data; start = hole->start; hole = rb_entry(node, struct file_extent_hole, node); @@ -478,10 +478,10 @@ static void record_root_in_trans(struct btrfs_trans_handle *trans, } } -static int device_record_compare(struct rb_node *node1, struct rb_node *node2) +static int device_record_compare(const struct rb_node *node1, const struct rb_node *node2) { - struct device_record *rec1; - struct device_record *rec2; + const struct device_record *rec1; + const struct device_record *rec2; rec1 = rb_entry(node1, struct device_record, node); rec2 = rb_entry(node2, struct device_record, node); diff --git a/common/extent-cache.c b/common/extent-cache.c index 4f6d48cd..4434fdca 100644 --- a/common/extent-cache.c +++ b/common/extent-cache.c @@ -29,12 +29,12 @@ struct cache_extent_search_range { u64 size; }; -static int cache_tree_comp_range(struct rb_node *node, void *data) +static int cache_tree_comp_range(const struct rb_node *node, const void *data) { - struct cache_extent *entry; - struct cache_extent_search_range *range; + const struct cache_extent *entry; + const struct cache_extent_search_range *range; - range = (struct cache_extent_search_range *)data; + range = (const struct cache_extent_search_range *)data; entry = rb_entry(node, struct cache_extent, rb_node); if (entry->start + entry->size <= range->start) @@ -45,9 +45,9 @@ static int cache_tree_comp_range(struct rb_node *node, void *data) return 0; } -static int cache_tree_comp_nodes(struct rb_node *node1, struct rb_node *node2) +static int cache_tree_comp_nodes(const struct rb_node *node1, const struct rb_node *node2) { - struct cache_extent *entry; + const struct cache_extent *entry; struct cache_extent_search_range range; entry = rb_entry(node2, struct cache_extent, rb_node); @@ -57,10 +57,10 @@ static int cache_tree_comp_nodes(struct rb_node *node1, struct rb_node *node2) return cache_tree_comp_range(node1, (void *)&range); } -static int cache_tree_comp_range2(struct rb_node *node, void *data) +static int cache_tree_comp_range2(const struct rb_node *node, const void *data) { - struct cache_extent *entry; - struct cache_extent_search_range *range; + const struct cache_extent *entry; + const struct cache_extent_search_range *range; range = (struct cache_extent_search_range *)data; entry = rb_entry(node, struct cache_extent, rb_node); @@ -77,9 +77,9 @@ static int cache_tree_comp_range2(struct rb_node *node, void *data) return 0; } -static int cache_tree_comp_nodes2(struct rb_node *node1, struct rb_node *node2) +static int cache_tree_comp_nodes2(const struct rb_node *node1, const struct rb_node *node2) { - struct cache_extent *entry; + const struct cache_extent *entry; struct cache_extent_search_range range; entry = rb_entry(node2, struct cache_extent, rb_node); @@ -87,7 +87,7 @@ static int cache_tree_comp_nodes2(struct rb_node *node1, struct rb_node *node2) range.start = entry->start; range.size = entry->size; - return cache_tree_comp_range2(node1, (void *)&range); + return cache_tree_comp_range2(node1, &range); } void cache_tree_init(struct cache_tree *tree) diff --git a/common/rbtree-utils.c b/common/rbtree-utils.c index 5aa9b433..61ade288 100644 --- a/common/rbtree-utils.c +++ b/common/rbtree-utils.c @@ -46,7 +46,7 @@ int rb_insert(struct rb_root *root, struct rb_node *node, return 0; } -struct rb_node *rb_search(struct rb_root *root, void *key, rb_compare_keys comp, +struct rb_node *rb_search(struct rb_root *root, const void *key, rb_compare_keys comp, struct rb_node **next_ret) { struct rb_node *n = root->rb_node; diff --git a/common/rbtree-utils.h b/common/rbtree-utils.h index 4006c684..1f288b0f 100644 --- a/common/rbtree-utils.h +++ b/common/rbtree-utils.h @@ -23,17 +23,16 @@ struct rb_node; struct rb_root; /* The common insert/search/free functions */ -typedef int (*rb_compare_nodes)(struct rb_node *node1, struct rb_node *node2); -typedef int (*rb_compare_keys)(struct rb_node *node, void *key); +typedef int (*rb_compare_nodes)(const struct rb_node *node1, const struct rb_node *node2); +typedef int (*rb_compare_keys)(const struct rb_node *node, const void *key); typedef void (*rb_free_node)(struct rb_node *node); -int rb_insert(struct rb_root *root, struct rb_node *node, - rb_compare_nodes comp); +int rb_insert(struct rb_root *root, struct rb_node *node, rb_compare_nodes comp); /* * In some cases, we need return the next node if we don't find the node we * specify. At this time, we can use next_ret. */ -struct rb_node *rb_search(struct rb_root *root, void *key, rb_compare_keys comp, +struct rb_node *rb_search(struct rb_root *root, const void *key, rb_compare_keys comp, struct rb_node **next_ret); void rb_free_nodes(struct rb_root *root, rb_free_node free_node); diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index 05323b2c..295bd50a 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c @@ -673,28 +673,28 @@ insert: return root; } -static int btrfs_global_roots_compare_keys(struct rb_node *node, - void *data) +static int btrfs_global_roots_compare_keys(const struct rb_node *node, + const void *data) { - struct btrfs_key *key = (struct btrfs_key *)data; - struct btrfs_root *root; + const struct btrfs_key *key = (struct btrfs_key *)data; + const struct btrfs_root *root; root = rb_entry(node, struct btrfs_root, rb_node); return btrfs_comp_cpu_keys(key, &root->root_key); } -static int btrfs_global_roots_compare(struct rb_node *node1, - struct rb_node *node2) +static int btrfs_global_roots_compare(const struct rb_node *node1, + const struct rb_node *node2) { - struct btrfs_root *root = rb_entry(node2, struct btrfs_root, rb_node); + const struct btrfs_root *root = rb_entry(node2, struct btrfs_root, rb_node); return btrfs_global_roots_compare_keys(node1, &root->root_key); } -static int btrfs_fs_roots_compare_objectids(struct rb_node *node, - void *data) +static int btrfs_fs_roots_compare_objectids(const struct rb_node *node, + const void *data) { - u64 objectid = *((u64 *)data); - struct btrfs_root *root; + u64 objectid = *((const u64 *)data); + const struct btrfs_root *root; root = rb_entry(node, struct btrfs_root, rb_node); if (objectid > root->objectid) @@ -705,12 +705,12 @@ static int btrfs_fs_roots_compare_objectids(struct rb_node *node, return 0; } -int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2) +int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_node *node2) { - struct btrfs_root *root; + const struct btrfs_root *root; root = rb_entry(node2, struct btrfs_root, rb_node); - return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid); + return btrfs_fs_roots_compare_objectids(node1, &root->objectid); } int btrfs_global_root_insert(struct btrfs_fs_info *fs_info, diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h index 68cdf5b0..647fccd9 100644 --- a/kernel-shared/disk-io.h +++ b/kernel-shared/disk-io.h @@ -237,7 +237,7 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid); int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, struct extent_buffer *eb); -int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2); +int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_node *node2); struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, struct btrfs_key *key);