1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef __DRBD_INTERVAL_H
3 #define __DRBD_INTERVAL_H
4 
5 #include <linux/types.h>
6 #include <linux/rbtree.h>
7 
8 struct drbd_interval {
9 	struct rb_node rb;
10 	sector_t sector;		/* start sector of the interval */
11 	unsigned int size;		/* size in bytes */
12 	sector_t end;			/* highest interval end in subtree */
13 	unsigned int local:1		/* local or remote request? */;
14 	unsigned int waiting:1;		/* someone is waiting for completion */
15 	unsigned int completed:1;	/* this has been completed already;
16 					 * ignore for conflict detection */
17 };
18 
drbd_clear_interval(struct drbd_interval * i)19 static inline void drbd_clear_interval(struct drbd_interval *i)
20 {
21 	RB_CLEAR_NODE(&i->rb);
22 }
23 
drbd_interval_empty(struct drbd_interval * i)24 static inline bool drbd_interval_empty(struct drbd_interval *i)
25 {
26 	return RB_EMPTY_NODE(&i->rb);
27 }
28 
29 extern bool drbd_insert_interval(struct rb_root *, struct drbd_interval *);
30 extern bool drbd_contains_interval(struct rb_root *, sector_t,
31 				   struct drbd_interval *);
32 extern void drbd_remove_interval(struct rb_root *, struct drbd_interval *);
33 extern struct drbd_interval *drbd_find_overlap(struct rb_root *, sector_t,
34 					unsigned int);
35 extern struct drbd_interval *drbd_next_overlap(struct drbd_interval *, sector_t,
36 					unsigned int);
37 
38 #define drbd_for_each_overlap(i, root, sector, size)		\
39 	for (i = drbd_find_overlap(root, sector, size);		\
40 	     i;							\
41 	     i = drbd_next_overlap(i, sector, size))
42 
43 #endif  /* __DRBD_INTERVAL_H */
44