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, 2019 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 boolean_t range_tree_find_in(range_tree_t *rt, uint64_t start, uint64_t size, 92 uint64_t *ostart, uint64_t *osize); 93 void range_tree_verify_not_present(range_tree_t *rt, 94 uint64_t start, uint64_t size); 95 range_seg_t *range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size); 96 void range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs, 97 uint64_t newstart, uint64_t newsize); 98 uint64_t range_tree_space(range_tree_t *rt); 99 uint64_t range_tree_numsegs(range_tree_t *rt); 100 boolean_t range_tree_is_empty(range_tree_t *rt); 101 void range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst); 102 void range_tree_stat_verify(range_tree_t *rt); 103 uint64_t range_tree_min(range_tree_t *rt); 104 uint64_t range_tree_max(range_tree_t *rt); 105 uint64_t range_tree_span(range_tree_t *rt); 106 107 void range_tree_add(void *arg, uint64_t start, uint64_t size); 108 void range_tree_remove(void *arg, uint64_t start, uint64_t size); 109 void range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size); 110 void range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta); 111 void range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size); 112 113 void range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg); 114 void range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg); 115 range_seg_t *range_tree_first(range_tree_t *rt); 116 117 void range_tree_remove_xor_add_segment(uint64_t start, uint64_t end, 118 range_tree_t *removefrom, range_tree_t *addto); 119 void range_tree_remove_xor_add(range_tree_t *rt, range_tree_t *removefrom, 120 range_tree_t *addto); 121 122 void rt_avl_create(range_tree_t *rt, void *arg); 123 void rt_avl_destroy(range_tree_t *rt, void *arg); 124 void rt_avl_add(range_tree_t *rt, range_seg_t *rs, void *arg); 125 void rt_avl_remove(range_tree_t *rt, range_seg_t *rs, void *arg); 126 void rt_avl_vacate(range_tree_t *rt, void *arg); 127 extern struct range_tree_ops rt_avl_ops; 128 129 void rt_avl_create(range_tree_t *rt, void *arg); 130 void rt_avl_destroy(range_tree_t *rt, void *arg); 131 void rt_avl_add(range_tree_t *rt, range_seg_t *rs, void *arg); 132 void rt_avl_remove(range_tree_t *rt, range_seg_t *rs, void *arg); 133 void rt_avl_vacate(range_tree_t *rt, void *arg); 134 extern struct range_tree_ops rt_avl_ops; 135 136 #ifdef __cplusplus 137 } 138 #endif 139 140 #endif /* _SYS_RANGE_TREE_H */ 141