xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/range_tree.h (revision 17a5fa85fe0c34b1146222e40a80b42f2aae8500)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
28  */
29 
30 #ifndef _SYS_RANGE_TREE_H
31 #define	_SYS_RANGE_TREE_H
32 
33 #include <sys/avl.h>
34 #include <sys/dmu.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #define	RANGE_TREE_HISTOGRAM_SIZE	64
41 
42 typedef struct range_tree_ops range_tree_ops_t;
43 
44 /*
45  * Note: the range_tree may not be accessed concurrently; consumers
46  * must provide external locking if required.
47  */
48 typedef struct range_tree {
49 	avl_tree_t	rt_root;	/* offset-ordered segment AVL tree */
50 	uint64_t	rt_space;	/* sum of all segments in the map */
51 	uint64_t	rt_gap;		/* allowable inter-segment gap */
52 	range_tree_ops_t *rt_ops;
53 	void		*rt_arg;
54 
55 	/* rt_avl_compare should only be set it rt_arg is an AVL tree */
56 	int (*rt_avl_compare)(const void *, const void *);
57 
58 	/*
59 	 * The rt_histogram maintains a histogram of ranges. Each bucket,
60 	 * rt_histogram[i], contains the number of ranges whose size is:
61 	 * 2^i <= size of range in bytes < 2^(i+1)
62 	 */
63 	uint64_t	rt_histogram[RANGE_TREE_HISTOGRAM_SIZE];
64 } range_tree_t;
65 
66 typedef struct range_seg {
67 	avl_node_t	rs_node;	/* AVL node */
68 	avl_node_t	rs_pp_node;	/* AVL picker-private node */
69 	uint64_t	rs_start;	/* starting offset of this segment */
70 	uint64_t	rs_end;		/* ending offset (non-inclusive) */
71 	uint64_t	rs_fill;	/* actual fill if gap mode is on */
72 } range_seg_t;
73 
74 struct range_tree_ops {
75 	void    (*rtop_create)(range_tree_t *rt, void *arg);
76 	void    (*rtop_destroy)(range_tree_t *rt, void *arg);
77 	void	(*rtop_add)(range_tree_t *rt, range_seg_t *rs, void *arg);
78 	void    (*rtop_remove)(range_tree_t *rt, range_seg_t *rs, void *arg);
79 	void	(*rtop_vacate)(range_tree_t *rt, void *arg);
80 };
81 
82 typedef void range_tree_func_t(void *arg, uint64_t start, uint64_t size);
83 
84 void range_tree_init(void);
85 void range_tree_fini(void);
86 range_tree_t *range_tree_create_impl(range_tree_ops_t *ops, void *arg,
87     int (*avl_compare)(const void*, const void*), uint64_t gap);
88 range_tree_t *range_tree_create(range_tree_ops_t *ops, void *arg);
89 void range_tree_destroy(range_tree_t *rt);
90 boolean_t range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size);
91 void range_tree_verify_not_present(range_tree_t *rt,
92     uint64_t start, uint64_t size);
93 range_seg_t *range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size);
94 void range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs,
95     uint64_t newstart, uint64_t newsize);
96 uint64_t range_tree_space(range_tree_t *rt);
97 boolean_t range_tree_is_empty(range_tree_t *rt);
98 void range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst);
99 void range_tree_stat_verify(range_tree_t *rt);
100 uint64_t range_tree_min(range_tree_t *rt);
101 uint64_t range_tree_max(range_tree_t *rt);
102 uint64_t range_tree_span(range_tree_t *rt);
103 
104 void range_tree_add(void *arg, uint64_t start, uint64_t size);
105 void range_tree_remove(void *arg, uint64_t start, uint64_t size);
106 void range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size);
107 void range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta);
108 void range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size);
109 
110 void range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg);
111 void range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg);
112 range_seg_t *range_tree_first(range_tree_t *rt);
113 
114 void rt_avl_create(range_tree_t *rt, void *arg);
115 void rt_avl_destroy(range_tree_t *rt, void *arg);
116 void rt_avl_add(range_tree_t *rt, range_seg_t *rs, void *arg);
117 void rt_avl_remove(range_tree_t *rt, range_seg_t *rs, void *arg);
118 void rt_avl_vacate(range_tree_t *rt, void *arg);
119 extern struct range_tree_ops rt_avl_ops;
120 
121 void rt_avl_create(range_tree_t *rt, void *arg);
122 void rt_avl_destroy(range_tree_t *rt, void *arg);
123 void rt_avl_add(range_tree_t *rt, range_seg_t *rs, void *arg);
124 void rt_avl_remove(range_tree_t *rt, range_seg_t *rs, void *arg);
125 void rt_avl_vacate(range_tree_t *rt, void *arg);
126 extern struct range_tree_ops rt_avl_ops;
127 
128 #ifdef	__cplusplus
129 }
130 #endif
131 
132 #endif	/* _SYS_RANGE_TREE_H */
133