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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
23 * All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
28 #include <sys/vdev_impl.h>
29 #include <sys/trim_map.h>
33 * Calculate the zio end, upgrading based on ashift which would be
34 * done by zio_vdev_io_start.
36 * This makes free range consolidation much more effective
37 * than it would otherwise be as well as ensuring that entire
38 * blocks are invalidated by writes.
40 #define TRIM_ZIO_END(vd, offset, size) (offset + \
41 P2ROUNDUP(size, 1ULL << vd->vdev_top->vdev_ashift))
43 #define TRIM_MAP_SINC(tm, size) \
44 atomic_add_64(&(tm)->tm_bytes, (size))
46 #define TRIM_MAP_SDEC(tm, size) \
47 atomic_add_64(&(tm)->tm_bytes, -(size))
49 #define TRIM_MAP_QINC(tm) \
50 atomic_inc_64(&(tm)->tm_pending); \
52 #define TRIM_MAP_QDEC(tm) \
53 atomic_dec_64(&(tm)->tm_pending);
55 typedef struct trim_map {
56 list_t tm_head; /* List of segments sorted by txg. */
57 avl_tree_t tm_queued_frees; /* AVL tree of segments waiting for TRIM. */
58 avl_tree_t tm_inflight_frees; /* AVL tree of in-flight TRIMs. */
59 avl_tree_t tm_inflight_writes; /* AVL tree of in-flight writes. */
60 list_t tm_pending_writes; /* Writes blocked on in-flight frees. */
62 uint64_t tm_pending; /* Count of pending TRIMs. */
63 uint64_t tm_bytes; /* Total size in bytes of queued TRIMs. */
66 typedef struct trim_seg {
67 avl_node_t ts_node; /* AVL node. */
68 list_node_t ts_next; /* List element. */
69 uint64_t ts_start; /* Starting offset of this segment. */
70 uint64_t ts_end; /* Ending offset (non-inclusive). */
71 uint64_t ts_txg; /* Segment creation txg. */
72 hrtime_t ts_time; /* Segment creation time. */
75 extern boolean_t zfs_trim_enabled;
77 static u_int trim_txg_delay = 32;
78 static u_int trim_timeout = 30;
79 static u_int trim_max_interval = 1;
80 /* Limit outstanding TRIMs to 2G (max size for a single TRIM request) */
81 static uint64_t trim_vdev_max_bytes = 2147483648;
82 /* Limit outstanding TRIMs to 64 (max ranges for a single TRIM request) */
83 static u_int trim_vdev_max_pending = 64;
85 SYSCTL_DECL(_vfs_zfs);
86 SYSCTL_NODE(_vfs_zfs, OID_AUTO, trim, CTLFLAG_RD, 0, "ZFS TRIM");
88 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, txg_delay, CTLFLAG_RWTUN, &trim_txg_delay,
89 0, "Delay TRIMs by up to this many TXGs");
90 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, timeout, CTLFLAG_RWTUN, &trim_timeout, 0,
91 "Delay TRIMs by up to this many seconds");
92 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, max_interval, CTLFLAG_RWTUN,
93 &trim_max_interval, 0,
94 "Maximum interval between TRIM queue processing (seconds)");
96 SYSCTL_DECL(_vfs_zfs_vdev);
97 SYSCTL_QUAD(_vfs_zfs_vdev, OID_AUTO, trim_max_bytes, CTLFLAG_RWTUN,
98 &trim_vdev_max_bytes, 0,
99 "Maximum pending TRIM bytes for a vdev");
100 SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, trim_max_pending, CTLFLAG_RWTUN,
101 &trim_vdev_max_pending, 0,
102 "Maximum pending TRIM segments for a vdev");
104 static void trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd);
107 trim_map_seg_compare(const void *x1, const void *x2)
109 const trim_seg_t *s1 = x1;
110 const trim_seg_t *s2 = x2;
112 if (s1->ts_start < s2->ts_start) {
113 if (s1->ts_end > s2->ts_start)
117 if (s1->ts_start > s2->ts_start) {
118 if (s1->ts_start < s2->ts_end)
126 trim_map_zio_compare(const void *x1, const void *x2)
128 const zio_t *z1 = x1;
129 const zio_t *z2 = x2;
131 if (z1->io_offset < z2->io_offset) {
132 if (z1->io_offset + z1->io_size > z2->io_offset)
136 if (z1->io_offset > z2->io_offset) {
137 if (z1->io_offset < z2->io_offset + z2->io_size)
145 trim_map_create(vdev_t *vd)
149 ASSERT(vd->vdev_ops->vdev_op_leaf);
151 if (!zfs_trim_enabled)
154 tm = kmem_zalloc(sizeof (*tm), KM_SLEEP);
155 mutex_init(&tm->tm_lock, NULL, MUTEX_DEFAULT, NULL);
156 list_create(&tm->tm_head, sizeof (trim_seg_t),
157 offsetof(trim_seg_t, ts_next));
158 list_create(&tm->tm_pending_writes, sizeof (zio_t),
159 offsetof(zio_t, io_trim_link));
160 avl_create(&tm->tm_queued_frees, trim_map_seg_compare,
161 sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
162 avl_create(&tm->tm_inflight_frees, trim_map_seg_compare,
163 sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
164 avl_create(&tm->tm_inflight_writes, trim_map_zio_compare,
165 sizeof (zio_t), offsetof(zio_t, io_trim_node));
166 vd->vdev_trimmap = tm;
170 trim_map_destroy(vdev_t *vd)
175 ASSERT(vd->vdev_ops->vdev_op_leaf);
177 if (!zfs_trim_enabled)
180 tm = vd->vdev_trimmap;
185 * We may have been called before trim_map_vdev_commit_done()
186 * had a chance to run, so do it now to prune the remaining
189 trim_map_vdev_commit_done(vd->vdev_spa, vd);
191 mutex_enter(&tm->tm_lock);
192 while ((ts = list_head(&tm->tm_head)) != NULL) {
193 avl_remove(&tm->tm_queued_frees, ts);
194 list_remove(&tm->tm_head, ts);
195 kmem_free(ts, sizeof (*ts));
196 TRIM_MAP_SDEC(tm, ts->ts_end - ts->ts_start);
199 mutex_exit(&tm->tm_lock);
201 avl_destroy(&tm->tm_queued_frees);
202 avl_destroy(&tm->tm_inflight_frees);
203 avl_destroy(&tm->tm_inflight_writes);
204 list_destroy(&tm->tm_pending_writes);
205 list_destroy(&tm->tm_head);
206 mutex_destroy(&tm->tm_lock);
207 kmem_free(tm, sizeof (*tm));
208 vd->vdev_trimmap = NULL;
212 trim_map_segment_add(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
215 trim_seg_t tsearch, *ts_before, *ts_after, *ts;
216 boolean_t merge_before, merge_after;
219 ASSERT(MUTEX_HELD(&tm->tm_lock));
223 tsearch.ts_start = start;
224 tsearch.ts_end = end;
226 ts = avl_find(&tm->tm_queued_frees, &tsearch, &where);
228 if (start < ts->ts_start)
229 trim_map_segment_add(tm, start, ts->ts_start, txg);
230 if (end > ts->ts_end)
231 trim_map_segment_add(tm, ts->ts_end, end, txg);
235 ts_before = avl_nearest(&tm->tm_queued_frees, where, AVL_BEFORE);
236 ts_after = avl_nearest(&tm->tm_queued_frees, where, AVL_AFTER);
238 merge_before = (ts_before != NULL && ts_before->ts_end == start);
239 merge_after = (ts_after != NULL && ts_after->ts_start == end);
241 if (merge_before && merge_after) {
242 TRIM_MAP_SINC(tm, ts_after->ts_start - ts_before->ts_end);
244 avl_remove(&tm->tm_queued_frees, ts_before);
245 list_remove(&tm->tm_head, ts_before);
246 ts_after->ts_start = ts_before->ts_start;
247 ts_after->ts_txg = txg;
248 ts_after->ts_time = time;
249 kmem_free(ts_before, sizeof (*ts_before));
250 } else if (merge_before) {
251 TRIM_MAP_SINC(tm, end - ts_before->ts_end);
252 ts_before->ts_end = end;
253 ts_before->ts_txg = txg;
254 ts_before->ts_time = time;
255 } else if (merge_after) {
256 TRIM_MAP_SINC(tm, ts_after->ts_start - start);
257 ts_after->ts_start = start;
258 ts_after->ts_txg = txg;
259 ts_after->ts_time = time;
261 TRIM_MAP_SINC(tm, end - start);
263 ts = kmem_alloc(sizeof (*ts), KM_SLEEP);
264 ts->ts_start = start;
268 avl_insert(&tm->tm_queued_frees, ts, where);
269 list_insert_tail(&tm->tm_head, ts);
274 trim_map_segment_remove(trim_map_t *tm, trim_seg_t *ts, uint64_t start,
278 boolean_t left_over, right_over;
280 ASSERT(MUTEX_HELD(&tm->tm_lock));
282 left_over = (ts->ts_start < start);
283 right_over = (ts->ts_end > end);
285 TRIM_MAP_SDEC(tm, end - start);
286 if (left_over && right_over) {
287 nts = kmem_alloc(sizeof (*nts), KM_SLEEP);
289 nts->ts_end = ts->ts_end;
290 nts->ts_txg = ts->ts_txg;
291 nts->ts_time = ts->ts_time;
293 avl_insert_here(&tm->tm_queued_frees, nts, ts, AVL_AFTER);
294 list_insert_after(&tm->tm_head, ts, nts);
296 } else if (left_over) {
298 } else if (right_over) {
301 avl_remove(&tm->tm_queued_frees, ts);
302 list_remove(&tm->tm_head, ts);
304 kmem_free(ts, sizeof (*ts));
309 trim_map_free_locked(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
313 ASSERT(MUTEX_HELD(&tm->tm_lock));
315 zsearch.io_offset = start;
316 zsearch.io_size = end - start;
318 zs = avl_find(&tm->tm_inflight_writes, &zsearch, NULL);
320 trim_map_segment_add(tm, start, end, txg);
323 if (start < zs->io_offset)
324 trim_map_free_locked(tm, start, zs->io_offset, txg);
325 if (zs->io_offset + zs->io_size < end)
326 trim_map_free_locked(tm, zs->io_offset + zs->io_size, end, txg);
330 trim_map_free(vdev_t *vd, uint64_t offset, uint64_t size, uint64_t txg)
332 trim_map_t *tm = vd->vdev_trimmap;
334 if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
337 mutex_enter(&tm->tm_lock);
338 trim_map_free_locked(tm, offset, TRIM_ZIO_END(vd, offset, size), txg);
339 mutex_exit(&tm->tm_lock);
343 trim_map_write_start(zio_t *zio)
345 vdev_t *vd = zio->io_vd;
346 trim_map_t *tm = vd->vdev_trimmap;
347 trim_seg_t tsearch, *ts;
348 boolean_t left_over, right_over;
351 if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
354 start = zio->io_offset;
355 end = TRIM_ZIO_END(zio->io_vd, start, zio->io_size);
356 tsearch.ts_start = start;
357 tsearch.ts_end = end;
359 mutex_enter(&tm->tm_lock);
362 * Checking for colliding in-flight frees.
364 ts = avl_find(&tm->tm_inflight_frees, &tsearch, NULL);
366 list_insert_tail(&tm->tm_pending_writes, zio);
367 mutex_exit(&tm->tm_lock);
371 ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
374 * Loop until all overlapping segments are removed.
377 trim_map_segment_remove(tm, ts, start, end);
378 ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
379 } while (ts != NULL);
381 avl_add(&tm->tm_inflight_writes, zio);
383 mutex_exit(&tm->tm_lock);
389 trim_map_write_done(zio_t *zio)
391 vdev_t *vd = zio->io_vd;
392 trim_map_t *tm = vd->vdev_trimmap;
395 * Don't check for vdev_notrim, since the write could have
396 * started before vdev_notrim was set.
398 if (!zfs_trim_enabled || tm == NULL)
401 mutex_enter(&tm->tm_lock);
403 * Don't fail if the write isn't in the tree, since the write
404 * could have started after vdev_notrim was set.
406 if (zio->io_trim_node.avl_child[0] ||
407 zio->io_trim_node.avl_child[1] ||
408 AVL_XPARENT(&zio->io_trim_node) ||
409 tm->tm_inflight_writes.avl_root == &zio->io_trim_node)
410 avl_remove(&tm->tm_inflight_writes, zio);
411 mutex_exit(&tm->tm_lock);
415 * Return the oldest segment (the one with the lowest txg / time) or NULL if:
416 * 1. The list is empty
417 * 2. The first element's txg is greater than txgsafe
418 * 3. The first element's txg is not greater than the txg argument and the
419 * the first element's time is not greater than time argument
422 trim_map_first(trim_map_t *tm, uint64_t txg, uint64_t txgsafe, hrtime_t time)
426 ASSERT(MUTEX_HELD(&tm->tm_lock));
427 VERIFY(txgsafe >= txg);
429 ts = list_head(&tm->tm_head);
430 if (ts != NULL && ts->ts_txg <= txgsafe &&
431 (ts->ts_txg <= txg || ts->ts_time <= time ||
432 tm->tm_bytes > trim_vdev_max_bytes ||
433 tm->tm_pending > trim_vdev_max_pending))
439 trim_map_vdev_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
441 trim_map_t *tm = vd->vdev_trimmap;
443 uint64_t size, offset, txgtarget, txgsafe;
446 ASSERT(vd->vdev_ops->vdev_op_leaf);
451 timelimit = gethrtime() - trim_timeout * NANOSEC;
452 if (vd->vdev_isl2cache) {
453 txgsafe = UINT64_MAX;
454 txgtarget = UINT64_MAX;
456 txgsafe = MIN(spa_last_synced_txg(spa), spa_freeze_txg(spa));
457 if (txgsafe > trim_txg_delay)
458 txgtarget = txgsafe - trim_txg_delay;
463 mutex_enter(&tm->tm_lock);
464 /* Loop until we have sent all outstanding free's */
465 while ((ts = trim_map_first(tm, txgtarget, txgsafe, timelimit))
467 list_remove(&tm->tm_head, ts);
468 avl_remove(&tm->tm_queued_frees, ts);
469 avl_add(&tm->tm_inflight_frees, ts);
470 size = ts->ts_end - ts->ts_start;
471 offset = ts->ts_start;
472 TRIM_MAP_SDEC(tm, size);
475 * We drop the lock while we call zio_nowait as the IO
476 * scheduler can result in a different IO being run e.g.
477 * a write which would result in a recursive lock.
479 mutex_exit(&tm->tm_lock);
481 zio_nowait(zio_trim(zio, spa, vd, offset, size));
483 mutex_enter(&tm->tm_lock);
485 mutex_exit(&tm->tm_lock);
489 trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd)
491 trim_map_t *tm = vd->vdev_trimmap;
493 list_t pending_writes;
495 uint64_t start, size;
498 ASSERT(vd->vdev_ops->vdev_op_leaf);
503 mutex_enter(&tm->tm_lock);
504 if (!avl_is_empty(&tm->tm_inflight_frees)) {
506 while ((ts = avl_destroy_nodes(&tm->tm_inflight_frees,
508 kmem_free(ts, sizeof (*ts));
511 list_create(&pending_writes, sizeof (zio_t), offsetof(zio_t,
513 list_move_tail(&pending_writes, &tm->tm_pending_writes);
514 mutex_exit(&tm->tm_lock);
516 while ((zio = list_remove_head(&pending_writes)) != NULL) {
517 zio_vdev_io_reissue(zio);
520 list_destroy(&pending_writes);
524 trim_map_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
531 if (vd->vdev_ops->vdev_op_leaf) {
532 trim_map_vdev_commit(spa, zio, vd);
534 for (c = 0; c < vd->vdev_children; c++)
535 trim_map_commit(spa, zio, vd->vdev_child[c]);
540 trim_map_commit_done(spa_t *spa, vdev_t *vd)
547 if (vd->vdev_ops->vdev_op_leaf) {
548 trim_map_vdev_commit_done(spa, vd);
550 for (c = 0; c < vd->vdev_children; c++)
551 trim_map_commit_done(spa, vd->vdev_child[c]);
556 trim_thread(void *arg)
562 (void) snprintf(curthread->td_name, sizeof(curthread->td_name),
563 "trim %s", spa_name(spa));
567 mutex_enter(&spa->spa_trim_lock);
568 if (spa->spa_trim_thread == NULL) {
569 spa->spa_trim_thread = curthread;
570 cv_signal(&spa->spa_trim_cv);
571 mutex_exit(&spa->spa_trim_lock);
575 (void) cv_timedwait(&spa->spa_trim_cv, &spa->spa_trim_lock,
576 hz * trim_max_interval);
577 mutex_exit(&spa->spa_trim_lock);
579 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
581 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
582 trim_map_commit(spa, zio, spa->spa_root_vdev);
583 (void) zio_wait(zio);
584 trim_map_commit_done(spa, spa->spa_root_vdev);
585 spa_config_exit(spa, SCL_STATE, FTAG);
590 trim_thread_create(spa_t *spa)
593 if (!zfs_trim_enabled)
596 mutex_init(&spa->spa_trim_lock, NULL, MUTEX_DEFAULT, NULL);
597 cv_init(&spa->spa_trim_cv, NULL, CV_DEFAULT, NULL);
598 mutex_enter(&spa->spa_trim_lock);
599 spa->spa_trim_thread = thread_create(NULL, 0, trim_thread, spa, 0, &p0,
600 TS_RUN, minclsyspri);
601 mutex_exit(&spa->spa_trim_lock);
605 trim_thread_destroy(spa_t *spa)
608 if (!zfs_trim_enabled)
610 if (spa->spa_trim_thread == NULL)
613 mutex_enter(&spa->spa_trim_lock);
614 /* Setting spa_trim_thread to NULL tells the thread to stop. */
615 spa->spa_trim_thread = NULL;
616 cv_signal(&spa->spa_trim_cv);
617 /* The thread will set it back to != NULL on exit. */
618 while (spa->spa_trim_thread == NULL)
619 cv_wait(&spa->spa_trim_cv, &spa->spa_trim_lock);
620 spa->spa_trim_thread = NULL;
621 mutex_exit(&spa->spa_trim_lock);
623 cv_destroy(&spa->spa_trim_cv);
624 mutex_destroy(&spa->spa_trim_lock);
628 trim_thread_wakeup(spa_t *spa)
631 if (!zfs_trim_enabled)
633 if (spa->spa_trim_thread == NULL)
636 mutex_enter(&spa->spa_trim_lock);
637 cv_signal(&spa->spa_trim_cv);
638 mutex_exit(&spa->spa_trim_lock);