xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_trim.c (revision b59a0cde6a5253f94494397ce5b18dbfa071e08c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2016, 2024 by Delphix. All rights reserved.
24  * Copyright (c) 2019 by Lawrence Livermore National Security, LLC.
25  * Copyright (c) 2021 Hewlett Packard Enterprise Development LP
26  * Copyright 2023 RackTop Systems, Inc.
27  */
28 
29 #include <sys/spa.h>
30 #include <sys/spa_impl.h>
31 #include <sys/txg.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/vdev_trim.h>
34 #include <sys/metaslab_impl.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/zap.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/arc_impl.h>
39 
40 /*
41  * TRIM is a feature which is used to notify a SSD that some previously
42  * written space is no longer allocated by the pool.  This is useful because
43  * writes to a SSD must be performed to blocks which have first been erased.
44  * Ensuring the SSD always has a supply of erased blocks for new writes
45  * helps prevent the performance from deteriorating.
46  *
47  * There are two supported TRIM methods; manual and automatic.
48  *
49  * Manual TRIM:
50  *
51  * A manual TRIM is initiated by running the 'zpool trim' command.  A single
52  * 'vdev_trim' thread is created for each leaf vdev, and it is responsible for
53  * managing that vdev TRIM process.  This involves iterating over all the
54  * metaslabs, calculating the unallocated space ranges, and then issuing the
55  * required TRIM I/Os.
56  *
57  * While a metaslab is being actively trimmed it is not eligible to perform
58  * new allocations.  After traversing all of the metaslabs the thread is
59  * terminated.  Finally, both the requested options and current progress of
60  * the TRIM are regularly written to the pool.  This allows the TRIM to be
61  * suspended and resumed as needed.
62  *
63  * Automatic TRIM:
64  *
65  * An automatic TRIM is enabled by setting the 'autotrim' pool property
66  * to 'on'.  When enabled, a `vdev_autotrim' thread is created for each
67  * top-level (not leaf) vdev in the pool.  These threads perform the same
68  * core TRIM process as a manual TRIM, but with a few key differences.
69  *
70  * 1) Automatic TRIM happens continuously in the background and operates
71  *    solely on recently freed blocks (ms_trim not ms_allocatable).
72  *
73  * 2) Each thread is associated with a top-level (not leaf) vdev.  This has
74  *    the benefit of simplifying the threading model, it makes it easier
75  *    to coordinate administrative commands, and it ensures only a single
76  *    metaslab is disabled at a time.  Unlike manual TRIM, this means each
77  *    'vdev_autotrim' thread is responsible for issuing TRIM I/Os for its
78  *    children.
79  *
80  * 3) There is no automatic TRIM progress information stored on disk, nor
81  *    is it reported by 'zpool status'.
82  *
83  * While the automatic TRIM process is highly effective it is more likely
84  * than a manual TRIM to encounter tiny ranges.  Ranges less than or equal to
85  * 'zfs_trim_extent_bytes_min' (32k) are considered too small to efficiently
86  * TRIM and are skipped.  This means small amounts of freed space may not
87  * be automatically trimmed.
88  *
89  * Furthermore, devices with attached hot spares and devices being actively
90  * replaced are skipped.  This is done to avoid adding additional stress to
91  * a potentially unhealthy device and to minimize the required rebuild time.
92  *
93  * For this reason it may be beneficial to occasionally manually TRIM a pool
94  * even when automatic TRIM is enabled.
95  */
96 
97 /*
98  * Maximum size of TRIM I/O, ranges will be chunked in to 128MiB lengths.
99  */
100 static unsigned int zfs_trim_extent_bytes_max = 128 * 1024 * 1024;
101 
102 /*
103  * Minimum size of TRIM I/O, extents smaller than 32Kib will be skipped.
104  */
105 static unsigned int zfs_trim_extent_bytes_min = 32 * 1024;
106 
107 /*
108  * Skip uninitialized metaslabs during the TRIM process.  This option is
109  * useful for pools constructed from large thinly-provisioned devices where
110  * TRIM operations are slow.  As a pool ages an increasing fraction of
111  * the pools metaslabs will be initialized progressively degrading the
112  * usefulness of this option.  This setting is stored when starting a
113  * manual TRIM and will persist for the duration of the requested TRIM.
114  */
115 unsigned int zfs_trim_metaslab_skip = 0;
116 
117 /*
118  * Maximum number of queued TRIM I/Os per leaf vdev.  The number of
119  * concurrent TRIM I/Os issued to the device is controlled by the
120  * zfs_vdev_trim_min_active and zfs_vdev_trim_max_active module options.
121  */
122 static unsigned int zfs_trim_queue_limit = 10;
123 
124 /*
125  * The minimum number of transaction groups between automatic trims of a
126  * metaslab.  This setting represents a trade-off between issuing more
127  * efficient TRIM operations, by allowing them to be aggregated longer,
128  * and issuing them promptly so the trimmed space is available.  Note
129  * that this value is a minimum; metaslabs can be trimmed less frequently
130  * when there are a large number of ranges which need to be trimmed.
131  *
132  * Increasing this value will allow frees to be aggregated for a longer
133  * time.  This can result is larger TRIM operations, and increased memory
134  * usage in order to track the ranges to be trimmed.  Decreasing this value
135  * has the opposite effect.  The default value of 32 was determined though
136  * testing to be a reasonable compromise.
137  */
138 static unsigned int zfs_trim_txg_batch = 32;
139 
140 /*
141  * The trim_args are a control structure which describe how a leaf vdev
142  * should be trimmed.  The core elements are the vdev, the metaslab being
143  * trimmed and a range tree containing the extents to TRIM.  All provided
144  * ranges must be within the metaslab.
145  */
146 typedef struct trim_args {
147 	/*
148 	 * These fields are set by the caller of vdev_trim_ranges().
149 	 */
150 	vdev_t		*trim_vdev;		/* Leaf vdev to TRIM */
151 	metaslab_t	*trim_msp;		/* Disabled metaslab */
152 	zfs_range_tree_t	*trim_tree;	/* TRIM ranges (in metaslab) */
153 	trim_type_t	trim_type;		/* Manual or auto TRIM */
154 	uint64_t	trim_extent_bytes_max;	/* Maximum TRIM I/O size */
155 	uint64_t	trim_extent_bytes_min;	/* Minimum TRIM I/O size */
156 	enum trim_flag	trim_flags;		/* TRIM flags (secure) */
157 
158 	/*
159 	 * These fields are updated by vdev_trim_ranges().
160 	 */
161 	hrtime_t	trim_start_time;	/* Start time */
162 	uint64_t	trim_bytes_done;	/* Bytes trimmed */
163 } trim_args_t;
164 
165 /*
166  * Determines whether a vdev_trim_thread() should be stopped.
167  */
168 static boolean_t
vdev_trim_should_stop(vdev_t * vd)169 vdev_trim_should_stop(vdev_t *vd)
170 {
171 	return (vd->vdev_trim_exit_wanted || !vdev_writeable(vd) ||
172 	    vd->vdev_detached || vd->vdev_top->vdev_removing ||
173 	    vd->vdev_top->vdev_rz_expanding);
174 }
175 
176 /*
177  * Determines whether a vdev_autotrim_thread() should be stopped.
178  */
179 static boolean_t
vdev_autotrim_should_stop(vdev_t * tvd)180 vdev_autotrim_should_stop(vdev_t *tvd)
181 {
182 	return (tvd->vdev_autotrim_exit_wanted ||
183 	    !vdev_writeable(tvd) || tvd->vdev_removing ||
184 	    tvd->vdev_rz_expanding ||
185 	    spa_get_autotrim(tvd->vdev_spa) == SPA_AUTOTRIM_OFF);
186 }
187 
188 /*
189  * Wait for given number of kicks, return true if the wait is aborted due to
190  * vdev_autotrim_exit_wanted.
191  */
192 static boolean_t
vdev_autotrim_wait_kick(vdev_t * vd,int num_of_kick)193 vdev_autotrim_wait_kick(vdev_t *vd, int num_of_kick)
194 {
195 	mutex_enter(&vd->vdev_autotrim_lock);
196 	for (int i = 0; i < num_of_kick; i++) {
197 		if (vd->vdev_autotrim_exit_wanted)
198 			break;
199 		cv_wait_idle(&vd->vdev_autotrim_kick_cv,
200 		    &vd->vdev_autotrim_lock);
201 	}
202 	boolean_t exit_wanted = vd->vdev_autotrim_exit_wanted;
203 	mutex_exit(&vd->vdev_autotrim_lock);
204 
205 	return (exit_wanted);
206 }
207 
208 /*
209  * The sync task for updating the on-disk state of a manual TRIM.  This
210  * is scheduled by vdev_trim_change_state().
211  */
212 static void
vdev_trim_zap_update_sync(void * arg,dmu_tx_t * tx)213 vdev_trim_zap_update_sync(void *arg, dmu_tx_t *tx)
214 {
215 	/*
216 	 * We pass in the guid instead of the vdev_t since the vdev may
217 	 * have been freed prior to the sync task being processed.  This
218 	 * happens when a vdev is detached as we call spa_config_vdev_exit(),
219 	 * stop the trimming thread, schedule the sync task, and free
220 	 * the vdev. Later when the scheduled sync task is invoked, it would
221 	 * find that the vdev has been freed.
222 	 */
223 	uint64_t guid = *(uint64_t *)arg;
224 	uint64_t txg = dmu_tx_get_txg(tx);
225 	kmem_free(arg, sizeof (uint64_t));
226 
227 	vdev_t *vd = spa_lookup_by_guid(tx->tx_pool->dp_spa, guid, B_FALSE);
228 	if (vd == NULL || vd->vdev_top->vdev_removing ||
229 	    !vdev_is_concrete(vd) || vd->vdev_top->vdev_rz_expanding)
230 		return;
231 
232 	uint64_t last_offset = vd->vdev_trim_offset[txg & TXG_MASK];
233 	vd->vdev_trim_offset[txg & TXG_MASK] = 0;
234 
235 	VERIFY3U(vd->vdev_leaf_zap, !=, 0);
236 
237 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
238 
239 	if (last_offset > 0 || vd->vdev_trim_last_offset == UINT64_MAX) {
240 
241 		if (vd->vdev_trim_last_offset == UINT64_MAX)
242 			last_offset = 0;
243 
244 		vd->vdev_trim_last_offset = last_offset;
245 		VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
246 		    VDEV_LEAF_ZAP_TRIM_LAST_OFFSET,
247 		    sizeof (last_offset), 1, &last_offset, tx));
248 	}
249 
250 	if (vd->vdev_trim_action_time > 0) {
251 		uint64_t val = (uint64_t)vd->vdev_trim_action_time;
252 		VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
253 		    VDEV_LEAF_ZAP_TRIM_ACTION_TIME, sizeof (val),
254 		    1, &val, tx));
255 	}
256 
257 	if (vd->vdev_trim_rate > 0) {
258 		uint64_t rate = (uint64_t)vd->vdev_trim_rate;
259 
260 		if (rate == UINT64_MAX)
261 			rate = 0;
262 
263 		VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
264 		    VDEV_LEAF_ZAP_TRIM_RATE, sizeof (rate), 1, &rate, tx));
265 	}
266 
267 	uint64_t partial = vd->vdev_trim_partial;
268 	if (partial == UINT64_MAX)
269 		partial = 0;
270 
271 	VERIFY0(zap_update(mos, vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_PARTIAL,
272 	    sizeof (partial), 1, &partial, tx));
273 
274 	uint64_t secure = vd->vdev_trim_secure;
275 	if (secure == UINT64_MAX)
276 		secure = 0;
277 
278 	VERIFY0(zap_update(mos, vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_SECURE,
279 	    sizeof (secure), 1, &secure, tx));
280 
281 
282 	uint64_t trim_state = vd->vdev_trim_state;
283 	VERIFY0(zap_update(mos, vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_STATE,
284 	    sizeof (trim_state), 1, &trim_state, tx));
285 }
286 
287 /*
288  * Update the on-disk state of a manual TRIM.  This is called to request
289  * that a TRIM be started/suspended/canceled, or to change one of the
290  * TRIM options (partial, secure, rate).
291  */
292 static void
vdev_trim_change_state(vdev_t * vd,vdev_trim_state_t new_state,uint64_t rate,boolean_t partial,boolean_t secure)293 vdev_trim_change_state(vdev_t *vd, vdev_trim_state_t new_state,
294     uint64_t rate, boolean_t partial, boolean_t secure)
295 {
296 	ASSERT(MUTEX_HELD(&vd->vdev_trim_lock));
297 	spa_t *spa = vd->vdev_spa;
298 
299 	if (new_state == vd->vdev_trim_state)
300 		return;
301 
302 	/*
303 	 * Copy the vd's guid, this will be freed by the sync task.
304 	 */
305 	uint64_t *guid = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
306 	*guid = vd->vdev_guid;
307 
308 	/*
309 	 * If we're suspending, then preserve the original start time.
310 	 */
311 	if (vd->vdev_trim_state != VDEV_TRIM_SUSPENDED) {
312 		vd->vdev_trim_action_time = gethrestime_sec();
313 	}
314 
315 	/*
316 	 * If we're activating, then preserve the requested rate and trim
317 	 * method.  Setting the last offset and rate to UINT64_MAX is used
318 	 * as a sentinel to indicate they should be reset to default values.
319 	 */
320 	if (new_state == VDEV_TRIM_ACTIVE) {
321 		if (vd->vdev_trim_state == VDEV_TRIM_COMPLETE ||
322 		    vd->vdev_trim_state == VDEV_TRIM_CANCELED) {
323 			vd->vdev_trim_last_offset = UINT64_MAX;
324 			vd->vdev_trim_rate = UINT64_MAX;
325 			vd->vdev_trim_partial = UINT64_MAX;
326 			vd->vdev_trim_secure = UINT64_MAX;
327 		}
328 
329 		if (rate != 0)
330 			vd->vdev_trim_rate = rate;
331 
332 		if (partial != 0)
333 			vd->vdev_trim_partial = partial;
334 
335 		if (secure != 0)
336 			vd->vdev_trim_secure = secure;
337 	}
338 
339 	vdev_trim_state_t old_state = vd->vdev_trim_state;
340 	boolean_t resumed = (old_state == VDEV_TRIM_SUSPENDED);
341 	vd->vdev_trim_state = new_state;
342 
343 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
344 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
345 	dsl_sync_task_nowait(spa_get_dsl(spa), vdev_trim_zap_update_sync,
346 	    guid, tx);
347 
348 	switch (new_state) {
349 	case VDEV_TRIM_ACTIVE:
350 		spa_event_notify(spa, vd, NULL,
351 		    resumed ? ESC_ZFS_TRIM_RESUME : ESC_ZFS_TRIM_START);
352 		spa_history_log_internal(spa, "trim", tx,
353 		    "vdev=%s activated", vd->vdev_path);
354 		break;
355 	case VDEV_TRIM_SUSPENDED:
356 		spa_event_notify(spa, vd, NULL, ESC_ZFS_TRIM_SUSPEND);
357 		spa_history_log_internal(spa, "trim", tx,
358 		    "vdev=%s suspended", vd->vdev_path);
359 		break;
360 	case VDEV_TRIM_CANCELED:
361 		if (old_state == VDEV_TRIM_ACTIVE ||
362 		    old_state == VDEV_TRIM_SUSPENDED) {
363 			spa_event_notify(spa, vd, NULL, ESC_ZFS_TRIM_CANCEL);
364 			spa_history_log_internal(spa, "trim", tx,
365 			    "vdev=%s canceled", vd->vdev_path);
366 		}
367 		break;
368 	case VDEV_TRIM_COMPLETE:
369 		spa_event_notify(spa, vd, NULL, ESC_ZFS_TRIM_FINISH);
370 		spa_history_log_internal(spa, "trim", tx,
371 		    "vdev=%s complete", vd->vdev_path);
372 		break;
373 	default:
374 		panic("invalid state %llu", (unsigned long long)new_state);
375 	}
376 
377 	dmu_tx_commit(tx);
378 
379 	if (new_state != VDEV_TRIM_ACTIVE)
380 		spa_notify_waiters(spa);
381 }
382 
383 /*
384  * The zio_done_func_t done callback for each manual TRIM issued.  It is
385  * responsible for updating the TRIM stats, reissuing failed TRIM I/Os,
386  * and limiting the number of in flight TRIM I/Os.
387  */
388 static void
vdev_trim_cb(zio_t * zio)389 vdev_trim_cb(zio_t *zio)
390 {
391 	vdev_t *vd = zio->io_vd;
392 
393 	mutex_enter(&vd->vdev_trim_io_lock);
394 	if (zio->io_error == ENXIO && !vdev_writeable(vd)) {
395 		/*
396 		 * The I/O failed because the vdev was unavailable; roll the
397 		 * last offset back. (This works because spa_sync waits on
398 		 * spa_txg_zio before it runs sync tasks.)
399 		 */
400 		uint64_t *offset =
401 		    &vd->vdev_trim_offset[zio->io_txg & TXG_MASK];
402 		*offset = MIN(*offset, zio->io_offset);
403 	} else {
404 		if (zio->io_error != 0) {
405 			vd->vdev_stat.vs_trim_errors++;
406 			spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_MANUAL,
407 			    0, 0, 0, 0, 1, zio->io_orig_size);
408 		} else {
409 			spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_MANUAL,
410 			    1, zio->io_orig_size, 0, 0, 0, 0);
411 		}
412 
413 		vd->vdev_trim_bytes_done += zio->io_orig_size;
414 	}
415 
416 	ASSERT3U(vd->vdev_trim_inflight[TRIM_TYPE_MANUAL], >, 0);
417 	vd->vdev_trim_inflight[TRIM_TYPE_MANUAL]--;
418 	cv_broadcast(&vd->vdev_trim_io_cv);
419 	mutex_exit(&vd->vdev_trim_io_lock);
420 
421 	spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
422 }
423 
424 /*
425  * The zio_done_func_t done callback for each automatic TRIM issued.  It
426  * is responsible for updating the TRIM stats and limiting the number of
427  * in flight TRIM I/Os.  Automatic TRIM I/Os are best effort and are
428  * never reissued on failure.
429  */
430 static void
vdev_autotrim_cb(zio_t * zio)431 vdev_autotrim_cb(zio_t *zio)
432 {
433 	vdev_t *vd = zio->io_vd;
434 
435 	mutex_enter(&vd->vdev_trim_io_lock);
436 
437 	if (zio->io_error != 0) {
438 		vd->vdev_stat.vs_trim_errors++;
439 		spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_AUTO,
440 		    0, 0, 0, 0, 1, zio->io_orig_size);
441 	} else {
442 		spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_AUTO,
443 		    1, zio->io_orig_size, 0, 0, 0, 0);
444 	}
445 
446 	ASSERT3U(vd->vdev_trim_inflight[TRIM_TYPE_AUTO], >, 0);
447 	vd->vdev_trim_inflight[TRIM_TYPE_AUTO]--;
448 	cv_broadcast(&vd->vdev_trim_io_cv);
449 	mutex_exit(&vd->vdev_trim_io_lock);
450 
451 	spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
452 }
453 
454 /*
455  * The zio_done_func_t done callback for each TRIM issued via
456  * vdev_trim_simple(). It is responsible for updating the TRIM stats and
457  * limiting the number of in flight TRIM I/Os.  Simple TRIM I/Os are best
458  * effort and are never reissued on failure.
459  */
460 static void
vdev_trim_simple_cb(zio_t * zio)461 vdev_trim_simple_cb(zio_t *zio)
462 {
463 	vdev_t *vd = zio->io_vd;
464 
465 	mutex_enter(&vd->vdev_trim_io_lock);
466 
467 	if (zio->io_error != 0) {
468 		vd->vdev_stat.vs_trim_errors++;
469 		spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_SIMPLE,
470 		    0, 0, 0, 0, 1, zio->io_orig_size);
471 	} else {
472 		spa_iostats_trim_add(vd->vdev_spa, TRIM_TYPE_SIMPLE,
473 		    1, zio->io_orig_size, 0, 0, 0, 0);
474 	}
475 
476 	ASSERT3U(vd->vdev_trim_inflight[TRIM_TYPE_SIMPLE], >, 0);
477 	vd->vdev_trim_inflight[TRIM_TYPE_SIMPLE]--;
478 	cv_broadcast(&vd->vdev_trim_io_cv);
479 	mutex_exit(&vd->vdev_trim_io_lock);
480 
481 	spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
482 }
483 /*
484  * Returns the average trim rate in bytes/sec for the ta->trim_vdev.
485  */
486 static uint64_t
vdev_trim_calculate_rate(trim_args_t * ta)487 vdev_trim_calculate_rate(trim_args_t *ta)
488 {
489 	return (ta->trim_bytes_done * 1000 /
490 	    (NSEC2MSEC(gethrtime() - ta->trim_start_time) + 1));
491 }
492 
493 /*
494  * Issues a physical TRIM and takes care of rate limiting (bytes/sec)
495  * and number of concurrent TRIM I/Os.
496  */
497 static int
vdev_trim_range(trim_args_t * ta,uint64_t start,uint64_t size)498 vdev_trim_range(trim_args_t *ta, uint64_t start, uint64_t size)
499 {
500 	vdev_t *vd = ta->trim_vdev;
501 	spa_t *spa = vd->vdev_spa;
502 	void *cb;
503 
504 	mutex_enter(&vd->vdev_trim_io_lock);
505 
506 	/*
507 	 * Limit manual TRIM I/Os to the requested rate.  This does not
508 	 * apply to automatic TRIM since no per vdev rate can be specified.
509 	 */
510 	if (ta->trim_type == TRIM_TYPE_MANUAL) {
511 		while (vd->vdev_trim_rate != 0 && !vdev_trim_should_stop(vd) &&
512 		    vdev_trim_calculate_rate(ta) > vd->vdev_trim_rate) {
513 			cv_timedwait_idle(&vd->vdev_trim_io_cv,
514 			    &vd->vdev_trim_io_lock, ddi_get_lbolt() +
515 			    MSEC_TO_TICK(10));
516 		}
517 	}
518 	ta->trim_bytes_done += size;
519 
520 	/* Limit in flight trimming I/Os */
521 	while (vd->vdev_trim_inflight[0] + vd->vdev_trim_inflight[1] +
522 	    vd->vdev_trim_inflight[2] >= zfs_trim_queue_limit) {
523 		cv_wait(&vd->vdev_trim_io_cv, &vd->vdev_trim_io_lock);
524 	}
525 	vd->vdev_trim_inflight[ta->trim_type]++;
526 	mutex_exit(&vd->vdev_trim_io_lock);
527 
528 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
529 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
530 	uint64_t txg = dmu_tx_get_txg(tx);
531 
532 	spa_config_enter(spa, SCL_STATE_ALL, vd, RW_READER);
533 	mutex_enter(&vd->vdev_trim_lock);
534 
535 	if (ta->trim_type == TRIM_TYPE_MANUAL &&
536 	    vd->vdev_trim_offset[txg & TXG_MASK] == 0) {
537 		uint64_t *guid = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
538 		*guid = vd->vdev_guid;
539 
540 		/* This is the first write of this txg. */
541 		dsl_sync_task_nowait(spa_get_dsl(spa),
542 		    vdev_trim_zap_update_sync, guid, tx);
543 	}
544 
545 	/*
546 	 * We know the vdev_t will still be around since all consumers of
547 	 * vdev_free must stop the trimming first.
548 	 */
549 	if ((ta->trim_type == TRIM_TYPE_MANUAL &&
550 	    vdev_trim_should_stop(vd)) ||
551 	    (ta->trim_type == TRIM_TYPE_AUTO &&
552 	    vdev_autotrim_should_stop(vd->vdev_top))) {
553 		mutex_enter(&vd->vdev_trim_io_lock);
554 		vd->vdev_trim_inflight[ta->trim_type]--;
555 		mutex_exit(&vd->vdev_trim_io_lock);
556 		spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
557 		mutex_exit(&vd->vdev_trim_lock);
558 		dmu_tx_commit(tx);
559 		return (SET_ERROR(EINTR));
560 	}
561 	mutex_exit(&vd->vdev_trim_lock);
562 
563 	if (ta->trim_type == TRIM_TYPE_MANUAL)
564 		vd->vdev_trim_offset[txg & TXG_MASK] = start + size;
565 
566 	if (ta->trim_type == TRIM_TYPE_MANUAL) {
567 		cb = vdev_trim_cb;
568 	} else if (ta->trim_type == TRIM_TYPE_AUTO) {
569 		cb = vdev_autotrim_cb;
570 	} else {
571 		cb = vdev_trim_simple_cb;
572 	}
573 
574 	zio_nowait(zio_trim(spa->spa_txg_zio[txg & TXG_MASK], vd,
575 	    start, size, cb, NULL, ZIO_PRIORITY_TRIM, ZIO_FLAG_CANFAIL,
576 	    ta->trim_flags));
577 	/* vdev_trim_cb and vdev_autotrim_cb release SCL_STATE_ALL */
578 
579 	dmu_tx_commit(tx);
580 
581 	return (0);
582 }
583 
584 /*
585  * Issues TRIM I/Os for all ranges in the provided ta->trim_tree range tree.
586  * Additional parameters describing how the TRIM should be performed must
587  * be set in the trim_args structure.  See the trim_args definition for
588  * additional information.
589  */
590 static int
vdev_trim_ranges(trim_args_t * ta)591 vdev_trim_ranges(trim_args_t *ta)
592 {
593 	vdev_t *vd = ta->trim_vdev;
594 	zfs_btree_t *t = &ta->trim_tree->rt_root;
595 	zfs_btree_index_t idx;
596 	uint64_t extent_bytes_max = ta->trim_extent_bytes_max;
597 	uint64_t extent_bytes_min = ta->trim_extent_bytes_min;
598 	spa_t *spa = vd->vdev_spa;
599 	int error = 0;
600 
601 	ta->trim_start_time = gethrtime();
602 	ta->trim_bytes_done = 0;
603 
604 	for (zfs_range_seg_t *rs = zfs_btree_first(t, &idx); rs != NULL;
605 	    rs = zfs_btree_next(t, &idx, &idx)) {
606 		uint64_t size = zfs_rs_get_end(rs, ta->trim_tree) -
607 		    zfs_rs_get_start(rs, ta->trim_tree);
608 
609 		if (extent_bytes_min && size < extent_bytes_min) {
610 			spa_iostats_trim_add(spa, ta->trim_type,
611 			    0, 0, 1, size, 0, 0);
612 			continue;
613 		}
614 
615 		/* Split range into legally-sized physical chunks */
616 		uint64_t writes_required = ((size - 1) / extent_bytes_max) + 1;
617 
618 		for (uint64_t w = 0; w < writes_required; w++) {
619 			error = vdev_trim_range(ta, VDEV_LABEL_START_SIZE +
620 			    zfs_rs_get_start(rs, ta->trim_tree) +
621 			    (w *extent_bytes_max), MIN(size -
622 			    (w * extent_bytes_max), extent_bytes_max));
623 			if (error != 0) {
624 				goto done;
625 			}
626 		}
627 	}
628 
629 done:
630 	/*
631 	 * Make sure all TRIMs for this metaslab have completed before
632 	 * returning. TRIM zios have lower priority over regular or syncing
633 	 * zios, so all TRIM zios for this metaslab must complete before the
634 	 * metaslab is re-enabled. Otherwise it's possible write zios to
635 	 * this metaslab could cut ahead of still queued TRIM zios for this
636 	 * metaslab causing corruption if the ranges overlap.
637 	 */
638 	mutex_enter(&vd->vdev_trim_io_lock);
639 	while (vd->vdev_trim_inflight[0] > 0) {
640 		cv_wait(&vd->vdev_trim_io_cv, &vd->vdev_trim_io_lock);
641 	}
642 	mutex_exit(&vd->vdev_trim_io_lock);
643 
644 	return (error);
645 }
646 
647 static void
vdev_trim_xlate_last_rs_end(void * arg,zfs_range_seg64_t * physical_rs)648 vdev_trim_xlate_last_rs_end(void *arg, zfs_range_seg64_t *physical_rs)
649 {
650 	uint64_t *last_rs_end = (uint64_t *)arg;
651 
652 	if (physical_rs->rs_end > *last_rs_end)
653 		*last_rs_end = physical_rs->rs_end;
654 }
655 
656 static void
vdev_trim_xlate_progress(void * arg,zfs_range_seg64_t * physical_rs)657 vdev_trim_xlate_progress(void *arg, zfs_range_seg64_t *physical_rs)
658 {
659 	vdev_t *vd = (vdev_t *)arg;
660 
661 	uint64_t size = physical_rs->rs_end - physical_rs->rs_start;
662 	vd->vdev_trim_bytes_est += size;
663 
664 	if (vd->vdev_trim_last_offset >= physical_rs->rs_end) {
665 		vd->vdev_trim_bytes_done += size;
666 	} else if (vd->vdev_trim_last_offset > physical_rs->rs_start &&
667 	    vd->vdev_trim_last_offset <= physical_rs->rs_end) {
668 		vd->vdev_trim_bytes_done +=
669 		    vd->vdev_trim_last_offset - physical_rs->rs_start;
670 	}
671 }
672 
673 /*
674  * Calculates the completion percentage of a manual TRIM.
675  */
676 static void
vdev_trim_calculate_progress(vdev_t * vd)677 vdev_trim_calculate_progress(vdev_t *vd)
678 {
679 	ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
680 	    spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
681 	ASSERT(vd->vdev_leaf_zap != 0);
682 
683 	vd->vdev_trim_bytes_est = 0;
684 	vd->vdev_trim_bytes_done = 0;
685 
686 	for (uint64_t i = 0; i < vd->vdev_top->vdev_ms_count; i++) {
687 		metaslab_t *msp = vd->vdev_top->vdev_ms[i];
688 		mutex_enter(&msp->ms_lock);
689 
690 		uint64_t ms_free = (msp->ms_size -
691 		    metaslab_allocated_space(msp)) /
692 		    vdev_get_ndisks(vd->vdev_top);
693 
694 		/*
695 		 * Convert the metaslab range to a physical range
696 		 * on our vdev. We use this to determine if we are
697 		 * in the middle of this metaslab range.
698 		 */
699 		zfs_range_seg64_t logical_rs, physical_rs, remain_rs;
700 		logical_rs.rs_start = msp->ms_start;
701 		logical_rs.rs_end = msp->ms_start + msp->ms_size;
702 
703 		/* Metaslab space after this offset has not been trimmed. */
704 		vdev_xlate(vd, &logical_rs, &physical_rs, &remain_rs);
705 		if (vd->vdev_trim_last_offset <= physical_rs.rs_start) {
706 			vd->vdev_trim_bytes_est += ms_free;
707 			mutex_exit(&msp->ms_lock);
708 			continue;
709 		}
710 
711 		/* Metaslab space before this offset has been trimmed */
712 		uint64_t last_rs_end = physical_rs.rs_end;
713 		if (!vdev_xlate_is_empty(&remain_rs)) {
714 			vdev_xlate_walk(vd, &remain_rs,
715 			    vdev_trim_xlate_last_rs_end, &last_rs_end);
716 		}
717 
718 		if (vd->vdev_trim_last_offset > last_rs_end) {
719 			vd->vdev_trim_bytes_done += ms_free;
720 			vd->vdev_trim_bytes_est += ms_free;
721 			mutex_exit(&msp->ms_lock);
722 			continue;
723 		}
724 
725 		/*
726 		 * If we get here, we're in the middle of trimming this
727 		 * metaslab.  Load it and walk the free tree for more
728 		 * accurate progress estimation.
729 		 */
730 		VERIFY0(metaslab_load(msp));
731 
732 		zfs_range_tree_t *rt = msp->ms_allocatable;
733 		zfs_btree_t *bt = &rt->rt_root;
734 		zfs_btree_index_t idx;
735 		for (zfs_range_seg_t *rs = zfs_btree_first(bt, &idx);
736 		    rs != NULL; rs = zfs_btree_next(bt, &idx, &idx)) {
737 			logical_rs.rs_start = zfs_rs_get_start(rs, rt);
738 			logical_rs.rs_end = zfs_rs_get_end(rs, rt);
739 
740 			vdev_xlate_walk(vd, &logical_rs,
741 			    vdev_trim_xlate_progress, vd);
742 		}
743 		mutex_exit(&msp->ms_lock);
744 	}
745 }
746 
747 /*
748  * Load from disk the vdev's manual TRIM information.  This includes the
749  * state, progress, and options provided when initiating the manual TRIM.
750  */
751 static int
vdev_trim_load(vdev_t * vd)752 vdev_trim_load(vdev_t *vd)
753 {
754 	int err = 0;
755 	ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
756 	    spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
757 	ASSERT(vd->vdev_leaf_zap != 0);
758 
759 	if (vd->vdev_trim_state == VDEV_TRIM_ACTIVE ||
760 	    vd->vdev_trim_state == VDEV_TRIM_SUSPENDED) {
761 		err = zap_lookup(vd->vdev_spa->spa_meta_objset,
762 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_LAST_OFFSET,
763 		    sizeof (vd->vdev_trim_last_offset), 1,
764 		    &vd->vdev_trim_last_offset);
765 		if (err == ENOENT) {
766 			vd->vdev_trim_last_offset = 0;
767 			err = 0;
768 		}
769 
770 		if (err == 0) {
771 			err = zap_lookup(vd->vdev_spa->spa_meta_objset,
772 			    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_RATE,
773 			    sizeof (vd->vdev_trim_rate), 1,
774 			    &vd->vdev_trim_rate);
775 			if (err == ENOENT) {
776 				vd->vdev_trim_rate = 0;
777 				err = 0;
778 			}
779 		}
780 
781 		if (err == 0) {
782 			err = zap_lookup(vd->vdev_spa->spa_meta_objset,
783 			    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_PARTIAL,
784 			    sizeof (vd->vdev_trim_partial), 1,
785 			    &vd->vdev_trim_partial);
786 			if (err == ENOENT) {
787 				vd->vdev_trim_partial = 0;
788 				err = 0;
789 			}
790 		}
791 
792 		if (err == 0) {
793 			err = zap_lookup(vd->vdev_spa->spa_meta_objset,
794 			    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_SECURE,
795 			    sizeof (vd->vdev_trim_secure), 1,
796 			    &vd->vdev_trim_secure);
797 			if (err == ENOENT) {
798 				vd->vdev_trim_secure = 0;
799 				err = 0;
800 			}
801 		}
802 	}
803 
804 	vdev_trim_calculate_progress(vd);
805 
806 	return (err);
807 }
808 
809 static void
vdev_trim_xlate_range_add(void * arg,zfs_range_seg64_t * physical_rs)810 vdev_trim_xlate_range_add(void *arg, zfs_range_seg64_t *physical_rs)
811 {
812 	trim_args_t *ta = arg;
813 	vdev_t *vd = ta->trim_vdev;
814 
815 	/*
816 	 * Only a manual trim will be traversing the vdev sequentially.
817 	 * For an auto trim all valid ranges should be added.
818 	 */
819 	if (ta->trim_type == TRIM_TYPE_MANUAL) {
820 
821 		/* Only add segments that we have not visited yet */
822 		if (physical_rs->rs_end <= vd->vdev_trim_last_offset)
823 			return;
824 
825 		/* Pick up where we left off mid-range. */
826 		if (vd->vdev_trim_last_offset > physical_rs->rs_start) {
827 			ASSERT3U(physical_rs->rs_end, >,
828 			    vd->vdev_trim_last_offset);
829 			physical_rs->rs_start = vd->vdev_trim_last_offset;
830 		}
831 	}
832 
833 	ASSERT3U(physical_rs->rs_end, >, physical_rs->rs_start);
834 
835 	zfs_range_tree_add(ta->trim_tree, physical_rs->rs_start,
836 	    physical_rs->rs_end - physical_rs->rs_start);
837 }
838 
839 /*
840  * Convert the logical range into physical ranges and add them to the
841  * range tree passed in the trim_args_t.
842  */
843 static void
vdev_trim_range_add(void * arg,uint64_t start,uint64_t size)844 vdev_trim_range_add(void *arg, uint64_t start, uint64_t size)
845 {
846 	trim_args_t *ta = arg;
847 	vdev_t *vd = ta->trim_vdev;
848 	zfs_range_seg64_t logical_rs;
849 	logical_rs.rs_start = start;
850 	logical_rs.rs_end = start + size;
851 
852 	/*
853 	 * Every range to be trimmed must be part of ms_allocatable.
854 	 * When ZFS_DEBUG_TRIM is set load the metaslab to verify this
855 	 * is always the case.
856 	 */
857 	if (zfs_flags & ZFS_DEBUG_TRIM) {
858 		metaslab_t *msp = ta->trim_msp;
859 		VERIFY0(metaslab_load(msp));
860 		VERIFY3B(msp->ms_loaded, ==, B_TRUE);
861 		VERIFY(zfs_range_tree_contains(msp->ms_allocatable, start,
862 		    size));
863 	}
864 
865 	ASSERT(vd->vdev_ops->vdev_op_leaf);
866 	vdev_xlate_walk(vd, &logical_rs, vdev_trim_xlate_range_add, arg);
867 }
868 
869 /*
870  * Each manual TRIM thread is responsible for trimming the unallocated
871  * space for each leaf vdev.  This is accomplished by sequentially iterating
872  * over its top-level metaslabs and issuing TRIM I/O for the space described
873  * by its ms_allocatable.  While a metaslab is undergoing trimming it is
874  * not eligible for new allocations.
875  */
876 static __attribute__((noreturn)) void
vdev_trim_thread(void * arg)877 vdev_trim_thread(void *arg)
878 {
879 	vdev_t *vd = arg;
880 	spa_t *spa = vd->vdev_spa;
881 	trim_args_t ta;
882 	int error = 0;
883 
884 	/*
885 	 * The VDEV_LEAF_ZAP_TRIM_* entries may have been updated by
886 	 * vdev_trim().  Wait for the updated values to be reflected
887 	 * in the zap in order to start with the requested settings.
888 	 */
889 	txg_wait_synced(spa_get_dsl(vd->vdev_spa), 0);
890 
891 	ASSERT(vdev_is_concrete(vd));
892 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
893 
894 	vd->vdev_trim_last_offset = 0;
895 	vd->vdev_trim_rate = 0;
896 	vd->vdev_trim_partial = 0;
897 	vd->vdev_trim_secure = 0;
898 
899 	VERIFY0(vdev_trim_load(vd));
900 
901 	ta.trim_vdev = vd;
902 	ta.trim_extent_bytes_max = zfs_trim_extent_bytes_max;
903 	ta.trim_extent_bytes_min = zfs_trim_extent_bytes_min;
904 	ta.trim_tree = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, NULL, 0, 0);
905 	ta.trim_type = TRIM_TYPE_MANUAL;
906 	ta.trim_flags = 0;
907 
908 	/*
909 	 * When a secure TRIM has been requested infer that the intent
910 	 * is that everything must be trimmed.  Override the default
911 	 * minimum TRIM size to prevent ranges from being skipped.
912 	 */
913 	if (vd->vdev_trim_secure) {
914 		ta.trim_flags |= ZIO_TRIM_SECURE;
915 		ta.trim_extent_bytes_min = SPA_MINBLOCKSIZE;
916 	}
917 
918 	uint64_t ms_count = 0;
919 	for (uint64_t i = 0; !vd->vdev_detached &&
920 	    i < vd->vdev_top->vdev_ms_count; i++) {
921 		metaslab_t *msp = vd->vdev_top->vdev_ms[i];
922 
923 		/*
924 		 * If we've expanded the top-level vdev or it's our
925 		 * first pass, calculate our progress.
926 		 */
927 		if (vd->vdev_top->vdev_ms_count != ms_count) {
928 			vdev_trim_calculate_progress(vd);
929 			ms_count = vd->vdev_top->vdev_ms_count;
930 		}
931 
932 		spa_config_exit(spa, SCL_CONFIG, FTAG);
933 		metaslab_disable(msp);
934 		mutex_enter(&msp->ms_lock);
935 		VERIFY0(metaslab_load(msp));
936 
937 		/*
938 		 * If a partial TRIM was requested skip metaslabs which have
939 		 * never been initialized and thus have never been written.
940 		 */
941 		if (msp->ms_sm == NULL && vd->vdev_trim_partial) {
942 			mutex_exit(&msp->ms_lock);
943 			metaslab_enable(msp, B_FALSE, B_FALSE);
944 			spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
945 			vdev_trim_calculate_progress(vd);
946 			continue;
947 		}
948 
949 		ta.trim_msp = msp;
950 		zfs_range_tree_walk(msp->ms_allocatable, vdev_trim_range_add,
951 		    &ta);
952 		zfs_range_tree_vacate(msp->ms_trim, NULL, NULL);
953 		mutex_exit(&msp->ms_lock);
954 
955 		error = vdev_trim_ranges(&ta);
956 		metaslab_enable(msp, B_TRUE, B_FALSE);
957 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
958 
959 		zfs_range_tree_vacate(ta.trim_tree, NULL, NULL);
960 		if (error != 0)
961 			break;
962 	}
963 
964 	spa_config_exit(spa, SCL_CONFIG, FTAG);
965 
966 	zfs_range_tree_destroy(ta.trim_tree);
967 
968 	mutex_enter(&vd->vdev_trim_lock);
969 	if (!vd->vdev_trim_exit_wanted) {
970 		if (vdev_writeable(vd)) {
971 			vdev_trim_change_state(vd, VDEV_TRIM_COMPLETE,
972 			    vd->vdev_trim_rate, vd->vdev_trim_partial,
973 			    vd->vdev_trim_secure);
974 		} else if (vd->vdev_faulted) {
975 			vdev_trim_change_state(vd, VDEV_TRIM_CANCELED,
976 			    vd->vdev_trim_rate, vd->vdev_trim_partial,
977 			    vd->vdev_trim_secure);
978 		}
979 	}
980 	ASSERT(vd->vdev_trim_thread != NULL || vd->vdev_trim_inflight[0] == 0);
981 
982 	/*
983 	 * Drop the vdev_trim_lock while we sync out the txg since it's
984 	 * possible that a device might be trying to come online and must
985 	 * check to see if it needs to restart a trim. That thread will be
986 	 * holding the spa_config_lock which would prevent the txg_wait_synced
987 	 * from completing.
988 	 */
989 	mutex_exit(&vd->vdev_trim_lock);
990 	txg_wait_synced(spa_get_dsl(spa), 0);
991 	mutex_enter(&vd->vdev_trim_lock);
992 
993 	vd->vdev_trim_thread = NULL;
994 	cv_broadcast(&vd->vdev_trim_cv);
995 	mutex_exit(&vd->vdev_trim_lock);
996 
997 	thread_exit();
998 }
999 
1000 /*
1001  * Initiates a manual TRIM for the vdev_t.  Callers must hold vdev_trim_lock,
1002  * the vdev_t must be a leaf and cannot already be manually trimming.
1003  */
1004 void
vdev_trim(vdev_t * vd,uint64_t rate,boolean_t partial,boolean_t secure)1005 vdev_trim(vdev_t *vd, uint64_t rate, boolean_t partial, boolean_t secure)
1006 {
1007 	ASSERT(MUTEX_HELD(&vd->vdev_trim_lock));
1008 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1009 	ASSERT(vdev_is_concrete(vd));
1010 	ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1011 	ASSERT(!vd->vdev_detached);
1012 	ASSERT(!vd->vdev_trim_exit_wanted);
1013 	ASSERT(!vd->vdev_top->vdev_removing);
1014 	ASSERT(!vd->vdev_rz_expanding);
1015 
1016 	vdev_trim_change_state(vd, VDEV_TRIM_ACTIVE, rate, partial, secure);
1017 	vd->vdev_trim_thread = thread_create(NULL, 0,
1018 	    vdev_trim_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
1019 }
1020 
1021 /*
1022  * Wait for the trimming thread to be terminated (canceled or stopped).
1023  */
1024 static void
vdev_trim_stop_wait_impl(vdev_t * vd)1025 vdev_trim_stop_wait_impl(vdev_t *vd)
1026 {
1027 	ASSERT(MUTEX_HELD(&vd->vdev_trim_lock));
1028 
1029 	while (vd->vdev_trim_thread != NULL)
1030 		cv_wait(&vd->vdev_trim_cv, &vd->vdev_trim_lock);
1031 
1032 	ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1033 	vd->vdev_trim_exit_wanted = B_FALSE;
1034 }
1035 
1036 /*
1037  * Wait for vdev trim threads which were listed to cleanly exit.
1038  */
1039 void
vdev_trim_stop_wait(spa_t * spa,list_t * vd_list)1040 vdev_trim_stop_wait(spa_t *spa, list_t *vd_list)
1041 {
1042 	(void) spa;
1043 	vdev_t *vd;
1044 
1045 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1046 	    spa->spa_export_thread == curthread);
1047 
1048 	while ((vd = list_remove_head(vd_list)) != NULL) {
1049 		mutex_enter(&vd->vdev_trim_lock);
1050 		vdev_trim_stop_wait_impl(vd);
1051 		mutex_exit(&vd->vdev_trim_lock);
1052 	}
1053 }
1054 
1055 /*
1056  * Stop trimming a device, with the resultant trimming state being tgt_state.
1057  * For blocking behavior pass NULL for vd_list.  Otherwise, when a list_t is
1058  * provided the stopping vdev is inserted in to the list.  Callers are then
1059  * required to call vdev_trim_stop_wait() to block for all the trim threads
1060  * to exit.  The caller must hold vdev_trim_lock and must not be writing to
1061  * the spa config, as the trimming thread may try to enter the config as a
1062  * reader before exiting.
1063  */
1064 void
vdev_trim_stop(vdev_t * vd,vdev_trim_state_t tgt_state,list_t * vd_list)1065 vdev_trim_stop(vdev_t *vd, vdev_trim_state_t tgt_state, list_t *vd_list)
1066 {
1067 	ASSERT(!spa_config_held(vd->vdev_spa, SCL_CONFIG|SCL_STATE, RW_WRITER));
1068 	ASSERT(MUTEX_HELD(&vd->vdev_trim_lock));
1069 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1070 	ASSERT(vdev_is_concrete(vd));
1071 
1072 	/*
1073 	 * Allow cancel requests to proceed even if the trim thread has
1074 	 * stopped.
1075 	 */
1076 	if (vd->vdev_trim_thread == NULL && tgt_state != VDEV_TRIM_CANCELED)
1077 		return;
1078 
1079 	vdev_trim_change_state(vd, tgt_state, 0, 0, 0);
1080 	vd->vdev_trim_exit_wanted = B_TRUE;
1081 
1082 	if (vd_list == NULL) {
1083 		vdev_trim_stop_wait_impl(vd);
1084 	} else {
1085 		ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1086 		    vd->vdev_spa->spa_export_thread == curthread);
1087 		list_insert_tail(vd_list, vd);
1088 	}
1089 }
1090 
1091 /*
1092  * Requests that all listed vdevs stop trimming.
1093  */
1094 static void
vdev_trim_stop_all_impl(vdev_t * vd,vdev_trim_state_t tgt_state,list_t * vd_list)1095 vdev_trim_stop_all_impl(vdev_t *vd, vdev_trim_state_t tgt_state,
1096     list_t *vd_list)
1097 {
1098 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_concrete(vd)) {
1099 		mutex_enter(&vd->vdev_trim_lock);
1100 		vdev_trim_stop(vd, tgt_state, vd_list);
1101 		mutex_exit(&vd->vdev_trim_lock);
1102 		return;
1103 	}
1104 
1105 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
1106 		vdev_trim_stop_all_impl(vd->vdev_child[i], tgt_state,
1107 		    vd_list);
1108 	}
1109 }
1110 
1111 /*
1112  * Convenience function to stop trimming of a vdev tree and set all trim
1113  * thread pointers to NULL.
1114  */
1115 void
vdev_trim_stop_all(vdev_t * vd,vdev_trim_state_t tgt_state)1116 vdev_trim_stop_all(vdev_t *vd, vdev_trim_state_t tgt_state)
1117 {
1118 	spa_t *spa = vd->vdev_spa;
1119 	list_t vd_list;
1120 	vdev_t *vd_l2cache;
1121 
1122 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1123 	    spa->spa_export_thread == curthread);
1124 
1125 	list_create(&vd_list, sizeof (vdev_t),
1126 	    offsetof(vdev_t, vdev_trim_node));
1127 
1128 	vdev_trim_stop_all_impl(vd, tgt_state, &vd_list);
1129 
1130 	/*
1131 	 * Iterate over cache devices and request stop trimming the
1132 	 * whole device in case we export the pool or remove the cache
1133 	 * device prematurely.
1134 	 */
1135 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
1136 		vd_l2cache = spa->spa_l2cache.sav_vdevs[i];
1137 		vdev_trim_stop_all_impl(vd_l2cache, tgt_state, &vd_list);
1138 	}
1139 
1140 	vdev_trim_stop_wait(spa, &vd_list);
1141 
1142 	if (vd->vdev_spa->spa_sync_on) {
1143 		/* Make sure that our state has been synced to disk */
1144 		txg_wait_synced(spa_get_dsl(vd->vdev_spa), 0);
1145 	}
1146 
1147 	list_destroy(&vd_list);
1148 }
1149 
1150 /*
1151  * Conditionally restarts a manual TRIM given its on-disk state.
1152  */
1153 void
vdev_trim_restart(vdev_t * vd)1154 vdev_trim_restart(vdev_t *vd)
1155 {
1156 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1157 	    vd->vdev_spa->spa_load_thread == curthread);
1158 	ASSERT(!spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
1159 
1160 	if (vd->vdev_leaf_zap != 0) {
1161 		mutex_enter(&vd->vdev_trim_lock);
1162 		uint64_t trim_state = VDEV_TRIM_NONE;
1163 		int err = zap_lookup(vd->vdev_spa->spa_meta_objset,
1164 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_STATE,
1165 		    sizeof (trim_state), 1, &trim_state);
1166 		ASSERT(err == 0 || err == ENOENT);
1167 		vd->vdev_trim_state = trim_state;
1168 
1169 		uint64_t timestamp = 0;
1170 		err = zap_lookup(vd->vdev_spa->spa_meta_objset,
1171 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_TRIM_ACTION_TIME,
1172 		    sizeof (timestamp), 1, &timestamp);
1173 		ASSERT(err == 0 || err == ENOENT);
1174 		vd->vdev_trim_action_time = timestamp;
1175 
1176 		if ((vd->vdev_trim_state == VDEV_TRIM_SUSPENDED ||
1177 		    vd->vdev_offline) && !vd->vdev_top->vdev_rz_expanding) {
1178 			/* load progress for reporting, but don't resume */
1179 			VERIFY0(vdev_trim_load(vd));
1180 		} else if (vd->vdev_trim_state == VDEV_TRIM_ACTIVE &&
1181 		    vdev_writeable(vd) && !vd->vdev_top->vdev_removing &&
1182 		    !vd->vdev_top->vdev_rz_expanding &&
1183 		    vd->vdev_trim_thread == NULL) {
1184 			VERIFY0(vdev_trim_load(vd));
1185 			vdev_trim(vd, vd->vdev_trim_rate,
1186 			    vd->vdev_trim_partial, vd->vdev_trim_secure);
1187 		}
1188 
1189 		mutex_exit(&vd->vdev_trim_lock);
1190 	}
1191 
1192 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
1193 		vdev_trim_restart(vd->vdev_child[i]);
1194 	}
1195 }
1196 
1197 /*
1198  * Used by the automatic TRIM when ZFS_DEBUG_TRIM is set to verify that
1199  * every TRIM range is contained within ms_allocatable.
1200  */
1201 static void
vdev_trim_range_verify(void * arg,uint64_t start,uint64_t size)1202 vdev_trim_range_verify(void *arg, uint64_t start, uint64_t size)
1203 {
1204 	trim_args_t *ta = arg;
1205 	metaslab_t *msp = ta->trim_msp;
1206 
1207 	VERIFY3B(msp->ms_loaded, ==, B_TRUE);
1208 	VERIFY3U(msp->ms_disabled, >, 0);
1209 	VERIFY(zfs_range_tree_contains(msp->ms_allocatable, start, size));
1210 }
1211 
1212 /*
1213  * Each automatic TRIM thread is responsible for managing the trimming of a
1214  * top-level vdev in the pool.  No automatic TRIM state is maintained on-disk.
1215  *
1216  * N.B. This behavior is different from a manual TRIM where a thread
1217  * is created for each leaf vdev, instead of each top-level vdev.
1218  */
1219 static __attribute__((noreturn)) void
vdev_autotrim_thread(void * arg)1220 vdev_autotrim_thread(void *arg)
1221 {
1222 	vdev_t *vd = arg;
1223 	spa_t *spa = vd->vdev_spa;
1224 	int shift = 0;
1225 
1226 	mutex_enter(&vd->vdev_autotrim_lock);
1227 	ASSERT3P(vd->vdev_top, ==, vd);
1228 	ASSERT3P(vd->vdev_autotrim_thread, !=, NULL);
1229 	mutex_exit(&vd->vdev_autotrim_lock);
1230 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1231 
1232 	while (!vdev_autotrim_should_stop(vd)) {
1233 		int txgs_per_trim = MAX(zfs_trim_txg_batch, 1);
1234 		uint64_t extent_bytes_max = zfs_trim_extent_bytes_max;
1235 		uint64_t extent_bytes_min = zfs_trim_extent_bytes_min;
1236 
1237 		/*
1238 		 * All of the metaslabs are divided in to groups of size
1239 		 * num_metaslabs / zfs_trim_txg_batch.  Each of these groups
1240 		 * is composed of metaslabs which are spread evenly over the
1241 		 * device.
1242 		 *
1243 		 * For example, when zfs_trim_txg_batch = 32 (default) then
1244 		 * group 0 will contain metaslabs 0, 32, 64, ...;
1245 		 * group 1 will contain metaslabs 1, 33, 65, ...;
1246 		 * group 2 will contain metaslabs 2, 34, 66, ...; and so on.
1247 		 *
1248 		 * On each pass through the while() loop one of these groups
1249 		 * is selected.  This is accomplished by using a shift value
1250 		 * to select the starting metaslab, then striding over the
1251 		 * metaslabs using the zfs_trim_txg_batch size.  This is
1252 		 * done to accomplish two things.
1253 		 *
1254 		 * 1) By dividing the metaslabs in to groups, and making sure
1255 		 *    that each group takes a minimum of one txg to process.
1256 		 *    Then zfs_trim_txg_batch controls the minimum number of
1257 		 *    txgs which must occur before a metaslab is revisited.
1258 		 *
1259 		 * 2) Selecting non-consecutive metaslabs distributes the
1260 		 *    TRIM commands for a group evenly over the entire device.
1261 		 *    This can be advantageous for certain types of devices.
1262 		 */
1263 		for (uint64_t i = shift % txgs_per_trim; i < vd->vdev_ms_count;
1264 		    i += txgs_per_trim) {
1265 			metaslab_t *msp = vd->vdev_ms[i];
1266 			zfs_range_tree_t *trim_tree;
1267 			boolean_t issued_trim = B_FALSE;
1268 			boolean_t wait_aborted = B_FALSE;
1269 
1270 			spa_config_exit(spa, SCL_CONFIG, FTAG);
1271 			metaslab_disable(msp);
1272 			spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1273 
1274 			mutex_enter(&msp->ms_lock);
1275 
1276 			/*
1277 			 * Skip the metaslab when it has never been allocated
1278 			 * or when there are no recent frees to trim.
1279 			 */
1280 			if (msp->ms_sm == NULL ||
1281 			    zfs_range_tree_is_empty(msp->ms_trim)) {
1282 				mutex_exit(&msp->ms_lock);
1283 				metaslab_enable(msp, B_FALSE, B_FALSE);
1284 				continue;
1285 			}
1286 
1287 			/*
1288 			 * Skip the metaslab when it has already been disabled.
1289 			 * This may happen when a manual TRIM or initialize
1290 			 * operation is running concurrently.  In the case
1291 			 * of a manual TRIM, the ms_trim tree will have been
1292 			 * vacated.  Only ranges added after the manual TRIM
1293 			 * disabled the metaslab will be included in the tree.
1294 			 * These will be processed when the automatic TRIM
1295 			 * next revisits this metaslab.
1296 			 */
1297 			if (msp->ms_disabled > 1) {
1298 				mutex_exit(&msp->ms_lock);
1299 				metaslab_enable(msp, B_FALSE, B_FALSE);
1300 				continue;
1301 			}
1302 
1303 			/*
1304 			 * Allocate an empty range tree which is swapped in
1305 			 * for the existing ms_trim tree while it is processed.
1306 			 */
1307 			trim_tree = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64,
1308 			    NULL, 0, 0);
1309 			zfs_range_tree_swap(&msp->ms_trim, &trim_tree);
1310 			ASSERT(zfs_range_tree_is_empty(msp->ms_trim));
1311 
1312 			/*
1313 			 * There are two cases when constructing the per-vdev
1314 			 * trim trees for a metaslab.  If the top-level vdev
1315 			 * has no children then it is also a leaf and should
1316 			 * be trimmed.  Otherwise our children are the leaves
1317 			 * and a trim tree should be constructed for each.
1318 			 */
1319 			trim_args_t *tap;
1320 			uint64_t children = vd->vdev_children;
1321 			if (children == 0) {
1322 				children = 1;
1323 				tap = kmem_zalloc(sizeof (trim_args_t) *
1324 				    children, KM_SLEEP);
1325 				tap[0].trim_vdev = vd;
1326 			} else {
1327 				tap = kmem_zalloc(sizeof (trim_args_t) *
1328 				    children, KM_SLEEP);
1329 
1330 				for (uint64_t c = 0; c < children; c++) {
1331 					tap[c].trim_vdev = vd->vdev_child[c];
1332 				}
1333 			}
1334 
1335 			for (uint64_t c = 0; c < children; c++) {
1336 				trim_args_t *ta = &tap[c];
1337 				vdev_t *cvd = ta->trim_vdev;
1338 
1339 				ta->trim_msp = msp;
1340 				ta->trim_extent_bytes_max = extent_bytes_max;
1341 				ta->trim_extent_bytes_min = extent_bytes_min;
1342 				ta->trim_type = TRIM_TYPE_AUTO;
1343 				ta->trim_flags = 0;
1344 
1345 				if (cvd->vdev_detached ||
1346 				    !vdev_writeable(cvd) ||
1347 				    !cvd->vdev_has_trim ||
1348 				    cvd->vdev_trim_thread != NULL) {
1349 					continue;
1350 				}
1351 
1352 				/*
1353 				 * When a device has an attached hot spare, or
1354 				 * is being replaced it will not be trimmed.
1355 				 * This is done to avoid adding additional
1356 				 * stress to a potentially unhealthy device,
1357 				 * and to minimize the required rebuild time.
1358 				 */
1359 				if (!cvd->vdev_ops->vdev_op_leaf)
1360 					continue;
1361 
1362 				ta->trim_tree = zfs_range_tree_create(NULL,
1363 				    ZFS_RANGE_SEG64, NULL, 0, 0);
1364 				zfs_range_tree_walk(trim_tree,
1365 				    vdev_trim_range_add, ta);
1366 			}
1367 
1368 			mutex_exit(&msp->ms_lock);
1369 			spa_config_exit(spa, SCL_CONFIG, FTAG);
1370 
1371 			/*
1372 			 * Issue the TRIM I/Os for all ranges covered by the
1373 			 * TRIM trees.  These ranges are safe to TRIM because
1374 			 * no new allocations will be performed until the call
1375 			 * to metaslab_enabled() below.
1376 			 */
1377 			for (uint64_t c = 0; c < children; c++) {
1378 				trim_args_t *ta = &tap[c];
1379 
1380 				/*
1381 				 * Always yield to a manual TRIM if one has
1382 				 * been started for the child vdev.
1383 				 */
1384 				if (ta->trim_tree == NULL ||
1385 				    ta->trim_vdev->vdev_trim_thread != NULL) {
1386 					continue;
1387 				}
1388 
1389 				/*
1390 				 * After this point metaslab_enable() must be
1391 				 * called with the sync flag set.  This is done
1392 				 * here because vdev_trim_ranges() is allowed
1393 				 * to be interrupted (EINTR) before issuing all
1394 				 * of the required TRIM I/Os.
1395 				 */
1396 				issued_trim = B_TRUE;
1397 
1398 				int error = vdev_trim_ranges(ta);
1399 				if (error)
1400 					break;
1401 			}
1402 
1403 			/*
1404 			 * Verify every range which was trimmed is still
1405 			 * contained within the ms_allocatable tree.
1406 			 */
1407 			if (zfs_flags & ZFS_DEBUG_TRIM) {
1408 				mutex_enter(&msp->ms_lock);
1409 				VERIFY0(metaslab_load(msp));
1410 				VERIFY3P(tap[0].trim_msp, ==, msp);
1411 				zfs_range_tree_walk(trim_tree,
1412 				    vdev_trim_range_verify, &tap[0]);
1413 				mutex_exit(&msp->ms_lock);
1414 			}
1415 
1416 			zfs_range_tree_vacate(trim_tree, NULL, NULL);
1417 			zfs_range_tree_destroy(trim_tree);
1418 
1419 			/*
1420 			 * Wait for couples of kicks, to ensure the trim io is
1421 			 * synced. If the wait is aborted due to
1422 			 * vdev_autotrim_exit_wanted, we need to signal
1423 			 * metaslab_enable() to wait for sync.
1424 			 */
1425 			if (issued_trim) {
1426 				wait_aborted = vdev_autotrim_wait_kick(vd,
1427 				    TXG_CONCURRENT_STATES + TXG_DEFER_SIZE);
1428 			}
1429 
1430 			metaslab_enable(msp, wait_aborted, B_FALSE);
1431 			spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1432 
1433 			for (uint64_t c = 0; c < children; c++) {
1434 				trim_args_t *ta = &tap[c];
1435 
1436 				if (ta->trim_tree == NULL)
1437 					continue;
1438 
1439 				zfs_range_tree_vacate(ta->trim_tree, NULL,
1440 				    NULL);
1441 				zfs_range_tree_destroy(ta->trim_tree);
1442 			}
1443 
1444 			kmem_free(tap, sizeof (trim_args_t) * children);
1445 
1446 			if (vdev_autotrim_should_stop(vd))
1447 				break;
1448 		}
1449 
1450 		spa_config_exit(spa, SCL_CONFIG, FTAG);
1451 
1452 		vdev_autotrim_wait_kick(vd, 1);
1453 
1454 		shift++;
1455 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1456 	}
1457 
1458 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
1459 		vdev_t *cvd = vd->vdev_child[c];
1460 		mutex_enter(&cvd->vdev_trim_io_lock);
1461 
1462 		while (cvd->vdev_trim_inflight[1] > 0) {
1463 			cv_wait(&cvd->vdev_trim_io_cv,
1464 			    &cvd->vdev_trim_io_lock);
1465 		}
1466 		mutex_exit(&cvd->vdev_trim_io_lock);
1467 	}
1468 
1469 	spa_config_exit(spa, SCL_CONFIG, FTAG);
1470 
1471 	/*
1472 	 * When exiting because the autotrim property was set to off, then
1473 	 * abandon any unprocessed ms_trim ranges to reclaim the memory.
1474 	 */
1475 	if (spa_get_autotrim(spa) == SPA_AUTOTRIM_OFF) {
1476 		for (uint64_t i = 0; i < vd->vdev_ms_count; i++) {
1477 			metaslab_t *msp = vd->vdev_ms[i];
1478 
1479 			mutex_enter(&msp->ms_lock);
1480 			zfs_range_tree_vacate(msp->ms_trim, NULL, NULL);
1481 			mutex_exit(&msp->ms_lock);
1482 		}
1483 	}
1484 
1485 	mutex_enter(&vd->vdev_autotrim_lock);
1486 	ASSERT(vd->vdev_autotrim_thread != NULL);
1487 	vd->vdev_autotrim_thread = NULL;
1488 	cv_broadcast(&vd->vdev_autotrim_cv);
1489 	mutex_exit(&vd->vdev_autotrim_lock);
1490 
1491 	thread_exit();
1492 }
1493 
1494 /*
1495  * Starts an autotrim thread, if needed, for each top-level vdev which can be
1496  * trimmed.  A top-level vdev which has been evacuated will never be trimmed.
1497  */
1498 void
vdev_autotrim(spa_t * spa)1499 vdev_autotrim(spa_t *spa)
1500 {
1501 	vdev_t *root_vd = spa->spa_root_vdev;
1502 
1503 	for (uint64_t i = 0; i < root_vd->vdev_children; i++) {
1504 		vdev_t *tvd = root_vd->vdev_child[i];
1505 
1506 		mutex_enter(&tvd->vdev_autotrim_lock);
1507 		if (vdev_writeable(tvd) && !tvd->vdev_removing &&
1508 		    tvd->vdev_autotrim_thread == NULL &&
1509 		    !tvd->vdev_rz_expanding) {
1510 			ASSERT3P(tvd->vdev_top, ==, tvd);
1511 
1512 			tvd->vdev_autotrim_thread = thread_create(NULL, 0,
1513 			    vdev_autotrim_thread, tvd, 0, &p0, TS_RUN,
1514 			    maxclsyspri);
1515 			ASSERT(tvd->vdev_autotrim_thread != NULL);
1516 		}
1517 		mutex_exit(&tvd->vdev_autotrim_lock);
1518 	}
1519 }
1520 
1521 /*
1522  * Wait for the vdev_autotrim_thread associated with the passed top-level
1523  * vdev to be terminated (canceled or stopped).
1524  */
1525 void
vdev_autotrim_stop_wait(vdev_t * tvd)1526 vdev_autotrim_stop_wait(vdev_t *tvd)
1527 {
1528 	mutex_enter(&tvd->vdev_autotrim_lock);
1529 	if (tvd->vdev_autotrim_thread != NULL) {
1530 		tvd->vdev_autotrim_exit_wanted = B_TRUE;
1531 		cv_broadcast(&tvd->vdev_autotrim_kick_cv);
1532 		cv_wait(&tvd->vdev_autotrim_cv,
1533 		    &tvd->vdev_autotrim_lock);
1534 
1535 		ASSERT3P(tvd->vdev_autotrim_thread, ==, NULL);
1536 		tvd->vdev_autotrim_exit_wanted = B_FALSE;
1537 	}
1538 	mutex_exit(&tvd->vdev_autotrim_lock);
1539 }
1540 
1541 void
vdev_autotrim_kick(spa_t * spa)1542 vdev_autotrim_kick(spa_t *spa)
1543 {
1544 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1545 
1546 	vdev_t *root_vd = spa->spa_root_vdev;
1547 	vdev_t *tvd;
1548 
1549 	for (uint64_t i = 0; i < root_vd->vdev_children; i++) {
1550 		tvd = root_vd->vdev_child[i];
1551 
1552 		mutex_enter(&tvd->vdev_autotrim_lock);
1553 		if (tvd->vdev_autotrim_thread != NULL)
1554 			cv_broadcast(&tvd->vdev_autotrim_kick_cv);
1555 		mutex_exit(&tvd->vdev_autotrim_lock);
1556 	}
1557 }
1558 
1559 /*
1560  * Wait for all of the vdev_autotrim_thread associated with the pool to
1561  * be terminated (canceled or stopped).
1562  */
1563 void
vdev_autotrim_stop_all(spa_t * spa)1564 vdev_autotrim_stop_all(spa_t *spa)
1565 {
1566 	vdev_t *root_vd = spa->spa_root_vdev;
1567 
1568 	for (uint64_t i = 0; i < root_vd->vdev_children; i++)
1569 		vdev_autotrim_stop_wait(root_vd->vdev_child[i]);
1570 }
1571 
1572 /*
1573  * Conditionally restart all of the vdev_autotrim_thread's for the pool.
1574  */
1575 void
vdev_autotrim_restart(spa_t * spa)1576 vdev_autotrim_restart(spa_t *spa)
1577 {
1578 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1579 	    spa->spa_load_thread == curthread);
1580 	if (spa->spa_autotrim)
1581 		vdev_autotrim(spa);
1582 }
1583 
1584 static __attribute__((noreturn)) void
vdev_trim_l2arc_thread(void * arg)1585 vdev_trim_l2arc_thread(void *arg)
1586 {
1587 	vdev_t		*vd = arg;
1588 	spa_t		*spa = vd->vdev_spa;
1589 	l2arc_dev_t	*dev = l2arc_vdev_get(vd);
1590 	trim_args_t	ta = {0};
1591 	zfs_range_seg64_t 	physical_rs;
1592 
1593 	ASSERT(vdev_is_concrete(vd));
1594 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1595 
1596 	vd->vdev_trim_last_offset = 0;
1597 	vd->vdev_trim_rate = 0;
1598 	vd->vdev_trim_partial = 0;
1599 	vd->vdev_trim_secure = 0;
1600 
1601 	ta.trim_vdev = vd;
1602 	ta.trim_tree = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, NULL, 0, 0);
1603 	ta.trim_type = TRIM_TYPE_MANUAL;
1604 	ta.trim_extent_bytes_max = zfs_trim_extent_bytes_max;
1605 	ta.trim_extent_bytes_min = SPA_MINBLOCKSIZE;
1606 	ta.trim_flags = 0;
1607 
1608 	physical_rs.rs_start = vd->vdev_trim_bytes_done = 0;
1609 	physical_rs.rs_end = vd->vdev_trim_bytes_est =
1610 	    vdev_get_min_asize(vd);
1611 
1612 	zfs_range_tree_add(ta.trim_tree, physical_rs.rs_start,
1613 	    physical_rs.rs_end - physical_rs.rs_start);
1614 
1615 	mutex_enter(&vd->vdev_trim_lock);
1616 	vdev_trim_change_state(vd, VDEV_TRIM_ACTIVE, 0, 0, 0);
1617 	mutex_exit(&vd->vdev_trim_lock);
1618 
1619 	(void) vdev_trim_ranges(&ta);
1620 
1621 	spa_config_exit(spa, SCL_CONFIG, FTAG);
1622 	mutex_enter(&vd->vdev_trim_io_lock);
1623 	while (vd->vdev_trim_inflight[TRIM_TYPE_MANUAL] > 0) {
1624 		cv_wait(&vd->vdev_trim_io_cv, &vd->vdev_trim_io_lock);
1625 	}
1626 	mutex_exit(&vd->vdev_trim_io_lock);
1627 
1628 	zfs_range_tree_vacate(ta.trim_tree, NULL, NULL);
1629 	zfs_range_tree_destroy(ta.trim_tree);
1630 
1631 	mutex_enter(&vd->vdev_trim_lock);
1632 	if (!vd->vdev_trim_exit_wanted && vdev_writeable(vd)) {
1633 		vdev_trim_change_state(vd, VDEV_TRIM_COMPLETE,
1634 		    vd->vdev_trim_rate, vd->vdev_trim_partial,
1635 		    vd->vdev_trim_secure);
1636 	}
1637 	ASSERT(vd->vdev_trim_thread != NULL ||
1638 	    vd->vdev_trim_inflight[TRIM_TYPE_MANUAL] == 0);
1639 
1640 	/*
1641 	 * Drop the vdev_trim_lock while we sync out the txg since it's
1642 	 * possible that a device might be trying to come online and
1643 	 * must check to see if it needs to restart a trim. That thread
1644 	 * will be holding the spa_config_lock which would prevent the
1645 	 * txg_wait_synced from completing. Same strategy as in
1646 	 * vdev_trim_thread().
1647 	 */
1648 	mutex_exit(&vd->vdev_trim_lock);
1649 	txg_wait_synced(spa_get_dsl(vd->vdev_spa), 0);
1650 	mutex_enter(&vd->vdev_trim_lock);
1651 
1652 	/*
1653 	 * Update the header of the cache device here, before
1654 	 * broadcasting vdev_trim_cv which may lead to the removal
1655 	 * of the device. The same applies for setting l2ad_trim_all to
1656 	 * false.
1657 	 */
1658 	spa_config_enter(vd->vdev_spa, SCL_L2ARC, vd,
1659 	    RW_READER);
1660 	memset(dev->l2ad_dev_hdr, 0, dev->l2ad_dev_hdr_asize);
1661 	l2arc_dev_hdr_update(dev);
1662 	spa_config_exit(vd->vdev_spa, SCL_L2ARC, vd);
1663 
1664 	vd->vdev_trim_thread = NULL;
1665 	if (vd->vdev_trim_state == VDEV_TRIM_COMPLETE)
1666 		dev->l2ad_trim_all = B_FALSE;
1667 
1668 	cv_broadcast(&vd->vdev_trim_cv);
1669 	mutex_exit(&vd->vdev_trim_lock);
1670 
1671 	thread_exit();
1672 }
1673 
1674 /*
1675  * Punches out TRIM threads for the L2ARC devices in a spa and assigns them
1676  * to vd->vdev_trim_thread variable. This facilitates the management of
1677  * trimming the whole cache device using TRIM_TYPE_MANUAL upon addition
1678  * to a pool or pool creation or when the header of the device is invalid.
1679  */
1680 void
vdev_trim_l2arc(spa_t * spa)1681 vdev_trim_l2arc(spa_t *spa)
1682 {
1683 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1684 
1685 	/*
1686 	 * Locate the spa's l2arc devices and kick off TRIM threads.
1687 	 */
1688 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
1689 		vdev_t *vd = spa->spa_l2cache.sav_vdevs[i];
1690 		l2arc_dev_t *dev = l2arc_vdev_get(vd);
1691 
1692 		if (dev == NULL || !dev->l2ad_trim_all) {
1693 			/*
1694 			 * Don't attempt TRIM if the vdev is UNAVAIL or if the
1695 			 * cache device was not marked for whole device TRIM
1696 			 * (ie l2arc_trim_ahead = 0, or the L2ARC device header
1697 			 * is valid with trim_state = VDEV_TRIM_COMPLETE and
1698 			 * l2ad_log_entries > 0).
1699 			 */
1700 			continue;
1701 		}
1702 
1703 		mutex_enter(&vd->vdev_trim_lock);
1704 		ASSERT(vd->vdev_ops->vdev_op_leaf);
1705 		ASSERT(vdev_is_concrete(vd));
1706 		ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1707 		ASSERT(!vd->vdev_detached);
1708 		ASSERT(!vd->vdev_trim_exit_wanted);
1709 		ASSERT(!vd->vdev_top->vdev_removing);
1710 		vdev_trim_change_state(vd, VDEV_TRIM_ACTIVE, 0, 0, 0);
1711 		vd->vdev_trim_thread = thread_create(NULL, 0,
1712 		    vdev_trim_l2arc_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
1713 		mutex_exit(&vd->vdev_trim_lock);
1714 	}
1715 }
1716 
1717 /*
1718  * A wrapper which calls vdev_trim_ranges(). It is intended to be called
1719  * on leaf vdevs.
1720  */
1721 int
vdev_trim_simple(vdev_t * vd,uint64_t start,uint64_t size)1722 vdev_trim_simple(vdev_t *vd, uint64_t start, uint64_t size)
1723 {
1724 	trim_args_t ta = {0};
1725 	zfs_range_seg64_t physical_rs;
1726 	int error;
1727 	physical_rs.rs_start = start;
1728 	physical_rs.rs_end = start + size;
1729 
1730 	ASSERT(vdev_is_concrete(vd));
1731 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1732 	ASSERT(!vd->vdev_detached);
1733 	ASSERT(!vd->vdev_top->vdev_removing);
1734 	ASSERT(!vd->vdev_top->vdev_rz_expanding);
1735 
1736 	ta.trim_vdev = vd;
1737 	ta.trim_tree = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, NULL, 0, 0);
1738 	ta.trim_type = TRIM_TYPE_SIMPLE;
1739 	ta.trim_extent_bytes_max = zfs_trim_extent_bytes_max;
1740 	ta.trim_extent_bytes_min = SPA_MINBLOCKSIZE;
1741 	ta.trim_flags = 0;
1742 
1743 	ASSERT3U(physical_rs.rs_end, >=, physical_rs.rs_start);
1744 
1745 	if (physical_rs.rs_end > physical_rs.rs_start) {
1746 		zfs_range_tree_add(ta.trim_tree, physical_rs.rs_start,
1747 		    physical_rs.rs_end - physical_rs.rs_start);
1748 	} else {
1749 		ASSERT3U(physical_rs.rs_end, ==, physical_rs.rs_start);
1750 	}
1751 
1752 	error = vdev_trim_ranges(&ta);
1753 
1754 	mutex_enter(&vd->vdev_trim_io_lock);
1755 	while (vd->vdev_trim_inflight[TRIM_TYPE_SIMPLE] > 0) {
1756 		cv_wait(&vd->vdev_trim_io_cv, &vd->vdev_trim_io_lock);
1757 	}
1758 	mutex_exit(&vd->vdev_trim_io_lock);
1759 
1760 	zfs_range_tree_vacate(ta.trim_tree, NULL, NULL);
1761 	zfs_range_tree_destroy(ta.trim_tree);
1762 
1763 	return (error);
1764 }
1765 
1766 EXPORT_SYMBOL(vdev_trim);
1767 EXPORT_SYMBOL(vdev_trim_stop);
1768 EXPORT_SYMBOL(vdev_trim_stop_all);
1769 EXPORT_SYMBOL(vdev_trim_stop_wait);
1770 EXPORT_SYMBOL(vdev_trim_restart);
1771 EXPORT_SYMBOL(vdev_autotrim);
1772 EXPORT_SYMBOL(vdev_autotrim_stop_all);
1773 EXPORT_SYMBOL(vdev_autotrim_stop_wait);
1774 EXPORT_SYMBOL(vdev_autotrim_restart);
1775 EXPORT_SYMBOL(vdev_trim_l2arc);
1776 EXPORT_SYMBOL(vdev_trim_simple);
1777 
1778 ZFS_MODULE_PARAM(zfs_trim, zfs_trim_, extent_bytes_max, UINT, ZMOD_RW,
1779 	"Max size of TRIM commands, larger will be split");
1780 
1781 ZFS_MODULE_PARAM(zfs_trim, zfs_trim_, extent_bytes_min, UINT, ZMOD_RW,
1782 	"Min size of TRIM commands, smaller will be skipped");
1783 
1784 ZFS_MODULE_PARAM(zfs_trim, zfs_trim_, metaslab_skip, UINT, ZMOD_RW,
1785 	"Skip metaslabs which have never been initialized");
1786 
1787 ZFS_MODULE_PARAM(zfs_trim, zfs_trim_, txg_batch, UINT, ZMOD_RW,
1788 	"Min number of txgs to aggregate frees before issuing TRIM");
1789 
1790 ZFS_MODULE_PARAM(zfs_trim, zfs_trim_, queue_limit, UINT, ZMOD_RW,
1791 	"Max queued TRIMs outstanding per leaf vdev");
1792