misc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_MISC_H
  3. #define BTRFS_MISC_H
  4. #include <linux/types.h>
  5. #include <linux/bitmap.h>
  6. #include <linux/sched.h>
  7. #include <linux/wait.h>
  8. #include <linux/math64.h>
  9. #include <linux/rbtree.h>
  10. /*
  11. * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
  12. */
  13. #define ENUM_BIT(name) \
  14. __ ## name ## _BIT, \
  15. name = (1U << __ ## name ## _BIT), \
  16. __ ## name ## _SEQ = __ ## name ## _BIT
  17. static inline void cond_wake_up(struct wait_queue_head *wq)
  18. {
  19. /*
  20. * This implies a full smp_mb barrier, see comments for
  21. * waitqueue_active why.
  22. */
  23. if (wq_has_sleeper(wq))
  24. wake_up(wq);
  25. }
  26. static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
  27. {
  28. /*
  29. * Special case for conditional wakeup where the barrier required for
  30. * waitqueue_active is implied by some of the preceding code. Eg. one
  31. * of such atomic operations (atomic_dec_and_return, ...), or a
  32. * unlock/lock sequence, etc.
  33. */
  34. if (waitqueue_active(wq))
  35. wake_up(wq);
  36. }
  37. static inline u64 mult_perc(u64 num, u32 percent)
  38. {
  39. return div_u64(num * percent, 100);
  40. }
  41. /* Copy of is_power_of_two that is 64bit safe */
  42. static inline bool is_power_of_two_u64(u64 n)
  43. {
  44. return n != 0 && (n & (n - 1)) == 0;
  45. }
  46. static inline bool has_single_bit_set(u64 n)
  47. {
  48. return is_power_of_two_u64(n);
  49. }
  50. /*
  51. * Simple bytenr based rb_tree relate structures
  52. *
  53. * Any structure wants to use bytenr as single search index should have their
  54. * structure start with these members.
  55. */
  56. struct rb_simple_node {
  57. struct rb_node rb_node;
  58. u64 bytenr;
  59. };
  60. static inline struct rb_node *rb_simple_search(const struct rb_root *root, u64 bytenr)
  61. {
  62. struct rb_node *node = root->rb_node;
  63. struct rb_simple_node *entry;
  64. while (node) {
  65. entry = rb_entry(node, struct rb_simple_node, rb_node);
  66. if (bytenr < entry->bytenr)
  67. node = node->rb_left;
  68. else if (bytenr > entry->bytenr)
  69. node = node->rb_right;
  70. else
  71. return node;
  72. }
  73. return NULL;
  74. }
  75. /*
  76. * Search @root from an entry that starts or comes after @bytenr.
  77. *
  78. * @root: the root to search.
  79. * @bytenr: bytenr to search from.
  80. *
  81. * Return the rb_node that start at or after @bytenr. If there is no entry at
  82. * or after @bytner return NULL.
  83. */
  84. static inline struct rb_node *rb_simple_search_first(const struct rb_root *root,
  85. u64 bytenr)
  86. {
  87. struct rb_node *node = root->rb_node, *ret = NULL;
  88. struct rb_simple_node *entry, *ret_entry = NULL;
  89. while (node) {
  90. entry = rb_entry(node, struct rb_simple_node, rb_node);
  91. if (bytenr < entry->bytenr) {
  92. if (!ret || entry->bytenr < ret_entry->bytenr) {
  93. ret = node;
  94. ret_entry = entry;
  95. }
  96. node = node->rb_left;
  97. } else if (bytenr > entry->bytenr) {
  98. node = node->rb_right;
  99. } else {
  100. return node;
  101. }
  102. }
  103. return ret;
  104. }
  105. static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
  106. struct rb_node *node)
  107. {
  108. struct rb_node **p = &root->rb_node;
  109. struct rb_node *parent = NULL;
  110. struct rb_simple_node *entry;
  111. while (*p) {
  112. parent = *p;
  113. entry = rb_entry(parent, struct rb_simple_node, rb_node);
  114. if (bytenr < entry->bytenr)
  115. p = &(*p)->rb_left;
  116. else if (bytenr > entry->bytenr)
  117. p = &(*p)->rb_right;
  118. else
  119. return parent;
  120. }
  121. rb_link_node(node, parent, p);
  122. rb_insert_color(node, root);
  123. return NULL;
  124. }
  125. static inline bool bitmap_test_range_all_set(const unsigned long *addr,
  126. unsigned long start,
  127. unsigned long nbits)
  128. {
  129. unsigned long found_zero;
  130. found_zero = find_next_zero_bit(addr, start + nbits, start);
  131. return (found_zero == start + nbits);
  132. }
  133. static inline bool bitmap_test_range_all_zero(const unsigned long *addr,
  134. unsigned long start,
  135. unsigned long nbits)
  136. {
  137. unsigned long found_set;
  138. found_set = find_next_bit(addr, start + nbits, start);
  139. return (found_set == start + nbits);
  140. }
  141. #endif