1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/bitmap.h>
8 #include <linux/sched.h>
9 #include <linux/wait.h>
10 #include <linux/math64.h>
11 #include <linux/rbtree.h>
14 * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
16 #define ENUM_BIT(name) \
18 name = (1U << __ ## name ## _BIT), \
19 __ ## name ## _SEQ = __ ## name ## _BIT
21 static inline void cond_wake_up(struct wait_queue_head
*wq
)
24 * This implies a full smp_mb barrier, see comments for
25 * waitqueue_active why.
27 if (wq_has_sleeper(wq
))
31 static inline void cond_wake_up_nomb(struct wait_queue_head
*wq
)
34 * Special case for conditional wakeup where the barrier required for
35 * waitqueue_active is implied by some of the preceding code. Eg. one
36 * of such atomic operations (atomic_dec_and_return, ...), or a
37 * unlock/lock sequence, etc.
39 if (waitqueue_active(wq
))
43 static inline u64
mult_perc(u64 num
, u32 percent
)
45 return div_u64(num
* percent
, 100);
47 /* Copy of is_power_of_two that is 64bit safe */
48 static inline bool is_power_of_two_u64(u64 n
)
50 return n
!= 0 && (n
& (n
- 1)) == 0;
53 static inline bool has_single_bit_set(u64 n
)
55 return is_power_of_two_u64(n
);
59 * Simple bytenr based rb_tree relate structures
61 * Any structure wants to use bytenr as single search index should have their
62 * structure start with these members.
64 struct rb_simple_node
{
65 struct rb_node rb_node
;
69 static inline struct rb_node
*rb_simple_search(const struct rb_root
*root
, u64 bytenr
)
71 struct rb_node
*node
= root
->rb_node
;
72 struct rb_simple_node
*entry
;
75 entry
= rb_entry(node
, struct rb_simple_node
, rb_node
);
77 if (bytenr
< entry
->bytenr
)
79 else if (bytenr
> entry
->bytenr
)
80 node
= node
->rb_right
;
88 * Search @root from an entry that starts or comes after @bytenr.
90 * @root: the root to search.
91 * @bytenr: bytenr to search from.
93 * Return the rb_node that start at or after @bytenr. If there is no entry at
94 * or after @bytner return NULL.
96 static inline struct rb_node
*rb_simple_search_first(const struct rb_root
*root
,
99 struct rb_node
*node
= root
->rb_node
, *ret
= NULL
;
100 struct rb_simple_node
*entry
, *ret_entry
= NULL
;
103 entry
= rb_entry(node
, struct rb_simple_node
, rb_node
);
105 if (bytenr
< entry
->bytenr
) {
106 if (!ret
|| entry
->bytenr
< ret_entry
->bytenr
) {
111 node
= node
->rb_left
;
112 } else if (bytenr
> entry
->bytenr
) {
113 node
= node
->rb_right
;
122 static inline struct rb_node
*rb_simple_insert(struct rb_root
*root
, u64 bytenr
,
123 struct rb_node
*node
)
125 struct rb_node
**p
= &root
->rb_node
;
126 struct rb_node
*parent
= NULL
;
127 struct rb_simple_node
*entry
;
131 entry
= rb_entry(parent
, struct rb_simple_node
, rb_node
);
133 if (bytenr
< entry
->bytenr
)
135 else if (bytenr
> entry
->bytenr
)
141 rb_link_node(node
, parent
, p
);
142 rb_insert_color(node
, root
);
146 static inline bool bitmap_test_range_all_set(const unsigned long *addr
,
150 unsigned long found_zero
;
152 found_zero
= find_next_zero_bit(addr
, start
+ nbits
, start
);
153 return (found_zero
== start
+ nbits
);
156 static inline bool bitmap_test_range_all_zero(const unsigned long *addr
,
160 unsigned long found_set
;
162 found_set
= find_next_bit(addr
, start
+ nbits
, start
);
163 return (found_set
== start
+ nbits
);