1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/rbtree_augmented.h>
4 #include "drbd_interval.h"
7 * interval_end - return end of @node
10 sector_t
interval_end(struct rb_node
*node
)
12 struct drbd_interval
*this = rb_entry(node
, struct drbd_interval
, rb
);
16 #define NODE_END(node) ((node)->sector + ((node)->size >> 9))
18 RB_DECLARE_CALLBACKS_MAX(static, augment_callbacks
,
19 struct drbd_interval
, rb
, sector_t
, end
, NODE_END
);
22 * drbd_insert_interval - insert a new interval into a tree
25 drbd_insert_interval(struct rb_root
*root
, struct drbd_interval
*this)
27 struct rb_node
**new = &root
->rb_node
, *parent
= NULL
;
28 sector_t this_end
= this->sector
+ (this->size
>> 9);
30 BUG_ON(!IS_ALIGNED(this->size
, 512));
33 struct drbd_interval
*here
=
34 rb_entry(*new, struct drbd_interval
, rb
);
37 if (here
->end
< this_end
)
39 if (this->sector
< here
->sector
)
40 new = &(*new)->rb_left
;
41 else if (this->sector
> here
->sector
)
42 new = &(*new)->rb_right
;
44 new = &(*new)->rb_left
;
46 new = &(*new)->rb_right
;
52 rb_link_node(&this->rb
, parent
, new);
53 rb_insert_augmented(&this->rb
, root
, &augment_callbacks
);
58 * drbd_contains_interval - check if a tree contains a given interval
59 * @sector: start sector of @interval
60 * @interval: may not be a valid pointer
62 * Returns if the tree contains the node @interval with start sector @start.
63 * Does not dereference @interval until @interval is known to be a valid object
64 * in @tree. Returns %false if @interval is in the tree but with a different
68 drbd_contains_interval(struct rb_root
*root
, sector_t sector
,
69 struct drbd_interval
*interval
)
71 struct rb_node
*node
= root
->rb_node
;
74 struct drbd_interval
*here
=
75 rb_entry(node
, struct drbd_interval
, rb
);
77 if (sector
< here
->sector
)
79 else if (sector
> here
->sector
)
80 node
= node
->rb_right
;
81 else if (interval
< here
)
83 else if (interval
> here
)
84 node
= node
->rb_right
;
92 * drbd_remove_interval - remove an interval from a tree
95 drbd_remove_interval(struct rb_root
*root
, struct drbd_interval
*this)
97 rb_erase_augmented(&this->rb
, root
, &augment_callbacks
);
101 * drbd_find_overlap - search for an interval overlapping with [sector, sector + size)
102 * @sector: start sector
103 * @size: size, aligned to 512 bytes
105 * Returns an interval overlapping with [sector, sector + size), or NULL if
106 * there is none. When there is more than one overlapping interval in the
107 * tree, the interval with the lowest start sector is returned, and all other
108 * overlapping intervals will be on the right side of the tree, reachable with
111 struct drbd_interval
*
112 drbd_find_overlap(struct rb_root
*root
, sector_t sector
, unsigned int size
)
114 struct rb_node
*node
= root
->rb_node
;
115 struct drbd_interval
*overlap
= NULL
;
116 sector_t end
= sector
+ (size
>> 9);
118 BUG_ON(!IS_ALIGNED(size
, 512));
121 struct drbd_interval
*here
=
122 rb_entry(node
, struct drbd_interval
, rb
);
125 sector
< interval_end(node
->rb_left
)) {
126 /* Overlap if any must be on left side */
127 node
= node
->rb_left
;
128 } else if (here
->sector
< end
&&
129 sector
< here
->sector
+ (here
->size
>> 9)) {
132 } else if (sector
>= here
->sector
) {
133 /* Overlap if any must be on right side */
134 node
= node
->rb_right
;
141 struct drbd_interval
*
142 drbd_next_overlap(struct drbd_interval
*i
, sector_t sector
, unsigned int size
)
144 sector_t end
= sector
+ (size
>> 9);
145 struct rb_node
*node
;
148 node
= rb_next(&i
->rb
);
151 i
= rb_entry(node
, struct drbd_interval
, rb
);
152 if (i
->sector
>= end
)
154 if (sector
< i
->sector
+ (i
->size
>> 9))