misc.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_MISC_H
  3. #define BTRFS_MISC_H
  4. #include <linux/sched.h>
  5. #include <linux/wait.h>
  6. #include <asm/div64.h>
  7. #include <linux/rbtree.h>
  8. #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
  9. static inline void cond_wake_up(struct wait_queue_head *wq)
  10. {
  11. /*
  12. * This implies a full smp_mb barrier, see comments for
  13. * waitqueue_active why.
  14. */
  15. if (wq_has_sleeper(wq))
  16. wake_up(wq);
  17. }
  18. static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
  19. {
  20. /*
  21. * Special case for conditional wakeup where the barrier required for
  22. * waitqueue_active is implied by some of the preceding code. Eg. one
  23. * of such atomic operations (atomic_dec_and_return, ...), or a
  24. * unlock/lock sequence, etc.
  25. */
  26. if (waitqueue_active(wq))
  27. wake_up(wq);
  28. }
  29. static inline u64 div_factor(u64 num, int factor)
  30. {
  31. if (factor == 10)
  32. return num;
  33. num *= factor;
  34. return div_u64(num, 10);
  35. }
  36. static inline u64 div_factor_fine(u64 num, int factor)
  37. {
  38. if (factor == 100)
  39. return num;
  40. num *= factor;
  41. return div_u64(num, 100);
  42. }
  43. /* Copy of is_power_of_two that is 64bit safe */
  44. static inline bool is_power_of_two_u64(u64 n)
  45. {
  46. return n != 0 && (n & (n - 1)) == 0;
  47. }
  48. static inline bool has_single_bit_set(u64 n)
  49. {
  50. return is_power_of_two_u64(n);
  51. }
  52. /*
  53. * Simple bytenr based rb_tree relate structures
  54. *
  55. * Any structure wants to use bytenr as single search index should have their
  56. * structure start with these members.
  57. */
  58. struct rb_simple_node {
  59. struct rb_node rb_node;
  60. u64 bytenr;
  61. };
  62. static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
  63. {
  64. struct rb_node *node = root->rb_node;
  65. struct rb_simple_node *entry;
  66. while (node) {
  67. entry = rb_entry(node, struct rb_simple_node, rb_node);
  68. if (bytenr < entry->bytenr)
  69. node = node->rb_left;
  70. else if (bytenr > entry->bytenr)
  71. node = node->rb_right;
  72. else
  73. return node;
  74. }
  75. return NULL;
  76. }
  77. static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
  78. struct rb_node *node)
  79. {
  80. struct rb_node **p = &root->rb_node;
  81. struct rb_node *parent = NULL;
  82. struct rb_simple_node *entry;
  83. while (*p) {
  84. parent = *p;
  85. entry = rb_entry(parent, struct rb_simple_node, rb_node);
  86. if (bytenr < entry->bytenr)
  87. p = &(*p)->rb_left;
  88. else if (bytenr > entry->bytenr)
  89. p = &(*p)->rb_right;
  90. else
  91. return parent;
  92. }
  93. rb_link_node(node, parent, p);
  94. rb_insert_color(node, root);
  95. return NULL;
  96. }
  97. #endif