xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/range_tree.h (revision 59ee40951f56cfa1c0e6c5c6aeea10605267116a)
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, 2015 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 	range_tree_ops_t *rt_ops;
52 	void		*rt_arg;
53 
54 	/*
55 	 * The rt_histogram maintains a histogram of ranges. Each bucket,
56 	 * rt_histogram[i], contains the number of ranges whose size is:
57 	 * 2^i <= size of range in bytes < 2^(i+1)
58 	 */
59 	uint64_t	rt_histogram[RANGE_TREE_HISTOGRAM_SIZE];
60 } range_tree_t;
61 
62 typedef struct range_seg {
63 	avl_node_t	rs_node;	/* AVL node */
64 	avl_node_t	rs_pp_node;	/* AVL picker-private node */
65 	uint64_t	rs_start;	/* starting offset of this segment */
66 	uint64_t	rs_end;		/* ending offset (non-inclusive) */
67 } range_seg_t;
68 
69 struct range_tree_ops {
70 	void    (*rtop_create)(range_tree_t *rt, void *arg);
71 	void    (*rtop_destroy)(range_tree_t *rt, void *arg);
72 	void	(*rtop_add)(range_tree_t *rt, range_seg_t *rs, void *arg);
73 	void    (*rtop_remove)(range_tree_t *rt, range_seg_t *rs, void *arg);
74 	void	(*rtop_vacate)(range_tree_t *rt, void *arg);
75 };
76 
77 typedef void range_tree_func_t(void *arg, uint64_t start, uint64_t size);
78 
79 void range_tree_init(void);
80 void range_tree_fini(void);
81 range_tree_t *range_tree_create(range_tree_ops_t *ops, void *arg);
82 void range_tree_destroy(range_tree_t *rt);
83 boolean_t range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size);
84 uint64_t range_tree_space(range_tree_t *rt);
85 void range_tree_verify(range_tree_t *rt, uint64_t start, uint64_t size);
86 void range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst);
87 void range_tree_stat_verify(range_tree_t *rt);
88 
89 void range_tree_add(void *arg, uint64_t start, uint64_t size);
90 void range_tree_remove(void *arg, uint64_t start, uint64_t size);
91 void range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size);
92 
93 void range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg);
94 void range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg);
95 
96 #ifdef	__cplusplus
97 }
98 #endif
99 
100 #endif	/* _SYS_RANGE_TREE_H */
101