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