161145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24eda14cbcSMatt Macy * Use is subject to license terms.
25eda14cbcSMatt Macy */
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
28eda14cbcSMatt Macy */
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy #include <sys/zfs_context.h>
31eda14cbcSMatt Macy #include <sys/range_tree.h>
32eda14cbcSMatt Macy #include <sys/space_reftree.h>
33eda14cbcSMatt Macy
34eda14cbcSMatt Macy /*
35eda14cbcSMatt Macy * Space reference trees.
36eda14cbcSMatt Macy *
37eda14cbcSMatt Macy * A range tree is a collection of integers. Every integer is either
38eda14cbcSMatt Macy * in the tree, or it's not. A space reference tree generalizes
39eda14cbcSMatt Macy * the idea: it allows its members to have arbitrary reference counts,
40eda14cbcSMatt Macy * as opposed to the implicit reference count of 0 or 1 in a range tree.
41eda14cbcSMatt Macy * This representation comes in handy when computing the union or
42eda14cbcSMatt Macy * intersection of multiple space maps. For example, the union of
43eda14cbcSMatt Macy * N range trees is the subset of the reference tree with refcnt >= 1.
44eda14cbcSMatt Macy * The intersection of N range trees is the subset with refcnt >= N.
45eda14cbcSMatt Macy *
46eda14cbcSMatt Macy * [It's very much like a Fourier transform. Unions and intersections
47eda14cbcSMatt Macy * are hard to perform in the 'range tree domain', so we convert the trees
48eda14cbcSMatt Macy * into the 'reference count domain', where it's trivial, then invert.]
49eda14cbcSMatt Macy *
50eda14cbcSMatt Macy * vdev_dtl_reassess() uses computations of this form to determine
51eda14cbcSMatt Macy * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
52eda14cbcSMatt Macy * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
53eda14cbcSMatt Macy * has an outage wherever refcnt >= vdev_children.
54eda14cbcSMatt Macy */
55eda14cbcSMatt Macy static int
space_reftree_compare(const void * x1,const void * x2)56eda14cbcSMatt Macy space_reftree_compare(const void *x1, const void *x2)
57eda14cbcSMatt Macy {
58eda14cbcSMatt Macy const space_ref_t *sr1 = (const space_ref_t *)x1;
59eda14cbcSMatt Macy const space_ref_t *sr2 = (const space_ref_t *)x2;
60eda14cbcSMatt Macy
61eda14cbcSMatt Macy int cmp = TREE_CMP(sr1->sr_offset, sr2->sr_offset);
62eda14cbcSMatt Macy if (likely(cmp))
63eda14cbcSMatt Macy return (cmp);
64eda14cbcSMatt Macy
65eda14cbcSMatt Macy return (TREE_PCMP(sr1, sr2));
66eda14cbcSMatt Macy }
67eda14cbcSMatt Macy
68eda14cbcSMatt Macy void
space_reftree_create(avl_tree_t * t)69eda14cbcSMatt Macy space_reftree_create(avl_tree_t *t)
70eda14cbcSMatt Macy {
71eda14cbcSMatt Macy avl_create(t, space_reftree_compare,
72eda14cbcSMatt Macy sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
73eda14cbcSMatt Macy }
74eda14cbcSMatt Macy
75eda14cbcSMatt Macy void
space_reftree_destroy(avl_tree_t * t)76eda14cbcSMatt Macy space_reftree_destroy(avl_tree_t *t)
77eda14cbcSMatt Macy {
78eda14cbcSMatt Macy space_ref_t *sr;
79eda14cbcSMatt Macy void *cookie = NULL;
80eda14cbcSMatt Macy
81eda14cbcSMatt Macy while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
82eda14cbcSMatt Macy kmem_free(sr, sizeof (*sr));
83eda14cbcSMatt Macy
84eda14cbcSMatt Macy avl_destroy(t);
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy
87eda14cbcSMatt Macy static void
space_reftree_add_node(avl_tree_t * t,uint64_t offset,int64_t refcnt)88eda14cbcSMatt Macy space_reftree_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy space_ref_t *sr;
91eda14cbcSMatt Macy
92eda14cbcSMatt Macy sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
93eda14cbcSMatt Macy sr->sr_offset = offset;
94eda14cbcSMatt Macy sr->sr_refcnt = refcnt;
95eda14cbcSMatt Macy
96eda14cbcSMatt Macy avl_add(t, sr);
97eda14cbcSMatt Macy }
98eda14cbcSMatt Macy
99eda14cbcSMatt Macy void
space_reftree_add_seg(avl_tree_t * t,uint64_t start,uint64_t end,int64_t refcnt)100eda14cbcSMatt Macy space_reftree_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
101eda14cbcSMatt Macy int64_t refcnt)
102eda14cbcSMatt Macy {
103eda14cbcSMatt Macy space_reftree_add_node(t, start, refcnt);
104eda14cbcSMatt Macy space_reftree_add_node(t, end, -refcnt);
105eda14cbcSMatt Macy }
106eda14cbcSMatt Macy
107eda14cbcSMatt Macy /*
108eda14cbcSMatt Macy * Convert (or add) a range tree into a reference tree.
109eda14cbcSMatt Macy */
110eda14cbcSMatt Macy void
space_reftree_add_map(avl_tree_t * t,zfs_range_tree_t * rt,int64_t refcnt)111b59a0cdeSMartin Matuska space_reftree_add_map(avl_tree_t *t, zfs_range_tree_t *rt, int64_t refcnt)
112eda14cbcSMatt Macy {
113eda14cbcSMatt Macy zfs_btree_index_t where;
114eda14cbcSMatt Macy
115b59a0cdeSMartin Matuska for (zfs_range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); rs;
116b59a0cdeSMartin Matuska rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
117b59a0cdeSMartin Matuska space_reftree_add_seg(t, zfs_rs_get_start(rs, rt),
118b59a0cdeSMartin Matuska zfs_rs_get_end(rs, rt), refcnt);
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy }
121eda14cbcSMatt Macy
122eda14cbcSMatt Macy /*
123eda14cbcSMatt Macy * Convert a reference tree into a range tree. The range tree will contain
124eda14cbcSMatt Macy * all members of the reference tree for which refcnt >= minref.
125eda14cbcSMatt Macy */
126eda14cbcSMatt Macy void
space_reftree_generate_map(avl_tree_t * t,zfs_range_tree_t * rt,int64_t minref)127b59a0cdeSMartin Matuska space_reftree_generate_map(avl_tree_t *t, zfs_range_tree_t *rt, int64_t minref)
128eda14cbcSMatt Macy {
129eda14cbcSMatt Macy uint64_t start = -1ULL;
130eda14cbcSMatt Macy int64_t refcnt = 0;
131eda14cbcSMatt Macy space_ref_t *sr;
132eda14cbcSMatt Macy
133b59a0cdeSMartin Matuska zfs_range_tree_vacate(rt, NULL, NULL);
134eda14cbcSMatt Macy
135eda14cbcSMatt Macy for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
136eda14cbcSMatt Macy refcnt += sr->sr_refcnt;
137eda14cbcSMatt Macy if (refcnt >= minref) {
138eda14cbcSMatt Macy if (start == -1ULL) {
139eda14cbcSMatt Macy start = sr->sr_offset;
140eda14cbcSMatt Macy }
141eda14cbcSMatt Macy } else {
142eda14cbcSMatt Macy if (start != -1ULL) {
143eda14cbcSMatt Macy uint64_t end = sr->sr_offset;
144eda14cbcSMatt Macy ASSERT(start <= end);
145eda14cbcSMatt Macy if (end > start)
146b59a0cdeSMartin Matuska zfs_range_tree_add(rt, start, end -
147b59a0cdeSMartin Matuska start);
148eda14cbcSMatt Macy start = -1ULL;
149eda14cbcSMatt Macy }
150eda14cbcSMatt Macy }
151eda14cbcSMatt Macy }
152*d0abb9a6SMartin Matuska ASSERT0(refcnt);
153eda14cbcSMatt Macy ASSERT(start == -1ULL);
154eda14cbcSMatt Macy }
155