misc.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/math64.h>
  11. #include <linux/rbtree.h>
  12. #include <linux/bio.h>
  13. /*
  14. * Convenience macros to define a pointer with the __free(kfree) and
  15. * __free(kvfree) cleanup attributes and initialized to NULL.
  16. */
  17. #define AUTO_KFREE(name) *name __free(kfree) = NULL
  18. #define AUTO_KVFREE(name) *name __free(kvfree) = NULL
  19. /*
  20. * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
  21. */
  22. #define ENUM_BIT(name) \
  23. __ ## name ## _BIT, \
  24. name = (1U << __ ## name ## _BIT), \
  25. __ ## name ## _SEQ = __ ## name ## _BIT
  26. static inline phys_addr_t bio_iter_phys(struct bio *bio, struct bvec_iter *iter)
  27. {
  28. struct bio_vec bv = bio_iter_iovec(bio, *iter);
  29. return bvec_phys(&bv);
  30. }
  31. /*
  32. * Iterate bio using btrfs block size.
  33. *
  34. * This will handle large folio and highmem.
  35. *
  36. * @paddr: Physical memory address of each iteration
  37. * @bio: The bio to iterate
  38. * @iter: The bvec_iter (pointer) to use.
  39. * @blocksize: The blocksize to iterate.
  40. *
  41. * This requires all folios in the bio to cover at least one block.
  42. */
  43. #define btrfs_bio_for_each_block(paddr, bio, iter, blocksize) \
  44. for (; (iter)->bi_size && \
  45. (paddr = bio_iter_phys((bio), (iter)), 1); \
  46. bio_advance_iter_single((bio), (iter), (blocksize)))
  47. /* Initialize a bvec_iter to the size of the specified bio. */
  48. static inline struct bvec_iter init_bvec_iter_for_bio(struct bio *bio)
  49. {
  50. struct bio_vec *bvec;
  51. u32 bio_size = 0;
  52. int i;
  53. bio_for_each_bvec_all(bvec, bio, i)
  54. bio_size += bvec->bv_len;
  55. return (struct bvec_iter) {
  56. .bi_sector = 0,
  57. .bi_size = bio_size,
  58. .bi_idx = 0,
  59. .bi_bvec_done = 0,
  60. };
  61. }
  62. #define btrfs_bio_for_each_block_all(paddr, bio, blocksize) \
  63. for (struct bvec_iter iter = init_bvec_iter_for_bio(bio); \
  64. (iter).bi_size && \
  65. (paddr = bio_iter_phys((bio), &(iter)), 1); \
  66. bio_advance_iter_single((bio), &(iter), (blocksize)))
  67. static inline void cond_wake_up(struct wait_queue_head *wq)
  68. {
  69. /*
  70. * This implies a full smp_mb barrier, see comments for
  71. * waitqueue_active why.
  72. */
  73. if (wq_has_sleeper(wq))
  74. wake_up(wq);
  75. }
  76. static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
  77. {
  78. /*
  79. * Special case for conditional wakeup where the barrier required for
  80. * waitqueue_active is implied by some of the preceding code. Eg. one
  81. * of such atomic operations (atomic_dec_and_return, ...), or a
  82. * unlock/lock sequence, etc.
  83. */
  84. if (waitqueue_active(wq))
  85. wake_up(wq);
  86. }
  87. static inline u64 mult_perc(u64 num, u32 percent)
  88. {
  89. return div_u64(num * percent, 100);
  90. }
  91. /* Copy of is_power_of_two that is 64bit safe */
  92. static inline bool is_power_of_two_u64(u64 n)
  93. {
  94. return n != 0 && (n & (n - 1)) == 0;
  95. }
  96. static inline bool has_single_bit_set(u64 n)
  97. {
  98. return is_power_of_two_u64(n);
  99. }
  100. /*
  101. * Simple bytenr based rb_tree relate structures
  102. *
  103. * Any structure wants to use bytenr as single search index should have their
  104. * structure start with these members.
  105. */
  106. struct rb_simple_node {
  107. struct rb_node rb_node;
  108. u64 bytenr;
  109. };
  110. static inline struct rb_node *rb_simple_search(const struct rb_root *root, u64 bytenr)
  111. {
  112. struct rb_node *node = root->rb_node;
  113. struct rb_simple_node *entry;
  114. while (node) {
  115. entry = rb_entry(node, struct rb_simple_node, rb_node);
  116. if (bytenr < entry->bytenr)
  117. node = node->rb_left;
  118. else if (bytenr > entry->bytenr)
  119. node = node->rb_right;
  120. else
  121. return node;
  122. }
  123. return NULL;
  124. }
  125. /*
  126. * Search @root from an entry that starts or comes after @bytenr.
  127. *
  128. * @root: the root to search.
  129. * @bytenr: bytenr to search from.
  130. *
  131. * Return the rb_node that start at or after @bytenr. If there is no entry at
  132. * or after @bytner return NULL.
  133. */
  134. static inline struct rb_node *rb_simple_search_first(const struct rb_root *root,
  135. u64 bytenr)
  136. {
  137. struct rb_node *node = root->rb_node, *ret = NULL;
  138. struct rb_simple_node *entry, *ret_entry = NULL;
  139. while (node) {
  140. entry = rb_entry(node, struct rb_simple_node, rb_node);
  141. if (bytenr < entry->bytenr) {
  142. if (!ret || entry->bytenr < ret_entry->bytenr) {
  143. ret = node;
  144. ret_entry = entry;
  145. }
  146. node = node->rb_left;
  147. } else if (bytenr > entry->bytenr) {
  148. node = node->rb_right;
  149. } else {
  150. return node;
  151. }
  152. }
  153. return ret;
  154. }
  155. static int rb_simple_node_bytenr_cmp(struct rb_node *new, const struct rb_node *existing)
  156. {
  157. struct rb_simple_node *new_entry = rb_entry(new, struct rb_simple_node, rb_node);
  158. struct rb_simple_node *existing_entry = rb_entry(existing, struct rb_simple_node, rb_node);
  159. if (new_entry->bytenr < existing_entry->bytenr)
  160. return -1;
  161. else if (new_entry->bytenr > existing_entry->bytenr)
  162. return 1;
  163. return 0;
  164. }
  165. static inline struct rb_node *rb_simple_insert(struct rb_root *root,
  166. struct rb_simple_node *simple_node)
  167. {
  168. return rb_find_add(&simple_node->rb_node, root, rb_simple_node_bytenr_cmp);
  169. }
  170. static inline bool bitmap_test_range_all_set(const unsigned long *addr,
  171. unsigned long start,
  172. unsigned long nbits)
  173. {
  174. unsigned long found_zero;
  175. found_zero = find_next_zero_bit(addr, start + nbits, start);
  176. return (found_zero == start + nbits);
  177. }
  178. static inline bool bitmap_test_range_all_zero(const unsigned long *addr,
  179. unsigned long start,
  180. unsigned long nbits)
  181. {
  182. unsigned long found_set;
  183. found_set = find_next_bit(addr, start + nbits, start);
  184. return (found_set == start + nbits);
  185. }
  186. #endif