drbd_interval.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <asm/bug.h>
  2. #include <linux/rbtree_augmented.h>
  3. #include "drbd_interval.h"
  4. /**
  5. * interval_end - return end of @node
  6. */
  7. static inline
  8. sector_t interval_end(struct rb_node *node)
  9. {
  10. struct drbd_interval *this = rb_entry(node, struct drbd_interval, rb);
  11. return this->end;
  12. }
  13. /**
  14. * compute_subtree_last - compute end of @node
  15. *
  16. * The end of an interval is the highest (start + (size >> 9)) value of this
  17. * node and of its children. Called for @node and its parents whenever the end
  18. * may have changed.
  19. */
  20. static inline sector_t
  21. compute_subtree_last(struct drbd_interval *node)
  22. {
  23. sector_t max = node->sector + (node->size >> 9);
  24. if (node->rb.rb_left) {
  25. sector_t left = interval_end(node->rb.rb_left);
  26. if (left > max)
  27. max = left;
  28. }
  29. if (node->rb.rb_right) {
  30. sector_t right = interval_end(node->rb.rb_right);
  31. if (right > max)
  32. max = right;
  33. }
  34. return max;
  35. }
  36. RB_DECLARE_CALLBACKS(static, augment_callbacks, struct drbd_interval, rb,
  37. sector_t, end, compute_subtree_last);
  38. /**
  39. * drbd_insert_interval - insert a new interval into a tree
  40. */
  41. bool
  42. drbd_insert_interval(struct rb_root *root, struct drbd_interval *this)
  43. {
  44. struct rb_node **new = &root->rb_node, *parent = NULL;
  45. sector_t this_end = this->sector + (this->size >> 9);
  46. BUG_ON(!IS_ALIGNED(this->size, 512));
  47. while (*new) {
  48. struct drbd_interval *here =
  49. rb_entry(*new, struct drbd_interval, rb);
  50. parent = *new;
  51. if (here->end < this_end)
  52. here->end = this_end;
  53. if (this->sector < here->sector)
  54. new = &(*new)->rb_left;
  55. else if (this->sector > here->sector)
  56. new = &(*new)->rb_right;
  57. else if (this < here)
  58. new = &(*new)->rb_left;
  59. else if (this > here)
  60. new = &(*new)->rb_right;
  61. else
  62. return false;
  63. }
  64. this->end = this_end;
  65. rb_link_node(&this->rb, parent, new);
  66. rb_insert_augmented(&this->rb, root, &augment_callbacks);
  67. return true;
  68. }
  69. /**
  70. * drbd_contains_interval - check if a tree contains a given interval
  71. * @sector: start sector of @interval
  72. * @interval: may not be a valid pointer
  73. *
  74. * Returns if the tree contains the node @interval with start sector @start.
  75. * Does not dereference @interval until @interval is known to be a valid object
  76. * in @tree. Returns %false if @interval is in the tree but with a different
  77. * sector number.
  78. */
  79. bool
  80. drbd_contains_interval(struct rb_root *root, sector_t sector,
  81. struct drbd_interval *interval)
  82. {
  83. struct rb_node *node = root->rb_node;
  84. while (node) {
  85. struct drbd_interval *here =
  86. rb_entry(node, struct drbd_interval, rb);
  87. if (sector < here->sector)
  88. node = node->rb_left;
  89. else if (sector > here->sector)
  90. node = node->rb_right;
  91. else if (interval < here)
  92. node = node->rb_left;
  93. else if (interval > here)
  94. node = node->rb_right;
  95. else
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * drbd_remove_interval - remove an interval from a tree
  102. */
  103. void
  104. drbd_remove_interval(struct rb_root *root, struct drbd_interval *this)
  105. {
  106. rb_erase_augmented(&this->rb, root, &augment_callbacks);
  107. }
  108. /**
  109. * drbd_find_overlap - search for an interval overlapping with [sector, sector + size)
  110. * @sector: start sector
  111. * @size: size, aligned to 512 bytes
  112. *
  113. * Returns an interval overlapping with [sector, sector + size), or NULL if
  114. * there is none. When there is more than one overlapping interval in the
  115. * tree, the interval with the lowest start sector is returned, and all other
  116. * overlapping intervals will be on the right side of the tree, reachable with
  117. * rb_next().
  118. */
  119. struct drbd_interval *
  120. drbd_find_overlap(struct rb_root *root, sector_t sector, unsigned int size)
  121. {
  122. struct rb_node *node = root->rb_node;
  123. struct drbd_interval *overlap = NULL;
  124. sector_t end = sector + (size >> 9);
  125. BUG_ON(!IS_ALIGNED(size, 512));
  126. while (node) {
  127. struct drbd_interval *here =
  128. rb_entry(node, struct drbd_interval, rb);
  129. if (node->rb_left &&
  130. sector < interval_end(node->rb_left)) {
  131. /* Overlap if any must be on left side */
  132. node = node->rb_left;
  133. } else if (here->sector < end &&
  134. sector < here->sector + (here->size >> 9)) {
  135. overlap = here;
  136. break;
  137. } else if (sector >= here->sector) {
  138. /* Overlap if any must be on right side */
  139. node = node->rb_right;
  140. } else
  141. break;
  142. }
  143. return overlap;
  144. }
  145. struct drbd_interval *
  146. drbd_next_overlap(struct drbd_interval *i, sector_t sector, unsigned int size)
  147. {
  148. sector_t end = sector + (size >> 9);
  149. struct rb_node *node;
  150. for (;;) {
  151. node = rb_next(&i->rb);
  152. if (!node)
  153. return NULL;
  154. i = rb_entry(node, struct drbd_interval, rb);
  155. if (i->sector >= end)
  156. return NULL;
  157. if (sector < i->sector + (i->size >> 9))
  158. return i;
  159. }
  160. }