xref: /netbsd-src/external/cddl/osnet/dist/uts/common/fs/zfs/trim_map.c (revision ba2539a9805a0544ff82c0003cc02fe1eee5603d)
13227e6cfSchs /*
23227e6cfSchs  * CDDL HEADER START
33227e6cfSchs  *
43227e6cfSchs  * The contents of this file are subject to the terms of the
53227e6cfSchs  * Common Development and Distribution License (the "License").
63227e6cfSchs  * You may not use this file except in compliance with the License.
73227e6cfSchs  *
83227e6cfSchs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93227e6cfSchs  * or http://www.opensolaris.org/os/licensing.
103227e6cfSchs  * See the License for the specific language governing permissions
113227e6cfSchs  * and limitations under the License.
123227e6cfSchs  *
133227e6cfSchs  * When distributing Covered Code, include this CDDL HEADER in each
143227e6cfSchs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153227e6cfSchs  * If applicable, add the following below this CDDL HEADER, with the
163227e6cfSchs  * fields enclosed by brackets "[]" replaced with your own identifying
173227e6cfSchs  * information: Portions Copyright [yyyy] [name of copyright owner]
183227e6cfSchs  *
193227e6cfSchs  * CDDL HEADER END
203227e6cfSchs  */
213227e6cfSchs /*
223227e6cfSchs  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
233227e6cfSchs  * All rights reserved.
243227e6cfSchs  */
253227e6cfSchs 
263227e6cfSchs #include <sys/zfs_context.h>
273227e6cfSchs #include <sys/spa_impl.h>
283227e6cfSchs #include <sys/vdev_impl.h>
293227e6cfSchs #include <sys/trim_map.h>
303227e6cfSchs #include <sys/time.h>
313227e6cfSchs 
323227e6cfSchs /*
333227e6cfSchs  * Calculate the zio end, upgrading based on ashift which would be
343227e6cfSchs  * done by zio_vdev_io_start.
353227e6cfSchs  *
363227e6cfSchs  * This makes free range consolidation much more effective
373227e6cfSchs  * than it would otherwise be as well as ensuring that entire
383227e6cfSchs  * blocks are invalidated by writes.
393227e6cfSchs  */
403227e6cfSchs #define	TRIM_ZIO_END(vd, offset, size)	(offset +		\
413227e6cfSchs  	P2ROUNDUP(size, 1ULL << vd->vdev_top->vdev_ashift))
423227e6cfSchs 
433227e6cfSchs /* Maximal segment size for ATA TRIM. */
443227e6cfSchs #define TRIM_MAP_SIZE_FACTOR	(512 << 16)
453227e6cfSchs 
463227e6cfSchs #define TRIM_MAP_SEGS(size)	(1 + (size) / TRIM_MAP_SIZE_FACTOR)
473227e6cfSchs 
483227e6cfSchs #define TRIM_MAP_ADD(tm, ts)	do {				\
493227e6cfSchs 	list_insert_tail(&(tm)->tm_head, (ts));			\
503227e6cfSchs 	(tm)->tm_pending += TRIM_MAP_SEGS((ts)->ts_end - (ts)->ts_start); \
513227e6cfSchs } while (0)
523227e6cfSchs 
533227e6cfSchs #define TRIM_MAP_REM(tm, ts)	do {				\
543227e6cfSchs 	list_remove(&(tm)->tm_head, (ts));			\
553227e6cfSchs 	(tm)->tm_pending -= TRIM_MAP_SEGS((ts)->ts_end - (ts)->ts_start); \
563227e6cfSchs } while (0)
573227e6cfSchs 
583227e6cfSchs typedef struct trim_map {
593227e6cfSchs 	list_t		tm_head;		/* List of segments sorted by txg. */
603227e6cfSchs 	avl_tree_t	tm_queued_frees;	/* AVL tree of segments waiting for TRIM. */
613227e6cfSchs 	avl_tree_t	tm_inflight_frees;	/* AVL tree of in-flight TRIMs. */
623227e6cfSchs 	avl_tree_t	tm_inflight_writes;	/* AVL tree of in-flight writes. */
633227e6cfSchs 	list_t		tm_pending_writes;	/* Writes blocked on in-flight frees. */
643227e6cfSchs 	kmutex_t	tm_lock;
653227e6cfSchs 	uint64_t	tm_pending;		/* Count of pending TRIMs. */
663227e6cfSchs } trim_map_t;
673227e6cfSchs 
683227e6cfSchs typedef struct trim_seg {
693227e6cfSchs 	avl_node_t	ts_node;	/* AVL node. */
703227e6cfSchs 	list_node_t	ts_next;	/* List element. */
713227e6cfSchs 	uint64_t	ts_start;	/* Starting offset of this segment. */
723227e6cfSchs 	uint64_t	ts_end;		/* Ending offset (non-inclusive). */
733227e6cfSchs 	uint64_t	ts_txg;		/* Segment creation txg. */
743227e6cfSchs 	hrtime_t	ts_time;	/* Segment creation time. */
753227e6cfSchs } trim_seg_t;
763227e6cfSchs 
773227e6cfSchs extern boolean_t zfs_trim_enabled;
783227e6cfSchs 
793227e6cfSchs static u_int trim_txg_delay = 32;	/* Keep deleted data up to 32 TXG */
803227e6cfSchs static u_int trim_timeout = 30;		/* Keep deleted data up to 30s */
813227e6cfSchs static u_int trim_max_interval = 1;	/* 1s delays between TRIMs */
823227e6cfSchs static u_int trim_vdev_max_pending = 10000; /* Keep up to 10K segments */
833227e6cfSchs 
843227e6cfSchs SYSCTL_DECL(_vfs_zfs);
853227e6cfSchs SYSCTL_NODE(_vfs_zfs, OID_AUTO, trim, CTLFLAG_RD, 0, "ZFS TRIM");
863227e6cfSchs 
873227e6cfSchs SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, txg_delay, CTLFLAG_RWTUN, &trim_txg_delay,
883227e6cfSchs     0, "Delay TRIMs by up to this many TXGs");
893227e6cfSchs SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, timeout, CTLFLAG_RWTUN, &trim_timeout, 0,
903227e6cfSchs     "Delay TRIMs by up to this many seconds");
913227e6cfSchs SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, max_interval, CTLFLAG_RWTUN,
923227e6cfSchs     &trim_max_interval, 0,
933227e6cfSchs     "Maximum interval between TRIM queue processing (seconds)");
943227e6cfSchs 
953227e6cfSchs SYSCTL_DECL(_vfs_zfs_vdev);
963227e6cfSchs SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, trim_max_pending, CTLFLAG_RWTUN,
973227e6cfSchs     &trim_vdev_max_pending, 0,
983227e6cfSchs     "Maximum pending TRIM segments for a vdev");
993227e6cfSchs 
1003227e6cfSchs static void trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd);
1013227e6cfSchs 
1023227e6cfSchs static int
trim_map_seg_compare(const void * x1,const void * x2)1033227e6cfSchs trim_map_seg_compare(const void *x1, const void *x2)
1043227e6cfSchs {
1053227e6cfSchs 	const trim_seg_t *s1 = x1;
1063227e6cfSchs 	const trim_seg_t *s2 = x2;
1073227e6cfSchs 
1083227e6cfSchs 	if (s1->ts_start < s2->ts_start) {
1093227e6cfSchs 		if (s1->ts_end > s2->ts_start)
1103227e6cfSchs 			return (0);
1113227e6cfSchs 		return (-1);
1123227e6cfSchs 	}
1133227e6cfSchs 	if (s1->ts_start > s2->ts_start) {
1143227e6cfSchs 		if (s1->ts_start < s2->ts_end)
1153227e6cfSchs 			return (0);
1163227e6cfSchs 		return (1);
1173227e6cfSchs 	}
1183227e6cfSchs 	return (0);
1193227e6cfSchs }
1203227e6cfSchs 
1213227e6cfSchs static int
trim_map_zio_compare(const void * x1,const void * x2)1223227e6cfSchs trim_map_zio_compare(const void *x1, const void *x2)
1233227e6cfSchs {
1243227e6cfSchs 	const zio_t *z1 = x1;
1253227e6cfSchs 	const zio_t *z2 = x2;
1263227e6cfSchs 
1273227e6cfSchs 	if (z1->io_offset < z2->io_offset) {
1283227e6cfSchs 		if (z1->io_offset + z1->io_size > z2->io_offset)
1293227e6cfSchs 			return (0);
1303227e6cfSchs 		return (-1);
1313227e6cfSchs 	}
1323227e6cfSchs 	if (z1->io_offset > z2->io_offset) {
1333227e6cfSchs 		if (z1->io_offset < z2->io_offset + z2->io_size)
1343227e6cfSchs 			return (0);
1353227e6cfSchs 		return (1);
1363227e6cfSchs 	}
1373227e6cfSchs 	return (0);
1383227e6cfSchs }
1393227e6cfSchs 
1403227e6cfSchs void
trim_map_create(vdev_t * vd)1413227e6cfSchs trim_map_create(vdev_t *vd)
1423227e6cfSchs {
1433227e6cfSchs 	trim_map_t *tm;
1443227e6cfSchs 
1453227e6cfSchs 	ASSERT(zfs_trim_enabled && !vd->vdev_notrim &&
1463227e6cfSchs 		vd->vdev_ops->vdev_op_leaf);
1473227e6cfSchs 
1483227e6cfSchs 	tm = kmem_zalloc(sizeof (*tm), KM_SLEEP);
1493227e6cfSchs 	mutex_init(&tm->tm_lock, NULL, MUTEX_DEFAULT, NULL);
1503227e6cfSchs 	list_create(&tm->tm_head, sizeof (trim_seg_t),
1513227e6cfSchs 	    offsetof(trim_seg_t, ts_next));
1523227e6cfSchs 	list_create(&tm->tm_pending_writes, sizeof (zio_t),
1533227e6cfSchs 	    offsetof(zio_t, io_trim_link));
1543227e6cfSchs 	avl_create(&tm->tm_queued_frees, trim_map_seg_compare,
1553227e6cfSchs 	    sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
1563227e6cfSchs 	avl_create(&tm->tm_inflight_frees, trim_map_seg_compare,
1573227e6cfSchs 	    sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
1583227e6cfSchs 	avl_create(&tm->tm_inflight_writes, trim_map_zio_compare,
1593227e6cfSchs 	    sizeof (zio_t), offsetof(zio_t, io_trim_node));
1603227e6cfSchs 	vd->vdev_trimmap = tm;
1613227e6cfSchs }
1623227e6cfSchs 
1633227e6cfSchs void
trim_map_destroy(vdev_t * vd)1643227e6cfSchs trim_map_destroy(vdev_t *vd)
1653227e6cfSchs {
1663227e6cfSchs 	trim_map_t *tm;
1673227e6cfSchs 	trim_seg_t *ts;
1683227e6cfSchs 
1693227e6cfSchs 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1703227e6cfSchs 
1713227e6cfSchs 	if (!zfs_trim_enabled)
1723227e6cfSchs 		return;
1733227e6cfSchs 
1743227e6cfSchs 	tm = vd->vdev_trimmap;
1753227e6cfSchs 	if (tm == NULL)
1763227e6cfSchs 		return;
1773227e6cfSchs 
1783227e6cfSchs 	/*
1793227e6cfSchs 	 * We may have been called before trim_map_vdev_commit_done()
1803227e6cfSchs 	 * had a chance to run, so do it now to prune the remaining
1813227e6cfSchs 	 * inflight frees.
1823227e6cfSchs 	 */
1833227e6cfSchs 	trim_map_vdev_commit_done(vd->vdev_spa, vd);
1843227e6cfSchs 
1853227e6cfSchs 	mutex_enter(&tm->tm_lock);
1863227e6cfSchs 	while ((ts = list_head(&tm->tm_head)) != NULL) {
1873227e6cfSchs 		avl_remove(&tm->tm_queued_frees, ts);
1883227e6cfSchs 		TRIM_MAP_REM(tm, ts);
1893227e6cfSchs 		kmem_free(ts, sizeof (*ts));
1903227e6cfSchs 	}
1913227e6cfSchs 	mutex_exit(&tm->tm_lock);
1923227e6cfSchs 
1933227e6cfSchs 	avl_destroy(&tm->tm_queued_frees);
1943227e6cfSchs 	avl_destroy(&tm->tm_inflight_frees);
1953227e6cfSchs 	avl_destroy(&tm->tm_inflight_writes);
1963227e6cfSchs 	list_destroy(&tm->tm_pending_writes);
1973227e6cfSchs 	list_destroy(&tm->tm_head);
1983227e6cfSchs 	mutex_destroy(&tm->tm_lock);
1993227e6cfSchs 	kmem_free(tm, sizeof (*tm));
2003227e6cfSchs 	vd->vdev_trimmap = NULL;
2013227e6cfSchs }
2023227e6cfSchs 
2033227e6cfSchs static void
trim_map_segment_add(trim_map_t * tm,uint64_t start,uint64_t end,uint64_t txg)2043227e6cfSchs trim_map_segment_add(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
2053227e6cfSchs {
2063227e6cfSchs 	avl_index_t where;
2073227e6cfSchs 	trim_seg_t tsearch, *ts_before, *ts_after, *ts;
2083227e6cfSchs 	boolean_t merge_before, merge_after;
2093227e6cfSchs 	hrtime_t time;
2103227e6cfSchs 
2113227e6cfSchs 	ASSERT(MUTEX_HELD(&tm->tm_lock));
2123227e6cfSchs 	VERIFY(start < end);
2133227e6cfSchs 
2143227e6cfSchs 	time = gethrtime();
2153227e6cfSchs 	tsearch.ts_start = start;
2163227e6cfSchs 	tsearch.ts_end = end;
2173227e6cfSchs 
2183227e6cfSchs 	ts = avl_find(&tm->tm_queued_frees, &tsearch, &where);
2193227e6cfSchs 	if (ts != NULL) {
2203227e6cfSchs 		if (start < ts->ts_start)
2213227e6cfSchs 			trim_map_segment_add(tm, start, ts->ts_start, txg);
2223227e6cfSchs 		if (end > ts->ts_end)
2233227e6cfSchs 			trim_map_segment_add(tm, ts->ts_end, end, txg);
2243227e6cfSchs 		return;
2253227e6cfSchs 	}
2263227e6cfSchs 
2273227e6cfSchs 	ts_before = avl_nearest(&tm->tm_queued_frees, where, AVL_BEFORE);
2283227e6cfSchs 	ts_after = avl_nearest(&tm->tm_queued_frees, where, AVL_AFTER);
2293227e6cfSchs 
2303227e6cfSchs 	merge_before = (ts_before != NULL && ts_before->ts_end == start);
2313227e6cfSchs 	merge_after = (ts_after != NULL && ts_after->ts_start == end);
2323227e6cfSchs 
2333227e6cfSchs 	if (merge_before && merge_after) {
2343227e6cfSchs 		avl_remove(&tm->tm_queued_frees, ts_before);
2353227e6cfSchs 		TRIM_MAP_REM(tm, ts_before);
2363227e6cfSchs 		TRIM_MAP_REM(tm, ts_after);
2373227e6cfSchs 		ts_after->ts_start = ts_before->ts_start;
2383227e6cfSchs 		ts_after->ts_txg = txg;
2393227e6cfSchs 		ts_after->ts_time = time;
2403227e6cfSchs 		TRIM_MAP_ADD(tm, ts_after);
2413227e6cfSchs 		kmem_free(ts_before, sizeof (*ts_before));
2423227e6cfSchs 	} else if (merge_before) {
2433227e6cfSchs 		TRIM_MAP_REM(tm, ts_before);
2443227e6cfSchs 		ts_before->ts_end = end;
2453227e6cfSchs 		ts_before->ts_txg = txg;
2463227e6cfSchs 		ts_before->ts_time = time;
2473227e6cfSchs 		TRIM_MAP_ADD(tm, ts_before);
2483227e6cfSchs 	} else if (merge_after) {
2493227e6cfSchs 		TRIM_MAP_REM(tm, ts_after);
2503227e6cfSchs 		ts_after->ts_start = start;
2513227e6cfSchs 		ts_after->ts_txg = txg;
2523227e6cfSchs 		ts_after->ts_time = time;
2533227e6cfSchs 		TRIM_MAP_ADD(tm, ts_after);
2543227e6cfSchs 	} else {
2553227e6cfSchs 		ts = kmem_alloc(sizeof (*ts), KM_SLEEP);
2563227e6cfSchs 		ts->ts_start = start;
2573227e6cfSchs 		ts->ts_end = end;
2583227e6cfSchs 		ts->ts_txg = txg;
2593227e6cfSchs 		ts->ts_time = time;
2603227e6cfSchs 		avl_insert(&tm->tm_queued_frees, ts, where);
2613227e6cfSchs 		TRIM_MAP_ADD(tm, ts);
2623227e6cfSchs 	}
2633227e6cfSchs }
2643227e6cfSchs 
2653227e6cfSchs static void
trim_map_segment_remove(trim_map_t * tm,trim_seg_t * ts,uint64_t start,uint64_t end)2663227e6cfSchs trim_map_segment_remove(trim_map_t *tm, trim_seg_t *ts, uint64_t start,
2673227e6cfSchs     uint64_t end)
2683227e6cfSchs {
2693227e6cfSchs 	trim_seg_t *nts;
2703227e6cfSchs 	boolean_t left_over, right_over;
2713227e6cfSchs 
2723227e6cfSchs 	ASSERT(MUTEX_HELD(&tm->tm_lock));
2733227e6cfSchs 
2743227e6cfSchs 	left_over = (ts->ts_start < start);
2753227e6cfSchs 	right_over = (ts->ts_end > end);
2763227e6cfSchs 
2773227e6cfSchs 	TRIM_MAP_REM(tm, ts);
2783227e6cfSchs 	if (left_over && right_over) {
2793227e6cfSchs 		nts = kmem_alloc(sizeof (*nts), KM_SLEEP);
2803227e6cfSchs 		nts->ts_start = end;
2813227e6cfSchs 		nts->ts_end = ts->ts_end;
2823227e6cfSchs 		nts->ts_txg = ts->ts_txg;
2833227e6cfSchs 		nts->ts_time = ts->ts_time;
2843227e6cfSchs 		ts->ts_end = start;
2853227e6cfSchs 		avl_insert_here(&tm->tm_queued_frees, nts, ts, AVL_AFTER);
2863227e6cfSchs 		TRIM_MAP_ADD(tm, ts);
2873227e6cfSchs 		TRIM_MAP_ADD(tm, nts);
2883227e6cfSchs 	} else if (left_over) {
2893227e6cfSchs 		ts->ts_end = start;
2903227e6cfSchs 		TRIM_MAP_ADD(tm, ts);
2913227e6cfSchs 	} else if (right_over) {
2923227e6cfSchs 		ts->ts_start = end;
2933227e6cfSchs 		TRIM_MAP_ADD(tm, ts);
2943227e6cfSchs 	} else {
2953227e6cfSchs 		avl_remove(&tm->tm_queued_frees, ts);
2963227e6cfSchs 		kmem_free(ts, sizeof (*ts));
2973227e6cfSchs 	}
2983227e6cfSchs }
2993227e6cfSchs 
3003227e6cfSchs static void
trim_map_free_locked(trim_map_t * tm,uint64_t start,uint64_t end,uint64_t txg)3013227e6cfSchs trim_map_free_locked(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
3023227e6cfSchs {
3033227e6cfSchs 	zio_t zsearch, *zs;
3043227e6cfSchs 
3053227e6cfSchs 	ASSERT(MUTEX_HELD(&tm->tm_lock));
3063227e6cfSchs 
3073227e6cfSchs 	zsearch.io_offset = start;
3083227e6cfSchs 	zsearch.io_size = end - start;
3093227e6cfSchs 
3103227e6cfSchs 	zs = avl_find(&tm->tm_inflight_writes, &zsearch, NULL);
3113227e6cfSchs 	if (zs == NULL) {
3123227e6cfSchs 		trim_map_segment_add(tm, start, end, txg);
3133227e6cfSchs 		return;
3143227e6cfSchs 	}
3153227e6cfSchs 	if (start < zs->io_offset)
3163227e6cfSchs 		trim_map_free_locked(tm, start, zs->io_offset, txg);
3173227e6cfSchs 	if (zs->io_offset + zs->io_size < end)
3183227e6cfSchs 		trim_map_free_locked(tm, zs->io_offset + zs->io_size, end, txg);
3193227e6cfSchs }
3203227e6cfSchs 
3213227e6cfSchs void
trim_map_free(vdev_t * vd,uint64_t offset,uint64_t size,uint64_t txg)3223227e6cfSchs trim_map_free(vdev_t *vd, uint64_t offset, uint64_t size, uint64_t txg)
3233227e6cfSchs {
3243227e6cfSchs 	trim_map_t *tm = vd->vdev_trimmap;
3253227e6cfSchs 
3263227e6cfSchs 	if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
3273227e6cfSchs 		return;
3283227e6cfSchs 
3293227e6cfSchs 	mutex_enter(&tm->tm_lock);
3303227e6cfSchs 	trim_map_free_locked(tm, offset, TRIM_ZIO_END(vd, offset, size), txg);
3313227e6cfSchs 	mutex_exit(&tm->tm_lock);
3323227e6cfSchs }
3333227e6cfSchs 
3343227e6cfSchs boolean_t
trim_map_write_start(zio_t * zio)3353227e6cfSchs trim_map_write_start(zio_t *zio)
3363227e6cfSchs {
3373227e6cfSchs 	vdev_t *vd = zio->io_vd;
3383227e6cfSchs 	trim_map_t *tm = vd->vdev_trimmap;
3393227e6cfSchs 	trim_seg_t tsearch, *ts;
3403227e6cfSchs 	boolean_t left_over, right_over;
3413227e6cfSchs 	uint64_t start, end;
3423227e6cfSchs 
3433227e6cfSchs 	if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
3443227e6cfSchs 		return (B_TRUE);
3453227e6cfSchs 
3463227e6cfSchs 	start = zio->io_offset;
3473227e6cfSchs 	end = TRIM_ZIO_END(zio->io_vd, start, zio->io_size);
3483227e6cfSchs 	tsearch.ts_start = start;
3493227e6cfSchs 	tsearch.ts_end = end;
3503227e6cfSchs 
3513227e6cfSchs 	mutex_enter(&tm->tm_lock);
3523227e6cfSchs 
3533227e6cfSchs 	/*
3543227e6cfSchs 	 * Checking for colliding in-flight frees.
3553227e6cfSchs 	 */
3563227e6cfSchs 	ts = avl_find(&tm->tm_inflight_frees, &tsearch, NULL);
3573227e6cfSchs 	if (ts != NULL) {
3583227e6cfSchs 		list_insert_tail(&tm->tm_pending_writes, zio);
3593227e6cfSchs 		mutex_exit(&tm->tm_lock);
3603227e6cfSchs 		return (B_FALSE);
3613227e6cfSchs 	}
3623227e6cfSchs 
3633227e6cfSchs 	ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
3643227e6cfSchs 	if (ts != NULL) {
3653227e6cfSchs 		/*
3663227e6cfSchs 		 * Loop until all overlapping segments are removed.
3673227e6cfSchs 		 */
3683227e6cfSchs 		do {
3693227e6cfSchs 			trim_map_segment_remove(tm, ts, start, end);
3703227e6cfSchs 			ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
3713227e6cfSchs 		} while (ts != NULL);
3723227e6cfSchs 	}
3733227e6cfSchs 	avl_add(&tm->tm_inflight_writes, zio);
3743227e6cfSchs 
3753227e6cfSchs 	mutex_exit(&tm->tm_lock);
3763227e6cfSchs 
3773227e6cfSchs 	return (B_TRUE);
3783227e6cfSchs }
3793227e6cfSchs 
3803227e6cfSchs void
trim_map_write_done(zio_t * zio)3813227e6cfSchs trim_map_write_done(zio_t *zio)
3823227e6cfSchs {
3833227e6cfSchs 	vdev_t *vd = zio->io_vd;
3843227e6cfSchs 	trim_map_t *tm = vd->vdev_trimmap;
3853227e6cfSchs 
3863227e6cfSchs 	/*
3873227e6cfSchs 	 * Don't check for vdev_notrim, since the write could have
3883227e6cfSchs 	 * started before vdev_notrim was set.
3893227e6cfSchs 	 */
3903227e6cfSchs 	if (!zfs_trim_enabled || tm == NULL)
3913227e6cfSchs 		return;
3923227e6cfSchs 
3933227e6cfSchs 	mutex_enter(&tm->tm_lock);
3943227e6cfSchs 	/*
3953227e6cfSchs 	 * Don't fail if the write isn't in the tree, since the write
3963227e6cfSchs 	 * could have started after vdev_notrim was set.
3973227e6cfSchs 	 */
3983227e6cfSchs 	if (zio->io_trim_node.avl_child[0] ||
3993227e6cfSchs 	    zio->io_trim_node.avl_child[1] ||
4003227e6cfSchs 	    AVL_XPARENT(&zio->io_trim_node) ||
4013227e6cfSchs 	    tm->tm_inflight_writes.avl_root == &zio->io_trim_node)
4023227e6cfSchs 		avl_remove(&tm->tm_inflight_writes, zio);
4033227e6cfSchs 	mutex_exit(&tm->tm_lock);
4043227e6cfSchs }
4053227e6cfSchs 
4063227e6cfSchs /*
4073227e6cfSchs  * Return the oldest segment (the one with the lowest txg / time) or NULL if:
4083227e6cfSchs  * 1. The list is empty
4093227e6cfSchs  * 2. The first element's txg is greater than txgsafe
4103227e6cfSchs  * 3. The first element's txg is not greater than the txg argument and the
4113227e6cfSchs  *    the first element's time is not greater than time argument
4123227e6cfSchs  */
4133227e6cfSchs static trim_seg_t *
trim_map_first(trim_map_t * tm,uint64_t txg,uint64_t txgsafe,hrtime_t time,boolean_t force)4143227e6cfSchs trim_map_first(trim_map_t *tm, uint64_t txg, uint64_t txgsafe, hrtime_t time,
4153227e6cfSchs     boolean_t force)
4163227e6cfSchs {
4173227e6cfSchs 	trim_seg_t *ts;
4183227e6cfSchs 
4193227e6cfSchs 	ASSERT(MUTEX_HELD(&tm->tm_lock));
4203227e6cfSchs 	VERIFY(txgsafe >= txg);
4213227e6cfSchs 
4223227e6cfSchs 	ts = list_head(&tm->tm_head);
4233227e6cfSchs 	if (ts != NULL && ts->ts_txg <= txgsafe &&
4243227e6cfSchs 	    (ts->ts_txg <= txg || ts->ts_time <= time || force))
4253227e6cfSchs 		return (ts);
4263227e6cfSchs 	return (NULL);
4273227e6cfSchs }
4283227e6cfSchs 
4293227e6cfSchs static void
trim_map_vdev_commit(spa_t * spa,zio_t * zio,vdev_t * vd)4303227e6cfSchs trim_map_vdev_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
4313227e6cfSchs {
4323227e6cfSchs 	trim_map_t *tm = vd->vdev_trimmap;
4333227e6cfSchs 	trim_seg_t *ts;
4343227e6cfSchs 	uint64_t size, offset, txgtarget, txgsafe;
4353227e6cfSchs 	int64_t hard, soft;
4363227e6cfSchs 	hrtime_t timelimit;
4373227e6cfSchs 
4383227e6cfSchs 	ASSERT(vd->vdev_ops->vdev_op_leaf);
4393227e6cfSchs 
4403227e6cfSchs 	if (tm == NULL)
4413227e6cfSchs 		return;
4423227e6cfSchs 
4433227e6cfSchs 	timelimit = gethrtime() - (hrtime_t)trim_timeout * NANOSEC;
4443227e6cfSchs 	if (vd->vdev_isl2cache) {
4453227e6cfSchs 		txgsafe = UINT64_MAX;
4463227e6cfSchs 		txgtarget = UINT64_MAX;
4473227e6cfSchs 	} else {
4483227e6cfSchs 		txgsafe = MIN(spa_last_synced_txg(spa), spa_freeze_txg(spa));
4493227e6cfSchs 		if (txgsafe > trim_txg_delay)
4503227e6cfSchs 			txgtarget = txgsafe - trim_txg_delay;
4513227e6cfSchs 		else
4523227e6cfSchs 			txgtarget = 0;
4533227e6cfSchs 	}
4543227e6cfSchs 
4553227e6cfSchs 	mutex_enter(&tm->tm_lock);
4563227e6cfSchs 	hard = 0;
4573227e6cfSchs 	if (tm->tm_pending > trim_vdev_max_pending)
4583227e6cfSchs 		hard = (tm->tm_pending - trim_vdev_max_pending) / 4;
4593227e6cfSchs 	soft = P2ROUNDUP(hard + tm->tm_pending / trim_timeout + 1, 64);
4603227e6cfSchs 	/* Loop until we have sent all outstanding free's */
4613227e6cfSchs 	while (soft > 0 &&
4623227e6cfSchs 	    (ts = trim_map_first(tm, txgtarget, txgsafe, timelimit, hard > 0))
4633227e6cfSchs 	    != NULL) {
4643227e6cfSchs 		TRIM_MAP_REM(tm, ts);
4653227e6cfSchs 		avl_remove(&tm->tm_queued_frees, ts);
4663227e6cfSchs 		avl_add(&tm->tm_inflight_frees, ts);
4673227e6cfSchs 		size = ts->ts_end - ts->ts_start;
4683227e6cfSchs 		offset = ts->ts_start;
4693227e6cfSchs 		/*
4703227e6cfSchs 		 * We drop the lock while we call zio_nowait as the IO
4713227e6cfSchs 		 * scheduler can result in a different IO being run e.g.
4723227e6cfSchs 		 * a write which would result in a recursive lock.
4733227e6cfSchs 		 */
4743227e6cfSchs 		mutex_exit(&tm->tm_lock);
4753227e6cfSchs 
4763227e6cfSchs 		zio_nowait(zio_trim(zio, spa, vd, offset, size));
4773227e6cfSchs 
4783227e6cfSchs 		soft -= TRIM_MAP_SEGS(size);
4793227e6cfSchs 		hard -= TRIM_MAP_SEGS(size);
4803227e6cfSchs 		mutex_enter(&tm->tm_lock);
4813227e6cfSchs 	}
4823227e6cfSchs 	mutex_exit(&tm->tm_lock);
4833227e6cfSchs }
4843227e6cfSchs 
4853227e6cfSchs static void
trim_map_vdev_commit_done(spa_t * spa,vdev_t * vd)4863227e6cfSchs trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd)
4873227e6cfSchs {
4883227e6cfSchs 	trim_map_t *tm = vd->vdev_trimmap;
4893227e6cfSchs 	trim_seg_t *ts;
4903227e6cfSchs 	list_t pending_writes;
4913227e6cfSchs 	zio_t *zio;
4923227e6cfSchs 	uint64_t start, size;
4933227e6cfSchs 	void *cookie;
4943227e6cfSchs 
4953227e6cfSchs 	ASSERT(vd->vdev_ops->vdev_op_leaf);
4963227e6cfSchs 
4973227e6cfSchs 	if (tm == NULL)
4983227e6cfSchs 		return;
4993227e6cfSchs 
5003227e6cfSchs 	mutex_enter(&tm->tm_lock);
5013227e6cfSchs 	if (!avl_is_empty(&tm->tm_inflight_frees)) {
5023227e6cfSchs 		cookie = NULL;
5033227e6cfSchs 		while ((ts = avl_destroy_nodes(&tm->tm_inflight_frees,
5043227e6cfSchs 		    &cookie)) != NULL) {
5053227e6cfSchs 			kmem_free(ts, sizeof (*ts));
5063227e6cfSchs 		}
5073227e6cfSchs 	}
5083227e6cfSchs 	list_create(&pending_writes, sizeof (zio_t), offsetof(zio_t,
5093227e6cfSchs 	    io_trim_link));
5103227e6cfSchs 	list_move_tail(&pending_writes, &tm->tm_pending_writes);
5113227e6cfSchs 	mutex_exit(&tm->tm_lock);
5123227e6cfSchs 
5133227e6cfSchs 	while ((zio = list_remove_head(&pending_writes)) != NULL) {
5143227e6cfSchs 		zio_vdev_io_reissue(zio);
5153227e6cfSchs 		zio_execute(zio);
5163227e6cfSchs 	}
5173227e6cfSchs 	list_destroy(&pending_writes);
5183227e6cfSchs }
5193227e6cfSchs 
5203227e6cfSchs static void
trim_map_commit(spa_t * spa,zio_t * zio,vdev_t * vd)5213227e6cfSchs trim_map_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
5223227e6cfSchs {
5233227e6cfSchs 	int c;
5243227e6cfSchs 
5253227e6cfSchs 	if (vd == NULL)
5263227e6cfSchs 		return;
5273227e6cfSchs 
5283227e6cfSchs 	if (vd->vdev_ops->vdev_op_leaf) {
5293227e6cfSchs 		trim_map_vdev_commit(spa, zio, vd);
5303227e6cfSchs 	} else {
5313227e6cfSchs 		for (c = 0; c < vd->vdev_children; c++)
5323227e6cfSchs 			trim_map_commit(spa, zio, vd->vdev_child[c]);
5333227e6cfSchs 	}
5343227e6cfSchs }
5353227e6cfSchs 
5363227e6cfSchs static void
trim_map_commit_done(spa_t * spa,vdev_t * vd)5373227e6cfSchs trim_map_commit_done(spa_t *spa, vdev_t *vd)
5383227e6cfSchs {
5393227e6cfSchs 	int c;
5403227e6cfSchs 
5413227e6cfSchs 	if (vd == NULL)
5423227e6cfSchs 		return;
5433227e6cfSchs 
5443227e6cfSchs 	if (vd->vdev_ops->vdev_op_leaf) {
5453227e6cfSchs 		trim_map_vdev_commit_done(spa, vd);
5463227e6cfSchs 	} else {
5473227e6cfSchs 		for (c = 0; c < vd->vdev_children; c++)
5483227e6cfSchs 			trim_map_commit_done(spa, vd->vdev_child[c]);
5493227e6cfSchs 	}
5503227e6cfSchs }
5513227e6cfSchs 
5523227e6cfSchs static void
trim_thread(void * arg)5533227e6cfSchs trim_thread(void *arg)
5543227e6cfSchs {
5553227e6cfSchs 	spa_t *spa = arg;
5563227e6cfSchs 	zio_t *zio;
5573227e6cfSchs 
558*ba2539a9Schs #ifdef __FreeBSD__
5593227e6cfSchs #ifdef _KERNEL
5603227e6cfSchs 	(void) snprintf(curthread->td_name, sizeof(curthread->td_name),
5613227e6cfSchs 	    "trim %s", spa_name(spa));
5623227e6cfSchs #endif
563*ba2539a9Schs #endif
564*ba2539a9Schs #ifdef __NetBSD__
565*ba2539a9Schs #ifdef _KERNEL
566*ba2539a9Schs 	size_t sz;
567*ba2539a9Schs 	char *name, *oname;
568*ba2539a9Schs 	struct lwp *l = curlwp;
569*ba2539a9Schs 
570*ba2539a9Schs 	name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
571*ba2539a9Schs 	snprintf(name, MAXCOMLEN, "trim %s", spa_name(spa));
572*ba2539a9Schs 	name[MAXCOMLEN - 1] = 0;
573*ba2539a9Schs 
574*ba2539a9Schs 	lwp_lock(l);
575*ba2539a9Schs 	oname = l->l_name;
576*ba2539a9Schs 	l->l_name = name;
577*ba2539a9Schs 	lwp_unlock(l);
578*ba2539a9Schs 
579*ba2539a9Schs 	if (oname != NULL)
580*ba2539a9Schs 		kmem_free(oname, MAXCOMLEN);
581*ba2539a9Schs #endif
582*ba2539a9Schs #endif
5833227e6cfSchs 
5843227e6cfSchs 	for (;;) {
5853227e6cfSchs 		mutex_enter(&spa->spa_trim_lock);
5863227e6cfSchs 		if (spa->spa_trim_thread == NULL) {
5873227e6cfSchs 			spa->spa_trim_thread = curthread;
5883227e6cfSchs 			cv_signal(&spa->spa_trim_cv);
5893227e6cfSchs 			mutex_exit(&spa->spa_trim_lock);
5903227e6cfSchs 			thread_exit();
5913227e6cfSchs 		}
5923227e6cfSchs 
5933227e6cfSchs 		(void) cv_timedwait(&spa->spa_trim_cv, &spa->spa_trim_lock,
5943227e6cfSchs 		    hz * trim_max_interval);
5953227e6cfSchs 		mutex_exit(&spa->spa_trim_lock);
5963227e6cfSchs 
5973227e6cfSchs 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
5983227e6cfSchs 
5993227e6cfSchs 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6003227e6cfSchs 		trim_map_commit(spa, zio, spa->spa_root_vdev);
6013227e6cfSchs 		(void) zio_wait(zio);
6023227e6cfSchs 		trim_map_commit_done(spa, spa->spa_root_vdev);
6033227e6cfSchs 		spa_config_exit(spa, SCL_STATE, FTAG);
6043227e6cfSchs 	}
6053227e6cfSchs }
6063227e6cfSchs 
6073227e6cfSchs void
trim_thread_create(spa_t * spa)6083227e6cfSchs trim_thread_create(spa_t *spa)
6093227e6cfSchs {
6103227e6cfSchs 
6113227e6cfSchs 	if (!zfs_trim_enabled)
6123227e6cfSchs 		return;
6133227e6cfSchs 
6143227e6cfSchs 	mutex_init(&spa->spa_trim_lock, NULL, MUTEX_DEFAULT, NULL);
6153227e6cfSchs 	cv_init(&spa->spa_trim_cv, NULL, CV_DEFAULT, NULL);
6163227e6cfSchs 	mutex_enter(&spa->spa_trim_lock);
6173227e6cfSchs 	spa->spa_trim_thread = thread_create(NULL, 0, trim_thread, spa, 0, &p0,
6183227e6cfSchs 	    TS_RUN, minclsyspri);
6193227e6cfSchs 	mutex_exit(&spa->spa_trim_lock);
6203227e6cfSchs }
6213227e6cfSchs 
6223227e6cfSchs void
trim_thread_destroy(spa_t * spa)6233227e6cfSchs trim_thread_destroy(spa_t *spa)
6243227e6cfSchs {
6253227e6cfSchs 
6263227e6cfSchs 	if (!zfs_trim_enabled)
6273227e6cfSchs 		return;
6283227e6cfSchs 	if (spa->spa_trim_thread == NULL)
6293227e6cfSchs 		return;
6303227e6cfSchs 
6313227e6cfSchs 	mutex_enter(&spa->spa_trim_lock);
6323227e6cfSchs 	/* Setting spa_trim_thread to NULL tells the thread to stop. */
6333227e6cfSchs 	spa->spa_trim_thread = NULL;
6343227e6cfSchs 	cv_signal(&spa->spa_trim_cv);
6353227e6cfSchs 	/* The thread will set it back to != NULL on exit. */
6363227e6cfSchs 	while (spa->spa_trim_thread == NULL)
6373227e6cfSchs 		cv_wait(&spa->spa_trim_cv, &spa->spa_trim_lock);
6383227e6cfSchs 	spa->spa_trim_thread = NULL;
6393227e6cfSchs 	mutex_exit(&spa->spa_trim_lock);
6403227e6cfSchs 
6413227e6cfSchs 	cv_destroy(&spa->spa_trim_cv);
6423227e6cfSchs 	mutex_destroy(&spa->spa_trim_lock);
6433227e6cfSchs }
6443227e6cfSchs 
6453227e6cfSchs void
trim_thread_wakeup(spa_t * spa)6463227e6cfSchs trim_thread_wakeup(spa_t *spa)
6473227e6cfSchs {
6483227e6cfSchs 
6493227e6cfSchs 	if (!zfs_trim_enabled)
6503227e6cfSchs 		return;
6513227e6cfSchs 	if (spa->spa_trim_thread == NULL)
6523227e6cfSchs 		return;
6533227e6cfSchs 
6543227e6cfSchs 	mutex_enter(&spa->spa_trim_lock);
6553227e6cfSchs 	cv_signal(&spa->spa_trim_cv);
6563227e6cfSchs 	mutex_exit(&spa->spa_trim_lock);
6573227e6cfSchs }
658