Linux 5.8-rc4
[linux/fpc-iii.git] / fs / btrfs / misc.h
blob6461ebc3a1c15d2c5aab49c964a79b2d93cb30be
1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef BTRFS_MISC_H
4 #define BTRFS_MISC_H
6 #include <linux/sched.h>
7 #include <linux/wait.h>
8 #include <asm/div64.h>
9 #include <linux/rbtree.h>
11 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
13 static inline void cond_wake_up(struct wait_queue_head *wq)
16 * This implies a full smp_mb barrier, see comments for
17 * waitqueue_active why.
19 if (wq_has_sleeper(wq))
20 wake_up(wq);
23 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
26 * Special case for conditional wakeup where the barrier required for
27 * waitqueue_active is implied by some of the preceding code. Eg. one
28 * of such atomic operations (atomic_dec_and_return, ...), or a
29 * unlock/lock sequence, etc.
31 if (waitqueue_active(wq))
32 wake_up(wq);
35 static inline u64 div_factor(u64 num, int factor)
37 if (factor == 10)
38 return num;
39 num *= factor;
40 return div_u64(num, 10);
43 static inline u64 div_factor_fine(u64 num, int factor)
45 if (factor == 100)
46 return num;
47 num *= factor;
48 return div_u64(num, 100);
51 /* Copy of is_power_of_two that is 64bit safe */
52 static inline bool is_power_of_two_u64(u64 n)
54 return n != 0 && (n & (n - 1)) == 0;
57 static inline bool has_single_bit_set(u64 n)
59 return is_power_of_two_u64(n);
63 * Simple bytenr based rb_tree relate structures
65 * Any structure wants to use bytenr as single search index should have their
66 * structure start with these members.
68 struct rb_simple_node {
69 struct rb_node rb_node;
70 u64 bytenr;
73 static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
75 struct rb_node *node = root->rb_node;
76 struct rb_simple_node *entry;
78 while (node) {
79 entry = rb_entry(node, struct rb_simple_node, rb_node);
81 if (bytenr < entry->bytenr)
82 node = node->rb_left;
83 else if (bytenr > entry->bytenr)
84 node = node->rb_right;
85 else
86 return node;
88 return NULL;
91 static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
92 struct rb_node *node)
94 struct rb_node **p = &root->rb_node;
95 struct rb_node *parent = NULL;
96 struct rb_simple_node *entry;
98 while (*p) {
99 parent = *p;
100 entry = rb_entry(parent, struct rb_simple_node, rb_node);
102 if (bytenr < entry->bytenr)
103 p = &(*p)->rb_left;
104 else if (bytenr > entry->bytenr)
105 p = &(*p)->rb_right;
106 else
107 return parent;
110 rb_link_node(node, parent, p);
111 rb_insert_color(node, root);
112 return NULL;
115 #endif