1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23eda14cbcSMatt Macy * Use is subject to license terms. 24eda14cbcSMatt Macy */ 25eda14cbcSMatt Macy /* 26eda14cbcSMatt Macy * Copyright (c) 2013, 2019 by Delphix. All rights reserved. 272c48331dSMatt Macy * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. 28eda14cbcSMatt Macy */ 29eda14cbcSMatt Macy 30eda14cbcSMatt Macy #include <sys/zfs_context.h> 31eda14cbcSMatt Macy #include <sys/spa.h> 32eda14cbcSMatt Macy #include <sys/dmu.h> 33eda14cbcSMatt Macy #include <sys/dnode.h> 34eda14cbcSMatt Macy #include <sys/zio.h> 35eda14cbcSMatt Macy #include <sys/range_tree.h> 36eda14cbcSMatt Macy 37eda14cbcSMatt Macy /* 38eda14cbcSMatt Macy * Range trees are tree-based data structures that can be used to 39eda14cbcSMatt Macy * track free space or generally any space allocation information. 40eda14cbcSMatt Macy * A range tree keeps track of individual segments and automatically 41eda14cbcSMatt Macy * provides facilities such as adjacent extent merging and extent 42eda14cbcSMatt Macy * splitting in response to range add/remove requests. 43eda14cbcSMatt Macy * 44eda14cbcSMatt Macy * A range tree starts out completely empty, with no segments in it. 45eda14cbcSMatt Macy * Adding an allocation via range_tree_add to the range tree can either: 46eda14cbcSMatt Macy * 1) create a new extent 47eda14cbcSMatt Macy * 2) extend an adjacent extent 48eda14cbcSMatt Macy * 3) merge two adjacent extents 49eda14cbcSMatt Macy * Conversely, removing an allocation via range_tree_remove can: 50eda14cbcSMatt Macy * 1) completely remove an extent 51eda14cbcSMatt Macy * 2) shorten an extent (if the allocation was near one of its ends) 52eda14cbcSMatt Macy * 3) split an extent into two extents, in effect punching a hole 53eda14cbcSMatt Macy * 54eda14cbcSMatt Macy * A range tree is also capable of 'bridging' gaps when adding 55eda14cbcSMatt Macy * allocations. This is useful for cases when close proximity of 56eda14cbcSMatt Macy * allocations is an important detail that needs to be represented 57eda14cbcSMatt Macy * in the range tree. See range_tree_set_gap(). The default behavior 58eda14cbcSMatt Macy * is not to bridge gaps (i.e. the maximum allowed gap size is 0). 59eda14cbcSMatt Macy * 60eda14cbcSMatt Macy * In order to traverse a range tree, use either the range_tree_walk() 61eda14cbcSMatt Macy * or range_tree_vacate() functions. 62eda14cbcSMatt Macy * 63eda14cbcSMatt Macy * To obtain more accurate information on individual segment 64eda14cbcSMatt Macy * operations that the range tree performs "under the hood", you can 65eda14cbcSMatt Macy * specify a set of callbacks by passing a range_tree_ops_t structure 66eda14cbcSMatt Macy * to the range_tree_create function. Any callbacks that are non-NULL 67eda14cbcSMatt Macy * are then called at the appropriate times. 68eda14cbcSMatt Macy * 69eda14cbcSMatt Macy * The range tree code also supports a special variant of range trees 70eda14cbcSMatt Macy * that can bridge small gaps between segments. This kind of tree is used 71eda14cbcSMatt Macy * by the dsl scanning code to group I/Os into mostly sequential chunks to 72eda14cbcSMatt Macy * optimize disk performance. The code here attempts to do this with as 73eda14cbcSMatt Macy * little memory and computational overhead as possible. One limitation of 74eda14cbcSMatt Macy * this implementation is that segments of range trees with gaps can only 75eda14cbcSMatt Macy * support removing complete segments. 76eda14cbcSMatt Macy */ 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy static inline void 79eda14cbcSMatt Macy rs_copy(range_seg_t *src, range_seg_t *dest, range_tree_t *rt) 80eda14cbcSMatt Macy { 81eda14cbcSMatt Macy ASSERT3U(rt->rt_type, <=, RANGE_SEG_NUM_TYPES); 82eda14cbcSMatt Macy size_t size = 0; 83eda14cbcSMatt Macy switch (rt->rt_type) { 84eda14cbcSMatt Macy case RANGE_SEG32: 85eda14cbcSMatt Macy size = sizeof (range_seg32_t); 86eda14cbcSMatt Macy break; 87eda14cbcSMatt Macy case RANGE_SEG64: 88eda14cbcSMatt Macy size = sizeof (range_seg64_t); 89eda14cbcSMatt Macy break; 90eda14cbcSMatt Macy case RANGE_SEG_GAP: 91eda14cbcSMatt Macy size = sizeof (range_seg_gap_t); 92eda14cbcSMatt Macy break; 93eda14cbcSMatt Macy default: 94eda14cbcSMatt Macy VERIFY(0); 95eda14cbcSMatt Macy } 96eda14cbcSMatt Macy bcopy(src, dest, size); 97eda14cbcSMatt Macy } 98eda14cbcSMatt Macy 99eda14cbcSMatt Macy void 100eda14cbcSMatt Macy range_tree_stat_verify(range_tree_t *rt) 101eda14cbcSMatt Macy { 102eda14cbcSMatt Macy range_seg_t *rs; 103eda14cbcSMatt Macy zfs_btree_index_t where; 104eda14cbcSMatt Macy uint64_t hist[RANGE_TREE_HISTOGRAM_SIZE] = { 0 }; 105eda14cbcSMatt Macy int i; 106eda14cbcSMatt Macy 107eda14cbcSMatt Macy for (rs = zfs_btree_first(&rt->rt_root, &where); rs != NULL; 108eda14cbcSMatt Macy rs = zfs_btree_next(&rt->rt_root, &where, &where)) { 109eda14cbcSMatt Macy uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt); 110eda14cbcSMatt Macy int idx = highbit64(size) - 1; 111eda14cbcSMatt Macy 112eda14cbcSMatt Macy hist[idx]++; 113eda14cbcSMatt Macy ASSERT3U(hist[idx], !=, 0); 114eda14cbcSMatt Macy } 115eda14cbcSMatt Macy 116eda14cbcSMatt Macy for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 117eda14cbcSMatt Macy if (hist[i] != rt->rt_histogram[i]) { 118eda14cbcSMatt Macy zfs_dbgmsg("i=%d, hist=%px, hist=%llu, rt_hist=%llu", 119eda14cbcSMatt Macy i, hist, hist[i], rt->rt_histogram[i]); 120eda14cbcSMatt Macy } 121eda14cbcSMatt Macy VERIFY3U(hist[i], ==, rt->rt_histogram[i]); 122eda14cbcSMatt Macy } 123eda14cbcSMatt Macy } 124eda14cbcSMatt Macy 125eda14cbcSMatt Macy static void 126eda14cbcSMatt Macy range_tree_stat_incr(range_tree_t *rt, range_seg_t *rs) 127eda14cbcSMatt Macy { 128eda14cbcSMatt Macy uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt); 129eda14cbcSMatt Macy int idx = highbit64(size) - 1; 130eda14cbcSMatt Macy 131eda14cbcSMatt Macy ASSERT(size != 0); 132eda14cbcSMatt Macy ASSERT3U(idx, <, 133eda14cbcSMatt Macy sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram)); 134eda14cbcSMatt Macy 135eda14cbcSMatt Macy rt->rt_histogram[idx]++; 136eda14cbcSMatt Macy ASSERT3U(rt->rt_histogram[idx], !=, 0); 137eda14cbcSMatt Macy } 138eda14cbcSMatt Macy 139eda14cbcSMatt Macy static void 140eda14cbcSMatt Macy range_tree_stat_decr(range_tree_t *rt, range_seg_t *rs) 141eda14cbcSMatt Macy { 142eda14cbcSMatt Macy uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt); 143eda14cbcSMatt Macy int idx = highbit64(size) - 1; 144eda14cbcSMatt Macy 145eda14cbcSMatt Macy ASSERT(size != 0); 146eda14cbcSMatt Macy ASSERT3U(idx, <, 147eda14cbcSMatt Macy sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram)); 148eda14cbcSMatt Macy 149eda14cbcSMatt Macy ASSERT3U(rt->rt_histogram[idx], !=, 0); 150eda14cbcSMatt Macy rt->rt_histogram[idx]--; 151eda14cbcSMatt Macy } 152eda14cbcSMatt Macy 153eda14cbcSMatt Macy static int 154eda14cbcSMatt Macy range_tree_seg32_compare(const void *x1, const void *x2) 155eda14cbcSMatt Macy { 156eda14cbcSMatt Macy const range_seg32_t *r1 = x1; 157eda14cbcSMatt Macy const range_seg32_t *r2 = x2; 158eda14cbcSMatt Macy 159eda14cbcSMatt Macy ASSERT3U(r1->rs_start, <=, r1->rs_end); 160eda14cbcSMatt Macy ASSERT3U(r2->rs_start, <=, r2->rs_end); 161eda14cbcSMatt Macy 162eda14cbcSMatt Macy return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start)); 163eda14cbcSMatt Macy } 164eda14cbcSMatt Macy 165eda14cbcSMatt Macy static int 166eda14cbcSMatt Macy range_tree_seg64_compare(const void *x1, const void *x2) 167eda14cbcSMatt Macy { 168eda14cbcSMatt Macy const range_seg64_t *r1 = x1; 169eda14cbcSMatt Macy const range_seg64_t *r2 = x2; 170eda14cbcSMatt Macy 171eda14cbcSMatt Macy ASSERT3U(r1->rs_start, <=, r1->rs_end); 172eda14cbcSMatt Macy ASSERT3U(r2->rs_start, <=, r2->rs_end); 173eda14cbcSMatt Macy 174eda14cbcSMatt Macy return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start)); 175eda14cbcSMatt Macy } 176eda14cbcSMatt Macy 177eda14cbcSMatt Macy static int 178eda14cbcSMatt Macy range_tree_seg_gap_compare(const void *x1, const void *x2) 179eda14cbcSMatt Macy { 180eda14cbcSMatt Macy const range_seg_gap_t *r1 = x1; 181eda14cbcSMatt Macy const range_seg_gap_t *r2 = x2; 182eda14cbcSMatt Macy 183eda14cbcSMatt Macy ASSERT3U(r1->rs_start, <=, r1->rs_end); 184eda14cbcSMatt Macy ASSERT3U(r2->rs_start, <=, r2->rs_end); 185eda14cbcSMatt Macy 186eda14cbcSMatt Macy return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start)); 187eda14cbcSMatt Macy } 188eda14cbcSMatt Macy 189eda14cbcSMatt Macy range_tree_t * 190eda14cbcSMatt Macy range_tree_create_impl(range_tree_ops_t *ops, range_seg_type_t type, void *arg, 191eda14cbcSMatt Macy uint64_t start, uint64_t shift, 192eda14cbcSMatt Macy int (*zfs_btree_compare) (const void *, const void *), 193eda14cbcSMatt Macy uint64_t gap) 194eda14cbcSMatt Macy { 195eda14cbcSMatt Macy range_tree_t *rt = kmem_zalloc(sizeof (range_tree_t), KM_SLEEP); 196eda14cbcSMatt Macy 197eda14cbcSMatt Macy ASSERT3U(shift, <, 64); 198eda14cbcSMatt Macy ASSERT3U(type, <=, RANGE_SEG_NUM_TYPES); 199eda14cbcSMatt Macy size_t size; 200eda14cbcSMatt Macy int (*compare) (const void *, const void *); 201eda14cbcSMatt Macy switch (type) { 202eda14cbcSMatt Macy case RANGE_SEG32: 203eda14cbcSMatt Macy size = sizeof (range_seg32_t); 204eda14cbcSMatt Macy compare = range_tree_seg32_compare; 205eda14cbcSMatt Macy break; 206eda14cbcSMatt Macy case RANGE_SEG64: 207eda14cbcSMatt Macy size = sizeof (range_seg64_t); 208eda14cbcSMatt Macy compare = range_tree_seg64_compare; 209eda14cbcSMatt Macy break; 210eda14cbcSMatt Macy case RANGE_SEG_GAP: 211eda14cbcSMatt Macy size = sizeof (range_seg_gap_t); 212eda14cbcSMatt Macy compare = range_tree_seg_gap_compare; 213eda14cbcSMatt Macy break; 214eda14cbcSMatt Macy default: 215eda14cbcSMatt Macy panic("Invalid range seg type %d", type); 216eda14cbcSMatt Macy } 217eda14cbcSMatt Macy zfs_btree_create(&rt->rt_root, compare, size); 218eda14cbcSMatt Macy 219eda14cbcSMatt Macy rt->rt_ops = ops; 220eda14cbcSMatt Macy rt->rt_gap = gap; 221eda14cbcSMatt Macy rt->rt_arg = arg; 222eda14cbcSMatt Macy rt->rt_type = type; 223eda14cbcSMatt Macy rt->rt_start = start; 224eda14cbcSMatt Macy rt->rt_shift = shift; 225eda14cbcSMatt Macy rt->rt_btree_compare = zfs_btree_compare; 226eda14cbcSMatt Macy 227eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_create != NULL) 228eda14cbcSMatt Macy rt->rt_ops->rtop_create(rt, rt->rt_arg); 229eda14cbcSMatt Macy 230eda14cbcSMatt Macy return (rt); 231eda14cbcSMatt Macy } 232eda14cbcSMatt Macy 233eda14cbcSMatt Macy range_tree_t * 234eda14cbcSMatt Macy range_tree_create(range_tree_ops_t *ops, range_seg_type_t type, 235eda14cbcSMatt Macy void *arg, uint64_t start, uint64_t shift) 236eda14cbcSMatt Macy { 237eda14cbcSMatt Macy return (range_tree_create_impl(ops, type, arg, start, shift, NULL, 0)); 238eda14cbcSMatt Macy } 239eda14cbcSMatt Macy 240eda14cbcSMatt Macy void 241eda14cbcSMatt Macy range_tree_destroy(range_tree_t *rt) 242eda14cbcSMatt Macy { 243eda14cbcSMatt Macy VERIFY0(rt->rt_space); 244eda14cbcSMatt Macy 245eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_destroy != NULL) 246eda14cbcSMatt Macy rt->rt_ops->rtop_destroy(rt, rt->rt_arg); 247eda14cbcSMatt Macy 248eda14cbcSMatt Macy zfs_btree_destroy(&rt->rt_root); 249eda14cbcSMatt Macy kmem_free(rt, sizeof (*rt)); 250eda14cbcSMatt Macy } 251eda14cbcSMatt Macy 252eda14cbcSMatt Macy void 253eda14cbcSMatt Macy range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta) 254eda14cbcSMatt Macy { 255eda14cbcSMatt Macy if (delta < 0 && delta * -1 >= rs_get_fill(rs, rt)) { 256eda14cbcSMatt Macy zfs_panic_recover("zfs: attempting to decrease fill to or " 257eda14cbcSMatt Macy "below 0; probable double remove in segment [%llx:%llx]", 258eda14cbcSMatt Macy (longlong_t)rs_get_start(rs, rt), 259eda14cbcSMatt Macy (longlong_t)rs_get_end(rs, rt)); 260eda14cbcSMatt Macy } 261eda14cbcSMatt Macy if (rs_get_fill(rs, rt) + delta > rs_get_end(rs, rt) - 262eda14cbcSMatt Macy rs_get_start(rs, rt)) { 263eda14cbcSMatt Macy zfs_panic_recover("zfs: attempting to increase fill beyond " 264eda14cbcSMatt Macy "max; probable double add in segment [%llx:%llx]", 265eda14cbcSMatt Macy (longlong_t)rs_get_start(rs, rt), 266eda14cbcSMatt Macy (longlong_t)rs_get_end(rs, rt)); 267eda14cbcSMatt Macy } 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 270eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg); 271eda14cbcSMatt Macy rs_set_fill(rs, rt, rs_get_fill(rs, rt) + delta); 272eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL) 273eda14cbcSMatt Macy rt->rt_ops->rtop_add(rt, rs, rt->rt_arg); 274eda14cbcSMatt Macy } 275eda14cbcSMatt Macy 276eda14cbcSMatt Macy static void 277eda14cbcSMatt Macy range_tree_add_impl(void *arg, uint64_t start, uint64_t size, uint64_t fill) 278eda14cbcSMatt Macy { 279eda14cbcSMatt Macy range_tree_t *rt = arg; 280eda14cbcSMatt Macy zfs_btree_index_t where; 281eda14cbcSMatt Macy range_seg_t *rs_before, *rs_after, *rs; 282eda14cbcSMatt Macy range_seg_max_t tmp, rsearch; 283eda14cbcSMatt Macy uint64_t end = start + size, gap = rt->rt_gap; 284eda14cbcSMatt Macy uint64_t bridge_size = 0; 285eda14cbcSMatt Macy boolean_t merge_before, merge_after; 286eda14cbcSMatt Macy 287eda14cbcSMatt Macy ASSERT3U(size, !=, 0); 288eda14cbcSMatt Macy ASSERT3U(fill, <=, size); 289eda14cbcSMatt Macy ASSERT3U(start + size, >, start); 290eda14cbcSMatt Macy 291eda14cbcSMatt Macy rs_set_start(&rsearch, rt, start); 292eda14cbcSMatt Macy rs_set_end(&rsearch, rt, end); 293eda14cbcSMatt Macy rs = zfs_btree_find(&rt->rt_root, &rsearch, &where); 294eda14cbcSMatt Macy 295eda14cbcSMatt Macy /* 296eda14cbcSMatt Macy * If this is a gap-supporting range tree, it is possible that we 297eda14cbcSMatt Macy * are inserting into an existing segment. In this case simply 298eda14cbcSMatt Macy * bump the fill count and call the remove / add callbacks. If the 299eda14cbcSMatt Macy * new range will extend an existing segment, we remove the 300eda14cbcSMatt Macy * existing one, apply the new extent to it and re-insert it using 301eda14cbcSMatt Macy * the normal code paths. 302eda14cbcSMatt Macy */ 303eda14cbcSMatt Macy if (rs != NULL) { 304eda14cbcSMatt Macy if (gap == 0) { 305eda14cbcSMatt Macy zfs_panic_recover("zfs: adding existent segment to " 306eda14cbcSMatt Macy "range tree (offset=%llx size=%llx)", 307eda14cbcSMatt Macy (longlong_t)start, (longlong_t)size); 308eda14cbcSMatt Macy return; 309eda14cbcSMatt Macy } 310eda14cbcSMatt Macy uint64_t rstart = rs_get_start(rs, rt); 311eda14cbcSMatt Macy uint64_t rend = rs_get_end(rs, rt); 312eda14cbcSMatt Macy if (rstart <= start && rend >= end) { 313eda14cbcSMatt Macy range_tree_adjust_fill(rt, rs, fill); 314eda14cbcSMatt Macy return; 315eda14cbcSMatt Macy } 316eda14cbcSMatt Macy 317eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 318eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg); 319eda14cbcSMatt Macy 320eda14cbcSMatt Macy range_tree_stat_decr(rt, rs); 321eda14cbcSMatt Macy rt->rt_space -= rend - rstart; 322eda14cbcSMatt Macy 323eda14cbcSMatt Macy fill += rs_get_fill(rs, rt); 324eda14cbcSMatt Macy start = MIN(start, rstart); 325eda14cbcSMatt Macy end = MAX(end, rend); 326eda14cbcSMatt Macy size = end - start; 327eda14cbcSMatt Macy 328*180f8225SMatt Macy zfs_btree_remove(&rt->rt_root, rs); 329eda14cbcSMatt Macy range_tree_add_impl(rt, start, size, fill); 330eda14cbcSMatt Macy return; 331eda14cbcSMatt Macy } 332eda14cbcSMatt Macy 333eda14cbcSMatt Macy ASSERT3P(rs, ==, NULL); 334eda14cbcSMatt Macy 335eda14cbcSMatt Macy /* 336eda14cbcSMatt Macy * Determine whether or not we will have to merge with our neighbors. 337eda14cbcSMatt Macy * If gap != 0, we might need to merge with our neighbors even if we 338eda14cbcSMatt Macy * aren't directly touching. 339eda14cbcSMatt Macy */ 340eda14cbcSMatt Macy zfs_btree_index_t where_before, where_after; 341eda14cbcSMatt Macy rs_before = zfs_btree_prev(&rt->rt_root, &where, &where_before); 342eda14cbcSMatt Macy rs_after = zfs_btree_next(&rt->rt_root, &where, &where_after); 343eda14cbcSMatt Macy 344eda14cbcSMatt Macy merge_before = (rs_before != NULL && rs_get_end(rs_before, rt) >= 345eda14cbcSMatt Macy start - gap); 346eda14cbcSMatt Macy merge_after = (rs_after != NULL && rs_get_start(rs_after, rt) <= end + 347eda14cbcSMatt Macy gap); 348eda14cbcSMatt Macy 349eda14cbcSMatt Macy if (merge_before && gap != 0) 350eda14cbcSMatt Macy bridge_size += start - rs_get_end(rs_before, rt); 351eda14cbcSMatt Macy if (merge_after && gap != 0) 352eda14cbcSMatt Macy bridge_size += rs_get_start(rs_after, rt) - end; 353eda14cbcSMatt Macy 354eda14cbcSMatt Macy if (merge_before && merge_after) { 355eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) { 356eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg); 357eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg); 358eda14cbcSMatt Macy } 359eda14cbcSMatt Macy 360eda14cbcSMatt Macy range_tree_stat_decr(rt, rs_before); 361eda14cbcSMatt Macy range_tree_stat_decr(rt, rs_after); 362eda14cbcSMatt Macy 363eda14cbcSMatt Macy rs_copy(rs_after, &tmp, rt); 364eda14cbcSMatt Macy uint64_t before_start = rs_get_start_raw(rs_before, rt); 365eda14cbcSMatt Macy uint64_t before_fill = rs_get_fill(rs_before, rt); 366eda14cbcSMatt Macy uint64_t after_fill = rs_get_fill(rs_after, rt); 367eda14cbcSMatt Macy zfs_btree_remove_idx(&rt->rt_root, &where_before); 368eda14cbcSMatt Macy 369eda14cbcSMatt Macy /* 370eda14cbcSMatt Macy * We have to re-find the node because our old reference is 371eda14cbcSMatt Macy * invalid as soon as we do any mutating btree operations. 372eda14cbcSMatt Macy */ 373eda14cbcSMatt Macy rs_after = zfs_btree_find(&rt->rt_root, &tmp, &where_after); 374eda14cbcSMatt Macy rs_set_start_raw(rs_after, rt, before_start); 375eda14cbcSMatt Macy rs_set_fill(rs_after, rt, after_fill + before_fill + fill); 376eda14cbcSMatt Macy rs = rs_after; 377eda14cbcSMatt Macy } else if (merge_before) { 378eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 379eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg); 380eda14cbcSMatt Macy 381eda14cbcSMatt Macy range_tree_stat_decr(rt, rs_before); 382eda14cbcSMatt Macy 383eda14cbcSMatt Macy uint64_t before_fill = rs_get_fill(rs_before, rt); 384eda14cbcSMatt Macy rs_set_end(rs_before, rt, end); 385eda14cbcSMatt Macy rs_set_fill(rs_before, rt, before_fill + fill); 386eda14cbcSMatt Macy rs = rs_before; 387eda14cbcSMatt Macy } else if (merge_after) { 388eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 389eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg); 390eda14cbcSMatt Macy 391eda14cbcSMatt Macy range_tree_stat_decr(rt, rs_after); 392eda14cbcSMatt Macy 393eda14cbcSMatt Macy uint64_t after_fill = rs_get_fill(rs_after, rt); 394eda14cbcSMatt Macy rs_set_start(rs_after, rt, start); 395eda14cbcSMatt Macy rs_set_fill(rs_after, rt, after_fill + fill); 396eda14cbcSMatt Macy rs = rs_after; 397eda14cbcSMatt Macy } else { 398eda14cbcSMatt Macy rs = &tmp; 399eda14cbcSMatt Macy 400eda14cbcSMatt Macy rs_set_start(rs, rt, start); 401eda14cbcSMatt Macy rs_set_end(rs, rt, end); 402eda14cbcSMatt Macy rs_set_fill(rs, rt, fill); 403eda14cbcSMatt Macy zfs_btree_add_idx(&rt->rt_root, rs, &where); 404eda14cbcSMatt Macy } 405eda14cbcSMatt Macy 406eda14cbcSMatt Macy if (gap != 0) { 407eda14cbcSMatt Macy ASSERT3U(rs_get_fill(rs, rt), <=, rs_get_end(rs, rt) - 408eda14cbcSMatt Macy rs_get_start(rs, rt)); 409eda14cbcSMatt Macy } else { 410eda14cbcSMatt Macy ASSERT3U(rs_get_fill(rs, rt), ==, rs_get_end(rs, rt) - 411eda14cbcSMatt Macy rs_get_start(rs, rt)); 412eda14cbcSMatt Macy } 413eda14cbcSMatt Macy 414eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL) 415eda14cbcSMatt Macy rt->rt_ops->rtop_add(rt, rs, rt->rt_arg); 416eda14cbcSMatt Macy 417eda14cbcSMatt Macy range_tree_stat_incr(rt, rs); 418eda14cbcSMatt Macy rt->rt_space += size + bridge_size; 419eda14cbcSMatt Macy } 420eda14cbcSMatt Macy 421eda14cbcSMatt Macy void 422eda14cbcSMatt Macy range_tree_add(void *arg, uint64_t start, uint64_t size) 423eda14cbcSMatt Macy { 424eda14cbcSMatt Macy range_tree_add_impl(arg, start, size, size); 425eda14cbcSMatt Macy } 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy static void 428eda14cbcSMatt Macy range_tree_remove_impl(range_tree_t *rt, uint64_t start, uint64_t size, 429eda14cbcSMatt Macy boolean_t do_fill) 430eda14cbcSMatt Macy { 431eda14cbcSMatt Macy zfs_btree_index_t where; 432eda14cbcSMatt Macy range_seg_t *rs; 433eda14cbcSMatt Macy range_seg_max_t rsearch, rs_tmp; 434eda14cbcSMatt Macy uint64_t end = start + size; 435eda14cbcSMatt Macy boolean_t left_over, right_over; 436eda14cbcSMatt Macy 437eda14cbcSMatt Macy VERIFY3U(size, !=, 0); 438eda14cbcSMatt Macy VERIFY3U(size, <=, rt->rt_space); 439eda14cbcSMatt Macy if (rt->rt_type == RANGE_SEG64) 440eda14cbcSMatt Macy ASSERT3U(start + size, >, start); 441eda14cbcSMatt Macy 442eda14cbcSMatt Macy rs_set_start(&rsearch, rt, start); 443eda14cbcSMatt Macy rs_set_end(&rsearch, rt, end); 444eda14cbcSMatt Macy rs = zfs_btree_find(&rt->rt_root, &rsearch, &where); 445eda14cbcSMatt Macy 446eda14cbcSMatt Macy /* Make sure we completely overlap with someone */ 447eda14cbcSMatt Macy if (rs == NULL) { 448eda14cbcSMatt Macy zfs_panic_recover("zfs: removing nonexistent segment from " 449eda14cbcSMatt Macy "range tree (offset=%llx size=%llx)", 450eda14cbcSMatt Macy (longlong_t)start, (longlong_t)size); 451eda14cbcSMatt Macy return; 452eda14cbcSMatt Macy } 453eda14cbcSMatt Macy 454eda14cbcSMatt Macy /* 455eda14cbcSMatt Macy * Range trees with gap support must only remove complete segments 456eda14cbcSMatt Macy * from the tree. This allows us to maintain accurate fill accounting 457eda14cbcSMatt Macy * and to ensure that bridged sections are not leaked. If we need to 458eda14cbcSMatt Macy * remove less than the full segment, we can only adjust the fill count. 459eda14cbcSMatt Macy */ 460eda14cbcSMatt Macy if (rt->rt_gap != 0) { 461eda14cbcSMatt Macy if (do_fill) { 462eda14cbcSMatt Macy if (rs_get_fill(rs, rt) == size) { 463eda14cbcSMatt Macy start = rs_get_start(rs, rt); 464eda14cbcSMatt Macy end = rs_get_end(rs, rt); 465eda14cbcSMatt Macy size = end - start; 466eda14cbcSMatt Macy } else { 467eda14cbcSMatt Macy range_tree_adjust_fill(rt, rs, -size); 468eda14cbcSMatt Macy return; 469eda14cbcSMatt Macy } 470eda14cbcSMatt Macy } else if (rs_get_start(rs, rt) != start || 471eda14cbcSMatt Macy rs_get_end(rs, rt) != end) { 472eda14cbcSMatt Macy zfs_panic_recover("zfs: freeing partial segment of " 473eda14cbcSMatt Macy "gap tree (offset=%llx size=%llx) of " 474eda14cbcSMatt Macy "(offset=%llx size=%llx)", 475eda14cbcSMatt Macy (longlong_t)start, (longlong_t)size, 476eda14cbcSMatt Macy (longlong_t)rs_get_start(rs, rt), 477eda14cbcSMatt Macy (longlong_t)rs_get_end(rs, rt) - rs_get_start(rs, 478eda14cbcSMatt Macy rt)); 479eda14cbcSMatt Macy return; 480eda14cbcSMatt Macy } 481eda14cbcSMatt Macy } 482eda14cbcSMatt Macy 483eda14cbcSMatt Macy VERIFY3U(rs_get_start(rs, rt), <=, start); 484eda14cbcSMatt Macy VERIFY3U(rs_get_end(rs, rt), >=, end); 485eda14cbcSMatt Macy 486eda14cbcSMatt Macy left_over = (rs_get_start(rs, rt) != start); 487eda14cbcSMatt Macy right_over = (rs_get_end(rs, rt) != end); 488eda14cbcSMatt Macy 489eda14cbcSMatt Macy range_tree_stat_decr(rt, rs); 490eda14cbcSMatt Macy 491eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 492eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg); 493eda14cbcSMatt Macy 494eda14cbcSMatt Macy if (left_over && right_over) { 495eda14cbcSMatt Macy range_seg_max_t newseg; 496eda14cbcSMatt Macy rs_set_start(&newseg, rt, end); 497eda14cbcSMatt Macy rs_set_end_raw(&newseg, rt, rs_get_end_raw(rs, rt)); 498eda14cbcSMatt Macy rs_set_fill(&newseg, rt, rs_get_end(rs, rt) - end); 499eda14cbcSMatt Macy range_tree_stat_incr(rt, &newseg); 500eda14cbcSMatt Macy 501eda14cbcSMatt Macy // This modifies the buffer already inside the range tree 502eda14cbcSMatt Macy rs_set_end(rs, rt, start); 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy rs_copy(rs, &rs_tmp, rt); 505eda14cbcSMatt Macy if (zfs_btree_next(&rt->rt_root, &where, &where) != NULL) 506eda14cbcSMatt Macy zfs_btree_add_idx(&rt->rt_root, &newseg, &where); 507eda14cbcSMatt Macy else 508eda14cbcSMatt Macy zfs_btree_add(&rt->rt_root, &newseg); 509eda14cbcSMatt Macy 510eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL) 511eda14cbcSMatt Macy rt->rt_ops->rtop_add(rt, &newseg, rt->rt_arg); 512eda14cbcSMatt Macy } else if (left_over) { 513eda14cbcSMatt Macy // This modifies the buffer already inside the range tree 514eda14cbcSMatt Macy rs_set_end(rs, rt, start); 515eda14cbcSMatt Macy rs_copy(rs, &rs_tmp, rt); 516eda14cbcSMatt Macy } else if (right_over) { 517eda14cbcSMatt Macy // This modifies the buffer already inside the range tree 518eda14cbcSMatt Macy rs_set_start(rs, rt, end); 519eda14cbcSMatt Macy rs_copy(rs, &rs_tmp, rt); 520eda14cbcSMatt Macy } else { 521eda14cbcSMatt Macy zfs_btree_remove_idx(&rt->rt_root, &where); 522eda14cbcSMatt Macy rs = NULL; 523eda14cbcSMatt Macy } 524eda14cbcSMatt Macy 525eda14cbcSMatt Macy if (rs != NULL) { 526eda14cbcSMatt Macy /* 527eda14cbcSMatt Macy * The fill of the leftover segment will always be equal to 528eda14cbcSMatt Macy * the size, since we do not support removing partial segments 529eda14cbcSMatt Macy * of range trees with gaps. 530eda14cbcSMatt Macy */ 531eda14cbcSMatt Macy rs_set_fill_raw(rs, rt, rs_get_end_raw(rs, rt) - 532eda14cbcSMatt Macy rs_get_start_raw(rs, rt)); 533eda14cbcSMatt Macy range_tree_stat_incr(rt, &rs_tmp); 534eda14cbcSMatt Macy 535eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL) 536eda14cbcSMatt Macy rt->rt_ops->rtop_add(rt, &rs_tmp, rt->rt_arg); 537eda14cbcSMatt Macy } 538eda14cbcSMatt Macy 539eda14cbcSMatt Macy rt->rt_space -= size; 540eda14cbcSMatt Macy } 541eda14cbcSMatt Macy 542eda14cbcSMatt Macy void 543eda14cbcSMatt Macy range_tree_remove(void *arg, uint64_t start, uint64_t size) 544eda14cbcSMatt Macy { 545eda14cbcSMatt Macy range_tree_remove_impl(arg, start, size, B_FALSE); 546eda14cbcSMatt Macy } 547eda14cbcSMatt Macy 548eda14cbcSMatt Macy void 549eda14cbcSMatt Macy range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size) 550eda14cbcSMatt Macy { 551eda14cbcSMatt Macy range_tree_remove_impl(rt, start, size, B_TRUE); 552eda14cbcSMatt Macy } 553eda14cbcSMatt Macy 554eda14cbcSMatt Macy void 555eda14cbcSMatt Macy range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs, 556eda14cbcSMatt Macy uint64_t newstart, uint64_t newsize) 557eda14cbcSMatt Macy { 558eda14cbcSMatt Macy int64_t delta = newsize - (rs_get_end(rs, rt) - rs_get_start(rs, rt)); 559eda14cbcSMatt Macy 560eda14cbcSMatt Macy range_tree_stat_decr(rt, rs); 561eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) 562eda14cbcSMatt Macy rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg); 563eda14cbcSMatt Macy 564eda14cbcSMatt Macy rs_set_start(rs, rt, newstart); 565eda14cbcSMatt Macy rs_set_end(rs, rt, newstart + newsize); 566eda14cbcSMatt Macy 567eda14cbcSMatt Macy range_tree_stat_incr(rt, rs); 568eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL) 569eda14cbcSMatt Macy rt->rt_ops->rtop_add(rt, rs, rt->rt_arg); 570eda14cbcSMatt Macy 571eda14cbcSMatt Macy rt->rt_space += delta; 572eda14cbcSMatt Macy } 573eda14cbcSMatt Macy 574eda14cbcSMatt Macy static range_seg_t * 575eda14cbcSMatt Macy range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size) 576eda14cbcSMatt Macy { 577eda14cbcSMatt Macy range_seg_max_t rsearch; 578eda14cbcSMatt Macy uint64_t end = start + size; 579eda14cbcSMatt Macy 580eda14cbcSMatt Macy VERIFY(size != 0); 581eda14cbcSMatt Macy 582eda14cbcSMatt Macy rs_set_start(&rsearch, rt, start); 583eda14cbcSMatt Macy rs_set_end(&rsearch, rt, end); 584eda14cbcSMatt Macy return (zfs_btree_find(&rt->rt_root, &rsearch, NULL)); 585eda14cbcSMatt Macy } 586eda14cbcSMatt Macy 587eda14cbcSMatt Macy range_seg_t * 588eda14cbcSMatt Macy range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size) 589eda14cbcSMatt Macy { 590eda14cbcSMatt Macy if (rt->rt_type == RANGE_SEG64) 591eda14cbcSMatt Macy ASSERT3U(start + size, >, start); 592eda14cbcSMatt Macy 593eda14cbcSMatt Macy range_seg_t *rs = range_tree_find_impl(rt, start, size); 594eda14cbcSMatt Macy if (rs != NULL && rs_get_start(rs, rt) <= start && 595eda14cbcSMatt Macy rs_get_end(rs, rt) >= start + size) { 596eda14cbcSMatt Macy return (rs); 597eda14cbcSMatt Macy } 598eda14cbcSMatt Macy return (NULL); 599eda14cbcSMatt Macy } 600eda14cbcSMatt Macy 601eda14cbcSMatt Macy void 602eda14cbcSMatt Macy range_tree_verify_not_present(range_tree_t *rt, uint64_t off, uint64_t size) 603eda14cbcSMatt Macy { 604eda14cbcSMatt Macy range_seg_t *rs = range_tree_find(rt, off, size); 605eda14cbcSMatt Macy if (rs != NULL) 606eda14cbcSMatt Macy panic("segment already in tree; rs=%p", (void *)rs); 607eda14cbcSMatt Macy } 608eda14cbcSMatt Macy 609eda14cbcSMatt Macy boolean_t 610eda14cbcSMatt Macy range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size) 611eda14cbcSMatt Macy { 612eda14cbcSMatt Macy return (range_tree_find(rt, start, size) != NULL); 613eda14cbcSMatt Macy } 614eda14cbcSMatt Macy 615eda14cbcSMatt Macy /* 616eda14cbcSMatt Macy * Returns the first subset of the given range which overlaps with the range 617eda14cbcSMatt Macy * tree. Returns true if there is a segment in the range, and false if there 618eda14cbcSMatt Macy * isn't. 619eda14cbcSMatt Macy */ 620eda14cbcSMatt Macy boolean_t 621eda14cbcSMatt Macy range_tree_find_in(range_tree_t *rt, uint64_t start, uint64_t size, 622eda14cbcSMatt Macy uint64_t *ostart, uint64_t *osize) 623eda14cbcSMatt Macy { 624eda14cbcSMatt Macy if (rt->rt_type == RANGE_SEG64) 625eda14cbcSMatt Macy ASSERT3U(start + size, >, start); 626eda14cbcSMatt Macy 627eda14cbcSMatt Macy range_seg_max_t rsearch; 628eda14cbcSMatt Macy rs_set_start(&rsearch, rt, start); 629eda14cbcSMatt Macy rs_set_end_raw(&rsearch, rt, rs_get_start_raw(&rsearch, rt) + 1); 630eda14cbcSMatt Macy 631eda14cbcSMatt Macy zfs_btree_index_t where; 632eda14cbcSMatt Macy range_seg_t *rs = zfs_btree_find(&rt->rt_root, &rsearch, &where); 633eda14cbcSMatt Macy if (rs != NULL) { 634eda14cbcSMatt Macy *ostart = start; 635eda14cbcSMatt Macy *osize = MIN(size, rs_get_end(rs, rt) - start); 636eda14cbcSMatt Macy return (B_TRUE); 637eda14cbcSMatt Macy } 638eda14cbcSMatt Macy 639eda14cbcSMatt Macy rs = zfs_btree_next(&rt->rt_root, &where, &where); 640eda14cbcSMatt Macy if (rs == NULL || rs_get_start(rs, rt) > start + size) 641eda14cbcSMatt Macy return (B_FALSE); 642eda14cbcSMatt Macy 643eda14cbcSMatt Macy *ostart = rs_get_start(rs, rt); 644eda14cbcSMatt Macy *osize = MIN(start + size, rs_get_end(rs, rt)) - 645eda14cbcSMatt Macy rs_get_start(rs, rt); 646eda14cbcSMatt Macy return (B_TRUE); 647eda14cbcSMatt Macy } 648eda14cbcSMatt Macy 649eda14cbcSMatt Macy /* 650eda14cbcSMatt Macy * Ensure that this range is not in the tree, regardless of whether 651eda14cbcSMatt Macy * it is currently in the tree. 652eda14cbcSMatt Macy */ 653eda14cbcSMatt Macy void 654eda14cbcSMatt Macy range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size) 655eda14cbcSMatt Macy { 656eda14cbcSMatt Macy range_seg_t *rs; 657eda14cbcSMatt Macy 658eda14cbcSMatt Macy if (size == 0) 659eda14cbcSMatt Macy return; 660eda14cbcSMatt Macy 661eda14cbcSMatt Macy if (rt->rt_type == RANGE_SEG64) 662eda14cbcSMatt Macy ASSERT3U(start + size, >, start); 663eda14cbcSMatt Macy 664eda14cbcSMatt Macy while ((rs = range_tree_find_impl(rt, start, size)) != NULL) { 665eda14cbcSMatt Macy uint64_t free_start = MAX(rs_get_start(rs, rt), start); 666eda14cbcSMatt Macy uint64_t free_end = MIN(rs_get_end(rs, rt), start + size); 667eda14cbcSMatt Macy range_tree_remove(rt, free_start, free_end - free_start); 668eda14cbcSMatt Macy } 669eda14cbcSMatt Macy } 670eda14cbcSMatt Macy 671eda14cbcSMatt Macy void 672eda14cbcSMatt Macy range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst) 673eda14cbcSMatt Macy { 674eda14cbcSMatt Macy range_tree_t *rt; 675eda14cbcSMatt Macy 676eda14cbcSMatt Macy ASSERT0(range_tree_space(*rtdst)); 677eda14cbcSMatt Macy ASSERT0(zfs_btree_numnodes(&(*rtdst)->rt_root)); 678eda14cbcSMatt Macy 679eda14cbcSMatt Macy rt = *rtsrc; 680eda14cbcSMatt Macy *rtsrc = *rtdst; 681eda14cbcSMatt Macy *rtdst = rt; 682eda14cbcSMatt Macy } 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy void 685eda14cbcSMatt Macy range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg) 686eda14cbcSMatt Macy { 687eda14cbcSMatt Macy if (rt->rt_ops != NULL && rt->rt_ops->rtop_vacate != NULL) 688eda14cbcSMatt Macy rt->rt_ops->rtop_vacate(rt, rt->rt_arg); 689eda14cbcSMatt Macy 690eda14cbcSMatt Macy if (func != NULL) { 691eda14cbcSMatt Macy range_seg_t *rs; 692eda14cbcSMatt Macy zfs_btree_index_t *cookie = NULL; 693eda14cbcSMatt Macy 694eda14cbcSMatt Macy while ((rs = zfs_btree_destroy_nodes(&rt->rt_root, &cookie)) != 695eda14cbcSMatt Macy NULL) { 696eda14cbcSMatt Macy func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) - 697eda14cbcSMatt Macy rs_get_start(rs, rt)); 698eda14cbcSMatt Macy } 699eda14cbcSMatt Macy } else { 700eda14cbcSMatt Macy zfs_btree_clear(&rt->rt_root); 701eda14cbcSMatt Macy } 702eda14cbcSMatt Macy 703eda14cbcSMatt Macy bzero(rt->rt_histogram, sizeof (rt->rt_histogram)); 704eda14cbcSMatt Macy rt->rt_space = 0; 705eda14cbcSMatt Macy } 706eda14cbcSMatt Macy 707eda14cbcSMatt Macy void 708eda14cbcSMatt Macy range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg) 709eda14cbcSMatt Macy { 710eda14cbcSMatt Macy zfs_btree_index_t where; 711eda14cbcSMatt Macy for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); 712eda14cbcSMatt Macy rs != NULL; rs = zfs_btree_next(&rt->rt_root, &where, &where)) { 713eda14cbcSMatt Macy func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) - 714eda14cbcSMatt Macy rs_get_start(rs, rt)); 715eda14cbcSMatt Macy } 716eda14cbcSMatt Macy } 717eda14cbcSMatt Macy 718eda14cbcSMatt Macy range_seg_t * 719eda14cbcSMatt Macy range_tree_first(range_tree_t *rt) 720eda14cbcSMatt Macy { 721eda14cbcSMatt Macy return (zfs_btree_first(&rt->rt_root, NULL)); 722eda14cbcSMatt Macy } 723eda14cbcSMatt Macy 724eda14cbcSMatt Macy uint64_t 725eda14cbcSMatt Macy range_tree_space(range_tree_t *rt) 726eda14cbcSMatt Macy { 727eda14cbcSMatt Macy return (rt->rt_space); 728eda14cbcSMatt Macy } 729eda14cbcSMatt Macy 730eda14cbcSMatt Macy uint64_t 731eda14cbcSMatt Macy range_tree_numsegs(range_tree_t *rt) 732eda14cbcSMatt Macy { 733eda14cbcSMatt Macy return ((rt == NULL) ? 0 : zfs_btree_numnodes(&rt->rt_root)); 734eda14cbcSMatt Macy } 735eda14cbcSMatt Macy 736eda14cbcSMatt Macy boolean_t 737eda14cbcSMatt Macy range_tree_is_empty(range_tree_t *rt) 738eda14cbcSMatt Macy { 739eda14cbcSMatt Macy ASSERT(rt != NULL); 740eda14cbcSMatt Macy return (range_tree_space(rt) == 0); 741eda14cbcSMatt Macy } 742eda14cbcSMatt Macy 743eda14cbcSMatt Macy /* ARGSUSED */ 744eda14cbcSMatt Macy void 745eda14cbcSMatt Macy rt_btree_create(range_tree_t *rt, void *arg) 746eda14cbcSMatt Macy { 747eda14cbcSMatt Macy zfs_btree_t *size_tree = arg; 748eda14cbcSMatt Macy 749eda14cbcSMatt Macy size_t size; 750eda14cbcSMatt Macy switch (rt->rt_type) { 751eda14cbcSMatt Macy case RANGE_SEG32: 752eda14cbcSMatt Macy size = sizeof (range_seg32_t); 753eda14cbcSMatt Macy break; 754eda14cbcSMatt Macy case RANGE_SEG64: 755eda14cbcSMatt Macy size = sizeof (range_seg64_t); 756eda14cbcSMatt Macy break; 757eda14cbcSMatt Macy case RANGE_SEG_GAP: 758eda14cbcSMatt Macy size = sizeof (range_seg_gap_t); 759eda14cbcSMatt Macy break; 760eda14cbcSMatt Macy default: 761eda14cbcSMatt Macy panic("Invalid range seg type %d", rt->rt_type); 762eda14cbcSMatt Macy } 763eda14cbcSMatt Macy zfs_btree_create(size_tree, rt->rt_btree_compare, size); 764eda14cbcSMatt Macy } 765eda14cbcSMatt Macy 766eda14cbcSMatt Macy /* ARGSUSED */ 767eda14cbcSMatt Macy void 768eda14cbcSMatt Macy rt_btree_destroy(range_tree_t *rt, void *arg) 769eda14cbcSMatt Macy { 770eda14cbcSMatt Macy zfs_btree_t *size_tree = arg; 771eda14cbcSMatt Macy ASSERT0(zfs_btree_numnodes(size_tree)); 772eda14cbcSMatt Macy 773eda14cbcSMatt Macy zfs_btree_destroy(size_tree); 774eda14cbcSMatt Macy } 775eda14cbcSMatt Macy 776eda14cbcSMatt Macy /* ARGSUSED */ 777eda14cbcSMatt Macy void 778eda14cbcSMatt Macy rt_btree_add(range_tree_t *rt, range_seg_t *rs, void *arg) 779eda14cbcSMatt Macy { 780eda14cbcSMatt Macy zfs_btree_t *size_tree = arg; 781eda14cbcSMatt Macy 782eda14cbcSMatt Macy zfs_btree_add(size_tree, rs); 783eda14cbcSMatt Macy } 784eda14cbcSMatt Macy 785eda14cbcSMatt Macy /* ARGSUSED */ 786eda14cbcSMatt Macy void 787eda14cbcSMatt Macy rt_btree_remove(range_tree_t *rt, range_seg_t *rs, void *arg) 788eda14cbcSMatt Macy { 789eda14cbcSMatt Macy zfs_btree_t *size_tree = arg; 790eda14cbcSMatt Macy 791eda14cbcSMatt Macy zfs_btree_remove(size_tree, rs); 792eda14cbcSMatt Macy } 793eda14cbcSMatt Macy 794eda14cbcSMatt Macy /* ARGSUSED */ 795eda14cbcSMatt Macy void 796eda14cbcSMatt Macy rt_btree_vacate(range_tree_t *rt, void *arg) 797eda14cbcSMatt Macy { 798eda14cbcSMatt Macy zfs_btree_t *size_tree = arg; 799eda14cbcSMatt Macy zfs_btree_clear(size_tree); 800eda14cbcSMatt Macy zfs_btree_destroy(size_tree); 801eda14cbcSMatt Macy 802eda14cbcSMatt Macy rt_btree_create(rt, arg); 803eda14cbcSMatt Macy } 804eda14cbcSMatt Macy 805eda14cbcSMatt Macy range_tree_ops_t rt_btree_ops = { 806eda14cbcSMatt Macy .rtop_create = rt_btree_create, 807eda14cbcSMatt Macy .rtop_destroy = rt_btree_destroy, 808eda14cbcSMatt Macy .rtop_add = rt_btree_add, 809eda14cbcSMatt Macy .rtop_remove = rt_btree_remove, 810eda14cbcSMatt Macy .rtop_vacate = rt_btree_vacate 811eda14cbcSMatt Macy }; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy /* 814eda14cbcSMatt Macy * Remove any overlapping ranges between the given segment [start, end) 815eda14cbcSMatt Macy * from removefrom. Add non-overlapping leftovers to addto. 816eda14cbcSMatt Macy */ 817eda14cbcSMatt Macy void 818eda14cbcSMatt Macy range_tree_remove_xor_add_segment(uint64_t start, uint64_t end, 819eda14cbcSMatt Macy range_tree_t *removefrom, range_tree_t *addto) 820eda14cbcSMatt Macy { 821eda14cbcSMatt Macy zfs_btree_index_t where; 822eda14cbcSMatt Macy range_seg_max_t starting_rs; 823eda14cbcSMatt Macy rs_set_start(&starting_rs, removefrom, start); 824eda14cbcSMatt Macy rs_set_end_raw(&starting_rs, removefrom, rs_get_start_raw(&starting_rs, 825eda14cbcSMatt Macy removefrom) + 1); 826eda14cbcSMatt Macy 827eda14cbcSMatt Macy range_seg_t *curr = zfs_btree_find(&removefrom->rt_root, 828eda14cbcSMatt Macy &starting_rs, &where); 829eda14cbcSMatt Macy 830eda14cbcSMatt Macy if (curr == NULL) 831eda14cbcSMatt Macy curr = zfs_btree_next(&removefrom->rt_root, &where, &where); 832eda14cbcSMatt Macy 833eda14cbcSMatt Macy range_seg_t *next; 834eda14cbcSMatt Macy for (; curr != NULL; curr = next) { 835eda14cbcSMatt Macy if (start == end) 836eda14cbcSMatt Macy return; 837eda14cbcSMatt Macy VERIFY3U(start, <, end); 838eda14cbcSMatt Macy 839eda14cbcSMatt Macy /* there is no overlap */ 840eda14cbcSMatt Macy if (end <= rs_get_start(curr, removefrom)) { 841eda14cbcSMatt Macy range_tree_add(addto, start, end - start); 842eda14cbcSMatt Macy return; 843eda14cbcSMatt Macy } 844eda14cbcSMatt Macy 845eda14cbcSMatt Macy uint64_t overlap_start = MAX(rs_get_start(curr, removefrom), 846eda14cbcSMatt Macy start); 847eda14cbcSMatt Macy uint64_t overlap_end = MIN(rs_get_end(curr, removefrom), 848eda14cbcSMatt Macy end); 849eda14cbcSMatt Macy uint64_t overlap_size = overlap_end - overlap_start; 850eda14cbcSMatt Macy ASSERT3S(overlap_size, >, 0); 851eda14cbcSMatt Macy range_seg_max_t rs; 852eda14cbcSMatt Macy rs_copy(curr, &rs, removefrom); 853eda14cbcSMatt Macy 854eda14cbcSMatt Macy range_tree_remove(removefrom, overlap_start, overlap_size); 855eda14cbcSMatt Macy 856eda14cbcSMatt Macy if (start < overlap_start) 857eda14cbcSMatt Macy range_tree_add(addto, start, overlap_start - start); 858eda14cbcSMatt Macy 859eda14cbcSMatt Macy start = overlap_end; 860eda14cbcSMatt Macy next = zfs_btree_find(&removefrom->rt_root, &rs, &where); 861eda14cbcSMatt Macy /* 862eda14cbcSMatt Macy * If we find something here, we only removed part of the 863eda14cbcSMatt Macy * curr segment. Either there's some left at the end 864eda14cbcSMatt Macy * because we've reached the end of the range we're removing, 865eda14cbcSMatt Macy * or there's some left at the start because we started 866eda14cbcSMatt Macy * partway through the range. Either way, we continue with 867eda14cbcSMatt Macy * the loop. If it's the former, we'll return at the start of 868eda14cbcSMatt Macy * the loop, and if it's the latter we'll see if there is more 869eda14cbcSMatt Macy * area to process. 870eda14cbcSMatt Macy */ 871eda14cbcSMatt Macy if (next != NULL) { 872eda14cbcSMatt Macy ASSERT(start == end || start == rs_get_end(&rs, 873eda14cbcSMatt Macy removefrom)); 874eda14cbcSMatt Macy } 875eda14cbcSMatt Macy 876eda14cbcSMatt Macy next = zfs_btree_next(&removefrom->rt_root, &where, &where); 877eda14cbcSMatt Macy } 878eda14cbcSMatt Macy VERIFY3P(curr, ==, NULL); 879eda14cbcSMatt Macy 880eda14cbcSMatt Macy if (start != end) { 881eda14cbcSMatt Macy VERIFY3U(start, <, end); 882eda14cbcSMatt Macy range_tree_add(addto, start, end - start); 883eda14cbcSMatt Macy } else { 884eda14cbcSMatt Macy VERIFY3U(start, ==, end); 885eda14cbcSMatt Macy } 886eda14cbcSMatt Macy } 887eda14cbcSMatt Macy 888eda14cbcSMatt Macy /* 889eda14cbcSMatt Macy * For each entry in rt, if it exists in removefrom, remove it 890eda14cbcSMatt Macy * from removefrom. Otherwise, add it to addto. 891eda14cbcSMatt Macy */ 892eda14cbcSMatt Macy void 893eda14cbcSMatt Macy range_tree_remove_xor_add(range_tree_t *rt, range_tree_t *removefrom, 894eda14cbcSMatt Macy range_tree_t *addto) 895eda14cbcSMatt Macy { 896eda14cbcSMatt Macy zfs_btree_index_t where; 897eda14cbcSMatt Macy for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); rs; 898eda14cbcSMatt Macy rs = zfs_btree_next(&rt->rt_root, &where, &where)) { 899eda14cbcSMatt Macy range_tree_remove_xor_add_segment(rs_get_start(rs, rt), 900eda14cbcSMatt Macy rs_get_end(rs, rt), removefrom, addto); 901eda14cbcSMatt Macy } 902eda14cbcSMatt Macy } 903eda14cbcSMatt Macy 904eda14cbcSMatt Macy uint64_t 905eda14cbcSMatt Macy range_tree_min(range_tree_t *rt) 906eda14cbcSMatt Macy { 907eda14cbcSMatt Macy range_seg_t *rs = zfs_btree_first(&rt->rt_root, NULL); 908eda14cbcSMatt Macy return (rs != NULL ? rs_get_start(rs, rt) : 0); 909eda14cbcSMatt Macy } 910eda14cbcSMatt Macy 911eda14cbcSMatt Macy uint64_t 912eda14cbcSMatt Macy range_tree_max(range_tree_t *rt) 913eda14cbcSMatt Macy { 914eda14cbcSMatt Macy range_seg_t *rs = zfs_btree_last(&rt->rt_root, NULL); 915eda14cbcSMatt Macy return (rs != NULL ? rs_get_end(rs, rt) : 0); 916eda14cbcSMatt Macy } 917eda14cbcSMatt Macy 918eda14cbcSMatt Macy uint64_t 919eda14cbcSMatt Macy range_tree_span(range_tree_t *rt) 920eda14cbcSMatt Macy { 921eda14cbcSMatt Macy return (range_tree_max(rt) - range_tree_min(rt)); 922eda14cbcSMatt Macy } 923