xref: /freebsd/sys/contrib/openzfs/module/zfs/space_reftree.c (revision 3078531de10dcae44b253a35125c949ff4235284)
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 https://opensource.org/licenses/CDDL-1.0.
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  * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
27  */
28 
29 #include <sys/zfs_context.h>
30 #include <sys/range_tree.h>
31 #include <sys/space_reftree.h>
32 
33 /*
34  * Space reference trees.
35  *
36  * A range tree is a collection of integers.  Every integer is either
37  * in the tree, or it's not.  A space reference tree generalizes
38  * the idea: it allows its members to have arbitrary reference counts,
39  * as opposed to the implicit reference count of 0 or 1 in a range tree.
40  * This representation comes in handy when computing the union or
41  * intersection of multiple space maps.  For example, the union of
42  * N range trees is the subset of the reference tree with refcnt >= 1.
43  * The intersection of N range trees is the subset with refcnt >= N.
44  *
45  * [It's very much like a Fourier transform.  Unions and intersections
46  * are hard to perform in the 'range tree domain', so we convert the trees
47  * into the 'reference count domain', where it's trivial, then invert.]
48  *
49  * vdev_dtl_reassess() uses computations of this form to determine
50  * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
51  * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
52  * has an outage wherever refcnt >= vdev_children.
53  */
54 static int
55 space_reftree_compare(const void *x1, const void *x2)
56 {
57 	const space_ref_t *sr1 = (const space_ref_t *)x1;
58 	const space_ref_t *sr2 = (const space_ref_t *)x2;
59 
60 	int cmp = TREE_CMP(sr1->sr_offset, sr2->sr_offset);
61 	if (likely(cmp))
62 		return (cmp);
63 
64 	return (TREE_PCMP(sr1, sr2));
65 }
66 
67 void
68 space_reftree_create(avl_tree_t *t)
69 {
70 	avl_create(t, space_reftree_compare,
71 	    sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
72 }
73 
74 void
75 space_reftree_destroy(avl_tree_t *t)
76 {
77 	space_ref_t *sr;
78 	void *cookie = NULL;
79 
80 	while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
81 		kmem_free(sr, sizeof (*sr));
82 
83 	avl_destroy(t);
84 }
85 
86 static void
87 space_reftree_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
88 {
89 	space_ref_t *sr;
90 
91 	sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
92 	sr->sr_offset = offset;
93 	sr->sr_refcnt = refcnt;
94 
95 	avl_add(t, sr);
96 }
97 
98 void
99 space_reftree_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
100     int64_t refcnt)
101 {
102 	space_reftree_add_node(t, start, refcnt);
103 	space_reftree_add_node(t, end, -refcnt);
104 }
105 
106 /*
107  * Convert (or add) a range tree into a reference tree.
108  */
109 void
110 space_reftree_add_map(avl_tree_t *t, range_tree_t *rt, int64_t refcnt)
111 {
112 	zfs_btree_index_t where;
113 
114 	for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); rs; rs =
115 	    zfs_btree_next(&rt->rt_root, &where, &where)) {
116 		space_reftree_add_seg(t, rs_get_start(rs, rt), rs_get_end(rs,
117 		    rt),  refcnt);
118 	}
119 }
120 
121 /*
122  * Convert a reference tree into a range tree.  The range tree will contain
123  * all members of the reference tree for which refcnt >= minref.
124  */
125 void
126 space_reftree_generate_map(avl_tree_t *t, range_tree_t *rt, int64_t minref)
127 {
128 	uint64_t start = -1ULL;
129 	int64_t refcnt = 0;
130 	space_ref_t *sr;
131 
132 	range_tree_vacate(rt, NULL, NULL);
133 
134 	for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
135 		refcnt += sr->sr_refcnt;
136 		if (refcnt >= minref) {
137 			if (start == -1ULL) {
138 				start = sr->sr_offset;
139 			}
140 		} else {
141 			if (start != -1ULL) {
142 				uint64_t end = sr->sr_offset;
143 				ASSERT(start <= end);
144 				if (end > start)
145 					range_tree_add(rt, start, end - start);
146 				start = -1ULL;
147 			}
148 		}
149 	}
150 	ASSERT(refcnt == 0);
151 	ASSERT(start == -1ULL);
152 }
153