xref: /freebsd/sys/contrib/openzfs/module/zfs/spa_log_spacemap.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2018, 2019 by Delphix. All rights reserved.
15  * Copyright (c) 2024-2026, Klara, Inc.
16  * Copyright (c) 2026, TrueNAS.
17  */
18 
19 #include <sys/dmu_objset.h>
20 #include <sys/metaslab.h>
21 #include <sys/metaslab_impl.h>
22 #include <sys/spa.h>
23 #include <sys/spa_impl.h>
24 #include <sys/spa_log_spacemap.h>
25 #include <sys/vdev_impl.h>
26 #include <sys/zap.h>
27 
28 /*
29  * Log Space Maps
30  *
31  * Log space maps are an optimization in ZFS metadata allocations for pools
32  * whose workloads are primarily random-writes. Random-write workloads are also
33  * typically random-free, meaning that they are freeing from locations scattered
34  * throughout the pool. This means that each TXG we will have to append some
35  * FREE records to almost every metaslab. With log space maps, we hold their
36  * changes in memory and log them altogether in one pool-wide space map on-disk
37  * for persistence. As more blocks are accumulated in the log space maps and
38  * more unflushed changes are accounted in memory, we flush a selected group
39  * of metaslabs every TXG to relieve memory pressure and potential overheads
40  * when loading the pool. Flushing a metaslab to disk relieves memory as we
41  * flush any unflushed changes from memory to disk (i.e. the metaslab's space
42  * map) and saves import time by making old log space maps obsolete and
43  * eventually destroying them. [A log space map is said to be obsolete when all
44  * its entries have made it to their corresponding metaslab space maps].
45  *
46  * == On disk data structures used ==
47  *
48  * - The pool has a new feature flag and a new entry in the MOS. The feature
49  *   is activated when we create the first log space map and remains active
50  *   for the lifetime of the pool. The new entry in the MOS Directory [refer
51  *   to DMU_POOL_LOG_SPACEMAP_ZAP] is populated with a ZAP whose key-value
52  *   pairs are of the form <key: txg, value: log space map object for that txg>.
53  *   This entry is our on-disk reference of the log space maps that exist in
54  *   the pool for each TXG and it is used during import to load all the
55  *   metaslab unflushed changes in memory. To see how this structure is first
56  *   created and later populated refer to spa_generate_syncing_log_sm(). To see
57  *   how it is used during import time refer to spa_ld_log_sm_metadata().
58  *
59  * - Each vdev has a new entry in its vdev_top_zap (see field
60  *   VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS) which holds the msp_unflushed_txg of
61  *   each metaslab in this vdev. This field is the on-disk counterpart of the
62  *   in-memory field ms_unflushed_txg which tells us from which TXG and onwards
63  *   the metaslab haven't had its changes flushed. During import, we use this
64  *   to ignore any entries in the space map log that are for this metaslab but
65  *   from a TXG before msp_unflushed_txg. At that point, we also populate its
66  *   in-memory counterpart and from there both fields are updated every time
67  *   we flush that metaslab.
68  *
69  * - A space map is created every TXG and, during that TXG, it is used to log
70  *   all incoming changes (the log space map). When created, the log space map
71  *   is referenced in memory by spa_syncing_log_sm and its object ID is inserted
72  *   to the space map ZAP mentioned above. The log space map is closed at the
73  *   end of the TXG and will be destroyed when it becomes fully obsolete. We
74  *   know when a log space map has become obsolete by looking at the oldest
75  *   (and smallest) ms_unflushed_txg in the pool. If the value of that is bigger
76  *   than the log space map's TXG, then it means that there is no metaslab who
77  *   doesn't have the changes from that log and we can therefore destroy it.
78  *   [see spa_cleanup_old_sm_logs()].
79  *
80  * == Important in-memory structures ==
81  *
82  * - The per-spa field spa_metaslabs_by_flushed sorts all the metaslabs in
83  *   the pool by their ms_unflushed_txg field. It is primarily used for three
84  *   reasons. First of all, it is used during flushing where we try to flush
85  *   metaslabs in-order from the oldest-flushed to the most recently flushed
86  *   every TXG. Secondly, it helps us to lookup the ms_unflushed_txg of the
87  *   oldest flushed metaslab to distinguish which log space maps have become
88  *   obsolete and which ones are still relevant. Finally it tells us which
89  *   metaslabs have unflushed changes in a pool where this feature was just
90  *   enabled, as we don't immediately add all of the pool's metaslabs but we
91  *   add them over time as they go through metaslab_sync(). The reason that
92  *   we do that is to ease these pools into the behavior of the flushing
93  *   algorithm (described later on).
94  *
95  * - The per-spa field spa_sm_logs_by_txg can be thought as the in-memory
96  *   counterpart of the space map ZAP mentioned above. It's an AVL tree whose
97  *   nodes represent the log space maps in the pool. This in-memory
98  *   representation of log space maps in the pool sorts the log space maps by
99  *   the TXG that they were created (which is also the TXG of their unflushed
100  *   changes). It also contains the following extra information for each
101  *   space map:
102  *   [1] The number of metaslabs that were last flushed on that TXG. This is
103  *       important because if that counter is zero and this is the oldest
104  *       log then it means that it is also obsolete.
105  *   [2] The number of blocks of that space map. This field is used by the
106  *       block heuristic of our flushing algorithm (described later on).
107  *       It represents how many blocks of metadata changes ZFS had to write
108  *       to disk for that TXG.
109  *
110  * - The per-spa field spa_log_summary is a list of entries that summarizes
111  *   the metaslab and block counts of all the nodes of the spa_sm_logs_by_txg
112  *   AVL tree mentioned above. The reason this exists is that our flushing
113  *   algorithm (described later) tries to estimate how many metaslabs to flush
114  *   in each TXG by iterating over all the log space maps and looking at their
115  *   block counts. Summarizing that information means that don't have to
116  *   iterate through each space map, minimizing the runtime overhead of the
117  *   flushing algorithm which would be induced in syncing context. In terms of
118  *   implementation the log summary is used as a queue:
119  *   * we modify or pop entries from its head when we flush metaslabs
120  *   * we modify or append entries to its tail when we sync changes.
121  *
122  * - Each metaslab has two new range trees that hold its unflushed changes,
123  *   ms_unflushed_allocs and ms_unflushed_frees. These are always disjoint.
124  *
125  * == Flushing algorithm ==
126  *
127  * The decision of how many metaslabs to flush on a give TXG is guided by
128  * two heuristics:
129  *
130  * [1] The memory heuristic -
131  * We keep track of the memory used by the unflushed trees from all the
132  * metaslabs [see sus_memused of spa_unflushed_stats] and we ensure that it
133  * stays below a certain threshold which is determined by an arbitrary hard
134  * limit and an arbitrary percentage of the system's memory [see
135  * spa_log_exceeds_memlimit()]. When we see that the memory usage of the
136  * unflushed changes are passing that threshold, we flush metaslabs, which
137  * empties their unflushed range trees, reducing the memory used.
138  *
139  * [2] The block heuristic -
140  * We try to keep the total number of blocks in the log space maps in check
141  * so the log doesn't grow indefinitely and we don't induce a lot of overhead
142  * when loading the pool. At the same time we don't want to flush a lot of
143  * metaslabs too often as this would defeat the purpose of the log space map.
144  * As a result we set a limit in the amount of blocks that we think it's
145  * acceptable for the log space maps to have and try not to cross it.
146  * [see sus_blocklimit from spa_unflushed_stats].
147  *
148  * In order to stay below the block limit every TXG we have to estimate how
149  * many metaslabs we need to flush based on the current rate of incoming blocks
150  * and our history of log space map blocks. The main idea here is to answer
151  * the question of how many metaslabs do we need to flush in order to get rid
152  * at least an X amount of log space map blocks. We can answer this question
153  * by iterating backwards from the oldest log space map to the newest one
154  * and looking at their metaslab and block counts. At this point the log summary
155  * mentioned above comes handy as it reduces the amount of things that we have
156  * to iterate (even though it may reduce the preciseness of our estimates due
157  * to its aggregation of data). So with that in mind, we project the incoming
158  * rate of the current TXG into the future and attempt to approximate how many
159  * metaslabs would we need to flush from now in order to avoid exceeding our
160  * block limit in different points in the future (granted that we would keep
161  * flushing the same number of metaslabs for every TXG). Then we take the
162  * maximum number from all these estimates to be on the safe side. For the
163  * exact implementation details of algorithm refer to
164  * spa_estimate_metaslabs_to_flush.
165  */
166 
167 /*
168  * This is used as the block size for the space maps used for the
169  * log space map feature. These space maps benefit from a bigger
170  * block size as we expect to be writing a lot of data to them at
171  * once.
172  */
173 static const unsigned long zfs_log_sm_blksz = 1ULL << 17;
174 
175 /*
176  * Percentage of the overall system's memory that ZFS allows to be
177  * used for unflushed changes (e.g. the sum of size of all the nodes
178  * in the unflushed trees).
179  *
180  * Note that this value is calculated over 1000000 for finer granularity
181  * (thus the _ppm suffix; reads as "parts per million"). As an example,
182  * the default of 1000 allows 0.1% of memory to be used.
183  */
184 static uint64_t zfs_unflushed_max_mem_ppm = 1000;
185 
186 /*
187  * Specific hard-limit in memory that ZFS allows to be used for
188  * unflushed changes.
189  */
190 static uint64_t zfs_unflushed_max_mem_amt = 1ULL << 30;
191 
192 /*
193  * The following tunable determines the number of blocks that can be used for
194  * the log space maps. It is expressed as a percentage of the total number of
195  * metaslabs in the pool (i.e. the default of 400 means that the number of log
196  * blocks is capped at 4 times the number of metaslabs).
197  *
198  * This value exists to tune our flushing algorithm, with higher values
199  * flushing metaslabs less often (doing less I/Os) per TXG versus lower values
200  * flushing metaslabs more aggressively with the upside of saving overheads
201  * when loading the pool. Another factor in this tradeoff is that flushing
202  * less often can potentially lead to better utilization of the metaslab space
203  * map's block size as we accumulate more changes per flush.
204  *
205  * Given that this tunable indirectly controls the flush rate (metaslabs
206  * flushed per txg) and that's why making it a percentage in terms of the
207  * number of metaslabs in the pool makes sense here.
208  *
209  * As a rule of thumb we default this tunable to 400% based on the following:
210  *
211  * 1] Assuming a constant flush rate and a constant incoming rate of log blocks
212  *    it is reasonable to expect that the amount of obsolete entries changes
213  *    linearly from txg to txg (e.g. the oldest log should have the most
214  *    obsolete entries, and the most recent one the least). With this we could
215  *    say that, at any given time, about half of the entries in the whole space
216  *    map log are obsolete. Thus for every two entries for a metaslab in the
217  *    log space map, only one of them is valid and actually makes it to the
218  *    metaslab's space map.
219  *    [factor of 2]
220  * 2] Each entry in the log space map is guaranteed to be two words while
221  *    entries in metaslab space maps are generally single-word.
222  *    [an extra factor of 2 - 400% overall]
223  * 3] Even if [1] and [2] are slightly less than 2 each, we haven't taken into
224  *    account any consolidation of segments from the log space map to the
225  *    unflushed range trees nor their history (e.g. a segment being allocated,
226  *    then freed, then allocated again means 3 log space map entries but 0
227  *    metaslab space map entries). Depending on the workload, we've seen ~1.8
228  *    non-obsolete log space map entries per metaslab entry, for a total of
229  *    ~600%. Since most of these estimates though are workload dependent, we
230  *    default on 400% to be conservative.
231  *
232  *    Thus we could say that even in the worst
233  *    case of [1] and [2], the factor should end up being 4.
234  *
235  * That said, regardless of the number of metaslabs in the pool we need to
236  * provide upper and lower bounds for the log block limit.
237  * [see zfs_unflushed_log_block_{min,max}]
238  */
239 static uint_t zfs_unflushed_log_block_pct = 400;
240 
241 /*
242  * If the number of metaslabs is small and our incoming rate is high, we could
243  * get into a situation that we are flushing all our metaslabs every TXG. Thus
244  * we always allow at least this many log blocks.
245  */
246 static uint64_t zfs_unflushed_log_block_min = 1000;
247 
248 /*
249  * If the log becomes too big, the import time of the pool can take a hit in
250  * terms of performance. Thus we have a hard limit in the size of the log in
251  * terms of blocks.
252  */
253 static uint64_t zfs_unflushed_log_block_max = (1ULL << 17);
254 
255 /*
256  * Also we have a hard limit in the size of the log in terms of dirty TXGs.
257  */
258 static uint64_t zfs_unflushed_log_txg_max = 1000;
259 
260 /*
261  * Max # of rows allowed for the log_summary. The tradeoff here is accuracy and
262  * stability of the flushing algorithm (longer summary) vs its runtime overhead
263  * (smaller summary is faster to traverse).
264  */
265 static uint64_t zfs_max_logsm_summary_length = 10;
266 
267 /*
268  * Tunable that sets the lower bound on the metaslabs to flush every TXG.
269  *
270  * Setting this to 0 has no effect since if the pool is idle we won't even be
271  * creating log space maps and therefore we won't be flushing. On the other
272  * hand if the pool has any incoming workload our block heuristic will start
273  * flushing metaslabs anyway.
274  *
275  * The point of this tunable is to be used in extreme cases where we really
276  * want to flush more metaslabs than our adaptable heuristic plans to flush.
277  */
278 static uint64_t zfs_min_metaslabs_to_flush = 1;
279 
280 /*
281  * Tunable that sets the lower bound on the number of unflushed metaslabs
282  * to flush every TXG during a condense operation, as a percentage of the
283  * number of unflushed metaslabs at the time the condense operation started.
284  */
285 static uint_t zfs_min_metaslabs_to_condense_pct = 5;
286 
287 /*
288  * Tunable that specifies how far in the past do we want to look when trying to
289  * estimate the incoming log blocks for the current TXG.
290  *
291  * Setting this too high may not only increase runtime but also minimize the
292  * effect of the incoming rates from the most recent TXGs as we take the
293  * average over all the blocks that we walk
294  * [see spa_estimate_incoming_log_blocks].
295  */
296 static uint64_t zfs_max_log_walking = 5;
297 
298 /*
299  * This tunable exists solely for testing purposes. It ensures that the log
300  * spacemaps are not flushed and destroyed during export in order for the
301  * relevant log spacemap import code paths to be tested (effectively simulating
302  * a crash).
303  */
304 int zfs_keep_log_spacemaps_at_export = 0;
305 
306 static uint64_t
spa_estimate_incoming_log_blocks(spa_t * spa)307 spa_estimate_incoming_log_blocks(spa_t *spa)
308 {
309 	ASSERT3U(spa_sync_pass(spa), ==, 1);
310 	uint64_t steps = 0, sum = 0;
311 	for (spa_log_sm_t *sls = avl_last(&spa->spa_sm_logs_by_txg);
312 	    sls != NULL && steps < zfs_max_log_walking;
313 	    sls = AVL_PREV(&spa->spa_sm_logs_by_txg, sls)) {
314 		if (sls->sls_txg == spa_syncing_txg(spa)) {
315 			/*
316 			 * skip the log created in this TXG as this would
317 			 * make our estimations inaccurate.
318 			 */
319 			continue;
320 		}
321 		sum += sls->sls_nblocks;
322 		steps++;
323 	}
324 	return ((steps > 0) ? DIV_ROUND_UP(sum, steps) : 0);
325 }
326 
327 uint64_t
spa_log_sm_blocklimit(spa_t * spa)328 spa_log_sm_blocklimit(spa_t *spa)
329 {
330 	return (spa->spa_unflushed_stats.sus_blocklimit);
331 }
332 
333 void
spa_log_sm_set_blocklimit(spa_t * spa)334 spa_log_sm_set_blocklimit(spa_t *spa)
335 {
336 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
337 		ASSERT0(spa_log_sm_blocklimit(spa));
338 		return;
339 	}
340 
341 	uint64_t msdcount = 0;
342 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
343 	    e; e = list_next(&spa->spa_log_summary, e))
344 		msdcount += e->lse_msdcount;
345 
346 	uint64_t limit = msdcount * zfs_unflushed_log_block_pct / 100;
347 	spa->spa_unflushed_stats.sus_blocklimit = MIN(MAX(limit,
348 	    zfs_unflushed_log_block_min), zfs_unflushed_log_block_max);
349 }
350 
351 uint64_t
spa_log_sm_nblocks(spa_t * spa)352 spa_log_sm_nblocks(spa_t *spa)
353 {
354 	return (spa->spa_unflushed_stats.sus_nblocks);
355 }
356 
357 /*
358  * Ensure that the in-memory log space map structures and the summary
359  * have the same block and metaslab counts.
360  */
361 static void
spa_log_summary_verify_counts(spa_t * spa)362 spa_log_summary_verify_counts(spa_t *spa)
363 {
364 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP));
365 
366 	if ((zfs_flags & ZFS_DEBUG_LOG_SPACEMAP) == 0)
367 		return;
368 
369 	uint64_t ms_in_avl = avl_numnodes(&spa->spa_metaslabs_by_flushed);
370 
371 	uint64_t ms_in_summary = 0, blk_in_summary = 0;
372 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
373 	    e; e = list_next(&spa->spa_log_summary, e)) {
374 		ms_in_summary += e->lse_mscount;
375 		blk_in_summary += e->lse_blkcount;
376 	}
377 
378 	uint64_t ms_in_logs = 0, blk_in_logs = 0;
379 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
380 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
381 		ms_in_logs += sls->sls_mscount;
382 		blk_in_logs += sls->sls_nblocks;
383 	}
384 
385 	VERIFY3U(ms_in_logs, ==, ms_in_summary);
386 	VERIFY3U(ms_in_logs, ==, ms_in_avl);
387 	VERIFY3U(blk_in_logs, ==, blk_in_summary);
388 	VERIFY3U(blk_in_logs, ==, spa_log_sm_nblocks(spa));
389 }
390 
391 static boolean_t
summary_entry_is_full(spa_t * spa,log_summary_entry_t * e,uint64_t txg)392 summary_entry_is_full(spa_t *spa, log_summary_entry_t *e, uint64_t txg)
393 {
394 	if (e->lse_end == txg)
395 		return (0);
396 	if (e->lse_txgcount >= DIV_ROUND_UP(zfs_unflushed_log_txg_max,
397 	    zfs_max_logsm_summary_length))
398 		return (1);
399 	uint64_t blocks_per_row = MAX(1,
400 	    DIV_ROUND_UP(spa_log_sm_blocklimit(spa),
401 	    zfs_max_logsm_summary_length));
402 	return (blocks_per_row <= e->lse_blkcount);
403 }
404 
405 /*
406  * Update the log summary information to reflect the fact that a metaslab
407  * was flushed or destroyed (e.g due to device removal or pool export/destroy).
408  *
409  * We typically flush the oldest flushed metaslab so the first (and oldest)
410  * entry of the summary is updated. However if that metaslab is getting loaded
411  * we may flush the second oldest one which may be part of an entry later in
412  * the summary. Moreover, if we call into this function from metaslab_fini()
413  * the metaslabs probably won't be ordered by ms_unflushed_txg. Thus we ask
414  * for a txg as an argument so we can locate the appropriate summary entry for
415  * the metaslab.
416  */
417 void
spa_log_summary_decrement_mscount(spa_t * spa,uint64_t txg,boolean_t dirty)418 spa_log_summary_decrement_mscount(spa_t *spa, uint64_t txg, boolean_t dirty)
419 {
420 	/*
421 	 * We don't track summary data for read-only pools and this function
422 	 * can be called from metaslab_fini(). In that case return immediately.
423 	 */
424 	if (!spa_writeable(spa))
425 		return;
426 
427 	log_summary_entry_t *target = NULL;
428 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
429 	    e != NULL; e = list_next(&spa->spa_log_summary, e)) {
430 		if (e->lse_start > txg)
431 			break;
432 		target = e;
433 	}
434 
435 	if (target == NULL || target->lse_mscount == 0) {
436 		/*
437 		 * We didn't find a summary entry for this metaslab. We must be
438 		 * at the teardown of a spa_load() attempt that got an error
439 		 * while reading the log space maps.
440 		 */
441 		VERIFY3S(spa_load_state(spa), ==, SPA_LOAD_ERROR);
442 		return;
443 	}
444 
445 	target->lse_mscount--;
446 	if (dirty)
447 		target->lse_msdcount--;
448 }
449 
450 /*
451  * Update the log summary information to reflect the fact that we destroyed
452  * old log space maps. Since we can only destroy the oldest log space maps,
453  * we decrement the block count of the oldest summary entry and potentially
454  * destroy it when that count hits 0.
455  *
456  * This function is called after a metaslab is flushed and typically that
457  * metaslab is the oldest flushed, which means that this function will
458  * typically decrement the block count of the first entry of the summary and
459  * potentially free it if the block count gets to zero (its metaslab count
460  * should be zero too at that point).
461  *
462  * There are certain scenarios though that don't work exactly like that so we
463  * need to account for them:
464  *
465  * Scenario [1]: It is possible that after we flushed the oldest flushed
466  * metaslab and we destroyed the oldest log space map, more recent logs had 0
467  * metaslabs pointing to them so we got rid of them too. This can happen due
468  * to metaslabs being destroyed through device removal, or because the oldest
469  * flushed metaslab was loading but we kept flushing more recently flushed
470  * metaslabs due to the memory pressure of unflushed changes. Because of that,
471  * we always iterate from the beginning of the summary and if blocks_gone is
472  * bigger than the block_count of the current entry we free that entry (we
473  * expect its metaslab count to be zero), we decrement blocks_gone and on to
474  * the next entry repeating this procedure until blocks_gone gets decremented
475  * to 0. Doing this also works for the typical case mentioned above.
476  *
477  * Scenario [2]: The oldest flushed metaslab isn't necessarily accounted by
478  * the first (and oldest) entry in the summary. If the first few entries of
479  * the summary were only accounting metaslabs from a device that was just
480  * removed, then the current oldest flushed metaslab could be accounted by an
481  * entry somewhere in the middle of the summary. Moreover flushing that
482  * metaslab will destroy all the log space maps older than its ms_unflushed_txg
483  * because they became obsolete after the removal. Thus, iterating as we did
484  * for scenario [1] works out for this case too.
485  *
486  * Scenario [3]: At times we decide to flush all the metaslabs in the pool
487  * in one TXG (either because we are exporting the pool or because our flushing
488  * heuristics decided to do so). When that happens all the log space maps get
489  * destroyed except the one created for the current TXG which doesn't have
490  * any log blocks yet. As log space maps get destroyed with every metaslab that
491  * we flush, entries in the summary are also destroyed. This brings a weird
492  * corner-case when we flush the last metaslab and the log space map of the
493  * current TXG is in the same summary entry with other log space maps that
494  * are older. When that happens we are eventually left with this one last
495  * summary entry whose blocks are gone (blocks_gone equals the entry's block
496  * count) but its metaslab count is non-zero (because it accounts all the
497  * metaslabs in the pool as they all got flushed). Under this scenario we can't
498  * free this last summary entry as it's referencing all the metaslabs in the
499  * pool and its block count will get incremented at the end of this sync (when
500  * we close the syncing log space map). Thus we just decrement its current
501  * block count and leave it alone. In the case that the pool gets exported,
502  * its metaslab count will be decremented over time as we call metaslab_fini()
503  * for all the metaslabs in the pool and the entry will be freed at
504  * spa_unload_log_sm_metadata().
505  */
506 void
spa_log_summary_decrement_blkcount(spa_t * spa,uint64_t blocks_gone)507 spa_log_summary_decrement_blkcount(spa_t *spa, uint64_t blocks_gone)
508 {
509 	log_summary_entry_t *e = list_head(&spa->spa_log_summary);
510 	ASSERT3P(e, !=, NULL);
511 	if (e->lse_txgcount > 0)
512 		e->lse_txgcount--;
513 	for (; e != NULL; e = list_head(&spa->spa_log_summary)) {
514 		if (e->lse_blkcount > blocks_gone) {
515 			e->lse_blkcount -= blocks_gone;
516 			blocks_gone = 0;
517 			break;
518 		} else if (e->lse_mscount == 0) {
519 			/* remove obsolete entry */
520 			blocks_gone -= e->lse_blkcount;
521 			list_remove(&spa->spa_log_summary, e);
522 			kmem_free(e, sizeof (log_summary_entry_t));
523 		} else {
524 			/* Verify that this is scenario [3] mentioned above. */
525 			VERIFY3U(blocks_gone, ==, e->lse_blkcount);
526 
527 			/*
528 			 * Assert that this is scenario [3] further by ensuring
529 			 * that this is the only entry in the summary.
530 			 */
531 			VERIFY3P(e, ==, list_tail(&spa->spa_log_summary));
532 			ASSERT3P(e, ==, list_head(&spa->spa_log_summary));
533 
534 			blocks_gone = e->lse_blkcount = 0;
535 			break;
536 		}
537 	}
538 
539 	/*
540 	 * Ensure that there is no way we are trying to remove more blocks
541 	 * than the # of blocks in the summary.
542 	 */
543 	ASSERT0(blocks_gone);
544 }
545 
546 void
spa_log_sm_decrement_mscount(spa_t * spa,uint64_t txg)547 spa_log_sm_decrement_mscount(spa_t *spa, uint64_t txg)
548 {
549 	spa_log_sm_t target = { .sls_txg = txg };
550 	spa_log_sm_t *sls = avl_find(&spa->spa_sm_logs_by_txg,
551 	    &target, NULL);
552 
553 	if (sls == NULL) {
554 		/*
555 		 * We must be at the teardown of a spa_load() attempt that
556 		 * got an error while reading the log space maps.
557 		 */
558 		VERIFY3S(spa_load_state(spa), ==, SPA_LOAD_ERROR);
559 		return;
560 	}
561 
562 	ASSERT(sls->sls_mscount > 0);
563 	sls->sls_mscount--;
564 }
565 
566 void
spa_log_sm_increment_current_mscount(spa_t * spa)567 spa_log_sm_increment_current_mscount(spa_t *spa)
568 {
569 	spa_log_sm_t *last_sls = avl_last(&spa->spa_sm_logs_by_txg);
570 	ASSERT3U(last_sls->sls_txg, ==, spa_syncing_txg(spa));
571 	last_sls->sls_mscount++;
572 }
573 
574 static void
summary_add_data(spa_t * spa,uint64_t txg,uint64_t metaslabs_flushed,uint64_t metaslabs_dirty,uint64_t nblocks)575 summary_add_data(spa_t *spa, uint64_t txg, uint64_t metaslabs_flushed,
576     uint64_t metaslabs_dirty, uint64_t nblocks)
577 {
578 	log_summary_entry_t *e = list_tail(&spa->spa_log_summary);
579 
580 	if (e == NULL || summary_entry_is_full(spa, e, txg)) {
581 		e = kmem_zalloc(sizeof (log_summary_entry_t), KM_SLEEP);
582 		e->lse_start = e->lse_end = txg;
583 		e->lse_txgcount = 1;
584 		list_insert_tail(&spa->spa_log_summary, e);
585 	}
586 
587 	ASSERT3U(e->lse_start, <=, txg);
588 	if (e->lse_end < txg) {
589 		e->lse_end = txg;
590 		e->lse_txgcount++;
591 	}
592 	e->lse_mscount += metaslabs_flushed;
593 	e->lse_msdcount += metaslabs_dirty;
594 	e->lse_blkcount += nblocks;
595 }
596 
597 static void
spa_log_summary_add_incoming_blocks(spa_t * spa,uint64_t nblocks)598 spa_log_summary_add_incoming_blocks(spa_t *spa, uint64_t nblocks)
599 {
600 	summary_add_data(spa, spa_syncing_txg(spa), 0, 0, nblocks);
601 }
602 
603 void
spa_log_summary_add_flushed_metaslab(spa_t * spa,boolean_t dirty)604 spa_log_summary_add_flushed_metaslab(spa_t *spa, boolean_t dirty)
605 {
606 	summary_add_data(spa, spa_syncing_txg(spa), 1, dirty ? 1 : 0, 0);
607 }
608 
609 void
spa_log_summary_dirty_flushed_metaslab(spa_t * spa,uint64_t txg)610 spa_log_summary_dirty_flushed_metaslab(spa_t *spa, uint64_t txg)
611 {
612 	log_summary_entry_t *target = NULL;
613 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
614 	    e != NULL; e = list_next(&spa->spa_log_summary, e)) {
615 		if (e->lse_start > txg)
616 			break;
617 		target = e;
618 	}
619 	ASSERT3P(target, !=, NULL);
620 	ASSERT3U(target->lse_mscount, !=, 0);
621 	target->lse_msdcount++;
622 }
623 
624 /*
625  * This function attempts to estimate how many metaslabs should
626  * we flush to satisfy our block heuristic for the log spacemap
627  * for the upcoming TXGs.
628  *
629  * Specifically, it first tries to estimate the number of incoming
630  * blocks in this TXG. Then by projecting that incoming rate to
631  * future TXGs and using the log summary, it figures out how many
632  * flushes we would need to do for future TXGs individually to
633  * stay below our block limit and returns the maximum number of
634  * flushes from those estimates.
635  */
636 static uint64_t
spa_estimate_metaslabs_to_flush(spa_t * spa)637 spa_estimate_metaslabs_to_flush(spa_t *spa)
638 {
639 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP));
640 	ASSERT3U(spa_sync_pass(spa), ==, 1);
641 	ASSERT(spa_log_sm_blocklimit(spa) != 0);
642 
643 	/*
644 	 * This variable contains the incoming rate that will be projected
645 	 * and used for our flushing estimates in the future.
646 	 */
647 	uint64_t incoming = spa_estimate_incoming_log_blocks(spa);
648 
649 	/*
650 	 * At any point in time this variable tells us how many
651 	 * TXGs in the future we are so we can make our estimations.
652 	 */
653 	uint64_t txgs_in_future = 1;
654 
655 	/*
656 	 * This variable tells us how much room do we have until we hit
657 	 * our limit. When it goes negative, it means that we've exceeded
658 	 * our limit and we need to flush.
659 	 *
660 	 * Note that since we start at the first TXG in the future (i.e.
661 	 * txgs_in_future starts from 1) we already decrement this
662 	 * variable by the incoming rate.
663 	 */
664 	int64_t available_blocks =
665 	    spa_log_sm_blocklimit(spa) - spa_log_sm_nblocks(spa) - incoming;
666 
667 	int64_t available_txgs = zfs_unflushed_log_txg_max;
668 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
669 	    e; e = list_next(&spa->spa_log_summary, e))
670 		available_txgs -= e->lse_txgcount;
671 
672 	/*
673 	 * This variable tells us the total number of flushes needed to
674 	 * keep the log size within the limit when we reach txgs_in_future.
675 	 */
676 	uint64_t total_flushes = 0;
677 
678 	/* Holds the current maximum of our estimates so far. */
679 	uint64_t max_flushes_pertxg = zfs_min_metaslabs_to_flush;
680 
681 	/*
682 	 * For our estimations we only look as far in the future
683 	 * as the summary allows us.
684 	 */
685 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
686 	    e; e = list_next(&spa->spa_log_summary, e)) {
687 
688 		/*
689 		 * If there is still room before we exceed our limit
690 		 * then keep skipping TXGs accumulating more blocks
691 		 * based on the incoming rate until we exceed it.
692 		 */
693 		if (available_blocks >= 0 && available_txgs >= 0) {
694 			uint64_t skip_txgs = (incoming == 0) ?
695 			    available_txgs + 1 : MIN(available_txgs + 1,
696 			    (available_blocks / incoming) + 1);
697 			available_blocks -= (skip_txgs * incoming);
698 			available_txgs -= skip_txgs;
699 			txgs_in_future += skip_txgs;
700 			ASSERT3S(available_blocks, >=, -incoming);
701 			ASSERT3S(available_txgs, >=, -1);
702 		}
703 
704 		/*
705 		 * At this point we're far enough into the future where
706 		 * the limit was just exceeded and we flush metaslabs
707 		 * based on the current entry in the summary, updating
708 		 * our available_blocks.
709 		 */
710 		ASSERT(available_blocks < 0 || available_txgs < 0);
711 		available_blocks += e->lse_blkcount;
712 		available_txgs += e->lse_txgcount;
713 		total_flushes += e->lse_msdcount;
714 
715 		/*
716 		 * Keep the running maximum of the total_flushes that
717 		 * we've done so far over the number of TXGs in the
718 		 * future that we are. The idea here is to estimate
719 		 * the average number of flushes that we should do
720 		 * every TXG so that when we are that many TXGs in the
721 		 * future we stay under the limit.
722 		 */
723 		max_flushes_pertxg = MAX(max_flushes_pertxg,
724 		    DIV_ROUND_UP(total_flushes, txgs_in_future));
725 	}
726 	return (max_flushes_pertxg);
727 }
728 
729 uint64_t
spa_log_sm_memused(spa_t * spa)730 spa_log_sm_memused(spa_t *spa)
731 {
732 	return (spa->spa_unflushed_stats.sus_memused);
733 }
734 
735 static boolean_t
spa_log_exceeds_memlimit(spa_t * spa)736 spa_log_exceeds_memlimit(spa_t *spa)
737 {
738 	if (spa_log_sm_memused(spa) > zfs_unflushed_max_mem_amt)
739 		return (B_TRUE);
740 
741 	uint64_t system_mem_allowed = ((physmem * PAGESIZE) *
742 	    zfs_unflushed_max_mem_ppm) / 1000000;
743 	if (spa_log_sm_memused(spa) > system_mem_allowed)
744 		return (B_TRUE);
745 
746 	return (B_FALSE);
747 }
748 
749 uint64_t
spa_log_sm_unflushed_metaslabs(spa_t * spa)750 spa_log_sm_unflushed_metaslabs(spa_t *spa)
751 {
752 	return (spa->spa_unflushed_stats.sus_nmetaslabs);
753 }
754 
755 void
spa_log_sm_increment_unflushed_metaslabs(spa_t * spa)756 spa_log_sm_increment_unflushed_metaslabs(spa_t *spa)
757 {
758 	spa->spa_unflushed_stats.sus_nmetaslabs++;
759 }
760 
761 void
spa_log_sm_decrement_unflushed_metaslabs(spa_t * spa)762 spa_log_sm_decrement_unflushed_metaslabs(spa_t *spa)
763 {
764 	spa->spa_unflushed_stats.sus_nmetaslabs--;
765 }
766 
767 void
spa_log_flushall_start(spa_t * spa,spa_log_flushall_mode_t mode,uint64_t txg)768 spa_log_flushall_start(spa_t *spa, spa_log_flushall_mode_t mode, uint64_t txg)
769 {
770 	/* Shouldn't happen, but its not dangerous if it does. */
771 	ASSERT3U(mode, !=, SPA_LOG_FLUSHALL_NONE);
772 	if (mode == SPA_LOG_FLUSHALL_NONE)
773 		return;
774 
775 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
776 
777 	/*
778 	 * For condense, we're flushing everything at this point in time, so
779 	 * it makes no sense to provide a non-zero txg. Remove this sanity
780 	 * check if this ever changes, and you'll need to adjust scns_total
781 	 * below as well to make sure the progress is shown correctly.
782 	 */
783 	IMPLY(mode == SPA_LOG_FLUSHALL_REQUEST, txg == 0);
784 	if (txg == 0)
785 		txg = spa_last_synced_txg(spa);
786 
787 	if (spa->spa_log_flushall_mode != SPA_LOG_FLUSHALL_EXPORT) {
788 		/*
789 		 * We can set _REQUEST even if its already in _REQUEST; this
790 		 * has the effect of just pushing out the end txg.
791 		 */
792 		spa->spa_log_flushall_mode = mode;
793 		spa->spa_log_flushall_txg = txg;
794 	}
795 
796 	if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_REQUEST) {
797 		/* Reset stats */
798 		spa_condense_stat_t *scns =
799 		    &spa->spa_condense_stats[SPA_CONDENSE_LOG_SPACEMAP];
800 		mutex_enter(&spa->spa_condense_stats_lock);
801 		scns->scns_start_time = gethrestime_sec();
802 		scns->scns_end_time = 0;
803 		scns->scns_processed = 0;
804 		scns->scns_total = spa_log_sm_unflushed_metaslabs(spa);
805 		mutex_exit(&spa->spa_condense_stats_lock);
806 
807 		/* Get it started immediately. */
808 		txg_kick(spa->spa_dsl_pool, spa_syncing_txg(spa) + 1);
809 	}
810 
811 	spa_config_exit(spa, SCL_VDEV, FTAG);
812 }
813 
814 void
spa_log_flushall_done(spa_t * spa)815 spa_log_flushall_done(spa_t *spa)
816 {
817 	if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_NONE)
818 		return;
819 
820 	IMPLY(spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_REQUEST,
821 	    spa_state(spa) == POOL_STATE_ACTIVE);
822 	IMPLY(spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_EXPORT,
823 	    spa_state(spa) == POOL_STATE_EXPORTED);
824 	ASSERT3U(spa->spa_log_flushall_txg, >, 0);
825 
826 	if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_REQUEST) {
827 		/*
828 		 * Finish stats. Note that the condense technically ends when
829 		 * all metaslabs have been considered, not when we've flushed
830 		 * as many as we've intended. So, we set the processed to the
831 		 * total just so everything looks right for the user even if
832 		 * if we actually flushed one or two less than requested.
833 		 */
834 		spa_condense_stat_t *scns =
835 		    &spa->spa_condense_stats[SPA_CONDENSE_LOG_SPACEMAP];
836 		mutex_enter(&spa->spa_condense_stats_lock);
837 		scns->scns_end_time = gethrestime_sec();
838 		scns->scns_processed = scns->scns_total;
839 		mutex_exit(&spa->spa_condense_stats_lock);
840 	}
841 
842 	spa->spa_log_flushall_mode = SPA_LOG_FLUSHALL_NONE;
843 	spa->spa_log_flushall_txg = 0;
844 
845 	spa_notify_waiters(spa);
846 }
847 
848 void
spa_log_flushall_cancel(spa_t * spa)849 spa_log_flushall_cancel(spa_t *spa)
850 {
851 	if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_NONE)
852 		return;
853 
854 	ASSERT3U(spa->spa_log_flushall_mode, ==, SPA_LOG_FLUSHALL_REQUEST);
855 
856 	spa->spa_log_flushall_mode = SPA_LOG_FLUSHALL_NONE;
857 	spa->spa_log_flushall_txg = 0;
858 
859 	/* Finish stats. */
860 	spa_condense_stat_t *scns =
861 	    &spa->spa_condense_stats[SPA_CONDENSE_LOG_SPACEMAP];
862 	mutex_enter(&spa->spa_condense_stats_lock);
863 	scns->scns_end_time = gethrestime_sec();
864 	mutex_exit(&spa->spa_condense_stats_lock);
865 
866 	spa_notify_waiters(spa);
867 }
868 
869 boolean_t
spa_log_flushall_active(spa_t * spa)870 spa_log_flushall_active(spa_t *spa)
871 {
872 	return (!!(spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_REQUEST));
873 }
874 
875 void
spa_flush_metaslabs(spa_t * spa,dmu_tx_t * tx)876 spa_flush_metaslabs(spa_t *spa, dmu_tx_t *tx)
877 {
878 	uint64_t txg = dmu_tx_get_txg(tx);
879 
880 	if (spa_sync_pass(spa) != 1)
881 		return;
882 
883 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
884 		return;
885 
886 	/*
887 	 * If we don't have any metaslabs with unflushed changes
888 	 * return immediately.
889 	 */
890 	if (avl_numnodes(&spa->spa_metaslabs_by_flushed) == 0)
891 		return;
892 
893 	/*
894 	 * During SPA export we leave a few empty TXGs to go by [see
895 	 * spa_final_dirty_txg() to understand why]. For this specific
896 	 * case, it is important to not flush any metaslabs as that
897 	 * would dirty this TXG.
898 	 *
899 	 * That said, during one of these dirty TXGs that is less or
900 	 * equal to spa_final_dirty(), spa_unload() will request that
901 	 * we try to flush all the metaslabs for that TXG before
902 	 * exporting the pool, thus we ensure that we didn't get a
903 	 * request of flushing everything before we attempt to return
904 	 * immediately.
905 	 */
906 	if (BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp) < txg &&
907 	    !dmu_objset_is_dirty(spa_meta_objset(spa), txg) &&
908 	    spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_NONE)
909 		return;
910 
911 	/*
912 	 * We need to generate a log space map before flushing because this
913 	 * will set up the in-memory data (i.e. node in spa_sm_logs_by_txg)
914 	 * for this TXG's flushed metaslab count (aka sls_mscount which is
915 	 * manipulated in many ways down the metaslab_flush() codepath).
916 	 *
917 	 * That is not to say that we may generate a log space map when we
918 	 * don't need it. If we are flushing metaslabs, that means that we
919 	 * were going to write changes to disk anyway, so even if we were
920 	 * not flushing, a log space map would have been created anyway in
921 	 * metaslab_sync().
922 	 */
923 	spa_generate_syncing_log_sm(spa, tx);
924 
925 	/*
926 	 * Determine how much to flush this this round, depending on the
927 	 * flushall mode.
928 	 */
929 	uint64_t want_to_flush;
930 	switch (spa->spa_log_flushall_mode) {
931 	case SPA_LOG_FLUSHALL_EXPORT:
932 		/*
933 		 * At export, flush everything we can until the export loop
934 		 * calls spa_log_flushall_done() to stop us.
935 		 */
936 		want_to_flush = UINT64_MAX;
937 		break;
938 
939 	case SPA_LOG_FLUSHALL_REQUEST: {
940 		/*
941 		 * If admin requested flush, flush some percentage of the
942 		 * total dirty metaslabs at the moment the request was made.
943 		 * Always flush at least as many as would be flushed if
944 		 * condense wasn't running.
945 		 */
946 		spa_condense_stat_t *scns =
947 		    &spa->spa_condense_stats[SPA_CONDENSE_LOG_SPACEMAP];
948 		want_to_flush = spa_estimate_metaslabs_to_flush(spa);
949 		mutex_enter(&spa->spa_condense_stats_lock);
950 		want_to_flush = MAX(want_to_flush, scns->scns_total *
951 		    MAX(MIN(zfs_min_metaslabs_to_condense_pct, 100), 1) / 100);
952 		mutex_exit(&spa->spa_condense_stats_lock);
953 		break;
954 	}
955 	default:
956 		/*
957 		 * Flush some number of metaslabs based on the block-heuristic
958 		 * of our flushing algorithm (see block comment of log space
959 		 * map feature).
960 		 */
961 		want_to_flush = spa_estimate_metaslabs_to_flush(spa);
962 		break;
963 	}
964 
965 	/* Used purely for verification purposes */
966 	uint64_t visited = 0;
967 
968 	/*
969 	 * Ideally we would only iterate through spa_metaslabs_by_flushed
970 	 * using only one variable (curr). We can't do that because
971 	 * metaslab_flush() mutates position of curr in the AVL when
972 	 * it flushes that metaslab by moving it to the end of the tree.
973 	 * Thus we always keep track of the original next node of the
974 	 * current node (curr) in another variable (next).
975 	 */
976 	metaslab_t *next = NULL;
977 	for (metaslab_t *curr = avl_first(&spa->spa_metaslabs_by_flushed);
978 	    curr != NULL; curr = next) {
979 		next = AVL_NEXT(&spa->spa_metaslabs_by_flushed, curr);
980 
981 		/*
982 		 * If this metaslab has been flushed this txg then we've done
983 		 * a full circle over the metaslabs.
984 		 */
985 		uint64_t unflushed_txg = metaslab_unflushed_txg(curr);
986 		if (unflushed_txg == txg) {
987 			spa_log_flushall_done(spa);
988 			break;
989 		}
990 
991 		/*
992 		 * If the admin requested flush, and we've reached a metaslab
993 		 * was was dirtied after the flush request began, then we've
994 		 * done all we can (metaslabs are sorted by their last dirtied
995 		 * txg, so all future ones on the list will also be dirtied
996 		 * after the requested txg).
997 		 */
998 		if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_REQUEST &&
999 		    unflushed_txg > spa->spa_log_flushall_txg) {
1000 			spa_log_flushall_done(spa);
1001 			break;
1002 		}
1003 
1004 		/*
1005 		 * If we are done flushing for the block heuristic and the
1006 		 * unflushed changes don't exceed the memory limit just stop.
1007 		 */
1008 		if (want_to_flush == 0 && !spa_log_exceeds_memlimit(spa))
1009 			break;
1010 
1011 		/*
1012 		 * If this metaslab had dirty blocks on it, flush it and update
1013 		 * the counters.
1014 		 */
1015 		if (metaslab_unflushed_dirty(curr)) {
1016 			mutex_enter(&curr->ms_sync_lock);
1017 			mutex_enter(&curr->ms_lock);
1018 			metaslab_flush(curr, tx);
1019 			mutex_exit(&curr->ms_lock);
1020 			mutex_exit(&curr->ms_sync_lock);
1021 
1022 			if (want_to_flush > 0)
1023 				want_to_flush--;
1024 
1025 			if (spa->spa_log_flushall_mode ==
1026 			    SPA_LOG_FLUSHALL_REQUEST) {
1027 				spa_condense_stat_t *scns =
1028 				    &spa->spa_condense_stats
1029 				    [SPA_CONDENSE_LOG_SPACEMAP];
1030 				mutex_enter(&spa->spa_condense_stats_lock);
1031 				scns->scns_processed++;
1032 				mutex_exit(&spa->spa_condense_stats_lock);
1033 			}
1034 		} else
1035 			metaslab_unflushed_bump(curr, tx, B_FALSE);
1036 
1037 		visited++;
1038 	}
1039 	ASSERT3U(avl_numnodes(&spa->spa_metaslabs_by_flushed), >=, visited);
1040 
1041 	spa_log_sm_set_blocklimit(spa);
1042 }
1043 
1044 /*
1045  * Close the log space map for this TXG and update the block counts
1046  * for the log's in-memory structure and the summary.
1047  */
1048 void
spa_sync_close_syncing_log_sm(spa_t * spa)1049 spa_sync_close_syncing_log_sm(spa_t *spa)
1050 {
1051 	if (spa_syncing_log_sm(spa) == NULL)
1052 		return;
1053 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP));
1054 
1055 	spa_log_sm_t *sls = avl_last(&spa->spa_sm_logs_by_txg);
1056 	ASSERT3U(sls->sls_txg, ==, spa_syncing_txg(spa));
1057 
1058 	sls->sls_nblocks = space_map_nblocks(spa_syncing_log_sm(spa));
1059 	spa->spa_unflushed_stats.sus_nblocks += sls->sls_nblocks;
1060 
1061 	/*
1062 	 * Note that we can't assert that sls_mscount is not 0,
1063 	 * because there is the case where the first metaslab
1064 	 * in spa_metaslabs_by_flushed is loading and we were
1065 	 * not able to flush any metaslabs the current TXG.
1066 	 */
1067 	ASSERT(sls->sls_nblocks != 0);
1068 
1069 	spa_log_summary_add_incoming_blocks(spa, sls->sls_nblocks);
1070 	spa_log_summary_verify_counts(spa);
1071 
1072 	space_map_close(spa->spa_syncing_log_sm);
1073 	spa->spa_syncing_log_sm = NULL;
1074 
1075 	/*
1076 	 * At this point we tried to flush as many metaslabs as we
1077 	 * can as the pool is getting exported. Reset the "flush all"
1078 	 * so the last few TXGs before closing the pool can be empty
1079 	 * (e.g. not dirty).
1080 	 */
1081 	if (spa->spa_log_flushall_mode == SPA_LOG_FLUSHALL_EXPORT) {
1082 		ASSERT3S(spa_state(spa), ==, POOL_STATE_EXPORTED);
1083 		spa_log_flushall_done(spa);
1084 	}
1085 }
1086 
1087 void
spa_cleanup_old_sm_logs(spa_t * spa,dmu_tx_t * tx)1088 spa_cleanup_old_sm_logs(spa_t *spa, dmu_tx_t *tx)
1089 {
1090 	objset_t *mos = spa_meta_objset(spa);
1091 
1092 	uint64_t spacemap_zap;
1093 	int error = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
1094 	    DMU_POOL_LOG_SPACEMAP_ZAP, sizeof (spacemap_zap), 1, &spacemap_zap);
1095 	if (error == ENOENT) {
1096 		ASSERT(avl_is_empty(&spa->spa_sm_logs_by_txg));
1097 		return;
1098 	}
1099 	VERIFY0(error);
1100 
1101 	metaslab_t *oldest = avl_first(&spa->spa_metaslabs_by_flushed);
1102 	uint64_t oldest_flushed_txg = metaslab_unflushed_txg(oldest);
1103 
1104 	/* Free all log space maps older than the oldest_flushed_txg. */
1105 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
1106 	    sls && sls->sls_txg < oldest_flushed_txg;
1107 	    sls = avl_first(&spa->spa_sm_logs_by_txg)) {
1108 		ASSERT0(sls->sls_mscount);
1109 		avl_remove(&spa->spa_sm_logs_by_txg, sls);
1110 		space_map_free_obj(mos, sls->sls_sm_obj, tx);
1111 		VERIFY0(zap_remove_int(mos, spacemap_zap, sls->sls_txg, tx));
1112 		spa_log_summary_decrement_blkcount(spa, sls->sls_nblocks);
1113 		spa->spa_unflushed_stats.sus_nblocks -= sls->sls_nblocks;
1114 		kmem_free(sls, sizeof (spa_log_sm_t));
1115 	}
1116 }
1117 
1118 static spa_log_sm_t *
spa_log_sm_alloc(uint64_t sm_obj,uint64_t txg)1119 spa_log_sm_alloc(uint64_t sm_obj, uint64_t txg)
1120 {
1121 	spa_log_sm_t *sls = kmem_zalloc(sizeof (*sls), KM_SLEEP);
1122 	sls->sls_sm_obj = sm_obj;
1123 	sls->sls_txg = txg;
1124 	return (sls);
1125 }
1126 
1127 void
spa_generate_syncing_log_sm(spa_t * spa,dmu_tx_t * tx)1128 spa_generate_syncing_log_sm(spa_t *spa, dmu_tx_t *tx)
1129 {
1130 	uint64_t txg = dmu_tx_get_txg(tx);
1131 	objset_t *mos = spa_meta_objset(spa);
1132 
1133 	if (spa_syncing_log_sm(spa) != NULL)
1134 		return;
1135 
1136 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_LOG_SPACEMAP))
1137 		return;
1138 
1139 	uint64_t spacemap_zap;
1140 	int error = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
1141 	    DMU_POOL_LOG_SPACEMAP_ZAP, sizeof (spacemap_zap), 1, &spacemap_zap);
1142 	if (error == ENOENT) {
1143 		ASSERT(avl_is_empty(&spa->spa_sm_logs_by_txg));
1144 
1145 		error = 0;
1146 		spacemap_zap = zap_create(mos,
1147 		    DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
1148 		VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
1149 		    DMU_POOL_LOG_SPACEMAP_ZAP, sizeof (spacemap_zap), 1,
1150 		    &spacemap_zap, tx));
1151 		spa_feature_incr(spa, SPA_FEATURE_LOG_SPACEMAP, tx);
1152 	}
1153 	VERIFY0(error);
1154 
1155 	uint64_t sm_obj;
1156 	ASSERT3U(zap_lookup_int_key(mos, spacemap_zap, txg, &sm_obj),
1157 	    ==, ENOENT);
1158 	sm_obj = space_map_alloc(mos, zfs_log_sm_blksz, tx);
1159 	VERIFY0(zap_add_int_key(mos, spacemap_zap, txg, sm_obj, tx));
1160 	avl_add(&spa->spa_sm_logs_by_txg, spa_log_sm_alloc(sm_obj, txg));
1161 
1162 	/*
1163 	 * We pass UINT64_MAX as the space map's representation size
1164 	 * and SPA_MINBLOCKSHIFT as the shift, to make the space map
1165 	 * accept any sorts of segments since there's no real advantage
1166 	 * to being more restrictive (given that we're already going
1167 	 * to be using 2-word entries).
1168 	 */
1169 	VERIFY0(space_map_open(&spa->spa_syncing_log_sm, mos, sm_obj,
1170 	    0, UINT64_MAX, SPA_MINBLOCKSHIFT));
1171 
1172 	spa_log_sm_set_blocklimit(spa);
1173 }
1174 
1175 /*
1176  * Find all the log space maps stored in the space map ZAP and sort
1177  * them by their TXG in spa_sm_logs_by_txg.
1178  */
1179 static int
spa_ld_log_sm_metadata(spa_t * spa)1180 spa_ld_log_sm_metadata(spa_t *spa)
1181 {
1182 	int error;
1183 	uint64_t spacemap_zap;
1184 
1185 	ASSERT(avl_is_empty(&spa->spa_sm_logs_by_txg));
1186 
1187 	error = zap_lookup(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
1188 	    DMU_POOL_LOG_SPACEMAP_ZAP, sizeof (spacemap_zap), 1, &spacemap_zap);
1189 	if (error == ENOENT) {
1190 		/* the space map ZAP doesn't exist yet */
1191 		return (0);
1192 	} else if (error != 0) {
1193 		spa_load_failed(spa, "spa_ld_log_sm_metadata(): failed at "
1194 		    "zap_lookup(DMU_POOL_DIRECTORY_OBJECT) [error %d]",
1195 		    error);
1196 		return (error);
1197 	}
1198 
1199 	zap_cursor_t zc;
1200 	zap_attribute_t *za = zap_attribute_alloc();
1201 	for (zap_cursor_init(&zc, spa_meta_objset(spa), spacemap_zap);
1202 	    (error = zap_cursor_retrieve(&zc, za)) == 0;
1203 	    zap_cursor_advance(&zc)) {
1204 		uint64_t log_txg = zfs_strtonum(za->za_name, NULL);
1205 		spa_log_sm_t *sls =
1206 		    spa_log_sm_alloc(za->za_first_integer, log_txg);
1207 		avl_add(&spa->spa_sm_logs_by_txg, sls);
1208 	}
1209 	zap_cursor_fini(&zc);
1210 	zap_attribute_free(za);
1211 	if (error != ENOENT) {
1212 		spa_load_failed(spa, "spa_ld_log_sm_metadata(): failed at "
1213 		    "zap_cursor_retrieve(spacemap_zap) [error %d]",
1214 		    error);
1215 		return (error);
1216 	}
1217 
1218 	for (metaslab_t *m = avl_first(&spa->spa_metaslabs_by_flushed);
1219 	    m; m = AVL_NEXT(&spa->spa_metaslabs_by_flushed, m)) {
1220 		spa_log_sm_t target = { .sls_txg = metaslab_unflushed_txg(m) };
1221 		spa_log_sm_t *sls = avl_find(&spa->spa_sm_logs_by_txg,
1222 		    &target, NULL);
1223 
1224 		/*
1225 		 * At this point if sls is zero it means that a bug occurred
1226 		 * in ZFS the last time the pool was open or earlier in the
1227 		 * import code path. In general, we would have placed a
1228 		 * VERIFY() here or in this case just let the kernel panic
1229 		 * with NULL pointer dereference when incrementing sls_mscount,
1230 		 * but since this is the import code path we can be a bit more
1231 		 * lenient. Thus, for DEBUG bits we always cause a panic, while
1232 		 * in production we log the error and just fail the import.
1233 		 */
1234 		ASSERT(sls != NULL);
1235 		if (sls == NULL) {
1236 			spa_load_failed(spa, "spa_ld_log_sm_metadata(): bug "
1237 			    "encountered: could not find log spacemap for "
1238 			    "TXG %llu [error %d]",
1239 			    (u_longlong_t)metaslab_unflushed_txg(m), ENOENT);
1240 			return (ENOENT);
1241 		}
1242 		sls->sls_mscount++;
1243 	}
1244 
1245 	return (0);
1246 }
1247 
1248 typedef struct spa_ld_log_sm_arg {
1249 	spa_t *slls_spa;
1250 	uint64_t slls_txg;
1251 } spa_ld_log_sm_arg_t;
1252 
1253 static int
spa_ld_log_sm_cb(space_map_entry_t * sme,void * arg)1254 spa_ld_log_sm_cb(space_map_entry_t *sme, void *arg)
1255 {
1256 	uint64_t offset = sme->sme_offset;
1257 	uint64_t size = sme->sme_run;
1258 	uint32_t vdev_id = sme->sme_vdev;
1259 
1260 	spa_ld_log_sm_arg_t *slls = arg;
1261 	spa_t *spa = slls->slls_spa;
1262 
1263 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
1264 
1265 	/*
1266 	 * If the vdev has been removed (i.e. it is indirect or a hole)
1267 	 * skip this entry. The contents of this vdev have already moved
1268 	 * elsewhere.
1269 	 */
1270 	if (!vdev_is_concrete(vd))
1271 		return (0);
1272 
1273 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1274 	ASSERT(!ms->ms_loaded);
1275 
1276 	/*
1277 	 * If we have already flushed entries for this TXG to this
1278 	 * metaslab's space map, then ignore it. Note that we flush
1279 	 * before processing any allocations/frees for that TXG, so
1280 	 * the metaslab's space map only has entries from *before*
1281 	 * the unflushed TXG.
1282 	 */
1283 	if (slls->slls_txg < metaslab_unflushed_txg(ms))
1284 		return (0);
1285 
1286 	switch (sme->sme_type) {
1287 	case SM_ALLOC:
1288 		zfs_range_tree_remove_xor_add_segment(offset, offset + size,
1289 		    ms->ms_unflushed_frees, ms->ms_unflushed_allocs);
1290 		break;
1291 	case SM_FREE:
1292 		zfs_range_tree_remove_xor_add_segment(offset, offset + size,
1293 		    ms->ms_unflushed_allocs, ms->ms_unflushed_frees);
1294 		break;
1295 	default:
1296 		panic("invalid maptype_t");
1297 		break;
1298 	}
1299 	if (!metaslab_unflushed_dirty(ms)) {
1300 		metaslab_set_unflushed_dirty(ms, B_TRUE);
1301 		spa_log_summary_dirty_flushed_metaslab(spa,
1302 		    metaslab_unflushed_txg(ms));
1303 	}
1304 	return (0);
1305 }
1306 
1307 static int
spa_ld_log_sm_data(spa_t * spa)1308 spa_ld_log_sm_data(spa_t *spa)
1309 {
1310 	spa_log_sm_t *sls, *psls;
1311 	int error = 0;
1312 
1313 	/*
1314 	 * If we are not going to do any writes there is no need
1315 	 * to read the log space maps.
1316 	 */
1317 	if (!spa_writeable(spa))
1318 		return (0);
1319 
1320 	ASSERT0(spa->spa_unflushed_stats.sus_nblocks);
1321 	ASSERT0(spa->spa_unflushed_stats.sus_memused);
1322 
1323 	hrtime_t read_logs_starttime = gethrtime();
1324 
1325 	/* Prefetch log spacemaps dnodes. */
1326 	for (sls = avl_first(&spa->spa_sm_logs_by_txg); sls;
1327 	    sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
1328 		dmu_prefetch_dnode(spa_meta_objset(spa), sls->sls_sm_obj,
1329 		    ZIO_PRIORITY_SYNC_READ);
1330 	}
1331 
1332 	uint_t pn = 0;
1333 	uint64_t ps = 0;
1334 	uint64_t nsm = 0;
1335 	psls = sls = avl_first(&spa->spa_sm_logs_by_txg);
1336 	while (sls != NULL) {
1337 		/* Prefetch log spacemaps up to 16 TXGs or MBs ahead. */
1338 		if (psls != NULL && pn < 16 &&
1339 		    (pn < 2 || ps < dmu_prefetch_max)) {
1340 			error = space_map_open(&psls->sls_sm,
1341 			    spa_meta_objset(spa), psls->sls_sm_obj, 0,
1342 			    UINT64_MAX, SPA_MINBLOCKSHIFT);
1343 			if (error != 0) {
1344 				spa_load_failed(spa, "spa_ld_log_sm_data(): "
1345 				    "failed at space_map_open(obj=%llu) "
1346 				    "[error %d]",
1347 				    (u_longlong_t)sls->sls_sm_obj, error);
1348 				goto out;
1349 			}
1350 			dmu_prefetch_stream(spa_meta_objset(spa),
1351 			    psls->sls_sm_obj, 0,
1352 			    space_map_length(psls->sls_sm), B_TRUE);
1353 			pn++;
1354 			ps += space_map_length(psls->sls_sm);
1355 			psls = AVL_NEXT(&spa->spa_sm_logs_by_txg, psls);
1356 			continue;
1357 		}
1358 
1359 		/* Load TXG log spacemap into ms_unflushed_allocs/frees. */
1360 		kpreempt(KPREEMPT_SYNC);
1361 		ASSERT0(sls->sls_nblocks);
1362 		sls->sls_nblocks = space_map_nblocks(sls->sls_sm);
1363 		spa->spa_unflushed_stats.sus_nblocks += sls->sls_nblocks;
1364 		summary_add_data(spa, sls->sls_txg,
1365 		    sls->sls_mscount, 0, sls->sls_nblocks);
1366 
1367 		spa_import_progress_set_notes_nolog(spa,
1368 		    "Read %llu of %lu log space maps", (u_longlong_t)nsm,
1369 		    avl_numnodes(&spa->spa_sm_logs_by_txg));
1370 
1371 		struct spa_ld_log_sm_arg vla = {
1372 			.slls_spa = spa,
1373 			.slls_txg = sls->sls_txg
1374 		};
1375 		error = space_map_iterate(sls->sls_sm,
1376 		    space_map_length(sls->sls_sm), spa_ld_log_sm_cb, &vla);
1377 		if (error != 0) {
1378 			spa_load_failed(spa, "spa_ld_log_sm_data(): failed "
1379 			    "at space_map_iterate(obj=%llu) [error %d]",
1380 			    (u_longlong_t)sls->sls_sm_obj, error);
1381 			goto out;
1382 		}
1383 
1384 		pn--;
1385 		ps -= space_map_length(sls->sls_sm);
1386 		nsm++;
1387 		space_map_close(sls->sls_sm);
1388 		sls->sls_sm = NULL;
1389 		sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls);
1390 
1391 		/* Update log block limits considering just loaded. */
1392 		spa_log_sm_set_blocklimit(spa);
1393 	}
1394 
1395 	hrtime_t read_logs_endtime = gethrtime();
1396 	spa_load_note(spa,
1397 	    "Read %lu log space maps (%llu total blocks - blksz = %llu bytes) "
1398 	    "in %lld ms", avl_numnodes(&spa->spa_sm_logs_by_txg),
1399 	    (u_longlong_t)spa_log_sm_nblocks(spa),
1400 	    (u_longlong_t)zfs_log_sm_blksz,
1401 	    (longlong_t)NSEC2MSEC(read_logs_endtime - read_logs_starttime));
1402 
1403 out:
1404 	if (error != 0) {
1405 		for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
1406 		    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
1407 			if (sls->sls_sm) {
1408 				space_map_close(sls->sls_sm);
1409 				sls->sls_sm = NULL;
1410 			}
1411 		}
1412 	} else {
1413 		ASSERT0(pn);
1414 		ASSERT0(ps);
1415 	}
1416 	/*
1417 	 * Now that the metaslabs contain their unflushed changes:
1418 	 * [1] recalculate their actual allocated space
1419 	 * [2] recalculate their weights
1420 	 * [3] sum up the memory usage of their unflushed range trees
1421 	 * [4] optionally load them, if debug_load is set
1422 	 *
1423 	 * Note that even in the case where we get here because of an
1424 	 * error (e.g. error != 0), we still want to update the fields
1425 	 * below in order to have a proper teardown in spa_unload().
1426 	 */
1427 	for (metaslab_t *m = avl_first(&spa->spa_metaslabs_by_flushed);
1428 	    m != NULL; m = AVL_NEXT(&spa->spa_metaslabs_by_flushed, m)) {
1429 		mutex_enter(&m->ms_lock);
1430 		m->ms_allocated_space = space_map_allocated(m->ms_sm) +
1431 		    zfs_range_tree_space(m->ms_unflushed_allocs) -
1432 		    zfs_range_tree_space(m->ms_unflushed_frees);
1433 
1434 		metaslab_space_update(m->ms_group,
1435 		    zfs_range_tree_space(m->ms_unflushed_allocs), 0, 0);
1436 		metaslab_space_update(m->ms_group,
1437 		    -zfs_range_tree_space(m->ms_unflushed_frees), 0, 0);
1438 
1439 		ASSERT0(m->ms_weight & METASLAB_ACTIVE_MASK);
1440 		metaslab_recalculate_weight_and_sort(m);
1441 
1442 		spa->spa_unflushed_stats.sus_memused +=
1443 		    metaslab_unflushed_changes_memused(m);
1444 
1445 		if (metaslab_debug_load && m->ms_sm != NULL) {
1446 			VERIFY0(metaslab_load(m));
1447 			metaslab_set_selected_txg(m, 0);
1448 		}
1449 		mutex_exit(&m->ms_lock);
1450 	}
1451 
1452 	return (error);
1453 }
1454 
1455 static int
spa_ld_unflushed_txgs(vdev_t * vd)1456 spa_ld_unflushed_txgs(vdev_t *vd)
1457 {
1458 	spa_t *spa = vd->vdev_spa;
1459 	objset_t *mos = spa_meta_objset(spa);
1460 
1461 	if (vd->vdev_top_zap == 0)
1462 		return (0);
1463 
1464 	uint64_t object = 0;
1465 	int error = zap_lookup(mos, vd->vdev_top_zap,
1466 	    VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
1467 	    sizeof (uint64_t), 1, &object);
1468 	if (error == ENOENT)
1469 		return (0);
1470 	else if (error != 0) {
1471 		spa_load_failed(spa, "spa_ld_unflushed_txgs(): failed at "
1472 		    "zap_lookup(vdev_top_zap=%llu) [error %d]",
1473 		    (u_longlong_t)vd->vdev_top_zap, error);
1474 		return (error);
1475 	}
1476 
1477 	for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
1478 		metaslab_t *ms = vd->vdev_ms[m];
1479 		ASSERT(ms != NULL);
1480 
1481 		metaslab_unflushed_phys_t entry;
1482 		uint64_t entry_size = sizeof (entry);
1483 		uint64_t entry_offset = ms->ms_id * entry_size;
1484 
1485 		error = dmu_read(mos, object,
1486 		    entry_offset, entry_size, &entry, 0);
1487 		if (error != 0) {
1488 			spa_load_failed(spa, "spa_ld_unflushed_txgs(): "
1489 			    "failed at dmu_read(obj=%llu) [error %d]",
1490 			    (u_longlong_t)object, error);
1491 			return (error);
1492 		}
1493 
1494 		ms->ms_unflushed_txg = entry.msp_unflushed_txg;
1495 		ms->ms_unflushed_dirty = B_FALSE;
1496 		ASSERT(zfs_range_tree_is_empty(ms->ms_unflushed_allocs));
1497 		ASSERT(zfs_range_tree_is_empty(ms->ms_unflushed_frees));
1498 		if (ms->ms_unflushed_txg != 0) {
1499 			mutex_enter(&spa->spa_flushed_ms_lock);
1500 			avl_add(&spa->spa_metaslabs_by_flushed, ms);
1501 			mutex_exit(&spa->spa_flushed_ms_lock);
1502 		}
1503 	}
1504 	return (0);
1505 }
1506 
1507 /*
1508  * Read all the log space map entries into their respective
1509  * metaslab unflushed trees and keep them sorted by TXG in the
1510  * SPA's metadata. In addition, setup all the metadata for the
1511  * memory and the block heuristics.
1512  */
1513 int
spa_ld_log_spacemaps(spa_t * spa)1514 spa_ld_log_spacemaps(spa_t *spa)
1515 {
1516 	int error;
1517 
1518 	spa_log_sm_set_blocklimit(spa);
1519 
1520 	for (uint64_t c = 0; c < spa->spa_root_vdev->vdev_children; c++) {
1521 		vdev_t *vd = spa->spa_root_vdev->vdev_child[c];
1522 		error = spa_ld_unflushed_txgs(vd);
1523 		if (error != 0)
1524 			return (error);
1525 	}
1526 
1527 	error = spa_ld_log_sm_metadata(spa);
1528 	if (error != 0)
1529 		return (error);
1530 
1531 	/*
1532 	 * Note: we don't actually expect anything to change at this point
1533 	 * but we grab the config lock so we don't fail any assertions
1534 	 * when using vdev_lookup_top().
1535 	 */
1536 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1537 	error = spa_ld_log_sm_data(spa);
1538 	spa_config_exit(spa, SCL_CONFIG, FTAG);
1539 
1540 	return (error);
1541 }
1542 
1543 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_max_mem_amt, U64, ZMOD_RW,
1544 	"Specific hard-limit in memory that ZFS allows to be used for "
1545 	"unflushed changes");
1546 
1547 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_max_mem_ppm, U64, ZMOD_RW,
1548 	"Percentage of the overall system memory that ZFS allows to be "
1549 	"used for unflushed changes (value is calculated over 1000000 for "
1550 	"finer granularity)");
1551 
1552 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_log_block_max, U64, ZMOD_RW,
1553 	"Hard limit (upper-bound) in the size of the space map log "
1554 	"in terms of blocks.");
1555 
1556 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_log_block_min, U64, ZMOD_RW,
1557 	"Lower-bound limit for the maximum amount of blocks allowed in "
1558 	"log spacemap (see zfs_unflushed_log_block_max)");
1559 
1560 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_log_txg_max, U64, ZMOD_RW,
1561 	"Hard limit (upper-bound) in the size of the space map log "
1562 	"in terms of dirty TXGs.");
1563 
1564 ZFS_MODULE_PARAM(zfs, zfs_, unflushed_log_block_pct, UINT, ZMOD_RW,
1565 	"Tunable used to determine the number of blocks that can be used for "
1566 	"the spacemap log, expressed as a percentage of the total number of "
1567 	"metaslabs in the pool (e.g. 400 means the number of log blocks is "
1568 	"capped at 4 times the number of metaslabs)");
1569 
1570 ZFS_MODULE_PARAM(zfs, zfs_, max_log_walking, U64, ZMOD_RW,
1571 	"The number of past TXGs that the flushing algorithm of the log "
1572 	"spacemap feature uses to estimate incoming log blocks");
1573 
1574 ZFS_MODULE_PARAM(zfs, zfs_, keep_log_spacemaps_at_export, INT, ZMOD_RW,
1575 	"Prevent the log spacemaps from being flushed and destroyed "
1576 	"during pool export/destroy");
1577 
1578 ZFS_MODULE_PARAM(zfs, zfs_, max_logsm_summary_length, U64, ZMOD_RW,
1579 	"Maximum number of rows allowed in the summary of the spacemap log");
1580 
1581 ZFS_MODULE_PARAM(zfs, zfs_, min_metaslabs_to_flush, U64, ZMOD_RW,
1582 	"Minimum number of metaslabs to flush per dirty TXG");
1583 
1584 ZFS_MODULE_PARAM(zfs, zfs_, min_metaslabs_to_condense_pct, UINT, ZMOD_RW,
1585 	"Minimum number of metaslabs to flush per TXG when condensing, "
1586 	"as a percent of the number of dirty metaslabs at condense start.");
1587