2 #include <linux/rbtree_augmented.h>
3 #include "drbd_interval.h"
6 * interval_end - return end of @node
9 sector_t
interval_end(struct rb_node
*node
)
11 struct drbd_interval
*this = rb_entry(node
, struct drbd_interval
, rb
);
16 * compute_subtree_last - compute end of @node
18 * The end of an interval is the highest (start + (size >> 9)) value of this
19 * node and of its children. Called for @node and its parents whenever the end
22 static inline sector_t
23 compute_subtree_last(struct drbd_interval
*node
)
25 sector_t max
= node
->sector
+ (node
->size
>> 9);
27 if (node
->rb
.rb_left
) {
28 sector_t left
= interval_end(node
->rb
.rb_left
);
32 if (node
->rb
.rb_right
) {
33 sector_t right
= interval_end(node
->rb
.rb_right
);
40 static void augment_propagate(struct rb_node
*rb
, struct rb_node
*stop
)
43 struct drbd_interval
*node
= rb_entry(rb
, struct drbd_interval
, rb
);
44 sector_t subtree_last
= compute_subtree_last(node
);
45 if (node
->end
== subtree_last
)
47 node
->end
= subtree_last
;
48 rb
= rb_parent(&node
->rb
);
52 static void augment_copy(struct rb_node
*rb_old
, struct rb_node
*rb_new
)
54 struct drbd_interval
*old
= rb_entry(rb_old
, struct drbd_interval
, rb
);
55 struct drbd_interval
*new = rb_entry(rb_new
, struct drbd_interval
, rb
);
60 static void augment_rotate(struct rb_node
*rb_old
, struct rb_node
*rb_new
)
62 struct drbd_interval
*old
= rb_entry(rb_old
, struct drbd_interval
, rb
);
63 struct drbd_interval
*new = rb_entry(rb_new
, struct drbd_interval
, rb
);
66 old
->end
= compute_subtree_last(old
);
69 static const struct rb_augment_callbacks augment_callbacks
= {
76 * drbd_insert_interval - insert a new interval into a tree
79 drbd_insert_interval(struct rb_root
*root
, struct drbd_interval
*this)
81 struct rb_node
**new = &root
->rb_node
, *parent
= NULL
;
83 BUG_ON(!IS_ALIGNED(this->size
, 512));
86 struct drbd_interval
*here
=
87 rb_entry(*new, struct drbd_interval
, rb
);
90 if (this->sector
< here
->sector
)
91 new = &(*new)->rb_left
;
92 else if (this->sector
> here
->sector
)
93 new = &(*new)->rb_right
;
95 new = &(*new)->rb_left
;
97 new = &(*new)->rb_right
;
102 rb_link_node(&this->rb
, parent
, new);
103 rb_insert_augmented(&this->rb
, root
, &augment_callbacks
);
108 * drbd_contains_interval - check if a tree contains a given interval
109 * @sector: start sector of @interval
110 * @interval: may not be a valid pointer
112 * Returns if the tree contains the node @interval with start sector @start.
113 * Does not dereference @interval until @interval is known to be a valid object
114 * in @tree. Returns %false if @interval is in the tree but with a different
118 drbd_contains_interval(struct rb_root
*root
, sector_t sector
,
119 struct drbd_interval
*interval
)
121 struct rb_node
*node
= root
->rb_node
;
124 struct drbd_interval
*here
=
125 rb_entry(node
, struct drbd_interval
, rb
);
127 if (sector
< here
->sector
)
128 node
= node
->rb_left
;
129 else if (sector
> here
->sector
)
130 node
= node
->rb_right
;
131 else if (interval
< here
)
132 node
= node
->rb_left
;
133 else if (interval
> here
)
134 node
= node
->rb_right
;
142 * drbd_remove_interval - remove an interval from a tree
145 drbd_remove_interval(struct rb_root
*root
, struct drbd_interval
*this)
147 rb_erase_augmented(&this->rb
, root
, &augment_callbacks
);
151 * drbd_find_overlap - search for an interval overlapping with [sector, sector + size)
152 * @sector: start sector
153 * @size: size, aligned to 512 bytes
155 * Returns an interval overlapping with [sector, sector + size), or NULL if
156 * there is none. When there is more than one overlapping interval in the
157 * tree, the interval with the lowest start sector is returned, and all other
158 * overlapping intervals will be on the right side of the tree, reachable with
161 struct drbd_interval
*
162 drbd_find_overlap(struct rb_root
*root
, sector_t sector
, unsigned int size
)
164 struct rb_node
*node
= root
->rb_node
;
165 struct drbd_interval
*overlap
= NULL
;
166 sector_t end
= sector
+ (size
>> 9);
168 BUG_ON(!IS_ALIGNED(size
, 512));
171 struct drbd_interval
*here
=
172 rb_entry(node
, struct drbd_interval
, rb
);
175 sector
< interval_end(node
->rb_left
)) {
176 /* Overlap if any must be on left side */
177 node
= node
->rb_left
;
178 } else if (here
->sector
< end
&&
179 sector
< here
->sector
+ (here
->size
>> 9)) {
182 } else if (sector
>= here
->sector
) {
183 /* Overlap if any must be on right side */
184 node
= node
->rb_right
;
191 struct drbd_interval
*
192 drbd_next_overlap(struct drbd_interval
*i
, sector_t sector
, unsigned int size
)
194 sector_t end
= sector
+ (size
>> 9);
195 struct rb_node
*node
;
198 node
= rb_next(&i
->rb
);
201 i
= rb_entry(node
, struct drbd_interval
, rb
);
202 if (i
->sector
>= end
)
204 if (sector
< i
->sector
+ (i
->size
>> 9))