xref: /freebsd/sys/contrib/openzfs/module/zfs/space_map.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin 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) 2012, 2019 by Delphix. 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/dmu_tx.h>
34eda14cbcSMatt Macy #include <sys/dnode.h>
35eda14cbcSMatt Macy #include <sys/dsl_pool.h>
36eda14cbcSMatt Macy #include <sys/zio.h>
37eda14cbcSMatt Macy #include <sys/space_map.h>
38eda14cbcSMatt Macy #include <sys/zfeature.h>
39eda14cbcSMatt Macy 
40eda14cbcSMatt Macy /*
41eda14cbcSMatt Macy  * Note on space map block size:
42eda14cbcSMatt Macy  *
43eda14cbcSMatt Macy  * The data for a given space map can be kept on blocks of any size.
44eda14cbcSMatt Macy  * Larger blocks entail fewer I/O operations, but they also cause the
45eda14cbcSMatt Macy  * DMU to keep more data in-core, and also to waste more I/O bandwidth
46eda14cbcSMatt Macy  * when only a few blocks have changed since the last transaction group.
47eda14cbcSMatt Macy  */
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy /*
50eda14cbcSMatt Macy  * Enabled whenever we want to stress test the use of double-word
51eda14cbcSMatt Macy  * space map entries.
52eda14cbcSMatt Macy  */
53eda14cbcSMatt Macy boolean_t zfs_force_some_double_word_sm_entries = B_FALSE;
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy /*
56eda14cbcSMatt Macy  * Override the default indirect block size of 128K, instead use 16K for
57eda14cbcSMatt Macy  * spacemaps (2^14 bytes).  This dramatically reduces write inflation since
58eda14cbcSMatt Macy  * appending to a spacemap typically has to write one data block (4KB) and one
59eda14cbcSMatt Macy  * or two indirect blocks (16K-32K, rather than 128K).
60eda14cbcSMatt Macy  */
61eda14cbcSMatt Macy int space_map_ibs = 14;
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy boolean_t
sm_entry_is_debug(uint64_t e)64eda14cbcSMatt Macy sm_entry_is_debug(uint64_t e)
65eda14cbcSMatt Macy {
66eda14cbcSMatt Macy 	return (SM_PREFIX_DECODE(e) == SM_DEBUG_PREFIX);
67eda14cbcSMatt Macy }
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy boolean_t
sm_entry_is_single_word(uint64_t e)70eda14cbcSMatt Macy sm_entry_is_single_word(uint64_t e)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy 	uint8_t prefix = SM_PREFIX_DECODE(e);
73eda14cbcSMatt Macy 	return (prefix != SM_DEBUG_PREFIX && prefix != SM2_PREFIX);
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy boolean_t
sm_entry_is_double_word(uint64_t e)77eda14cbcSMatt Macy sm_entry_is_double_word(uint64_t e)
78eda14cbcSMatt Macy {
79eda14cbcSMatt Macy 	return (SM_PREFIX_DECODE(e) == SM2_PREFIX);
80eda14cbcSMatt Macy }
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy /*
83eda14cbcSMatt Macy  * Iterate through the space map, invoking the callback on each (non-debug)
84eda14cbcSMatt Macy  * space map entry. Stop after reading 'end' bytes of the space map.
85eda14cbcSMatt Macy  */
86eda14cbcSMatt Macy int
space_map_iterate(space_map_t * sm,uint64_t end,sm_cb_t callback,void * arg)87eda14cbcSMatt Macy space_map_iterate(space_map_t *sm, uint64_t end, sm_cb_t callback, void *arg)
88eda14cbcSMatt Macy {
89eda14cbcSMatt Macy 	uint64_t blksz = sm->sm_blksz;
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 	ASSERT3U(blksz, !=, 0);
92eda14cbcSMatt Macy 	ASSERT3U(end, <=, space_map_length(sm));
93eda14cbcSMatt Macy 	ASSERT0(P2PHASE(end, sizeof (uint64_t)));
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 	dmu_prefetch(sm->sm_os, space_map_object(sm), 0, 0, end,
96eda14cbcSMatt Macy 	    ZIO_PRIORITY_SYNC_READ);
97eda14cbcSMatt Macy 
98eda14cbcSMatt Macy 	int error = 0;
99eda14cbcSMatt Macy 	uint64_t txg = 0, sync_pass = 0;
100eda14cbcSMatt Macy 	for (uint64_t block_base = 0; block_base < end && error == 0;
101eda14cbcSMatt Macy 	    block_base += blksz) {
102eda14cbcSMatt Macy 		dmu_buf_t *db;
103eda14cbcSMatt Macy 		error = dmu_buf_hold(sm->sm_os, space_map_object(sm),
104eda14cbcSMatt Macy 		    block_base, FTAG, &db, DMU_READ_PREFETCH);
105eda14cbcSMatt Macy 		if (error != 0)
106eda14cbcSMatt Macy 			return (error);
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy 		uint64_t *block_start = db->db_data;
109eda14cbcSMatt Macy 		uint64_t block_length = MIN(end - block_base, blksz);
110eda14cbcSMatt Macy 		uint64_t *block_end = block_start +
111eda14cbcSMatt Macy 		    (block_length / sizeof (uint64_t));
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy 		VERIFY0(P2PHASE(block_length, sizeof (uint64_t)));
114eda14cbcSMatt Macy 		VERIFY3U(block_length, !=, 0);
115eda14cbcSMatt Macy 		ASSERT3U(blksz, ==, db->db_size);
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy 		for (uint64_t *block_cursor = block_start;
118eda14cbcSMatt Macy 		    block_cursor < block_end && error == 0; block_cursor++) {
119eda14cbcSMatt Macy 			uint64_t e = *block_cursor;
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy 			if (sm_entry_is_debug(e)) {
122eda14cbcSMatt Macy 				/*
123eda14cbcSMatt Macy 				 * Debug entries are only needed to record the
124eda14cbcSMatt Macy 				 * current TXG and sync pass if available.
125eda14cbcSMatt Macy 				 *
126eda14cbcSMatt Macy 				 * Note though that sometimes there can be
127eda14cbcSMatt Macy 				 * debug entries that are used as padding
128eda14cbcSMatt Macy 				 * at the end of space map blocks in-order
129eda14cbcSMatt Macy 				 * to not split a double-word entry in the
130eda14cbcSMatt Macy 				 * middle between two blocks. These entries
131eda14cbcSMatt Macy 				 * have their TXG field set to 0 and we
132eda14cbcSMatt Macy 				 * skip them without recording the TXG.
133eda14cbcSMatt Macy 				 * [see comment in space_map_write_seg()]
134eda14cbcSMatt Macy 				 */
135eda14cbcSMatt Macy 				uint64_t e_txg = SM_DEBUG_TXG_DECODE(e);
136eda14cbcSMatt Macy 				if (e_txg != 0) {
137eda14cbcSMatt Macy 					txg = e_txg;
138eda14cbcSMatt Macy 					sync_pass = SM_DEBUG_SYNCPASS_DECODE(e);
139eda14cbcSMatt Macy 				} else {
140eda14cbcSMatt Macy 					ASSERT0(SM_DEBUG_SYNCPASS_DECODE(e));
141eda14cbcSMatt Macy 				}
142eda14cbcSMatt Macy 				continue;
143eda14cbcSMatt Macy 			}
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 			uint64_t raw_offset, raw_run, vdev_id;
146eda14cbcSMatt Macy 			maptype_t type;
147eda14cbcSMatt Macy 			if (sm_entry_is_single_word(e)) {
148eda14cbcSMatt Macy 				type = SM_TYPE_DECODE(e);
149eda14cbcSMatt Macy 				vdev_id = SM_NO_VDEVID;
150eda14cbcSMatt Macy 				raw_offset = SM_OFFSET_DECODE(e);
151eda14cbcSMatt Macy 				raw_run = SM_RUN_DECODE(e);
152eda14cbcSMatt Macy 			} else {
153eda14cbcSMatt Macy 				/* it is a two-word entry */
154eda14cbcSMatt Macy 				ASSERT(sm_entry_is_double_word(e));
155eda14cbcSMatt Macy 				raw_run = SM2_RUN_DECODE(e);
156eda14cbcSMatt Macy 				vdev_id = SM2_VDEV_DECODE(e);
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 				/* move on to the second word */
159eda14cbcSMatt Macy 				block_cursor++;
160eda14cbcSMatt Macy 				e = *block_cursor;
161eda14cbcSMatt Macy 				VERIFY3P(block_cursor, <=, block_end);
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 				type = SM2_TYPE_DECODE(e);
164eda14cbcSMatt Macy 				raw_offset = SM2_OFFSET_DECODE(e);
165eda14cbcSMatt Macy 			}
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 			uint64_t entry_offset = (raw_offset << sm->sm_shift) +
168eda14cbcSMatt Macy 			    sm->sm_start;
169eda14cbcSMatt Macy 			uint64_t entry_run = raw_run << sm->sm_shift;
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy 			VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift));
172eda14cbcSMatt Macy 			VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift));
173eda14cbcSMatt Macy 			ASSERT3U(entry_offset, >=, sm->sm_start);
174eda14cbcSMatt Macy 			ASSERT3U(entry_offset, <, sm->sm_start + sm->sm_size);
175eda14cbcSMatt Macy 			ASSERT3U(entry_run, <=, sm->sm_size);
176eda14cbcSMatt Macy 			ASSERT3U(entry_offset + entry_run, <=,
177eda14cbcSMatt Macy 			    sm->sm_start + sm->sm_size);
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 			space_map_entry_t sme = {
180eda14cbcSMatt Macy 			    .sme_type = type,
181eda14cbcSMatt Macy 			    .sme_vdev = vdev_id,
182eda14cbcSMatt Macy 			    .sme_offset = entry_offset,
183eda14cbcSMatt Macy 			    .sme_run = entry_run,
184eda14cbcSMatt Macy 			    .sme_txg = txg,
185eda14cbcSMatt Macy 			    .sme_sync_pass = sync_pass
186eda14cbcSMatt Macy 			};
187eda14cbcSMatt Macy 			error = callback(&sme, arg);
188eda14cbcSMatt Macy 		}
189eda14cbcSMatt Macy 		dmu_buf_rele(db, FTAG);
190eda14cbcSMatt Macy 	}
191eda14cbcSMatt Macy 	return (error);
192eda14cbcSMatt Macy }
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy /*
195eda14cbcSMatt Macy  * Reads the entries from the last block of the space map into
196eda14cbcSMatt Macy  * buf in reverse order. Populates nwords with number of words
197eda14cbcSMatt Macy  * in the last block.
198eda14cbcSMatt Macy  *
199eda14cbcSMatt Macy  * Refer to block comment within space_map_incremental_destroy()
200eda14cbcSMatt Macy  * to understand why this function is needed.
201eda14cbcSMatt Macy  */
202eda14cbcSMatt Macy static int
space_map_reversed_last_block_entries(space_map_t * sm,uint64_t * buf,uint64_t bufsz,uint64_t * nwords)203eda14cbcSMatt Macy space_map_reversed_last_block_entries(space_map_t *sm, uint64_t *buf,
204eda14cbcSMatt Macy     uint64_t bufsz, uint64_t *nwords)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	int error = 0;
207eda14cbcSMatt Macy 	dmu_buf_t *db;
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	/*
210eda14cbcSMatt Macy 	 * Find the offset of the last word in the space map and use
211eda14cbcSMatt Macy 	 * that to read the last block of the space map with
212eda14cbcSMatt Macy 	 * dmu_buf_hold().
213eda14cbcSMatt Macy 	 */
214eda14cbcSMatt Macy 	uint64_t last_word_offset =
215eda14cbcSMatt Macy 	    sm->sm_phys->smp_length - sizeof (uint64_t);
216eda14cbcSMatt Macy 	error = dmu_buf_hold(sm->sm_os, space_map_object(sm), last_word_offset,
217eda14cbcSMatt Macy 	    FTAG, &db, DMU_READ_NO_PREFETCH);
218eda14cbcSMatt Macy 	if (error != 0)
219eda14cbcSMatt Macy 		return (error);
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy 	ASSERT3U(sm->sm_object, ==, db->db_object);
222eda14cbcSMatt Macy 	ASSERT3U(sm->sm_blksz, ==, db->db_size);
223eda14cbcSMatt Macy 	ASSERT3U(bufsz, >=, db->db_size);
224eda14cbcSMatt Macy 	ASSERT(nwords != NULL);
225eda14cbcSMatt Macy 
226eda14cbcSMatt Macy 	uint64_t *words = db->db_data;
227eda14cbcSMatt Macy 	*nwords =
228eda14cbcSMatt Macy 	    (sm->sm_phys->smp_length - db->db_offset) / sizeof (uint64_t);
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 	ASSERT3U(*nwords, <=, bufsz / sizeof (uint64_t));
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 	uint64_t n = *nwords;
233eda14cbcSMatt Macy 	uint64_t j = n - 1;
234eda14cbcSMatt Macy 	for (uint64_t i = 0; i < n; i++) {
235eda14cbcSMatt Macy 		uint64_t entry = words[i];
236eda14cbcSMatt Macy 		if (sm_entry_is_double_word(entry)) {
237eda14cbcSMatt Macy 			/*
238eda14cbcSMatt Macy 			 * Since we are populating the buffer backwards
239eda14cbcSMatt Macy 			 * we have to be extra careful and add the two
240eda14cbcSMatt Macy 			 * words of the double-word entry in the right
241eda14cbcSMatt Macy 			 * order.
242eda14cbcSMatt Macy 			 */
243eda14cbcSMatt Macy 			ASSERT3U(j, >, 0);
244eda14cbcSMatt Macy 			buf[j - 1] = entry;
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 			i++;
247eda14cbcSMatt Macy 			ASSERT3U(i, <, n);
248eda14cbcSMatt Macy 			entry = words[i];
249eda14cbcSMatt Macy 			buf[j] = entry;
250eda14cbcSMatt Macy 			j -= 2;
251eda14cbcSMatt Macy 		} else {
252eda14cbcSMatt Macy 			ASSERT(sm_entry_is_debug(entry) ||
253eda14cbcSMatt Macy 			    sm_entry_is_single_word(entry));
254eda14cbcSMatt Macy 			buf[j] = entry;
255eda14cbcSMatt Macy 			j--;
256eda14cbcSMatt Macy 		}
257eda14cbcSMatt Macy 	}
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	/*
260eda14cbcSMatt Macy 	 * Assert that we wrote backwards all the
261eda14cbcSMatt Macy 	 * way to the beginning of the buffer.
262eda14cbcSMatt Macy 	 */
263eda14cbcSMatt Macy 	ASSERT3S(j, ==, -1);
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 	dmu_buf_rele(db, FTAG);
266eda14cbcSMatt Macy 	return (error);
267eda14cbcSMatt Macy }
268eda14cbcSMatt Macy 
269eda14cbcSMatt Macy /*
270eda14cbcSMatt Macy  * Note: This function performs destructive actions - specifically
271eda14cbcSMatt Macy  * it deletes entries from the end of the space map. Thus, callers
272eda14cbcSMatt Macy  * should ensure that they are holding the appropriate locks for
273eda14cbcSMatt Macy  * the space map that they provide.
274eda14cbcSMatt Macy  */
275eda14cbcSMatt Macy int
space_map_incremental_destroy(space_map_t * sm,sm_cb_t callback,void * arg,dmu_tx_t * tx)276eda14cbcSMatt Macy space_map_incremental_destroy(space_map_t *sm, sm_cb_t callback, void *arg,
277eda14cbcSMatt Macy     dmu_tx_t *tx)
278eda14cbcSMatt Macy {
279eda14cbcSMatt Macy 	uint64_t bufsz = MAX(sm->sm_blksz, SPA_MINBLOCKSIZE);
280eda14cbcSMatt Macy 	uint64_t *buf = zio_buf_alloc(bufsz);
281eda14cbcSMatt Macy 
282eda14cbcSMatt Macy 	dmu_buf_will_dirty(sm->sm_dbuf, tx);
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy 	/*
285eda14cbcSMatt Macy 	 * Ideally we would want to iterate from the beginning of the
286eda14cbcSMatt Macy 	 * space map to the end in incremental steps. The issue with this
287eda14cbcSMatt Macy 	 * approach is that we don't have any field on-disk that points
288eda14cbcSMatt Macy 	 * us where to start between each step. We could try zeroing out
289eda14cbcSMatt Macy 	 * entries that we've destroyed, but this doesn't work either as
290eda14cbcSMatt Macy 	 * an entry that is 0 is a valid one (ALLOC for range [0x0:0x200]).
291eda14cbcSMatt Macy 	 *
292eda14cbcSMatt Macy 	 * As a result, we destroy its entries incrementally starting from
293eda14cbcSMatt Macy 	 * the end after applying the callback to each of them.
294eda14cbcSMatt Macy 	 *
295eda14cbcSMatt Macy 	 * The problem with this approach is that we cannot literally
296eda14cbcSMatt Macy 	 * iterate through the words in the space map backwards as we
297eda14cbcSMatt Macy 	 * can't distinguish two-word space map entries from their second
298eda14cbcSMatt Macy 	 * word. Thus we do the following:
299eda14cbcSMatt Macy 	 *
300eda14cbcSMatt Macy 	 * 1] We get all the entries from the last block of the space map
301eda14cbcSMatt Macy 	 *    and put them into a buffer in reverse order. This way the
302eda14cbcSMatt Macy 	 *    last entry comes first in the buffer, the second to last is
303eda14cbcSMatt Macy 	 *    second, etc.
304eda14cbcSMatt Macy 	 * 2] We iterate through the entries in the buffer and we apply
305eda14cbcSMatt Macy 	 *    the callback to each one. As we move from entry to entry we
306eda14cbcSMatt Macy 	 *    we decrease the size of the space map, deleting effectively
307eda14cbcSMatt Macy 	 *    each entry.
308eda14cbcSMatt Macy 	 * 3] If there are no more entries in the space map or the callback
309eda14cbcSMatt Macy 	 *    returns a value other than 0, we stop iterating over the
310eda14cbcSMatt Macy 	 *    space map. If there are entries remaining and the callback
311eda14cbcSMatt Macy 	 *    returned 0, we go back to step [1].
312eda14cbcSMatt Macy 	 */
313eda14cbcSMatt Macy 	int error = 0;
314eda14cbcSMatt Macy 	while (space_map_length(sm) > 0 && error == 0) {
315eda14cbcSMatt Macy 		uint64_t nwords = 0;
316eda14cbcSMatt Macy 		error = space_map_reversed_last_block_entries(sm, buf, bufsz,
317eda14cbcSMatt Macy 		    &nwords);
318eda14cbcSMatt Macy 		if (error != 0)
319eda14cbcSMatt Macy 			break;
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 		ASSERT3U(nwords, <=, bufsz / sizeof (uint64_t));
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 		for (uint64_t i = 0; i < nwords; i++) {
324eda14cbcSMatt Macy 			uint64_t e = buf[i];
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 			if (sm_entry_is_debug(e)) {
327eda14cbcSMatt Macy 				sm->sm_phys->smp_length -= sizeof (uint64_t);
328eda14cbcSMatt Macy 				continue;
329eda14cbcSMatt Macy 			}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 			int words = 1;
332eda14cbcSMatt Macy 			uint64_t raw_offset, raw_run, vdev_id;
333eda14cbcSMatt Macy 			maptype_t type;
334eda14cbcSMatt Macy 			if (sm_entry_is_single_word(e)) {
335eda14cbcSMatt Macy 				type = SM_TYPE_DECODE(e);
336eda14cbcSMatt Macy 				vdev_id = SM_NO_VDEVID;
337eda14cbcSMatt Macy 				raw_offset = SM_OFFSET_DECODE(e);
338eda14cbcSMatt Macy 				raw_run = SM_RUN_DECODE(e);
339eda14cbcSMatt Macy 			} else {
340eda14cbcSMatt Macy 				ASSERT(sm_entry_is_double_word(e));
341eda14cbcSMatt Macy 				words = 2;
342eda14cbcSMatt Macy 
343eda14cbcSMatt Macy 				raw_run = SM2_RUN_DECODE(e);
344eda14cbcSMatt Macy 				vdev_id = SM2_VDEV_DECODE(e);
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 				/* move to the second word */
347eda14cbcSMatt Macy 				i++;
348eda14cbcSMatt Macy 				e = buf[i];
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy 				ASSERT3P(i, <=, nwords);
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy 				type = SM2_TYPE_DECODE(e);
353eda14cbcSMatt Macy 				raw_offset = SM2_OFFSET_DECODE(e);
354eda14cbcSMatt Macy 			}
355eda14cbcSMatt Macy 
356eda14cbcSMatt Macy 			uint64_t entry_offset =
357eda14cbcSMatt Macy 			    (raw_offset << sm->sm_shift) + sm->sm_start;
358eda14cbcSMatt Macy 			uint64_t entry_run = raw_run << sm->sm_shift;
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy 			VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift));
361eda14cbcSMatt Macy 			VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift));
362eda14cbcSMatt Macy 			VERIFY3U(entry_offset, >=, sm->sm_start);
363eda14cbcSMatt Macy 			VERIFY3U(entry_offset, <, sm->sm_start + sm->sm_size);
364eda14cbcSMatt Macy 			VERIFY3U(entry_run, <=, sm->sm_size);
365eda14cbcSMatt Macy 			VERIFY3U(entry_offset + entry_run, <=,
366eda14cbcSMatt Macy 			    sm->sm_start + sm->sm_size);
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 			space_map_entry_t sme = {
369eda14cbcSMatt Macy 			    .sme_type = type,
370eda14cbcSMatt Macy 			    .sme_vdev = vdev_id,
371eda14cbcSMatt Macy 			    .sme_offset = entry_offset,
372eda14cbcSMatt Macy 			    .sme_run = entry_run
373eda14cbcSMatt Macy 			};
374eda14cbcSMatt Macy 			error = callback(&sme, arg);
375eda14cbcSMatt Macy 			if (error != 0)
376eda14cbcSMatt Macy 				break;
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 			if (type == SM_ALLOC)
379eda14cbcSMatt Macy 				sm->sm_phys->smp_alloc -= entry_run;
380eda14cbcSMatt Macy 			else
381eda14cbcSMatt Macy 				sm->sm_phys->smp_alloc += entry_run;
382eda14cbcSMatt Macy 			sm->sm_phys->smp_length -= words * sizeof (uint64_t);
383eda14cbcSMatt Macy 		}
384eda14cbcSMatt Macy 	}
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy 	if (space_map_length(sm) == 0) {
387eda14cbcSMatt Macy 		ASSERT0(error);
388eda14cbcSMatt Macy 		ASSERT0(space_map_allocated(sm));
389eda14cbcSMatt Macy 	}
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 	zio_buf_free(buf, bufsz);
392eda14cbcSMatt Macy 	return (error);
393eda14cbcSMatt Macy }
394eda14cbcSMatt Macy 
395eda14cbcSMatt Macy typedef struct space_map_load_arg {
396eda14cbcSMatt Macy 	space_map_t	*smla_sm;
397b59a0cdeSMartin Matuska 	zfs_range_tree_t	*smla_rt;
398eda14cbcSMatt Macy 	maptype_t	smla_type;
399eda14cbcSMatt Macy } space_map_load_arg_t;
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy static int
space_map_load_callback(space_map_entry_t * sme,void * arg)402eda14cbcSMatt Macy space_map_load_callback(space_map_entry_t *sme, void *arg)
403eda14cbcSMatt Macy {
404eda14cbcSMatt Macy 	space_map_load_arg_t *smla = arg;
405eda14cbcSMatt Macy 	if (sme->sme_type == smla->smla_type) {
406b59a0cdeSMartin Matuska 		VERIFY3U(zfs_range_tree_space(smla->smla_rt) + sme->sme_run, <=,
407eda14cbcSMatt Macy 		    smla->smla_sm->sm_size);
408b59a0cdeSMartin Matuska 		zfs_range_tree_add(smla->smla_rt, sme->sme_offset,
409b59a0cdeSMartin Matuska 		    sme->sme_run);
410eda14cbcSMatt Macy 	} else {
411b59a0cdeSMartin Matuska 		zfs_range_tree_remove(smla->smla_rt, sme->sme_offset,
412b59a0cdeSMartin Matuska 		    sme->sme_run);
413eda14cbcSMatt Macy 	}
414eda14cbcSMatt Macy 
415eda14cbcSMatt Macy 	return (0);
416eda14cbcSMatt Macy }
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy /*
419eda14cbcSMatt Macy  * Load the spacemap into the rangetree, like space_map_load. But only
420eda14cbcSMatt Macy  * read the first 'length' bytes of the spacemap.
421eda14cbcSMatt Macy  */
422eda14cbcSMatt Macy int
space_map_load_length(space_map_t * sm,zfs_range_tree_t * rt,maptype_t maptype,uint64_t length)423b59a0cdeSMartin Matuska space_map_load_length(space_map_t *sm, zfs_range_tree_t *rt, maptype_t maptype,
424eda14cbcSMatt Macy     uint64_t length)
425eda14cbcSMatt Macy {
426eda14cbcSMatt Macy 	space_map_load_arg_t smla;
427eda14cbcSMatt Macy 
428b59a0cdeSMartin Matuska 	VERIFY0(zfs_range_tree_space(rt));
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy 	if (maptype == SM_FREE)
431b59a0cdeSMartin Matuska 		zfs_range_tree_add(rt, sm->sm_start, sm->sm_size);
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy 	smla.smla_rt = rt;
434eda14cbcSMatt Macy 	smla.smla_sm = sm;
435eda14cbcSMatt Macy 	smla.smla_type = maptype;
436eda14cbcSMatt Macy 	int err = space_map_iterate(sm, length,
437eda14cbcSMatt Macy 	    space_map_load_callback, &smla);
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy 	if (err != 0)
440b59a0cdeSMartin Matuska 		zfs_range_tree_vacate(rt, NULL, NULL);
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy 	return (err);
443eda14cbcSMatt Macy }
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy /*
446eda14cbcSMatt Macy  * Load the space map disk into the specified range tree. Segments of maptype
447eda14cbcSMatt Macy  * are added to the range tree, other segment types are removed.
448eda14cbcSMatt Macy  */
449eda14cbcSMatt Macy int
space_map_load(space_map_t * sm,zfs_range_tree_t * rt,maptype_t maptype)450b59a0cdeSMartin Matuska space_map_load(space_map_t *sm, zfs_range_tree_t *rt, maptype_t maptype)
451eda14cbcSMatt Macy {
452eda14cbcSMatt Macy 	return (space_map_load_length(sm, rt, maptype, space_map_length(sm)));
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy void
space_map_histogram_clear(space_map_t * sm)456eda14cbcSMatt Macy space_map_histogram_clear(space_map_t *sm)
457eda14cbcSMatt Macy {
458eda14cbcSMatt Macy 	if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
459eda14cbcSMatt Macy 		return;
460eda14cbcSMatt Macy 
461da5137abSMartin Matuska 	memset(sm->sm_phys->smp_histogram, 0,
462da5137abSMartin Matuska 	    sizeof (sm->sm_phys->smp_histogram));
463eda14cbcSMatt Macy }
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy boolean_t
space_map_histogram_verify(space_map_t * sm,zfs_range_tree_t * rt)466b59a0cdeSMartin Matuska space_map_histogram_verify(space_map_t *sm, zfs_range_tree_t *rt)
467eda14cbcSMatt Macy {
468eda14cbcSMatt Macy 	/*
469eda14cbcSMatt Macy 	 * Verify that the in-core range tree does not have any
470eda14cbcSMatt Macy 	 * ranges smaller than our sm_shift size.
471eda14cbcSMatt Macy 	 */
472eda14cbcSMatt Macy 	for (int i = 0; i < sm->sm_shift; i++) {
473eda14cbcSMatt Macy 		if (rt->rt_histogram[i] != 0)
474eda14cbcSMatt Macy 			return (B_FALSE);
475eda14cbcSMatt Macy 	}
476eda14cbcSMatt Macy 	return (B_TRUE);
477eda14cbcSMatt Macy }
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy void
space_map_histogram_add(space_map_t * sm,zfs_range_tree_t * rt,dmu_tx_t * tx)480b59a0cdeSMartin Matuska space_map_histogram_add(space_map_t *sm, zfs_range_tree_t *rt, dmu_tx_t *tx)
481eda14cbcSMatt Macy {
482eda14cbcSMatt Macy 	int idx = 0;
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
485eda14cbcSMatt Macy 	VERIFY3U(space_map_object(sm), !=, 0);
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy 	if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
488eda14cbcSMatt Macy 		return;
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy 	dmu_buf_will_dirty(sm->sm_dbuf, tx);
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy 	ASSERT(space_map_histogram_verify(sm, rt));
493eda14cbcSMatt Macy 	/*
494eda14cbcSMatt Macy 	 * Transfer the content of the range tree histogram to the space
495eda14cbcSMatt Macy 	 * map histogram. The space map histogram contains 32 buckets ranging
496eda14cbcSMatt Macy 	 * between 2^sm_shift to 2^(32+sm_shift-1). The range tree,
497eda14cbcSMatt Macy 	 * however, can represent ranges from 2^0 to 2^63. Since the space
498eda14cbcSMatt Macy 	 * map only cares about allocatable blocks (minimum of sm_shift) we
499eda14cbcSMatt Macy 	 * can safely ignore all ranges in the range tree smaller than sm_shift.
500eda14cbcSMatt Macy 	 */
501b59a0cdeSMartin Matuska 	for (int i = sm->sm_shift; i < ZFS_RANGE_TREE_HISTOGRAM_SIZE; i++) {
502eda14cbcSMatt Macy 
503eda14cbcSMatt Macy 		/*
504eda14cbcSMatt Macy 		 * Since the largest histogram bucket in the space map is
505eda14cbcSMatt Macy 		 * 2^(32+sm_shift-1), we need to normalize the values in
506eda14cbcSMatt Macy 		 * the range tree for any bucket larger than that size. For
507eda14cbcSMatt Macy 		 * example given an sm_shift of 9, ranges larger than 2^40
508eda14cbcSMatt Macy 		 * would get normalized as if they were 1TB ranges. Assume
509eda14cbcSMatt Macy 		 * the range tree had a count of 5 in the 2^44 (16TB) bucket,
510eda14cbcSMatt Macy 		 * the calculation below would normalize this to 5 * 2^4 (16).
511eda14cbcSMatt Macy 		 */
512eda14cbcSMatt Macy 		ASSERT3U(i, >=, idx + sm->sm_shift);
513eda14cbcSMatt Macy 		sm->sm_phys->smp_histogram[idx] +=
514eda14cbcSMatt Macy 		    rt->rt_histogram[i] << (i - idx - sm->sm_shift);
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 		/*
517eda14cbcSMatt Macy 		 * Increment the space map's index as long as we haven't
518eda14cbcSMatt Macy 		 * reached the maximum bucket size. Accumulate all ranges
519eda14cbcSMatt Macy 		 * larger than the max bucket size into the last bucket.
520eda14cbcSMatt Macy 		 */
521eda14cbcSMatt Macy 		if (idx < SPACE_MAP_HISTOGRAM_SIZE - 1) {
522eda14cbcSMatt Macy 			ASSERT3U(idx + sm->sm_shift, ==, i);
523eda14cbcSMatt Macy 			idx++;
524eda14cbcSMatt Macy 			ASSERT3U(idx, <, SPACE_MAP_HISTOGRAM_SIZE);
525eda14cbcSMatt Macy 		}
526eda14cbcSMatt Macy 	}
527eda14cbcSMatt Macy }
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy static void
space_map_write_intro_debug(space_map_t * sm,maptype_t maptype,dmu_tx_t * tx)530eda14cbcSMatt Macy space_map_write_intro_debug(space_map_t *sm, maptype_t maptype, dmu_tx_t *tx)
531eda14cbcSMatt Macy {
532eda14cbcSMatt Macy 	dmu_buf_will_dirty(sm->sm_dbuf, tx);
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 	uint64_t dentry = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) |
535eda14cbcSMatt Macy 	    SM_DEBUG_ACTION_ENCODE(maptype) |
536eda14cbcSMatt Macy 	    SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(tx->tx_pool->dp_spa)) |
537eda14cbcSMatt Macy 	    SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 	dmu_write(sm->sm_os, space_map_object(sm), sm->sm_phys->smp_length,
540eda14cbcSMatt Macy 	    sizeof (dentry), &dentry, tx);
541eda14cbcSMatt Macy 
542eda14cbcSMatt Macy 	sm->sm_phys->smp_length += sizeof (dentry);
543eda14cbcSMatt Macy }
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy /*
546eda14cbcSMatt Macy  * Writes one or more entries given a segment.
547eda14cbcSMatt Macy  *
548eda14cbcSMatt Macy  * Note: The function may release the dbuf from the pointer initially
549eda14cbcSMatt Macy  * passed to it, and return a different dbuf. Also, the space map's
550eda14cbcSMatt Macy  * dbuf must be dirty for the changes in sm_phys to take effect.
551eda14cbcSMatt Macy  */
552eda14cbcSMatt Macy static void
space_map_write_seg(space_map_t * sm,uint64_t rstart,uint64_t rend,maptype_t maptype,uint64_t vdev_id,uint8_t words,dmu_buf_t ** dbp,const void * tag,dmu_tx_t * tx)553eda14cbcSMatt Macy space_map_write_seg(space_map_t *sm, uint64_t rstart, uint64_t rend,
554eda14cbcSMatt Macy     maptype_t maptype, uint64_t vdev_id, uint8_t words, dmu_buf_t **dbp,
555a0b956f5SMartin Matuska     const void *tag, dmu_tx_t *tx)
556eda14cbcSMatt Macy {
557eda14cbcSMatt Macy 	ASSERT3U(words, !=, 0);
558eda14cbcSMatt Macy 	ASSERT3U(words, <=, 2);
559eda14cbcSMatt Macy 
560eda14cbcSMatt Macy 	/* ensure the vdev_id can be represented by the space map */
561eda14cbcSMatt Macy 	ASSERT3U(vdev_id, <=, SM_NO_VDEVID);
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy 	/*
564eda14cbcSMatt Macy 	 * if this is a single word entry, ensure that no vdev was
565eda14cbcSMatt Macy 	 * specified.
566eda14cbcSMatt Macy 	 */
567eda14cbcSMatt Macy 	IMPLY(words == 1, vdev_id == SM_NO_VDEVID);
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy 	dmu_buf_t *db = *dbp;
570eda14cbcSMatt Macy 	ASSERT3U(db->db_size, ==, sm->sm_blksz);
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 	uint64_t *block_base = db->db_data;
573eda14cbcSMatt Macy 	uint64_t *block_end = block_base + (sm->sm_blksz / sizeof (uint64_t));
574eda14cbcSMatt Macy 	uint64_t *block_cursor = block_base +
575eda14cbcSMatt Macy 	    (sm->sm_phys->smp_length - db->db_offset) / sizeof (uint64_t);
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy 	ASSERT3P(block_cursor, <=, block_end);
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	uint64_t size = (rend - rstart) >> sm->sm_shift;
580eda14cbcSMatt Macy 	uint64_t start = (rstart - sm->sm_start) >> sm->sm_shift;
581eda14cbcSMatt Macy 	uint64_t run_max = (words == 2) ? SM2_RUN_MAX : SM_RUN_MAX;
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 	ASSERT3U(rstart, >=, sm->sm_start);
584eda14cbcSMatt Macy 	ASSERT3U(rstart, <, sm->sm_start + sm->sm_size);
585eda14cbcSMatt Macy 	ASSERT3U(rend - rstart, <=, sm->sm_size);
586eda14cbcSMatt Macy 	ASSERT3U(rend, <=, sm->sm_start + sm->sm_size);
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 	while (size != 0) {
589eda14cbcSMatt Macy 		ASSERT3P(block_cursor, <=, block_end);
590eda14cbcSMatt Macy 
591eda14cbcSMatt Macy 		/*
592eda14cbcSMatt Macy 		 * If we are at the end of this block, flush it and start
593eda14cbcSMatt Macy 		 * writing again from the beginning.
594eda14cbcSMatt Macy 		 */
595eda14cbcSMatt Macy 		if (block_cursor == block_end) {
596eda14cbcSMatt Macy 			dmu_buf_rele(db, tag);
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy 			uint64_t next_word_offset = sm->sm_phys->smp_length;
599eda14cbcSMatt Macy 			VERIFY0(dmu_buf_hold(sm->sm_os,
600eda14cbcSMatt Macy 			    space_map_object(sm), next_word_offset,
601eda14cbcSMatt Macy 			    tag, &db, DMU_READ_PREFETCH));
602eda14cbcSMatt Macy 			dmu_buf_will_dirty(db, tx);
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 			/* update caller's dbuf */
605eda14cbcSMatt Macy 			*dbp = db;
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 			ASSERT3U(db->db_size, ==, sm->sm_blksz);
608eda14cbcSMatt Macy 
609eda14cbcSMatt Macy 			block_base = db->db_data;
610eda14cbcSMatt Macy 			block_cursor = block_base;
611eda14cbcSMatt Macy 			block_end = block_base +
612eda14cbcSMatt Macy 			    (db->db_size / sizeof (uint64_t));
613eda14cbcSMatt Macy 		}
614eda14cbcSMatt Macy 
615eda14cbcSMatt Macy 		/*
616eda14cbcSMatt Macy 		 * If we are writing a two-word entry and we only have one
617eda14cbcSMatt Macy 		 * word left on this block, just pad it with an empty debug
618eda14cbcSMatt Macy 		 * entry and write the two-word entry in the next block.
619eda14cbcSMatt Macy 		 */
620eda14cbcSMatt Macy 		uint64_t *next_entry = block_cursor + 1;
621eda14cbcSMatt Macy 		if (next_entry == block_end && words > 1) {
622eda14cbcSMatt Macy 			ASSERT3U(words, ==, 2);
623eda14cbcSMatt Macy 			*block_cursor = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) |
624eda14cbcSMatt Macy 			    SM_DEBUG_ACTION_ENCODE(0) |
625eda14cbcSMatt Macy 			    SM_DEBUG_SYNCPASS_ENCODE(0) |
626eda14cbcSMatt Macy 			    SM_DEBUG_TXG_ENCODE(0);
627eda14cbcSMatt Macy 			block_cursor++;
628eda14cbcSMatt Macy 			sm->sm_phys->smp_length += sizeof (uint64_t);
629eda14cbcSMatt Macy 			ASSERT3P(block_cursor, ==, block_end);
630eda14cbcSMatt Macy 			continue;
631eda14cbcSMatt Macy 		}
632eda14cbcSMatt Macy 
633eda14cbcSMatt Macy 		uint64_t run_len = MIN(size, run_max);
634eda14cbcSMatt Macy 		switch (words) {
635eda14cbcSMatt Macy 		case 1:
636eda14cbcSMatt Macy 			*block_cursor = SM_OFFSET_ENCODE(start) |
637eda14cbcSMatt Macy 			    SM_TYPE_ENCODE(maptype) |
638eda14cbcSMatt Macy 			    SM_RUN_ENCODE(run_len);
639eda14cbcSMatt Macy 			block_cursor++;
640eda14cbcSMatt Macy 			break;
641eda14cbcSMatt Macy 		case 2:
642eda14cbcSMatt Macy 			/* write the first word of the entry */
643eda14cbcSMatt Macy 			*block_cursor = SM_PREFIX_ENCODE(SM2_PREFIX) |
644eda14cbcSMatt Macy 			    SM2_RUN_ENCODE(run_len) |
645eda14cbcSMatt Macy 			    SM2_VDEV_ENCODE(vdev_id);
646eda14cbcSMatt Macy 			block_cursor++;
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy 			/* move on to the second word of the entry */
649eda14cbcSMatt Macy 			ASSERT3P(block_cursor, <, block_end);
650eda14cbcSMatt Macy 			*block_cursor = SM2_TYPE_ENCODE(maptype) |
651eda14cbcSMatt Macy 			    SM2_OFFSET_ENCODE(start);
652eda14cbcSMatt Macy 			block_cursor++;
653eda14cbcSMatt Macy 			break;
654eda14cbcSMatt Macy 		default:
655eda14cbcSMatt Macy 			panic("%d-word space map entries are not supported",
656eda14cbcSMatt Macy 			    words);
657eda14cbcSMatt Macy 			break;
658eda14cbcSMatt Macy 		}
659eda14cbcSMatt Macy 		sm->sm_phys->smp_length += words * sizeof (uint64_t);
660eda14cbcSMatt Macy 
661eda14cbcSMatt Macy 		start += run_len;
662eda14cbcSMatt Macy 		size -= run_len;
663eda14cbcSMatt Macy 	}
664eda14cbcSMatt Macy 	ASSERT0(size);
665eda14cbcSMatt Macy 
666eda14cbcSMatt Macy }
667eda14cbcSMatt Macy 
668eda14cbcSMatt Macy /*
669eda14cbcSMatt Macy  * Note: The space map's dbuf must be dirty for the changes in sm_phys to
670eda14cbcSMatt Macy  * take effect.
671eda14cbcSMatt Macy  */
672eda14cbcSMatt Macy static void
space_map_write_impl(space_map_t * sm,zfs_range_tree_t * rt,maptype_t maptype,uint64_t vdev_id,dmu_tx_t * tx)673b59a0cdeSMartin Matuska space_map_write_impl(space_map_t *sm, zfs_range_tree_t *rt, maptype_t maptype,
674eda14cbcSMatt Macy     uint64_t vdev_id, dmu_tx_t *tx)
675eda14cbcSMatt Macy {
676eda14cbcSMatt Macy 	spa_t *spa = tx->tx_pool->dp_spa;
677eda14cbcSMatt Macy 	dmu_buf_t *db;
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 	space_map_write_intro_debug(sm, maptype, tx);
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy #ifdef ZFS_DEBUG
682eda14cbcSMatt Macy 	/*
683eda14cbcSMatt Macy 	 * We do this right after we write the intro debug entry
684eda14cbcSMatt Macy 	 * because the estimate does not take it into account.
685eda14cbcSMatt Macy 	 */
686eda14cbcSMatt Macy 	uint64_t initial_objsize = sm->sm_phys->smp_length;
687eda14cbcSMatt Macy 	uint64_t estimated_growth =
688eda14cbcSMatt Macy 	    space_map_estimate_optimal_size(sm, rt, SM_NO_VDEVID);
689eda14cbcSMatt Macy 	uint64_t estimated_final_objsize = initial_objsize + estimated_growth;
690eda14cbcSMatt Macy #endif
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 	/*
693eda14cbcSMatt Macy 	 * Find the offset right after the last word in the space map
694eda14cbcSMatt Macy 	 * and use that to get a hold of the last block, so we can
695eda14cbcSMatt Macy 	 * start appending to it.
696eda14cbcSMatt Macy 	 */
697eda14cbcSMatt Macy 	uint64_t next_word_offset = sm->sm_phys->smp_length;
698eda14cbcSMatt Macy 	VERIFY0(dmu_buf_hold(sm->sm_os, space_map_object(sm),
699eda14cbcSMatt Macy 	    next_word_offset, FTAG, &db, DMU_READ_PREFETCH));
700eda14cbcSMatt Macy 	ASSERT3U(db->db_size, ==, sm->sm_blksz);
701eda14cbcSMatt Macy 
702eda14cbcSMatt Macy 	dmu_buf_will_dirty(db, tx);
703eda14cbcSMatt Macy 
704eda14cbcSMatt Macy 	zfs_btree_t *t = &rt->rt_root;
705eda14cbcSMatt Macy 	zfs_btree_index_t where;
706b59a0cdeSMartin Matuska 	for (zfs_range_seg_t *rs = zfs_btree_first(t, &where); rs != NULL;
707eda14cbcSMatt Macy 	    rs = zfs_btree_next(t, &where, &where)) {
708b59a0cdeSMartin Matuska 		uint64_t offset = (zfs_rs_get_start(rs, rt) - sm->sm_start) >>
709eda14cbcSMatt Macy 		    sm->sm_shift;
710b59a0cdeSMartin Matuska 		uint64_t length = (zfs_rs_get_end(rs, rt) -
711b59a0cdeSMartin Matuska 		    zfs_rs_get_start(rs, rt)) >> sm->sm_shift;
712eda14cbcSMatt Macy 		uint8_t words = 1;
713eda14cbcSMatt Macy 
714eda14cbcSMatt Macy 		/*
715eda14cbcSMatt Macy 		 * We only write two-word entries when both of the following
716eda14cbcSMatt Macy 		 * are true:
717eda14cbcSMatt Macy 		 *
718eda14cbcSMatt Macy 		 * [1] The feature is enabled.
719eda14cbcSMatt Macy 		 * [2] The offset or run is too big for a single-word entry,
720eda14cbcSMatt Macy 		 *	or the vdev_id is set (meaning not equal to
721eda14cbcSMatt Macy 		 *	SM_NO_VDEVID).
722eda14cbcSMatt Macy 		 *
723eda14cbcSMatt Macy 		 * Note that for purposes of testing we've added the case that
724eda14cbcSMatt Macy 		 * we write two-word entries occasionally when the feature is
725eda14cbcSMatt Macy 		 * enabled and zfs_force_some_double_word_sm_entries has been
726eda14cbcSMatt Macy 		 * set.
727eda14cbcSMatt Macy 		 */
728eda14cbcSMatt Macy 		if (spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_V2) &&
729eda14cbcSMatt Macy 		    (offset >= (1ULL << SM_OFFSET_BITS) ||
730eda14cbcSMatt Macy 		    length > SM_RUN_MAX ||
731eda14cbcSMatt Macy 		    vdev_id != SM_NO_VDEVID ||
732eda14cbcSMatt Macy 		    (zfs_force_some_double_word_sm_entries &&
73333b8c039SMartin Matuska 		    random_in_range(100) == 0)))
734eda14cbcSMatt Macy 			words = 2;
735eda14cbcSMatt Macy 
736b59a0cdeSMartin Matuska 		space_map_write_seg(sm, zfs_rs_get_start(rs, rt),
737b59a0cdeSMartin Matuska 		    zfs_rs_get_end(rs, rt), maptype, vdev_id, words, &db,
738b59a0cdeSMartin Matuska 		    FTAG, tx);
739eda14cbcSMatt Macy 	}
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy 	dmu_buf_rele(db, FTAG);
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy #ifdef ZFS_DEBUG
744eda14cbcSMatt Macy 	/*
745eda14cbcSMatt Macy 	 * We expect our estimation to be based on the worst case
746eda14cbcSMatt Macy 	 * scenario [see comment in space_map_estimate_optimal_size()].
747eda14cbcSMatt Macy 	 * Therefore we expect the actual objsize to be equal or less
748eda14cbcSMatt Macy 	 * than whatever we estimated it to be.
749eda14cbcSMatt Macy 	 */
750eda14cbcSMatt Macy 	ASSERT3U(estimated_final_objsize, >=, sm->sm_phys->smp_length);
751eda14cbcSMatt Macy #endif
752eda14cbcSMatt Macy }
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy /*
755eda14cbcSMatt Macy  * Note: This function manipulates the state of the given space map but
756eda14cbcSMatt Macy  * does not hold any locks implicitly. Thus the caller is responsible
757eda14cbcSMatt Macy  * for synchronizing writes to the space map.
758eda14cbcSMatt Macy  */
759eda14cbcSMatt Macy void
space_map_write(space_map_t * sm,zfs_range_tree_t * rt,maptype_t maptype,uint64_t vdev_id,dmu_tx_t * tx)760b59a0cdeSMartin Matuska space_map_write(space_map_t *sm, zfs_range_tree_t *rt, maptype_t maptype,
761eda14cbcSMatt Macy     uint64_t vdev_id, dmu_tx_t *tx)
762eda14cbcSMatt Macy {
763eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(dmu_objset_pool(sm->sm_os)));
764eda14cbcSMatt Macy 	VERIFY3U(space_map_object(sm), !=, 0);
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 	dmu_buf_will_dirty(sm->sm_dbuf, tx);
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy 	/*
769eda14cbcSMatt Macy 	 * This field is no longer necessary since the in-core space map
770eda14cbcSMatt Macy 	 * now contains the object number but is maintained for backwards
771eda14cbcSMatt Macy 	 * compatibility.
772eda14cbcSMatt Macy 	 */
773eda14cbcSMatt Macy 	sm->sm_phys->smp_object = sm->sm_object;
774eda14cbcSMatt Macy 
775b59a0cdeSMartin Matuska 	if (zfs_range_tree_is_empty(rt)) {
776eda14cbcSMatt Macy 		VERIFY3U(sm->sm_object, ==, sm->sm_phys->smp_object);
777eda14cbcSMatt Macy 		return;
778eda14cbcSMatt Macy 	}
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 	if (maptype == SM_ALLOC)
781b59a0cdeSMartin Matuska 		sm->sm_phys->smp_alloc += zfs_range_tree_space(rt);
782eda14cbcSMatt Macy 	else
783b59a0cdeSMartin Matuska 		sm->sm_phys->smp_alloc -= zfs_range_tree_space(rt);
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 	uint64_t nodes = zfs_btree_numnodes(&rt->rt_root);
786b59a0cdeSMartin Matuska 	uint64_t rt_space = zfs_range_tree_space(rt);
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 	space_map_write_impl(sm, rt, maptype, vdev_id, tx);
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 	/*
791eda14cbcSMatt Macy 	 * Ensure that the space_map's accounting wasn't changed
792eda14cbcSMatt Macy 	 * while we were in the middle of writing it out.
793eda14cbcSMatt Macy 	 */
794eda14cbcSMatt Macy 	VERIFY3U(nodes, ==, zfs_btree_numnodes(&rt->rt_root));
795b59a0cdeSMartin Matuska 	VERIFY3U(zfs_range_tree_space(rt), ==, rt_space);
796eda14cbcSMatt Macy }
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy static int
space_map_open_impl(space_map_t * sm)799eda14cbcSMatt Macy space_map_open_impl(space_map_t *sm)
800eda14cbcSMatt Macy {
801eda14cbcSMatt Macy 	int error;
802eda14cbcSMatt Macy 	u_longlong_t blocks;
803eda14cbcSMatt Macy 
804eda14cbcSMatt Macy 	error = dmu_bonus_hold(sm->sm_os, sm->sm_object, sm, &sm->sm_dbuf);
805eda14cbcSMatt Macy 	if (error)
806eda14cbcSMatt Macy 		return (error);
807eda14cbcSMatt Macy 
808eda14cbcSMatt Macy 	dmu_object_size_from_db(sm->sm_dbuf, &sm->sm_blksz, &blocks);
809eda14cbcSMatt Macy 	sm->sm_phys = sm->sm_dbuf->db_data;
810eda14cbcSMatt Macy 	return (0);
811eda14cbcSMatt Macy }
812eda14cbcSMatt Macy 
813eda14cbcSMatt Macy int
space_map_open(space_map_t ** smp,objset_t * os,uint64_t object,uint64_t start,uint64_t size,uint8_t shift)814eda14cbcSMatt Macy space_map_open(space_map_t **smp, objset_t *os, uint64_t object,
815eda14cbcSMatt Macy     uint64_t start, uint64_t size, uint8_t shift)
816eda14cbcSMatt Macy {
817eda14cbcSMatt Macy 	space_map_t *sm;
818eda14cbcSMatt Macy 	int error;
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	ASSERT(*smp == NULL);
821eda14cbcSMatt Macy 	ASSERT(os != NULL);
822eda14cbcSMatt Macy 	ASSERT(object != 0);
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy 	sm = kmem_alloc(sizeof (space_map_t), KM_SLEEP);
825eda14cbcSMatt Macy 
826eda14cbcSMatt Macy 	sm->sm_start = start;
827eda14cbcSMatt Macy 	sm->sm_size = size;
828eda14cbcSMatt Macy 	sm->sm_shift = shift;
829eda14cbcSMatt Macy 	sm->sm_os = os;
830eda14cbcSMatt Macy 	sm->sm_object = object;
831eda14cbcSMatt Macy 	sm->sm_blksz = 0;
832eda14cbcSMatt Macy 	sm->sm_dbuf = NULL;
833eda14cbcSMatt Macy 	sm->sm_phys = NULL;
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy 	error = space_map_open_impl(sm);
836eda14cbcSMatt Macy 	if (error != 0) {
837eda14cbcSMatt Macy 		space_map_close(sm);
838eda14cbcSMatt Macy 		return (error);
839eda14cbcSMatt Macy 	}
840eda14cbcSMatt Macy 	*smp = sm;
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy 	return (0);
843eda14cbcSMatt Macy }
844eda14cbcSMatt Macy 
845eda14cbcSMatt Macy void
space_map_close(space_map_t * sm)846eda14cbcSMatt Macy space_map_close(space_map_t *sm)
847eda14cbcSMatt Macy {
848eda14cbcSMatt Macy 	if (sm == NULL)
849eda14cbcSMatt Macy 		return;
850eda14cbcSMatt Macy 
851eda14cbcSMatt Macy 	if (sm->sm_dbuf != NULL)
852eda14cbcSMatt Macy 		dmu_buf_rele(sm->sm_dbuf, sm);
853eda14cbcSMatt Macy 	sm->sm_dbuf = NULL;
854eda14cbcSMatt Macy 	sm->sm_phys = NULL;
855eda14cbcSMatt Macy 
856eda14cbcSMatt Macy 	kmem_free(sm, sizeof (*sm));
857eda14cbcSMatt Macy }
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy void
space_map_truncate(space_map_t * sm,int blocksize,dmu_tx_t * tx)860eda14cbcSMatt Macy space_map_truncate(space_map_t *sm, int blocksize, dmu_tx_t *tx)
861eda14cbcSMatt Macy {
862eda14cbcSMatt Macy 	objset_t *os = sm->sm_os;
863eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(os);
864eda14cbcSMatt Macy 	dmu_object_info_t doi;
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
867eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
868eda14cbcSMatt Macy 	VERIFY3U(dmu_tx_get_txg(tx), <=, spa_final_dirty_txg(spa));
869eda14cbcSMatt Macy 
870eda14cbcSMatt Macy 	dmu_object_info_from_db(sm->sm_dbuf, &doi);
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy 	/*
873eda14cbcSMatt Macy 	 * If the space map has the wrong bonus size (because
874eda14cbcSMatt Macy 	 * SPA_FEATURE_SPACEMAP_HISTOGRAM has recently been enabled), or
875eda14cbcSMatt Macy 	 * the wrong block size (because space_map_blksz has changed),
876eda14cbcSMatt Macy 	 * free and re-allocate its object with the updated sizes.
877eda14cbcSMatt Macy 	 *
878eda14cbcSMatt Macy 	 * Otherwise, just truncate the current object.
879eda14cbcSMatt Macy 	 */
880eda14cbcSMatt Macy 	if ((spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) &&
881eda14cbcSMatt Macy 	    doi.doi_bonus_size != sizeof (space_map_phys_t)) ||
882eda14cbcSMatt Macy 	    doi.doi_data_block_size != blocksize ||
883eda14cbcSMatt Macy 	    doi.doi_metadata_block_size != 1 << space_map_ibs) {
884eda14cbcSMatt Macy 		zfs_dbgmsg("txg %llu, spa %s, sm %px, reallocating "
88533b8c039SMartin Matuska 		    "object[%llu]: old bonus %llu, old blocksz %u",
88633b8c039SMartin Matuska 		    (u_longlong_t)dmu_tx_get_txg(tx), spa_name(spa), sm,
88733b8c039SMartin Matuska 		    (u_longlong_t)sm->sm_object,
88833b8c039SMartin Matuska 		    (u_longlong_t)doi.doi_bonus_size,
88933b8c039SMartin Matuska 		    doi.doi_data_block_size);
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy 		space_map_free(sm, tx);
892eda14cbcSMatt Macy 		dmu_buf_rele(sm->sm_dbuf, sm);
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 		sm->sm_object = space_map_alloc(sm->sm_os, blocksize, tx);
895eda14cbcSMatt Macy 		VERIFY0(space_map_open_impl(sm));
896eda14cbcSMatt Macy 	} else {
897eda14cbcSMatt Macy 		VERIFY0(dmu_free_range(os, space_map_object(sm), 0, -1ULL, tx));
898eda14cbcSMatt Macy 
899eda14cbcSMatt Macy 		/*
900eda14cbcSMatt Macy 		 * If the spacemap is reallocated, its histogram
901eda14cbcSMatt Macy 		 * will be reset.  Do the same in the common case so that
902eda14cbcSMatt Macy 		 * bugs related to the uncommon case do not go unnoticed.
903eda14cbcSMatt Macy 		 */
904da5137abSMartin Matuska 		memset(sm->sm_phys->smp_histogram, 0,
905eda14cbcSMatt Macy 		    sizeof (sm->sm_phys->smp_histogram));
906eda14cbcSMatt Macy 	}
907eda14cbcSMatt Macy 
908eda14cbcSMatt Macy 	dmu_buf_will_dirty(sm->sm_dbuf, tx);
909eda14cbcSMatt Macy 	sm->sm_phys->smp_length = 0;
910eda14cbcSMatt Macy 	sm->sm_phys->smp_alloc = 0;
911eda14cbcSMatt Macy }
912eda14cbcSMatt Macy 
913eda14cbcSMatt Macy uint64_t
space_map_alloc(objset_t * os,int blocksize,dmu_tx_t * tx)914eda14cbcSMatt Macy space_map_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
915eda14cbcSMatt Macy {
916eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(os);
917eda14cbcSMatt Macy 	uint64_t object;
918eda14cbcSMatt Macy 	int bonuslen;
919eda14cbcSMatt Macy 
920eda14cbcSMatt Macy 	if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
921eda14cbcSMatt Macy 		spa_feature_incr(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
922eda14cbcSMatt Macy 		bonuslen = sizeof (space_map_phys_t);
923eda14cbcSMatt Macy 		ASSERT3U(bonuslen, <=, dmu_bonus_max());
924eda14cbcSMatt Macy 	} else {
925eda14cbcSMatt Macy 		bonuslen = SPACE_MAP_SIZE_V0;
926eda14cbcSMatt Macy 	}
927eda14cbcSMatt Macy 
928eda14cbcSMatt Macy 	object = dmu_object_alloc_ibs(os, DMU_OT_SPACE_MAP, blocksize,
929eda14cbcSMatt Macy 	    space_map_ibs, DMU_OT_SPACE_MAP_HEADER, bonuslen, tx);
930eda14cbcSMatt Macy 
931eda14cbcSMatt Macy 	return (object);
932eda14cbcSMatt Macy }
933eda14cbcSMatt Macy 
934eda14cbcSMatt Macy void
space_map_free_obj(objset_t * os,uint64_t smobj,dmu_tx_t * tx)935eda14cbcSMatt Macy space_map_free_obj(objset_t *os, uint64_t smobj, dmu_tx_t *tx)
936eda14cbcSMatt Macy {
937eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(os);
938eda14cbcSMatt Macy 	if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
939eda14cbcSMatt Macy 		dmu_object_info_t doi;
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 		VERIFY0(dmu_object_info(os, smobj, &doi));
942eda14cbcSMatt Macy 		if (doi.doi_bonus_size != SPACE_MAP_SIZE_V0) {
943eda14cbcSMatt Macy 			spa_feature_decr(spa,
944eda14cbcSMatt Macy 			    SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
945eda14cbcSMatt Macy 		}
946eda14cbcSMatt Macy 	}
947eda14cbcSMatt Macy 
948eda14cbcSMatt Macy 	VERIFY0(dmu_object_free(os, smobj, tx));
949eda14cbcSMatt Macy }
950eda14cbcSMatt Macy 
951eda14cbcSMatt Macy void
space_map_free(space_map_t * sm,dmu_tx_t * tx)952eda14cbcSMatt Macy space_map_free(space_map_t *sm, dmu_tx_t *tx)
953eda14cbcSMatt Macy {
954eda14cbcSMatt Macy 	if (sm == NULL)
955eda14cbcSMatt Macy 		return;
956eda14cbcSMatt Macy 
957eda14cbcSMatt Macy 	space_map_free_obj(sm->sm_os, space_map_object(sm), tx);
958eda14cbcSMatt Macy 	sm->sm_object = 0;
959eda14cbcSMatt Macy }
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy /*
962eda14cbcSMatt Macy  * Given a range tree, it makes a worst-case estimate of how much
963eda14cbcSMatt Macy  * space would the tree's segments take if they were written to
964eda14cbcSMatt Macy  * the given space map.
965eda14cbcSMatt Macy  */
966eda14cbcSMatt Macy uint64_t
space_map_estimate_optimal_size(space_map_t * sm,zfs_range_tree_t * rt,uint64_t vdev_id)967b59a0cdeSMartin Matuska space_map_estimate_optimal_size(space_map_t *sm, zfs_range_tree_t *rt,
968eda14cbcSMatt Macy     uint64_t vdev_id)
969eda14cbcSMatt Macy {
970eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(sm->sm_os);
971eda14cbcSMatt Macy 	uint64_t shift = sm->sm_shift;
972eda14cbcSMatt Macy 	uint64_t *histogram = rt->rt_histogram;
973eda14cbcSMatt Macy 	uint64_t entries_for_seg = 0;
974eda14cbcSMatt Macy 
975eda14cbcSMatt Macy 	/*
976eda14cbcSMatt Macy 	 * In order to get a quick estimate of the optimal size that this
977eda14cbcSMatt Macy 	 * range tree would have on-disk as a space map, we iterate through
978eda14cbcSMatt Macy 	 * its histogram buckets instead of iterating through its nodes.
979eda14cbcSMatt Macy 	 *
980eda14cbcSMatt Macy 	 * Note that this is a highest-bound/worst-case estimate for the
981eda14cbcSMatt Macy 	 * following reasons:
982eda14cbcSMatt Macy 	 *
983eda14cbcSMatt Macy 	 * 1] We assume that we always add a debug padding for each block
984eda14cbcSMatt Macy 	 *    we write and we also assume that we start at the last word
985eda14cbcSMatt Macy 	 *    of a block attempting to write a two-word entry.
986eda14cbcSMatt Macy 	 * 2] Rounding up errors due to the way segments are distributed
987eda14cbcSMatt Macy 	 *    in the buckets of the range tree's histogram.
988eda14cbcSMatt Macy 	 * 3] The activation of zfs_force_some_double_word_sm_entries
989eda14cbcSMatt Macy 	 *    (tunable) when testing.
990eda14cbcSMatt Macy 	 *
991eda14cbcSMatt Macy 	 * = Math and Rounding Errors =
992eda14cbcSMatt Macy 	 *
993eda14cbcSMatt Macy 	 * rt_histogram[i] bucket of a range tree represents the number
994eda14cbcSMatt Macy 	 * of entries in [2^i, (2^(i+1))-1] of that range_tree. Given
995eda14cbcSMatt Macy 	 * that, we want to divide the buckets into groups: Buckets that
996eda14cbcSMatt Macy 	 * can be represented using a single-word entry, ones that can
997eda14cbcSMatt Macy 	 * be represented with a double-word entry, and ones that can
998eda14cbcSMatt Macy 	 * only be represented with multiple two-word entries.
999eda14cbcSMatt Macy 	 *
1000eda14cbcSMatt Macy 	 * [Note that if the new encoding feature is not enabled there
1001eda14cbcSMatt Macy 	 * are only two groups: single-word entry buckets and multiple
1002eda14cbcSMatt Macy 	 * single-word entry buckets. The information below assumes
1003eda14cbcSMatt Macy 	 * two-word entries enabled, but it can easily applied when
1004eda14cbcSMatt Macy 	 * the feature is not enabled]
1005eda14cbcSMatt Macy 	 *
1006eda14cbcSMatt Macy 	 * To find the highest bucket that can be represented with a
1007eda14cbcSMatt Macy 	 * single-word entry we look at the maximum run that such entry
1008eda14cbcSMatt Macy 	 * can have, which is 2^(SM_RUN_BITS + sm_shift) [remember that
1009eda14cbcSMatt Macy 	 * the run of a space map entry is shifted by sm_shift, thus we
1010eda14cbcSMatt Macy 	 * add it to the exponent]. This way, excluding the value of the
1011eda14cbcSMatt Macy 	 * maximum run that can be represented by a single-word entry,
1012eda14cbcSMatt Macy 	 * all runs that are smaller exist in buckets 0 to
1013eda14cbcSMatt Macy 	 * SM_RUN_BITS + shift - 1.
1014eda14cbcSMatt Macy 	 *
1015eda14cbcSMatt Macy 	 * To find the highest bucket that can be represented with a
1016eda14cbcSMatt Macy 	 * double-word entry, we follow the same approach. Finally, any
1017eda14cbcSMatt Macy 	 * bucket higher than that are represented with multiple two-word
1018eda14cbcSMatt Macy 	 * entries. To be more specific, if the highest bucket whose
1019eda14cbcSMatt Macy 	 * segments can be represented with a single two-word entry is X,
1020eda14cbcSMatt Macy 	 * then bucket X+1 will need 2 two-word entries for each of its
1021eda14cbcSMatt Macy 	 * segments, X+2 will need 4, X+3 will need 8, ...etc.
1022eda14cbcSMatt Macy 	 *
1023eda14cbcSMatt Macy 	 * With all of the above we make our estimation based on bucket
1024eda14cbcSMatt Macy 	 * groups. There is a rounding error though. As we mentioned in
1025eda14cbcSMatt Macy 	 * the example with the one-word entry, the maximum run that can
1026eda14cbcSMatt Macy 	 * be represented in a one-word entry 2^(SM_RUN_BITS + shift) is
1027eda14cbcSMatt Macy 	 * not part of bucket SM_RUN_BITS + shift - 1. Thus, segments of
1028eda14cbcSMatt Macy 	 * that length fall into the next bucket (and bucket group) where
1029eda14cbcSMatt Macy 	 * we start counting two-word entries and this is one more reason
1030eda14cbcSMatt Macy 	 * why the estimated size may end up being bigger than the actual
1031eda14cbcSMatt Macy 	 * size written.
1032eda14cbcSMatt Macy 	 */
1033eda14cbcSMatt Macy 	uint64_t size = 0;
1034eda14cbcSMatt Macy 	uint64_t idx = 0;
1035eda14cbcSMatt Macy 
1036eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) ||
1037eda14cbcSMatt Macy 	    (vdev_id == SM_NO_VDEVID && sm->sm_size < SM_OFFSET_MAX)) {
1038eda14cbcSMatt Macy 
1039eda14cbcSMatt Macy 		/*
1040eda14cbcSMatt Macy 		 * If we are trying to force some double word entries just
1041eda14cbcSMatt Macy 		 * assume the worst-case of every single word entry being
1042eda14cbcSMatt Macy 		 * written as a double word entry.
1043eda14cbcSMatt Macy 		 */
1044eda14cbcSMatt Macy 		uint64_t entry_size =
1045eda14cbcSMatt Macy 		    (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) &&
1046eda14cbcSMatt Macy 		    zfs_force_some_double_word_sm_entries) ?
1047eda14cbcSMatt Macy 		    (2 * sizeof (uint64_t)) : sizeof (uint64_t);
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy 		uint64_t single_entry_max_bucket = SM_RUN_BITS + shift - 1;
1050eda14cbcSMatt Macy 		for (; idx <= single_entry_max_bucket; idx++)
1051eda14cbcSMatt Macy 			size += histogram[idx] * entry_size;
1052eda14cbcSMatt Macy 
1053eda14cbcSMatt Macy 		if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2)) {
1054b59a0cdeSMartin Matuska 			for (; idx < ZFS_RANGE_TREE_HISTOGRAM_SIZE; idx++) {
1055eda14cbcSMatt Macy 				ASSERT3U(idx, >=, single_entry_max_bucket);
1056eda14cbcSMatt Macy 				entries_for_seg =
1057eda14cbcSMatt Macy 				    1ULL << (idx - single_entry_max_bucket);
1058eda14cbcSMatt Macy 				size += histogram[idx] *
1059eda14cbcSMatt Macy 				    entries_for_seg * entry_size;
1060eda14cbcSMatt Macy 			}
1061eda14cbcSMatt Macy 			return (size);
1062eda14cbcSMatt Macy 		}
1063eda14cbcSMatt Macy 	}
1064eda14cbcSMatt Macy 
1065eda14cbcSMatt Macy 	ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2));
1066eda14cbcSMatt Macy 
1067eda14cbcSMatt Macy 	uint64_t double_entry_max_bucket = SM2_RUN_BITS + shift - 1;
1068eda14cbcSMatt Macy 	for (; idx <= double_entry_max_bucket; idx++)
1069eda14cbcSMatt Macy 		size += histogram[idx] * 2 * sizeof (uint64_t);
1070eda14cbcSMatt Macy 
1071b59a0cdeSMartin Matuska 	for (; idx < ZFS_RANGE_TREE_HISTOGRAM_SIZE; idx++) {
1072eda14cbcSMatt Macy 		ASSERT3U(idx, >=, double_entry_max_bucket);
1073eda14cbcSMatt Macy 		entries_for_seg = 1ULL << (idx - double_entry_max_bucket);
1074eda14cbcSMatt Macy 		size += histogram[idx] *
1075eda14cbcSMatt Macy 		    entries_for_seg * 2 * sizeof (uint64_t);
1076eda14cbcSMatt Macy 	}
1077eda14cbcSMatt Macy 
1078eda14cbcSMatt Macy 	/*
1079eda14cbcSMatt Macy 	 * Assume the worst case where we start with the padding at the end
1080eda14cbcSMatt Macy 	 * of the current block and we add an extra padding entry at the end
1081eda14cbcSMatt Macy 	 * of all subsequent blocks.
1082eda14cbcSMatt Macy 	 */
1083eda14cbcSMatt Macy 	size += ((size / sm->sm_blksz) + 1) * sizeof (uint64_t);
1084eda14cbcSMatt Macy 
1085eda14cbcSMatt Macy 	return (size);
1086eda14cbcSMatt Macy }
1087eda14cbcSMatt Macy 
1088eda14cbcSMatt Macy uint64_t
space_map_object(space_map_t * sm)1089eda14cbcSMatt Macy space_map_object(space_map_t *sm)
1090eda14cbcSMatt Macy {
1091eda14cbcSMatt Macy 	return (sm != NULL ? sm->sm_object : 0);
1092eda14cbcSMatt Macy }
1093eda14cbcSMatt Macy 
1094eda14cbcSMatt Macy int64_t
space_map_allocated(space_map_t * sm)1095eda14cbcSMatt Macy space_map_allocated(space_map_t *sm)
1096eda14cbcSMatt Macy {
1097eda14cbcSMatt Macy 	return (sm != NULL ? sm->sm_phys->smp_alloc : 0);
1098eda14cbcSMatt Macy }
1099eda14cbcSMatt Macy 
1100eda14cbcSMatt Macy uint64_t
space_map_length(space_map_t * sm)1101eda14cbcSMatt Macy space_map_length(space_map_t *sm)
1102eda14cbcSMatt Macy {
1103eda14cbcSMatt Macy 	return (sm != NULL ? sm->sm_phys->smp_length : 0);
1104eda14cbcSMatt Macy }
1105eda14cbcSMatt Macy 
1106eda14cbcSMatt Macy uint64_t
space_map_nblocks(space_map_t * sm)1107eda14cbcSMatt Macy space_map_nblocks(space_map_t *sm)
1108eda14cbcSMatt Macy {
1109eda14cbcSMatt Macy 	if (sm == NULL)
1110eda14cbcSMatt Macy 		return (0);
1111eda14cbcSMatt Macy 	return (DIV_ROUND_UP(space_map_length(sm), sm->sm_blksz));
1112eda14cbcSMatt Macy }
1113