0001
0002
0003 #ifndef BTRFS_MISC_H
0004 #define BTRFS_MISC_H
0005
0006 #include <linux/sched.h>
0007 #include <linux/wait.h>
0008 #include <linux/math64.h>
0009 #include <linux/rbtree.h>
0010
0011 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
0012
0013 static inline void cond_wake_up(struct wait_queue_head *wq)
0014 {
0015
0016
0017
0018
0019 if (wq_has_sleeper(wq))
0020 wake_up(wq);
0021 }
0022
0023 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
0024 {
0025
0026
0027
0028
0029
0030
0031 if (waitqueue_active(wq))
0032 wake_up(wq);
0033 }
0034
0035 static inline u64 div_factor(u64 num, int factor)
0036 {
0037 if (factor == 10)
0038 return num;
0039 num *= factor;
0040 return div_u64(num, 10);
0041 }
0042
0043 static inline u64 div_factor_fine(u64 num, int factor)
0044 {
0045 if (factor == 100)
0046 return num;
0047 num *= factor;
0048 return div_u64(num, 100);
0049 }
0050
0051
0052 static inline bool is_power_of_two_u64(u64 n)
0053 {
0054 return n != 0 && (n & (n - 1)) == 0;
0055 }
0056
0057 static inline bool has_single_bit_set(u64 n)
0058 {
0059 return is_power_of_two_u64(n);
0060 }
0061
0062
0063
0064
0065
0066
0067
0068 struct rb_simple_node {
0069 struct rb_node rb_node;
0070 u64 bytenr;
0071 };
0072
0073 static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
0074 {
0075 struct rb_node *node = root->rb_node;
0076 struct rb_simple_node *entry;
0077
0078 while (node) {
0079 entry = rb_entry(node, struct rb_simple_node, rb_node);
0080
0081 if (bytenr < entry->bytenr)
0082 node = node->rb_left;
0083 else if (bytenr > entry->bytenr)
0084 node = node->rb_right;
0085 else
0086 return node;
0087 }
0088 return NULL;
0089 }
0090
0091 static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
0092 struct rb_node *node)
0093 {
0094 struct rb_node **p = &root->rb_node;
0095 struct rb_node *parent = NULL;
0096 struct rb_simple_node *entry;
0097
0098 while (*p) {
0099 parent = *p;
0100 entry = rb_entry(parent, struct rb_simple_node, rb_node);
0101
0102 if (bytenr < entry->bytenr)
0103 p = &(*p)->rb_left;
0104 else if (bytenr > entry->bytenr)
0105 p = &(*p)->rb_right;
0106 else
0107 return parent;
0108 }
0109
0110 rb_link_node(node, parent, p);
0111 rb_insert_color(node, root);
0112 return NULL;
0113 }
0114
0115 #endif